From 1c049630b1060f7b166200160c8aa69a713b3a1a Mon Sep 17 00:00:00 2001 From: Vitalii Mikhailov Date: Sat, 2 Sep 2023 15:53:45 +0300 Subject: [PATCH] Disable `UnpatchAll` without harmonyID --- src/Bannerlord.Harmony/Bannerlord.Harmony.csproj | 6 +----- src/Bannerlord.Harmony/SubModule.cs | 12 ++++++++++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Bannerlord.Harmony/Bannerlord.Harmony.csproj b/src/Bannerlord.Harmony/Bannerlord.Harmony.csproj index cfd6623..3fa5a88 100644 --- a/src/Bannerlord.Harmony/Bannerlord.Harmony.csproj +++ b/src/Bannerlord.Harmony/Bannerlord.Harmony.csproj @@ -56,12 +56,8 @@ + - $(HarmonyVersion) diff --git a/src/Bannerlord.Harmony/SubModule.cs b/src/Bannerlord.Harmony/SubModule.cs index 51e3ca2..b1ec6c1 100644 --- a/src/Bannerlord.Harmony/SubModule.cs +++ b/src/Bannerlord.Harmony/SubModule.cs @@ -36,6 +36,7 @@ public class SubModule : MBSubModuleBase private const string SWarningExpectIssues = @"{=xTeLdSrXk4}{NL}This is not recommended. Expect issues!{NL}If your game crashes and you had this warning, please, mention it in the bug report!"; private static readonly HarmonyRef Harmony = new("Bannerlord.Harmony.GauntletUISubModule"); + private static readonly HarmonyRef Harmony2 = new("Bannerlord.Harmony.UnpatchAll"); private readonly DebugUI _debugUI = new(); @@ -46,6 +47,10 @@ protected override void OnSubModuleLoad() base.OnSubModuleLoad(); ValidateHarmony(); + + Harmony2.Patch( + SymbolExtensions2.GetMethodInfo((HarmonyLib.Harmony x) => x.UnpatchAll(null)), + prefix: new HarmonyMethod(typeof(SubModule), nameof(UnpatchAllPrefix))); } protected override void OnBeforeInitialModuleScreenSetAsRoot() @@ -53,7 +58,7 @@ protected override void OnBeforeInitialModuleScreenSetAsRoot() base.OnBeforeInitialModuleScreenSetAsRoot(); Harmony.Patch( - AccessTools2.Method("TaleWorlds.MountAndBlade.MBSubModuleBase:OnBeforeInitialModuleScreenSetAsRoot"), + AccessTools2.Method(typeof(MBSubModuleBase), "OnBeforeInitialModuleScreenSetAsRoot"), postfix: new HarmonyMethod(typeof(SubModule), nameof(OnBeforeInitialModuleScreenSetAsRootPostfix))); } @@ -80,12 +85,15 @@ private static void OnBeforeInitialModuleScreenSetAsRootPostfix(MBSubModuleBase // will be able to initialize the chat system we use to log info. ValidateLoadOrder(); Harmony.Unpatch( - AccessTools2.Method("TaleWorlds.MountAndBlade.MBSubModuleBase:OnBeforeInitialModuleScreenSetAsRoot"), + AccessTools2.Method(typeof(MBSubModuleBase), "OnBeforeInitialModuleScreenSetAsRoot"), HarmonyPatchType.All, Harmony.Id); } } + [MethodImpl(MethodImplOptions.NoInlining)] + private static bool UnpatchAllPrefix(string? harmonyID) => harmonyID is not null; + private static void ValidateLoadOrder() { var loadedModules = ModuleInfoHelper.GetLoadedModules().ToList();