Collect field defaults into Il2CppInspector.FieldDefaultValue on load
This commit is contained in:
@@ -17,9 +17,80 @@ namespace Il2CppInspector
|
|||||||
public Il2CppBinary Binary { get; }
|
public Il2CppBinary Binary { get; }
|
||||||
public Metadata Metadata { get; }
|
public Metadata Metadata { get; }
|
||||||
|
|
||||||
|
// Shortcuts
|
||||||
|
public Dictionary<int, string> Strings => Metadata.Strings;
|
||||||
|
|
||||||
|
public Il2CppTypeDefinition[] TypeDefinitions => Metadata.Types;
|
||||||
|
public List<Il2CppType> TypeUsages => Binary.Types;
|
||||||
|
public Dictionary<int, object> FieldDefaultValue { get; } = new Dictionary<int, object>();
|
||||||
|
|
||||||
public Il2CppInspector(Il2CppBinary binary, Metadata metadata) {
|
public Il2CppInspector(Il2CppBinary binary, Metadata metadata) {
|
||||||
|
// Store stream representations
|
||||||
Binary = binary;
|
Binary = binary;
|
||||||
Metadata = metadata;
|
Metadata = metadata;
|
||||||
|
|
||||||
|
// Get all field default values
|
||||||
|
foreach (var fdv in Metadata.FieldDefaultValues) {
|
||||||
|
// No default
|
||||||
|
if (fdv.dataIndex == -1) {
|
||||||
|
FieldDefaultValue.Add(fdv.fieldIndex, null);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get pointer in binary to default value
|
||||||
|
var pValue = Metadata.Header.fieldAndParameterDefaultValueDataOffset + fdv.dataIndex;
|
||||||
|
var type = TypeUsages[fdv.typeIndex];
|
||||||
|
|
||||||
|
// Default value is null
|
||||||
|
if (pValue == 0) {
|
||||||
|
FieldDefaultValue.Add(fdv.fieldIndex, null);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
object value = null;
|
||||||
|
Metadata.Position = pValue;
|
||||||
|
switch (type.type) {
|
||||||
|
case Il2CppTypeEnum.IL2CPP_TYPE_BOOLEAN:
|
||||||
|
value = Metadata.ReadBoolean();
|
||||||
|
break;
|
||||||
|
case Il2CppTypeEnum.IL2CPP_TYPE_U1:
|
||||||
|
case Il2CppTypeEnum.IL2CPP_TYPE_I1:
|
||||||
|
value = Metadata.ReadByte();
|
||||||
|
break;
|
||||||
|
case Il2CppTypeEnum.IL2CPP_TYPE_CHAR:
|
||||||
|
value = Metadata.ReadChar();
|
||||||
|
break;
|
||||||
|
case Il2CppTypeEnum.IL2CPP_TYPE_U2:
|
||||||
|
value = Metadata.ReadUInt16();
|
||||||
|
break;
|
||||||
|
case Il2CppTypeEnum.IL2CPP_TYPE_I2:
|
||||||
|
value = Metadata.ReadInt16();
|
||||||
|
break;
|
||||||
|
case Il2CppTypeEnum.IL2CPP_TYPE_U4:
|
||||||
|
value = Metadata.ReadUInt32();
|
||||||
|
break;
|
||||||
|
case Il2CppTypeEnum.IL2CPP_TYPE_I4:
|
||||||
|
value = Metadata.ReadInt32();
|
||||||
|
break;
|
||||||
|
case Il2CppTypeEnum.IL2CPP_TYPE_U8:
|
||||||
|
value = Metadata.ReadUInt64();
|
||||||
|
break;
|
||||||
|
case Il2CppTypeEnum.IL2CPP_TYPE_I8:
|
||||||
|
value = Metadata.ReadInt64();
|
||||||
|
break;
|
||||||
|
case Il2CppTypeEnum.IL2CPP_TYPE_R4:
|
||||||
|
value = Metadata.ReadSingle();
|
||||||
|
break;
|
||||||
|
case Il2CppTypeEnum.IL2CPP_TYPE_R8:
|
||||||
|
value = Metadata.ReadDouble();
|
||||||
|
break;
|
||||||
|
case Il2CppTypeEnum.IL2CPP_TYPE_STRING:
|
||||||
|
var uiLen = Metadata.ReadInt32();
|
||||||
|
value = Encoding.UTF8.GetString(Metadata.ReadBytes(uiLen));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
FieldDefaultValue.Add(fdv.fieldIndex, value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Il2CppInspector> LoadFromFile(string codeFile, string metadataFile) {
|
public static List<Il2CppInspector> LoadFromFile(string codeFile, string metadataFile) {
|
||||||
@@ -37,9 +108,9 @@ namespace Il2CppInspector
|
|||||||
var memoryStream = new MemoryStream(File.ReadAllBytes(codeFile));
|
var memoryStream = new MemoryStream(File.ReadAllBytes(codeFile));
|
||||||
IFileFormatReader stream =
|
IFileFormatReader stream =
|
||||||
(((IFileFormatReader) ElfReader.Load(memoryStream) ??
|
(((IFileFormatReader) ElfReader.Load(memoryStream) ??
|
||||||
PEReader.Load(memoryStream)) ??
|
PEReader.Load(memoryStream)) ??
|
||||||
MachOReader.Load(memoryStream)) ??
|
MachOReader.Load(memoryStream)) ??
|
||||||
UBReader.Load(memoryStream);
|
UBReader.Load(memoryStream);
|
||||||
if (stream == null) {
|
if (stream == null) {
|
||||||
Console.Error.WriteLine("Unsupported executable file format");
|
Console.Error.WriteLine("Unsupported executable file format");
|
||||||
return null;
|
return null;
|
||||||
@@ -77,16 +148,17 @@ namespace Il2CppInspector
|
|||||||
public string GetTypeName(Il2CppType pType) {
|
public string GetTypeName(Il2CppType pType) {
|
||||||
string ret;
|
string ret;
|
||||||
if (pType.type == Il2CppTypeEnum.IL2CPP_TYPE_CLASS || pType.type == Il2CppTypeEnum.IL2CPP_TYPE_VALUETYPE) {
|
if (pType.type == Il2CppTypeEnum.IL2CPP_TYPE_CLASS || pType.type == Il2CppTypeEnum.IL2CPP_TYPE_VALUETYPE) {
|
||||||
Il2CppTypeDefinition klass = Metadata.Types[pType.datapoint];
|
Il2CppTypeDefinition klass = TypeDefinitions[pType.datapoint];
|
||||||
ret = Metadata.Strings[klass.nameIndex];
|
ret = Strings[klass.nameIndex];
|
||||||
}
|
}
|
||||||
else if (pType.type == Il2CppTypeEnum.IL2CPP_TYPE_GENERICINST) {
|
else if (pType.type == Il2CppTypeEnum.IL2CPP_TYPE_GENERICINST) {
|
||||||
Il2CppGenericClass generic_class = Binary.Image.ReadMappedObject<Il2CppGenericClass>(pType.datapoint);
|
Il2CppGenericClass generic_class = Binary.Image.ReadMappedObject<Il2CppGenericClass>(pType.datapoint);
|
||||||
Il2CppTypeDefinition pMainDef = Metadata.Types[generic_class.typeDefinitionIndex];
|
Il2CppTypeDefinition pMainDef = TypeDefinitions[generic_class.typeDefinitionIndex];
|
||||||
ret = Metadata.Strings[pMainDef.nameIndex];
|
ret = Strings[pMainDef.nameIndex];
|
||||||
var typeNames = new List<string>();
|
var typeNames = new List<string>();
|
||||||
Il2CppGenericInst pInst = Binary.Image.ReadMappedObject<Il2CppGenericInst>(generic_class.context.class_inst);
|
Il2CppGenericInst pInst =
|
||||||
var pointers = Binary.Image.ReadMappedArray<uint>(pInst.type_argv, (int)pInst.type_argc);
|
Binary.Image.ReadMappedObject<Il2CppGenericInst>(generic_class.context.class_inst);
|
||||||
|
var pointers = Binary.Image.ReadMappedArray<uint>(pInst.type_argv, (int) pInst.type_argc);
|
||||||
for (int i = 0; i < pInst.type_argc; ++i) {
|
for (int i = 0; i < pInst.type_argc; ++i) {
|
||||||
var pOriType = Binary.Image.ReadMappedObject<Il2CppType>(pointers[i]);
|
var pOriType = Binary.Image.ReadMappedObject<Il2CppType>(pointers[i]);
|
||||||
typeNames.Add(GetTypeName(pOriType));
|
typeNames.Add(GetTypeName(pOriType));
|
||||||
@@ -103,13 +175,14 @@ namespace Il2CppInspector
|
|||||||
ret = $"{GetTypeName(type)}[]";
|
ret = $"{GetTypeName(type)}[]";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if ((int)pType.type >= DefineConstants.CSharpTypeString.Count)
|
if ((int) pType.type >= DefineConstants.CSharpTypeString.Count)
|
||||||
ret = "unknow";
|
ret = "unknow";
|
||||||
else
|
else
|
||||||
ret = DefineConstants.CSharpTypeString[(int)pType.type];
|
ret = DefineConstants.CSharpTypeString[(int) pType.type];
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int GetFieldOffsetFromIndex(int typeIndex, int fieldIndexInType) {
|
public int GetFieldOffsetFromIndex(int typeIndex, int fieldIndexInType) {
|
||||||
// Versions from 22 onwards use an array of pointers in fieldOffsets
|
// Versions from 22 onwards use an array of pointers in fieldOffsets
|
||||||
bool fieldOffsetsArePointers = (Metadata.Version >= 22);
|
bool fieldOffsetsArePointers = (Metadata.Version >= 22);
|
||||||
@@ -122,70 +195,13 @@ namespace Il2CppInspector
|
|||||||
|
|
||||||
// All older versions use values directly in the array
|
// All older versions use values directly in the array
|
||||||
if (!fieldOffsetsArePointers) {
|
if (!fieldOffsetsArePointers) {
|
||||||
var typeDef = Metadata.Types[typeIndex];
|
var typeDef = TypeDefinitions[typeIndex];
|
||||||
return Binary.FieldOffsets[typeDef.fieldStart + fieldIndexInType];
|
return Binary.FieldOffsets[typeDef.fieldStart + fieldIndexInType];
|
||||||
}
|
}
|
||||||
|
|
||||||
var ptr = Binary.FieldOffsets[typeIndex];
|
var ptr = Binary.FieldOffsets[typeIndex];
|
||||||
Binary.Image.Stream.Position = Binary.Image.MapVATR((uint)ptr) + 4 * fieldIndexInType;
|
Binary.Image.Stream.Position = Binary.Image.MapVATR((uint) ptr) + 4 * fieldIndexInType;
|
||||||
return Binary.Image.Stream.ReadInt32();
|
return Binary.Image.Stream.ReadInt32();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public object GetDefaultValueForField(int fieldIndex) {
|
|
||||||
var def = Metadata.FieldDefaultValues.FirstOrDefault(x => x.fieldIndex == fieldIndex);
|
|
||||||
|
|
||||||
if (def == null || def.dataIndex == -1)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
var pValue = Metadata.Header.fieldAndParameterDefaultValueDataOffset + def.dataIndex;
|
|
||||||
Il2CppType type = Binary.Types[def.typeIndex];
|
|
||||||
if (pValue == 0)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
object value = null;
|
|
||||||
Metadata.Position = pValue;
|
|
||||||
switch (type.type) {
|
|
||||||
case Il2CppTypeEnum.IL2CPP_TYPE_BOOLEAN:
|
|
||||||
value = Metadata.ReadBoolean();
|
|
||||||
break;
|
|
||||||
case Il2CppTypeEnum.IL2CPP_TYPE_U1:
|
|
||||||
case Il2CppTypeEnum.IL2CPP_TYPE_I1:
|
|
||||||
value = Metadata.ReadByte();
|
|
||||||
break;
|
|
||||||
case Il2CppTypeEnum.IL2CPP_TYPE_CHAR:
|
|
||||||
value = Metadata.ReadChar();
|
|
||||||
break;
|
|
||||||
case Il2CppTypeEnum.IL2CPP_TYPE_U2:
|
|
||||||
value = Metadata.ReadUInt16();
|
|
||||||
break;
|
|
||||||
case Il2CppTypeEnum.IL2CPP_TYPE_I2:
|
|
||||||
value = Metadata.ReadInt16();
|
|
||||||
break;
|
|
||||||
case Il2CppTypeEnum.IL2CPP_TYPE_U4:
|
|
||||||
value = Metadata.ReadUInt32();
|
|
||||||
break;
|
|
||||||
case Il2CppTypeEnum.IL2CPP_TYPE_I4:
|
|
||||||
value = Metadata.ReadInt32();
|
|
||||||
break;
|
|
||||||
case Il2CppTypeEnum.IL2CPP_TYPE_U8:
|
|
||||||
value = Metadata.ReadUInt64();
|
|
||||||
break;
|
|
||||||
case Il2CppTypeEnum.IL2CPP_TYPE_I8:
|
|
||||||
value = Metadata.ReadInt64();
|
|
||||||
break;
|
|
||||||
case Il2CppTypeEnum.IL2CPP_TYPE_R4:
|
|
||||||
value = Metadata.ReadSingle();
|
|
||||||
break;
|
|
||||||
case Il2CppTypeEnum.IL2CPP_TYPE_R8:
|
|
||||||
value = Metadata.ReadDouble();
|
|
||||||
break;
|
|
||||||
case Il2CppTypeEnum.IL2CPP_TYPE_STRING:
|
|
||||||
var uiLen = Metadata.ReadInt32();
|
|
||||||
value = Encoding.UTF8.GetString(Metadata.ReadBytes(uiLen));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ namespace Il2CppInspector
|
|||||||
public Il2CppParameterDefinition[] Params { get; }
|
public Il2CppParameterDefinition[] Params { get; }
|
||||||
public Il2CppFieldDefinition[] Fields { get; }
|
public Il2CppFieldDefinition[] Fields { get; }
|
||||||
public Il2CppFieldDefaultValue[] FieldDefaultValues { get; }
|
public Il2CppFieldDefaultValue[] FieldDefaultValues { get; }
|
||||||
|
|
||||||
public Dictionary<int, string> Strings { get; } = new Dictionary<int, string>();
|
public Dictionary<int, string> Strings { get; } = new Dictionary<int, string>();
|
||||||
|
|
||||||
public Metadata(Stream stream) : base(stream)
|
public Metadata(Stream stream) : base(stream)
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ namespace Il2CppInspector.Reflection {
|
|||||||
Model = model;
|
Model = model;
|
||||||
Definition = Model.Package.Metadata.Images[imageIndex];
|
Definition = Model.Package.Metadata.Images[imageIndex];
|
||||||
Index = Definition.assemblyIndex;
|
Index = Definition.assemblyIndex;
|
||||||
FullName = Model.Package.Metadata.Strings[Definition.nameIndex];
|
FullName = Model.Package.Strings[Definition.nameIndex];
|
||||||
|
|
||||||
if (Definition.entryPointIndex != -1) {
|
if (Definition.entryPointIndex != -1) {
|
||||||
// TODO: Generate EntryPoint method from entryPointIndex
|
// TODO: Generate EntryPoint method from entryPointIndex
|
||||||
|
|||||||
@@ -53,9 +53,9 @@ namespace Il2CppInspector.Reflection {
|
|||||||
base(declaringType) {
|
base(declaringType) {
|
||||||
Definition = pkg.Metadata.Fields[fieldIndex];
|
Definition = pkg.Metadata.Fields[fieldIndex];
|
||||||
Index = fieldIndex;
|
Index = fieldIndex;
|
||||||
Name = pkg.Metadata.Strings[pkg.Metadata.Fields[fieldIndex].nameIndex];
|
Name = pkg.Strings[pkg.Metadata.Fields[fieldIndex].nameIndex];
|
||||||
|
|
||||||
fieldType = pkg.Binary.Types[Definition.typeIndex];
|
fieldType = pkg.TypeUsages[Definition.typeIndex];
|
||||||
if ((fieldType.attrs & DefineConstants.FIELD_ATTRIBUTE_PRIVATE) == DefineConstants.FIELD_ATTRIBUTE_PRIVATE)
|
if ((fieldType.attrs & DefineConstants.FIELD_ATTRIBUTE_PRIVATE) == DefineConstants.FIELD_ATTRIBUTE_PRIVATE)
|
||||||
Attributes |= FieldAttributes.Private;
|
Attributes |= FieldAttributes.Private;
|
||||||
if ((fieldType.attrs & DefineConstants.FIELD_ATTRIBUTE_PUBLIC) == DefineConstants.FIELD_ATTRIBUTE_PUBLIC)
|
if ((fieldType.attrs & DefineConstants.FIELD_ATTRIBUTE_PUBLIC) == DefineConstants.FIELD_ATTRIBUTE_PUBLIC)
|
||||||
|
|||||||
@@ -116,10 +116,10 @@ namespace Il2CppInspector.Reflection {
|
|||||||
// Initialize from specified type index in metadata
|
// Initialize from specified type index in metadata
|
||||||
public TypeInfo(Il2CppInspector pkg, int typeIndex, Assembly owner) :
|
public TypeInfo(Il2CppInspector pkg, int typeIndex, Assembly owner) :
|
||||||
base(owner) {
|
base(owner) {
|
||||||
Definition = pkg.Metadata.Types[typeIndex];
|
Definition = pkg.TypeDefinitions[typeIndex];
|
||||||
Index = typeIndex;
|
Index = typeIndex;
|
||||||
Namespace = pkg.Metadata.Strings[Definition.namespaceIndex];
|
Namespace = pkg.Strings[Definition.namespaceIndex];
|
||||||
Name = pkg.Metadata.Strings[pkg.Metadata.Types[typeIndex].nameIndex];
|
Name = pkg.Strings[pkg.TypeDefinitions[typeIndex].nameIndex];
|
||||||
|
|
||||||
IsSerializable = (Definition.flags & DefineConstants.TYPE_ATTRIBUTE_SERIALIZABLE) != 0;
|
IsSerializable = (Definition.flags & DefineConstants.TYPE_ATTRIBUTE_SERIALIZABLE) != 0;
|
||||||
IsPublic = (Definition.flags & DefineConstants.TYPE_ATTRIBUTE_VISIBILITY_MASK) == DefineConstants.TYPE_ATTRIBUTE_PUBLIC;
|
IsPublic = (Definition.flags & DefineConstants.TYPE_ATTRIBUTE_VISIBILITY_MASK) == DefineConstants.TYPE_ATTRIBUTE_PUBLIC;
|
||||||
|
|||||||
Reference in New Issue
Block a user