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

View File

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

View File

@ -7,20 +7,18 @@ namespace BizHawk.Client.Common
{ {
public JoypadLuaLibrary(Lua lua) public JoypadLuaLibrary(Lua lua)
{ {
_lua = lua; Lua = lua;
} }
public override string Name { get { return "joypad"; } } public override string Name { get { return "joypad"; } }
private readonly Lua _lua;
[LuaMethodAttributes( [LuaMethodAttributes(
"get", "get",
"returns a lua table of the controller buttons pressed. If supplied, it will only return a table of buttons for the given controller" "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) public LuaTable Get(int? controller = null)
{ {
var buttons = _lua.NewTable(); var buttons = Lua.NewTable();
foreach (var button in Global.ControllerOutput.Source.Type.BoolButtons) foreach (var button in Global.ControllerOutput.Source.Type.BoolButtons)
{ {
if (!controller.HasValue) if (!controller.HasValue)
@ -58,7 +56,7 @@ namespace BizHawk.Client.Common
)] )]
public LuaTable GetImmediate() public LuaTable GetImmediate()
{ {
var buttons = _lua.NewTable(); var buttons = Lua.NewTable();
foreach (var button in Global.ActiveController.Type.BoolButtons) foreach (var button in Global.ActiveController.Type.BoolButtons)
{ {
buttons[button] = Global.ActiveController[button]; 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 // TODO: this needs a major refactor, as well as MemoryLuaLibrary, and this shoudl inherit memorylua library and extend it
public class MainMemoryLuaLibrary : LuaLibraryBase public class MainMemoryLuaLibrary : LuaLibraryBase
{ {
private readonly Lua _lua;
public MainMemoryLuaLibrary(Lua lua) public MainMemoryLuaLibrary(Lua lua)
{ {
_lua = lua; Lua = lua;
} }
public override string Name { get { return "mainmemory"; } } public override string Name { get { return "mainmemory"; } }
@ -120,7 +118,7 @@ namespace BizHawk.Client.Common
public LuaTable ReadByteRange(int addr, int length) public LuaTable ReadByteRange(int addr, int length)
{ {
var lastAddr = length + addr; var lastAddr = length + addr;
var table = _lua.NewTable(); var table = Lua.NewTable();
if (lastAddr < Global.Emulator.MemoryDomains.MainMemory.Size) if (lastAddr < Global.Emulator.MemoryDomains.MainMemory.Size)
{ {
for (var i = addr; i <= lastAddr; i++) for (var i = addr; i <= lastAddr; i++)

View File

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

View File

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

View File

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

View File

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

View File

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