/// <summary> /// 运行lua文件并获取结果 /// </summary> /// <param name="file"></param> /// <param name="args"></param> /// <returns></returns> public static string Run(string file, ArrayList args = null) { //文件不存在 if (!File.Exists("user_script_send_convert/" + file)) { return(""); } using (var lua = new vJine.Lua.LuaContext()) { try { lua.set("runType", "send");//一次性处理标志 lua.set("file", file); Initial(lua, "send"); if (args != null) { for (int i = 0; i < args.Count; i += 2) { lua.set((string)args[i], args[i + 1].ToString()); } } return(lua.load("core_script/once.lua")[0].ToString()); } catch (Exception e) { throw new Exception(e.ToString()); } } }
/// <summary> /// 新建一个新的lua虚拟机 /// </summary> public static void New(string file) { canRun = false; isRunning = true; if (tokenSource != null) { tokenSource.Dispose(); } tokenSource = new CancellationTokenSource();//task取消指示 //文件不存在 if (!File.Exists(file)) { return; } lua = new vJine.Lua.LuaContext(); Task.Run(() => { while (!canRun) { Task.Delay(100).Wait(); } try { lua.set("runType", "script");//一次性处理标志 LuaLoader.Initial(lua); lua.load(file); } catch (Exception ex) { StopLua(ex.ToString()); } runTigger(); }, tokenSource.Token); }