forked from focus-creative-games/il2cpp_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethod.cpp
More file actions
395 lines (323 loc) · 12.3 KB
/
Method.cpp
File metadata and controls
395 lines (323 loc) · 12.3 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include "il2cpp-config.h"
#include "vm/Method.h"
#include "il2cpp-tabledefs.h"
#include "il2cpp-class-internals.h"
#include "vm/Class.h"
#include "vm/MetadataCache.h"
#include "vm/Object.h"
#include "vm/Reflection.h"
#include "vm/Runtime.h"
#include "vm/Type.h"
namespace il2cpp
{
namespace vm
{
const Il2CppType* Method::GetReturnType(const MethodInfo* method)
{
return method->return_type;
}
Il2CppClass *Method::GetDeclaringType(const MethodInfo* method)
{
return method->klass;
}
const char* Method::GetName(const MethodInfo *method)
{
return method->name;
}
std::string Method::GetNameWithGenericTypes(const MethodInfo* method)
{
std::string str;
str += method->name;
if (method->is_inflated && method->genericMethod->context.method_inst)
{
const Il2CppGenericInst *inst = method->genericMethod->context.method_inst;
str += '<';
for (unsigned int i = 0; i < inst->type_argc; ++i)
{
str += Type::GetName(inst->type_argv[i], IL2CPP_TYPE_NAME_FORMAT_FULL_NAME);
if (i < inst->type_argc - 1)
str += ",";
}
str += '>';
}
return str;
}
bool Method::IsGeneric(const MethodInfo *method)
{
return method->is_generic;
}
bool Method::IsInflated(const MethodInfo *method)
{
return method->is_inflated;
}
bool Method::IsGenericInstance(const MethodInfo *method)
{
return method->is_inflated && !method->is_generic;
}
bool Method::IsGenericInstanceMethod(const MethodInfo *method)
{
return method->is_inflated && !method->is_generic && method->genericMethod->context.method_inst;
}
bool Method::IsDefaultInterfaceMethodOnGenericInstance(const MethodInfo* method)
{
return method->methodPointer && Class::IsInterface(method->klass) && Class::IsInflated(method->klass) && !method->klass->is_import_or_windows_runtime;
}
bool Method::IsInstance(const MethodInfo *method)
{
return !(method->flags & METHOD_ATTRIBUTE_STATIC);
}
uint32_t Method::GetParamCount(const MethodInfo *method)
{
return method->parameters_count;
}
uint32_t Method::GetGenericParamCount(const MethodInfo *method)
{
if (IsGeneric(method) && method->genericContainerHandle != NULL)
return MetadataCache::GetGenericContainerCount(method->genericContainerHandle);
return 0;
}
const Il2CppType* Method::GetParam(const MethodInfo *method, uint32_t index)
{
if (index < method->parameters_count)
return method->parameters[index];
else
return NULL;
}
const char* Method::GetParamName(const MethodInfo *method, uint32_t index)
{
IL2CPP_ASSERT(method != NULL && "Method::GetParamName cannot be invoked with a NULL MethodInfo.");
if (index >= method->parameters_count)
return NULL;
if (method->is_inflated)
{
method = il2cpp::vm::MetadataCache::GetGenericMethodDefinition(method);
}
// we construct some 'pseudo' methods for things like arrays
if (!method->methodMetadataHandle)
return NULL;
Il2CppMetadataParameterInfo paramInfo = MetadataCache::GetParameterInfo(method->klass, method->methodMetadataHandle, index);
return paramInfo.name;
}
Il2CppClass* Method::GetClass(const MethodInfo *method)
{
return method->klass;
}
bool Method::HasAttribute(const MethodInfo *method, Il2CppClass *attr_class)
{
return Reflection::HasAttribute(method, attr_class);
}
uint32_t Method::GetImplementationFlags(const MethodInfo *method)
{
return method->iflags;
}
uint32_t Method::GetFlags(const MethodInfo *method)
{
return method->flags;
}
uint32_t Method::GetToken(const MethodInfo *method)
{
return method->token;
}
// From ECMA-335, I.10.2 Overloading
// Methods and properties can be overloaded by:
// * Number of parameters
// * Type of any parameter
// * Calling convention <------ not stored in our metadata yet
// * Custom modifiers <------ not supported by il2cpp
// * Whether a parameter is passed by value or by reference
static bool AreParametersSame(const Il2CppType** params1, const Il2CppType** params2, int count)
{
for (int i = 0; i < count; i++)
{
const Il2CppType* param1 = params1[i];
const Il2CppType* param2 = params2[i];
if (param1->byref != param2->byref)
{
return false;
}
if (Class::FromIl2CppType(param1) != Class::FromIl2CppType(param2))
{
return false;
}
}
return true;
}
static int CompareParameters(const Il2CppType** params1, const Il2CppType** params2, int count)
{
for (int i = 0; i < count; i++)
{
const Il2CppType* param1 = params1[i];
const Il2CppType* param2 = params2[i];
if (param1->byref == param2->byref)
{
return Class::FromIl2CppType(param1) < Class::FromIl2CppType(param2);
}
return param1->byref < param2->byref;
}
return true;
}
bool Method::IsSameOverloadSignature(const MethodInfo* method1, const MethodInfo* method2)
{
if (method1->parameters_count != method2->parameters_count)
{
return false;
}
return AreParametersSame(method1->parameters, method2->parameters, method1->parameters_count);
}
bool Method::IsSameOverloadSignature(const PropertyInfo* property1, const PropertyInfo* property2)
{
uint8_t parameterCount1, parameterCount2;
const Il2CppType** parameters1;
const Il2CppType** parameters2;
if (property1->get != NULL)
{
parameterCount1 = property1->get->parameters_count;
parameters1 = property1->get->parameters;
}
else
{
// In set method, value is the last parameter, so we just don't care about it
parameterCount1 = property1->set->parameters_count - 1;
parameters1 = property1->set->parameters;
}
if (property2->get != NULL)
{
parameterCount2 = property2->get->parameters_count;
parameters2 = property2->get->parameters;
}
else
{
parameterCount2 = property2->set->parameters_count - 1;
parameters2 = property2->set->parameters;
}
if (parameterCount1 != parameterCount2)
{
return false;
}
return AreParametersSame(parameters1, parameters2, parameterCount1);
}
int Method::CompareOverloadSignature(const PropertyInfo* property1, const PropertyInfo* property2)
{
uint8_t parameterCount1, parameterCount2;
const Il2CppType** parameters1;
const Il2CppType** parameters2;
if (property1->get != NULL)
{
parameterCount1 = property1->get->parameters_count;
parameters1 = property1->get->parameters;
}
else
{
// In set method, value is the last parameter, so we just don't care about it
parameterCount1 = property1->set->parameters_count - 1;
parameters1 = property1->set->parameters;
}
if (property2->get != NULL)
{
parameterCount2 = property2->get->parameters_count;
parameters2 = property2->get->parameters;
}
else
{
parameterCount2 = property2->set->parameters_count - 1;
parameters2 = property2->set->parameters;
}
if (parameterCount1 == parameterCount2)
{
return CompareParameters(parameters1, parameters2, parameterCount1);
}
return parameterCount1 < parameterCount2;
}
const char* Method::GetParameterDefaultValue(const MethodInfo* method, int32_t parameterPosition, const Il2CppType** type, bool* isExplicitySetNullDefaultValue)
{
return reinterpret_cast<const char*>(MetadataCache::GetParameterDefaultValue(method, parameterPosition, type, isExplicitySetNullDefaultValue));
}
uint32_t Method::GetParameterToken(const MethodInfo* method, int32_t index)
{
if (index >= method->parameters_count)
return 0;
if (method->is_inflated)
{
method = il2cpp::vm::MetadataCache::GetGenericMethodDefinition(method);
}
// we construct some 'pseudo' methods for things like arrays
if (!method->methodMetadataHandle)
return 0;
#if SUPPORT_METHOD_RETURN_TYPE_CUSTOM_ATTRIBUTE
// We are looking at the return parameter, which is not stored as a parameter, but the method has its token
if (index == -1)
{
return MetadataCache::GetReturnParameterToken(method->methodMetadataHandle);
}
#endif
Il2CppMetadataParameterInfo paramInfo = MetadataCache::GetParameterInfo(method->klass, method->methodMetadataHandle, index);
return paramInfo.token;
}
std::string Method::GetFullName(const MethodInfo* method)
{
std::string str;
str += Type::GetName(&method->klass->byval_arg, IL2CPP_TYPE_NAME_FORMAT_FULL_NAME);
str += "::";
str += Method::GetNameWithGenericTypes(method);
return str;
}
static void AmbiguousImplementationMethod()
{
il2cpp::vm::Runtime::RaiseAmbiguousImplementationException(NULL);
}
static void AmbiguousImplementationMethodInvoker(Il2CppMethodPointer ptr, const MethodInfo* method, void* obj, void** args, void* ret)
{
il2cpp::vm::Runtime::RaiseAmbiguousImplementationException(method);
}
static void EntryPointNotFoundImplementationMethod()
{
il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetEntryPointNotFoundException(""));
}
static void EntryPointNotFoundMethodInvoker(Il2CppMethodPointer ptr, const MethodInfo* method, void* obj, void** args, void* ret)
{
std::string name = "";
if (method != NULL && method->name != NULL)
name = Method::GetFullName(method);
il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetEntryPointNotFoundException(name.c_str()));
}
const static MethodInfo ambiguousMethodInfo =
{
AmbiguousImplementationMethod, // method_ptr
AmbiguousImplementationMethod, // virtual_method_ptr
AmbiguousImplementationMethodInvoker, // invoker_method
};
const static MethodInfo entryPointNoFoundMethodInfo =
{
EntryPointNotFoundImplementationMethod, // method_ptr
EntryPointNotFoundImplementationMethod, // virtual_method_ptr
EntryPointNotFoundMethodInvoker, // invoker_method
};
const MethodInfo* Method::GetAmbiguousMethodInfo()
{
IL2CPP_ASSERT(ambiguousMethodInfo.methodPointer == AmbiguousImplementationMethod);
IL2CPP_ASSERT(ambiguousMethodInfo.virtualMethodPointer == AmbiguousImplementationMethod);
IL2CPP_ASSERT(ambiguousMethodInfo.invoker_method == AmbiguousImplementationMethodInvoker);
// GenericMethod::GetMethod relies on ambiguousMethodInfo being a singleton
return &ambiguousMethodInfo;
}
const MethodInfo* Method::GetEntryPointNotFoundMethodInfo()
{
IL2CPP_ASSERT(entryPointNoFoundMethodInfo.methodPointer == EntryPointNotFoundImplementationMethod);
IL2CPP_ASSERT(entryPointNoFoundMethodInfo.virtualMethodPointer == EntryPointNotFoundImplementationMethod);
IL2CPP_ASSERT(entryPointNoFoundMethodInfo.invoker_method == EntryPointNotFoundMethodInvoker);
return &entryPointNoFoundMethodInfo;
}
bool Method::IsAmbiguousMethodInfo(const MethodInfo* method)
{
return method == &ambiguousMethodInfo || metadata::GenericMethod::IsGenericAmbiguousMethodInfo(method);
}
bool Method::IsEntryPointNotFoundMethodInfo(const MethodInfo* method)
{
return method == &entryPointNoFoundMethodInfo;
}
bool Method::HasFullGenericSharingSignature(const MethodInfo* method)
{
return method->has_full_generic_sharing_signature;
}
} /* namespace vm */
} /* namespace il2cpp */