From 3137a543a760c6e2ad2fafca9743b2f71ce47b10 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 28 May 2024 14:07:18 -0400 Subject: [PATCH] Decouple execution contexts from Options class --- CHANGELIST.md | 1 + .../Aaru/ExecutionContext.cs | 26 +++---- MPF.ExecutionContexts/BaseExecutionContext.cs | 72 +++++++++++++++++-- .../DiscImageCreator/ExecutionContext.cs | 33 ++++----- .../MPF.ExecutionContexts.csproj | 4 -- .../Redumper/ExecutionContext.cs | 34 +++++---- MPF.Frontend/DumpEnvironment.cs | 6 +- MPF.Test/Modules/DiscImageCreatorTests.cs | 12 ++-- 8 files changed, 126 insertions(+), 62 deletions(-) diff --git a/CHANGELIST.md b/CHANGELIST.md index de843721..5f4c6a5e 100644 --- a/CHANGELIST.md +++ b/CHANGELIST.md @@ -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) diff --git a/MPF.ExecutionContexts/Aaru/ExecutionContext.cs b/MPF.ExecutionContexts/Aaru/ExecutionContext.cs index 600a86c1..48b53db4 100644 --- a/MPF.ExecutionContexts/Aaru/ExecutionContext.cs +++ b/MPF.ExecutionContexts/Aaru/ExecutionContext.cs @@ -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) { } /// - 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 options) : base(system, type, drivePath, filename, driveSpeed, options) { } @@ -1190,7 +1189,7 @@ namespace MPF.ExecutionContexts.Aaru } /// - protected override void SetDefaultParameters(string? drivePath, string filename, int? driveSpeed, Options options) + protected override void SetDefaultParameters(string? drivePath, string filename, int? driveSpeed, Dictionary 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 diff --git a/MPF.ExecutionContexts/BaseExecutionContext.cs b/MPF.ExecutionContexts/BaseExecutionContext.cs index acf97664..3c8f483f 100644 --- a/MPF.ExecutionContexts/BaseExecutionContext.cs +++ b/MPF.ExecutionContexts/BaseExecutionContext.cs @@ -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 /// Drive path to use /// Filename to use /// Drive speed to use - /// Options object containing all settings that may be used for setting parameters - public BaseExecutionContext(RedumpSystem? system, MediaType? type, string? drivePath, string filename, int? driveSpeed, Options options) + /// Dictionary object containing all settings that may be used for setting parameters + public BaseExecutionContext(RedumpSystem? system, MediaType? type, string? drivePath, string filename, int? driveSpeed, Dictionary options) { this.System = system; this.Type = type; @@ -169,8 +168,8 @@ namespace MPF.ExecutionContexts /// Drive path to use /// Filename to use /// Drive speed to use - /// Options object containing all settings that may be used for setting parameters - protected abstract void SetDefaultParameters(string? drivePath, string filename, int? driveSpeed, Options options); + /// Dictionary containing all settings that may be used for setting parameters + protected abstract void SetDefaultParameters(string? drivePath, string filename, int? driveSpeed, Dictionary options); /// /// Scan a possible parameter string and populate whatever possible @@ -226,6 +225,69 @@ namespace MPF.ExecutionContexts #endregion + #region Option Processing + + /// + /// Get a Boolean setting from a settings, dictionary + /// + /// Dictionary representing the settings + /// Setting key to get a value for + /// Default value to return if no value is found + /// Setting value if possible, default value otherwise + protected static bool GetBooleanSetting(Dictionary 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; + } + } + + /// + /// Get an Int32 setting from a settings, dictionary + /// + /// Dictionary representing the settings + /// Setting key to get a value for + /// Default value to return if no value is found + /// Setting value if possible, default value otherwise + protected static int GetInt32Setting(Dictionary 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; + } + } + + /// + /// Get a String setting from a settings, dictionary + /// + /// Dictionary representing the settings + /// Setting key to get a value for + /// Default value to return if no value is found + /// Setting value if possible, default value otherwise + protected static string? GetStringSetting(Dictionary settings, string key, string? defaultValue) + { + if (settings.ContainsKey(key)) + return settings[key]; + else + return defaultValue; + } + + #endregion + #region Parameter Parsing /// diff --git a/MPF.ExecutionContexts/DiscImageCreator/ExecutionContext.cs b/MPF.ExecutionContexts/DiscImageCreator/ExecutionContext.cs index 9efcf9d7..61a4c68a 100644 --- a/MPF.ExecutionContexts/DiscImageCreator/ExecutionContext.cs +++ b/MPF.ExecutionContexts/DiscImageCreator/ExecutionContext.cs @@ -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) { } /// - 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 options) : base(system, type, drivePath, filename, driveSpeed, options) { } @@ -944,7 +943,7 @@ namespace MPF.ExecutionContexts.DiscImageCreator } /// - protected override void SetDefaultParameters(string? drivePath, string filename, int? driveSpeed, Options options) + protected override void SetDefaultParameters(string? drivePath, string filename, int? driveSpeed, Dictionary 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: diff --git a/MPF.ExecutionContexts/MPF.ExecutionContexts.csproj b/MPF.ExecutionContexts/MPF.ExecutionContexts.csproj index 0b88877b..b51c3c12 100644 --- a/MPF.ExecutionContexts/MPF.ExecutionContexts.csproj +++ b/MPF.ExecutionContexts/MPF.ExecutionContexts.csproj @@ -25,10 +25,6 @@ - - - - diff --git a/MPF.ExecutionContexts/Redumper/ExecutionContext.cs b/MPF.ExecutionContexts/Redumper/ExecutionContext.cs index 9f300646..f745fe17 100644 --- a/MPF.ExecutionContexts/Redumper/ExecutionContext.cs +++ b/MPF.ExecutionContexts/Redumper/ExecutionContext.cs @@ -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) { } /// - 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 options) : base(system, type, drivePath, filename, driveSpeed, options) { } @@ -538,7 +537,7 @@ namespace MPF.ExecutionContexts.Redumper } /// - protected override void SetDefaultParameters(string? drivePath, string filename, int? driveSpeed, Options options) + protected override void SetDefaultParameters(string? drivePath, string filename, int? driveSpeed, Dictionary 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); } } diff --git a/MPF.Frontend/DumpEnvironment.cs b/MPF.Frontend/DumpEnvironment.cs index 3d98e532..fb1a1086 100644 --- a/MPF.Frontend/DumpEnvironment.cs +++ b/MPF.Frontend/DumpEnvironment.cs @@ -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, diff --git a/MPF.Test/Modules/DiscImageCreatorTests.cs b/MPF.Test/Modules/DiscImageCreatorTests.cs index fde9cffa..1dde40c5 100644 --- a/MPF.Test/Modules/DiscImageCreatorTests.cs +++ b/MPF.Test/Modules/DiscImageCreatorTests.cs @@ -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(expected ?? Array.Empty()); HashSet 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(expected ?? Array.Empty()); HashSet 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(expected ?? Array.Empty()); HashSet 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(expected ?? Array.Empty()); HashSet 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(expected ?? Array.Empty()); HashSet actualSet = GenerateUsedKeys(actual);