forked from ByNameModding/BNM-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass.cpp
More file actions
504 lines (408 loc) · 17.4 KB
/
Copy pathClass.cpp
File metadata and controls
504 lines (408 loc) · 17.4 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
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
#include <BNM/Class.hpp>
#include <BNM/FieldBase.hpp>
#include <BNM/MethodBase.hpp>
#include <BNM/PropertyBase.hpp>
#include <BNM/EventBase.hpp>
#include <BNM/DebugMessages.hpp>
#include <Internals.hpp>
using namespace BNM;
Class::Class(const IL2CPP::Il2CppObject *obj) {
if (!obj) return;
_data = obj->klass;
}
Class::Class(const IL2CPP::Il2CppType *type) {
if (!type) return;
_data = Internal::il2cppMethods.il2cpp_class_from_il2cpp_type(type);
}
Class::Class(const MonoType *type) {
if (!type) return;
_data = Internal::il2cppMethods.il2cpp_class_from_il2cpp_type(type->type);
}
Class::Class(const CompileTimeClass &type) { _data = type; }
static IL2CPP::Il2CppClass *TryGetClassWithoutImage(const std::string_view &_namespace, const std::string_view &_name) {
auto &assemblies = *Internal::Assembly$$GetAllAssemblies();
for (auto assembly : assemblies) {
auto image = Internal::il2cppMethods.il2cpp_assembly_get_image(assembly);
if (auto _data = Internal::TryGetClassInImage(image, _namespace, _name); _data) return _data;
}
return nullptr;
}
Class::Class(const std::string_view &_namespace, const std::string_view &_name) {
if (_data = TryGetClassWithoutImage(_namespace, _name); _data) return;
BNM_LOG_WARN(DBG_BNM_MSG_Class_Constructor_NotFound, _namespace.data(), _name.data());
}
Class::Class(const std::string_view &_namespace, const std::string_view &_name, const BNM::Image &image) {
if (!image) {
BNM_LOG_WARN(DBG_BNM_MSG_Class_Constructor_Image_Warn, image.str().data(), _namespace.data(), _name.data());
_data = nullptr;
return;
}
if (_data = Internal::TryGetClassInImage(image, _namespace, _name); _data) return;
BNM_LOG_WARN(DBG_BNM_MSG_Class_Constructor_Image_NotFound, image.str().data(), _namespace.data(), _name.data());
}
std::vector<Class> Class::GetInnerClasses(bool includeParent) const {
BNM_LOG_ERR_IF(!_data, DBG_BNM_MSG_Class_Dead_Error);
if (!_data) return {};
TryInit();
std::vector<Class> ret{};
auto curClass = _data;
do {
for (uint16_t i = 0; i < curClass->nested_type_count; ++i) ret.emplace_back(curClass->nestedTypes[i]);
if (includeParent) curClass = curClass->parent;
else break;
} while (curClass);
return std::move(ret);
}
std::vector<FieldBase> Class::GetFields(bool includeParent) const {
BNM_LOG_ERR_IF(!_data, DBG_BNM_MSG_Class_Dead_Error);
if (!_data) return {};
TryInit();
std::vector<FieldBase> ret{};
auto curClass = _data;
do {
auto end = curClass->fields + curClass->field_count;
for (IL2CPP::FieldInfo *currentField = curClass->fields; currentField != end; ++currentField) ret.emplace_back(currentField);
if (includeParent) curClass = curClass->parent;
else break;
} while (curClass);
return std::move(ret);
}
std::vector<MethodBase> Class::GetMethods(bool includeParent) const {
BNM_LOG_ERR_IF(!_data, DBG_BNM_MSG_Class_Dead_Error);
if (!_data) return {};
TryInit();
std::vector<MethodBase> ret{};
auto curClass = _data;
do {
for (uint16_t i = 0; i < curClass->method_count; ++i) ret.emplace_back(curClass->methods[i]);
if (includeParent) curClass = curClass->parent;
else break;
} while (curClass);
return std::move(ret);
}
std::vector<PropertyBase> Class::GetProperties(bool includeParent) const {
BNM_LOG_ERR_IF(!_data, DBG_BNM_MSG_Class_Dead_Error);
if (!_data) return {};
TryInit();
std::vector<PropertyBase> ret{};
auto curClass = _data;
do {
auto end = curClass->properties + curClass->property_count;
for (auto currentProperty = curClass->properties; currentProperty != end; ++currentProperty) ret.emplace_back(currentProperty);
if (includeParent) curClass = curClass->parent;
else break;
} while (curClass);
return std::move(ret);
}
std::vector<EventBase> Class::GetEvents(bool includeParent) const {
BNM_LOG_ERR_IF(!_data, DBG_BNM_MSG_Class_Dead_Error);
if (!_data) return {};
TryInit();
std::vector<EventBase> ret{};
auto curClass = _data;
do {
auto end = curClass->events + curClass->event_count;
for (auto currentEvent = curClass->events; currentEvent != end; ++currentEvent) ret.emplace_back(currentEvent);
if (includeParent) curClass = curClass->parent;
else break;
} while (curClass);
return std::move(ret);
}
MethodBase Class::GetMethod(const std::string_view &name, int parameters) const {
BNM_LOG_ERR_IF(!_data, DBG_BNM_MSG_Class_Dead_Error);
if (!_data) return {};
TryInit();
auto method = Internal::IterateMethods(*this, [&name, ¶meters](IL2CPP::MethodInfo *method) {
return name == method->name && (method->parameters_count == parameters || parameters == -1);
});
if (method != nullptr) return method;
BNM_LOG_WARN(DBG_BNM_MSG_Class_GetMethod_Count_NotFound, _data->namespaze, _data->name, name.data(), parameters);
return {};
}
MethodBase Class::GetMethod(const std::string_view &name, const std::initializer_list<std::string_view> ¶metersName) const {
BNM_LOG_ERR_IF(!_data, DBG_BNM_MSG_Class_Dead_Error);
if (!_data) return {};
TryInit();
auto parameters = (uint8_t) parametersName.size();
auto method = Internal::IterateMethods(*this, [&name, ¶meters, ¶metersName](IL2CPP::MethodInfo *method) {
if (name != method->name || method->parameters_count != parameters) return false;
for (uint8_t i = 0; i < parameters; ++i) if (Internal::il2cppMethods.il2cpp_method_get_param_name(method, i) != parametersName.begin()[i]) return false;
return true;
});
if (method != nullptr) return method;
BNM_LOG_WARN(DBG_BNM_MSG_Class_GetMethod_Names_NotFound, _data->namespaze, _data->name, name.data(), parameters);
return {};
}
MethodBase Class::GetMethod(const std::string_view &name, const std::initializer_list<BNM::CompileTimeClass> ¶metersType) const {
BNM_LOG_ERR_IF(!_data, DBG_BNM_MSG_Class_Dead_Error);
if (!_data) return {};
TryInit();
auto parameters = (uint8_t) parametersType.size();
auto method = Internal::IterateMethods(*this, [&name, parameters, ¶metersType](IL2CPP::MethodInfo *method) {
if (name != method->name || method->parameters_count != parameters) return false;
for (uint8_t i = 0; i < parameters; ++i) {
#if UNITY_VER < 212
auto param = (method->parameters + i)->parameter_type;
#else
auto param = method->parameters[i];
#endif
if (Class(param).GetClass() != parametersType.begin()[i].ToIl2CppClass()) return false;
}
return true;
});
if (method != nullptr) return method;
BNM_LOG_WARN(DBG_BNM_MSG_Class_GetMethod_Types_NotFound, _data->namespaze, _data->name, name.data(), parameters);
return {};
}
PropertyBase Class::GetProperty(const std::string_view &name) const {
BNM_LOG_ERR_IF(!_data, DBG_BNM_MSG_Class_Dead_Error);
if (!_data) return {};
TryInit();
auto curClass = _data;
do {
auto end = curClass->properties + curClass->property_count;
for (auto currentProperty = curClass->properties; currentProperty != end; ++currentProperty) if (name == currentProperty->name) return currentProperty;
curClass = curClass->parent;
} while (curClass);
BNM_LOG_WARN(DBG_BNM_MSG_Class_GetProperty_NotFound, str().c_str(), name.data());
return {};
}
Class Class::GetInnerClass(const std::string_view &name) const {
BNM_LOG_ERR_IF(!_data, DBG_BNM_MSG_Class_Dead_Error);
if (!_data) return {};
TryInit();
auto curClass = _data;
do {
for (uint16_t i = 0; i < curClass->nested_type_count; ++i) {
auto cls = curClass->nestedTypes[i];
if (name == cls->name) return cls;
}
curClass = curClass->parent;
} while (curClass);
BNM_LOG_WARN(DBG_BNM_MSG_Class_GetInnerClass_NotFound, _data->namespaze, _data->name, name.data());
return {};
}
FieldBase Class::GetField(const std::string_view &name) const {
BNM_LOG_ERR_IF(!_data, DBG_BNM_MSG_Class_Dead_Error);
if (!_data) return {};
TryInit();
auto curClass = _data;
do {
auto end = curClass->fields + curClass->field_count;
for (auto currentField = curClass->fields; currentField != end; ++currentField) {
if (name != currentField->name) continue;
return currentField;
}
curClass = curClass->parent;
} while (curClass);
BNM_LOG_WARN(DBG_BNM_MSG_Class_GetField_NotFound, _data->namespaze, _data->name, name.data());
return {};
}
BNM::EventBase Class::GetEvent(const std::string_view &name) const {
BNM_LOG_ERR_IF(!_data, DBG_BNM_MSG_Class_Dead_Error);
if (!_data) return {};
TryInit();
auto curClass = _data;
do {
auto end = curClass->events + curClass->event_count;
for (auto currentEvent = curClass->events; currentEvent != end; ++currentEvent) {
if (name != currentEvent->name) continue;
return currentEvent;
}
curClass = curClass->parent;
} while (curClass);
BNM_LOG_WARN(DBG_BNM_MSG_Class_GetEvent_NotFound, _data->namespaze, _data->name, name.data());
return {};
}
Class Class::GetParent() const {
BNM_LOG_ERR_IF(!_data, DBG_BNM_MSG_Class_Dead_Error);
if (!_data) return {};
TryInit();
return _data->parent;
}
Class Class::GetArray() const {
BNM_LOG_ERR_IF(!_data, DBG_BNM_MSG_Class_Dead_Error);
if (!_data) return {};
TryInit();
return Internal::il2cppMethods.il2cpp_array_class_get(_data, 1);
}
Class Class::GetPointer() const {
BNM_LOG_ERR_IF(!_data, DBG_BNM_MSG_Class_Dead_Error);
if (!_data) return {};
TryInit();
return Internal::GetPointer(*this);
}
Class Class::GetReference() const {
BNM_LOG_ERR_IF(!_data, DBG_BNM_MSG_Class_Dead_Error);
if (!_data) return {};
TryInit();
return Internal::GetReference(*this);
}
Class Class::GetGeneric(const std::initializer_list<CompileTimeClass> &templateTypes) const {
BNM_LOG_ERR_IF(!_data, DBG_BNM_MSG_Class_Dead_Error);
if (!_data) return {};
TryInit();
return Internal::TryMakeGenericClass(*this, templateTypes);
}
IL2CPP::Il2CppType *Class::GetIl2CppType() const {
BNM_LOG_ERR_IF(!_data, DBG_BNM_MSG_Class_Dead_Error);
if (!_data) return nullptr;
TryInit();
#if UNITY_VER > 174
return (IL2CPP::Il2CppType *)&_data->byval_arg;
#else
return (IL2CPP::Il2CppType *)_data->byval_arg;
#endif
}
MonoType *Class::GetMonoType() const {
BNM_LOG_ERR_IF(!_data, DBG_BNM_MSG_Class_Dead_Error);
if (!_data) return nullptr;
TryInit();
return (MonoType *) Internal::il2cppMethods.il2cpp_type_get_object(GetIl2CppType());
}
BNM::CompileTimeClass Class::GetCompileTimeClass() const {
BNM_LOG_ERR_IF(!_data, DBG_BNM_MSG_Class_Dead_Error);
TryInit();
return {._loadedClass = *this, ._autoFree = false};
}
Class::operator BNM::CompileTimeClass() const { return GetCompileTimeClass(); }
BNM::IL2CPP::Il2CppObject *Class::CreateNewInstance() const {
BNM_LOG_ERR_IF(!_data, DBG_BNM_MSG_Class_Dead_Error);
if (!_data) return nullptr;
TryInit();
if ((_data->flags & (0x00000080 | 0x00000020))) // TYPE_ATTRIBUTE_ABSTRACT | TYPE_ATTRIBUTE_INTERFACE
BNM_LOG_WARN(DBG_BNM_MSG_Class_CreateNewInstance_Abstract_Warn, str().c_str());
auto obj = Internal::il2cppMethods.il2cpp_object_new(_data);
if (obj) memset((char*)obj + sizeof(IL2CPP::Il2CppObject), 0, _data->instance_size - sizeof(IL2CPP::Il2CppObject));
return (BNM::IL2CPP::Il2CppObject *) obj;
}
// Try initializing the class if it is alive
void Class::TryInit() const { if (_data) Internal::Class$$Init(_data); }
IL2CPP::Il2CppObject *Class::ObjBox(IL2CPP::Il2CppClass *_data, void *data) {
return Internal::il2cppMethods.il2cpp_value_box(_data, data);
}
IL2CPP::Il2CppArray *Class::ArrayNew(IL2CPP::Il2CppClass *cls, IL2CPP::il2cpp_array_size_t length) {
return Internal::il2cppMethods.il2cpp_array_new(cls, length);
}
void *Class::NewListInstance() {
return Internal::customListTemplateClass.CreateNewInstance();
}
Class Class::GetListClass() {
return Internal::vmData.System$$List;
}
namespace CompileTimeClassProcessors {
typedef void (*ProcessorType)(CompileTimeClass &target, CompileTimeClass::_BaseInfo *info);
static void Warn(CompileTimeClass&, CompileTimeClass::_BaseInfo*) {
BNM_LOG_WARN(DBG_BNM_MSG_CompileTimeClass_ToClass_default_Warn);
}
// _ClassInfo
static void ProcessClassInfo(CompileTimeClass &target, CompileTimeClass::_BaseInfo *info) {
auto classInfo = (CompileTimeClass::_ClassInfo *) info;
auto _namespace = classInfo->_namespace ? classInfo->_namespace : std::string_view{};
if (!classInfo->_imageName || !strlen(classInfo->_imageName)) {
target._loadedClass = TryGetClassWithoutImage(_namespace, classInfo->_name);
return;
}
BNM::Image image{};
auto &assemblies = *Internal::Assembly$$GetAllAssemblies();
for (auto assembly: assemblies) {
auto currentImage = Internal::il2cppMethods.il2cpp_assembly_get_image(assembly);
if (!Internal::CompareImageName(currentImage, classInfo->_imageName)) continue;
image = currentImage;
break;
}
target._loadedClass = Internal::TryGetClassInImage(image, _namespace, classInfo->_name);
}
// _InnerInfo
static void ProcessInnerInfo(CompileTimeClass &target, CompileTimeClass::_BaseInfo *info) {
if (!target._loadedClass) {
BNM_LOG_WARN(DBG_BNM_MSG_CompileTimeClass_ToClass_Inner_Warn);
return;
}
auto innerInfo = (CompileTimeClass::_InnerInfo *) info;
if (!target._loadedClass) return;
target._loadedClass = target._loadedClass.GetInnerClass(innerInfo->_name);
}
// _ModifierInfo
static void ProcessModifierInfo(CompileTimeClass &target, CompileTimeClass::_BaseInfo *info) {
if (!target._loadedClass) {
BNM_LOG_WARN(DBG_BNM_MSG_CompileTimeClass_ToClass_Modifier_Warn);
return;
}
auto modifierInfo = (CompileTimeClass::_ModifierInfo *) info;
switch (modifierInfo->_modifierType) {
case CompileTimeClass::ModifierType::Pointer: {
target._loadedClass = target._loadedClass.GetPointer();
} break;
case CompileTimeClass::ModifierType::Reference: {
target._loadedClass = target._loadedClass.GetReference();
} break;
case CompileTimeClass::ModifierType::Array: {
target._loadedClass = target._loadedClass.GetArray();
} break;
case CompileTimeClass::ModifierType::None: break;
}
}
// _GenericInfo
static void ProcessGenericInfo(CompileTimeClass &target, CompileTimeClass::_BaseInfo *info) {
if (!target._loadedClass) {
BNM_LOG_WARN(DBG_BNM_MSG_CompileTimeClass_ToClass_Generic_Warn);
return;
}
auto genericInfo = (CompileTimeClass::_GenericInfo *) info;
// Экономия памяти, даже если пользователь не хочет
for (auto &type : genericInfo->_types) ((BNM::CompileTimeClass &)type)._autoFree = true;
target._loadedClass = Internal::TryMakeGenericClass(target._loadedClass, genericInfo->_types);
genericInfo->_types.clear();
genericInfo->_types.shrink_to_fit();
}
ProcessorType processors[(uint8_t) CompileTimeClass::_BaseType::MaxCount] = {
Warn,
ProcessClassInfo,
ProcessInnerInfo,
ProcessModifierInfo,
ProcessGenericInfo,
};
}
Class CompileTimeClass::ToClass() {
if (_isReferenced) {
_isReferenced = false;
auto ref = reference ? *reference : nullptr;
_loadedClass = ref;
}
if (_loadedClass) return _loadedClass;
if (_stack.empty()) return {(IL2CPP::Il2CppClass *) nullptr};
for (auto info : _stack) {
auto index = (uint8_t) info->_baseType;
if (index >= (uint8_t) CompileTimeClass::_BaseType::MaxCount) {
BNM_LOG_WARN(DBG_BNM_MSG_CompileTimeClass_ToClass_OoB_Warn, (size_t) index);
continue;
}
CompileTimeClassProcessors::processors[index](*this, (_BaseInfo *) info);
}
if (_autoFree) {
_autoFree = false;
Free();
}
_loadedClass.TryInit();
return _loadedClass;
}
Class CompileTimeClass::ToClass() const {
return ((CompileTimeClass *)this)->ToClass();
}
CompileTimeClass::operator Class() { return ToClass(); }
CompileTimeClass::operator Class() const { return ToClass(); }
IL2CPP::Il2CppType *CompileTimeClass::ToIl2CppType() { return ToClass().GetIl2CppType(); }
IL2CPP::Il2CppType *CompileTimeClass::ToIl2CppType() const { return ToClass().GetIl2CppType(); }
CompileTimeClass::operator IL2CPP::Il2CppType*() { return ToIl2CppType(); }
CompileTimeClass::operator IL2CPP::Il2CppType*() const { return ToIl2CppType(); }
IL2CPP::Il2CppClass *CompileTimeClass::ToIl2CppClass() { return ToClass().GetClass(); }
IL2CPP::Il2CppClass *CompileTimeClass::ToIl2CppClass() const { return ToClass().GetClass(); }
CompileTimeClass::operator IL2CPP::Il2CppClass*() { return ToIl2CppClass(); }
CompileTimeClass::operator IL2CPP::Il2CppClass*() const { return ToIl2CppClass(); }
void CompileTimeClass::Free() {
if (_autoFree) return;
_autoFree = true;
for (auto info : _stack) BNM_free((void *) info);
_stack.clear();
}