diff --git a/Il2CppInspector/FileFormatReader.cs b/Il2CppInspector/FileFormatReader.cs index 6e1d273..54f133a 100644 --- a/Il2CppInspector/FileFormatReader.cs +++ b/Il2CppInspector/FileFormatReader.cs @@ -7,6 +7,8 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; +using System.Reflection; using NoisyCowStudios.Bin2Object; namespace Il2CppInspector @@ -35,6 +37,22 @@ namespace Il2CppInspector List ReadMappedObjectPointerArray(uint uiAddr, int count) where U : new(); } + internal class FileFormatReader + { + // Helper method to try all defined file formats when the contents of the binary is unknown + public static IFileFormatReader Load(Stream stream) { + var types = Assembly.GetExecutingAssembly().DefinedTypes + .Where(x => x.ImplementedInterfaces.Contains(typeof(IFileFormatReader)) && !x.IsGenericTypeDefinition); + + foreach (var type in types) { + if (type.BaseType.GetMethod("Load", new [] {typeof(Stream)}) + .Invoke(null, new object[] { stream }) is IFileFormatReader loaded) + return loaded; + } + return null; + } + } + internal class FileFormatReader : BinaryObjectReader, IFileFormatReader where T : FileFormatReader { public FileFormatReader(Stream stream) : base(stream) { } diff --git a/Il2CppInspector/Il2CppInspector.cs b/Il2CppInspector/Il2CppInspector.cs index ba92e94..0ba747a 100644 --- a/Il2CppInspector/Il2CppInspector.cs +++ b/Il2CppInspector/Il2CppInspector.cs @@ -157,13 +157,8 @@ namespace Il2CppInspector Console.WriteLine("Detected metadata version " + metadata.Version); - // Load the il2cpp code file (try ELF, PE, Mach-O and Universal Binary) - var memoryStream = new MemoryStream(File.ReadAllBytes(codeFile)); - IFileFormatReader stream = - (((IFileFormatReader) ElfReader.Load(memoryStream) ?? - PEReader.Load(memoryStream)) ?? - MachOReader.Load(memoryStream)) ?? - UBReader.Load(memoryStream); + // Load the il2cpp code file (try all available file formats) + IFileFormatReader stream = FileFormatReader.Load(new MemoryStream(File.ReadAllBytes(codeFile))); if (stream == null) { Console.Error.WriteLine("Unsupported executable file format"); return null;