From d7067225fef098e0643e0aa487e6c423d2d28191 Mon Sep 17 00:00:00 2001 From: LukeFZ <17146677+LukeFZ@users.noreply.github.com> Date: Thu, 30 Nov 2023 21:36:17 +0100 Subject: [PATCH] fix Il2CppType definition for 27.2+ (better) --- Il2CppInspector.Common/IL2CPP/Il2CppBinary.cs | 8 ++- .../IL2CPP/Il2CppBinaryClasses.cs | 68 ++++++------------- .../Reflection/TypeModel.cs | 2 +- 3 files changed, 30 insertions(+), 48 deletions(-) diff --git a/Il2CppInspector.Common/IL2CPP/Il2CppBinary.cs b/Il2CppInspector.Common/IL2CPP/Il2CppBinary.cs index 928a186..d08547f 100644 --- a/Il2CppInspector.Common/IL2CPP/Il2CppBinary.cs +++ b/Il2CppInspector.Common/IL2CPP/Il2CppBinary.cs @@ -382,7 +382,13 @@ namespace Il2CppInspector // Type references (pointer array) var typeRefPointers = Image.ReadMappedArray(MetadataRegistration.ptypes, (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(MetadataRegistration.ptypes, (int) MetadataRegistration.typesCount); + + TypeReferences = + Image.Version >= 27.2 + ? Image.ReadMappedObjectPointerArray(MetadataRegistration.ptypes, (int) MetadataRegistration.typesCount) + .Cast() + .ToList() + : Image.ReadMappedObjectPointerArray(MetadataRegistration.ptypes, (int)MetadataRegistration.typesCount); // Custom attribute constructors (function pointers) // This is managed in Il2CppInspector for metadata >= 27 diff --git a/Il2CppInspector.Common/IL2CPP/Il2CppBinaryClasses.cs b/Il2CppInspector.Common/IL2CPP/Il2CppBinaryClasses.cs index 45e81b1..9138f8b 100644 --- a/Il2CppInspector.Common/IL2CPP/Il2CppBinaryClasses.cs +++ b/Il2CppInspector.Common/IL2CPP/Il2CppBinaryClasses.cs @@ -200,63 +200,39 @@ namespace Il2CppInspector // From metadata.h / il2cpp-runtime-metadata.h public class Il2CppType { + public ulong datapoint; + public ulong bits; // this should be private but we need it to be public for BinaryObjectReader to work + //public Union data { get; set; } + + public virtual uint attrs => (uint) bits & 0xffff; + public virtual Il2CppTypeEnum type => (Il2CppTypeEnum)((bits >> 16) & 0xff); + + public virtual uint num_mods => (uint) (bits >> 24) & 0x3f; + public virtual bool byref => ((bits >> 30) & 1) == 1; + public virtual bool pinned => ((bits >> 31) & 1) == 1; + public virtual bool valuetype => false; + /* union { TypeDefinitionIndex klassIndex; // for VALUETYPE and CLASS ( (uint) bits & 0xffff; /* param attributes or field flags */ - public Il2CppTypeEnum type => (Il2CppTypeEnum)((bits >> 16) & 0xff); - // Unity 2021.1 (v27.2): num_mods becomes 1 bit shorter, shifting byref and pinned left 1 bit, valuetype bit added - - public uint NumMods(double version) => (uint) (bits >> 24) & (version >= 27.2 ? 0x1fu : 0x3fu); - public bool ByRef(double version) => (bits >> (version >= 27.2 ? 29 : 30) & 1) == 1; - public bool Pinned(double version) => (bits >> (version >= 27.2 ? 30 : 31) & 1) == 1; - public bool ValueType(double version) => version >= 27.2 && ((bits >> 31) & 1) == 1; // Was only added in 27.2 - - /*public class Union - { - public ulong dummy; - /// - /// for VALUETYPE and CLASS - /// - public long klassIndex => (long)dummy; - /// - /// for VALUETYPE and CLASS at runtime - /// - public ulong typeHandle => dummy; - /// - /// for PTR and SZARRAY - /// - public ulong type => dummy; - /// - /// for ARRAY - /// - public ulong array => dummy; - /// - /// for VAR and MVAR - /// - public long genericParameterIndex => (long)dummy; - /// - /// for VAR and MVAR at runtime - /// - public ulong genericParameterHandle => dummy; - /// - /// for GENERICINST - /// - public ulong generic_class => dummy; - }*/ + // Unity 2021.1 (v27.2): num_mods becomes 1 bit shorter, shifting byref and pinned left 1 bit, valuetype bit added + public class Il2CppTypeV272 : Il2CppType + { + public override uint num_mods => (uint) (bits >> 24) & 0x1f; + public override bool byref => ((bits >> 29) & 1) == 1; + public override bool pinned => ((bits >> 30) & 1) == 1; + public override bool valuetype => ((bits >> 31) & 1) == 1; } public class Il2CppGenericClass diff --git a/Il2CppInspector.Common/Reflection/TypeModel.cs b/Il2CppInspector.Common/Reflection/TypeModel.cs index 19b460d..40d7847 100644 --- a/Il2CppInspector.Common/Reflection/TypeModel.cs +++ b/Il2CppInspector.Common/Reflection/TypeModel.cs @@ -259,7 +259,7 @@ namespace Il2CppInspector.Reflection } // Create a reference type if necessary - return typeRef.ByRef(Package.Version) ? underlyingType.MakeByRefType() : underlyingType; + return typeRef.byref ? underlyingType.MakeByRefType() : underlyingType; } // Basic primitive types are specified via a flag value