Add progress callbacks to Il2CppBinary and Il2CppInspector

This commit is contained in:
Katy Coe
2020-12-06 17:33:07 +01:00
parent 440ec60404
commit 515365e9e9
8 changed files with 41 additions and 27 deletions

View File

@@ -80,17 +80,22 @@ namespace Il2CppInspector
// One assembly may contain multiple modules
public Dictionary<string, Il2CppCodeGenModule> Modules { get; private set; }
protected Il2CppBinary(IFileFormatReader stream) {
private EventHandler<string> OnStatusUpdate { get; set; }
private void StatusUpdate(string status) => OnStatusUpdate?.Invoke(this, status);
protected Il2CppBinary(IFileFormatReader stream, EventHandler<string> statusCallback = null) {
Image = stream;
OnStatusUpdate = statusCallback;
}
protected Il2CppBinary(IFileFormatReader stream, uint codeRegistration, uint metadataRegistration) {
protected Il2CppBinary(IFileFormatReader stream, uint codeRegistration, uint metadataRegistration, EventHandler<string> statusCallback = null) {
Image = stream;
OnStatusUpdate = statusCallback;
Configure(codeRegistration, metadataRegistration);
}
// Load and initialize a binary of any supported architecture
private static Il2CppBinary LoadImpl(IFileFormatReader stream) {
private static Il2CppBinary LoadImpl(IFileFormatReader stream, EventHandler<string> statusCallback) {
// Get type from image architecture
var type = Assembly.GetExecutingAssembly().GetType("Il2CppInspector.Il2CppBinary" + stream.Arch.ToUpper());
if (type == null)
@@ -102,12 +107,12 @@ namespace Il2CppInspector
stream[0].Stream.PrimitiveMappings.Add(typeof(ulong), typeof(uint));
}
return (Il2CppBinary) Activator.CreateInstance(type, stream[0]);
return (Il2CppBinary) Activator.CreateInstance(type, stream[0], statusCallback);
}
// Load binary without a global-metadata.dat available
public static Il2CppBinary Load(IFileFormatReader stream, double metadataVersion) {
var inst = LoadImpl(stream);
public static Il2CppBinary Load(IFileFormatReader stream, double metadataVersion, EventHandler<string> statusCallback = null) {
var inst = LoadImpl(stream, statusCallback);
return inst.Initialize(metadataVersion) ? inst : null;
}
@@ -116,13 +121,13 @@ namespace Il2CppInspector
// If it is specified and both symbol table and function scanning fail,
// Metadata will be used to try to find the required structures with data analysis
// If it is not specified, data analysis will not be performed
public static Il2CppBinary Load(IFileFormatReader stream, Metadata metadata) {
var inst = LoadImpl(stream);
public static Il2CppBinary Load(IFileFormatReader stream, Metadata metadata, EventHandler<string> statusCallback = null) {
var inst = LoadImpl(stream, statusCallback);
return inst.Initialize(metadata) ? inst : null;
}
// Initialize binary without a global-metadata.dat available
public bool Initialize(double metadataVersion) {
public bool Initialize(double metadataVersion, EventHandler<string> statusCallback = null) {
Image.Version = metadataVersion;
if (!((FindMetadataFromSymbols() ?? FindMetadataFromCode()) is var ptrs))
@@ -133,7 +138,7 @@ namespace Il2CppInspector
}
// Initialize binary with a global-metadata.dat available
public bool Initialize(Metadata metadata) {
public bool Initialize(Metadata metadata, EventHandler<string> statusCallback = null) {
Image.Version = metadata.Version;
if (!((FindMetadataFromSymbols() ?? FindMetadataFromCode() ?? FindMetadataFromData(metadata)) is var ptrs))

View File

@@ -497,18 +497,18 @@ namespace Il2CppInspector
var streams = GetStreamsFromPackage(packageFiles, silent);
if (!streams.HasValue)
return null;
return LoadFromStream(streams.Value.Binary, streams.Value.Metadata, silent);
return LoadFromStream(streams.Value.Binary, streams.Value.Metadata, silent: silent);
}
// Load from a binary file and metadata file
public static List<Il2CppInspector> LoadFromFile(string binaryFile, string metadataFile, bool silent = false)
=> LoadFromStream(new FileStream(binaryFile, FileMode.Open, FileAccess.Read, FileShare.Read),
new MemoryStream(File.ReadAllBytes(metadataFile)),
silent);
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, Stream metadataStream, bool silent = false) {
public static List<Il2CppInspector> LoadFromStream(Stream binaryStream, Stream metadataStream, EventHandler<string> statusCallback = null, bool silent = false) {
// Silent operation if requested
var stdout = Console.Out;
@@ -518,7 +518,7 @@ namespace Il2CppInspector
// Load the metadata file
Metadata metadata;
try {
metadata = new Metadata(metadataStream);
metadata = new Metadata(metadataStream, statusCallback);
}
catch (Exception ex) {
Console.Error.WriteLine(ex.Message);
@@ -531,7 +531,7 @@ namespace Il2CppInspector
// Load the il2cpp code file (try all available file formats)
IFileFormatReader stream;
try {
stream = FileFormatReader.Load(binaryStream);
stream = FileFormatReader.Load(binaryStream, statusCallback);
if (stream == null)
throw new InvalidOperationException("Unsupported executable file format");
@@ -553,7 +553,7 @@ namespace Il2CppInspector
// Architecture-agnostic load attempt
try {
if (Il2CppBinary.Load(image, metadata) is Il2CppBinary binary) {
if (Il2CppBinary.Load(image, metadata, statusCallback) is Il2CppBinary binary) {
Console.WriteLine("IL2CPP binary version " + image.Version);
processors.Add(new Il2CppInspector(binary, metadata));

View File

@@ -45,7 +45,7 @@ namespace Il2CppInspector
public Dictionary<int, string> Strings { get; } = new Dictionary<int, string>();
public Metadata(Stream stream) : base(stream)
public Metadata(Stream stream, EventHandler<string> statusCallback = null) : base(stream)
{
// Read magic bytes
if (ReadUInt32() != 0xFAB11BAF) {
@@ -170,6 +170,9 @@ namespace Il2CppInspector
// Only do this if we need to because it's very slow
if (stringOffsets.Except(Strings.Keys).Any()) {
Console.WriteLine("Decrypting strings...");
statusCallback?.Invoke(this, "Decrypting strings");
// Start again
Strings.Clear();
Position = Header.stringOffset;