2013-10-31 00:31:25 +00:00
|
|
|
|
using System;
|
2014-01-25 15:05:53 +00:00
|
|
|
|
using System.Reflection;
|
2013-10-31 00:31:25 +00:00
|
|
|
|
|
2014-01-25 15:05:53 +00:00
|
|
|
|
using BizHawk.Common;
|
2013-10-31 00:31:25 +00:00
|
|
|
|
using LuaInterface;
|
|
|
|
|
|
2013-10-31 23:55:17 +00:00
|
|
|
|
namespace BizHawk.Client.Common
|
2013-10-31 00:31:25 +00:00
|
|
|
|
{
|
|
|
|
|
public abstract class LuaLibraryBase
|
|
|
|
|
{
|
|
|
|
|
public abstract string Name { get; }
|
|
|
|
|
public abstract string[] Functions { get; }
|
|
|
|
|
|
|
|
|
|
public virtual void LuaRegister(Lua lua, ILuaDocumentation docs = null)
|
|
|
|
|
{
|
|
|
|
|
lua.NewTable(Name);
|
2013-12-30 01:58:44 +00:00
|
|
|
|
foreach (var methodName in Functions)
|
2013-10-31 00:31:25 +00:00
|
|
|
|
{
|
2013-12-30 01:58:44 +00:00
|
|
|
|
var func = Name + "." + methodName;
|
2013-10-31 00:31:25 +00:00
|
|
|
|
var method = GetType().GetMethod(Name + "_" + methodName);
|
2013-10-31 18:43:01 +00:00
|
|
|
|
lua.RegisterFunction(func, this, method);
|
2013-10-31 00:31:25 +00:00
|
|
|
|
|
|
|
|
|
if (docs != null)
|
|
|
|
|
{
|
|
|
|
|
docs.Add(Name, methodName, method);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-25 15:05:53 +00:00
|
|
|
|
// TODO: eventually only use this, and rename it
|
|
|
|
|
public virtual void LuaRegisterNew(Lua lua, ILuaDocumentation docs = null)
|
|
|
|
|
{
|
|
|
|
|
lua.NewTable(Name);
|
|
|
|
|
|
|
|
|
|
foreach (var nameLookup in Functions)
|
|
|
|
|
{
|
|
|
|
|
var luaMethodName = Name + "." + nameLookup.ToLower();
|
|
|
|
|
var actualMethodName = GetType().GetMethod(nameLookup);
|
|
|
|
|
lua.RegisterFunction(luaMethodName, this, actualMethodName);
|
|
|
|
|
|
|
|
|
|
if (docs != null)
|
|
|
|
|
{
|
|
|
|
|
docs.Add(Name, nameLookup, actualMethodName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-30 01:58:44 +00:00
|
|
|
|
protected static int LuaInt(object luaArg)
|
2013-10-31 00:31:25 +00:00
|
|
|
|
{
|
2013-12-30 16:36:15 +00:00
|
|
|
|
return (int)(double)luaArg;
|
2013-10-31 00:31:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-30 01:58:44 +00:00
|
|
|
|
protected static uint LuaUInt(object luaArg)
|
2013-10-31 00:31:25 +00:00
|
|
|
|
{
|
2013-12-30 16:36:15 +00:00
|
|
|
|
return (uint)(double)luaArg;
|
2013-10-31 00:31:25 +00:00
|
|
|
|
}
|
2013-10-31 18:09:40 +00:00
|
|
|
|
|
2014-01-19 16:58:21 +00:00
|
|
|
|
protected static long LuaLong(object luaArg)
|
|
|
|
|
{
|
|
|
|
|
return (long)(double)luaArg;
|
2014-01-21 17:28:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static ulong LuaULong(object luaArg)
|
|
|
|
|
{
|
|
|
|
|
return (ulong)(double)luaArg;
|
|
|
|
|
}
|
2014-01-19 16:58:21 +00:00
|
|
|
|
|
2013-10-31 18:09:40 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// LuaInterface requires the exact match of parameter count, except optional parameters.
|
|
|
|
|
/// So, if you want to support variable arguments, declare them as optional and pass
|
|
|
|
|
/// them to this method.
|
|
|
|
|
/// </summary>
|
2013-12-30 01:58:44 +00:00
|
|
|
|
protected static object[] LuaVarArgs(params object[] luaArgs)
|
2013-10-31 18:09:40 +00:00
|
|
|
|
{
|
2013-12-30 01:58:44 +00:00
|
|
|
|
int n = luaArgs.Length;
|
2013-10-31 18:09:40 +00:00
|
|
|
|
int trim = 0;
|
|
|
|
|
for (int i = n - 1; i >= 0; --i)
|
2013-12-30 01:58:44 +00:00
|
|
|
|
{
|
|
|
|
|
if (luaArgs[i] == null)
|
|
|
|
|
{
|
|
|
|
|
++trim;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var lua_result = new object[n - trim];
|
|
|
|
|
Array.Copy(luaArgs, lua_result, n - trim);
|
2013-10-31 18:09:40 +00:00
|
|
|
|
return lua_result;
|
|
|
|
|
}
|
2013-10-31 00:31:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|