From 7910a79917e06ef4be23fa5566207418a2202fc8 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Fri, 26 Nov 2021 14:06:57 -0800 Subject: [PATCH] Minor code cleanups --- MPF.Core/Data/ProcessingQueue.cs | 5 +- MPF.Library/DumpEnvironment.cs | 138 ++++++++++++++----------------- MPF.Library/InfoTool.cs | 95 +++++++++++---------- MPF/ViewModels/MainViewModel.cs | 8 +- 4 files changed, 112 insertions(+), 134 deletions(-) diff --git a/MPF.Core/Data/ProcessingQueue.cs b/MPF.Core/Data/ProcessingQueue.cs index a453d926..f2522722 100644 --- a/MPF.Core/Data/ProcessingQueue.cs +++ b/MPF.Core/Data/ProcessingQueue.cs @@ -33,10 +33,7 @@ namespace MPF.Core.Data /// /// Dispose the current instance /// - public void Dispose() - { - this.TokenSource.Cancel(); - } + public void Dispose() => this.TokenSource.Cancel(); /// /// Enqueue a new item for processing diff --git a/MPF.Library/DumpEnvironment.cs b/MPF.Library/DumpEnvironment.cs index 442b209a..87d31a87 100644 --- a/MPF.Library/DumpEnvironment.cs +++ b/MPF.Library/DumpEnvironment.cs @@ -62,9 +62,8 @@ namespace MPF.Library #region Event Handlers /// - /// Geneeic way of reporting a message + /// Generic way of reporting a message /// - /// String value to report public EventHandler ReportStatus; /// @@ -75,18 +74,12 @@ namespace MPF.Library /// /// Event handler for data returned from a process /// - private void OutputToLog(object proc, string args) - { - outputQueue.Enqueue(args); - } + private void OutputToLog(object proc, string args) => outputQueue.Enqueue(args); /// /// Process the outputs in the queue /// - private void ProcessOutputs(string nextOutput) - { - ReportStatus.Invoke(this, nextOutput); - } + private void ProcessOutputs(string nextOutput) => ReportStatus.Invoke(this, nextOutput); #endregion @@ -112,7 +105,7 @@ namespace MPF.Library this.Options = options; // Output paths - (this.OutputDirectory, this.OutputFilename) = InfoTool.NormalizeOutputPaths(outputDirectory, outputFilename, options.InternalProgram == InternalProgram.DiscImageCreator); + (this.OutputDirectory, this.OutputFilename) = InfoTool.NormalizeOutputPaths(outputDirectory, outputFilename); // UI information this.Drive = drive; @@ -220,69 +213,19 @@ namespace MPF.Library /// /// Cancel an in-progress dumping process /// - public void CancelDumping() - { - Parameters.KillInternalProgram(); - } + public void CancelDumping() => Parameters.KillInternalProgram(); /// /// Eject the disc using DiscImageCreator /// - public async void EjectDisc() - { - // Validate that the path is configured - if (string.IsNullOrWhiteSpace(Options.DiscImageCreatorPath)) - return; - - // Validate that the required program exists - if (!File.Exists(Options.DiscImageCreatorPath)) - return; - - CancelDumping(); - - // Validate we're not trying to eject a non-optical - if (Drive.InternalDriveType != InternalDriveType.Optical) - return; - - var parameters = new Modules.DiscImageCreator.Parameters(string.Empty) - { - BaseCommand = Modules.DiscImageCreator.CommandStrings.Eject, - DriveLetter = Drive.Letter.ToString(), - ExecutablePath = Options.DiscImageCreatorPath, - }; - - await ExecuteInternalProgram(parameters); - } + public async Task EjectDisc() => + await RunStandaloneDiscImageCreatorCommand(Modules.DiscImageCreator.CommandStrings.Eject); /// /// Reset the current drive using DiscImageCreator /// - public async void ResetDrive() - { - // Validate that the path is configured - if (string.IsNullOrWhiteSpace(Options.DiscImageCreatorPath)) - return; - - // Validate that the required program exists - if (!File.Exists(Options.DiscImageCreatorPath)) - return; - - // Precautionary check for dumping, just in case - CancelDumping(); - - // Validate we're not trying to reset a non-optical - if (Drive.InternalDriveType != InternalDriveType.Optical) - return; - - Modules.DiscImageCreator.Parameters parameters = new Modules.DiscImageCreator.Parameters(string.Empty) - { - BaseCommand = Modules.DiscImageCreator.CommandStrings.Reset, - DriveLetter = Drive.Letter.ToString(), - ExecutablePath = Options.DiscImageCreatorPath, - }; - - await ExecuteInternalProgram(parameters); - } + public async Task ResetDrive() => + await RunStandaloneDiscImageCreatorCommand(Modules.DiscImageCreator.CommandStrings.Reset); /// /// Execute the initial invocation of the dumping programs @@ -328,7 +271,7 @@ namespace MPF.Library /// /// Optional result progress callback /// Optional protection progress callback - /// Optional user prompt to deal with submsision information + /// Optional user prompt to deal with submission information /// Result instance with the outcome public async Task VerifyAndSaveDumpOutput( IProgress resultProgress = null, @@ -359,21 +302,21 @@ namespace MPF.Library protectionProgress); resultProgress?.Report(Result.Success("Extracting information complete!")); - // Eject the disc automatically if confugured to + // Eject the disc automatically if configured to if (Options.EjectAfterDump == true) { resultProgress?.Report(Result.Success($"Ejecting disc in drive {Drive.Letter}")); - EjectDisc(); + await EjectDisc(); } - // Reset the drive automatically if confugured to + // Reset the drive automatically if configured to if (Options.InternalProgram == InternalProgram.DiscImageCreator && Options.DICResetDriveAfterDump) { resultProgress?.Report(Result.Success($"Resetting drive {Drive.Letter}")); - ResetDrive(); + await ResetDrive(); } - // Get user-modifyable information if confugured to + // Get user-modifiable information if confugured to if (Options.PromptForDiscInformation && processUserInfo != null) { resultProgress?.Report(Result.Success("Waiting for additional disc information...")); @@ -446,10 +389,7 @@ namespace MPF.Library /// Run any additional tools given a DumpEnvironment /// /// Result instance with the outcome - private Result ExecuteAdditionalTools() - { - return Result.Success("No external tools needed!"); - } + private Result ExecuteAdditionalTools() => Result.Success("No external tools needed!"); /// /// Run internal program async with an input set of parameters @@ -499,7 +439,7 @@ namespace MPF.Library return Result.Failure("Error! Current configuration is not supported!"); // Fix the output paths, just in case - (OutputDirectory, OutputFilename) = InfoTool.NormalizeOutputPaths(OutputDirectory, OutputFilename, Options.InternalProgram == InternalProgram.DiscImageCreator); + (OutputDirectory, OutputFilename) = InfoTool.NormalizeOutputPaths(OutputDirectory, OutputFilename); // Validate that the output path isn't on the dumping drive string fullOutputPath = Path.GetFullPath(Path.Combine(OutputDirectory, OutputFilename)); @@ -519,6 +459,50 @@ namespace MPF.Library return Tools.GetSupportStatus(System, Type); } + /// + /// Validate that DIscImageCreator is able to be found + /// + /// True if DiscImageCreator is found properly, false otherwise + private bool RequiredProgramsExist() + { + // Validate that the path is configured + if (string.IsNullOrWhiteSpace(Options.DiscImageCreatorPath)) + return false; + + // Validate that the required program exists + if (!File.Exists(Options.DiscImageCreatorPath)) + return false; + + return true; + } + + /// + /// Run a standalone DiscImageCreator command + /// + /// Command string to run + /// The output of the command on success, null on error + private async Task RunStandaloneDiscImageCreatorCommand(string command) + { + // Validate that DiscImageCreator is all set + if (!RequiredProgramsExist()) + return null; + + // Validate we're not trying to eject a non-optical + if (Drive.InternalDriveType != InternalDriveType.Optical) + return null; + + CancelDumping(); + + var parameters = new Modules.DiscImageCreator.Parameters(string.Empty) + { + BaseCommand = command, + DriveLetter = Drive.Letter.ToString(), + ExecutablePath = Options.DiscImageCreatorPath, + }; + + return await ExecuteInternalProgram(parameters); + } + #endregion } } diff --git a/MPF.Library/InfoTool.cs b/MPF.Library/InfoTool.cs index 221f60cb..2e9550f3 100644 --- a/MPF.Library/InfoTool.cs +++ b/MPF.Library/InfoTool.cs @@ -481,7 +481,7 @@ namespace MPF.Library } /// - /// Get the existance of an anti-modchip string from a PlayStation disc, if possible + /// Get the existence of an anti-modchip string from a PlayStation disc, if possible /// /// Drive object representing the current drive /// Anti-modchip existence if possible, false on error @@ -867,6 +867,50 @@ namespace MPF.Library } } + /// + /// Get the adjusted name of the media based on layers, if applicable + /// + /// MediaType to get the proper name for + /// Size of the current media + /// First layerbreak value, as applicable + /// Second layerbreak value, as applicable + /// Third ayerbreak value, as applicable + /// String representation of the media, including layer specification + public static string GetFixedMediaType(MediaType? mediaType, long size, long layerbreak, long layerbreak2, long layerbreak3) + { + switch (mediaType) + { + case MediaType.DVD: + if (layerbreak != default) + return $"{mediaType.LongName()}-9"; + else + return $"{mediaType.LongName()}-5"; + + case MediaType.BluRay: + if (layerbreak3 != default) + return $"{mediaType.LongName()}-128"; + else if (layerbreak2 != default) + return $"{mediaType.LongName()}-100"; + else if (layerbreak != default && size > 53_687_063_712) + return $"{mediaType.LongName()}-66"; + else if (layerbreak != default) + return $"{mediaType.LongName()}-50"; + else if (size > 26_843_531_856) + return $"{mediaType.LongName()}-33"; + else + return $"{mediaType.LongName()}-25"; + + case MediaType.UMD: + if (layerbreak != default) + return $"{mediaType.LongName()}-DL"; + else + return $"{mediaType.LongName()}-SL"; + + default: + return mediaType.LongName(); + } + } + /// /// Write the data to the output folder /// @@ -997,50 +1041,6 @@ namespace MPF.Library AddIfExists(output, key, string.Join(", ", value.Select(o => o.ToString())), indent); } - /// - /// Get the adjusted name of the media baed on layers, if applicable - /// - /// MediaType to get the proper name for - /// Size of the current media - /// First layerbreak value, as applicable - /// Second layerbreak value, as applicable - /// Third ayerbreak value, as applicable - /// String representation of the media, including layer specification - private static string GetFixedMediaType(MediaType? mediaType, long size, long layerbreak, long layerbreak2, long layerbreak3) - { - switch (mediaType) - { - case MediaType.DVD: - if (layerbreak != default) - return $"{mediaType.LongName()}-9"; - else - return $"{mediaType.LongName()}-5"; - - case MediaType.BluRay: - if (layerbreak3 != default) - return $"{mediaType.LongName()}-128"; - else if (layerbreak2 != default) - return $"{mediaType.LongName()}-100"; - else if (layerbreak != default && size > 53_687_063_712) - return $"{mediaType.LongName()}-66"; - else if (layerbreak != default) - return $"{mediaType.LongName()}-50"; - else if (size > 26_843_531_856) - return $"{mediaType.LongName()}-33"; - else - return $"{mediaType.LongName()}-25"; - - case MediaType.UMD: - if (layerbreak != default) - return $"{mediaType.LongName()}-DL"; - else - return $"{mediaType.LongName()}-SL"; - - default: - return mediaType.LongName(); - } - } - #endregion #region Normalization @@ -1050,8 +1050,7 @@ namespace MPF.Library /// /// Directory name to normalize /// Filename to normalize - /// True to replace '.' with '_' in filenames, false otherwise - public static (string, string) NormalizeOutputPaths(string directory, string filename, bool replacePeriods) + public static (string, string) NormalizeOutputPaths(string directory, string filename) { try { @@ -1079,8 +1078,6 @@ namespace MPF.Library directory = directory.Replace(c, '_'); foreach (char c in Path.GetInvalidFileNameChars()) filename = filename.Replace(c, '_'); - if (replacePeriods) - filename = Path.GetFileNameWithoutExtension(filename).Replace('.', '_') + "." + Path.GetExtension(filename).TrimStart('.'); // If we had a directory separator at the end before, add it again if (endedWithDirectorySeparator) diff --git a/MPF/ViewModels/MainViewModel.cs b/MPF/ViewModels/MainViewModel.cs index b07319fe..0481344b 100644 --- a/MPF/ViewModels/MainViewModel.cs +++ b/MPF/ViewModels/MainViewModel.cs @@ -287,7 +287,7 @@ namespace MPF.GUI.ViewModels /// /// Toggle the Start/Stop button /// - public void ToggleStartStop() + public async void ToggleStartStop() { // Dump or stop the dump if ((string)App.Instance.StartStopButton.Content == Interface.StartDumping) @@ -303,13 +303,13 @@ namespace MPF.GUI.ViewModels if (Env.Options.EjectAfterDump == true) { App.Logger.VerboseLogLn($"Ejecting disc in drive {Env.Drive.Letter}"); - Env.EjectDisc(); + await Env.EjectDisc(); } if (App.Options.DICResetDriveAfterDump) { App.Logger.VerboseLogLn($"Resetting drive {Env.Drive.Letter}"); - Env.ResetDrive(); + await Env.ResetDrive(); } } @@ -873,7 +873,7 @@ namespace MPF.GUI.ViewModels string trimmedPath = Env.Parameters.OutputPath?.Trim('"') ?? string.Empty; string outputDirectory = Path.GetDirectoryName(trimmedPath); string outputFilename = Path.GetFileName(trimmedPath); - (outputDirectory, outputFilename) = InfoTool.NormalizeOutputPaths(outputDirectory, outputFilename, App.Options.InternalProgram == InternalProgram.DiscImageCreator); + (outputDirectory, outputFilename) = InfoTool.NormalizeOutputPaths(outputDirectory, outputFilename); if (!string.IsNullOrWhiteSpace(outputDirectory)) App.Instance.OutputDirectoryTextBox.Text = outputDirectory; else