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).
This commit is contained in:
Kurt 2024-09-30 23:56:09 -05:00
parent b187633ec6
commit 9479e8cb5f
2 changed files with 30 additions and 5 deletions

View file

@ -280,8 +280,20 @@ public partial class Main : Form
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

View file

@ -83,9 +83,13 @@ internal static class Program
private static string GetErrorMessage(Exception e)
{
return IsPluginError<IPlugin>(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<IPlugin>(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 @@ internal static class Program
{
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 @@ internal static class Program
&& 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
}