From 14a678b57d42e1e42b6c16f7afc46526cba050f7 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 18 Oct 2020 19:49:46 -0700 Subject: [PATCH] Revise save detect logic flow Double-Clicking SAV tab no longer hides any error message; now shows it like the auto-detect would. Move high-level detection method to Core --- PKHeX.Core/Saves/Util/SaveFinder.cs | 18 +++++++++++++++ PKHeX.WinForms/MainWindow/Main.cs | 36 +++++++++++------------------ 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/PKHeX.Core/Saves/Util/SaveFinder.cs b/PKHeX.Core/Saves/Util/SaveFinder.cs index 1942e7eb3..1e72becf7 100644 --- a/PKHeX.Core/Saves/Util/SaveFinder.cs +++ b/PKHeX.Core/Saves/Util/SaveFinder.cs @@ -159,5 +159,23 @@ namespace PKHeX.Core possible = possiblePaths; return true; } + + public static bool DetectSaveFile(out string path, out SaveFile? sav) => DetectSaveFile(out path, out sav, Environment.GetLogicalDrives()); + + public static bool DetectSaveFile(out string path, out SaveFile? sav, IReadOnlyList drives) + { + string errorMsg = string.Empty; + var result = FindMostRecentSaveFile(drives, ref errorMsg); + if (result == null) + { + path = errorMsg; + sav = null; + return false; + } + + path = result.FilePath!; + sav = result; + return File.Exists(path); + } } } diff --git a/PKHeX.WinForms/MainWindow/Main.cs b/PKHeX.WinForms/MainWindow/Main.cs index ea69cbe0f..a8f9d8b2f 100644 --- a/PKHeX.WinForms/MainWindow/Main.cs +++ b/PKHeX.WinForms/MainWindow/Main.cs @@ -187,13 +187,16 @@ namespace PKHeX.WinForms try #endif { - string? path = null; + string path = string.Empty; SaveFile? sav = null; - if (Settings.Default.DetectSaveOnStartup && !DetectSaveFile(out path, out sav)) - WinFormsUtil.Error(path); // `path` contains the error message + if (Settings.Default.DetectSaveOnStartup && !SaveFinder.DetectSaveFile(out path, out sav)) + { + if (!string.IsNullOrWhiteSpace(path)) + WinFormsUtil.Error(path); // `path` contains the error message + } bool savLoaded = false; - if (sav != null && path != null) + if (sav != null && path.Length != 0) { savLoaded = OpenSAV(sav, path); } @@ -1204,28 +1207,15 @@ namespace PKHeX.WinForms private void ClickSaveFileName(object sender, EventArgs e) { - if (!DetectSaveFile(out string path, out var sav)) - return; - if (WinFormsUtil.Prompt(MessageBoxButtons.YesNo, MsgFileLoadSaveDetectReload, path) == DialogResult.Yes) - LoadFile(sav, path); // load save - } - - private static bool DetectSaveFile(out string path, out SaveFile? sav) - { - string msg = string.Empty; - var result = SaveFinder.FindMostRecentSaveFile(Environment.GetLogicalDrives(), ref msg); - if (result == null) + if (!SaveFinder.DetectSaveFile(out string path, out var sav)) { - if (!string.IsNullOrWhiteSpace(msg)) - WinFormsUtil.Error(msg); - path = string.Empty; - sav = null; - return false; + if (!string.IsNullOrWhiteSpace(path)) + WinFormsUtil.Error(path); // `path` contains the error message + return; } - path = result.FilePath!; - sav = result; - return File.Exists(path); + if (WinFormsUtil.Prompt(MessageBoxButtons.YesNo, MsgFileLoadSaveDetectReload, path) == DialogResult.Yes) + LoadFile(sav, path); // load save } private static void PromptBackup()