From 911cb096de3a14679da5517b7e26e3a25211a0f3 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Sun, 26 Jul 2020 05:29:04 +1000 Subject: [PATCH] Merge ApiContainer and ApiSubsetContainer (also merge DelegatingLuaLibrary and DelegatingLuaLibraryEmu) Not sure why this wasn't done earlier, there doesn't seem to be any technical roadblocks. The contract of the API provider requires an implementation for IEmuClientApi et al. so they (all 15 interfaces) are free to be used from Client.Common. --- ...{ApiSubsetContainer.cs => ApiContainer.cs} | 13 ++++++++----- .../lua/DelegatingLuaLibrary.cs | 4 ++-- .../Api/ApiContainer.cs | 19 ------------------- src/BizHawk.Client.EmuHawk/Api/ApiManager.cs | 4 ++-- .../tools/Lua/DelegatingLuaLibraryEmu.cs | 18 ------------------ .../tools/Lua/Libraries/ClientLuaLibrary.cs | 2 +- .../tools/Lua/Libraries/CommLuaLibrary.cs | 2 +- .../tools/Lua/Libraries/GuiLuaLibrary.cs | 2 +- .../tools/Lua/Libraries/InputLuaLibrary.cs | 2 +- .../Lua/Libraries/SaveStateLuaLibrary.cs | 2 +- .../tools/Lua/Win32LuaLibraries.cs | 3 +-- 11 files changed, 18 insertions(+), 53 deletions(-) rename src/BizHawk.Client.Common/Api/{ApiSubsetContainer.cs => ApiContainer.cs} (61%) delete mode 100644 src/BizHawk.Client.EmuHawk/Api/ApiContainer.cs delete mode 100644 src/BizHawk.Client.EmuHawk/tools/Lua/DelegatingLuaLibraryEmu.cs diff --git a/src/BizHawk.Client.Common/Api/ApiSubsetContainer.cs b/src/BizHawk.Client.Common/Api/ApiContainer.cs similarity index 61% rename from src/BizHawk.Client.Common/Api/ApiSubsetContainer.cs rename to src/BizHawk.Client.Common/Api/ApiContainer.cs index d06fb45e0d..24e1c3e9a8 100644 --- a/src/BizHawk.Client.Common/Api/ApiSubsetContainer.cs +++ b/src/BizHawk.Client.Common/Api/ApiContainer.cs @@ -3,23 +3,26 @@ using System.Collections.Generic; namespace BizHawk.Client.Common { - public class ApiSubsetContainer : IApiContainer + public sealed class ApiContainer : IApiContainer { public Dictionary Libraries { get; set; } + public ICommApi Comm => (ICommApi) Libraries[typeof(ICommApi)]; + public IEmuClientApi EmuClient => (IEmuClientApi) Libraries[typeof(IEmuClientApi)]; public IEmulationApi Emulation => (IEmulationApi) Libraries[typeof(IEmulationApi)]; public IGameInfoApi GameInfo => (IGameInfoApi) Libraries[typeof(IGameInfoApi)]; + public IGuiApi Gui => (IGuiApi) Libraries[typeof(IGuiApi)]; + public IInputApi Input => (IInputApi) Libraries[typeof(IInputApi)]; public IJoypadApi Joypad => (IJoypadApi) Libraries[typeof(IJoypadApi)]; public IMemoryApi Memory => (IMemoryApi) Libraries[typeof(IMemoryApi)]; public IMemoryEventsApi MemoryEvents => (IMemoryEventsApi) Libraries[typeof(IMemoryEventsApi)]; public IMemorySaveStateApi MemorySaveState => (IMemorySaveStateApi) Libraries[typeof(IMemorySaveStateApi)]; public IMovieApi Movie => (IMovieApi) Libraries[typeof(IMovieApi)]; + public ISaveStateApi SaveState => (ISaveStateApi) Libraries[typeof(ISaveStateApi)]; public ISQLiteApi SQLite => (ISQLiteApi) Libraries[typeof(ISQLiteApi)]; public IUserDataApi UserData => (IUserDataApi) Libraries[typeof(IUserDataApi)]; + public IToolApi Tool => (IToolApi) Libraries[typeof(IToolApi)]; - public ApiSubsetContainer(Dictionary libs) - { - Libraries = libs; - } + public ApiContainer(Dictionary libs) => Libraries = libs; } } diff --git a/src/BizHawk.Client.Common/lua/DelegatingLuaLibrary.cs b/src/BizHawk.Client.Common/lua/DelegatingLuaLibrary.cs index dc921d6f44..575bdd011d 100644 --- a/src/BizHawk.Client.Common/lua/DelegatingLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/DelegatingLuaLibrary.cs @@ -4,13 +4,13 @@ using NLua; namespace BizHawk.Client.Common { - /// Extends by including an for the library to delegate its calls through. Some APIs may not be delegated. + /// Extends by including an for the library to delegate its calls through. public abstract class DelegatingLuaLibrary : LuaLibraryBase { protected DelegatingLuaLibrary(Lua lua) : base(lua) {} protected DelegatingLuaLibrary(Lua lua, Action logOutputCallback) : base(lua, logOutputCallback) {} - public ApiSubsetContainer APIs { protected get; set; } + public ApiContainer APIs { protected get; set; } } } diff --git a/src/BizHawk.Client.EmuHawk/Api/ApiContainer.cs b/src/BizHawk.Client.EmuHawk/Api/ApiContainer.cs deleted file mode 100644 index 22a4a12c2b..0000000000 --- a/src/BizHawk.Client.EmuHawk/Api/ApiContainer.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections.Generic; - -using BizHawk.Client.Common; - -namespace BizHawk.Client.EmuHawk -{ - public sealed class ApiContainer : ApiSubsetContainer - { - public ICommApi Comm => (ICommApi) Libraries[typeof(ICommApi)]; - public IEmuClientApi EmuClient => (IEmuClientApi) Libraries[typeof(IEmuClientApi)]; - public IGuiApi Gui => (IGuiApi) Libraries[typeof(IGuiApi)]; - public IInputApi Input => (IInputApi) Libraries[typeof(IInputApi)]; - public ISaveStateApi SaveState => (ISaveStateApi) Libraries[typeof(ISaveStateApi)]; - public IToolApi Tool => (IToolApi) Libraries[typeof(IToolApi)]; - - public ApiContainer(Dictionary libs) : base(libs) {} - } -} diff --git a/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs b/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs index 3a1806216d..a55d4ad846 100644 --- a/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs +++ b/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs @@ -29,8 +29,8 @@ namespace BizHawk.Client.EmuHawk Action logCallback) { var libDict = new Dictionary(); - foreach (var api in Assembly.GetAssembly(typeof(ApiSubsetContainer)).GetTypes() - .Concat(Assembly.GetAssembly(typeof(ApiContainer)).GetTypes()) + foreach (var api in Assembly.GetAssembly(typeof(IEmuClientApi)).GetTypes() + .Concat(Assembly.GetAssembly(typeof(EmuClientApi)).GetTypes()) .Where(t => /*t.IsClass && */t.IsSealed && typeof(IExternalApi).IsAssignableFrom(t) && ServiceInjector.IsAvailable(serviceProvider, t))) diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/DelegatingLuaLibraryEmu.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/DelegatingLuaLibraryEmu.cs deleted file mode 100644 index dc9f0c13e7..0000000000 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/DelegatingLuaLibraryEmu.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; - -using NLua; - -using BizHawk.Client.Common; - -namespace BizHawk.Client.EmuHawk -{ - /// As , but also includes EmuHawk APIs via an . - public abstract class DelegatingLuaLibraryEmu : DelegatingLuaLibrary - { - protected DelegatingLuaLibraryEmu(Lua lua) : base(lua) {} - - protected DelegatingLuaLibraryEmu(Lua lua, Action logOutputCallback) : base(lua, logOutputCallback) {} - - public new ApiContainer APIs { protected get; set; } - } -} diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/ClientLuaLibrary.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/ClientLuaLibrary.cs index 1ced36fdae..f20530b5b3 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/ClientLuaLibrary.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/ClientLuaLibrary.cs @@ -16,7 +16,7 @@ using NLua; namespace BizHawk.Client.EmuHawk { [Description("A library for manipulating the EmuHawk client UI")] - public sealed class ClientLuaLibrary : DelegatingLuaLibraryEmu + public sealed class ClientLuaLibrary : DelegatingLuaLibrary { [RequiredService] private IEmulator Emulator { get; set; } diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/CommLuaLibrary.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/CommLuaLibrary.cs index 19bd66c1a8..e07beb3371 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/CommLuaLibrary.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/CommLuaLibrary.cs @@ -7,7 +7,7 @@ using System.Text; namespace BizHawk.Client.EmuHawk { [Description("A library for communicating with other programs")] - public sealed class CommLuaLibrary : DelegatingLuaLibraryEmu + public sealed class CommLuaLibrary : DelegatingLuaLibrary { public CommLuaLibrary(Lua lua) : base(lua) { } diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/GuiLuaLibrary.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/GuiLuaLibrary.cs index 326459c93a..553ba29336 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/GuiLuaLibrary.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/GuiLuaLibrary.cs @@ -5,7 +5,7 @@ using BizHawk.Client.Common; namespace BizHawk.Client.EmuHawk { - public sealed class GuiLuaLibrary : DelegatingLuaLibraryEmu, IDisposable + public sealed class GuiLuaLibrary : DelegatingLuaLibrary, IDisposable { public GuiLuaLibrary(Lua lua) : base(lua) { } diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/InputLuaLibrary.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/InputLuaLibrary.cs index cf096e1145..3f54364b2c 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/InputLuaLibrary.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/InputLuaLibrary.cs @@ -6,7 +6,7 @@ using BizHawk.Client.Common; namespace BizHawk.Client.EmuHawk { - public sealed class InputLuaLibrary : DelegatingLuaLibraryEmu + public sealed class InputLuaLibrary : DelegatingLuaLibrary { public InputLuaLibrary(Lua lua) : base(lua) { } diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/SaveStateLuaLibrary.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/SaveStateLuaLibrary.cs index de084dd1f5..02936e8098 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/SaveStateLuaLibrary.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/SaveStateLuaLibrary.cs @@ -6,7 +6,7 @@ using BizHawk.Client.Common; namespace BizHawk.Client.EmuHawk { - public sealed class SaveStateLuaLibrary : DelegatingLuaLibraryEmu + public sealed class SaveStateLuaLibrary : DelegatingLuaLibrary { public SaveStateLuaLibrary(Lua lua) : base(lua) { } diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs index a8b01afdd0..276ea2b823 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs @@ -69,8 +69,7 @@ namespace BizHawk.Client.EmuHawk } ApiContainerInstance = ApiManager.RestartLua(mainForm, serviceProvider, ConsoleLuaLibrary.LogOutput); - if (instance is DelegatingLuaLibraryEmu dlgInstanceEmu) dlgInstanceEmu.APIs = ApiContainerInstance; // this is necessary as the property has the `new` modifier - else if (instance is DelegatingLuaLibrary dlgInstance) dlgInstance.APIs = ApiContainerInstance; + if (instance is DelegatingLuaLibrary dlgInstance) dlgInstance.APIs = ApiContainerInstance; EnumerateLuaFunctions(instance.Name, lib, instance); Libraries.Add(lib, instance);