[RombaSharp, SabreTools] Better switch statements

This commit is contained in:
Matt Nadareski
2017-12-05 19:09:32 -08:00
parent 1025dd558e
commit 2dfb3f6e69
2 changed files with 223 additions and 291 deletions

View File

@@ -174,65 +174,6 @@ namespace RombaSharp
// Check all of the flag names and translate to arguments // Check all of the flag names and translate to arguments
switch (feat.Key) switch (feat.Key)
{ {
// Top-level features
case "Help":
// No-op as this should be caught
break;
case "Archive":
archive = true;
break;
case "Build":
build = true;
break;
case "Stats":
dbstats = true;
break;
case "Rescan Depots":
depotRescan = true;
break;
case "Diffdat":
diffdat = true;
break;
case "Dir2Dat":
dir2dat = true;
break;
case "Export":
export = true;
break;
case "Fixdat":
fixdat = true;
break;
case "Import":
import = true;
break;
case "Lookup":
lookup = true;
break;
case "Memstats":
memstats = true;
break;
case "Merge":
merge = true;
break;
case "Miss":
miss = true;
break;
case "Purge Backup":
purgeBackup = true;
break;
case "Purge Delete":
purgeDelete = true;
break;
case "Refresh DATs":
refreshDats = true;
break;
case "Progress":
progress = true;
break;
case "Shutdown":
shutdown = true;
break;
// User flags // User flags
case "copy": case "copy":
copy = true; copy = true;
@@ -257,133 +198,143 @@ namespace RombaSharp
} }
} }
// If a switch that requires a filename is set and no file is, show the help screen
if (inputs.Count == 0 && (archive || build || depotRescan || dir2dat || fixdat ||
import || lookup || merge || miss))
{
Globals.Logger.Error("This feature requires at least one input");
_help.OutputGenericHelp();
Globals.Logger.Close();
return;
}
// Now take care of each mode in succesion // Now take care of each mode in succesion
switch(feature)
// Adds ROM files from the specified directories to the ROM archive
if (archive)
{ {
InitArchive(inputs, onlyNeeded); case "Help":
} // No-op as this should be caught
break;
// For each specified DAT file it creates the torrentzip files // Adds ROM files from the specified directories to the ROM archive
else if (build) case "Archive":
{ if (inputs.Count == 0)
InitBuild(inputs, copy); {
} Globals.Logger.Error("This feature requires at least one input");
_help.OutputIndividualFeature(feature);
// Prints db stats break;
else if (dbstats) }
{ InitArchive(inputs, onlyNeeded);
DisplayDBStats(); break;
} // For each specified DAT file it creates the torrentzip files
case "Build":
// Rescan a specific depot if (inputs.Count == 0)
else if (depotRescan) {
{ Globals.Logger.Error("This feature requires at least one input");
foreach (string input in inputs) _help.OutputIndividualFeature(feature);
{ break;
Rescan(input); }
} InitBuild(inputs, copy);
} break;
// Prints db stats
// Creates a DAT file with those entries that are in new DAT case "Stats":
else if (diffdat) DisplayDBStats();
{ break;
InitDiffDat(newdat); // Rescan a specific depot
} case "Rescan Depots":
if (inputs.Count == 0)
// Creates a DAT file for the specified input directory {
else if (dir2dat) Globals.Logger.Error("This feature requires at least one input");
{ _help.OutputIndividualFeature(feature);
InitDir2Dat(inputs); break;
} }
foreach (string input in inputs)
// Export the database to file {
else if (export) Rescan(input);
{ }
ExportDatabase(); break;
} // Creates a DAT file with those entries that are in new DAT
case "Diffdat":
// For each specified DAT file it creates a fix DAT InitDiffDat(newdat);
else if (fixdat) break;
{ // Creates a DAT file for the specified input directory
InitFixdat(inputs); case "Dir2Dat":
} if (inputs.Count == 0)
{
// Import a CSV into the database Globals.Logger.Error("This feature requires at least one input");
else if (import) _help.OutputIndividualFeature(feature);
{ break;
InitImport(inputs); }
} InitDir2Dat(inputs);
break;
// For each specified hash it looks up any available information // Export the database to file
else if (lookup) case "Export":
{ ExportDatabase();
InitLookup(inputs); break;
} // For each specified DAT file it creates a fix DAT
case "Fixdat":
// Prints memory stats if (inputs.Count == 0)
else if (memstats) {
{ Globals.Logger.Error("This feature requires at least one input");
DisplayMemoryStats(); _help.OutputIndividualFeature(feature);
} break;
}
// Merges depots InitFixdat(inputs);
else if (merge) break;
{ // Import a CSV into the database
InitMerge(inputs, depotPath, onlyNeeded); case "Import":
} if (inputs.Count == 0)
{
// For each specified DAT file it creates a miss file and a have file Globals.Logger.Error("This feature requires at least one input");
else if (miss) _help.OutputIndividualFeature(feature);
{ break;
InitMiss(inputs); }
} InitImport(inputs);
break;
// Shows progress of the currently running command // For each specified hash it looks up any available information
else if (progress) case "Lookup":
{ if (inputs.Count == 0)
Globals.Logger.User("This feature is not used in RombaSharp: progress"); {
} Globals.Logger.Error("This feature requires at least one input");
_help.OutputIndividualFeature(feature);
// Moves DAT index entries for orphaned DATs break;
else if (purgeBackup) }
{ InitLookup(inputs);
PurgeBackup(logOnly); break;
} // Prints memory stats
case "Memstats":
// Deletes DAT index entries for orphaned DATs DisplayMemoryStats();
else if (purgeDelete) break;
{ // Merges depots
PurgeDelete(logOnly); case "Merge":
} if (inputs.Count == 0)
{
// Refreshes the DAT index from the files in the DAT master directory tree Globals.Logger.Error("This feature requires at least one input");
else if (refreshDats) _help.OutputIndividualFeature(feature);
{ break;
RefreshDatabase(); }
} InitMerge(inputs, depotPath, onlyNeeded);
break;
// Gracefully shuts down server // For each specified DAT file it creates a miss file and a have file
else if (shutdown) case "Miss":
{ if (inputs.Count == 0)
Globals.Logger.User("This feature is not used in RombaSharp: shutdown"); {
} Globals.Logger.Error("This feature requires at least one input");
_help.OutputIndividualFeature(feature);
// If nothing is set, show the help break;
else }
{ InitMiss(inputs);
_help.OutputGenericHelp(); break;
// Moves DAT index entries for orphaned DATs
case "Purge Backup":
PurgeBackup(logOnly);
break;
// Deletes DAT index entries for orphaned DATs
case "Purge Delete":
PurgeDelete(logOnly);
break;
// Refreshes the DAT index from the files in the DAT master directory tree
case "Refresh DATs":
RefreshDatabase();
break;
// Shows progress of the currently running command
case "Progress":
Globals.Logger.User("This feature is not used in RombaSharp: progress");
break;
case "Shutdown":
Globals.Logger.User("This feature is not used in RombaSharp: shutdown");
break;
default:
_help.OutputGenericHelp();
break;
} }
Globals.Logger.Close(); Globals.Logger.Close();

