forked from SamboyCoding/Cpp2IL
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathIl2CppTypeDefinition.cs
More file actions
405 lines (316 loc) · 13.4 KB
/
Il2CppTypeDefinition.cs
File metadata and controls
405 lines (316 loc) · 13.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
using System;
using System.Linq;
using System.Reflection;
using LibCpp2IL.BinaryStructures;
using LibCpp2IL.Reflection;
namespace LibCpp2IL.Metadata;
public class Il2CppTypeDefinition : ReadableClass
{
public int NameIndex;
public int NamespaceIndex;
[Version(Max = 24)] public int CustomAttributeIndex;
public int ByvalTypeIndex;
[Version(Max = 24.5f)] //Removed in v27
public int ByrefTypeIndex;
public int DeclaringTypeIndex;
public int ParentIndex;
public int ElementTypeIndex; // we can probably remove this one. Only used for enums
[Version(Max = 24.15f)] public int RgctxStartIndex;
[Version(Max = 24.15f)] public int RgctxCount;
public int GenericContainerIndex;
public uint Flags;
public int FirstFieldIdx;
public int FirstMethodIdx;
public int FirstEventId;
public int FirstPropertyId;
public int NestedTypesStart;
public int InterfacesStart;
public int VtableStart;
public int InterfaceOffsetsStart;
public ushort MethodCount;
public ushort PropertyCount;
public ushort FieldCount;
public ushort EventCount;
public ushort NestedTypeCount;
public ushort VtableCount;
public ushort InterfacesCount;
public ushort InterfaceOffsetsCount;
// bitfield to portably encode boolean values as single bits
// 01 - valuetype;
// 02 - enumtype;
// 03 - has_finalize;
// 04 - has_cctor;
// 05 - is_blittable;
// 06 - is_import_or_windows_runtime;
// 07-10 - One of nine possible PackingSize values (0, 1, 2, 4, 8, 16, 32, 64, or 128)
// 11 - PackingSize is default
// 12 - ClassSize is default
// 13-16 - One of nine possible PackingSize values (0, 1, 2, 4, 8, 16, 32, 64, or 128) - the specified packing size (even for explicit layouts)
public uint Bitfield;
public uint Token;
public bool IsValueType => (Bitfield >> 0 & 0x1) == 1;
public bool IsEnumType => (Bitfield >> 1 & 0x1) == 1;
public bool HasFinalizer => (Bitfield >> 2 & 0x1) == 1;
public bool HasCctor => (Bitfield >> 3 & 0x1) == 1;
public bool IsBlittable => (Bitfield >> 4 & 0x1) == 1;
public bool IsImportOrWindowsRuntime => (Bitfield >> 5 & 0x1) == 1;
public uint PackingSize => ((Il2CppPackingSizeEnum)(Bitfield >> 6 & 0xF)).NumericalValue();
public bool PackingSizeIsDefault => (Bitfield >> 10 & 0x1) == 1;
public bool ClassSizeIsDefault => (Bitfield >> 11 & 0x1) == 1;
public uint SpecifiedPackingSize => ((Il2CppPackingSizeEnum)(Bitfield >> 12 & 0xF)).NumericalValue();
public bool IsByRefLike => (Bitfield >> 16 & 0x1) == 1;
public TypeAttributes Attributes => (TypeAttributes)Flags;
public Il2CppType RawType => LibCpp2IlMain.Binary!.AllTypes[ByvalTypeIndex];
public Il2CppTypeDefinitionSizes RawSizes
{
get
{
var sizePtr = LibCpp2IlMain.Binary!.TypeDefinitionSizePointers[TypeIndex];
return LibCpp2IlMain.Binary.ReadReadableAtVirtualAddress<Il2CppTypeDefinitionSizes>(sizePtr);
}
}
public int Size => RawSizes.native_size;
public Il2CppInterfaceOffset[] InterfaceOffsets
{
get
{
if (InterfaceOffsetsStart < 0) return [];
return LibCpp2IlMain.TheMetadata!.interfaceOffsets.SubArray(InterfaceOffsetsStart, InterfaceOffsetsCount);
}
}
public MetadataUsage?[] VTable
{
get
{
if (VtableStart < 0) return [];
return LibCpp2IlMain.TheMetadata!.VTableMethodIndices.SubArray(VtableStart, VtableCount).Select(v => MetadataUsage.DecodeMetadataUsage(v, 0)).ToArray();
}
}
public int TypeIndex => LibCpp2IlReflection.GetTypeIndexFromType(this);
public bool IsAbstract => ((TypeAttributes)Flags & TypeAttributes.Abstract) != 0;
public bool IsInterface => ((TypeAttributes)Flags & TypeAttributes.Interface) != 0;
private Il2CppImageDefinition? _cachedDeclaringAssembly;
public Il2CppImageDefinition? DeclaringAssembly
{
get
{
if (_cachedDeclaringAssembly == null)
{
if (LibCpp2IlMain.TheMetadata == null) return null;
LibCpp2ILUtils.PopulateDeclaringAssemblyCache();
}
return _cachedDeclaringAssembly;
}
internal set => _cachedDeclaringAssembly = value;
}
public Il2CppCodeGenModule? CodeGenModule => LibCpp2IlMain.Binary == null ? null : LibCpp2IlMain.Binary.GetCodegenModuleByName(DeclaringAssembly!.Name!);
public Il2CppRGCTXDefinition[] RgctXs
{
get
{
if (LibCpp2IlMain.MetadataVersion < 24.2f)
{
//No codegen modules here.
return LibCpp2IlMain.TheMetadata!.RgctxDefinitions!.Skip(RgctxStartIndex).Take(RgctxCount).ToArray();
}
var cgm = CodeGenModule;
if (cgm == null)
return [];
var rangePair = cgm.RGCTXRanges.FirstOrDefault(r => r.token == Token);
if (rangePair == null)
return [];
return LibCpp2IlMain.Binary!.GetRgctxDataForPair(cgm, rangePair);
}
}
public ulong[] RgctxMethodPointers
{
get
{
var index = LibCpp2IlMain.Binary!.GetCodegenModuleIndexByName(DeclaringAssembly!.Name!);
if (index < 0)
return [];
var pointers = LibCpp2IlMain.Binary!.GetCodegenModuleMethodPointers(index);
return RgctXs
.Where(r => r.type == Il2CppRGCTXDataType.IL2CPP_RGCTX_DATA_METHOD)
.Select(r => pointers[r.MethodIndex])
.ToArray();
}
}
private string? _cachedNamespace;
public string? Namespace
{
get
{
if (_cachedNamespace == null)
_cachedNamespace = LibCpp2IlMain.TheMetadata == null ? null : LibCpp2IlMain.TheMetadata.GetStringFromIndex(NamespaceIndex);
return _cachedNamespace;
}
}
private string? _cachedName;
public string? Name
{
get
{
if (_cachedName == null)
_cachedName = LibCpp2IlMain.TheMetadata == null ? null : LibCpp2IlMain.TheMetadata.GetStringFromIndex(NameIndex);
return _cachedName;
}
}
public string? FullName
{
get
{
if (LibCpp2IlMain.TheMetadata == null)
return null;
if (DeclaringType != null)
return $"{DeclaringType.FullName}+{Name}";
return $"{(string.IsNullOrEmpty(Namespace) ? "" : $"{Namespace}.")}{Name}";
}
}
public Il2CppType? RawBaseType => ParentIndex == -1 ? null : LibCpp2IlMain.Binary!.GetType(ParentIndex);
public Il2CppTypeReflectionData? BaseType => ParentIndex == -1 || LibCpp2IlMain.Binary == null ? null : LibCpp2ILUtils.GetTypeReflectionData(LibCpp2IlMain.Binary!.GetType(ParentIndex));
public Il2CppFieldDefinition[]? Fields
{
get
{
if (LibCpp2IlMain.TheMetadata == null)
return null;
if (FirstFieldIdx < 0 || FieldCount == 0)
return [];
var ret = new Il2CppFieldDefinition[FieldCount];
Array.Copy(LibCpp2IlMain.TheMetadata.fieldDefs, FirstFieldIdx, ret, 0, FieldCount);
return ret;
}
}
public FieldAttributes[]? FieldAttributes => Fields?
.Select(f => f.typeIndex)
.Select(idx => LibCpp2IlMain.Binary!.GetType(idx))
.Select(t => (FieldAttributes)t.Attrs)
.ToArray();
public object?[]? FieldDefaults => Fields?
.Select((f, idx) => (f.FieldIndex, FieldAttributes![idx]))
.Select(tuple => (tuple.Item2 & System.Reflection.FieldAttributes.HasDefault) != 0 ? LibCpp2IlMain.TheMetadata!.GetFieldDefaultValueFromIndex(tuple.FieldIndex) : null)
.Select(def => def == null ? null : LibCpp2ILUtils.GetDefaultValue(def.dataIndex, def.typeIndex))
.ToArray();
public Il2CppFieldReflectionData[]? FieldInfos
{
get
{
var fields = Fields;
var attributes = FieldAttributes;
var defaults = FieldDefaults;
if (fields == null || attributes == null || defaults == null)
return null;
var ret = new Il2CppFieldReflectionData[FieldCount];
for (var i = 0; i < FieldCount; i++)
{
ret[i] = new(
fields[i],
attributes![i],
defaults![i],
i,
LibCpp2IlMain.Binary!.GetFieldOffsetFromIndex(TypeIndex, i, fields[i].FieldIndex, IsValueType, attributes[i].HasFlag(System.Reflection.FieldAttributes.Static))
);
}
return ret;
}
}
public Il2CppMethodDefinition[]? Methods
{
get
{
if (LibCpp2IlMain.TheMetadata == null)
return null;
if (FirstMethodIdx < 0 || MethodCount == 0)
return [];
var ret = new Il2CppMethodDefinition[MethodCount];
Array.Copy(LibCpp2IlMain.TheMetadata.methodDefs, FirstMethodIdx, ret, 0, MethodCount);
return ret;
}
}
public Il2CppPropertyDefinition[]? Properties
{
get
{
if (LibCpp2IlMain.TheMetadata == null)
return null;
if (FirstPropertyId < 0 || PropertyCount == 0)
return [];
var ret = new Il2CppPropertyDefinition[PropertyCount];
Array.Copy(LibCpp2IlMain.TheMetadata.propertyDefs, FirstPropertyId, ret, 0, PropertyCount);
return ret.Select(p =>
{
p.DeclaringType = this;
return p;
}).ToArray();
}
}
public Il2CppEventDefinition[]? Events => LibCpp2IlMain.TheMetadata == null
? null
: LibCpp2IlMain.TheMetadata.eventDefs.Skip(FirstEventId).Take(EventCount).Select(e =>
{
e.DeclaringType = this;
return e;
}).ToArray();
public Il2CppTypeDefinition[]? NestedTypes => LibCpp2IlMain.TheMetadata == null ? null : LibCpp2IlMain.TheMetadata.nestedTypeIndices.Skip(NestedTypesStart).Take(NestedTypeCount).Select(idx => LibCpp2IlMain.TheMetadata.typeDefs[idx]).ToArray();
public Il2CppType[] RawInterfaces => LibCpp2IlMain.TheMetadata == null || LibCpp2IlMain.Binary == null
? []
: LibCpp2IlMain.TheMetadata.interfaceIndices
.Skip(InterfacesStart)
.Take(InterfacesCount)
.Select(idx => LibCpp2IlMain.Binary.GetType(idx))
.ToArray();
public Il2CppTypeReflectionData[]? Interfaces => LibCpp2IlMain.TheMetadata == null || LibCpp2IlMain.Binary == null
? null
: RawInterfaces
.Select(LibCpp2ILUtils.GetTypeReflectionData)
.ToArray();
public Il2CppTypeDefinition? DeclaringType => LibCpp2IlMain.TheMetadata == null || LibCpp2IlMain.Binary == null || DeclaringTypeIndex < 0 ? null : LibCpp2IlMain.TheMetadata.typeDefs[LibCpp2IlMain.Binary.GetType(DeclaringTypeIndex).Data.ClassIndex];
public Il2CppTypeDefinition? ElementType => LibCpp2IlMain.TheMetadata == null || LibCpp2IlMain.Binary == null || ElementTypeIndex < 0 ? null : LibCpp2IlMain.TheMetadata.typeDefs[LibCpp2IlMain.Binary.GetType(ElementTypeIndex).Data.ClassIndex];
public Il2CppGenericContainer? GenericContainer => GenericContainerIndex < 0 ? null : LibCpp2IlMain.TheMetadata?.genericContainers[GenericContainerIndex];
public Il2CppType EnumUnderlyingType => IsEnumType ? LibCpp2IlMain.Binary!.GetType(ElementTypeIndex) : throw new InvalidOperationException("Cannot get the underlying type of a non-enum type.");
public override string? ToString()
{
if (LibCpp2IlMain.TheMetadata == null)
return base.ToString();
return $"Il2CppTypeDefinition[namespace='{Namespace}', name='{Name}', parentType={BaseType?.ToString() ?? "null"}, assembly={DeclaringAssembly}]";
}
public override void Read(ClassReadingBinaryReader reader)
{
NameIndex = reader.ReadInt32();
NamespaceIndex = reader.ReadInt32();
if (IsAtMost(24f))
CustomAttributeIndex = reader.ReadInt32();
ByvalTypeIndex = reader.ReadInt32();
if (IsLessThan(27f))
ByrefTypeIndex = reader.ReadInt32();
DeclaringTypeIndex = reader.ReadInt32();
ParentIndex = reader.ReadInt32();
ElementTypeIndex = reader.ReadInt32();
if (IsAtMost(24.15f))
{
RgctxStartIndex = reader.ReadInt32();
RgctxCount = reader.ReadInt32();
}
GenericContainerIndex = reader.ReadInt32();
Flags = reader.ReadUInt32();
FirstFieldIdx = reader.ReadInt32();
FirstMethodIdx = reader.ReadInt32();
FirstEventId = reader.ReadInt32();
FirstPropertyId = reader.ReadInt32();
NestedTypesStart = reader.ReadInt32();
InterfacesStart = reader.ReadInt32();
VtableStart = reader.ReadInt32();
InterfaceOffsetsStart = reader.ReadInt32();
MethodCount = reader.ReadUInt16();
PropertyCount = reader.ReadUInt16();
FieldCount = reader.ReadUInt16();
EventCount = reader.ReadUInt16();
NestedTypeCount = reader.ReadUInt16();
VtableCount = reader.ReadUInt16();
InterfacesCount = reader.ReadUInt16();
InterfaceOffsetsCount = reader.ReadUInt16();
Bitfield = reader.ReadUInt32();
Token = reader.ReadUInt32();
}
}