From 7869d7af036336f5acbacbeab63452311afad1f4 Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Sat, 9 Jan 2021 22:11:06 +0100 Subject: [PATCH] DLL: Output custom attributes --- .../Outputs/AssemblyShims.cs | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/Il2CppInspector.Common/Outputs/AssemblyShims.cs b/Il2CppInspector.Common/Outputs/AssemblyShims.cs index 331ea46..74a0e69 100644 --- a/Il2CppInspector.Common/Outputs/AssemblyShims.cs +++ b/Il2CppInspector.Common/Outputs/AssemblyShims.cs @@ -35,7 +35,7 @@ namespace Il2CppInspector.Outputs return ctor; } - // Add custom attribute to type with named property arguments + // Add custom attribute to item with named property arguments // 'module' is the module that owns 'type'; type.Module may still be null when this is called public static CustomAttribute AddAttribute(this IHasCustomAttribute def, ModuleDef module, TypeDef attrTypeDef, params (string prop, object value)[] args) { var attRef = module.Import(attrTypeDef); @@ -185,6 +185,10 @@ namespace Il2CppInspector.Outputs if (type.Definition != null) mType.AddAttribute(module, tokenAttribute, ("Token", $"0x{type.Definition.token:X8}")); + // Add custom attribute attributes + foreach (var ca in type.CustomAttributes) + AddCustomAttribute(module, mType, ca); + return mType; } @@ -209,6 +213,10 @@ namespace Il2CppInspector.Outputs // Add token attribute mField.AddAttribute(module, tokenAttribute, ("Token", $"0x{field.Definition.token:X8}")); + // Add custom attribute attributes + foreach (var ca in field.CustomAttributes) + AddCustomAttribute(module, mField, ca); + mType.Fields.Add(mField); return mField; } @@ -236,6 +244,10 @@ namespace Il2CppInspector.Outputs // Add token attribute mProp.AddAttribute(module, tokenAttribute, ("Token", $"0x{prop.Definition.token:X8}")); + // Add custom attribute attributes + foreach (var ca in prop.CustomAttributes) + AddCustomAttribute(module, mProp, ca); + // Add property to type mType.Properties.Add(mProp); return mProp; @@ -252,6 +264,10 @@ namespace Il2CppInspector.Outputs // Add token attribute mEvent.AddAttribute(module, tokenAttribute, ("Token", $"0x{evt.Definition.token:X8}")); + // Add custom attribute attributes + foreach (var ca in evt.CustomAttributes) + AddCustomAttribute(module, mEvent, ca); + // Add property to type mType.Events.Add(mEvent); return mEvent; @@ -309,6 +325,11 @@ namespace Il2CppInspector.Outputs else p.AddAttribute(module, metadataOffsetAttribute, ("Offset", $"0x{param.DefaultValueMetadataAddress:X8}")); } + + // Add custom attribute attributes + foreach (var ca in param.CustomAttributes) + AddCustomAttribute(module, p, ca); + mMethod.ParamDefs.Add(p); } @@ -356,11 +377,23 @@ namespace Il2CppInspector.Outputs mMethod.AddAttribute(module, addressAttribute, args.ToArray()); } + // Add custom attribute attributes + foreach (var ca in method.CustomAttributes) + AddCustomAttribute(module, mMethod, ca); + // Add method to type mType.Methods.Add(mMethod); return mMethod; } + // Add a custom attributes attribute to an item + private CustomAttribute AddCustomAttribute(ModuleDef module, IHasCustomAttribute def, CustomAttributeData ca) + => def.AddAttribute(module, attributeAttribute, + ("Name", ca.AttributeType.Name), + ("RVA", (ca.VirtualAddress.Start - model.Package.BinaryImage.GlobalOffset).ToAddressString()), + ("Offset", string.Format("0x{0:X}", model.Package.BinaryImage.MapVATR(ca.VirtualAddress.Start))) + ); + // Generate type recursively with all nested types and add to module private TypeDefUser AddType(ModuleDef module, TypeInfo type) { var mType = CreateType(module, type); @@ -449,6 +482,10 @@ namespace Il2CppInspector.Outputs var module = CreateAssembly(asm.ShortName); modules.Add(module); + // Add custom attribute attributes + foreach (var ca in asm.CustomAttributes) + AddCustomAttribute(module, module.Assembly, ca); + // Add all types // Only references to previously-added modules will be resolved foreach (var type in asm.DefinedTypes.Where(t => !t.IsNested))