zoukankan      html  css  js  c++  java
  • .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         }
    作者:skig
    欢迎转载,但请注明出处。如果大家有任何疑问,可以给我留言,我会及时回复。
  • 相关阅读:
    java.lang.NoSuchMethodError
    asm相关内容想下载(包括 jar 包)
    Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/objectweb/asm/Type
    用Navicat连接mysql报错:2003-Can't connect to MySql server on '10.100.0.109'(10039)
    The type java.lang.reflect.AnnotatedElement cannot be resolved. It is indirectly referenced from required .class files
    The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files
    交通测速方式
    卡口和电子警察的区别
    Myeclipse连接Mysql数据库时报错:Error while performing database login with the pro driver:unable
    在window上安装mysql
  • 原文地址:https://www.cnblogs.com/skig/p/netcore-lua-MoonSharp.html
Copyright © 2011-2022 走看看