Prepare for EmuHawk-tied libraries to delegate to ApiHawk

Made ApiContainer inherit from APISubsetContainer, added
DelegatingLuaLibraryEmu, and reworked init logic in EmuLuaLibrary
This commit is contained in:
YoshiRulz 2019-12-15 03:52:48 +10:00
parent 3099a11a04
commit 104c17e77c
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
5 changed files with 38 additions and 31 deletions

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public sealed class APISubsetContainer : IApiContainer public class APISubsetContainer : IApiContainer
{ {
public Dictionary<Type, IExternalApi> Libraries { get; set; } public Dictionary<Type, IExternalApi> Libraries { get; set; }

View File

@ -1,33 +1,18 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using BizHawk.Client.ApiHawk;
using BizHawk.Client.Common; using BizHawk.Client.Common;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {
public sealed class ApiContainer : IApiContainer public sealed class ApiContainer : APISubsetContainer
{ {
public IComm Comm => (IComm)Libraries[typeof(CommApi)]; public IComm Comm => (IComm) Libraries[typeof(CommApi)];
public IEmu Emu => (IEmu)Libraries[typeof(EmuApi)]; public IGui Gui => (IGui) Libraries[typeof(GuiApi)];
public IGameInfo GameInfo => (IGameInfo)Libraries[typeof(GameInfoApi)]; public IInput Input => (IInput) Libraries[typeof(InputApi)];
public IGui Gui => (IGui)Libraries[typeof(GuiApi)]; public ISaveState SaveState => (ISaveState) Libraries[typeof(SaveStateApi)];
public IInput Input => (IInput)Libraries[typeof(InputApi)]; public ITool Tool => (ITool) Libraries[typeof(ToolApi)];
public IJoypad Joypad => (IJoypad)Libraries[typeof(JoypadApi)];
public IMem Mem => (IMem)Libraries[typeof(MemApi)]; public ApiContainer(Dictionary<Type, IExternalApi> libs) : base(libs) {}
public IMemEvents MemEvents => (IMemEvents)Libraries[typeof(MemEventsApi)];
public IMemorySaveState MemorySaveState => (IMemorySaveState)Libraries[typeof(MemorySaveStateApi)];
public IInputMovie Movie => (IInputMovie)Libraries[typeof(MovieApi)];
public ISaveState SaveState => (ISaveState)Libraries[typeof(SaveStateApi)];
public ISql Sql => (ISql)Libraries[typeof(SqlApi)];
public ITool Tool => (ITool)Libraries[typeof(ToolApi)];
public IUserData UserData => (IUserData)Libraries[typeof(UserDataApi)];
public Dictionary<Type, IExternalApi> Libraries { get; set; }
public ApiContainer(Dictionary<Type, IExternalApi> libs)
{
Libraries = libs;
}
} }
} }

View File

@ -917,6 +917,7 @@
<Compile Include="tools\HexEditor\HexFind.Designer.cs"> <Compile Include="tools\HexEditor\HexFind.Designer.cs">
<DependentUpon>HexFind.cs</DependentUpon> <DependentUpon>HexFind.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="tools\Lua\Libraries\DelegatingLuaLibraryEmu.cs" />
<Compile Include="tools\Lua\Libraries\EmuLuaLibrary.Client.cs" /> <Compile Include="tools\Lua\Libraries\EmuLuaLibrary.Client.cs" />
<Compile Include="tools\Lua\Libraries\EmuLuaLibrary.Communication.cs" /> <Compile Include="tools\Lua\Libraries\EmuLuaLibrary.Communication.cs" />
<Compile Include="tools\Lua\Libraries\EmuLuaLibrary.Console.cs" /> <Compile Include="tools\Lua\Libraries\EmuLuaLibrary.Console.cs" />

View File

@ -0,0 +1,18 @@
using System;
using NLua;
using BizHawk.Client.Common;
namespace BizHawk.Client.EmuHawk
{
/// <summary>As <see cref="DelegatingLuaLibrary"/>, but also includes EmuHawk APIs via a <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

@ -24,13 +24,13 @@ namespace BizHawk.Client.EmuHawk
public EmuLuaLibrary(IEmulatorServiceProvider serviceProvider) public EmuLuaLibrary(IEmulatorServiceProvider serviceProvider)
: this() : this()
{ {
static APISubsetContainer InitApiHawkContainerInstance(IEmulatorServiceProvider sp, Action<string> logCallback) static ApiContainer InitApiHawkContainerInstance(IEmulatorServiceProvider sp, Action<string> logCallback)
{ {
var ctorParamTypes = new[] { typeof(Action<string>) }; var ctorParamTypes = new[] { typeof(Action<string>) };
var ctorParams = new object[] { logCallback }; var ctorParams = new object[] { logCallback };
var libDict = new Dictionary<Type, IExternalApi>(); var libDict = new Dictionary<Type, IExternalApi>();
foreach (var api in Assembly.Load("BizHawk.Client.ApiHawk").GetTypes() foreach (var api in Assembly.GetAssembly(typeof(EmuApi)).GetTypes()
.Concat(Assembly.GetAssembly(typeof(APISubsetContainer)).GetTypes()) .Concat(Assembly.GetAssembly(typeof(ToolApi)).GetTypes())
.Where(t => t.IsSealed && typeof(IExternalApi).IsAssignableFrom(t) && ServiceInjector.IsAvailable(sp, t))) .Where(t => t.IsSealed && typeof(IExternalApi).IsAssignableFrom(t) && ServiceInjector.IsAvailable(sp, t)))
{ {
var ctorWithParams = api.GetConstructor(ctorParamTypes); var ctorWithParams = api.GetConstructor(ctorParamTypes);
@ -38,7 +38,7 @@ namespace BizHawk.Client.EmuHawk
ServiceInjector.UpdateServices(sp, instance); ServiceInjector.UpdateServices(sp, instance);
libDict.Add(api, instance); libDict.Add(api, instance);
} }
return ApiHawkContainerInstance = new APISubsetContainer(libDict); return ApiHawkContainerInstance = new ApiContainer(libDict);
} }
LuaWait = new AutoResetEvent(false); LuaWait = new AutoResetEvent(false);
@ -73,8 +73,11 @@ namespace BizHawk.Client.EmuHawk
instance.LuaRegister(lib, Docs); instance.LuaRegister(lib, Docs);
instance.LogOutputCallback = ConsoleLuaLibrary.LogOutput; instance.LogOutputCallback = ConsoleLuaLibrary.LogOutput;
ServiceInjector.UpdateServices(serviceProvider, instance); ServiceInjector.UpdateServices(serviceProvider, instance);
if (instance is DelegatingLuaLibrary dlgInstance)
dlgInstance.APIs = ApiHawkContainerInstance ?? InitApiHawkContainerInstance(serviceProvider, ConsoleLuaLibrary.LogOutput); ApiHawkContainerInstance ??= InitApiHawkContainerInstance(serviceProvider, ConsoleLuaLibrary.LogOutput);
if (instance is DelegatingLuaLibraryEmu dlgInstanceEmu) dlgInstanceEmu.APIs = ApiHawkContainerInstance; // this is necessary as the property has the `new` modifier
else if (instance is DelegatingLuaLibrary dlgInstance) dlgInstance.APIs = ApiHawkContainerInstance;
Libraries.Add(lib, instance); Libraries.Add(lib, instance);
} }
} }
@ -98,7 +101,7 @@ namespace BizHawk.Client.EmuHawk
} }
/// <remarks>lazily instantiated</remarks> /// <remarks>lazily instantiated</remarks>
private static APISubsetContainer ApiHawkContainerInstance; private static ApiContainer ApiHawkContainerInstance;
private Lua _lua = new Lua(); private Lua _lua = new Lua();
private Lua _currThread; private Lua _currThread;