[SimpleSort] Add header support, fix scan levels

This commit is contained in:
Matt Nadareski
2016-06-14 12:36:25 -07:00
parent e1b90fea0a
commit 0fcff7f857
4 changed files with 177 additions and 26 deletions

View File

@@ -19,31 +19,67 @@ namespace SabreTools.Helper
/// <param name="output">Output directory to build to</param> /// <param name="output">Output directory to build to</param>
/// <param name="archiveType">Type of archive to attempt to write to</param> /// <param name="archiveType">Type of archive to attempt to write to</param>
/// <param name="rom">RomData representing the new information</param> /// <param name="rom">RomData representing the new information</param>
public static void WriteArchiveOrFile(string input, string output, ArchiveType archiveType, RomData rom) public static void WriteFileToArchive(string input, string output, ArchiveType archiveType, RomData rom)
{ {
string archiveFileName = output + Path.DirectorySeparatorChar + rom.Game + ".zip"; string archiveFileName = output + Path.DirectorySeparatorChar + rom.Game + ".zip";
string singleFileName = output + Path.DirectorySeparatorChar + rom.Game + Path.DirectorySeparatorChar + rom.Name; string singleFileName = output + Path.DirectorySeparatorChar + rom.Game + Path.DirectorySeparatorChar + rom.Name;
IWriter outarchive = null; IWritableArchive outarchive = null;
FileStream fs = null;
try try
{ {
fs = File.OpenWrite(archiveFileName); if (!File.Exists(archiveFileName))
outarchive = WriterFactory.Open(fs, ArchiveType.Zip, CompressionType.Deflate);
outarchive.Write(rom.Name, input);
}
catch
{ {
if (!File.Exists(singleFileName)) outarchive = ArchiveFactory.Create(archiveType) as IWritableArchive;
{
File.Copy(input, singleFileName);
} }
else
{
outarchive = ArchiveFactory.Open(archiveFileName, Options.LookForHeader) as IWritableArchive;
}
outarchive.AddEntry(rom.Name, input);
outarchive.SaveTo(archiveFileName, new CompressionInfo { Type = CompressionType.Deflate });
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
outarchive?.Dispose();
}
}
/// <summary>
/// Copy a file either to an output archive or to an output folder
/// </summary>
/// <param name="input">Input filename to be moved</param>
/// <param name="output">Output directory to build to</param>
/// <param name="archiveType">Type of archive to attempt to write to</param>
/// <param name="rom">RomData representing the new information</param>
public static void WriteFolderToArchive(string input, string output, ArchiveType archiveType)
{
string archiveFileName = output + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(input) + ".zip";
IWritableArchive outarchive = null;
try
{
if (!File.Exists(archiveFileName))
{
outarchive = ArchiveFactory.Create(archiveType) as IWritableArchive;
}
else
{
outarchive = ArchiveFactory.Open(archiveFileName, Options.LookForHeader) as IWritableArchive;
}
outarchive.AddAllFromDirectory(input, "*", SearchOption.AllDirectories);
outarchive.SaveTo(archiveFileName, new CompressionInfo { Type = CompressionType.Deflate });
}
catch (Exception ex)
{
Console.WriteLine(ex);
} }
finally finally
{ {
outarchive?.Dispose(); outarchive?.Dispose();
fs?.Close();
fs?.Dispose();
} }
} }

View File

@@ -608,7 +608,7 @@ namespace SabreTools.Helper
long adjustedLength = br.BaseStream.Length - bytesToRemoveFromTail; long adjustedLength = br.BaseStream.Length - bytesToRemoveFromTail;
// Seek to the correct position // Seek to the correct position
br.BaseStream.Seek(bytesToRemoveFromHead, SeekOrigin.Begin); br.BaseStream.Seek((bytesToRemoveFromHead < 0 ? 0 : bytesToRemoveFromHead), SeekOrigin.Begin);
// Now read the file in chunks and write out // Now read the file in chunks and write out
byte[] buffer = new byte[bufferSize]; byte[] buffer = new byte[bufferSize];
@@ -695,5 +695,22 @@ namespace SabreTools.Helper
} }
} }
} }
/// <summary>
/// Copy a file to a new location, creating directories as needed
/// </summary>
/// <param name="input">Input filename</param>
/// <param name="output">Output filename</param>
public static void CopyFileToNewLocation(string input, string output)
{
if (File.Exists(input) && !File.Exists(output))
{
if (!Directory.Exists(Path.GetDirectoryName(output)))
{
Directory.CreateDirectory(Path.GetDirectoryName(output));
}
File.Copy(input, output);
}
}
} }
} }

View File

