DLL: Output custom attributes

This commit is contained in:
Katy Coe
2021-01-09 22:11:06 +01:00
parent d4db6666d9
commit 7869d7af03

View File

@@ -35,7 +35,7 @@ namespace Il2CppInspector.Outputs
return ctor; 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 // '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) { public static CustomAttribute AddAttribute(this IHasCustomAttribute def, ModuleDef module, TypeDef attrTypeDef, params (string prop, object value)[] args) {
var attRef = module.Import(attrTypeDef); var attRef = module.Import(attrTypeDef);
@@ -185,6 +185,10 @@ namespace Il2CppInspector.Outputs
if (type.Definition != null) if (type.Definition != null)
mType.AddAttribute(module, tokenAttribute, ("Token", $"0x{type.Definition.token:X8}")); 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; return mType;
} }
@@ -209,6 +213,10 @@ namespace Il2CppInspector.Outputs
// Add token attribute // Add token attribute
mField.AddAttribute(module, tokenAttribute, ("Token", $"0x{field.Definition.token:X8}")); 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); mType.Fields.Add(mField);
return mField; return mField;
} }
@@ -236,6 +244,10 @@ namespace Il2CppInspector.Outputs
// Add token attribute // Add token attribute
mProp.AddAttribute(module, tokenAttribute, ("Token", $"0x{prop.Definition.token:X8}")); 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 // Add property to type
mType.Properties.Add(mProp); mType.Properties.Add(mProp);
return mProp; return mProp;
@@ -252,6 +264,10 @@ namespace Il2CppInspector.Outputs
// Add token attribute // Add token attribute
mEvent.AddAttribute(module, tokenAttribute, ("Token", $"0x{evt.Definition.token:X8}")); 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 // Add property to type
mType.Events.Add(mEvent); mType.Events.Add(mEvent);
return mEvent; return mEvent;
@@ -309,6 +325,11 @@ namespace Il2CppInspector.Outputs
else else
p.AddAttribute(module, metadataOffsetAttribute, ("Offset", $"0x{param.DefaultValueMetadataAddress:X8}")); 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); mMethod.ParamDefs.Add(p);
} }
@@ -356,11 +377,23 @@ namespace Il2CppInspector.Outputs
mMethod.AddAttribute(module, addressAttribute, args.ToArray()); mMethod.AddAttribute(module, addressAttribute, args.ToArray());
} }
// Add custom attribute attributes
foreach (var ca in method.CustomAttributes)
AddCustomAttribute(module, mMethod, ca);
// Add method to type // Add method to type
mType.Methods.Add(mMethod); mType.Methods.Add(mMethod);
return 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 // Generate type recursively with all nested types and add to module
private TypeDefUser AddType(ModuleDef module, TypeInfo type) { private TypeDefUser AddType(ModuleDef module, TypeInfo type) {
var mType = CreateType(module, type); var mType = CreateType(module, type);
@@ -449,6 +482,10 @@ namespace Il2CppInspector.Outputs
var module = CreateAssembly(asm.ShortName); var module = CreateAssembly(asm.ShortName);
modules.Add(module); modules.Add(module);
// Add custom attribute attributes
foreach (var ca in asm.CustomAttributes)
AddCustomAttribute(module, module.Assembly, ca);
// Add all types // Add all types
// Only references to previously-added modules will be resolved // Only references to previously-added modules will be resolved
foreach (var type in asm.DefinedTypes.Where(t => !t.IsNested)) foreach (var type in asm.DefinedTypes.Where(t => !t.IsNested))