forked from NaOrganization/UniResolver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNaResolver.h
630 lines (556 loc) · 24.6 KB
/
NaResolver.h
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
//**************************************//
// Hi NaResolver //
// Author: MidTerm //
// Version: v1.6.4 //
// License: MIT //
//**************************************//
#if !_HAS_CXX17
#error "The contents of NaResolver are available only with C++17 or later."
#else
#undef GetClassName
#pragma once
#include <string>
#include <vector>
#include <unordered_map>
#include <codecvt>
#if defined(__NARESOLVER_ONLY_API)
typedef void Il2CppDomain;
typedef void Il2CppAssembly;
typedef void Il2CppClass;
typedef void Il2CppThread;
typedef void Il2CppImage;
typedef void Il2CppString;
typedef void MethodInfo;
typedef void Il2CppType;
typedef void FieldInfo;
typedef void* Il2CppMethodPointer;
typedef wchar_t Il2CppChar;
#endif
class Il2CppManager
{
public:
HMODULE assemblyModule = 0;
std::unordered_map<std::string, void*> il2cppMethodMap =
{
{"il2cpp_domain_get", nullptr},
{"il2cpp_domain_assembly_open", nullptr},
{"il2cpp_domain_get_assemblies", nullptr},
{"il2cpp_type_get_name", nullptr},
{"il2cpp_type_is_byref", nullptr},
{"il2cpp_type_get_attrs", nullptr},
{"il2cpp_type_get_class_or_element_class", nullptr},
{"il2cpp_thread_attach", nullptr},
{"il2cpp_thread_detach", nullptr},
{"il2cpp_string_new", nullptr},
{"il2cpp_string_chars", nullptr},
{"il2cpp_string_length", nullptr},
{"il2cpp_gc_disable", nullptr},
{"il2cpp_class_get_methods", nullptr},
{"il2cpp_class_from_name", nullptr},
{"il2cpp_class_get_name", nullptr},
{"il2cpp_class_get_namespace", nullptr},
{"il2cpp_class_get_image", nullptr},
{"il2cpp_class_get_fields", nullptr},
{"il2cpp_class_get_parent", nullptr},
{"il2cpp_class_get_nested_types", nullptr},
{"il2cpp_class_get_flags", nullptr},
{"il2cpp_class_is_generic", nullptr},
{"il2cpp_class_is_enum", nullptr},
{"il2cpp_class_is_valuetype", nullptr},
{"il2cpp_class_from_il2cpp_type", nullptr},
{"il2cpp_class_get_type", nullptr},
{"il2cpp_class_get_element_class", nullptr},
{"il2cpp_assembly_get_image", nullptr},
{"il2cpp_method_get_name", nullptr},
{"il2cpp_method_get_return_type", nullptr},
{"il2cpp_method_get_param_count", nullptr},
{"il2cpp_method_get_param", nullptr},
{"il2cpp_method_get_flags", nullptr},
{"il2cpp_method_get_param_name", nullptr},
{"il2cpp_image_get_assembly", nullptr},
{"il2cpp_image_get_class_count", nullptr},
{"il2cpp_image_get_class", nullptr},
{"il2cpp_field_get_name", nullptr},
{"il2cpp_field_get_flags", nullptr},
{"il2cpp_field_get_type", nullptr},
{"il2cpp_field_get_offset", nullptr},
{"il2cpp_field_static_get_value", nullptr},
};
inline void ImportMethods()
{
assemblyModule = GetModuleHandleW(L"GameAssembly.dll");
if (!assemblyModule)
{
throw std::exception("Failed to get GameAssembly.dll module handle");
}
for (auto& m : il2cppMethodMap)
{
m.second = GetProcAddress(assemblyModule, m.first.c_str());
if (!m.second)
{
throw std::exception(("Failed to get " + m.first + " function address").c_str());
}
}
}
inline void SetMapMethodAddress(std::string method, void* newAddress)
{
if (il2cppMethodMap.find(method) == il2cppMethodMap.end())
return;
il2cppMethodMap[method] = newAddress;
}
inline Il2CppDomain* GetDomain() { return ((Il2CppDomain * (*)(void)) il2cppMethodMap["il2cpp_domain_get"])(); }
inline const Il2CppAssembly* OpenDomainAssembly(Il2CppDomain* domain, const char* name) { return ((const Il2CppAssembly * (*)(Il2CppDomain*, const char*)) il2cppMethodMap["il2cpp_domain_assembly_open"])(domain, name); }
inline const Il2CppAssembly** GetAssemblies(const Il2CppDomain* domain, size_t* size) { return ((const Il2CppAssembly * *(*)(const Il2CppDomain*, size_t*)) il2cppMethodMap["il2cpp_domain_get_assemblies"])(domain, size); }
inline char* GetTypeName(const Il2CppType* type) { return ((char* (*)(const Il2CppType*))il2cppMethodMap["il2cpp_type_get_name"])(type); }
inline bool TypeIsByref(const Il2CppType* type) { return ((bool (*)(const Il2CppType*))il2cppMethodMap["il2cpp_type_is_byref"])(type); }
inline uint32_t GetTypeAttrs(const Il2CppType* type) { return ((uint32_t(*)(const Il2CppType*))il2cppMethodMap["il2cpp_type_get_attrs"])(type); }
inline Il2CppClass* GetTypeClassOrElementClass(const Il2CppType* type) { return ((Il2CppClass * (*)(const Il2CppType*)) il2cppMethodMap["il2cpp_type_get_class_or_element_class"])(type); }
inline Il2CppThread* AttachThread(Il2CppDomain* domain) { return ((Il2CppThread * (*)(Il2CppDomain*)) il2cppMethodMap["il2cpp_thread_attach"])(domain); }
inline void DetachThread(Il2CppThread* thread) { ((void (*)(Il2CppThread*))il2cppMethodMap["il2cpp_thread_detach"])(thread); }
inline Il2CppString* NewString(const char* string) { return ((Il2CppString * (*)(const char*)) il2cppMethodMap["il2cpp_string_new"])(string); }
inline Il2CppChar* GetChars(Il2CppString* string) { return ((Il2CppChar * (*)(Il2CppString*)) il2cppMethodMap["il2cpp_string_chars"])(string); }
inline int32_t GetStringLength(Il2CppString* string) { return ((int32_t(*)(Il2CppString*))il2cppMethodMap["il2cpp_string_length"])(string); }
inline void DisableGC() { ((void (*)(void))il2cppMethodMap["il2cpp_gc_disable"])(); }
inline const MethodInfo* GetClassMethods(Il2CppClass* klass, void** iter) { return ((const MethodInfo * (*)(Il2CppClass*, void**))il2cppMethodMap["il2cpp_class_get_methods"])(klass, iter); }
inline Il2CppClass* GetClassFromName(const Il2CppImage* image, const char* nameSpace, const char* name) { return ((Il2CppClass * (*)(const Il2CppImage*, const char*, const char*)) il2cppMethodMap["il2cpp_class_from_name"])(image, nameSpace, name); }
inline const char* GetClassName(Il2CppClass* klass) { return ((const char* (*)(Il2CppClass*))il2cppMethodMap["il2cpp_class_get_name"])(klass); }
inline const char* GetClassNamespace(Il2CppClass* klass) { return ((const char* (*)(Il2CppClass*))il2cppMethodMap["il2cpp_class_get_namespace"])(klass); }
inline const Il2CppImage* GetClassImage(Il2CppClass* klass) { return ((const Il2CppImage * (*)(Il2CppClass*))il2cppMethodMap["il2cpp_class_get_image"])(klass); }
inline FieldInfo* GetClassFields(Il2CppClass* klass, void** iter) { return ((FieldInfo * (*)(Il2CppClass*, void**)) il2cppMethodMap["il2cpp_class_get_fields"])(klass, iter); }
inline Il2CppClass* GetClassParent(Il2CppClass* klass) { return ((Il2CppClass * (*)(Il2CppClass*)) il2cppMethodMap["il2cpp_class_get_parent"])(klass); }
inline Il2CppType* GetClassNestedTypes(Il2CppClass* klass, void** iter) { return ((Il2CppType * (*)(Il2CppClass*, void**)) il2cppMethodMap["il2cpp_class_get_nested_types"])(klass, iter); }
inline int GetClassFlags(const Il2CppClass* klass) { return ((int (*)(const Il2CppClass*))il2cppMethodMap["il2cpp_class_get_flags"])(klass); }
inline bool ClassIsGeneric(const Il2CppClass* klass) { return ((bool (*)(const Il2CppClass*))il2cppMethodMap["il2cpp_class_is_generic"])(klass); }
inline bool ClassIsEnum(const Il2CppClass* klass) { return ((bool (*)(const Il2CppClass*))il2cppMethodMap["il2cpp_class_is_enum"])(klass); }
inline bool ClassIsValueType(const Il2CppClass* klass) { return ((bool (*)(const Il2CppClass*))il2cppMethodMap["il2cpp_class_is_valuetype"])(klass); }
inline Il2CppClass* GetClassFromType(const Il2CppType* type) { return ((Il2CppClass * (*)(const Il2CppType*))il2cppMethodMap["il2cpp_class_from_il2cpp_type"])(type); }
inline const Il2CppType* GetClassType(const Il2CppClass* klass) { return ((const Il2CppType * (*)(const Il2CppClass*))il2cppMethodMap["il2cpp_class_get_type"])(klass); }
inline Il2CppClass* GetClassElementClass(Il2CppClass* klass) { return ((Il2CppClass * (*)(Il2CppClass*))il2cppMethodMap["il2cpp_class_get_element_class"])(klass); }
inline const Il2CppImage* GetAssemblyImage(const Il2CppAssembly* assembly) { return ((const Il2CppImage * (*)(const Il2CppAssembly*))il2cppMethodMap["il2cpp_assembly_get_image"])(assembly); }
inline const char* GetMethodName(const MethodInfo* method) { return ((const char* (*)(const MethodInfo*))il2cppMethodMap["il2cpp_method_get_name"])(method); }
inline const Il2CppType* GetMethodReturnType(const MethodInfo* method) { return ((const Il2CppType * (*)(const MethodInfo*))il2cppMethodMap["il2cpp_method_get_return_type"])(method); }
inline uint32_t GetMethodParamCount(const MethodInfo* method) { return ((uint32_t(*)(const MethodInfo*))il2cppMethodMap["il2cpp_method_get_param_count"])(method); }
inline const Il2CppType* GetMethodParam(const MethodInfo* method, uint32_t index) { return ((const Il2CppType * (*)(const MethodInfo*, uint32_t))il2cppMethodMap["il2cpp_method_get_param"])(method, index); }
inline uint32_t GetMethodFlags(const MethodInfo* method, uint32_t* iflags) { return ((uint32_t(*)(const MethodInfo*, uint32_t*))il2cppMethodMap["il2cpp_method_get_flags"])(method, iflags); }
inline const char* GetMethodParamName(const MethodInfo* method, uint32_t index) { return ((const char* (*)(const MethodInfo*, uint32_t))il2cppMethodMap["il2cpp_method_get_param_name"])(method, index); }
inline const Il2CppAssembly* GetImageAssembly(const Il2CppImage* image) { return ((const Il2CppAssembly * (*)(const Il2CppImage*))il2cppMethodMap["il2cpp_image_get_assembly"])(image); }
inline size_t GetImageClassCount(const Il2CppImage* image) { return ((size_t(*)(const Il2CppImage*))il2cppMethodMap["il2cpp_image_get_class_count"])(image); }
inline const Il2CppClass* GetImageClass(const Il2CppImage* image, size_t index) { return ((const Il2CppClass * (*)(const Il2CppImage*, size_t))il2cppMethodMap["il2cpp_image_get_class"])(image, index); }
inline const char* GetFieldName(FieldInfo* field) { return ((const char* (*)(FieldInfo*))il2cppMethodMap["il2cpp_field_get_name"])(field); }
inline int GetFieldFlags(FieldInfo* field) { return ((int (*)(FieldInfo*))il2cppMethodMap["il2cpp_field_get_flags"])(field); }
inline const Il2CppType* GetFieldType(FieldInfo* field) { return ((const Il2CppType * (*)(FieldInfo*))il2cppMethodMap["il2cpp_field_get_type"])(field); }
inline size_t GetFieldOffset(FieldInfo* field) { return ((size_t(*)(FieldInfo*))il2cppMethodMap["il2cpp_field_get_offset"])(field); }
inline void StaticFieldGetValue(FieldInfo* field, void* value) { ((void (*)(FieldInfo*, void*))il2cppMethodMap["il2cpp_field_static_get_value"])(field, value); }
};
namespace Signature
{
namespace Class
{
inline std::string Create(std::string assembly, std::string nameSpace, std::string name)
{
return std::string("(") + assembly + ")" + nameSpace + (nameSpace.empty() ? "" : ".") + name;
}
inline std::string Create(Il2CppClass* klass, Il2CppManager il2CppManager)
{
struct AssemblyStructure
{
char __pad__[sizeof(void*) + sizeof(uint32_t) + sizeof(int32_t) + sizeof(int32_t)];
const char* name;
};
const Il2CppImage* image = il2CppManager.GetClassImage(klass);
if (!image)
return std::string();
const AssemblyStructure* assembly = (AssemblyStructure*)il2CppManager.GetImageAssembly(image);
if (!assembly)
return std::string();
return Create(assembly->name, il2CppManager.GetClassNamespace(klass), il2CppManager.GetClassName(klass));
}
inline void Analysis(std::string signature, std::string& assembly, std::string& nameSpace, std::string& name)
{
assembly = signature.substr(signature.find("(") + 1, signature.find(")") - signature.find("(") - 1);
signature = signature.substr(signature.find(")") + 1);
nameSpace = signature.find(".") == std::string::npos ? "" : signature.substr(0, signature.rfind("."));
name = signature.substr(signature.rfind(".") + 1);
}
}
namespace Method
{
inline void Analysis(std::string signature, std::string& returnKlass, std::string& name, std::vector<std::string>& parameters)
{
returnKlass = signature.substr(0, signature.find(" "));
signature = signature.substr(signature.find(" ") + 1);
name = signature.substr(0, signature.find("("));
signature = signature.substr(signature.find("(") + 1);
if (signature.find(")") == std::string::npos)
return;
signature = signature.substr(0, signature.find(")"));
if (signature.find("MPA_") != std::string::npos)
{
int index = std::stoi(signature.substr(signature.find("MPA_") + 4));
for (int i = 0; i < index; i++)
{
parameters.push_back("AUTO");
}
return;
}
if (signature.size() <= 0)
{
parameters = std::vector<std::string>();
return;
}
while (signature.find(",") != std::string::npos)
{
parameters.push_back(signature.substr(0, signature.find(",")));
signature = signature.substr(signature.find(",") + 2);
}
parameters.push_back(signature);
}
inline std::string Create(const MethodInfo* method, Il2CppManager il2CppManager)
{
std::string signature = il2CppManager.GetTypeName(il2CppManager.GetMethodReturnType(method)) + std::string(" ") + il2CppManager.GetMethodName(method) + "(";
int paramCount = il2CppManager.GetMethodParamCount(method);
for (int i = 0; i < paramCount; i++)
{
signature += il2CppManager.GetTypeName(il2CppManager.GetMethodParam(method, i)) + std::string(", ");
}
if (paramCount > 0)
{
signature = signature.substr(0, signature.size() - 2);
}
return signature + ")";
}
}
}
namespace ConfusedTranslate
{
struct Klass
{
std::string assembly;
std::string nameSpace;
std::string originalName;
std::string confusedName;
};
struct Method
{
Klass klass;
std::string originalName;
std::string confusedName;
};
inline std::vector<Klass> klass = std::vector<Klass>();
inline std::vector<Method> method = std::vector<Method>();
inline std::string RestoreKlass(std::string signature)
{
std::string assembly, nameSpace, name;
Signature::Class::Analysis(signature, assembly, nameSpace, name);
for (auto& k : klass)
{
if (k.assembly.compare(assembly) == 0 && k.nameSpace.compare(nameSpace) == 0 && k.originalName.compare(name) == 0)
return Signature::Class::Create(k.assembly, k.nameSpace, k.confusedName);
}
return signature;
}
inline std::string RestoreMethod(std::string klassSignature, std::string methodName)
{
std::string assembly, nameSpace, name;
Signature::Class::Analysis(klassSignature, assembly, nameSpace, name);
for (auto& m : method)
{
if (m.klass.assembly.compare(assembly) == 0 && m.klass.nameSpace.compare(nameSpace) == 0 && (m.klass.confusedName.compare(name) == 0 || m.klass.originalName.compare(name) == 0) && m.originalName.compare(methodName) == 0)
return m.confusedName;
}
return methodName;
}
}
class NaResolver
{
public:
struct Config
{
bool disableGC = false;
bool enableLogger = false;
bool attachedThread = true;
struct LoggerConfig
{
void (*fatal)(std::string, ...);
void (*info)(std::string, ...);
void (*debug)(std::string, ...);
void (*error)(std::string, ...);
} logger;
};
Il2CppManager il2CppManager;
Il2CppDomain* domain;
Il2CppThread* attachedThread;
std::unordered_map<std::string, const Il2CppAssembly*> assemblies;
std::unordered_map<std::string, std::unordered_map<std::string, std::unordered_map<std::string, Il2CppClass*>>> classes;
inline NaResolver();
inline bool Setup(Config config = NaResolver::Config());
inline void Destroy();
inline Il2CppClass* GetClassEx(std::string assembly, std::string nameSpace, std::string name);
inline Il2CppClass* GetClass(std::string signature);
inline Il2CppMethodPointer GetMethod(Il2CppClass* klass, std::string signature);
inline FieldInfo* GetField(Il2CppClass* klass, std::string name);
inline const Il2CppType* GetType(std::string signature);
inline const Il2CppType* GetType(Il2CppClass* klass);
inline std::string StringConvert(Il2CppString* string);
private:
inline bool ClassExistsInCache(std::string assembly, std::string nameSpace, std::string signature);
inline const Il2CppAssembly* GetAssembly(std::string name);
inline bool MethodVerifyParams(const MethodInfo* method, std::vector<std::string> parameters);
void (*LogFatal)(std::string, ...);
void (*LogInfo)(std::string, ...);
void (*LogDebug)(std::string, ...);
void (*LogError)(std::string, ...);
};
inline NaResolver* Il2CppResolver = new NaResolver();
NaResolver::NaResolver()
{
domain = nullptr;
assemblies = std::unordered_map<std::string, const Il2CppAssembly*>();
classes = std::unordered_map<std::string, std::unordered_map<std::string, std::unordered_map<std::string, Il2CppClass*>>>();
}
inline bool NaResolver::Setup(Config config)
{
LogFatal = config.logger.fatal;
LogError = config.logger.error;
LogInfo = config.logger.info;
LogDebug = config.logger.debug;
if (!config.enableLogger)
{
LogFatal = LogInfo = LogDebug = LogError = (decltype(LogFatal))([](std::string, ...) -> void {});
}
try
{
il2CppManager.ImportMethods();
}
catch (const std::exception& e)
{
LogFatal("[NaResolver] Failed to import il2cpp methods: %s", e.what());
return false;
}
if (domain = il2CppManager.GetDomain(), domain == nullptr)
{
LogFatal("[NaResolver] Domain get failed.");
return false;
}
if (config.attachedThread)
{
attachedThread = il2CppManager.AttachThread(domain);
}
if (config.disableGC)
{
il2CppManager.DisableGC();
}
LogInfo("[NaResolver] Setup success.");
return true;
}
inline void NaResolver::Destroy()
{
if (attachedThread != nullptr)
{
il2CppManager.DetachThread(attachedThread);
attachedThread = nullptr;
}
domain = nullptr;
assemblies.clear();
classes.clear();
LogInfo("[NaResolver] Destroy success.");
}
inline Il2CppClass* NaResolver::GetClassEx(std::string _assembly, std::string _nameSpace, std::string _name)
{
std::string assembly = _assembly, nameSpace = _nameSpace, name = _name;
std::string signature = Signature::Class::Create(assembly, nameSpace, name);
if (nameSpace.compare("") == 0)
{
nameSpace = "__NO_NAMESPACE__";
}
if (ClassExistsInCache(assembly, nameSpace, signature))
{
return classes[assembly][nameSpace][signature];
}
Signature::Class::Analysis(ConfusedTranslate::RestoreKlass(signature), assembly, nameSpace, name);
const Il2CppAssembly* pAssembly = GetAssembly(assembly);
if (pAssembly == nullptr)
{
LogError("[NaResolver] Get assembly failed (%s).", assembly.c_str());
return nullptr;
}
const Il2CppImage* pImage = il2CppManager.GetAssemblyImage(pAssembly);
if (!pImage)
{
LogError("[NaResolver] pImage is null for %s.", signature.c_str());
return nullptr;
}
Il2CppClass* pClass = il2CppManager.GetClassFromName(pImage, nameSpace.c_str(), name.c_str());
if (!pClass)
{
LogError("[NaResolver] pClass is null for %s.", signature.c_str());
return nullptr;
}
if (classes.find(assembly) == classes.end())
{
classes.insert(std::make_pair(assembly, std::unordered_map<std::string, std::unordered_map<std::string, Il2CppClass*>>()));
}
if (nameSpace.compare("") == 0)
{
nameSpace = "__NO_NAMESPACE__";
}
if (classes[assembly].find(nameSpace) == classes[assembly].end())
{
classes[assembly].insert(std::make_pair(nameSpace, std::unordered_map<std::string, Il2CppClass*>()));
}
classes[assembly][nameSpace].insert(std::make_pair(signature, pClass));
LogInfo("[NaResolver] Find class: %s", signature.c_str());
return pClass;
}
inline Il2CppClass* NaResolver::GetClass(std::string signature)
{
std::string assembly, nameSpace, name;
Signature::Class::Analysis(ConfusedTranslate::RestoreKlass(signature), assembly, nameSpace, name);
return GetClassEx(assembly, nameSpace, name);
}
inline Il2CppMethodPointer NaResolver::GetMethod(Il2CppClass* klass, std::string signature)
{
if (klass == nullptr)
{
return nullptr;
}
std::string name = "";
std::string returnType = "";
std::vector<std::string> parameters = std::vector<std::string>();
Signature::Method::Analysis(signature, returnType, name, parameters);
name = ConfusedTranslate::RestoreMethod(Signature::Class::Create(klass, il2CppManager), name);
void* iterator = nullptr;
const MethodInfo* method = nullptr;
while ((method = il2CppManager.GetClassMethods(klass, &iterator)) != nullptr)
{
std::string methodName = il2CppManager.GetMethodName(method);
if (methodName.compare(name) != 0 && name.compare("AUTO") != 0)
continue;
std::string returnTypeName = il2CppManager.GetTypeName(il2CppManager.GetMethodReturnType(method));
if (returnTypeName.compare(returnType) != 0 && returnType.compare("AUTO") != 0)
continue;
if (!MethodVerifyParams(method, parameters))
continue;
LogInfo("[NaResolver] Find method: %s", signature.c_str());
#if defined(__NARESOLVER_ONLY_API)
return *(void**)method;
#else
return method->methodPointer;
#endif
}
LogFatal("[NaResolver] Could not find the method: %s", signature.c_str());
return nullptr;
}
inline FieldInfo* NaResolver::GetField(Il2CppClass* klass, std::string name)
{
if (klass == nullptr)
{
return nullptr;
}
void* iterator = nullptr;
FieldInfo* field = nullptr;
while ((field = il2CppManager.GetClassFields(klass, &iterator)) != nullptr)
{
std::string fieldName = il2CppManager.GetFieldName(field);
if (fieldName.compare(name) != 0)
continue;
LogInfo("[NaResolver] Find field: %s", name.c_str());
return const_cast<FieldInfo*>(field);
}
}
inline const Il2CppType* NaResolver::GetType(std::string signature)
{
std::string assembly, nameSpace, name;
Signature::Class::Analysis(ConfusedTranslate::RestoreKlass(signature), assembly, nameSpace, name);
Il2CppClass* klass = GetClassEx(assembly, nameSpace, name);
if (klass == nullptr)
{
return nullptr;
}
return il2CppManager.GetClassType(klass);
}
inline const Il2CppType* NaResolver::GetType(Il2CppClass* klass)
{
if (klass == nullptr)
{
return nullptr;
}
return il2CppManager.GetClassType(klass);
}
inline std::string NaResolver::StringConvert(Il2CppString* string)
{
if (!string)
{
return std::string();
}
char16_t* chars = (char16_t*)il2CppManager.GetChars(string);
int len = (int)il2CppManager.GetStringLength(string);
int size = WideCharToMultiByte(CP_UTF8, 0, (LPCWCH)chars, len, NULL, 0, NULL, NULL);
char* buffer = new char[size + 1];
WideCharToMultiByte(CP_UTF8, 0, (LPCWCH)chars, len, buffer, size, NULL, NULL);
buffer[size] = '\0';
std::string result = std::string(buffer);
delete[] buffer;
return result;
}
inline bool NaResolver::ClassExistsInCache(std::string assembly, std::string nameSpace, std::string signature)
{
if (classes.find(assembly) == classes.end())
{
return false;
}
if (classes[assembly].find(nameSpace) == classes[assembly].end())
{
return false;
}
return classes[assembly][nameSpace].find(signature) != classes[assembly][nameSpace].end();
}
inline const Il2CppAssembly* NaResolver::GetAssembly(std::string name)
{
if (assemblies.find(name) != assemblies.end())
{
return assemblies[name];
}
const Il2CppAssembly* assembly = il2CppManager.OpenDomainAssembly(domain, name.c_str());
if (!assembly)
{
return nullptr;
}
assemblies[name] = assembly;
return assembly;
}
inline bool NaResolver::MethodVerifyParams(const MethodInfo* method, std::vector<std::string> parameters)
{
uint32_t methodParamCount = il2CppManager.GetMethodParamCount(method);
if (methodParamCount != parameters.size())
{
return false;
}
for (uint32_t j = 0; j < methodParamCount; j++)
{
if (parameters[j].compare("AUTO") == 0)
{
continue;
}
std::string parameterName = il2CppManager.GetTypeName(il2CppManager.GetMethodParam(method, j));
if (parameterName.compare(parameters[j]) != 0)
{
return false;
}
}
return true;
}
#define STATIC_AREA_OFFSET (sizeof(void *) == 8 ? 0xB8 : 0xC5)
#define CLASS(assembly, namespaze, klass) \
static struct Il2CppClass *ThisClass() { return Il2CppResolver->GetClassEx(assembly, namespaze, klass); }
#define MEMBER(klass, name, offset) \
struct \
{ \
char __pad__##name[offset]; \
klass name; \
}
#define STATIC_MEMBER(klass, name, offset) \
static klass get_##name() { return *reinterpret_cast<klass *>(*reinterpret_cast<uintptr_t *>((uintptr_t)ThisClass() + STATIC_AREA_OFFSET) + offset); } \
static void set_##name(klass value) { *reinterpret_cast<klass *>(*reinterpret_cast<uintptr_t *>((uintptr_t)ThisClass() + STATIC_AREA_OFFSET) + offset) = value; }
#define METHOD(returnType, parameters, signature) static auto function = (returnType(*) parameters)(Il2CppResolver->GetMethod(ThisClass(), signature));
#endif