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.
This commit is contained in:
YoshiRulz 2020-07-26 05:29:04 +10:00
parent 1fa7395e20
commit 911cb096de
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
11 changed files with 18 additions and 53 deletions

View File

@ -3,23 +3,26 @@ using System.Collections.Generic;
namespace BizHawk.Client.Common
{
public class ApiSubsetContainer : IApiContainer
public sealed class ApiContainer : IApiContainer
{
public Dictionary<Type, IExternalApi> 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<Type, IExternalApi> libs)
{
Libraries = libs;
}
public ApiContainer(Dictionary<Type, IExternalApi> libs) => Libraries = libs;
}
}

View File

@ -4,13 +4,13 @@ using NLua;
namespace BizHawk.Client.Common
{
/// <summary>Extends <see cref="LuaLibraryBase"/> by including an <see cref="ApiSubsetContainer"/> for the library to delegate its calls through. Some APIs may not be delegated.</summary>
/// <summary>Extends <see cref="LuaLibraryBase"/> by including an <see cref="ApiContainer"/> for the library to delegate its calls through.</summary>
public abstract class DelegatingLuaLibrary : LuaLibraryBase
{
protected DelegatingLuaLibrary(Lua lua) : base(lua) {}
protected DelegatingLuaLibrary(Lua lua, Action<string> logOutputCallback) : base(lua, logOutputCallback) {}
public ApiSubsetContainer APIs { protected get; set; }
public ApiContainer APIs { protected get; set; }
}
}

View File

@ -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<Type, IExternalApi> libs) : base(libs) {}
}
}

View File

@ -29,8 +29,8 @@ namespace BizHawk.Client.EmuHawk
Action<string> logCallback)
{
var libDict = new Dictionary<Type, IExternalApi>();
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)))

View File

@ -1,18 +0,0 @@
using System;
using NLua;
using BizHawk.Client.Common;
namespace BizHawk.Client.EmuHawk
{
/// <summary>As <see cref="DelegatingLuaLibrary"/>, but also includes EmuHawk APIs via an <see cref="ApiContainer"/>.</summary>
public abstract class DelegatingLuaLibraryEmu : DelegatingLuaLibrary
{
protected DelegatingLuaLibraryEmu(Lua lua) : base(lua) {}
protected DelegatingLuaLibraryEmu(Lua lua, Action<string> logOutputCallback) : base(lua, logOutputCallback) {}
public new ApiContainer APIs { protected get; set; }
}
}

View File

@ -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; }

View File

@ -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) { }

View File

@ -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) { }

View File

@ -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) { }

View File

@ -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) { }

View File

@ -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);