Fix Sort multiple outputs

This commit is contained in:
Matt Nadareski
2020-08-28 21:38:27 -07:00
parent e76d19033f
commit 5fb140f406
7 changed files with 9 additions and 73 deletions

View File

@@ -2686,28 +2686,7 @@ namespace SabreTools.Library.DatFiles
return; return;
// Set the deletion variables // Set the deletion variables
bool usedExternally, usedInternally = false; bool usedExternally = false, usedInternally = false;
// Scan the file externally
// TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually
BaseFile externalFileInfo = FileExtensions.GetInfo(
file,
omitFromScan: (quickScan ? Hash.SecureHashes : Hash.DeepHashes),
header: Header.HeaderSkipper,
asFiles: asFiles);
DatItem externalDatItem = null;
if (externalFileInfo.Type == FileType.AaruFormat)
externalDatItem = new Media(externalFileInfo);
else if (externalFileInfo.Type == FileType.CHD)
externalDatItem = new Disk(externalFileInfo);
else if (externalFileInfo.Type == FileType.None)
externalDatItem = new Rom(externalFileInfo);
usedExternally = RebuildIndividualFile(externalDatItem, file, outDir, date, inverse, outputFormat, updateDat, null /* isZip */);
// Scan the file internally
// Create an empty list of BaseFile for archive entries // Create an empty list of BaseFile for archive entries
List<BaseFile> entries = null; List<BaseFile> entries = null;
@@ -2726,7 +2705,7 @@ namespace SabreTools.Library.DatFiles
entries = archive.GetChildren(omitFromScan: (quickScan ? Hash.SecureHashes : Hash.DeepHashes), date: date); entries = archive.GetChildren(omitFromScan: (quickScan ? Hash.SecureHashes : Hash.DeepHashes), date: date);
} }
// If the entries list is null, we encountered an error and should scan exteranlly // If the entries list is null, we encountered an error or have a file and should scan externally
if (entries == null && File.Exists(file)) if (entries == null && File.Exists(file))
{ {
// TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually // TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually

View File

@@ -328,15 +328,11 @@ namespace SabreTools.Library.FileTypes
// If either input is null or empty, return // If either input is null or empty, return
if (inputStream == null || rom == null || rom.Name == null) if (inputStream == null || rom == null || rom.Name == null)
{
return success; return success;
}
// If the stream is not readable, return // If the stream is not readable, return
if (!inputStream.CanRead) if (!inputStream.CanRead)
{
return success; return success;
}
// Set internal variables // Set internal variables
FileStream outputStream = null; FileStream outputStream = null;
@@ -352,9 +348,7 @@ namespace SabreTools.Library.FileTypes
{ {
// If the full output path doesn't exist, create it // If the full output path doesn't exist, create it
if (!Directory.Exists(Path.GetDirectoryName(fileName))) if (!Directory.Exists(Path.GetDirectoryName(fileName)))
{
Directory.CreateDirectory(Path.GetDirectoryName(fileName)); Directory.CreateDirectory(Path.GetDirectoryName(fileName));
}
// Overwrite output files by default // Overwrite output files by default
outputStream = FileExtensions.TryCreate(fileName); outputStream = FileExtensions.TryCreate(fileName);
@@ -372,14 +366,13 @@ namespace SabreTools.Library.FileTypes
outputStream.Write(ibuffer, 0, ilen); outputStream.Write(ibuffer, 0, ilen);
outputStream.Flush(); outputStream.Flush();
} }
outputStream.Dispose(); outputStream.Dispose();
if (rom.ItemType == ItemType.Rom) if (rom.ItemType == ItemType.Rom)
{ {
if (date && !string.IsNullOrWhiteSpace((rom as Rom).Date)) if (date && !string.IsNullOrWhiteSpace((rom as Rom).Date))
{
File.SetCreationTime(fileName, DateTime.Parse((rom as Rom).Date)); File.SetCreationTime(fileName, DateTime.Parse((rom as Rom).Date));
}
} }
success = true; success = true;
@@ -392,7 +385,6 @@ namespace SabreTools.Library.FileTypes
} }
finally finally
{ {
inputStream.Dispose();
outputStream?.Dispose(); outputStream?.Dispose();
} }

View File

@@ -491,6 +491,7 @@ namespace SabreTools.Library.FileTypes
ds.Write(ibuffer, 0, ilen); ds.Write(ibuffer, 0, ilen);
ds.Flush(); ds.Flush();
} }
ds.Dispose(); ds.Dispose();
// Now write the standard footer // Now write the standard footer
@@ -500,7 +501,6 @@ namespace SabreTools.Library.FileTypes
// Dispose of everything // Dispose of everything
sw.Dispose(); sw.Dispose();
outputStream.Dispose(); outputStream.Dispose();
inputStream.Dispose();
} }
return true; return true;

View File

