Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Jan 24, 2020
1 parent 7a3ae2a commit ec047da
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
31 changes: 31 additions & 0 deletions Harmony/Internal/PatchTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,36 @@ internal static List<MethodInfo> GetReversePatches(Type patchType)
.Where(m => m.GetCustomAttributes(true).Any(a => a.GetType().FullName == attr))
.ToList();
}

internal static MethodBase GetOriginalMethod(this HarmonyMethod attr)
{
switch (attr.methodType)
{
case MethodType.Normal:
if (attr.methodName == null)
return null;
return AccessTools.DeclaredMethod(attr.declaringType, attr.methodName, attr.argumentTypes);

case MethodType.Getter:
if (attr.methodName == null)
return null;
return AccessTools.DeclaredProperty(attr.declaringType, attr.methodName).GetGetMethod(true);

case MethodType.Setter:
if (attr.methodName == null)
return null;
return AccessTools.DeclaredProperty(attr.declaringType, attr.methodName).GetSetMethod(true);

case MethodType.Constructor:
return AccessTools.DeclaredConstructor(attr.declaringType, attr.argumentTypes);

case MethodType.StaticConstructor:
return AccessTools.GetDeclaredConstructors(attr.declaringType)
.Where(c => c.IsStatic)
.FirstOrDefault();
}

return null;
}
}
}
15 changes: 6 additions & 9 deletions Harmony/Public/PatchClassProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;

namespace HarmonyLib
{
Expand Down Expand Up @@ -67,15 +66,18 @@ public PatchClassProcessor(Harmony instance, Type type)
}

/// <summary>Applies the patches</summary>
/// <returns>A list of all created dynamic methods</returns>
/// <returns>A list of all created dynamic methods or null if patch class is not annotated</returns>
///
public List<MethodInfo> Patch()
{
if (containerAttributes == null)
return null;

var mainPrepareResult = RunMethod<HarmonyPrepare, bool>(true);
if (mainPrepareResult == false)
{
RunMethod<HarmonyCleanup>();
return null;
return new List<MethodInfo>();
}

foreach (var reversePatchMethod in reversePatchMethods)
Expand Down Expand Up @@ -123,12 +125,7 @@ List<MethodInfo> PatchWithAttributes()
var jobs = new PatchJobs<MethodInfo>();
foreach (var patchMethod in patchMethods)
{
if (patchMethod.info.declaringType == null)
throw new ArgumentException($"Undefined class for method for patch method {patchMethod.info.method.FullDescription()}");
if (patchMethod.info.methodName == null)
throw new ArgumentException($"Undefined method name for patch method {patchMethod.info.method.FullDescription()}");

var original = AccessTools.Method(patchMethod.info.declaringType, patchMethod.info.methodName, patchMethod.info.argumentTypes);
var original = patchMethod.info.GetOriginalMethod();
if (original == null)
throw new ArgumentException($"Undefined target method for patch method {patchMethod.info.method.FullDescription()}");

Expand Down

0 comments on commit ec047da

Please sign in to comment.