Skip to content

Commit

Permalink
Fixes for #266 #268 #269, using MMC 20.3.25.2
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Mar 25, 2020
1 parent eea4d04 commit f430378
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 8 deletions.
10 changes: 5 additions & 5 deletions Harmony/Harmony.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
<Authors>Andreas Pardeike</Authors>
<AssemblyName>0Harmony</AssemblyName>
<SignAssembly>true</SignAssembly>
<Version>2.0.0.8</Version>
<Version>2.0.0.9</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://github.com/pardeike/Harmony</PackageProjectUrl>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageTags>Harmony,Mono,Patch,Patching,Runtime,Detour,Detours,Aspect,Aspects</PackageTags>
<AssemblyVersion>2.0.0.8</AssemblyVersion>
<FileVersion>2.0.0.8</FileVersion>
<AssemblyVersion>2.0.0.9</AssemblyVersion>
<FileVersion>2.0.0.9</FileVersion>
<PackageIcon>HarmonyLogo.png</PackageIcon>
<PackageIconUrl>https://raw.githubusercontent.com/pardeike/Harmony/master/HarmonyLogo.png</PackageIconUrl>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down Expand Up @@ -85,7 +85,7 @@

<ItemGroup>
<!-- https://dev.azure.com/MonoMod/MonoMod/_packaging?_a=feed&feed=DevBuilds -->
<PackageReference Include="MonoMod.Common" Version="20.2.27.1">
<PackageReference Include="MonoMod.Common" Version="20.3.25.2">
<PrivateAssets Condition="!$(IsCoreOrStandard)">All</PrivateAssets>
</PackageReference>
<PackageReference Include="ILRepack.MSBuild.Task" Version="2.0.13" PrivateAssets="All" />
Expand All @@ -96,7 +96,7 @@
<WorkingDirectory>$(MSBuildThisFileDirectory)bin\$(Configuration)</WorkingDirectory>
</PropertyGroup>
<ItemGroup>
<OldNugetPackages Include="$(WorkingDirectory)\$(PackageId).*.nupkg" Exclude="$(WorkingDirectory)\$(PackageId).$(Version).nupkg" />
<OldNugetPackages Include="$(WorkingDirectory)\$(PackageId).*.nupkg" Exclude="$(WorkingDirectory)\$(PackageId).$(Version).nupkg" />
</ItemGroup>
<Delete Files="@(OldNugetPackages)" />
</Target>
Expand Down
2 changes: 1 addition & 1 deletion Harmony/Internal/AccessCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Harmony/Public/Harmony.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
2 changes: 1 addition & 1 deletion HarmonyTests/HarmonyTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MonoMod.Common" Version="20.2.27.1" />
<PackageReference Include="MonoMod.Common" Version="20.3.25.2" />
</ItemGroup>

<Target Name="ChangeAliasesOfStrongNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
Expand Down
36 changes: 36 additions & 0 deletions HarmonyTests/Patching/Assets/PatchClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions HarmonyTests/Patching/Assets/Specials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
yield return new CodeInstruction(OpCodes.Ldnull);
yield return new CodeInstruction(OpCodes.Ret);
}
}
}
13 changes: 13 additions & 0 deletions HarmonyTests/Patching/Specials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
19 changes: 19 additions & 0 deletions HarmonyTests/Patching/StaticPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
13 changes: 13 additions & 0 deletions HarmonyTests/Traverse/Assets/TraverseMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,17 @@ static T WithGenericParameter<T>(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;
}
}
}
11 changes: 11 additions & 0 deletions HarmonyTests/Traverse/TestTraverse_Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,16 @@ public void Traverse_Method_OutParameters()
Assert.AreEqual("ok", mtrv1.GetValue<string>());
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<bool>("test", false));
Assert.AreEqual(false, mtrv1.GetValue<bool>("test", true));
}
}
}

0 comments on commit f430378

Please sign in to comment.