diff --git a/BizHawk.Client.EmuHawk/Api/ApiManager.cs b/BizHawk.Client.EmuHawk/Api/ApiManager.cs index ea14bf4d1d..fc41386aac 100644 --- a/BizHawk.Client.EmuHawk/Api/ApiManager.cs +++ b/BizHawk.Client.EmuHawk/Api/ApiManager.cs @@ -15,7 +15,7 @@ namespace BizHawk.Client.EmuHawk public static class ApiManager { private static ApiContainer container; - private static void Register(IEmulatorServiceProvider serviceProvider) + private static IExternalApiProvider Register(IEmulatorServiceProvider serviceProvider) { foreach (var api in Assembly.Load("BizHawk.Client.Common").GetTypes() .Concat(Assembly.Load("BizHawk.Client.ApiHawk").GetTypes()) @@ -27,13 +27,14 @@ namespace BizHawk.Client.EmuHawk Libraries.Add(api, instance); } container = new ApiContainer(Libraries); - GlobalWin.ApiProvider = new BasicApiProvider(container); + return new BasicApiProvider(container); } + private static readonly Dictionary Libraries = new Dictionary(); - public static void Restart(IEmulatorServiceProvider newServiceProvider) + public static IExternalApiProvider Restart(IEmulatorServiceProvider newServiceProvider) { Libraries.Clear(); - Register(newServiceProvider); + return Register(newServiceProvider); } } } diff --git a/BizHawk.Client.EmuHawk/GlobalWin.cs b/BizHawk.Client.EmuHawk/GlobalWin.cs index fa26cc5d71..2c2d78f161 100644 --- a/BizHawk.Client.EmuHawk/GlobalWin.cs +++ b/BizHawk.Client.EmuHawk/GlobalWin.cs @@ -1,5 +1,4 @@ using BizHawk.Bizware.BizwareGL; -using BizHawk.Client.ApiHawk; // ReSharper disable StyleCop.SA1401 namespace BizHawk.Client.EmuHawk @@ -8,7 +7,6 @@ namespace BizHawk.Client.EmuHawk { public static MainForm MainForm; public static ToolManager Tools; - public static BasicApiProvider ApiProvider; /// /// the IGL to be used for rendering diff --git a/BizHawk.Client.EmuHawk/MainForm.cs b/BizHawk.Client.EmuHawk/MainForm.cs index 90bf3f1be3..f5ca1d01b1 100644 --- a/BizHawk.Client.EmuHawk/MainForm.cs +++ b/BizHawk.Client.EmuHawk/MainForm.cs @@ -356,7 +356,7 @@ namespace BizHawk.Client.EmuHawk Sound.StartSound(); InputManager.RewireInputChain(); - GlobalWin.Tools = new ToolManager(this); + GlobalWin.Tools = new ToolManager(this, Emulator); RewireSound(); // Workaround for windows, location is -32000 when minimized, if they close it during this time, that's what gets saved @@ -3831,8 +3831,7 @@ namespace BizHawk.Client.EmuHawk } } - ApiManager.Restart(Emulator.ServiceProvider); - Tools.Restart(); + Tools.Restart(Emulator); if (Config.LoadCheatFileByGame) { @@ -4001,8 +4000,7 @@ namespace BizHawk.Client.EmuHawk Emulator = new NullEmulator(coreComm); Global.Game = GameInfo.NullInstance; - Tools.Restart(); - ApiManager.Restart(Emulator.ServiceProvider); + Tools.Restart(Emulator); RewireSound(); ClearHolds(); ToolFormBase.UpdateCheatRelatedTools(null, null); diff --git a/BizHawk.Client.EmuHawk/tools/ToolManager.cs b/BizHawk.Client.EmuHawk/tools/ToolManager.cs index a8ce97d472..c72f9c7073 100644 --- a/BizHawk.Client.EmuHawk/tools/ToolManager.cs +++ b/BizHawk.Client.EmuHawk/tools/ToolManager.cs @@ -19,6 +19,8 @@ namespace BizHawk.Client.EmuHawk public class ToolManager { private readonly Form _owner; + private IExternalApiProvider _apiProvider; + private IEmulator _emulator; // TODO: merge ToolHelper code where logical // For instance, add an IToolForm property called UsesCheats, so that a UpdateCheatRelatedTools() method can update all tools of this type @@ -28,10 +30,11 @@ namespace BizHawk.Client.EmuHawk /// /// Initializes a new instance of the class. /// - /// Form that handle the ToolManager - public ToolManager(Form owner) + public ToolManager(Form owner, IEmulator emulator) { _owner = owner; + _emulator = emulator; + _apiProvider = ApiManager.Restart(_emulator.ServiceProvider); } /// @@ -125,10 +128,10 @@ namespace BizHawk.Client.EmuHawk if (isExternal) { - ApiInjector.UpdateApis(GlobalWin.ApiProvider, newTool); + ApiInjector.UpdateApis(_apiProvider, newTool); } - ServiceInjector.UpdateServices(Global.Emulator.ServiceProvider, newTool); + ServiceInjector.UpdateServices(_emulator.ServiceProvider, newTool); string toolType = typeof(T).ToString(); // auto settings @@ -482,8 +485,10 @@ namespace BizHawk.Client.EmuHawk } } - public void Restart() + public void Restart(IEmulator emulator) { + _emulator = emulator; + _apiProvider = ApiManager.Restart(_emulator.ServiceProvider); // If Cheat tool is loaded, restarting will restart the list too anyway if (!Has()) { @@ -494,14 +499,14 @@ namespace BizHawk.Client.EmuHawk foreach (var tool in _tools) { - if (ServiceInjector.IsAvailable(Global.Emulator.ServiceProvider, tool.GetType())) + if (ServiceInjector.IsAvailable(_emulator.ServiceProvider, tool.GetType())) { - ServiceInjector.UpdateServices(Global.Emulator.ServiceProvider, tool); + ServiceInjector.UpdateServices(_emulator.ServiceProvider, tool); if ((tool.IsHandleCreated && !tool.IsDisposed) || tool is RamWatch) // Hack for RAM Watch - in display watches mode it wants to keep running even closed, it will handle disposed logic { if (tool is IExternalToolForm) - ApiInjector.UpdateApis(GlobalWin.ApiProvider, tool); + ApiInjector.UpdateApis(_apiProvider, tool); tool.Restart(); } } @@ -732,7 +737,7 @@ namespace BizHawk.Client.EmuHawk public bool IsAvailable(Type tool) { - if (!ServiceInjector.IsAvailable(Global.Emulator.ServiceProvider, tool) + if (!ServiceInjector.IsAvailable(_emulator.ServiceProvider, tool) || !LazyAsmTypes.Value.Contains(tool.AssemblyQualifiedName)) // not a tool { return false; @@ -744,8 +749,8 @@ namespace BizHawk.Client.EmuHawk return true; // no ToolAttribute on given type -> assumed all supported } - var displayName = Global.Emulator.DisplayName(); - var systemId = Global.Emulator.SystemId; + var displayName = _emulator.DisplayName(); + var systemId = _emulator.SystemId; return !attr.UnsupportedCores.Contains(displayName) // not unsupported && (!attr.SupportedSystems.Any() || attr.SupportedSystems.Contains(systemId)); // supported (no supported list -> assumed all supported) }