Move useful helper to a better place

This commit is contained in:
Matt Nadareski
2026-05-22 09:24:03 -04:00
parent 5642f601a3
commit 481020bd08
3 changed files with 21 additions and 21 deletions

View File

@@ -136,7 +136,7 @@ namespace SabreTools.Wrappers
try
{
long offset = initialOffset + ((dr.ExtentLocation.LittleEndian + (long)dr.ExtendedAttributeRecordLength) * blockLength);
var wrapper = GetFileWrapper(offset, filename);
var wrapper = WrapperFactory.GetFileWrapper(_dataSource, offset, filename);
if (wrapper is not null && wrapper is IPrintable printable)
{
// Print info for embedded file
@@ -160,7 +160,7 @@ namespace SabreTools.Wrappers
try
{
long offset = initialOffset + ((dr.ExtentLocation.BigEndian + dr.ExtendedAttributeRecordLength) * blockLength);
var wrapper = GetFileWrapper(offset, filename);
var wrapper = WrapperFactory.GetFileWrapper(_dataSource, offset, filename);
if (wrapper is not null && wrapper is IPrintable printable)
{
// Print info for embedded file
@@ -181,17 +181,6 @@ namespace SabreTools.Wrappers
}
}
private IWrapper? GetFileWrapper(long offset, string filename)
{
_dataSource.Seek(offset, SeekOrigin.Begin);
byte[] magic = _dataSource.PeekBytes(16);
string extension = Path.GetExtension(filename).TrimStart('.');
WrapperType ft = WrapperFactory.GetFileType(magic, extension);
return WrapperFactory.CreateWrapper(ft, _dataSource);
}
protected static void Print(StringBuilder builder, byte[] systemArea)
{
if (systemArea.Length == 0)

View File

@@ -1190,5 +1190,23 @@ namespace SabreTools.Wrappers
// We couldn't find a supported match
return WrapperType.UNKNOWN;
}
/// <summary>
/// Get a file wrapper based on an offset within a stream
/// </summary>
public static IWrapper? GetFileWrapper(Stream? data, long offset, string filename)
{
// Null streams can't create wrappers
if (data is null)
return null;
data.Seek(offset, SeekOrigin.Begin);
byte[] magic = data.PeekBytes(16);
string extension = Path.GetExtension(filename).TrimStart('.');
WrapperType ft = GetFileType(magic, extension);
return CreateWrapper(ft, data);
}
}
}

View File

@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.IO;
using System.Text;
using SabreTools.Data.Models.XDVDFS;
using SabreTools.Numerics.Extensions;
using SabreTools.Text.Extensions;
namespace SabreTools.Wrappers
@@ -81,13 +80,7 @@ namespace SabreTools.Wrappers
// Parse embedded file
try
{
_dataSource.Seek(initialOffset + (Constants.SectorSize * dr.ExtentOffset), SeekOrigin.Begin);
byte[] magic = _dataSource.PeekBytes(16);
string extension = Path.GetExtension(filename).TrimStart('.');
WrapperType ft = WrapperFactory.GetFileType(magic, extension);
var wrapper = WrapperFactory.CreateWrapper(ft, _dataSource);
IWrapper? wrapper = WrapperFactory.GetFileWrapper(_dataSource, initialOffset + (Constants.SectorSize * dr.ExtentOffset), filename);
if (wrapper is null || wrapper is not IPrintable printable)
continue;