pardeike / Harmony

A library for patching, replacing and decorating .NET and Mono methods during runtime
https://www.patreon.com/pardeike
MIT License
5.34k stars 499 forks source link

UnpatchAll throws errors when Harmony 1.2 and Harmony 2.0 are both present. #169

Closed boformer closed 5 years ago

boformer commented 5 years ago

Describe the bug

I have 2 Cities: Skylines mods:

Both mods are really simple, using PatchAll and UnpatchAll when they are enabled/disabled.

When the game loads and all mods are disabled, I can enable and disable the 1.2 mod without any Errors.

But as soon as I enable 2.0 mod (that's probably the moment when harmony patches ists older sibling), I can no longer disable the 1.2 mod without errors.

This happens to all Harmony 1.2 mods I've tested (3 different mods).

The mod that was compiled with Harmony 2.0 can be enabled and disabled at any time.

To Reproduce

Sample Code of a mod: https://gist.github.com/boformer/d7e7bf2af33f4515352f17fcc19443d0

Harmony log (1.2-only and mixed 2.0 and 1.2, where 2.0 was enabled first): https://gist.github.com/boformer/e15c2bb9a47382251c76983fd48984d0

The error in UnpatchAll:

ArgumentNullException: Argument cannot be null.
Parameter name: method
  at Harmony.HarmonyMethod..ctor (System.Reflection.MethodInfo method) [0x00000] in <filename unknown>:0 
  at Harmony.PatchProcessor..ctor (Harmony.HarmonyInstance instance, System.Collections.Generic.List`1 originals, Harmony.HarmonyMethod prefix, Harmony.HarmonyMethod postfix, Harmony.HarmonyMethod transpiler) [0x00000] in <filename unknown>:0 
  at Harmony.HarmonyInstance.Unpatch (System.Reflection.MethodBase original, System.Reflection.MethodInfo patch) [0x00000] in <filename unknown>:0 
  at Harmony.HarmonyInstance+<>c__DisplayClass11_1.<UnpatchAll>b__1 (Harmony.Patch patchInfo) [0x00000] in <filename unknown>:0 
  at Harmony.CollectionExtensions.Do[Patch] (IEnumerable`1 sequence, System.Action`1 action) [0x00000] in <filename unknown>:0 
  at Harmony.CollectionExtensions.DoIf[Patch] (IEnumerable`1 sequence, System.Func`2 condition, System.Action`1 action) [0x00000] in <filename unknown>:0 
  at Harmony.HarmonyInstance.UnpatchAll (System.String harmonyID) [0x00000] in <filename unknown>:0 
  at BehindTheSkyline.Mod.OnDisabled () [0x00000] in <filename unknown>:0 

Runtime environment (please complete the following information):

pardeike commented 5 years ago

Can you try to use

HarmonyInstance.SELF_PATCHING = false

before you run any 2.0 code and see if the issue is self-patching related?

boformer commented 5 years ago

This is working fine now!