Move TreatAsFile up a library layer

This commit is contained in:
Matt Nadareski
2025-01-05 21:51:35 -05:00
parent 35f2284c6a
commit c67fe8a4d5
9 changed files with 66 additions and 65 deletions

View File

@@ -128,24 +128,24 @@ namespace SabreTools.DatItems
/// Create a specific type of DatItem to be used based on a BaseFile /// Create a specific type of DatItem to be used based on a BaseFile
/// </summary> /// </summary>
/// <param name="baseFile">BaseFile containing information to be created</param> /// <param name="baseFile">BaseFile containing information to be created</param>
/// <param name="asFiles">TreatAsFiles representing special format scanning</param> /// <param name="asFile">TreatAsFile representing special format scanning</param>
/// <returns>DatItem of the specific internal type that corresponds to the inputs</returns> /// <returns>DatItem of the specific internal type that corresponds to the inputs</returns>
public static DatItem? Create(BaseFile? baseFile, TreatAsFile asFiles = 0x00) public static DatItem? Create(BaseFile? baseFile, TreatAsFile asFile = 0x00)
{ {
return baseFile switch return baseFile switch
{ {
// Disk // Disk
#if NET20 || NET35 #if NET20 || NET35
FileTypes.CHD.CHDFile when (asFiles & TreatAsFile.CHD) == 0 => new Disk(baseFile), FileTypes.CHD.CHDFile when (asFile & TreatAsFile.CHD) == 0 => new Disk(baseFile),
#else #else
FileTypes.CHD.CHDFile when !asFiles.HasFlag(TreatAsFile.CHD) => new Disk(baseFile), FileTypes.CHD.CHDFile when !asFile.HasFlag(TreatAsFile.CHD) => new Disk(baseFile),
#endif #endif
// Media // Media
#if NET20 || NET35 #if NET20 || NET35
FileTypes.Aaru.AaruFormat when (asFiles & TreatAsFile.AaruFormat) == 0 => new Media(baseFile), FileTypes.Aaru.AaruFormat when (asFile & TreatAsFile.AaruFormat) == 0 => new Media(baseFile),
#else #else
FileTypes.Aaru.AaruFormat when !asFiles.HasFlag(TreatAsFile.AaruFormat) => new Media(baseFile), FileTypes.Aaru.AaruFormat when !asFile.HasFlag(TreatAsFile.AaruFormat) => new Media(baseFile),
#endif #endif
// Rom // Rom

View File

@@ -3,6 +3,21 @@ using SabreTools.Core;
namespace SabreTools.DatItems namespace SabreTools.DatItems
{ {
/// <summary>
/// Determines what sort of files only use external hashes
/// </summary>
/// TODO: Can FileType be used instead?
[Flags]
public enum TreatAsFile
{
CHD = 1 << 0,
Archive = 1 << 1,
AaruFormat = 1 << 2,
NonArchive = CHD | AaruFormat,
All = CHD | Archive | AaruFormat,
}
/// <summary> /// <summary>
/// Determine the chip type /// Determine the chip type
/// </summary> /// </summary>

View File

@@ -66,8 +66,8 @@ namespace SabreTools.DatTools
/// </summary> /// </summary>
/// <param name="datFile">Current DatFile object to add to</param> /// <param name="datFile">Current DatFile object to add to</param>
/// <param name="basePath">Base folder to be used in creating the DAT</param> /// <param name="basePath">Base folder to be used in creating the DAT</param>
/// <param name="asFiles">TreatAsFiles representing CHD and Archive scanning</param> /// <param name="asFile">TreatAsFile representing CHD and Archive scanning</param>
public bool PopulateFromDir(DatFile datFile, string basePath, TreatAsFile asFiles = 0x00) public bool PopulateFromDir(DatFile datFile, string basePath, TreatAsFile asFile = 0x00)
{ {
// Set the progress variables // Set the progress variables
long totalSize = 0; long totalSize = 0;
@@ -109,7 +109,7 @@ namespace SabreTools.DatTools
{ {
currentSize += new FileInfo(item).Length; currentSize += new FileInfo(item).Length;
CheckFileForHashes(datFile, item, basePath, asFiles); CheckFileForHashes(datFile, item, basePath, asFile);
logger.User(totalSize, currentSize, item); logger.User(totalSize, currentSize, item);
} }
@@ -125,7 +125,7 @@ namespace SabreTools.DatTools
logger.User(totalSize, currentSize); logger.User(totalSize, currentSize);
string? parentPath = Path.GetDirectoryName(Path.GetDirectoryName(basePath)); string? parentPath = Path.GetDirectoryName(Path.GetDirectoryName(basePath));
CheckFileForHashes(datFile, basePath, parentPath, asFiles); CheckFileForHashes(datFile, basePath, parentPath, asFile);
logger.User(totalSize, totalSize, basePath); logger.User(totalSize, totalSize, basePath);
} }
@@ -139,8 +139,8 @@ namespace SabreTools.DatTools
/// <param name="datFile">Current DatFile object to add to</param> /// <param name="datFile">Current DatFile object to add to</param>
/// <param name="item">Filename of the item to be checked</param> /// <param name="item">Filename of the item to be checked</param>
/// <param name="basePath">Base folder to be used in creating the DAT</param> /// <param name="basePath">Base folder to be used in creating the DAT</param>
/// <param name="asFiles">TreatAsFiles representing CHD and Archive scanning</param> /// <param name="asFile">TreatAsFile representing CHD and Archive scanning</param>
private void CheckFileForHashes(DatFile datFile, string item, string? basePath, TreatAsFile asFiles) private void CheckFileForHashes(DatFile datFile, string item, string? basePath, TreatAsFile asFile)
{ {
// If we're in depot mode, process it separately // If we're in depot mode, process it separately
if (CheckDepotFile(datFile, item)) if (CheckDepotFile(datFile, item))
@@ -157,9 +157,9 @@ namespace SabreTools.DatTools
// Skip if we're treating archives as files and skipping files // Skip if we're treating archives as files and skipping files
#if NET20 || NET35 #if NET20 || NET35
if ((asFiles & TreatAsFile.Archive) != 0 && _skipFileType == SkipFileType.File) if ((asFile & TreatAsFile.Archive) != 0 && _skipFileType == SkipFileType.File)
#else #else
if (asFiles.HasFlag(TreatAsFile.Archive) && _skipFileType == SkipFileType.File) if (asFile.HasFlag(TreatAsFile.Archive) && _skipFileType == SkipFileType.File)
#endif #endif
{ {
return; return;
@@ -173,9 +173,9 @@ namespace SabreTools.DatTools
// Process as archive if we're not treating archives as files // Process as archive if we're not treating archives as files
#if NET20 || NET35 #if NET20 || NET35
else if ((asFiles & TreatAsFile.Archive) == 0) else if ((asFile & TreatAsFile.Archive) == 0)
#else #else
else if (!asFiles.HasFlag(TreatAsFile.Archive)) else if (!asFile.HasFlag(TreatAsFile.Archive))
#endif #endif
{ {
var extracted = archive.GetChildren(); var extracted = archive.GetChildren();
@@ -192,7 +192,7 @@ namespace SabreTools.DatTools
// Process as file if we're treating archives as files // Process as file if we're treating archives as files
else else
{ {
ProcessFile(datFile, item, basePath, asFiles); ProcessFile(datFile, item, basePath, asFile);
} }
} }
@@ -205,7 +205,7 @@ namespace SabreTools.DatTools
// Process as file // Process as file
else else
ProcessFile(datFile, item, basePath, asFiles); ProcessFile(datFile, item, basePath, asFile);
} }
} }
@@ -401,13 +401,13 @@ namespace SabreTools.DatTools
/// <param name="datFile">Current DatFile object to add to</param> /// <param name="datFile">Current DatFile object to add to</param>
/// <param name="item">File to be added</param> /// <param name="item">File to be added</param>
/// <param name="basePath">Path the represents the parent directory</param> /// <param name="basePath">Path the represents the parent directory</param>
/// <param name="asFiles">TreatAsFiles representing CHD and Archive scanning</param> /// <param name="asFile">TreatAsFile representing CHD and Archive scanning</param>
private void ProcessFile(DatFile datFile, string item, string? basePath, TreatAsFile asFiles) private void ProcessFile(DatFile datFile, string item, string? basePath, TreatAsFile asFile)
{ {
logger.Verbose($"'{Path.GetFileName(item)}' treated like a file"); logger.Verbose($"'{Path.GetFileName(item)}' treated like a file");
var header = datFile.Header.GetStringFieldValue(Models.Metadata.Header.HeaderKey); var header = datFile.Header.GetStringFieldValue(Models.Metadata.Header.HeaderKey);
BaseFile? baseFile = FileTypeTool.GetInfo(item, header, _hashes); BaseFile? baseFile = FileTypeTool.GetInfo(item, header, _hashes);
DatItem? datItem = DatItem.Create(baseFile, asFiles); DatItem? datItem = DatItem.Create(baseFile, asFile);
if (datItem != null) if (datItem != null)
ProcessFileHelper(datFile, item, datItem, basePath, string.Empty); ProcessFileHelper(datFile, item, datItem, basePath, string.Empty);
} }

View File

@@ -188,7 +188,7 @@ namespace SabreTools.DatTools
/// <param name="delete">True if input files should be deleted, false otherwise</param> /// <param name="delete">True if input files should be deleted, false otherwise</param>
/// <param name="inverse">True if the DAT should be used as a filter instead of a template, false otherwise</param> /// <param name="inverse">True if the DAT should be used as a filter instead of a template, false otherwise</param>
/// <param name="outputFormat">Output format that files should be written to</param> /// <param name="outputFormat">Output format that files should be written to</param>
/// <param name="asFiles">TreatAsFiles representing special format scanning</param> /// <param name="asFile">TreatAsFile representing special format scanning</param>
/// <returns>True if rebuilding was a success, false otherwise</returns> /// <returns>True if rebuilding was a success, false otherwise</returns>
public static bool RebuildGeneric( public static bool RebuildGeneric(
DatFile datFile, DatFile datFile,
@@ -199,7 +199,7 @@ namespace SabreTools.DatTools
bool delete = false, bool delete = false,
bool inverse = false, bool inverse = false,
OutputFormat outputFormat = OutputFormat.Folder, OutputFormat outputFormat = OutputFormat.Folder,
TreatAsFile asFiles = 0x00) TreatAsFile asFile = 0x00)
{ {
#region Perform setup #region Perform setup
@@ -239,7 +239,7 @@ namespace SabreTools.DatTools
if (System.IO.File.Exists(input)) if (System.IO.File.Exists(input))
{ {
logger.User($"Checking file: {input}"); logger.User($"Checking file: {input}");
bool rebuilt = RebuildGenericHelper(datFile, input, outDir, quickScan, date, inverse, outputFormat, asFiles); bool rebuilt = RebuildGenericHelper(datFile, input, outDir, quickScan, date, inverse, outputFormat, asFile);
// If we are supposed to delete the file, do so // If we are supposed to delete the file, do so
if (delete && rebuilt) if (delete && rebuilt)
@@ -257,7 +257,7 @@ namespace SabreTools.DatTools
#endif #endif
{ {
logger.User($"Checking file: {file}"); logger.User($"Checking file: {file}");
bool rebuilt = RebuildGenericHelper(datFile, file, outDir, quickScan, date, inverse, outputFormat, asFiles); bool rebuilt = RebuildGenericHelper(datFile, file, outDir, quickScan, date, inverse, outputFormat, asFile);
// If we are supposed to delete the file, do so // If we are supposed to delete the file, do so
if (delete && rebuilt) if (delete && rebuilt)
@@ -283,7 +283,7 @@ namespace SabreTools.DatTools
/// <param name="date">True if the date from the DAT should be used if available, false otherwise</param> /// <param name="date">True if the date from the DAT should be used if available, false otherwise</param>
/// <param name="inverse">True if the DAT should be used as a filter instead of a template, false otherwise</param> /// <param name="inverse">True if the DAT should be used as a filter instead of a template, false otherwise</param>
/// <param name="outputFormat">Output format that files should be written to</param> /// <param name="outputFormat">Output format that files should be written to</param>
/// <param name="asFiles">TreatAsFiles representing special format scanning</param> /// <param name="asFile">TreatAsFile representing special format scanning</param>
/// <returns>True if the file was used to rebuild, false otherwise</returns> /// <returns>True if the file was used to rebuild, false otherwise</returns>
private static bool RebuildGenericHelper( private static bool RebuildGenericHelper(
DatFile datFile, DatFile datFile,
@@ -293,7 +293,7 @@ namespace SabreTools.DatTools
bool date, bool date,
bool inverse, bool inverse,
OutputFormat outputFormat, OutputFormat outputFormat,
TreatAsFile asFiles) TreatAsFile asFile)
{ {
// If we somehow have a null filename, return // If we somehow have a null filename, return
if (file == null) if (file == null)
@@ -331,15 +331,15 @@ namespace SabreTools.DatTools
if (internalFileInfo == null) if (internalFileInfo == null)
internalDatItem = null; internalDatItem = null;
#if NET20 || NET35 #if NET20 || NET35
else if (internalFileInfo is FileTypes.Aaru.AaruFormat && (asFiles & TreatAsFile.AaruFormat) == 0) else if (internalFileInfo is FileTypes.Aaru.AaruFormat && (asFile & TreatAsFile.AaruFormat) == 0)
#else #else
else if (internalFileInfo is FileTypes.Aaru.AaruFormat && !asFiles.HasFlag(TreatAsFile.AaruFormat)) else if (internalFileInfo is FileTypes.Aaru.AaruFormat && !asFile.HasFlag(TreatAsFile.AaruFormat))
#endif #endif
internalDatItem = new Media(internalFileInfo); internalDatItem = new Media(internalFileInfo);
#if NET20 || NET35 #if NET20 || NET35
else if (internalFileInfo is FileTypes.CHD.CHDFile && (asFiles & TreatAsFile.CHD) == 0) else if (internalFileInfo is FileTypes.CHD.CHDFile && (asFile & TreatAsFile.CHD) == 0)
#else #else
else if (internalFileInfo is FileTypes.CHD.CHDFile && !asFiles.HasFlag(TreatAsFile.CHD)) else if (internalFileInfo is FileTypes.CHD.CHDFile && !asFile.HasFlag(TreatAsFile.CHD))
#endif #endif
internalDatItem = new Disk(internalFileInfo); internalDatItem = new Disk(internalFileInfo);
else else

View File

@@ -1245,19 +1245,4 @@ namespace SabreTools.FileTypes
Torrent7Zip, Torrent7Zip,
TorrentRar, TorrentRar,
} }
/// <summary>
/// Determines what sort of files get externally hashed
/// </summary>
/// TODO: Can FileType be used instead?
[Flags]
public enum TreatAsFile
{
CHD = 1 << 0,
Archive = 1 << 1,
AaruFormat = 1 << 2,
NonArchive = CHD | AaruFormat,
All = CHD | Archive | AaruFormat,
}
} }

View File

@@ -1,7 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using SabreTools.Core;
using SabreTools.Core.Filter; using SabreTools.Core.Filter;
using SabreTools.Core.Tools; using SabreTools.Core.Tools;
using SabreTools.DatItems;
using SabreTools.DatFiles; using SabreTools.DatFiles;
using SabreTools.DatTools; using SabreTools.DatTools;
using SabreTools.FileTypes; using SabreTools.FileTypes;
@@ -1882,7 +1882,7 @@ Some special strings that can be used:
// Set threading flag, if necessary // Set threading flag, if necessary
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
if (features.ContainsKey(ThreadsInt32Value)) if (features.ContainsKey(ThreadsInt32Value))
Globals.MaxThreads = GetInt32(features, ThreadsInt32Value); Core.Globals.MaxThreads = GetInt32(features, ThreadsInt32Value);
#endif #endif
// Failure conditions // Failure conditions
@@ -1996,19 +1996,19 @@ Some special strings that can be used:
} }
/// <summary> /// <summary>
/// Get TreatAsFiles from feature list /// Get TreatAsFile from feature list
/// </summary> /// </summary>
protected static TreatAsFile GetTreatAsFiles(Dictionary<string, Feature?> features) protected static TreatAsFile GetTreatAsFile(Dictionary<string, Feature?> features)
{ {
TreatAsFile asFiles = 0x00; TreatAsFile asFile = 0x00;
if (GetBoolean(features, AaruFormatsAsFilesValue)) if (GetBoolean(features, AaruFormatsAsFilesValue))
asFiles |= TreatAsFile.AaruFormat; asFile |= TreatAsFile.AaruFormat;
if (GetBoolean(features, ArchivesAsFilesValue)) if (GetBoolean(features, ArchivesAsFilesValue))
asFiles |= TreatAsFile.Archive; asFile |= TreatAsFile.Archive;
if (GetBoolean(features, ChdsAsFilesValue)) if (GetBoolean(features, ChdsAsFilesValue))
asFiles |= TreatAsFile.CHD; asFile |= TreatAsFile.CHD;
return asFiles; return asFile;
} }
/// <summary> /// <summary>

View File

@@ -2,8 +2,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using SabreTools.DatFiles; using SabreTools.DatFiles;
using SabreTools.DatItems;
using SabreTools.DatTools; using SabreTools.DatTools;
using SabreTools.FileTypes;
using SabreTools.Help; using SabreTools.Help;
namespace SabreTools.Features namespace SabreTools.Features
@@ -60,7 +60,7 @@ namespace SabreTools.Features
// Get feature flags // Get feature flags
bool addBlankFiles = GetBoolean(features, AddBlankFilesValue); bool addBlankFiles = GetBoolean(features, AddBlankFilesValue);
bool addFileDates = GetBoolean(features, AddDateValue); bool addFileDates = GetBoolean(features, AddDateValue);
TreatAsFile asFiles = GetTreatAsFiles(features); TreatAsFile asFile = GetTreatAsFile(features);
bool noAutomaticDate = GetBoolean(features, NoAutomaticDateValue); bool noAutomaticDate = GetBoolean(features, NoAutomaticDateValue);
var includeInScan = GetIncludeInScan(features); var includeInScan = GetIncludeInScan(features);
var skipFileType = GetSkipFileType(features); var skipFileType = GetSkipFileType(features);
@@ -91,7 +91,7 @@ namespace SabreTools.Features
datdata.FillHeaderFromPath(basePath, noAutomaticDate); datdata.FillHeaderFromPath(basePath, noAutomaticDate);
// Now populate from the path // Now populate from the path
bool success = dfd.PopulateFromDir(datdata, basePath, asFiles); bool success = dfd.PopulateFromDir(datdata, basePath, asFile);
if (success) if (success)
{ {
// Perform additional processing steps // Perform additional processing steps

View File

@@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using SabreTools.DatFiles; using SabreTools.DatFiles;
using SabreTools.DatItems;
using SabreTools.DatTools; using SabreTools.DatTools;
using SabreTools.FileTypes; using SabreTools.FileTypes;
using SabreTools.Help; using SabreTools.Help;
@@ -59,7 +60,7 @@ namespace SabreTools.Features
return false; return false;
// Get feature flags // Get feature flags
TreatAsFile asFiles = GetTreatAsFiles(features); TreatAsFile asFile = GetTreatAsFile(features);
bool date = GetBoolean(features, AddDateValue); bool date = GetBoolean(features, AddDateValue);
bool delete = GetBoolean(features, DeleteValue); bool delete = GetBoolean(features, DeleteValue);
bool inverse = GetBoolean(features, InverseValue); bool inverse = GetBoolean(features, InverseValue);
@@ -112,7 +113,7 @@ namespace SabreTools.Features
if (inputDepot?.IsActive ?? false) if (inputDepot?.IsActive ?? false)
success = Rebuilder.RebuildDepot(datdata, Inputs, Path.Combine(OutputDir!, datdata.Header.GetStringFieldValue(DatHeader.FileNameKey)!), date, delete, inverse, outputFormat); success = Rebuilder.RebuildDepot(datdata, Inputs, Path.Combine(OutputDir!, datdata.Header.GetStringFieldValue(DatHeader.FileNameKey)!), date, delete, inverse, outputFormat);
else else
success = Rebuilder.RebuildGeneric(datdata, Inputs, Path.Combine(OutputDir!, datdata.Header.GetStringFieldValue(DatHeader.FileNameKey)!), quickScan, date, delete, inverse, outputFormat, asFiles); success = Rebuilder.RebuildGeneric(datdata, Inputs, Path.Combine(OutputDir!, datdata.Header.GetStringFieldValue(DatHeader.FileNameKey)!), quickScan, date, delete, inverse, outputFormat, asFile);
// If we have a success and we're updating the DAT, write it out // If we have a success and we're updating the DAT, write it out
if (success && updateDat) if (success && updateDat)
@@ -155,7 +156,7 @@ namespace SabreTools.Features
if (inputDepot?.IsActive ?? false) if (inputDepot?.IsActive ?? false)
success = Rebuilder.RebuildDepot(datdata, Inputs, OutputDir!, date, delete, inverse, outputFormat); success = Rebuilder.RebuildDepot(datdata, Inputs, OutputDir!, date, delete, inverse, outputFormat);
else else
success = Rebuilder.RebuildGeneric(datdata, Inputs, OutputDir!, quickScan, date, delete, inverse, outputFormat, asFiles); success = Rebuilder.RebuildGeneric(datdata, Inputs, OutputDir!, quickScan, date, delete, inverse, outputFormat, asFile);
// If we have a success and we're updating the DAT, write it out // If we have a success and we're updating the DAT, write it out
if (success && updateDat) if (success && updateDat)

View File

@@ -1,7 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using SabreTools.DatFiles; using SabreTools.DatFiles;
using SabreTools.DatItems;
using SabreTools.DatTools; using SabreTools.DatTools;
using SabreTools.FileTypes;
using SabreTools.Hashing; using SabreTools.Hashing;
using SabreTools.Help; using SabreTools.Help;
using SabreTools.IO; using SabreTools.IO;
@@ -50,7 +50,7 @@ namespace SabreTools.Features
var datfilePaths = PathTool.GetFilesOnly(datfiles); var datfilePaths = PathTool.GetFilesOnly(datfiles);
// Get feature flags // Get feature flags
TreatAsFile asFiles = GetTreatAsFiles(features); TreatAsFile asFile = GetTreatAsFile(features);
bool hashOnly = GetBoolean(features, HashOnlyValue); bool hashOnly = GetBoolean(features, HashOnlyValue);
bool quickScan = GetBoolean(features, QuickValue); bool quickScan = GetBoolean(features, QuickValue);
HashType[] hashes = quickScan ? [HashType.CRC32] : [HashType.CRC32, HashType.MD5, HashType.SHA1]; HashType[] hashes = quickScan ? [HashType.CRC32] : [HashType.CRC32, HashType.MD5, HashType.SHA1];
@@ -93,7 +93,7 @@ namespace SabreTools.Features
logger.User("Processing files:\n"); logger.User("Processing files:\n");
foreach (string input in Inputs) foreach (string input in Inputs)
{ {
dfd.PopulateFromDir(datdata, input, asFiles); dfd.PopulateFromDir(datdata, input, asFile);
} }
Verification.VerifyGeneric(datdata, hashOnly); Verification.VerifyGeneric(datdata, hashOnly);
@@ -147,7 +147,7 @@ namespace SabreTools.Features
logger.User("Processing files:\n"); logger.User("Processing files:\n");
foreach (string input in Inputs) foreach (string input in Inputs)
{ {
dfd.PopulateFromDir(datdata, input, asFiles); dfd.PopulateFromDir(datdata, input, asFile);
} }
Verification.VerifyGeneric(datdata, hashOnly); Verification.VerifyGeneric(datdata, hashOnly);