DLL: Eliminate Il2CppInspector.DLL dependency on System.Private.CoreLib

This commit is contained in:
Katy Coe
2021-01-10 13:35:19 +01:00
parent f3c95adde6
commit ba1f69540f

View File

@@ -80,9 +80,12 @@ namespace Il2CppInspector.Outputs
// Create DLL with our custom types // Create DLL with our custom types
var module = CreateAssembly("Il2CppInspector.dll"); var module = CreateAssembly("Il2CppInspector.dll");
var attributeCtor = typeof(Attribute).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0]; // Import our IL2CPP application's copy of System.Attribute
var attributeTypeRef = module.Import(typeof(Attribute)); // to avoid introducing a dependency on System.Private.CoreLib (.NET Core) from Il2CppInspector itself
var attributeCtorRef = module.Import(attributeCtor); var attributeType = model.TypesByFullName["System.Attribute"];
var attributeCtor = attributeType.DeclaredConstructors.First(c => !c.IsPublic && !c.IsStatic);
var attributeTypeRef = GetTypeRef(module, attributeType);
var attributeCtorRef = new MemberRefUser(attributeTypeRef.Module, ".ctor", MethodSig.CreateInstance(module.CorLibTypes.Void), attributeTypeRef);
var stringField = new FieldSig(module.CorLibTypes.String); var stringField = new FieldSig(module.CorLibTypes.String);
@@ -354,6 +357,7 @@ namespace Il2CppInspector.Outputs
mMethod.Body.Variables.Add(result); mMethod.Body.Variables.Add(result);
inst.Add(OpCodes.Ldloca_S.ToInstruction(result)); inst.Add(OpCodes.Ldloca_S.ToInstruction(result));
// NOTE: This line creates a reference to an external mscorlib.dll, which we'd prefer to avoid
inst.Add(OpCodes.Initobj.ToInstruction(mMethod.ReturnType.ToTypeDefOrRef())); inst.Add(OpCodes.Initobj.ToInstruction(mMethod.ReturnType.ToTypeDefOrRef()));
inst.Add(OpCodes.Ldloc_0.ToInstruction()); inst.Add(OpCodes.Ldloc_0.ToInstruction());
inst.Add(OpCodes.Ret.ToInstruction()); inst.Add(OpCodes.Ret.ToInstruction());
@@ -480,12 +484,6 @@ namespace Il2CppInspector.Outputs
// Create folder for DLLs // Create folder for DLLs
Directory.CreateDirectory(outputPath); Directory.CreateDirectory(outputPath);
// Generate our custom types assembly
var baseDll = CreateBaseAssembly();
// Write base assembly to disk
baseDll.Write(Path.Combine(outputPath, baseDll.Name));
// Get all custom attributes with no parameters // Get all custom attributes with no parameters
// We'll add these directly to objects instead of the attribute generator function pointer // We'll add these directly to objects instead of the attribute generator function pointer
directApplyAttributes = model.TypesByDefinitionIndex directApplyAttributes = model.TypesByDefinitionIndex
@@ -493,26 +491,34 @@ namespace Il2CppInspector.Outputs
&& !t.DeclaredFields.Any() && !t.DeclaredProperties.Any()) && !t.DeclaredFields.Any() && !t.DeclaredProperties.Any())
.ToDictionary(t => t, t => (TypeDef) null); .ToDictionary(t => t, t => (TypeDef) null);
// Generate all application assemblies and types // Generate blank assemblies
// We have to do this before adding anything else so we can reference every type // We have to do this before adding anything else so we can reference every module
modules.Clear(); modules.Clear();
// Create all assemblies
foreach (var asm in model.Assemblies) { foreach (var asm in model.Assemblies) {
// Create assembly and add primary module to list // Create assembly and add primary module to list
var module = CreateAssembly(asm.ShortName); var module = CreateAssembly(asm.ShortName);
modules.Add(asm, module); modules.Add(asm, module);
}
// Generate our custom types assembly (relies on mscorlib.dll being added above)
var baseDll = CreateBaseAssembly();
// Write base assembly to disk
baseDll.Write(Path.Combine(outputPath, baseDll.Name));
// Add assembly custom attribute attributes (must do this after all assemblies are created due to type referencing)
foreach (var asm in model.Assemblies) {
var module = modules[asm];
foreach (var ca in asm.CustomAttributes)
AddCustomAttribute(module, module.Assembly, ca);
// Add token attributes // Add token attributes
module.AddAttribute(module, tokenAttribute, ("Token", $"0x{asm.ImageDefinition.token:X8}")); module.AddAttribute(module, tokenAttribute, ("Token", $"0x{asm.ImageDefinition.token:X8}"));
module.Assembly.AddAttribute(module, tokenAttribute, ("Token", $"0x{asm.MetadataToken:X8}")); module.Assembly.AddAttribute(module, tokenAttribute, ("Token", $"0x{asm.MetadataToken:X8}"));
} }
// Add assembly custom attribute attributes (must do this after all assemblies are created due to type referencing)
foreach (var asm in model.Assemblies)
foreach (var ca in asm.CustomAttributes)
AddCustomAttribute(modules[asm], modules[asm].Assembly, ca);
// Add all types // Add all types
foreach (var asm in model.Assemblies) { foreach (var asm in model.Assemblies) {
statusCallback?.Invoke(this, "Preparing " + asm.ShortName); statusCallback?.Invoke(this, "Preparing " + asm.ShortName);