From 9479e8cb5fa2d5a6f024869e5aba550557296ec3 Mon Sep 17 00:00:00 2001 From: Kurt Date: Mon, 30 Sep 2024 23:56:09 -0500 Subject: [PATCH] Better catch program init exceptions (plugins) Discard plugins that fail to load, rather than aborting the entire plugin load operation Add friendly message for unzipping fail (no PKHeX.Core.dll self-extracted). --- PKHeX.WinForms/MainWindow/Main.cs | 16 ++++++++++++++-- PKHeX.WinForms/Program.cs | 19 ++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/PKHeX.WinForms/MainWindow/Main.cs b/PKHeX.WinForms/MainWindow/Main.cs index a0d23a0d2..8dd7f4dca 100644 --- a/PKHeX.WinForms/MainWindow/Main.cs +++ b/PKHeX.WinForms/MainWindow/Main.cs @@ -280,8 +280,20 @@ private void FormLoadPlugins() WinFormsUtil.Error(MsgPluginFailLoad, c); return; } - foreach (var p in Plugins.OrderBy(z => z.Priority)) - p.Initialize(C_SAV, PKME_Tabs, menuStrip1, Program.CurrentVersion); + + var list = Plugins.OrderBy(z => z.Priority).ToList(); + foreach (var p in list) + { + try + { + p.Initialize(C_SAV, PKME_Tabs, menuStrip1, Program.CurrentVersion); + } + catch (Exception ex) + { + WinFormsUtil.Error(MsgPluginFailLoad, ex); + Plugins.Remove(p); + } + } } // Main Menu Strip UI Functions diff --git a/PKHeX.WinForms/Program.cs b/PKHeX.WinForms/Program.cs index 970898189..6f493bc2b 100644 --- a/PKHeX.WinForms/Program.cs +++ b/PKHeX.WinForms/Program.cs @@ -83,9 +83,13 @@ private static void UIThreadException(object sender, ThreadExceptionEventArgs t) private static string GetErrorMessage(Exception e) { - return IsPluginError(e, out var pluginName) - ? $"An error occurred in a PKHeX plugin. Please report this error to the plugin author/maintainer.\n{pluginName}" - : "An error occurred in PKHeX. Please report this error to the PKHeX author."; + try + { + if (IsPluginError(e, out var pluginName)) + return $"An error occurred in a PKHeX plugin. Please report this error to the plugin author/maintainer.\n{pluginName}"; + } + catch { } + return "An error occurred in PKHeX. Please report this error to the PKHeX author."; } // Handle the UI exceptions by showing a dialog box, and asking the user if they wish to abort execution. @@ -100,6 +104,10 @@ private static void CurrentDomain_UnhandledException(object sender, UnhandledExc { Error("You have upgraded PKHeX incorrectly. Please delete PKHeX.Core.dll."); } + else if (IsPkhexCoreMissing(ex)) + { + Error("You have installed PKHeX incorrectly. Please ensure you have unzipped all files before running."); + } else if (ex != null) { var msg = GetErrorMessage(ex); @@ -185,5 +193,10 @@ private static bool IsOldPkhexCorePresent(Exception? ex) && File.Exists("PKHeX.Core.dll") && AssemblyName.GetAssemblyName("PKHeX.Core.dll").Version < CurrentVersion; } + + private static bool IsPkhexCoreMissing(Exception? ex) + { + return ex is FileNotFoundException { FileName: {} n } && n.Contains("PKHeX.Core"); + } #endif }