From 47255fc3315679c24f3e6628c4dab2e099bc383c Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 2 Sep 2025 08:00:51 -0400 Subject: [PATCH] Sync NE with PE Wise extraction --- .../Wrappers/NewExecutable.cs | 98 ++++++++++--------- 1 file changed, 53 insertions(+), 45 deletions(-) diff --git a/SabreTools.Serialization/Wrappers/NewExecutable.cs b/SabreTools.Serialization/Wrappers/NewExecutable.cs index 5400b257..34ac1b1b 100644 --- a/SabreTools.Serialization/Wrappers/NewExecutable.cs +++ b/SabreTools.Serialization/Wrappers/NewExecutable.cs @@ -536,13 +536,24 @@ namespace SabreTools.Serialization.Wrappers } // Try to find the overlay header - long offset = FindWiseOverlayHeader(includeDebug); - if (offset < 0) - { - if (includeDebug) Console.Error.WriteLine("Could not find the overlay header"); - return false; - } + long offset = FindWiseOverlayHeader(); + if (offset > 0 && offset < Length) + return ExtractWiseOverlay(outputDirectory, includeDebug, source, offset); + // Everything else could not extract + return false; + } + + /// + /// Extract using Wise overlay + /// + /// Output directory to write to + /// True to include debug data, false otherwise + /// Potentially multi-part stream to read + /// Offset to the start of the overlay header + /// True if extraction succeeded, false otherwise + private bool ExtractWiseOverlay(string outputDirectory, bool includeDebug, Stream source, long offset) + { // Seek to the overlay and parse source.Seek(offset, SeekOrigin.Begin); var header = WiseOverlayHeader.Create(source); @@ -582,6 +593,42 @@ namespace SabreTools.Serialization.Wrappers #region Resources + /// + /// Find the location of a Wise overlay header, if it exists + /// + /// True to include debug data, false otherwise + /// Offset to the overlay header on success, -1 otherwise + public long FindWiseOverlayHeader() + { + // Get the overlay offset + long overlayOffset = OverlayAddress; + if (overlayOffset < 0 || overlayOffset >= Length) + return -1; + + // Attempt to get the overlay header + _dataSource.Seek(overlayOffset, SeekOrigin.Begin); + var header = WiseOverlayHeader.Create(_dataSource); + if (header != null) + return overlayOffset; + + // Align and loop to see if it can be found + _dataSource.Seek(overlayOffset, SeekOrigin.Begin); + _dataSource.AlignToBoundary(0x10); + overlayOffset = _dataSource.Position; + while (_dataSource.Position < Length) + { + _dataSource.Seek(overlayOffset, SeekOrigin.Begin); + header = WiseOverlayHeader.Create(_dataSource); + if (header != null) + return overlayOffset; + + overlayOffset += 0x10; + } + + header = null; + return -1; + } + /// /// Get a single resource entry /// @@ -694,45 +741,6 @@ namespace SabreTools.Serialization.Wrappers return offset; } - /// - /// Find the location of a Wise overlay header, if it exists - /// - /// True to include debug data, false otherwise - /// Offset to the overlay header on success, -1 otherwise - public long FindWiseOverlayHeader(bool includeDebug) - { - // Get the overlay offset - long overlayOffset = OverlayAddress; - if (overlayOffset < 0 || overlayOffset >= Length) - { - if (includeDebug) Console.Error.WriteLine("Could not parse the overlay header"); - return -1; - } - - // Attempt to get the overlay header - _dataSource.Seek(overlayOffset, SeekOrigin.Begin); - var header = WiseOverlayHeader.Create(_dataSource); - if (header != null) - return overlayOffset; - - // Align and loop to see if it can be found - _dataSource.Seek(overlayOffset, SeekOrigin.Begin); - _dataSource.AlignToBoundary(0x10); - overlayOffset = _dataSource.Position; - while (_dataSource.Position < Length) - { - _dataSource.Seek(overlayOffset, SeekOrigin.Begin); - header = WiseOverlayHeader.Create(_dataSource); - if (header != null) - return overlayOffset; - - overlayOffset += 0x10; - } - - header = null; - return -1; - } - #endregion #region Segments