Skip to content

Commit

Permalink
Disable UnpatchAll without harmonyID
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Sep 2, 2023
1 parent 0ad2af1 commit 1c04963
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 1 addition & 5 deletions src/Bannerlord.Harmony/Bannerlord.Harmony.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,8 @@
</AssemblyAttribute>
</ItemGroup>


<Target Name="SetVersion" BeforeTargets="PrepareForBuild" DependsOnTargets="RunResolvePackageDependencies">
<!--
<PropertyGroup>
<Version>2.3.0-prerelease.2</Version>
</PropertyGroup>
-->
<PropertyGroup>
<Version>$(HarmonyVersion)</Version>
</PropertyGroup>
Expand Down
12 changes: 10 additions & 2 deletions src/Bannerlord.Harmony/SubModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -46,14 +47,18 @@ 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()
{
base.OnBeforeInitialModuleScreenSetAsRoot();

Harmony.Patch(
AccessTools2.Method("TaleWorlds.MountAndBlade.MBSubModuleBase:OnBeforeInitialModuleScreenSetAsRoot"),
AccessTools2.Method(typeof(MBSubModuleBase), "OnBeforeInitialModuleScreenSetAsRoot"),
postfix: new HarmonyMethod(typeof(SubModule), nameof(OnBeforeInitialModuleScreenSetAsRootPostfix)));
}

Expand All @@ -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();
Expand Down

0 comments on commit 1c04963

Please sign in to comment.