Encapsulate conversion to/from LuaTable, stop passing NLua to Lua libs
This commit is contained in:
parent
e69cab3297
commit
c8e10120db
|
@ -26,8 +26,8 @@ namespace BizHawk.Client.Common
|
||||||
|
|
||||||
public IMainFormForApi MainForm { get; set; }
|
public IMainFormForApi MainForm { get; set; }
|
||||||
|
|
||||||
public ClientLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public ClientLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "client";
|
public override string Name => "client";
|
||||||
|
|
||||||
|
@ -231,7 +231,7 @@ namespace BizHawk.Client.Common
|
||||||
[LuaMethod("transformPoint", "Transforms a point (x, y) in emulator space to a point in client space")]
|
[LuaMethod("transformPoint", "Transforms a point (x, y) in emulator space to a point in client space")]
|
||||||
public LuaTable TransformPoint(int x, int y) {
|
public LuaTable TransformPoint(int x, int y) {
|
||||||
var transformed = APIs.EmuClient.TransformPoint(new Point(x, y));
|
var transformed = APIs.EmuClient.TransformPoint(new Point(x, y));
|
||||||
var table = Lua.NewTable();
|
var table = _th.CreateTable();
|
||||||
table["x"] = transformed.X;
|
table["x"] = transformed.X;
|
||||||
table["y"] = transformed.Y;
|
table["y"] = transformed.Y;
|
||||||
return table;
|
return table;
|
||||||
|
@ -262,14 +262,14 @@ namespace BizHawk.Client.Common
|
||||||
|
|
||||||
[LuaMethodExample("local nlcliget = client.getavailabletools( );")]
|
[LuaMethodExample("local nlcliget = client.getavailabletools( );")]
|
||||||
[LuaMethod("getavailabletools", "Returns a list of the tools currently open")]
|
[LuaMethod("getavailabletools", "Returns a list of the tools currently open")]
|
||||||
public LuaTable GetAvailableTools() => APIs.Tool.AvailableTools.Select(tool => tool.Name.ToLower()).EnumerateToLuaTable(Lua);
|
public LuaTable GetAvailableTools() => _th.EnumerateToLuaTable(APIs.Tool.AvailableTools.Select(tool => tool.Name.ToLower()));
|
||||||
|
|
||||||
[LuaMethodExample("local nlcliget = client.gettool( \"Tool name\" );")]
|
[LuaMethodExample("local nlcliget = client.gettool( \"Tool name\" );")]
|
||||||
[LuaMethod("gettool", "Returns an object that represents a tool of the given name (not case sensitive). If the tool is not open, it will be loaded if available. Use gettools to get a list of names")]
|
[LuaMethod("gettool", "Returns an object that represents a tool of the given name (not case sensitive). If the tool is not open, it will be loaded if available. Use gettools to get a list of names")]
|
||||||
public LuaTable GetTool(string name)
|
public LuaTable GetTool(string name)
|
||||||
{
|
{
|
||||||
var selectedTool = APIs.Tool.GetTool(name);
|
var selectedTool = APIs.Tool.GetTool(name);
|
||||||
return selectedTool == null ? null : Lua.TableFromObject(selectedTool);
|
return selectedTool == null ? null : _th.ObjectToTable(selectedTool);
|
||||||
}
|
}
|
||||||
|
|
||||||
[LuaMethodExample("local nlclicre = client.createinstance( \"objectname\" );")]
|
[LuaMethodExample("local nlclicre = client.createinstance( \"objectname\" );")]
|
||||||
|
@ -277,7 +277,7 @@ namespace BizHawk.Client.Common
|
||||||
public LuaTable CreateInstance(string name)
|
public LuaTable CreateInstance(string name)
|
||||||
{
|
{
|
||||||
var instance = APIs.Tool.GetTool(name);
|
var instance = APIs.Tool.GetTool(name);
|
||||||
return instance == null ? null : Lua.TableFromObject(instance);
|
return instance == null ? null : _th.ObjectToTable(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
[LuaMethodExample("client.displaymessages( true );")]
|
[LuaMethodExample("client.displaymessages( true );")]
|
||||||
|
|
|
@ -4,8 +4,6 @@ using System.ComponentModel;
|
||||||
using System.Net.WebSockets;
|
using System.Net.WebSockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
using NLua;
|
|
||||||
|
|
||||||
namespace BizHawk.Client.Common
|
namespace BizHawk.Client.Common
|
||||||
{
|
{
|
||||||
[Description("A library for communicating with other programs")]
|
[Description("A library for communicating with other programs")]
|
||||||
|
@ -13,8 +11,8 @@ namespace BizHawk.Client.Common
|
||||||
{
|
{
|
||||||
private readonly IDictionary<Guid, ClientWebSocketWrapper> _websockets = new Dictionary<Guid, ClientWebSocketWrapper>();
|
private readonly IDictionary<Guid, ClientWebSocketWrapper> _websockets = new Dictionary<Guid, ClientWebSocketWrapper>();
|
||||||
|
|
||||||
public CommLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public CommLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "comm";
|
public override string Name => "comm";
|
||||||
|
|
||||||
|
|
|
@ -13,8 +13,8 @@ namespace BizHawk.Client.Common
|
||||||
public Action FrameAdvanceCallback { get; set; }
|
public Action FrameAdvanceCallback { get; set; }
|
||||||
public Action YieldCallback { get; set; }
|
public Action YieldCallback { get; set; }
|
||||||
|
|
||||||
public EmulationLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public EmulationLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "emu";
|
public override string Name => "emu";
|
||||||
|
|
||||||
|
@ -44,12 +44,7 @@ namespace BizHawk.Client.Common
|
||||||
|
|
||||||
[LuaMethodExample("local nlemuget = emu.getregisters( );")]
|
[LuaMethodExample("local nlemuget = emu.getregisters( );")]
|
||||||
[LuaMethod("getregisters", "returns the complete set of available flags and registers for a given core")]
|
[LuaMethod("getregisters", "returns the complete set of available flags and registers for a given core")]
|
||||||
public LuaTable GetRegisters()
|
public LuaTable GetRegisters() => _th.DictToTable(APIs.Emulation.GetRegisters());
|
||||||
{
|
|
||||||
return APIs.Emulation
|
|
||||||
.GetRegisters()
|
|
||||||
.ToLuaTable(Lua);
|
|
||||||
}
|
|
||||||
|
|
||||||
[LuaMethodExample("emu.setregister( emu.getregisters( )[ 0 ], -1000 );")]
|
[LuaMethodExample("emu.setregister( emu.getregisters( )[ 0 ], -1000 );")]
|
||||||
[LuaMethod("setregister", "sets the given register name to the given value")]
|
[LuaMethod("setregister", "sets the given register name to the given value")]
|
||||||
|
|
|
@ -7,8 +7,8 @@ namespace BizHawk.Client.Common
|
||||||
{
|
{
|
||||||
public sealed class GameInfoLuaLibrary : LuaLibraryBase
|
public sealed class GameInfoLuaLibrary : LuaLibraryBase
|
||||||
{
|
{
|
||||||
public GameInfoLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public GameInfoLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "gameinfo";
|
public override string Name => "gameinfo";
|
||||||
|
|
||||||
|
@ -38,11 +38,6 @@ namespace BizHawk.Client.Common
|
||||||
|
|
||||||
[LuaMethodExample("local nlgamget = gameinfo.getoptions( );")]
|
[LuaMethodExample("local nlgamget = gameinfo.getoptions( );")]
|
||||||
[LuaMethod("getoptions", "returns the game options for the currently loaded rom. Options vary per platform")]
|
[LuaMethod("getoptions", "returns the game options for the currently loaded rom. Options vary per platform")]
|
||||||
public LuaTable GetOptions()
|
public LuaTable GetOptions() => _th.DictToTable(APIs.GameInfo.GetOptions());
|
||||||
{
|
|
||||||
return APIs.GameInfo
|
|
||||||
.GetOptions()
|
|
||||||
.ToLuaTable(Lua);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
using NLua;
|
using NLua;
|
||||||
|
|
||||||
|
@ -9,8 +10,8 @@ namespace BizHawk.Client.Common
|
||||||
{
|
{
|
||||||
public Func<int, int, int?, int?, LuaTable> CreateLuaCanvasCallback { get; set; }
|
public Func<int, int, int?, int?, LuaTable> CreateLuaCanvasCallback { get; set; }
|
||||||
|
|
||||||
public GuiLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public GuiLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "gui";
|
public override string Name => "gui";
|
||||||
|
|
||||||
|
@ -62,7 +63,7 @@ namespace BizHawk.Client.Common
|
||||||
{
|
{
|
||||||
var pointsArr = new Point[4];
|
var pointsArr = new Point[4];
|
||||||
var i = 0;
|
var i = 0;
|
||||||
foreach (LuaTable point in points.Values)
|
foreach (var point in _th.EnumerateValues<LuaTable>(points))
|
||||||
{
|
{
|
||||||
pointsArr[i] = new Point(LuaInt(point[1]), LuaInt(point[2]));
|
pointsArr[i] = new Point(LuaInt(point[1]), LuaInt(point[2]));
|
||||||
i++;
|
i++;
|
||||||
|
@ -123,11 +124,12 @@ namespace BizHawk.Client.Common
|
||||||
[LuaMethod("drawPolygon", "Draws a polygon using the table of coordinates specified in points. This should be a table of tables(each of size 2). If x or y is passed, the polygon will be translated by the passed coordinate pair. Line is the color of the polygon. Background is the optional fill color")]
|
[LuaMethod("drawPolygon", "Draws a polygon using the table of coordinates specified in points. This should be a table of tables(each of size 2). If x or y is passed, the polygon will be translated by the passed coordinate pair. Line is the color of the polygon. Background is the optional fill color")]
|
||||||
public void DrawPolygon(LuaTable points, int? offsetX = null, int? offsetY = null, Color? line = null, Color? background = null)
|
public void DrawPolygon(LuaTable points, int? offsetX = null, int? offsetY = null, Color? line = null, Color? background = null)
|
||||||
{
|
{
|
||||||
|
var pointsList = _th.EnumerateValues<LuaTable>(points).ToList();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var pointsArr = new Point[points.Values.Count];
|
var pointsArr = new Point[pointsList.Count];
|
||||||
var i = 0;
|
var i = 0;
|
||||||
foreach (LuaTable point in points.Values)
|
foreach (var point in pointsList)
|
||||||
{
|
{
|
||||||
pointsArr[i] = new Point(LuaInt(point[1]) + (offsetX ?? 0), LuaInt(point[2]) + (offsetY ?? 0));
|
pointsArr[i] = new Point(LuaInt(point[1]) + (offsetX ?? 0), LuaInt(point[2]) + (offsetY ?? 0));
|
||||||
i++;
|
i++;
|
||||||
|
|
|
@ -6,17 +6,17 @@ namespace BizHawk.Client.Common
|
||||||
{
|
{
|
||||||
public sealed class InputLuaLibrary : LuaLibraryBase
|
public sealed class InputLuaLibrary : LuaLibraryBase
|
||||||
{
|
{
|
||||||
public InputLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public InputLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "input";
|
public override string Name => "input";
|
||||||
|
|
||||||
[LuaMethodExample("local nlinpget = input.get( );")]
|
[LuaMethodExample("local nlinpget = input.get( );")]
|
||||||
[LuaMethod("get", "Returns a lua table of all the buttons the user is currently pressing on their keyboard and gamepads\nAll buttons that are pressed have their key values set to true; all others remain nil.")]
|
[LuaMethod("get", "Returns a lua table of all the buttons the user is currently pressing on their keyboard and gamepads\nAll buttons that are pressed have their key values set to true; all others remain nil.")]
|
||||||
public LuaTable Get() => APIs.Input.Get().ToLuaTable(Lua);
|
public LuaTable Get() => _th.DictToTable(APIs.Input.Get());
|
||||||
|
|
||||||
[LuaMethodExample("local nlinpget = input.getmouse( );")]
|
[LuaMethodExample("local nlinpget = input.getmouse( );")]
|
||||||
[LuaMethod("getmouse", "Returns a lua table of the mouse X/Y coordinates and button states. Table keys are X, Y, Left, Middle, Right, XButton1, XButton2, Wheel.")]
|
[LuaMethod("getmouse", "Returns a lua table of the mouse X/Y coordinates and button states. Table keys are X, Y, Left, Middle, Right, XButton1, XButton2, Wheel.")]
|
||||||
public LuaTable GetMouse() => APIs.Input.GetMouse().ToLuaTable(Lua);
|
public LuaTable GetMouse() => _th.DictToTable(APIs.Input.GetMouse());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,8 @@ namespace BizHawk.Client.Common
|
||||||
{
|
{
|
||||||
public sealed class JoypadLuaLibrary : LuaLibraryBase
|
public sealed class JoypadLuaLibrary : LuaLibraryBase
|
||||||
{
|
{
|
||||||
public JoypadLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public JoypadLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "joypad";
|
public override string Name => "joypad";
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ namespace BizHawk.Client.Common
|
||||||
[LuaMethod("get", "returns a lua table of the controller buttons pressed. If supplied, it will only return a table of buttons for the given controller")]
|
[LuaMethod("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)
|
public LuaTable Get(int? controller = null)
|
||||||
{
|
{
|
||||||
var table = APIs.Joypad.Get(controller).ToLuaTable(Lua);
|
var table = _th.DictToTable(APIs.Joypad.Get(controller));
|
||||||
table["clear"] = null;
|
table["clear"] = null;
|
||||||
table["getluafunctionslist"] = null;
|
table["getluafunctionslist"] = null;
|
||||||
table["output"] = null;
|
table["output"] = null;
|
||||||
|
@ -26,7 +26,7 @@ namespace BizHawk.Client.Common
|
||||||
|
|
||||||
[LuaMethodExample("local nljoyget = joypad.getimmediate( );")]
|
[LuaMethodExample("local nljoyget = joypad.getimmediate( );")]
|
||||||
[LuaMethod("getimmediate", "returns a lua table of any controller buttons currently pressed by the user")]
|
[LuaMethod("getimmediate", "returns a lua table of any controller buttons currently pressed by the user")]
|
||||||
public LuaTable GetImmediate(int? controller = null) => APIs.Joypad.GetImmediate(controller).ToLuaTable(Lua);
|
public LuaTable GetImmediate(int? controller = null) => _th.DictToTable(APIs.Joypad.GetImmediate(controller));
|
||||||
|
|
||||||
[LuaMethodExample("joypad.setfrommnemonicstr( \"| 0, 0, 0, 100,...R..B....|\" );")]
|
[LuaMethodExample("joypad.setfrommnemonicstr( \"| 0, 0, 0, 100,...R..B....|\" );")]
|
||||||
[LuaMethod("setfrommnemonicstr", "sets the given buttons to their provided values for the current frame, string will be interpreted the same way an entry from a movie input log would be")]
|
[LuaMethod("setfrommnemonicstr", "sets the given buttons to their provided values for the current frame, string will be interpreted the same way an entry from a movie input log would be")]
|
||||||
|
@ -37,7 +37,10 @@ namespace BizHawk.Client.Common
|
||||||
public void Set(LuaTable buttons, int? controller = null)
|
public void Set(LuaTable buttons, int? controller = null)
|
||||||
{
|
{
|
||||||
var dict = new Dictionary<string, bool>();
|
var dict = new Dictionary<string, bool>();
|
||||||
foreach (var k in buttons.Keys) dict[k.ToString()] = Convert.ToBoolean(buttons[k]); // Accepts 1/0 or true/false
|
foreach (var (k, v) in _th.EnumerateEntries<object, object>(buttons))
|
||||||
|
{
|
||||||
|
dict[k.ToString()] = Convert.ToBoolean(v); // Accepts 1/0 or true/false
|
||||||
|
}
|
||||||
APIs.Joypad.Set(dict, controller);
|
APIs.Joypad.Set(dict, controller);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,7 +49,10 @@ namespace BizHawk.Client.Common
|
||||||
public void SetAnalog(LuaTable controls, object controller = null)
|
public void SetAnalog(LuaTable controls, object controller = null)
|
||||||
{
|
{
|
||||||
var dict = new Dictionary<string, int?>();
|
var dict = new Dictionary<string, int?>();
|
||||||
foreach (var k in controls.Keys) dict[k.ToString()] = double.TryParse(controls[k].ToString(), out var d) ? (int) d : (int?) null;
|
foreach (var (k, v) in _th.EnumerateEntries<object, object>(controls))
|
||||||
|
{
|
||||||
|
dict[k.ToString()] = double.TryParse(v.ToString(), out var d) ? (int) d : (int?) null;
|
||||||
|
}
|
||||||
APIs.Joypad.SetAnalog(dict, controller);
|
APIs.Joypad.SetAnalog(dict, controller);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,19 +9,14 @@ namespace BizHawk.Client.Common
|
||||||
[Description("These functions behavior identically to the mainmemory functions but the user can set the memory domain to read and write from. The default domain is the system bus. Use getcurrentmemorydomain(), and usememorydomain() to control which domain is used. Each core has its own set of valid memory domains. Use getmemorydomainlist() to get a list of memory domains for the current core loaded.")]
|
[Description("These functions behavior identically to the mainmemory functions but the user can set the memory domain to read and write from. The default domain is the system bus. Use getcurrentmemorydomain(), and usememorydomain() to control which domain is used. Each core has its own set of valid memory domains. Use getmemorydomainlist() to get a list of memory domains for the current core loaded.")]
|
||||||
public sealed class MemoryLuaLibrary : LuaLibraryBase
|
public sealed class MemoryLuaLibrary : LuaLibraryBase
|
||||||
{
|
{
|
||||||
public MemoryLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public MemoryLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "memory";
|
public override string Name => "memory";
|
||||||
|
|
||||||
[LuaMethodExample("local nlmemget = memory.getmemorydomainlist();")]
|
[LuaMethodExample("local nlmemget = memory.getmemorydomainlist();")]
|
||||||
[LuaMethod("getmemorydomainlist", "Returns a string of the memory domains for the loaded platform core. List will be a single string delimited by line feeds")]
|
[LuaMethod("getmemorydomainlist", "Returns a string of the memory domains for the loaded platform core. List will be a single string delimited by line feeds")]
|
||||||
public LuaTable GetMemoryDomainList()
|
public LuaTable GetMemoryDomainList() => _th.ListToTable(APIs.Memory.GetMemoryDomainList());
|
||||||
{
|
|
||||||
return APIs.Memory
|
|
||||||
.GetMemoryDomainList()
|
|
||||||
.ToLuaTable(Lua);
|
|
||||||
}
|
|
||||||
|
|
||||||
[LuaMethodExample("local uimemget = memory.getmemorydomainsize( mainmemory.getname( ) );")]
|
[LuaMethodExample("local uimemget = memory.getmemorydomainsize( mainmemory.getname( ) );")]
|
||||||
[LuaMethod("getmemorydomainsize", "Returns the number of bytes of the specified memory domain. If no domain is specified, or the specified domain doesn't exist, returns the current domain size")]
|
[LuaMethod("getmemorydomainsize", "Returns the number of bytes of the specified memory domain. If no domain is specified, or the specified domain doesn't exist, returns the current domain size")]
|
||||||
|
@ -53,12 +48,7 @@ namespace BizHawk.Client.Common
|
||||||
|
|
||||||
[LuaMethodExample("local nlmemrea = memory.readbyterange( 0x100, 30, mainmemory.getname( ) );")]
|
[LuaMethodExample("local nlmemrea = memory.readbyterange( 0x100, 30, mainmemory.getname( ) );")]
|
||||||
[LuaMethod("readbyterange", "Reads the address range that starts from address, and is length long. Returns the result into a table of key value pairs (where the address is the key).")]
|
[LuaMethod("readbyterange", "Reads the address range that starts from address, and is length long. Returns the result into a table of key value pairs (where the address is the key).")]
|
||||||
public LuaTable ReadByteRange(int addr, int length, string domain = null)
|
public LuaTable ReadByteRange(int addr, int length, string domain = null) => _th.ListToTable(APIs.Memory.ReadByteRange(addr, length, domain));
|
||||||
{
|
|
||||||
return APIs.Memory
|
|
||||||
.ReadByteRange(addr, length, domain)
|
|
||||||
.ToLuaTable(Lua);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <remarks>TODO C# version requires a contiguous address range</remarks>
|
/// <remarks>TODO C# version requires a contiguous address range</remarks>
|
||||||
[LuaMethodExample("")]
|
[LuaMethodExample("")]
|
||||||
|
@ -66,17 +56,20 @@ namespace BizHawk.Client.Common
|
||||||
public void WriteByteRange(LuaTable memoryblock, string domain = null)
|
public void WriteByteRange(LuaTable memoryblock, string domain = null)
|
||||||
{
|
{
|
||||||
#if true
|
#if true
|
||||||
foreach (var addr in memoryblock.Keys) APIs.Memory.WriteByte(LuaInt(addr), (uint) memoryblock[addr], domain);
|
foreach (var (addr, v) in _th.EnumerateEntries<double, double>(memoryblock))
|
||||||
|
{
|
||||||
|
APIs.Memory.WriteByte(LuaInt(addr), (uint) v, domain);
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
var d = string.IsNullOrEmpty(domain) ? Domain : DomainList[VerifyMemoryDomain(domain)];
|
var d = string.IsNullOrEmpty(domain) ? Domain : DomainList[VerifyMemoryDomain(domain)];
|
||||||
if (d.CanPoke())
|
if (d.CanPoke())
|
||||||
{
|
{
|
||||||
foreach (var address in memoryblock.Keys)
|
foreach (var (address, v) in _th.EnumerateEntries<double, double>(memoryblock))
|
||||||
{
|
{
|
||||||
var addr = LuaInt(address);
|
var addr = LuaInt(address);
|
||||||
if (addr < d.Size)
|
if (addr < d.Size)
|
||||||
{
|
{
|
||||||
d.PokeByte(addr, (byte)LuaInt(memoryblock[address]));
|
d.PokeByte(addr, (byte) LuaInt(v));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
using System;
|
using System;
|
||||||
using NLua;
|
|
||||||
|
|
||||||
// ReSharper disable UnusedMember.Global
|
// ReSharper disable UnusedMember.Global
|
||||||
// ReSharper disable UnusedAutoPropertyAccessor.Local
|
// ReSharper disable UnusedAutoPropertyAccessor.Local
|
||||||
|
@ -7,8 +6,8 @@ namespace BizHawk.Client.Common
|
||||||
{
|
{
|
||||||
public sealed class MemorySavestateLuaLibrary : LuaLibraryBase
|
public sealed class MemorySavestateLuaLibrary : LuaLibraryBase
|
||||||
{
|
{
|
||||||
public MemorySavestateLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public MemorySavestateLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "memorysavestate";
|
public override string Name => "memorysavestate";
|
||||||
|
|
||||||
|
|
|
@ -7,8 +7,8 @@ namespace BizHawk.Client.Common
|
||||||
{
|
{
|
||||||
public sealed class MovieLuaLibrary : LuaLibraryBase
|
public sealed class MovieLuaLibrary : LuaLibraryBase
|
||||||
{
|
{
|
||||||
public MovieLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public MovieLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "movie";
|
public override string Name => "movie";
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ namespace BizHawk.Client.Common
|
||||||
|
|
||||||
[LuaMethodExample("local nlmovget = movie.getinput( 500 );")]
|
[LuaMethodExample("local nlmovget = movie.getinput( 500 );")]
|
||||||
[LuaMethod("getinput", "Returns a table of buttons pressed on a given frame of the loaded movie")]
|
[LuaMethod("getinput", "Returns a table of buttons pressed on a given frame of the loaded movie")]
|
||||||
public LuaTable GetInput(int frame, int? controller = null) => APIs.Movie.GetInput(frame, controller).ToLuaTable(Lua);
|
public LuaTable GetInput(int frame, int? controller = null) => _th.DictToTable(APIs.Movie.GetInput(frame, controller));
|
||||||
|
|
||||||
[LuaMethodExample("local stmovget = movie.getinputasmnemonic( 500 );")]
|
[LuaMethodExample("local stmovget = movie.getinputasmnemonic( 500 );")]
|
||||||
[LuaMethod("getinputasmnemonic", "Returns the input of a given frame of the loaded movie in a raw inputlog string")]
|
[LuaMethod("getinputasmnemonic", "Returns the input of a given frame of the loaded movie in a raw inputlog string")]
|
||||||
|
@ -82,30 +82,14 @@ namespace BizHawk.Client.Common
|
||||||
|
|
||||||
[LuaMethodExample("local nlmovget = movie.getheader( );")]
|
[LuaMethodExample("local nlmovget = movie.getheader( );")]
|
||||||
[LuaMethod("getheader", "If a movie is active, will return the movie header as a lua table")]
|
[LuaMethod("getheader", "If a movie is active, will return the movie header as a lua table")]
|
||||||
public LuaTable GetHeader()
|
public LuaTable GetHeader() => _th.DictToTable(APIs.Movie.GetHeader());
|
||||||
{
|
|
||||||
|
|
||||||
return APIs.Movie
|
|
||||||
.GetHeader()
|
|
||||||
.ToLuaTable(Lua);
|
|
||||||
}
|
|
||||||
|
|
||||||
[LuaMethodExample("local nlmovget = movie.getcomments( );")]
|
[LuaMethodExample("local nlmovget = movie.getcomments( );")]
|
||||||
[LuaMethod("getcomments", "If a movie is active, will return the movie comments as a lua table")]
|
[LuaMethod("getcomments", "If a movie is active, will return the movie comments as a lua table")]
|
||||||
public LuaTable GetComments()
|
public LuaTable GetComments() => _th.ListToTable(APIs.Movie.GetComments());
|
||||||
{
|
|
||||||
return APIs.Movie
|
|
||||||
.GetComments()
|
|
||||||
.ToLuaTable(Lua);
|
|
||||||
}
|
|
||||||
|
|
||||||
[LuaMethodExample("local nlmovget = movie.getsubtitles( );")]
|
[LuaMethodExample("local nlmovget = movie.getsubtitles( );")]
|
||||||
[LuaMethod("getsubtitles", "If a movie is active, will return the movie subtitles as a lua table")]
|
[LuaMethod("getsubtitles", "If a movie is active, will return the movie subtitles as a lua table")]
|
||||||
public LuaTable GetSubtitles()
|
public LuaTable GetSubtitles() => _th.ListToTable(APIs.Movie.GetSubtitles());
|
||||||
{
|
|
||||||
return APIs.Movie
|
|
||||||
.GetSubtitles()
|
|
||||||
.ToLuaTable(Lua);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,16 +2,14 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
|
|
||||||
using NLua;
|
|
||||||
|
|
||||||
// ReSharper disable UnusedMember.Global
|
// ReSharper disable UnusedMember.Global
|
||||||
namespace BizHawk.Client.Common
|
namespace BizHawk.Client.Common
|
||||||
{
|
{
|
||||||
[Description("A library for performing SQLite operations.")]
|
[Description("A library for performing SQLite operations.")]
|
||||||
public sealed class SQLiteLuaLibrary : LuaLibraryBase
|
public sealed class SQLiteLuaLibrary : LuaLibraryBase
|
||||||
{
|
{
|
||||||
public SQLiteLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public SQLiteLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "SQL";
|
public override string Name => "SQL";
|
||||||
|
|
||||||
|
@ -34,7 +32,7 @@ namespace BizHawk.Client.Common
|
||||||
public object ReadCommand(string query = "")
|
public object ReadCommand(string query = "")
|
||||||
{
|
{
|
||||||
var result = APIs.SQLite.ReadCommand(query);
|
var result = APIs.SQLite.ReadCommand(query);
|
||||||
return result is Dictionary<string, object> dict ? dict.ToLuaTable(Lua) : result;
|
return result is Dictionary<string, object> dict ? _th.DictToTable(dict) : result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,11 @@
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
using NLua;
|
|
||||||
|
|
||||||
namespace BizHawk.Client.Common
|
namespace BizHawk.Client.Common
|
||||||
{
|
{
|
||||||
public sealed class SaveStateLuaLibrary : LuaLibraryBase
|
public sealed class SaveStateLuaLibrary : LuaLibraryBase
|
||||||
{
|
{
|
||||||
public SaveStateLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public SaveStateLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "savestate";
|
public override string Name => "savestate";
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using NLua;
|
|
||||||
|
|
||||||
// ReSharper disable UnusedMember.Global
|
// ReSharper disable UnusedMember.Global
|
||||||
namespace BizHawk.Client.Common
|
namespace BizHawk.Client.Common
|
||||||
|
@ -8,8 +7,8 @@ namespace BizHawk.Client.Common
|
||||||
[Description("A library for setting and retrieving dynamic data that will be saved and loaded with savestates")]
|
[Description("A library for setting and retrieving dynamic data that will be saved and loaded with savestates")]
|
||||||
public sealed class UserDataLuaLibrary : LuaLibraryBase
|
public sealed class UserDataLuaLibrary : LuaLibraryBase
|
||||||
{
|
{
|
||||||
public UserDataLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public UserDataLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "userdata";
|
public override string Name => "userdata";
|
||||||
|
|
||||||
|
|
|
@ -19,41 +19,6 @@ namespace BizHawk.Client.Common
|
||||||
/// <exception cref="ArithmeticException"><paramref name="d"/> ≥ 2^53 or <paramref name="d"/> ≤ -2^53</exception>
|
/// <exception cref="ArithmeticException"><paramref name="d"/> ≥ 2^53 or <paramref name="d"/> ≤ -2^53</exception>
|
||||||
public static long AsInteger(this double d) => PrecisionLimits.Contains(d) ? (long) d : throw new ArithmeticException("integer value exceeds the precision of Lua's integer-as-double");
|
public static long AsInteger(this double d) => PrecisionLimits.Contains(d) ? (long) d : throw new ArithmeticException("integer value exceeds the precision of Lua's integer-as-double");
|
||||||
|
|
||||||
public static LuaTable EnumerateToLuaTable<T>(this IEnumerable<T> list, Lua lua) => list.ToList().ToLuaTable(lua);
|
public static LuaTable EnumerateToLuaTable<T>(this NLuaTableHelper tableHelper, IEnumerable<T> list) => tableHelper.ListToTable(list.ToList());
|
||||||
|
|
||||||
public static LuaTable ToLuaTable<T>(this IList<T> list, Lua lua, int indexFrom = 0)
|
|
||||||
{
|
|
||||||
var table = lua.NewTable();
|
|
||||||
for (int i = 0, l = list.Count; i != l; i++) table[indexFrom + i] = list[i];
|
|
||||||
return table;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static LuaTable ToLuaTable<T>(this IDictionary<string, T> dictionary, Lua lua)
|
|
||||||
{
|
|
||||||
var table = lua.NewTable();
|
|
||||||
|
|
||||||
foreach (var kvp in dictionary)
|
|
||||||
{
|
|
||||||
table[kvp.Key] = kvp.Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return table;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static LuaTable TableFromObject(this Lua lua, object obj)
|
|
||||||
{
|
|
||||||
var table = lua.NewTable();
|
|
||||||
foreach (var method in obj.GetType().GetMethods())
|
|
||||||
{
|
|
||||||
if (!method.IsPublic) continue;
|
|
||||||
var foundAttrs = method.GetCustomAttributes(typeof(LuaMethodAttribute), false);
|
|
||||||
table[method.Name] = lua.RegisterFunction(
|
|
||||||
foundAttrs.Length == 0 ? string.Empty : ((LuaMethodAttribute) foundAttrs[0]).Name, // empty string will default to the actual method name
|
|
||||||
obj,
|
|
||||||
method
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return table;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,14 @@
|
||||||
using System;
|
using System;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
|
|
||||||
using NLua;
|
|
||||||
|
|
||||||
// ReSharper disable UnusedMember.Global
|
// ReSharper disable UnusedMember.Global
|
||||||
namespace BizHawk.Client.Common
|
namespace BizHawk.Client.Common
|
||||||
{
|
{
|
||||||
[Description("A library for performing standard bitwise operations.")]
|
[Description("A library for performing standard bitwise operations.")]
|
||||||
public sealed class BitLuaLibrary : LuaLibraryBase
|
public sealed class BitLuaLibrary : LuaLibraryBase
|
||||||
{
|
{
|
||||||
public BitLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public BitLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "bit";
|
public override string Name => "bit";
|
||||||
|
|
||||||
|
|
|
@ -24,8 +24,8 @@ namespace BizHawk.Client.Common
|
||||||
[OptionalService]
|
[OptionalService]
|
||||||
private IMemoryDomains Domains { get; set; }
|
private IMemoryDomains Domains { get; set; }
|
||||||
|
|
||||||
public EventsLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public EventsLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "event";
|
public override string Name => "event";
|
||||||
|
|
||||||
|
@ -360,8 +360,8 @@ namespace BizHawk.Client.Common
|
||||||
public LuaTable AvailableScopes()
|
public LuaTable AvailableScopes()
|
||||||
{
|
{
|
||||||
return DebuggableCore?.MemoryCallbacksAvailable() == true
|
return DebuggableCore?.MemoryCallbacksAvailable() == true
|
||||||
? DebuggableCore.MemoryCallbacks.AvailableScopes.ToLuaTable(Lua)
|
? _th.ListToTable(DebuggableCore.MemoryCallbacks.AvailableScopes)
|
||||||
: Lua.NewTable();
|
: _th.CreateTable();
|
||||||
}
|
}
|
||||||
|
|
||||||
private string ProcessScope(string scope)
|
private string ProcessScope(string scope)
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
|
|
||||||
using NLua;
|
|
||||||
using BizHawk.Emulation.Cores.Consoles.Sega.gpgx;
|
using BizHawk.Emulation.Cores.Consoles.Sega.gpgx;
|
||||||
|
|
||||||
// ReSharper disable UnusedMember.Global
|
// ReSharper disable UnusedMember.Global
|
||||||
|
@ -11,8 +10,8 @@ namespace BizHawk.Client.Common
|
||||||
[Description("Functions specific to GenesisHawk (functions may not run when an Genesis game is not loaded)")]
|
[Description("Functions specific to GenesisHawk (functions may not run when an Genesis game is not loaded)")]
|
||||||
public sealed class GenesisLuaLibrary : LuaLibraryBase
|
public sealed class GenesisLuaLibrary : LuaLibraryBase
|
||||||
{
|
{
|
||||||
public GenesisLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public GenesisLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "genesis";
|
public override string Name => "genesis";
|
||||||
|
|
||||||
|
|
|
@ -17,8 +17,8 @@ namespace BizHawk.Client.Common
|
||||||
[OptionalService]
|
[OptionalService]
|
||||||
private IMemoryDomains MemoryDomainCore { get; set; }
|
private IMemoryDomains MemoryDomainCore { get; set; }
|
||||||
|
|
||||||
public MainMemoryLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public MainMemoryLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "mainmemory";
|
public override string Name => "mainmemory";
|
||||||
|
|
||||||
|
@ -63,9 +63,7 @@ namespace BizHawk.Client.Common
|
||||||
[LuaMethod("readbyterange", "Reads the address range that starts from address, and is length long. Returns the result into a table of key value pairs (where the address is the key).")]
|
[LuaMethod("readbyterange", "Reads the address range that starts from address, and is length long. Returns the result into a table of key value pairs (where the address is the key).")]
|
||||||
public LuaTable ReadByteRange(int addr, int length)
|
public LuaTable ReadByteRange(int addr, int length)
|
||||||
{
|
{
|
||||||
return APIs.Memory
|
return _th.ListToTable(APIs.Memory.ReadByteRange(addr, length, Domain.Name));
|
||||||
.ReadByteRange(addr, length, Domain.Name)
|
|
||||||
.ToLuaTable(Lua);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <remarks>TODO C# version requires a contiguous address range</remarks>
|
/// <remarks>TODO C# version requires a contiguous address range</remarks>
|
||||||
|
@ -74,17 +72,20 @@ namespace BizHawk.Client.Common
|
||||||
public void WriteByteRange(LuaTable memoryblock)
|
public void WriteByteRange(LuaTable memoryblock)
|
||||||
{
|
{
|
||||||
#if true
|
#if true
|
||||||
foreach (var addr in memoryblock.Keys) APIs.Memory.WriteByte(LuaInt(addr), (uint) memoryblock[addr], Domain.Name);
|
foreach (var (addr, v) in _th.EnumerateEntries<double, double>(memoryblock))
|
||||||
|
{
|
||||||
|
APIs.Memory.WriteByte(LuaInt(addr), (uint) v, Domain.Name);
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
var d = Domain;
|
var d = Domain;
|
||||||
if (d.CanPoke())
|
if (d.CanPoke())
|
||||||
{
|
{
|
||||||
foreach (var address in memoryblock.Keys)
|
foreach (var (address, v) in _th.EnumerateEntries<double, double>(memoryblock))
|
||||||
{
|
{
|
||||||
var addr = LuaInt(address);
|
var addr = LuaInt(address);
|
||||||
if (addr < d.Size)
|
if (addr < d.Size)
|
||||||
{
|
{
|
||||||
d.PokeByte(addr, (byte)LuaInt(memoryblock[address]));
|
d.PokeByte(addr, (byte) LuaInt(v));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
|
|
||||||
using NLua;
|
|
||||||
|
|
||||||
using BizHawk.Emulation.Common;
|
using BizHawk.Emulation.Common;
|
||||||
using BizHawk.Emulation.Cores.Nintendo.NES;
|
using BizHawk.Emulation.Cores.Nintendo.NES;
|
||||||
using BizHawk.Emulation.Cores.Consoles.Nintendo.QuickNES;
|
using BizHawk.Emulation.Cores.Consoles.Nintendo.QuickNES;
|
||||||
|
@ -21,8 +19,8 @@ namespace BizHawk.Client.Common
|
||||||
[OptionalService]
|
[OptionalService]
|
||||||
private IMemoryDomains MemoryDomains { get; set; }
|
private IMemoryDomains MemoryDomains { get; set; }
|
||||||
|
|
||||||
public NESLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public NESLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "nes";
|
public override string Name => "nes";
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using NLua;
|
|
||||||
using BizHawk.Emulation.Cores.Nintendo.SNES;
|
using BizHawk.Emulation.Cores.Nintendo.SNES;
|
||||||
|
|
||||||
// ReSharper disable UnusedMember.Global
|
// ReSharper disable UnusedMember.Global
|
||||||
|
@ -10,8 +10,8 @@ namespace BizHawk.Client.Common
|
||||||
[Description("Functions specific to SNESHawk (functions may not run when an SNES game is not loaded)")]
|
[Description("Functions specific to SNESHawk (functions may not run when an SNES game is not loaded)")]
|
||||||
public sealed class SNESLuaLibrary : LuaLibraryBase
|
public sealed class SNESLuaLibrary : LuaLibraryBase
|
||||||
{
|
{
|
||||||
public SNESLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public SNESLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "snes";
|
public override string Name => "snes";
|
||||||
|
|
||||||
|
|
|
@ -11,8 +11,8 @@ namespace BizHawk.Client.Common
|
||||||
{
|
{
|
||||||
public override string Name => "bizstring";
|
public override string Name => "bizstring";
|
||||||
|
|
||||||
public StringLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public StringLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
[LuaMethodExample("local stbizhex = bizstring.hex( -12345 );")]
|
[LuaMethodExample("local stbizhex = bizstring.hex( -12345 );")]
|
||||||
[LuaMethod("hex", "Converts the number to a string representation of the hexadecimal value of the given number")]
|
[LuaMethod("hex", "Converts the number to a string representation of the hexadecimal value of the given number")]
|
||||||
|
@ -125,8 +125,7 @@ namespace BizHawk.Client.Common
|
||||||
[LuaMethodExample("local nlbizspl = bizstring.split( \"Some, string\", \", \" );")]
|
[LuaMethodExample("local nlbizspl = bizstring.split( \"Some, string\", \", \" );")]
|
||||||
[LuaMethod("split", "Splits str into a Lua-style array using the given separator (consecutive separators in str will NOT create empty entries in the array). If the separator is not a string exactly one char long, ',' will be used.")]
|
[LuaMethod("split", "Splits str into a Lua-style array using the given separator (consecutive separators in str will NOT create empty entries in the array). If the separator is not a string exactly one char long, ',' will be used.")]
|
||||||
public LuaTable Split(string str, string separator) => string.IsNullOrEmpty(str)
|
public LuaTable Split(string str, string separator) => string.IsNullOrEmpty(str)
|
||||||
? Lua.NewTable()
|
? _th.CreateTable()
|
||||||
: str.Split(new[] { separator?.Length == 1 ? separator[0] : ',' }, StringSplitOptions.RemoveEmptyEntries)
|
: _th.ListToTable(str.Split(new[] { separator?.Length == 1 ? separator[0] : ',' }, StringSplitOptions.RemoveEmptyEntries), indexFrom: 1);
|
||||||
.ToLuaTable(Lua, 1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,6 +40,9 @@ namespace BizHawk.Client.Common
|
||||||
public abstract void Close();
|
public abstract void Close();
|
||||||
public abstract void EndLuaDrawing();
|
public abstract void EndLuaDrawing();
|
||||||
public abstract void ExecuteString(string command);
|
public abstract void ExecuteString(string command);
|
||||||
|
|
||||||
|
public abstract NLuaTableHelper GetTableHelper();
|
||||||
|
|
||||||
public abstract void Restart(IEmulatorServiceProvider newServiceProvider);
|
public abstract void Restart(IEmulatorServiceProvider newServiceProvider);
|
||||||
public abstract (bool WaitForFrame, bool Terminated) ResumeScript(LuaFile lf);
|
public abstract (bool WaitForFrame, bool Terminated) ResumeScript(LuaFile lf);
|
||||||
public abstract void SpawnAndSetFileThread(string pathToLoad, LuaFile lf);
|
public abstract void SpawnAndSetFileThread(string pathToLoad, LuaFile lf);
|
||||||
|
|
|
@ -2,17 +2,15 @@
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
using NLua;
|
|
||||||
|
|
||||||
namespace BizHawk.Client.Common
|
namespace BizHawk.Client.Common
|
||||||
{
|
{
|
||||||
public abstract class LuaLibraryBase
|
public abstract class LuaLibraryBase
|
||||||
{
|
{
|
||||||
protected LuaLibraryBase(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
protected LuaLibraryBase(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
{
|
{
|
||||||
LogOutputCallback = logOutputCallback;
|
LogOutputCallback = logOutputCallback;
|
||||||
Lua = lua;
|
|
||||||
_luaLibsImpl = luaLibsImpl;
|
_luaLibsImpl = luaLibsImpl;
|
||||||
|
_th = _luaLibsImpl.GetTableHelper();
|
||||||
APIs = apiContainer;
|
APIs = apiContainer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,10 +25,10 @@ namespace BizHawk.Client.Common
|
||||||
|
|
||||||
protected readonly Action<string> LogOutputCallback;
|
protected readonly Action<string> LogOutputCallback;
|
||||||
|
|
||||||
public Lua Lua { get; }
|
|
||||||
|
|
||||||
protected readonly LuaLibraries _luaLibsImpl;
|
protected readonly LuaLibraries _luaLibsImpl;
|
||||||
|
|
||||||
|
protected readonly NLuaTableHelper _th;
|
||||||
|
|
||||||
public static void ClearCurrentThread()
|
public static void ClearCurrentThread()
|
||||||
{
|
{
|
||||||
lock (ThreadMutex)
|
lock (ThreadMutex)
|
||||||
|
|
|
@ -0,0 +1,56 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
using NLua;
|
||||||
|
|
||||||
|
namespace BizHawk.Client.Common
|
||||||
|
{
|
||||||
|
public sealed class NLuaTableHelper
|
||||||
|
{
|
||||||
|
private readonly Lua _lua;
|
||||||
|
|
||||||
|
public NLuaTableHelper(Lua lua) => _lua = lua;
|
||||||
|
|
||||||
|
public LuaTable CreateTable() => _lua.NewTable();
|
||||||
|
|
||||||
|
public LuaTable DictToTable<T>(IDictionary<string, T> dictionary)
|
||||||
|
{
|
||||||
|
var table = _lua.NewTable();
|
||||||
|
|
||||||
|
foreach (var kvp in dictionary)
|
||||||
|
{
|
||||||
|
table[kvp.Key] = kvp.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<(TKey Key, TValue Value)> EnumerateEntries<TKey, TValue>(LuaTable table)
|
||||||
|
=> table.Keys.Cast<TKey>().Select(k => (k, (TValue) table[k]));
|
||||||
|
|
||||||
|
public IEnumerable<T> EnumerateValues<T>(LuaTable table) => table.Values.Cast<T>();
|
||||||
|
|
||||||
|
public LuaTable ListToTable<T>(IList<T> list, int indexFrom = 0)
|
||||||
|
{
|
||||||
|
var table = _lua.NewTable();
|
||||||
|
for (int i = 0, l = list.Count; i != l; i++) table[indexFrom + i] = list[i];
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LuaTable ObjectToTable(object obj)
|
||||||
|
{
|
||||||
|
var table = _lua.NewTable();
|
||||||
|
foreach (var method in obj.GetType().GetMethods())
|
||||||
|
{
|
||||||
|
if (!method.IsPublic) continue;
|
||||||
|
var foundAttrs = method.GetCustomAttributes(typeof(LuaMethodAttribute), false);
|
||||||
|
table[method.Name] = _lua.RegisterFunction(
|
||||||
|
foundAttrs.Length == 0 ? string.Empty : ((LuaMethodAttribute) foundAttrs[0]).Name, // empty string will default to the actual method name
|
||||||
|
obj,
|
||||||
|
method
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,8 +11,8 @@ namespace BizHawk.Client.EmuHawk
|
||||||
{
|
{
|
||||||
public ToolManager Tools { get; set; }
|
public ToolManager Tools { get; set; }
|
||||||
|
|
||||||
public ConsoleLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public ConsoleLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "console";
|
public override string Name => "console";
|
||||||
|
|
||||||
|
|
|
@ -14,8 +14,8 @@ namespace BizHawk.Client.EmuHawk
|
||||||
[Description("A library for creating and managing custom dialogs")]
|
[Description("A library for creating and managing custom dialogs")]
|
||||||
public sealed class FormsLuaLibrary : LuaLibraryBase
|
public sealed class FormsLuaLibrary : LuaLibraryBase
|
||||||
{
|
{
|
||||||
public FormsLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public FormsLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "forms";
|
public override string Name => "forms";
|
||||||
|
|
||||||
|
@ -192,7 +192,7 @@ namespace BizHawk.Client.EmuHawk
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
var dropdownItems = items.Values.Cast<string>().ToList();
|
var dropdownItems = _th.EnumerateValues<string>(items).ToList();
|
||||||
dropdownItems.Sort();
|
dropdownItems.Sort();
|
||||||
|
|
||||||
var dropdown = new LuaDropDown(dropdownItems);
|
var dropdown = new LuaDropDown(dropdownItems);
|
||||||
|
@ -1246,7 +1246,7 @@ namespace BizHawk.Client.EmuHawk
|
||||||
{
|
{
|
||||||
if (control is LuaDropDown)
|
if (control is LuaDropDown)
|
||||||
{
|
{
|
||||||
var dropdownItems = items.Values.Cast<string>().ToList();
|
var dropdownItems = _th.EnumerateValues<string>(items).ToList();
|
||||||
dropdownItems.Sort();
|
dropdownItems.Sort();
|
||||||
(control as LuaDropDown).SetItems(dropdownItems);
|
(control as LuaDropDown).SetItems(dropdownItems);
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,8 +18,8 @@ namespace BizHawk.Client.EmuHawk
|
||||||
{
|
{
|
||||||
public ToolManager Tools { get; set; }
|
public ToolManager Tools { get; set; }
|
||||||
|
|
||||||
public TAStudioLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action<string> logOutputCallback)
|
public TAStudioLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
|
||||||
: base(luaLibsImpl, apiContainer, lua, logOutputCallback) {}
|
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
|
||||||
|
|
||||||
public override string Name => "tastudio";
|
public override string Name => "tastudio";
|
||||||
|
|
||||||
|
@ -165,7 +165,7 @@ namespace BizHawk.Client.EmuHawk
|
||||||
|
|
||||||
[LuaMethodExample("local nltasget = tastudio.getselection( );")]
|
[LuaMethodExample("local nltasget = tastudio.getselection( );")]
|
||||||
[LuaMethod("getselection", "gets the currently selected frames")]
|
[LuaMethod("getselection", "gets the currently selected frames")]
|
||||||
public LuaTable GetSelection() => Engaged() ? Tastudio.GetSelection().EnumerateToLuaTable(Lua) : Lua.NewTable();
|
public LuaTable GetSelection() => Engaged() ? _th.EnumerateToLuaTable(Tastudio.GetSelection()) : _th.CreateTable();
|
||||||
|
|
||||||
[LuaMethodExample("")]
|
[LuaMethodExample("")]
|
||||||
[LuaMethod("submitinputchange", "")]
|
[LuaMethod("submitinputchange", "")]
|
||||||
|
@ -375,32 +375,25 @@ namespace BizHawk.Client.EmuHawk
|
||||||
[LuaMethod("getbranches", "Returns a list of the current tastudio branches. Each entry will have the Id, Frame, and Text properties of the branch")]
|
[LuaMethod("getbranches", "Returns a list of the current tastudio branches. Each entry will have the Id, Frame, and Text properties of the branch")]
|
||||||
public LuaTable GetBranches()
|
public LuaTable GetBranches()
|
||||||
{
|
{
|
||||||
if (Engaged())
|
if (!Engaged()) return _th.CreateTable();
|
||||||
{
|
return _th.EnumerateToLuaTable(Tastudio.CurrentTasMovie.Branches.Select(b => new
|
||||||
return Tastudio.CurrentTasMovie.Branches
|
|
||||||
.Select(b => new
|
|
||||||
{
|
{
|
||||||
Id = b.Uuid.ToString(),
|
Id = b.Uuid.ToString(),
|
||||||
b.Frame,
|
b.Frame,
|
||||||
Text = b.UserText
|
Text = b.UserText
|
||||||
})
|
}));
|
||||||
.EnumerateToLuaTable(Lua);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Lua.NewTable();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[LuaMethodExample("local nltasget = tastudio.getbranchinput( \"97021544-2454-4483-824f-47f75e7fcb6a\", 500 );")]
|
[LuaMethodExample("local nltasget = tastudio.getbranchinput( \"97021544-2454-4483-824f-47f75e7fcb6a\", 500 );")]
|
||||||
[LuaMethod("getbranchinput", "Gets the controller state of the given frame with the given branch identifier")]
|
[LuaMethod("getbranchinput", "Gets the controller state of the given frame with the given branch identifier")]
|
||||||
public LuaTable GetBranchInput(string branchId, int frame)
|
public LuaTable GetBranchInput(string branchId, int frame)
|
||||||
{
|
{
|
||||||
var table = Lua.NewTable();
|
var table = _th.CreateTable();
|
||||||
|
if (!Engaged()) return table;
|
||||||
|
|
||||||
if (Engaged())
|
|
||||||
{
|
|
||||||
var controller = Tastudio.GetBranchInput(branchId, frame);
|
var controller = Tastudio.GetBranchInput(branchId, frame);
|
||||||
if (controller != null)
|
if (controller == null) return table;
|
||||||
{
|
|
||||||
foreach (var button in controller.Definition.BoolButtons)
|
foreach (var button in controller.Definition.BoolButtons)
|
||||||
{
|
{
|
||||||
table[button] = controller.IsPressed(button);
|
table[button] = controller.IsPressed(button);
|
||||||
|
@ -410,8 +403,6 @@ namespace BizHawk.Client.EmuHawk
|
||||||
{
|
{
|
||||||
table[button] = controller.AxisValue(button);
|
table[button] = controller.AxisValue(button);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,9 @@ namespace BizHawk.Client.EmuHawk
|
||||||
public override void ExecuteString(string command)
|
public override void ExecuteString(string command)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override NLuaTableHelper GetTableHelper() => null;
|
||||||
|
|
||||||
private static readonly LuaFunctionList EmptyLuaFunList = new LuaFunctionList();
|
private static readonly LuaFunctionList EmptyLuaFunList = new LuaFunctionList();
|
||||||
public override LuaFunctionList RegisteredFunctions => EmptyLuaFunList;
|
public override LuaFunctionList RegisteredFunctions => EmptyLuaFunList;
|
||||||
public override GuiLuaLibrary GuiLibrary => null;
|
public override GuiLuaLibrary GuiLibrary => null;
|
||||||
|
|
|
@ -41,6 +41,7 @@ namespace BizHawk.Client.EmuHawk
|
||||||
}
|
}
|
||||||
|
|
||||||
if (true /*NLua.Lua.WhichLua == "NLua"*/) _lua["keepalives"] = _lua.NewTable();
|
if (true /*NLua.Lua.WhichLua == "NLua"*/) _lua["keepalives"] = _lua.NewTable();
|
||||||
|
_th = new NLuaTableHelper(_lua);
|
||||||
_mainForm = mainForm;
|
_mainForm = mainForm;
|
||||||
LuaWait = new AutoResetEvent(false);
|
LuaWait = new AutoResetEvent(false);
|
||||||
Docs.Clear();
|
Docs.Clear();
|
||||||
|
@ -59,7 +60,7 @@ namespace BizHawk.Client.EmuHawk
|
||||||
|
|
||||||
if (addLibrary)
|
if (addLibrary)
|
||||||
{
|
{
|
||||||
var instance = (LuaLibraryBase) Activator.CreateInstance(lib, this, apiContainer, _lua, (Action<string>) LogToLuaConsole);
|
var instance = (LuaLibraryBase) Activator.CreateInstance(lib, this, apiContainer, (Action<string>) LogToLuaConsole);
|
||||||
ServiceInjector.UpdateServices(serviceProvider, instance);
|
ServiceInjector.UpdateServices(serviceProvider, instance);
|
||||||
|
|
||||||
// TODO: make EmuHawk libraries have a base class with common properties such as this
|
// TODO: make EmuHawk libraries have a base class with common properties such as this
|
||||||
|
@ -79,7 +80,7 @@ namespace BizHawk.Client.EmuHawk
|
||||||
{
|
{
|
||||||
var canvas = new LuaCanvas(width, height, x, y, LogToLuaConsole);
|
var canvas = new LuaCanvas(width, height, x, y, LogToLuaConsole);
|
||||||
canvas.Show();
|
canvas.Show();
|
||||||
return _lua.TableFromObject(canvas);
|
return _th.ObjectToTable(canvas);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
else if (instance is TAStudioLuaLibrary tastudioLib)
|
else if (instance is TAStudioLuaLibrary tastudioLib)
|
||||||
|
@ -105,6 +106,8 @@ namespace BizHawk.Client.EmuHawk
|
||||||
private Lua _lua = new Lua();
|
private Lua _lua = new Lua();
|
||||||
private Lua _currThread;
|
private Lua _currThread;
|
||||||
|
|
||||||
|
private readonly NLuaTableHelper _th;
|
||||||
|
|
||||||
private static Action<object[]> _logToLuaConsoleCallback = a => Console.WriteLine("a Lua lib is logging during init and the console lib hasn't been initialised yet");
|
private static Action<object[]> _logToLuaConsoleCallback = a => Console.WriteLine("a Lua lib is logging during init and the console lib hasn't been initialised yet");
|
||||||
|
|
||||||
private FormsLuaLibrary FormsLibrary => (FormsLuaLibrary)Libraries[typeof(FormsLuaLibrary)];
|
private FormsLuaLibrary FormsLibrary => (FormsLuaLibrary)Libraries[typeof(FormsLuaLibrary)];
|
||||||
|
@ -117,6 +120,8 @@ namespace BizHawk.Client.EmuHawk
|
||||||
|
|
||||||
private static void LogToLuaConsole(object outputs) => _logToLuaConsoleCallback(new[] { outputs });
|
private static void LogToLuaConsole(object outputs) => _logToLuaConsoleCallback(new[] { outputs });
|
||||||
|
|
||||||
|
public override NLuaTableHelper GetTableHelper() => _th;
|
||||||
|
|
||||||
public override void Restart(IEmulatorServiceProvider newServiceProvider)
|
public override void Restart(IEmulatorServiceProvider newServiceProvider)
|
||||||
{
|
{
|
||||||
foreach (var lib in Libraries)
|
foreach (var lib in Libraries)
|
||||||
|
|
Loading…
Reference in New Issue