-
Notifications
You must be signed in to change notification settings - Fork 43
/
PatchModels.cs
152 lines (133 loc) · 4.29 KB
/
PatchModels.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace HarmonyLib
{
// PatchJobs holds the information during correlation
// of methods and patches while processing attribute patches
//
internal class PatchJobs<T>
{
internal class Job
{
internal MethodBase original;
internal T replacement;
internal List<HarmonyMethod> prefixes = new List<HarmonyMethod>();
internal List<HarmonyMethod> postfixes = new List<HarmonyMethod>();
internal List<HarmonyMethod> transpilers = new List<HarmonyMethod>();
internal List<HarmonyMethod> finalizers = new List<HarmonyMethod>();
internal List<HarmonyMethod> ilmanipulators = new List<HarmonyMethod>();
internal void AddPatch(AttributePatch patch)
{
switch (patch.type)
{
case HarmonyPatchType.Prefix:
prefixes.Add(patch.info);
break;
case HarmonyPatchType.Postfix:
postfixes.Add(patch.info);
break;
case HarmonyPatchType.Transpiler:
transpilers.Add(patch.info);
break;
case HarmonyPatchType.Finalizer:
finalizers.Add(patch.info);
break;
case HarmonyPatchType.ILManipulator:
ilmanipulators.Add(patch.info);
break;
}
}
}
internal Dictionary<MethodBase, Job> state = new Dictionary<MethodBase, Job>();
internal Job GetJob(MethodBase method)
{
if (method is null) return null;
if (state.TryGetValue(method, out var job) is false)
{
job = new Job() { original = method };
state[method] = job;
}
return job;
}
internal List<Job> GetJobs()
{
return state.Values.Where(job =>
job.prefixes.Count +
job.postfixes.Count +
job.transpilers.Count +
job.finalizers.Count +
job.ilmanipulators.Count > 0
).ToList();
}
internal List<T> GetReplacements()
{
return state.Values.Select(job => job.replacement).ToList();
}
}
// AttributePatch contains all information for a patch defined by attributes
//
internal class AttributePatch
{
static readonly HarmonyPatchType[] allPatchTypes = new[] {
HarmonyPatchType.Prefix,
HarmonyPatchType.Postfix,
HarmonyPatchType.Transpiler,
HarmonyPatchType.Finalizer,
HarmonyPatchType.ReversePatch,
HarmonyPatchType.ILManipulator,
};
internal HarmonyMethod info;
internal HarmonyPatchType? type;
static readonly string harmonyAttributeName = typeof(HarmonyAttribute).FullName;
internal static IEnumerable<AttributePatch> Create(MethodInfo patch)
{
if (patch is null)
throw new NullReferenceException("Patch method cannot be null");
var allAttributes = patch.GetCustomAttributes(true);
var methodName = patch.Name;
var type = GetPatchType(methodName, allAttributes);
if (type is null)
return Enumerable.Empty<AttributePatch>();
if (type != HarmonyPatchType.ReversePatch && patch.IsStatic is false)
throw new ArgumentException("Patch method " + patch.FullDescription() + " must be static");
var list = allAttributes
.Where(attr => attr.GetType().BaseType.FullName == harmonyAttributeName)
.Select(attr =>
{
var f_info = AccessTools.Field(attr.GetType(), nameof(HarmonyAttribute.info));
return f_info.GetValue(attr);
})
.Select(harmonyInfo => AccessTools.MakeDeepCopy<HarmonyMethod>(harmonyInfo))
.ToList();
var info = HarmonyMethod.Merge(list);
static bool Same(HarmonyMethod m1, HarmonyMethod m2) =>
m1.GetDeclaringType() == m2.GetDeclaringType() && m1.methodName == m2.methodName;
var completeMethods = list.Where(m =>
m.GetDeclaringType() != null && m.methodName != null &&
!Same(m, info)).ToList();
completeMethods.Add(info);
foreach (var completeMethod in completeMethods)
completeMethod.method = patch;
return completeMethods.Select(i => new AttributePatch() { info = i, type = type }).ToList();
}
static HarmonyPatchType? GetPatchType(string methodName, object[] allAttributes)
{
var harmonyAttributes = new HashSet<string>(allAttributes
.Select(attr => attr.GetType().FullName)
.Where(name => name.StartsWith("Harmony")));
HarmonyPatchType? type = null;
foreach (var patchType in allPatchTypes)
{
var name = patchType.ToString();
if (name == methodName || harmonyAttributes.Contains($"HarmonyLib.Harmony{name}"))
{
type = patchType;
break;
}
}
return type;
}
}
}