diff --git a/Harmony/Harmony.csproj b/Harmony/Harmony.csproj index e431bc02..ef517db9 100644 --- a/Harmony/Harmony.csproj +++ b/Harmony/Harmony.csproj @@ -11,13 +11,13 @@ Andreas Pardeike 0Harmony true - 2.0.0.8 + 2.0.0.9 LICENSE https://github.com/pardeike/Harmony false Harmony,Mono,Patch,Patching,Runtime,Detour,Detours,Aspect,Aspects - 2.0.0.8 - 2.0.0.8 + 2.0.0.9 + 2.0.0.9 HarmonyLogo.png https://raw.githubusercontent.com/pardeike/Harmony/master/HarmonyLogo.png true @@ -85,7 +85,7 @@ - + All @@ -96,7 +96,7 @@ $(MSBuildThisFileDirectory)bin\$(Configuration) - + diff --git a/Harmony/Internal/AccessCache.cs b/Harmony/Internal/AccessCache.cs index e9429345..a1a42bf7 100644 --- a/Harmony/Internal/AccessCache.cs +++ b/Harmony/Internal/AccessCache.cs @@ -83,7 +83,7 @@ internal PropertyInfo GetPropertyInfo(Type type, string name, MemberType memberT internal MethodBase GetMethodInfo(Type type, string name, Type[] arguments, MemberType memberType = MemberType.Any, bool declaredOnly = false) { - var value = Get(declaredMethods, type, name, arguments, () => type.GetMethod(name, declaredOnlyBindingFlags[memberType])); + var value = Get(declaredMethods, type, name, arguments, () => type.GetMethod(name, declaredOnlyBindingFlags[memberType], null, arguments, null)); if (value == null && declaredOnly == false) value = Get(inheritedMethods, type, name, arguments, () => AccessTools.Method(type, name, arguments)); return value; diff --git a/Harmony/Public/Harmony.cs b/Harmony/Public/Harmony.cs index 87b13e65..e7e32001 100644 --- a/Harmony/Public/Harmony.cs +++ b/Harmony/Public/Harmony.cs @@ -147,8 +147,8 @@ public void UnpatchAll(string harmonyID = null) foreach (var original in originals) { var info = GetPatchInfo(original); - info.Prefixes.DoIf(IDCheck, patchInfo => Unpatch(original, patchInfo.PatchMethod)); info.Postfixes.DoIf(IDCheck, patchInfo => Unpatch(original, patchInfo.PatchMethod)); + info.Prefixes.DoIf(IDCheck, patchInfo => Unpatch(original, patchInfo.PatchMethod)); info.Transpilers.DoIf(IDCheck, patchInfo => Unpatch(original, patchInfo.PatchMethod)); info.Finalizers.DoIf(IDCheck, patchInfo => Unpatch(original, patchInfo.PatchMethod)); } diff --git a/HarmonyTests/HarmonyTests.csproj b/HarmonyTests/HarmonyTests.csproj index 376635b5..f4a0a251 100644 --- a/HarmonyTests/HarmonyTests.csproj +++ b/HarmonyTests/HarmonyTests.csproj @@ -49,7 +49,7 @@ - + diff --git a/HarmonyTests/Patching/Assets/PatchClasses.cs b/HarmonyTests/Patching/Assets/PatchClasses.cs index c89c6984..10c2ad8f 100644 --- a/HarmonyTests/Patching/Assets/PatchClasses.cs +++ b/HarmonyTests/Patching/Assets/PatchClasses.cs @@ -1008,6 +1008,42 @@ public static void Method() } } + public struct UnityColor + { + public float r; + public float g; + public float b; + public float a; + } + + public class APIUser + { + } + + public class Class18 + { + [MethodImpl(MethodImplOptions.NoInlining)] + public static UnityColor GetDefaultNameplateColor(APIUser user) + { + return new UnityColor() { r = 0, g = 0, b = 0, a = 0 }; + } + } + + [HarmonyPatch(typeof(Class18))] + [HarmonyPatch(nameof(Class18.GetDefaultNameplateColor))] + public static class Class18Patch + { + public static bool prefixExecuted = false; + + public static bool Prefix(APIUser __0, ref UnityColor __result) + { + _ = __0; + prefixExecuted = true; + __result = new UnityColor() { r = 1, g = 1, b = 1, a = 1 }; + return false; + } + } + // disabled - see test case /* public class ClassExceptionFilter diff --git a/HarmonyTests/Patching/Assets/Specials.cs b/HarmonyTests/Patching/Assets/Specials.cs index 8a5e5d65..8103a28c 100644 --- a/HarmonyTests/Patching/Assets/Specials.cs +++ b/HarmonyTests/Patching/Assets/Specials.cs @@ -173,4 +173,14 @@ static void Prefix(ConcreteClass __instance, string def, AnotherStruct loc) Console.WriteLine("ConcreteClass_Patch.Prefix"); } } + + [HarmonyPatch(typeof(AppDomain), nameof(AppDomain.GetData))] + public class ExternalMethod_Patch + { + static IEnumerable Transpiler(IEnumerable instructions) + { + yield return new CodeInstruction(OpCodes.Ldnull); + yield return new CodeInstruction(OpCodes.Ret); + } + } } \ No newline at end of file diff --git a/HarmonyTests/Patching/Specials.cs b/HarmonyTests/Patching/Specials.cs index 609ca5c2..04a660cf 100644 --- a/HarmonyTests/Patching/Specials.cs +++ b/HarmonyTests/Patching/Specials.cs @@ -227,5 +227,18 @@ public void Test_PatchExceptionWithCleanup3() Assert.NotNull(patcher, "Patch processor"); _ = patcher.Patch(); } + + [Test] + public void Test_PatchExternalMethod() + { + var patchClass = typeof(ExternalMethod_Patch); + Assert.NotNull(patchClass); + + var instance = new Harmony("test"); + Assert.NotNull(instance, "Harmony instance"); + var patcher = instance.CreateClassProcessor(patchClass); + Assert.NotNull(patcher, "Patch processor"); + _ = patcher.Patch(); + } } } \ No newline at end of file diff --git a/HarmonyTests/Patching/StaticPatches.cs b/HarmonyTests/Patching/StaticPatches.cs index 2758843a..88e9f7e1 100644 --- a/HarmonyTests/Patching/StaticPatches.cs +++ b/HarmonyTests/Patching/StaticPatches.cs @@ -383,5 +383,24 @@ public void Test_Affecting_Original_Prefixes() Assert.AreEqual("Prefix3", events[2]); Assert.AreEqual("Prefix5", events[3]); } + + [Test] + public void Test_Class18() + { + var instance = new Harmony("test"); + Assert.NotNull(instance, "instance"); + var processor = instance.CreateClassProcessor(typeof(Class18Patch)); + Assert.NotNull(processor, "processor"); + + var methods = processor.Patch(); + Assert.NotNull(methods, "methods"); + Assert.AreEqual(1, methods.Count); + + Class18Patch.prefixExecuted = false; + var color = Class18.GetDefaultNameplateColor(new APIUser()); + + Assert.IsTrue(Class18Patch.prefixExecuted, "prefixExecuted"); + Assert.AreEqual((float)1, color.r); + } } } \ No newline at end of file diff --git a/HarmonyTests/Traverse/Assets/TraverseMethods.cs b/HarmonyTests/Traverse/Assets/TraverseMethods.cs index af609437..87f2085b 100644 --- a/HarmonyTests/Traverse/Assets/TraverseMethods.cs +++ b/HarmonyTests/Traverse/Assets/TraverseMethods.cs @@ -69,4 +69,17 @@ static T WithGenericParameter(T refParameter) } #pragma warning restore IDE0051 } + + public class TraverseMethods_Overloads + { + public bool SomeMethod(string p1, bool p2 = true) + { + return !p2; + } + + public int SomeMethod(string p1, int p2, bool p3 = true) + { + return 0; + } + } } \ No newline at end of file diff --git a/HarmonyTests/Traverse/TestTraverse_Methods.cs b/HarmonyTests/Traverse/TestTraverse_Methods.cs index f3353e91..bfdeda10 100644 --- a/HarmonyTests/Traverse/TestTraverse_Methods.cs +++ b/HarmonyTests/Traverse/TestTraverse_Methods.cs @@ -69,5 +69,16 @@ public void Traverse_Method_OutParameters() Assert.AreEqual("ok", mtrv1.GetValue()); Assert.AreEqual("hello", parameters[0]); } + + [Test] + public void Traverse_Method_Overloads() + { + var instance = new TraverseMethods_Overloads(); + var trv = Traverse.Create(instance); + + var mtrv1 = trv.Method("SomeMethod", new Type[] { typeof(string), typeof(bool) }); + Assert.AreEqual(true, mtrv1.GetValue("test", false)); + Assert.AreEqual(false, mtrv1.GetValue("test", true)); + } } } \ No newline at end of file