another extension method for lua table creation

This commit is contained in:
adelikat 2019-12-15 09:46:39 -06:00
parent 7ebfd42229
commit d2f21f84e3
6 changed files with 36 additions and 42 deletions

View File

@ -73,11 +73,9 @@ 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)
{
var result = APIs.Mem.ReadByteRange(addr, length, Domain.Name);
var table = Lua.NewTable();
var count = result.Count;
for (var i = 0; i != count; i++) table[i] = result[i];
return table;
return APIs.Mem
.ReadByteRange(addr, length, Domain.Name)
.ToLuaTable(Lua);
}
/// <remarks>TODO C# version requires a contiguous address range</remarks>

View File

@ -27,11 +27,9 @@ namespace BizHawk.Client.Common
[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()
{
var result = APIs.Mem.GetMemoryDomainList();
var table = Lua.NewTable();
var count = result.Count;
for (var i = 0; i != count; i++) table[i] = result[i];
return table;
return APIs.Mem
.GetMemoryDomainList()
.ToLuaTable(Lua);
}
[LuaMethodExample("local uimemget = memory.getmemorydomainsize( mainmemory.getname( ) );")]
@ -70,11 +68,9 @@ 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, string domain = null)
{
var result = APIs.Mem.ReadByteRange(addr, length, domain);
var table = Lua.NewTable();
var count = result.Count;
for (var i = 0; i != count; i++) table[i] = result[i];
return table;
return APIs.Mem
.ReadByteRange(addr, length, domain)
.ToLuaTable(Lua);
}
/// <remarks>TODO C# version requires a contiguous address range</remarks>

View File

@ -102,22 +102,18 @@ namespace BizHawk.Client.Common
[LuaMethod("getcomments", "If a movie is active, will return the movie comments as a lua table")]
public LuaTable GetComments()
{
var result = APIs.Movie.GetComments();
var table = Lua.NewTable();
var count = result.Count;
for (var i = 0; i != count; i++) table[i] = result[i];
return table;
return APIs.Movie
.GetComments()
.ToLuaTable(Lua);
}
[LuaMethodExample("local nlmovget = movie.getsubtitles( );")]
[LuaMethod("getsubtitles", "If a movie is active, will return the movie subtitles as a lua table")]
public LuaTable GetSubtitles()
{
var result = APIs.Movie.GetSubtitles();
var table = Lua.NewTable();
var count = result.Count;
for (var i = 0; i != count; i++) table[i] = result[i];
return table;
return APIs.Movie
.GetSubtitles()
.ToLuaTable(Lua);
}
}
}

View File

@ -8,6 +8,17 @@ namespace BizHawk.Client.Common
{
public static class LuaExtensions
{
public static LuaTable ToLuaTable<T>(this IList<T> list, Lua lua)
{
var table = lua.NewTable();
for (int i = 0; i < list.Count; i++)
{
table[i] = list[i];
}
return table;
}
public static LuaTable ToLuaTable<T>(this IDictionary<string, T> dictionary, Lua lua)
{
var table = lua.NewTable();

View File

@ -419,14 +419,13 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod("getavailabletools", "Returns a list of the tools currently open")]
public LuaTable GetAvailableTools()
{
var t = Lua.NewTable();
var tools = GlobalWin.Tools.AvailableTools.ToList();
for (int i = 0; i < tools.Count; i++)
{
t[i] = tools[i].Name.ToLower();
}
var xx = GlobalWin.Tools.AvailableTools.ToList();
var x = GlobalWin.Tools.AvailableTools
.Select(t => t.Name.ToLower())
.ToList()
.ToLuaTable(Lua);
return t;
return x;
}
[LuaMethodExample("local nlcliget = client.gettool( \"Tool name\" );")]

View File

@ -368,26 +368,20 @@ 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()
{
var table = Lua.NewTable();
if (Engaged())
{
var branches = Tastudio.CurrentTasMovie.Branches
return Tastudio.CurrentTasMovie.Branches
.Select(b => new
{
Id = b.UniqueIdentifier.ToString(),
b.Frame,
Text = b.UserText
})
.ToList();
for (int i = 0; i < branches.Count; i++)
{
table[i] = branches[i];
}
.ToList()
.ToLuaTable(Lua);
}
return table;
return Lua.NewTable();
}
[LuaMethodExample("local nltasget = tastudio.getbranchinput( \"97021544-2454-4483-824f-47f75e7fcb6a\", 500 );")]