.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         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM