Simplify some lua table creation with an extension method
This commit is contained in:
parent
d955c468db
commit
7ebfd42229
|
@ -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 );")]
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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....|\" );")]
|
||||
|
|
|
@ -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( );")]
|
||||
|
|
|
@ -39,10 +39,9 @@ namespace BizHawk.Client.Common
|
|||
var result = APIs.Sql.ReadCommand(query);
|
||||
if (result is Dictionary<string, object> dict)
|
||||
{
|
||||
var table = Lua.NewTable();
|
||||
foreach (var kvp in dict) table[kvp.Key] = kvp.Value;
|
||||
return table;
|
||||
return dict.ToLuaTable(Lua);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<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();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue