Model: Add GetMethodBody() to CustomAttributeData and MethodInvoker

This commit is contained in:
Katy Coe
2020-08-09 20:53:10 +02:00
parent 010bec7dc0
commit a252800052
3 changed files with 13 additions and 5 deletions

View File

@@ -34,6 +34,9 @@ namespace Il2CppInspector.Reflection
public override string ToString() => "[" + AttributeType.FullName + "]"; public override string ToString() => "[" + AttributeType.FullName + "]";
// Get the machine code of the C++ function
public byte[] GetMethodBody() => Model.Package.BinaryImage.ReadMappedBytes(VirtualAddress.Start, (int) (VirtualAddress.End - VirtualAddress.Start));
// Get all the custom attributes for a given assembly, type, member or parameter // Get all the custom attributes for a given assembly, type, member or parameter
private static IEnumerable<CustomAttributeData> getCustomAttributes(Assembly asm, int customAttributeIndex) { private static IEnumerable<CustomAttributeData> getCustomAttributes(Assembly asm, int customAttributeIndex) {
if (customAttributeIndex < 0) if (customAttributeIndex < 0)

View File

@@ -89,8 +89,7 @@ namespace Il2CppInspector.Reflection
return null; return null;
var image = Assembly.Model.Package.BinaryImage; var image = Assembly.Model.Package.BinaryImage;
image.Position = image.MapVATR(VirtualAddress.Value.Start); return image.ReadMappedBytes(VirtualAddress.Value.Start, (int) (VirtualAddress.Value.End - VirtualAddress.Value.Start));
return image.ReadBytes((int) (VirtualAddress.Value.End - VirtualAddress.Value.Start));
} }
public string CSharpName => public string CSharpName =>

View File

@@ -20,6 +20,9 @@ namespace Il2CppInspector.Reflection
// IL2CPP invoker index // IL2CPP invoker index
public int Index { get; } public int Index { get; }
// IL2CPP package
public Il2CppInspector Package { get; }
// Virtual address of the invoker function // Virtual address of the invoker function
public (ulong Start, ulong End) VirtualAddress { get; } public (ulong Start, ulong End) VirtualAddress { get; }
@@ -35,7 +38,7 @@ namespace Il2CppInspector.Reflection
// Find the correct method invoker for a method with a specific signature // Find the correct method invoker for a method with a specific signature
public MethodInvoker(MethodBase exampleMethod, int index) { public MethodInvoker(MethodBase exampleMethod, int index) {
var model = exampleMethod.Assembly.Model; var model = exampleMethod.Assembly.Model;
var package = exampleMethod.Assembly.Model.Package; Package = exampleMethod.Assembly.Model.Package;
Index = index; Index = index;
IsStatic = exampleMethod.IsStatic; IsStatic = exampleMethod.IsStatic;
@@ -43,8 +46,8 @@ namespace Il2CppInspector.Reflection
ReturnType = exampleMethod.IsConstructor ? model.TypesByFullName["System.Void"] : mapParameterType(model, ((MethodInfo) exampleMethod).ReturnType); ReturnType = exampleMethod.IsConstructor ? model.TypesByFullName["System.Void"] : mapParameterType(model, ((MethodInfo) exampleMethod).ReturnType);
ParameterTypes = exampleMethod.DeclaredParameters.Select(p => mapParameterType(model, p.ParameterType)).ToArray(); ParameterTypes = exampleMethod.DeclaredParameters.Select(p => mapParameterType(model, p.ParameterType)).ToArray();
var start = package.MethodInvokePointers[Index]; var start = Package.MethodInvokePointers[Index];
VirtualAddress = (start & 0xffff_ffff_ffff_fffe, package.FunctionAddresses[start]); VirtualAddress = (start & 0xffff_ffff_ffff_fffe, Package.FunctionAddresses[start]);
} }
// The invokers use Object for all reference types, and SByte for booleans // The invokers use Object for all reference types, and SByte for booleans
@@ -54,6 +57,9 @@ namespace Il2CppInspector.Reflection
_ => type _ => type
}; };
// Get the machine code of the C++ function
public byte[] GetMethodBody() => Package.BinaryImage.ReadMappedBytes(VirtualAddress.Start, (int) (VirtualAddress.End - VirtualAddress.Start));
public string Name => $"RuntimeInvoker_{!IsStatic}{ReturnType.BaseName.ToCIdentifier()}_" + string.Join("_", ParameterTypes.Select(p => p.BaseName.ToCIdentifier())); public string Name => $"RuntimeInvoker_{!IsStatic}{ReturnType.BaseName.ToCIdentifier()}_" + string.Join("_", ParameterTypes.Select(p => p.BaseName.ToCIdentifier()));
// Display as a C++ method signature; MethodInfo* is the same as RuntimeMethod* (see codegen/il2cpp-codegen-metadata.h) // Display as a C++ method signature; MethodInfo* is the same as RuntimeMethod* (see codegen/il2cpp-codegen-metadata.h)