-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathHarmonyExt.cs
232 lines (214 loc) · 7.83 KB
/
HarmonyExt.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using HarmonyLib;
using StardewModdingAPI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Shockah.Kokoro;
public static class HarmonyExt
{
private static void WarnOnDebugAssembly(IMonitor monitor, Assembly? assembly)
{
if (assembly?.IsBuiltInDebugConfiguration() == true)
monitor.LogOnce($"{assembly.GetName().Name} was built in debug configuration - patching may fail. If it does fail, please ask that mod's developer to build it in release configuration.", LogLevel.Warn);
}
public static void Patch(
this Harmony self,
MethodBase? original,
IMonitor monitor,
LogLevel problemLogLevel = LogLevel.Error,
LogLevel successLogLevel = LogLevel.Trace,
HarmonyMethod? prefix = null,
HarmonyMethod? postfix = null,
HarmonyMethod? transpiler = null,
HarmonyMethod? finalizer = null
)
{
if (original is null)
{
monitor.Log($"Could not patch method - the mod may not work correctly.\nReason: Unknown method to patch.", problemLogLevel);
return;
}
if (transpiler is not null)
WarnOnDebugAssembly(monitor, original.DeclaringType?.Assembly);
self.Patch(original, prefix, postfix, transpiler, finalizer);
monitor.Log($"Patched method {original.FullDescription()}.", successLogLevel);
}
public static bool TryPatch(
this Harmony self,
Func<MethodBase?> original,
IMonitor monitor,
LogLevel problemLogLevel = LogLevel.Error,
LogLevel successLogLevel = LogLevel.Trace,
HarmonyMethod? prefix = null,
HarmonyMethod? postfix = null,
HarmonyMethod? transpiler = null,
HarmonyMethod? finalizer = null
)
{
var originalMethod = original();
if (originalMethod is null)
{
monitor.Log($"Could not patch method - the mod may not work correctly.\nReason: Unknown method to patch.", problemLogLevel);
return false;
}
try
{
if (transpiler is not null)
WarnOnDebugAssembly(monitor, originalMethod.DeclaringType?.Assembly);
self.Patch(originalMethod, prefix, postfix, transpiler, finalizer);
monitor.Log($"Patched method {originalMethod.FullDescription()}.", successLogLevel);
return true;
}
catch (Exception ex)
{
monitor.Log($"Could not patch method {originalMethod} - the mod may not work correctly.\nReason: {ex}", problemLogLevel);
return false;
}
}
public static void PatchVirtual(
this Harmony self,
MethodBase? original,
IMonitor monitor,
LogLevel problemLogLevel = LogLevel.Error,
LogLevel successLogLevel = LogLevel.Trace,
HarmonyMethod? prefix = null,
HarmonyMethod? postfix = null,
HarmonyMethod? transpiler = null,
HarmonyMethod? finalizer = null
)
{
if (original is null)
{
monitor.Log($"Could not patch method - the mod may not work correctly.\nReason: Unknown method to patch.", problemLogLevel);
return;
}
Type? declaringType = original.DeclaringType;
if (declaringType == null)
throw new ArgumentException($"{nameof(original)}.{nameof(original.DeclaringType)} is null.");
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
IEnumerable<Type> subtypes = Enumerable.Empty<Type>();
try
{
subtypes = assembly.GetTypes().Where(t => t.IsAssignableTo(declaringType));
}
catch (Exception ex)
{
monitor.Log($"There was a problem while getting types defined in assembly {assembly.GetName().Name}, ignoring it. Reason:\n{ex}", LogLevel.Trace);
}
foreach (Type subtype in subtypes)
{
var originalParameters = original.GetParameters();
var subtypeOriginal = AccessTools.Method(
subtype,
original.Name,
originalParameters.Select(p => p.ParameterType).ToArray()
);
if (subtypeOriginal is null)
continue;
if (!subtypeOriginal.IsDeclaredMember())
continue;
if (!subtypeOriginal.HasMethodBody())
continue;
static bool ContainsNonSpecialArguments(HarmonyMethod patch)
=> patch.method.GetParameters().Any(p => !(p.Name ?? "").StartsWith("__"));
if (
(prefix is not null && ContainsNonSpecialArguments(prefix)) ||
(postfix is not null && ContainsNonSpecialArguments(postfix)) ||
(finalizer is not null && ContainsNonSpecialArguments(finalizer))
)
{
var subtypeOriginalParameters = subtypeOriginal.GetParameters();
for (int i = 0; i < original.GetParameters().Length; i++)
if (originalParameters[i].Name != subtypeOriginalParameters[i].Name)
throw new InvalidOperationException($"Method {declaringType.Name}.{original.Name} cannot be automatically patched for subtype {subtype.Name}, because argument #{i} has a mismatched name: `{originalParameters[i].Name}` vs `{subtypeOriginalParameters[i].Name}`.");
}
self.Patch(subtypeOriginal, prefix, subtypeOriginal.HasMethodBody() ? postfix : null, transpiler, finalizer);
monitor.Log($"Patched method {subtypeOriginal.FullDescription()}.", successLogLevel);
}
}
}
public static int TryPatchVirtual(
this Harmony self,
Func<MethodBase?> original,
IMonitor monitor,
LogLevel problemLogLevel = LogLevel.Error,
LogLevel successLogLevel = LogLevel.Trace,
HarmonyMethod? prefix = null,
HarmonyMethod? postfix = null,
HarmonyMethod? transpiler = null,
HarmonyMethod? finalizer = null
)
{
var originalMethod = original();
if (originalMethod is null)
{
monitor.Log($"Could not patch method - the mod may not work correctly.\nReason: Unknown method to patch.", problemLogLevel);
return 0;
}
int patched = 0;
try
{
Type? declaringType = originalMethod.DeclaringType;
if (declaringType == null)
throw new ArgumentException($"{nameof(original)}.{nameof(originalMethod.DeclaringType)} is null.");
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
IEnumerable<Type> subtypes = Enumerable.Empty<Type>();
try
{
subtypes = assembly.GetTypes().Where(t => t.IsAssignableTo(declaringType));
}
catch (Exception ex)
{
monitor.Log($"There was a problem while getting types defined in assembly {assembly.GetName().Name}, ignoring it. Reason:\n{ex}", LogLevel.Trace);
}
foreach (Type subtype in subtypes)
{
try
{
var originalParameters = originalMethod.GetParameters();
var subtypeOriginal = AccessTools.Method(
subtype,
originalMethod.Name,
originalParameters.Select(p => p.ParameterType).ToArray()
);
if (subtypeOriginal is null)
continue;
if (!subtypeOriginal.IsDeclaredMember())
continue;
if (!subtypeOriginal.HasMethodBody())
continue;
static bool ContainsNonSpecialArguments(HarmonyMethod patch)
=> patch.method.GetParameters().Any(p => !(p.Name ?? "").StartsWith("__"));
if (
(prefix is not null && ContainsNonSpecialArguments(prefix)) ||
(postfix is not null && ContainsNonSpecialArguments(postfix)) ||
(finalizer is not null && ContainsNonSpecialArguments(finalizer))
)
{
var subtypeOriginalParameters = subtypeOriginal.GetParameters();
for (int i = 0; i < originalMethod.GetParameters().Length; i++)
if (originalParameters[i].Name != subtypeOriginalParameters[i].Name)
throw new InvalidOperationException($"Method {declaringType.Name}.{originalMethod.Name} cannot be automatically patched for subtype {subtype.Name}, because argument #{i} has a mismatched name: `{originalParameters[i].Name}` vs `{subtypeOriginalParameters[i].Name}`.");
}
self.Patch(subtypeOriginal, prefix, postfix, transpiler, finalizer);
monitor.Log($"Patched method {subtypeOriginal.FullDescription()}.", successLogLevel);
patched++;
}
catch (Exception ex)
{
monitor.Log($"Could not patch method - the mod may not work correctly.\nReason: {ex}", problemLogLevel);
}
}
}
return patched;
}
catch (Exception ex)
{
monitor.Log($"Could not patch method {originalMethod} - the mod may not work correctly.\nReason: {ex}", problemLogLevel);
return patched;
}
}
}