From f6eed36284f40933d5c42391d3356b05a30993bf Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Wed, 9 Dec 2020 19:43:29 +0100 Subject: [PATCH] IL2CPP: Add APIs to save processed metadata and binary to disk --- .../FileFormatReaders/ElfReader.cs | 2 ++ .../FileFormatReaders/FileFormatReader.cs | 3 +++ Il2CppInspector.Common/IL2CPP/Il2CppBinary.cs | 12 +++++++++++- Il2CppInspector.Common/IL2CPP/Il2CppInspector.cs | 4 ++++ Il2CppInspector.Common/IL2CPP/Metadata.cs | 7 +++++++ Il2CppInspector.Common/IL2CPP/ReconstructMetadata.cs | 2 +- 6 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Il2CppInspector.Common/FileFormatReaders/ElfReader.cs b/Il2CppInspector.Common/FileFormatReaders/ElfReader.cs index 6922a15..c68f992 100644 --- a/Il2CppInspector.Common/FileFormatReaders/ElfReader.cs +++ b/Il2CppInspector.Common/FileFormatReaders/ElfReader.cs @@ -364,6 +364,8 @@ namespace Il2CppInspector xorSection(".text", xorKey, stripeSize); xorSection(".rodata", xorKey, stripeSize); + + IsModified = true; } } diff --git a/Il2CppInspector.Common/FileFormatReaders/FileFormatReader.cs b/Il2CppInspector.Common/FileFormatReaders/FileFormatReader.cs index 3cdb217..2dd7baa 100644 --- a/Il2CppInspector.Common/FileFormatReaders/FileFormatReader.cs +++ b/Il2CppInspector.Common/FileFormatReaders/FileFormatReader.cs @@ -21,6 +21,7 @@ namespace Il2CppInspector long Length { get; } uint NumImages { get; } string DefaultFilename { get; } + bool IsModified { get; } IEnumerable Images { get; } IFileFormatReader this[uint index] { get; } long Position { get; set; } @@ -125,6 +126,8 @@ namespace Il2CppInspector public abstract string DefaultFilename { get; } + public bool IsModified { get; protected set; } = false; + public long Length => BaseStream.Length; public uint NumImages { get; protected set; } = 1; diff --git a/Il2CppInspector.Common/IL2CPP/Il2CppBinary.cs b/Il2CppInspector.Common/IL2CPP/Il2CppBinary.cs index 8b1a46c..6d37cd5 100644 --- a/Il2CppInspector.Common/IL2CPP/Il2CppBinary.cs +++ b/Il2CppInspector.Common/IL2CPP/Il2CppBinary.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; @@ -85,7 +86,8 @@ namespace Il2CppInspector private void StatusUpdate(string status) => OnStatusUpdate?.Invoke(this, status); // Set if something in the binary has been modified / decrypted - public bool IsModified { get; private set; } = false; + private bool isModified = false; + public bool IsModified => Image.IsModified || isModified; protected Il2CppBinary(IFileFormatReader stream, EventHandler statusCallback = null) { Image = stream; @@ -130,6 +132,14 @@ namespace Il2CppInspector return inst.Initialize(metadata) ? inst : null; } + // Save binary to file, overwriting if necessary + // Save metadata to file, overwriting if necessary + public void SaveToFile(string pathname) { + Image.Position = 0; + using (var outFile = new FileStream(pathname, FileMode.Create, FileAccess.Write)) + Image.Stream.BaseStream.CopyTo(outFile); + } + // Initialize binary without a global-metadata.dat available public bool Initialize(double metadataVersion, EventHandler statusCallback = null) { Image.Version = metadataVersion; diff --git a/Il2CppInspector.Common/IL2CPP/Il2CppInspector.cs b/Il2CppInspector.Common/IL2CPP/Il2CppInspector.cs index 726179d..f5d0488 100644 --- a/Il2CppInspector.Common/IL2CPP/Il2CppInspector.cs +++ b/Il2CppInspector.Common/IL2CPP/Il2CppInspector.cs @@ -594,6 +594,10 @@ namespace Il2CppInspector Console.SetOut(stdout); return processors; } + + // Savers + public void SaveMetadataToFile(string pathname) => Metadata.SaveToFile(pathname); + public void SaveBinaryToFile(string pathname) => Binary.SaveToFile(pathname); #endregion } } diff --git a/Il2CppInspector.Common/IL2CPP/Metadata.cs b/Il2CppInspector.Common/IL2CPP/Metadata.cs index bd33d52..dc15504 100644 --- a/Il2CppInspector.Common/IL2CPP/Metadata.cs +++ b/Il2CppInspector.Common/IL2CPP/Metadata.cs @@ -203,6 +203,13 @@ namespace Il2CppInspector StringLiterals[i] = ReadFixedLengthString(Header.stringLiteralDataOffset + stringLiteralList[i].dataIndex, stringLiteralList[i].length); } + // Save metadata to file, overwriting if necessary + public void SaveToFile(string pathname) { + Position = 0; + using (var outFile = new FileStream(pathname, FileMode.Create, FileAccess.Write)) + BaseStream.CopyTo(outFile); + } + internal int Sizeof(Type type) => Sizeof(type, Version); public static int Sizeof(Type type, double metadataVersion, int longSizeBytes = 8) { diff --git a/Il2CppInspector.Common/IL2CPP/ReconstructMetadata.cs b/Il2CppInspector.Common/IL2CPP/ReconstructMetadata.cs index 3723571..defb73e 100644 --- a/Il2CppInspector.Common/IL2CPP/ReconstructMetadata.cs +++ b/Il2CppInspector.Common/IL2CPP/ReconstructMetadata.cs @@ -320,7 +320,7 @@ namespace Il2CppInspector CodeRegistration.interopDataCount = 0; // TODO: Write changes to stream - IsModified = true; + isModified = true; // Things we need from Il2CppMetadataRegistration