From 3345d4566af480e9e8e50f5e85bc5904dcd108a7 Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Wed, 16 Oct 2019 17:00:30 +0200 Subject: [PATCH] Roll up Il2CppInspector.Binary and Il2CppInspector.Metadata --- Il2CppInspector/Il2CppInspector.cs | 25 ++++++++++++++++----- Il2CppInspector/Il2CppReflector.cs | 4 ++-- Il2CppInspector/Reflection/Assembly.cs | 8 +++---- Il2CppInspector/Reflection/EventInfo.cs | 4 ++-- Il2CppInspector/Reflection/FieldInfo.cs | 4 ++-- Il2CppInspector/Reflection/MethodInfo.cs | 10 ++++----- Il2CppInspector/Reflection/ParameterInfo.cs | 6 ++--- Il2CppInspector/Reflection/PropertyInfo.cs | 4 ++-- Il2CppInspector/Reflection/TypeInfo.cs | 6 ++--- 9 files changed, 43 insertions(+), 28 deletions(-) diff --git a/Il2CppInspector/Il2CppInspector.cs b/Il2CppInspector/Il2CppInspector.cs index 74ac2a3..bfcc99b 100644 --- a/Il2CppInspector/Il2CppInspector.cs +++ b/Il2CppInspector/Il2CppInspector.cs @@ -12,17 +12,32 @@ using System.Text; namespace Il2CppInspector { + // Il2CppInspector ties together the binary and metadata files into a congruent API surface public class Il2CppInspector { - public Il2CppBinary Binary { get; } - public Metadata Metadata { get; } + private Il2CppBinary Binary { get; } + private Metadata Metadata { get; } // Shortcuts + public double Version => Metadata.Version; + public Dictionary Strings => Metadata.Strings; public Il2CppTypeDefinition[] TypeDefinitions => Metadata.Types; - public List TypeUsages => Binary.Types; + 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 int[] InterfaceUsageIndices => Metadata.InterfaceUsageIndices; public Dictionary FieldDefaultValue { get; } = new Dictionary(); public List FieldOffsets { get; } + public List TypeUsages => Binary.Types; + public Dictionary Modules => Binary.Modules; + public uint[] MethodPointers => Binary.MethodPointers; + + // TODO: Finish all file access in the constructor and eliminate the need for this + public IFileFormatReader BinaryImage { get; } public Il2CppInspector(Il2CppBinary binary, Metadata metadata) { // Store stream representations @@ -96,10 +111,10 @@ namespace Il2CppInspector // Get all field offsets // Versions from 22 onwards use an array of pointers in Binary.FieldOffsetData - bool fieldOffsetsArePointers = (Metadata.Version >= 22); + bool fieldOffsetsArePointers = (Version >= 22); // Some variants of 21 also use an array of pointers - if (Metadata.Version == 21) { + if (Version == 21) { var f = Binary.FieldOffsetData; // We detect this by relying on the fact Module, Object, ValueType, Attribute, _Attribute and Int32 // are always the first six defined types, and that all but Int32 have no fields diff --git a/Il2CppInspector/Il2CppReflector.cs b/Il2CppInspector/Il2CppReflector.cs index 8e09716..76718de 100644 --- a/Il2CppInspector/Il2CppReflector.cs +++ b/Il2CppInspector/Il2CppReflector.cs @@ -1,5 +1,5 @@ /* - Copyright 2017 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com + Copyright 2017-2019 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com All rights reserved. */ @@ -19,7 +19,7 @@ namespace Il2CppInspector.Reflection Package = package; // Create Assembly objects from Il2Cpp package - for (var image = 0; image < package.Metadata.Images.Length; image++) + for (var image = 0; image < package.Images.Length; image++) Assemblies.Add(new Assembly(this, image)); } diff --git a/Il2CppInspector/Reflection/Assembly.cs b/Il2CppInspector/Reflection/Assembly.cs index 95f555c..2987aa1 100644 --- a/Il2CppInspector/Reflection/Assembly.cs +++ b/Il2CppInspector/Reflection/Assembly.cs @@ -1,5 +1,5 @@ /* - Copyright 2017 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com + Copyright 2017-2019 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com All rights reserved. */ @@ -34,7 +34,7 @@ namespace Il2CppInspector.Reflection { // Initialize from specified assembly index in package public Assembly(Il2CppReflector model, int imageIndex) { Model = model; - Definition = Model.Package.Metadata.Images[imageIndex]; + Definition = Model.Package.Images[imageIndex]; Index = Definition.assemblyIndex; FullName = Model.Package.Strings[Definition.nameIndex]; @@ -43,8 +43,8 @@ namespace Il2CppInspector.Reflection { } // Find corresponding module (we'll need this for method pointers) - if (Model.Package.Metadata.Version >= 24.1) - Module = Model.Package.Binary.Modules[FullName]; + if (Model.Package.Version >= 24.1) + Module = Model.Package.Modules[FullName]; // Generate types in DefinedTypes from typeStart to typeStart+typeCount-1 for (var t = Definition.typeStart; t < Definition.typeStart + Definition.typeCount; t++) diff --git a/Il2CppInspector/Reflection/EventInfo.cs b/Il2CppInspector/Reflection/EventInfo.cs index a467027..f89ecf1 100644 --- a/Il2CppInspector/Reflection/EventInfo.cs +++ b/Il2CppInspector/Reflection/EventInfo.cs @@ -1,5 +1,5 @@ /* - Copyright 2017 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com + Copyright 2017-2019 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com All rights reserved. */ @@ -33,7 +33,7 @@ namespace Il2CppInspector.Reflection public EventInfo(Il2CppInspector pkg, int eventIndex, TypeInfo declaringType) : base(declaringType) { - Definition = pkg.Metadata.Events[eventIndex]; + Definition = pkg.Events[eventIndex]; Index = eventIndex; Name = pkg.Strings[Definition.nameIndex]; diff --git a/Il2CppInspector/Reflection/FieldInfo.cs b/Il2CppInspector/Reflection/FieldInfo.cs index 7d9f9f2..3598913 100644 --- a/Il2CppInspector/Reflection/FieldInfo.cs +++ b/Il2CppInspector/Reflection/FieldInfo.cs @@ -1,5 +1,5 @@ /* - Copyright 2017 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com + Copyright 2017-2019 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com All rights reserved. */ @@ -83,7 +83,7 @@ namespace Il2CppInspector.Reflection { public FieldInfo(Il2CppInspector pkg, int fieldIndex, TypeInfo declaringType) : base(declaringType) { - Definition = pkg.Metadata.Fields[fieldIndex]; + Definition = pkg.Fields[fieldIndex]; Index = fieldIndex; Offset = pkg.FieldOffsets[fieldIndex]; Name = pkg.Strings[Definition.nameIndex]; diff --git a/Il2CppInspector/Reflection/MethodInfo.cs b/Il2CppInspector/Reflection/MethodInfo.cs index adab50e..fdaf842 100644 --- a/Il2CppInspector/Reflection/MethodInfo.cs +++ b/Il2CppInspector/Reflection/MethodInfo.cs @@ -31,7 +31,7 @@ namespace Il2CppInspector.Reflection public MethodInfo(Il2CppInspector pkg, int methodIndex, TypeInfo declaringType) : base(declaringType) { - Definition = pkg.Metadata.Methods[methodIndex]; + Definition = pkg.Methods[methodIndex]; Index = methodIndex; Name = pkg.Strings[Definition.nameIndex]; @@ -39,16 +39,16 @@ namespace Il2CppInspector.Reflection if (Definition.methodIndex >= 0) { // Global method pointer array - if (pkg.Metadata.Version < 24.1) { - VirtualAddress = pkg.Binary.MethodPointers[Definition.methodIndex]; + if (pkg.Version < 24.1) { + VirtualAddress = pkg.MethodPointers[Definition.methodIndex]; } // Per-module method pointer array uses the bottom 24 bits of the method's metadata token // Derived from il2cpp::vm::MetadataCache::GetMethodPointer else { var method = (Definition.token & 0xffffff) - 1; - pkg.Binary.Image.Position = pkg.Binary.Image.MapVATR(Assembly.Module.methodPointers + method * 4); - VirtualAddress = pkg.Binary.Image.ReadUInt32(); + pkg.BinaryImage.Position = pkg.BinaryImage.MapVATR(Assembly.Module.methodPointers + method * 4); + VirtualAddress = pkg.BinaryImage.ReadUInt32(); } // Remove ARM Thumb marker LSB if necessary diff --git a/Il2CppInspector/Reflection/ParameterInfo.cs b/Il2CppInspector/Reflection/ParameterInfo.cs index bab31fa..5fd67f7 100644 --- a/Il2CppInspector/Reflection/ParameterInfo.cs +++ b/Il2CppInspector/Reflection/ParameterInfo.cs @@ -1,5 +1,5 @@ /* - Copyright 2017 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com + Copyright 2017-2019 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com All rights reserved. */ @@ -51,8 +51,8 @@ namespace Il2CppInspector.Reflection return; } - var param = pkg.Metadata.Params[paramIndex]; - Name = pkg.Metadata.Strings[param.nameIndex]; + var param = pkg.Params[paramIndex]; + Name = pkg.Strings[param.nameIndex]; Position = paramIndex - declaringMethod.Definition.parameterStart; paramType = pkg.TypeUsages[param.typeIndex]; diff --git a/Il2CppInspector/Reflection/PropertyInfo.cs b/Il2CppInspector/Reflection/PropertyInfo.cs index 8d59db8..d35b6a9 100644 --- a/Il2CppInspector/Reflection/PropertyInfo.cs +++ b/Il2CppInspector/Reflection/PropertyInfo.cs @@ -1,5 +1,5 @@ /* - Copyright 2017 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com + Copyright 2017-2019 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com All rights reserved. */ @@ -25,7 +25,7 @@ namespace Il2CppInspector.Reflection { public PropertyInfo(Il2CppInspector pkg, int propIndex, TypeInfo declaringType) : base(declaringType) { - var prop = pkg.Metadata.Properties[propIndex]; + var prop = pkg.Properties[propIndex]; Name = pkg.Strings[prop.nameIndex]; diff --git a/Il2CppInspector/Reflection/TypeInfo.cs b/Il2CppInspector/Reflection/TypeInfo.cs index 6ee3f6c..c8d62bb 100644 --- a/Il2CppInspector/Reflection/TypeInfo.cs +++ b/Il2CppInspector/Reflection/TypeInfo.cs @@ -1,5 +1,5 @@ /* - Copyright 2017 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com + Copyright 2017-2019 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com All rights reserved. */ @@ -183,7 +183,7 @@ namespace Il2CppInspector.Reflection { // Add all implemented interfaces implementedInterfaces = new Il2CppType[Definition.interfaces_count]; for (var i = 0; i < Definition.interfaces_count; i++) - implementedInterfaces[i] = pkg.TypeUsages[pkg.Metadata.InterfaceUsageIndices[Definition.interfacesStart + i]]; + implementedInterfaces[i] = pkg.TypeUsages[pkg.InterfaceUsageIndices[Definition.interfacesStart + i]]; // Add all fields for (var f = Definition.fieldStart; f < Definition.fieldStart + Definition.field_count; f++) @@ -205,7 +205,7 @@ namespace Il2CppInspector.Reflection { // Initialize type from binary usage public TypeInfo(Il2CppReflector model, Il2CppType pType, MemberTypes memberType) : base(null) { - var image = model.Package.Binary.Image; + var image = model.Package.BinaryImage; IsNested = true; MemberType = memberType;