Refactor Il2CppBinary loads

This commit is contained in:
Katy Coe
2019-10-21 11:07:30 +02:00
parent 2ae0904226
commit 3fb3b15d8f
2 changed files with 18 additions and 19 deletions

View File

@@ -9,6 +9,7 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
namespace Il2CppInspector
{
@@ -44,6 +45,19 @@ namespace Il2CppInspector
Configure(Image, codeRegistration, metadataRegistration);
}
// Load and initialize a binary of any supported architecture
public static Il2CppBinary Load(IFileFormatReader stream, double metadataVersion) {
// Get type from image architecture
var type = Assembly.GetExecutingAssembly().GetType("Il2CppInspector.Il2CppBinary" + stream.Arch.ToUpper());
if (type == null)
return null;
var inst = (Il2CppBinary) Activator.CreateInstance(type, new object[] {stream});
// Try to process the IL2CPP image; return the instance if succeeded, otherwise null
return inst.Initialize(metadataVersion) ? inst : null;
}
// Architecture-specific search function
protected abstract (uint, uint) ConsiderCode(uint loc, uint globalOffset);

View File

@@ -167,27 +167,12 @@ namespace Il2CppInspector
// Multi-image binaries may contain more than one Il2Cpp image
var processors = new List<Il2CppInspector>();
foreach (var image in stream.Images) {
Il2CppBinary binary;
// We are currently supporting x86 and ARM architectures
switch (image.Arch) {
case "x86":
binary = new Il2CppBinaryX86(image);
break;
case "ARM":
binary = new Il2CppBinaryARM(image);
break;
default:
Console.Error.WriteLine("Unsupported architecture");
return null;
}
// Find code and metadata regions
if (!binary.Initialize(metadata.Version)) {
Console.Error.WriteLine("Could not process IL2CPP image");
// Architecture-agnostic load attempt
if (Il2CppBinary.Load(image, metadata.Version) is Il2CppBinary binary) {
processors.Add(new Il2CppInspector(binary, metadata));
}
else {
processors.Add(new Il2CppInspector(binary, metadata));
Console.Error.WriteLine("Could not process IL2CPP image");
}
}
return processors;