-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathIl2cppApi.lua
3116 lines (2829 loc) · 117 KB
/
Il2cppApi.lua
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local __bundle_require, __bundle_loaded, __bundle_register, __bundle_modules = (function(superRequire)
local loadingPlaceholder = {[{}] = true}
local register
local modules = {}
local require
local loaded = {}
register = function(name, body)
if not modules[name] then
modules[name] = body
end
end
require = function(name)
local loadedModule = loaded[name]
if loadedModule then
if loadedModule == loadingPlaceholder then
return nil
end
else
if not modules[name] then
if not superRequire then
local identifier = type(name) == 'string' and '\"' .. name .. '\"' or tostring(name)
error('Tried to require ' .. identifier .. ', but no such module has been registered')
else
return superRequire(name)
end
end
loaded[name] = loadingPlaceholder
loadedModule = modules[name](require, loaded, register, modules)
loaded[name] = loadedModule
end
return loadedModule
end
return require, loaded, register, modules
end)(require)
__bundle_register("GGIl2cpp", function(require, _LOADED, __bundle_register, __bundle_modules)
require("utils.il2cppconst")
require("il2cpp")
---@class ClassInfoRaw
---@field ClassName string | nil
---@field ClassInfoAddress number
---@field ImageName string
---@class ClassInfo
---@field ClassName string
---@field ClassAddress string
---@field Methods MethodInfo[] | nil
---@field Fields FieldInfo[] | nil
---@field Parent ParentClassInfo | nil
---@field ClassNameSpace string
---@field StaticFieldData number | nil
---@field IsEnum boolean
---@field TypeMetadataHandle number
---@field InstanceSize number
---@field Token string
---@field ImageName string
---@field GetFieldWithName fun(self : ClassInfo, name : string) : FieldInfo | nil @Get FieldInfo by Field Name. If Fields weren't dumped, then this function return `nil`. Also, if Field isn't found by name, then function will return `nil`
---@field GetMethodsWithName fun(self : ClassInfo, name : string) : MethodInfo[] | nil @Get MethodInfo[] by MethodName. If Methods weren't dumped, then this function return `nil`. Also, if Method isn't found by name, then function will return `table with zero size`
---@field GetFieldWithOffset fun(self : ClassInfo, fieldOffset : number) : FieldInfo | nil
---@class ParentClassInfo
---@field ClassName string
---@field ClassAddress string
---@class FieldInfoRaw
---@field FieldInfoAddress number
---@field ClassName string | nil
---@class ClassMemory
---@field config ClassConfig
---@field result ClassInfo[] | ErrorSearch
---@field len number
---@field isNew boolean | nil
---@class MethodMemory
---@field len number
---@field result MethodInfo[] | ErrorSearch
---@field isNew boolean | nil
---@class FieldInfo
---@field ClassName string
---@field ClassAddress string
---@field FieldName string
---@field Offset string
---@field IsStatic boolean
---@field Type string
---@field IsConst boolean
---@field Access string
---@field GetConstValue fun(self : FieldInfo) : nil | string | number
---@class MethodInfoRaw
---@field MethodName string | nil
---@field Offset number | nil
---@field MethodInfoAddress number
---@field ClassName string | nil
---@field MethodAddress number
---@class ErrorSearch
---@field Error string
---@class MethodInfo
---@field MethodName string
---@field Offset string
---@field AddressInMemory string
---@field MethodInfoAddress number
---@field ClassName string
---@field ClassAddress string
---@field ParamCount number
---@field ReturnType string
---@field IsStatic boolean
---@field IsAbstract boolean
---@field Access string
---@class Il2cppApi
---@field FieldApiOffset number
---@field FieldApiType number
---@field FieldApiClassOffset number
---@field ClassApiNameOffset number
---@field ClassApiMethodsStep number
---@field ClassApiCountMethods number
---@field ClassApiMethodsLink number
---@field ClassApiFieldsLink number
---@field ClassApiFieldsStep number
---@field ClassApiCountFields number
---@field ClassApiParentOffset number
---@field ClassApiNameSpaceOffset number
---@field ClassApiStaticFieldDataOffset number
---@field ClassApiEnumType number
---@field ClassApiEnumRsh number
---@field ClassApiTypeMetadataHandle number
---@field ClassApiInstanceSize number
---@field ClassApiToken number
---@field MethodsApiClassOffset number
---@field MethodsApiNameOffset number
---@field MethodsApiParamCount number
---@field MethodsApiReturnType number
---@field MethodsApiFlags number
---@field typeDefinitionsSize number
---@field typeDefinitionsOffset number
---@field stringOffset number
---@field fieldDefaultValuesOffset number
---@field fieldDefaultValuesSize number
---@field fieldAndParameterDefaultValueDataOffset number
---@field TypeApiType number
---@field Il2CppTypeDefinitionApifieldStart number
---@field MetadataRegistrationApitypes number
---@class ClassConfig
---@field Class number | string @Class Name or Address Class
---@field FieldsDump boolean
---@field MethodsDump boolean
---@class Il2cppConfig
---@field libilcpp table | nil
---@field globalMetadata table | nil
---@field il2cppVersion number | nil
---@field globalMetadataHeader number | nil
---@field metadataRegistration number | nil
---@class Il2CppTypeDefinitionApi
---@field fieldStart number
---@class MethodFlags
---@field Access string[]
---@field METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK number
---@field METHOD_ATTRIBUTE_STATIC number
---@field METHOD_ATTRIBUTE_ABSTRACT number
---@class FieldFlags
---@field Access string[]
---@field FIELD_ATTRIBUTE_FIELD_ACCESS_MASK number
---@field FIELD_ATTRIBUTE_STATIC number
---@field FIELD_ATTRIBUTE_LITERAL number
return Il2cpp
end)
__bundle_register("il2cpp", function(require, _LOADED, __bundle_register, __bundle_modules)
local Il2cppMemory = require("utils.il2cppmemory")
local VersionEngine = require("utils.version")
local AndroidInfo = require("utils.androidinfo")
local Searcher = require("utils.universalsearcher")
local PatchApi = require("utils.patchapi")
---@class Il2cpp
local Il2cppBase = {
il2cppStart = 0,
il2cppEnd = 0,
globalMetadataStart = 0,
globalMetadataEnd = 0,
globalMetadataHeader = 0,
MainType = AndroidInfo.platform and gg.TYPE_QWORD or gg.TYPE_DWORD,
pointSize = AndroidInfo.platform and 8 or 4,
---@type Il2CppTypeDefinitionApi
Il2CppTypeDefinitionApi = {},
MetadataRegistrationApi = require("il2cppstruct.metadataRegistration"),
TypeApi = require("il2cppstruct.type"),
MethodsApi = require("il2cppstruct.method"),
GlobalMetadataApi = require("il2cppstruct.globalmetadata"),
FieldApi = require("il2cppstruct.field"),
ClassApi = require("il2cppstruct.class"),
ObjectApi = require("il2cppstruct.object"),
ClassInfoApi = require("il2cppstruct.api.classinfo"),
FieldInfoApi = require("il2cppstruct.api.fieldinfo"),
---@type MyString
String = require("il2cppstruct.il2cppstring"),
MemoryManager = require("utils.malloc"),
--- Patch `Bytescodes` to `add`
---
--- Example:
--- arm64:
--- `mov w0,#0x1`
--- `ret`
---
--- `Il2cpp.PatchesAddress(0x100, "\x20\x00\x80\x52\xc0\x03\x5f\xd6")`
---@param add number
---@param Bytescodes string
---@return Patch
PatchesAddress = function(add, Bytescodes)
local patchCode = {}
for code in string.gmatch(Bytescodes, '.') do
patchCode[#patchCode + 1] = {
address = add + #patchCode,
value = string.byte(code),
flags = gg.TYPE_BYTE
}
end
---@type Patch
local patch = PatchApi:Create(patchCode)
patch:Patch()
return patch
end,
--- Searches for a method, or rather information on the method, by name or by offset, you can also send an address in memory to it.
---
--- Return table with information about methods.
---@generic TypeForSearch : number | string
---@param searchParams TypeForSearch[] @TypeForSearch = number | string
---@return table<number, MethodInfo[] | ErrorSearch>
FindMethods = function(searchParams)
Il2cppMemory:SaveResults()
for i = 1, #searchParams do
---@type number | string
searchParams[i] = Il2cpp.MethodsApi:Find(searchParams[i])
end
Il2cppMemory:ClearSavedResults()
return searchParams
end,
--- Searches for a class, by name, or by address in memory.
---
--- Return table with information about class.
---@param searchParams ClassConfig[]
---@return table<number, ClassInfo[] | ErrorSearch>
FindClass = function(searchParams)
Il2cppMemory:SaveResults()
for i = 1, #searchParams do
searchParams[i] = Il2cpp.ClassApi:Find(searchParams[i])
end
Il2cppMemory:ClearSavedResults()
return searchParams
end,
--- Searches for an object by name or by class address, in memory.
---
--- In some cases, the function may return an incorrect result for certain classes. For example, sometimes the garbage collector may not have time to remove an object from memory and then a `fake object` will appear or for a turnover, the object may still be `not implemented` or `not created`.
---
--- Returns a table of objects.
---@param searchParams table
---@return table
FindObject = function(searchParams)
Il2cppMemory:SaveResults()
for i = 1, #searchParams do
searchParams[i] = Il2cpp.ObjectApi:Find(Il2cpp.ClassApi:Find({Class = searchParams[i]}))
end
Il2cppMemory:ClearSavedResults()
return searchParams
end,
--- Searches for a field, or rather information about the field, by name or by address in memory.
---
--- Return table with information about fields.
---@generic TypeForSearch : number | string
---@param searchParams TypeForSearch[] @TypeForSearch = number | string
---@return table<number, FieldInfo[] | ErrorSearch>
FindFields = function(searchParams)
Il2cppMemory:SaveResults()
for i = 1, #searchParams do
---@type number | string
local searchParam = searchParams[i]
local searchResult = Il2cppMemory:GetInformationOfField(searchParam)
if not searchResult then
searchResult = Il2cpp.FieldApi:Find(searchParam)
Il2cppMemory:SetInformationOfField(searchParam, searchResult)
end
searchParams[i] = searchResult
end
Il2cppMemory:ClearSavedResults()
return searchParams
end,
---@param Address number
---@param length? number
---@return string
Utf8ToString = function(Address, length)
local chars, char = {}, {
address = Address,
flags = gg.TYPE_BYTE
}
if not length then
repeat
_char = string.char(gg.getValues({char})[1].value & 0xFF)
chars[#chars + 1] = _char
char.address = char.address + 0x1
until string.find(_char, "[%z%s]")
return table.concat(chars, "", 1, #chars - 1)
else
for i = 1, length do
local _char = gg.getValues({char})[1].value
chars[i] = string.char(_char & 0xFF)
char.address = char.address + 0x1
end
return table.concat(chars)
end
end,
---@param bytes string
ChangeBytesOrder = function(bytes)
local newBytes, index, lenBytes = {}, 0, #bytes / 2
for byte in string.gmatch(bytes, "..") do
newBytes[lenBytes - index] = byte
index = index + 1
end
return table.concat(newBytes)
end,
FixValue = function(val)
return AndroidInfo.platform and val & 0x00FFFFFFFFFFFFFF or val & 0xFFFFFFFF
end,
GetValidAddress = function(Address)
local lastByte = Address & 0x000000000000000F
local delta = 0
local checkTable = {[12] = true, [4] = true, [8] = true, [0] = true}
while not checkTable[lastByte - delta] do
delta = delta + 1
end
return Address - delta
end,
---@param self Il2cpp
---@param address number | string
SearchPointer = function(self, address)
address = self.ChangeBytesOrder(type(address) == 'number' and string.format('%X', address) or address)
gg.searchNumber('h ' .. address)
gg.refineNumber('h ' .. address:sub(1, 6))
gg.refineNumber('h ' .. address:sub(1, 2))
local FindsResult = gg.getResults(gg.getResultsCount())
gg.clearResults()
return FindsResult
end,
}
---@type Il2cpp
Il2cpp = setmetatable({}, {
---@param self Il2cpp
---@param config? Il2cppConfig
__call = function(self, config)
config = config or {}
getmetatable(self).__index = Il2cppBase
if config.libilcpp then
self.il2cppStart, self.il2cppEnd = config.libilcpp.start, config.libilcpp['end']
else
self.il2cppStart, self.il2cppEnd = Searcher.FindIl2cpp()
end
if config.globalMetadata then
self.globalMetadataStart, self.globalMetadataEnd = config.globalMetadata.start, config.globalMetadata['end']
else
self.globalMetadataStart, self.globalMetadataEnd = Searcher:FindGlobalMetaData()
end
if config.globalMetadataHeader then
self.globalMetadataHeader = config.globalMetadataHeader
else
self.globalMetadataHeader = self.globalMetadataStart
end
self.MetadataRegistrationApi.metadataRegistration = config.metadataRegistration
VersionEngine:ChooseVersion(config.il2cppVersion, self.globalMetadataHeader)
Il2cppMemory:ClearMemorize()
end,
__index = function(self, key)
assert(key == "PatchesAddress", "You didn't call 'Il2cpp'")
return Il2cppBase[key]
end
})
return Il2cpp
end)
__bundle_register("utils.malloc", function(require, _LOADED, __bundle_register, __bundle_modules)
local MemoryManager = {
availableMemory = 0,
lastAddress = 0,
NewAlloc = function(self)
self.lastAddress = gg.allocatePage(gg.PROT_READ | gg.PROT_WRITE)
self.availableMemory = 4096
end,
}
local M = {
---@param size number
MAlloc = function(size)
local manager = MemoryManager
if size > manager.availableMemory then
manager:NewAlloc()
end
local address = manager.lastAddress
manager.availableMemory = manager.availableMemory - size
manager.lastAddress = manager.lastAddress + size
return address
end,
}
return M
end)
__bundle_register("il2cppstruct.il2cppstring", function(require, _LOADED, __bundle_register, __bundle_modules)
---@class StringApi
---@field address number
---@field pointToStr number
---@field Fields table<string, number>
---@field ClassAddress number
local StringApi = {
---@param self StringApi
---@param newStr string
EditString = function(self, newStr)
local _stringLength = gg.getValues{{address = self.address + self.Fields._stringLength, flags = gg.TYPE_DWORD}}[1].value
_stringLength = _stringLength * 2
local bytes = gg.bytes(newStr, "UTF-16LE")
if _stringLength == #bytes then
local strStart = self.address + self.Fields._firstChar
for i, v in ipairs(bytes) do
bytes[i] = {
address = strStart + (i - 1),
flags = gg.TYPE_BYTE,
value = v
}
end
gg.setValues(bytes)
elseif _stringLength > #bytes then
local strStart = self.address + self.Fields._firstChar
local _bytes = {}
for i = 1, _stringLength do
_bytes[#_bytes + 1] = {
address = strStart + (i - 1),
flags = gg.TYPE_BYTE,
value = bytes[i] or 0
}
end
gg.setValues(_bytes)
elseif _stringLength < #bytes then
self.address = Il2cpp.MemoryManager.MAlloc(self.Fields._firstChar + #bytes + 8)
local length = #bytes % 2 == 1 and #bytes + 1 or #bytes
local _bytes = {
{ -- Head
address = self.address,
flags = Il2cpp.MainType,
value = self.ClassAddress
},
{ -- _stringLength
address = self.address + self.Fields._stringLength,
flags = gg.TYPE_DWORD,
value = length / 2
}
}
local strStart = self.address + self.Fields._firstChar
for i = 1, length do
_bytes[#_bytes + 1] = {
address = strStart + (i - 1),
flags = gg.TYPE_BYTE,
value = bytes[i] or 0
}
end
_bytes[#_bytes + 1] = {
address = self.pointToStr,
flags = Il2cpp.MainType,
value = self.address
}
gg.setValues(_bytes)
end
end,
---@param self StringApi
---@return string
ReadString = function(self)
local _stringLength = gg.getValues{{address = self.address + self.Fields._stringLength, flags = gg.TYPE_DWORD}}[1].value
local bytes = {}
if _stringLength > 0 and _stringLength < 200 then
local strStart = self.address + self.Fields._firstChar
for i = 0, _stringLength do
bytes[#bytes + 1] = {
address = strStart + (i << 1),
flags = gg.TYPE_WORD
}
end
bytes = gg.getValues(bytes)
local code = {[[return "]]}
for i, v in ipairs(bytes) do
code[#code + 1] = string.format([[\u{%x}]], v.value & 0xFFFF)
end
code[#code + 1] = '"'
local read, err = load(table.concat(code))
if read then
return read()
end
end
return ""
end
}
---@class MyString
---@field From fun(address : number) : StringApi | nil
local String = {
---@param address number
---@return StringApi | nil
From = function(address)
local pointToStr = gg.getValues({{address = Il2cpp.FixValue(address), flags = Il2cpp.MainType}})[1]
local str = setmetatable(
{
address = Il2cpp.FixValue(pointToStr.value),
Fields = {},
pointToStr = Il2cpp.FixValue(address)
}, {__index = StringApi})
local pointClassAddress = gg.getValues({{address = str.address, flags = Il2cpp.MainType}})[1].value
local stringInfo = Il2cpp.FindClass({{Class = Il2cpp.FixValue(pointClassAddress), FieldsDump = true}})[1]
for i, v in ipairs(stringInfo) do
if v.ClassNameSpace == "System" then
str.ClassAddress = tonumber(v.ClassAddress, 16)
for indexField, FieldInfo in ipairs(v.Fields) do
str.Fields[FieldInfo.FieldName] = tonumber(FieldInfo.Offset, 16)
end
return str
end
end
return nil
end,
}
return String
end)
__bundle_register("il2cppstruct.api.fieldinfo", function(require, _LOADED, __bundle_register, __bundle_modules)
local Il2cppMemory = require("utils.il2cppmemory")
---@type FieldInfo
local FieldInfoApi = {
---@param self FieldInfo
---@return nil | string | number
GetConstValue = function(self)
if self.IsConst then
local fieldIndex = getmetatable(self).fieldIndex
local defaultValue = Il2cppMemory:GetDefaultValue(fieldIndex)
if not defaultValue then
defaultValue = Il2cpp.GlobalMetadataApi:GetDefaultFieldValue(fieldIndex)
Il2cppMemory:SetDefaultValue(fieldIndex, defaultValue)
elseif defaultValue == "nil" then
return nil
end
return defaultValue
end
return nil
end
}
return FieldInfoApi
end)
__bundle_register("utils.il2cppmemory", function(require, _LOADED, __bundle_register, __bundle_modules)
-- Memorizing Il2cpp Search Result
---@class Il2cppMemory
---@field Methods table<number | string, MethodMemory>
---@field Classes table<string | number, ClassMemory>
---@field Fields table<number | string, FieldInfo[] | ErrorSearch>
---@field Results table
---@field Types table<number, string>
---@field DefaultValues table<number, string | number>
---@field GetInformaionOfMethod fun(self : Il2cppMemory, searchParam : number | string) : MethodMemory | nil
---@field SetInformaionOfMethod fun(self : Il2cppMemory, searchParam : string | number, searchResult : MethodMemory) : void
---@field GetInformationOfClass fun(self : Il2cppMemory, searchParam : string | number) : ClassMemory | nil
---@field SetInformationOfClass fun(self : Il2cppMemory, searchParam : string | number, searchResult : ClassMemory) : void
---@field GetInformationOfField fun(self : Il2cppMemory, searchParam : number | string) : FieldInfo[] | nil | ErrorSearch
---@field SetInformationOfField fun(self : Il2cppMemory, searchParam : string | number, searchResult : FieldInfo[] | ErrorSearch) : void
---@field GetInformationOfType fun(self : Il2cppMemory, index : number) : string | nil
---@field SetInformationOfType fun(self : Il2cppMemory, index : number, typeName : string)
---@field SaveResults fun(self : Il2cppMemory) : void
---@field ClearSavedResults fun(self : Il2cppMemory) : void
local Il2cppMemory = {
Methods = {},
Classes = {},
Fields = {},
DefaultValues = {},
Results = {},
Types = {},
---@param self Il2cppMemory
---@return nil | string
GetInformationOfType = function(self, index)
return self.Types[index]
end,
---@param self Il2cppMemory
SetInformationOfType = function(self, index, typeName)
self.Types[index] = typeName
end,
---@param self Il2cppMemory
SaveResults = function(self)
if gg.getResultsCount() > 0 then
self.Results = gg.getResults(gg.getResultsCount())
end
end,
---@param self Il2cppMemory
ClearSavedResults = function(self)
self.Results = {}
end,
---@param self Il2cppMemory
---@param fieldIndex number
---@return string | number | nil
GetDefaultValue = function(self, fieldIndex)
return self.DefaultValues[fieldIndex]
end,
---@param self Il2cppMemory
---@param fieldIndex number
---@param defaultValue number | string | nil
SetDefaultValue = function(self, fieldIndex, defaultValue)
self.DefaultValues[fieldIndex] = defaultValue or "nil"
end,
---@param self Il2cppMemory
---@param searchParam number | string
---@return FieldInfo[] | nil | ErrorSearch
GetInformationOfField = function(self, searchParam)
return self.Fields[searchParam]
end,
---@param self Il2cppMemory
---@param searchParam number | string
---@param searchResult FieldInfo[] | ErrorSearch
SetInformationOfField = function(self, searchParam, searchResult)
if not searchResult.Error then
self.Fields[searchParam] = searchResult
end
end,
GetInformaionOfMethod = function(self, searchParam)
return self.Methods[searchParam]
end,
SetInformaionOfMethod = function(self, searchParam, searchResult)
if not searchResult.Error then
self.Methods[searchParam] = searchResult
end
end,
GetInformationOfClass = function(self, searchParam)
return self.Classes[searchParam]
end,
SetInformationOfClass = function(self, searchParam, searchResult)
self.Classes[searchParam] = searchResult
end,
---@param self Il2cppMemory
---@return void
ClearMemorize = function(self)
self.Methods = {}
self.Classes = {}
self.Fields = {}
self.DefaultValues = {}
self.Results = {}
self.Types = {}
end
}
return Il2cppMemory
end)
__bundle_register("il2cppstruct.api.classinfo", function(require, _LOADED, __bundle_register, __bundle_modules)
local ClassInfoApi = {
---Get FieldInfo by Field Name. If Field isn't found by name, then function will return `nil`
---@param self ClassInfo
---@param name string
---@return FieldInfo | nil
GetFieldWithName = function(self, name)
local FieldsInfo = self.Fields
if FieldsInfo then
for fieldIndex = 1, #FieldsInfo do
if FieldsInfo[fieldIndex].FieldName == name then
return FieldsInfo[fieldIndex]
end
end
else
local ClassAddress = tonumber(self.ClassAddress, 16)
local _ClassInfo = gg.getValues({
{ -- Link as Fields
address = ClassAddress + Il2cpp.ClassApi.FieldsLink,
flags = Il2cpp.MainType
},
{ -- Fields Count
address = ClassAddress + Il2cpp.ClassApi.CountFields,
flags = gg.TYPE_WORD
}
})
self.Fields = Il2cpp.ClassApi:GetClassFields(Il2cpp.FixValue(_ClassInfo[1].value), _ClassInfo[2].value, {
ClassName = self.ClassName,
IsEnum = self.IsEnum,
TypeMetadataHandle = self.TypeMetadataHandle
})
return self:GetFieldWithName(name)
end
return nil
end,
---Get MethodInfo[] by MethodName. If Method isn't found by name, then function will return `table with zero size`
---@param self ClassInfo
---@param name string
---@return MethodInfo[]
GetMethodsWithName = function(self, name)
local MethodsInfo, MethodsInfoResult = self.Methods, {}
if MethodsInfo then
for methodIndex = 1, #MethodsInfo do
if MethodsInfo[methodIndex].MethodName == name then
MethodsInfoResult[#MethodsInfoResult + 1] = MethodsInfo[methodIndex]
end
end
return MethodsInfoResult
else
local ClassAddress = tonumber(self.ClassAddress, 16)
local _ClassInfo = gg.getValues({
{ -- Link as Methods
address = ClassAddress + Il2cpp.ClassApi.MethodsLink,
flags = Il2cpp.MainType
},
{ -- Methods Count
address = ClassAddress + Il2cpp.ClassApi.CountMethods,
flags = gg.TYPE_WORD
}
})
self.Methods = Il2cpp.ClassApi:GetClassMethods(Il2cpp.FixValue(_ClassInfo[1].value), _ClassInfo[2].value,
self.ClassName)
return self:GetMethodsWithName(name)
end
end,
---@param self ClassInfo
---@param fieldOffset number
---@return nil | FieldInfo
GetFieldWithOffset = function(self, fieldOffset)
if not self.Fields then
local ClassAddress = tonumber(self.ClassAddress, 16)
local _ClassInfo = gg.getValues({
{ -- Link as Fields
address = ClassAddress + Il2cpp.ClassApi.FieldsLink,
flags = Il2cpp.MainType
},
{ -- Fields Count
address = ClassAddress + Il2cpp.ClassApi.CountFields,
flags = gg.TYPE_WORD
}
})
self.Fields = Il2cpp.ClassApi:GetClassFields(Il2cpp.FixValue(_ClassInfo[1].value), _ClassInfo[2].value, {
ClassName = self.ClassName,
IsEnum = self.IsEnum,
TypeMetadataHandle = self.TypeMetadataHandle
})
end
if #self.Fields > 0 then
local klass = self
while klass ~= nil do
if klass.Fields and klass.InstanceSize >= fieldOffset then
local lastField
for indexField, field in ipairs(klass.Fields) do
if not (field.IsStatic or field.IsConst) then
local offset = tonumber(field.Offset, 16)
if offset > 0 then
local maybeStruct = fieldOffset < offset
if indexField == 1 and maybeStruct then
break
elseif offset == fieldOffset or indexField == #klass.Fields then
return field
elseif maybeStruct then
return lastField
else
lastField = field
end
end
end
end
end
klass = klass.Parent ~= nil
and Il2cpp.FindClass({
{
Class = tonumber(klass.Parent.ClassAddress, 16),
FieldsDump = true
}
})[1][1]
or nil
end
end
return nil
end
}
return ClassInfoApi
end)
__bundle_register("il2cppstruct.object", function(require, _LOADED, __bundle_register, __bundle_modules)
local AndroidInfo = require("utils.androidinfo")
---@class ObjectApi
local ObjectApi = {
---@param self ObjectApi
---@param Objects table
FilterObjects = function(self, Objects)
local FilterObjects = {}
for k, v in ipairs(gg.getValuesRange(Objects)) do
if v == 'A' then
FilterObjects[#FilterObjects + 1] = Objects[k]
end
end
Objects = FilterObjects
gg.loadResults(Objects)
gg.searchPointer(0)
if gg.getResultsCount() <= 0 and AndroidInfo.platform and AndroidInfo.sdk >= 30 then
local FixRefToObjects = {}
for k, v in ipairs(Objects) do
gg.searchNumber(tostring(v.address | 0xB400000000000000), gg.TYPE_QWORD)
---@type tablelib
local RefToObject = gg.getResults(gg.getResultsCount())
table.move(RefToObject, 1, #RefToObject, #FixRefToObjects + 1, FixRefToObjects)
gg.clearResults()
end
gg.loadResults(FixRefToObjects)
end
local RefToObjects, FilterObjects = gg.getResults(gg.getResultsCount()), {}
gg.clearResults()
for k, v in ipairs(gg.getValuesRange(RefToObjects)) do
if v == 'A' then
FilterObjects[#FilterObjects + 1] = {
address = Il2cpp.FixValue(RefToObjects[k].value),
flags = RefToObjects[k].flags
}
end
end
gg.loadResults(FilterObjects)
local _FilterObjects = gg.getResults(gg.getResultsCount())
gg.clearResults()
return _FilterObjects
end,
---@param self ObjectApi
---@param ClassAddress string
FindObjects = function(self, ClassAddress)
gg.clearResults()
gg.setRanges(0)
gg.setRanges(gg.REGION_C_HEAP | gg.REGION_C_HEAP | gg.REGION_ANONYMOUS | gg.REGION_C_BSS | gg.REGION_C_DATA |
gg.REGION_C_ALLOC)
gg.loadResults({{
address = tonumber(ClassAddress, 16),
flags = Il2cpp.MainType
}})
gg.searchPointer(0)
if gg.getResultsCount() <= 0 and AndroidInfo.platform and AndroidInfo.sdk >= 30 then
gg.searchNumber(tostring(tonumber(ClassAddress, 16) | 0xB400000000000000), gg.TYPE_QWORD)
end
local FindsResult = gg.getResults(gg.getResultsCount())
gg.clearResults()
return self:FilterObjects(FindsResult)
end,
---@param self ObjectApi
---@param ClassesInfo ClassInfo[]
Find = function(self, ClassesInfo)
local Objects = {}
for j = 1, #ClassesInfo do
local FindResult = self:FindObjects(ClassesInfo[j].ClassAddress)
table.move(FindResult, 1, #FindResult, #Objects + 1, Objects)
end
return Objects
end,
FindHead = function(Address)
local validAddress = Il2cpp.GetValidAddress(Address)
local mayBeHead = {}
for i = 1, 1000 do
mayBeHead[i] = {
address = validAddress - (4 * (i - 1)),
flags = Il2cpp.MainType
}
end
mayBeHead = gg.getValues(mayBeHead)
for i = 1, #mayBeHead do
local mayBeClass = Il2cpp.FixValue(mayBeHead[i].value)
if Il2cpp.ClassApi.IsClassInfo(mayBeClass) then
return mayBeHead[i]
end
end
return {value = 0, address = 0}
end,
}
return ObjectApi
end)
__bundle_register("utils.androidinfo", function(require, _LOADED, __bundle_register, __bundle_modules)
local AndroidInfo = {
platform = gg.getTargetInfo().x64,
sdk = gg.getTargetInfo().targetSdkVersion
}
return AndroidInfo
end)
__bundle_register("il2cppstruct.class", function(require, _LOADED, __bundle_register, __bundle_modules)
local Protect = require("utils.protect")
local StringUtils = require("utils.stringutils")
local Il2cppMemory = require("utils.il2cppmemory")
---@class ClassApi
---@field NameOffset number
---@field MethodsStep number
---@field CountMethods number
---@field MethodsLink number
---@field FieldsLink number
---@field FieldsStep number
---@field CountFields number
---@field ParentOffset number
---@field NameSpaceOffset number
---@field StaticFieldDataOffset number