Minor code cleanups

This commit is contained in:
Matt Nadareski
2021-11-26 14:06:57 -08:00
parent cc7acfcd00
commit 7910a79917
4 changed files with 112 additions and 134 deletions

View File

@@ -33,10 +33,7 @@ namespace MPF.Core.Data
/// <summary>
/// Dispose the current instance
/// </summary>
public void Dispose()
{
this.TokenSource.Cancel();
}
public void Dispose() => this.TokenSource.Cancel();
/// <summary>
/// Enqueue a new item for processing

View File

@@ -62,9 +62,8 @@ namespace MPF.Library
#region Event Handlers
/// <summary>
/// Geneeic way of reporting a message
/// Generic way of reporting a message
/// </summary>
/// <param name="message">String value to report</param>
public EventHandler<string> ReportStatus;
/// <summary>
@@ -75,18 +74,12 @@ namespace MPF.Library
/// <summary>
/// Event handler for data returned from a process
/// </summary>
private void OutputToLog(object proc, string args)
{
outputQueue.Enqueue(args);
}
private void OutputToLog(object proc, string args) => outputQueue.Enqueue(args);
/// <summary>
/// Process the outputs in the queue
/// </summary>
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
/// <summary>
/// Cancel an in-progress dumping process
/// </summary>
public void CancelDumping()
{
Parameters.KillInternalProgram();
}
public void CancelDumping() => Parameters.KillInternalProgram();
/// <summary>
/// Eject the disc using DiscImageCreator
/// </summary>
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<string> EjectDisc() =>
await RunStandaloneDiscImageCreatorCommand(Modules.DiscImageCreator.CommandStrings.Eject);
/// <summary>
/// Reset the current drive using DiscImageCreator
/// </summary>
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<string> ResetDrive() =>
await RunStandaloneDiscImageCreatorCommand(Modules.DiscImageCreator.CommandStrings.Reset);
/// <summary>
/// Execute the initial invocation of the dumping programs
@@ -328,7 +271,7 @@ namespace MPF.Library
/// </summary>
/// <param name="resultProgress">Optional result progress callback</param>
/// <param name="protectionProgress">Optional protection progress callback</param>
/// <param name="processUserInfo">Optional user prompt to deal with submsision information</param>
/// <param name="processUserInfo">Optional user prompt to deal with submission information</param>
/// <returns>Result instance with the outcome</returns>
public async Task<Result> VerifyAndSaveDumpOutput(
IProgress<Result> 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
/// </summary>
/// <returns>Result instance with the outcome</returns>
private Result ExecuteAdditionalTools()
{
return Result.Success("No external tools needed!");
}
private Result ExecuteAdditionalTools() => Result.Success("No external tools needed!");
/// <summary>
/// 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);
}
/// <summary>
/// Validate that DIscImageCreator is able to be found
/// </summary>
/// <returns>True if DiscImageCreator is found properly, false otherwise</returns>
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;
}
/// <summary>
/// Run a standalone DiscImageCreator command
/// </summary>
/// <param name="command">Command string to run</param>
/// <returns>The output of the command on success, null on error</returns>
private async Task<string> 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
}
}

View File

@@ -481,7 +481,7 @@ namespace MPF.Library
}
/// <summary>
/// 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
/// </summary>
/// <param name="drive">Drive object representing the current drive</param>
/// <returns>Anti-modchip existence if possible, false on error</returns>
@@ -867,6 +867,50 @@ namespace MPF.Library
}
}
/// <summary>
/// Get the adjusted name of the media based on layers, if applicable
/// </summary>
/// <param name="mediaType">MediaType to get the proper name for</param>
/// <param name="size">Size of the current media</param>
/// <param name="layerbreak">First layerbreak value, as applicable</param>
/// <param name="layerbreak2">Second layerbreak value, as applicable</param>
/// <param name="layerbreak3">Third ayerbreak value, as applicable</param>
/// <returns>String representation of the media, including layer specification</returns>
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();
}
}
/// <summary>
/// Write the data to the output folder
/// </summary>
@@ -997,50 +1041,6 @@ namespace MPF.Library
AddIfExists(output, key, string.Join(", ", value.Select(o => o.ToString())), indent);
}
/// <summary>
/// Get the adjusted name of the media baed on layers, if applicable
/// </summary>
/// <param name="mediaType">MediaType to get the proper name for</param>
/// <param name="size">Size of the current media</param>
/// <param name="layerbreak">First layerbreak value, as applicable</param>
/// <param name="layerbreak2">Second layerbreak value, as applicable</param>
/// <param name="layerbreak3">Third ayerbreak value, as applicable</param>
/// <returns>String representation of the media, including layer specification</returns>
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
/// </summary>
/// <param name="directory">Directory name to normalize</param>
/// <param name="filename">Filename to normalize</param>
/// <param name="replacePeriods">True to replace '.' with '_' in filenames, false otherwise</param>
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)

View File

@@ -287,7 +287,7 @@ namespace MPF.GUI.ViewModels
/// <summary>
/// Toggle the Start/Stop button
/// </summary>
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