From 8b6f45d9b79bc0a49cd97286d3e16c9d2f0c60d0 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 1 Sep 2025 20:37:23 -0400 Subject: [PATCH] Detangle Wise a bit more --- .../Wrappers/NewExecutable.cs | 4 +- .../Wrappers/PortableExecutable.cs | 4 +- .../Wrappers/WiseOverlayHeader.cs | 49 +++++++++---------- .../Wrappers/WiseScript.cs | 8 ++- 4 files changed, 31 insertions(+), 34 deletions(-) diff --git a/SabreTools.Serialization/Wrappers/NewExecutable.cs b/SabreTools.Serialization/Wrappers/NewExecutable.cs index e9b92488..38e89499 100644 --- a/SabreTools.Serialization/Wrappers/NewExecutable.cs +++ b/SabreTools.Serialization/Wrappers/NewExecutable.cs @@ -548,7 +548,7 @@ namespace SabreTools.Serialization.Wrappers } // Extract the header-defined files - bool extracted = header.ExtractHeaderDefinedFiles(outputDirectory, includeDebug, out long dataStart); + bool extracted = header.ExtractHeaderDefinedFiles(outputDirectory, includeDebug); if (!extracted) { if (includeDebug) Console.Error.WriteLine("Could not extract header-defined files"); @@ -570,7 +570,7 @@ namespace SabreTools.Serialization.Wrappers sourceDirectory = Path.GetDirectoryName(Path.GetFullPath(Filename)); // Process the state machine - return script.ProcessStateMachine(header, sourceDirectory, dataStart, outputDirectory, includeDebug); + return script.ProcessStateMachine(header, sourceDirectory, outputDirectory, includeDebug); } #endregion diff --git a/SabreTools.Serialization/Wrappers/PortableExecutable.cs b/SabreTools.Serialization/Wrappers/PortableExecutable.cs index 9318e531..5b62a68b 100644 --- a/SabreTools.Serialization/Wrappers/PortableExecutable.cs +++ b/SabreTools.Serialization/Wrappers/PortableExecutable.cs @@ -1487,7 +1487,7 @@ namespace SabreTools.Serialization.Wrappers } // Extract the header-defined files - bool extracted = header.ExtractHeaderDefinedFiles(outputDirectory, includeDebug, out long dataStart); + bool extracted = header.ExtractHeaderDefinedFiles(outputDirectory, includeDebug); if (!extracted) { if (includeDebug) Console.Error.WriteLine("Could not extract header-defined files"); @@ -1509,7 +1509,7 @@ namespace SabreTools.Serialization.Wrappers sourceDirectory = Path.GetDirectoryName(Path.GetFullPath(Filename)); // Process the state machine - return script.ProcessStateMachine(header, sourceDirectory, dataStart, outputDirectory, includeDebug); + return script.ProcessStateMachine(header, sourceDirectory, outputDirectory, includeDebug); } /// diff --git a/SabreTools.Serialization/Wrappers/WiseOverlayHeader.cs b/SabreTools.Serialization/Wrappers/WiseOverlayHeader.cs index 10312df0..51acfef2 100644 --- a/SabreTools.Serialization/Wrappers/WiseOverlayHeader.cs +++ b/SabreTools.Serialization/Wrappers/WiseOverlayHeader.cs @@ -74,6 +74,17 @@ namespace SabreTools.Serialization.Wrappers } } + /// + /// Installer data offset + /// + /// + /// This is the offset marking the point after all of the + /// header-defined files. It is only set during extraction + /// and is not used otherwise. It is automatically set if + /// is called. + /// + public long InstallerDataOffset { get; private set; } + /// public uint Ctl3d32DeflatedSize => Model.Ctl3d32DeflatedSize; @@ -218,15 +229,12 @@ namespace SabreTools.Serialization.Wrappers /// Output directory to write to /// True to include debug data, false otherwise /// True if the files extracted successfully, false otherwise - public bool ExtractHeaderDefinedFiles(string outputDirectory, bool includeDebug, out long dataStart) + public bool ExtractHeaderDefinedFiles(string outputDirectory, bool includeDebug) { // Seek to the compressed data offset _dataSource.Seek(CompressedDataOffset, SeekOrigin.Begin); if (includeDebug) Console.WriteLine($"Beginning of header-defined files: {CompressedDataOffset}"); - // Determine where the remaining compressed data starts - dataStart = _dataSource.Position; - // Extract WiseColors.dib, if it exists var expected = new DeflateInfo { InputSize = DibDeflatedSize, OutputSize = DibInflatedSize, Crc32 = 0 }; if (InflateWrapper.ExtractFile(_dataSource, "WiseColors.dib", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) @@ -292,24 +300,21 @@ namespace SabreTools.Serialization.Wrappers if (InflateWrapper.ExtractFile(_dataSource, IsPKZIP ? null : "FILE00XX.DAT", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) return false; - dataStart = _dataSource.Position; + InstallerDataOffset = _dataSource.Position; + return true; } /// /// Attempt to extract a file defined by a file header /// - /// Start of the deflated data /// Deflate information /// File index for automatic naming /// Output directory to write to /// True to include debug data, false otherwise /// True if the file extracted successfully, false otherwise - public ExtractionStatus ExtractFile(long dataStart, - InstallFile obj, - int index, - string outputDirectory, - bool includeDebug) + /// Requires to be set + public ExtractionStatus ExtractFile(InstallFile obj, int index, string outputDirectory, bool includeDebug) { // Get expected values var expected = new DeflateInfo @@ -322,7 +327,7 @@ namespace SabreTools.Serialization.Wrappers // Perform path replacements string filename = obj.DestinationPathname ?? $"WISE{index:X4}"; filename = filename.Replace("%", string.Empty); - _dataSource.Seek(dataStart + obj.DeflateStart, SeekOrigin.Begin); + _dataSource.Seek(InstallerDataOffset + obj.DeflateStart, SeekOrigin.Begin); return InflateWrapper.ExtractFile(_dataSource, filename, outputDirectory, @@ -334,18 +339,12 @@ namespace SabreTools.Serialization.Wrappers /// /// Attempt to extract a file defined by a file header /// - /// Start of the deflated data /// Deflate information - /// File index for automatic naming /// Output directory to write to - /// if PKZIP containers are used /// True to include debug data, false otherwise /// True if the file extracted successfully, false otherwise - public ExtractionStatus ExtractFile(long dataStart, - DisplayBillboard obj, - int index, - string outputDirectory, - bool includeDebug) + /// Requires to be set + public ExtractionStatus ExtractFile(DisplayBillboard obj, string outputDirectory, bool includeDebug) { // Get the generated base name string baseName = $"CustomBillboardSet_{obj.Flags:X4}-{obj.Operand_2}-{obj.Operand_3}"; @@ -372,8 +371,8 @@ namespace SabreTools.Serialization.Wrappers }; // Perform path replacements - string filename = $"{baseName}{index:X4}"; - _dataSource.Seek(dataStart + info.DeflateStart, SeekOrigin.Begin); + string filename = $"{baseName}{i:X4}"; + _dataSource.Seek(InstallerDataOffset + info.DeflateStart, SeekOrigin.Begin); _ = InflateWrapper.ExtractFile(_dataSource, filename, outputDirectory, expected, IsPKZIP, includeDebug); } @@ -384,12 +383,12 @@ namespace SabreTools.Serialization.Wrappers /// /// Attempt to extract a file defined by a file header /// - /// Start of the deflated data /// Deflate information /// Output directory to write to /// True to include debug data, false otherwise /// True if the file extracted successfully, false otherwise - public ExtractionStatus ExtractFile(long dataStart, CustomDialogSet obj, string outputDirectory, bool includeDebug) + /// Requires to be set + public ExtractionStatus ExtractFile(CustomDialogSet obj, string outputDirectory, bool includeDebug) { // Get expected values var expected = new DeflateInfo @@ -402,7 +401,7 @@ namespace SabreTools.Serialization.Wrappers // Perform path replacements string filename = $"CustomDialogSet_{obj.DisplayVariable}-{obj.Name}"; filename = filename.Replace("%", string.Empty); - _dataSource.Seek(dataStart + obj.DeflateStart, SeekOrigin.Begin); + _dataSource.Seek(InstallerDataOffset + obj.DeflateStart, SeekOrigin.Begin); return InflateWrapper.ExtractFile(_dataSource, filename, outputDirectory, expected, IsPKZIP, includeDebug); } diff --git a/SabreTools.Serialization/Wrappers/WiseScript.cs b/SabreTools.Serialization/Wrappers/WiseScript.cs index e47709a3..458dcba5 100644 --- a/SabreTools.Serialization/Wrappers/WiseScript.cs +++ b/SabreTools.Serialization/Wrappers/WiseScript.cs @@ -134,13 +134,11 @@ namespace SabreTools.Serialization.Wrappers /// /// Overlay header used for reference /// Directory where installer files live, if possible - /// Start of the deflated data /// Output directory to write to /// True to include debug data, false otherwise /// True if there were no errors during processing, false otherwise public bool ProcessStateMachine(WiseOverlayHeader header, string? sourceDirectory, - long dataStart, string outputDirectory, bool includeDebug) { @@ -164,7 +162,7 @@ namespace SabreTools.Serialization.Wrappers return false; // Try to extract to the output directory - header.ExtractFile(dataStart, fileHeader, ++normalFileCount, outputDirectory, includeDebug); + header.ExtractFile(fileHeader, ++normalFileCount, outputDirectory, includeDebug); break; case OperationCode.EditIniFile: @@ -180,7 +178,7 @@ namespace SabreTools.Serialization.Wrappers return false; // Try to extract to the output directory - header.ExtractFile(dataStart, displayBillboard, ++normalFileCount, outputDirectory, includeDebug); + header.ExtractFile(displayBillboard, outputDirectory, includeDebug); break; case OperationCode.DeleteFile: @@ -296,7 +294,7 @@ namespace SabreTools.Serialization.Wrappers // Try to extract to the output directory ++normalFileCount; - header.ExtractFile(dataStart, customDialogSet, outputDirectory, includeDebug); + header.ExtractFile(customDialogSet, outputDirectory, includeDebug); break; case OperationCode.GetTemporaryFilename: