Ensure file pickers (via `IDialogParent`) don't choke on bad paths
see also #4077 but that's still open while this is clear-cut
This commit is contained in:
parent
58de1a0486
commit
1cee9dff60
|
@ -4499,6 +4499,20 @@ namespace BizHawk.Client.EmuHawk
|
||||||
|
|
||||||
public IDialogController DialogController => this;
|
public IDialogController DialogController => this;
|
||||||
|
|
||||||
|
private static string SanitiseForFileDialog(string initDir)
|
||||||
|
{
|
||||||
|
if (Directory.Exists(initDir)) return initDir;
|
||||||
|
#if DEBUG
|
||||||
|
throw new ArgumentException(
|
||||||
|
paramName: nameof(initDir),
|
||||||
|
message: File.Exists(initDir)
|
||||||
|
? $"file picker called with {nameof(initDir)} set to a non-dir"
|
||||||
|
: $"file picker called with {nameof(initDir)} set to a nonexistent path");
|
||||||
|
#else
|
||||||
|
return File.Exists(initDir) ? Path.GetDirectoryName(initDir) : string.Empty;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
public IReadOnlyList<string>/*?*/ ShowFileMultiOpenDialog(
|
public IReadOnlyList<string>/*?*/ ShowFileMultiOpenDialog(
|
||||||
IDialogParent dialogParent,
|
IDialogParent dialogParent,
|
||||||
string/*?*/ filterStr,
|
string/*?*/ filterStr,
|
||||||
|
@ -4514,10 +4528,11 @@ namespace BizHawk.Client.EmuHawk
|
||||||
FileName = initFileName ?? string.Empty,
|
FileName = initFileName ?? string.Empty,
|
||||||
Filter = filterStr ?? string.Empty,
|
Filter = filterStr ?? string.Empty,
|
||||||
FilterIndex = filterIndex,
|
FilterIndex = filterIndex,
|
||||||
InitialDirectory = initDir,
|
InitialDirectory = SanitiseForFileDialog(initDir),
|
||||||
Multiselect = maySelectMultiple,
|
Multiselect = maySelectMultiple,
|
||||||
RestoreDirectory = discardCWDChange,
|
RestoreDirectory = discardCWDChange,
|
||||||
Title = windowTitle ?? string.Empty,
|
Title = windowTitle ?? string.Empty,
|
||||||
|
ValidateNames = false, // only raises confusing errors, doesn't affect result
|
||||||
};
|
};
|
||||||
var result = dialogParent.ShowDialogWithTempMute(ofd);
|
var result = dialogParent.ShowDialogWithTempMute(ofd);
|
||||||
filterIndex = ofd.FilterIndex;
|
filterIndex = ofd.FilterIndex;
|
||||||
|
@ -4538,9 +4553,10 @@ namespace BizHawk.Client.EmuHawk
|
||||||
DefaultExt = fileExt ?? string.Empty,
|
DefaultExt = fileExt ?? string.Empty,
|
||||||
FileName = initFileName ?? string.Empty,
|
FileName = initFileName ?? string.Empty,
|
||||||
Filter = filterStr ?? string.Empty,
|
Filter = filterStr ?? string.Empty,
|
||||||
InitialDirectory = initDir,
|
InitialDirectory = SanitiseForFileDialog(initDir),
|
||||||
OverwritePrompt = !muteOverwriteWarning,
|
OverwritePrompt = !muteOverwriteWarning,
|
||||||
RestoreDirectory = discardCWDChange,
|
RestoreDirectory = discardCWDChange,
|
||||||
|
ValidateNames = false, // only raises confusing errors, doesn't affect result
|
||||||
};
|
};
|
||||||
var result = dialogParent.ShowDialogWithTempMute(sfd);
|
var result = dialogParent.ShowDialogWithTempMute(sfd);
|
||||||
return result.IsOk() ? sfd.FileName : null;
|
return result.IsOk() ? sfd.FileName : null;
|
||||||
|
|
Loading…
Reference in New Issue