C#与lua交互

发现一个c#与lua脚本交互的工具MoonSharp

 

http://www.moonsharp.org/

 

直接下载release版本,添加MoonSharp.Interpreter.dll的引用之后,即可使用。示例代码如下:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MoonSharp.Interpreter;

namespace LuaOperation
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("result1 = {0}", CallbackTest1());
            Console.WriteLine("result2 = {0}", CallbackTest2());
            Console.WriteLine("result3 = {0}", EnumerableTest());
            Console.WriteLine("result4 = {0}", TableTest1());
            Console.WriteLine("result5 = {0}", TableTest2());
            Console.WriteLine("result6 = {0}", TableTestReverse());
            Console.WriteLine("result7 = {0}", TableTestReverseSafer());
            Console.WriteLine("result8 = {0}", TableTestReverseWithTable());
            Console.ReadKey();
        }

        private static double CallbackTest1()
        {
            string scriptCode = @"    
        -- defines a factorial function
        function fact (n)
            if (n == 0) then
                return 1
            else
                return n * fact(n - 1);
            end
        end";

            Script script = new Script();

            script.DoString(scriptCode);

            DynValue res = script.Call(script.Globals["fact"], 4);

            return res.Number;
        }

        private static int Mul(int a, int b)
        {
            return a * b;
        }

        private static double CallbackTest2()
        {
            string scriptCode = @"    
        -- defines a factorial function
        function fact (n)
            if (n == 0) then
                return 1
            else
                return Mul(n, fact(n - 1));
            end
        end";

            Script script = new Script();

            script.Globals["Mul"] = (Func<int, int, int>)Mul;

            script.DoString(scriptCode);

            DynValue res = script.Call(script.Globals["fact"], 4);

            return res.Number;
        }

        private static IEnumerable<int> GetNumbers()
        {
            for (int i = 1; i <= 10; i++)
                yield return i;
        }

        private static double EnumerableTest()
        {
            string scriptCode = @"    
        total = 0;
        
        for i in getNumbers() do
            total = total + i;
        end

        return total;
    ";

            Script script = new Script();

            script.Globals["getNumbers"] = (Func<IEnumerable<int>>)GetNumbers;

            DynValue res = script.DoString(scriptCode);

            return res.Number;
        }

        private static List<int> GetNumberList()
        {
            List<int> lst = new List<int>();

            for (int i = 1; i <= 10; i++)
                lst.Add(i);

            return lst;
        }

        private static double TableTest1()
        {
            string scriptCode = @"    
        total = 0;

        tbl = getNumbers()
        
        for _, i in ipairs(tbl) do
            total = total + i;
        end

        return total;
    ";

            Script script = new Script();

            script.Globals["getNumbers"] = (Func<List<int>>)GetNumberList;

            DynValue res = script.DoString(scriptCode);

            return res.Number;
        }

        private static Table GetNumberTable(Script script)
        {
            Table tbl = new Table(script);

            for (int i = 1; i <= 10; i++)
                tbl[i] = i;

            return tbl;
        }

        private static double TableTest2()
        {
            string scriptCode = @"    
        total = 0;

        tbl = getNumbers()
        
        for _, i in ipairs(tbl) do
            total = total + i;
        end

        return total;
    ";

            Script script = new Script();

            script.Globals["getNumbers"] = (Func<Script, Table>)(GetNumberTable);

            DynValue res = script.DoString(scriptCode);

            return res.Number;
        }

        public static double TableTestReverse()
        {
            string scriptCode = @"    
		return dosum { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
	";

            Script script = new Script();

            script.Globals["dosum"] = (Func<List<int>, int>)(l => l.Sum());

            DynValue res = script.DoString(scriptCode);

            return res.Number;
        }

        public static double TableTestReverseSafer()
        {
            string scriptCode = @"    
		return dosum { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
	";

            Script script = new Script();

            script.Globals["dosum"] = (Func<List<object>, int>)(l => l.OfType<int>().Sum());

            DynValue res = script.DoString(scriptCode);

            return res.Number;
        }

        static double Sum(Table t)
        {
            var nums = from v in t.Values
                       where v.Type == DataType.Number
                       select v.Number;

            return nums.Sum();
        }


        private static double TableTestReverseWithTable()
        {
            string scriptCode = @"    
        return dosum { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
    ";

            Script script = new Script();

            script.Globals["dosum"] = (Func<Table, double>)Sum;

            DynValue res = script.DoString(scriptCode);

            return res.Number;
        }
    }
}

 

MoonSharp还包含了Unity3D的包,可以直接导入unity3d使用,非常方便。

转载于:https://my.oschina.net/propagator/blog/798459

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值