首页 > 代码库 > .net core中加载lua脚本的类库: MoonSharp

.net core中加载lua脚本的类库: MoonSharp

前言

MoonSharp是一个支持C#调用lua脚本的类库,支持.net, .net core, mono, unity,因此在.net core中也能够使用,而且加载和调用lua也很方便简单;

官网:http://www.moonsharp.org/

源码: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
View Code

 

传递数据(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         }

 

.net core中加载lua脚本的类库: MoonSharp