question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Transpiles fail in Unity 2019.4.27, HarmonyX 2.5.5

See original GitHub issue

The following error occurs when trying to use transpile patches on any method. Prefix and Postfix patches work fine.

Game : Outer Wilds (Steam) v1.1.10 OS : Windows 10 HarmonyX Version : 2.5.5

Exception while patching DebugInputManager.Awake: HarmonyLib.HarmonyException: IL Compile Error (unknown location) ---> HarmonyLib.HarmonyException: IL Compile Error (unknown location) ---> System.BadImageFormatException: Method has zero rva
  at (wrapper managed-to-native) System.RuntimeMethodHandle.GetFunctionPointer(intptr)
  at System.RuntimeMethodHandle.GetFunctionPointer () [0x00000] in <44afb4564e9347cf99a1865351ea8f4a>:0
  at MonoMod.RuntimeDetour.Platforms.DetourRuntimeILPlatform.GetFunctionPointer (System.Reflection.MethodBase method, System.RuntimeMethodHandle handle) [0x00000] in <953a8b1875364c7e892cef5080bf5687>:0
  at MonoMod.RuntimeDetour.Platforms.DetourRuntimeILPlatform.GetNativeStart (System.Reflection.MethodBase method) [0x00019] in <953a8b1875364c7e892cef5080bf5687>:0
  at MonoMod.RuntimeDetour.DetourHelper.GetNativeStart (System.Reflection.MethodBase method) [0x00005] in <953a8b1875364c7e892cef5080bf5687>:0
  at MonoMod.RuntimeDetour.Detour._TopApply () [0x00025] in <953a8b1875364c7e892cef5080bf5687>:0
  at MonoMod.RuntimeDetour.Detour._RefreshChain (System.Reflection.MethodBase method) [0x00151] in <953a8b1875364c7e892cef5080bf5687>:0
  at MonoMod.RuntimeDetour.Detour.Apply () [0x00053] in <953a8b1875364c7e892cef5080bf5687>:0
  at MonoMod.RuntimeDetour.Detour..ctor (System.Reflection.MethodBase from, System.Reflection.MethodBase to, MonoMod.RuntimeDetour.DetourConfig& config) [0x002ec] in <953a8b1875364c7e892cef5080bf5687>:0
  at DMD<Refresh>?-2143303168._MonoMod_RuntimeDetour_ILHook+Context::Refresh (MonoMod.RuntimeDetour.ILHook+Context this) [0x0012d] in <a896941d614b4ac9a9d401eaea161531>:0
  at DMD<>?-2143303168.Trampoline<MonoMod.RuntimeDetour.ILHook+Context::Refresh>?-1076096000 (System.Object ) [0x00020] in <19b6e05bfec045fea901aa4a660b03c1>:0
  at HarmonyLib.Internal.RuntimeFixes.StackTraceFixes.OnILChainRefresh (System.Object self) [0x00000] in <7f26c0a74c5b43c2a5fc5efd29ec63d6>:0
  at MonoMod.RuntimeDetour.ILHook.Apply () [0x00059] in <953a8b1875364c7e892cef5080bf5687>:0
  at HarmonyLib.Public.Patching.ManagedMethodPatcher.DetourTo (System.Reflection.MethodBase replacement) [0x00047] in <7f26c0a74c5b43c2a5fc5efd29ec63d6>:0
   --- End of inner exception stack trace ---
  at HarmonyLib.Public.Patching.ManagedMethodPatcher.DetourTo (System.Reflection.MethodBase replacement) [0x0005f] in <7f26c0a74c5b43c2a5fc5efd29ec63d6>:0
  at HarmonyLib.PatchFunctions.UpdateWrapper (System.Reflection.MethodBase original, HarmonyLib.PatchInfo patchInfo) [0x00033] in <7f26c0a74c5b43c2a5fc5efd29ec63d6>:0
   --- End of inner exception stack trace ---
  at HarmonyLib.PatchFunctions.UpdateWrapper (System.Reflection.MethodBase original, HarmonyLib.PatchInfo patchInfo) [0x0005d] in <7f26c0a74c5b43c2a5fc5efd29ec63d6>:0
  at HarmonyLib.PatchProcessor.Patch () [0x000fb] in <7f26c0a74c5b43c2a5fc5efd29ec63d6>:0
  at HarmonyLib.Harmony.Patch (System.Reflection.MethodBase original, HarmonyLib.HarmonyMethod prefix, HarmonyLib.HarmonyMethod postfix, HarmonyLib.HarmonyMethod transpiler, HarmonyLib.HarmonyMethod finalizer, HarmonyLib.HarmonyMethod ilmanipulator) [0x00031] in <7f26c0a74c5b43c2a5fc5efd29ec63d6>:0

The code we use is as follows : Transpile(methodInfo, typeof(Patches), nameof(Patches.EmptyMethod));

public static IEnumerable<CodeInstruction> EmptyMethod(IEnumerable<CodeInstruction> _) => 
  		new List<CodeInstruction>();

The IL code of the method being patched is this : image

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
misternebulacommented, Oct 6, 2021

Yeah, inserting a single ret fixed this issue. Thanks for the surprisingly easy fix! 😛

0reactions
ghorsingtoncommented, Oct 6, 2021

This could indeed be true. Harmony 2 in itself introduced some changes to how patches are generated. I’ll have a proper look, but I’m fairly sure Harmony 1 always inserted an extra ret to the end of the method, which just happened to not cause issues when you empty a method.

This should be fixable on our side. As such, I’ll keep this issue open and close it once we have a fix for this by automatically adding a ret if a transpiler happened to (brutally) empty a method.

As a side note (for future cases), it’s much more modder-friendly to instead use a prefix with “run original” flag set to false. In other words, it’s enough to have

static bool Prefix()
{
    return false;
}

This will skip the original method code but will still allow other transpilers/code to properly inspect the original IL.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Build completed with a result of 'Failed'
Hi guys, I have a weird problem with Unity. Sometimes my android build fails with this: Error building Player: 2 errors Build completed...
Read more >
Erros to unity 2019 and unity hub
Does the old project have timeline installed? The error messages say that it is missing. Try installing it using package manager. Gregoryl, Apr ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found