diff --git a/src/BizHawk.Client.Common/lua/CommonLibs/ClientLuaLibrary.cs b/src/BizHawk.Client.Common/lua/CommonLibs/ClientLuaLibrary.cs index bc26baed95..c86faf836b 100644 --- a/src/BizHawk.Client.Common/lua/CommonLibs/ClientLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/CommonLibs/ClientLuaLibrary.cs @@ -26,8 +26,8 @@ namespace BizHawk.Client.Common public IMainFormForApi MainForm { get; set; } - public ClientLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public ClientLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} 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")] public LuaTable TransformPoint(int x, int y) { var transformed = APIs.EmuClient.TransformPoint(new Point(x, y)); - var table = Lua.NewTable(); + var table = _th.CreateTable(); table["x"] = transformed.X; table["y"] = transformed.Y; return table; @@ -262,14 +262,14 @@ namespace BizHawk.Client.Common [LuaMethodExample("local nlcliget = client.getavailabletools( );")] [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\" );")] [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) { 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\" );")] @@ -277,7 +277,7 @@ namespace BizHawk.Client.Common public LuaTable CreateInstance(string 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 );")] diff --git a/src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs b/src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs index f3b57e18ce..7601789c20 100644 --- a/src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs @@ -4,8 +4,6 @@ using System.ComponentModel; using System.Net.WebSockets; using System.Text; -using NLua; - namespace BizHawk.Client.Common { [Description("A library for communicating with other programs")] @@ -13,8 +11,8 @@ namespace BizHawk.Client.Common { private readonly IDictionary _websockets = new Dictionary(); - public CommLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public CommLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} public override string Name => "comm"; diff --git a/src/BizHawk.Client.Common/lua/CommonLibs/EmulationLuaLibrary.cs b/src/BizHawk.Client.Common/lua/CommonLibs/EmulationLuaLibrary.cs index cfe6e20d8b..8069317f89 100644 --- a/src/BizHawk.Client.Common/lua/CommonLibs/EmulationLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/CommonLibs/EmulationLuaLibrary.cs @@ -13,8 +13,8 @@ namespace BizHawk.Client.Common public Action FrameAdvanceCallback { get; set; } public Action YieldCallback { get; set; } - public EmulationLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public EmulationLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} public override string Name => "emu"; @@ -44,12 +44,7 @@ namespace BizHawk.Client.Common [LuaMethodExample("local nlemuget = emu.getregisters( );")] [LuaMethod("getregisters", "returns the complete set of available flags and registers for a given core")] - public LuaTable GetRegisters() - { - return APIs.Emulation - .GetRegisters() - .ToLuaTable(Lua); - } + public LuaTable GetRegisters() => _th.DictToTable(APIs.Emulation.GetRegisters()); [LuaMethodExample("emu.setregister( emu.getregisters( )[ 0 ], -1000 );")] [LuaMethod("setregister", "sets the given register name to the given value")] diff --git a/src/BizHawk.Client.Common/lua/CommonLibs/GameInfoLuaLibrary.cs b/src/BizHawk.Client.Common/lua/CommonLibs/GameInfoLuaLibrary.cs index 62fb87775c..ab3988bb5c 100644 --- a/src/BizHawk.Client.Common/lua/CommonLibs/GameInfoLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/CommonLibs/GameInfoLuaLibrary.cs @@ -7,8 +7,8 @@ namespace BizHawk.Client.Common { public sealed class GameInfoLuaLibrary : LuaLibraryBase { - public GameInfoLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public GameInfoLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} public override string Name => "gameinfo"; @@ -38,11 +38,6 @@ namespace BizHawk.Client.Common [LuaMethodExample("local nlgamget = gameinfo.getoptions( );")] [LuaMethod("getoptions", "returns the game options for the currently loaded rom. Options vary per platform")] - public LuaTable GetOptions() - { - return APIs.GameInfo - .GetOptions() - .ToLuaTable(Lua); - } + public LuaTable GetOptions() => _th.DictToTable(APIs.GameInfo.GetOptions()); } } diff --git a/src/BizHawk.Client.Common/lua/CommonLibs/GuiLuaLibrary.cs b/src/BizHawk.Client.Common/lua/CommonLibs/GuiLuaLibrary.cs index 96ffae1e48..95e9ee5053 100644 --- a/src/BizHawk.Client.Common/lua/CommonLibs/GuiLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/CommonLibs/GuiLuaLibrary.cs @@ -1,5 +1,6 @@ using System; using System.Drawing; +using System.Linq; using NLua; @@ -9,8 +10,8 @@ namespace BizHawk.Client.Common { public Func CreateLuaCanvasCallback { get; set; } - public GuiLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public GuiLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} public override string Name => "gui"; @@ -62,7 +63,7 @@ namespace BizHawk.Client.Common { var pointsArr = new Point[4]; var i = 0; - foreach (LuaTable point in points.Values) + foreach (var point in _th.EnumerateValues(points)) { pointsArr[i] = new Point(LuaInt(point[1]), LuaInt(point[2])); 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")] public void DrawPolygon(LuaTable points, int? offsetX = null, int? offsetY = null, Color? line = null, Color? background = null) { + var pointsList = _th.EnumerateValues(points).ToList(); try { - var pointsArr = new Point[points.Values.Count]; + var pointsArr = new Point[pointsList.Count]; 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)); i++; diff --git a/src/BizHawk.Client.Common/lua/CommonLibs/InputLuaLibrary.cs b/src/BizHawk.Client.Common/lua/CommonLibs/InputLuaLibrary.cs index fd1dfae8c0..8409590fc9 100644 --- a/src/BizHawk.Client.Common/lua/CommonLibs/InputLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/CommonLibs/InputLuaLibrary.cs @@ -6,17 +6,17 @@ namespace BizHawk.Client.Common { public sealed class InputLuaLibrary : LuaLibraryBase { - public InputLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public InputLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} public override string Name => "input"; [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.")] - public LuaTable Get() => APIs.Input.Get().ToLuaTable(Lua); + public LuaTable Get() => _th.DictToTable(APIs.Input.Get()); [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.")] - public LuaTable GetMouse() => APIs.Input.GetMouse().ToLuaTable(Lua); + public LuaTable GetMouse() => _th.DictToTable(APIs.Input.GetMouse()); } } diff --git a/src/BizHawk.Client.Common/lua/CommonLibs/JoypadLuaLibrary.cs b/src/BizHawk.Client.Common/lua/CommonLibs/JoypadLuaLibrary.cs index aedd617cde..d1aeac408c 100644 --- a/src/BizHawk.Client.Common/lua/CommonLibs/JoypadLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/CommonLibs/JoypadLuaLibrary.cs @@ -8,8 +8,8 @@ namespace BizHawk.Client.Common { public sealed class JoypadLuaLibrary : LuaLibraryBase { - public JoypadLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public JoypadLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} 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")] 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["getluafunctionslist"] = null; table["output"] = null; @@ -26,7 +26,7 @@ namespace BizHawk.Client.Common [LuaMethodExample("local nljoyget = joypad.getimmediate( );")] [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....|\" );")] [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) { var dict = new Dictionary(); - 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(buttons)) + { + dict[k.ToString()] = Convert.ToBoolean(v); // Accepts 1/0 or true/false + } APIs.Joypad.Set(dict, controller); } @@ -46,7 +49,10 @@ namespace BizHawk.Client.Common public void SetAnalog(LuaTable controls, object controller = null) { var dict = new Dictionary(); - 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(controls)) + { + dict[k.ToString()] = double.TryParse(v.ToString(), out var d) ? (int) d : (int?) null; + } APIs.Joypad.SetAnalog(dict, controller); } } diff --git a/src/BizHawk.Client.Common/lua/CommonLibs/MemoryLuaLibrary.cs b/src/BizHawk.Client.Common/lua/CommonLibs/MemoryLuaLibrary.cs index e54fcca423..6b2f535928 100644 --- a/src/BizHawk.Client.Common/lua/CommonLibs/MemoryLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/CommonLibs/MemoryLuaLibrary.cs @@ -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.")] public sealed class MemoryLuaLibrary : LuaLibraryBase { - public MemoryLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public MemoryLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} public override string Name => "memory"; [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")] - public LuaTable GetMemoryDomainList() - { - return APIs.Memory - .GetMemoryDomainList() - .ToLuaTable(Lua); - } + public LuaTable GetMemoryDomainList() => _th.ListToTable(APIs.Memory.GetMemoryDomainList()); [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")] @@ -53,12 +48,7 @@ namespace BizHawk.Client.Common [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).")] - public LuaTable ReadByteRange(int addr, int length, string domain = null) - { - return APIs.Memory - .ReadByteRange(addr, length, domain) - .ToLuaTable(Lua); - } + public LuaTable ReadByteRange(int addr, int length, string domain = null) => _th.ListToTable(APIs.Memory.ReadByteRange(addr, length, domain)); /// TODO C# version requires a contiguous address range [LuaMethodExample("")] @@ -66,17 +56,20 @@ namespace BizHawk.Client.Common public void WriteByteRange(LuaTable memoryblock, string domain = null) { #if true - foreach (var addr in memoryblock.Keys) APIs.Memory.WriteByte(LuaInt(addr), (uint) memoryblock[addr], domain); + foreach (var (addr, v) in _th.EnumerateEntries(memoryblock)) + { + APIs.Memory.WriteByte(LuaInt(addr), (uint) v, domain); + } #else var d = string.IsNullOrEmpty(domain) ? Domain : DomainList[VerifyMemoryDomain(domain)]; if (d.CanPoke()) { - foreach (var address in memoryblock.Keys) + foreach (var (address, v) in _th.EnumerateEntries(memoryblock)) { var addr = LuaInt(address); if (addr < d.Size) { - d.PokeByte(addr, (byte)LuaInt(memoryblock[address])); + d.PokeByte(addr, (byte) LuaInt(v)); } else { diff --git a/src/BizHawk.Client.Common/lua/CommonLibs/MemorySavestateLuaLibrary.cs b/src/BizHawk.Client.Common/lua/CommonLibs/MemorySavestateLuaLibrary.cs index bc5565a65c..8608e609b6 100644 --- a/src/BizHawk.Client.Common/lua/CommonLibs/MemorySavestateLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/CommonLibs/MemorySavestateLuaLibrary.cs @@ -1,5 +1,4 @@ using System; -using NLua; // ReSharper disable UnusedMember.Global // ReSharper disable UnusedAutoPropertyAccessor.Local @@ -7,8 +6,8 @@ namespace BizHawk.Client.Common { public sealed class MemorySavestateLuaLibrary : LuaLibraryBase { - public MemorySavestateLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public MemorySavestateLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} public override string Name => "memorysavestate"; diff --git a/src/BizHawk.Client.Common/lua/CommonLibs/MovieLuaLibrary.cs b/src/BizHawk.Client.Common/lua/CommonLibs/MovieLuaLibrary.cs index 537f3d03b7..45652f3ad4 100644 --- a/src/BizHawk.Client.Common/lua/CommonLibs/MovieLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/CommonLibs/MovieLuaLibrary.cs @@ -7,8 +7,8 @@ namespace BizHawk.Client.Common { public sealed class MovieLuaLibrary : LuaLibraryBase { - public MovieLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public MovieLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} public override string Name => "movie"; @@ -26,7 +26,7 @@ namespace BizHawk.Client.Common [LuaMethodExample("local nlmovget = movie.getinput( 500 );")] [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 );")] [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( );")] [LuaMethod("getheader", "If a movie is active, will return the movie header as a lua table")] - public LuaTable GetHeader() - { - - return APIs.Movie - .GetHeader() - .ToLuaTable(Lua); - } + public LuaTable GetHeader() => _th.DictToTable(APIs.Movie.GetHeader()); [LuaMethodExample("local nlmovget = movie.getcomments( );")] [LuaMethod("getcomments", "If a movie is active, will return the movie comments as a lua table")] - public LuaTable GetComments() - { - return APIs.Movie - .GetComments() - .ToLuaTable(Lua); - } + public LuaTable GetComments() => _th.ListToTable(APIs.Movie.GetComments()); [LuaMethodExample("local nlmovget = movie.getsubtitles( );")] [LuaMethod("getsubtitles", "If a movie is active, will return the movie subtitles as a lua table")] - public LuaTable GetSubtitles() - { - return APIs.Movie - .GetSubtitles() - .ToLuaTable(Lua); - } + public LuaTable GetSubtitles() => _th.ListToTable(APIs.Movie.GetSubtitles()); } } diff --git a/src/BizHawk.Client.Common/lua/CommonLibs/SQLiteLuaLibrary.cs b/src/BizHawk.Client.Common/lua/CommonLibs/SQLiteLuaLibrary.cs index c337025e7e..5305651653 100644 --- a/src/BizHawk.Client.Common/lua/CommonLibs/SQLiteLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/CommonLibs/SQLiteLuaLibrary.cs @@ -2,16 +2,14 @@ using System.Collections.Generic; using System.ComponentModel; -using NLua; - // ReSharper disable UnusedMember.Global namespace BizHawk.Client.Common { [Description("A library for performing SQLite operations.")] public sealed class SQLiteLuaLibrary : LuaLibraryBase { - public SQLiteLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public SQLiteLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} public override string Name => "SQL"; @@ -34,7 +32,7 @@ namespace BizHawk.Client.Common public object ReadCommand(string query = "") { var result = APIs.SQLite.ReadCommand(query); - return result is Dictionary dict ? dict.ToLuaTable(Lua) : result; + return result is Dictionary dict ? _th.DictToTable(dict) : result; } } } diff --git a/src/BizHawk.Client.Common/lua/CommonLibs/SaveStateLuaLibrary.cs b/src/BizHawk.Client.Common/lua/CommonLibs/SaveStateLuaLibrary.cs index 3952ff001e..5e5e659c13 100644 --- a/src/BizHawk.Client.Common/lua/CommonLibs/SaveStateLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/CommonLibs/SaveStateLuaLibrary.cs @@ -1,13 +1,11 @@ using System; -using NLua; - namespace BizHawk.Client.Common { public sealed class SaveStateLuaLibrary : LuaLibraryBase { - public SaveStateLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public SaveStateLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} public override string Name => "savestate"; diff --git a/src/BizHawk.Client.Common/lua/CommonLibs/UserDataLuaLibrary.cs b/src/BizHawk.Client.Common/lua/CommonLibs/UserDataLuaLibrary.cs index de68eeae22..91a2926177 100644 --- a/src/BizHawk.Client.Common/lua/CommonLibs/UserDataLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/CommonLibs/UserDataLuaLibrary.cs @@ -1,6 +1,5 @@ using System; using System.ComponentModel; -using NLua; // ReSharper disable UnusedMember.Global 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")] public sealed class UserDataLuaLibrary : LuaLibraryBase { - public UserDataLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public UserDataLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} public override string Name => "userdata"; diff --git a/src/BizHawk.Client.Common/lua/LuaHelper.cs b/src/BizHawk.Client.Common/lua/LuaHelper.cs index f5c705f880..d6e554c395 100644 --- a/src/BizHawk.Client.Common/lua/LuaHelper.cs +++ b/src/BizHawk.Client.Common/lua/LuaHelper.cs @@ -19,41 +19,6 @@ namespace BizHawk.Client.Common /// ≥ 2^53 or ≤ -2^53 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(this IEnumerable list, Lua lua) => list.ToList().ToLuaTable(lua); - - public static LuaTable ToLuaTable(this IList 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(this IDictionary 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; - } + public static LuaTable EnumerateToLuaTable(this NLuaTableHelper tableHelper, IEnumerable list) => tableHelper.ListToTable(list.ToList()); } } diff --git a/src/BizHawk.Client.Common/lua/LuaHelperLibs/BitLuaLibrary.cs b/src/BizHawk.Client.Common/lua/LuaHelperLibs/BitLuaLibrary.cs index 07b4ea4c9d..31b6a78266 100644 --- a/src/BizHawk.Client.Common/lua/LuaHelperLibs/BitLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/LuaHelperLibs/BitLuaLibrary.cs @@ -1,16 +1,14 @@ using System; using System.ComponentModel; -using NLua; - // ReSharper disable UnusedMember.Global namespace BizHawk.Client.Common { [Description("A library for performing standard bitwise operations.")] public sealed class BitLuaLibrary : LuaLibraryBase { - public BitLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public BitLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} public override string Name => "bit"; diff --git a/src/BizHawk.Client.Common/lua/LuaHelperLibs/EventsLuaLibrary.cs b/src/BizHawk.Client.Common/lua/LuaHelperLibs/EventsLuaLibrary.cs index b23a05065f..b1efe07a58 100644 --- a/src/BizHawk.Client.Common/lua/LuaHelperLibs/EventsLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/LuaHelperLibs/EventsLuaLibrary.cs @@ -24,8 +24,8 @@ namespace BizHawk.Client.Common [OptionalService] private IMemoryDomains Domains { get; set; } - public EventsLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public EventsLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} public override string Name => "event"; @@ -360,8 +360,8 @@ namespace BizHawk.Client.Common public LuaTable AvailableScopes() { return DebuggableCore?.MemoryCallbacksAvailable() == true - ? DebuggableCore.MemoryCallbacks.AvailableScopes.ToLuaTable(Lua) - : Lua.NewTable(); + ? _th.ListToTable(DebuggableCore.MemoryCallbacks.AvailableScopes) + : _th.CreateTable(); } private string ProcessScope(string scope) diff --git a/src/BizHawk.Client.Common/lua/LuaHelperLibs/GenesisLuaLibrary.cs b/src/BizHawk.Client.Common/lua/LuaHelperLibs/GenesisLuaLibrary.cs index 049b0016d5..42bec69041 100644 --- a/src/BizHawk.Client.Common/lua/LuaHelperLibs/GenesisLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/LuaHelperLibs/GenesisLuaLibrary.cs @@ -1,7 +1,6 @@ using System; using System.ComponentModel; -using NLua; using BizHawk.Emulation.Cores.Consoles.Sega.gpgx; // 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)")] public sealed class GenesisLuaLibrary : LuaLibraryBase { - public GenesisLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public GenesisLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} public override string Name => "genesis"; diff --git a/src/BizHawk.Client.Common/lua/LuaHelperLibs/MainMemoryLuaLibrary.cs b/src/BizHawk.Client.Common/lua/LuaHelperLibs/MainMemoryLuaLibrary.cs index 491ff25b0f..fe3f018761 100644 --- a/src/BizHawk.Client.Common/lua/LuaHelperLibs/MainMemoryLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/LuaHelperLibs/MainMemoryLuaLibrary.cs @@ -17,8 +17,8 @@ namespace BizHawk.Client.Common [OptionalService] private IMemoryDomains MemoryDomainCore { get; set; } - public MainMemoryLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public MainMemoryLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} 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).")] public LuaTable ReadByteRange(int addr, int length) { - return APIs.Memory - .ReadByteRange(addr, length, Domain.Name) - .ToLuaTable(Lua); + return _th.ListToTable(APIs.Memory.ReadByteRange(addr, length, Domain.Name)); } /// TODO C# version requires a contiguous address range @@ -74,17 +72,20 @@ namespace BizHawk.Client.Common public void WriteByteRange(LuaTable memoryblock) { #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(memoryblock)) + { + APIs.Memory.WriteByte(LuaInt(addr), (uint) v, Domain.Name); + } #else var d = Domain; if (d.CanPoke()) { - foreach (var address in memoryblock.Keys) + foreach (var (address, v) in _th.EnumerateEntries(memoryblock)) { var addr = LuaInt(address); if (addr < d.Size) { - d.PokeByte(addr, (byte)LuaInt(memoryblock[address])); + d.PokeByte(addr, (byte) LuaInt(v)); } else { diff --git a/src/BizHawk.Client.Common/lua/LuaHelperLibs/NESLuaLibrary.cs b/src/BizHawk.Client.Common/lua/LuaHelperLibs/NESLuaLibrary.cs index 9fe5eb2520..3b14272155 100644 --- a/src/BizHawk.Client.Common/lua/LuaHelperLibs/NESLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/LuaHelperLibs/NESLuaLibrary.cs @@ -1,8 +1,6 @@ using System; using System.ComponentModel; -using NLua; - using BizHawk.Emulation.Common; using BizHawk.Emulation.Cores.Nintendo.NES; using BizHawk.Emulation.Cores.Consoles.Nintendo.QuickNES; @@ -21,8 +19,8 @@ namespace BizHawk.Client.Common [OptionalService] private IMemoryDomains MemoryDomains { get; set; } - public NESLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public NESLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} public override string Name => "nes"; diff --git a/src/BizHawk.Client.Common/lua/LuaHelperLibs/SNESLuaLibrary.cs b/src/BizHawk.Client.Common/lua/LuaHelperLibs/SNESLuaLibrary.cs index 15319f495c..8cc9bbe51c 100644 --- a/src/BizHawk.Client.Common/lua/LuaHelperLibs/SNESLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/LuaHelperLibs/SNESLuaLibrary.cs @@ -1,6 +1,6 @@ using System; using System.ComponentModel; -using NLua; + using BizHawk.Emulation.Cores.Nintendo.SNES; // 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)")] public sealed class SNESLuaLibrary : LuaLibraryBase { - public SNESLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public SNESLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} public override string Name => "snes"; diff --git a/src/BizHawk.Client.Common/lua/LuaHelperLibs/StringLuaLibrary.cs b/src/BizHawk.Client.Common/lua/LuaHelperLibs/StringLuaLibrary.cs index 2f66af820f..6c6777d3c3 100644 --- a/src/BizHawk.Client.Common/lua/LuaHelperLibs/StringLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/LuaHelperLibs/StringLuaLibrary.cs @@ -11,8 +11,8 @@ namespace BizHawk.Client.Common { public override string Name => "bizstring"; - public StringLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public StringLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} [LuaMethodExample("local stbizhex = bizstring.hex( -12345 );")] [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\", \", \" );")] [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) - ? Lua.NewTable() - : str.Split(new[] { separator?.Length == 1 ? separator[0] : ',' }, StringSplitOptions.RemoveEmptyEntries) - .ToLuaTable(Lua, 1); + ? _th.CreateTable() + : _th.ListToTable(str.Split(new[] { separator?.Length == 1 ? separator[0] : ',' }, StringSplitOptions.RemoveEmptyEntries), indexFrom: 1); } } diff --git a/src/BizHawk.Client.Common/lua/LuaLibraries.cs b/src/BizHawk.Client.Common/lua/LuaLibraries.cs index a4fbdc85a0..8bc1ec5a31 100644 --- a/src/BizHawk.Client.Common/lua/LuaLibraries.cs +++ b/src/BizHawk.Client.Common/lua/LuaLibraries.cs @@ -40,6 +40,9 @@ namespace BizHawk.Client.Common public abstract void Close(); public abstract void EndLuaDrawing(); public abstract void ExecuteString(string command); + + public abstract NLuaTableHelper GetTableHelper(); + public abstract void Restart(IEmulatorServiceProvider newServiceProvider); public abstract (bool WaitForFrame, bool Terminated) ResumeScript(LuaFile lf); public abstract void SpawnAndSetFileThread(string pathToLoad, LuaFile lf); diff --git a/src/BizHawk.Client.Common/lua/LuaLibraryBase.cs b/src/BizHawk.Client.Common/lua/LuaLibraryBase.cs index eab11b17ec..58c196cfd2 100644 --- a/src/BizHawk.Client.Common/lua/LuaLibraryBase.cs +++ b/src/BizHawk.Client.Common/lua/LuaLibraryBase.cs @@ -2,17 +2,15 @@ using System.Drawing; using System.Threading; -using NLua; - namespace BizHawk.Client.Common { public abstract class LuaLibraryBase { - protected LuaLibraryBase(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) + protected LuaLibraryBase(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) { LogOutputCallback = logOutputCallback; - Lua = lua; _luaLibsImpl = luaLibsImpl; + _th = _luaLibsImpl.GetTableHelper(); APIs = apiContainer; } @@ -27,10 +25,10 @@ namespace BizHawk.Client.Common protected readonly Action LogOutputCallback; - public Lua Lua { get; } - protected readonly LuaLibraries _luaLibsImpl; + protected readonly NLuaTableHelper _th; + public static void ClearCurrentThread() { lock (ThreadMutex) diff --git a/src/BizHawk.Client.Common/lua/NLuaTableHelper.cs b/src/BizHawk.Client.Common/lua/NLuaTableHelper.cs new file mode 100644 index 0000000000..293751875e --- /dev/null +++ b/src/BizHawk.Client.Common/lua/NLuaTableHelper.cs @@ -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(IDictionary dictionary) + { + var table = _lua.NewTable(); + + foreach (var kvp in dictionary) + { + table[kvp.Key] = kvp.Value; + } + + return table; + } + + public IEnumerable<(TKey Key, TValue Value)> EnumerateEntries(LuaTable table) + => table.Keys.Cast().Select(k => (k, (TValue) table[k])); + + public IEnumerable EnumerateValues(LuaTable table) => table.Values.Cast(); + + public LuaTable ListToTable(IList 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; + } + } +} diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/ConsoleLuaLibrary.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/ConsoleLuaLibrary.cs index eee4875db4..3d59bba8ac 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/ConsoleLuaLibrary.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/ConsoleLuaLibrary.cs @@ -11,8 +11,8 @@ namespace BizHawk.Client.EmuHawk { public ToolManager Tools { get; set; } - public ConsoleLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public ConsoleLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} public override string Name => "console"; diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/FormsLuaLibrary.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/FormsLuaLibrary.cs index 1cdfdd81b1..a677db680a 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/FormsLuaLibrary.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/FormsLuaLibrary.cs @@ -14,8 +14,8 @@ namespace BizHawk.Client.EmuHawk [Description("A library for creating and managing custom dialogs")] public sealed class FormsLuaLibrary : LuaLibraryBase { - public FormsLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public FormsLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} public override string Name => "forms"; @@ -192,7 +192,7 @@ namespace BizHawk.Client.EmuHawk return 0; } - var dropdownItems = items.Values.Cast().ToList(); + var dropdownItems = _th.EnumerateValues(items).ToList(); dropdownItems.Sort(); var dropdown = new LuaDropDown(dropdownItems); @@ -1246,7 +1246,7 @@ namespace BizHawk.Client.EmuHawk { if (control is LuaDropDown) { - var dropdownItems = items.Values.Cast().ToList(); + var dropdownItems = _th.EnumerateValues(items).ToList(); dropdownItems.Sort(); (control as LuaDropDown).SetItems(dropdownItems); } diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/TAStudioLuaLibrary.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/TAStudioLuaLibrary.cs index 981ba3398c..77aaaf0ab5 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/TAStudioLuaLibrary.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/TAStudioLuaLibrary.cs @@ -18,8 +18,8 @@ namespace BizHawk.Client.EmuHawk { public ToolManager Tools { get; set; } - public TAStudioLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Lua lua, Action logOutputCallback) - : base(luaLibsImpl, apiContainer, lua, logOutputCallback) {} + public TAStudioLuaLibrary(LuaLibraries luaLibsImpl, ApiContainer apiContainer, Action logOutputCallback) + : base(luaLibsImpl, apiContainer, logOutputCallback) {} public override string Name => "tastudio"; @@ -165,7 +165,7 @@ namespace BizHawk.Client.EmuHawk [LuaMethodExample("local nltasget = tastudio.getselection( );")] [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("")] [LuaMethod("submitinputchange", "")] @@ -375,42 +375,33 @@ 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")] 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(), - b.Frame, - Text = b.UserText - }) - .EnumerateToLuaTable(Lua); - } - - return Lua.NewTable(); + Id = b.Uuid.ToString(), + b.Frame, + Text = b.UserText + })); } [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")] 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); + if (controller == null) return table; + + foreach (var button in controller.Definition.BoolButtons) { - var controller = Tastudio.GetBranchInput(branchId, frame); - if (controller != null) - { - foreach (var button in controller.Definition.BoolButtons) - { - table[button] = controller.IsPressed(button); - } + table[button] = controller.IsPressed(button); + } - foreach (var button in controller.Definition.Axes.Keys) - { - table[button] = controller.AxisValue(button); - } - } + foreach (var button in controller.Definition.Axes.Keys) + { + table[button] = controller.AxisValue(button); } return table; diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/UnixLuaLibraries.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/UnixLuaLibraries.cs index b650d50256..63dd701b7d 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/UnixLuaLibraries.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/UnixLuaLibraries.cs @@ -34,6 +34,9 @@ namespace BizHawk.Client.EmuHawk public override void ExecuteString(string command) { } + + public override NLuaTableHelper GetTableHelper() => null; + private static readonly LuaFunctionList EmptyLuaFunList = new LuaFunctionList(); public override LuaFunctionList RegisteredFunctions => EmptyLuaFunList; public override GuiLuaLibrary GuiLibrary => null; diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs index c99c92d807..4e32c2a952 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs @@ -41,6 +41,7 @@ namespace BizHawk.Client.EmuHawk } if (true /*NLua.Lua.WhichLua == "NLua"*/) _lua["keepalives"] = _lua.NewTable(); + _th = new NLuaTableHelper(_lua); _mainForm = mainForm; LuaWait = new AutoResetEvent(false); Docs.Clear(); @@ -59,7 +60,7 @@ namespace BizHawk.Client.EmuHawk if (addLibrary) { - var instance = (LuaLibraryBase) Activator.CreateInstance(lib, this, apiContainer, _lua, (Action) LogToLuaConsole); + var instance = (LuaLibraryBase) Activator.CreateInstance(lib, this, apiContainer, (Action) LogToLuaConsole); ServiceInjector.UpdateServices(serviceProvider, instance); // 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); canvas.Show(); - return _lua.TableFromObject(canvas); + return _th.ObjectToTable(canvas); }; } else if (instance is TAStudioLuaLibrary tastudioLib) @@ -105,6 +106,8 @@ namespace BizHawk.Client.EmuHawk private Lua _lua = new Lua(); private Lua _currThread; + private readonly NLuaTableHelper _th; + private static Action _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)]; @@ -117,6 +120,8 @@ namespace BizHawk.Client.EmuHawk private static void LogToLuaConsole(object outputs) => _logToLuaConsoleCallback(new[] { outputs }); + public override NLuaTableHelper GetTableHelper() => _th; + public override void Restart(IEmulatorServiceProvider newServiceProvider) { foreach (var lib in Libraries)