-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathMpPatch.cs
211 lines (178 loc) · 6.29 KB
/
MpPatch.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
using HarmonyLib;
using Multiplayer.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using Multiplayer.Client.Util;
using Verse;
namespace Multiplayer.Client
{
/// <summary>
/// Applies a normal Harmony patch, but allows multiple targets
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
[MeansImplicitUse(ImplicitUseTargetFlags.WithMembers)]
public abstract class MpPatch : Attribute
{
private Type type;
private string typeName;
private string methodName;
private Type[] argTypes;
private MethodType methodType;
private int? lambdaOrdinal;
private MethodBase method;
public Type Type
{
get
{
if (type != null)
return type;
type = MpReflection.GetTypeByName(typeName);
if (type == null)
throw new Exception($"Couldn't find type {typeName}");
return type;
}
}
public MethodBase Method
{
get
{
if (method != null)
return method;
if (lambdaOrdinal != null)
return MpMethodUtil.GetLambda(Type, methodName, methodType, argTypes, lambdaOrdinal.Value);
method = MpMethodUtil.GetMethod(Type, methodName, methodType, argTypes);
if (method == null)
throw new MissingMethodException($"Couldn't find method {methodName} in type {Type}");
return method;
}
}
protected MpPatch(Type type, string innerType, string methodName) : this($"{type}+{innerType}", methodName)
{
}
protected MpPatch(string typeName, string methodName)
{
this.typeName = typeName;
this.methodName = methodName;
}
protected MpPatch(Type type, string methodName, Type[] argTypes = null)
{
this.type = type;
this.methodName = methodName;
this.argTypes = argTypes;
}
protected MpPatch(Type type, MethodType methodType, Type[] argTypes = null)
{
this.type = type;
this.methodType = methodType;
this.argTypes = argTypes;
}
protected MpPatch(Type type, string methodName, int lambdaOrdinal)
{
this.type = type;
this.methodName = methodName;
this.lambdaOrdinal = lambdaOrdinal;
}
}
public static class MpPatchExtensions
{
public static void DoAllMpPatches(this Harmony harmony)
{
foreach (Type type in Assembly.GetCallingAssembly().GetTypes())
{
harmony.DoMpPatches(type);
}
}
// Use null as harmony instance to just collect the methods
public static List<MethodBase> DoMpPatches(this Harmony harmony, Type type)
{
List<MethodBase> result = null;
foreach (var m in type.GetDeclaredMethods().Where(m => m.IsStatic))
{
foreach (MpPatch attr in m.AllAttributes<MpPatch>())
{
try
{
MethodBase toPatch = attr.Method;
HarmonyMethod patch = new HarmonyMethod(m);
if (harmony != null) {
harmony.PatchMeasure(
toPatch,
(attr is MpPrefix) ? patch : null,
(attr is MpPostfix) ? patch : null,
(attr is MpTranspiler) ? patch : null
);
}
result ??= new List<MethodBase>();
result.Add(toPatch);
}
catch (Exception e)
{
Log.Error($"MpPatch {m.DeclaringType}.{m.Name} failed with exception: {e}");
Multiplayer.loadingErrors = true;
}
}
}
return result;
}
}
/// <summary>
/// Prefix method attribute
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class MpPrefix : MpPatch
{
public MpPrefix(string typeName, string method) : base(typeName, method)
{
}
public MpPrefix(Type type, string method, Type[] argTypes = null) : base(type, method, argTypes)
{
}
public MpPrefix(Type type, string innerType, string method) : base(type, innerType, method)
{
}
public MpPrefix(Type parentType, string parentMethod, int lambdaOrdinal) : base(parentType, parentMethod, lambdaOrdinal)
{
}
}
/// <summary>
/// Postfix method attribute
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class MpPostfix : MpPatch
{
public MpPostfix(string typeName, string method) : base(typeName, method)
{
}
public MpPostfix(Type type, string method, Type[] argTypes = null) : base(type, method, argTypes)
{
}
public MpPostfix(Type type, string innerType, string method) : base(type, innerType, method)
{
}
public MpPostfix(Type parentType, string parentMethod, int lambdaOrdinal) : base(parentType, parentMethod, lambdaOrdinal)
{
}
}
/// <summary>
/// Transpiler method attribute
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class MpTranspiler : MpPatch
{
public MpTranspiler(string typeName, string method) : base(typeName, method)
{
}
public MpTranspiler(Type type, string method, Type[] argTypes) : base(type, method, argTypes)
{
}
public MpTranspiler(Type type, string innerType, string method) : base(type, innerType, method)
{
}
public MpTranspiler(Type parentType, string parentMethod, int lambdaOrdinal) : base(parentType, parentMethod, lambdaOrdinal)
{
}
}
}