diff --git a/IPA.Injector/Injector.cs b/IPA.Injector/Injector.cs index c4fb1538..52404fd1 100644 --- a/IPA.Injector/Injector.cs +++ b/IPA.Injector/Injector.cs @@ -96,6 +96,8 @@ namespace IPA.Injector GameVersionEarly.Load(); + HarmonyProtector.Protect(); + pluginAsyncLoadTask = PluginLoader.LoadTask(); permissionFixTask = PermissionFix.FixPermissions(new DirectoryInfo(Environment.CurrentDirectory)); } diff --git a/IPA.Loader/IPA.Loader.csproj b/IPA.Loader/IPA.Loader.csproj index 795d5bb4..c85d8dba 100644 --- a/IPA.Loader/IPA.Loader.csproj +++ b/IPA.Loader/IPA.Loader.csproj @@ -98,6 +98,7 @@ + diff --git a/IPA.Loader/Loader/HarmonyProtector.cs b/IPA.Loader/Loader/HarmonyProtector.cs new file mode 100644 index 00000000..3df4b20a --- /dev/null +++ b/IPA.Loader/Loader/HarmonyProtector.cs @@ -0,0 +1,44 @@ +using Harmony; +using System.Collections.Generic; +using System.Reflection; + +namespace IPA.Loader +{ + internal static class HarmonyProtector + { + private static HarmonyInstance instance; + private static Assembly selfAssem; + private static Assembly harmonyAssem; + + public static void Protect(HarmonyInstance inst = null) + { + selfAssem = Assembly.GetExecutingAssembly(); + harmonyAssem = typeof(HarmonyInstance).Assembly; + + if (inst == null) + { + if (instance == null) + instance = HarmonyInstance.Create("BSIPA Safeguard"); + + inst = instance; + } + + var target = typeof(PatchProcessor).GetMethod("Patch"); + var patch = typeof(HarmonyProtector).GetMethod(nameof(PatchProcessor_Patch_Prefix)); + + inst.Patch(target, new HarmonyMethod(patch)); + } + + private static void PatchProcessor_Patch_Prefix(PatchProcessor __instance, ref List ___originals) + { + for (int i = 0; i < ___originals.Count; i++) + { + var mi = ___originals[i]; + var asm = mi.DeclaringType.Assembly; + + if (asm.Equals(selfAssem) || asm.Equals(harmonyAssem)) + ___originals.RemoveAt(i--); + } + } + } +} diff --git a/IPA.Loader/Logging/StdoutInterceptor.cs b/IPA.Loader/Logging/StdoutInterceptor.cs index 495815b8..38136751 100644 --- a/IPA.Loader/Logging/StdoutInterceptor.cs +++ b/IPA.Loader/Logging/StdoutInterceptor.cs @@ -125,11 +125,11 @@ namespace IPA.Logging var getFg = foregroundProperty?.GetGetMethod(); if (resetColor != null) - harmony.Patch(resetColor, transpiler: new HarmonyMethod(typeof(ConsoleHarmonyPatches), "PatchResetColor")); + harmony.Patch(resetColor, transpiler: new HarmonyMethod(typeof(ConsoleHarmonyPatches), nameof(PatchResetColor))); if (foregroundProperty != null) { - harmony.Patch(setFg, transpiler: new HarmonyMethod(typeof(ConsoleHarmonyPatches), "PatchSetForegroundColor")); - harmony.Patch(getFg, transpiler: new HarmonyMethod(typeof(ConsoleHarmonyPatches), "PatchGetForegroundColor")); + harmony.Patch(setFg, transpiler: new HarmonyMethod(typeof(ConsoleHarmonyPatches), nameof(PatchSetForegroundColor))); + harmony.Patch(getFg, transpiler: new HarmonyMethod(typeof(ConsoleHarmonyPatches), nameof(PatchGetForegroundColor))); } }