Rework metadata struct loading to use new struct versioning

This commit is contained in:
LukeFZ
2024-08-17 13:52:09 +02:00
parent 6c59434984
commit 43d7433e12
69 changed files with 496 additions and 382 deletions

View File

@@ -94,6 +94,9 @@ namespace Il2CppInspector
long[] ReadMappedWordArray(ulong uiAddr, int count);
List<U> ReadMappedObjectPointerArray<U>(ulong uiAddr, int count) where U : new();
ulong ReadMappedUWord(ulong uiAddr);
ulong[] ReadMappedUWordArray(ulong uiAddr, int count);
void WriteEndianBytes(byte[] bytes);
void Write(long int64);
void Write(ulong uint64);
@@ -144,6 +147,9 @@ namespace Il2CppInspector
public ImmutableArray<TType> ReadVersionedObjectArray<TType>(long count)
where TType : IReadable, new();
public ImmutableArray<TType> ReadMappedVersionedObjectPointerArray<TType>(ulong addr, int count)
where TType : IReadable, new();
}
public class FileFormatStream
@@ -356,5 +362,32 @@ namespace Il2CppInspector
public ImmutableArray<TType> ReadMappedVersionedObjectArray<TType>(ulong addr, long count) where TType : IReadable, new()
=> ReadVersionedObjectArray<TType>(MapVATR(addr), count);
public ImmutableArray<TType> ReadMappedVersionedObjectPointerArray<TType>(ulong addr, int count)
where TType : IReadable, new()
{
var pointers = ReadMappedUWordArray(addr, count);
var builder = ImmutableArray.CreateBuilder<TType>((int)count);
for (long i = 0; i < count; i++)
builder.Add(ReadMappedVersionedObject<TType>(pointers[i]));
return builder.MoveToImmutable();
}
public ulong ReadMappedUWord(ulong uiAddr)
{
Position = MapVATR(uiAddr);
return ReadNUInt();
}
public ulong[] ReadMappedUWordArray(ulong uiAddr, int count)
{
Position = MapVATR(uiAddr);
var arr = new ulong[count];
for (int i = 0; i < count; i++)
arr[i] = ReadNUInt();
return arr;
}
}
}

View File

