Use actual size of static array initializers

This commit is contained in:
LukeFZ
2023-12-01 08:14:12 +01:00
parent 7e7d345c30
commit 7120970ece
6 changed files with 38 additions and 4 deletions

View File

@@ -89,6 +89,8 @@ namespace Il2CppInspector
// One assembly may contain multiple modules // One assembly may contain multiple modules
public Dictionary<string, Il2CppCodeGenModule> Modules { get; private set; } public Dictionary<string, Il2CppCodeGenModule> Modules { get; private set; }
public List<Il2CppTypeDefinitionSizes> TypeDefinitionSizes { get; private set; }
// Status update callback // Status update callback
private EventHandler<string> OnStatusUpdate { get; set; } private EventHandler<string> OnStatusUpdate { get; set; }
private void StatusUpdate(string status) => OnStatusUpdate?.Invoke(this, status); private void StatusUpdate(string status) => OnStatusUpdate?.Invoke(this, status);
@@ -424,6 +426,9 @@ namespace Il2CppInspector
GenericMethodInvokerIndices.Add(MethodSpecs[tableEntry.genericMethodIndex], tableEntry.indices.invokerIndex); GenericMethodInvokerIndices.Add(MethodSpecs[tableEntry.genericMethodIndex], tableEntry.indices.invokerIndex);
} }
TypeDefinitionSizes = Image.ReadMappedObjectPointerArray<Il2CppTypeDefinitionSizes>(
MetadataRegistration.typeDefinitionsSizes, (int) MetadataRegistration.typeDefinitionsSizesCount);
// Plugin hook to pre-process binary // Plugin hook to pre-process binary
isModified |= PluginHooks.PostProcessBinary(this).IsStreamModified; isModified |= PluginHooks.PostProcessBinary(this).IsStreamModified;
} }

View File

@@ -291,4 +291,12 @@ namespace Il2CppInspector
[Version(Min = 27.1)] [Version(Min = 27.1)]
public int adjustorThunk; public int adjustorThunk;
} }
public class Il2CppTypeDefinitionSizes
{
public uint instanceSize;
public int nativeSize;
public uint staticFieldsSize;
public uint threadStaticFieldsSize;
}
} }

View File

@@ -66,6 +66,7 @@ namespace Il2CppInspector
public Il2CppMethodSpec[] MethodSpecs => Binary.MethodSpecs; public Il2CppMethodSpec[] MethodSpecs => Binary.MethodSpecs;
public Dictionary<Il2CppMethodSpec, ulong> GenericMethodPointers { get; } public Dictionary<Il2CppMethodSpec, ulong> GenericMethodPointers { get; }
public Dictionary<Il2CppMethodSpec, int> GenericMethodInvokerIndices => Binary.GenericMethodInvokerIndices; public Dictionary<Il2CppMethodSpec, int> GenericMethodInvokerIndices => Binary.GenericMethodInvokerIndices;
public List<Il2CppTypeDefinitionSizes> TypeDefinitionSizes => Binary.TypeDefinitionSizes;
// TODO: Finish all file access in the constructor and eliminate the need for this // TODO: Finish all file access in the constructor and eliminate the need for this
public IFileFormatStream BinaryImage => Binary.Image; public IFileFormatStream BinaryImage => Binary.Image;

View File

