From 56a9e333e963ac512c5e3a75fc7e070cd402c791 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Mon, 6 Sep 2021 15:51:11 +1000 Subject: [PATCH] Allow ToolManager.IsAvailable to check ext. tools --- src/BizHawk.Client.EmuHawk/MainForm.cs | 2 +- .../tools/ExternalToolManager.cs | 4 ++++ src/BizHawk.Client.EmuHawk/tools/ToolManager.cs | 15 ++++++--------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/BizHawk.Client.EmuHawk/MainForm.cs b/src/BizHawk.Client.EmuHawk/MainForm.cs index 566db423cb..7015c740d0 100644 --- a/src/BizHawk.Client.EmuHawk/MainForm.cs +++ b/src/BizHawk.Client.EmuHawk/MainForm.cs @@ -393,8 +393,8 @@ namespace BizHawk.Client.EmuHawk Controls.Add(_presentationPanel); Controls.SetChildIndex(_presentationPanel, 0); - Tools = new ToolManager(this, Config, DisplayManager, InputManager, Emulator, MovieSession, Game); ExtToolManager = new ExternalToolManager(Config.PathEntries, () => (EmuClientApi.SystemIdConverter.Convert(Emulator.SystemId), Game.Hash)); + Tools = new ToolManager(this, Config, DisplayManager, ExtToolManager, InputManager, Emulator, MovieSession, Game); // TODO GL - move these event handlers somewhere less obnoxious line in the On* overrides Load += (o, e) => diff --git a/src/BizHawk.Client.EmuHawk/tools/ExternalToolManager.cs b/src/BizHawk.Client.EmuHawk/tools/ExternalToolManager.cs index efd566cc2e..d90aa045b5 100644 --- a/src/BizHawk.Client.EmuHawk/tools/ExternalToolManager.cs +++ b/src/BizHawk.Client.EmuHawk/tools/ExternalToolManager.cs @@ -21,6 +21,8 @@ namespace BizHawk.Client.EmuHawk private readonly List MenuItems = new List(); + internal readonly IList PossibleExtToolTypeNames = new List(); + public ExternalToolManager(PathEntryCollection paths, Func<(CoreSystem System, string Hash)> getLoadedRomInfoCallback) { _getLoadedRomInfoCallback = getLoadedRomInfoCallback; @@ -53,6 +55,7 @@ namespace BizHawk.Client.EmuHawk internal void BuildToolStrip() { MenuItems.Clear(); + PossibleExtToolTypeNames.Clear(); if (DirectoryMonitor == null) return; DirectoryInfo di = new(DirectoryMonitor.Path); if (!di.Exists) return; @@ -93,6 +96,7 @@ namespace BizHawk.Client.EmuHawk } item.Text = toolAttribute.Name; item.Tag = (externalToolFile.Location, entryPoint.FullName); // Tag set => no errors (show custom icon even when disabled) + PossibleExtToolTypeNames.Add(entryPoint.AssemblyQualifiedName); if (applicabilityAttrs.Count == 1) { var (system, loadedRomHash) = _getLoadedRomInfoCallback(); diff --git a/src/BizHawk.Client.EmuHawk/tools/ToolManager.cs b/src/BizHawk.Client.EmuHawk/tools/ToolManager.cs index 323e9e62d0..1fb394ec91 100644 --- a/src/BizHawk.Client.EmuHawk/tools/ToolManager.cs +++ b/src/BizHawk.Client.EmuHawk/tools/ToolManager.cs @@ -21,6 +21,7 @@ namespace BizHawk.Client.EmuHawk private readonly MainForm _owner; private Config _config; private readonly DisplayManager _displayManager; + private readonly ExternalToolManager _extToolManager; private readonly InputManager _inputManager; private IExternalApiProvider _apiProvider; private IEmulator _emulator; @@ -45,6 +46,7 @@ namespace BizHawk.Client.EmuHawk MainForm owner, Config config, DisplayManager displayManager, + ExternalToolManager extToolManager, InputManager inputManager, IEmulator emulator, IMovieSession movieSession, @@ -53,6 +55,7 @@ namespace BizHawk.Client.EmuHawk _owner = owner; _config = config; _displayManager = displayManager; + _extToolManager = extToolManager; _inputManager = inputManager; _emulator = emulator; _movieSession = movieSession; @@ -731,18 +734,12 @@ namespace BizHawk.Client.EmuHawk } } - private static readonly Lazy> LazyAsmTypes = new Lazy>(() => - EmuHawk.ReflectionCache.Types // Confining the search to only EmuHawk, for now at least, we may want to broaden for external tools one day - .Select(t => t.AssemblyQualifiedName) - .ToList()); + private static readonly IList PossibleToolTypeNames = EmuHawk.ReflectionCache.Types.Select(t => t.AssemblyQualifiedName).ToList(); public bool IsAvailable(Type tool) { - if (!ServiceInjector.IsAvailable(_emulator.ServiceProvider, tool) - || !LazyAsmTypes.Value.Contains(tool.AssemblyQualifiedName)) // not a tool - { - return false; - } + if (!ServiceInjector.IsAvailable(_emulator.ServiceProvider, tool)) return false; + if (!PossibleToolTypeNames.Contains(tool.AssemblyQualifiedName) && !_extToolManager.PossibleExtToolTypeNames.Contains(tool.AssemblyQualifiedName)) return false; // not a tool ToolAttribute attr = tool.GetCustomAttributes(false).OfType().SingleOrDefault(); if (attr == null)