Handle invalid characters when changing program

This commit is contained in:
Matt Nadareski
2023-09-26 21:09:53 -04:00
parent f02904ea49
commit 4362ed71e0
5 changed files with 20 additions and 15 deletions

View File

@@ -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)

View File

@@ -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)

View File

@@ -1682,16 +1682,20 @@ namespace MPF.Library
/// Normalize a split set of paths
/// </summary>
/// <param name="path">Path value to normalize</param>
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);

View File

@@ -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);
}

View File

@@ -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);