@@ -16,6 +16,7 @@ namespace SabreTools.Helper
/// <param name="noMD5">True if MD5 hashes should not be calculated, false otherwise</param> /// <param name="noMD5">True if MD5 hashes should not be calculated, false otherwise</param>
/// <param name="noSHA1">True if SHA-1 hashes should not be calcluated, false otherwise</param> /// <param name="noSHA1">True if SHA-1 hashes should not be calcluated, false otherwise</param>
/// <returns>Populated RomData object if success, empty one on error</returns> /// <returns>Populated RomData object if success, empty one on error</returns>
/// <remarks>Add read-offset for hash info</remarks>
public static RomData GetSingleFileInfo(string input, bool noMD5 = false, bool noSHA1 = false) public static RomData GetSingleFileInfo(string input, bool noMD5 = false, bool noSHA1 = false)
{ {
RomData rom = new RomData RomData rom = new RomData

View File

@@ -1,4 +1,5 @@
using SabreTools.Helper; using SabreTools.Helper;
using SharpCompress.Archive;
using SharpCompress.Common; using SharpCompress.Common;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -249,6 +250,16 @@ namespace SabreTools
_outdir = Path.GetFullPath(_outdir); _outdir = Path.GetFullPath(_outdir);
} }
// Then create or clean the temp directory
if (!Directory.Exists(_tempdir))
{
Directory.CreateDirectory(_tempdir);
}
else
{
Output.CleanDirectory(_tempdir);
}
// Then, loop through and check each of the inputs // Then, loop through and check each of the inputs
_logger.User("Starting to loop through inputs"); _logger.User("Starting to loop through inputs");
foreach (string input in _inputs) foreach (string input in _inputs)
@@ -275,6 +286,20 @@ namespace SabreTools
} }
} }
// Now process the output directory and write all to zipfiles
foreach (string dir in Directory.EnumerateDirectories(_outdir, "*", SearchOption.TopDirectoryOnly))
{
ArchiveTools.WriteFolderToArchive(dir, _outdir, ArchiveType.Zip);
try
{
Directory.Delete(dir, true);
}
catch (Exception ex)
{
_logger.Error(ex.ToString());
}
}
// Now one final delete of the temp directory // Now one final delete of the temp directory
Directory.Delete(_tempdir, true); Directory.Delete(_tempdir, true);
@@ -295,7 +320,35 @@ namespace SabreTools
input = Path.GetFullPath(input); input = Path.GetFullPath(input);
_logger.User("Beginning processing of '" + input + "'"); _logger.User("Beginning processing of '" + input + "'");
// Get the hash of the file first // If we have an archive, scan it if necessary
bool shouldscan = true;
try
{
IArchive temp = ArchiveFactory.Open(input);
switch (temp.Type)
{
case ArchiveType.GZip:
shouldscan = (_gz != ArchiveScanLevel.Internal);
break;
case ArchiveType.Rar:
shouldscan = (_rar != ArchiveScanLevel.Internal);
break;
case ArchiveType.SevenZip:
shouldscan = (_7z != ArchiveScanLevel.Internal);
break;
case ArchiveType.Zip:
shouldscan = (_zip != ArchiveScanLevel.Internal);
break;
}
}
catch
{
shouldscan = true;
}
// Hash and match the external files
if (shouldscan)
{
RomData rom = RomTools.GetSingleFileInfo(input); RomData rom = RomTools.GetSingleFileInfo(input);
// If we have a blank RomData, it's an error // If we have a blank RomData, it's an error
@@ -309,7 +362,51 @@ namespace SabreTools
_logger.User("File '" + input + "' had " + foundroms.Count + " matches in the DAT!"); _logger.User("File '" + input + "' had " + foundroms.Count + " matches in the DAT!");
foreach (RomData found in foundroms) foreach (RomData found in foundroms)
{ {
ArchiveTools.WriteArchiveOrFile(input, _outdir, ArchiveType.Zip, found); _logger.Log("Matched name: " + found.Name);
string singleFileName = _outdir + Path.DirectorySeparatorChar + found.Game + Path.DirectorySeparatorChar + found.Name;
Output.CopyFileToNewLocation(input, singleFileName);
}
// Now get the headerless file if it exists
int hs = 0;
RomTools.GetFileHeaderType(input, out hs, _logger);
if (hs > 0)
{
string newinput = input + ".new";
_logger.Log("Creating unheadered file: '" + newinput + "'");
Output.RemoveBytesFromFile(input, newinput, hs, 0);
RomData drom = RomTools.GetSingleFileInfo(newinput);
// If we have a blank RomData, it's an error
if (drom.Name == null)
{
return false;
}
// Try to find the matches to the file that was found
List<RomData> founddroms = RomTools.GetDuplicates(drom, _datdata);
_logger.User("File '" + newinput + "' had " + founddroms.Count + " matches in the DAT!");
foreach (RomData found in founddroms)
{
_logger.Log("Matched name: " + found.Name);
// First output the headerless rom
string singleFileName = _outdir + Path.DirectorySeparatorChar + found.Game + Path.DirectorySeparatorChar + found.Name;
Output.CopyFileToNewLocation(newinput, singleFileName);
// Then output the headered rom (renamed)
RomData newfound = found;
newfound.Name = Path.GetFileNameWithoutExtension(newfound.Name) + " (" + rom.CRC + ")" + Path.GetExtension(newfound.Name);
_logger.Log("Matched name: " + newfound.Name);
singleFileName = _outdir + Path.DirectorySeparatorChar + newfound.Game + Path.DirectorySeparatorChar + newfound.Name;
Output.CopyFileToNewLocation(input, singleFileName);
}
// Now remove this temporary file
File.Delete(newinput);
}
} }
// Now, if the file is a supported archive type, also run on all files within // Now, if the file is a supported archive type, also run on all files within