diff --git a/SabreTools.Library/DatFiles/DatFile.cs b/SabreTools.Library/DatFiles/DatFile.cs index d33f5f21..f2c4d6ba 100644 --- a/SabreTools.Library/DatFiles/DatFile.cs +++ b/SabreTools.Library/DatFiles/DatFile.cs @@ -1934,7 +1934,7 @@ namespace SabreTools.Library.DatFiles #endregion - // TODO: See if any of the helper methods can be broken up a bit more neatly + // TODO: See if any of the methods can be broken up a bit more neatly #region Populate DAT from Directory /// @@ -2012,8 +2012,15 @@ namespace SabreTools.Library.DatFiles } else if (File.Exists(basePath)) { - CheckFileForHashes(basePath, Path.GetDirectoryName(Path.GetDirectoryName(basePath)), omitFromScan, asFiles, - skipFileType, addBlanks, addDate, copyFiles); + CheckFileForHashes( + basePath, + Path.GetDirectoryName(Path.GetDirectoryName(basePath)), + omitFromScan, + asFiles, + skipFileType, + addBlanks, + addDate, + copyFiles); } // Now that we're done, delete the temp folder (if it's not the default) @@ -2045,39 +2052,12 @@ namespace SabreTools.Library.DatFiles bool addDate, bool copyFiles) { - // Special case for if we are in Depot mode (all names are supposed to be SHA-1 hashes) - if (Header.OutputDepot?.IsActive == true) - { - GZipArchive gzarc = new GZipArchive(item); - BaseFile baseFile = gzarc.GetTorrentGZFileInfo(); - - // If the rom is valid, write it out - if (baseFile != null && baseFile.Filename != null) - { - // Add the list if it doesn't exist already - Rom rom = new Rom(baseFile); - Items.Add(rom.GetKey(Field.DatItem_CRC), rom); - Globals.Logger.User($"File added: {Path.GetFileNameWithoutExtension(item)}{Environment.NewLine}"); - } - else - { - Globals.Logger.User($"File not added: {Path.GetFileNameWithoutExtension(item)}{Environment.NewLine}"); - return; - } - + // If we're in depot mode, process it separately + if (CheckDepotFile(item)) return; - } // If we're copying files, copy it first and get the new filename - string newItem = item; - string newBasePath = basePath; - if (copyFiles) - { - newBasePath = Path.Combine(Globals.TempDir, Guid.NewGuid().ToString()); - newItem = Path.GetFullPath(Path.Combine(newBasePath, Path.GetFullPath(item).Remove(0, basePath.Length + 1))); - DirectoryExtensions.TryCreateDirectory(Path.GetDirectoryName(newItem)); - File.Copy(item, newItem, true); - } + (string newItem, string newBasePath) = CopyIfNeeded(item, basePath, copyFiles); // Initialize possible archive variables BaseArchive archive = BaseArchive.Create(newItem); @@ -2096,49 +2076,109 @@ namespace SabreTools.Library.DatFiles // If the extracted list is null, just scan the item itself if (extracted == null) - { ProcessFile(newItem, newBasePath, omitFromScan, addDate, asFiles); - } // Otherwise, add all of the found items else - { - // First take care of the found items - Parallel.ForEach(extracted, Globals.ParallelOptions, rom => - { - DatItem datItem = DatItem.Create(rom); - ProcessFileHelper(newItem, - datItem, - basePath, - (Path.GetDirectoryName(Path.GetFullPath(item)) + Path.DirectorySeparatorChar).Remove(0, basePath.Length) + Path.GetFileNameWithoutExtension(item)); - }); - - // Then, if we're looking for blanks, get all of the blank folders and add them - if (addBlanks) - { - List empties = new List(); - - // Now get all blank folders from the archive - if (archive != null) - empties = archive.GetEmptyFolders(); - - // Add add all of the found empties to the DAT - Parallel.ForEach(empties, Globals.ParallelOptions, empty => - { - Rom emptyRom = new Rom(Path.Combine(empty, "_"), newItem); - ProcessFileHelper(newItem, - emptyRom, - basePath, - (Path.GetDirectoryName(Path.GetFullPath(item)) + Path.DirectorySeparatorChar).Remove(0, basePath.Length) + Path.GetFileNameWithoutExtension(item)); - }); - } - } + ProcessArchive(newItem, newBasePath, addBlanks, archive, extracted); // Cue to delete the file if it's a copy if (copyFiles && item != newItem) DirectoryExtensions.TryDelete(newBasePath); } + /// + /// Check an item as if it's supposed to be in a depot + /// + /// Filename of the item to be checked + /// True if we checked a depot file, false otherwise + private bool CheckDepotFile(string item) + { + // If we're not in Depot mode, return false + if (Header.OutputDepot?.IsActive != true) + return false; + + // Check the file as if it were in a depot + GZipArchive gzarc = new GZipArchive(item); + BaseFile baseFile = gzarc.GetTorrentGZFileInfo(); + + // If the rom is valid, add it + if (baseFile != null && baseFile.Filename != null) + { + // Add the list if it doesn't exist already + Rom rom = new Rom(baseFile); + Items.Add(rom.GetKey(Field.DatItem_CRC), rom); + Globals.Logger.User($"File added: {Path.GetFileNameWithoutExtension(item)}{Environment.NewLine}"); + } + else + { + Globals.Logger.User($"File not added: {Path.GetFileNameWithoutExtension(item)}{Environment.NewLine}"); + return true; + } + + return true; + } + + /// + /// Copy a file to the temp directory if needed and return the new paths + /// + /// Filename of the item to be checked + /// Base folder to be used in creating the DAT + /// True if files should be copied to the temp directory before hashing, false otherwise + /// New item and base path strings + private static (string item, string basePath) CopyIfNeeded(string item, string basePath, bool copyFiles) + { + string newItem = item; + string newBasePath = basePath; + if (copyFiles) + { + newBasePath = Path.Combine(Globals.TempDir, Guid.NewGuid().ToString()); + newItem = Path.GetFullPath(Path.Combine(newBasePath, Path.GetFullPath(item).Remove(0, basePath.Length + 1))); + DirectoryExtensions.TryCreateDirectory(Path.GetDirectoryName(newItem)); + File.Copy(item, newItem, true); + } + + return (newItem, newBasePath); + } + + /// + /// Process a single file as an archive + /// + /// File to be added + /// Path the represents the parent directory + /// True if blank items should be created for empty folders, false otherwise + /// BaseArchive to get blank folders from, if necessary + /// List of BaseFiles representing the internal files + private void ProcessArchive(string item, string basePath, bool addBlanks, BaseArchive archive, List extracted) + { + // Get the parent path for all items + string parent = (Path.GetDirectoryName(Path.GetFullPath(item)) + Path.DirectorySeparatorChar).Remove(0, basePath.Length) + Path.GetFileNameWithoutExtension(item); + + // First take care of the found items + Parallel.ForEach(extracted, Globals.ParallelOptions, baseFile => + { + DatItem datItem = DatItem.Create(baseFile); + ProcessFileHelper(item, datItem, basePath, parent); + }); + + // Then, if we're looking for blanks, get all of the blank folders and add them + if (addBlanks) + { + List empties = new List(); + + // Now get all blank folders from the archive + if (archive != null) + empties = archive.GetEmptyFolders(); + + // Add add all of the found empties to the DAT + Parallel.ForEach(empties, Globals.ParallelOptions, empty => + { + Rom emptyRom = new Rom(Path.Combine(empty, "_"), item); + ProcessFileHelper(item, emptyRom, basePath, parent); + }); + } + } + /// /// Process a single file as a file /// @@ -2150,12 +2190,7 @@ namespace SabreTools.Library.DatFiles private void ProcessFile(string item, string basePath, Hash omitFromScan, bool addDate, TreatAsFiles asFiles) { Globals.Logger.Verbose($"'{Path.GetFileName(item)}' treated like a file"); - BaseFile baseFile = FileExtensions.GetInfo( - item, - omitFromScan: omitFromScan, - date: addDate, - header: Header.HeaderSkipper, - asFiles: asFiles); + BaseFile baseFile = FileExtensions.GetInfo(item, omitFromScan, addDate, Header.HeaderSkipper, asFiles); ProcessFileHelper(item, DatItem.Create(baseFile), basePath, string.Empty); }