-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHPHelper.cs
More file actions
208 lines (190 loc) · 8.07 KB
/
HPHelper.cs
File metadata and controls
208 lines (190 loc) · 8.07 KB
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
// 一个方便的Harmony补丁框架,可以自动Patch所有由[HPHelper]与[HPPrefix]或[HPPostfix]标记的方法。
// 作者:曾小皮-ZengXiaoPi
// 在MIT许可证下发布,允许自由分享、修改、分发。
// 用法:
// 0. 将HPPatcher.LogError改为你的日志记录方法。
// 1. 在你需要Patch的类中添加[HPHelper]属性,并设置需要Patch的类型、方法名、参数类型(可选)、是否无参数(可选)。
// 注意,如果你需要Patch的方法有多个重载,而你刚好需要Patch没有参数的方法,那么你需要在[HPHelper]属性中设置TargetUseNoParameter为true。
// 2. 在你需要Patch的方法中添加[HPPrefix]或[HPPostfix]属性。
// 3. 在你需要Patch的入口处调用HPPatcher.PatchAll(HarmonyInstance.Create("your.mod.name"), typeof(YourPatchClass));
using BossAmiya;
using Harmony;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace HPHelper
{
[AttributeUsage(AttributeTargets.Method)]
public class HPPrefixAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Method)]
public class HPPostfixAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Method)]
public class HPTranspilerAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class HPHelperAttribute : Attribute
{
public Type TargetType { get; }
public string TargetTypeName { get; }
public string MethodName { get; }
public Type[] ParameterTypes { get; }
public bool TargetUseNoParameter { get; }
public HPHelperAttribute(Type targetType, string methodName, params Type[] parameterTypes)
{
TargetType = targetType;
MethodName = methodName;
ParameterTypes = parameterTypes;
TargetUseNoParameter = false;
}
public HPHelperAttribute(string targetTypeName, string methodName, params Type[] parameterTypes)
{
TargetTypeName = targetTypeName;
MethodName = methodName;
ParameterTypes = parameterTypes;
TargetUseNoParameter = false;
}
public HPHelperAttribute(Type targetType, string methodName)
{
TargetType = targetType;
MethodName = methodName;
ParameterTypes = null;
TargetUseNoParameter = false;
}
public HPHelperAttribute(string targetTypeName, string methodName)
{
TargetTypeName = targetTypeName;
MethodName = methodName;
ParameterTypes = null;
TargetUseNoParameter = false;
}
public HPHelperAttribute(Type targetType, string methodName, bool targetUseNoParameter)
{
TargetType = targetType;
MethodName = methodName;
ParameterTypes = null;
TargetUseNoParameter = true;
}
public HPHelperAttribute(string targetTypeName, string methodName, bool targetUseNoParameter)
{
TargetTypeName = targetTypeName;
MethodName = methodName;
ParameterTypes = null;
TargetUseNoParameter = true;
}
}
public static class HPPatcher
{
/// <summary>
/// 日志记录。
/// </summary>
/// <param name="message">消息</param>
public static void LogError(string message)
{
// 请修改此处为你自己的日志记录方法
Harmony_Patch.logger.Error(message);
}
/// <summary>
/// 自动Patch所有由[HPHelper]与[HPPrefix]或[HPPostfix]或[Transpiler]标记的方法
/// </summary>
/// <param name="harmony">HarmonyInstance实例</param>
/// <param name="patchClass">你需要Patch的类</param>
public static void PatchAll(HarmonyInstance harmony, Type patchClass)
{
foreach (var method in patchClass.GetMethods(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public))
{
try
{
ProcessMethod(harmony, method);
}
catch (Exception ex)
{
LogError($"Error patching {method.Name}: {ex}");
}
}
}
private static void ProcessMethod(HarmonyInstance harmony, MethodInfo patchMethod)
{
try
{
var hpHelper = (HPHelperAttribute)Attribute.GetCustomAttribute(
patchMethod,
typeof(HPHelperAttribute)
);
if (hpHelper == null) return;
int fixTypeCount = 0;
bool isPrefix = patchMethod.IsDefined(typeof(HPPrefixAttribute), false);
bool isPostfix = patchMethod.IsDefined(typeof(HPPostfixAttribute), false);
bool isTranspiler = patchMethod.IsDefined(typeof(HPTranspilerAttribute), false);
if (isPrefix) fixTypeCount++;
if (isPostfix) fixTypeCount++;
if (isTranspiler) fixTypeCount++;
if (fixTypeCount == 0) return;
if (fixTypeCount > 1)
throw new InvalidOperationException("Method cannot have more than one fix type attribute");
Type targetType;
if (ReferenceEquals(hpHelper.TargetType, null))
{
targetType = AccessTools.TypeByName(hpHelper.TargetTypeName);
}
else
{
targetType = hpHelper.TargetType;
}
if (ReferenceEquals(targetType, null))
throw new ArgumentException($"Could not find type: {hpHelper.TargetTypeName ?? "null"}");
Type[] parameterTypes = hpHelper.ParameterTypes;
if (parameterTypes == null)
{
if (hpHelper.TargetUseNoParameter)
{
parameterTypes = new Type[0];
}
else
{
parameterTypes = GetParameterTypes(targetType, hpHelper.MethodName);
}
}
MethodBase originalMethod = targetType.GetMethod(hpHelper.MethodName, AccessTools.all, null, parameterTypes, null)
?? throw new MissingMethodException($"Method {hpHelper.MethodName} not found in {targetType}");
var harmonyMethod = new HarmonyMethod(patchMethod);
harmony.Patch(
original: originalMethod,
prefix: isPrefix ? harmonyMethod : null,
postfix: isPostfix ? harmonyMethod : null,
transpiler: isTranspiler ? harmonyMethod : null
);
}
catch (Exception ex)
{
LogError($"Error processing method {patchMethod.Name}: " + ex.ToString());
}
}
public static Type[] GetParameterTypes(Type type, string methodName)
{
MethodInfo[] methods = type.GetMethods(AccessTools.all);
List<MethodInfo> methodList = new List<MethodInfo>();
foreach (var method in methods)
{
if (method.Name == methodName)
{
methodList.Add(method);
}
}
methods = methodList.ToArray();
if (methods.Length == 0)
{
throw new ArgumentException($"The method '{methodName}' does not exist in type '{type.Name}'.");
}
if (methods.Length > 1)
{
throw new AmbiguousMatchException($"The method '{methodName}' is ambiguous in type '{type.Name}'.");
}
ParameterInfo[] parameters = methods[0].GetParameters();
List<Type> parameterList = new List<Type>();
foreach (var parameter in parameters)
{
parameterList.Add(parameter.ParameterType);
}
return parameterList.ToArray();
}
}
}