-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Resolvers.cs
155 lines (144 loc) · 5.79 KB
/
Resolvers.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
// all the resolve functions for the weaver
// NOTE: these functions should be made extensions, but right now they still
// make heavy use of Weaver.fail and we'd have to check each one's return
// value for null otherwise.
// (original FieldType.Resolve returns null if not found too, so
// exceptions would be a bit inconsistent here)
using Mono.CecilX;
namespace Mirror.Weaver
{
public static class Resolvers
{
public static MethodReference ResolveMethod(TypeReference tr, AssemblyDefinition scriptDef, string name)
{
//Console.WriteLine("ResolveMethod " + t.ToString () + " " + name);
if (tr == null)
{
Weaver.Error("Type missing for " + name);
return null;
}
foreach (MethodDefinition methodRef in tr.Resolve().Methods)
{
if (methodRef.Name == name)
{
return scriptDef.MainModule.ImportReference(methodRef);
}
}
Weaver.Error($"{tr}.{name}() not found");
return null;
}
// TODO reuse ResolveMethod in here after Weaver.fail was removed
public static MethodReference ResolveMethodInParents(TypeReference tr, AssemblyDefinition scriptDef, string name)
{
if (tr == null)
{
Weaver.Error("Type missing for " + name);
return null;
}
foreach (MethodDefinition methodRef in tr.Resolve().Methods)
{
if (methodRef.Name == name)
{
return scriptDef.MainModule.ImportReference(methodRef);
}
}
// Could not find the method in this class, try the parent
return ResolveMethodInParents(tr.Resolve().BaseType, scriptDef, name);
}
// System.Byte[] arguments need a version with a string
public static MethodReference ResolveMethodWithArg(TypeReference tr, AssemblyDefinition scriptDef, string name, string argTypeFullName)
{
foreach (MethodDefinition methodRef in tr.Resolve().Methods)
{
if (methodRef.Name == name)
{
if (methodRef.Parameters.Count == 1)
{
if (methodRef.Parameters[0].ParameterType.FullName == argTypeFullName)
{
return scriptDef.MainModule.ImportReference(methodRef);
}
}
}
}
Weaver.Error($"{tr}.{name}({argTypeFullName}) not found");
return null;
}
// reuse ResolveMethodWithArg string version
public static MethodReference ResolveMethodWithArg(TypeReference tr, AssemblyDefinition scriptDef, string name, TypeReference argType)
{
return ResolveMethodWithArg(tr, scriptDef, name, argType.FullName);
}
public static MethodDefinition ResolveDefaultPublicCtor(TypeReference variable)
{
foreach (MethodDefinition methodRef in variable.Resolve().Methods)
{
if (methodRef.Name == ".ctor" &&
methodRef.Resolve().IsPublic &&
methodRef.Parameters.Count == 0)
{
return methodRef;
}
}
return null;
}
public static GenericInstanceMethod ResolveMethodGeneric(TypeReference t, AssemblyDefinition scriptDef, string name, TypeReference genericType)
{
foreach (MethodDefinition methodRef in t.Resolve().Methods)
{
if (methodRef.Name == name)
{
if (methodRef.Parameters.Count == 0)
{
if (methodRef.GenericParameters.Count == 1)
{
MethodReference tmp = scriptDef.MainModule.ImportReference(methodRef);
GenericInstanceMethod gm = new GenericInstanceMethod(tmp);
gm.GenericArguments.Add(genericType);
if (gm.GenericArguments[0].FullName == genericType.FullName)
{
return gm;
}
}
}
}
}
Weaver.Error($"{t}.{name}<{genericType}>() not found");
return null;
}
public static MethodReference ResolveMethod(TypeReference t, AssemblyDefinition scriptDef, System.Func<MethodDefinition, bool> predicate)
{
foreach (MethodDefinition methodRef in t.Resolve().Methods)
{
if (predicate(methodRef))
{
return scriptDef.MainModule.ImportReference(methodRef);
}
}
Weaver.Error($"Method not found");
return null;
}
public static FieldReference ResolveField(TypeReference tr, AssemblyDefinition scriptDef, string name)
{
foreach (FieldDefinition fd in tr.Resolve().Fields)
{
if (fd.Name == name)
{
return scriptDef.MainModule.ImportReference(fd);
}
}
return null;
}
public static MethodReference ResolveProperty(TypeReference tr, AssemblyDefinition scriptDef, string name)
{
foreach (PropertyDefinition pd in tr.Resolve().Properties)
{
if (pd.Name == name)
{
return scriptDef.MainModule.ImportReference(pd.GetMethod);
}
}
return null;
}
}
}