@@ -438,15 +438,11 @@ namespace SabreTools.Library.FileTypes
// If either input is null or empty, return // If either input is null or empty, return
if (inputStream == null || rom == null || rom.Name == null) if (inputStream == null || rom == null || rom.Name == null)
{
return success; return success;
}
// If the stream is not readable, return // If the stream is not readable, return
if (!inputStream.CanRead) if (!inputStream.CanRead)
{
return success; return success;
}
// Seek to the beginning of the stream // Seek to the beginning of the stream
inputStream.Seek(0, SeekOrigin.Begin); inputStream.Seek(0, SeekOrigin.Begin);
@@ -464,9 +460,7 @@ namespace SabreTools.Library.FileTypes
{ {
// If the full output path doesn't exist, create it // If the full output path doesn't exist, create it
if (!Directory.Exists(Path.GetDirectoryName(archiveFileName))) if (!Directory.Exists(Path.GetDirectoryName(archiveFileName)))
{
Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName)); Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName));
}
// If the archive doesn't exist, create it and put the single file // If the archive doesn't exist, create it and put the single file
if (!File.Exists(archiveFileName)) if (!File.Exists(archiveFileName))
@@ -496,7 +490,7 @@ namespace SabreTools.Library.FileTypes
writeStream.Write(ibuffer, 0, ilen); writeStream.Write(ibuffer, 0, ilen);
writeStream.Flush(); writeStream.Flush();
} }
inputStream.Dispose();
zipFile.ZipFileCloseWriteStream(Utilities.StringToByteArray(rom.CRC)); zipFile.ZipFileCloseWriteStream(Utilities.StringToByteArray(rom.CRC));
} }
@@ -572,7 +566,6 @@ namespace SabreTools.Library.FileTypes
writeStream.Flush(); writeStream.Flush();
} }
inputStream.Dispose();
zipFile.ZipFileCloseWriteStream(Utilities.StringToByteArray(rom.CRC)); zipFile.ZipFileCloseWriteStream(Utilities.StringToByteArray(rom.CRC));
} }
@@ -611,14 +604,12 @@ namespace SabreTools.Library.FileTypes
} }
finally finally
{ {
inputStream?.Dispose();
} }
// If the old file exists, delete it and replace // If the old file exists, delete it and replace
if (File.Exists(archiveFileName)) if (File.Exists(archiveFileName))
{
FileExtensions.TryDelete(archiveFileName); FileExtensions.TryDelete(archiveFileName);
}
File.Move(tempFile, archiveFileName); File.Move(tempFile, archiveFileName);
return true; return true;

View File

@@ -313,15 +313,11 @@ namespace SabreTools.Library.FileTypes
// If either input is null or empty, return // If either input is null or empty, return
if (inputStream == null || rom == null || rom.Name == null) if (inputStream == null || rom == null || rom.Name == null)
{
return success; return success;
}
// If the stream is not readable, return // If the stream is not readable, return
if (!inputStream.CanRead) if (!inputStream.CanRead)
{
return success; return success;
}
// Get the output archive name from the first rebuild rom // Get the output archive name from the first rebuild rom
string archiveFileName = Path.Combine(outDir, Sanitizer.RemovePathUnsafeCharacters(rom.Machine.Name) + (rom.Machine.Name.EndsWith(".tar") ? string.Empty : ".tar")); string archiveFileName = Path.Combine(outDir, Sanitizer.RemovePathUnsafeCharacters(rom.Machine.Name) + (rom.Machine.Name.EndsWith(".tar") ? string.Empty : ".tar"));
@@ -334,9 +330,7 @@ namespace SabreTools.Library.FileTypes
{ {
// If the full output path doesn't exist, create it // If the full output path doesn't exist, create it
if (!Directory.Exists(Path.GetDirectoryName(archiveFileName))) if (!Directory.Exists(Path.GetDirectoryName(archiveFileName)))
{
Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName)); Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName));
}
// If the archive doesn't exist, create it and put the single file // If the archive doesn't exist, create it and put the single file
if (!File.Exists(archiveFileName)) if (!File.Exists(archiveFileName))
@@ -344,9 +338,7 @@ namespace SabreTools.Library.FileTypes
// Get temporary date-time if possible // Get temporary date-time if possible
DateTime? usableDate = null; DateTime? usableDate = null;
if (date && !string.IsNullOrWhiteSpace(rom.Date) && DateTime.TryParse(rom.Date.Replace('\\', '/'), out DateTime dt)) if (date && !string.IsNullOrWhiteSpace(rom.Date) && DateTime.TryParse(rom.Date.Replace('\\', '/'), out DateTime dt))
{
usableDate = dt; usableDate = dt;
}
// Copy the input stream to the output // Copy the input stream to the output
inputStream.Seek(0, SeekOrigin.Begin); inputStream.Seek(0, SeekOrigin.Begin);
@@ -367,9 +359,7 @@ namespace SabreTools.Library.FileTypes
// If the old one doesn't contain the new file, then add it // If the old one doesn't contain the new file, then add it
if (!entries.Contains(rom.Name.Replace('\\', '/'))) if (!entries.Contains(rom.Name.Replace('\\', '/')))
{
inputIndexMap.Add(rom.Name.Replace('\\', '/'), -1); inputIndexMap.Add(rom.Name.Replace('\\', '/'), -1);
}
// Then add all of the old entries to it too // Then add all of the old entries to it too
for (int i = 0; i < entries.Count; i++) for (int i = 0; i < entries.Count; i++)
@@ -397,9 +387,7 @@ namespace SabreTools.Library.FileTypes
// Get temporary date-time if possible // Get temporary date-time if possible
DateTime? usableDate = null; DateTime? usableDate = null;
if (date && !string.IsNullOrWhiteSpace(rom.Date) && DateTime.TryParse(rom.Date.Replace('\\', '/'), out DateTime dt)) if (date && !string.IsNullOrWhiteSpace(rom.Date) && DateTime.TryParse(rom.Date.Replace('\\', '/'), out DateTime dt))
{
usableDate = dt; usableDate = dt;
}
// If we have the input file, add it now // If we have the input file, add it now
if (index < 0) if (index < 0)
@@ -435,16 +423,14 @@ namespace SabreTools.Library.FileTypes
} }
finally finally
{ {
inputStream.Dispose();
tarFile.Dispose(); tarFile.Dispose();
oldTarFile.Dispose(); oldTarFile.Dispose();
} }
// If the old file exists, delete it and replace // If the old file exists, delete it and replace
if (File.Exists(archiveFileName)) if (File.Exists(archiveFileName))
{
FileExtensions.TryDelete(archiveFileName); FileExtensions.TryDelete(archiveFileName);
}
File.Move(tempFile, archiveFileName); File.Move(tempFile, archiveFileName);
return success; return success;

