Skip to content

Commit b7b8dd2

Browse files
committed
Add HarmonyBackend config to Il2Cpp as well
1 parent cbae410 commit b7b8dd2

File tree

3 files changed

+60
-50
lines changed

3 files changed

+60
-50
lines changed

BepInEx.IL2CPP/Preloader.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using BepInEx.Preloader.Core;
77
using BepInEx.Preloader.Core.Logging;
88
using BepInEx.Preloader.Core.Patching;
9+
using BepInEx.Preloader.RuntimeFixes;
910
using UnhollowerBaseLib;
1011
using UnhollowerBaseLib.Runtime;
1112

@@ -38,6 +39,8 @@ public static void Run()
3839
{
3940
try
4041
{
42+
HarmonyBackendFix.Initialize();
43+
4144
ConsoleManager.Initialize(false);
4245

4346
PreloaderLog = new PreloaderConsoleListener();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.ComponentModel;
3+
using BepInEx.Configuration;
4+
5+
namespace BepInEx.Preloader.RuntimeFixes;
6+
7+
public static class HarmonyBackendFix
8+
{
9+
private static readonly ConfigEntry<MonoModBackend> ConfigHarmonyBackend = ConfigFile.CoreConfig.Bind(
10+
"Preloader",
11+
"HarmonyBackend",
12+
MonoModBackend.auto,
13+
"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.");
14+
15+
public static void Initialize()
16+
{
17+
switch (ConfigHarmonyBackend.Value)
18+
{
19+
case MonoModBackend.auto:
20+
break;
21+
case MonoModBackend.dynamicmethod:
22+
case MonoModBackend.methodbuilder:
23+
case MonoModBackend.cecil:
24+
Environment.SetEnvironmentVariable("MONOMOD_DMD_TYPE", ConfigHarmonyBackend.Value.ToString());
25+
break;
26+
default:
27+
throw new ArgumentOutOfRangeException(nameof(ConfigHarmonyBackend), ConfigHarmonyBackend.Value,
28+
"Unknown backend");
29+
}
30+
}
31+
32+
private enum MonoModBackend
33+
{
34+
// Enum names are important!
35+
[Description("Auto")]
36+
auto = 0,
37+
38+
[Description("DynamicMethod")]
39+
dynamicmethod,
40+
41+
[Description("MethodBuilder")]
42+
methodbuilder,
43+
44+
[Description("Cecil")]
45+
cecil
46+
}
47+
}

BepInEx.Preloader.Unity/UnityPreloader.cs

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.ComponentModel;
32
using System.Diagnostics;
43
using System.IO;
54
using System.Linq;
@@ -19,6 +18,15 @@ namespace BepInEx.Preloader.Unity;
1918
/// </summary>
2019
internal static class UnityPreloader
2120
{
21+
#region Config
22+
23+
internal static readonly ConfigEntry<bool> ConfigApplyRuntimePatches = ConfigFile.CoreConfig.Bind(
24+
"Preloader", "ApplyRuntimePatches",
25+
true,
26+
"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.");
27+
28+
#endregion
29+
2230
/// <summary>
2331
/// The log writer that is specific to the preloader.
2432
/// </summary>
@@ -33,7 +41,7 @@ public static void Run()
3341
{
3442
try
3543
{
36-
InitializeHarmony();
44+
HarmonyBackendFix.Initialize();
3745

3846
ConsoleManager.Initialize(false);
3947
AllocateConsole();
@@ -151,52 +159,4 @@ public static string GetUnityVersion()
151159

152160
return $"Unknown ({(IsPostUnity2017 ? "post" : "pre")}-2017)";
153161
}
154-
155-
private static void InitializeHarmony()
156-
{
157-
switch (ConfigHarmonyBackend.Value)
158-
{
159-
case MonoModBackend.auto:
160-
break;
161-
case MonoModBackend.dynamicmethod:
162-
case MonoModBackend.methodbuilder:
163-
case MonoModBackend.cecil:
164-
Environment.SetEnvironmentVariable("MONOMOD_DMD_TYPE", ConfigHarmonyBackend.Value.ToString());
165-
break;
166-
default:
167-
throw new ArgumentOutOfRangeException(nameof(ConfigHarmonyBackend), ConfigHarmonyBackend.Value,
168-
"Unknown backend");
169-
}
170-
}
171-
172-
private enum MonoModBackend
173-
{
174-
// Enum names are important!
175-
[Description("Auto")]
176-
auto = 0,
177-
178-
[Description("DynamicMethod")]
179-
dynamicmethod,
180-
181-
[Description("MethodBuilder")]
182-
methodbuilder,
183-
184-
[Description("Cecil")]
185-
cecil
186-
}
187-
188-
#region Config
189-
190-
internal static readonly ConfigEntry<bool> ConfigApplyRuntimePatches = ConfigFile.CoreConfig.Bind(
191-
"Preloader", "ApplyRuntimePatches",
192-
true,
193-
"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.");
194-
195-
private static readonly ConfigEntry<MonoModBackend> ConfigHarmonyBackend = ConfigFile.CoreConfig.Bind(
196-
"Preloader",
197-
"HarmonyBackend",
198-
MonoModBackend.auto,
199-
"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.");
200-
201-
#endregion
202162
}

0 commit comments

Comments
 (0)