vJine.Lua
vJine.Lua是Lua语言的C#封装库,可实现通过C#直接运行Lua脚本并与Lua脚本交互的功能。
1. 授权:
相关资源:
nuget:(https://www.nuget.org/packages?q=vjine)
API文档:(http://git.oschina.net/vjine/vJine.Lua/attach_files)
Lua官网:(http://www.lua.org)
Lua中国开发者:(http://www.luaer.cn)
babelua(基于Visual Studio扩展的Lua编辑器):(https://babelua.codeplex.com/)
QQ群:115051701
2. 说明:
1)vJine.Lua采用C++/CIL封装自Lua,当前Lua版本为(Lua 5.3.0)
2)暂不支持方法重载,注入时同名方法将被覆盖。
3. 功能简介:
//引用命名空间
using vJine.Lua;
//获取Lua版本
string V = LuaContext.Version;
//实例化LuaContext
LuaContext luaContext = new LuaContext();
string vars_name = "vJine.Lua.Vars.bool";
//设置与读取堆栈变量
bool v_bool = true;
luaContext.set(true);
luaContext.get(out v_bool);
Debug.Assert(v_bool == true);
luaContext.set(false);
luaContext.get(out v_bool);
Debug.Assert(v_bool == false);
//设置与读取全局变量
luaContext.set(vars_name, true);
luaContext.get(vars_name, out v_bool);
Debug.Assert(v_bool == true);
luaContext.set(vars_name, false);
luaContext.get(vars_name, out v_bool);
Debug.Assert(v_bool == false);
//注册:匿名方法
luaContext.reg("vJine.Lua.funcs.anony", new Action<MyStatus>((MyStatus E) => {
string msg = string.Format("result[anonymous]:{0}", E);
}));
//注册:静态方法
luaContext.reg("vJine.Lua.funcs.M", new Action<MyStatus>(MyClass.M));
//注册:实例方法
MyClass xq = new MyClass();
luaContext.reg("vJine.Lua.funcs.m", new Action<MyStatus>(x1.m));
//注册类:默认名称
luaContext.reg<MyClass>();
//注册类:自定义名称
luaContext.reg<MyClass>("vJine.Lua.YourClass");
//注入脚本:
object[] R = luaContext.inject(
"function callback(my_arg) return my_arg .. ' on_the_fly' end return true, 1.234, callback('vJine.Lua')");
//调用脚本
object[] Obj = luaContext.exec("callback", "vJine.Lua");
//加载脚本:
R = luaContext.load("d:/vJine.Lua/start.lua");
//打印堆栈变量:
luaContext.print_vars();
//打印全局变量:
luaContext.print_var("vJine.Lua.UT.MyClass");
//类定义:
public class MyClass {
public MyClass My { get; set; } public bool B { get; set; }
public bool? b { get; set; }
public byte[] B_ { get; set; } public double D { get; set; }
public double d { get; set; } public Guid G { get; set; }
public Guid? g { get; set; }
public Guid[] G_ { get; set; } public string S { get; set; }
public string s { get; set; }
public string[] s_ { get; set; } public DateTime DT { get; set; }
public DateTime? dt { get; set; }
public DateTime[] DT_ { get; set; } public MyStatus E { get; set; }
public MyStatus? e { get; set; }
public MyStatus[] E_ { get; set; } publicstaticvoidM(MyStatusE){
string msg = string.Format("result[anonymous]:{0}", E);
} publicvoidm(MyStatusE){
string msg = string.Format("result[anonymous]:{0}", E);
} publicstaticstringSM(inta,stringb){
return string.Format("SM:Result:a,{0};b,{1}", a, b);
} publicstringIM(inta,stringb){
return string.Format("IM:Result:a,{0};b,{1}", a, b);
} publicstringshow_properties(){
return string.Format("Properties:b,{0};d,{1};s,{2}", this.B, this.D, this.s);
}
}