DLL: Add initial code to create custom attributes
This commit is contained in:
109
Il2CppInspector.Common/Outputs/AssemblyShims.cs
Normal file
109
Il2CppInspector.Common/Outputs/AssemblyShims.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user