zolantris / ValheimMods

Collection of ValheimMods, ValheimRaft (originally created by Sarcen) and a few other upcoming mods including a vehicles mod.
GNU General Public License v3.0
8 stars 4 forks source link

Transpiler tweaks for compatibility #81

Closed searica closed 1 month ago

searica commented 1 month ago

Hey, I got an issue report about a compatibility issue with my mod MoreVanillaBuildPrefabs. So I did a quick bit of testing and found that it was a clash between transpiler patches on Player.PlacePiece.

After going over both our patches I think I've found a way to resolve the compatibility issue (hence this PR). I've commented the changes I made and tried to explain my reasoning for them, which I hope is helpful. It essentially boils down to making a couple tweaks to avoid relying on references existing at specific positions on the evaluation stack so that the transpiler patch won't fail due to another transpiler changing the order somehow.

I haven't compiled your project though so I can't confirm that this does fix things though, but I hope it does or that it at least gives you a place to start. I did also check if there was something I could change on my end but the transpiler patch I've got isn't throwing any errors and is already as minimally invasive as I know how to do. Happy to collaborate if you have other ideas though.

zolantris commented 1 month ago

Doesn't seem to work. The issue is due to the change from void to GameObject in the return type.

Error

InvalidProgramException: Invalid IL code in (wrapper dynamic-method) Player:DMD<Player::PlacePiece> (Player,Piece): IL_0270: ret       

  at (wrapper managed-to-native) System.RuntimeMethodHandle.GetFunctionPointer(intptr)
  at System.RuntimeMethodHandle.GetFunctionPointer () [0x00000] in <17d9ce77f27a4bd2afb5ba32c9bea976>:0 
  at MonoMod.RuntimeDetour.Platforms.DetourRuntimeILPlatform.GetFunctionPointer (System.Reflection.MethodBase method, System.RuntimeMethodHandle handle) [0x00000] in <4e2760c7517c4ea79c633d67e84b319f>:0 
  at MonoMod.RuntimeDetour.Platforms.DetourRuntimeILPlatform.GetNativeStart (System.Reflection.MethodBase method) [0x0004d] in <4e2760c7517c4ea79c633d67e84b319f>:0 
  at MonoMod.RuntimeDetour.DetourHelper.GetNativeStart (System.Reflection.MethodBase method) [0x00005] in <4e2760c7517c4ea79c633d67e84b319f>:0 
  at MonoMod.RuntimeDetour.Detour._TopApply () [0x00025] in <4e2760c7517c4ea79c633d67e84b319f>:0 
  at MonoMod.RuntimeDetour.Detour._RefreshChain (System.Reflection.MethodBase method) [0x00149] in <4e2760c7517c4ea79c633d67e84b319f>:0 
  at MonoMod.RuntimeDetour.Detour.Apply () [0x00053] in <4e2760c7517c4ea79c633d67e84b319f>:0 
  at MonoMod.RuntimeDetour.Detour..ctor (System.Reflection.MethodBase from, System.Reflection.MethodBase to, MonoMod.RuntimeDetour.DetourConfig& config) [0x002e1] in <4e2760c7517c4ea79c633d67e84b319f>:0 
  at (wrapper dynamic-method) MonoMod.RuntimeDetour.ILHook+Context.DMD<MonoMod.RuntimeDetour.ILHook+Context::Refresh>(MonoMod.RuntimeDetour.ILHook/Context)
  at (wrapper dynamic-method) MonoMod.Utils.DynamicMethodDefinition.Trampoline<MonoMod.RuntimeDetour.ILHook+Context::Refresh>?1948338890(object)
  at HarmonyLib.Internal.RuntimeFixes.StackTraceFixes.OnILChainRefresh (System.Object self) [0x00000] in <474744d65d8e460fa08cd5fd82b5d65f>:0 
  at MonoMod.RuntimeDetour.ILHook.Apply () [0x00059] in <4e2760c7517c4ea79c633d67e84b319f>:0 
  at HarmonyLib.Public.Patching.ManagedMethodPatcher.DetourTo (System.Reflection.MethodBase replacement) [0x00047] in <474744d65d8e460fa08cd5fd82b5d65f>:0 
Rethrow as HarmonyException: IL Compile Error (unknown location)
  at HarmonyLib.Public.Patching.ManagedMethodPatcher.DetourTo (System.Reflection.MethodBase replacement) [0x0005f] in <474744d65d8e460fa08cd5fd82b5d65f>:0 
  at HarmonyLib.PatchFunctions.UpdateWrapper (System.Reflection.MethodBase original, HarmonyLib.PatchInfo patchInfo) [0x00033] in <474744d65d8e460fa08cd5fd82b5d65f>:0 
Rethrow as HarmonyException: IL Compile Error (unknown location)
  at HarmonyLib.PatchClassProcessor.ReportException (System.Exception exception, System.Reflection.MethodBase original) [0x00045] in <474744d65d8e460fa08cd5fd82b5d65f>:0 
  at HarmonyLib.PatchClassProcessor.Patch () [0x00095] in <474744d65d8e460fa08cd5fd82b5d65f>:0 
  at HarmonyLib.Harmony.PatchAll (System.Type type) [0x00008] in <474744d65d8e460fa08cd5fd82b5d65f>:0 
  at ValheimRAFT.Patches.PatchController.Apply (System.String harmonyGuid) [0x0004b] in <11790d60c910432ab3b99bb7682707ad>:0 
  at ValheimRAFT.ValheimRaftPlugin.Awake () [0x0000f] in <11790d60c910432ab3b99bb7682707ad>:0 
zolantris commented 1 month ago

Closing this. Got things working in #83. Let's move comments over to there. I'll likely merge and deploy it in ~12 hours from now. Let me know if there's any problems in with the approach in the PR comments.

FYI I ended up porting some of the helpers to do the same approach you had for MoreVanillaPrefabs.