pardeike / Harmony

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

__runOriginal in Postfix always return false #473

Closed BarehSolok closed 2 years ago

BarehSolok commented 2 years ago

Describe the bug __runOriginal in Postfix always return false even if original was not skipped.

Expected behavior __runOriginal in Postfix should return true if original method was not skipped.

Screenshots / Code

[HarmonyPatch(typeof(ZombieManager), "sendZombieBoulder")]
[HarmonyPrefix]
internal static bool sendZombieBoulder(bool __runOriginal, Zombie zombie, Vector3 origin, Vector3 direction)
{
    Console.WriteLine($"[DEBUG] Prefix __runOriginal:{__runOriginal}");
    return true;
}

[HarmonyPatch(typeof(ZombieManager), "sendZombieBoulder")]
[HarmonyPostfix]
internal static void sendZombieBoulderPost(bool __runOriginal, Zombie zombie, Vector3 origin, Vector3 direction)
{
    Console.WriteLine($"[DEBUG] Postfix __runOriginal:{__runOriginal}");
}

image

Runtime environment (please complete the following information):

pardeike commented 2 years ago

I added at test for this in f99da8364dbbf0f0530e98fd070314d6bb81a9e3 and it looks like the code behaves correctly. Not sure why it fails for you.

BarehSolok commented 2 years ago

i can reproduce this with .Net framework 4.8 console app

internal class Program
    {
        public static void Main(string[] args)
        {
            var harmony = new Harmony("test");
            harmony.PatchAll();

            MyClass.Test("asd");
        }

        public static class MyClass
        {
            public static void Test(string s)
            {
                Console.WriteLine("[DEBUG] Test method ran, Postfix __runOriginal should be True");
            }
        }

        [HarmonyPatch(typeof(MyClass), "Test")]
        public static class MyClass2
        {
            public static bool Prefix(bool __runOriginal, string s)
            {
                var pre = __runOriginal;
                Console.WriteLine($"[DEBUG] Prefix __runOriginal:{pre}");
                return true;
            }
            public static void Postfix(bool __runOriginal, string s)
            {
                var post = __runOriginal;
                Console.WriteLine($"[DEBUG] Postfix __runOriginal:{post}");
            }
        }
    }

image

I also take one of your test about this runOriginal injection

internal class Program
    {
        public static void Main(string[] args)
        {
            var harmony = new Harmony("test");
            harmony.PatchAll();

            Class22.Method22();
            Console.WriteLine($"[DEBUG] Prefix __runOriginal:{Class22.bool1}");
            Console.WriteLine($"[DEBUG] Postfix __runOriginal:{Class22.bool4}");
        }

        [HarmonyPatch(typeof(Class22), nameof(Class22.Method22))]
        public class Class22
        {
            public static bool? bool1;
            public static bool? bool4;

            [MethodImpl(MethodImplOptions.NoInlining)]
            public static void Method22()
            {
                try
                {
                    Console.WriteLine("[DEBUG] Method22 ran, Postfix __runOriginal should be True");
                }
                catch
                {
                    throw;
                }
            }

            [HarmonyPrefix]
            public static bool Prefix1(bool __runOriginal)
            {
                bool1 = __runOriginal;
                return true;
            }

            [HarmonyPostfix]
            public static void Postfix(bool __runOriginal)
            {
                bool4 = __runOriginal;
            }
        }
    }

image

Runtime environment (please complete the following information):

i received same result after changing to net framework 4.6, 4.7.2, and net core 3.1, net 5.0

pardeike commented 2 years ago

After investigating a bit more, I found the reason we differ is that I already fixed it in this commit 6006140d929565cb49ee07d482f96ec0d51a7aed which is planed to be released in v2.2.2