[ALL] Code cleanup, merge DFD and DFDP flags

This commit is contained in:
Matt Nadareski
2016-09-13 12:40:09 -07:00
parent 9574765b3c
commit bb53d3c3eb
6 changed files with 47 additions and 291 deletions

View File

@@ -110,31 +110,7 @@ namespace SabreTools.Helper
helptext.Add(" -system= System name (system only)");
helptext.Add(" -source= Source name (source only)");
helptext.Add(" -url= URL (source only)");
helptext.Add(" -d, --dfd Create a DAT from each input directory");
helptext.Add(" -nm, --noMD5 Don't include MD5 in output");
helptext.Add(" -ns, --noSHA1 Don't include SHA1 in output");
helptext.Add(" -b, --bare Don't include date in file name");
helptext.Add(" -u, --unzip Force unzipping in created DAT");
helptext.Add(" -f, --files Treat archives as files");
helptext.Add(" -oc, --output-cmp Output in CMP format");
helptext.Add(" -om, --output-miss Output in Missfile format");
helptext.Add(" -omd5, --output-md5 Output in MD5 format");
helptext.Add(" -or, --output-rc Output in RomCenter format");
helptext.Add(" -os, --output-sd Output in SabreDAT format");
helptext.Add(" -osfv, --ouput-sfv Output in SFV format");
helptext.Add(" -osha1, --output-sha1 Output in SHA-1 format");
helptext.Add(" -ox, --output-xml Output in Logiqx XML format");
helptext.Add(" -gz, --gz-files Allow reading of GZIP files as archives");
helptext.Add(" -ro, --romba Read files from a Romba input");
helptext.Add(" -f=, --filename= Set the external name of the DAT");
helptext.Add(" -n=, --name= Set the internal name of the DAT");
helptext.Add(" -de=, --desc= Set the description of the DAT");
helptext.Add(" -c=, --cat= Set the category of the DAT");
helptext.Add(" -v=, --version= Set the version of the DAT");
helptext.Add(" -au=, --author= Set the author of the DAT");
helptext.Add(" -sd, --superdat Enable SuperDAT creation");
helptext.Add(" -t=, --temp= Set the temporary directory to use");
helptext.Add(" -dp, --dfdp Create a DAT using multithreading");
helptext.Add(" -d, --dfd Create a DAT from an input directory");
helptext.Add(" -nm, --noMD5 Don't include MD5 in output");
helptext.Add(" -ns, --noSHA1 Don't include SHA1 in output");
helptext.Add(" -b, --bare Don't include date in file name");

View File

@@ -543,7 +543,7 @@ namespace SabreTools.Helper
else
{
string archiveFileName = Path.Combine(_outdir, found.Machine.Name + ".zip");
FileTools.CopyFileBetweenManagedArchives(input, archiveFileName, rom.Name, found.Name, _logger);
FileTools.CopyFileBetweenArchives(input, archiveFileName, rom.Name, found.Name, _logger);
}
}
}

View File

