public void ExecuteScript(string name) { try { _global.DoChunk(_chunks[name]); } catch (Exception e) { DebugHelper.logError("ExecuteScript()", e.Message); throw e; } }
void LoadScene(string id) { if (lua != null) { lua.Dispose(); } Elements = new List <XmlUIElement>(); lua = new Lua(); env = lua.CreateEnvironment(); env.DoChunk("events = {}", "$internal"); env.Add(apiname, api); env.Add("dom", new LuaDom(this)); env.Add("sound", new LuaSound(this)); LuaStyleEnvironment.RegisterFuncs(env); _g = (dynamic)env; var scn = xml.Scenes.Where((x) => x.ID == id).First(); if (scn.Scripts != null) { foreach (var script in scn.Scripts) { env.DoChunk(script, "$xml"); } } foreach (var item in scn.Items) { if (item is XInt.Button) { var btn = (XInt.Button)item; Elements.Add(new XmlUIButton(this, btn, styles.Where((x) => x.ID == btn.Style).First())); } else if (item is XInt.Image) { Elements.Add(new XmlUIImage((XInt.Image)item, this)); } else if (item is XInt.ServerList) { var sl = (XInt.ServerList)item; Elements.Add(new XmlUIServerList(sl, styles.Where((x) => x.ID == sl.Style).First(), this)); } else if (item is XInt.Panel) { var pnl = (XInt.Panel)item; Elements.Add(new XmlUIPanel(pnl, styles.Where((x) => x.ID == pnl.Style).First(), this)); } else if (item is XInt.ChatBox) { var cb = (XInt.ChatBox)item; Elements.Add(new XmlChatBox(cb, styles.Where((x) => x.ID == cb.Style).First(), this)); } } }
public static void Do(LuaChunk c, object e, float time) { var g = (dynamic)G; g.e = e; g.time = time; G.DoChunk(c); }
public List <TestResult> Run() { LuaUtil.PublishObjectMethods(api, luaEnv); api.LuaEnv = luaEnv; luaEnv.DoChunk(myChunk); api.ExecTestCase(); return(api.TestResults()); }
private void ReadScript(string path) { var tc = NUnit.Framework.TestContext.CurrentContext.TestDirectory; var data = System.IO.File.ReadAllText(tc + path); scriptChunk = lua.CompileChunk(data, "scriptChunk", options); luaEnv.DoChunk(scriptChunk); }
public static void DoFunction(Player player, string function) { lock (FuncLock) { DialData = new DialogData(); PlayerHandle = player.Handle; Global.DoChunk(function, "main"); } }
private static void TestDynamic() { var lua = new Lua(); var global = new LuaGlobalPortable(lua) { ["workspace"] = new DynData() }; var r = global.DoChunk("return workspace.Part", "Test.lua"); Console.WriteLine(r.ToString()); }
public void Call(string s) { env.DoChunk(s, "$internal"); }
public static void LuaThread() { if (watcher == null) { Watch(); } App.c = Chroma.Instance; App.c.Initialize(); App.c.DeviceAccess += C_DeviceAccess; App.NewScriptsContext(); // WE NEED TO ENSURE CHROMA IS INITIALISED callbacks = new List <dynamic>(); var ms_luaDebug = new LuaStackTraceDebugger(); var ms_luaCompileOptions = new LuaCompileOptions(); ms_luaCompileOptions.DebugEngine = ms_luaDebug; scriptThreads = new Collection <Thread>(); string path = @"%appdata%\ChromaSync"; path = Environment.ExpandEnvironmentVariables(path); string scriptsPath = Path.Combine(path, "scripts"); string packagesPath = Path.Combine(path, "packages"); if (!Directory.Exists(scriptsPath)) { Directory.CreateDirectory(scriptsPath); } // Todo: Get all scripts including the packages var files = Directory.GetFiles(path, "*.lua", SearchOption.AllDirectories); foreach (string st in files) { var v = RegistryKeeper.GetValue(st); MenuItem menuItem = new MenuItem(Path.GetFileName(st)); menuItem.Name = Path.GetFileName(st); menuItem.Tag = st; menuItem.Click += MenuItem_Click; if (!st.Contains("\\ChromaSync\\packages\\")) { App.scriptsMenu.MenuItems.Add(menuItem); } if (v.Equals("True")) { menuItem.Checked = true; scriptThreads.Add( new Thread(() => { using (Lua l = new Lua()) { LuaGlobalPortable g = l.CreateEnvironment(); dynamic dg = g; dg.DebugLua = new Func <object, bool>(debug); dg.ConvertInt = new Func <object, int>(convertInt); dg.NewCustom = new Func <string, Color, object>(newCustom); dg.IntToByte = new Func <int, byte>(IntToByte); dg.Headset = Headset.Instance; dg.Keyboard = Keyboard.Instance; dg.Mouse = Mouse.Instance; dg.Keypad = Keypad.Instance; dg.Mousepad = Mousepad.Instance; dg.RegisterForEvents = new Func <string, object, bool>(registerEvents); debug("starting Lua script: " + st); try { LuaChunk compiled = l.CompileChunk(st, ms_luaCompileOptions); var d = g.DoChunk(compiled); } catch (LuaException e) { App.Log.Error(e); } catch (Exception e) { App.Log.Info(e); Thread.ResetAbort(); } } })); scriptThreads.Last().Start(); } } }
/// <summary> /// Inits the Lua Engine /// </summary> public static bool Start() { ConsoleUtils.Write(ConsoleMsgType.Status, "Initializing Lua Engine...\n"); LuaEngine = new Lua(); Global = LuaEngine.CreateEnvironment(); // Register Functions GetFunctions(); List <string> scriptFiles = new List <string>(); if (!File.Exists("npc/scripts_main.txt")) { ConsoleUtils.Write(ConsoleMsgType.FatalError, "Could not find file npc/scripts_main.txt. Terminating...\n"); return(false); } scriptFiles.AddRange(File.ReadAllLines("npc/scripts_main.txt")); StringBuilder chunkText = new StringBuilder(); for (int i = 0; i < scriptFiles.Count; i++) { if (scriptFiles[i].StartsWith("//")) { // Ignore comments continue; } else if (scriptFiles[i].StartsWith("npc:")) { // Loads a Lua string fname = scriptFiles[i].Split(new char[] { ':' }, 2)[1].TrimStart(' '); if (!File.Exists(fname)) { ConsoleUtils.Write(ConsoleMsgType.Error, "Could not find file '{0}'...\n", fname); continue; } chunkText.Append(" "); chunkText.Append(File.ReadAllText(fname)); } else if (scriptFiles[i].StartsWith("import:")) { // imports another txt string fname = scriptFiles[i].Split(new char[] { ':' }, 2)[1].TrimStart(' '); if (!File.Exists(fname)) { ConsoleUtils.Write(ConsoleMsgType.Error, "Could not find file '{0}'...\n", fname); continue; } scriptFiles.AddRange(File.ReadAllLines(fname)); } } Chunk = LuaEngine.CompileChunk(chunkText.ToString(), "main", null); Global.DoChunk(Chunk); /* * var luaFunc = LuaSysGlobal["GetStruct"] as Func<LuaResult>; * if (luaFunc == null) * return null; * LuaResult r = luaFunc(); */ ConsoleUtils.Write(ConsoleMsgType.Status, "Lua Engine initialized...\n"); return(true); }
public static void LuaThread() { if (watcher == null) { Watch(); } // WE NEED TO ENSURE CHROMA IS INITIALISED var c = Chroma.Instance; callbacks = new List <dynamic>(); var ms_luaDebug = new LuaStackTraceDebugger(); var ms_luaCompileOptions = new LuaCompileOptions(); ms_luaCompileOptions.DebugEngine = ms_luaDebug; scriptThreads = new Collection <Thread>(); EventHook.MouseHook.MouseAction += new EventHandler(Event); string path = @"%appdata%\ChromaSync"; path = Environment.ExpandEnvironmentVariables(path); path = Path.Combine(path, "scripts"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } foreach (string st in Directory.GetFiles(path, "*_main.lua", SearchOption.AllDirectories)) { scriptThreads.Add( new Thread(() => { using (Lua l = new Lua()) { LuaGlobalPortable g = l.CreateEnvironment(); dynamic dg = g; dg.DebugLua = new Func <object, bool>(debug); dg.ConvertInt = new Func <JValue, int>(convertInt); dg.NewCustom = new Func <string, Color, object>(newCustom); dg.IntToByte = new Func <int, byte>(IntToByte); dg.Headset = Headset.Instance; dg.Keyboard = Keyboard.Instance; dg.Mouse = Mouse.Instance; dg.Keypad = Keypad.Instance; dg.Mousepad = Mousepad.Instance; dg.RegisterForEvents = new Func <string, object, bool>(registerEvents); debug("starting Lua script: " + st); try { LuaChunk compiled = l.CompileChunk(st, ms_luaCompileOptions); var d = g.DoChunk(compiled); } catch (LuaException e) { debug(e.FileName + ": " + e.Line + ": " + e.Message); } catch (Exception e) { debug(e.Message); } } })); scriptThreads.Last().Start(); } }