Decouple execution contexts from Options class

This commit is contained in:
Matt Nadareski
2024-05-28 14:07:18 -04:00
parent 7b832049e8
commit 3137a543a7
8 changed files with 126 additions and 62 deletions

View File

@@ -153,6 +153,7 @@
- Move ResultEventArgs to Frontend
- Remove unused reporter delegate
- Move StringEventArgs to Frontend
- Decouple execution contexts from Options class
### 3.1.9a (2024-05-21)

View File

@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using MPF.Core;
using SabreTools.RedumpLib.Data;
namespace MPF.ExecutionContexts.Aaru
@@ -117,7 +116,7 @@ namespace MPF.ExecutionContexts.Aaru
public ExecutionContext(string? parameters) : base(parameters) { }
/// <inheritdoc/>
public ExecutionContext(RedumpSystem? system, MediaType? type, string? drivePath, string filename, int? driveSpeed, Options options)
public ExecutionContext(RedumpSystem? system, MediaType? type, string? drivePath, string filename, int? driveSpeed, Dictionary<string, string?> options)
: base(system, type, drivePath, filename, driveSpeed, options)
{
}
@@ -1190,7 +1189,7 @@ namespace MPF.ExecutionContexts.Aaru
}
/// <inheritdoc/>
protected override void SetDefaultParameters(string? drivePath, string filename, int? driveSpeed, Options options)
protected override void SetDefaultParameters(string? drivePath, string filename, int? driveSpeed, Dictionary<string, string?> options)
{
BaseCommand = $"{CommandStrings.MediaPrefixLong} {CommandStrings.MediaDump}";
@@ -1209,21 +1208,22 @@ namespace MPF.ExecutionContexts.Aaru
return;
// Set retry count
if (options.AaruRereadCount > 0)
int rereadCount = GetInt32Setting(options, "AaruRereadCount", 5);
if (rereadCount > 0)
{
this[FlagStrings.RetryPassesLong] = true;
RetryPassesValue = (short)options.AaruRereadCount;
RetryPassesValue = (short)rereadCount;
}
// Set user-defined options
if (options.AaruEnableDebug)
this[FlagStrings.DebugLong] = options.AaruEnableDebug;
if (options.AaruEnableVerbose)
this[FlagStrings.VerboseLong] = options.AaruEnableVerbose;
if (options.AaruForceDumping)
this[FlagStrings.ForceLong] = options.AaruForceDumping;
if (options.AaruStripPersonalData)
this[FlagStrings.PrivateLong] = options.AaruStripPersonalData;
if (GetBooleanSetting(options, "AaruEnableDebug", false))
this[FlagStrings.DebugLong] = true;
if (GetBooleanSetting(options, "AaruEnableVerbose", false))
this[FlagStrings.VerboseLong] = true;
if (GetBooleanSetting(options, "AaruForceDumping", true))
this[FlagStrings.ForceLong] = true;
if (GetBooleanSetting(options, "AaruStripPersonalData", false))
this[FlagStrings.PrivateLong] = true;
// TODO: Look at dump-media formats and the like and see what options there are there to fill in defaults
// Now sort based on disc type

View File

@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text.RegularExpressions;
using MPF.Core;
using SabreTools.RedumpLib.Data;
namespace MPF.ExecutionContexts
@@ -111,8 +110,8 @@ namespace MPF.ExecutionContexts
/// <param name="drivePath">Drive path to use</param>
/// <param name="filename">Filename to use</param>
/// <param name="driveSpeed">Drive speed to use</param>
/// <param name="options">Options object containing all settings that may be used for setting parameters</param>
public BaseExecutionContext(RedumpSystem? system, MediaType? type, string? drivePath, string filename, int? driveSpeed, Options options)
/// <param name="options">Dictionary object containing all settings that may be used for setting parameters</param>
public BaseExecutionContext(RedumpSystem? system, MediaType? type, string? drivePath, string filename, int? driveSpeed, Dictionary<string, string?> options)
{
this.System = system;
this.Type = type;
@@ -169,8 +168,8 @@ namespace MPF.ExecutionContexts
/// <param name="drivePath">Drive path to use</param>
/// <param name="filename">Filename to use</param>
/// <param name="driveSpeed">Drive speed to use</param>
/// <param name="options">Options object containing all settings that may be used for setting parameters</param>
protected abstract void SetDefaultParameters(string? drivePath, string filename, int? driveSpeed, Options options);
/// <param name="options">Dictionary containing all settings that may be used for setting parameters</param>
protected abstract void SetDefaultParameters(string? drivePath, string filename, int? driveSpeed, Dictionary<string, string?> options);
/// <summary>
/// Scan a possible parameter string and populate whatever possible
@@ -226,6 +225,69 @@ namespace MPF.ExecutionContexts
#endregion
#region Option Processing
/// <summary>
/// Get a Boolean setting from a settings, dictionary
/// </summary>
/// <param name="settings">Dictionary representing the settings</param>
/// <param name="key">Setting key to get a value for</param>
/// <param name="defaultValue">Default value to return if no value is found</param>
/// <returns>Setting value if possible, default value otherwise</returns>
protected static bool GetBooleanSetting(Dictionary<string, string?> settings, string key, bool defaultValue)
{
if (settings.ContainsKey(key))
{
if (bool.TryParse(settings[key], out bool value))
return value;
else
return defaultValue;
}
else
{
return defaultValue;
}
}
/// <summary>
/// Get an Int32 setting from a settings, dictionary
/// </summary>
/// <param name="settings">Dictionary representing the settings</param>
/// <param name="key">Setting key to get a value for</param>
/// <param name="defaultValue">Default value to return if no value is found</param>
/// <returns>Setting value if possible, default value otherwise</returns>
protected static int GetInt32Setting(Dictionary<string, string?> settings, string key, int defaultValue)
{
if (settings.ContainsKey(key))
{
if (int.TryParse(settings[key], out int value))
return value;
else
return defaultValue;
}
else
{
return defaultValue;
}
}
/// <summary>
/// Get a String setting from a settings, dictionary
/// </summary>
/// <param name="settings">Dictionary representing the settings</param>
/// <param name="key">Setting key to get a value for</param>
/// <param name="defaultValue">Default value to return if no value is found</param>
/// <returns>Setting value if possible, default value otherwise</returns>
protected static string? GetStringSetting(Dictionary<string, string?> settings, string key, string? defaultValue)
{
if (settings.ContainsKey(key))
return settings[key];
else
return defaultValue;
}
#endregion
#region Parameter Parsing
/// <summary>

View File

@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using MPF.Core;
using SabreTools.RedumpLib.Data;
namespace MPF.ExecutionContexts.DiscImageCreator
@@ -167,7 +166,7 @@ namespace MPF.ExecutionContexts.DiscImageCreator
public ExecutionContext(string? parameters) : base(parameters) { }
/// <inheritdoc/>
public ExecutionContext(RedumpSystem? system, MediaType? type, string? drivePath, string filename, int? driveSpeed, Options options)
public ExecutionContext(RedumpSystem? system, MediaType? type, string? drivePath, string filename, int? driveSpeed, Dictionary<string, string?> options)
: base(system, type, drivePath, filename, driveSpeed, options)
{
}
@@ -944,7 +943,7 @@ namespace MPF.ExecutionContexts.DiscImageCreator
}
/// <inheritdoc/>
protected override void SetDefaultParameters(string? drivePath, string filename, int? driveSpeed, Options options)
protected override void SetDefaultParameters(string? drivePath, string filename, int? driveSpeed, Dictionary<string, string?> options)
{
SetBaseCommand(this.System, this.Type);
@@ -958,23 +957,25 @@ namespace MPF.ExecutionContexts.DiscImageCreator
return;
// Set disable beep flag, if needed
if (options.DICQuietMode)
if (GetBooleanSetting(options, "DICQuietMode", false))
this[FlagStrings.DisableBeep] = true;
// Set the C2 reread count
C2OpcodeValue[0] = options.DICRereadCount switch
int cdRereadCount = GetInt32Setting(options, "DICRereadCount", 20);
C2OpcodeValue[0] = cdRereadCount switch
{
-1 => null,
0 => 20,
_ => options.DICRereadCount,
_ => cdRereadCount,
};
// Set the DVD/HD-DVD/BD reread count
DVDRereadValue = options.DICDVDRereadCount switch
int dvdRereadCount = GetInt32Setting(options, "DICDVDRereadCount", 10);
DVDRereadValue = dvdRereadCount switch
{
-1 => null,
0 => 10,
_ => options.DICDVDRereadCount,
_ => dvdRereadCount,
};
// Now sort based on disc type
@@ -982,9 +983,9 @@ namespace MPF.ExecutionContexts.DiscImageCreator
{
case MediaType.CDROM:
this[FlagStrings.C2Opcode] = true;
this[FlagStrings.MultiSectorRead] = options.DICMultiSectorRead;
if (options.DICMultiSectorRead)
this.MultiSectorReadValue = options.DICMultiSectorReadValue;
this[FlagStrings.MultiSectorRead] = GetBooleanSetting(options, "DICMultiSectorRead", false);
if (this[FlagStrings.MultiSectorRead] == true)
this.MultiSectorReadValue = GetInt32Setting(options, "DICMultiSectorReadValue", 0);
switch (this.System)
{
@@ -992,8 +993,8 @@ namespace MPF.ExecutionContexts.DiscImageCreator
case RedumpSystem.IBMPCcompatible:
this[FlagStrings.NoFixSubQSecuROM] = true;
this[FlagStrings.ScanFileProtect] = true;
this[FlagStrings.ScanSectorProtect] = options.DICParanoidMode;
this[FlagStrings.SubchannelReadLevel] = options.DICParanoidMode;
this[FlagStrings.ScanSectorProtect] = GetBooleanSetting(options, "DICParanoidMode", false);
this[FlagStrings.SubchannelReadLevel] = GetBooleanSetting(options, "DICParanoidMode", false);
if (this[FlagStrings.SubchannelReadLevel] == true)
SubchannelReadLevelValue = 2;
@@ -1015,15 +1016,15 @@ namespace MPF.ExecutionContexts.DiscImageCreator
}
break;
case MediaType.DVD:
this[FlagStrings.CopyrightManagementInformation] = options.DICUseCMIFlag;
this[FlagStrings.ScanFileProtect] = options.DICParanoidMode;
this[FlagStrings.CopyrightManagementInformation] = GetBooleanSetting(options, "DICUseCMIFlag", false);
this[FlagStrings.ScanFileProtect] = GetBooleanSetting(options, "DICParanoidMode", false);
this[FlagStrings.DVDReread] = true;
break;
case MediaType.GDROM:
this[FlagStrings.C2Opcode] = true;
break;
case MediaType.HDDVD:
this[FlagStrings.CopyrightManagementInformation] = options.DICUseCMIFlag;
this[FlagStrings.CopyrightManagementInformation] = GetBooleanSetting(options, "DICUseCMIFlag", false);
this[FlagStrings.DVDReread] = true;
break;
case MediaType.BluRay:

View File

@@ -25,10 +25,6 @@
<InternalsVisibleTo Include="MPF.Test" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MPF.Core\MPF.Core.csproj" />
</ItemGroup>
<!-- Support for old .NET versions -->
<ItemGroup Condition="$(TargetFramework.StartsWith(`net2`)) OR $(TargetFramework.StartsWith(`net3`)) OR $(TargetFramework.StartsWith(`net40`))">
<PackageReference Include="MinAsyncBridge" Version="0.12.4" />

View File

@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using MPF.Core;
using SabreTools.RedumpLib.Data;
namespace MPF.ExecutionContexts.Redumper
@@ -159,7 +158,7 @@ namespace MPF.ExecutionContexts.Redumper
public ExecutionContext(string? parameters) : base(parameters) { }
/// <inheritdoc/>
public ExecutionContext(RedumpSystem? system, MediaType? type, string? drivePath, string filename, int? driveSpeed, Options options)
public ExecutionContext(RedumpSystem? system, MediaType? type, string? drivePath, string filename, int? driveSpeed, Dictionary<string, string?> options)
: base(system, type, drivePath, filename, driveSpeed, options)
{
}
@@ -538,7 +537,7 @@ namespace MPF.ExecutionContexts.Redumper
}
/// <inheritdoc/>
protected override void SetDefaultParameters(string? drivePath, string filename, int? driveSpeed, Options options)
protected override void SetDefaultParameters(string? drivePath, string filename, int? driveSpeed, Dictionary<string, string?> options)
{
// If we don't have a CD, DVD, HD-DVD, or BD, we can't dump using redumper
if (this.Type != MediaType.CDROM
@@ -580,21 +579,26 @@ namespace MPF.ExecutionContexts.Redumper
SpeedValue = driveSpeed;
// Set user-defined options
if (options.RedumperEnableVerbose)
this[FlagStrings.Verbose] = options.RedumperEnableVerbose;
if (options.RedumperEnableDebug)
this[FlagStrings.Debug] = options.RedumperEnableDebug;
if (options.RedumperReadMethod != RedumperReadMethod.NONE)
if (GetBooleanSetting(options, "RedumperEnableVerbose", true))
this[FlagStrings.Verbose] = true;
if (GetBooleanSetting(options, "RedumperEnableDebug", false))
this[FlagStrings.Debug] = true;
string? readMethod = GetStringSetting(options, "RedumperReadMethod", "NONE");
if (!string.IsNullOrEmpty(readMethod) && readMethod != "NONE")
{
this[FlagStrings.DriveReadMethod] = true;
DriveReadMethodValue = options.RedumperReadMethod.ToString();
DriveReadMethodValue = readMethod;
}
if (options.RedumperSectorOrder != RedumperSectorOrder.NONE)
string? sectorOrder = GetStringSetting(options, "RedumperSectorOrder", "NONE");
if (!string.IsNullOrEmpty(sectorOrder) && sectorOrder != "NONE")
{
this[FlagStrings.DriveSectorOrder] = true;
DriveSectorOrderValue = options.RedumperSectorOrder.ToString();
DriveSectorOrderValue = sectorOrder;
}
if (options.RedumperUseGenericDriveType)
if (GetBooleanSetting(options, "RedumperUseGenericDriveType", false))
{
this[FlagStrings.DriveType] = true;
DriveTypeValue = "GENERIC";
@@ -619,12 +623,12 @@ namespace MPF.ExecutionContexts.Redumper
}
this[FlagStrings.Retries] = true;
RetriesValue = options.RedumperRereadCount;
RetriesValue = GetInt32Setting(options, "RedumperRereadCount", 20);
if (options.RedumperEnableLeadinRetry)
if (GetBooleanSetting(options, "RedumperEnableLeadinRetry", false))
{
this[FlagStrings.PlextorLeadinRetries] = true;
PlextorLeadinRetriesValue = options.RedumperLeadinRetryCount;
PlextorLeadinRetriesValue = GetInt32Setting(options, "RedumperLeadinRetryCount", 4);
}
}

View File

@@ -244,9 +244,9 @@ namespace MPF.Frontend
// Set the proper parameters
_executionContext = _internalProgram switch
{
InternalProgram.Aaru => new ExecutionContexts.Aaru.ExecutionContext(_system, _type, _drive.Name, OutputPath, driveSpeed, _options),
InternalProgram.DiscImageCreator => new ExecutionContexts.DiscImageCreator.ExecutionContext(_system, _type, _drive.Name, OutputPath, driveSpeed, _options),
InternalProgram.Redumper => new ExecutionContexts.Redumper.ExecutionContext(_system, _type, _drive.Name, OutputPath, driveSpeed, _options),
InternalProgram.Aaru => new ExecutionContexts.Aaru.ExecutionContext(_system, _type, _drive.Name, OutputPath, driveSpeed, _options.Settings),
InternalProgram.DiscImageCreator => new ExecutionContexts.DiscImageCreator.ExecutionContext(_system, _type, _drive.Name, OutputPath, driveSpeed, _options.Settings),
InternalProgram.Redumper => new ExecutionContexts.Redumper.ExecutionContext(_system, _type, _drive.Name, OutputPath, driveSpeed, _options.Settings),
// If no dumping program found, set to null
InternalProgram.NONE => null,

View File

@@ -21,7 +21,7 @@ namespace MPF.Test.Modules
public void ParametersFromSystemAndTypeTest(RedumpSystem? knownSystem, MediaType? mediaType, string? expected)
{
var options = new Options();
var actual = new ExecutionContext(knownSystem, mediaType, "D:\\", "disc.bin", 16, options);
var actual = new ExecutionContext(knownSystem, mediaType, "D:\\", "disc.bin", 16, options.Settings);
Assert.Equal(expected, actual.BaseCommand);
}
@@ -32,7 +32,7 @@ namespace MPF.Test.Modules
public void ParametersFromOptionsSpecialDefaultTest(RedumpSystem? knownSystem, MediaType? mediaType, string[]? expected)
{
var options = new Options();
var actual = new ExecutionContext(knownSystem, mediaType, "D:\\", "disc.bin", 16, options);
var actual = new ExecutionContext(knownSystem, mediaType, "D:\\", "disc.bin", 16, options.Settings);
var expectedSet = new HashSet<string>(expected ?? Array.Empty<string>());
HashSet<string> actualSet = GenerateUsedKeys(actual);
@@ -45,7 +45,7 @@ namespace MPF.Test.Modules
public void ParametersFromOptionsC2RereadTest(RedumpSystem? knownSystem, MediaType? mediaType, int rereadC2, string[] expected)
{
var options = new Options { DICRereadCount = rereadC2 };
var actual = new ExecutionContext(knownSystem, mediaType, "D:\\", "disc.bin", 16, options);
var actual = new ExecutionContext(knownSystem, mediaType, "D:\\", "disc.bin", 16, options.Settings);
var expectedSet = new HashSet<string>(expected ?? Array.Empty<string>());
HashSet<string> actualSet = GenerateUsedKeys(actual);
@@ -65,7 +65,7 @@ namespace MPF.Test.Modules
public void ParametersFromOptionsDVDRereadTest(RedumpSystem? knownSystem, MediaType? mediaType, int rereadDVDBD, string[] expected)
{
var options = new Options { DICDVDRereadCount = rereadDVDBD };
var actual = new ExecutionContext(knownSystem, mediaType, "D:\\", "disc.bin", 16, options);
var actual = new ExecutionContext(knownSystem, mediaType, "D:\\", "disc.bin", 16, options.Settings);
var expectedSet = new HashSet<string>(expected ?? Array.Empty<string>());
HashSet<string> actualSet = GenerateUsedKeys(actual);
@@ -89,7 +89,7 @@ namespace MPF.Test.Modules
public void ParametersFromOptionsMultiSectorReadTest(RedumpSystem? knownSystem, MediaType? mediaType, bool multiSectorRead, string[] expected)
{
var options = new Options { DICMultiSectorRead = multiSectorRead };
var actual = new ExecutionContext(knownSystem, mediaType, "D:\\", "disc.bin", 16, options);
var actual = new ExecutionContext(knownSystem, mediaType, "D:\\", "disc.bin", 16, options.Settings);
var expectedSet = new HashSet<string>(expected ?? Array.Empty<string>());
HashSet<string> actualSet = GenerateUsedKeys(actual);
@@ -110,7 +110,7 @@ namespace MPF.Test.Modules
public void ParametersFromOptionsParanoidModeTest(RedumpSystem? knownSystem, MediaType? mediaType, bool paranoidMode, string[] expected)
{
var options = new Options { DICParanoidMode = paranoidMode };
var actual = new ExecutionContext(knownSystem, mediaType, "D:\\", "disc.bin", 16, options);
var actual = new ExecutionContext(knownSystem, mediaType, "D:\\", "disc.bin", 16, options.Settings);
var expectedSet = new HashSet<string>(expected ?? Array.Empty<string>());
HashSet<string> actualSet = GenerateUsedKeys(actual);