2013-10-31 00:31:25 +00:00
|
|
|
|
using System;
|
2014-01-25 19:49:29 +00:00
|
|
|
|
using System.Linq;
|
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; }
|
2014-05-20 20:25:18 +00:00
|
|
|
|
public Action<string> LogOutputCallback { get; set; }
|
2014-05-20 20:34:51 +00:00
|
|
|
|
public Lua Lua { get; set; }
|
2014-05-20 20:25:18 +00:00
|
|
|
|
|
|
|
|
|
protected void Log(object message)
|
|
|
|
|
{
|
|
|
|
|
if (LogOutputCallback != null)
|
|
|
|
|
{
|
|
|
|
|
LogOutputCallback(message.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-31 00:31:25 +00:00
|
|
|
|
|
|
|
|
|
public virtual void LuaRegister(Lua lua, ILuaDocumentation docs = null)
|
2014-01-25 15:05:53 +00:00
|
|
|
|
{
|
|
|
|
|
lua.NewTable(Name);
|
|
|
|
|
|
2014-01-25 19:49:29 +00:00
|
|
|
|
var luaAttr = typeof(LuaMethodAttributes);
|
|
|
|
|
|
|
|
|
|
var methods = GetType()
|
|
|
|
|
.GetMethods()
|
|
|
|
|
.Where(m => m.GetCustomAttributes(luaAttr, false).Any());
|
|
|
|
|
|
|
|
|
|
foreach (var method in methods)
|
2014-01-25 15:05:53 +00:00
|
|
|
|
{
|
2014-01-25 19:49:29 +00:00
|
|
|
|
var luaMethodAttr = method.GetCustomAttributes(luaAttr, false).First() as LuaMethodAttributes;
|
|
|
|
|
var luaName = Name + "." + luaMethodAttr.Name;
|
|
|
|
|
lua.RegisterFunction(luaName, this, method);
|
2014-01-25 15:05:53 +00:00
|
|
|
|
|
|
|
|
|
if (docs != null)
|
|
|
|
|
{
|
2014-01-26 00:01:40 +00:00
|
|
|
|
docs.Add(Name, luaMethodAttr.Name, method, luaMethodAttr.Description);
|
2014-01-25 15:05:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|