From 307cf29dcdb474e1141a5fe101e1993bfecdfc8b Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Sat, 18 Jul 2020 03:33:33 +0200 Subject: [PATCH] C++: Re-factor custom attribute generator signature handling --- Il2CppInspector.Common/Model/AppModel.cs | 14 -------------- .../Outputs/IDAPythonScript.cs | 16 +++++++--------- .../Reflection/CustomAttributeData.cs | 7 +++++++ 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/Il2CppInspector.Common/Model/AppModel.cs b/Il2CppInspector.Common/Model/AppModel.cs index 27ca4f0..41e68c9 100644 --- a/Il2CppInspector.Common/Model/AppModel.cs +++ b/Il2CppInspector.Common/Model/AppModel.cs @@ -53,9 +53,6 @@ namespace Il2CppInspector.Model // For il2cpp < 19, the key is the string literal ordinal instead of the address public Dictionary Strings = new Dictionary(); - // All of the custom attribute generator functions in the library - public Dictionary CustomAttributeGenerators = new Dictionary(); - public bool StringIndexesAreOrdinals => Package.MetadataUsages == null; // The .NET type model for the application @@ -222,17 +219,6 @@ namespace Il2CppInspector.Model } } - // Add custom attribute generators to the model - foreach (var cppMethod in ILModel.AttributesByIndices.Values.Where(m => m.VirtualAddress.HasValue)) { - var cppMethodName = declarationGenerator.TypeNamer.GetName(cppMethod.AttributeType) + "_CustomAttributesCacheGenerator"; - var fnPtrType = CppFnPtrType.FromSignature(CppTypeCollection, $"void (*{cppMethodName})(CustomAttributesCache *)"); - fnPtrType.Name = cppMethodName; - fnPtrType.Group = "custom_attribute_generators"; - - var appMethod = new AppMethod(null, fnPtrType, cppMethod.VirtualAddress.Value.Start) {Group = fnPtrType.Group}; - CustomAttributeGenerators.Add(cppMethod, appMethod); - } - // This is to allow this method to be chained after a new expression return this; } diff --git a/Il2CppInspector.Common/Outputs/IDAPythonScript.cs b/Il2CppInspector.Common/Outputs/IDAPythonScript.cs index 12b213f..9f088de 100755 --- a/Il2CppInspector.Common/Outputs/IDAPythonScript.cs +++ b/Il2CppInspector.Common/Outputs/IDAPythonScript.cs @@ -104,11 +104,13 @@ typedef __int64 int64_t; writeMethods(model.GetMethodGroup("types_from_generic_methods")); writeSectionHeader("Custom attributes generators"); - writeMethods(model.CustomAttributeGenerators.Values, asRefs: true); + foreach (var method in model.ILModel.AttributesByIndices.Values) { + writeTypedName(method.VirtualAddress.Value.Start, method.Signature, method.Name); + } writeSectionHeader("Method.Invoke thunks"); foreach (var method in model.ILModel.MethodInvokers.Where(m => m != null)) { - writeTypedName(method.VirtualAddress.Start, method.ToString(), method.Name); + writeTypedName(method.VirtualAddress.Start, method.Signature, method.Name); } } @@ -117,14 +119,10 @@ typedef __int64 int64_t; writeDecls(type.ToString()); } - private void writeMethods(IEnumerable methods, bool asRefs = false) { + private void writeMethods(IEnumerable methods) { foreach (var method in methods) { - var address = asRefs? method.MethodInfoPtrAddress : method.MethodCodeAddress; - writeTypedName(address, method.CppFnPtrType.ToSignatureString(), method.CppFnPtrType.Name); - - // C++-only methods won't have an IL equivalent, eg. invokers and custom attribute generators - if (method.Method != null) - writeComment(address, method.Method); + writeTypedName(method.MethodCodeAddress, method.CppFnPtrType.ToSignatureString(), method.CppFnPtrType.Name); + writeComment(method.MethodCodeAddress, method.Method); } } diff --git a/Il2CppInspector.Common/Reflection/CustomAttributeData.cs b/Il2CppInspector.Common/Reflection/CustomAttributeData.cs index 25e8a12..46bc0e2 100644 --- a/Il2CppInspector.Common/Reflection/CustomAttributeData.cs +++ b/Il2CppInspector.Common/Reflection/CustomAttributeData.cs @@ -25,6 +25,13 @@ namespace Il2CppInspector.Reflection // The last one will be wrong but there is no way to calculate it (Model.Package.CustomAttributeGenerators[Index], Model.Package.FunctionAddresses[Model.Package.CustomAttributeGenerators[Index]]); + // C++ method names + // TODO: Known issue here where we should be using CppDeclarationGenerator.TypeNamer to ensure uniqueness + public string Name => $"{AttributeType.Name.ToCIdentifier()}_CustomAttributesCacheGenerator"; + + // C++ method signature + public string Signature => $"void {Name}(CustomAttributesCache *)"; + public override string ToString() => "[" + AttributeType.FullName + "]"; // Get all the custom attributes for a given assembly, type, member or parameter