Skip to content

Commit b4b68f2

Browse files
committed
add WIP il2cppdumper port as tool
1 parent bbde69e commit b4b68f2

6 files changed

Lines changed: 964 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .metadata import Metadata
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
from enum import Enum
2+
from io import BytesIO
3+
from struct import pack, unpack
4+
from typing import List, Iterator, Tuple, Union, BinaryIO, get_origin, get_args
5+
6+
# save original int class
7+
_int = int
8+
9+
10+
class CustomIntWrapper(int):
11+
__size: int
12+
__format: str
13+
14+
@classmethod
15+
def read_from(cls, f: BytesIO):
16+
return cls(
17+
unpack("<" + getattr(cls, "__format"), f.read(getattr(cls, "__size")))[0]
18+
)
19+
20+
21+
def CustomIntWrapperFactory(name: str, __size: int, __format: str) -> CustomIntWrapper:
22+
return type(name, (CustomIntWrapper,), {"__size": __size, "__format": __format})
23+
24+
25+
byte = CustomIntWrapperFactory("byte", 1, "B")
26+
short = CustomIntWrapperFactory("short", 2, "h")
27+
ushort = CustomIntWrapperFactory("ushort", 2, "H")
28+
int = CustomIntWrapperFactory("int", 4, "i")
29+
uint = CustomIntWrapperFactory("uint", 4, "I")
30+
long = CustomIntWrapperFactory("long", 8, "q")
31+
ulong = CustomIntWrapperFactory("ulong", 8, "Q")
32+
33+
34+
class Version:
35+
Min: float
36+
Max: float
37+
38+
def __new__(cls, Min: float = 0, Max: float = 99):
39+
spec = []
40+
if Min:
41+
spec.append(f"Min={Min}")
42+
if Max != 99:
43+
spec.append(f"Max={Max}")
44+
newclass = type(
45+
f"Version ({', '.join(spec)})", (Version,), {"Min": Min, "Max": Max}
46+
)
47+
return newclass
48+
49+
@classmethod
50+
def check_compatiblity(cls, version):
51+
return cls.Min <= version <= cls.Max
52+
53+
54+
class MetaDataClass:
55+
version: float
56+
size: int
57+
parseString: str
58+
59+
def __init__(self, reader: BinaryIO = None) -> None:
60+
if not (self.version):
61+
raise NotImplementedError(
62+
"Using an unversioned MetaDataClass isn't possible."
63+
)
64+
if reader:
65+
self.read_from(reader)
66+
67+
def read_from(self, reader: BytesIO):
68+
self.__dict__.update(
69+
zip(
70+
self.__annotations__.keys(),
71+
unpack(self.parseString, reader.read(self.size)),
72+
)
73+
)
74+
75+
def write_to(self, writer: BytesIO):
76+
writer.write(
77+
pack(
78+
"<" + self.parseString,
79+
(self.get(key) for key in self.__annotations__.keys()),
80+
)
81+
)
82+
83+
@classmethod
84+
def generate_versioned_subclass(cls, version: float):
85+
# fetch fields & calculate size
86+
compatible_fields = {}
87+
size = 0
88+
parseString = []
89+
for key, clz in cls.__annotations__.items():
90+
if get_origin(clz) == Union:
91+
clz, *version_checks = get_args(clz)
92+
if not any(
93+
version_check.check_compatiblity(version)
94+
for version_check in version_checks
95+
):
96+
continue
97+
compatible_fields[key] = clz
98+
size += getattr(clz, "__size")
99+
parseString.append(getattr(clz, "__format"))
100+
101+
newclass = type(
102+
f"{cls.__name__} - V{version:.1f}",
103+
(MetaDataClass,),
104+
{
105+
"__annotations__": compatible_fields,
106+
"size": size,
107+
"version": version,
108+
"parseString": "".join(parseString),
109+
},
110+
)
111+
return newclass

‎UnityPy/tools/libil2cpp_helper/il2cpp.py‎

