Skip to content

Commit 2adaaeb

Browse files
committed
Bug fixes; v1.0.6
1 parent 39b990f commit 2adaaeb

File tree

4 files changed

+14
-50
lines changed

4 files changed

+14
-50
lines changed

Harmony/PatchFunctions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,14 @@ public static void UpdateWrapper(MethodBase original, PatchInfo patchInfo)
6767
var sortedPostfixes = GetSortedPatchMethods(patchInfo.postfixes);
6868
var sortedProcessors = GetSortedProcessors(patchInfo.processors);
6969

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

7473
var originalCodeStart = Memory.GetMethodStart(original);
7574
var patchCodeStart = Memory.GetMethodStart(replacement);
7675
Memory.WriteJump(originalCodeStart, patchCodeStart);
76+
77+
PatchTools.RememberObject(original, replacement); // no gc for new value + release old value to gc
7778
}
7879
}
7980
}

Harmony/PatchProcessor.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using Harmony.ILCopying;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
4-
using System.Linq;
53
using System.Reflection;
64

75
namespace Harmony

Harmony/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.5.0")]
36-
[assembly: AssemblyFileVersion("1.0.5.0")]
35+
[assembly: AssemblyVersion("1.0.6.0")]
36+
[assembly: AssemblyFileVersion("1.0.6.0")]

Harmony/Tools/PatchTools.cs

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,18 @@ namespace Harmony
99
public static class PatchTools
1010
{
1111
// this holds all the objects we want to keep alive so they don't get garbage-collected
12-
static object[] objectReferences;
13-
[MethodImpl(MethodImplOptions.Synchronized)]
14-
public static void KeepAliveForever(object obj)
12+
static Dictionary<object, object> objectReferences = new Dictionary<object, object>();
13+
public static void RememberObject(object key, object value)
1514
{
16-
if (objectReferences == null)
17-
objectReferences = new object[0];
18-
objectReferences.Add(obj);
15+
objectReferences[key] = value;
1916
}
2017

21-
public static MethodInfo GetPatchMethod<T>(Type patchType, string name, Type[] parameter)
18+
public static MethodInfo GetPatchMethod<T>(Type patchType, string name, Type[] parameters = null)
2219
{
2320
var method = patchType.GetMethods(AccessTools.all)
2421
.FirstOrDefault(m => m.GetCustomAttributes(typeof(T), true).Count() > 0);
2522
if (method == null)
26-
method = patchType.GetMethod(name, AccessTools.all, null, parameter, null);
23+
method = AccessTools.Method(patchType, name, parameters);
2724
return method;
2825
}
2926

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

35-
var parameters = original.GetParameters();
36-
var prefixParams = new List<Type>();
37-
var postfixParams = new List<Type>();
38-
if (original.IsStatic == false)
39-
{
40-
prefixParams.Add(type);
41-
postfixParams.Add(type);
42-
}
43-
var returnedType = AccessTools.GetReturnedType(original);
44-
if (returnedType != typeof(void))
45-
{
46-
var retRef = returnedType.MakeByRefType();
47-
prefixParams.Add(retRef);
48-
postfixParams.Add(retRef);
49-
}
50-
parameters.Do(pi =>
51-
{
52-
var paramRef = pi.ParameterType.MakeByRefType();
53-
if (pi.IsOut == false) // prefix patches should not get out-parameters
54-
prefixParams.Add(paramRef);
55-
postfixParams.Add(paramRef);
56-
});
57-
58-
prefix = GetPatchMethod<HarmonyPrefix>(patchType, "Prefix", prefixParams.ToArray());
59-
postfix = GetPatchMethod<HarmonyPostfix>(patchType, "Postfix", postfixParams.ToArray());
32+
prefix = GetPatchMethod<HarmonyPrefix>(patchType, "Prefix");
33+
postfix = GetPatchMethod<HarmonyPostfix>(patchType, "Postfix");
6034
if (prefix == null && postfix == null)
61-
{
62-
var prefixMethod = "Prefix(" + string.Join(", ", prefixParams.Select(p => p.FullName).ToArray()) + ")";
63-
var postfixMethod = "Postfix(" + string.Join(", ", postfixParams.Select(p => p.FullName).ToArray()) + ")";
64-
throw new MissingMethodException("No prefix/postfix patch for " + type.FullName + "." + methodName + "() found that matches " + prefixMethod + " or " + postfixMethod);
65-
}
66-
67-
if (prefix != null && prefix.ReturnType != typeof(bool))
68-
throw new MissingMethodException("Prefix() must return bool (return true to execute original method)");
69-
if (postfix != null && postfix.ReturnType != typeof(void))
70-
throw new MissingMethodException("Postfix() must not return anything");
35+
throw new MissingMethodException("No prefix/postfix patch for " + type.FullName + "." + methodName + "() found");
7136
}
7237
}
7338
}

0 commit comments

Comments
 (0)