Files
SabreTools/RombaSharp/Program.cs

232 lines
7.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
2020-08-01 13:25:32 -07:00
using RombaSharp.Features;
2020-12-08 13:23:59 -08:00
using SabreTools.Core;
2020-12-07 13:57:26 -08:00
using SabreTools.Help;
using SabreTools.IO;
2020-12-07 14:29:45 -08:00
using SabreTools.Logging;
namespace RombaSharp
{
/// <summary>
/// Entry class for the RombaSharp application
/// </summary>
/// <remarks>
/// In the database, we want to enable "offline mode". That is, when a user does an operation
/// that needs to read from the depot themselves, if the depot folder cannot be found, the
/// user is prompted to reconnect the depot OR skip that depot entirely.
/// </remarks>
2020-08-01 13:25:32 -07:00
public class Program
{
#region Static Variables
/// <summary>
/// Help object that determines available functionality
/// </summary>
private static FeatureSet? _help;
/// <summary>
/// Logging object
/// </summary>
private static readonly Logger logger = new();
#endregion
/// <summary>
/// Entry class for the RombaSharp application
/// </summary>
public static void Main(string[] args)
{
// Perform initial setup and verification
LoggerImpl.SetFilename(Path.Combine(PathTool.GetRuntimeDirectory(), "logs", "romba.log"), true);
LoggerImpl.AppendPrefix = true;
LoggerImpl.LowestLogLevel = LogLevel.VERBOSE;
LoggerImpl.ThrowOnError = false;
LoggerImpl.Start();
// Create a new Help object for this program
2020-08-01 13:25:32 -07:00
_help = RetrieveHelp();
// Credits take precidence over all
if ((new List<string>(args)).Contains("--credits"))
{
FeatureSet.OutputCredits();
LoggerImpl.Close();
return;
}
// If there's no arguments, show help
if (args.Length == 0)
{
_help.OutputGenericHelp();
LoggerImpl.Close();
return;
}
// Get the first argument as a feature flag
string featureName = args[0];
// Verify that the flag is valid
if (!_help.TopLevelFlag(featureName))
{
logger.User($"'{featureName}' is not valid feature flag");
_help.OutputIndividualFeature(featureName);
LoggerImpl.Close();
return;
}
// Get the proper name for the feature
featureName = _help.GetFeatureName(featureName);
// Get the associated feature
BaseFeature? feature = _help[featureName] as BaseFeature;
// If we had the help feature first
2020-08-01 13:25:32 -07:00
if (featureName == DisplayHelp.Value || featureName == DisplayHelpDetailed.Value)
{
feature!.ProcessArgs(args, _help);
LoggerImpl.Close();
return;
}
// Now verify that all other flags are valid
if (!feature!.ProcessArgs(args, _help))
{
LoggerImpl.Close();
return;
}
2021-02-03 11:10:19 -08:00
// Set the new log level based on settings
LoggerImpl.LowestLogLevel = feature.LogLevel;
// If output is being redirected or we are in script mode, don't allow clear screens
if (!Console.IsOutputRedirected && feature.ScriptMode)
{
Console.Clear();
Globals.SetConsoleHeader("SabreTools");
2021-02-03 11:10:19 -08:00
}
// Now process the current feature
Dictionary<string, Feature?> features = _help.GetEnabledFeatures();
2021-03-19 20:52:11 -07:00
bool success = false;
switch (featureName)
{
2020-08-01 13:25:32 -07:00
case DisplayHelpDetailed.Value:
case DisplayHelp.Value:
// No-op as this should be caught
break;
// Require input verification
2020-08-01 13:25:32 -07:00
case Archive.Value:
case Build.Value:
2020-08-01 13:25:32 -07:00
case DatStats.Value:
case Fixdat.Value:
case Import.Value:
case Lookup.Value:
case Merge.Value:
case Miss.Value:
case RescanDepots.Value:
VerifyInputs(feature.Inputs, featureName);
2021-03-19 20:52:11 -07:00
success = feature.ProcessFeatures(features);
break;
// Requires no input verification
2020-08-01 13:25:32 -07:00
case Cancel.Value:
case DbStats.Value:
case Diffdat.Value:
case Dir2Dat.Value:
case EDiffdat.Value:
case Export.Value:
case Memstats.Value:
case Progress.Value:
case PurgeBackup.Value:
case PurgeDelete.Value:
case RefreshDats.Value:
case Shutdown.Value:
case Features.Version.Value:
2021-03-19 20:52:11 -07:00
success = feature.ProcessFeatures(features);
break;
// If nothing is set, show the help
default:
_help.OutputGenericHelp();
break;
}
2021-03-19 20:52:11 -07:00
// If the feature failed, output help
if (!success)
{
logger.Error("An error occurred during processing!");
_help.OutputIndividualFeature(featureName);
}
LoggerImpl.Close();
return;
}
2020-08-01 13:25:32 -07:00
/// <summary>
/// Generate a Help object for this program
/// </summary>
/// <returns>Populated Help object</returns>
2020-12-08 17:05:08 -08:00
private static FeatureSet RetrieveHelp()
2020-08-01 13:25:32 -07:00
{
// Create and add the header to the Help object
string barrier = "-----------------------------------------";
List<string> helpHeader = new()
2020-08-01 13:25:32 -07:00
{
"RombaSharp - C# port of the Romba rom management tool",
barrier,
"Usage: RombaSharp [option] [filename|dirname] ...",
string.Empty
};
// Create the base help object with header
FeatureSet help = new(helpHeader);
2020-08-01 13:25:32 -07:00
// Add all of the features
help.Add(new DisplayHelp());
help.Add(new DisplayHelpDetailed());
help.Add(new Archive());
help.Add(new Build());
2020-08-01 13:25:32 -07:00
help.Add(new Cancel());
help.Add(new DatStats());
help.Add(new DbStats());
help.Add(new Diffdat());
help.Add(new Dir2Dat());
help.Add(new EDiffdat());
help.Add(new Export());
help.Add(new Fixdat());
help.Add(new Import());
help.Add(new Lookup());
help.Add(new Memstats());
help.Add(new Merge());
help.Add(new Miss());
help.Add(new PurgeBackup());
help.Add(new PurgeDelete());
help.Add(new RefreshDats());
help.Add(new RescanDepots());
help.Add(new Progress());
help.Add(new Shutdown());
help.Add(new Features.Version());
return help;
}
/// <summary>
/// Verify that there are inputs, show help otherwise
/// </summary>
/// <param name="inputs">List of inputs</param>
/// <param name="feature">Name of the current feature</param>
private static void VerifyInputs(List<string> inputs, string feature)
{
if (inputs.Count == 0)
{
logger.Error("This feature requires at least one input");
_help?.OutputIndividualFeature(feature);
Environment.Exit(0);
}
}
}
}