Cleanup between namespace extraction

This commit is contained in:
Matt Nadareski
2020-12-08 11:09:05 -08:00
parent 96f0a94b10
commit 174be6ca54
78 changed files with 809 additions and 1231 deletions

View File

@@ -4,8 +4,6 @@ using System.IO;
using System.Linq;
using SabreTools.Data;
using SabreTools.IO;
using SabreTools.Library.DatItems;
using SabreTools.Library.Tools;
using Compress;
using Compress.SevenZip;
@@ -46,11 +44,7 @@ namespace SabreTools.Library.FileTypes
#region Extraction
/// <summary>
/// Attempt to extract a file as an archive
/// </summary>
/// <param name="outDir">Output directory for archive extraction</param>
/// <returns>True if the extraction was a success, false otherwise</returns>
/// <inheritdoc/>
public override bool CopyAll(string outDir)
{
bool encounteredErrors = true;
@@ -135,12 +129,7 @@ namespace SabreTools.Library.FileTypes
return encounteredErrors;
}
/// <summary>
/// Attempt to extract a file from an archive
/// </summary>
/// <param name="entryName">Name of the entry to be extracted</param>
/// <param name="outDir">Output directory for archive extraction</param>
/// <returns>Name of the extracted file, null on error</returns>
/// <inheritdoc/>
public override string CopyToFile(string entryName, string outDir)
{
// Try to extract a stream using the given information
@@ -181,12 +170,7 @@ namespace SabreTools.Library.FileTypes
return realEntry;
}
/// <summary>
/// Attempt to extract a stream from an archive
/// </summary>
/// <param name="entryName">Name of the entry to be extracted</param>
/// <param name="realEntry">Output representing the entry name that was found</param>
/// <returns>MemoryStream representing the entry, null on error</returns>
/// <inheritdoc/>
public override (MemoryStream, string) CopyToStream(string entryName)
{
MemoryStream ms = new MemoryStream();
@@ -255,10 +239,7 @@ namespace SabreTools.Library.FileTypes
#region Information
/// <summary>
/// Generate a list of DatItem objects from the header values in an archive
/// </summary>
/// <returns>List of DatItem objects representing the found data</returns>
/// <inheritdoc/>
public override List<BaseFile> GetChildren()
{
List<BaseFile> found = new List<BaseFile>();
@@ -329,11 +310,7 @@ namespace SabreTools.Library.FileTypes
return found;
}
/// <summary>
/// Generate a list of empty folders in an archive
/// </summary>
/// <param name="input">Input file to get data from</param>
/// <returns>List of empty folders in the archive</returns>
/// <inheritdoc/>
public override List<string> GetEmptyFolders()
{
List<string> empties = new List<string>();
@@ -381,9 +358,7 @@ namespace SabreTools.Library.FileTypes
return empties;
}
/// <summary>
/// Check whether the input file is a standardized format
/// </summary>
/// <inheritdoc/>
public override bool IsTorrent()
{
SevenZ zf = new SevenZ();
@@ -400,33 +375,21 @@ namespace SabreTools.Library.FileTypes
#region Writing
/// <summary>
/// Write an input file to a torrent7z archive
/// </summary>
/// <param name="inputFile">Input filename to be moved</param>
/// <param name="outDir">Output directory to build to</param>
/// <param name="rom">DatItem representing the new information</param>
/// <returns>True if the archive was written properly, false otherwise</returns>
public override bool Write(string inputFile, string outDir, Rom rom)
/// <inheritdoc/>
public override bool Write(string inputFile, string outDir, BaseFile baseFile)
{
// Get the file stream for the file and write out
return Write(File.OpenRead(inputFile), outDir, rom);
return Write(File.OpenRead(inputFile), outDir, baseFile);
}
/// <summary>
/// Write an input file to a torrent7z archive
/// </summary>
/// <param name="inputStream">Input stream to be moved</param>
/// <param name="outDir">Output directory to build to</param>
/// <param name="rom">DatItem representing the new information</param>
/// <returns>True if the archive was written properly, false otherwise</returns>
public override bool Write(Stream inputStream, string outDir, Rom rom)
/// <inheritdoc/>
public override bool Write(Stream inputStream, string outDir, BaseFile baseFile)
{
bool success = false;
string tempFile = Path.Combine(outDir, $"tmp{Guid.NewGuid()}");
// If either input is null or empty, return
if (inputStream == null || rom == null || rom.Name == null)
if (inputStream == null || baseFile == null || baseFile.Filename == null)
return success;
// If the stream is not readable, return
@@ -437,7 +400,7 @@ namespace SabreTools.Library.FileTypes
inputStream.Seek(0, SeekOrigin.Begin);
// Get the output archive name from the first rebuild rom
string archiveFileName = Path.Combine(outDir, Sanitizer.RemovePathUnsafeCharacters(rom.Machine.Name) + (rom.Machine.Name.EndsWith(".7z") ? string.Empty : ".7z"));
string archiveFileName = Path.Combine(outDir, Sanitizer.RemovePathUnsafeCharacters(baseFile.Parent) + (baseFile.Parent.EndsWith(".7z") ? string.Empty : ".7z"));
// Set internal variables
Stream writeStream = null;
@@ -461,14 +424,14 @@ namespace SabreTools.Library.FileTypes
ulong istreamSize = (ulong)(inputStream.Length);
DateTime dt = DateTime.Now;
if (UseDates && !string.IsNullOrWhiteSpace(rom.Date) && DateTime.TryParse(rom.Date.Replace('\\', '/'), out dt))
if (UseDates && !string.IsNullOrWhiteSpace(baseFile.Date) && DateTime.TryParse(baseFile.Date.Replace('\\', '/'), out dt))
{
uint msDosDateTime = Utilities.ConvertDateTimeToMsDosTimeFormat(dt);
zipFile.ZipFileOpenWriteStream(false, false, rom.Name.Replace('\\', '/'), istreamSize, 0, msDosDateTime, out writeStream);
zipFile.ZipFileOpenWriteStream(false, false, baseFile.Filename.Replace('\\', '/'), istreamSize, 0, msDosDateTime, out writeStream);
}
else
{
zipFile.ZipFileOpenWriteStream(false, true, rom.Name.Replace('\\', '/'), istreamSize, 0, null, out writeStream);
zipFile.ZipFileOpenWriteStream(false, true, baseFile.Filename.Replace('\\', '/'), istreamSize, 0, null, out writeStream);
}
// Copy the input stream to the output
@@ -480,7 +443,7 @@ namespace SabreTools.Library.FileTypes
writeStream.Flush();
}
zipFile.ZipFileCloseWriteStream(Utilities.StringToByteArray(rom.CRC));
zipFile.ZipFileCloseWriteStream(baseFile.CRC);
}
// Otherwise, sort the input files and write out in the correct order
@@ -498,9 +461,9 @@ namespace SabreTools.Library.FileTypes
}
// If the old one doesn't contain the new file, then add it
if (!oldZipFileContents.Contains(rom.Name.Replace('\\', '/')))
if (!oldZipFileContents.Contains(baseFile.Filename.Replace('\\', '/')))
{
inputIndexMap.Add(rom.Name.Replace('\\', '/'), -1);
inputIndexMap.Add(baseFile.Filename.Replace('\\', '/'), -1);
}
// Then add all of the old entries to it too
@@ -536,14 +499,14 @@ namespace SabreTools.Library.FileTypes
ulong istreamSize = (ulong)(inputStream.Length);
DateTime dt = DateTime.Now;
if (UseDates && !string.IsNullOrWhiteSpace(rom.Date) && DateTime.TryParse(rom.Date.Replace('\\', '/'), out dt))
if (UseDates && !string.IsNullOrWhiteSpace(baseFile.Date) && DateTime.TryParse(baseFile.Date.Replace('\\', '/'), out dt))
{
uint msDosDateTime = Utilities.ConvertDateTimeToMsDosTimeFormat(dt);
zipFile.ZipFileOpenWriteStream(false, false, rom.Name.Replace('\\', '/'), istreamSize, 0, msDosDateTime, out writeStream);
zipFile.ZipFileOpenWriteStream(false, false, baseFile.Filename.Replace('\\', '/'), istreamSize, 0, msDosDateTime, out writeStream);
}
else
{
zipFile.ZipFileOpenWriteStream(false, true, rom.Name.Replace('\\', '/'), istreamSize, 0, null, out writeStream);
zipFile.ZipFileOpenWriteStream(false, true, baseFile.Filename.Replace('\\', '/'), istreamSize, 0, null, out writeStream);
}
// Copy the input stream to the output
@@ -555,7 +518,7 @@ namespace SabreTools.Library.FileTypes
writeStream.Flush();
}
zipFile.ZipFileCloseWriteStream(Utilities.StringToByteArray(rom.CRC));
zipFile.ZipFileCloseWriteStream(baseFile.CRC);
}
// Otherwise, copy the file from the old archive
@@ -604,26 +567,20 @@ namespace SabreTools.Library.FileTypes
return true;
}
/// <summary>
/// Write a set of input files to a torrent7z archive (assuming the same output archive name)
/// </summary>
/// <param name="inputFiles">Input files to be moved</param>
/// <param name="outDir">Output directory to build to</param>
/// <param name="rom">DatItem representing the new information</param>
/// <returns>True if the archive was written properly, false otherwise</returns>
public override bool Write(List<string> inputFiles, string outDir, List<Rom> roms)
/// <inheritdoc/>
public override bool Write(List<string> inputFiles, string outDir, List<BaseFile> baseFiles)
{
bool success = false;
string tempFile = Path.Combine(outDir, $"tmp{Guid.NewGuid()}");
// If either list of roms is null or empty, return
if (inputFiles == null || roms == null || inputFiles.Count == 0 || roms.Count == 0)
if (inputFiles == null || baseFiles == null || inputFiles.Count == 0 || baseFiles.Count == 0)
{
return success;
}
// If the number of inputs is less than the number of available roms, return
if (inputFiles.Count < roms.Count)
if (inputFiles.Count < baseFiles.Count)
{
return success;
}
@@ -638,7 +595,7 @@ namespace SabreTools.Library.FileTypes
}
// Get the output archive name from the first rebuild rom
string archiveFileName = Path.Combine(outDir, Sanitizer.RemovePathUnsafeCharacters(roms[0].Machine.Name) + (roms[0].Machine.Name.EndsWith(".7z") ? string.Empty : ".7z"));
string archiveFileName = Path.Combine(outDir, Sanitizer.RemovePathUnsafeCharacters(baseFiles[0].Parent) + (baseFiles[0].Parent.EndsWith(".7z") ? string.Empty : ".7z"));
// Set internal variables
Stream writeStream = null;
@@ -663,7 +620,7 @@ namespace SabreTools.Library.FileTypes
Dictionary<string, int> inputIndexMap = new Dictionary<string, int>();
for (int i = 0; i < inputFiles.Count; i++)
{
inputIndexMap.Add(roms[i].Name.Replace('\\', '/'), i);
inputIndexMap.Add(baseFiles[i].Filename.Replace('\\', '/'), i);
}
// Sort the keys in TZIP order
@@ -681,14 +638,14 @@ namespace SabreTools.Library.FileTypes
ulong istreamSize = (ulong)(new FileInfo(inputFiles[index]).Length);
DateTime dt = DateTime.Now;
if (UseDates && !string.IsNullOrWhiteSpace(roms[index].Date) && DateTime.TryParse(roms[index].Date.Replace('\\', '/'), out dt))
if (UseDates && !string.IsNullOrWhiteSpace(baseFiles[index].Date) && DateTime.TryParse(baseFiles[index].Date.Replace('\\', '/'), out dt))
{
uint msDosDateTime = Utilities.ConvertDateTimeToMsDosTimeFormat(dt);
zipFile.ZipFileOpenWriteStream(false, false, roms[index].Name.Replace('\\', '/'), istreamSize, 0, msDosDateTime, out writeStream);
zipFile.ZipFileOpenWriteStream(false, false, baseFiles[index].Filename.Replace('\\', '/'), istreamSize, 0, msDosDateTime, out writeStream);
}
else
{
zipFile.ZipFileOpenWriteStream(false, true, roms[index].Name.Replace('\\', '/'), istreamSize, 0, null, out writeStream);
zipFile.ZipFileOpenWriteStream(false, true, baseFiles[index].Filename.Replace('\\', '/'), istreamSize, 0, null, out writeStream);
}
// Copy the input stream to the output
@@ -701,7 +658,7 @@ namespace SabreTools.Library.FileTypes
}
freadStream.Dispose();
zipFile.ZipFileCloseWriteStream(Utilities.StringToByteArray(roms[index].CRC));
zipFile.ZipFileCloseWriteStream(baseFiles[index].CRC);
}
}
@@ -722,12 +679,12 @@ namespace SabreTools.Library.FileTypes
}
// If the old one contains the new file, then just skip out
if (oldZipFileContents.Contains(roms[i].Name.Replace('\\', '/')))
if (oldZipFileContents.Contains(baseFiles[i].Filename.Replace('\\', '/')))
{
continue;
}
inputIndexMap.Add(roms[i].Name.Replace('\\', '/'), -(i + 1));
inputIndexMap.Add(baseFiles[i].Filename.Replace('\\', '/'), -(i + 1));
}
// Then add all of the old entries to it too
@@ -764,14 +721,14 @@ namespace SabreTools.Library.FileTypes
ulong istreamSize = (ulong)(new FileInfo(inputFiles[-index - 1]).Length);
DateTime dt = DateTime.Now;
if (UseDates && !string.IsNullOrWhiteSpace(roms[-index - 1].Date) && DateTime.TryParse(roms[-index - 1].Date.Replace('\\', '/'), out dt))
if (UseDates && !string.IsNullOrWhiteSpace(baseFiles[-index - 1].Date) && DateTime.TryParse(baseFiles[-index - 1].Date.Replace('\\', '/'), out dt))
{
uint msDosDateTime = Utilities.ConvertDateTimeToMsDosTimeFormat(dt);
zipFile.ZipFileOpenWriteStream(false, false, roms[-index - 1].Name.Replace('\\', '/'), istreamSize, 0, msDosDateTime, out writeStream);
zipFile.ZipFileOpenWriteStream(false, false, baseFiles[-index - 1].Filename.Replace('\\', '/'), istreamSize, 0, msDosDateTime, out writeStream);
}
else
{
zipFile.ZipFileOpenWriteStream(false, true, roms[-index - 1].Name.Replace('\\', '/'), istreamSize, 0, null, out writeStream);
zipFile.ZipFileOpenWriteStream(false, true, baseFiles[-index - 1].Filename.Replace('\\', '/'), istreamSize, 0, null, out writeStream);
}
// Copy the input stream to the output
@@ -783,7 +740,7 @@ namespace SabreTools.Library.FileTypes
writeStream.Flush();
}
freadStream.Dispose();
zipFile.ZipFileCloseWriteStream(Utilities.StringToByteArray(roms[-index - 1].CRC));
zipFile.ZipFileCloseWriteStream(baseFiles[-index - 1].CRC);
}
// Otherwise, copy the file from the old archive