@@ -7,6 +7,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using dnlib.DotNet; using dnlib.DotNet;
@@ -45,12 +46,26 @@ namespace Il2CppInspector.Outputs
// Attribute arguments // Attribute arguments
var attrArgs = args.Select(a => var attrArgs = args.Select(a =>
new CANamedArgument(true, module.CorLibTypes.String, a.prop, new CAArgument(module.CorLibTypes.String, a.value))); {
var arg = ToArgument(a.value);
return new CANamedArgument(true, arg.Type, a.prop, arg);
}).ToList();
var attr = new CustomAttribute(attCtorRef, null, attrArgs); var attr = new CustomAttribute(attCtorRef, null, attrArgs);
def.CustomAttributes.Add(attr); def.CustomAttributes.Add(attr);
return attr; return attr;
CAArgument ToArgument(object value)
{
return value switch
{
string str => new CAArgument(module.CorLibTypes.String, str),
int i => new CAArgument(module.CorLibTypes.Int32, i),
bool b => new CAArgument(module.CorLibTypes.Boolean, b),
_ => throw new UnreachableException()
};
}
} }
} }
@@ -249,7 +264,10 @@ namespace Il2CppInspector.Outputs
// Static array initializer preview // Static array initializer preview
if (field.HasFieldRVA) { if (field.HasFieldRVA) {
var preview = model.Package.Metadata.ReadBytes((long) field.DefaultValueMetadataAddress, 8); // Attempt to get field size
var fieldSize = field.FieldType.Sizes.nativeSize;
var preview = model.Package.Metadata.ReadBytes((long) field.DefaultValueMetadataAddress, fieldSize);
var previewText = string.Join(" ", preview.Select(b => $"{b:x2}")); var previewText = string.Join(" ", preview.Select(b => $"{b:x2}"));
mField.AddAttribute(module, metadataPreviewAttribute, ("Data", previewText)); mField.AddAttribute(module, metadataPreviewAttribute, ("Data", previewText));

View File

@@ -426,10 +426,10 @@ namespace Il2CppInspector.Outputs
sb.Append($" // Metadata: {field.DefaultValueMetadataAddress.ToAddressString()}"); sb.Append($" // Metadata: {field.DefaultValueMetadataAddress.ToAddressString()}");
// For static array initializers, output metadata address and preview // For static array initializers, output metadata address and preview
if (field.HasFieldRVA && !SuppressMetadata) { if (field.HasFieldRVA && !SuppressMetadata) {
var preview = model.Package.Metadata.ReadBytes((long) field.DefaultValueMetadataAddress, 8); var preview = model.Package.Metadata.ReadBytes((long) field.DefaultValueMetadataAddress, field.FieldType.Sizes.nativeSize);
var previewText = string.Join(" ", preview.Select(b => $"{b:x2}")); var previewText = string.Join(" ", preview.Select(b => $"{b:x2}"));
sb.Append($" // Starts with: {previewText} - Metadata: {field.DefaultValueMetadataAddress.ToAddressString()}"); sb.Append($" // Static value: {previewText} - Metadata: {field.DefaultValueMetadataAddress.ToAddressString()}");
} }
sb.Append("\n"); sb.Append("\n");
} }

View File

@@ -20,6 +20,7 @@ namespace Il2CppInspector.Reflection
{ {
// IL2CPP-specific data // IL2CPP-specific data
public Il2CppTypeDefinition Definition { get; } public Il2CppTypeDefinition Definition { get; }
public Il2CppTypeDefinitionSizes Sizes { get; }
public int Index { get; } = -1; public int Index { get; } = -1;
// This dictionary will cache all instantiated generic types out of this definition. // This dictionary will cache all instantiated generic types out of this definition.
@@ -739,6 +740,7 @@ namespace Il2CppInspector.Reflection
var pkg = Assembly.Model.Package; var pkg = Assembly.Model.Package;
Definition = pkg.TypeDefinitions[typeIndex]; Definition = pkg.TypeDefinitions[typeIndex];
Sizes = pkg.TypeDefinitionSizes[typeIndex];
MetadataToken = (int) Definition.token; MetadataToken = (int) Definition.token;
Index = typeIndex; Index = typeIndex;
Namespace = Regex.Replace(pkg.Strings[Definition.namespaceIndex], @"[^A-Za-z0-9_\-\.<>{}]", ""); Namespace = Regex.Replace(pkg.Strings[Definition.namespaceIndex], @"[^A-Za-z0-9_\-\.<>{}]", "");