@@ -234,97 +234,12 @@ Options:
Optionally, a temp folder can be supplied in the case the default temp directory
(inside the running folder) is not preferred. This is used for any operations
that require an archive to be extracted.
-dp, --dfdp Create a DAT from each input directory using parallel code
Create a DAT file from an input directory or set of files. By default, this will
output a DAT named based on the input directory and the current date. It will also
treat all archives as possible games and add all three hashes for each file.
-nm, --noMD5 Don't include MD5 in output
This allows the user to skip calculating the MD5 for each of the files which will
speed up the creation of the DAT.
-ns, --noSHA1 Don't include SHA1 in output
This allows the user to skip calculating the SHA-1 for each of the files which will
speed up the creation of the DAT.
-b, --bare Don't include date in file name
Normally, the DAT will be created with the date in the file name. This flag removes
that but keeps the date tag intact.
-u, --unzip Force unzipping in created DAT
This sets the 'forcepacking="unzip"' flag in the outputted DAT. When used with a
file manager that allows for it, this will force the outputted files to be in
subdirectories instead of archives.
-f, --files Treat archives as files
Instead of trying to enumerate the files within archives, treat the archives as
files themselves. This is good for uncompressed sets that include archives that
should be read as-is.
-oc, --output-cmp Output in CMP format
Add outputting the created DAT to clrmamepro format
-om, --output-miss Output in Missfile format
Add outputting the created DAT to GoodTools miss format
-omd5, --output-md5 Output in MD5 format
Add outputting the created DAT to MD5 format
-or, --output-rc Output in RomCenter format
Add outputting the created DAT to RomCenter format
-os, --output-sd Output in SabreDAT format
Add outputting the created DAT to SabreDAT XML format
-osfv, --ouput-sfv Output in SFV format
Add outputting the created DAT to SFV format
-osha1, --output-sha1 Output in SHA-1 format
Add outputting the created DAT to SHA1 format
-ox, --output-xml Output in Logiqx XML format (default)
Add outputting the created DAT to Logiqx XML format
-gz, --gz-files Allow reading of GZIP files as archives
Since GZip files are not commonly used for file storage, this flag allows for
any GZip archives to have their contents hashed instead.
-ro, --romba Read files from a Romba input
Allow for reading of GZipped files as if they were from a Romba depot. This
implies that the files will be in the TorrentGZ format as well, including
naming convention.
-f=, --filename= Set the external name of the DAT
Set the base filename for the output DAT(s) [default is folder name plus date]
-n=, --name= Set the internal name of the DAT
Set the internal name for the output DAT(s) [default is folder name plus date]
-de=, --desc= Set the description of the DAT
Set the description for the output DAT(s) [default is the folder name]
-c=, --cat= Set the category of the DAT
Set the category for the output DAT(s) [default is blank]
-v=, --version= Set the version of the DAT
Set the version for the output DAT(s) [default is blank]
-au=, --author= Set the author of the DAT
Set the author for the output DAT(s) [default is blank]
-sd, --superdat Enable SuperDAT creation
Set the type flag to "SuperDAT" for the output DAT as well as preserving the
directory structure of the inputted folder, if applicable
-t=, --temp= Set the name of the temporary directory
Optionally, a temp folder can be supplied in the case the default temp directory
(inside the running folder) is not preferred. This is used for any operations
that require an archive to be extracted.
-mt={4} Amount of threads to use
Optionally, set the number of threads to use for the multithreaded operations.
The default is 4 threads; -1 means unlimited threads created.
The default is 4 threads; -1 means unlimited threads created. If the user specifies
that only 1 thread is to be used, it defaults to the original, serial implementation
of the DFD code.
-es, --ext-split Split a DAT by two file extensions
For a DAT, or set of DATs, allow for splitting based on a list of input extensions.

View File

