From 9bdfe33bafd8a30d633f9b6ecbb80212c73c30f3 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 1 Sep 2025 20:27:46 -0400 Subject: [PATCH] Move some Wise things around to make more sense in broader context --- .../Wrappers/NewExecutable.cs | 7 +- .../Wrappers/PortableExecutable.cs | 7 +- .../Wrappers/WiseOverlayHeader.cs | 131 ++++++++++++++-- .../Wrappers/WiseScript.cs | 142 +----------------- 4 files changed, 128 insertions(+), 159 deletions(-) diff --git a/SabreTools.Serialization/Wrappers/NewExecutable.cs b/SabreTools.Serialization/Wrappers/NewExecutable.cs index 7f9c644b..e9b92488 100644 --- a/SabreTools.Serialization/Wrappers/NewExecutable.cs +++ b/SabreTools.Serialization/Wrappers/NewExecutable.cs @@ -570,12 +570,7 @@ namespace SabreTools.Serialization.Wrappers sourceDirectory = Path.GetDirectoryName(Path.GetFullPath(Filename)); // Process the state machine - return script.ProcessStateMachine(_dataSource, - sourceDirectory, - dataStart, - outputDirectory, - header.IsPKZIP, - includeDebug); + return script.ProcessStateMachine(header, sourceDirectory, dataStart, outputDirectory, includeDebug); } #endregion diff --git a/SabreTools.Serialization/Wrappers/PortableExecutable.cs b/SabreTools.Serialization/Wrappers/PortableExecutable.cs index fe8dfe0b..9318e531 100644 --- a/SabreTools.Serialization/Wrappers/PortableExecutable.cs +++ b/SabreTools.Serialization/Wrappers/PortableExecutable.cs @@ -1509,12 +1509,7 @@ namespace SabreTools.Serialization.Wrappers sourceDirectory = Path.GetDirectoryName(Path.GetFullPath(Filename)); // Process the state machine - return script.ProcessStateMachine(_dataSource, - sourceDirectory, - dataStart, - outputDirectory, - header.IsPKZIP, - includeDebug); + return script.ProcessStateMachine(header, sourceDirectory, dataStart, outputDirectory, includeDebug); } /// diff --git a/SabreTools.Serialization/Wrappers/WiseOverlayHeader.cs b/SabreTools.Serialization/Wrappers/WiseOverlayHeader.cs index 6330e4d3..10312df0 100644 --- a/SabreTools.Serialization/Wrappers/WiseOverlayHeader.cs +++ b/SabreTools.Serialization/Wrappers/WiseOverlayHeader.cs @@ -3,6 +3,7 @@ using System.IO; using SabreTools.IO.Compression.Deflate; using SabreTools.IO.Streams; using SabreTools.Models.WiseInstaller; +using SabreTools.Models.WiseInstaller.Actions; namespace SabreTools.Serialization.Wrappers { @@ -238,52 +239,52 @@ namespace SabreTools.Serialization.Wrappers // Extract WISE0001.DLL, if it exists expected = new DeflateInfo { InputSize = WiseDllDeflatedSize, OutputSize = -1, Crc32 = 0 }; - if (InflateWrapper.ExtractFile(_dataSource, "WISE0001.DLL", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) + if (InflateWrapper.ExtractFile(_dataSource, IsPKZIP ? null : "WISE0001.DLL", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) return false; // Extract CTL3D32.DLL, if it exists expected = new DeflateInfo { InputSize = Ctl3d32DeflatedSize, OutputSize = -1, Crc32 = 0 }; - if (InflateWrapper.ExtractFile(_dataSource, "CTL3D32.DLL", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) + if (InflateWrapper.ExtractFile(_dataSource, IsPKZIP ? null : "CTL3D32.DLL", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) return false; // Extract FILE0004, if it exists expected = new DeflateInfo { InputSize = SomeData4DeflatedSize, OutputSize = -1, Crc32 = 0 }; - if (InflateWrapper.ExtractFile(_dataSource, "FILE0004", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) + if (InflateWrapper.ExtractFile(_dataSource, IsPKZIP ? null : "FILE0004", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) return false; // Extract Ocxreg32.EXE, if it exists expected = new DeflateInfo { InputSize = RegToolDeflatedSize, OutputSize = -1, Crc32 = 0 }; - if (InflateWrapper.ExtractFile(_dataSource, "Ocxreg32.EXE", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) + if (InflateWrapper.ExtractFile(_dataSource, IsPKZIP ? null : "Ocxreg32.EXE", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) return false; // Extract PROGRESS.DLL, if it exists expected = new DeflateInfo { InputSize = ProgressDllDeflatedSize, OutputSize = -1, Crc32 = 0 }; - if (InflateWrapper.ExtractFile(_dataSource, "PROGRESS.DLL", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) + if (InflateWrapper.ExtractFile(_dataSource, IsPKZIP ? null : "PROGRESS.DLL", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) return false; // Extract FILE0007, if it exists expected = new DeflateInfo { InputSize = SomeData7DeflatedSize, OutputSize = -1, Crc32 = 0 }; - if (InflateWrapper.ExtractFile(_dataSource, "FILE0007", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) + if (InflateWrapper.ExtractFile(_dataSource, IsPKZIP ? null : "FILE0007", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) return false; // Extract FILE0008, if it exists expected = new DeflateInfo { InputSize = SomeData8DeflatedSize, OutputSize = -1, Crc32 = 0 }; - if (InflateWrapper.ExtractFile(_dataSource, "FILE0008", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) + if (InflateWrapper.ExtractFile(_dataSource, IsPKZIP ? null : "FILE0008", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) return false; // Extract FILE0009, if it exists expected = new DeflateInfo { InputSize = SomeData9DeflatedSize, OutputSize = -1, Crc32 = 0 }; - if (InflateWrapper.ExtractFile(_dataSource, "FILE0009", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) + if (InflateWrapper.ExtractFile(_dataSource, IsPKZIP ? null : "FILE0009", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) return false; // Extract FILE000A, if it exists expected = new DeflateInfo { InputSize = SomeData10DeflatedSize, OutputSize = -1, Crc32 = 0 }; - if (InflateWrapper.ExtractFile(_dataSource, "FILE000A", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) + if (InflateWrapper.ExtractFile(_dataSource, IsPKZIP ? null : "FILE000A", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) return false; // Extract install script, if it exists expected = new DeflateInfo { InputSize = InstallScriptDeflatedSize, OutputSize = -1, Crc32 = 0 }; - if (InflateWrapper.ExtractFile(_dataSource, "INSTALL_SCRIPT", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) + if (InflateWrapper.ExtractFile(_dataSource, IsPKZIP ? null : "INSTALL_SCRIPT", outputDirectory, expected, IsPKZIP, includeDebug) == ExtractionStatus.FAIL) return false; // Extract FILE000{n}.DAT, if it exists @@ -295,6 +296,116 @@ namespace SabreTools.Serialization.Wrappers 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) + { + // Get expected values + var expected = new DeflateInfo + { + InputSize = obj.DeflateEnd - obj.DeflateStart, + OutputSize = obj.InflatedSize, + Crc32 = obj.Crc32, + }; + + // Perform path replacements + string filename = obj.DestinationPathname ?? $"WISE{index:X4}"; + filename = filename.Replace("%", string.Empty); + _dataSource.Seek(dataStart + obj.DeflateStart, SeekOrigin.Begin); + return InflateWrapper.ExtractFile(_dataSource, + filename, + outputDirectory, + expected, + IsPKZIP, + includeDebug); + } + + /// + /// 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) + { + // Get the generated base name + string baseName = $"CustomBillboardSet_{obj.Flags:X4}-{obj.Operand_2}-{obj.Operand_3}"; + + // If there are no deflate objects + if (obj.DeflateInfo == null) + { + if (includeDebug) Console.WriteLine($"Skipping {baseName} because the deflate object array is null!"); + return ExtractionStatus.FAIL; + } + + // Loop through the values + for (int i = 0; i < obj.DeflateInfo.Length; i++) + { + // Get the deflate info object + var info = obj.DeflateInfo[i]; + + // Get expected values + var expected = new DeflateInfo + { + InputSize = info.DeflateEnd - info.DeflateStart, + OutputSize = info.InflatedSize, + Crc32 = 0, + }; + + // Perform path replacements + string filename = $"{baseName}{index:X4}"; + _dataSource.Seek(dataStart + info.DeflateStart, SeekOrigin.Begin); + _ = InflateWrapper.ExtractFile(_dataSource, filename, outputDirectory, expected, IsPKZIP, includeDebug); + } + + // Always return good -- TODO: Fix this + return ExtractionStatus.GOOD; + } + + /// + /// 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) + { + // Get expected values + var expected = new DeflateInfo + { + InputSize = obj.DeflateEnd - obj.DeflateStart, + OutputSize = obj.InflatedSize, + Crc32 = 0, + }; + + // Perform path replacements + string filename = $"CustomDialogSet_{obj.DisplayVariable}-{obj.Name}"; + filename = filename.Replace("%", string.Empty); + _dataSource.Seek(dataStart + obj.DeflateStart, SeekOrigin.Begin); + return InflateWrapper.ExtractFile(_dataSource, filename, outputDirectory, expected, IsPKZIP, includeDebug); + } + /// /// Open a potential WISE installer file and any additional files /// diff --git a/SabreTools.Serialization/Wrappers/WiseScript.cs b/SabreTools.Serialization/Wrappers/WiseScript.cs index 66191e4b..e47709a3 100644 --- a/SabreTools.Serialization/Wrappers/WiseScript.cs +++ b/SabreTools.Serialization/Wrappers/WiseScript.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.IO; using System.Text; -using SabreTools.IO.Compression.Deflate; #if NETFRAMEWORK || NETSTANDARD using SabreTools.IO.Extensions; #endif @@ -133,19 +132,16 @@ namespace SabreTools.Serialization.Wrappers /// /// Process the state machine and perform all required actions /// - /// Stream representing the Wise installer + /// Overlay header used for reference /// Directory where installer files live, if possible - /// Parsed script to retrieve information from /// Start of the deflated data /// Output directory to write to - /// Indicates if PKZIP containers are used /// True to include debug data, false otherwise /// True if there were no errors during processing, false otherwise - public bool ProcessStateMachine(Stream data, + public bool ProcessStateMachine(WiseOverlayHeader header, string? sourceDirectory, long dataStart, string outputDirectory, - bool isPkzip, bool includeDebug) { // If the state machine is invalid @@ -168,7 +164,7 @@ namespace SabreTools.Serialization.Wrappers return false; // Try to extract to the output directory - ExtractFile(data, dataStart, fileHeader, ++normalFileCount, outputDirectory, isPkzip, includeDebug); + header.ExtractFile(dataStart, fileHeader, ++normalFileCount, outputDirectory, includeDebug); break; case OperationCode.EditIniFile: @@ -184,7 +180,7 @@ namespace SabreTools.Serialization.Wrappers return false; // Try to extract to the output directory - ExtractFile(data, dataStart, displayBillboard, ++normalFileCount, outputDirectory, isPkzip, includeDebug); + header.ExtractFile(dataStart, displayBillboard, ++normalFileCount, outputDirectory, includeDebug); break; case OperationCode.DeleteFile: @@ -300,7 +296,7 @@ namespace SabreTools.Serialization.Wrappers // Try to extract to the output directory ++normalFileCount; - ExtractFile(data, dataStart, customDialogSet, outputDirectory, isPkzip, includeDebug); + header.ExtractFile(dataStart, customDialogSet, outputDirectory, includeDebug); break; case OperationCode.GetTemporaryFilename: @@ -326,134 +322,6 @@ namespace SabreTools.Serialization.Wrappers return true; } - #endregion - - #region Extraction - - /// - /// Attempt to extract a file defined by a file header - /// - /// Stream representing the Wise installer - /// Start of the deflated data - /// Deflate information - /// File index for automatic naming - /// Output directory to write to - /// Indicates if PKZIP containers are used - /// True to include debug data, false otherwise - /// True if the file extracted successfully, false otherwise - public static ExtractionStatus ExtractFile(Stream data, - long dataStart, - InstallFile obj, - int index, - string outputDirectory, - bool isPkzip, - bool includeDebug) - { - // Get expected values - var expected = new DeflateInfo - { - InputSize = obj.DeflateEnd - obj.DeflateStart, - OutputSize = obj.InflatedSize, - Crc32 = obj.Crc32, - }; - - // Perform path replacements - string filename = obj.DestinationPathname ?? $"WISE{index:X4}"; - filename = filename.Replace("%", string.Empty); - data.Seek(dataStart + obj.DeflateStart, SeekOrigin.Begin); - return InflateWrapper.ExtractFile(data, - filename, - outputDirectory, - expected, - isPkzip, - includeDebug); - } - - /// - /// Attempt to extract a file defined by a file header - /// - /// Stream representing the Wise installer - /// Start of the deflated data - /// Deflate information - /// File index for automatic naming - /// Output directory to write to - /// Indicates if PKZIP containers are used - /// True to include debug data, false otherwise - /// True if the file extracted successfully, false otherwise - public static ExtractionStatus ExtractFile(Stream data, - long dataStart, - DisplayBillboard obj, - int index, - string outputDirectory, - bool isPkzip, - bool includeDebug) - { - // Get the generated base name - string baseName = $"CustomBillboardSet_{obj.Flags:X4}-{obj.Operand_2}-{obj.Operand_3}"; - - // If there are no deflate objects - if (obj.DeflateInfo == null) - { - if (includeDebug) Console.WriteLine($"Skipping {baseName} because the deflate object array is null!"); - return ExtractionStatus.FAIL; - } - - // Loop through the values - for (int i = 0; i < obj.DeflateInfo.Length; i++) - { - // Get the deflate info object - var info = obj.DeflateInfo[i]; - - // Get expected values - var expected = new DeflateInfo - { - InputSize = info.DeflateEnd - info.DeflateStart, - OutputSize = info.InflatedSize, - Crc32 = 0, - }; - - // Perform path replacements - string filename = $"{baseName}{index:X4}"; - data.Seek(dataStart + info.DeflateStart, SeekOrigin.Begin); - _ = InflateWrapper.ExtractFile(data, filename, outputDirectory, expected, isPkzip, includeDebug); - } - - // Always return good -- TODO: Fix this - return ExtractionStatus.GOOD; - } - - /// - /// Attempt to extract a file defined by a file header - /// - /// Stream representing the Wise installer - /// Start of the deflated data - /// Deflate information - /// Output directory to write to - /// Indicates if PKZIP containers are used - /// True to include debug data, false otherwise - /// True if the file extracted successfully, false otherwise - public static ExtractionStatus ExtractFile(Stream data, - long dataStart, - CustomDialogSet obj, - string outputDirectory, - bool isPkzip, - bool includeDebug) - { - // Get expected values - var expected = new DeflateInfo - { - InputSize = obj.DeflateEnd - obj.DeflateStart, - OutputSize = obj.InflatedSize, - Crc32 = 0, - }; - - // Perform path replacements - string filename = $"CustomDialogSet_{obj.DisplayVariable}-{obj.Name}"; - filename = filename.Replace("%", string.Empty); - data.Seek(dataStart + obj.DeflateStart, SeekOrigin.Begin); - return InflateWrapper.ExtractFile(data, filename, outputDirectory, expected, isPkzip, includeDebug); - } - /// /// Attempt to write INI data to the correct file ///