.net core中加载lua脚本的类库: MoonSharp
lua脚本简介
Lua 是一个小巧的脚本语言。是巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de Janeiro)里的一个研究小组,由Roberto Ierusalimschy、Waldemar Celes 和 Luiz Henrique de Figueiredo所组成并于1993年开发。 其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能。Lua由标准C编写而成,几乎在所有操作系统和平台上都可以编译,运行。Lua并没有提供强大的库,这是由它的定位决定的。所以Lua不适合作为开发独立应用程序的语言。Lua 有一个同时进行的JIT项目,提供在特定平台上的即时编译功能。
Lua脚本可以很容易的被C/C++ 代码调用,也可以反过来调用C/C++的函数,这使得Lua在应用程序中可以被广泛应用。不仅仅作为扩展脚本,也可以作为普通的配置文件,代替XML,ini等文件格式,并且更容易理解和维护。 Lua由标准C编写而成,代码简洁优美,几乎在所有操作系统和平台上都可以编译,运行。 一个完整的Lua解释器不过200k,在目前所有脚本引擎中,Lua的速度是最快的。这一切都决定了Lua是作为嵌入式脚本的最佳选择。
以上来源:百度百科
前言
MoonSharp是一个支持C#调用lua脚本的类库,支持.net, .net core, mono, unity,因此在.net core中也能够使用,而且加载和调用lua也很方便简单;
源码:https://github.com/xanathar/moonsharp
nuget:PM> Install-Package MoonSharp
使用
加载脚本
1 string scriptCode = @" 2 sum = 0 3 for i = 1, 100 do 4 sum = sum + i 5 end 6 return sum"; 7 DynValue res = Script.RunString(scriptCode); //运行脚本 8 Console.WriteLine(res.Number); //输出:5050
加载脚本文件:
Console.WriteLine(Script.RunFile("luatest.txt").Number); //指定文件名加载脚本并运行
文件内容:
sum = 0
for i = 1, 100 do
sum = sum + i
end
return sum
传递数据(lua全局变量)
1 string scriptCode = @" 2 function fact (n) 3 if (n == 0) then 4 return 1 5 else 6 return n*fact(n - 1) 7 end 8 end 9 return fact(num)"; 10 11 Script script = new Script(); 12 script.Globals["num"] = 5; //对脚本中的全局变量 num 赋值 13 14 Console.WriteLine(script.DoString(scriptCode).Number); //输出:120
lua函数的定义与调用
1 Script script = new Script(); 2 //先加载定义的函数 3 script.DoString(@" 4 function fact(n) 5 if (n == 0) then 6 return 1 7 else 8 return n * fact(n - 1) 9 end 10 end 11 "); 12 13 //如果该函数会重复利用的,那么就应该这么调用,而不是每次都调用DoString加载脚本调用(每次都加载脚本是费性能的) 14 Console.WriteLine(script.Call(script.Globals["fact"], 5).Number); //输出:120 15 Console.WriteLine(script.Call(script.Globals["fact"], DynValue.NewNumber(5)).Number); //和上面的一样
传递集合参数
1 Script script = new Script();
2 script.DoString(@"
3 function sum(list)
4 local total = 0;
5 for i,v in ipairs(list) do
6 total = total + v;
7 end
8 return total;
9 end
10 ");
11
12 Console.WriteLine(script.Call(script.Globals["sum"], new List<int>() { 1, 3, 5, 7, 9 })); //输出:25
多值返回:Tuple
1 Script script = new Script();
2 script.DoString(@"
3 function sum(kv)
4 local total = 0;
5 local ks = '';
6 for k,v in pairs(kv) do
7 total = total + v;
8 ks = ks .. k .. ','; --字符串拼接
9 end
10 return ks, total; --多值返回:Tuple
11 end
12 ");
13
14 var dict = new Dictionary<string, int>() //传递字典
15 {
16 { "k1", 1 },
17 { "k2", 2 },
18 { "k3", 3 },
19 };
20 var tp = script.Call(script.Globals["sum"], dict).Tuple; //返回tuple类型
21 Console.WriteLine(tp[0].String); //输出:k1,k2,k3,
22 Console.WriteLine(tp[0].Number); //输出:0, 如果是String类型的调用Number会输出:0
23 Console.WriteLine(tp[1].Number); //输出:6
lua脚本中加载和调用C#代码定义的函数
1 public static void CallList()
2 {
3 Script script = new Script();
4 script.Globals["getlist"] = (Func<List<int>, List<int>>)GetList; //加载C#中定义的函数
5 script.DoString(@"
6 function sum(list)
7 local total = 0;
8 for i,v in ipairs(list) do
9 total = total + v;
10 end
11 return total;
12 end
13 ");
14
15 string scode = @"return sum(getlist( { 11, 12, 13, 14, 15 } ))"; //脚本中调用C#中定义的函数
16 Console.WriteLine(script.DoString(scode)); //输出:120
17
18 }
19
20 private static List<int> GetList(List<int> list)
21 {
22 for (int i = 1; i <= 10; i++)
23 list.Add(i);
24 return list;
25 }
使用来源:http://www.cnblogs.com/skig/archive/2017/02/05/netcore-lua-MoonSharp.html


浙公网安备 33010602011771号