From a252800052b6eeb701c0855890df42e8b92deb6f Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Sun, 9 Aug 2020 20:53:10 +0200 Subject: [PATCH] Model: Add GetMethodBody() to CustomAttributeData and MethodInvoker --- .../Reflection/CustomAttributeData.cs | 3 +++ Il2CppInspector.Common/Reflection/MethodBase.cs | 3 +-- Il2CppInspector.Common/Reflection/MethodInvoker.cs | 12 +++++++++--- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Il2CppInspector.Common/Reflection/CustomAttributeData.cs b/Il2CppInspector.Common/Reflection/CustomAttributeData.cs index 6585248..d3de450 100644 --- a/Il2CppInspector.Common/Reflection/CustomAttributeData.cs +++ b/Il2CppInspector.Common/Reflection/CustomAttributeData.cs @@ -34,6 +34,9 @@ namespace Il2CppInspector.Reflection 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 private static IEnumerable getCustomAttributes(Assembly asm, int customAttributeIndex) { if (customAttributeIndex < 0) diff --git a/Il2CppInspector.Common/Reflection/MethodBase.cs b/Il2CppInspector.Common/Reflection/MethodBase.cs index 8644b17..aad0a36 100644 --- a/Il2CppInspector.Common/Reflection/MethodBase.cs +++ b/Il2CppInspector.Common/Reflection/MethodBase.cs @@ -89,8 +89,7 @@ namespace Il2CppInspector.Reflection return null; var image = Assembly.Model.Package.BinaryImage; - image.Position = image.MapVATR(VirtualAddress.Value.Start); - return image.ReadBytes((int) (VirtualAddress.Value.End - VirtualAddress.Value.Start)); + return image.ReadMappedBytes(VirtualAddress.Value.Start, (int) (VirtualAddress.Value.End - VirtualAddress.Value.Start)); } public string CSharpName => diff --git a/Il2CppInspector.Common/Reflection/MethodInvoker.cs b/Il2CppInspector.Common/Reflection/MethodInvoker.cs index d4a2732..c002a01 100644 --- a/Il2CppInspector.Common/Reflection/MethodInvoker.cs +++ b/Il2CppInspector.Common/Reflection/MethodInvoker.cs @@ -20,6 +20,9 @@ namespace Il2CppInspector.Reflection // IL2CPP invoker index public int Index { get; } + // IL2CPP package + public Il2CppInspector Package { get; } + // Virtual address of the invoker function 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 public MethodInvoker(MethodBase exampleMethod, int index) { var model = exampleMethod.Assembly.Model; - var package = exampleMethod.Assembly.Model.Package; + Package = exampleMethod.Assembly.Model.Package; Index = index; IsStatic = exampleMethod.IsStatic; @@ -43,8 +46,8 @@ namespace Il2CppInspector.Reflection ReturnType = exampleMethod.IsConstructor ? model.TypesByFullName["System.Void"] : mapParameterType(model, ((MethodInfo) exampleMethod).ReturnType); ParameterTypes = exampleMethod.DeclaredParameters.Select(p => mapParameterType(model, p.ParameterType)).ToArray(); - var start = package.MethodInvokePointers[Index]; - VirtualAddress = (start & 0xffff_ffff_ffff_fffe, package.FunctionAddresses[start]); + var start = Package.MethodInvokePointers[Index]; + VirtualAddress = (start & 0xffff_ffff_ffff_fffe, Package.FunctionAddresses[start]); } // The invokers use Object for all reference types, and SByte for booleans @@ -54,6 +57,9 @@ namespace Il2CppInspector.Reflection _ => 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())); // Display as a C++ method signature; MethodInfo* is the same as RuntimeMethod* (see codegen/il2cpp-codegen-metadata.h)