diff --git a/CHANGELIST.md b/CHANGELIST.md index 406002ea..2af00fa5 100644 --- a/CHANGELIST.md +++ b/CHANGELIST.md @@ -7,6 +7,7 @@ - Normalize publish scripts - Update AppVeyor to match scripts - Add release publish scripts +- Handle invalid characters when changing program ### 2.6.4 (2023-09-25) diff --git a/MPF.Library/DumpEnvironment.cs b/MPF.Library/DumpEnvironment.cs index 3de9243b..49257495 100644 --- a/MPF.Library/DumpEnvironment.cs +++ b/MPF.Library/DumpEnvironment.cs @@ -105,7 +105,7 @@ namespace MPF.Library this.Options = options; // Output paths - this.OutputPath = InfoTool.NormalizeOutputPaths(outputPath); + this.OutputPath = InfoTool.NormalizeOutputPaths(outputPath, true); // UI information this.Drive = drive; @@ -131,7 +131,7 @@ namespace MPF.Library try { // Normalize the output path - string outputPath = InfoTool.NormalizeOutputPaths(this.OutputPath); + string outputPath = InfoTool.NormalizeOutputPaths(this.OutputPath, true); // Replace all instances in the output directory string outputDirectory = Path.GetDirectoryName(outputPath); @@ -498,7 +498,7 @@ namespace MPF.Library return Result.Failure("Error! Current configuration is not supported!"); // Fix the output paths, just in case - this.OutputPath = InfoTool.NormalizeOutputPaths(this.OutputPath); + this.OutputPath = InfoTool.NormalizeOutputPaths(this.OutputPath, true); // Validate that the output path isn't on the dumping drive if (this.OutputPath[0] == Drive.Letter) diff --git a/MPF.Library/InfoTool.cs b/MPF.Library/InfoTool.cs index 8a10b293..21947cfc 100644 --- a/MPF.Library/InfoTool.cs +++ b/MPF.Library/InfoTool.cs @@ -1682,16 +1682,20 @@ namespace MPF.Library /// Normalize a split set of paths /// /// Path value to normalize - public static string NormalizeOutputPaths(string path) + public static string NormalizeOutputPaths(string path, bool getFullPath) { // The easy way try { - // Trim quotes from the path - path = path.Trim('"'); + // If we have an invalid path + if (string.IsNullOrWhiteSpace(path)) + return string.Empty; + + // Remove quotes from path + path = path.Replace("\"", string.Empty); // Try getting the combined path and returning that directly - string fullPath = Path.GetFullPath(path); + string fullPath = getFullPath ? Path.GetFullPath(path) : path; string fullDirectory = Path.GetDirectoryName(fullPath); string fullFile = Path.GetFileName(fullPath); diff --git a/MPF.Test/Library/InfoToolTests.cs b/MPF.Test/Library/InfoToolTests.cs index ed47dc40..eb5c4011 100644 --- a/MPF.Test/Library/InfoToolTests.cs +++ b/MPF.Test/Library/InfoToolTests.cs @@ -63,7 +63,7 @@ namespace MPF.Test.Library if (!string.IsNullOrWhiteSpace(expectedPath)) expectedPath = Path.GetFullPath(expectedPath); - string actualPath = InfoTool.NormalizeOutputPaths(outputPath); + string actualPath = InfoTool.NormalizeOutputPaths(outputPath, false); Assert.Equal(expectedPath, actualPath); } diff --git a/MPF.UI.Core/ViewModels/MainViewModel.cs b/MPF.UI.Core/ViewModels/MainViewModel.cs index 32a31f0a..3dfde82d 100644 --- a/MPF.UI.Core/ViewModels/MainViewModel.cs +++ b/MPF.UI.Core/ViewModels/MainViewModel.cs @@ -1033,8 +1033,9 @@ namespace MPF.UI.Core.ViewModels else if (driveChanged) { string label = drive?.FormattedVolumeLabel ?? systemType.LongName(); - string oldFilename = Path.GetFileNameWithoutExtension(this.Parent.OutputPathTextBox.Text); - string directory = Path.GetDirectoryName(this.Parent.OutputPathTextBox.Text); + string oldPath = InfoTool.NormalizeOutputPaths(this.Parent.OutputPathTextBox.Text, false); + string oldFilename = Path.GetFileNameWithoutExtension(oldPath); + string directory = Path.GetDirectoryName(oldPath); string filename = $"{label}{extension ?? ".bin"}"; // If the previous path included the label @@ -1051,8 +1052,9 @@ namespace MPF.UI.Core.ViewModels // Otherwise, reset the extension of the currently set path else { - string filename = Path.GetFileNameWithoutExtension(this.Parent.OutputPathTextBox.Text); - string directory = Path.GetDirectoryName(this.Parent.OutputPathTextBox.Text); + string oldPath = InfoTool.NormalizeOutputPaths(this.Parent.OutputPathTextBox.Text, false); + string filename = Path.GetFileNameWithoutExtension(oldPath); + string directory = Path.GetDirectoryName(oldPath); filename = $"{filename}{extension ?? ".bin"}"; this.Parent.OutputPathTextBox.Text = Path.Combine(directory, filename); @@ -1089,9 +1091,7 @@ namespace MPF.UI.Core.ViewModels // Disable change handling DisableEventHandlers(); - string trimmedPath = Env.Parameters.OutputPath?.Trim('"') ?? string.Empty; - trimmedPath = InfoTool.NormalizeOutputPaths(trimmedPath); - this.Parent.OutputPathTextBox.Text = trimmedPath; + this.Parent.OutputPathTextBox.Text = InfoTool.NormalizeOutputPaths(Env.Parameters.OutputPath, true); MediaType? mediaType = Env.Parameters.GetMediaType(); int mediaTypeIndex = MediaTypes.FindIndex(m => m == mediaType);