diff --git a/SabreTools.Helper/Dats/Partials/DatFile.Rebuild.cs b/SabreTools.Helper/Dats/Partials/DatFile.Rebuild.cs index 9ea4e741..6ea6f691 100644 --- a/SabreTools.Helper/Dats/Partials/DatFile.Rebuild.cs +++ b/SabreTools.Helper/Dats/Partials/DatFile.Rebuild.cs @@ -398,45 +398,36 @@ namespace SabreTools.Helper.Dats // If we're supposed to scan the file internally if (shouldInternalProcess) { - // If quickscan is set, do so + // Create an empty list of Roms for archive entries + List entries = new List(); + usedInternally = true; + + // If we're in quickscan, use the header information if (quickScan) { - List extracted = ArchiveTools.GetArchiveFileInfo(file); - usedInternally = true; - - foreach (Rom rom in extracted) - { - usedInternally &= RebuildIndividualFile(rom, file, outDir, tempSubDir, date, inverse, outputFormat, - romba, updateDat, true /* isZip */, headerToCheckAgainst); - } + entries = ArchiveTools.GetArchiveFileInfo(file); } - // Otherwise, attempt to extract the files to the temporary directory + // Otherwise get the deeper information else { - bool encounteredErrors = ArchiveTools.ExtractArchive(file, tempSubDir, archiveScanLevel); + entries = ArchiveTools.GetExtendedArchiveFileInfo(file); + } - // If the file was an archive and was extracted successfully, check it - if (!encounteredErrors) + // If the entries list is null, we encountered an error and should scan exteranlly + if (entries == null && File.Exists(file)) + { + // TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually + Rom rom = FileTools.GetFileInfo(file, omitFromScan: (quickScan ? Hash.SecureHashes : Hash.DeepHashes)); + usedExternally = RebuildIndividualFile(rom, file, outDir, tempSubDir, date, inverse, outputFormat, + romba, updateDat, false /* isZip */, headerToCheckAgainst); + } + // Otherwise, loop through the entries and try to match + else + { + foreach (Rom entry in entries) { - usedInternally = true; - - Globals.Logger.Verbose(Path.GetFileName(file) + " treated like an archive"); - List extracted = Directory.EnumerateFiles(tempSubDir, "*", SearchOption.AllDirectories).ToList(); - foreach (string entry in extracted) - { - // TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually - Rom rom = FileTools.GetFileInfo(entry, omitFromScan: (quickScan ? Hash.SecureHashes : Hash.DeepHashes)); - usedInternally &= RebuildIndividualFile(rom, entry, outDir, tempSubDir, date, inverse, outputFormat, - romba, updateDat, false /* isZip */, headerToCheckAgainst); - } - } - // Otherwise, just get the info on the file itself - else if (File.Exists(file)) - { - // TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually - Rom rom = FileTools.GetFileInfo(file, omitFromScan: (quickScan ? Hash.SecureHashes : Hash.DeepHashes)); - usedExternally = RebuildIndividualFile(rom, file, outDir, tempSubDir, date, inverse, outputFormat, - romba, updateDat, false /* isZip */, headerToCheckAgainst); + usedInternally &= RebuildIndividualFile(entry, file, outDir, tempSubDir, date, inverse, outputFormat, + romba, updateDat, true /* isZip */, headerToCheckAgainst); } } } @@ -444,16 +435,7 @@ namespace SabreTools.Helper.Dats // If we are supposed to delete the file, do so if (delete && (usedExternally || usedInternally)) { - try - { - Globals.Logger.Verbose("Attempting to delete input file '" + file + "'"); - FileTools.TryDeleteFile(file, true); - Globals.Logger.Verbose("File '" + file + "' deleted"); - } - catch (Exception ex) - { - Globals.Logger.Error("An error occurred while trying to delete '" + file + "' " + ex.ToString()); - } + FileTools.TryDeleteFile(file); } // Now delete the temp directory @@ -500,6 +482,7 @@ namespace SabreTools.Helper.Dats } // If we have an archive input, get the real name of the file to use + // TODO: Remove the need to extract the file first; reimplement ArchiveToArchive? if (isZip) { // Otherwise, extract the file to the temp folder diff --git a/SabreTools.Helper/Tools/ArchiveTools.cs b/SabreTools.Helper/Tools/ArchiveTools.cs index b5fff4b5..8f96ade7 100644 --- a/SabreTools.Helper/Tools/ArchiveTools.cs +++ b/SabreTools.Helper/Tools/ArchiveTools.cs @@ -43,6 +43,43 @@ namespace SabreTools.Helper.Tools { private const int _bufferSize = 4096 * 128; + #region Archive-to-Archive handling + + /// + /// Transfer a single file from one archive to another + /// + /// Input archive name + /// Input entry name + /// Output directory + /// Output Rom information + /// True if dates are preserved, false otherwise (default) + /// True if the transfer was a success, false otherwise + public static bool Transfer(string inputArchive, string inputEntry, string outputDir, Rom outputEntry, bool date = false) + { + // Verify inputs + // Create list versions + // return Transfer(multiple) + return false; + } + + /// + /// Transfer multiple files from one archive to another + /// + /// Input archive names + /// Input entry names + /// Output directory + /// Output Rom informations + /// True if dates are preserved, false otherwise (default) + /// True if the transfesr were a success, false otherwise + public static bool Transfer(List inputArchives, List inputEntries, string outputDir, List outputEntries, bool date = false) + { + // Verify inputs + // For each item, extract to stream, write to archive + return false; + } + + #endregion + #region Extraction /// @@ -241,7 +278,6 @@ namespace SabreTools.Helper.Tools SevenZipArchive sza = SevenZipArchive.Open(input, new ReaderOptions { LeaveStreamOpen = false, }); foreach (SevenZipArchiveEntry entry in sza.Entries) { - Globals.Logger.Verbose("Current entry name: '" + entry.Key + "'"); if (entry != null && !entry.IsDirectory && entry.Key.Contains(entryName)) { realEntry = entry.Key; @@ -293,7 +329,6 @@ namespace SabreTools.Helper.Tools RarArchive ra = RarArchive.Open(input, new ReaderOptions { LeaveStreamOpen = false, }); foreach (RarArchiveEntry entry in ra.Entries) { - Globals.Logger.Verbose("Current entry name: '" + entry.Key + "'"); if (entry != null && !entry.IsDirectory && entry.Key.Contains(entryName)) { realEntry = entry.Key; @@ -317,7 +352,6 @@ namespace SabreTools.Helper.Tools TarArchive ta = TarArchive.Open(input, new ReaderOptions { LeaveStreamOpen = false, }); foreach (TarArchiveEntry entry in ta.Entries) { - Globals.Logger.Verbose("Current entry name: '" + entry.Key + "'"); if (entry != null && !entry.IsDirectory && entry.Key.Contains(entryName)) { realEntry = entry.Key; @@ -347,7 +381,6 @@ namespace SabreTools.Helper.Tools for (int i = 0; i < zf.EntriesCount && zr == ZipReturn.ZipGood; i++) { - Globals.Logger.Verbose("Current entry name: '" + zf.Entries[i].FileName + "'"); if (zf.Entries[i].FileName.Contains(entryName)) { realEntry = zf.Entries[i].FileName; @@ -376,6 +409,8 @@ namespace SabreTools.Helper.Tools zipfileout.Dispose(); } } + + zf.Dispose(); break; } } @@ -397,6 +432,7 @@ namespace SabreTools.Helper.Tools /// /// Input file to get data from /// List of RomData objects representing the found data + /// TODO: Can this be merged with the extended one? public static List GetArchiveFileInfo(string input) { List roms = new List(); @@ -747,6 +783,7 @@ namespace SabreTools.Helper.Tools catch (Exception) { // Don't log file open errors + return null; } return found;