From f1f0f1695c3dbd18ff939d06a8f718733ee2a957 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Tue, 11 Oct 2022 10:47:52 +1000 Subject: [PATCH] Encapsulate `OpenFileDialog`/`SaveFileDialog` --- .../DialogControllerExtensions.cs | 123 ++++++++++++++ .../IDialogController.cs | 40 +++++ .../AVOut/SynclessRecordingTools.cs | 32 ++-- .../Extensions/ControlExtensions.cs | 18 +- src/BizHawk.Client.EmuHawk/MainForm.Events.cs | 132 +++++---------- src/BizHawk.Client.EmuHawk/MainForm.cs | 159 ++++++++++-------- .../OpenAdvancedChooser.cs | 4 +- .../config/DisplayConfig.cs | 13 +- .../config/FirmwaresConfig.cs | 28 ++- .../config/GB/ColorChooserForm.cs | 36 ++-- .../config/GB/SameBoyColorChooserForm.cs | 36 ++-- .../config/NES/NESGraphicsConfig.cs | 20 +-- .../config/NES/QuickNesConfig.cs | 20 +-- .../debug/FirmwareAutopatchDebugToolForm.cs | 5 +- .../debug/N64RomByteswapToolForm.cs | 18 +- .../movie/EditSubtitlesForm.cs | 24 +-- src/BizHawk.Client.EmuHawk/movie/PlayMovie.cs | 16 +- .../movie/RecordMovie.cs | 22 +-- src/BizHawk.Client.EmuHawk/tools/BatchRun.cs | 6 +- src/BizHawk.Client.EmuHawk/tools/CDL.cs | 8 +- .../tools/HexEditor/HexEditor.cs | 75 +++------ .../tools/Lua/Libraries/FormsLuaLibrary.cs | 23 ++- .../tools/Lua/LuaConsole.cs | 106 +++++------- .../tools/Macros/MacroInput.cs | 34 ++-- .../MultiDiskBundler/MultiDiskBundler.cs | 18 +- .../MultiDiskBundler/MultiDiskFileSelector.cs | 18 +- .../tools/NES/NESMusicRipper.cs | 13 +- .../tools/TAStudio/TAStudio.MenuItems.cs | 28 ++- .../tools/ToolFormBase.cs | 34 ++-- .../tools/TraceLogger.cs | 26 +-- 30 files changed, 548 insertions(+), 587 deletions(-) diff --git a/src/BizHawk.Client.Common/DialogControllerExtensions.cs b/src/BizHawk.Client.Common/DialogControllerExtensions.cs index e42361ebe5..d6f17dccd3 100644 --- a/src/BizHawk.Client.Common/DialogControllerExtensions.cs +++ b/src/BizHawk.Client.Common/DialogControllerExtensions.cs @@ -1,6 +1,7 @@ #nullable enable using System; +using System.Collections.Generic; namespace BizHawk.Client.Common { @@ -57,6 +58,85 @@ namespace BizHawk.Client.Common EMsgBoxIcon? icon = null) => dialogParent.DialogController.ShowMessageBox3(owner: dialogParent, text: text, caption: caption, icon: icon); + /// Creates and shows a System.Windows.Forms.OpenFileDialog or equivalent with the receiver () as its parent + /// OpenFileDialog.RestoreDirectory (isn't this useless when specifying ? keeping it for backcompat) + /// OpenFileDialog.Filter + /// OpenFileDialog.InitialDirectory; initial browse location + /// OpenFileDialog.FileName; pre-selected file (overrides ?) + /// filename of selected file, or iff cancelled + public static string? ShowFileOpenDialog( + this IDialogParent dialogParent, + string initDir, + bool discardCWDChange = false, + FilesystemFilterSet? filter = null, + string? initFileName = null) + => dialogParent.ShowFileMultiOpenDialog( + discardCWDChange: discardCWDChange, + filterStr: filter?.ToString(), + initDir: initDir, + initFileName: initFileName)?[0]; + + /// Creates and shows a System.Windows.Forms.OpenFileDialog or equivalent with the receiver () as its parent + /// OpenFileDialog.Filter + /// OpenFileDialog.FilterIndex; initially selected entry in + /// OpenFileDialog.InitialDirectory; initial browse location + /// OpenFileDialog.Title + /// filename of selected file, or iff cancelled + /// only used from MainForm, but don't move it there + public static string? ShowFileOpenDialog( + this IDialogParent dialogParent, + FilesystemFilterSet? filter, + ref int filterIndex, + string initDir, + string? windowTitle = null) + => dialogParent.DialogController.ShowFileMultiOpenDialog( + dialogParent: dialogParent, + filterStr: filter?.ToString(), + filterIndex: ref filterIndex, + initDir: initDir, + windowTitle: windowTitle)?[0]; + + /// Creates and shows a System.Windows.Forms.OpenFileDialog or equivalent with the receiver () as its parent + /// OpenFileDialog.Filter + /// OpenFileDialog.InitialDirectory; initial browse location + /// OpenFileDialog.FileName; pre-selected file (overrides ?) + /// filename of selected file, or iff cancelled + /// only used from Lua, but don't move it there + public static string? ShowFileOpenDialog( + this IDialogParent dialogParent, + string? filterStr, + string initDir, + string? initFileName = null) + => dialogParent.ShowFileMultiOpenDialog( + filterStr: filterStr, + initDir: initDir, + initFileName: initFileName)?[0]; + + /// Creates and shows a System.Windows.Forms.SaveFileDialog or equivalent with the receiver () as its parent + /// SaveFileDialog.RestoreDirectory (renamed for clarity without inverting value; isn't this useless when specifying ? keeping it for backcompat) + /// SaveFileDialog.DefaultExt; used only when the user's chosen filename doesn't have an extension (omit leading '.') + /// SaveFileDialog.Filter + /// SaveFileDialog.InitialDirectory; initial browse location + /// SaveFileDialog.FileName; pre-selected file (overrides ?) + /// SaveFileDialog.OverwritePrompt (renamed for clarity with inverted value) + /// filename of selected destination, or iff cancelled + public static string? ShowFileSaveDialog( + this IDialogParent dialogParent, + string initDir, + bool discardCWDChange = false, + string? fileExt = null, + FilesystemFilterSet? filter = null, + string? initFileName = null, + bool muteOverwriteWarning = false) + => dialogParent.DialogController.ShowFileSaveDialog( + dialogParent: dialogParent, + discardCWDChange: discardCWDChange, + fileExt: fileExt, + filterStr: filter?.ToString(), + initDir: initDir, + initFileName: initFileName, + muteOverwriteWarning: muteOverwriteWarning); + /// /// Creates and shows a System.Windows.Forms.MessageBox or equivalent without a parent, with the given , /// and with the given and if they're specified. @@ -92,5 +172,48 @@ namespace BizHawk.Client.Common string? caption = null, EMsgBoxIcon? icon = null) => dialogController.ShowMessageBox3(owner: null, text: text, caption: caption, icon: icon); + + /// Creates and shows a System.Windows.Forms.OpenFileDialog or equivalent with the receiver () as its parent + /// OpenFileDialog.RestoreDirectory (renamed for clarity without inverting value; isn't this useless when specifying ? keeping it for backcompat) + /// OpenFileDialog.Filter (call ToString on a /) + /// OpenFileDialog.InitialDirectory; initial browse location + /// OpenFileDialog.FileName; pre-selected file (overrides ?) + /// OpenFileDialog.Multiselect + /// filenames of selected files, or iff cancelled + private static IReadOnlyList? ShowFileMultiOpenDialog( + this IDialogParent dialogParent, + string? filterStr, + string initDir, + bool discardCWDChange = false, + string? initFileName = null, + bool maySelectMultiple = false) + { + var filterIndex = 1; // you'd think the default would be 0, but it's not + return dialogParent.DialogController.ShowFileMultiOpenDialog( + dialogParent: dialogParent, + discardCWDChange: discardCWDChange, + filterStr: filterStr, + filterIndex: ref filterIndex, + initDir: initDir, + initFileName: initFileName, + maySelectMultiple: maySelectMultiple); + } + + /// Creates and shows a System.Windows.Forms.OpenFileDialog or equivalent with the receiver () as its parent + /// OpenFileDialog.RestoreDirectory (isn't this useless when specifying ? keeping it for backcompat) + /// OpenFileDialog.Filter + /// OpenFileDialog.InitialDirectory; initial browse location + /// filenames of selected files, or iff cancelled + public static IReadOnlyList? ShowFileMultiOpenDialog( + this IDialogParent dialogParent, + string initDir, + bool discardCWDChange = false, + FilesystemFilterSet? filter = null) + => dialogParent.ShowFileMultiOpenDialog( + discardCWDChange: discardCWDChange, + filterStr: filter?.ToString(), + initDir: initDir, + initFileName: null, + maySelectMultiple: true); } } diff --git a/src/BizHawk.Client.Common/IDialogController.cs b/src/BizHawk.Client.Common/IDialogController.cs index 839ad2e26f..2ea6edb0d7 100644 --- a/src/BizHawk.Client.Common/IDialogController.cs +++ b/src/BizHawk.Client.Common/IDialogController.cs @@ -1,11 +1,51 @@ #nullable enable +using System.Collections.Generic; + namespace BizHawk.Client.Common { public interface IDialogController { void AddOnScreenMessage(string message); + /// Creates and shows a System.Windows.Forms.OpenFileDialog or equivalent + /// parent window + /// OpenFileDialog.RestoreDirectory (renamed for clarity without inverting value; isn't this useless when specifying ? keeping it for backcompat) + /// OpenFileDialog.Filter (call ToString on a /) + /// OpenFileDialog.FilterIndex; initially selected entry in + /// OpenFileDialog.InitialDirectory; initial browse location + /// OpenFileDialog.FileName; pre-selected file (overrides ?) + /// OpenFileDialog.Multiselect + /// OpenFileDialog.Title + /// filenames of selected files, or iff cancelled + IReadOnlyList? ShowFileMultiOpenDialog( + IDialogParent dialogParent, + string? filterStr, + ref int filterIndex, + string initDir, + bool discardCWDChange = false, + string? initFileName = null, + bool maySelectMultiple = false, + string? windowTitle = null); + + /// Creates and shows a System.Windows.Forms.SaveFileDialog or equivalent + /// parent window + /// SaveFileDialog.RestoreDirectory (renamed for clarity without inverting value; isn't this useless when specifying ? keeping it for backcompat) + /// SaveFileDialog.DefaultExt; used only when the user's chosen filename doesn't have an extension (omit leading '.') + /// SaveFileDialog.Filter (call ToString on a /) + /// SaveFileDialog.InitialDirectory; initial browse location + /// SaveFileDialog.FileName; pre-selected file (overrides ?) + /// SaveFileDialog.OverwritePrompt (renamed for clarity with inverted value) + /// filename of selected destination, or iff cancelled + string? ShowFileSaveDialog( + IDialogParent dialogParent, + bool discardCWDChange, + string? fileExt, + string? filterStr, + string initDir, + string? initFileName, + bool muteOverwriteWarning); + /// /// Creates and shows a System.Windows.Forms.MessageBox or equivalent with the given , /// and with the given , , and if they're specified. diff --git a/src/BizHawk.Client.EmuHawk/AVOut/SynclessRecordingTools.cs b/src/BizHawk.Client.EmuHawk/AVOut/SynclessRecordingTools.cs index b91365563d..45981c468b 100644 --- a/src/BizHawk.Client.EmuHawk/AVOut/SynclessRecordingTools.cs +++ b/src/BizHawk.Client.EmuHawk/AVOut/SynclessRecordingTools.cs @@ -37,18 +37,12 @@ namespace BizHawk.Client.EmuHawk #if AVI_SUPPORT public void Run() { - var ofd = new OpenFileDialog - { - FileName = $"{_game.FilesystemSafeName()}.syncless.txt", - InitialDirectory = _config.PathEntries.AvAbsolutePath() - }; + var result = this.ShowFileOpenDialog( + initDir: _config.PathEntries.AvAbsolutePath(), + initFileName: $"{_game.FilesystemSafeName()}.syncless.txt"); + if (result is null) return; - if (ofd.ShowDialog() == DialogResult.Cancel) - { - return; - } - - _mSynclessConfigFile = ofd.FileName; + _mSynclessConfigFile = result; //---- this is pretty crappy: var lines = File.ReadAllLines(_mSynclessConfigFile); @@ -120,15 +114,11 @@ namespace BizHawk.Client.EmuHawk height = bmp.Height; } - var sfd = new SaveFileDialog - { - FileName = Path.ChangeExtension(_mSynclessConfigFile, ".avi") - }; - sfd.InitialDirectory = Path.GetDirectoryName(sfd.FileName); - if (sfd.ShowDialog() == DialogResult.Cancel) - { - return; - } + var initFileName = Path.ChangeExtension(_mSynclessConfigFile, ".avi"); + var result = this.ShowFileSaveDialog( + initDir: Path.GetDirectoryName(initFileName)!, + initFileName: initFileName); + if (result is null) return; using var avw = new AviWriter(this); avw.SetAudioParameters(44100, 2, 16); // hacky @@ -136,7 +126,7 @@ namespace BizHawk.Client.EmuHawk avw.SetVideoParameters(width, height); var token = avw.AcquireVideoCodecToken(_config); avw.SetVideoCodecToken(token); - avw.OpenFile(sfd.FileName); + avw.OpenFile(result); foreach (var fi in _mFrameInfos) { using (var bb = new BitmapBuffer(fi.PngPath, new BitmapLoadOptions())) diff --git a/src/BizHawk.Client.EmuHawk/Extensions/ControlExtensions.cs b/src/BizHawk.Client.EmuHawk/Extensions/ControlExtensions.cs index 8be53011fc..9466491ff5 100644 --- a/src/BizHawk.Client.EmuHawk/Extensions/ControlExtensions.cs +++ b/src/BizHawk.Client.EmuHawk/Extensions/ControlExtensions.cs @@ -237,17 +237,13 @@ namespace BizHawk.Client.EmuHawk public static void SaveAsFile(this Bitmap bitmap, IGameInfo game, string suffix, string systemId, PathEntryCollection paths, IDialogParent parent) { - using var sfd = new SaveFileDialog - { - FileName = $"{game.FilesystemSafeName()}-{suffix}", - InitialDirectory = paths.ScreenshotAbsolutePathFor(systemId), - Filter = FilesystemFilterSet.Screenshots.ToString(), - RestoreDirectory = true - }; - - if (parent.ShowDialogWithTempMute(sfd) != DialogResult.OK) return; - - var file = new FileInfo(sfd.FileName); + var result = parent.ShowFileSaveDialog( + discardCWDChange: true, + filter: FilesystemFilterSet.Screenshots, + initDir: paths.ScreenshotAbsolutePathFor(systemId), + initFileName: $"{game.FilesystemSafeName()}-{suffix}"); + if (result is null) return; + FileInfo file = new(result); string extension = file.Extension.ToUpper(); ImageFormat i = extension switch { diff --git a/src/BizHawk.Client.EmuHawk/MainForm.Events.cs b/src/BizHawk.Client.EmuHawk/MainForm.Events.cs index de0e526606..67837eefcf 100644 --- a/src/BizHawk.Client.EmuHawk/MainForm.Events.cs +++ b/src/BizHawk.Client.EmuHawk/MainForm.Events.cs @@ -319,12 +319,12 @@ namespace BizHawk.Client.EmuHawk var args = new LoadRomArgs(); - var filter = RomLoader.RomFilter.ToString(); + var filter = RomLoader.RomFilter; if (oac.Result == AdvancedRomLoaderType.LibretroLaunchGame) { args.OpenAdvanced = new OpenAdvanced_Libretro(); - filter = oac.SuggestedExtensionFilter; + filter = oac.SuggestedExtensionFilter!; } else if (oac.Result == AdvancedRomLoaderType.ClassicLaunchGame) { @@ -333,31 +333,20 @@ namespace BizHawk.Client.EmuHawk else if (oac.Result == AdvancedRomLoaderType.MameLaunchGame) { args.OpenAdvanced = new OpenAdvanced_MAME(); - filter = MAMERomsFSFilterSet.ToString(); + filter = MAMERomsFSFilterSet; } else { throw new InvalidOperationException("Automatic Alpha Sanitizer"); } - - /*************************/ - /* CLONE OF CODE FROM OpenRom (mostly) */ - using var ofd = new OpenFileDialog - { - InitialDirectory = Config.PathEntries.RomAbsolutePath(Emulator.SystemId), - Filter = filter, - RestoreDirectory = false, - FilterIndex = _lastOpenRomFilter, - Title = "Open Advanced" - }; - - if (!this.ShowDialogWithTempMute(ofd).IsOk()) return; - - var file = new FileInfo(ofd.FileName); + var result = this.ShowFileOpenDialog( + filter: filter, + filterIndex: ref _lastOpenRomFilter, + initDir: Config.PathEntries.RomAbsolutePath(Emulator.SystemId), + windowTitle: "Open Advanced"); + if (result is null) return; + FileInfo file = new(result); Config.PathEntries.LastRomPath = file.DirectoryName; - _lastOpenRomFilter = ofd.FilterIndex; - /*************************/ - LoadRom(file.FullName, args); } @@ -507,21 +496,11 @@ namespace BizHawk.Client.EmuHawk private void ImportMovieMenuItem_Click(object sender, EventArgs e) { - using var ofd = new OpenFileDialog - { - InitialDirectory = Config.PathEntries.RomAbsolutePath(Emulator.SystemId), - Multiselect = true, - Filter = MovieImport.AvailableImporters.ToString(), - RestoreDirectory = false - }; - - if (this.ShowDialogWithTempMute(ofd).IsOk()) - { - foreach (var fn in ofd.FileNames) - { - ProcessMovieImport(fn, false); - } - } + var result = this.ShowFileMultiOpenDialog( + discardCWDChange: false, + filter: MovieImport.AvailableImporters, + initDir: Config.PathEntries.RomAbsolutePath(Emulator.SystemId)); + if (result is not null) foreach (var fn in result) ProcessMovieImport(fn, false); } private void SaveMovieMenuItem_Click(object sender, EventArgs e) @@ -631,17 +610,11 @@ namespace BizHawk.Client.EmuHawk private void ScreenshotAsMenuItem_Click(object sender, EventArgs e) { var (dir, file) = $"{ScreenshotPrefix()}.{DateTime.Now:yyyy-MM-dd HH.mm.ss}.png".SplitPathToDirAndFile(); - using var sfd = new SaveFileDialog - { - InitialDirectory = dir, - FileName = file, - Filter = ScreenshotsFSFilterSet.ToString(), - }; - - if (this.ShowDialogWithTempMute(sfd).IsOk()) - { - TakeScreenshot(sfd.FileName); - } + var result = this.ShowFileSaveDialog( + filter: ScreenshotsFSFilterSet, + initDir: dir, + initFileName: file); + if (result is not null) TakeScreenshot(result); } private void ScreenshotClipboardMenuItem_Click(object sender, EventArgs e) @@ -1177,16 +1150,13 @@ namespace BizHawk.Client.EmuHawk private void SaveConfigAsMenuItem_Click(object sender, EventArgs e) { var (dir, file) = _getConfigPath().SplitPathToDirAndFile(); - using var sfd = new SaveFileDialog + var result = this.ShowFileSaveDialog( + filter: ConfigFileFSFilterSet, + initDir: dir, + initFileName: file); + if (result is not null) { - InitialDirectory = dir, - FileName = file, - Filter = ConfigFileFSFilterString - }; - - if (this.ShowDialogWithTempMute(sfd).IsOk()) - { - SaveConfig(sfd.FileName); + SaveConfig(result); AddOnScreenMessage("Copied settings"); } } @@ -1199,17 +1169,8 @@ namespace BizHawk.Client.EmuHawk private void LoadConfigFromMenuItem_Click(object sender, EventArgs e) { var (dir, file) = _getConfigPath().SplitPathToDirAndFile(); - using var ofd = new OpenFileDialog - { - InitialDirectory = dir, - FileName = file, - Filter = ConfigFileFSFilterString - }; - - if (this.ShowDialogWithTempMute(ofd).IsOk()) - { - LoadConfigFile(ofd.FileName); - } + var result = this.ShowFileOpenDialog(filter: ConfigFileFSFilterSet, initDir: dir!, initFileName: file!); + if (result is not null) LoadConfigFile(result); } private void ToolsSubMenu_DropDownOpened(object sender, EventArgs e) @@ -1561,16 +1522,14 @@ namespace BizHawk.Client.EmuHawk private void Ti83LoadTIFileMenuItem_Click(object sender, EventArgs e) { if (Emulator is not TI83 ti83) return; - using var ofd = new OpenFileDialog - { - Filter = TI83ProgramFilesFSFilterSet.ToString(), - InitialDirectory = Config.PathEntries.RomAbsolutePath(Emulator.SystemId), - RestoreDirectory = true - }; - if (!ofd.ShowDialog().IsOk()) return; + var result = this.ShowFileOpenDialog( + discardCWDChange: true, + filter: TI83ProgramFilesFSFilterSet, + initDir: Config.PathEntries.RomAbsolutePath(Emulator.SystemId)); + if (result is null) return; try { - ti83.LinkPort.SendFileToCalc(File.OpenRead(ofd.FileName), true); + ti83.LinkPort.SendFileToCalc(File.OpenRead(result), true); } catch (IOException ex) { @@ -1578,7 +1537,7 @@ namespace BizHawk.Client.EmuHawk $"Invalid file format. Reason: {ex.Message} \nForce transfer? This may cause the calculator to crash."; if (this.ShowMessageBox3(owner: null, message, "Upload Failed", EMsgBoxIcon.Question) == true) { - ti83.LinkPort.SendFileToCalc(File.OpenRead(ofd.FileName), false); + ti83.LinkPort.SendFileToCalc(File.OpenRead(result), false); } } } @@ -2179,22 +2138,19 @@ namespace BizHawk.Client.EmuHawk private void ZXSpectrumExportSnapshotMenuItemMenuItem_Click(object sender, EventArgs e) { - using var zxSnapExpDialog = new SaveFileDialog - { - DefaultExt = "szx", - Filter = ZXStateFilesFSFilterSet.ToString(), - RestoreDirectory = true, - SupportMultiDottedExtensions = true, - Title = "EXPERIMENTAL - Export 3rd party snapshot formats" - }; - try { - if (zxSnapExpDialog.ShowDialog().IsOk()) + var result = this.ShowFileSaveDialog( + discardCWDChange: true, + fileExt: "szx", +// SupportMultiDottedExtensions = true, // I think this should be enabled globally if we're going to do it --yoshi + filter: ZXStateFilesFSFilterSet, + initDir: Config.PathEntries.ToolsAbsolutePath()); + if (result is not null) { var speccy = (ZXSpectrum)Emulator; var snap = speccy.GetSZXSnapshot(); - File.WriteAllBytes(zxSnapExpDialog.FileName, snap); + File.WriteAllBytes(result, snap); } } catch (Exception) @@ -2489,7 +2445,7 @@ namespace BizHawk.Client.EmuHawk { if (MovieSession.Movie.IsActive()) { - using var form = new EditSubtitlesForm(this, MovieSession.Movie, MovieSession.ReadOnly); + using EditSubtitlesForm form = new(this, MovieSession.Movie, Config.PathEntries, readOnly: MovieSession.ReadOnly); form.ShowDialog(); } } diff --git a/src/BizHawk.Client.EmuHawk/MainForm.cs b/src/BizHawk.Client.EmuHawk/MainForm.cs index 2f380e9e07..75a4aeaac9 100644 --- a/src/BizHawk.Client.EmuHawk/MainForm.cs +++ b/src/BizHawk.Client.EmuHawk/MainForm.cs @@ -1609,29 +1609,24 @@ namespace BizHawk.Client.EmuHawk public bool RunLibretroCoreChooser() { - using var ofd = new OpenFileDialog(); - - if (Config.LibretroCore != null) + string initFileName = null; + string initDir; + if (Config.LibretroCore is not null) { - (ofd.InitialDirectory, ofd.FileName) = Config.LibretroCore.SplitPathToDirAndFile(); + (initDir, initFileName) = Config.LibretroCore.SplitPathToDirAndFile(); } else { - var initDir = Config.PathEntries.AbsolutePathForType(VSystemID.Raw.Libretro, "Cores"); + initDir = Config.PathEntries.AbsolutePathForType(VSystemID.Raw.Libretro, "Cores"); Directory.CreateDirectory(initDir); - ofd.InitialDirectory = initDir; } - - ofd.RestoreDirectory = true; - ofd.Filter = LibretroCoresFSFilterSet.ToString(); - - if (ofd.ShowDialog() == DialogResult.Cancel) - { - return false; - } - - Config.LibretroCore = ofd.FileName; - + var result = this.ShowFileOpenDialog( + discardCWDChange: true, + filter: LibretroCoresFSFilterSet, + initDir: initDir!, + initFileName: initFileName); + if (result is null) return false; + Config.LibretroCore = result; return true; } @@ -2399,25 +2394,20 @@ namespace BizHawk.Client.EmuHawk MainformMenu, new object/*?*/[] { c }); - public static readonly string ConfigFileFSFilterString = new FilesystemFilter("Config File", new[] { "ini" }).ToString(); + public static readonly FilesystemFilterSet ConfigFileFSFilterSet = new(new FilesystemFilter("Config File", new[] { "ini" })) + { + AppendAllFilesEntry = false, + }; private void OpenRom() { - using var ofd = new OpenFileDialog - { - InitialDirectory = Config.PathEntries.RomAbsolutePath(Emulator.SystemId), - Filter = RomLoader.RomFilter.ToString(), - RestoreDirectory = false, - FilterIndex = _lastOpenRomFilter - }; - - if (this.ShowDialogWithTempMute(ofd) != DialogResult.OK) return; - - var file = new FileInfo(ofd.FileName); - _lastOpenRomFilter = ofd.FilterIndex; - - var lra = new LoadRomArgs { OpenAdvanced = new OpenAdvanced_OpenRom { Path = file.FullName } }; - LoadRom(file.FullName, lra); + var result = this.ShowFileOpenDialog( + filter: RomLoader.RomFilter, + filterIndex: ref _lastOpenRomFilter, + initDir: Config.PathEntries.RomAbsolutePath(Emulator.SystemId)); + if (result is null) return; + var filePath = new FileInfo(result).FullName; + LoadRom(filePath, new LoadRomArgs { OpenAdvanced = new OpenAdvanced_OpenRom { Path = filePath } }); } private void CoreSyncSettings(object sender, RomLoader.SettingsLoadArgs e) @@ -3422,20 +3412,16 @@ namespace BizHawk.Client.EmuHawk } else { - using SaveFileDialog sfd = new() - { - FileName = $"{Game.FilesystemSafeName()}.{ext}", - Filter = new FilesystemFilterSet(new FilesystemFilter(ext, new[] { ext })).ToString(), - InitialDirectory = Config.PathEntries.AvAbsolutePath(), - }; - - if (this.ShowDialogWithTempMute(sfd) == DialogResult.Cancel) + var result = this.ShowFileSaveDialog( + filter: new(new FilesystemFilter(ext, new[] { ext })), + initDir: Config.PathEntries.AvAbsolutePath(), + initFileName: $"{Game.FilesystemSafeName()}.{ext}"); + if (result is null) { aw.Dispose(); return; } - - pathForOpenFile = sfd.FileName; + pathForOpenFile = result; } aw.OpenFile(pathForOpenFile); @@ -4413,19 +4399,12 @@ namespace BizHawk.Client.EmuHawk file.Directory.Create(); } - using var sfd = new SaveFileDialog - { - AddExtension = true, - DefaultExt = "State", - Filter = EmuHawkSaveStatesFSFilterSet.ToString(), - InitialDirectory = path, - FileName = $"{SaveStatePrefix()}.QuickSave0.State" - }; - - if (this.ShowDialogWithTempMute(sfd) == DialogResult.OK) - { - SaveState(sfd.FileName, sfd.FileName); - } + var result = this.ShowFileSaveDialog( + fileExt: "State", + filter: EmuHawkSaveStatesFSFilterSet, + initDir: path, + initFileName: $"{SaveStatePrefix()}.QuickSave0.State"); + if (result is not null) SaveState(path: result, userFriendlyStateName: result); if (Tools.IsLoaded()) { @@ -4446,21 +4425,12 @@ namespace BizHawk.Client.EmuHawk return; } - using var ofd = new OpenFileDialog - { - InitialDirectory = Config.PathEntries.SaveStateAbsolutePath(Game.System), - Filter = EmuHawkSaveStatesFSFilterSet.ToString(), - RestoreDirectory = true - }; - - if (this.ShowDialogWithTempMute(ofd) != DialogResult.OK) return; - - if (!File.Exists(ofd.FileName)) - { - return; - } - - LoadState(ofd.FileName, Path.GetFileName(ofd.FileName)); + var result = this.ShowFileOpenDialog( + discardCWDChange: true, + filter: EmuHawkSaveStatesFSFilterSet, + initDir: Config.PathEntries.SaveStateAbsolutePath(Game.System)); + if (result is null || !File.Exists(result)) return; + LoadState(result, Path.GetFileName(result)); } private void SelectSlot(int slot) @@ -4662,6 +4632,53 @@ namespace BizHawk.Client.EmuHawk public IDialogController DialogController => this; + public IReadOnlyList/*?*/ ShowFileMultiOpenDialog( + IDialogParent dialogParent, + string/*?*/ filterStr, + ref int filterIndex, + string initDir, + bool discardCWDChange = false, + string/*?*/ initFileName = null, + bool maySelectMultiple = false, + string/*?*/ windowTitle = null) + { + using OpenFileDialog ofd = new() + { + FileName = initFileName ?? string.Empty, + Filter = filterStr ?? string.Empty, + FilterIndex = filterIndex, + InitialDirectory = initDir, + Multiselect = maySelectMultiple, + RestoreDirectory = discardCWDChange, + Title = windowTitle ?? string.Empty, + }; + var result = dialogParent.ShowDialogWithTempMute(ofd); + filterIndex = ofd.FilterIndex; + return result.IsOk() && ofd.FileNames.Length is not 0 ? ofd.FileNames : null; + } + + public string/*?*/ ShowFileSaveDialog( + IDialogParent dialogParent, + bool discardCWDChange, + string/*?*/ fileExt, + string/*?*/ filterStr, + string initDir, + string/*?*/ initFileName, + bool muteOverwriteWarning) + { + using SaveFileDialog sfd = new() + { + DefaultExt = fileExt ?? string.Empty, + FileName = initFileName ?? string.Empty, + Filter = filterStr ?? string.Empty, + InitialDirectory = initDir, + OverwritePrompt = !muteOverwriteWarning, + RestoreDirectory = discardCWDChange, + }; + var result = dialogParent.ShowDialogWithTempMute(sfd); + return result.IsOk() ? sfd.FileName : null; + } + public void ShowMessageBox( IDialogParent/*?*/ owner, string text, diff --git a/src/BizHawk.Client.EmuHawk/OpenAdvancedChooser.cs b/src/BizHawk.Client.EmuHawk/OpenAdvancedChooser.cs index 266ed6ab89..3cc4c8feb9 100644 --- a/src/BizHawk.Client.EmuHawk/OpenAdvancedChooser.cs +++ b/src/BizHawk.Client.EmuHawk/OpenAdvancedChooser.cs @@ -38,7 +38,7 @@ namespace BizHawk.Client.EmuHawk public AdvancedRomLoaderType Result; - public string SuggestedExtensionFilter; + public FilesystemFilterSet SuggestedExtensionFilter = null; public OpenAdvancedChooser(IDialogController dialogController, Config config, Func createCoreComm, IGameInfo game, Func libretroCoreChooserCallback) { @@ -113,7 +113,7 @@ namespace BizHawk.Client.EmuHawk { var entries = new List { new FilesystemFilter("ROMs", _currentDescription.ValidExtensions.Split('|')) }; if (!_currentDescription.NeedsArchives) entries.Add(FilesystemFilter.Archives); // "needs archives" means the relevant archive extensions are already in the list, and we shouldn't scan archives for roms - SuggestedExtensionFilter = new FilesystemFilterSet(entries.ToArray()).ToString(); + SuggestedExtensionFilter = new(entries.ToArray()); Result = AdvancedRomLoaderType.LibretroLaunchGame; DialogResult = DialogResult.OK; Close(); diff --git a/src/BizHawk.Client.EmuHawk/config/DisplayConfig.cs b/src/BizHawk.Client.EmuHawk/config/DisplayConfig.cs index 16bcf4674d..48f80204a2 100755 --- a/src/BizHawk.Client.EmuHawk/config/DisplayConfig.cs +++ b/src/BizHawk.Client.EmuHawk/config/DisplayConfig.cs @@ -268,15 +268,14 @@ namespace BizHawk.Client.EmuHawk private void BtnSelectUserFilter_Click(object sender, EventArgs e) { - using var ofd = new OpenFileDialog - { - Filter = CgShaderPresetsFSFilterSet.ToString(), - FileName = _pathSelection - }; - if (!this.ShowDialogAsChild(ofd).IsOk()) return; + var result = this.ShowFileOpenDialog( + filter: CgShaderPresetsFSFilterSet, + initDir: Path.GetDirectoryName(_pathSelection)!, + initFileName: _pathSelection); + if (result is null) return; rbUser.Checked = true; - var choice = Path.GetFullPath(ofd.FileName); + var choice = Path.GetFullPath(result); //test the preset using (var stream = File.OpenRead(choice)) diff --git a/src/BizHawk.Client.EmuHawk/config/FirmwaresConfig.cs b/src/BizHawk.Client.EmuHawk/config/FirmwaresConfig.cs index 1006fcda95..43f36ac8a9 100644 --- a/src/BizHawk.Client.EmuHawk/config/FirmwaresConfig.cs +++ b/src/BizHawk.Client.EmuHawk/config/FirmwaresConfig.cs @@ -430,25 +430,24 @@ namespace BizHawk.Client.EmuHawk private void TsmiSetCustomization_Click(object sender, EventArgs e) { - using var ofd = new OpenFileDialog - { - InitialDirectory = _currSelectorDir, - RestoreDirectory = true - }; + var result = this.ShowFileOpenDialog( + discardCWDChange: true, + initDir: _currSelectorDir); + if (result is null) return; + string firmwarePath = _pathEntries.FirmwareAbsolutePath(); - if (!ofd.ShowDialog().IsOk()) return; // remember the location we selected this firmware from, maybe there are others - _currSelectorDir = Path.GetDirectoryName(ofd.FileName); + _currSelectorDir = Path.GetDirectoryName(result); try { - using var hf = new HawkFile(ofd.FileName); + using HawkFile hf = new(result); // for each selected item, set the user choice (even though multiple selection for this operation is no longer allowed) foreach (ListViewItem lvi in lvFirmwares.SelectedItems) { var fr = (FirmwareRecord) lvi.Tag; - string filePath = ofd.FileName; + var filePath = result; // if the selected file is an archive, allow the user to pick the inside file // to always be copied to the global firmwares directory @@ -486,7 +485,7 @@ namespace BizHawk.Client.EmuHawk { var fi = new FileInfo(filePath); filePath = Path.Combine(firmwarePath, fi.Name); - File.Copy(ofd.FileName, filePath); + File.Copy(result, filePath); } catch (Exception ex) { @@ -582,13 +581,8 @@ namespace BizHawk.Client.EmuHawk private void TbbImport_Click(object sender, EventArgs e) { - using var ofd = new OpenFileDialog { Multiselect = true }; - if (ofd.ShowDialog() != DialogResult.OK) - { - return; - } - - RunImportJob(ofd.FileNames); + var result = this.ShowFileMultiOpenDialog(initDir: _pathEntries.FirmwareAbsolutePath()); + if (result is not null) RunImportJob(result); } private bool RunImportJobSingle(string basePath, string f, ref string errors) diff --git a/src/BizHawk.Client.EmuHawk/config/GB/ColorChooserForm.cs b/src/BizHawk.Client.EmuHawk/config/GB/ColorChooserForm.cs index 3a7ea4d78d..575b2f9e4d 100644 --- a/src/BizHawk.Client.EmuHawk/config/GB/ColorChooserForm.cs +++ b/src/BizHawk.Client.EmuHawk/config/GB/ColorChooserForm.cs @@ -302,18 +302,11 @@ namespace BizHawk.Client.EmuHawk private void Button6_Click(object sender, EventArgs e) { - using var ofd = new OpenFileDialog - { - InitialDirectory = _config.PathEntries.ScreenshotAbsolutePathFor(VSystemID.Raw.GB), - Filter = FilesystemFilterSet.Palettes.ToString(), - RestoreDirectory = true - }; - - var result = ofd.ShowDialog(this); - if (result.IsOk()) - { - LoadColorFile(ofd.FileName, true); - } + var result = this.ShowFileOpenDialog( + discardCWDChange: true, + filter: FilesystemFilterSet.Palettes, + initDir: _config.PathEntries.ScreenshotAbsolutePathFor(VSystemID.Raw.GB)); + if (result is not null) LoadColorFile(result, alert: true); } private void ColorChooserForm_DragDrop(object sender, DragEventArgs e) @@ -338,19 +331,12 @@ namespace BizHawk.Client.EmuHawk private void Button7_Click(object sender, EventArgs e) { - using var sfd = new SaveFileDialog - { - InitialDirectory = _config.PathEntries.PalettesAbsolutePathFor(VSystemID.Raw.GB), - FileName = $"{_game.Name}.pal", - Filter = FilesystemFilterSet.Palettes.ToString(), - RestoreDirectory = true - }; - - var result = sfd.ShowDialog(this); - if (result.IsOk()) - { - SaveColorFile(sfd.FileName); - } + var result = this.ShowFileSaveDialog( + discardCWDChange: true, + filter: FilesystemFilterSet.Palettes, + initDir: _config.PathEntries.PalettesAbsolutePathFor(VSystemID.Raw.GB), + initFileName: $"{_game.Name}.pal"); + if (result is not null) SaveColorFile(result); } private void DefaultButton_Click(object sender, EventArgs e) diff --git a/src/BizHawk.Client.EmuHawk/config/GB/SameBoyColorChooserForm.cs b/src/BizHawk.Client.EmuHawk/config/GB/SameBoyColorChooserForm.cs index 9ec2822b48..19f35c7469 100644 --- a/src/BizHawk.Client.EmuHawk/config/GB/SameBoyColorChooserForm.cs +++ b/src/BizHawk.Client.EmuHawk/config/GB/SameBoyColorChooserForm.cs @@ -264,18 +264,11 @@ namespace BizHawk.Client.EmuHawk private void Button6_Click(object sender, EventArgs e) { - using var ofd = new OpenFileDialog - { - InitialDirectory = _config.PathEntries.ScreenshotAbsolutePathFor(VSystemID.Raw.GB), - Filter = FilesystemFilterSet.Palettes.ToString(), - RestoreDirectory = true - }; - - var result = ofd.ShowDialog(this); - if (result.IsOk()) - { - LoadColorFile(ofd.FileName, true); - } + var result = this.ShowFileOpenDialog( + discardCWDChange: true, + filter: FilesystemFilterSet.Palettes, + initDir: _config.PathEntries.ScreenshotAbsolutePathFor(VSystemID.Raw.GB)); + if (result is not null) LoadColorFile(result, alert: true); } private void ColorChooserForm_DragDrop(object sender, DragEventArgs e) @@ -300,19 +293,12 @@ namespace BizHawk.Client.EmuHawk private void Button7_Click(object sender, EventArgs e) { - using var sfd = new SaveFileDialog - { - InitialDirectory = _config.PathEntries.PalettesAbsolutePathFor(VSystemID.Raw.GB), - FileName = $"{_game.Name}.pal", - Filter = FilesystemFilterSet.Palettes.ToString(), - RestoreDirectory = true - }; - - var result = sfd.ShowDialog(this); - if (result.IsOk()) - { - SaveColorFile(sfd.FileName); - } + var result = this.ShowFileSaveDialog( + discardCWDChange: true, + filter: FilesystemFilterSet.Palettes, + initDir: _config.PathEntries.PalettesAbsolutePathFor(VSystemID.Raw.GB), + initFileName: $"{_game.Name}.pal"); + if (result is not null) SaveColorFile(result); } private void OK_Click(object sender, EventArgs e) diff --git a/src/BizHawk.Client.EmuHawk/config/NES/NESGraphicsConfig.cs b/src/BizHawk.Client.EmuHawk/config/NES/NESGraphicsConfig.cs index 2c62a28309..b68e93b6fd 100644 --- a/src/BizHawk.Client.EmuHawk/config/NES/NESGraphicsConfig.cs +++ b/src/BizHawk.Client.EmuHawk/config/NES/NESGraphicsConfig.cs @@ -59,20 +59,12 @@ namespace BizHawk.Client.EmuHawk private void BrowsePalette_Click(object sender, EventArgs e) { - using var ofd = new OpenFileDialog - { - InitialDirectory = _config.PathEntries.PalettesAbsolutePathFor(VSystemID.Raw.NES), - Filter = FilesystemFilterSet.Palettes.ToString(), - RestoreDirectory = true - }; - - var result = ofd.ShowDialog(); - if (result != DialogResult.OK) - { - return; - } - - PalettePath.Text = ofd.FileName; + var result = this.ShowFileOpenDialog( + discardCWDChange: true, + filter: FilesystemFilterSet.Palettes, + initDir: _config.PathEntries.PalettesAbsolutePathFor(VSystemID.Raw.NES)); + if (result is null) return; + PalettePath.Text = result; AutoLoadPalette.Checked = true; SetPaletteImage(); } diff --git a/src/BizHawk.Client.EmuHawk/config/NES/QuickNesConfig.cs b/src/BizHawk.Client.EmuHawk/config/NES/QuickNesConfig.cs index e81f17c3f3..63880cfeb2 100644 --- a/src/BizHawk.Client.EmuHawk/config/NES/QuickNesConfig.cs +++ b/src/BizHawk.Client.EmuHawk/config/NES/QuickNesConfig.cs @@ -83,20 +83,12 @@ namespace BizHawk.Client.EmuHawk private void ButtonPal_Click(object sender, EventArgs e) { - using var ofd = new OpenFileDialog - { - InitialDirectory = _config.PathEntries.PalettesAbsolutePathFor(VSystemID.Raw.NES), - Filter = FilesystemFilterSet.Palettes.ToString(), - RestoreDirectory = true - }; - - var result = ofd.ShowDialog(); - if (result != DialogResult.OK) - { - return; - } - - var palette = new HawkFile(ofd.FileName); + var result = this.ShowFileOpenDialog( + discardCWDChange: true, + filter: FilesystemFilterSet.Palettes, + initDir: _config.PathEntries.PalettesAbsolutePathFor(VSystemID.Raw.NES)); + if (result is null) return; + HawkFile palette = new(result); if (palette.Exists) { diff --git a/src/BizHawk.Client.EmuHawk/debug/FirmwareAutopatchDebugToolForm.cs b/src/BizHawk.Client.EmuHawk/debug/FirmwareAutopatchDebugToolForm.cs index 8081b2699d..2d42aeff84 100644 --- a/src/BizHawk.Client.EmuHawk/debug/FirmwareAutopatchDebugToolForm.cs +++ b/src/BizHawk.Client.EmuHawk/debug/FirmwareAutopatchDebugToolForm.cs @@ -32,9 +32,8 @@ namespace BizHawk.Client.EmuHawk.ForDebugging SzTextBoxEx txtBaseFile = new() { Size = new(224, 23) }; void ShowBaseFilePicker() { - using OpenFileDialog ofd = new() { InitialDirectory = Config!.PathEntries.FirmwareAbsolutePath() }; - this.ShowDialogAsChild(ofd); - txtBaseFile.Text = ofd.FileName; + var filename = this.ShowFileSaveDialog(initDir: Config!.PathEntries.FirmwareAbsolutePath()); + if (filename is not null) txtBaseFile.Text = filename; } CheckBoxEx cbDryRun = new() { Checked = true, Text = "dry run (skip writing to disk)" }; void DoPatch() diff --git a/src/BizHawk.Client.EmuHawk/debug/N64RomByteswapToolForm.cs b/src/BizHawk.Client.EmuHawk/debug/N64RomByteswapToolForm.cs index 9630adb2db..8e79324c3f 100644 --- a/src/BizHawk.Client.EmuHawk/debug/N64RomByteswapToolForm.cs +++ b/src/BizHawk.Client.EmuHawk/debug/N64RomByteswapToolForm.cs @@ -23,9 +23,8 @@ namespace BizHawk.Client.EmuHawk.ForDebugging SzTextBoxEx txtBaseFile = new() { Size = new(224, 23) }; void ChooseBaseFile() { - using OpenFileDialog ofd = new() { InitialDirectory = Config!.PathEntries.RomAbsolutePath() }; - this.ShowDialogAsChild(ofd); - txtBaseFile.Text = ofd.FileName; + var filename = this.ShowFileOpenDialog(initDir: Config!.PathEntries.RomAbsolutePath()); + if (filename is not null) txtBaseFile.Text = filename; } ComboBox comboFormats = new() { @@ -36,19 +35,16 @@ namespace BizHawk.Client.EmuHawk.ForDebugging SzTextBoxEx txtTargetFile = new() { Size = new(224, 23) }; void ChooseTargetFile() { - using SaveFileDialog sfd = new() - { - DefaultExt = comboFormats.SelectedIndex switch + var filename = this.ShowFileSaveDialog( + fileExt: comboFormats.SelectedIndex switch { 0 => "n64", 1 => "v64", 2 => "z64", - _ => string.Empty + _ => null }, - InitialDirectory = Config!.PathEntries.RomAbsolutePath(), - }; - this.ShowDialogAsChild(sfd); - txtTargetFile.Text = sfd.FileName; + initDir: Config!.PathEntries.RomAbsolutePath()); + if (filename is not null) txtTargetFile.Text = filename; } void DoConvert() { diff --git a/src/BizHawk.Client.EmuHawk/movie/EditSubtitlesForm.cs b/src/BizHawk.Client.EmuHawk/movie/EditSubtitlesForm.cs index 1f2503acf1..288441daaa 100644 --- a/src/BizHawk.Client.EmuHawk/movie/EditSubtitlesForm.cs +++ b/src/BizHawk.Client.EmuHawk/movie/EditSubtitlesForm.cs @@ -13,13 +13,16 @@ namespace BizHawk.Client.EmuHawk { private static readonly FilesystemFilterSet SubRipFilesFSFilterSet = new(new FilesystemFilter("SubRip Files", new[] { "srt" })); + private readonly PathEntryCollection _pathEntries; + private readonly IMovie _selectedMovie; private readonly bool _readOnly; public IDialogController DialogController { get; } - public EditSubtitlesForm(IDialogController dialogController, IMovie movie, bool readOnly) + public EditSubtitlesForm(IDialogController dialogController, IMovie movie, PathEntryCollection pathEntries, bool readOnly) { + _pathEntries = pathEntries; _selectedMovie = movie; _readOnly = readOnly; DialogController = dialogController; @@ -202,21 +205,10 @@ namespace BizHawk.Client.EmuHawk private void Export_Click(object sender, EventArgs e) { // Get file to save as - using var form = new SaveFileDialog - { - AddExtension = true, - Filter = SubRipFilesFSFilterSet.ToString(), - }; - - var result = form.ShowDialog(); - var fileName = form.FileName; - - form.Dispose(); - - if (result != DialogResult.OK) - { - return; - } + var fileName = this.ShowFileSaveDialog( + filter: SubRipFilesFSFilterSet, + initDir: _pathEntries.MovieAbsolutePath()); + if (fileName is null) return; double fps; try diff --git a/src/BizHawk.Client.EmuHawk/movie/PlayMovie.cs b/src/BizHawk.Client.EmuHawk/movie/PlayMovie.cs index 6a666aab30..31eb42566a 100644 --- a/src/BizHawk.Client.EmuHawk/movie/PlayMovie.cs +++ b/src/BizHawk.Client.EmuHawk/movie/PlayMovie.cs @@ -501,26 +501,24 @@ namespace BizHawk.Client.EmuHawk var indices = MovieView.SelectedIndices; if (indices.Count > 0) { - var s = new EditSubtitlesForm(DialogController, _movieList[MovieView.SelectedIndices[0]], true); + using EditSubtitlesForm s = new(DialogController, _movieList[MovieView.SelectedIndices[0]], _config.PathEntries, readOnly: true); s.Show(); } } private void BrowseMovies_Click(object sender, EventArgs e) { - using var ofd = new OpenFileDialog - { - Filter = MoviesFSFilterSet.ToString(), - InitialDirectory = _config.PathEntries.MovieAbsolutePath() - }; - if (!this.ShowDialogWithTempMute(ofd).IsOk()) return; - var file = new FileInfo(ofd.FileName); + var result = this.ShowFileOpenDialog( + filter: MoviesFSFilterSet, + initDir: _config.PathEntries.MovieAbsolutePath()); + if (result is null) return; + FileInfo file = new(result); if (!file.Exists) { return; } - int? index = AddMovieToList(ofd.FileName, true); + int? index = AddMovieToList(result, true); RefreshMovieList(); if (index.HasValue) { diff --git a/src/BizHawk.Client.EmuHawk/movie/RecordMovie.cs b/src/BizHawk.Client.EmuHawk/movie/RecordMovie.cs index 0a2e0d0565..2c9f09b3a6 100644 --- a/src/BizHawk.Client.EmuHawk/movie/RecordMovie.cs +++ b/src/BizHawk.Client.EmuHawk/movie/RecordMovie.cs @@ -198,21 +198,13 @@ namespace BizHawk.Client.EmuHawk } var filterset = _movieSession.Movie.GetFSFilterSet(); - using var sfd = new SaveFileDialog - { - InitialDirectory = movieFolderPath, - DefaultExt = $".{filterset.Filters[0].Extensions.First()}", - FileName = RecordBox.Text, - OverwritePrompt = false, - Filter = filterset.ToString(), - }; - - var result = this.ShowDialogWithTempMute(sfd); - if (result == DialogResult.OK - && !string.IsNullOrWhiteSpace(sfd.FileName)) - { - RecordBox.Text = sfd.FileName; - } + var result = this.ShowFileSaveDialog( + fileExt: $".{filterset.Filters[0].Extensions.First()}", + filter: filterset, + initDir: movieFolderPath, + initFileName: RecordBox.Text, + muteOverwriteWarning: true); + if (!string.IsNullOrWhiteSpace(result)) RecordBox.Text = result; } private void RecordMovie_Load(object sender, EventArgs e) diff --git a/src/BizHawk.Client.EmuHawk/tools/BatchRun.cs b/src/BizHawk.Client.EmuHawk/tools/BatchRun.cs index a6d9ddd180..bef6713fb2 100644 --- a/src/BizHawk.Client.EmuHawk/tools/BatchRun.cs +++ b/src/BizHawk.Client.EmuHawk/tools/BatchRun.cs @@ -130,9 +130,9 @@ namespace BizHawk.Client.EmuHawk DialogController.ShowMessageBox("No results to save!"); return; } - using var sfd = new SaveFileDialog(); - if (!sfd.ShowDialog(this).IsOk()) return; - using TextWriter tw = new StreamWriter(sfd.FileName); + var result = this.ShowFileSaveDialog(initDir: _config.PathEntries.ToolsAbsolutePath()); + if (result is null) return; + using TextWriter tw = new StreamWriter(result); foreach (var r in _mostRecentResults) { r.DumpTo(tw); diff --git a/src/BizHawk.Client.EmuHawk/tools/CDL.cs b/src/BizHawk.Client.EmuHawk/tools/CDL.cs index d6da7836d3..385a7895c7 100644 --- a/src/BizHawk.Client.EmuHawk/tools/CDL.cs +++ b/src/BizHawk.Client.EmuHawk/tools/CDL.cs @@ -446,12 +446,10 @@ namespace BizHawk.Client.EmuHawk this.ModalMessageBox("Cannot disassemble with no CDL loaded!", "Alert"); return; } - - using var sfd = new SaveFileDialog(); - var result = sfd.ShowDialog(this); - if (result == DialogResult.OK) + var result = this.ShowFileSaveDialog(initDir: Config!.PathEntries.ToolsAbsolutePath()); + if (result is not null) { - using var fs = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write); + using var fs = new FileStream(result, FileMode.Create, FileAccess.Write); CodeDataLogger.DisassembleCDL(fs, _cdl); } } diff --git a/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs b/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs index 7ea3aef763..a916cc07ac 100644 --- a/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs +++ b/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs @@ -922,38 +922,24 @@ namespace BizHawk.Client.EmuHawk } private string GetBinarySaveFileFromUser() - { - using var sfd = new SaveFileDialog - { - Filter = (_domain.Name is ROM_DOMAIN_NAME + => this.ShowFileSaveDialog( + discardCWDChange: true, + filter: _domain.Name is ROM_DOMAIN_NAME ? CreateBinaryDumpFSFilterSet(Path.GetExtension(RomName).RemovePrefix('.')) - : BinFilesFSFilterSet).ToString(), - RestoreDirectory = true, - InitialDirectory = RomDirectory, - FileName = _domain.Name is ROM_DOMAIN_NAME + : BinFilesFSFilterSet, + initDir: RomDirectory, + initFileName: _domain.Name is ROM_DOMAIN_NAME ? RomName - : Game.FilesystemSafeName(), - }; - - var result = this.ShowDialogWithTempMute(sfd); - return result == DialogResult.OK ? sfd.FileName : ""; - } + : Game.FilesystemSafeName()) ?? string.Empty; private string GetSaveFileFromUser() - { - using var sfd = new SaveFileDialog - { - FileName = _domain.Name is ROM_DOMAIN_NAME + => this.ShowFileSaveDialog( + discardCWDChange: true, + filter: HexDumpsFSFilterSet, + initDir: RomDirectory, + initFileName: _domain.Name is ROM_DOMAIN_NAME ? $"{Path.GetFileNameWithoutExtension(RomName)}.txt" - : Game.FilesystemSafeName(), - Filter = HexDumpsFSFilterSet.ToString(), - InitialDirectory = RomDirectory, - RestoreDirectory = true - }; - - var result = this.ShowDialogWithTempMute(sfd); - return result == DialogResult.OK ? sfd.FileName : ""; - } + : Game.FilesystemSafeName()) ?? string.Empty; private void ResetScrollBar() { @@ -1275,15 +1261,11 @@ namespace BizHawk.Client.EmuHawk return; } - using var sfd = new OpenFileDialog - { - Filter = ImportableFSFilterSet.ToString(), - RestoreDirectory = true - }; - - if (this.ShowDialogWithTempMute(sfd) != DialogResult.OK) return; - - var path = sfd.FileName; + var path = this.ShowFileOpenDialog( + discardCWDChange: true, + filter: ImportableFSFilterSet, + initDir: Config!.PathEntries.ToolsAbsolutePath()); + if (path is null) return; using var inf = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read); long todo = Math.Min(inf.Length, _domain.Size); @@ -1318,19 +1300,14 @@ namespace BizHawk.Client.EmuHawk private void LoadTableFileMenuItem_Click(object sender, EventArgs e) { - string initialDirectory = Config.PathEntries.ToolsAbsolutePath(); - var romName = Config.RecentRoms.MostRecent.SubstringAfterLast('|'); - - using var ofd = new OpenFileDialog - { - FileName = $"{Path.GetFileNameWithoutExtension(romName)}.tbl", - InitialDirectory = initialDirectory, - Filter = TextTablesFSFilterSet.ToString(), - RestoreDirectory = false - }; - if (!this.ShowDialogWithTempMute(ofd).IsOk()) return; - LoadTable(ofd.FileName); - RecentTables.Add(ofd.FileName); + var result = this.ShowFileOpenDialog( + discardCWDChange: false, + filter: TextTablesFSFilterSet, + initDir: Config!.PathEntries.ToolsAbsolutePath(), + initFileName: $"{Path.GetFileNameWithoutExtension(Config.RecentRoms.MostRecent.SubstringAfterLast('|'))}.tbl"); + if (result is null) return; + LoadTable(result); + RecentTables.Add(result); GeneralUpdate(); } diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/FormsLuaLibrary.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/FormsLuaLibrary.cs index ebef86b64d..651db67236 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/FormsLuaLibrary.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/FormsLuaLibrary.cs @@ -7,6 +7,8 @@ using System.Linq; using System.Windows.Forms; using BizHawk.Client.Common; +using BizHawk.Common.PathExtensions; + using NLua; namespace BizHawk.Client.EmuHawk @@ -386,19 +388,14 @@ namespace BizHawk.Client.EmuHawk [LuaArbitraryStringParam] string initialDirectory = null, [LuaASCIIStringParam] string filter = null) { - var openFileDialog1 = new OpenFileDialog(); - if (initialDirectory is not null) openFileDialog1.InitialDirectory = FixString(initialDirectory); - if (fileName is not null) openFileDialog1.FileName = FixString(fileName); - - openFileDialog1.AddExtension = true; - openFileDialog1.Filter = filter ?? FilesystemFilter.AllFilesEntry; - - if (openFileDialog1.ShowDialog() == DialogResult.OK) - { - return UnFixString(openFileDialog1.FileName); - } - - return ""; + var initFileName = fileName is null ? null : FixString(fileName); + var initDir = initialDirectory is null ? null : FixString(initialDirectory); + if (initDir is null && initFileName is not null) initDir = Path.GetDirectoryName(initFileName); + var result = ((IDialogParent) MainForm).ShowFileOpenDialog( + filterStr: filter ?? FilesystemFilter.AllFilesEntry, + initDir: initDir ?? PathEntries.LuaAbsolutePath(), + initFileName: initFileName); + return result is not null ? UnFixString(result) : string.Empty; } [LuaMethodExample("local inforpic = forms.pictureBox( 333, 2, 48, 18, 24 );")] diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs index 782d54a900..566d619637 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs @@ -654,26 +654,23 @@ namespace BizHawk.Client.EmuHawk private FileInfo GetSaveFileFromUser() { - var sfd = new SaveFileDialog(); + string initDir; + string initFileName; if (!string.IsNullOrWhiteSpace(LuaImp.ScriptList.Filename)) { - (sfd.InitialDirectory, sfd.FileName, _) = LuaImp.ScriptList.Filename.SplitPathToDirFileAndExt(); - } - else if (!Game.IsNullInstance()) - { - sfd.FileName = Game.FilesystemSafeName(); - sfd.InitialDirectory = Config.PathEntries.LuaAbsolutePath(); + (initDir, initFileName, _) = LuaImp.ScriptList.Filename.SplitPathToDirFileAndExt(); } else { - sfd.FileName = "NULL"; - sfd.InitialDirectory = Config.PathEntries.LuaAbsolutePath(); + initDir = Config!.PathEntries.LuaAbsolutePath(); + initFileName = Game.IsNullInstance() ? "NULL" : Game.FilesystemSafeName(); } - - sfd.Filter = SessionsFSFilterSet.ToString(); - sfd.RestoreDirectory = true; - var result = this.ShowDialogWithTempMute(sfd); - return result.IsOk() ? new FileInfo(sfd.FileName) : null; + var result = this.ShowFileSaveDialog( + discardCWDChange: true, + filter: SessionsFSFilterSet, + initDir: initDir, + initFileName: initFileName); + return result is not null ? new FileInfo(result) : null; } private void SaveSessionAs() @@ -791,18 +788,11 @@ namespace BizHawk.Client.EmuHawk { var initDir = Config!.PathEntries.LuaAbsolutePath(); Directory.CreateDirectory(initDir); - var ofd = new OpenFileDialog - { - InitialDirectory = initDir, - Filter = SessionsFSFilterSet.ToString(), - RestoreDirectory = true, - Multiselect = false - }; - var result = this.ShowDialogWithTempMute(ofd); - if (result.IsOk() && !string.IsNullOrWhiteSpace(ofd.FileName)) - { - LoadLuaSession(ofd.FileName); - } + var result = this.ShowFileOpenDialog( + discardCWDChange: true, + filter: SessionsFSFilterSet, + initDir: initDir); + if (result is not null) LoadLuaSession(result); } private void SaveSessionMenuItem_Click(object sender, EventArgs e) @@ -850,26 +840,21 @@ namespace BizHawk.Client.EmuHawk initDir = Config!.PathEntries.LuaAbsolutePath(); ext = Path.GetFileNameWithoutExtension(Game.Name); } - var sfd = new SaveFileDialog - { - InitialDirectory = initDir, - DefaultExt = ".lua", - FileName = ext, - OverwritePrompt = true, - Filter = JustScriptsFSFilterSet.ToString(), - }; - - var result = this.ShowDialogWithTempMute(sfd); - if (!result.IsOk() || string.IsNullOrWhiteSpace(sfd.FileName)) return; + var result = this.ShowFileSaveDialog( + fileExt: ".lua", + filter: JustScriptsFSFilterSet, + initDir: initDir, + initFileName: ext); + if (string.IsNullOrWhiteSpace(result)) return; string defaultTemplate = "while true do\n\temu.frameadvance();\nend"; - File.WriteAllText(sfd.FileName, defaultTemplate); - LuaImp.ScriptList.Add(new LuaFile(Path.GetFileNameWithoutExtension(sfd.FileName), sfd.FileName)); - Config.RecentLua.Add(sfd.FileName); + File.WriteAllText(result, defaultTemplate); + LuaImp.ScriptList.Add(new LuaFile(Path.GetFileNameWithoutExtension(result), result)); + Config!.RecentLua.Add(result); UpdateDialog(); Process.Start(new ProcessStartInfo { Verb = "Open", - FileName = sfd.FileName + FileName = result, }); AddFileWatches(); } @@ -878,16 +863,12 @@ namespace BizHawk.Client.EmuHawk { var initDir = Config!.PathEntries.LuaAbsolutePath(); Directory.CreateDirectory(initDir); - var ofd = new OpenFileDialog - { - InitialDirectory = initDir, - Filter = ScriptsAndTextFilesFSFilterSet.ToString(), - RestoreDirectory = true, - Multiselect = true - }; - var result = this.ShowDialogWithTempMute(ofd); - if (!result.IsOk() || ofd.FileNames is null) return; - foreach (var file in ofd.FileNames) + var result = this.ShowFileMultiOpenDialog( + discardCWDChange: true, + filter: ScriptsAndTextFilesFSFilterSet, + initDir: initDir); + if (result is null) return; + foreach (var file in result) { LoadLuaFile(file); Config.RecentLua.Add(file); @@ -998,24 +979,21 @@ namespace BizHawk.Client.EmuHawk } var (dir, fileNoExt, _) = script.Path.SplitPathToDirFileAndExt(); - var sfd = new SaveFileDialog - { - InitialDirectory = dir, - DefaultExt = ".lua", - FileName = $"{fileNoExt} (1)", - OverwritePrompt = true, - Filter = JustScriptsFSFilterSet.ToString(), - }; - if (!sfd.ShowDialog().IsOk()) return; + var result = this.ShowFileSaveDialog( + fileExt: ".lua", + filter: JustScriptsFSFilterSet, + initDir: dir, + initFileName: $"{fileNoExt} (1)"); + if (result is null) return; string text = File.ReadAllText(script.Path); - File.WriteAllText(sfd.FileName, text); - LuaImp.ScriptList.Add(new LuaFile(Path.GetFileNameWithoutExtension(sfd.FileName), sfd.FileName)); - Config.RecentLua.Add(sfd.FileName); + File.WriteAllText(result, text); + LuaImp.ScriptList.Add(new LuaFile(Path.GetFileNameWithoutExtension(result), result)); + Config!.RecentLua.Add(result); UpdateDialog(); Process.Start(new ProcessStartInfo { Verb = "Open", - FileName = sfd.FileName + FileName = result, }); } } diff --git a/src/BizHawk.Client.EmuHawk/tools/Macros/MacroInput.cs b/src/BizHawk.Client.EmuHawk/tools/Macros/MacroInput.cs index b423dedbc1..bc8af317ac 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Macros/MacroInput.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Macros/MacroInput.cs @@ -265,12 +265,6 @@ namespace BizHawk.Client.EmuHawk private bool SaveMacroAs(MovieZone macro) { string suggestedFolder = SuggestedFolder(Config, Game); - using var dialog = new SaveFileDialog - { - InitialDirectory = suggestedFolder, - FileName = macro.Name, - Filter = MacrosFSFilterSet.ToString() - }; // Create directory? bool create = false; @@ -280,34 +274,34 @@ namespace BizHawk.Client.EmuHawk create = true; } - if (this.ShowDialogWithTempMute(dialog) != DialogResult.OK) + var result = this.ShowFileSaveDialog( + filter: MacrosFSFilterSet, + initDir: suggestedFolder, + initFileName: macro.Name); + if (result is null) { if (create) { - Directory.Delete(dialog.InitialDirectory); + Directory.Delete(suggestedFolder); } return false; } - macro.Save(dialog.FileName); - Config.RecentMacros.Add(dialog.FileName); + macro.Save(result); + Config!.RecentMacros.Add(result); return true; } private MovieZone LoadMacro(IEmulator emulator = null, ToolManager tools = null) { - using var dialog = new OpenFileDialog - { - InitialDirectory = SuggestedFolder(Config, Game), - Filter = MacrosFSFilterSet.ToString() - }; - - if (this.ShowDialogWithTempMute(dialog) != DialogResult.OK) return null; - - Config.RecentMacros.Add(dialog.FileName); - return new MovieZone(dialog.FileName, MainForm, emulator ?? Emulator, MovieSession, tools ?? Tools); + var result = this.ShowFileOpenDialog( + filter: MacrosFSFilterSet, + initDir: SuggestedFolder(Config, Game)); + if (result is null) return null; + Config!.RecentMacros.Add(result); + return new MovieZone(result, MainForm, emulator ?? Emulator, MovieSession, tools ?? Tools); } } } diff --git a/src/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskBundler.cs b/src/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskBundler.cs index 5b250b699b..34630bfc89 100644 --- a/src/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskBundler.cs +++ b/src/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskBundler.cs @@ -254,20 +254,14 @@ namespace BizHawk.Client.EmuHawk filename = Path.ChangeExtension(Game.FilesystemSafeName(), ".xml"); } - initialDirectory = Path.GetDirectoryName(filename); + initialDirectory = Path.GetDirectoryName(filename) ?? string.Empty; } - using var sfd = new SaveFileDialog - { - FileName = filename, - InitialDirectory = initialDirectory, - Filter = BundlesFSFilterSet.ToString(), - }; - - if (this.ShowDialogWithTempMute(sfd) != DialogResult.Cancel) - { - NameBox.Text = sfd.FileName; - } + var result = this.ShowFileSaveDialog( + filter: BundlesFSFilterSet, + initDir: initialDirectory, + initFileName: filename); + if (result is not null) NameBox.Text = result; } private void SystemDropDown_SelectedIndexChanged(object sender, EventArgs e) diff --git a/src/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskFileSelector.cs b/src/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskFileSelector.cs index cf202e2402..e287d594a8 100644 --- a/src/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskFileSelector.cs +++ b/src/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskFileSelector.cs @@ -72,20 +72,14 @@ namespace BizHawk.Client.EmuHawk private void BrowseButton_Click(object sender, EventArgs e) { - using var ofd = new OpenFileDialog - { - InitialDirectory = _pathEntries.RomAbsolutePath(), - Filter = RomLoader.RomFilter.ToString(), - RestoreDirectory = true - }; - - if (this.ShowDialogWithTempMute(ofd) != DialogResult.OK) return; - - var hawkPath = ofd.FileName; - + var hawkPath = this.ShowFileOpenDialog( + discardCWDChange: true, + filter: RomLoader.RomFilter, + initDir: _pathEntries.RomAbsolutePath()); + if (hawkPath is null) return; try { - var file = new FileInfo(ofd.FileName); + FileInfo file = new(hawkPath); var path = EmuHawkUtil.ResolveShortcut(file.FullName); using var hf = new HawkFile(path); diff --git a/src/BizHawk.Client.EmuHawk/tools/NES/NESMusicRipper.cs b/src/BizHawk.Client.EmuHawk/tools/NES/NESMusicRipper.cs index 13a0a53867..a97fcebf44 100644 --- a/src/BizHawk.Client.EmuHawk/tools/NES/NESMusicRipper.cs +++ b/src/BizHawk.Client.EmuHawk/tools/NES/NESMusicRipper.cs @@ -103,17 +103,12 @@ namespace BizHawk.Client.EmuHawk private void Export_Click(object sender, EventArgs e) { //acquire target - using var sfd = new SaveFileDialog - { - Filter = RenoiseFilesFSFilterSet.ToString(), - }; - if (sfd.ShowDialog().IsOk()) - { - return; - } + var outPath = this.ShowFileSaveDialog( + filter: RenoiseFilesFSFilterSet, + initDir: Config!.PathEntries.ToolsAbsolutePath()); + if (outPath is null) return; // configuration: - var outPath = sfd.FileName; string templatePath = Path.Combine(Path.GetDirectoryName(outPath) ?? "", "template.xrns"); int configuredPatternLength = int.Parse(txtPatternLength.Text); diff --git a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs index e8be2e1ba4..1bb81312cc 100644 --- a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs +++ b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs @@ -88,19 +88,11 @@ namespace BizHawk.Client.EmuHawk { filename = ""; } - - // need to be fancy here, so call the ofd constructor directly instead of helper - var ofd = new OpenFileDialog - { - FileName = filename, - InitialDirectory = Config.PathEntries.MovieAbsolutePath(), - Filter = MoviesFSFilterSet.ToString(), - }; - - if (this.ShowDialogWithTempMute(ofd).IsOk()) - { - LoadMovieFile(ofd.FileName, false); - } + var result = this.ShowFileOpenDialog( + filter: MoviesFSFilterSet, + initDir: Config!.PathEntries.MovieAbsolutePath(), + initFileName: filename); + if (result is not null) LoadMovieFile(result, askToSave: false); } /// @@ -792,11 +784,11 @@ namespace BizHawk.Client.EmuHawk { if (DialogController.ShowMessageBox2($"Bad data between frames {lastState} and {Emulator.Frame}. Save the relevant state (raw data)?", "Integrity Failed!")) { - var sfd = new SaveFileDialog { FileName = "integrity.fresh" }; - if (sfd.ShowDialog().IsOk()) + var result = this.ShowFileSaveDialog(initDir: Config!.PathEntries.ToolsAbsolutePath(), initFileName: "integrity.fresh"); + if (result is not null) { - File.WriteAllBytes(sfd.FileName, state); - var path = Path.ChangeExtension(sfd.FileName, ".greenzoned"); + File.WriteAllBytes(result, state); + var path = Path.ChangeExtension(result, ".greenzoned"); File.WriteAllBytes(path, greenZone); } } @@ -1054,7 +1046,7 @@ namespace BizHawk.Client.EmuHawk private void SubtitlesMenuItem_Click(object sender, EventArgs e) { - var form = new EditSubtitlesForm(DialogController, CurrentTasMovie, false); + using EditSubtitlesForm form = new(DialogController, CurrentTasMovie, Config!.PathEntries, readOnly: false); form.ShowDialog(); } diff --git a/src/BizHawk.Client.EmuHawk/tools/ToolFormBase.cs b/src/BizHawk.Client.EmuHawk/tools/ToolFormBase.cs index 4b8996b9ea..5916ae9ec7 100644 --- a/src/BizHawk.Client.EmuHawk/tools/ToolFormBase.cs +++ b/src/BizHawk.Client.EmuHawk/tools/ToolFormBase.cs @@ -81,33 +81,25 @@ namespace BizHawk.Client.EmuHawk public FileInfo OpenFileDialog(string currentFile, string path, FilesystemFilterSet filterSet) { Directory.CreateDirectory(path); - using var ofd = new OpenFileDialog - { - FileName = !string.IsNullOrWhiteSpace(currentFile) + var result = this.ShowFileOpenDialog( + discardCWDChange: true, + filter: filterSet, + initDir: path, + initFileName: !string.IsNullOrWhiteSpace(currentFile) ? Path.GetFileName(currentFile) - : $"{Game.FilesystemSafeName()}.{filterSet.Filters.FirstOrDefault()?.Extensions.FirstOrDefault()}", - InitialDirectory = path, - Filter = filterSet.ToString(), - RestoreDirectory = true - }; - - var result = this.ShowDialogWithTempMute(ofd); - return result.IsOk() ? new FileInfo(ofd.FileName) : null; + : $"{Game.FilesystemSafeName()}.{filterSet.Filters.FirstOrDefault()?.Extensions.FirstOrDefault()}"); + return result is not null ? new FileInfo(result) : null; } public static FileInfo SaveFileDialog(string currentFile, string path, FilesystemFilterSet filterSet, IDialogParent parent) { Directory.CreateDirectory(path); - using var sfd = new SaveFileDialog - { - FileName = Path.GetFileName(currentFile), - InitialDirectory = path, - Filter = filterSet.ToString(), - RestoreDirectory = true - }; - - var result = parent.ShowDialogWithTempMute(sfd); - return result.IsOk() ? new FileInfo(sfd.FileName) : null; + var result = parent.ShowFileSaveDialog( + discardCWDChange: true, + filter: filterSet, + initDir: path, + initFileName: Path.GetFileName(currentFile)); + return result is not null ? new FileInfo(result) : null; } public FileInfo GetWatchFileFromUser(string currentFile) diff --git a/src/BizHawk.Client.EmuHawk/tools/TraceLogger.cs b/src/BizHawk.Client.EmuHawk/tools/TraceLogger.cs index 734db44290..567097a5b5 100644 --- a/src/BizHawk.Client.EmuHawk/tools/TraceLogger.cs +++ b/src/BizHawk.Client.EmuHawk/tools/TraceLogger.cs @@ -285,27 +285,29 @@ namespace BizHawk.Client.EmuHawk private FileInfo GetFileFromUser() { - using var sfd = new SaveFileDialog(); + string initDir; + string initFileName; if (LogFile == null) { - sfd.FileName = Game.FilesystemSafeName() + _extension; - sfd.InitialDirectory = Config.PathEntries.LogAbsolutePath(); + initFileName = Game.FilesystemSafeName() + _extension; + initDir = Config!.PathEntries.LogAbsolutePath(); } else if (!string.IsNullOrWhiteSpace(LogFile.FullName)) { - sfd.FileName = Game.FilesystemSafeName(); - sfd.InitialDirectory = Path.GetDirectoryName(LogFile.FullName); + initFileName = Game.FilesystemSafeName(); + initDir = Path.GetDirectoryName(LogFile.FullName) ?? string.Empty; } else { - sfd.FileName = Path.GetFileNameWithoutExtension(LogFile.FullName); - sfd.InitialDirectory = Config.PathEntries.LogAbsolutePath(); + initFileName = Path.GetFileNameWithoutExtension(LogFile.FullName); + initDir = Config!.PathEntries.LogAbsolutePath(); } - - sfd.Filter = LogFilesFSFilterSet.ToString(); - sfd.RestoreDirectory = true; - var result = this.ShowDialogWithTempMute(sfd); - return result.IsOk() ? new FileInfo(sfd.FileName) : null; + var result = this.ShowFileSaveDialog( + discardCWDChange: true, + filter: LogFilesFSFilterSet, + initDir: initDir, + initFileName: initFileName); + return result is not null ? new FileInfo(result) : null; } private void SaveLogMenuItem_Click(object sender, EventArgs e)