Create and use base feature in Check

This commit is contained in:
Matt Nadareski
2025-10-06 16:06:23 -04:00
parent 896caec9cd
commit ee08bfe0fc
5 changed files with 143 additions and 147 deletions

View File

@@ -10,6 +10,7 @@
- Add placeholder command set creation
- Reduce unnecessary shared code
- Create and use main features for CLI and Check
- Create and use base feature in Check
### 3.4.2 (2025-09-30)

View File

@@ -0,0 +1,128 @@
using System;
using System.IO;
#if NET40
using System.Threading.Tasks;
#endif
using MPF.Frontend;
using SabreTools.RedumpLib.Data;
using SabreTools.RedumpLib.Web;
namespace MPF.Check.Features
{
internal abstract class BaseFeature : SabreTools.CommandLine.Feature
{
#region Properties
/// <summary>
/// Progrma-specific options
/// </summary>
public Program.CommandOptions CommandOptions { get; protected set; }
/// <summary>
/// User-defined options
/// </summary>
public Options Options { get; protected set; }
/// <summary>
/// Currently-selected system
/// </summary>
public RedumpSystem? System { get; protected set; }
#endregion
protected BaseFeature(string name, string[] flags, string description, string? detailed = null)
: base(name, flags, description, detailed)
{
CommandOptions = new Program.CommandOptions();
Options = new Options()
{
// Internal Program
InternalProgram = InternalProgram.NONE,
// Extra Dumping Options
ScanForProtection = false,
AddPlaceholders = true,
PullAllInformation = false,
AddFilenameSuffix = false,
OutputSubmissionJSON = false,
IncludeArtifacts = false,
CompressLogFiles = false,
DeleteUnnecessaryFiles = false,
CreateIRDAfterDumping = false,
// Protection Scanning Options
ScanArchivesForProtection = true,
IncludeDebugProtectionInformation = false,
HideDriveLetters = false,
// Redump Login Information
RetrieveMatchInformation = true,
RedumpUsername = null,
RedumpPassword = null,
};
}
/// <inheritdoc/>
public override bool Execute()
{
if (Options.InternalProgram == InternalProgram.NONE)
{
Program.DisplayHelp("A program name needs to be provided");
return false;
}
// Validate the supplied credentials
if (Options.RetrieveMatchInformation
&& !string.IsNullOrEmpty(Options.RedumpUsername)
&& !string.IsNullOrEmpty(Options.RedumpPassword))
{
bool? validated = RedumpClient.ValidateCredentials(Options.RedumpUsername!, Options.RedumpPassword!).GetAwaiter().GetResult();
string message = validated switch
{
true => "Redump username and password accepted!",
false => "Redump username and password denied!",
null => "An error occurred validating your credentials!",
};
Console.WriteLine(message);
}
// Loop through all the rest of the args
for (int i = 0; i < Inputs.Count; i++)
{
// Check for a file
if (!File.Exists(Inputs[i].Trim('"')))
{
Program.DisplayHelp($"{Inputs[i].Trim('"')} does not exist");
return false;
}
// Get the full file path
string filepath = Path.GetFullPath(Inputs[i].Trim('"'));
// Now populate an environment
Drive? drive = null;
if (!string.IsNullOrEmpty(CommandOptions.DevicePath))
drive = Drive.Create(null, CommandOptions.DevicePath!);
var env = new DumpEnvironment(Options,
filepath,
drive,
System,
internalProgram: null);
env.SetProcessor();
// Finally, attempt to do the output dance
var result = env.VerifyAndSaveDumpOutput(seedInfo: CommandOptions.Seed)
.ConfigureAwait(false).GetAwaiter().GetResult();
Console.WriteLine(result.Message);
}
return true;
}
/// <inheritdoc/>
public override bool VerifyInputs() => Inputs.Count > 0;
}
}

View File

@@ -6,7 +6,7 @@ using SabreTools.RedumpLib.Data;
namespace MPF.Check.Features
{
internal sealed class InteractiveFeature : SabreTools.CommandLine.Feature
internal sealed class InteractiveFeature : BaseFeature
{
#region Feature Definition
@@ -18,59 +18,13 @@ namespace MPF.Check.Features
#endregion
#region Properties
/// <summary>
/// Progrma-specific options
/// </summary>
public Program.CommandOptions CommandOptions { get; private set; }
/// <summary>
/// User-defined options
/// </summary>
public Options Options { get; }
/// <summary>
/// Currently-selected system
/// </summary>
public RedumpSystem? System { get; private set; }
#endregion
public InteractiveFeature()
: base(DisplayName, _flags, _description)
{
CommandOptions = new Program.CommandOptions();
Options = new Options()
{
// Internal Program
InternalProgram = InternalProgram.NONE,
// Extra Dumping Options
ScanForProtection = false,
AddPlaceholders = true,
PullAllInformation = false,
AddFilenameSuffix = false,
OutputSubmissionJSON = false,
IncludeArtifacts = false,
CompressLogFiles = false,
DeleteUnnecessaryFiles = false,
CreateIRDAfterDumping = false,
// Protection Scanning Options
ScanArchivesForProtection = true,
IncludeDebugProtectionInformation = false,
HideDriveLetters = false,
// Redump Login Information
RetrieveMatchInformation = true,
RedumpUsername = null,
RedumpPassword = null,
};
}
/// <inheritdoc/>
public override bool Execute()
public override bool ProcessArgs(string[] args, int index)
{
// Create return values
CommandOptions = new Program.CommandOptions();

View File

@@ -1,11 +1,15 @@
using System;
using System.IO;
using MPF.Frontend;
using SabreTools.CommandLine.Inputs;
using SabreTools.RedumpLib;
using SabreTools.RedumpLib.Data;
using SabreTools.RedumpLib.Web;
namespace MPF.Check.Features
{
internal sealed class MainFeature : SabreTools.CommandLine.Feature
internal sealed class MainFeature : BaseFeature
{
#region Feature Definition
@@ -71,20 +75,6 @@ namespace MPF.Check.Features
#endregion
#region Properties
/// <summary>
/// Progrma-specific options
/// </summary>
public Program.CommandOptions CommandOptions { get; private set; }
/// <summary>
/// User-defined options
/// </summary>
public Options Options { get; }
#endregion
public MainFeature()
: base(DisplayName, _flags, _description)
{
@@ -148,12 +138,11 @@ namespace MPF.Check.Features
if (args == null || args.Length == 0)
return true;
// Invalid index values are not processed
if (index < 0 || index >= args.Length)
return false;
// The first argument is the system type
System = Extensions.ToRedumpSystem(args[0].Trim('"'));
// Loop through the arguments and parse out values
for (; index < args.Length; index++)
for (index = 1; index < args.Length; index++)
{
// Use specific program
if (_useInput.ProcessInput(args, ref index))
@@ -248,9 +237,6 @@ namespace MPF.Check.Features
}
/// <inheritdoc/>
public override bool Execute() => true;
/// <inheritdoc/>
public override bool VerifyInputs() => true;
public override bool VerifyInputs() => Inputs.Count > 0;
}
}

View File

@@ -1,17 +1,14 @@
using System;
using System.Collections.Generic;
using System.IO;
#if NET40
using System.Threading.Tasks;
#endif
using MPF.Check.Features;
using MPF.Frontend;
using MPF.Frontend.Features;
using SabreTools.CommandLine;
using SabreTools.CommandLine.Features;
using SabreTools.CommandLine.Inputs;
using SabreTools.RedumpLib.Data;
using SabreTools.RedumpLib.Web;
namespace MPF.Check
{
@@ -81,12 +78,6 @@ namespace MPF.Check
return;
}
// Setup common outputs
Options options;
CommandOptions opts;
RedumpSystem? knownSystem;
int startIndex;
// Get the first argument as a feature flag
string featureName = args[0];
@@ -104,81 +95,17 @@ namespace MPF.Check
// Interactive Mode
case InteractiveFeature interactive:
startIndex = 1;
interactive.ProcessArgs(args, 0);
interactive.Execute();
opts = interactive.CommandOptions;
options = interactive.Options;
knownSystem = interactive.System;
break;
// Default Behavior
default:
startIndex = 0;
var mainFeature = new MainFeature();
mainFeature.ProcessArgs(args, 0);
opts = mainFeature.CommandOptions;
options = mainFeature.Options;
knownSystem = Extensions.ToRedumpSystem(featureName.Trim('"'));
args = [.. mainFeature.Inputs];
mainFeature.Execute();
break;
}
if (options.InternalProgram == InternalProgram.NONE)
{
DisplayHelp("A program name needs to be provided");
return;
}
// Validate the supplied credentials
if (options.RetrieveMatchInformation
&& !string.IsNullOrEmpty(options.RedumpUsername)
&& !string.IsNullOrEmpty(options.RedumpPassword))
{
bool? validated = RedumpClient.ValidateCredentials(options.RedumpUsername!, options.RedumpPassword!).GetAwaiter().GetResult();
string message = validated switch
{
true => "Redump username and password accepted!",
false => "Redump username and password denied!",
null => "An error occurred validating your credentials!",
};
Console.WriteLine(message);
}
// Loop through all the rest of the args
for (int i = startIndex; i < args.Length; i++)
{
// Check for a file
if (!File.Exists(args[i].Trim('"')))
{
DisplayHelp($"{args[i].Trim('"')} does not exist");
return;
}
// Get the full file path
string filepath = Path.GetFullPath(args[i].Trim('"'));
// Now populate an environment
Drive? drive = null;
if (!string.IsNullOrEmpty(opts.DevicePath))
drive = Drive.Create(null, opts.DevicePath!);
var env = new DumpEnvironment(options,
filepath,
drive,
knownSystem,
internalProgram: null);
env.SetProcessor();
// Finally, attempt to do the output dance
var result = env.VerifyAndSaveDumpOutput(seedInfo: opts.Seed)
.ConfigureAwait(false).GetAwaiter().GetResult();
Console.WriteLine(result.Message);
}
}
/// <summary>
@@ -235,7 +162,7 @@ namespace MPF.Check
/// Display help for MPF.Check
/// </summary>
/// <param name="error">Error string to prefix the help text with</param>
private static void DisplayHelp(string? error = null)
internal static void DisplayHelp(string? error = null)
{
if (error != null)
Console.WriteLine(error);