GUI: Load binary file

This commit is contained in:
Katy Coe
2020-02-07 06:09:16 +01:00
parent 59043a723a
commit 8a574664f0
3 changed files with 58 additions and 8 deletions

View File

@@ -7,6 +7,7 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using Il2CppInspector; using Il2CppInspector;
using Inspector = Il2CppInspector.Il2CppInspector;
namespace Il2CppInspectorGUI namespace Il2CppInspectorGUI
{ {
@@ -15,7 +16,9 @@ namespace Il2CppInspectorGUI
/// </summary> /// </summary>
public partial class App : Application public partial class App : Application
{ {
public Metadata CurrentMetadata { get; private set; } private Metadata metadata;
public List<Inspector> Il2CppImages { get; } = new List<Inspector>();
public Exception LastException { get; private set; } public Exception LastException { get; private set; }
@@ -23,7 +26,43 @@ namespace Il2CppInspectorGUI
public Task<bool> LoadMetadataAsync(string metadataFile) => public Task<bool> LoadMetadataAsync(string metadataFile) =>
Task.Run(() => { Task.Run(() => {
try { try {
CurrentMetadata = new Metadata(new MemoryStream(File.ReadAllBytes(metadataFile))); metadata = new Metadata(new MemoryStream(File.ReadAllBytes(metadataFile)));
return true;
}
catch (Exception ex) {
LastException = ex;
return false;
}
});
public Task<bool> LoadBinaryAsync(string binaryFile) =>
Task.Run(() => {
try {
// This may throw other exceptions from the individual loaders as well
IFileFormatReader stream = FileFormatReader.Load(binaryFile);
if (stream == null) {
throw new InvalidOperationException("Could not determine the binary file format");
}
if (!stream.Images.Any()) {
throw new InvalidOperationException("Could not find any binary images in the file");
}
// Multi-image binaries may contain more than one Il2Cpp image
Il2CppImages.Clear();
foreach (var image in stream.Images) {
// Architecture-agnostic load attempt
try {
// If we can't load the IL2CPP data here, it's probably packed or obfuscated; ignore it
if (Il2CppBinary.Load(image, metadata.Version) is Il2CppBinary binary) {
Il2CppImages.Add(new Inspector(binary, metadata));
}
}
// Unsupported architecture; ignore it
catch (NotImplementedException) { }
}
if (!Il2CppImages.Any()) {
throw new InvalidOperationException("Could not auto-detect any IL2CPP binary images in the file");
}
return true; return true;
} }
catch (Exception ex) { catch (Exception ex) {

View File

@@ -71,7 +71,7 @@
<!-- Main content --> <!-- Main content -->
<Grid> <Grid>
<!-- Information about the loaded metadata and binary --> <!-- Information about the loaded metadata and binary -->
<Grid> <Grid Name="gridImageDetails">
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height ="Auto" /> <RowDefinition Height ="Auto" />
<RowDefinition Height ="Auto" /> <RowDefinition Height ="Auto" />
@@ -84,7 +84,7 @@
<ColumnDefinition Width="25*"></ColumnDefinition> <ColumnDefinition Width="25*"></ColumnDefinition>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="IL2CPP Metadata version"></Label> <Label Grid.Row="0" Grid.Column="0" Content="IL2CPP Metadata version"></Label>
<Label Name="lblMetadataVersion" Grid.Row="0" Grid.Column="1" Content="{Binding Path=Version}"></Label> <Label Grid.Row="0" Grid.Column="1" Content="{Binding Path=Version}"></Label>
</Grid> </Grid>
</Grid> </Grid>
@@ -92,6 +92,6 @@
<Rectangle Name="rectModalLightBoxBackground" Fill="Black" Opacity="0.4"></Rectangle> <Rectangle Name="rectModalLightBoxBackground" Fill="Black" Opacity="0.4"></Rectangle>
<Button Name="btnSelectMetadataFile" Style="{StaticResource LightBoxButton}" Margin="100" Click="BtnSelectMetadataFile_OnClick">Select an IL2CPP metadata file</Button> <Button Name="btnSelectMetadataFile" Style="{StaticResource LightBoxButton}" Margin="100" Click="BtnSelectMetadataFile_OnClick">Select an IL2CPP metadata file</Button>
<Button Name="btnSelectBinaryFile" Style="{StaticResource LightBoxButton}" Margin="100" Click="BtnSelectBinaryFile_OnClick" Visibility="Hidden">Select an IL2CPP binary file</Button> <Button Name="btnSelectBinaryFile" Style="{StaticResource LightBoxButton}" Margin="100" Click="BtnSelectBinaryFile_OnClick" Visibility="Hidden">Select an IL2CPP binary file</Button>
<Button Name="btnBack" VerticalAlignment="Bottom" DockPanel.Dock="Bottom" Style="{StaticResource LightBoxButton}" Margin="100,0,550,20" FontSize="18" Click="BtnBack_OnClick" Visibility="Hidden">&lt;&lt; Back</Button> <Button Name="btnBack" VerticalAlignment="Bottom" DockPanel.Dock="Bottom" Style="{StaticResource LightBoxButton}" Margin="20,0,630,20" FontSize="18" Click="BtnBack_OnClick" Visibility="Hidden">&lt;&lt; Back</Button>
</Grid> </Grid>
</Window> </Window>

View File

@@ -45,7 +45,6 @@ namespace Il2CppInspectorGUI
// Load the metadata file // Load the metadata file
if (await app.LoadMetadataAsync(openFileDialog.FileName)) { if (await app.LoadMetadataAsync(openFileDialog.FileName)) {
// Metadata loaded successfully // Metadata loaded successfully
lblMetadataVersion.DataContext = app.CurrentMetadata;
btnSelectBinaryFile.Visibility = Visibility.Visible; btnSelectBinaryFile.Visibility = Visibility.Visible;
btnBack.Visibility = Visibility.Visible; btnBack.Visibility = Visibility.Visible;
} }
@@ -75,11 +74,22 @@ namespace Il2CppInspectorGUI
if (openFileDialog.ShowDialog() == true) { if (openFileDialog.ShowDialog() == true) {
// Load the binary file // Load the binary file
if (await app.LoadBinaryAsync(openFileDialog.FileName)) {
// Binary loaded successfully
// TODO: Set DataContext
// TODO: Format, Endianness, Bits, Arch, GlobalOffset, symbol table size, relocations size, CodeReg, MetaReg
rectModalLightBoxBackground.Visibility = Visibility.Hidden;
}
else {
MessageBox.Show(this, app.LastException.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
btnSelectBinaryFile.Visibility = Visibility.Visible;
}
} }
else { else {
btnSelectBinaryFile.Visibility = Visibility.Visible; btnSelectBinaryFile.Visibility = Visibility.Visible;
btnBack.IsEnabled = true;
} }
btnBack.IsEnabled = true;
} }
/// <summary> /// <summary>
@@ -90,7 +100,8 @@ namespace Il2CppInspectorGUI
private void BtnBack_OnClick(object sender, RoutedEventArgs e) { private void BtnBack_OnClick(object sender, RoutedEventArgs e) {
var app = (App) Application.Current; var app = (App) Application.Current;
lblMetadataVersion.DataContext = null; rectModalLightBoxBackground.Visibility = Visibility.Visible;
gridImageDetails.DataContext = null;
btnSelectBinaryFile.Visibility = Visibility.Hidden; btnSelectBinaryFile.Visibility = Visibility.Hidden;
btnBack.Visibility = Visibility.Hidden; btnBack.Visibility = Visibility.Hidden;
btnSelectMetadataFile.Visibility = Visibility.Visible; btnSelectMetadataFile.Visibility = Visibility.Visible;