API: Allow passing of load options to binary loaders

This commit is contained in:
Katy Coe
2020-12-11 22:44:48 +01:00
parent 95db3ad19b
commit 08c1559222
9 changed files with 59 additions and 39 deletions

View File

@@ -58,14 +58,14 @@ namespace Il2CppInspector
// ZipArchiveEntry does not support seeking so we have to close and re-open for each possible load format
var binary = binaryFiles[index].Open();
loaded = ElfReader32.Load(binary, OnStatusUpdate);
loaded = ElfReader32.Load(binary, LoadOptions, OnStatusUpdate);
binary.Close();
if (loaded != null)
return loaded;
binary = binaryFiles[index].Open();
loaded = ElfReader64.Load(binary, OnStatusUpdate);
loaded = ElfReader64.Load(binary, LoadOptions, OnStatusUpdate);
binary.Close();
return loaded;

View File

@@ -58,14 +58,14 @@ namespace Il2CppInspector
// ZipArchiveEntry does not support seeking so we have to close and re-open for each possible load format
var binary = binaryFiles[index].Open();
loaded = ElfReader32.Load(binary, OnStatusUpdate);
loaded = ElfReader32.Load(binary, LoadOptions, OnStatusUpdate);
binary.Close();
if (loaded != null)
return loaded;
binary = binaryFiles[index].Open();
loaded = ElfReader64.Load(binary, OnStatusUpdate);
loaded = ElfReader64.Load(binary, LoadOptions, OnStatusUpdate);
binary.Close();
return loaded;

View File

@@ -97,17 +97,18 @@ namespace Il2CppInspector
public class FileFormatReader
{
// Helper method to try all defined file formats when the contents of the binary is unknown
public static IFileFormatReader Load(string filename, EventHandler<string> statusCallback = null) => Load(new FileStream(filename, FileMode.Open, FileAccess.Read), statusCallback);
public static IFileFormatReader Load(string filename, LoadOptions loadOptions = null, EventHandler<string> statusCallback = null)
=> Load(new FileStream(filename, FileMode.Open, FileAccess.Read), loadOptions, statusCallback);
public static IFileFormatReader Load(Stream stream, EventHandler<string> statusCallback = null) {
public static IFileFormatReader Load(Stream stream, LoadOptions loadOptions = null, EventHandler<string> statusCallback = null) {
var types = Assembly.GetExecutingAssembly().DefinedTypes
.Where(x => x.ImplementedInterfaces.Contains(typeof(IFileFormatReader)) && !x.IsGenericTypeDefinition);
foreach (var type in types) {
try {
if (type.GetMethod("Load", BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public,
null, new[] {typeof(Stream), typeof(EventHandler<string>)}, null)
.Invoke(null, new object[] {stream, statusCallback}) is IFileFormatReader loaded)
null, new[] {typeof(Stream), typeof(LoadOptions), typeof(EventHandler<string>)}, null)
.Invoke(null, new object[] {stream, loadOptions, statusCallback}) is IFileFormatReader loaded)
return loaded;
}
catch (TargetInvocationException ex) {
@@ -142,6 +143,9 @@ namespace Il2CppInspector
public virtual int Bits => throw new NotImplementedException();
// Extra parameters to be passed to a loader
protected LoadOptions LoadOptions;
public EventHandler<string> OnStatusUpdate { get; set; }
protected void StatusUpdate(string status) => OnStatusUpdate?.Invoke(this, status);
@@ -153,12 +157,12 @@ namespace Il2CppInspector
}
}
public static T Load(string filename, EventHandler<string> statusCallback = null) {
public static T Load(string filename, LoadOptions loadOptions = null, EventHandler<string> statusCallback = null) {
using var stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
return Load(stream, statusCallback);
return Load(stream, loadOptions, statusCallback);
}
public static T Load(Stream stream, EventHandler<string> statusCallback = null) {
public static T Load(Stream stream, LoadOptions loadOptions = null, EventHandler<string> statusCallback = null) {
// Copy the original stream in case we modify it
var ms = new MemoryStream();
@@ -168,10 +172,11 @@ namespace Il2CppInspector
ms.Position = 0;
var pe = (T) Activator.CreateInstance(typeof(T), ms);
return pe.InitImpl(statusCallback) ? pe : null;
return pe.InitImpl(loadOptions, statusCallback) ? pe : null;
}
private bool InitImpl(EventHandler<string> statusCallback = null) {
private bool InitImpl(LoadOptions loadOptions, EventHandler<string> statusCallback) {
LoadOptions = loadOptions;
OnStatusUpdate = statusCallback;
return Init();
}

View File

@@ -0,0 +1,15 @@
/*
Copyright 2017-2020 Katy Coe - http://www.djkaty.com - https://github.com/djkaty
All rights reserved.
*/
namespace Il2CppInspector
{
// Modifiers for use when loading binary files
public class LoadOptions
{
// For dumped ELF files, the virtual address to which we should rebase - ignored for other file types
public ulong? ImageBase;
}
}

View File

@@ -41,7 +41,7 @@ namespace Il2CppInspector
Endianness = Endianness.Little;
using var s = new MemoryStream(ReadBytes((int) arch.Size));
return (IFileFormatReader) MachOReader32.Load(s, OnStatusUpdate) ?? MachOReader64.Load(s, OnStatusUpdate);
return (IFileFormatReader) MachOReader32.Load(s, LoadOptions, OnStatusUpdate) ?? MachOReader64.Load(s, LoadOptions, OnStatusUpdate);
}
}
}

View File

@@ -515,22 +515,22 @@ namespace Il2CppInspector
}
// Load from an AAB, IPA or one or more APK files
public static List<Il2CppInspector> LoadFromPackage(IEnumerable<string> packageFiles, bool silent = false) {
public static List<Il2CppInspector> LoadFromPackage(IEnumerable<string> packageFiles, LoadOptions loadOptions = null, bool silent = false) {
var streams = GetStreamsFromPackage(packageFiles, silent);
if (!streams.HasValue)
return null;
return LoadFromStream(streams.Value.Binary, streams.Value.Metadata, silent: silent);
return LoadFromStream(streams.Value.Binary, streams.Value.Metadata, loadOptions, silent: silent);
}
// Load from a binary file and metadata file
public static List<Il2CppInspector> LoadFromFile(string binaryFile, string metadataFile, bool silent = false)
public static List<Il2CppInspector> LoadFromFile(string binaryFile, string metadataFile, LoadOptions loadOptions = null, bool silent = false)
=> LoadFromStream(new FileStream(binaryFile, FileMode.Open, FileAccess.Read, FileShare.Read),
new MemoryStream(File.ReadAllBytes(metadataFile)),
silent: silent);
loadOptions, silent: silent);
// Load from a binary stream and metadata stream
// Must be a seekable stream otherwise we catch a System.IO.NotSupportedException
public static List<Il2CppInspector> LoadFromStream(Stream binaryStream, MemoryStream metadataStream, EventHandler<string> statusCallback = null, bool silent = false) {
public static List<Il2CppInspector> LoadFromStream(Stream binaryStream, MemoryStream metadataStream, LoadOptions loadOptions = null, EventHandler<string> statusCallback = null, bool silent = false) {
// Silent operation if requested
var stdout = Console.Out;
@@ -553,7 +553,7 @@ namespace Il2CppInspector
// Load the il2cpp code file (try all available file formats)
IFileFormatReader stream;
try {
stream = FileFormatReader.Load(binaryStream, statusCallback);
stream = FileFormatReader.Load(binaryStream, loadOptions, statusCallback);
if (stream == null)
throw new InvalidOperationException("Unsupported executable file format");