diff --git a/RombaSharp/Features/Dir2Dat.cs b/RombaSharp/Features/Dir2Dat.cs index 10116c9c..28f12742 100644 --- a/RombaSharp/Features/Dir2Dat.cs +++ b/RombaSharp/Features/Dir2Dat.cs @@ -51,7 +51,7 @@ namespace RombaSharp.Features DatFile datfile = DatFile.Create(); datfile.Header.Name = string.IsNullOrWhiteSpace(name) ? "untitled" : name; datfile.Header.Description = description; - datfile.PopulateFromDir(source, bare: true, asFiles: TreatAsFiles.AaruFormats | TreatAsFiles.CHDs); + datfile.PopulateFromDir(source, asFiles: TreatAsFiles.AaruFormats | TreatAsFiles.CHDs); datfile.Write(outDir: outdat); } } diff --git a/SabreTools.Library/DatFiles/DatFile.cs b/SabreTools.Library/DatFiles/DatFile.cs index 8a12e67e..602701e6 100644 --- a/SabreTools.Library/DatFiles/DatFile.cs +++ b/SabreTools.Library/DatFiles/DatFile.cs @@ -195,6 +195,34 @@ namespace SabreTools.Library.DatFiles Header.ConditionalCopy(datHeader); } + /// + /// Fill the header values based on existing Header and path + /// + /// Path used for creating a name, if necessary + /// True if the date should be omitted from name and description, false otherwise + public void FillHeaderFromPath(string path, bool bare) + { + // If the description is defined but not the name, set the name from the description + if (string.IsNullOrWhiteSpace(Header.Name) && !string.IsNullOrWhiteSpace(Header.Description)) + { + Header.Name = Header.Description; + } + + // If the name is defined but not the description, set the description from the name + else if (!string.IsNullOrWhiteSpace(Header.Name) && string.IsNullOrWhiteSpace(Header.Description)) + { + Header.Description = Header.Name + (bare ? string.Empty : $" ({Header.Date})"); + } + + // If neither the name or description are defined, set them from the automatic values + else if (string.IsNullOrWhiteSpace(Header.Name) && string.IsNullOrWhiteSpace(Header.Description)) + { + string[] splitpath = path.TrimEnd(Path.DirectorySeparatorChar).Split(Path.DirectorySeparatorChar); + Header.Name = splitpath.Last(); + Header.Description = Header.Name + (bare ? string.Empty : $" ({Header.Date})"); + } + } + #endregion #region Converting and Updating @@ -2098,50 +2126,22 @@ namespace SabreTools.Library.DatFiles /// /// Base folder to be used in creating the DAT /// Hash flag saying what hashes should not be calculated - /// True if the date should be omitted from the DAT, false otherwise /// 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 /// True if dates should be archived for all files, false otherwise /// Output directory to /// True if files should be copied to the temp directory before hashing, false otherwise - /// ExtraIni object to apply to the DatFile - /// Filter object to be passed to the DatItem level - /// True if DatFile tags override splitting, false otherwise /// TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually public bool PopulateFromDir( string basePath, Hash omitFromScan = Hash.DeepHashes, - bool bare = false, TreatAsFiles asFiles = 0x00, SkipFileType skipFileType = SkipFileType.None, bool addBlanks = false, bool addDate = false, - bool copyFiles = false, - ExtraIni extras = null, - Filter filter = null, - bool useTags = false) + bool copyFiles = false) { - // If the description is defined but not the name, set the name from the description - if (string.IsNullOrWhiteSpace(Header.Name) && !string.IsNullOrWhiteSpace(Header.Description)) - { - Header.Name = Header.Description; - } - - // If the name is defined but not the description, set the description from the name - else if (!string.IsNullOrWhiteSpace(Header.Name) && string.IsNullOrWhiteSpace(Header.Description)) - { - Header.Description = Header.Name + (bare ? string.Empty : $" ({Header.Date})"); - } - - // If neither the name or description are defined, set them from the automatic values - else if (string.IsNullOrWhiteSpace(Header.Name) && string.IsNullOrWhiteSpace(Header.Description)) - { - string[] splitpath = basePath.TrimEnd(Path.DirectorySeparatorChar).Split(Path.DirectorySeparatorChar); - Header.Name = splitpath.Last(); - Header.Description = Header.Name + (bare ? string.Empty : $" ({Header.Date})"); - } - // Clean the temp directory path Globals.TempDir = DirectoryExtensions.Ensure(Globals.TempDir, temp: true); @@ -2204,14 +2204,6 @@ namespace SabreTools.Library.DatFiles if (Globals.TempDir != Path.GetTempPath()) DirectoryExtensions.TryDelete(Globals.TempDir); - // If we have valid extras, perform the application now - if (extras != null && extras != default(ExtraIni)) - ApplyExtras(extras); - - // If we have a valid filter, perform the filtering now - if (filter != null && filter != default(Filter)) - ApplyFilter(filter, useTags); - return true; } @@ -2346,8 +2338,7 @@ namespace SabreTools.Library.DatFiles omitFromScan: omitFromScan, date: addDate, header: Header.HeaderSkipper, - aaruFormatAsFiles: asFiles.HasFlag(TreatAsFiles.AaruFormats), - chdsAsFiles: asFiles.HasFlag(TreatAsFiles.CHDs)); + asFiles: asFiles); ProcessFileHelper(item, DatItem.Create(baseFile), basePath, string.Empty); } @@ -2676,7 +2667,7 @@ namespace SabreTools.Library.DatFiles /// True if the DAT should be used as a filter instead of a template, false otherwise /// Output format that files should be written to /// True if the updated DAT should be output, false otherwise - /// TreatAsFiles representing CHD and Archive scanning + /// TreatAsFiles representing special format scanning /// True if rebuilding was a success, false otherwise public bool RebuildGeneric( List inputs, @@ -2811,7 +2802,7 @@ namespace SabreTools.Library.DatFiles /// True if the DAT should be used as a filter instead of a template, false otherwise /// Output format that files should be written to /// True if the updated DAT should be output, false otherwise - /// TreatAsFiles representing CHD and Archive scanning + /// TreatAsFiles representing special format scanning private void RebuildGenericHelper( string file, string outDir, @@ -2837,8 +2828,7 @@ namespace SabreTools.Library.DatFiles file, omitFromScan: (quickScan ? Hash.SecureHashes : Hash.DeepHashes), header: Header.HeaderSkipper, - aaruFormatAsFiles: asFiles.HasFlag(TreatAsFiles.AaruFormats), - chdsAsFiles: asFiles.HasFlag(TreatAsFiles.CHDs)); + asFiles: asFiles); DatItem externalDatItem = null; if (externalFileInfo.Type == FileType.AaruFormat) @@ -2876,8 +2866,7 @@ namespace SabreTools.Library.DatFiles BaseFile internalFileInfo = FileExtensions.GetInfo( file, omitFromScan: (quickScan ? Hash.SecureHashes : Hash.DeepHashes), - aaruFormatAsFiles: asFiles.HasFlag(TreatAsFiles.AaruFormats), - chdsAsFiles: asFiles.HasFlag(TreatAsFiles.CHDs)); + asFiles: asFiles); DatItem internalDatItem = null; if (internalFileInfo.Type == FileType.AaruFormat) @@ -2937,7 +2926,7 @@ namespace SabreTools.Library.DatFiles outputFormat = OutputFormat.Folder; } - // If we have a disk or media, change it into a Rom for later use + // If we have a Disk or Media, change it into a Rom for later use if (datItem.ItemType == ItemType.Disk) datItem = (datItem as Disk).ConvertToRom(); if (datItem.ItemType == ItemType.Media) @@ -3266,10 +3255,9 @@ namespace SabreTools.Library.DatFiles PopulateFromDir( input, quickScan ? Hash.SecureHashes : Hash.DeepHashes, - bare: true, - asFiles: asFiles, - extras: extras, - filter: filter); + asFiles: asFiles); + ApplyExtras(extras); + ApplyFilter(filter, false); } // Setup the fixdat @@ -3465,22 +3453,26 @@ namespace SabreTools.Library.DatFiles { nodump.Items.Add(key, item); } + // If the file has a SHA-512 else if ((item.ItemType == ItemType.Rom && !string.IsNullOrWhiteSpace((item as Rom).SHA512))) { sha512.Items.Add(key, item); } + // If the file has a SHA-384 else if ((item.ItemType == ItemType.Rom && !string.IsNullOrWhiteSpace((item as Rom).SHA384))) { sha384.Items.Add(key, item); } + // If the file has a SHA-256 else if ((item.ItemType == ItemType.Media && !string.IsNullOrWhiteSpace((item as Media).SHA256)) || (item.ItemType == ItemType.Rom && !string.IsNullOrWhiteSpace((item as Rom).SHA256))) { sha256.Items.Add(key, item); } + // If the file has a SHA-1 else if ((item.ItemType == ItemType.Disk && !string.IsNullOrWhiteSpace((item as Disk).SHA1)) || (item.ItemType == ItemType.Media && !string.IsNullOrWhiteSpace((item as Media).SHA1)) @@ -3488,6 +3480,7 @@ namespace SabreTools.Library.DatFiles { sha1.Items.Add(key, item); } + #if NET_FRAMEWORK // If the file has a RIPEMD160 else if ((item.ItemType == ItemType.Rom && !string.IsNullOrWhiteSpace((item as Rom).RIPEMD160))) @@ -3495,11 +3488,11 @@ namespace SabreTools.Library.DatFiles ripemd160.Items.Add(key, item); } #endif + // If the file has an MD5 else if ((item.ItemType == ItemType.Disk && !string.IsNullOrWhiteSpace((item as Disk).MD5)) - || (item.ItemType == ItemType.Media && !string.IsNullOrWhiteSpace((item as Media).MD5) + || (item.ItemType == ItemType.Media && !string.IsNullOrWhiteSpace((item as Media).MD5)) || (item.ItemType == ItemType.Rom && !string.IsNullOrWhiteSpace((item as Rom).MD5))) - ) { md5.Items.Add(key, item); } @@ -3714,8 +3707,8 @@ namespace SabreTools.Library.DatFiles List items = Items[key]; foreach (DatItem item in items) { - // If the file is a Disk - if (item.ItemType == ItemType.Disk) + // If the file is a Disk + if (item.ItemType == ItemType.Disk) diskdat.Items.Add(key, item); // If the file is a Media @@ -3930,7 +3923,6 @@ namespace SabreTools.Library.DatFiles } } - return; } diff --git a/SabreTools.Library/IO/FileExtensions.cs b/SabreTools.Library/IO/FileExtensions.cs index 051b9357..0f842f61 100644 --- a/SabreTools.Library/IO/FileExtensions.cs +++ b/SabreTools.Library/IO/FileExtensions.cs @@ -339,10 +339,9 @@ namespace SabreTools.Library.IO /// Hash flag saying what hashes should not be calculated (defaults to none) /// True if the file Date should be included, false otherwise (default) /// Populated string representing the name of the skipper to use, a blank string to use the first available checker, null otherwise - /// True if AaruFormats should be treated like regular files, false otherwise - /// True if CHDs should be treated like regular files, false otherwise + /// TreatAsFiles representing special format scanning /// Populated BaseFile object if success, empty one on error - public static BaseFile GetInfo(string input, Hash omitFromScan = 0x0, bool date = false, string header = null, bool aaruFormatAsFiles = true, bool chdsAsFiles = true) + public static BaseFile GetInfo(string input, Hash omitFromScan = 0x0, bool date = false, string header = null, TreatAsFiles asFiles = 0x00) { // Add safeguard if file doesn't exist if (!File.Exists(input)) @@ -363,7 +362,7 @@ namespace SabreTools.Library.IO // Transform the stream and get the information from it rule.TransformStream(inputStream, outputStream, keepReadOpen: false, keepWriteOpen: true); - baseFile = outputStream.GetInfo(omitFromScan: omitFromScan, keepReadOpen: false, aaruFormatAsFiles: aaruFormatAsFiles, chdsAsFiles: chdsAsFiles); + baseFile = outputStream.GetInfo(omitFromScan: omitFromScan, keepReadOpen: false, asFiles: asFiles); // Dispose of the streams outputStream.Dispose(); @@ -372,12 +371,12 @@ namespace SabreTools.Library.IO // Otherwise, just get the info else { - baseFile = TryOpenRead(input).GetInfo(omitFromScan: omitFromScan, keepReadOpen: false, aaruFormatAsFiles: aaruFormatAsFiles, chdsAsFiles: chdsAsFiles); + baseFile = TryOpenRead(input).GetInfo(omitFromScan: omitFromScan, keepReadOpen: false, asFiles: asFiles); } } else { - baseFile = TryOpenRead(input).GetInfo(omitFromScan: omitFromScan, keepReadOpen: false, aaruFormatAsFiles: aaruFormatAsFiles, chdsAsFiles: chdsAsFiles); + baseFile = TryOpenRead(input).GetInfo(omitFromScan: omitFromScan, keepReadOpen: false, asFiles: asFiles); } // Add unique data from the file diff --git a/SabreTools.Library/IO/StreamExtensions.cs b/SabreTools.Library/IO/StreamExtensions.cs index bf99e42a..1e01c54f 100644 --- a/SabreTools.Library/IO/StreamExtensions.cs +++ b/SabreTools.Library/IO/StreamExtensions.cs @@ -45,12 +45,11 @@ namespace SabreTools.Library.IO /// Size of the input stream /// Hash flag saying what hashes should not be calculated (defaults to none) /// True if the underlying read stream should be kept open, false otherwise - /// True if AaruFormats should be treated like regular files, false otherwise - /// True if CHDs should be treated like regular files, false otherwise + /// TreatAsFiles representing special format scanning /// Populated BaseFile object if success, empty one on error - public static BaseFile GetInfo(this Stream input, long size = -1, Hash omitFromScan = 0x0, bool keepReadOpen = false, bool aaruFormatAsFiles = true, bool chdsAsFiles = true) + public static BaseFile GetInfo(this Stream input, long size = -1, Hash omitFromScan = 0x0, bool keepReadOpen = false, TreatAsFiles asFiles = 0x00) { - return GetInfoAsync(input, size, omitFromScan, keepReadOpen, aaruFormatAsFiles, chdsAsFiles).ConfigureAwait(false).GetAwaiter().GetResult(); + return GetInfoAsync(input, size, omitFromScan, keepReadOpen, asFiles).ConfigureAwait(false).GetAwaiter().GetResult(); } /// @@ -60,17 +59,16 @@ namespace SabreTools.Library.IO /// Size of the input stream /// Hash flag saying what hashes should not be calculated (defaults to none) /// True if the underlying read stream should be kept open, false otherwise - /// True if AaruFormats should be treated like regular files, false otherwise - /// True if CHDs should be treated like regular files, false otherwise + /// TreatAsFiles representing special format scanning /// Populated BaseFile object if success, empty one on error - public static async Task GetInfoAsync(Stream input, long size = -1, Hash omitFromScan = 0x0, bool keepReadOpen = false, bool aaruFormatAsFiles = true, bool chdsAsFiles = true) + public static async Task GetInfoAsync(Stream input, long size = -1, Hash omitFromScan = 0x0, bool keepReadOpen = false, TreatAsFiles asFiles = 0x00) { // If we want to automatically set the size if (size == -1) size = input.Length; // We first check to see if it's an AaruFormat if we have to - if (!aaruFormatAsFiles) + if (!asFiles.HasFlag(TreatAsFiles.AaruFormats)) { var aaruFormat = AaruFormat.Create(input); input.SeekIfPossible(); @@ -86,7 +84,7 @@ namespace SabreTools.Library.IO } // Then, we first check to see if it's a CHD if we have to - if (!chdsAsFiles) + if (!asFiles.HasFlag(TreatAsFiles.CHDs)) { var chd = CHDFile.Create(input); input.SeekIfPossible(); diff --git a/SabreTools.Library/Skippers/Transform.cs b/SabreTools.Library/Skippers/Transform.cs index 67c0160d..b6473752 100644 --- a/SabreTools.Library/Skippers/Transform.cs +++ b/SabreTools.Library/Skippers/Transform.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using SabreTools.Library.Data; +using SabreTools.Library.DatFiles; using SabreTools.Library.FileTypes; using SabreTools.Library.IO; using SabreTools.Library.Tools; @@ -118,7 +119,7 @@ namespace SabreTools.Library.Skippers // Now add the information to the database if it's not already there if (!nostore) { - BaseFile baseFile = FileExtensions.GetInfo(newfile, aaruFormatAsFiles: true, chdsAsFiles: true); + BaseFile baseFile = FileExtensions.GetInfo(newfile, asFiles: TreatAsFiles.AaruFormats | TreatAsFiles.CHDs); DatabaseTools.AddHeaderToDatabase(hstr, Utilities.ByteArrayToString(baseFile.SHA1), rule.SourceFile); } @@ -138,7 +139,7 @@ namespace SabreTools.Library.Skippers Directory.CreateDirectory(outDir); // First, get the SHA-1 hash of the file - BaseFile baseFile = FileExtensions.GetInfo(file, aaruFormatAsFiles: true, chdsAsFiles: true); + BaseFile baseFile = FileExtensions.GetInfo(file, asFiles: TreatAsFiles.AaruFormats | TreatAsFiles.CHDs); // Retrieve a list of all related headers from the database List headers = DatabaseTools.RetrieveHeadersFromDatabase(Utilities.ByteArrayToString(baseFile.SHA1)); diff --git a/SabreTools/Features/DatFromDir.cs b/SabreTools/Features/DatFromDir.cs index c52b7be8..6aba0041 100644 --- a/SabreTools/Features/DatFromDir.cs +++ b/SabreTools/Features/DatFromDir.cs @@ -77,21 +77,24 @@ namespace SabreTools.Features // Clone the base Dat for information DatFile datdata = DatFile.Create(basedat.Header); + // Get the base path and fill the header, if needed string basePath = Path.GetFullPath(path); + datdata.FillHeaderFromPath(basePath, noAutomaticDate); + + // Now populate from the path bool success = datdata.PopulateFromDir( basePath, omitFromScan, - noAutomaticDate, asFiles, skipFileType, addBlankFiles, addFileDates, - copyFiles, - Extras, - Filter); + copyFiles); if (success) { + datdata.ApplyExtras(Extras); + datdata.ApplyFilter(Filter, false); datdata.Write(OutputDir); } else