@@ -422,80 +422,6 @@ namespace SabreTools.Helper
#region Archive-to-Archive Handling
/// <summary>
/// Attempt to copy a file between archives
/// </summary>
/// <param name="inputArchive">Source archive name</param>
/// <param name="outputArchive">Destination archive name</param>
/// <param name="sourceEntryName">Input entry name</param>
/// <param name="destEntryName">Output entry name</param>
/// <param name="logger">Logger object for file and console output</param>
/// <returns>True if the copy was a success, false otherwise</returns>
public static bool CopyFileBetweenArchives(string inputArchive, string outputArchive,
string sourceEntryName, string destEntryName, Logger logger)
{
bool success = false;
// First get the archive types
ArchiveType? iat = GetCurrentArchiveType(inputArchive, logger);
ArchiveType? oat = (File.Exists(outputArchive) ? GetCurrentArchiveType(outputArchive, logger) : ArchiveType.Zip);
// If we got back null (or the output is not a Zipfile), then it's not an archive, so we we return
if (iat == null || (oat == null || oat != ArchiveType.Zip) || inputArchive == outputArchive)
{
return success;
}
IReader reader = null;
ZipArchive outarchive = null;
try
{
reader = ReaderFactory.Open(File.OpenRead(inputArchive));
if (iat == ArchiveType.Zip || iat == ArchiveType.SevenZip || iat == ArchiveType.Rar)
{
while (reader.MoveToNextEntry())
{
logger.Log("Current entry name: '" + reader.Entry.Key + "'");
if (reader.Entry != null && reader.Entry.Key.Contains(sourceEntryName))
{
if (!File.Exists(outputArchive))
{
outarchive = ZipFile.Open(outputArchive, ZipArchiveMode.Create);
}
else
{
outarchive = ZipFile.Open(outputArchive, ZipArchiveMode.Update);
}
if (outarchive.Mode == ZipArchiveMode.Create || outarchive.GetEntry(destEntryName) == null)
{
ZipArchiveEntry iae = outarchive.CreateEntry(destEntryName, CompressionLevel.Optimal) as ZipArchiveEntry;
using (Stream iaestream = iae.Open())
{
reader.WriteEntryTo(iaestream);
}
}
success = true;
}
}
}
}
catch (Exception ex)
{
logger.Error(ex.ToString());
success = false;
}
finally
{
reader?.Dispose();
outarchive?.Dispose();
}
return success;
}
/// <summary>
/// Attempt to copy a file between archives using SharpCompress
/// </summary>
@@ -505,7 +431,7 @@ namespace SabreTools.Helper
/// <param name="destEntryName">Output entry name</param>
/// <param name="logger">Logger object for file and console output</param>
/// <returns>True if the copy was a success, false otherwise</returns>
public static bool CopyFileBetweenManagedArchives(string inputArchive, string outputArchive,
public static bool CopyFileBetweenArchives(string inputArchive, string outputArchive,
string sourceEntryName, string destEntryName, Logger logger)
{
bool success = false;

View File

@@ -82,72 +82,6 @@ namespace SabreTools
return ss.Convert();
}
/// <summary>
/// Wrap creating a DAT file from files or a directory
/// </summary>
/// <param name="input">List of innput filenames</param>
/// <param name="filename">New filename</param>
/// <param name="name">New name</param>
/// <param name="description">New description</param>
/// <param name="category">New category</param>
/// <param name="version">New version</param>
/// <param name="author">New author</param>
/// <param name="forceunpack">True to set forcepacking="unzip" on the created file, false otherwise</param>
/// <param name="outputFormat">OutputFormat to be used for outputting the DAT</param>
/// <param name="romba">True to enable reading a directory like a Romba depot, false otherwise</param>
/// <param name="superdat">True to enable SuperDAT-style reading, false otherwise</param>
/// <param name="noMD5">True to disable getting MD5 hash, false otherwise</param>
/// <param name="noSHA1">True to disable getting SHA-1 hash, false otherwise</param>
/// <param name="bare">True if the date should be omitted from the DAT, false otherwise</param>
/// <param name="archivesAsFiles">True if archives should be treated as files, false otherwise</param>
/// <param name="enableGzip">True if GZIP archives should be treated as files, false otherwise</param>
/// <param name="tempDir">Name of the directory to create a temp folder in (blank is current directory</param>
private static void InitDatFromDir(List<string> inputs,
string filename,
string name,
string description,
string category,
string version,
string author,
bool forceunpack,
OutputFormat outputFormat,
bool romba,
bool superdat,
bool noMD5,
bool noSHA1,
bool bare,
bool archivesAsFiles,
bool enableGzip,
string tempDir)
{
// Create a new DATFromDir object and process the inputs
Dat datdata = new Dat
{
FileName = filename,
Name = name,
Description = description,
Category = category,
Version = version,
Date = DateTime.Now.ToString("yyyy-MM-dd"),
Author = author,
ForcePacking = (forceunpack ? ForcePacking.Unzip : ForcePacking.None),
OutputFormat = (outputFormat == 0 ? OutputFormat.Xml : outputFormat),
Romba = romba,
Type = (superdat ? "SuperDAT" : ""),
Files = new Dictionary<string, List<Rom>>(),
};
DATFromDir dfd = new DATFromDir(inputs, datdata, noMD5, noSHA1, bare, archivesAsFiles, enableGzip, tempDir, _logger);
bool success = dfd.Start();
// If we failed, show the help
if (!success)
{
Console.WriteLine();
Build.Help();
}
}
/// <summary>
/// Wrap creating a DAT file from files or a directory in parallel
/// </summary>
@@ -169,7 +103,7 @@ namespace SabreTools
/// <param name="enableGzip">True if GZIP archives should be treated as files, false otherwise</param>
/// <param name="tempDir">Name of the directory to create a temp folder in (blank is current directory</param>
/// <param name="maxDegreeOfParallelism">Integer representing the maximum amount of parallelization to be used</param>
private static void InitDatFromDirParallel(List<string> inputs,
private static void InitDatFromDir(List<string> inputs,
string filename,
string name,
string description,
@@ -204,30 +138,48 @@ namespace SabreTools
Type = (superdat ? "SuperDAT" : ""),
};
// For each input directory, create a DAT
foreach (string path in inputs)
// If the user has only set a single thread, use the original version
if (maxDegreeOfParallelism == 1)
{
if (Directory.Exists(path))
DATFromDir dfd = new DATFromDir(inputs, basedat, noMD5, noSHA1, bare, archivesAsFiles, enableGzip, tempDir, _logger);
bool success = dfd.Start();
// If we failed, show the help
if (!success)
{
// Clone the base Dat for information
Dat datdata = (Dat)basedat.Clone();
datdata.Files = new Dictionary<string, List<Rom>>();
string basePath = Path.GetFullPath(path);
DATFromDirParallel dfd = new DATFromDirParallel(basePath, datdata, noMD5, noSHA1, bare, archivesAsFiles, enableGzip, tempDir, maxDegreeOfParallelism, _logger);
bool success = dfd.Start();
// If it was a success, write the DAT out
if (success)
Console.WriteLine();
Build.Help();
}
}
// Otherwise, make full use of the threads
else
{
// For each input directory, create a DAT
foreach (string path in inputs)
{
if (Directory.Exists(path))
{
DatTools.WriteDatfile(dfd.DatData, "", _logger);
}
// Clone the base Dat for information
Dat datdata = (Dat)basedat.Clone();
datdata.Files = new Dictionary<string, List<Rom>>();
// Otherwise, show the help
else
{
Console.WriteLine();
Build.Help();
string basePath = Path.GetFullPath(path);
DATFromDirParallel dfd = new DATFromDirParallel(basePath, datdata, noMD5, noSHA1, bare, archivesAsFiles, enableGzip, tempDir, maxDegreeOfParallelism, _logger);
bool success = dfd.Start();
// If it was a success, write the DAT out
if (success)
{
DatTools.WriteDatfile(dfd.DatData, "", _logger);
}
// Otherwise, show the help
else
{
Console.WriteLine();
Build.Help();
}
}
}
}

View File

@@ -81,7 +81,6 @@ namespace SabreTools
bare = false,
clean = false,
datfromdir = false,
datfromdirparallel = false,
datprefix = false,
dedup = false,
enableGzip = false,
@@ -238,11 +237,6 @@ namespace SabreTools
case "--diff-nd":
diffMode |= DiffMode.NoDupes;
break;
case "-dp":
case "--d2dp":
case "--dfdp":
datfromdirparallel = true;
break;
case "-es":
case "--ext-split":
extsplit = true;
@@ -624,7 +618,7 @@ namespace SabreTools
}
// If more than one switch is enabled, show the help screen
if (!(add ^ datfromdir ^ datfromdirparallel ^ extsplit ^ generate ^ genall ^ hashsplit ^ headerer ^ import ^ listsrc ^
if (!(add ^ datfromdir ^ extsplit ^ generate ^ genall ^ hashsplit ^ headerer ^ import ^ listsrc ^
listsys ^ (merge || diffMode != 0 || update || outputFormat != 0 || tsv != null|| trim) ^ rem ^ stats ^ typesplit))
{
_logger.Error("Only one feature switch is allowed at a time");
@@ -634,7 +628,7 @@ namespace SabreTools
}
// If a switch that requires a filename is set and no file is, show the help screen
if (inputs.Count == 0 && (datfromdir || datfromdirparallel || extsplit || hashsplit || headerer
if (inputs.Count == 0 && (datfromdir || extsplit || hashsplit || headerer
|| (merge || diffMode != 0 || update || outputFormat != 0 || tsv != null) || stats || trim || typesplit))
{
_logger.Error("This feature requires at least one input");
@@ -666,13 +660,6 @@ namespace SabreTools
else if (datfromdir)
{
InitDatFromDir(inputs, filename, name, description, category, version, author, forceunpack, outputFormat,
romba, superdat, noMD5, noSHA1, bare, archivesAsFiles, enableGzip, tempdir);
}
// Create a DAT from a directory or set of directories in parallel
else if (datfromdirparallel)
{
InitDatFromDirParallel(inputs, filename, name, description, category, version, author, forceunpack, outputFormat,
romba, superdat, noMD5, noSHA1, bare, archivesAsFiles, enableGzip, tempdir, maxParallelism);
}