View File

@@ -71,16 +71,6 @@ namespace SabreTools
return; return;
} }
// Feature flags
bool datFromDir = false,
extract = false,
restore = false,
sort = false,
split = false,
stats = false,
update = false,
verify = false;
// User flags // User flags
bool addBlankFilesForEmptyFolder = false, bool addBlankFilesForEmptyFolder = false,
addFileDates = false, addFileDates = false,
@@ -187,38 +177,6 @@ namespace SabreTools
// Check all of the flag names and translate to arguments // Check all of the flag names and translate to arguments
switch (feat.Key) switch (feat.Key)
{ {
// Top-level features
case "Help":
// No-op as this should be caught
break;
case "DATFromDir":
datFromDir = true;
break;
case "Extract":
extract = true;
break;
case "Restore":
restore = true;
break;
case "Script":
// No-op as this should be caught
break;
case "Split":
split = true;
break;
case "Sort":
sort = true;
break;
case "Stats":
stats = true;
break;
case "Update":
update = true;
break;
case "Verify":
verify = true;
break;
// User flags // User flags
case "add-blank": case "add-blank":
addBlankFilesForEmptyFolder = true; addBlankFilesForEmptyFolder = true;
@@ -714,73 +672,96 @@ namespace SabreTools
} }
} }
// If a switch that requires a filename is set and no file is, show the help screen
if (inputs.Count == 0
&& (datFromDir || extract || restore || split || stats || update || verify))
{
Globals.Logger.Error("This feature requires at least one input");
_help.OutputIndividualFeature(feature);
Globals.Logger.Close();
return;
}
// Now take care of each mode in succesion // Now take care of each mode in succesion
switch (feature)
// Create a DAT from a directory or set of directories
if (datFromDir)
{ {
InitDatFromDir(inputs, datHeader, omitFromScan, removeDateFromAutomaticName, archivesAsFiles, chdsAsFiles, case "Help":
skipFileType, addBlankFilesForEmptyFolder, addFileDates, tempDir, outDir, copyFiles); // No-op as this should be caught
} break;
// Create a DAT from a directory or set of directories
// If we're in header extract and remove mode case "DATFromDir":
else if (extract) if (inputs.Count == 0)
{ {
InitExtractRemoveHeader(inputs, outDir, nostore); Globals.Logger.Error("This feature requires at least one input");
} _help.OutputIndividualFeature(feature);
break;
// If we're in header restore mode }
else if (restore) InitDatFromDir(inputs, datHeader, omitFromScan, removeDateFromAutomaticName, archivesAsFiles, chdsAsFiles,
{ skipFileType, addBlankFilesForEmptyFolder, addFileDates, tempDir, outDir, copyFiles);
InitReplaceHeader(inputs, outDir); break;
} // If we're in header extract and remove mode
case "Extract":
// If we're using the sorter if (inputs.Count == 0)
else if (sort) {
{ Globals.Logger.Error("This feature requires at least one input");
InitSort(datfiles, inputs, outDir, depot, quickScan, addFileDates, delete, inverse, _help.OutputIndividualFeature(feature);
outputFormat, datHeader.Romba, sevenzip, gz, rar, zip, updateDat, datHeader.Header, splitType, chdsAsFiles); break;
} }
InitExtractRemoveHeader(inputs, outDir, nostore);
// Split a DAT by the split type break;
else if (split) // If we're in header restore mode
{ case "Restore":
InitSplit(inputs, outDir, inplace, datHeader.DatFormat, splittingMode, exta, extb, shortname, basedat); if (inputs.Count == 0)
} {
Globals.Logger.Error("This feature requires at least one input");
// Get statistics on input files _help.OutputIndividualFeature(feature);
else if (stats) break;
{ }
InitStats(inputs, datHeader.FileName, outDir, filter.Single, showBaddumpColumn, showNodumpColumn, statDatFormat); InitReplaceHeader(inputs, outDir);
} break;
case "Script":
// Convert, update, merge, diff, and filter a DAT or folder of DATs // No-op as this should be caught
else if (update) break;
{ // If we're using the sorter
InitUpdate(inputs, basePaths, datHeader, updateMode, inplace, skip, removeDateFromAutomaticName, filter, case "Sort":
splitType, outDir, cleanGameNames, removeUnicode, descAsName); InitSort(datfiles, inputs, outDir, depot, quickScan, addFileDates, delete, inverse,
} outputFormat, datHeader.Romba, sevenzip, gz, rar, zip, updateDat, datHeader.Header, splitType, chdsAsFiles);
break;
// If we're using the verifier // Split a DAT by the split type
else if (verify) case "Split":
{ if (inputs.Count == 0)
InitVerify(datfiles, inputs, depot, hashOnly, quickScan, datHeader.Header, splitType, chdsAsFiles); {
} Globals.Logger.Error("This feature requires at least one input");
_help.OutputIndividualFeature(feature);
// If nothing is set, show the help break;
else }
{ InitSplit(inputs, outDir, inplace, datHeader.DatFormat, splittingMode, exta, extb, shortname, basedat);
_help.OutputGenericHelp(); break;
// Get statistics on input files
case "Stats":
if (inputs.Count == 0)
{
Globals.Logger.Error("This feature requires at least one input");
_help.OutputIndividualFeature(feature);
break;
}
InitStats(inputs, datHeader.FileName, outDir, filter.Single, showBaddumpColumn, showNodumpColumn, statDatFormat);
break;
// Convert, update, merge, diff, and filter a DAT or folder of DATs
case "Update":
if (inputs.Count == 0)
{
Globals.Logger.Error("This feature requires at least one input");
_help.OutputIndividualFeature(feature);
break;
}
InitUpdate(inputs, basePaths, datHeader, updateMode, inplace, skip, removeDateFromAutomaticName, filter,
splitType, outDir, cleanGameNames, removeUnicode, descAsName);
break;
// If we're using the verifier
case "Verify":
if (inputs.Count == 0)
{
Globals.Logger.Error("This feature requires at least one input");
_help.OutputIndividualFeature(feature);
break;
}
InitVerify(datfiles, inputs, depot, hashOnly, quickScan, datHeader.Header, splitType, chdsAsFiles);
break;
// If nothing is set, show the help
default:
_help.OutputGenericHelp();
break;
} }
Globals.Logger.Close(); Globals.Logger.Close();