Lua - unify constructors and have all libraries receive a lua context and an output callback, simplify some convolved registration code
This commit is contained in:
parent
6401e6d719
commit
17568997bf
|
@ -1,7 +1,16 @@
|
|||
namespace BizHawk.Client.Common
|
||||
using System;
|
||||
using LuaInterface;
|
||||
|
||||
namespace BizHawk.Client.Common
|
||||
{
|
||||
public class BitLuaLibrary : LuaLibraryBase
|
||||
{
|
||||
public BitLuaLibrary(Lua lua)
|
||||
: base(lua) { }
|
||||
|
||||
public BitLuaLibrary(Lua lua, Action<string> logOutputCallback)
|
||||
: base(lua, logOutputCallback) { }
|
||||
|
||||
public override string Name { get { return "bit"; } }
|
||||
|
||||
[LuaMethodAttributes(
|
||||
|
|
|
@ -16,9 +16,15 @@ namespace BizHawk.Client.Common
|
|||
private readonly Action _frameAdvanceCallback;
|
||||
private readonly Action _yieldCallback;
|
||||
|
||||
public EmulatorLuaLibrary(Lua lua, Action frameAdvanceCallback, Action yieldCallback)
|
||||
public EmulatorLuaLibrary(Lua lua)
|
||||
: base(lua) { }
|
||||
|
||||
public EmulatorLuaLibrary(Lua lua, Action<string> logOutputCallback)
|
||||
: base(lua, logOutputCallback) { }
|
||||
|
||||
public EmulatorLuaLibrary(Lua lua, Action<string> logOutputCallback, Action frameAdvanceCallback, Action yieldCallback)
|
||||
: this(lua, logOutputCallback)
|
||||
{
|
||||
Lua = lua;
|
||||
_frameAdvanceCallback = frameAdvanceCallback;
|
||||
_yieldCallback = yieldCallback;
|
||||
}
|
||||
|
|
|
@ -9,10 +9,11 @@ namespace BizHawk.Client.Common
|
|||
{
|
||||
private readonly LuaFunctionList _luaFunctions = new LuaFunctionList();
|
||||
|
||||
public EventLuaLibrary(Action<object> logOutputCallback)
|
||||
{
|
||||
LogOutputCallback = logOutputCallback;
|
||||
}
|
||||
public EventLuaLibrary(Lua lua)
|
||||
: base(lua) { }
|
||||
|
||||
public EventLuaLibrary(Lua lua, Action<string> logOutputCallback)
|
||||
: base(lua, logOutputCallback) { }
|
||||
|
||||
public override string Name { get { return "event"; } }
|
||||
|
||||
|
|
|
@ -6,9 +6,10 @@ namespace BizHawk.Client.Common
|
|||
public class GameInfoLuaLibrary : LuaLibraryBase
|
||||
{
|
||||
public GameInfoLuaLibrary(Lua lua)
|
||||
{
|
||||
Lua = lua;
|
||||
}
|
||||
: base(lua) { }
|
||||
|
||||
public GameInfoLuaLibrary(Lua lua, Action<string> logOutputCallback)
|
||||
: base(lua, logOutputCallback) { }
|
||||
|
||||
public override string Name { get { return "gameinfo"; } }
|
||||
|
||||
|
|
|
@ -6,9 +6,10 @@ namespace BizHawk.Client.Common
|
|||
public class JoypadLuaLibrary : LuaLibraryBase
|
||||
{
|
||||
public JoypadLuaLibrary(Lua lua)
|
||||
{
|
||||
Lua = lua;
|
||||
}
|
||||
: base(lua) { }
|
||||
|
||||
public JoypadLuaLibrary(Lua lua, Action<string> logOutputCallback)
|
||||
: base(lua, logOutputCallback) { }
|
||||
|
||||
public override string Name { get { return "joypad"; } }
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
|
||||
using LuaInterface;
|
||||
|
||||
namespace BizHawk.Client.Common
|
||||
|
@ -8,9 +7,10 @@ namespace BizHawk.Client.Common
|
|||
public class MainMemoryLuaLibrary : LuaLibraryBase
|
||||
{
|
||||
public MainMemoryLuaLibrary(Lua lua)
|
||||
{
|
||||
Lua = lua;
|
||||
}
|
||||
: base(lua) { }
|
||||
|
||||
public MainMemoryLuaLibrary(Lua lua, Action<string> logOutputCallback)
|
||||
: base(lua, logOutputCallback) { }
|
||||
|
||||
public override string Name { get { return "mainmemory"; } }
|
||||
|
||||
|
|
|
@ -7,12 +7,14 @@ namespace BizHawk.Client.Common
|
|||
{
|
||||
public class MemoryLuaLibrary : LuaLibraryBase
|
||||
{
|
||||
// TODO: when is this ever set by default?
|
||||
private int _currentMemoryDomain; // Main memory by default
|
||||
|
||||
public MemoryLuaLibrary(Lua lua)
|
||||
{
|
||||
Lua = lua;
|
||||
}
|
||||
: base(lua) { }
|
||||
|
||||
public MemoryLuaLibrary(Lua lua, Action<string> logOutputCallback)
|
||||
: base(lua, logOutputCallback) { }
|
||||
|
||||
public override string Name { get { return "memory"; } }
|
||||
|
||||
|
|
|
@ -1,13 +1,16 @@
|
|||
using LuaInterface;
|
||||
using System;
|
||||
using LuaInterface;
|
||||
|
||||
namespace BizHawk.Client.Common
|
||||
{
|
||||
public class MovieLuaLibrary : LuaLibraryBase
|
||||
{
|
||||
public MovieLuaLibrary(Lua lua)
|
||||
{
|
||||
Lua = lua;
|
||||
}
|
||||
: base(lua) { }
|
||||
|
||||
public MovieLuaLibrary(Lua lua, Action<string> logOutputCallback)
|
||||
: base(lua, logOutputCallback) { }
|
||||
|
||||
|
||||
public override string Name { get { return "movie"; } }
|
||||
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
using System.Linq;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
using LuaInterface;
|
||||
|
||||
using BizHawk.Emulation.Cores.Nintendo.NES;
|
||||
using BizHawk.Emulation.Cores.Consoles.Nintendo.QuickNES;
|
||||
|
@ -11,6 +14,12 @@ namespace BizHawk.Client.Common
|
|||
// perhaps with the new core config system, one could
|
||||
// automatically bring out all of the settings to a lua table, with names. that
|
||||
// would be completely arbitrary and would remove the whole requirement for this mess
|
||||
public NesLuaLibrary(Lua lua)
|
||||
: base(lua) { }
|
||||
|
||||
public NesLuaLibrary(Lua lua, Action<string> logOutputCallback)
|
||||
: base(lua, logOutputCallback) { }
|
||||
|
||||
public override string Name { get { return "nes"; } }
|
||||
|
||||
[LuaMethodAttributes(
|
||||
|
|
|
@ -1,9 +1,17 @@
|
|||
using BizHawk.Emulation.Cores.Nintendo.SNES;
|
||||
using System;
|
||||
using LuaInterface;
|
||||
using BizHawk.Emulation.Cores.Nintendo.SNES;
|
||||
|
||||
namespace BizHawk.Client.Common
|
||||
{
|
||||
public class SnesLuaLibrary : LuaLibraryBase
|
||||
{
|
||||
public SnesLuaLibrary(Lua lua)
|
||||
: base(lua) { }
|
||||
|
||||
public SnesLuaLibrary(Lua lua, Action<string> logOutputCallback)
|
||||
: base(lua, logOutputCallback) { }
|
||||
|
||||
public override string Name { get { return "snes"; } }
|
||||
|
||||
[LuaMethodAttributes(
|
||||
|
|
|
@ -10,9 +10,10 @@ namespace BizHawk.Client.Common
|
|||
public override string Name { get { return "bizstring"; } }
|
||||
|
||||
public StringLuaLibrary(Lua lua)
|
||||
{
|
||||
Lua = lua;
|
||||
}
|
||||
: base(lua) { }
|
||||
|
||||
public StringLuaLibrary(Lua lua, Action<string> logOutputCallback)
|
||||
: base(lua, logOutputCallback) { }
|
||||
|
||||
[LuaMethodAttributes(
|
||||
"hex",
|
||||
|
|
|
@ -7,6 +7,17 @@ namespace BizHawk.Client.Common
|
|||
{
|
||||
public abstract class LuaLibraryBase
|
||||
{
|
||||
public LuaLibraryBase(Lua lua)
|
||||
{
|
||||
Lua = lua;
|
||||
}
|
||||
|
||||
public LuaLibraryBase(Lua lua, Action<string> logOutputCallback)
|
||||
: this(lua)
|
||||
{
|
||||
LogOutputCallback = logOutputCallback;
|
||||
}
|
||||
|
||||
public abstract string Name { get; }
|
||||
public Action<string> LogOutputCallback { get; set; }
|
||||
public Lua Lua { get; set; }
|
||||
|
@ -19,9 +30,9 @@ namespace BizHawk.Client.Common
|
|||
}
|
||||
}
|
||||
|
||||
public virtual void LuaRegister(Lua lua, ILuaDocumentation docs = null)
|
||||
public virtual void LuaRegister(ILuaDocumentation docs = null)
|
||||
{
|
||||
lua.NewTable(Name);
|
||||
Lua.NewTable(Name);
|
||||
|
||||
var luaAttr = typeof(LuaMethodAttributes);
|
||||
|
||||
|
@ -33,7 +44,7 @@ namespace BizHawk.Client.Common
|
|||
{
|
||||
var luaMethodAttr = method.GetCustomAttributes(luaAttr, false).First() as LuaMethodAttributes;
|
||||
var luaName = Name + "." + luaMethodAttr.Name;
|
||||
lua.RegisterFunction(luaName, this, method);
|
||||
Lua.RegisterFunction(luaName, this, method);
|
||||
|
||||
if (docs != null)
|
||||
{
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using LuaInterface;
|
||||
using BizHawk.Client.Common;
|
||||
|
||||
namespace BizHawk.Client.EmuHawk
|
||||
|
@ -15,13 +17,11 @@ namespace BizHawk.Client.EmuHawk
|
|||
{ 4, "Scanlines" },
|
||||
};
|
||||
|
||||
public EmuHawkLuaLibrary(Action<object> logOutputCallback)
|
||||
: this()
|
||||
{
|
||||
LogOutputCallback = logOutputCallback;
|
||||
}
|
||||
public EmuHawkLuaLibrary(Lua lua)
|
||||
: base(lua) { }
|
||||
|
||||
public EmuHawkLuaLibrary() { }
|
||||
public EmuHawkLuaLibrary(Lua lua, Action<string> logOutputCallback)
|
||||
: base(lua, logOutputCallback) { }
|
||||
|
||||
public override string Name { get { return "client"; } }
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
|
@ -9,6 +10,12 @@ namespace BizHawk.Client.EmuHawk
|
|||
{
|
||||
public class ConsoleLuaLibrary : LuaLibraryBase
|
||||
{
|
||||
public ConsoleLuaLibrary(Lua lua)
|
||||
: base(lua) { }
|
||||
|
||||
public ConsoleLuaLibrary(Lua lua, Action<string> logOutputCallback)
|
||||
: base(lua, logOutputCallback) { }
|
||||
|
||||
public override string Name { get { return "console"; } }
|
||||
|
||||
[LuaMethodAttributes(
|
||||
|
|
|
@ -11,6 +11,12 @@ namespace BizHawk.Client.EmuHawk
|
|||
{
|
||||
public class FormsLuaLibrary : LuaLibraryBase
|
||||
{
|
||||
public FormsLuaLibrary(Lua lua)
|
||||
: base(lua) { }
|
||||
|
||||
public FormsLuaLibrary(Lua lua, Action<string> logOutputCallback)
|
||||
: base(lua, logOutputCallback) { }
|
||||
|
||||
// TODO: replace references to ConsoleLuaLibrary.Log with a callback that is passed in
|
||||
public override string Name { get { return "forms"; } }
|
||||
|
||||
|
|
|
@ -10,6 +10,12 @@ namespace BizHawk.Client.EmuHawk
|
|||
{
|
||||
public class GuiLuaLibrary : LuaLibraryBase
|
||||
{
|
||||
public GuiLuaLibrary(Lua lua)
|
||||
: base(lua) { }
|
||||
|
||||
public GuiLuaLibrary(Lua lua, Action<string> logOutputCallback)
|
||||
: base(lua, logOutputCallback) { }
|
||||
|
||||
public override string Name { get { return "gui"; } }
|
||||
|
||||
#region Gui API
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System.Linq;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Windows.Forms;
|
||||
|
||||
using BizHawk.Client.Common;
|
||||
|
@ -9,9 +10,10 @@ namespace BizHawk.Client.EmuHawk
|
|||
public class InputLuaLibrary : LuaLibraryBase
|
||||
{
|
||||
public InputLuaLibrary(Lua lua)
|
||||
{
|
||||
Lua = lua;
|
||||
}
|
||||
: base(lua) { }
|
||||
|
||||
public InputLuaLibrary(Lua lua, Action<string> logOutputCallback)
|
||||
: base(lua, logOutputCallback) { }
|
||||
|
||||
public override string Name { get { return "input"; } }
|
||||
|
||||
|
|
|
@ -1,10 +1,19 @@
|
|||
using System.IO;
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
using LuaInterface;
|
||||
using BizHawk.Client.Common;
|
||||
|
||||
namespace BizHawk.Client.EmuHawk
|
||||
{
|
||||
public class SavestateLuaLibrary : LuaLibraryBase
|
||||
{
|
||||
public SavestateLuaLibrary(Lua lua)
|
||||
: base(lua) { }
|
||||
|
||||
public SavestateLuaLibrary(Lua lua, Action<string> logOutputCallback)
|
||||
: base(lua, logOutputCallback) { }
|
||||
|
||||
public override string Name { get { return "savestate"; } }
|
||||
|
||||
[LuaMethodAttributes(
|
||||
|
|
|
@ -8,9 +8,9 @@ namespace BizHawk.Client.EmuHawk
|
|||
{
|
||||
public class EmuLuaLibrary
|
||||
{
|
||||
private readonly FormsLuaLibrary _formsLibrary = new FormsLuaLibrary();
|
||||
private readonly EventLuaLibrary _eventLibrary = new EventLuaLibrary(ConsoleLuaLibrary.LogOutput);
|
||||
private readonly GuiLuaLibrary _guiLibrary = new GuiLuaLibrary();
|
||||
private readonly FormsLuaLibrary _formsLibrary;
|
||||
private readonly EventLuaLibrary _eventLibrary;
|
||||
private readonly GuiLuaLibrary _guiLibrary;
|
||||
private readonly LuaConsole _caller;
|
||||
|
||||
private Lua _lua = new Lua();
|
||||
|
@ -27,7 +27,42 @@ namespace BizHawk.Client.EmuHawk
|
|||
LuaWait = new AutoResetEvent(false);
|
||||
Docs.Clear();
|
||||
_caller = passed.Get();
|
||||
LuaRegister(_lua);
|
||||
|
||||
// Register lua libraries
|
||||
_lua.RegisterFunction("print", this, GetType().GetMethod("Print"));
|
||||
|
||||
_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);
|
||||
|
||||
new EmulatorLuaLibrary(
|
||||
_lua,
|
||||
ConsoleLuaLibrary.LogOutput,
|
||||
Frameadvance,
|
||||
EmuYield).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();
|
||||
}
|
||||
|
||||
public LuaDocumentation Docs { get; private set; }
|
||||
|
@ -76,48 +111,6 @@ namespace BizHawk.Client.EmuHawk
|
|||
_guiLibrary.Dispose();
|
||||
}
|
||||
|
||||
|
||||
public void LuaRegister(Lua lua)
|
||||
{
|
||||
lua.RegisterFunction("print", this, GetType().GetMethod("Print"));
|
||||
|
||||
new BitLuaLibrary().LuaRegister(lua, Docs);
|
||||
new EmuHawkLuaLibrary(ConsoleLuaLibrary.LogOutput).LuaRegister(lua, Docs);
|
||||
new ConsoleLuaLibrary().LuaRegister(lua, Docs);
|
||||
|
||||
new EmulatorLuaLibrary(
|
||||
_lua,
|
||||
Frameadvance,
|
||||
EmuYield).LuaRegister(lua, Docs);
|
||||
|
||||
_eventLibrary.LuaRegister(lua, Docs);
|
||||
_formsLibrary.LuaRegister(lua, Docs);
|
||||
_guiLibrary.LuaRegister(lua, Docs);
|
||||
new InputLuaLibrary(_lua).LuaRegister(lua, Docs);
|
||||
new JoypadLuaLibrary(_lua).LuaRegister(lua, Docs);
|
||||
|
||||
var memory = new MemoryLuaLibrary(_lua)
|
||||
{
|
||||
LogOutputCallback = ConsoleLuaLibrary.LogOutput
|
||||
};
|
||||
memory.LuaRegister(lua, Docs);
|
||||
|
||||
var mainmemory = new MainMemoryLuaLibrary(_lua)
|
||||
{
|
||||
LogOutputCallback = ConsoleLuaLibrary.LogOutput
|
||||
};
|
||||
mainmemory.LuaRegister(lua, Docs);
|
||||
|
||||
new MovieLuaLibrary(_lua).LuaRegister(lua, Docs);
|
||||
new NesLuaLibrary().LuaRegister(lua, Docs);
|
||||
new SavestateLuaLibrary().LuaRegister(lua, Docs);
|
||||
new SnesLuaLibrary().LuaRegister(lua, Docs);
|
||||
new StringLuaLibrary(_lua).LuaRegister(lua, Docs);
|
||||
new GameInfoLuaLibrary(_lua).LuaRegister(lua, Docs);
|
||||
|
||||
Docs.Sort();
|
||||
}
|
||||
|
||||
public Lua SpawnCoroutine(string file)
|
||||
{
|
||||
var lua = _lua.NewThread();
|
||||
|
|
Loading…
Reference in New Issue