View File

@@ -373,7 +373,6 @@ namespace SabreTools.Library.FileTypes
// Dispose of everything // Dispose of everything
outputStream.Dispose(); outputStream.Dispose();
inputStream.Dispose();
} }
return true; return true;

View File

@@ -443,15 +443,11 @@ namespace SabreTools.Library.FileTypes
// If either input is null or empty, return // If either input is null or empty, return
if (inputStream == null || rom == null || rom.Name == null) if (inputStream == null || rom == null || rom.Name == null)
{
return success; return success;
}
// If the stream is not readable, return // If the stream is not readable, return
if (!inputStream.CanRead) if (!inputStream.CanRead)
{
return success; return success;
}
// Seek to the beginning of the stream // Seek to the beginning of the stream
inputStream.Seek(0, SeekOrigin.Begin); inputStream.Seek(0, SeekOrigin.Begin);
@@ -469,9 +465,7 @@ namespace SabreTools.Library.FileTypes
{ {
// If the full output path doesn't exist, create it // If the full output path doesn't exist, create it
if (!Directory.Exists(Path.GetDirectoryName(archiveFileName))) if (!Directory.Exists(Path.GetDirectoryName(archiveFileName)))
{
Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName)); Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName));
}
// If the archive doesn't exist, create it and put the single file // If the archive doesn't exist, create it and put the single file
if (!File.Exists(archiveFileName)) if (!File.Exists(archiveFileName))
@@ -501,7 +495,7 @@ namespace SabreTools.Library.FileTypes
writeStream.Write(ibuffer, 0, ilen); writeStream.Write(ibuffer, 0, ilen);
writeStream.Flush(); writeStream.Flush();
} }
inputStream.Dispose();
zipFile.ZipFileCloseWriteStream(Utilities.StringToByteArray(rom.CRC)); zipFile.ZipFileCloseWriteStream(Utilities.StringToByteArray(rom.CRC));
} }
@@ -521,9 +515,7 @@ namespace SabreTools.Library.FileTypes
// If the old one doesn't contain the new file, then add it // If the old one doesn't contain the new file, then add it
if (!oldZipFileContents.Contains(rom.Name.Replace('\\', '/'))) if (!oldZipFileContents.Contains(rom.Name.Replace('\\', '/')))
{
inputIndexMap.Add(rom.Name.Replace('\\', '/'), -1); inputIndexMap.Add(rom.Name.Replace('\\', '/'), -1);
}
// Then add all of the old entries to it too // Then add all of the old entries to it too
for (int i = 0; i < oldZipFile.LocalFilesCount(); i++) for (int i = 0; i < oldZipFile.LocalFilesCount(); i++)
@@ -577,7 +569,6 @@ namespace SabreTools.Library.FileTypes
writeStream.Flush(); writeStream.Flush();
} }
inputStream.Dispose();
zipFile.ZipFileCloseWriteStream(Utilities.StringToByteArray(rom.CRC)); zipFile.ZipFileCloseWriteStream(Utilities.StringToByteArray(rom.CRC));
} }
@@ -617,14 +608,12 @@ namespace SabreTools.Library.FileTypes
} }
finally finally
{ {
inputStream?.Dispose();
} }
// If the old file exists, delete it and replace // If the old file exists, delete it and replace
if (File.Exists(archiveFileName)) if (File.Exists(archiveFileName))
{
FileExtensions.TryDelete(archiveFileName); FileExtensions.TryDelete(archiveFileName);
}
File.Move(tempFile, archiveFileName); File.Move(tempFile, archiveFileName);
return true; return true;