Simplify some lua table creation with an extension method

This commit is contained in:
adelikat 2019-12-15 09:22:10 -06:00
parent d955c468db
commit 7ebfd42229
7 changed files with 41 additions and 38 deletions

View File

@ -49,10 +49,9 @@ namespace BizHawk.Client.Common
[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()
{ {
var result = APIs.Emu.GetRegisters(); return APIs.Emu
var table = Lua.NewTable(); .GetRegisters()
foreach (var kvp in result) table[kvp.Key] = kvp.Value; .ToLuaTable(Lua);
return table;
} }
[LuaMethodExample("emu.setregister( emu.getregisters( )[ 0 ], -1000 );")] [LuaMethodExample("emu.setregister( emu.getregisters( )[ 0 ], -1000 );")]

View File

@ -1,8 +1,6 @@
using System; using System;
using NLua; using NLua;
using BizHawk.Emulation.Common;
// ReSharper disable UnusedMember.Global // ReSharper disable UnusedMember.Global
// ReSharper disable UnusedAutoPropertyAccessor.Local // ReSharper disable UnusedAutoPropertyAccessor.Local
namespace BizHawk.Client.Common 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")] [LuaMethod("getoptions", "returns the game options for the currently loaded rom. Options vary per platform")]
public LuaTable GetOptions() public LuaTable GetOptions()
{ {
var dict = APIs.GameInfo.GetOptions(); return APIs.GameInfo
var table = Lua.NewTable(); .GetOptions()
foreach (var kvp in dict) table[kvp.Key] = kvp.Value; .ToLuaTable(Lua);
return table;
} }
} }
} }

View File

@ -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")] [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 result = APIs.Joypad.Get(controller); return APIs.Joypad
var table = Lua.NewTable(); .Get(controller)
foreach (var kvp in result) table[kvp.Key] = kvp.Value; .ToLuaTable(Lua);
return table;
} }
[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() public LuaTable GetImmediate()
{ {
var result = APIs.Joypad.GetImmediate(); return APIs.Joypad
var table = Lua.NewTable(); .GetImmediate()
foreach (var kvp in result) table[kvp.Key] = kvp.Value; .ToLuaTable(Lua);
return table;
} }
[LuaMethodExample("joypad.setfrommnemonicstr( \"| 0, 0, 0, 100,...R..B....|\" );")] [LuaMethodExample("joypad.setfrommnemonicstr( \"| 0, 0, 0, 100,...R..B....|\" );")]

View File

@ -31,10 +31,9 @@ namespace BizHawk.Client.Common
[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) public LuaTable GetInput(int frame)
{ {
var result = APIs.Movie.GetInput(frame); return APIs.Movie
var table = Lua.NewTable(); .GetInput(frame)
foreach (var kvp in result) table[kvp.Key] = kvp.Value; .ToLuaTable(Lua);
return table;
} }
[LuaMethodExample("local stmovget = movie.getinputasmnemonic( 500 );")] [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")] [LuaMethod("getheader", "If a movie is active, will return the movie header as a lua table")]
public LuaTable GetHeader() public LuaTable GetHeader()
{ {
var result = APIs.Movie.GetHeader();
var table = Lua.NewTable(); return APIs.Movie
foreach (var kvp in result) table[kvp.Key] = kvp.Value; .GetHeader()
return table; .ToLuaTable(Lua);
} }
[LuaMethodExample("local nlmovget = movie.getcomments( );")] [LuaMethodExample("local nlmovget = movie.getcomments( );")]

View File

@ -39,10 +39,9 @@ namespace BizHawk.Client.Common
var result = APIs.Sql.ReadCommand(query); var result = APIs.Sql.ReadCommand(query);
if (result is Dictionary<string, object> dict) if (result is Dictionary<string, object> dict)
{ {
var table = Lua.NewTable(); return dict.ToLuaTable(Lua);
foreach (var kvp in dict) table[kvp.Key] = kvp.Value;
return table;
} }
return result; return result;
} }
} }

View File

@ -1,4 +1,5 @@
using System.Linq; using System.Collections.Generic;
using System.Linq;
using System.Reflection; using System.Reflection;
using NLua; using NLua;
@ -7,6 +8,18 @@ namespace BizHawk.Client.Common
{ {
public static class LuaExtensions public static class LuaExtensions
{ {
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) public static LuaTable TableFromObject(this Lua lua, object obj)
{ {
var table = lua.NewTable(); var table = lua.NewTable();

View File

@ -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.")] [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() public LuaTable Get()
{ {
var result = APIs.Input.Get(); return APIs.Input
var table = Lua.NewTable(); .Get()
foreach (var kvp in result) table[kvp.Key] = kvp.Value; .ToLuaTable(Lua);
return table;
} }
[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() public LuaTable GetMouse()
{ {
var result = APIs.Input.GetMouse(); return APIs.Input
var table = Lua.NewTable(); .GetMouse()
foreach (var kvp in result) table[kvp.Key] = kvp.Value; .ToLuaTable(Lua);
return table;
} }
} }
} }