From ac91285a1c231de6b70e78e114a66cf104f0e407 Mon Sep 17 00:00:00 2001 From: SuuperW Date: Mon, 18 Sep 2023 21:57:38 -0500 Subject: [PATCH] Create IToolManager, as part of moving LuaLibraries to BizHawk.Client.Common (to support testing). --- .../tools/Interfaces/IToolManager.cs | 124 ++++++++++++++++++ src/BizHawk.Client.EmuHawk/Api/ApiManager.cs | 8 +- .../Api/Libraries/ToolApi.cs | 4 +- .../tools/ToolManager.cs | 4 +- 4 files changed, 132 insertions(+), 8 deletions(-) create mode 100644 src/BizHawk.Client.Common/tools/Interfaces/IToolManager.cs diff --git a/src/BizHawk.Client.Common/tools/Interfaces/IToolManager.cs b/src/BizHawk.Client.Common/tools/Interfaces/IToolManager.cs new file mode 100644 index 0000000000..906235fdbe --- /dev/null +++ b/src/BizHawk.Client.Common/tools/Interfaces/IToolManager.cs @@ -0,0 +1,124 @@ +using System; +using System.Collections.Generic; +using System.Drawing; + +using BizHawk.Emulation.Common; + +namespace BizHawk.Client.Common +{ + public interface IToolManager + { + /// + /// Loads the tool dialog T (T must implements ) , if it does not exist it will be created, if it is already open, it will be focused + /// This method should be used only if you can't use the generic one + /// + /// Type of tool you want to load + /// Define if the tool form has to get the focus or not (Default is true) + /// An instantiated + /// Raised if can't cast into IToolForm + IToolForm Load(Type toolType, bool focus = true); + + /// + /// Loads the tool dialog T (T must implement ) , if it does not exist it will be created, if it is already open, it will be focused + /// + /// Define if the tool form has to get the focus or not (Default is true) + /// Path to the .dll of the external tool + /// Type of tool you want to load + /// An instantiated + T Load(bool focus = true, string toolPath = "") + where T : class, IToolForm; + + /// Loads the external tool's entry form. + IExternalToolForm LoadExternalToolForm(string toolPath, string customFormTypeName, bool focus = true, bool skipExtToolWarning = false); + + void AutoLoad(); + + /// + /// Determines whether a given IToolForm is already loaded + /// + /// Type of tool to check + /// yo why do we have 4 versions of this, each with slightly different behaviour in edge cases --yoshi + bool IsLoaded() where T : IToolForm; + + bool IsLoaded(Type toolType); + + bool IsOnScreen(Point topLeft); + + /// + /// Returns true if an instance of T exists + /// + /// Type of tool to check + bool Has() where T : IToolForm; + + /// iff a tool of the given is active + bool Has(Type toolType); + + /// + /// Gets the instance of T, or creates and returns a new instance + /// + /// Type of tool to get + IToolForm Get() where T : class, IToolForm; + + /// + /// returns the instance of , regardless of whether it's loaded,
+ /// but doesn't create and load a new instance if it's not found + ///
+ /// + /// does not check is a class implementing ;
+ /// you may pass any class or interface + ///
+ IToolForm/*?*/ LazyGet(Type toolType); + + (Image/*?*/ Icon, string Name) GetIconAndNameFor(Type toolType); + + IEnumerable AvailableTools { get; } + + /// + /// Calls UpdateValues() on an instance of T, if it exists + /// + /// Type of tool to update + void UpdateValues() where T : IToolForm; + + void Restart(Config config, IEmulator emulator, IGameInfo game); + + /// + /// Calls Restart() on an instance of T, if it exists + /// + /// Type of tool to restart + void Restart() where T : IToolForm; + + /// + /// Runs AskSave on every tool dialog, false is returned if any tool returns false + /// + bool AskSave(); + + /// + /// If T exists, this call will close the tool, and remove it from memory + /// + /// Type of tool to close + void Close() where T : IToolForm; + + void Close(Type toolType); + + void Close(); + + void UpdateToolsBefore(); + + void UpdateToolsAfter(); + + void FastUpdateBefore(); + + void FastUpdateAfter(); + + bool IsAvailable(Type tool); + + bool IsAvailable(); + + void LoadRamWatch(bool loadDialog); + + string GenerateDefaultCheatFilename(); + + void UpdateCheatRelatedTools(object sender, CheatCollection.CheatListEventArgs e); + + } +} diff --git a/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs b/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs index 3eed6c0213..4493c801fa 100644 --- a/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs +++ b/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs @@ -39,7 +39,7 @@ namespace BizHawk.Client.EmuHawk DisplayManagerBase displayManager, InputManager inputManager, IMovieSession movieSession, - ToolManager toolManager, + IToolManager toolManager, Config config, IEmulator emulator, IGameInfo game) @@ -51,7 +51,7 @@ namespace BizHawk.Client.EmuHawk [typeof(DisplayManagerBase)] = displayManager, [typeof(InputManager)] = inputManager, [typeof(IMovieSession)] = movieSession, - [typeof(ToolManager)] = toolManager, + [typeof(IToolManager)] = toolManager, [typeof(Config)] = config, [typeof(IEmulator)] = emulator, [typeof(IGameInfo)] = game, @@ -73,7 +73,7 @@ namespace BizHawk.Client.EmuHawk DisplayManagerBase displayManager, InputManager inputManager, IMovieSession movieSession, - ToolManager toolManager, + IToolManager toolManager, Config config, IEmulator emulator, IGameInfo game) @@ -90,7 +90,7 @@ namespace BizHawk.Client.EmuHawk DisplayManagerBase displayManager, InputManager inputManager, IMovieSession movieSession, - ToolManager toolManager, + IToolManager toolManager, Config config, IEmulator emulator, IGameInfo game) diff --git a/src/BizHawk.Client.EmuHawk/Api/Libraries/ToolApi.cs b/src/BizHawk.Client.EmuHawk/Api/Libraries/ToolApi.cs index b46073ee06..6b7789ecc2 100644 --- a/src/BizHawk.Client.EmuHawk/Api/Libraries/ToolApi.cs +++ b/src/BizHawk.Client.EmuHawk/Api/Libraries/ToolApi.cs @@ -9,11 +9,11 @@ namespace BizHawk.Client.EmuHawk { public sealed class ToolApi : IToolApi { - private readonly ToolManager _toolManager; + private readonly IToolManager _toolManager; public IEnumerable AvailableTools => _toolManager.AvailableTools.ToList(); // defensive copy in case ToolManager's implementation changes - public ToolApi(ToolManager toolManager) => _toolManager = toolManager; + public ToolApi(IToolManager toolManager) => _toolManager = toolManager; public IToolForm GetTool(string name) { diff --git a/src/BizHawk.Client.EmuHawk/tools/ToolManager.cs b/src/BizHawk.Client.EmuHawk/tools/ToolManager.cs index b2c55fed72..18bfb0b91e 100644 --- a/src/BizHawk.Client.EmuHawk/tools/ToolManager.cs +++ b/src/BizHawk.Client.EmuHawk/tools/ToolManager.cs @@ -17,7 +17,7 @@ using BizHawk.WinForms.Controls; namespace BizHawk.Client.EmuHawk { - public class ToolManager + public class ToolManager : IToolManager { private readonly MainForm _owner; private Config _config; @@ -72,7 +72,7 @@ namespace BizHawk.Client.EmuHawk /// Define if the tool form has to get the focus or not (Default is true) /// An instantiated /// Raised if can't cast into IToolForm - internal IToolForm Load(Type toolType, bool focus = true) + public IToolForm Load(Type toolType, bool focus = true) { if (!typeof(IToolForm).IsAssignableFrom(toolType)) {