From 7ebfd42229fad0b526dca13039adc94d3eb8810c Mon Sep 17 00:00:00 2001 From: adelikat Date: Sun, 15 Dec 2019 09:22:10 -0600 Subject: [PATCH] Simplify some lua table creation with an extension method --- BizHawk.Client.Common/lua/EmuLuaLibrary.Emu.cs | 7 +++---- .../lua/EmuLuaLibrary.GameInfo.cs | 9 +++------ BizHawk.Client.Common/lua/EmuLuaLibrary.Joypad.cs | 14 ++++++-------- BizHawk.Client.Common/lua/EmuLuaLibrary.Movie.cs | 15 +++++++-------- BizHawk.Client.Common/lua/EmuLuaLibrary.SQL.cs | 5 ++--- BizHawk.Client.Common/lua/LuaHelper.cs | 15 ++++++++++++++- .../tools/Lua/Libraries/EmuLuaLibrary.Input.cs | 14 ++++++-------- 7 files changed, 41 insertions(+), 38 deletions(-) diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.Emu.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.Emu.cs index 724ae492ba..5e5e2519d7 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.Emu.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.Emu.cs @@ -49,10 +49,9 @@ namespace BizHawk.Client.Common [LuaMethod("getregisters", "returns the complete set of available flags and registers for a given core")] public LuaTable GetRegisters() { - var result = APIs.Emu.GetRegisters(); - var table = Lua.NewTable(); - foreach (var kvp in result) table[kvp.Key] = kvp.Value; - return table; + return APIs.Emu + .GetRegisters() + .ToLuaTable(Lua); } [LuaMethodExample("emu.setregister( emu.getregisters( )[ 0 ], -1000 );")] diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.GameInfo.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.GameInfo.cs index c89338dd0e..60dd10cb64 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.GameInfo.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.GameInfo.cs @@ -1,8 +1,6 @@ using System; using NLua; -using BizHawk.Emulation.Common; - // ReSharper disable UnusedMember.Global // ReSharper disable UnusedAutoPropertyAccessor.Local namespace BizHawk.Client.Common @@ -45,10 +43,9 @@ namespace BizHawk.Client.Common [LuaMethod("getoptions", "returns the game options for the currently loaded rom. Options vary per platform")] public LuaTable GetOptions() { - var dict = APIs.GameInfo.GetOptions(); - var table = Lua.NewTable(); - foreach (var kvp in dict) table[kvp.Key] = kvp.Value; - return table; + return APIs.GameInfo + .GetOptions() + .ToLuaTable(Lua); } } } diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.Joypad.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.Joypad.cs index cdb69c37f0..2cb24c412f 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.Joypad.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.Joypad.cs @@ -20,20 +20,18 @@ 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 result = APIs.Joypad.Get(controller); - var table = Lua.NewTable(); - foreach (var kvp in result) table[kvp.Key] = kvp.Value; - return table; + return APIs.Joypad + .Get(controller) + .ToLuaTable(Lua); } [LuaMethodExample("local nljoyget = joypad.getimmediate( );")] [LuaMethod("getimmediate", "returns a lua table of any controller buttons currently pressed by the user")] public LuaTable GetImmediate() { - var result = APIs.Joypad.GetImmediate(); - var table = Lua.NewTable(); - foreach (var kvp in result) table[kvp.Key] = kvp.Value; - return table; + return APIs.Joypad + .GetImmediate() + .ToLuaTable(Lua); } [LuaMethodExample("joypad.setfrommnemonicstr( \"| 0, 0, 0, 100,...R..B....|\" );")] diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.Movie.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.Movie.cs index 2362fa7985..04a0adfaf3 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.Movie.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.Movie.cs @@ -31,10 +31,9 @@ namespace BizHawk.Client.Common [LuaMethod("getinput", "Returns a table of buttons pressed on a given frame of the loaded movie")] public LuaTable GetInput(int frame) { - var result = APIs.Movie.GetInput(frame); - var table = Lua.NewTable(); - foreach (var kvp in result) table[kvp.Key] = kvp.Value; - return table; + return APIs.Movie + .GetInput(frame) + .ToLuaTable(Lua); } [LuaMethodExample("local stmovget = movie.getinputasmnemonic( 500 );")] @@ -93,10 +92,10 @@ namespace BizHawk.Client.Common [LuaMethod("getheader", "If a movie is active, will return the movie header as a lua table")] public LuaTable GetHeader() { - var result = APIs.Movie.GetHeader(); - var table = Lua.NewTable(); - foreach (var kvp in result) table[kvp.Key] = kvp.Value; - return table; + + return APIs.Movie + .GetHeader() + .ToLuaTable(Lua); } [LuaMethodExample("local nlmovget = movie.getcomments( );")] diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.SQL.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.SQL.cs index 3d3a870923..0e1b95f5ec 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.SQL.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.SQL.cs @@ -39,10 +39,9 @@ namespace BizHawk.Client.Common var result = APIs.Sql.ReadCommand(query); if (result is Dictionary dict) { - var table = Lua.NewTable(); - foreach (var kvp in dict) table[kvp.Key] = kvp.Value; - return table; + return dict.ToLuaTable(Lua); } + return result; } } diff --git a/BizHawk.Client.Common/lua/LuaHelper.cs b/BizHawk.Client.Common/lua/LuaHelper.cs index 1eb74cff21..a481ea8448 100644 --- a/BizHawk.Client.Common/lua/LuaHelper.cs +++ b/BizHawk.Client.Common/lua/LuaHelper.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System.Collections.Generic; +using System.Linq; using System.Reflection; using NLua; @@ -7,6 +8,18 @@ namespace BizHawk.Client.Common { public static class LuaExtensions { + 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(); diff --git a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Input.cs b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Input.cs index 6f7e805923..0b2780dbc7 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Input.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Input.cs @@ -20,20 +20,18 @@ namespace BizHawk.Client.EmuHawk [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() { - var result = APIs.Input.Get(); - var table = Lua.NewTable(); - foreach (var kvp in result) table[kvp.Key] = kvp.Value; - return table; + return APIs.Input + .Get() + .ToLuaTable(Lua); } [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() { - var result = APIs.Input.GetMouse(); - var table = Lua.NewTable(); - foreach (var kvp in result) table[kvp.Key] = kvp.Value; - return table; + return APIs.Input + .GetMouse() + .ToLuaTable(Lua); } } }