Add DelegatingLuaLibrary and set up ApiHawk instantiation

This commit is contained in:
YoshiRulz 2019-11-16 17:19:04 +10:00
parent 27b2d1b6ad
commit a7ffdd948e
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
4 changed files with 65 additions and 0 deletions

View File

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
namespace BizHawk.Client.Common
{
public sealed class APISubsetContainer : IApiContainer
{
public Dictionary<Type, IExternalApi> Libraries { get; set; }
public IEmu Emu => (IEmu) Libraries[typeof(EmuApi)];
public IGameInfo GameInfo => (IGameInfo) Libraries[typeof(GameInfoApi)];
public IJoypad Joypad => (IJoypad) Libraries[typeof(JoypadApi)];
public IMem Mem => (IMem) Libraries[typeof(MemApi)];
public IMemEvents MemEvents => (IMemEvents) Libraries[typeof(MemEventsApi)];
public IMemorySaveState MemorySaveState => (IMemorySaveState) Libraries[typeof(MemorySaveStateApi)];
public IInputMovie Movie => (IInputMovie) Libraries[typeof(MovieApi)];
public ISql Sql => (ISql) Libraries[typeof(SqlApi)];
public IUserData UserData => (IUserData) Libraries[typeof(UserDataApi)];
public APISubsetContainer(Dictionary<Type, IExternalApi> libs)
{
Libraries = libs;
}
}
}

View File

@ -82,6 +82,7 @@
<Compile Include="..\Version\VersionInfo.cs">
<Link>VersionInfo.cs</Link>
</Compile>
<Compile Include="Api\APISubsetContainer.cs" />
<Compile Include="Api\Classes\EmuApi.cs" />
<Compile Include="Api\Classes\GameInfoApi.cs" />
<Compile Include="Api\Classes\JoypadApi.cs" />
@ -142,6 +143,7 @@
<Compile Include="IPS.cs" />
<Compile Include="IZipWriter.cs" />
<Compile Include="KeyTurbo.cs" />
<Compile Include="lua\DelegatingLuaLibrary.cs" />
<Compile Include="lua\EmuLuaLibrary.Bit.cs" />
<Compile Include="lua\EmuLuaLibrary.Emu.cs" />
<Compile Include="lua\EmuLuaLibrary.Events.cs" />

View File

@ -0,0 +1,16 @@
using System;
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>
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; }
}
}

View File

@ -24,6 +24,23 @@ namespace BizHawk.Client.EmuHawk
public EmuLuaLibrary(IEmulatorServiceProvider serviceProvider)
: this()
{
static APISubsetContainer InitApiHawkContainerInstance(IEmulatorServiceProvider sp, Action<string> logCallback)
{
var ctorParamTypes = new[] { typeof(Action<string>) };
var ctorParams = new object[] { logCallback };
var libDict = new Dictionary<Type, IExternalApi>();
foreach (var api in Assembly.Load("BizHawk.Client.ApiHawk").GetTypes()
.Concat(Assembly.GetAssembly(typeof(APISubsetContainer)).GetTypes())
.Where(t => t.IsSealed && typeof(IExternalApi).IsAssignableFrom(t) && ServiceInjector.IsAvailable(sp, t)))
{
var ctorWithParams = api.GetConstructor(ctorParamTypes);
var instance = (IExternalApi) (ctorWithParams == null ? Activator.CreateInstance(api) : ctorWithParams.Invoke(ctorParams));
ServiceInjector.UpdateServices(sp, instance);
libDict.Add(api, instance);
}
return ApiHawkContainerInstance = new APISubsetContainer(libDict);
}
LuaWait = new AutoResetEvent(false);
Docs.Clear();
@ -56,6 +73,8 @@ namespace BizHawk.Client.EmuHawk
instance.LuaRegister(lib, Docs);
instance.LogOutputCallback = ConsoleLuaLibrary.LogOutput;
ServiceInjector.UpdateServices(serviceProvider, instance);
if (instance is DelegatingLuaLibrary dlgInstance)
dlgInstance.APIs = ApiHawkContainerInstance ?? InitApiHawkContainerInstance(serviceProvider, ConsoleLuaLibrary.LogOutput);
Libraries.Add(lib, instance);
}
}
@ -78,6 +97,9 @@ namespace BizHawk.Client.EmuHawk
}
}
/// <remarks>lazily instantiated</remarks>
private static APISubsetContainer ApiHawkContainerInstance;
private Lua _lua = new Lua();
private Lua _currThread;