From 601e781ef9b9697ec326c946e771df7a4cd893b8 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 19 Aug 2025 22:01:10 -0400 Subject: [PATCH] Data source shennanigans --- .../Deserializers/BFPK.cs | 1 + SabreTools.Serialization/Deserializers/CHD.cs | 1 + .../Wrappers/DataSource.cs | 19 +++++++++++++++++++ .../Wrappers/NewExecutable.cs | 15 ++------------- SabreTools.Serialization/Wrappers/VPK.cs | 11 ++++++----- .../Wrappers/WrapperBaseT.cs | 2 +- 6 files changed, 30 insertions(+), 19 deletions(-) diff --git a/SabreTools.Serialization/Deserializers/BFPK.cs b/SabreTools.Serialization/Deserializers/BFPK.cs index 432484d6..05f0d790 100644 --- a/SabreTools.Serialization/Deserializers/BFPK.cs +++ b/SabreTools.Serialization/Deserializers/BFPK.cs @@ -64,6 +64,7 @@ namespace SabreTools.Serialization.Deserializers /// Parse a Stream into a FileEntry /// /// Stream to parse + /// Initial offset to use in address comparisons /// Filled FileEntry on success, null on error public static FileEntry ParseFileEntry(Stream data, long initialOffset) { diff --git a/SabreTools.Serialization/Deserializers/CHD.cs b/SabreTools.Serialization/Deserializers/CHD.cs index 2db3d876..ac35c5a4 100644 --- a/SabreTools.Serialization/Deserializers/CHD.cs +++ b/SabreTools.Serialization/Deserializers/CHD.cs @@ -100,6 +100,7 @@ namespace SabreTools.Serialization.Deserializers /// /// Get the matching CHD version, if possible /// + /// Initial offset to use in address comparisons /// Matching version, 0 if none private static uint GetVersion(Stream data, long initialOffset) { diff --git a/SabreTools.Serialization/Wrappers/DataSource.cs b/SabreTools.Serialization/Wrappers/DataSource.cs index 5bdc22bc..d17fce9c 100644 --- a/SabreTools.Serialization/Wrappers/DataSource.cs +++ b/SabreTools.Serialization/Wrappers/DataSource.cs @@ -89,6 +89,25 @@ namespace SabreTools.Serialization.Wrappers }; } + /// + /// Get the filename from the source, if possible + /// + /// String representing the filename on success, null otherwise + /// This only works if the source was a + public string? GetFilename() + { + // Only streams can have a filename + if (_dataSourceType != DataSourceType.Stream) + return null; + + // Only file streams can have a filename + if (_streamData == null || _streamData is not FileStream fs) + return null; + + // Return the name + return fs.Name; + } + /// /// Get the usable length of the underlying data /// diff --git a/SabreTools.Serialization/Wrappers/NewExecutable.cs b/SabreTools.Serialization/Wrappers/NewExecutable.cs index 69de6a7c..65045cfd 100644 --- a/SabreTools.Serialization/Wrappers/NewExecutable.cs +++ b/SabreTools.Serialization/Wrappers/NewExecutable.cs @@ -53,23 +53,12 @@ namespace SabreTools.Serialization.Wrappers foreach (var entry in SegmentTable) { // Get end of segment data - long offset = _initialPosition + (entry.Offset * (1 << Header.SegmentAlignmentShiftCount)) + entry.Length; + long offset = (entry.Offset * (1 << Header.SegmentAlignmentShiftCount)) + entry.Length; // Read and find the end of the relocation data if ((entry.FlagWord & SegmentTableEntryFlag.RELOCINFO) != 0) { - Stream? dataStream = null; - if (_byteArrayData != null) - dataStream = new MemoryStream(_byteArrayData); - else if (_streamData != null) - dataStream = _streamData; - else - break; - - dataStream.Seek(offset, SeekOrigin.Begin); - var relocationData = Deserializers.NewExecutable.ParsePerSegmentData(dataStream); - - offset = dataStream.Position; + // TODO: When the model and deserializer get updated, fix this } if (offset > endOfSectionData) diff --git a/SabreTools.Serialization/Wrappers/VPK.cs b/SabreTools.Serialization/Wrappers/VPK.cs index b608bc5c..3e1e5cc0 100644 --- a/SabreTools.Serialization/Wrappers/VPK.cs +++ b/SabreTools.Serialization/Wrappers/VPK.cs @@ -29,15 +29,16 @@ namespace SabreTools.Serialization.Wrappers return _archiveFilenames; // If we don't have a source filename - if (!(_streamData is FileStream fs) || string.IsNullOrEmpty(fs.Name)) + string? sourceFilename = _dataSource.GetFilename(); + if (string.IsNullOrEmpty(sourceFilename)) return null; // If the filename is not the right format - string extension = Path.GetExtension(fs.Name).TrimStart('.'); - string? directoryName = Path.GetDirectoryName(fs.Name); + string extension = Path.GetExtension(sourceFilename).TrimStart('.'); + string? directoryName = Path.GetDirectoryName(sourceFilename); string fileName = directoryName == null - ? Path.GetFileNameWithoutExtension(fs.Name) - : Path.Combine(directoryName, Path.GetFileNameWithoutExtension(fs.Name)); + ? Path.GetFileNameWithoutExtension(sourceFilename) + : Path.Combine(directoryName, Path.GetFileNameWithoutExtension(sourceFilename)); if (fileName.Length < 3) return null; diff --git a/SabreTools.Serialization/Wrappers/WrapperBaseT.cs b/SabreTools.Serialization/Wrappers/WrapperBaseT.cs index 7fbad8b6..dff4f47b 100644 --- a/SabreTools.Serialization/Wrappers/WrapperBaseT.cs +++ b/SabreTools.Serialization/Wrappers/WrapperBaseT.cs @@ -30,7 +30,7 @@ namespace SabreTools.Serialization.Wrappers /// /// Source of the original data /// - private readonly DataSource _dataSource; + protected readonly DataSource _dataSource; #if NETCOREAPP ///