Data source shennanigans

This commit is contained in:
Matt Nadareski
2025-08-19 22:01:10 -04:00
parent d26ad35573
commit 601e781ef9
6 changed files with 30 additions and 19 deletions

View File

@@ -64,6 +64,7 @@ namespace SabreTools.Serialization.Deserializers
/// Parse a Stream into a FileEntry
/// </summary>
/// <param name="data">Stream to parse</param>
/// <param name="initialOffset">Initial offset to use in address comparisons</param>
/// <returns>Filled FileEntry on success, null on error</returns>
public static FileEntry ParseFileEntry(Stream data, long initialOffset)
{

View File

@@ -100,6 +100,7 @@ namespace SabreTools.Serialization.Deserializers
/// <summary>
/// Get the matching CHD version, if possible
/// </summary>
/// <param name="initialOffset">Initial offset to use in address comparisons</param>
/// <returns>Matching version, 0 if none</returns>
private static uint GetVersion(Stream data, long initialOffset)
{

View File

@@ -89,6 +89,25 @@ namespace SabreTools.Serialization.Wrappers
};
}
/// <summary>
/// Get the filename from the source, if possible
/// </summary>
/// <returns>String representing the filename on success, null otherwise</returns>
/// <remarks>This only works if the source was a <see cref="FileStream"/></remarks>
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;
}
/// <summary>
/// Get the usable length of the underlying data
/// </summary>

View File

@@ -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)

View File

@@ -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;

View File

@@ -30,7 +30,7 @@ namespace SabreTools.Serialization.Wrappers
/// <summary>
/// Source of the original data
/// </summary>
private readonly DataSource _dataSource;
protected readonly DataSource _dataSource;
#if NETCOREAPP
/// <summary>