From 3fb3b15d8f0debd20942030bc1545e36d9fe21e5 Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Mon, 21 Oct 2019 11:07:30 +0200 Subject: [PATCH] Refactor Il2CppBinary loads --- Il2CppInspector/Il2CppBinary.cs | 14 ++++++++++++++ Il2CppInspector/Il2CppInspector.cs | 23 ++++------------------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/Il2CppInspector/Il2CppBinary.cs b/Il2CppInspector/Il2CppBinary.cs index 7af6291..f804689 100644 --- a/Il2CppInspector/Il2CppBinary.cs +++ b/Il2CppInspector/Il2CppBinary.cs @@ -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); diff --git a/Il2CppInspector/Il2CppInspector.cs b/Il2CppInspector/Il2CppInspector.cs index 0ba747a..148e5dd 100644 --- a/Il2CppInspector/Il2CppInspector.cs +++ b/Il2CppInspector/Il2CppInspector.cs @@ -167,27 +167,12 @@ namespace Il2CppInspector // Multi-image binaries may contain more than one Il2Cpp image var processors = new List(); 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;