From a3faf6fbd7ed058dce1a437df73d8dc5ba711540 Mon Sep 17 00:00:00 2001 From: James Groom Date: Sat, 30 Mar 2024 07:12:24 +1000 Subject: [PATCH] Skip generating ext. tool menu items for files that aren't assemblies --- .../tools/ExternalToolManager.cs | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/BizHawk.Client.EmuHawk/tools/ExternalToolManager.cs b/src/BizHawk.Client.EmuHawk/tools/ExternalToolManager.cs index 8700cbdf63..b8b5c704d4 100644 --- a/src/BizHawk.Client.EmuHawk/tools/ExternalToolManager.cs +++ b/src/BizHawk.Client.EmuHawk/tools/ExternalToolManager.cs @@ -8,6 +8,8 @@ using System.Reflection; using System.Windows.Forms; using BizHawk.Client.Common; using BizHawk.Common; +using BizHawk.Common.PathExtensions; +using BizHawk.Common.StringExtensions; using BizHawk.Emulation.Common; namespace BizHawk.Client.EmuHawk @@ -109,12 +111,15 @@ namespace BizHawk.Client.EmuHawk if (DirectoryMonitor == null) return; DirectoryInfo di = new(DirectoryMonitor.Path); if (!di.Exists) return; - foreach (var fi in di.GetFiles("*.dll")) MenuItems.Add(GenerateToolTipFromFileName(fi.FullName)); + foreach (var fi in di.GetFiles("*.dll")) Process(fi.FullName); } /// Generates a from an assembly at containing an external tool. - /// a with its containing a - private ToolStripMenuItem GenerateToolTipFromFileName(string fileName) + /// + /// a with its containing a , or + /// if the file is not a .NET assembly + /// + private ToolStripMenuItem/*?*/ GenerateToolTipFromFileName(string fileName) { if (fileName == null) throw new Exception(); var item = new ToolStripMenuItem(Path.GetFileName(fileName)) @@ -186,6 +191,11 @@ namespace BizHawk.Client.EmuHawk if (!string.IsNullOrWhiteSpace(toolAttribute.Description)) item.ToolTipText = toolAttribute.Description; return item; } + catch (BadImageFormatException) + { + Console.WriteLine($"ignoring /{fileName.MakeRelativeTo(DirectoryMonitor.Path).RemovePrefix("./")} as it doesn't seem to be an assembly (are you not targeting `net48`?)"); + return null; + } catch (Exception e) { #if DEBUG @@ -196,7 +206,6 @@ namespace BizHawk.Client.EmuHawk #endif item.ToolTipText = e switch { - BadImageFormatException => "This assembly can't be loaded, probably because it's corrupt or targets an incompatible .NET runtime.", ExternalToolApplicabilityAttributeBase.DuplicateException => "The IExternalToolForm has conflicting applicability attributes.", ExternalToolAttribute.MissingException => "The assembly doesn't contain a class implementing IExternalToolForm and annotated with [ExternalTool].", ReflectionTypeLoadException => "Something went wrong while trying to load the assembly.", @@ -214,8 +223,12 @@ namespace BizHawk.Client.EmuHawk /// Object that raised the event /// Event arguments private void DirectoryMonitor_Created(object sender, FileSystemEventArgs e) + => Process(e.FullPath); + + private void Process(string fileName) { - MenuItems.Add(GenerateToolTipFromFileName(e.FullPath)); + var item = GenerateToolTipFromFileName(fileName); + if (item is not null) MenuItems.Add(item); } public IReadOnlyCollection ToolStripItems