public static LuaValue Wait(LuaValue[] args) { LuaNumber time = args[0] as LuaNumber; if (time == null) { LuaFunction cond = args[0] as LuaFunction; if (cond == null) { throw new Exception("wait: object '" + args[0] + "' is not a number or function!"); } bool condOK = false; while (!condOK) { try { condOK = cond.Invoke(null).GetBooleanValue(); } catch (Exception e) { A8Console.WriteLine("Exception on wait: " + e.GetType().Name + " - " + e.Message); } if (!condOK) { System.Threading.Thread.Sleep(100); } } } else { System.Threading.Thread.Sleep(int.Parse((time.Number * 1000).ToString())); } return(null); }
void Update() { while (server.Server.IsBound && server.Pending()) { SocketStateObject newConn = new SocketStateObject(); newConn.socket = server.AcceptSocket(); connections.Add(newConn); } List <SocketStateObject> cleanup = new List <SocketStateObject>(); byte[] sendData; lock (this) { sendData = Encoding.UTF8.GetBytes(sendCache.ToString()); sendCache = new StringBuilder(); } LuaRuntime.GlobalEnvironment = luaEnv; foreach (SocketStateObject client in connections) { if (!client.socket.Connected) { cleanup.Add(client); continue; } if (client.socket.Available > 0) { int br = client.socket.Receive(client.buffer, SocketStateObject.BufferSize, SocketFlags.Partial); client.sb.Append(Encoding.UTF8.GetString(client.buffer, 0, br)); string tmp = client.sb.ToString(); if (tmp.Contains("\n")) { string[] pcs = tmp.Replace("\r", "").Split('\n'); foreach (string line in pcs.Take(pcs.Length - 1)) { try { LuaRuntime.Run(line, luaEnv); } catch (Exception e) { A8Console.WriteLine(e.GetType().Name + ": " + e.Message); luaEnv.SetNameValue("lastError", ObjectToLua.ToLuaValue(e)); } } client.sb = new StringBuilder(pcs.Last()); } } if (sendData.Length > 0) { client.socket.Send(sendData); } } connections.RemoveAll(i => cleanup.Contains(i)); }
public static void RegisterFunctions(LuaTable module) { module.Register("write", Write); module.Register("writeline", WriteLine); module.Register("clear", new LuaFunc(delegate(LuaValue[] args) { A8Console.Clear(); return(LuaNil.Nil); })); }
public void runScript(string script) { try { LuaRuntime.GlobalEnvironment = luaEnv; LuaRuntime.Run(script, luaEnv); } catch (Exception e) { A8Console.WriteLine(e.GetType().Name + ": " + e.Message); luaEnv.SetNameValue("lastError", ObjectToLua.ToLuaValue(e)); } }
private static void PrintTable(LuaTable tbl, string indent, LuaValue _key = null) { /* sample output: * table: 002CCBA8 * { * field = value * X = 10 * y = function: 002CCBA8 * } */ string i = indent; A8Console.Write(i); if (_key != null) { A8Console.Write(_key.ToString() + " = "); } A8Console.WriteLine(tbl.ToString() + "\n" + i + "{"); foreach (LuaValue key in tbl.Keys) { LuaValue v = tbl.GetValue(key); if (v.GetTypeCode() == "table") { // check that its not a reference of itself if (!scanned.ContainsKey(key)) { scanned.SetKeyValue(key, v); PrintTable(v as LuaTable, i + " ", key); } else { A8Console.WriteLine(i + " " + key.ToString() + " = " + v.ToString()); } } else { scanned.SetKeyValue(key, v); A8Console.WriteLine(i + " " + key.ToString() + " = " + v.ToString()); } }/* * foreach (LuaValue key in tbl.MetaTable.Keys) * { * A8Console.WriteLine(i + "(MetaTable): " + key.ToString() + " = " + tbl.MetaTable.GetValue(key).ToString()); * }*/ A8Console.WriteLine(i + "}"); }
public bool Resume(LuaValue[] args) { if (thread == null) { thread = new Thread(new ThreadStart(delegate() { try { _status = "running"; func.Invoke(args); _status = "dead"; Running = null; } catch (Exception e) { A8Console.WriteLine("Coroutine exception: " + e.GetType().Name + " - " + e.Message + "\n" + e.StackTrace); _status = "dead"; } })); //thread.SetApartmentState(ApartmentState.MTA); thread.Start(); } else { if (_status == "dead") { throw new Exception("Error: coroutine is dead, it cannot be resumed!"); } try { if (_status == "suspended") { lock (thread) { Monitor.Pulse(thread); } } else { thread.Start(); } _status = "running"; } catch (Exception ex) { _status = "dead"; throw ex; } } Running = this; return(true); }
public void consoleRun() { A8Console.WriteLine("> " + line); if (history.LastOrDefault() != line) { history.Add(line); } historyPos = 0; try { LuaRuntime.GlobalEnvironment = luaEnv; LuaRuntime.Run(line, luaEnv); } catch (Exception e) { A8Console.WriteLine(e.GetType().Name + ": " + e.Message); luaEnv.SetNameValue("lastError", ObjectToLua.ToLuaValue(e)); } line = ""; }
/* * public static LuaValue OpenFile(LuaValue[] values) * { * OpenFileDialog ofd = new OpenFileDialog(); * ofd.Filter = "Lua files|*.lua|SharpLua files|*.slua|wLua files|*.wlua|All Files|*.*"; * if (ofd.ShowDialog() == DialogResult.OK) * { * A8Console.WriteLine("Loading file '" + Path.GetFileName(ofd.FileName) + "'..."); * return LuaRuntime.RunFile(ofd.FileName, LuaRuntime.GlobalEnvironment); * } * else * return LuaNil.Nil; * } */ public static LuaValue Require(LuaValue[] args) { // get loaders table LuaTable t = (LuaRuntime.GlobalEnvironment.GetValue("package") as LuaTable).GetValue("loaders") as LuaTable; if (t == null) { throw new Exception("Cannot get loaders table from package module!"); } if (t.Count == 0) { throw new Exception("Loaders table is empty!"); } // whether package was found/loaded LuaBoolean b = LuaBoolean.False; LuaValue module = null; foreach (LuaValue key in t.Keys) { LuaFunction f = t.GetValue(key) as LuaFunction; if (f != null) { LuaMultiValue lmv = f.Invoke(new LuaValue[] { new LuaString(args[0].Value.ToString()) }) as LuaMultiValue; b = lmv.Values[0] as LuaBoolean; if (b.BoolValue == true) { module = lmv.Values[1]; break; } } else { throw new Exception("Cannot cast type '" + t.GetValue(key).GetType().ToString() + "' to type 'LuaFunction'"); } } if (b.BoolValue == false) { A8Console.WriteLine("Could not load package '" + args[0].Value.ToString() + "'!"); } return(module); }
public MechJebModuleAutom8(MechJebCore core) : base(core) { instance = this; luaEnv = LuaRuntime.CreateGlobalEnviroment(); mechjeb = new LuaTable(); core.registerLuaMembers(mechjeb); luaEnv.SetNameValue("mechjeb", mechjeb); luaEnv.SetNameValue("vessel", ObjectToLua.ToLuaValue(vesselState)); if (KSP.IO.File.Exists <MuMechJeb>("autorun.lua")) { try { LuaRuntime.GlobalEnvironment = luaEnv; LuaRuntime.RunFile("autorun.lua", luaEnv); } catch (Exception e) { A8Console.WriteLine(e.GetType().Name + ": " + e.Message); luaEnv.SetNameValue("lastError", ObjectToLua.ToLuaValue(e)); } } }
public static LuaValue WriteLine(LuaValue[] args) { A8Console.WriteLine(string.Join(" ", args.Select(x => x.ToString()).ToArray())); return(LuaNil.Nil); }
public static LuaValue Dump(LuaValue[] args) { A8Console.WriteLine(Inspector.Inspect(args[0].Value)); return(LuaNil.Nil); }
public static void RegisterFunctions(LuaTable module) { // cpath -> cspath? module.SetNameValue("cpath", new LuaString(".\\?;.\\?.dll;.\\?.exe")); module.SetNameValue("path", new LuaString(".\\?;.\\?.lua;.\\?.slua;.\\?.wlua;" + System.Environment.GetEnvironmentVariable("LUA_PATH"))); module.SetNameValue("bpath", new LuaString(".\\?;.\\?.luac;.\\?.out;.\\?.sluac")); module.SetNameValue("loaded", new LuaTable()); module.SetNameValue("preload", new LuaTable()); module.Register("seeall", SeeAll); // Load package loader functions LuaTable loaderfunctions = new LuaTable(); loaderfunctions.Register("PreloadSearcher", new LuaFunc(delegate(LuaValue[] args) { LuaTable t = (LuaRuntime.GlobalEnvironment.GetValue("package") as LuaTable).GetValue("preload") as LuaTable; string mod = (args[0] as LuaString).Text; LuaValue v = t.GetValue(mod); if (v != null && v != LuaNil.Nil) { return(new LuaMultiValue(new LuaValue[] { LuaBoolean.True, v as LuaTable })); } else { return(new LuaMultiValue(new LuaValue[] { LuaBoolean.False })); } })); loaderfunctions.Register("PathSearcher", new LuaFunc(delegate(LuaValue[] args) { // get package.path variable string path = (LuaRuntime.GlobalEnvironment.GetValue("package") as LuaTable).GetValue("path").Value.ToString(); // split into paths string[] paths = path.Split(';'); // check file names foreach (string p in paths) { foreach (LuaValue arg in args) { string sfn = arg.Value.ToString(); string fn = p.Replace("?", sfn); if (File.Exists <PackageLib>(fn)) { LuaTable m = new LuaTable(); m.AddValue(LuaRuntime.RunFile(fn, LuaRuntime.GlobalEnvironment)); return(new LuaMultiValue(new LuaValue[] { LuaBoolean.True, LuaRuntime.GlobalEnvironment.GetValue(fn) })); // Path.GetFileNameWithoutExtension(fn) } } } return(new LuaMultiValue(new LuaValue[] { LuaBoolean.False })); })); loaderfunctions.Register("CSPathSearcher", new LuaFunc(delegate(LuaValue[] args) { // get package.path variable string path = (LuaRuntime.GlobalEnvironment.GetValue("package") as LuaTable).GetValue("cpath").Value.ToString(); // split into paths string[] paths = path.Split(';'); // check file names foreach (string p in paths) { foreach (LuaValue arg in args) { string sfn = arg.Value.ToString(); string fn = p.Replace("?", sfn); if (File.Exists <PackageLib>(fn)) { A8Console.WriteLine("Loading file '" + fn + "'..."); string[] modules = ExternalLibraryLoader.Load(fn); LuaTable t = LuaRuntime.GlobalEnvironment.GetValue(modules[0]) as LuaTable; return(new LuaMultiValue(new LuaValue[] { LuaBoolean.True, t })); } } } return(new LuaMultiValue(new LuaValue[] { LuaBoolean.False })); })); loaderfunctions.Register("BinarySearcher", new LuaFunc(delegate(LuaValue[] args) { // get package.path variable string path = (LuaRuntime.GlobalEnvironment.GetValue("package") as LuaTable).GetValue("bpath").Value.ToString(); // split into paths string[] paths = path.Split(';'); // check file names foreach (string p in paths) { foreach (LuaValue arg in args) { string sfn = arg.Value.ToString(); string fn = p.Replace("?", sfn); if (File.Exists <PackageLib>(fn)) { LuaTable m = new LuaTable(); bool isBreak; m.AddValue((Serializer.Deserialize(fn) as AST.Chunk).Execute(LuaRuntime.GlobalEnvironment, out isBreak)); return(new LuaMultiValue(new LuaValue[] { LuaBoolean.True, LuaRuntime.GlobalEnvironment.GetValue(fn) })); // Path.GetFileNameWithoutExtension(fn) } } } return(new LuaMultiValue(new LuaValue[] { LuaBoolean.False })); })); module.SetNameValue("loaders", loaderfunctions); module.Register("loadlib", new LuaFunc((LuaValue[] args) => { return(args[0]); })); }
public static LuaValue Print(LuaValue[] values) { A8Console.WriteLine(string.Join(" ", values.Select(x => x.ToString()).ToArray())); return(null); }