diff --git a/RombaSharp/Features/Archive.cs b/RombaSharp/Features/Archive.cs index 9bbc6173..40fdc6e4 100644 --- a/RombaSharp/Features/Archive.cs +++ b/RombaSharp/Features/Archive.cs @@ -59,6 +59,7 @@ have a current entry in the DAT index."; bool noDb = GetBoolean(features, NoDbValue); bool onlyNeeded = GetBoolean(features, OnlyNeededValue); HashType[] hashes = [HashType.CRC32, HashType.MD5, HashType.SHA1]; + var dfd = new DatFromDir(hashes, SkipFileType.None, addBlanks: false); // First we want to get just all directories from the inputs List onlyDirs = []; @@ -72,8 +73,8 @@ have a current entry in the DAT index."; DatFile df = DatFile.Create(); foreach (string dir in onlyDirs) { - DatFromDir.PopulateFromDir(df, dir, asFiles: TreatAsFile.NonArchive, hashes: hashes); - DatFromDir.PopulateFromDir(df, dir, asFiles: TreatAsFile.All, hashes: hashes); + dfd.PopulateFromDir(df, dir, TreatAsFile.NonArchive); + dfd.PopulateFromDir(df, dir, TreatAsFile.All); } // Create an empty Dat for files that need to be rebuilt diff --git a/RombaSharp/Features/Dir2Dat.cs b/RombaSharp/Features/Dir2Dat.cs index e179f15c..9baee271 100644 --- a/RombaSharp/Features/Dir2Dat.cs +++ b/RombaSharp/Features/Dir2Dat.cs @@ -42,6 +42,7 @@ namespace RombaSharp.Features string? source = GetString(features, SourceStringValue); string? outdat = GetString(features, OutStringValue); HashType[] hashes = [HashType.CRC32, HashType.MD5, HashType.SHA1]; + var dfd = new DatFromDir(hashes, SkipFileType.None, addBlanks: false); // Ensure the output directory outdat = outdat.Ensure(create: true); @@ -57,7 +58,7 @@ namespace RombaSharp.Features DatFile datfile = DatFile.Create(); datfile.Header.SetFieldValue(SabreTools.Models.Metadata.Header.NameKey, string.IsNullOrWhiteSpace(name) ? "untitled" : name); datfile.Header.SetFieldValue(SabreTools.Models.Metadata.Header.DescriptionKey, description); - DatFromDir.PopulateFromDir(datfile, source, asFiles: TreatAsFile.NonArchive, hashes: hashes); + dfd.PopulateFromDir(datfile, source, TreatAsFile.NonArchive); Writer.Write(datfile, outdat!); return true; } diff --git a/RombaSharp/Features/RefreshDats.cs b/RombaSharp/Features/RefreshDats.cs index 8b9f060e..ef8402b5 100644 --- a/RombaSharp/Features/RefreshDats.cs +++ b/RombaSharp/Features/RefreshDats.cs @@ -45,6 +45,7 @@ contents of any changed dats."; int workers = GetInt32(features, WorkersInt32Value); string? missingSha1s = GetString(features, MissingSha1sStringValue); HashType[] hashes = [HashType.CRC32, HashType.MD5, HashType.SHA1]; + var dfd = new DatFromDir(hashes, SkipFileType.None, addBlanks: false); // Make sure the db is set if (string.IsNullOrWhiteSpace(_db)) @@ -70,7 +71,7 @@ contents of any changed dats."; // First get a list of SHA-1's from the input DATs DatFile datroot = DatFile.Create(); datroot.Header.SetFieldValue(SabreTools.Models.Metadata.Header.TypeKey, "SuperDAT"); - DatFromDir.PopulateFromDir(datroot, _dats, asFiles: TreatAsFile.NonArchive, hashes: hashes); + dfd.PopulateFromDir(datroot, _dats, TreatAsFile.NonArchive); datroot.Items.BucketBy(ItemKey.SHA1, DedupeType.None); // Create a List of dat hashes in the database (SHA-1) diff --git a/RombaSharp/Features/RescanDepots.cs b/RombaSharp/Features/RescanDepots.cs index 9cd242fe..fc779c75 100644 --- a/RombaSharp/Features/RescanDepots.cs +++ b/RombaSharp/Features/RescanDepots.cs @@ -36,6 +36,7 @@ namespace RombaSharp.Features return false; HashType[] hashes = [HashType.CRC32, HashType.MD5, HashType.SHA1]; + var dfd = new DatFromDir(hashes, SkipFileType.None, addBlanks: false); logger.Error("This feature is not yet implemented: rescan-depots"); @@ -74,7 +75,7 @@ namespace RombaSharp.Features // Now rescan the depot itself DatFile depot = DatFile.Create(); - DatFromDir.PopulateFromDir(depot, depotname, asFiles: TreatAsFile.NonArchive, hashes: hashes); + dfd.PopulateFromDir(depot, depotname, TreatAsFile.NonArchive); depot.Items.BucketBy(ItemKey.SHA1, DedupeType.None); // Set the base queries to use diff --git a/SabreTools.DatTools/DatFromDir.cs b/SabreTools.DatTools/DatFromDir.cs index e59d7032..11f0aa0b 100644 --- a/SabreTools.DatTools/DatFromDir.cs +++ b/SabreTools.DatTools/DatFromDir.cs @@ -4,9 +4,6 @@ using System.Threading; #if NET40_OR_GREATER || NETCOREAPP using System.Threading.Tasks; #endif -#if NET452_OR_GREATER || NETCOREAPP -using SabreTools.Core; -#endif using SabreTools.Core.Tools; using SabreTools.DatFiles; using SabreTools.DatItems; @@ -25,6 +22,25 @@ namespace SabreTools.DatTools /// public class DatFromDir { + #region Fields + + /// + /// Hashes to include in the information + /// + private readonly HashType[] _hashes; + + /// + /// Type of files that should be skipped + /// + private readonly SkipFileType _skipFileType; + + /// + /// Indicates if blank items should be created for empty folders + /// + private readonly bool _addBlanks; + + #endregion + #region Logging /// @@ -34,26 +50,25 @@ namespace SabreTools.DatTools #endregion + #region Constructors + + public DatFromDir(HashType[]? hashes, SkipFileType skipFileType, bool addBlanks) + { + _hashes = hashes ?? [HashType.CRC32, HashType.MD5, HashType.SHA1]; + _skipFileType = skipFileType; + _addBlanks = addBlanks; + } + + #endregion + /// /// Create a new Dat from a directory /// /// Current DatFile object to add to /// Base folder to be used in creating the DAT /// TreatAsFiles representing CHD and Archive scanning - /// Type of files that should be skipped - /// True if blank items should be created for empty folders, false otherwise - /// Hashes to include in the information - public static bool PopulateFromDir( - DatFile datFile, - string basePath, - TreatAsFile asFiles = 0x00, - SkipFileType skipFileType = SkipFileType.None, - bool addBlanks = false, - HashType[]? hashes = null) + public bool PopulateFromDir(DatFile datFile, string basePath, TreatAsFile asFiles = 0x00) { - // If no hashes are set, use the standard array - hashes ??= [HashType.CRC32, HashType.MD5, HashType.SHA1]; - // Set the progress variables long totalSize = 0; long currentSize = 0; @@ -74,7 +89,7 @@ namespace SabreTools.DatTools // Loop through and add the file sizes #if NET452_OR_GREATER || NETCOREAPP - Parallel.ForEach(files, Globals.ParallelOptions, item => + Parallel.ForEach(files, Core.Globals.ParallelOptions, item => #elif NET40_OR_GREATER Parallel.ForEach(files, item => #else @@ -94,12 +109,12 @@ namespace SabreTools.DatTools { currentSize += new FileInfo(item).Length; - CheckFileForHashes(datFile, item, basePath, asFiles, skipFileType, addBlanks, hashes); + CheckFileForHashes(datFile, item, basePath, asFiles); logger.User(totalSize, currentSize, item); } // Now find all folders that are empty, if we are supposed to - if (addBlanks) + if (_addBlanks) ProcessDirectoryBlanks(datFile, basePath); } else if (System.IO.File.Exists(basePath)) @@ -110,7 +125,7 @@ namespace SabreTools.DatTools logger.User(totalSize, currentSize); string? parentPath = Path.GetDirectoryName(Path.GetDirectoryName(basePath)); - CheckFileForHashes(datFile, basePath, parentPath, asFiles, skipFileType, addBlanks, hashes); + CheckFileForHashes(datFile, basePath, parentPath, asFiles); logger.User(totalSize, totalSize, basePath); } @@ -125,17 +140,7 @@ namespace SabreTools.DatTools /// Filename of the item to be checked /// Base folder to be used in creating the DAT /// TreatAsFiles representing CHD and Archive scanning - /// Type of files that should be skipped - /// True if blank items should be created for empty folders, false otherwise - /// Hashes to include in the information - private static void CheckFileForHashes( - DatFile datFile, - string item, - string? basePath, - TreatAsFile asFiles, - SkipFileType skipFileType, - bool addBlanks, - HashType[] hashes) + private void CheckFileForHashes(DatFile datFile, string item, string? basePath, TreatAsFile asFiles) { // If we're in depot mode, process it separately if (CheckDepotFile(datFile, item)) @@ -148,20 +153,20 @@ namespace SabreTools.DatTools if (archive != null) { // Set the archive flags - archive.AvailableHashTypes = hashes; + archive.AvailableHashTypes = _hashes; // Skip if we're treating archives as files and skipping files #if NETFRAMEWORK - if ((asFiles & TreatAsFile.Archive) != 0 && skipFileType == SkipFileType.File) + if ((asFiles & TreatAsFile.Archive) != 0 && _skipFileType == SkipFileType.File) #else - if (asFiles.HasFlag(TreatAsFile.Archive) && skipFileType == SkipFileType.File) + if (asFiles.HasFlag(TreatAsFile.Archive) && _skipFileType == SkipFileType.File) #endif { return; } // Skip if we're skipping archives - else if (skipFileType == SkipFileType.Archive) + else if (_skipFileType == SkipFileType.Archive) { return; } @@ -180,14 +185,14 @@ namespace SabreTools.DatTools ProcessArchive(datFile, item, basePath, extracted); // Now find all folders that are empty, if we are supposed to - if (addBlanks) + if (_addBlanks) ProcessArchiveBlanks(datFile, item, basePath, archive); } // Process as file if we're treating archives as files else { - ProcessFile(datFile, item, basePath, hashes, asFiles); + ProcessFile(datFile, item, basePath, asFiles); } } @@ -195,12 +200,12 @@ namespace SabreTools.DatTools else { // Skip if we're skipping files - if (skipFileType == SkipFileType.File) + if (_skipFileType == SkipFileType.File) return; // Process as file else - ProcessFile(datFile, item, basePath, hashes, asFiles); + ProcessFile(datFile, item, basePath, asFiles); } } @@ -251,7 +256,7 @@ namespace SabreTools.DatTools // First take care of the found items #if NET452_OR_GREATER || NETCOREAPP - Parallel.ForEach(extracted, Globals.ParallelOptions, baseFile => + Parallel.ForEach(extracted, Core.Globals.ParallelOptions, baseFile => #elif NET40_OR_GREATER Parallel.ForEach(extracted, baseFile => #else @@ -294,7 +299,7 @@ namespace SabreTools.DatTools // Add add all of the found empties to the DAT #if NET452_OR_GREATER || NETCOREAPP - Parallel.ForEach(empties, Globals.ParallelOptions, empty => + Parallel.ForEach(empties, Core.Globals.ParallelOptions, empty => #elif NET40_OR_GREATER Parallel.ForEach(empties, empty => #else @@ -329,7 +334,7 @@ namespace SabreTools.DatTools List empties = basePath.ListEmpty() ?? []; #if NET452_OR_GREATER || NETCOREAPP - Parallel.ForEach(empties, Globals.ParallelOptions, dir => + Parallel.ForEach(empties, Core.Globals.ParallelOptions, dir => #elif NET40_OR_GREATER Parallel.ForEach(empties, dir => #else @@ -396,12 +401,12 @@ namespace SabreTools.DatTools /// Current DatFile object to add to /// File to be added /// Path the represents the parent directory - /// Hashes to include in the information /// TreatAsFiles representing CHD and Archive scanning - private static void ProcessFile(DatFile datFile, string item, string? basePath, HashType[] hashes, TreatAsFile asFiles) + private void ProcessFile(DatFile datFile, string item, string? basePath, TreatAsFile asFiles) { logger.Verbose($"'{Path.GetFileName(item)}' treated like a file"); - BaseFile? baseFile = BaseFile.GetInfo(item, header: datFile.Header.GetStringFieldValue(Models.Metadata.Header.HeaderKey), hashes: hashes, asFiles: asFiles); + var header = datFile.Header.GetStringFieldValue(Models.Metadata.Header.HeaderKey); + BaseFile? baseFile = BaseFile.GetInfo(item, header, _hashes, asFiles); DatItem? datItem = DatItem.Create(baseFile); if (datItem != null) ProcessFileHelper(datFile, item, datItem, basePath, string.Empty); diff --git a/SabreTools/Features/Batch.cs b/SabreTools/Features/Batch.cs index 6524d65a..e7c93aee 100644 --- a/SabreTools/Features/Batch.cs +++ b/SabreTools/Features/Batch.cs @@ -279,11 +279,12 @@ Reset the internal state: reset();"; public override void Process(BatchState batchState) { HashType[] hashes = [HashType.CRC32, HashType.MD5, HashType.SHA1]; + var dfd = new DatTools.DatFromDir(hashes, SkipFileType.None, addBlanks: false); // Assume there could be multiple foreach (string input in Arguments) { - DatTools.DatFromDir.PopulateFromDir(batchState.DatFile, input, hashes: hashes); + dfd.PopulateFromDir(batchState.DatFile, input); } // TODO: We might not want to remove dates in the future diff --git a/SabreTools/Features/DatFromDir.cs b/SabreTools/Features/DatFromDir.cs index 5eed43ba..76b561c5 100644 --- a/SabreTools/Features/DatFromDir.cs +++ b/SabreTools/Features/DatFromDir.cs @@ -64,6 +64,7 @@ namespace SabreTools.Features bool noAutomaticDate = GetBoolean(features, NoAutomaticDateValue); var includeInScan = GetIncludeInScan(features); var skipFileType = GetSkipFileType(features); + var dfd = new DatTools.DatFromDir(includeInScan, skipFileType, addBlankFiles); // Apply the specialized field removals to the cleaner if (!addFileDates) @@ -90,14 +91,7 @@ namespace SabreTools.Features datdata.FillHeaderFromPath(basePath, noAutomaticDate); // Now populate from the path - bool success = DatTools.DatFromDir.PopulateFromDir( - datdata, - basePath, - asFiles, - skipFileType, - addBlankFiles, - hashes: includeInScan); - + bool success = dfd.PopulateFromDir(datdata, basePath, asFiles); if (success) { // Perform additional processing steps diff --git a/SabreTools/Features/Verify.cs b/SabreTools/Features/Verify.cs index bc77d1e5..f2cdc00b 100644 --- a/SabreTools/Features/Verify.cs +++ b/SabreTools/Features/Verify.cs @@ -54,6 +54,7 @@ namespace SabreTools.Features bool hashOnly = GetBoolean(features, HashOnlyValue); bool quickScan = GetBoolean(features, QuickValue); HashType[] hashes = quickScan ? [HashType.CRC32] : [HashType.CRC32, HashType.MD5, HashType.SHA1]; + var dfd = new DatTools.DatFromDir(hashes, SkipFileType.None, addBlanks: false); // If we are in individual mode, process each DAT on their own if (GetBoolean(features, IndividualValue)) @@ -92,7 +93,7 @@ namespace SabreTools.Features logger.User("Processing files:\n"); foreach (string input in Inputs) { - DatTools.DatFromDir.PopulateFromDir(datdata, input, asFiles: asFiles, hashes: hashes); + dfd.PopulateFromDir(datdata, input, asFiles); } Verification.VerifyGeneric(datdata, hashOnly); @@ -146,7 +147,7 @@ namespace SabreTools.Features logger.User("Processing files:\n"); foreach (string input in Inputs) { - DatTools.DatFromDir.PopulateFromDir(datdata, input, asFiles: asFiles, hashes: hashes); + dfd.PopulateFromDir(datdata, input, asFiles); } Verification.VerifyGeneric(datdata, hashOnly);