pardeike / Harmony

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

x86 platform makes patch fail #614

Open snamds opened 5 months ago

snamds commented 5 months ago

Describe the bug The patch I wanna do works when built with x64/AnyCPU platform but it doesn't work with x86 platform

To Reproduce

        public static void Patch()
        {
            Harmony harmony = new Harmony("TestPatch");

            foreach (Type type in Assembly.GetAssembly(typeof(TimeZone)).GetTypes()
                            .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(TimeZone))))
            {
                MethodInfo originalMethod1 = type.GetMethod("ToLocalTime");
                var preFixMethodInfo1 = typeof(Program).GetMethod("PrefixTest", BindingFlags.Static | BindingFlags.NonPublic);
                HarmonyMethod prefix1 = new HarmonyMethod(preFixMethodInfo1);
                harmony.Patch(originalMethod1, prefix1, null, null, null);
            }

        }

        static void Main(string[] args)
        {
            Patch();

            DateTime date3 = TimeZone.CurrentTimeZone.ToLocalTime(DateTime.Now);
        }

        static bool PrefixTest(ref DateTime __result, DateTime __0)
        {
            __result = DateTime.UtcNow;

            return false;
        }

The result is quite random. AccessViolationException / System.ExecutionEngineException / random "date3" values returned

Expected behavior TimeZone.CurrentTimeZone.ToLocalTime with the current prefix code should return DateTime.UtcNow

Runtime environment (please complete the following information):

Additional context Add any other context about the problem here.

pardeike commented 1 week ago

This most likely tried to patch unsupported types/methods (generic or external).