From b7b8dd2d8410245b44c3a5e7ca974b75b4579c94 Mon Sep 17 00:00:00 2001 From: ghorsington Date: Sat, 1 Jan 2022 13:37:48 +0200 Subject: [PATCH] Add HarmonyBackend config to Il2Cpp as well --- BepInEx.IL2CPP/Preloader.cs | 3 + .../RuntimeFixes/HarmonyBackendFix.cs | 47 +++++++++++++++ BepInEx.Preloader.Unity/UnityPreloader.cs | 60 ++++--------------- 3 files changed, 60 insertions(+), 50 deletions(-) create mode 100644 BepInEx.Preloader.Core/RuntimeFixes/HarmonyBackendFix.cs diff --git a/BepInEx.IL2CPP/Preloader.cs b/BepInEx.IL2CPP/Preloader.cs index 8635668..d907716 100644 --- a/BepInEx.IL2CPP/Preloader.cs +++ b/BepInEx.IL2CPP/Preloader.cs @@ -6,6 +6,7 @@ using BepInEx.Logging; using BepInEx.Preloader.Core; using BepInEx.Preloader.Core.Logging; using BepInEx.Preloader.Core.Patching; +using BepInEx.Preloader.RuntimeFixes; using UnhollowerBaseLib; using UnhollowerBaseLib.Runtime; @@ -38,6 +39,8 @@ public static class Preloader { try { + HarmonyBackendFix.Initialize(); + ConsoleManager.Initialize(false); PreloaderLog = new PreloaderConsoleListener(); diff --git a/BepInEx.Preloader.Core/RuntimeFixes/HarmonyBackendFix.cs b/BepInEx.Preloader.Core/RuntimeFixes/HarmonyBackendFix.cs new file mode 100644 index 0000000..615bc40 --- /dev/null +++ b/BepInEx.Preloader.Core/RuntimeFixes/HarmonyBackendFix.cs @@ -0,0 +1,47 @@ +using System; +using System.ComponentModel; +using BepInEx.Configuration; + +namespace BepInEx.Preloader.RuntimeFixes; + +public static class HarmonyBackendFix +{ + private static readonly ConfigEntry 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 + } +} diff --git a/BepInEx.Preloader.Unity/UnityPreloader.cs b/BepInEx.Preloader.Unity/UnityPreloader.cs index 99d0f4a..63470ad 100644 --- a/BepInEx.Preloader.Unity/UnityPreloader.cs +++ b/BepInEx.Preloader.Unity/UnityPreloader.cs @@ -1,5 +1,4 @@ using System; -using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Linq; @@ -19,6 +18,15 @@ namespace BepInEx.Preloader.Unity; /// internal static class UnityPreloader { + #region Config + + internal static readonly ConfigEntry 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 + /// /// The log writer that is specific to the preloader. /// @@ -33,7 +41,7 @@ internal static class UnityPreloader { try { - InitializeHarmony(); + HarmonyBackendFix.Initialize(); ConsoleManager.Initialize(false); AllocateConsole(); @@ -151,52 +159,4 @@ internal static class UnityPreloader 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 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 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 }