发现一个c#与lua脚本交互的工具MoonSharp
直接下载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使用,非常方便。