From 738c51d596e76772f0f2bbcc9ecb94901293b506 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sat, 14 Oct 2023 19:26:56 -0700 Subject: [PATCH] Save/Open file dialog init Initialize outside of object initializer, solves warnings if any property sets throw exceptions --- .../Controls/PKM Editor/VerticalTabControl.cs | 16 +++----- .../Controls/SAV Editor/BoxEditor.cs | 8 +++- .../Controls/SAV Editor/SAVEditor.cs | 7 +++- PKHeX.WinForms/Misc/ErrorWindow.cs | 12 +++--- .../Subforms/PKM Editors/RibbonEditor.cs | 4 +- PKHeX.WinForms/Subforms/ReportGrid.cs | 8 ++-- .../Save Editors/Gen5/SAV_CGearSkin.cs | 33 ++++++---------- .../Subforms/Save Editors/Gen5/SAV_Misc5.cs | 8 +++- .../Subforms/Save Editors/Gen6/SAV_Link6.cs | 8 ++-- .../Save Editors/Gen6/SAV_SecretBase.cs | 4 +- .../Save Editors/Gen7/SAV_Trainer7GG.cs | 22 +++++------ .../Save Editors/Gen8/SAV_BlockDump8.cs | 15 +++++--- .../Subforms/Save Editors/SAV_Wondercard.cs | 3 +- PKHeX.WinForms/Util/DevUtil.cs | 4 +- PKHeX.WinForms/Util/WinFormsUtil.cs | 38 ++++++++----------- 15 files changed, 88 insertions(+), 102 deletions(-) diff --git a/PKHeX.WinForms/Controls/PKM Editor/VerticalTabControl.cs b/PKHeX.WinForms/Controls/PKM Editor/VerticalTabControl.cs index 53798467b..61d2b48ee 100644 --- a/PKHeX.WinForms/Controls/PKM Editor/VerticalTabControl.cs +++ b/PKHeX.WinForms/Controls/PKM Editor/VerticalTabControl.cs @@ -32,11 +32,9 @@ protected override void OnDrawItem(DrawItemEventArgs e) e.DrawBackground(); } - using var flags = new StringFormat - { - Alignment = StringAlignment.Center, - LineAlignment = StringAlignment.Center, - }; + using var flags = new StringFormat(); + flags.Alignment = StringAlignment.Center; + flags.LineAlignment = StringAlignment.Center; using var text = new SolidBrush(ForeColor); var tab = TabPages[index]; graphics.DrawString(tab.Text, Font, text, bounds, flags); @@ -90,11 +88,9 @@ protected override void OnDrawItem(DrawItemEventArgs e) e.DrawBackground(); } - using var flags = new StringFormat - { - Alignment = StringAlignment.Center, - LineAlignment = StringAlignment.Center, - }; + using var flags = new StringFormat(); + flags.Alignment = StringAlignment.Center; + flags.LineAlignment = StringAlignment.Center; using var text = new SolidBrush(ForeColor); var tab = TabPages[index]; graphics.DrawString(tab.Text, Font, text, bounds, flags); diff --git a/PKHeX.WinForms/Controls/SAV Editor/BoxEditor.cs b/PKHeX.WinForms/Controls/SAV Editor/BoxEditor.cs index 88729445e..6fc730493 100644 --- a/PKHeX.WinForms/Controls/SAV Editor/BoxEditor.cs +++ b/PKHeX.WinForms/Controls/SAV Editor/BoxEditor.cs @@ -225,7 +225,9 @@ public bool SaveBoxBinary() if (dr == DialogResult.Yes) { - using var sfd = new SaveFileDialog { Filter = "Box Data|*.bin", FileName = "pcdata.bin" }; + using var sfd = new SaveFileDialog(); + sfd.Filter = "Box Data|*.bin"; + sfd.FileName = "pcdata.bin"; if (sfd.ShowDialog() != DialogResult.OK) return false; File.WriteAllBytes(sfd.FileName, SAV.GetPCBinary()); @@ -233,7 +235,9 @@ public bool SaveBoxBinary() } if (dr == DialogResult.No) { - using var sfd = new SaveFileDialog { Filter = "Box Data|*.bin", FileName = $"boxdata {CurrentBoxName}.bin" }; + using var sfd = new SaveFileDialog(); + sfd.Filter = "Box Data|*.bin"; + sfd.FileName = $"boxdata {CurrentBoxName}.bin"; if (sfd.ShowDialog() != DialogResult.OK) return false; File.WriteAllBytes(sfd.FileName, SAV.GetBoxBinary(CurrentBox)); diff --git a/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs b/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs index f6e457b30..2f1b634e8 100644 --- a/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs +++ b/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs @@ -724,7 +724,9 @@ private void B_JPEG_Click(object sender, EventArgs e) return; } string filename = $"{s6.JPEGTitle}'s picture"; - using var sfd = new SaveFileDialog { FileName = filename, Filter = "JPEG|*.jpeg" }; + using var sfd = new SaveFileDialog(); + sfd.FileName = filename; + sfd.Filter = "JPEG|*.jpeg"; if (sfd.ShowDialog() != DialogResult.OK) return; File.WriteAllBytes(sfd.FileName, jpeg); @@ -818,7 +820,8 @@ public bool ExportBackup() } var suggestion = Util.CleanFileName(SAV.Metadata.BAKName); - using var sfd = new SaveFileDialog { FileName = suggestion }; + using var sfd = new SaveFileDialog(); + sfd.FileName = suggestion; if (sfd.ShowDialog() != DialogResult.OK) return false; diff --git a/PKHeX.WinForms/Misc/ErrorWindow.cs b/PKHeX.WinForms/Misc/ErrorWindow.cs index ed974d69c..71cfd232c 100644 --- a/PKHeX.WinForms/Misc/ErrorWindow.cs +++ b/PKHeX.WinForms/Misc/ErrorWindow.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Text; using System.Windows.Forms; @@ -9,12 +9,10 @@ public sealed partial class ErrorWindow : Form public static DialogResult ShowErrorDialog(string friendlyMessage, Exception ex, bool allowContinue) { var lang = System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName; - using var dialog = new ErrorWindow(lang) - { - ShowContinue = allowContinue, - Message = friendlyMessage, - Error = ex, - }; + using var dialog = new ErrorWindow(lang); + dialog.ShowContinue = allowContinue; + dialog.Message = friendlyMessage; + dialog.Error = ex; var dialogResult = dialog.ShowDialog(); if (dialogResult == DialogResult.Abort) Environment.Exit(1); diff --git a/PKHeX.WinForms/Subforms/PKM Editors/RibbonEditor.cs b/PKHeX.WinForms/Subforms/PKM Editors/RibbonEditor.cs index 68519b8e7..0fb0f4ff3 100644 --- a/PKHeX.WinForms/Subforms/PKM Editors/RibbonEditor.cs +++ b/PKHeX.WinForms/Subforms/PKM Editors/RibbonEditor.cs @@ -204,9 +204,7 @@ private void AddRibbonNumericUpDown(RibbonInfo rib, int row, Control label) nud.ValueChanged += (sender, e) => { var controlName = PrefixPB + rib.Name; - var pb = FLP_Ribbons.Controls[controlName]; - if (pb is null) - throw new ArgumentException($"{controlName} not found in {FLP_Ribbons.Name}."); + var pb = FLP_Ribbons.Controls[controlName] ?? throw new ArgumentException($"{controlName} not found in {FLP_Ribbons.Name}."); pb.Visible = (rib.RibbonCount = (byte)nud.Value) != 0; pb.BackgroundImage = RibbonSpriteUtil.GetRibbonSprite(rib.Name, (int)nud.Maximum, (int)nud.Value); diff --git a/PKHeX.WinForms/Subforms/ReportGrid.cs b/PKHeX.WinForms/Subforms/ReportGrid.cs index de60bef62..e34f0d997 100644 --- a/PKHeX.WinForms/Subforms/ReportGrid.cs +++ b/PKHeX.WinForms/Subforms/ReportGrid.cs @@ -101,11 +101,9 @@ private void PromptSaveCSV(object sender, FormClosingEventArgs e) { if (WinFormsUtil.Prompt(MessageBoxButtons.YesNo, MsgReportExportCSV) != DialogResult.Yes) return; - using var savecsv = new SaveFileDialog - { - Filter = "Spreadsheet|*.csv", - FileName = "Box Data Dump.csv", - }; + using var savecsv = new SaveFileDialog(); + savecsv.Filter = "Spreadsheet|*.csv"; + savecsv.FileName = "Box Data Dump.csv"; if (savecsv.ShowDialog() == DialogResult.OK) { Hide(); diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen5/SAV_CGearSkin.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen5/SAV_CGearSkin.cs index 3283a9647..1fe288b11 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen5/SAV_CGearSkin.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen5/SAV_CGearSkin.cs @@ -28,12 +28,9 @@ public SAV_CGearSkin(SaveFile sav) private void B_ImportPNG_Click(object sender, EventArgs e) { - using var ofd = new OpenFileDialog - { - Filter = "PNG File|*.png", - FileName = "Background.png", - }; - + using var ofd = new OpenFileDialog(); + ofd.Filter = "PNG File|*.png"; + ofd.FileName = "Background.png"; if (ofd.ShowDialog() != DialogResult.OK) return; @@ -51,25 +48,19 @@ private void B_ImportPNG_Click(object sender, EventArgs e) private void B_ExportPNG_Click(object sender, EventArgs e) { - Image png = PB_Background.Image; - using var sfd = new SaveFileDialog - { - Filter = "PNG File|*.png", - FileName = "Background.png", - }; - + using var sfd = new SaveFileDialog(); + sfd.Filter = "PNG File|*.png"; + sfd.FileName = "Background.png"; if (sfd.ShowDialog() != DialogResult.OK) return; - png.Save(sfd.FileName, ImageFormat.Png); + PB_Background.Image.Save(sfd.FileName, ImageFormat.Png); } private void B_ImportCGB_Click(object sender, EventArgs e) { - using var ofd = new OpenFileDialog - { - Filter = CGearBackground.Filter + "|PokeStock C-Gear Skin|*.psk", - }; + using var ofd = new OpenFileDialog(); + ofd.Filter = CGearBackground.Filter + "|PokeStock C-Gear Skin|*.psk"; if (ofd.ShowDialog() != DialogResult.OK) return; @@ -89,10 +80,8 @@ private void B_ImportCGB_Click(object sender, EventArgs e) private void B_ExportCGB_Click(object sender, EventArgs e) { - using var sfd = new SaveFileDialog - { - Filter = CGearBackground.Filter, - }; + using var sfd = new SaveFileDialog(); + sfd.Filter = CGearBackground.Filter; if (sfd.ShowDialog() != DialogResult.OK) return; diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen5/SAV_Misc5.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen5/SAV_Misc5.cs index 0e6fddabb..845fbd2bc 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen5/SAV_Misc5.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen5/SAV_Misc5.cs @@ -769,7 +769,9 @@ private void B_UnlockAllMusicalProps_Click(object sender, EventArgs e) private void B_DumpFC_Click(object sender, EventArgs e) { - using var sfd = new SaveFileDialog { Filter = ForestCityBinFilter, FileName = string.Format(ForestCityBinPath, SAV.Version) }; + using var sfd = new SaveFileDialog(); + sfd.Filter = ForestCityBinFilter; + sfd.FileName = string.Format(ForestCityBinPath, SAV.Version); if (sfd.ShowDialog() != DialogResult.OK) return; @@ -779,7 +781,9 @@ private void B_DumpFC_Click(object sender, EventArgs e) private void B_ImportFC_Click(object sender, EventArgs e) { - using var ofd = new OpenFileDialog { Filter = ForestCityBinFilter, FileName = string.Format(ForestCityBinPath, SAV.Version) }; + using var ofd = new OpenFileDialog(); + ofd.Filter = ForestCityBinFilter; + ofd.FileName = string.Format(ForestCityBinPath, SAV.Version); if (ofd.ShowDialog() != DialogResult.OK) return; diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_Link6.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_Link6.cs index a5dbcb718..8edabb363 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_Link6.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_Link6.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using System.Linq; using System.Windows.Forms; @@ -42,7 +42,8 @@ private void B_Cancel_Click(object sender, EventArgs e) private void B_Import_Click(object sender, EventArgs e) { - using var ofd = new OpenFileDialog { Filter = PL6.Filter }; + using var ofd = new OpenFileDialog(); + ofd.Filter = PL6.Filter; if (ofd.ShowDialog() != DialogResult.OK) return; @@ -58,7 +59,8 @@ private void B_Import_Click(object sender, EventArgs e) private void B_Export_Click(object sender, EventArgs e) { - using var sfd = new SaveFileDialog { Filter = PL6.Filter }; + using var sfd = new SaveFileDialog(); + sfd.Filter = PL6.Filter; if (sfd.ShowDialog() != DialogResult.OK) return; diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_SecretBase.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_SecretBase.cs index bbab2b618..c82528817 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_SecretBase.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_SecretBase.cs @@ -418,7 +418,9 @@ private void B_Export_Click(object sender, EventArgs e) var tr = sb.TrainerName; if (string.IsNullOrWhiteSpace(tr)) tr = "Trainer"; - using var sfd = new SaveFileDialog { Filter = "Secret Base Data|*.sb6", FileName = $"{sb.BaseLocation:D2} - {Util.CleanFileName(tr)}.sb6" }; + using var sfd = new SaveFileDialog(); + sfd.Filter = "Secret Base Data|*.sb6"; + sfd.FileName = $"{sb.BaseLocation:D2} - {Util.CleanFileName(tr)}.sb6"; if (sfd.ShowDialog() != DialogResult.OK) return; diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7GG.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7GG.cs index c4d0da973..7db9f9d04 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7GG.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7GG.cs @@ -165,12 +165,10 @@ private void B_ExportGoFiles_Click(object sender, EventArgs e) private void B_Import_Click(object sender, EventArgs e) { - using var sfd = new OpenFileDialog - { - Filter = GoFilter, - FilterIndex = 0, - RestoreDirectory = true, - }; + using var sfd = new OpenFileDialog(); + sfd.Filter = GoFilter; + sfd.FilterIndex = 0; + sfd.RestoreDirectory = true; // Export if (sfd.ShowDialog() != DialogResult.OK) @@ -211,13 +209,11 @@ private void B_Export_Click(object sender, EventArgs e) index = Math.Clamp(index, 0, max); var data = Park[index]; - using var sfd = new SaveFileDialog - { - FileName = data.FileName, - Filter = GoFilter, - FilterIndex = 0, - RestoreDirectory = true, - }; + using var sfd = new SaveFileDialog(); + sfd.FileName = data.FileName; + sfd.Filter = GoFilter; + sfd.FilterIndex = 0; + sfd.RestoreDirectory = true; if (sfd.ShowDialog() != DialogResult.OK) return; diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_BlockDump8.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_BlockDump8.cs index 5f2eadac4..ce303589d 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_BlockDump8.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_BlockDump8.cs @@ -164,7 +164,8 @@ private void B_ImportFolder_Click(object sender, EventArgs e) private void B_ExportAllSingle_Click(object sender, EventArgs e) { - using var sfd = new SaveFileDialog { FileName = "raw.bin" }; + using var sfd = new SaveFileDialog(); + sfd.FileName = "raw.bin"; if (sfd.ShowDialog() != DialogResult.OK) return; @@ -186,7 +187,8 @@ private void B_ExportAllSingle_Click(object sender, EventArgs e) private void B_LoadOld_Click(object sender, EventArgs e) { - using var ofd = new OpenFileDialog { FileName = "main" }; + using var ofd = new OpenFileDialog(); + ofd.FileName = "main"; if (ofd.ShowDialog() != DialogResult.OK) return; TB_OldSAV.Text = ofd.FileName; @@ -196,7 +198,8 @@ private void B_LoadOld_Click(object sender, EventArgs e) private void B_LoadNew_Click(object sender, EventArgs e) { - using var ofd = new OpenFileDialog { FileName = "main" }; + using var ofd = new OpenFileDialog(); + ofd.FileName = "main"; if (ofd.ShowDialog() != DialogResult.OK) return; TB_NewSAV.Text = ofd.FileName; @@ -232,7 +235,8 @@ private void CompareSaves() private static void ExportSelectBlock(SCBlock block) { var name = GetBlockFileNameWithoutExtension(block); - using var sfd = new SaveFileDialog { FileName = $"{name}.bin" }; + using var sfd = new SaveFileDialog(); + sfd.FileName = $"{name}.bin"; if (sfd.ShowDialog() != DialogResult.OK) return; File.WriteAllBytes(sfd.FileName, block.Data); @@ -242,7 +246,8 @@ private static void ImportSelectBlock(SCBlock blockTarget) { var key = blockTarget.Key; var data = blockTarget.Data; - using var ofd = new OpenFileDialog { FileName = $"{key:X8}.bin" }; + using var ofd = new OpenFileDialog(); + ofd.FileName = $"{key:X8}.bin"; if (ofd.ShowDialog() != DialogResult.OK) return; diff --git a/PKHeX.WinForms/Subforms/Save Editors/SAV_Wondercard.cs b/PKHeX.WinForms/Subforms/Save Editors/SAV_Wondercard.cs index 3c765d9c9..e0964f5d7 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/SAV_Wondercard.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/SAV_Wondercard.cs @@ -142,7 +142,8 @@ private void SetCardID(int cardID) private void B_Import_Click(object sender, EventArgs e) { var fileFilter = WinFormsUtil.GetMysterGiftFilter(SAV.Context); - using var import = new OpenFileDialog { Filter = fileFilter }; + using var import = new OpenFileDialog(); + import.Filter = fileFilter; if (import.ShowDialog() != DialogResult.OK) return; diff --git a/PKHeX.WinForms/Util/DevUtil.cs b/PKHeX.WinForms/Util/DevUtil.cs index 6ae793960..8fd895bbe 100644 --- a/PKHeX.WinForms/Util/DevUtil.cs +++ b/PKHeX.WinForms/Util/DevUtil.cs @@ -166,9 +166,7 @@ private static string GetResourcePath(params string[] subdir) var path = Application.StartupPath; while (true) { - var parent = Directory.GetParent(path); - if (parent is null) - throw new DirectoryNotFoundException(); + var parent = Directory.GetParent(path) ?? throw new DirectoryNotFoundException(path); path = parent.FullName; if (path.EndsWith(repo)) return Path.Combine(path, Path.Combine(subdir)); diff --git a/PKHeX.WinForms/Util/WinFormsUtil.cs b/PKHeX.WinForms/Util/WinFormsUtil.cs index 7f512d590..5d2511c13 100644 --- a/PKHeX.WinForms/Util/WinFormsUtil.cs +++ b/PKHeX.WinForms/Util/WinFormsUtil.cs @@ -256,15 +256,13 @@ public static void AddSaveFileExtensions(IEnumerable exts) public static bool OpenSAVPKMDialog(IEnumerable extensions, out string? path) { string supported = string.Join(";", extensions.Select(s => $"*.{s}").Concat(new[] { "*.pk" })); - using var ofd = new OpenFileDialog - { - Filter = "All Files|*.*" + + using var ofd = new OpenFileDialog(); + ofd.Filter = "All Files|*.*" + $"|Supported Files (*.*)|main;*.bin;{supported};*.bak" + ExtraSaveExtensions + "|Save Files (*.sav)|main" + ExtraSaveExtensions + "|Decrypted PKM File (*.pk)|" + supported + "|Binary File|*.bin" + - "|Backup File|*.bak", - }; + "|Backup File|*.bak"; // Detect main SaveFile? sav = null; @@ -306,12 +304,10 @@ public static bool SavePKMDialog(PKM pk) (allowEncrypted ? $"|Encrypted PKM File|*.e{pkx[1..]}" : string.Empty) + "|Binary File|*.bin" + "|All Files|*.*"; - using var sfd = new SaveFileDialog - { - Filter = genericFilter, - DefaultExt = pkx, - FileName = Util.CleanFileName(pk.FileName), - }; + using var sfd = new SaveFileDialog(); + sfd.Filter = genericFilter; + sfd.DefaultExt = pkx; + sfd.FileName = Util.CleanFileName(pk.FileName); if (sfd.ShowDialog() != DialogResult.OK) return false; @@ -346,13 +342,11 @@ private static void SaveBackup(string path) /// Result of whether or not the file was saved. public static bool ExportSAVDialog(SaveFile sav, int currentBox = 0) { - using var sfd = new SaveFileDialog - { - Filter = sav.Metadata.Filter, - FileName = sav.Metadata.FileName, - FilterIndex = 1000, // default to last, All Files - RestoreDirectory = true, - }; + using var sfd = new SaveFileDialog(); + sfd.Filter = sav.Metadata.Filter; + sfd.FileName = sav.Metadata.FileName; + sfd.FilterIndex = 1000; // default to last, All Files + sfd.RestoreDirectory = true; if (Directory.Exists(sav.Metadata.FileFolder)) sfd.InitialDirectory = sav.Metadata.FileFolder; @@ -403,11 +397,9 @@ private static void ExportSAV(SaveFile sav, string path) /// Result of whether or not the file was saved. public static bool ExportMGDialog(DataMysteryGift gift) { - using var sfd = new SaveFileDialog - { - Filter = GetMysterGiftFilter(gift.Context), - FileName = Util.CleanFileName(gift.FileName), - }; + using var sfd = new SaveFileDialog(); + sfd.Filter = GetMysterGiftFilter(gift.Context); + sfd.FileName = Util.CleanFileName(gift.FileName); if (sfd.ShowDialog() != DialogResult.OK) return false;