Skip to content

Commit

Permalink
Add HarmonyBackend config to Il2Cpp as well
Browse files Browse the repository at this point in the history
  • Loading branch information
ghorsington committed Jan 1, 2022
1 parent cbae410 commit b7b8dd2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 50 deletions.
3 changes: 3 additions & 0 deletions BepInEx.IL2CPP/Preloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using BepInEx.Preloader.Core;
using BepInEx.Preloader.Core.Logging;
using BepInEx.Preloader.Core.Patching;
using BepInEx.Preloader.RuntimeFixes;
using UnhollowerBaseLib;
using UnhollowerBaseLib.Runtime;

Expand Down Expand Up @@ -38,6 +39,8 @@ public static void Run()
{
try
{
HarmonyBackendFix.Initialize();

ConsoleManager.Initialize(false);

PreloaderLog = new PreloaderConsoleListener();
Expand Down
47 changes: 47 additions & 0 deletions BepInEx.Preloader.Core/RuntimeFixes/HarmonyBackendFix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.ComponentModel;
using BepInEx.Configuration;

namespace BepInEx.Preloader.RuntimeFixes;

public static class HarmonyBackendFix
{
private static readonly ConfigEntry<MonoModBackend> ConfigHarmonyBackend = ConfigFile.CoreConfig.Bind(
"Preloader",
"HarmonyBackend",
MonoModBackend.auto,
"Specifies which MonoMod backend to use for Harmony patches. Auto uses the best available backend.\nThis setting should only be used for development purposes (e.g. debugging in dnSpy). Other code might override this setting.");

public static void Initialize()
{
switch (ConfigHarmonyBackend.Value)
{
case MonoModBackend.auto:
break;
case MonoModBackend.dynamicmethod:
case MonoModBackend.methodbuilder:
case MonoModBackend.cecil:
Environment.SetEnvironmentVariable("MONOMOD_DMD_TYPE", ConfigHarmonyBackend.Value.ToString());
break;
default:
throw new ArgumentOutOfRangeException(nameof(ConfigHarmonyBackend), ConfigHarmonyBackend.Value,
"Unknown backend");
}
}

private enum MonoModBackend
{
// Enum names are important!
[Description("Auto")]
auto = 0,

[Description("DynamicMethod")]
dynamicmethod,

[Description("MethodBuilder")]
methodbuilder,

[Description("Cecil")]
cecil
}
}
60 changes: 10 additions & 50 deletions BepInEx.Preloader.Unity/UnityPreloader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
Expand All @@ -19,6 +18,15 @@ namespace BepInEx.Preloader.Unity;
/// </summary>
internal static class UnityPreloader
{
#region Config

internal static readonly ConfigEntry<bool> ConfigApplyRuntimePatches = ConfigFile.CoreConfig.Bind(
"Preloader", "ApplyRuntimePatches",
true,
"Enables or disables runtime patches.\nThis should always be true, unless you cannot start the game due to a Harmony related issue (such as running .NET Standard runtime) or you know what you're doing.");

#endregion

/// <summary>
/// The log writer that is specific to the preloader.
/// </summary>
Expand All @@ -33,7 +41,7 @@ public static void Run()
{
try
{
InitializeHarmony();
HarmonyBackendFix.Initialize();

ConsoleManager.Initialize(false);
AllocateConsole();
Expand Down Expand Up @@ -151,52 +159,4 @@ public static string GetUnityVersion()

return $"Unknown ({(IsPostUnity2017 ? "post" : "pre")}-2017)";
}

private static void InitializeHarmony()
{
switch (ConfigHarmonyBackend.Value)
{
case MonoModBackend.auto:
break;
case MonoModBackend.dynamicmethod:
case MonoModBackend.methodbuilder:
case MonoModBackend.cecil:
Environment.SetEnvironmentVariable("MONOMOD_DMD_TYPE", ConfigHarmonyBackend.Value.ToString());
break;
default:
throw new ArgumentOutOfRangeException(nameof(ConfigHarmonyBackend), ConfigHarmonyBackend.Value,
"Unknown backend");
}
}

private enum MonoModBackend
{
// Enum names are important!
[Description("Auto")]
auto = 0,

[Description("DynamicMethod")]
dynamicmethod,

[Description("MethodBuilder")]
methodbuilder,

[Description("Cecil")]
cecil
}

#region Config

internal static readonly ConfigEntry<bool> ConfigApplyRuntimePatches = ConfigFile.CoreConfig.Bind(
"Preloader", "ApplyRuntimePatches",
true,
"Enables or disables runtime patches.\nThis should always be true, unless you cannot start the game due to a Harmony related issue (such as running .NET Standard runtime) or you know what you're doing.");

private static readonly ConfigEntry<MonoModBackend> ConfigHarmonyBackend = ConfigFile.CoreConfig.Bind(
"Preloader",
"HarmonyBackend",
MonoModBackend.auto,
"Specifies which MonoMod backend to use for Harmony patches. Auto uses the best available backend.\nThis setting should only be used for development purposes (e.g. debugging in dnSpy). Other code might override this setting.");

#endregion
}

0 comments on commit b7b8dd2

Please sign in to comment.