DLL: Add initial code to create custom attributes

This commit is contained in:
Katy Coe
2021-01-05 03:42:51 +01:00
parent 370d15e8d9
commit a5d0ea39e6
3 changed files with 111 additions and 0 deletions

View File

@@ -38,6 +38,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="dnlib" Version="3.3.2" />
<PackageReference Include="McMaster.NETCore.Plugins" Version="1.3.1" />
<PackageReference Include="Microsoft.NETCore.Targets" Version="3.0.0" PrivateAssets="all" />
<PackageReference Include="CxxDemangler" Version="0.2.4.11">

View File

@@ -0,0 +1,109 @@
/*
Copyright 2017-2020 Perfare - https://github.com/Perfare/Il2CppDumper
Copyright 2021 Katy Coe - http://www.djkaty.com - https://github.com/djkaty
All rights reserved.
*/
using System;
using System.IO;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
using Il2CppInspector.Reflection;
using Assembly = System.Reflection.Assembly;
using BindingFlags = System.Reflection.BindingFlags;
namespace Il2CppInspector.Outputs
{
public static class dnlibExtensions
{
// Add a default parameterless constructor that calls a specified base constructor
public static MethodDefUser AddDefaultConstructor(this TypeDefUser type, IMethod @base) {
var ctor = new MethodDefUser(".ctor", MethodSig.CreateInstance(type.Module.CorLibTypes.Void),
MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName);
var ctorBody = new CilBody();
ctorBody.Instructions.Add(OpCodes.Ldarg_0.ToInstruction());
ctorBody.Instructions.Add(OpCodes.Call.ToInstruction(@base));
ctorBody.Instructions.Add(OpCodes.Ret.ToInstruction());
type.Methods.Add(ctor);
return ctor;
}
}
// Output module to create .NET DLLs containing type definitions
public class AssemblyShims
{
private readonly TypeModel model;
// The namespace for our custom types
private const string rootNamespace = "Il2CppInspector.DLL"; // Il2CppDummyDll
public AssemblyShims(TypeModel model) => this.model = model;
// Create a new DLL assembly definition
private ModuleDefUser CreateAssembly(string name) {
var module = new ModuleDefUser(name + ".dll") { Kind = ModuleKind.Dll };
var ourVersion = Assembly.GetAssembly(typeof(Il2CppInspector)).GetName().Version;
var asm = new AssemblyDefUser(name, ourVersion);
asm.Modules.Add(module);
return module;
}
// Generate and save all DLLs
public void Write(string outputPath) {
Directory.CreateDirectory(outputPath);
// Create DLL with our custom types
var module = CreateAssembly("Il2CppInspector");
var importer = new Importer(module);
var attributeCtor = typeof(Attribute).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0];
var attributeTypeRef = importer.Import(typeof(Attribute));
var attributeCtorRef = importer.Import(attributeCtor);
var stringField = new FieldSig(module.CorLibTypes.String);
// Create a type deriving from System.Attribute and add it to the assembly
TypeDefUser createAttribute(string name) {
var attribute = new TypeDefUser(rootNamespace, name, attributeTypeRef);
attribute.Attributes = TypeAttributes.Public | TypeAttributes.BeforeFieldInit;
module.Types.Add(attribute);
return attribute;
}
// Create our custom attributes for compatibility with Il2CppDumper
var addressAttribute = createAttribute("AddressAttribute");
addressAttribute.Fields.Add(new FieldDefUser("RVA", stringField, FieldAttributes.Public));
addressAttribute.Fields.Add(new FieldDefUser("Offset", stringField, FieldAttributes.Public));
addressAttribute.Fields.Add(new FieldDefUser("VA", stringField, FieldAttributes.Public));
addressAttribute.Fields.Add(new FieldDefUser("Slot", stringField, FieldAttributes.Public));
addressAttribute.AddDefaultConstructor(attributeCtorRef);
var fieldOffsetAttribute = createAttribute("FieldOffsetAttribute");
fieldOffsetAttribute.Fields.Add(new FieldDefUser("Offset", stringField, FieldAttributes.Public));
fieldOffsetAttribute.AddDefaultConstructor(attributeCtorRef);
var attributeAttribute = createAttribute("AttributeAttribute");
attributeAttribute.Fields.Add(new FieldDefUser("Name", stringField, FieldAttributes.Public));
attributeAttribute.Fields.Add(new FieldDefUser("RVA", stringField, FieldAttributes.Public));
attributeAttribute.Fields.Add(new FieldDefUser("Offset", stringField, FieldAttributes.Public));
attributeAttribute.AddDefaultConstructor(attributeCtorRef);
var metadataOffsetAttribute = createAttribute("MetadataOffsetAttribute");
metadataOffsetAttribute.Fields.Add(new FieldDefUser("Offset", stringField, FieldAttributes.Public));
metadataOffsetAttribute.AddDefaultConstructor(attributeCtorRef);
var tokenAttribute = createAttribute("TokenAttribute");
tokenAttribute.Fields.Add(new FieldDefUser("Token", stringField, FieldAttributes.Public));
tokenAttribute.AddDefaultConstructor(attributeCtorRef);
// Write DLL to disk
module.Write(Path.Combine(outputPath, module.Name));
}
}
}

View File

@@ -707,6 +707,7 @@ This project uses:
- [Ookii.Dialogs.Wpf](https://github.com/augustoproiete/ookii-dialogs-wpf)
- [XamlAnimatedGif](https://github.com/XamlAnimatedGif/WpfAnimatedGif) by Thomas Levesque
- [.NET Core Plugins](https://github.com/natemcmaster/DotNetCorePlugins) by Nate McMaster
- [dnlib](https://github.com/0xd4d/dnlib) by 0xd4d
Thanks to the following individuals whose code and research helped me develop this tool: