From bbfd370f49c1230ae5c1f75d3e2a6c18376394f2 Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Thu, 16 Jul 2020 17:16:23 +0200 Subject: [PATCH] AppModel: Add custom attribute generators --- Il2CppInspector.Common/Model/AppModel.cs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/Il2CppInspector.Common/Model/AppModel.cs b/Il2CppInspector.Common/Model/AppModel.cs index 3a45cb6..0fa6e8d 100644 --- a/Il2CppInspector.Common/Model/AppModel.cs +++ b/Il2CppInspector.Common/Model/AppModel.cs @@ -53,6 +53,9 @@ 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 @@ -130,7 +133,7 @@ namespace Il2CppInspector.Model // Initialize ordered type list for code output DependencyOrderedCppTypes = new List(); - // Add method definitions to C++ type model + // Add method definitions and types used by them to C++ type model Group = "types_from_methods"; foreach (var method in ILModel.MethodsByDefinitionIndex.Where(m => m.VirtualAddress.HasValue)) { @@ -141,7 +144,7 @@ namespace Il2CppInspector.Model Methods.Add(method, fnPtr, new AppMethod(method, fnPtr) {Group = Group}); } - // Add generic methods to C++ type model + // Add generic methods definitions and types used by them to C++ type model Group = "types_from_generic_methods"; foreach (var method in ILModel.GenericMethods.Values.Where(m => m.VirtualAddress.HasValue)) { @@ -152,7 +155,7 @@ namespace Il2CppInspector.Model Methods.Add(method, fnPtr, new AppMethod(method, fnPtr) {Group = Group}); } - // Add metadata usage types to C++ type model + // Add types from metadata usage list to C++ type model // Not supported in il2cpp <19 Group = "types_from_usages"; @@ -208,6 +211,7 @@ namespace Il2CppInspector.Model } } + // Add string literals for metadata <19 to the model else { /* Version < 19 calls `il2cpp_codegen_string_literal_from_index` to get string literals. * Unfortunately, metadata references are just loose globals in Il2CppMetadataUsage.cpp @@ -218,6 +222,20 @@ namespace Il2CppInspector.Model } } + // Add custom attribute generators to the model + foreach (var method in ILModel.AttributesByIndices.Values.Where(m => m.VirtualAddress.HasValue)) { + var cppTypeName = declarationGenerator.TypeNamer.GetName(method.AttributeType); + var fnPtrType = CppFnPtrType.FromSignature(CppTypeCollection, $"void (*{cppTypeName}_CustomAttributesCacheGenerator)(CustomAttributesCache *)"); + fnPtrType.Group = "custom_attribute_generators"; + + // Get first constructor for attribute + // This is not strictly what the cache generator C++ function is but it'll do + var ctor = method.AttributeType.DeclaredConstructors.First(); + + var appMethod = new AppMethod(ctor, fnPtrType, method.VirtualAddress.Value.Start); + CustomAttributeGenerators.Add(method, appMethod); + } + // This is to allow this method to be chained after a new expression return this; }