BepInEx / HarmonyX

Harmony built on top of MonoMod.RuntimeDetours with additional features
MIT License
329 stars 42 forks source link

Get `Invalid IL code` when access `__instance` of inner class #74

Open cruiseliu opened 1 year ago

cruiseliu commented 1 year ago

The following patch does not work:

    [HarmonyPatch]
    static class Patch {
        public static MethodBase TargetMethod() {
            Type innerClass = AccessTools.Inner(typeof(OuterClass), "InnerClass");
            return AccessTools.Method(innerClass, "Method");
        }

        public static void Prefix(object __instance) {
            context.Logger.LogInfo("Hello");
        }
    }

Exception:

[Error  : Unity Log] InvalidProgramException: Invalid IL code in (wrapper dynamic-method) Pathea.GeneratorNs.GeneratorModule/GroupData:DMD<Pathea.GeneratorNs.GeneratorModule+GroupData::GetRandomIdList> (Pathea.GeneratorNs.GeneratorModule/GroupData&,System.Collections.Generic.List`1<int>,single): IL_00e3: call      0x00000029

Stack trace:
System.RuntimeMethodHandle.GetFunctionPointer () (at <c9d3ffd4b98649ee9989e1908eaca019>:0)
MonoMod.RuntimeDetour.Platforms.DetourRuntimeILPlatform.GetFunctionPointer (System.Reflection.MethodBase method, System.RuntimeMethodHandle handle) (at <4e2760c7517c4ea79c633d67e84b319f>:0)
MonoMod.RuntimeDetour.Platforms.DetourRuntimeILPlatform.GetNativeStart (System.Reflection.MethodBase method) (at <4e2760c7517c4ea79c633d67e84b319f>:0)
MonoMod.RuntimeDetour.DetourHelper.GetNativeStart (System.Reflection.MethodBase method) (at <4e2760c7517c4ea79c633d67e84b319f>:0)
MonoMod.RuntimeDetour.Detour._TopApply () (at <4e2760c7517c4ea79c633d67e84b319f>:0)
MonoMod.RuntimeDetour.Detour._RefreshChain (System.Reflection.MethodBase method) (at <4e2760c7517c4ea79c633d67e84b319f>:0)
MonoMod.RuntimeDetour.Detour.Apply () (at <4e2760c7517c4ea79c633d67e84b319f>:0)
MonoMod.RuntimeDetour.Detour..ctor (System.Reflection.MethodBase from, System.Reflection.MethodBase to, MonoMod.RuntimeDetour.DetourConfig& config) (at <4e2760c7517c4ea79c633d67e84b319f>:0)
(wrapper dynamic-method) MonoMod.RuntimeDetour.ILHook+Context.DMD<MonoMod.RuntimeDetour.ILHook+Context::Refresh>(MonoMod.RuntimeDetour.ILHook/Context)
(wrapper dynamic-method) MonoMod.Utils.DynamicMethodDefinition.Trampoline<MonoMod.RuntimeDetour.ILHook+Context::Refresh>?-1766901906(object)
HarmonyLib.Internal.RuntimeFixes.StackTraceFixes.OnILChainRefresh (System.Object self) (at <474744d65d8e460fa08cd5fd82b5d65f>:0)
MonoMod.RuntimeDetour.ILHook.Apply () (at <4e2760c7517c4ea79c633d67e84b319f>:0)
HarmonyLib.Public.Patching.ManagedMethodPatcher.DetourTo (System.Reflection.MethodBase replacement) (at <474744d65d8e460fa08cd5fd82b5d65f>:0)
Rethrow as HarmonyException: IL Compile Error (unknown location)
HarmonyLib.Public.Patching.ManagedMethodPatcher.DetourTo (System.Reflection.MethodBase replacement) (at <474744d65d8e460fa08cd5fd82b5d65f>:0)
HarmonyLib.PatchFunctions.UpdateWrapper (System.Reflection.MethodBase original, HarmonyLib.PatchInfo patchInfo) (at <474744d65d8e460fa08cd5fd82b5d65f>:0)
Rethrow as HarmonyException: IL Compile Error (unknown location)
HarmonyLib.PatchClassProcessor.ReportException (System.Exception exception, System.Reflection.MethodBase original) (at <474744d65d8e460fa08cd5fd82b5d65f>:0)
HarmonyLib.PatchClassProcessor.Patch () (at <474744d65d8e460fa08cd5fd82b5d65f>:0)
HarmonyLib.Harmony.<PatchAll>b__11_0 (System.Type type) (at <474744d65d8e460fa08cd5fd82b5d65f>:0)
HarmonyLib.CollectionExtensions.Do[T] (System.Collections.Generic.IEnumerable`1[T] sequence, System.Action`1[T] action) (at <474744d65d8e460fa08cd5fd82b5d65f>:0)
HarmonyLib.Harmony.PatchAll (System.Reflection.Assembly assembly) (at <474744d65d8e460fa08cd5fd82b5d65f>:0)
HarmonyLib.Harmony.CreateAndPatchAll (System.Reflection.Assembly assembly, System.String harmonyInstanceId) (at <474744d65d8e460fa08cd5fd82b5d65f>:0)
LzHello.Plugin.Awake () (at <33195a17d6b049dcb94e207219f3dec4>:0)
UnityEngine.GameObject:AddComponent(Type)
BepInEx.Bootstrap.Chainloader:Start()
UnityEngine.InputSystem.InputSystem:.cctor()

I'm using BepInEx 5.4.21. The inner "class" I'm trying to patch is a struct, FYI.

mathlover3 commented 4 months ago

i have the same problum. how to fix?

ManlyMarco commented 4 months ago

It might already be fixed in latest HarmonyX release.

ReallyBadDeveloper commented 3 months ago

doesn't look like it... still getting the same error