Support metadata v23

This commit is contained in:
Katy Coe
2017-10-20 03:00:28 +02:00
parent fc1ff07036
commit e5568d0dde
6 changed files with 88 additions and 35 deletions

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using NoisyCowStudios.Bin2Object;
namespace Il2CppInspector namespace Il2CppInspector
{ {
@@ -7,23 +8,49 @@ namespace Il2CppInspector
{ {
public uint methodPointersCount; public uint methodPointersCount;
public uint pmethodPointers; public uint pmethodPointers;
public uint delegateWrappersFromNativeToManagedCount; public uint delegateWrappersFromNativeToManagedCount; // (was renamed to reversePInvokeWrapperCount in v22)
public uint delegateWrappersFromNativeToManaged; // note the double indirection to handle different calling conventions public uint delegateWrappersFromNativeToManaged; // (was renamed to reversePInvokeWrappers in v22)
// Removed in metadata v23
[Version(Max = 22)]
public uint delegateWrappersFromManagedToNativeCount; public uint delegateWrappersFromManagedToNativeCount;
[Version(Max = 22)]
public uint delegateWrappersFromManagedToNative; public uint delegateWrappersFromManagedToNative;
[Version(Max = 22)]
public uint marshalingFunctionsCount; public uint marshalingFunctionsCount;
[Version(Max = 22)]
public uint marshalingFunctions; public uint marshalingFunctions;
[Version(Max = 22)]
public uint ccwMarshalingFunctionsCount; public uint ccwMarshalingFunctionsCount;
[Version(Max = 22)]
public uint ccwMarshalingFunctions; public uint ccwMarshalingFunctions;
public uint genericMethodPointersCount; public uint genericMethodPointersCount;
public uint genericMethodPointers; public uint genericMethodPointers;
public uint invokerPointersCount; public uint invokerPointersCount;
public uint invokerPointers; public uint invokerPointers;
public int customAttributeCount; public int customAttributeCount;
public uint customAttributeGenerators; public uint customAttributeGenerators;
// Removed in metadata v23
[Version(Max = 22)]
public int guidCount; public int guidCount;
[Version(Max = 22)]
public uint guids; // Il2CppGuid public uint guids; // Il2CppGuid
// Added in metadata v22
[Version(Min = 22)]
public uint unresolvedVirtualCallCount;
[Version(Min = 22)]
public uint unresolvedVirtualCallPointers;
// Added in metadata v23
[Version(Min = 23)]
public uint interopDataCount;
[Version(Min = 23)]
public uint interopData;
// Properties
public uint[] methodPointers public uint[] methodPointers
{ {
get; set; get; set;

View File

@@ -55,7 +55,7 @@ namespace Il2CppInspector
} }
// Find code and metadata regions // Find code and metadata regions
if (!il2cpp.Load()) { if (!il2cpp.Load(metadata.Version)) {
Console.Error.WriteLine("Could not process IL2CPP image"); Console.Error.WriteLine("Could not process IL2CPP image");
return null; return null;
} }

View File

@@ -27,7 +27,8 @@ namespace Il2CppInspector
protected abstract (uint, uint) Search(uint loc, uint globalOffset); protected abstract (uint, uint) Search(uint loc, uint globalOffset);
// Check all search locations // Check all search locations
public bool Load() { public bool Load(int version) {
Image.Stream.Version = version;
var addrs = Image.GetSearchLocations(); var addrs = Image.GetSearchLocations();
foreach (var loc in addrs) foreach (var loc in addrs)
if (loc != 0) { if (loc != 0) {

View File

@@ -28,32 +28,37 @@ namespace Il2CppInspector
public string GetTypeNamespace(Il2CppTypeDefinition type) => GetString(type.namespaceIndex); public string GetTypeNamespace(Il2CppTypeDefinition type) => GetString(type.namespaceIndex);
public string GetTypeName(Il2CppTypeDefinition type) => GetString(type.nameIndex); public string GetTypeName(Il2CppTypeDefinition type) => GetString(type.nameIndex);
public Metadata(Stream stream) : base(stream) public Metadata(Stream stream) : base(stream)
{ {
pMetadataHdr = ReadObject<Il2CppGlobalMetadataHeader>(); // Read magic bytes
if (pMetadataHdr.sanity != 0xFAB11BAF) if (ReadUInt32() != 0xFAB11BAF) {
{
throw new Exception("ERROR: Metadata file supplied is not valid metadata file."); throw new Exception("ERROR: Metadata file supplied is not valid metadata file.");
} }
if (pMetadataHdr.version != 21 && pMetadataHdr.version != 22)
// Set object versioning for Bin2Object from metadata version
Version = ReadInt32();
// Rewind and read metadata header in full
Position -= 8;
pMetadataHdr = ReadObject<Il2CppGlobalMetadataHeader>();
if (Version != 21 && Version != 22 && Version != 23)
{ {
throw new Exception($"ERROR: Metadata file supplied is not a supported version ({pMetadataHdr.version})."); throw new Exception($"ERROR: Metadata file supplied is not a supported version ({pMetadataHdr.version}).");
} }
var uiImageCount = pMetadataHdr.imagesCount / MySizeOf(typeof(Il2CppImageDefinition));
var uiNumTypes = pMetadataHdr.typeDefinitionsCount / MySizeOf(typeof(Il2CppTypeDefinition)); var uiImageCount = pMetadataHdr.imagesCount / Sizeof(typeof(Il2CppImageDefinition));
var uiNumTypes = pMetadataHdr.typeDefinitionsCount / Sizeof(typeof(Il2CppTypeDefinition));
Images = ReadArray<Il2CppImageDefinition>(pMetadataHdr.imagesOffset, uiImageCount); Images = ReadArray<Il2CppImageDefinition>(pMetadataHdr.imagesOffset, uiImageCount);
//GetTypeDefFromIndex //GetTypeDefFromIndex
Types = ReadArray<Il2CppTypeDefinition>(pMetadataHdr.typeDefinitionsOffset, uiNumTypes); Types = ReadArray<Il2CppTypeDefinition>(pMetadataHdr.typeDefinitionsOffset, uiNumTypes);
//GetMethodDefinition //GetMethodDefinition
Methods = ReadArray<Il2CppMethodDefinition>(pMetadataHdr.methodsOffset, pMetadataHdr.methodsCount / MySizeOf(typeof(Il2CppMethodDefinition))); Methods = ReadArray<Il2CppMethodDefinition>(pMetadataHdr.methodsOffset, pMetadataHdr.methodsCount / Sizeof(typeof(Il2CppMethodDefinition)));
//GetParameterFromIndex //GetParameterFromIndex
parameterDefs = ReadArray<Il2CppParameterDefinition>(pMetadataHdr.parametersOffset, pMetadataHdr.parametersCount / MySizeOf(typeof(Il2CppParameterDefinition))); parameterDefs = ReadArray<Il2CppParameterDefinition>(pMetadataHdr.parametersOffset, pMetadataHdr.parametersCount / Sizeof(typeof(Il2CppParameterDefinition)));
//GetFieldDefFromIndex //GetFieldDefFromIndex
Fields = ReadArray<Il2CppFieldDefinition>(pMetadataHdr.fieldsOffset, pMetadataHdr.fieldsCount / MySizeOf(typeof(Il2CppFieldDefinition))); Fields = ReadArray<Il2CppFieldDefinition>(pMetadataHdr.fieldsOffset, pMetadataHdr.fieldsCount / Sizeof(typeof(Il2CppFieldDefinition)));
//GetFieldDefaultFromIndex //GetFieldDefaultFromIndex
fieldDefaultValues = ReadArray<Il2CppFieldDefaultValue>(pMetadataHdr.fieldDefaultValuesOffset, pMetadataHdr.fieldDefaultValuesCount / MySizeOf(typeof(Il2CppFieldDefaultValue))); fieldDefaultValues = ReadArray<Il2CppFieldDefaultValue>(pMetadataHdr.fieldDefaultValuesOffset, pMetadataHdr.fieldDefaultValuesCount / Sizeof(typeof(Il2CppFieldDefaultValue)));
} }
public Il2CppFieldDefaultValue GetFieldDefaultFromIndex(int idx) public Il2CppFieldDefaultValue GetFieldDefaultFromIndex(int idx)
@@ -71,27 +76,24 @@ namespace Il2CppInspector
return ReadNullTerminatedString(pMetadataHdr.stringOffset + idx); return ReadNullTerminatedString(pMetadataHdr.stringOffset + idx);
} }
private int MySizeOf(Type type) private int Sizeof(Type type)
{ {
int size = 0; int size = 0;
foreach (var i in type.GetTypeInfo().GetFields()) foreach (var i in type.GetTypeInfo().GetFields())
{ {
if (i.FieldType == typeof(int)) // Only process fields for our selected object versioning
{ var versionAttr = i.GetCustomAttribute<VersionAttribute>(false);
if (versionAttr != null) {
if (versionAttr.Min != -1 && versionAttr.Min > Version)
continue;
if (versionAttr.Max != -1 && versionAttr.Max < Version)
continue;
}
if (i.FieldType == typeof(int) || i.FieldType == typeof(uint))
size += 4; size += 4;
} if (i.FieldType == typeof(short) || i.FieldType == typeof(ushort))
else if (i.FieldType == typeof(uint))
{
size += 4;
}
else if (i.FieldType == typeof(short))
{
size += 2; size += 2;
}
else if (i.FieldType == typeof(ushort))
{
size += 2;
}
} }
return size; return size;
} }

View File

@@ -2,12 +2,14 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using NoisyCowStudios.Bin2Object;
namespace Il2CppInspector namespace Il2CppInspector
{ {
#pragma warning disable CS0649 #pragma warning disable CS0649
public class Il2CppGlobalMetadataHeader public class Il2CppGlobalMetadataHeader
{ {
// Metadata v21
public uint sanity; public uint sanity;
public int version; public int version;
public int stringLiteralOffset; // string data for managed code public int stringLiteralOffset; // string data for managed code
@@ -68,6 +70,22 @@ namespace Il2CppInspector
public int attributesInfoCount; public int attributesInfoCount;
public int attributeTypesOffset; // TypeIndex public int attributeTypesOffset; // TypeIndex
public int attributeTypesCount; public int attributeTypesCount;
// Added in metadata v22
[Version(Min = 22)]
public int unresolvedVirtualCallParameterTypesOffset; // TypeIndex
[Version(Min = 22)]
public int unresolvedVirtualCallParameterTypesCount;
[Version(Min = 22)]
public int unresolvedVirtualCallParameterRangesOffset; // Il2CppRange
[Version(Min = 22)]
public int unresolvedVirtualCallParameterRangesCount;
// Added in metadata v23
[Version(Min = 23)]
public int windowsRuntimeTypeNamesOffset; // Il2CppWindowsRuntimeTypeNamePair
[Version(Min = 23)]
public int windowsRuntimeTypeNamesSize;
} }
public class Il2CppImageDefinition public class Il2CppImageDefinition
@@ -100,9 +118,14 @@ namespace Il2CppInspector
public int genericContainerIndex; public int genericContainerIndex;
public int delegateWrapperFromManagedToNativeIndex; // Removed in metadata v23
[Version(Max = 22)]
public int delegateWrapperFromManagedToNativeIndex; // (was renamed to reversePInvokeWrapperIndex in v22)
[Version(Max = 22)]
public int marshalingFunctionsIndex; public int marshalingFunctionsIndex;
[Version(Max = 22)]
public int ccwFunctionIndex; public int ccwFunctionIndex;
[Version(Max = 22)]
public int guidIndex; public int guidIndex;
public uint flags; public uint flags;
@@ -131,7 +154,7 @@ namespace Il2CppInspector
// 03 - has_finalize; // 03 - has_finalize;
// 04 - has_cctor; // 04 - has_cctor;
// 05 - is_blittable; // 05 - is_blittable;
// 06 - is_import; // 06 - is_import; (from v22: is_import_or_windows_runtime)
// 07-10 - One of nine possible PackingSize values (0, 1, 2, 4, 8, 16, 32, 64, or 128) // 07-10 - One of nine possible PackingSize values (0, 1, 2, 4, 8, 16, 32, 64, or 128)
public uint bitfield; public uint bitfield;
public uint token; public uint token;
@@ -147,7 +170,7 @@ namespace Il2CppInspector
public int genericContainerIndex; public int genericContainerIndex;
public int methodIndex; public int methodIndex;
public int invokerIndex; public int invokerIndex;
public int delegateWrapperIndex; public int delegateWrapperIndex; // (was renamed to reversePInvokeWrapperIndex in v22)
public int rgctxStartIndex; public int rgctxStartIndex;
public int rgctxCount; public int rgctxCount;
public uint token; public uint token;