@@ -4,11 +4,11 @@ using System.Diagnostics;
using System.IO;
using System.Linq;
using dnlib.DotNet;
using Il2CppInspector.Next;
using Il2CppInspector.Next.BinaryMetadata;
using Il2CppInspector.Next.Metadata;
using Il2CppInspector.Reflection;
using Il2CppInspector.Utils;
using NoisyCowStudios.Bin2Object;
namespace Il2CppInspector
{
@@ -16,7 +16,7 @@ namespace Il2CppInspector
{
private readonly Il2CppInspector _inspector;
private readonly Assembly _assembly;
private readonly BinaryObjectStream _data;
private readonly BinaryObjectStreamReader _data;
private readonly uint _start;
private readonly uint _end;
@@ -26,7 +26,7 @@ namespace Il2CppInspector
public uint Count { get; }
public CustomAttributeDataReader(Il2CppInspector inspector, Assembly assembly, BinaryObjectStream data, uint startOffset, uint endOffset)
public CustomAttributeDataReader(Il2CppInspector inspector, Assembly assembly, BinaryObjectStreamReader data, uint startOffset, uint endOffset)
{
_inspector = inspector;
_assembly = assembly;
@@ -146,8 +146,8 @@ namespace Il2CppInspector
private TypeInfo ConvertTypeDef(Il2CppTypeDefinition typeDef, Il2CppTypeEnum type)
=> typeDef.IsValid
? _assembly.Model.GetTypeDefinitionFromTypeEnum(type)
: _assembly.Model.TypesByDefinitionIndex[Array.IndexOf(_inspector.TypeDefinitions, typeDef)];
? _assembly.Model.TypesByDefinitionIndex[_inspector.TypeDefinitions.IndexOf(typeDef)]
: _assembly.Model.GetTypeDefinitionFromTypeEnum(type);
private (TypeInfo, int) ReadCustomAttributeNamedArgumentClassAndIndex(TypeInfo attrInfo)
{

View File

@@ -8,6 +8,7 @@
using Il2CppInspector.Next;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Linq;
@@ -37,16 +38,16 @@ namespace Il2CppInspector
public ulong CodeRegistrationPointer { get; private set; }
public ulong MetadataRegistrationPointer { get; private set; }
public ulong RegistrationFunctionPointer { get; private set; }
public Dictionary<string, ulong> CodeGenModulePointers { get; } = new Dictionary<string, ulong>();
public Dictionary<string, ulong> CodeGenModulePointers { get; } = new();
// Only for <=v24.1
public ulong[] GlobalMethodPointers { get; set; }
// Only for >=v24.2
public Dictionary<Il2CppCodeGenModule, ulong[]> ModuleMethodPointers { get; set; } = new Dictionary<Il2CppCodeGenModule, ulong[]>();
public Dictionary<Il2CppCodeGenModule, ulong[]> ModuleMethodPointers { get; set; } = new();
// Only for >=v24.2. In earlier versions, invoker indices are stored in Il2CppMethodDefinition in the metadata file
public Dictionary<Il2CppCodeGenModule, int[]> MethodInvokerIndices { get; set; } = new Dictionary<Il2CppCodeGenModule, int[]>();
public Dictionary<Il2CppCodeGenModule, ImmutableArray<int>> MethodInvokerIndices { get; set; } = new();
// NOTE: In versions <21 and earlier releases of v21, use FieldOffsets:
// global field index => field offset
@@ -54,7 +55,7 @@ namespace Il2CppInspector
// type index => RVA in image where the list of field offsets for the type start (4 bytes per field)
// Negative field offsets from start of each function
public uint[] FieldOffsets { get; private set; }
public ImmutableArray<uint> FieldOffsets { get; private set; }
// Pointers to field offsets
public long[] FieldOffsetPointers { get; private set; }
@@ -68,13 +69,13 @@ namespace Il2CppInspector
public ulong[] MethodInvokePointers { get; private set; }
// Version 16 and below: method references for vtable
public uint[] VTableMethodReferences { get; private set; }
public ImmutableArray<uint> VTableMethodReferences { get; private set; }
// Generic method specs for vtables
public Il2CppMethodSpec[] MethodSpecs { get; private set; }
public ImmutableArray<Il2CppMethodSpec> MethodSpecs { get; private set; }
// List of run-time concrete generic class and method signatures
public List<Il2CppGenericInst> GenericInstances { get; private set; }
public ImmutableArray<Il2CppGenericInst> GenericInstances { get; private set; }
// List of constructed generic method function pointers corresponding to each possible method instantiation
public Dictionary<Il2CppMethodSpec, ulong> GenericMethodPointers { get; } = new Dictionary<Il2CppMethodSpec, ulong>();
@@ -83,7 +84,7 @@ namespace Il2CppInspector
public Dictionary<Il2CppMethodSpec, int> GenericMethodInvokerIndices { get; } = new Dictionary<Il2CppMethodSpec, int>();
// Every type reference (TypeRef) sorted by index
public List<Il2CppType> TypeReferences { get; private set; }
public ImmutableArray<Il2CppType> TypeReferences { get; private set; }
// Every type reference index sorted by virtual address
public Dictionary<ulong, int> TypeReferenceIndicesByAddress { get; private set; }
@@ -92,7 +93,7 @@ namespace Il2CppInspector
// One assembly may contain multiple modules
public Dictionary<string, Il2CppCodeGenModule> Modules { get; private set; }
public List<Il2CppTypeDefinitionSizes> TypeDefinitionSizes { get; private set; }
public ImmutableArray<Il2CppTypeDefinitionSizes> TypeDefinitionSizes { get; private set; }
// Status update callback
private EventHandler<string> OnStatusUpdate { get; set; }
@@ -280,30 +281,8 @@ namespace Il2CppInspector
Console.WriteLine("MetadataRegistration struct found at 0x{0:X16} (file offset 0x{1:X8})", Image.Bits == 32 ? metadataRegistration & 0xffff_ffff : metadataRegistration, Image.MapVATR(metadataRegistration));
// Root structures from which we find everything else
CodeRegistration = Image.ReadMappedObject<Il2CppCodeRegistration>(codeRegistration);
MetadataRegistration = Image.ReadMappedObject<Il2CppMetadataRegistration>(metadataRegistration);
// genericAdjustorThunks was inserted before invokerPointersCount in 24.5 and 27.1
// pointer expected if we need to bump version
if (Image.Version == MetadataVersions.V244 && CodeRegistration.InvokerPointersCount > 0x50000)
{
Image.Version = MetadataVersions.V245;
CodeRegistration = Image.ReadMappedObject<Il2CppCodeRegistration>(codeRegistration);
}
if (Image.Version == MetadataVersions.V244 && CodeRegistration.ReversePInvokeWrapperCount > 0x50000) {
Image.Version = MetadataVersions.V245;
codeRegistration -= 1 * pointerSize;
CodeRegistration = Image.ReadMappedObject<Il2CppCodeRegistration>(codeRegistration);
}
if ((Image.Version == MetadataVersions.V290 || Image.Version == MetadataVersions.V310) &&
(long)CodeRegistration.GenericMethodPointersCount - MetadataRegistration.GenericMethodTableCount > 0x10000)
{
Image.Version = new StructVersion(Image.Version.Major, 1, Image.Version.Tag);
codeRegistration -= 2 * pointerSize;
CodeRegistration = Image.ReadMappedObject<Il2CppCodeRegistration>(codeRegistration);
}
CodeRegistration = Image.ReadMappedVersionedObject<Il2CppCodeRegistration>(codeRegistration);
MetadataRegistration = Image.ReadMappedVersionedObject<Il2CppMetadataRegistration>(metadataRegistration);
// Plugin hook to pre-process binary
isModified |= PluginHooks.PreProcessBinary(this).IsStreamModified;
@@ -326,7 +305,7 @@ namespace Il2CppInspector
// The global method pointer list was deprecated in v24.2 in favour of Il2CppCodeGenModule
if (Image.Version <= MetadataVersions.V241)
GlobalMethodPointers = Image.ReadMappedArray<ulong>(CodeRegistration.MethodPointers, (int) CodeRegistration.MethodPointersCount);
GlobalMethodPointers = Image.ReadMappedUWordArray(CodeRegistration.MethodPointers, (int) CodeRegistration.MethodPointersCount);
// After v24 method pointers and RGCTX data were stored in Il2CppCodeGenModules
if (Image.Version >= MetadataVersions.V242) {
@@ -336,12 +315,12 @@ namespace Il2CppInspector
// if this changes we'll have to get smarter about disambiguating these two.
if (CodeRegistration.CodeGenModulesCount == 0) {
Image.Version = MetadataVersions.V243;
CodeRegistration = Image.ReadMappedObject<Il2CppCodeRegistration>(codeRegistration);
CodeRegistration = Image.ReadMappedVersionedObject<Il2CppCodeRegistration>(codeRegistration);
}
// Array of pointers to Il2CppCodeGenModule
var codeGenModulePointers = Image.ReadMappedArray<ulong>(CodeRegistration.CodeGenModules, (int) CodeRegistration.CodeGenModulesCount);
var modules = Image.ReadMappedObjectPointerArray<Il2CppCodeGenModule>(CodeRegistration.CodeGenModules, (int) CodeRegistration.CodeGenModulesCount);
var codeGenModulePointers = Image.ReadMappedUWordArray(CodeRegistration.CodeGenModules, (int) CodeRegistration.CodeGenModulesCount);
var modules = Image.ReadMappedVersionedObjectPointerArray<Il2CppCodeGenModule>(CodeRegistration.CodeGenModules, (int) CodeRegistration.CodeGenModulesCount);
foreach (var mp in modules.Zip(codeGenModulePointers, (m, p) => new { Module = m, Pointer = p })) {
var module = mp.Module;
@@ -355,13 +334,13 @@ namespace Il2CppInspector
// the entire method pointer array will be NULL values, causing the methodPointer to be mapped to .bss
// and therefore out of scope of the binary image
try {
ModuleMethodPointers.Add(module, Image.ReadMappedArray<ulong>(module.MethodPointers, (int) module.MethodPointerCount));
ModuleMethodPointers.Add(module, Image.ReadMappedUWordArray(module.MethodPointers, (int) module.MethodPointerCount));
} catch (InvalidOperationException) {
ModuleMethodPointers.Add(module, new ulong[module.MethodPointerCount]);
}
// Read method invoker pointer indices - one per method
MethodInvokerIndices.Add(module, Image.ReadMappedArray<int>(module.InvokerIndices, (int) module.MethodPointerCount));
MethodInvokerIndices.Add(module, Image.ReadMappedPrimitiveArray<int>(module.InvokerIndices, (int) module.MethodPointerCount));
}
}
@@ -381,24 +360,24 @@ namespace Il2CppInspector
// All older versions use values directly in the array
if (!fieldOffsetsArePointers)
FieldOffsets = Image.ReadMappedArray<uint>(MetadataRegistration.FieldOffsets, (int)MetadataRegistration.FieldOffsetsCount);
FieldOffsets = Image.ReadMappedPrimitiveArray<uint>(MetadataRegistration.FieldOffsets, (int)MetadataRegistration.FieldOffsetsCount);
else
FieldOffsetPointers = Image.ReadMappedWordArray(MetadataRegistration.FieldOffsets, (int)MetadataRegistration.FieldOffsetsCount);
// Type references (pointer array)
var typeRefPointers = Image.ReadMappedArray<ulong>(MetadataRegistration.Types, (int) MetadataRegistration.TypesCount);
var typeRefPointers = Image.ReadMappedUWordArray(MetadataRegistration.Types, (int) MetadataRegistration.TypesCount);
TypeReferenceIndicesByAddress = typeRefPointers.Zip(Enumerable.Range(0, typeRefPointers.Length), (a, i) => new { a, i }).ToDictionary(x => x.a, x => x.i);
TypeReferences = Image.ReadMappedObjectPointerArray<Il2CppType>(MetadataRegistration.Types, (int)MetadataRegistration.TypesCount);
TypeReferences = Image.ReadMappedVersionedObjectPointerArray<Il2CppType>(MetadataRegistration.Types, (int)MetadataRegistration.TypesCount);
// Custom attribute constructors (function pointers)
// This is managed in Il2CppInspector for metadata >= 27
if (Image.Version < MetadataVersions.V270) {
CustomAttributeGenerators = Image.ReadMappedArray<ulong>(CodeRegistration.CustomAttributeGenerators, (int) CodeRegistration.CustomAttributeCount);
CustomAttributeGenerators = Image.ReadMappedUWordArray(CodeRegistration.CustomAttributeGenerators, (int) CodeRegistration.CustomAttributeCount);
}
// Method.Invoke function pointers
MethodInvokePointers = Image.ReadMappedArray<ulong>(CodeRegistration.InvokerPointers, (int) CodeRegistration.InvokerPointersCount);
MethodInvokePointers = Image.ReadMappedUWordArray(CodeRegistration.InvokerPointers, (int) CodeRegistration.InvokerPointersCount);
// TODO: Function pointers as shown below
// reversePInvokeWrappers
@@ -408,24 +387,24 @@ namespace Il2CppInspector
// >=23: interopData
if (Image.Version < MetadataVersions.V190) {
VTableMethodReferences = Image.ReadMappedArray<uint>(MetadataRegistration.MethodReferences, (int)MetadataRegistration.MethodReferencesCount);
VTableMethodReferences = Image.ReadMappedPrimitiveArray<uint>(MetadataRegistration.MethodReferences, (int)MetadataRegistration.MethodReferencesCount);
}
// Generic type and method specs (open and closed constructed types)
MethodSpecs = Image.ReadMappedArray<Il2CppMethodSpec>(MetadataRegistration.MethodSpecs, (int) MetadataRegistration.MethodSpecsCount);
MethodSpecs = Image.ReadMappedVersionedObjectArray<Il2CppMethodSpec>(MetadataRegistration.MethodSpecs, (int) MetadataRegistration.MethodSpecsCount);
// Concrete generic class and method signatures
GenericInstances = Image.ReadMappedObjectPointerArray<Il2CppGenericInst>(MetadataRegistration.GenericInsts, (int) MetadataRegistration.GenericInstsCount);
GenericInstances = Image.ReadMappedVersionedObjectPointerArray<Il2CppGenericInst>(MetadataRegistration.GenericInsts, (int) MetadataRegistration.GenericInstsCount);
// Concrete generic method pointers
var genericMethodPointers = Image.ReadMappedArray<ulong>(CodeRegistration.GenericMethodPointers, (int) CodeRegistration.GenericMethodPointersCount);
var genericMethodTable = Image.ReadMappedArray<Il2CppGenericMethodFunctionsDefinitions>(MetadataRegistration.GenericMethodTable, (int) MetadataRegistration.GenericMethodTableCount);
var genericMethodPointers = Image.ReadMappedUWordArray(CodeRegistration.GenericMethodPointers, (int) CodeRegistration.GenericMethodPointersCount);
var genericMethodTable = Image.ReadMappedVersionedObjectArray<Il2CppGenericMethodFunctionsDefinitions>(MetadataRegistration.GenericMethodTable, (int) MetadataRegistration.GenericMethodTableCount);
foreach (var tableEntry in genericMethodTable) {
GenericMethodPointers.Add(MethodSpecs[tableEntry.GenericMethodIndex], genericMethodPointers[tableEntry.Indices.MethodIndex]);
GenericMethodInvokerIndices.Add(MethodSpecs[tableEntry.GenericMethodIndex], tableEntry.Indices.InvokerIndex);
}
TypeDefinitionSizes = Image.ReadMappedObjectPointerArray<Il2CppTypeDefinitionSizes>(
TypeDefinitionSizes = Image.ReadMappedVersionedObjectPointerArray<Il2CppTypeDefinitionSizes>(
MetadataRegistration.TypeDefinitionsSizes, (int) MetadataRegistration.TypeDefinitionsSizesCount);
// Plugin hook to pre-process binary

View File

@@ -9,6 +9,7 @@ using Il2CppInspector.Utils;
using NoisyCowStudios.Bin2Object;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.IO.Compression;
using System.Linq;
@@ -41,38 +42,38 @@ namespace Il2CppInspector
public Dictionary<int, string> Strings => Metadata.Strings;
public string[] StringLiterals => Metadata.StringLiterals;
public Il2CppTypeDefinition[] TypeDefinitions => Metadata.Types;
public Il2CppAssemblyDefinition[] Assemblies => Metadata.Assemblies;
public Il2CppImageDefinition[] Images => Metadata.Images;
public Il2CppMethodDefinition[] Methods => Metadata.Methods;
public Il2CppParameterDefinition[] Params => Metadata.Params;
public Il2CppFieldDefinition[] Fields => Metadata.Fields;
public Il2CppPropertyDefinition[] Properties => Metadata.Properties;
public Il2CppEventDefinition[] Events => Metadata.Events;
public Il2CppGenericContainer[] GenericContainers => Metadata.GenericContainers;
public Il2CppGenericParameter[] GenericParameters => Metadata.GenericParameters;
public int[] GenericConstraintIndices => Metadata.GenericConstraintIndices;
public Il2CppCustomAttributeTypeRange[] AttributeTypeRanges => Metadata.AttributeTypeRanges;
public Il2CppCustomAttributeDataRange[] AttributeDataRanges => Metadata.AttributeDataRanges;
public Il2CppInterfaceOffsetPair[] InterfaceOffsets => Metadata.InterfaceOffsets;
public int[] InterfaceUsageIndices => Metadata.InterfaceUsageIndices;
public int[] NestedTypeIndices => Metadata.NestedTypeIndices;
public int[] AttributeTypeIndices => Metadata.AttributeTypeIndices;
public uint[] VTableMethodIndices => Metadata.VTableMethodIndices;
public Il2CppFieldRef[] FieldRefs => Metadata.FieldRefs;
public ImmutableArray<Il2CppTypeDefinition> TypeDefinitions => Metadata.Types;
public ImmutableArray<Il2CppAssemblyDefinition> Assemblies => Metadata.Assemblies;
public ImmutableArray<Il2CppImageDefinition> Images => Metadata.Images;
public ImmutableArray<Il2CppMethodDefinition> Methods => Metadata.Methods;
public ImmutableArray<Il2CppParameterDefinition> Params => Metadata.Params;
public ImmutableArray<Il2CppFieldDefinition> Fields => Metadata.Fields;
public ImmutableArray<Il2CppPropertyDefinition> Properties => Metadata.Properties;
public ImmutableArray<Il2CppEventDefinition> Events => Metadata.Events;
public ImmutableArray<Il2CppGenericContainer> GenericContainers => Metadata.GenericContainers;
public ImmutableArray<Il2CppGenericParameter> GenericParameters => Metadata.GenericParameters;
public ImmutableArray<int> GenericConstraintIndices => Metadata.GenericConstraintIndices;
public ImmutableArray<Il2CppCustomAttributeTypeRange> AttributeTypeRanges => Metadata.AttributeTypeRanges;
public ImmutableArray<Il2CppCustomAttributeDataRange> AttributeDataRanges => Metadata.AttributeDataRanges;
public ImmutableArray<Il2CppInterfaceOffsetPair> InterfaceOffsets => Metadata.InterfaceOffsets;
public ImmutableArray<int> InterfaceUsageIndices => Metadata.InterfaceUsageIndices;
public ImmutableArray<int> NestedTypeIndices => Metadata.NestedTypeIndices;
public ImmutableArray<int> AttributeTypeIndices => Metadata.AttributeTypeIndices;
public ImmutableArray<uint> VTableMethodIndices => Metadata.VTableMethodIndices;
public ImmutableArray<Il2CppFieldRef> FieldRefs => Metadata.FieldRefs;
public Dictionary<int, (ulong, object)> FieldDefaultValue { get; } = new Dictionary<int, (ulong, object)>();
public Dictionary<int, (ulong, object)> ParameterDefaultValue { get; } = new Dictionary<int, (ulong, object)>();
public List<long> FieldOffsets { get; }
public List<Il2CppType> TypeReferences => Binary.TypeReferences;
public ImmutableArray<Il2CppType> TypeReferences => Binary.TypeReferences;
public Dictionary<ulong, int> TypeReferenceIndicesByAddress => Binary.TypeReferenceIndicesByAddress;
public List<Il2CppGenericInst> GenericInstances => Binary.GenericInstances;
public ImmutableArray<Il2CppGenericInst> GenericInstances => Binary.GenericInstances;
public Dictionary<string, Il2CppCodeGenModule> Modules => Binary.Modules;
public ulong[] CustomAttributeGenerators { get; }
public ulong[] MethodInvokePointers { get; }
public Il2CppMethodSpec[] MethodSpecs => Binary.MethodSpecs;
public ImmutableArray<Il2CppMethodSpec> MethodSpecs => Binary.MethodSpecs;
public Dictionary<Il2CppMethodSpec, ulong> GenericMethodPointers { get; }
public Dictionary<Il2CppMethodSpec, int> GenericMethodInvokerIndices => Binary.GenericMethodInvokerIndices;
public List<Il2CppTypeDefinitionSizes> TypeDefinitionSizes => Binary.TypeDefinitionSizes;
public ImmutableArray<Il2CppTypeDefinitionSizes> TypeDefinitionSizes => Binary.TypeDefinitionSizes;
// TODO: Finish all file access in the constructor and eliminate the need for this
public IFileFormatStream BinaryImage => Binary.Image;
@@ -121,7 +122,7 @@ namespace Il2CppInspector
// Unfortunately the value supplied in MetadataRegistration.matadataUsagesCount seems to be incorrect,
// so we have to calculate the correct number of usages above before reading the usage address list from the binary
var count = usages.Keys.Max() + 1;
var addresses = Binary.Image.ReadMappedArray<ulong>(Binary.MetadataRegistration.MetadataUsages, (int) count);
var addresses = Binary.Image.ReadMappedUWordArray(Binary.MetadataRegistration.MetadataUsages, (int) count);
foreach (var usage in usages)
usage.Value.SetAddress(addresses[usage.Key]);
@@ -161,7 +162,7 @@ namespace Il2CppInspector
{
return usage.Type switch
{
MetadataUsageType.TypeInfo or MetadataUsageType.Type => TypeReferences.Count > usage.SourceIndex,
MetadataUsageType.TypeInfo or MetadataUsageType.Type => TypeReferences.Length > usage.SourceIndex,
MetadataUsageType.MethodDef => Methods.Length > usage.SourceIndex,
MetadataUsageType.FieldInfo or MetadataUsageType.FieldRva => FieldRefs.Length > usage.SourceIndex,
MetadataUsageType.StringLiteral => StringLiterals.Length > usage.SourceIndex,
@@ -347,7 +348,7 @@ namespace Il2CppInspector
// Version >= 24.2
var methodInModule = (methodDef.Token & 0xffffff);
return Binary.MethodInvokerIndices[module][methodInModule - 1];
return Binary.MethodInvokerIndices[module][(int)methodInModule - 1];
}
public MetadataUsage[] GetVTable(Il2CppTypeDefinition definition) {

View File

@@ -11,6 +11,7 @@ using System.Linq;
using System.Text;
using Il2CppInspector.Next;
using Il2CppInspector.Next.BinaryMetadata;
using VersionedSerialization;
namespace Il2CppInspector
{
@@ -147,7 +148,7 @@ namespace Il2CppInspector
potentialCodeGenModules - (ulong) i * ptrSize, 1))
{
var expectedImageCountPtr = potentialCodeRegistrationPtr - ptrSize;
var expectedImageCount = ptrSize == 4 ? Image.ReadMappedInt32(expectedImageCountPtr) : Image.ReadMappedInt64(expectedImageCountPtr);
var expectedImageCount = Image.ReadMappedWord(expectedImageCountPtr);
if (expectedImageCount == imagesCount)
return potentialCodeRegistrationPtr;
}
@@ -206,11 +207,12 @@ namespace Il2CppInspector
// pCodeGenModules is the last field in CodeRegistration so we subtract the size of one pointer from the struct size
codeRegistration = codeRegVa - ((ulong) metadata.Sizeof(typeof(Il2CppCodeRegistration), Image.Version, Image.Bits / 8) - ptrSize);
var codeRegSize = (ulong)Il2CppCodeRegistration.Size(Image.Version, Image.Bits == 32);
codeRegistration = codeRegVa - codeRegSize - ptrSize;
// In v24.3, windowsRuntimeFactoryTable collides with codeGenModules. So far no samples have had windowsRuntimeFactoryCount > 0;
// if this changes we'll have to get smarter about disambiguating these two.
var cr = Image.ReadMappedObject<Il2CppCodeRegistration>(codeRegistration);
var cr = Image.ReadMappedVersionedObject<Il2CppCodeRegistration>(codeRegistration);
if (Image.Version == MetadataVersions.V242 && cr.InteropDataCount == 0) {
Image.Version = MetadataVersions.V243;
@@ -224,6 +226,22 @@ namespace Il2CppInspector
Image.Version = MetadataVersions.V271;
codeRegistration -= ptrSize;
}
// genericAdjustorThunks was inserted before invokerPointersCount in 24.5 and 27.1
// pointer expected if we need to bump version
if (Image.Version == MetadataVersions.V244 && cr.InvokerPointersCount > 0x50000)
{
Image.Version = MetadataVersions.V245;
codeRegistration += 1 * ptrSize;
cr = Image.ReadMappedVersionedObject<Il2CppCodeRegistration>(codeRegistration);
}
if ((Image.Version == MetadataVersions.V290 || Image.Version == MetadataVersions.V310) &&
cr.InteropData + cr.InteropDataCount >= (ulong)Image.Length)
{
Image.Version = new StructVersion(Image.Version.Major, 1, Image.Version.Tag);
cr = Image.ReadMappedVersionedObject<Il2CppCodeRegistration>(codeRegistration);
}
}
// Find CodeRegistration
@@ -239,7 +257,7 @@ namespace Il2CppInspector
// the count of custom attribute generators; the distance between them
// depends on the il2cpp version so we just use ReadMappedObject to simplify the math
foreach (var va in vas) {
var cr = Image.ReadMappedObject<Il2CppCodeRegistration>(va);
var cr = Image.ReadMappedVersionedObject<Il2CppCodeRegistration>(va);
if (cr.CustomAttributeCount == metadata.AttributeTypeRanges.Length)
codeRegistration = va;
@@ -255,15 +273,16 @@ namespace Il2CppInspector
// Find TypeDefinitionsSizesCount (4th last field) then work back to the start of the struct
// This saves us from guessing where metadataUsagesCount is later
var mrSize = (ulong) metadata.Sizeof(typeof(Il2CppMetadataRegistration), Image.Version, Image.Bits / 8);
var mrSize = (ulong)Il2CppMetadataRegistration.Size(Image.Version, Image.Bits == 32);
var typesLength = (ulong) metadata.Types.Length;
vas = FindAllMappedWords(imageBytes, typesLength).Select(a => a - mrSize + ptrSize * 4);
// >= 19 && < 27
if (Image.Version < MetadataVersions.V270)
foreach (var va in vas) {
var mr = Image.ReadMappedObject<Il2CppMetadataRegistration>(va);
foreach (var va in vas)
{
var mr = Image.ReadMappedVersionedObject<Il2CppMetadataRegistration>(va);
if (mr.MetadataUsagesCount == (ulong) metadata.MetadataUsageLists.Length)
metadataRegistration = va;
}
@@ -273,22 +292,17 @@ namespace Il2CppInspector
// Synonyms: copying, piracy, theft, strealing, infringement of copyright
// >= 27
else {
// We're going to just sanity check all of the fields
// All counts should be under a certain threshold
// All pointers should be mappable to the binary
var mrFieldCount = mrSize / (ulong) (Image.Bits / 8);
foreach (var va in vas) {
var mrWords = Image.ReadMappedWordArray(va, (int) mrFieldCount);
// Even field indices are counts, odd field indices are pointers
bool ok = true;
for (var i = 0; i < mrWords.Length && ok; i++) {
ok = i % 2 == 0 || Image.TryMapVATR((ulong) mrWords[i], out _);
}
if (ok)
else
{
foreach (var va in vas)
{
var mr = Image.ReadMappedVersionedObject<Il2CppMetadataRegistration>(va);
if (mr.TypeDefinitionsSizesCount == metadata.Types.Length
&& mr.FieldOffsetsCount == metadata.Types.Length)
{
metadataRegistration = va;
break;
}
}
}
if (metadataRegistration == 0)

View File

@@ -7,6 +7,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Reflection;
@@ -21,30 +22,30 @@ namespace Il2CppInspector
{
public Il2CppGlobalMetadataHeader Header { get; set; }
public Il2CppAssemblyDefinition[] Assemblies { get; set; }
public Il2CppImageDefinition[] Images { get; set; }
public Il2CppTypeDefinition[] Types { get; set; }
public Il2CppMethodDefinition[] Methods { get; set; }
public Il2CppParameterDefinition[] Params { get; set; }
public Il2CppFieldDefinition[] Fields { get; set; }
public Il2CppFieldDefaultValue[] FieldDefaultValues { get; set; }
public Il2CppParameterDefaultValue[] ParameterDefaultValues { get; set; }
public Il2CppPropertyDefinition[] Properties { get; set; }
public Il2CppEventDefinition[] Events { get; set; }
public Il2CppGenericContainer[] GenericContainers { get; set; }
public Il2CppGenericParameter[] GenericParameters { get; set; }
public Il2CppCustomAttributeTypeRange[] AttributeTypeRanges { get; set; }
public Il2CppCustomAttributeDataRange[] AttributeDataRanges { get; set; }
public Il2CppInterfaceOffsetPair[] InterfaceOffsets { get; set; }
public Il2CppMetadataUsageList[] MetadataUsageLists { get; set; }
public Il2CppMetadataUsagePair[] MetadataUsagePairs { get; set; }
public Il2CppFieldRef[] FieldRefs { get; set; }
public ImmutableArray<Il2CppAssemblyDefinition> Assemblies { get; set; }
public ImmutableArray<Il2CppImageDefinition> Images { get; set; }
public ImmutableArray<Il2CppTypeDefinition> Types { get; set; }
public ImmutableArray<Il2CppMethodDefinition> Methods { get; set; }
public ImmutableArray<Il2CppParameterDefinition> Params { get; set; }
public ImmutableArray<Il2CppFieldDefinition> Fields { get; set; }
public ImmutableArray<Il2CppFieldDefaultValue> FieldDefaultValues { get; set; }
public ImmutableArray<Il2CppParameterDefaultValue> ParameterDefaultValues { get; set; }
public ImmutableArray<Il2CppPropertyDefinition> Properties { get; set; }
public ImmutableArray<Il2CppEventDefinition> Events { get; set; }
public ImmutableArray<Il2CppGenericContainer> GenericContainers { get; set; }
public ImmutableArray<Il2CppGenericParameter> GenericParameters { get; set; }
public ImmutableArray<Il2CppCustomAttributeTypeRange> AttributeTypeRanges { get; set; }
public ImmutableArray<Il2CppCustomAttributeDataRange> AttributeDataRanges { get; set; }
public ImmutableArray<Il2CppInterfaceOffsetPair> InterfaceOffsets { get; set; }
public ImmutableArray<Il2CppMetadataUsageList> MetadataUsageLists { get; set; }
public ImmutableArray<Il2CppMetadataUsagePair> MetadataUsagePairs { get; set; }
public ImmutableArray<Il2CppFieldRef> FieldRefs { get; set; }
public int[] InterfaceUsageIndices { get; set; }
public int[] NestedTypeIndices { get; set; }
public int[] AttributeTypeIndices { get; set; }
public int[] GenericConstraintIndices { get; set; }
public uint[] VTableMethodIndices { get; set; }
public ImmutableArray<int> InterfaceUsageIndices { get; set; }
public ImmutableArray<int> NestedTypeIndices { get; set; }
public ImmutableArray<int> AttributeTypeIndices { get; set; }
public ImmutableArray<int> GenericConstraintIndices { get; set; }
public ImmutableArray<uint> VTableMethodIndices { get; set; }
public string[] StringLiterals { get; set; }
public Dictionary<int, string> Strings { get; private set; } = new Dictionary<int, string>();
@@ -81,7 +82,7 @@ namespace Il2CppInspector
StatusUpdate("Processing metadata");
// Read metadata header
Header = ReadObject<Il2CppGlobalMetadataHeader>(0);
Header = ReadVersionedObject<Il2CppGlobalMetadataHeader>(0);
// Check for correct magic bytes
if (!Header.SanityValid) {
@@ -96,7 +97,7 @@ namespace Il2CppInspector
}
// Rewind and read metadata header with the correct version settings
Header = ReadObject<Il2CppGlobalMetadataHeader>(0);
Header = ReadVersionedObject<Il2CppGlobalMetadataHeader>(0);
// Sanity checking
// Unity.IL2CPP.MetadataCacheWriter.WriteLibIl2CppMetadata always writes the metadata information in the same order it appears in the header,
@@ -110,21 +111,21 @@ namespace Il2CppInspector
if (!pluginResult.SkipValidation) {
var realHeaderLength = Header.StringLiteralOffset;
if (realHeaderLength != Sizeof(typeof(Il2CppGlobalMetadataHeader))) {
if (realHeaderLength != Sizeof<Il2CppGlobalMetadataHeader>()) {
if (Version == MetadataVersions.V240) {
Version = MetadataVersions.V242;
Header = ReadObject<Il2CppGlobalMetadataHeader>(0);
Header = ReadVersionedObject<Il2CppGlobalMetadataHeader>(0);
}
}
if (realHeaderLength != Sizeof(typeof(Il2CppGlobalMetadataHeader))) {
if (realHeaderLength != Sizeof<Il2CppGlobalMetadataHeader>()) {
throw new InvalidOperationException("Could not verify the integrity of the metadata file or accurately identify the metadata sub-version");
}
}
// Load all the relevant metadata using offsets provided in the header
if (Version >= MetadataVersions.V160)
Images = ReadArray<Il2CppImageDefinition>(Header.ImagesOffset, Header.ImagesSize / Sizeof(typeof(Il2CppImageDefinition)));
Images = ReadVersionedObjectArray<Il2CppImageDefinition>(Header.ImagesOffset, Header.ImagesSize / Sizeof<Il2CppImageDefinition>());
// As an additional sanity check, all images in the metadata should have Mono.Cecil.MetadataToken == 1
// In metadata v24.1, two extra fields were added which will cause the below test to fail.
@@ -135,32 +136,32 @@ namespace Il2CppInspector
Version = MetadataVersions.V241;
// No need to re-read the header, it's the same for both sub-versions
Images = ReadArray<Il2CppImageDefinition>(Header.ImagesOffset, Header.ImagesSize / Sizeof(typeof(Il2CppImageDefinition)));
Images = ReadVersionedObjectArray<Il2CppImageDefinition>(Header.ImagesOffset, Header.ImagesSize / Sizeof<Il2CppImageDefinition>());
if (Images.Any(x => x.Token != 1))
throw new InvalidOperationException("Could not verify the integrity of the metadata file image list");
}
Types = ReadArray<Il2CppTypeDefinition>(Header.TypeDefinitionsOffset, Header.TypeDefinitionsSize / Sizeof(typeof(Il2CppTypeDefinition)));
Methods = ReadArray<Il2CppMethodDefinition>(Header.MethodsOffset, Header.MethodsSize / Sizeof(typeof(Il2CppMethodDefinition)));
Params = ReadArray<Il2CppParameterDefinition>(Header.ParametersOffset, Header.ParametersSize / Sizeof(typeof(Il2CppParameterDefinition)));
Fields = ReadArray<Il2CppFieldDefinition>(Header.FieldsOffset, Header.FieldsSize / Sizeof(typeof(Il2CppFieldDefinition)));
FieldDefaultValues = ReadArray<Il2CppFieldDefaultValue>(Header.FieldDefaultValuesOffset, Header.FieldDefaultValuesSize / Sizeof(typeof(Il2CppFieldDefaultValue)));
Properties = ReadArray<Il2CppPropertyDefinition>(Header.PropertiesOffset, Header.PropertiesSize / Sizeof(typeof(Il2CppPropertyDefinition)));
Events = ReadArray<Il2CppEventDefinition>(Header.EventsOffset, Header.EventsSize / Sizeof(typeof(Il2CppEventDefinition)));
InterfaceUsageIndices = ReadArray<int>(Header.InterfacesOffset, Header.InterfacesSize / sizeof(int));
NestedTypeIndices = ReadArray<int>(Header.NestedTypesOffset, Header.NestedTypesSize / sizeof(int));
GenericContainers = ReadArray<Il2CppGenericContainer>(Header.GenericContainersOffset, Header.GenericContainersSize / Sizeof(typeof(Il2CppGenericContainer)));
GenericParameters = ReadArray<Il2CppGenericParameter>(Header.GenericParametersOffset, Header.GenericParametersSize / Sizeof(typeof(Il2CppGenericParameter)));
GenericConstraintIndices = ReadArray<int>(Header.GenericParameterConstraintsOffset, Header.GenericParameterConstraintsSize / sizeof(int));
InterfaceOffsets = ReadArray<Il2CppInterfaceOffsetPair>(Header.InterfaceOffsetsOffset, Header.InterfaceOffsetsSize / Sizeof(typeof(Il2CppInterfaceOffsetPair)));
VTableMethodIndices = ReadArray<uint>(Header.VTableMethodsOffset, Header.VTableMethodsSize / sizeof(uint));
Types = ReadVersionedObjectArray<Il2CppTypeDefinition>(Header.TypeDefinitionsOffset, Header.TypeDefinitionsSize / Sizeof<Il2CppTypeDefinition>());
Methods = ReadVersionedObjectArray<Il2CppMethodDefinition>(Header.MethodsOffset, Header.MethodsSize / Sizeof<Il2CppMethodDefinition>());
Params = ReadVersionedObjectArray<Il2CppParameterDefinition>(Header.ParametersOffset, Header.ParametersSize / Sizeof<Il2CppParameterDefinition>());
Fields = ReadVersionedObjectArray<Il2CppFieldDefinition>(Header.FieldsOffset, Header.FieldsSize / Sizeof<Il2CppFieldDefinition>());
FieldDefaultValues = ReadVersionedObjectArray<Il2CppFieldDefaultValue>(Header.FieldDefaultValuesOffset, Header.FieldDefaultValuesSize / Sizeof<Il2CppFieldDefaultValue>());
Properties = ReadVersionedObjectArray<Il2CppPropertyDefinition>(Header.PropertiesOffset, Header.PropertiesSize / Sizeof<Il2CppPropertyDefinition>());
Events = ReadVersionedObjectArray<Il2CppEventDefinition>(Header.EventsOffset, Header.EventsSize / Sizeof<Il2CppEventDefinition>());
InterfaceUsageIndices = ReadPrimitiveArray<int>(Header.InterfacesOffset, Header.InterfacesSize / sizeof(int));
NestedTypeIndices = ReadPrimitiveArray<int>(Header.NestedTypesOffset, Header.NestedTypesSize / sizeof(int));
GenericContainers = ReadVersionedObjectArray<Il2CppGenericContainer>(Header.GenericContainersOffset, Header.GenericContainersSize / Sizeof<Il2CppGenericContainer>());
GenericParameters = ReadVersionedObjectArray<Il2CppGenericParameter>(Header.GenericParametersOffset, Header.GenericParametersSize / Sizeof<Il2CppGenericParameter>());
GenericConstraintIndices = ReadPrimitiveArray<int>(Header.GenericParameterConstraintsOffset, Header.GenericParameterConstraintsSize / sizeof(int));
InterfaceOffsets = ReadVersionedObjectArray<Il2CppInterfaceOffsetPair>(Header.InterfaceOffsetsOffset, Header.InterfaceOffsetsSize / Sizeof<Il2CppInterfaceOffsetPair>());
VTableMethodIndices = ReadPrimitiveArray<uint>(Header.VTableMethodsOffset, Header.VTableMethodsSize / sizeof(uint));
if (Version >= MetadataVersions.V160) {
// In v24.4 hashValueIndex was removed from Il2CppAssemblyNameDefinition, which is a field in Il2CppAssemblyDefinition
// The number of images and assemblies should be the same. If they are not, we deduce that we are using v24.4
// Note the version comparison matches both 24.2 and 24.3 here since 24.3 is tested for during binary loading
var assemblyCount = Header.AssembliesSize / Sizeof(typeof(Il2CppAssemblyDefinition));
var assemblyCount = Header.AssembliesSize / Sizeof<Il2CppAssemblyDefinition>();
var changedAssemblyDefStruct = false;
if ((Version == MetadataVersions.V241 || Version == MetadataVersions.V242 || Version == MetadataVersions.V243) && assemblyCount < Images.Length)
{
@@ -169,29 +170,29 @@ namespace Il2CppInspector
Version = MetadataVersions.V244;
}
Assemblies = ReadArray<Il2CppAssemblyDefinition>(Header.AssembliesOffset, Images.Length);
Assemblies = ReadVersionedObjectArray<Il2CppAssemblyDefinition>(Header.AssembliesOffset, Images.Length);
if (changedAssemblyDefStruct)
Version = MetadataVersions.V241;
ParameterDefaultValues = ReadArray<Il2CppParameterDefaultValue>(Header.ParameterDefaultValuesOffset, Header.ParameterDefaultValuesSize / Sizeof(typeof(Il2CppParameterDefaultValue)));
ParameterDefaultValues = ReadVersionedObjectArray<Il2CppParameterDefaultValue>(Header.ParameterDefaultValuesOffset, Header.ParameterDefaultValuesSize / Sizeof<Il2CppParameterDefaultValue>());
}
if (Version >= MetadataVersions.V190 && Version < MetadataVersions.V270) {
MetadataUsageLists = ReadArray<Il2CppMetadataUsageList>(Header.MetadataUsageListsOffset, Header.MetadataUsageListsCount / Sizeof(typeof(Il2CppMetadataUsageList)));
MetadataUsagePairs = ReadArray<Il2CppMetadataUsagePair>(Header.MetadataUsagePairsOffset, Header.MetadataUsagePairsCount / Sizeof(typeof(Il2CppMetadataUsagePair)));
MetadataUsageLists = ReadVersionedObjectArray<Il2CppMetadataUsageList>(Header.MetadataUsageListsOffset, Header.MetadataUsageListsCount / Sizeof<Il2CppMetadataUsageList>());
MetadataUsagePairs = ReadVersionedObjectArray<Il2CppMetadataUsagePair>(Header.MetadataUsagePairsOffset, Header.MetadataUsagePairsCount / Sizeof<Il2CppMetadataUsagePair>());
}
if (Version >= MetadataVersions.V190) {
FieldRefs = ReadArray<Il2CppFieldRef>(Header.FieldRefsOffset, Header.FieldRefsSize / Sizeof(typeof(Il2CppFieldRef)));
FieldRefs = ReadVersionedObjectArray<Il2CppFieldRef>(Header.FieldRefsOffset, Header.FieldRefsSize / Sizeof<Il2CppFieldRef>());
}
if (Version >= MetadataVersions.V210 && Version < MetadataVersions.V290) {
AttributeTypeIndices = ReadArray<int>(Header.AttributesTypesOffset, Header.AttributesTypesCount / sizeof(int));
AttributeTypeRanges = ReadArray<Il2CppCustomAttributeTypeRange>(Header.AttributesInfoOffset, Header.AttributesInfoCount / Sizeof(typeof(Il2CppCustomAttributeTypeRange)));
AttributeTypeIndices = ReadPrimitiveArray<int>(Header.AttributesTypesOffset, Header.AttributesTypesCount / sizeof(int));
AttributeTypeRanges = ReadVersionedObjectArray<Il2CppCustomAttributeTypeRange>(Header.AttributesInfoOffset, Header.AttributesInfoCount / Sizeof<Il2CppCustomAttributeTypeRange>());
}
if (Version >= MetadataVersions.V290)
{
AttributeDataRanges = ReadArray<Il2CppCustomAttributeDataRange>(Header.AttributeDataRangeOffset,
Header.AttributeDataRangeSize / Sizeof(typeof(Il2CppCustomAttributeDataRange)));
AttributeDataRanges = ReadVersionedObjectArray<Il2CppCustomAttributeDataRange>(Header.AttributeDataRangeOffset,
Header.AttributeDataRangeSize / Sizeof<Il2CppCustomAttributeDataRange>());
}
if (Version == MetadataVersions.V290 || Version == MetadataVersions.V310)
@@ -207,8 +208,8 @@ namespace Il2CppInspector
{
Version = new StructVersion(Version.Major, 1, Version.Tag);
Methods = ReadArray<Il2CppMethodDefinition>(Header.MethodsOffset,
Header.MethodsSize / Sizeof(typeof(Il2CppMethodDefinition)));
Methods = ReadVersionedObjectArray<Il2CppMethodDefinition>(Header.MethodsOffset,
Header.MethodsSize / Sizeof<Il2CppMethodDefinition>());
}
}
}
@@ -231,7 +232,7 @@ namespace Il2CppInspector
StringLiterals = pluginGetStringLiteralsResult.StringLiterals.ToArray();
else {
var stringLiteralList = ReadArray<Il2CppStringLiteral>(Header.StringLiteralOffset, Header.StringLiteralSize / Sizeof(typeof(Il2CppStringLiteral)));
var stringLiteralList = ReadVersionedObjectArray<Il2CppStringLiteral>(Header.StringLiteralOffset, Header.StringLiteralSize / Sizeof<Il2CppStringLiteral>());
StringLiterals = new string[stringLiteralList.Length];
for (var i = 0; i < stringLiteralList.Length; i++)
@@ -249,42 +250,6 @@ namespace Il2CppInspector
CopyTo(outFile);
}
public int Sizeof(Type type) => Sizeof(type, Version);
public int Sizeof(Type type, StructVersion metadataVersion, int longSizeBytes = 8)
{
var doubleRepresentation = metadataVersion.AsDouble;
if (Reader.ObjectMappings.TryGetValue(type, out var streamType))
type = streamType;
int size = 0;
foreach (var i in type.GetTypeInfo().GetFields())
{
// Only process fields for our selected object versioning (always process if none supplied)
var versions = i.GetCustomAttributes<VersionAttribute>(false).Select(v => (v.Min, v.Max)).ToList();
if (versions.Any() && !versions.Any(v => (v.Min <= doubleRepresentation || v.Min == -1) && (v.Max >= doubleRepresentation || v.Max == -1)))
continue;
if (i.FieldType == typeof(long) || i.FieldType == typeof(ulong))
size += longSizeBytes;
else if (i.FieldType == typeof(int) || i.FieldType == typeof(uint))
size += 4;
else if (i.FieldType == typeof(short) || i.FieldType == typeof(ushort))
size += 2;
// Fixed-length array
else if (i.FieldType.IsArray) {
var attr = i.GetCustomAttribute<ArrayLengthAttribute>(false) ??
throw new InvalidOperationException("Array field " + i.Name + " must have ArrayLength attribute");
size += attr.FixedSize;
}
// Embedded object
else
size += Sizeof(i.FieldType, metadataVersion);
}
return size;
}
public int Sizeof<T>() where T : IReadable => T.Size(Version, Is32Bit);
}
}

View File

@@ -21,7 +21,7 @@ namespace Il2CppInspector
FieldRva = 7
}
public class MetadataUsage
public record struct MetadataUsage
{
public MetadataUsageType Type { get; }
public int SourceIndex { get; }
@@ -39,7 +39,7 @@ namespace Il2CppInspector
if (package.Version < MetadataVersions.V190) {
/* These encoded indices appear only in vtables, and are decoded by IsGenericMethodIndex/GetDecodedMethodIndex */
var isGeneric = encodedIndex & 0x80000000;
index = package.Binary.VTableMethodReferences[encodedIndex & 0x7FFFFFFF];
index = package.Binary.VTableMethodReferences[(int)(encodedIndex & 0x7FFFFFFF)];
usageType = (isGeneric != 0) ? MetadataUsageType.MethodRef : MetadataUsageType.MethodDef;
} else {
/* These encoded indices appear in metadata usages, and are decoded by GetEncodedIndexType/GetDecodedMethodIndex */

View File

@@ -3,16 +3,13 @@
namespace Il2CppInspector.Next.BinaryMetadata;
[VersionedStruct]
public partial struct Il2CppArrayType
public partial record struct Il2CppArrayType
{
public Pointer<Il2CppType> ElementType;
public byte Rank;
public byte NumSizes;
public byte NumLowerBound;
[CustomSerialization("reader.ReadNUInt();", "is32Bit ? 4 : 8")]
public ulong Sizes; // int*
[CustomSerialization("reader.ReadNUInt();", "is32Bit ? 4 : 8")]
public ulong LoBounds; // int*
public PrimitivePointer<int> Sizes;
public PrimitivePointer<int> LoBounds;
}

View File

@@ -3,17 +3,16 @@
namespace Il2CppInspector.Next.BinaryMetadata;
[VersionedStruct]
public partial struct Il2CppCodeGenModule
public partial record struct Il2CppCodeGenModule
{
[CustomSerialization("reader.ReadNUInt();", "is32Bit ? 4 : 8")]
public ulong ModuleName; // const char*
public PrimitivePointer<byte> ModuleName; // const char*
[Aligned(0)]
[NativeInteger]
public uint MethodPointerCount;
public Pointer<Il2CppMethodPointer> MethodPointers;
[Aligned(0)]
[NativeInteger]
[VersionCondition(EqualTo = "24.5")]
[VersionCondition(GreaterThan = "27.1")]
public uint AdjustorThunksCount;
@@ -22,24 +21,22 @@ public partial struct Il2CppCodeGenModule
[VersionCondition(GreaterThan = "27.1")]
public Pointer<Il2CppTokenAdjustorThunkPair> AdjustorThunks;
[CustomSerialization("reader.ReadNUInt();", "is32Bit ? 4 : 8")]
public ulong InvokerIndices; // int*
public PrimitivePointer<int> InvokerIndices; // int*
[Aligned(0)]
[NativeInteger]
public uint ReversePInvokeWrapperCount;
public Pointer<Il2CppTokenIndexMethodTuple> ReversePInvokeWrapperIndices;
[Aligned(0)]
[NativeInteger]
public uint RgctxRangesCount;
public Pointer<Il2CppTokenRangePair> RgctxRanges;
[Aligned(0)]
[NativeInteger]
public uint RgctxsCount;
public Pointer<Il2CppRgctxDefinition> Rgctxs;
[CustomSerialization("reader.ReadNUInt();", "is32Bit ? 4 : 8")]
public ulong DebuggerMetadata; // Pointer<Il2CppDebuggerMetadataRegistration> DebuggerMetadata;
public PrimitivePointer<byte> DebuggerMetadata; // Pointer<Il2CppDebuggerMetadataRegistration> DebuggerMetadata;
[VersionCondition(GreaterThan = "27.0", LessThan = "27.2")]
public Pointer<Il2CppMethodPointer> CustomAttributeCacheGenerator;
@@ -48,14 +45,11 @@ public partial struct Il2CppCodeGenModule
public Il2CppMethodPointer ModuleInitializer;
[VersionCondition(GreaterThan = "27.0")]
[Aligned(0)]
public ulong StaticConstructorTypeIndices; // TypeDefinitionIndex*
public PrimitivePointer<int> StaticConstructorTypeIndices; // TypeDefinitionIndex*
[VersionCondition(GreaterThan = "27.0")]
[CustomSerialization("reader.ReadNUInt();", "is32Bit ? 4 : 8")]
public ulong MetadataRegistration; // Pointer<Il2CppMetadataRegistration>
public PrimitivePointer<byte> MetadataRegistration; // Pointer<Il2CppMetadataRegistration>
[VersionCondition(GreaterThan = "27.0")]
[CustomSerialization("reader.ReadNUInt();", "is32Bit ? 4 : 8")]
public ulong CodeRegistration; // Pointer<Il2CppCodeRegistration>
public PrimitivePointer<byte> CodeRegistration; // Pointer<Il2CppCodeRegistration>
}

View File

@@ -5,38 +5,42 @@ namespace Il2CppInspector.Next.BinaryMetadata;
using InvokerMethod = Il2CppMethodPointer;
[VersionedStruct]
public partial struct Il2CppCodeRegistration
public partial record struct Il2CppCodeRegistration
{
[VersionCondition(LessThan = "24.1"), Aligned(0)]
[NativeInteger]
[VersionCondition(LessThan = "24.1")]
public uint MethodPointersCount;
[VersionCondition(LessThan = "24.1")]
public Pointer<Il2CppMethodPointer> MethodPointers;
[Aligned(0)]
[NativeInteger]
public uint ReversePInvokeWrapperCount;
public Pointer<Il2CppMethodPointer> ReversePInvokeWrappers;
[VersionCondition(LessThan = "22.0"), Aligned(0)]
[NativeInteger]
[VersionCondition(LessThan = "22.0")]
public uint DelegateWrappersFromManagedToNativeCount;
[VersionCondition(LessThan = "22.0")]
public Pointer<Il2CppMethodPointer> DelegateWrappersFromManagedToNative;
[VersionCondition(LessThan = "22.0"), Aligned(0)]
[NativeInteger]
[VersionCondition(LessThan = "22.0")]
public uint MarshalingFunctionsCount;
[VersionCondition(LessThan = "22.0")]
public Pointer<Il2CppMethodPointer> MarshalingFunctions;
[VersionCondition(GreaterThan = "21.0", LessThan = "22.0"), Aligned(0)]
[NativeInteger]
[VersionCondition(GreaterThan = "21.0", LessThan = "22.0")]
public uint CcwMarshalingFunctionsCount;
[VersionCondition(GreaterThan = "21.0", LessThan = "22.0")]
public Pointer<Il2CppMethodPointer> CcwMarshalingFunctions;
[Aligned(0)]
[NativeInteger]
public uint GenericMethodPointersCount;
public Pointer<Il2CppMethodPointer> GenericMethodPointers;
@@ -45,29 +49,32 @@ public partial struct Il2CppCodeRegistration
[VersionCondition(GreaterThan = "27.1")]
public Pointer<Il2CppMethodPointer> GenericAdjustorThunks;
[Aligned(0)]
[NativeInteger]
public uint InvokerPointersCount;
public Pointer<InvokerMethod> InvokerPointers;
[VersionCondition(LessThan = "24.5"), Aligned(0)]
[NativeInteger]
[VersionCondition(LessThan = "24.5")]
public int CustomAttributeCount;
[VersionCondition(LessThan = "24.5")]
public Pointer<Il2CppMethodPointer> CustomAttributeGenerators;
[VersionCondition(GreaterThan = "21.0", LessThan = "22.0"), Aligned(0)]
[NativeInteger]
[VersionCondition(GreaterThan = "21.0", LessThan = "22.0")]
public int GuidCount;
[VersionCondition(GreaterThan = "21.0", LessThan = "22.0")]
public Pointer<Il2CppGuid> Guids;
[NativeInteger]
[VersionCondition(GreaterThan = "22.0", LessThan = "29.0")]
public int UnresolvedVirtualCallCount;
[NativeInteger]
[VersionCondition(EqualTo = "29.1"), VersionCondition(EqualTo = "31.1")]
[VersionCondition(EqualTo = "29.2"), VersionCondition(EqualTo = "31.2")]
[Aligned(0)]
public uint UnresolvedIndirectCallCount; // UnresolvedVirtualCallCount pre 29.1
[VersionCondition(GreaterThan = "22.0")]
@@ -81,19 +88,22 @@ public partial struct Il2CppCodeRegistration
[VersionCondition(EqualTo = "29.2"), VersionCondition(EqualTo = "31.2")]
public Pointer<Il2CppMethodPointer> UnresolvedStaticCallPointers;
[VersionCondition(GreaterThan = "23.0"), Aligned(0)]
[NativeInteger]
[VersionCondition(GreaterThan = "23.0")]
public uint InteropDataCount;
[VersionCondition(GreaterThan = "23.0")]
public Pointer<Il2CppInteropData> InteropData;
[VersionCondition(GreaterThan = "24.3"), Aligned(0)]
[NativeInteger]
[VersionCondition(GreaterThan = "24.3")]
public uint WindowsRuntimeFactoryCount;
[VersionCondition(GreaterThan = "24.3")]
public Pointer<Il2CppWindowsRuntimeFactoryTableEntry> WindowsRuntimeFactoryTable;
[VersionCondition(GreaterThan = "24.2"), Aligned(0)]
[NativeInteger]
[VersionCondition(GreaterThan = "24.2")]
public uint CodeGenModulesCount;
[VersionCondition(GreaterThan = "24.2")]

View File

@@ -3,9 +3,10 @@
namespace Il2CppInspector.Next.BinaryMetadata;
[VersionedStruct]
public partial struct Il2CppGenericClass
public partial record struct Il2CppGenericClass
{
[VersionCondition(LessThan = "24.5"), Aligned(0)]
[NativeInteger]
[VersionCondition(LessThan = "24.5")]
public int TypeDefinitionIndex;
[VersionCondition(GreaterThan = "27.0")]
@@ -13,6 +14,5 @@ public partial struct Il2CppGenericClass
public Il2CppGenericContext Context;
[CustomSerialization("reader.ReadNUInt();", "is32Bit ? 4 : 8")]
public ulong CachedClass; // Il2CppClass*, optional
public PrimitivePointer<byte> CachedClass; // Il2CppClass*, optional
}

View File

@@ -3,7 +3,7 @@
namespace Il2CppInspector.Next.BinaryMetadata;
[VersionedStruct]
public partial struct Il2CppGenericContext
public partial record struct Il2CppGenericContext
{
public Pointer<Il2CppGenericInst> ClassInst;
public Pointer<Il2CppGenericInst> MethodInst;

View File

@@ -3,11 +3,11 @@
namespace Il2CppInspector.Next.BinaryMetadata;
[VersionedStruct]
public partial struct Il2CppGenericInst
public partial record struct Il2CppGenericInst
{
public readonly bool Valid => TypeArgc > 0;
[Aligned(0)]
[NativeInteger]
public uint TypeArgc;
public Pointer<Pointer<Il2CppType>> TypeArgv;

View File

@@ -5,7 +5,7 @@ namespace Il2CppInspector.Next.BinaryMetadata;
using GenericMethodIndex = int;
[VersionedStruct]
public partial struct Il2CppGenericMethodFunctionsDefinitions
public partial record struct Il2CppGenericMethodFunctionsDefinitions
{
public GenericMethodIndex GenericMethodIndex;
public Il2CppGenericMethodIndices Indices;

View File

@@ -5,7 +5,7 @@ namespace Il2CppInspector.Next.BinaryMetadata;
using MethodIndex = int;
[VersionedStruct]
public partial struct Il2CppGenericMethodIndices
public partial record struct Il2CppGenericMethodIndices
{
public MethodIndex MethodIndex;
public MethodIndex InvokerIndex;

View File

@@ -2,8 +2,7 @@
namespace Il2CppInspector.Next.BinaryMetadata;
public struct Il2CppGuid : IReadable
public record struct Il2CppGuid : IReadable
{
public Guid Value;

View File

@@ -8,7 +8,7 @@ using PInvokeMarshalCleanupFunc = Il2CppMethodPointer;
using CreateCCWFunc = Il2CppMethodPointer;
[VersionedStruct]
public partial struct Il2CppInteropData
public partial record struct Il2CppInteropData
{
public Il2CppMethodPointer DelegatePInvokeWrapperFunction;
public PInvokeMarshalToNativeFunc PInvokeMarshalToNativeFunction;

View File

@@ -7,51 +7,50 @@ using FieldIndex = int;
using TypeDefinitionIndex = int;
[VersionedStruct]
public partial struct Il2CppMetadataRegistration
public partial record struct Il2CppMetadataRegistration
{
[Aligned(0)]
[NativeInteger]
public int GenericClassesCount;
public Pointer<Pointer<Il2CppGenericClass>> GenericClasses;
[Aligned(0)]
[NativeInteger]
public int GenericInstsCount;
public Pointer<Pointer<Il2CppGenericInst>> GenericInsts;
[Aligned(0)]
[NativeInteger]
public int GenericMethodTableCount;
public Pointer<Il2CppGenericMethodFunctionsDefinitions> GenericMethodTable;
[Aligned(0)]
[NativeInteger]
public int TypesCount;
public Pointer<Pointer<Il2CppType>> Types;
[Aligned(0)]
[NativeInteger]
public int MethodSpecsCount;
public Pointer<Il2CppMethodSpec> MethodSpecs;
[NativeInteger]
[VersionCondition(LessThan = "16.0")]
public int MethodReferencesCount;
[VersionCondition(LessThan = "16.0")]
[CustomSerialization("reader.ReadNUInt();", "is32Bit ? 4 : 8")]
public ulong MethodReferences; // uint**
public PrimitivePointer<PrimitivePointer<uint>> MethodReferences; // uint**
[Aligned(0)]
[NativeInteger]
public FieldIndex FieldOffsetsCount;
[CustomSerialization("reader.ReadNUInt();", "is32Bit ? 4 : 8")]
public ulong FieldOffsets; // int**
public PrimitivePointer<PrimitivePointer<int>> FieldOffsets; // int**
[Aligned(0)]
[NativeInteger]
public TypeDefinitionIndex TypeDefinitionsSizesCount;
public Pointer<Pointer<Il2CppTypeDefinitionSizes>> TypeDefinitionsSizes;
[Aligned(0)]
[NativeInteger]
[VersionCondition(GreaterThan = "19.0")]
public ulong MetadataUsagesCount;

View File

@@ -3,11 +3,11 @@
namespace Il2CppInspector.Next.BinaryMetadata;
[VersionedStruct]
public partial struct Il2CppMethodPointer(ulong addr = 0)
public partial record struct Il2CppMethodPointer(ulong addr = 0)
{
public static readonly Il2CppMethodPointer Null = new();
[CustomSerialization("reader.ReadNUInt();", "is32Bit ? 4 : 8")]
[NativeInteger]
public ulong Value { get; set; } = addr;
public readonly bool IsNull => Value == 0;

View File

@@ -6,7 +6,7 @@ using MethodIndex = int;
using GenericInstIndex = int;
[VersionedStruct]
public partial struct Il2CppMethodSpec
public partial record struct Il2CppMethodSpec
{
public MethodIndex MethodDefinitionIndex;
public GenericInstIndex ClassIndexIndex;

View File

@@ -3,7 +3,7 @@
namespace Il2CppInspector.Next.BinaryMetadata;
[VersionedStruct]
public partial struct Il2CppRange
public partial record struct Il2CppRange
{
public int Start;
public int Length;

View File

@@ -6,7 +6,7 @@ namespace Il2CppInspector.Next.BinaryMetadata;
using TypeIndex = int;
[VersionedStruct]
public partial struct Il2CppRgctxConstrainedData
public partial record struct Il2CppRgctxConstrainedData
{
public TypeIndex TypeIndex;
public Il2CppMetadataUsage EncodedMethodIndex;

View File

@@ -3,14 +3,13 @@
namespace Il2CppInspector.Next.BinaryMetadata;
[VersionedStruct]
public partial struct Il2CppRgctxDefinition
public partial record struct Il2CppRgctxDefinition
{
[Aligned(0)]
[NativeInteger]
public Il2CppRgctxDataType Type;
[CustomSerialization("reader.ReadNUInt();", "is32Bit ? 4 : 8")]
public ulong Data; // void*
public PrimitivePointer<byte> Data; // void*
public readonly Pointer<Il2CppRgctxDefinitionData> Definition => Data;
public readonly Pointer<Il2CppRgctxConstrainedData> Constrained => Data;
public readonly Pointer<Il2CppRgctxDefinitionData> Definition => Data.PointerValue;
public readonly Pointer<Il2CppRgctxConstrainedData> Constrained => Data.PointerValue;
}

View File

@@ -6,7 +6,7 @@ using MethodIndex = int;
using TypeIndex = int;
[VersionedStruct]
public partial struct Il2CppRgctxDefinitionData
public partial record struct Il2CppRgctxDefinitionData
{
public int Value;

View File

@@ -3,9 +3,9 @@
namespace Il2CppInspector.Next.BinaryMetadata;
[VersionedStruct]
public partial struct Il2CppTokenAdjustorThunkPair
public partial record struct Il2CppTokenAdjustorThunkPair
{
[Aligned(0)]
[NativeInteger]
public uint Token;
public Il2CppMethodPointer AdjustorThunk;

View File

@@ -3,13 +3,12 @@
namespace Il2CppInspector.Next.BinaryMetadata;
[VersionedStruct]
public partial struct Il2CppTokenIndexMethodTuple
public partial record struct Il2CppTokenIndexMethodTuple
{
public uint Token;
public int Index;
[CustomSerialization("reader.ReadNUInt();", "is32Bit ? 4 : 8")]
public ulong Method; // void**
public PrimitivePointer<Il2CppMethodPointer> Method; // void**
public uint GenericMethodIndex;
}

View File

@@ -3,7 +3,7 @@
namespace Il2CppInspector.Next.BinaryMetadata;
[VersionedStruct]
public partial struct Il2CppTokenRangePair
public partial record struct Il2CppTokenRangePair
{
public uint Token;
public Il2CppRange Range;

View File

@@ -3,7 +3,7 @@
namespace Il2CppInspector.Next.BinaryMetadata;
[VersionedStruct]
public partial struct Il2CppTypeDefinitionSizes
public partial record struct Il2CppTypeDefinitionSizes
{
public uint InstanceSize;
public int NativeSize;

View File

@@ -3,7 +3,7 @@
namespace Il2CppInspector.Next.BinaryMetadata;
[VersionedStruct]
public partial struct Il2CppWindowsRuntimeFactoryTableEntry
public partial record struct Il2CppWindowsRuntimeFactoryTableEntry
{
public Pointer<Il2CppType> Type;
public Il2CppMethodPointer CreateFactoryFunction;

View File

@@ -8,6 +8,18 @@ namespace Il2CppInspector.Next;
public class BinaryObjectStreamReader : BinaryObjectStream, IReader
{
public new StructVersion Version
{
get => _version;
set
{
_version = value;
base.Version = _version.AsDouble;
}
}
private StructVersion _version;
public virtual int Bits { get; set; }
public bool Is32Bit => Bits == 32;
@@ -127,4 +139,9 @@ public class BinaryObjectStreamReader : BinaryObjectStream, IReader
Position = addr;
return ReadVersionedObjectArray<TType>(count, Version);
}
public void Skip(int count)
{
Position += count;
}
}

View File

@@ -7,7 +7,7 @@ using ImageIndex = int;
[VersionedStruct]
[StructLayout(LayoutKind.Explicit)]
public partial struct Il2CppAssemblyDefinition
public partial record struct Il2CppAssemblyDefinition
{
[FieldOffset(20)]
[VersionCondition(LessThan = "15.0")]

View File

@@ -17,7 +17,7 @@ public struct PublicKeyToken
[VersionedStruct]
[StructLayout(LayoutKind.Explicit)]
public partial struct Il2CppAssemblyNameDefinition
public partial record struct Il2CppAssemblyNameDefinition
{
[FieldOffset(0)]
public StringIndex NameIndex;

View File

@@ -3,7 +3,7 @@
namespace Il2CppInspector.Next.Metadata;
[VersionedStruct]
public partial struct Il2CppCustomAttributeDataRange
public partial record struct Il2CppCustomAttributeDataRange
{
public uint Token { get; private set; }
public uint StartOffset { get; private set; }

View File

@@ -3,7 +3,7 @@
namespace Il2CppInspector.Next.Metadata;
[VersionedStruct]
public partial struct Il2CppCustomAttributeTypeRange
public partial record struct Il2CppCustomAttributeTypeRange
{
[VersionCondition(GreaterThan = "24.1")]
public uint Token { get; private set; }

View File

@@ -6,7 +6,7 @@ using MethodIndex = int;
using VersionedSerialization.Attributes;
[VersionedStruct]
public partial struct Il2CppEventDefinition
public partial record struct Il2CppEventDefinition
{
public StringIndex NameIndex { get; private set; }
public TypeIndex TypeIndex { get; private set; }

View File

@@ -7,7 +7,7 @@ using TypeIndex = int;
using DefaultValueDataIndex = int;
[VersionedStruct]
public partial struct Il2CppFieldDefaultValue
public partial record struct Il2CppFieldDefaultValue
{
public FieldIndex FieldIndex { get; private set; }
public TypeIndex TypeIndex { get; private set; }

View File

@@ -5,7 +5,7 @@ using StringIndex = int;
using TypeIndex = int;
[VersionedStruct]
public partial struct Il2CppFieldDefinition
public partial record struct Il2CppFieldDefinition
{
public StringIndex NameIndex { get; private set; }
public TypeIndex TypeIndex { get; private set; }

View File

@@ -5,7 +5,7 @@ using FieldIndex = int;
using TypeIndex = int;
[VersionedStruct]
public partial struct Il2CppFieldMarshaledSize
public partial record struct Il2CppFieldMarshaledSize
{
public FieldIndex FieldIndex { get; private set; }
public TypeIndex TypeIndex { get; private set; }

View File

@@ -6,7 +6,7 @@ using FieldIndex = int;
using TypeIndex = int;
[VersionedStruct]
public partial struct Il2CppFieldRef
public partial record struct Il2CppFieldRef
{
public TypeIndex TypeIndex { get; private set; }
public FieldIndex FieldIndex { get; private set; }

View File

@@ -4,7 +4,7 @@ using VersionedSerialization.Attributes;
using GenericParameterIndex = int;
[VersionedStruct]
public partial struct Il2CppGenericContainer
public partial record struct Il2CppGenericContainer
{
public int OwnerIndex { get; private set; }
public int TypeArgc { get; private set; }

View File

@@ -8,7 +8,7 @@ using StringIndex = int;
using GenericParameterConstraintIndex = short;
[VersionedStruct]
public partial struct Il2CppGenericParameter
public partial record struct Il2CppGenericParameter
{
public GenericContainerIndex OwnerIndex { get; private set; }
public StringIndex NameIndex { get; private set; }

View File

@@ -25,7 +25,7 @@ namespace Il2CppInspector.Next.Metadata;
// Metadata version is written at the end of Unity.IL2CPP.MetadataCacheWriter.WriteLibIl2CppMetadata or WriteMetadata (Unity.IL2CPP.dll)
[VersionedStruct]
public partial struct Il2CppGlobalMetadataHeader
public partial record struct Il2CppGlobalMetadataHeader
{
public int Sanity { get; private set; }
public int Version { get; private set; }

View File

@@ -8,7 +8,7 @@ using CustomAttributeIndex = int;
using VersionedSerialization.Attributes;
[VersionedStruct]
public partial struct Il2CppImageDefinition
public partial record struct Il2CppImageDefinition
{
public StringIndex NameIndex { get; private set; }
public AssemblyIndex AssemblyIndex { get; private set; }

View File

@@ -5,7 +5,7 @@ namespace Il2CppInspector.Next.Metadata;
using TypeIndex = int;
[VersionedStruct]
public partial struct Il2CppInterfaceOffsetPair
public partial record struct Il2CppInterfaceOffsetPair
{
public TypeIndex InterfaceTypeIndex { get; private set; }
public int Offset { get; private set; }

View File

@@ -3,7 +3,7 @@
namespace Il2CppInspector.Next.Metadata;
[VersionedStruct]
public partial struct Il2CppMetadataRange
public partial record struct Il2CppMetadataRange
{
public int Start { get; private set; }
public int Length { get; private set; }

View File

@@ -7,7 +7,7 @@ using VersionedSerialization.Attributes;
using EncodedMethodIndex = uint;
[VersionedStruct]
public partial struct Il2CppMetadataUsage
public partial record struct Il2CppMetadataUsage
{
private const uint TypeMask = 0b111u << 29;
private const uint InflatedMask = 0b1;

View File

@@ -3,7 +3,7 @@
namespace Il2CppInspector.Next.Metadata;
[VersionedStruct]
public partial struct Il2CppMetadataUsageList
public partial record struct Il2CppMetadataUsageList
{
public int Start { get; private set; }
public int Count { get; private set; }

View File

@@ -3,7 +3,7 @@
namespace Il2CppInspector.Next.Metadata;
[VersionedStruct]
public partial struct Il2CppMetadataUsagePair
public partial record struct Il2CppMetadataUsagePair
{
public uint DestinationIndex { get; private set; }
public uint EncodedSourceIndex { get; private set; }

View File

@@ -10,7 +10,7 @@ using ParameterIndex = int;
using GenericContainerIndex = int;
[VersionedStruct]
public partial struct Il2CppMethodDefinition
public partial record struct Il2CppMethodDefinition
{
public StringIndex NameIndex { get; private set; }

View File

@@ -6,7 +6,7 @@ using DefaultValueDataIndex = int;
using VersionedSerialization.Attributes;
[VersionedStruct]
public partial struct Il2CppParameterDefaultValue
public partial record struct Il2CppParameterDefaultValue
{
public ParameterIndex ParameterIndex { get; private set; }
public TypeIndex TypeIndex { get; private set; }

View File

@@ -5,7 +5,7 @@ using StringIndex = int;
using TypeIndex = int;
[VersionedStruct]
public partial struct Il2CppParameterDefinition
public partial record struct Il2CppParameterDefinition
{
public StringIndex NameIndex { get; private set; }
public uint Token { get; private set; }

View File

@@ -7,7 +7,7 @@ using StringIndex = int;
using MethodIndex = int;
[VersionedStruct]
public partial struct Il2CppPropertyDefinition
public partial record struct Il2CppPropertyDefinition
{
public StringIndex NameIndex { get; private set; }
public MethodIndex Get { get; private set; }

View File

@@ -4,7 +4,7 @@ using VersionedSerialization.Attributes;
using StringLiteralIndex = int;
[VersionedStruct]
public partial struct Il2CppStringLiteral
public partial record struct Il2CppStringLiteral
{
public uint Length { get; private set; }
public StringLiteralIndex DataIndex { get; private set; }

View File

@@ -15,7 +15,7 @@ using InterfacesIndex = int;
using VTableIndex = int;
[VersionedStruct]
public partial struct Il2CppTypeDefinition
public partial record struct Il2CppTypeDefinition
{
public const TypeIndex InvalidTypeIndex = -1;

View File

@@ -3,7 +3,7 @@
namespace Il2CppInspector.Next.Metadata;
[VersionedStruct]
public partial struct Il2CppTypeDefinitionBitfield
public partial record struct Il2CppTypeDefinitionBitfield
{
private uint _value;

View File

@@ -5,7 +5,7 @@ using StringIndex = int;
using TypeIndex = int;
[VersionedStruct]
public partial struct Il2CppWindowsRuntimeTypeNamePair
public partial record struct Il2CppWindowsRuntimeTypeNamePair
{
public StringIndex NameIndex { get; private set; }
public TypeIndex TypeIndex { get; private set; }

View File

@@ -4,9 +4,9 @@ using VersionedSerialization.Attributes;
namespace Il2CppInspector.Next;
public struct Pointer<T>(ulong value = 0) : IReadable, IEquatable<Pointer<T>> where T : IReadable, new()
public struct Pointer<T>(ulong value = 0) : IReadable, IEquatable<Pointer<T>> where T : struct, IReadable
{
[CustomSerialization("reader.ReadNUInt();", "is32Bit ? 4 : 8")]
[NativeInteger]
private ulong _value = value;
public readonly ulong PointerValue => _value;

View File

@@ -0,0 +1,60 @@
using System.Collections.Immutable;
using VersionedSerialization;
using VersionedSerialization.Attributes;
namespace Il2CppInspector.Next;
public struct PrimitivePointer<T>(ulong value = 0) : IReadable, IEquatable<PrimitivePointer<T>> where T : unmanaged
{
[NativeInteger]
private ulong _value = value;
public readonly ulong PointerValue => _value;
public readonly bool Null => _value == 0;
public void Read<TReader>(ref TReader reader, in StructVersion version = default) where TReader : IReader, allows ref struct
{
_value = reader.ReadNUInt();
}
public static int Size(in StructVersion version = default, bool is32Bit = false)
{
return is32Bit ? 4 : 8;
}
public readonly T Read(ref SpanReader reader)
{
reader.Offset = (int)PointerValue;
return reader.ReadPrimitive<T>();
}
public readonly ImmutableArray<T> ReadArray(ref SpanReader reader, long count)
{
reader.Offset = (int)PointerValue;
return reader.ReadPrimitiveArray<T>(count);
}
public static implicit operator PrimitivePointer<T>(ulong value) => new(value);
public static implicit operator ulong(PrimitivePointer<T> ptr) => ptr.PointerValue;
#region Equality operators + ToString
public static bool operator ==(PrimitivePointer<T> left, PrimitivePointer<T> right)
=> left._value == right._value;
public static bool operator !=(PrimitivePointer<T> left, PrimitivePointer<T> right)
=> !(left == right);
public readonly override bool Equals(object? obj)
=> obj is PrimitivePointer<T> other && Equals(other);
public readonly bool Equals(PrimitivePointer<T> other)
=> this == other;
public readonly override int GetHashCode()
=> HashCode.Combine(_value);
public readonly override string ToString() => $"0x{_value:X} <{typeof(T).Name}>";
#endregion
}

View File

@@ -87,7 +87,7 @@ namespace Il2CppInspector.Reflection
public TypeModel(Il2CppInspector package) {
Package = package;
TypesByDefinitionIndex = new TypeInfo[package.TypeDefinitions.Length];
TypesByReferenceIndex = new TypeInfo[package.TypeReferences.Count];
TypesByReferenceIndex = new TypeInfo[package.TypeReferences.Length];
GenericParameterTypes = new TypeInfo[package.GenericParameters.Length];
MethodsByDefinitionIndex = new MethodBase[package.Methods.Length];
MethodInvokers = new MethodInvoker[package.MethodInvokePointers.Length];
@@ -99,7 +99,7 @@ namespace Il2CppInspector.Reflection
// Create and reference types from TypeRefs
// Note that you can't resolve any TypeRefs until all the TypeDefs have been processed
for (int typeRefIndex = 0; typeRefIndex < package.TypeReferences.Count; typeRefIndex++) {
for (int typeRefIndex = 0; typeRefIndex < package.TypeReferences.Length; typeRefIndex++) {
if(TypesByReferenceIndex[typeRefIndex] != null) {
/* type already generated - probably by forward reference through GetTypeFromVirtualAddress */
continue;
@@ -191,7 +191,7 @@ namespace Il2CppInspector.Reflection
public TypeInfo[] ResolveGenericArguments(Il2CppGenericInst inst) {
// Get list of pointers to type parameters (both unresolved and concrete)
var genericTypeArguments = Package.BinaryImage.ReadMappedArray<ulong>(inst.TypeArgv, (int)inst.TypeArgc);
var genericTypeArguments = Package.BinaryImage.ReadMappedUWordArray(inst.TypeArgv, (int)inst.TypeArgc);
return genericTypeArguments.Select(a => GetTypeFromVirtualAddress(a)).ToArray();
}
@@ -212,7 +212,7 @@ namespace Il2CppInspector.Reflection
// Constructed types
case Il2CppTypeEnum.IL2CPP_TYPE_GENERICINST:
// TODO: Replace with array load from Il2CppMetadataRegistration.genericClasses
var generic = image.ReadMappedObject<Il2CppGenericClass>(typeRef.Data.GenericClass); // Il2CppGenericClass *
var generic = image.ReadMappedVersionedObject<Il2CppGenericClass>(typeRef.Data.GenericClass); // Il2CppGenericClass *
// Get generic type definition
TypeInfo genericTypeDef;
@@ -230,13 +230,13 @@ namespace Il2CppInspector.Reflection
// Get the instantiation
// TODO: Replace with array load from Il2CppMetadataRegistration.genericInsts
var genericInstance = image.ReadMappedObject<Il2CppGenericInst>(generic.Context.ClassInst);
var genericInstance = image.ReadMappedVersionedObject<Il2CppGenericInst>(generic.Context.ClassInst);
var genericArguments = ResolveGenericArguments(genericInstance);
underlyingType = genericTypeDef.MakeGenericType(genericArguments);
break;
case Il2CppTypeEnum.IL2CPP_TYPE_ARRAY:
var descriptor = image.ReadMappedObject<Il2CppArrayType>(typeRef.Data.ArrayType);
var descriptor = image.ReadMappedVersionedObject<Il2CppArrayType>(typeRef.Data.ArrayType);
var elementType = GetTypeFromVirtualAddress(descriptor.ElementType);
underlyingType = elementType.MakeArrayType(descriptor.Rank);
break;

View File

@@ -1,8 +1,6 @@
using NoisyCowStudios.Bin2Object;
using System.Text;
using System;
using System.Diagnostics;
using System.IO;
using Il2CppInspector.Next;
using Il2CppInspector.Next.BinaryMetadata;
using Il2CppInspector.Next.Metadata;
@@ -11,7 +9,7 @@ namespace Il2CppInspector.Utils;
public static class BlobReader
{
public static object GetConstantValueFromBlob(Il2CppInspector inspector, Il2CppTypeEnum type, BinaryObjectStream blob)
public static object GetConstantValueFromBlob(Il2CppInspector inspector, Il2CppTypeEnum type, BinaryObjectStreamReader blob)
{
const byte kArrayTypeWithDifferentElements = 1;
@@ -28,7 +26,7 @@ public static class BlobReader
break;
case Il2CppTypeEnum.IL2CPP_TYPE_CHAR:
// UTF-8 character assumed
value = BitConverter.ToChar(blob.ReadBytes(2), 0);
value = (char)blob.ReadPrimitive<short>();
break;
case Il2CppTypeEnum.IL2CPP_TYPE_U2:
value = blob.ReadUInt16();
@@ -162,7 +160,7 @@ public static class BlobReader
if (typeEnum == Il2CppTypeEnum.IL2CPP_TYPE_ENUM)
{
var typeIndex = blob.ReadCompressedInt32();
var typeHandle = (uint)inspector.TypeReferences[typeIndex].Data.KlassIndex;
var typeHandle = inspector.TypeReferences[typeIndex].Data.KlassIndex;
enumType = inspector.TypeDefinitions[typeHandle];
var elementTypeHandle = inspector.TypeReferences[enumType.ElementTypeIndex].Data.KlassIndex;

View File

@@ -1,4 +1,5 @@
using VersionedSerialization.Generator.Utils;
using Microsoft.CodeAnalysis.CSharp;
using VersionedSerialization.Generator.Utils;
namespace VersionedSerialization.Generator.Models;
@@ -6,8 +7,7 @@ public sealed record ObjectSerializationInfo(
string Namespace,
string Name,
bool HasBaseType,
bool IsStruct,
bool ShouldGenerateSizeMethod,
SyntaxKind DefinitionType,
bool CanGenerateSizeMethod,
ImmutableEquatableArray<PropertySerializationInfo> Properties
);

View File

@@ -6,6 +6,6 @@ public sealed record PropertySerializationInfo(
string Name,
string ReadMethod,
string SizeExpression,
int Alignment,
PropertyType Type,
ImmutableEquatableArray<VersionCondition> VersionConditions
);

View File

@@ -16,6 +16,9 @@ public enum PropertyType
Int32,
Int64,
String,
Custom,
NativeInteger,
UNativeInteger,
}
public static class PropertyTypeExtensions
@@ -35,6 +38,9 @@ public static class PropertyTypeExtensions
PropertyType.Int32 => nameof(PropertyType.Int32),
PropertyType.Int64 => nameof(PropertyType.Int64),
PropertyType.String => nameof(String),
PropertyType.Custom => "",
PropertyType.NativeInteger => "NInt",
PropertyType.UNativeInteger => "NUInt",
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};
@@ -43,6 +49,40 @@ public static class PropertyTypeExtensions
{
PropertyType.Boolean => true,
PropertyType.String => true,
PropertyType.Custom => true,
PropertyType.NativeInteger => true,
PropertyType.UNativeInteger => true,
_ => false
};
public static int GetTypeSize(this PropertyType type)
=> type switch
{
PropertyType.Unsupported => -1,
PropertyType.None => 0,
PropertyType.UInt8 => 1,
PropertyType.Int8 => 1,
PropertyType.Boolean => 1,
PropertyType.UInt16 => 2,
PropertyType.UInt32 => 4,
PropertyType.UInt64 => 8,
PropertyType.Int16 => 2,
PropertyType.Int32 => 4,
PropertyType.Int64 => 8,
PropertyType.String => -1,
PropertyType.Custom => -1,
PropertyType.NativeInteger => -1,
PropertyType.UNativeInteger => -1,
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};
public static bool IsUnsignedType(this PropertyType type)
=> type switch
{
PropertyType.UInt8
or PropertyType.UInt16
or PropertyType.UInt32
or PropertyType.UInt64 => true,
_ => false
};
}

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
@@ -20,7 +19,7 @@ namespace VersionedSerialization.Generator
var valueProvider = context.SyntaxProvider
.ForAttributeWithMetadataName(Constants.VersionedStructAttribute,
static (node, _) => node is ClassDeclarationSyntax or StructDeclarationSyntax,
static (node, _) => node is ClassDeclarationSyntax or StructDeclarationSyntax or RecordDeclarationSyntax,
static (context, _) => (ContextClass: (TypeDeclarationSyntax)context.TargetNode, context.SemanticModel))
.Combine(context.CompilationProvider)
.Select(static (tuple, cancellationToken) => ParseSerializationInfo(tuple.Left.ContextClass, tuple.Left.SemanticModel, tuple.Right, cancellationToken))
@@ -63,7 +62,16 @@ namespace VersionedSerialization.Generator
generator.LeaveScope();
}
generator.EnterScope($"public partial {(info.IsStruct ? "struct" : "class")} {info.Name} : IReadable");
var definitionType = info.DefinitionType switch
{
SyntaxKind.ClassDeclaration => "class",
SyntaxKind.StructDeclaration => "struct",
SyntaxKind.RecordDeclaration => "record",
SyntaxKind.RecordStructDeclaration => "record struct",
_ => throw new IndexOutOfRangeException()
};
generator.EnterScope($"public partial {definitionType} {info.Name} : IReadable");
GenerateReadMethod(generator, info);
generator.AppendLine();
GenerateSizeMethod(generator, info);
@@ -92,12 +100,7 @@ namespace VersionedSerialization.Generator
GenerateVersionCondition(property.VersionConditions, generator);
generator.EnterScope();
generator.AppendLine($"size += {property.SizeExpression};");
if (property.Alignment != 0)
generator.AppendLine($"size += size % {property.Alignment} == 0 ? 0 : {property.Alignment} - (size % {property.Alignment});");
generator.LeaveScope();
}
@@ -121,10 +124,6 @@ namespace VersionedSerialization.Generator
generator.EnterScope();
generator.AppendLine($"this.{property.Name} = {property.ReadMethod}");
if (property.Alignment != 0)
generator.AppendLine($"reader.Align({property.Alignment});");
generator.LeaveScope();
}
@@ -176,9 +175,10 @@ namespace VersionedSerialization.Generator
{
var classSymbol = model.GetDeclaredSymbol(contextClass, cancellationToken) ?? throw new InvalidOperationException();
var alignedAttribute = compilation.GetTypeByMetadataName(Constants.AlignedAttribute);
//var versionedStructAttribute = compilation.GetTypeByMetadataName(Constants.VersionedStructAttribute);
var versionConditionAttribute = compilation.GetTypeByMetadataName(Constants.VersionConditionAttribute);
var customSerializationAttribute = compilation.GetTypeByMetadataName(Constants.CustomSerializationAttribute);
var nativeIntegerAttribute = compilation.GetTypeByMetadataName(Constants.NativeIntegerAttribute);
var canGenerateSizeMethod = true;
@@ -190,7 +190,6 @@ namespace VersionedSerialization.Generator
|| member is IPropertySymbol { SetMethod: null })
continue;
var alignment = 0;
var versionConditions = new List<VersionCondition>();
ITypeSymbol type;
@@ -207,41 +206,12 @@ namespace VersionedSerialization.Generator
}
var typeInfo = ParseType(type);
canGenerateSizeMethod &= typeInfo.Type != PropertyType.String;
string readMethod;
if (typeInfo.Type == PropertyType.None)
{
readMethod = $"reader.ReadVersionedObject<{typeInfo.ComplexTypeName}>(in version);";
}
else
{
readMethod = typeInfo.Type.IsSeperateMethod()
? $"reader.Read{typeInfo.Type.GetTypeName()}();"
: $"reader.ReadPrimitive<{typeInfo.Type.GetTypeName()}>();";
if (typeInfo.ComplexTypeName != "")
readMethod = $"({typeInfo.ComplexTypeName}){readMethod}";
}
string sizeExpression;
if (typeInfo.Type == PropertyType.None)
{
sizeExpression = $"{typeInfo.ComplexTypeName}.Size(in version, is32Bit)";
}
else
{
sizeExpression = $"sizeof({typeInfo.Type.GetTypeName()})";
}
string? readMethod = null;
string? sizeExpression = null;
foreach (var attribute in member.GetAttributes())
{
if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, alignedAttribute))
{
alignment = (int)attribute.ConstructorArguments[0].Value!;
}
else if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, versionConditionAttribute))
if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, versionConditionAttribute))
{
StructVersion? lessThan = null,
moreThan = null,
@@ -278,16 +248,54 @@ namespace VersionedSerialization.Generator
}
else if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, customSerializationAttribute))
{
typeInfo = (PropertyType.Custom, "", typeInfo.IsArray);
readMethod = (string)attribute.ConstructorArguments[0].Value!;
sizeExpression = (string)attribute.ConstructorArguments[1].Value!;
}
else if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, nativeIntegerAttribute))
{
typeInfo = (typeInfo.Type.IsUnsignedType()
? PropertyType.UNativeInteger
: PropertyType.NativeInteger,
typeInfo.ComplexTypeName == ""
? typeInfo.Type.GetTypeName()
: typeInfo.ComplexTypeName,
typeInfo.IsArray);
}
}
canGenerateSizeMethod &= typeInfo.Type != PropertyType.String;
if (readMethod == null)
{
if (typeInfo.Type == PropertyType.None)
{
readMethod = $"reader.ReadVersionedObject<{typeInfo.ComplexTypeName}>(in version);";
}
else
{
readMethod = typeInfo.Type.IsSeperateMethod()
? $"reader.Read{typeInfo.Type.GetTypeName()}();"
: $"reader.ReadPrimitive<{typeInfo.Type.GetTypeName()}>();";
if (typeInfo.ComplexTypeName != "")
readMethod = $"({typeInfo.ComplexTypeName}){readMethod}";
}
}
sizeExpression ??= typeInfo.Type switch
{
PropertyType.None => $"{typeInfo.ComplexTypeName}.Size(in version, is32Bit)",
PropertyType.NativeInteger or PropertyType.UNativeInteger =>
"is32Bit ? sizeof(uint) : sizeof(ulong)",
_ => $"sizeof({typeInfo.Type.GetTypeName()})"
};
properties.Add(new PropertySerializationInfo(
member.Name,
readMethod,
sizeExpression,
alignment,
typeInfo.Type,
versionConditions.ToImmutableEquatableArray()
));
}
@@ -307,8 +315,7 @@ namespace VersionedSerialization.Generator
classSymbol.ContainingNamespace.ToDisplayString(),
classSymbol.Name,
hasBaseType,
contextClass.Kind() == SyntaxKind.StructDeclaration,
true,
contextClass.Kind(),
canGenerateSizeMethod,
properties.ToImmutableEquatableArray()
);

View File

@@ -5,9 +5,9 @@ public static class Constants
private const string AttributeNamespace = "VersionedSerialization.Attributes";
public const string VersionedStructAttribute = $"{AttributeNamespace}.{nameof(VersionedStructAttribute)}";
public const string AlignedAttribute = $"{AttributeNamespace}.{nameof(AlignedAttribute)}";
public const string VersionConditionAttribute = $"{AttributeNamespace}.{nameof(VersionConditionAttribute)}";
public const string CustomSerializationAttribute = $"{AttributeNamespace}.{nameof(CustomSerializationAttribute)}";
public const string NativeIntegerAttribute = $"{AttributeNamespace}.{nameof(NativeIntegerAttribute)}";
public const string LessThan = nameof(LessThan);
public const string GreaterThan = nameof(GreaterThan);

View File

@@ -1,6 +0,0 @@
namespace VersionedSerialization.Attributes;
#pragma warning disable CS9113 // Parameter is unread.
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class AlignedAttribute(int alignment) : Attribute;
#pragma warning restore CS9113 // Parameter is unread.

View File

@@ -0,0 +1,3 @@
namespace VersionedSerialization.Attributes;
public class NativeIntegerAttribute : Attribute;

View File

@@ -18,5 +18,6 @@ public interface IReader
T ReadVersionedObject<T>(in StructVersion version = default) where T : IReadable, new();
ImmutableArray<T> ReadVersionedObjectArray<T>(long count, in StructVersion version = default) where T : IReadable, new();
public void Align(int alignment = 0);
void Align(int alignment = 0);
void Skip(int count);
}

View File

@@ -143,4 +143,9 @@ public ref struct SpanReader(ReadOnlySpan<byte> data, int offset = 0, bool littl
if (rem != 0)
Offset += alignment - rem;
}
public void Skip(int count)
{
Offset += count;
}
}