Refactor LuaLibraryBase to have a Lua context rather than misc libraries being in charge of that when needed. Still todo: clean up the inconsistent constructor logic, vs setting these as public properties

This commit is contained in:
adelikat 2014-05-20 20:34:51 +00:00
parent f19d15d1ed
commit 6401e6d719
9 changed files with 20 additions and 33 deletions

View File

@ -13,13 +13,12 @@ namespace BizHawk.Client.Common
{
public class EmulatorLuaLibrary : LuaLibraryBase
{
private readonly Lua _lua;
private readonly Action _frameAdvanceCallback;
private readonly Action _yieldCallback;
public EmulatorLuaLibrary(Lua lua, Action frameAdvanceCallback, Action yieldCallback)
{
_lua = lua;
Lua = lua;
_frameAdvanceCallback = frameAdvanceCallback;
_yieldCallback = yieldCallback;
}
@ -68,7 +67,7 @@ namespace BizHawk.Client.Common
)]
public LuaTable GetRegisters()
{
var table = _lua.NewTable();
var table = Lua.NewTable();
foreach (var kvp in Global.Emulator.GetCpuFlagsAndRegisters())
{
table[kvp.Key] = kvp.Value;

View File

@ -7,13 +7,11 @@ namespace BizHawk.Client.Common
{
public GameInfoLuaLibrary(Lua lua)
{
_lua = lua;
Lua = lua;
}
public override string Name { get { return "gameinfo"; } }
private readonly Lua _lua;
[LuaMethodAttributes(
"getromname",
"returns the path of the currently loaded rom, if a rom is loaded"
@ -99,7 +97,7 @@ namespace BizHawk.Client.Common
)]
public LuaTable GetOptions()
{
var options = _lua.NewTable();
var options = Lua.NewTable();
if (Global.Game != null)
{

View File

@ -7,20 +7,18 @@ namespace BizHawk.Client.Common
{
public JoypadLuaLibrary(Lua lua)
{
_lua = lua;
Lua = lua;
}
public override string Name { get { return "joypad"; } }
private readonly Lua _lua;
[LuaMethodAttributes(
"get",
"returns a lua table of the controller buttons pressed. If supplied, it will only return a table of buttons for the given controller"
)]
public LuaTable Get(int? controller = null)
{
var buttons = _lua.NewTable();
var buttons = Lua.NewTable();
foreach (var button in Global.ControllerOutput.Source.Type.BoolButtons)
{
if (!controller.HasValue)
@ -58,7 +56,7 @@ namespace BizHawk.Client.Common
)]
public LuaTable GetImmediate()
{
var buttons = _lua.NewTable();
var buttons = Lua.NewTable();
foreach (var button in Global.ActiveController.Type.BoolButtons)
{
buttons[button] = Global.ActiveController[button];

View File

@ -7,11 +7,9 @@ namespace BizHawk.Client.Common
// TODO: this needs a major refactor, as well as MemoryLuaLibrary, and this shoudl inherit memorylua library and extend it
public class MainMemoryLuaLibrary : LuaLibraryBase
{
private readonly Lua _lua;
public MainMemoryLuaLibrary(Lua lua)
{
_lua = lua;
Lua = lua;
}
public override string Name { get { return "mainmemory"; } }
@ -120,7 +118,7 @@ namespace BizHawk.Client.Common
public LuaTable ReadByteRange(int addr, int length)
{
var lastAddr = length + addr;
var table = _lua.NewTable();
var table = Lua.NewTable();
if (lastAddr < Global.Emulator.MemoryDomains.MainMemory.Size)
{
for (var i = addr; i <= lastAddr; i++)

View File

@ -7,12 +7,11 @@ namespace BizHawk.Client.Common
{
public class MemoryLuaLibrary : LuaLibraryBase
{
private readonly Lua _lua;
private int _currentMemoryDomain; // Main memory by default
public MemoryLuaLibrary(Lua lua)
{
_lua = lua;
Lua = lua;
}
public override string Name { get { return "memory"; } }
@ -103,7 +102,7 @@ namespace BizHawk.Client.Common
)]
public LuaTable GetMemoryDomainList()
{
var table = _lua.NewTable();
var table = Lua.NewTable();
for (int i = 0; i < Global.Emulator.MemoryDomains.Count; i++)
{
table[i] = Global.Emulator.MemoryDomains[i].Name;
@ -119,7 +118,7 @@ namespace BizHawk.Client.Common
public LuaTable ReadByteRange(int addr, int length)
{
var lastAddr = length + addr;
var table = _lua.NewTable();
var table = Lua.NewTable();
if (lastAddr < Global.Emulator.MemoryDomains[_currentMemoryDomain].Size)
{

View File

@ -4,11 +4,9 @@ namespace BizHawk.Client.Common
{
public class MovieLuaLibrary : LuaLibraryBase
{
private readonly Lua _lua;
public MovieLuaLibrary(Lua lua)
{
_lua = lua;
Lua = lua;
}
public override string Name { get { return "movie"; } }
@ -28,7 +26,7 @@ namespace BizHawk.Client.Common
)]
public LuaTable GetInput(int frame)
{
var input = _lua.NewTable();
var input = Lua.NewTable();
var m = new MovieControllerAdapter { Type = Global.MovieSession.MovieControllerAdapter.Type };
m.SetControllersAsMnemonic(

View File

@ -7,13 +7,11 @@ namespace BizHawk.Client.Common
{
public class StringLuaLibrary : LuaLibraryBase
{
private readonly Lua _lua;
public override string Name { get { return "bizstring"; } }
public StringLuaLibrary(Lua lua)
{
_lua = lua;
Lua = lua;
}
[LuaMethodAttributes(
@ -144,7 +142,7 @@ namespace BizHawk.Client.Common
)]
public LuaTable Split(string str, string separator)
{
var table = _lua.NewTable();
var table = Lua.NewTable();
var splitStr = str.Split(
new char[] { separator.FirstOrDefault() },
StringSplitOptions.RemoveEmptyEntries);

View File

@ -9,6 +9,7 @@ namespace BizHawk.Client.Common
{
public abstract string Name { get; }
public Action<string> LogOutputCallback { get; set; }
public Lua Lua { get; set; }
protected void Log(object message)
{

View File

@ -10,20 +10,18 @@ namespace BizHawk.Client.EmuHawk
{
public InputLuaLibrary(Lua lua)
{
_lua = lua;
Lua = lua;
}
public override string Name { get { return "input"; } }
private readonly Lua _lua;
[LuaMethodAttributes(
"get",
"Returns a lua table of all the buttons the user is currently pressing on their keyboard and gamepads"
)]
public LuaTable Get()
{
var buttons = _lua.NewTable();
var buttons = Lua.NewTable();
foreach (var kvp in Global.ControllerInputCoalescer.BoolButtons().Where(kvp => kvp.Value))
{
buttons[kvp.Key] = true;
@ -38,7 +36,7 @@ namespace BizHawk.Client.EmuHawk
)]
public LuaTable GetMouse()
{
var buttons = _lua.NewTable();
var buttons = Lua.NewTable();
//TODO - need to specify whether in "emu" or "native" coordinate space.
var p = GlobalWin.DisplayManager.UntransformPoint(Control.MousePosition);
buttons["X"] = p.X;