Lua - Refactor how lua libraries are registered by searching assemblies and finding implementations and building a list of these

This commit is contained in:
adelikat 2014-06-01 22:02:59 +00:00
parent e7c2c40d79
commit 83ada011e8
18 changed files with 74 additions and 70 deletions

View File

@ -3,7 +3,7 @@ using LuaInterface;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public class BitLuaLibrary : LuaLibraryBase public sealed class BitLuaLibrary : LuaLibraryBase
{ {
public BitLuaLibrary(Lua lua) public BitLuaLibrary(Lua lua)
: base(lua) { } : base(lua) { }

View File

@ -12,7 +12,7 @@ using LuaInterface;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public class EmulatorLuaLibrary : LuaLibraryBase public sealed class EmulatorLuaLibrary : LuaLibraryBase
{ {
public Action FrameAdvanceCallback { get; set; } public Action FrameAdvanceCallback { get; set; }
public Action YieldCallback { get; set; } public Action YieldCallback { get; set; }

View File

@ -5,7 +5,7 @@ using LuaInterface;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public class EventLuaLibrary : LuaLibraryBase public sealed class EventLuaLibrary : LuaLibraryBase
{ {
private readonly LuaFunctionList _luaFunctions = new LuaFunctionList(); private readonly LuaFunctionList _luaFunctions = new LuaFunctionList();

View File

@ -3,7 +3,7 @@ using LuaInterface;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public class GameInfoLuaLibrary : LuaLibraryBase public sealed class GameInfoLuaLibrary : LuaLibraryBase
{ {
public GameInfoLuaLibrary(Lua lua) public GameInfoLuaLibrary(Lua lua)
: base(lua) { } : base(lua) { }

View File

@ -3,7 +3,7 @@ using LuaInterface;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public class JoypadLuaLibrary : LuaLibraryBase public sealed class JoypadLuaLibrary : LuaLibraryBase
{ {
public JoypadLuaLibrary(Lua lua) public JoypadLuaLibrary(Lua lua)
: base(lua) { } : base(lua) { }

View File

@ -4,7 +4,7 @@ using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public class MainMemoryLuaLibrary : LuaMemoryBase public sealed class MainMemoryLuaLibrary : LuaMemoryBase
{ {
public MainMemoryLuaLibrary(Lua lua) public MainMemoryLuaLibrary(Lua lua)
: base(lua) { } : base(lua) { }

View File

@ -4,7 +4,7 @@ using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public class MemoryLuaLibrary : LuaMemoryBase public sealed class MemoryLuaLibrary : LuaMemoryBase
{ {
private int _currentMemoryDomain; // Main memory by default probably (index 0 is currently always main memory but may never be) private int _currentMemoryDomain; // Main memory by default probably (index 0 is currently always main memory but may never be)

View File

@ -3,7 +3,7 @@ using LuaInterface;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public class MovieLuaLibrary : LuaLibraryBase public sealed class MovieLuaLibrary : LuaLibraryBase
{ {
public MovieLuaLibrary(Lua lua) public MovieLuaLibrary(Lua lua)
: base(lua) { } : base(lua) { }

View File

@ -8,7 +8,7 @@ using BizHawk.Emulation.Cores.Consoles.Nintendo.QuickNES;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public class NesLuaLibrary : LuaLibraryBase public sealed class NesLuaLibrary : LuaLibraryBase
{ {
// TODO: // TODO:
// perhaps with the new core config system, one could // perhaps with the new core config system, one could

View File

@ -4,7 +4,7 @@ using BizHawk.Emulation.Cores.Nintendo.SNES;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public class SnesLuaLibrary : LuaLibraryBase public sealed class SnesLuaLibrary : LuaLibraryBase
{ {
public SnesLuaLibrary(Lua lua) public SnesLuaLibrary(Lua lua)
: base(lua) { } : base(lua) { }

View File

@ -5,7 +5,7 @@ using LuaInterface;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public class StringLuaLibrary : LuaLibraryBase public sealed class StringLuaLibrary : LuaLibraryBase
{ {
public override string Name { get { return "bizstring"; } } public override string Name { get { return "bizstring"; } }

View File

@ -6,7 +6,7 @@ using BizHawk.Client.Common;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {
public class EmuHawkLuaLibrary : LuaLibraryBase public sealed class EmuHawkLuaLibrary : LuaLibraryBase
{ {
private readonly Dictionary<int, string> _filterMappings = new Dictionary<int, string> private readonly Dictionary<int, string> _filterMappings = new Dictionary<int, string>
{ {

View File

@ -8,7 +8,7 @@ using LuaInterface;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {
public class ConsoleLuaLibrary : LuaLibraryBase public sealed class ConsoleLuaLibrary : LuaLibraryBase
{ {
public ConsoleLuaLibrary(Lua lua) public ConsoleLuaLibrary(Lua lua)
: base(lua) { } : base(lua) { }

View File

@ -9,7 +9,7 @@ using LuaInterface;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {
public class FormsLuaLibrary : LuaLibraryBase public sealed class FormsLuaLibrary : LuaLibraryBase
{ {
public FormsLuaLibrary(Lua lua) public FormsLuaLibrary(Lua lua)
: base(lua) { } : base(lua) { }

View File

@ -8,7 +8,7 @@ using LuaInterface;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {
public class GuiLuaLibrary : LuaLibraryBase public sealed class GuiLuaLibrary : LuaLibraryBase
{ {
public GuiLuaLibrary(Lua lua) public GuiLuaLibrary(Lua lua)
: base(lua) { } : base(lua) { }

View File

@ -7,7 +7,7 @@ using LuaInterface;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {
public class InputLuaLibrary : LuaLibraryBase public sealed class InputLuaLibrary : LuaLibraryBase
{ {
public InputLuaLibrary(Lua lua) public InputLuaLibrary(Lua lua)
: base(lua) { } : base(lua) { }

View File

@ -6,7 +6,7 @@ using BizHawk.Client.Common;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {
public class SavestateLuaLibrary : LuaLibraryBase public sealed class SavestateLuaLibrary : LuaLibraryBase
{ {
public SavestateLuaLibrary(Lua lua) public SavestateLuaLibrary(Lua lua)
: base(lua) { } : base(lua) { }

View File

@ -1,16 +1,17 @@
using System; using System;
using System.Linq;
using System.Threading; using System.Threading;
using BizHawk.Client.Common; using BizHawk.Client.Common;
using LuaInterface; using LuaInterface;
using System.Reflection;
using System.Collections.Generic;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {
public class EmuLuaLibrary public class EmuLuaLibrary
{ {
private readonly FormsLuaLibrary _formsLibrary; private readonly Dictionary<Type, LuaLibraryBase> Libraries = new Dictionary<Type, LuaLibraryBase>();
private readonly EventLuaLibrary _eventLibrary;
private readonly GuiLuaLibrary _guiLibrary;
private readonly LuaConsole _caller; private readonly LuaConsole _caller;
private Lua _lua = new Lua(); private Lua _lua = new Lua();
@ -21,6 +22,26 @@ namespace BizHawk.Client.EmuHawk
Docs = new LuaDocumentation(); Docs = new LuaDocumentation();
} }
private FormsLuaLibrary FormsLibrary
{
get { return (FormsLuaLibrary)Libraries[typeof(FormsLuaLibrary)]; }
}
private EventLuaLibrary EventsLibrary
{
get { return (EventLuaLibrary)Libraries[typeof(EventLuaLibrary)]; }
}
private EmulatorLuaLibrary EmulatorLuaLibrary
{
get { return (EmulatorLuaLibrary)Libraries[typeof(EmulatorLuaLibrary)]; }
}
public GuiLuaLibrary GuiLibrary
{
get { return (GuiLuaLibrary)Libraries[typeof(GuiLuaLibrary)]; }
}
public EmuLuaLibrary(LuaConsole passed) public EmuLuaLibrary(LuaConsole passed)
: this() : this()
{ {
@ -29,44 +50,32 @@ namespace BizHawk.Client.EmuHawk
_caller = passed.Get(); _caller = passed.Get();
// Register lua libraries // Register lua libraries
var libs = Assembly
.Load("BizHawk.Client.Common")
.GetTypes()
.Where(t => typeof(LuaLibraryBase).IsAssignableFrom(t))
.Where(t => t.IsSealed)
.ToList();
libs.AddRange(
Assembly
.GetAssembly(typeof(EmuLuaLibrary))
.GetTypes()
.Where(t => typeof(LuaLibraryBase).IsAssignableFrom(t))
.Where(t => t.IsSealed)
);
foreach (var lib in libs)
{
var instance = (LuaLibraryBase)Activator.CreateInstance(lib, _lua);
instance.LuaRegister(Docs);
Libraries.Add(lib, instance);
}
_lua.RegisterFunction("print", this, GetType().GetMethod("Print")); _lua.RegisterFunction("print", this, GetType().GetMethod("Print"));
// TODO: Search the assemblies for objects that inherit LuaBaseLibrary, and instantiate and register them and put them into an array, EmulatorLuaLibrary.FrameAdvanceCallback = Frameadvance;
// rather than call them all by name here EmulatorLuaLibrary.YieldCallback = EmuYield;
_formsLibrary = new FormsLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput);
_formsLibrary.LuaRegister(Docs);
_eventLibrary = new EventLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput);
_eventLibrary.LuaRegister(Docs);
_guiLibrary = new GuiLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput);
_guiLibrary.LuaRegister(Docs);
new BitLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
new EmuHawkLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
new ConsoleLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
var emuLib = new EmulatorLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput)
{
FrameAdvanceCallback = Frameadvance,
YieldCallback = EmuYield
};
emuLib.LuaRegister(Docs);
new InputLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
new JoypadLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
new MemoryLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
new MainMemoryLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
new MovieLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
new NesLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
new SavestateLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
new SnesLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
new StringLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
new GameInfoLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
Docs.Sort(); Docs.Sort();
} }
@ -76,50 +85,45 @@ namespace BizHawk.Client.EmuHawk
public EventWaitHandle LuaWait { get; private set; } public EventWaitHandle LuaWait { get; private set; }
public bool FrameAdvanceRequested { get; private set; } public bool FrameAdvanceRequested { get; private set; }
public GuiLuaLibrary GuiLibrary
{
get { return _guiLibrary; }
}
public LuaFunctionList RegisteredFunctions public LuaFunctionList RegisteredFunctions
{ {
get { return _eventLibrary.RegisteredFunctions; } get { return EventsLibrary.RegisteredFunctions; }
} }
public void WindowClosed(IntPtr handle) public void WindowClosed(IntPtr handle)
{ {
_formsLibrary.WindowClosed(handle); FormsLibrary.WindowClosed(handle);
} }
public void CallSaveStateEvent(string name) public void CallSaveStateEvent(string name)
{ {
_eventLibrary.CallSaveStateEvent(name); EventsLibrary.CallSaveStateEvent(name);
} }
public void CallLoadStateEvent(string name) public void CallLoadStateEvent(string name)
{ {
_eventLibrary.CallLoadStateEvent(name); EventsLibrary.CallLoadStateEvent(name);
} }
public void CallFrameBeforeEvent() public void CallFrameBeforeEvent()
{ {
_eventLibrary.CallFrameBeforeEvent(); EventsLibrary.CallFrameBeforeEvent();
} }
public void CallFrameAfterEvent() public void CallFrameAfterEvent()
{ {
_eventLibrary.CallFrameAfterEvent(); EventsLibrary.CallFrameAfterEvent();
} }
public void CallExitEvent(Lua thread) public void CallExitEvent(Lua thread)
{ {
_eventLibrary.CallExitEvent(thread); EventsLibrary.CallExitEvent(thread);
} }
public void Close() public void Close()
{ {
_lua = new Lua(); _lua = new Lua();
_guiLibrary.Dispose(); GuiLibrary.Dispose();
} }
public Lua SpawnCoroutine(string file) public Lua SpawnCoroutine(string file)
@ -132,7 +136,7 @@ namespace BizHawk.Client.EmuHawk
public ResumeResult ResumeScript(Lua script) public ResumeResult ResumeScript(Lua script)
{ {
_eventLibrary.CurrentThread = script; EventsLibrary.CurrentThread = script;
_currThread = script; _currThread = script;
var execResult = script.Resume(0); var execResult = script.Resume(0);
_currThread = null; _currThread = null;