Whitespace-only changes.
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
from .helper import *
2+
3+
4+
class Il2CppCodeRegistration(MetaDataClass):
5+
methodPointersCount: Union[long, Version(Max=24.1)]
6+
methodPointers: Union[ulong, Version(Max=24.1)]
7+
delegateWrappersFromNativeToManagedCount: Union[ulong, Version(Max=21)]
8+
delegateWrappersFromNativeToManaged: Union[
9+
ulong, Version(Max=21)
10+
] # note the double indirection to handle different calling conventions
11+
reversePInvokeWrapperCount: Union[long, Version(Min=22)]
12+
reversePInvokeWrappers: Union[ulong, Version(Min=22)]
13+
delegateWrappersFromManagedToNativeCount: Union[ulong, Version(Max=22)]
14+
delegateWrappersFromManagedToNative: Union[ulong, Version(Max=22)]
15+
marshalingFunctionsCount: Union[ulong, Version(Max=22)]
16+
marshalingFunctions: Union[ulong, Version(Max=22)]
17+
ccwMarshalingFunctionsCount: Union[ulong, Version(Min=21, Max=22)]
18+
ccwMarshalingFunctions: Union[ulong, Version(Min=21, Max=22)]
19+
genericMethodPointersCount: long
20+
genericMethodPointers: ulong
21+
genericAdjustorThunks: Union[ulong, Version(Min=24.4, Max=24.4), Version(Min=27.1)]
22+
invokerPointersCount: long
23+
invokerPointers: ulong
24+
customAttributeCount: Union[long, Version(Max=24.4)]
25+
customAttributeGenerators: Union[ulong, Version(Max=24.4)]
26+
guidCount: Union[long, Version(Min=21, Max=22)]
27+
guids: Union[ulong, Version(Min=21, Max=22)] # Il2CppGuid
28+
unresolvedVirtualCallCount: Union[long, Version(Min=22)]
29+
unresolvedVirtualCallPointers: Union[ulong, Version(Min=22)]
30+
interopDataCount: Union[ulong, Version(Min=23)]
31+
interopData: Union[ulong, Version(Min=23)]
32+
windowsRuntimeFactoryCount: Union[ulong, Version(Min=24.3)]
33+
windowsRuntimeFactoryTable: Union[ulong, Version(Min=24.3)]
34+
codeGenModulesCount: Union[long, Version(Min=24.2)]
35+
codeGenModules: Union[ulong, Version(Min=24.2)]
36+
37+
38+
class Il2CppMetadataRegistration(MetaDataClass):
39+
genericClassesCount: long
40+
genericClasses: ulong
41+
genericInstsCount: long
42+
genericInsts: ulong
43+
genericMethodTableCount: long
44+
genericMethodTable: ulong
45+
typesCount: long
46+
types: ulong
47+
methodSpecsCount: long
48+
methodSpecs: ulong
49+
methodReferencesCount: Union[long, Version(Max=16)]
50+
methodReferences: Union[ulong, Version(Max=16)]
51+
52+
fieldOffsetsCount: long
53+
fieldOffsets: ulong
54+
55+
typeDefinitionsSizesCount: long
56+
typeDefinitionsSizes: ulong
57+
metadataUsagesCount: Union[ulong, Version(Min=19)]
58+
metadataUsages: Union[ulong, Version(Min=19)]
59+
60+
61+
class Il2CppTypeEnum(uint, Enum):
62+
IL2CPP_TYPE_END = 0x00 # End of List
63+
IL2CPP_TYPE_VOID = 0x01
64+
IL2CPP_TYPE_BOOLEAN = 0x02
65+
IL2CPP_TYPE_CHAR = 0x03
66+
IL2CPP_TYPE_I1 = 0x04
67+
IL2CPP_TYPE_U1 = 0x05
68+
IL2CPP_TYPE_I2 = 0x06
69+
IL2CPP_TYPE_U2 = 0x07
70+
IL2CPP_TYPE_I4 = 0x08
71+
IL2CPP_TYPE_U4 = 0x09
72+
IL2CPP_TYPE_I8 = 0x0A
73+
IL2CPP_TYPE_U8 = 0x0B
74+
IL2CPP_TYPE_R4 = 0x0C
75+
IL2CPP_TYPE_R8 = 0x0D
76+
IL2CPP_TYPE_STRING = 0x0E
77+
IL2CPP_TYPE_PTR = 0x0F # arg: <type> token
78+
IL2CPP_TYPE_BYREF = 0x10 # arg: <type> token
79+
IL2CPP_TYPE_VALUETYPE = 0x11 # arg: <type> token
80+
IL2CPP_TYPE_CLASS = 0x12 # arg: <type> token
81+
IL2CPP_TYPE_VAR = 0x13 # Generic parameter in a generic type definition, represented as number (compressed unsigned integer) number
82+
IL2CPP_TYPE_ARRAY = 0x14 # type, rank, boundsCount, bound1, loCount, lo1
83+
IL2CPP_TYPE_GENERICINST = 0x15 # <type> <type-arg-count> <type-1> \x{2026} <type-n>
84+
IL2CPP_TYPE_TYPEDBYREF = 0x16
85+
IL2CPP_TYPE_I = 0x18
86+
IL2CPP_TYPE_U = 0x19
87+
IL2CPP_TYPE_FNPTR = 0x1B # arg: full method signature
88+
IL2CPP_TYPE_OBJECT = 0x1C
89+
IL2CPP_TYPE_SZARRAY = 0x1D # 0-based one-dim-array
90+
IL2CPP_TYPE_MVAR = 0x1E # Generic parameter in a generic method definition, represented as number (compressed unsigned integer)
91+
IL2CPP_TYPE_CMOD_REQD = 0x1F # arg: typedef or typeref token
92+
IL2CPP_TYPE_CMOD_OPT = 0x20 # optional arg: typedef or typref token
93+
IL2CPP_TYPE_INTERNAL = 0x21 # CLR internal type
94+
95+
IL2CPP_TYPE_MODIFIER = 0x40 # Or with the following types
96+
IL2CPP_TYPE_SENTINEL = 0x41 # Sentinel for varargs method signature
97+
IL2CPP_TYPE_PINNED = 0x45 # Local var that points to pinned object
98+
99+
IL2CPP_TYPE_ENUM = 0x55 # an enumeration
100+
101+
102+
# class Il2CppType(MetaDataClass):
103+
# datapoint: ulong
104+
# bits: uint
105+
# data { get: Union set; }
106+
# attrs { get: uint set; }
107+
# type { get: Il2CppTypeEnum set; }
108+
# num_mods { get: uint set; }
109+
# byref { get: uint set; }
110+
# pinned { get: uint set; }
111+
112+
# public void Init()
113+
# {
114+
# attrs = bits & 0xffff;
115+
# type = (Il2CppTypeEnum)((bits >> 16) & 0xff);
116+
# num_mods = (bits >> 24) & 0x3f;
117+
# byref = (bits >> 30) & 1;
118+
# pinned = bits >> 31;
119+
# data = new Union { dummy = datapoint };
120+
# }
121+
122+
# public class Union
123+
# {
124+
# dummy: ulong
125+
# #/ <summary>
126+
# #/ for VALUETYPE and CLASS
127+
# #/ </summary>
128+
# klassIndex => (long)dummy: long
129+
# #/ <summary>
130+
# #/ for VALUETYPE and CLASS at runtime
131+
# #/ </summary>
132+
# typeHandle => dummy: ulong
133+
# #/ <summary>
134+
# #/ for PTR and SZARRAY
135+
# #/ </summary>
136+
# type => dummy: ulong
137+
# #/ <summary>
138+
# #/ for ARRAY
139+
# #/ </summary>
140+
# array => dummy: ulong
141+
# #/ <summary>
142+
# #/ for VAR and MVAR
143+
# #/ </summary>
144+
# genericParameterIndex => (long)dummy: long
145+
# #/ <summary>
146+
# #/ for VAR and MVAR at runtime
147+
# #/ </summary>
148+
# genericParameterHandle => dummy: ulong
149+
# #/ <summary>
150+
# #/ for GENERICINST
151+
# #/ </summary>
152+
# generic_class => dummy: ulong
153+
# }
154+
# }
155+
156+
157+
class Il2CppGenericContext(MetaDataClass):
158+
# The instantiation corresponding to the class generic parameters
159+
class_inst: ulong
160+
# The instantiation corresponding to the method generic parameters
161+
method_inst: ulong
162+
163+
164+
class Il2CppGenericClass(MetaDataClass):
165+
typeDefinitionIndex: Union[long, Version(Max=24.4)] # the generic type definition
166+
type: Union[ulong, Version(Min=27)] # the generic type definition
167+
context: Il2CppGenericContext # a context that contains the type instantiation doesn't contain any method instantiation
168+
cached_class: ulong # if present, the Il2CppClass corresponding to the instantiation.
169+
170+
171+
class Il2CppGenericInst(MetaDataClass):
172+
type_argc: long
173+
type_argv: ulong
174+
175+
176+
class Il2CppArrayType(MetaDataClass):
177+
etype: ulong
178+
rank: byte
179+
numsizes: byte
180+
numlobounds: byte
181+
sizes: ulong
182+
lobounds: ulong
183+
184+
185+
class Il2CppGenericMethodIndices(MetaDataClass):
186+
methodIndex: int
187+
invokerIndex: int
188+
adjustorThunk: Union[int, Version(Min=24.4, Max=24.4), Version(Min=27.1)]
189+
190+
191+
class Il2CppGenericMethodFunctionsDefinitions(MetaDataClass):
192+
genericMethodIndex: int
193+
indices: Il2CppGenericMethodIndices
194+
195+
196+
class Il2CppMethodSpec(MetaDataClass):
197+
methodDefinitionIndex: int
198+
classIndexIndex: int
199+
methodIndexIndex: int
200+
201+
202+
class Il2CppCodeGenModule(MetaDataClass):
203+
moduleName: ulong
204+
methodPointerCount: long
205+
methodPointers: ulong
206+
adjustorThunkCount: Union[long, Version(Min=24.4, Max=24.4), Version(Min=27.1)]
207+
adjustorThunks: Union[ulong, Version(Min=24.4, Max=24.4), Version(Min=27.1)]
208+
invokerIndices: ulong
209+
reversePInvokeWrapperCount: ulong
210+
reversePInvokeWrapperIndices: ulong
211+
rgctxRangesCount: long
212+
rgctxRanges: ulong
213+
rgctxsCount: long
214+
rgctxs: ulong
215+
debuggerMetadata: ulong
216+
customAttributeCacheGenerator: Union[ulong, Version(Min=27)]
217+
moduleInitializer: Union[ulong, Version(Min=27)]
218+
staticConstructorTypeIndices: Union[ulong, Version(Min=27)]
219+
metadataRegistration: Union[ulong, Version(Min=27)] # Per-assembly mode only
220+
codeRegistaration: Union[ulong, Version(Min=27)] # Per-assembly mode only
221+
222+
223+
class Il2CppRange(MetaDataClass):
224+
start: int
225+
length: int
226+
227+
228+
class Il2CppTokenRangePair(MetaDataClass):
229+
token: uint
230+
range: Il2CppRange

0 commit comments

Comments
 (0)