HarmonyX icon indicating copy to clipboard operation
HarmonyX copied to clipboard

Transpiler patching an Enumerator's MoveNext method "blocks" Postfix patches on Enumerators created inside that method

Open benediktwerner opened this issue 2 years ago • 0 comments

Example code to be patched:

class C {
    IEnumerator FirstEnumerator() {
        yield return SecondEnumerator();
    }

    static IEnumerator SecondEnumerator() {
        yield break;
    }
}

Example plugin:

[BepInPlugin("test", "test", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
    static ManualLogSource L;

    private void Awake()
    {
        L = Logger;
        Harmony.CreateAndPatchAll(typeof(Plugin));
    }

    [HarmonyTranspiler]
    [HarmonyPatch(typeof(C), nameof(C.FirstEnumerator), MethodType.Enumerator)]
    public static IEnumerable<CodeInstruction> Test1(IEnumerable<CodeInstruction> instructions)
    {
        return instructions;
    }

    [HarmonyPostfix]
    [HarmonyPatch(typeof(C), nameof(C.SecondEnumerator))]
    public static void Test2()
    {
        L.LogDebug("postfix");
    }
}

When FirstEnumerator is executed, postfix is never logged. Removing the Test1 patch fixes it. This only seems to happen when the Test1 patch is applied before the Test2 patch and only on Enumerators as far as I can tell.

Version info:

[Message:   BepInEx] BepInEx 5.4.21.0 - Stacklands (04.05.2022 01:33:52)
[Info   :   BepInEx] Running under Unity v2020.3.6.3378102
[Info   :   BepInEx] CLR runtime version: 4.0.30319.17020

benediktwerner avatar Nov 18 '22 07:11 benediktwerner