Skip to content

Commit

Permalink
Bug fixes; v1.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Feb 4, 2017
1 parent 39b990f commit 2adaaeb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 50 deletions.
5 changes: 3 additions & 2 deletions Harmony/PatchFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ public static void UpdateWrapper(MethodBase original, PatchInfo patchInfo)
var sortedPostfixes = GetSortedPatchMethods(patchInfo.postfixes);
var sortedProcessors = GetSortedProcessors(patchInfo.processors);

var replacement = MethodPatcher.CreatePatchedMethod(original, sortedPostfixes, sortedPostfixes, sortedProcessors);
var replacement = MethodPatcher.CreatePatchedMethod(original, sortedPrefixes, sortedPostfixes, sortedProcessors);
if (replacement == null) throw new MissingMethodException("Cannot create dynamic replacement for " + original);
PatchTools.KeepAliveForever(replacement);

var originalCodeStart = Memory.GetMethodStart(original);
var patchCodeStart = Memory.GetMethodStart(replacement);
Memory.WriteJump(originalCodeStart, patchCodeStart);

PatchTools.RememberObject(original, replacement); // no gc for new value + release old value to gc
}
}
}
4 changes: 1 addition & 3 deletions Harmony/PatchProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Harmony.ILCopying;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Harmony
Expand Down
4 changes: 2 additions & 2 deletions Harmony/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.5.0")]
[assembly: AssemblyFileVersion("1.0.5.0")]
[assembly: AssemblyVersion("1.0.6.0")]
[assembly: AssemblyFileVersion("1.0.6.0")]
51 changes: 8 additions & 43 deletions Harmony/Tools/PatchTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,18 @@ namespace Harmony
public static class PatchTools
{
// this holds all the objects we want to keep alive so they don't get garbage-collected
static object[] objectReferences;
[MethodImpl(MethodImplOptions.Synchronized)]
public static void KeepAliveForever(object obj)
static Dictionary<object, object> objectReferences = new Dictionary<object, object>();
public static void RememberObject(object key, object value)
{
if (objectReferences == null)
objectReferences = new object[0];
objectReferences.Add(obj);
objectReferences[key] = value;
}

public static MethodInfo GetPatchMethod<T>(Type patchType, string name, Type[] parameter)
public static MethodInfo GetPatchMethod<T>(Type patchType, string name, Type[] parameters = null)
{
var method = patchType.GetMethods(AccessTools.all)
.FirstOrDefault(m => m.GetCustomAttributes(typeof(T), true).Count() > 0);
if (method == null)
method = patchType.GetMethod(name, AccessTools.all, null, parameter, null);
method = AccessTools.Method(patchType, name, parameters);
return method;
}

Expand All @@ -32,42 +29,10 @@ public static void GetPatches(Type patchType, MethodBase original, out MethodInf
var type = original.DeclaringType;
var methodName = original.Name;

var parameters = original.GetParameters();
var prefixParams = new List<Type>();
var postfixParams = new List<Type>();
if (original.IsStatic == false)
{
prefixParams.Add(type);
postfixParams.Add(type);
}
var returnedType = AccessTools.GetReturnedType(original);
if (returnedType != typeof(void))
{
var retRef = returnedType.MakeByRefType();
prefixParams.Add(retRef);
postfixParams.Add(retRef);
}
parameters.Do(pi =>
{
var paramRef = pi.ParameterType.MakeByRefType();
if (pi.IsOut == false) // prefix patches should not get out-parameters
prefixParams.Add(paramRef);
postfixParams.Add(paramRef);
});

prefix = GetPatchMethod<HarmonyPrefix>(patchType, "Prefix", prefixParams.ToArray());
postfix = GetPatchMethod<HarmonyPostfix>(patchType, "Postfix", postfixParams.ToArray());
prefix = GetPatchMethod<HarmonyPrefix>(patchType, "Prefix");
postfix = GetPatchMethod<HarmonyPostfix>(patchType, "Postfix");
if (prefix == null && postfix == null)
{
var prefixMethod = "Prefix(" + string.Join(", ", prefixParams.Select(p => p.FullName).ToArray()) + ")";
var postfixMethod = "Postfix(" + string.Join(", ", postfixParams.Select(p => p.FullName).ToArray()) + ")";
throw new MissingMethodException("No prefix/postfix patch for " + type.FullName + "." + methodName + "() found that matches " + prefixMethod + " or " + postfixMethod);
}

if (prefix != null && prefix.ReturnType != typeof(bool))
throw new MissingMethodException("Prefix() must return bool (return true to execute original method)");
if (postfix != null && postfix.ReturnType != typeof(void))
throw new MissingMethodException("Postfix() must not return anything");
throw new MissingMethodException("No prefix/postfix patch for " + type.FullName + "." + methodName + "() found");
}
}
}

0 comments on commit 2adaaeb

Please sign in to comment.