Abstract binary file format detection into FileFormatReader.Load
This commit is contained in:
@@ -7,6 +7,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
using NoisyCowStudios.Bin2Object;
|
using NoisyCowStudios.Bin2Object;
|
||||||
|
|
||||||
namespace Il2CppInspector
|
namespace Il2CppInspector
|
||||||
@@ -35,6 +37,22 @@ namespace Il2CppInspector
|
|||||||
List<U> ReadMappedObjectPointerArray<U>(uint uiAddr, int count) where U : new();
|
List<U> ReadMappedObjectPointerArray<U>(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<T> : BinaryObjectReader, IFileFormatReader where T : FileFormatReader<T>
|
internal class FileFormatReader<T> : BinaryObjectReader, IFileFormatReader where T : FileFormatReader<T>
|
||||||
{
|
{
|
||||||
public FileFormatReader(Stream stream) : base(stream) { }
|
public FileFormatReader(Stream stream) : base(stream) { }
|
||||||
|
|||||||
@@ -157,13 +157,8 @@ namespace Il2CppInspector
|
|||||||
|
|
||||||
Console.WriteLine("Detected metadata version " + metadata.Version);
|
Console.WriteLine("Detected metadata version " + metadata.Version);
|
||||||
|
|
||||||
// Load the il2cpp code file (try ELF, PE, Mach-O and Universal Binary)
|
// Load the il2cpp code file (try all available file formats)
|
||||||
var memoryStream = new MemoryStream(File.ReadAllBytes(codeFile));
|
IFileFormatReader stream = FileFormatReader.Load(new MemoryStream(File.ReadAllBytes(codeFile)));
|
||||||
IFileFormatReader stream =
|
|
||||||
(((IFileFormatReader) ElfReader.Load(memoryStream) ??
|
|
||||||
PEReader.Load(memoryStream)) ??
|
|
||||||
MachOReader.Load(memoryStream)) ??
|
|
||||||
UBReader.Load(memoryStream);
|
|
||||||
if (stream == null) {
|
if (stream == null) {
|
||||||
Console.Error.WriteLine("Unsupported executable file format");
|
Console.Error.WriteLine("Unsupported executable file format");
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user