/// <summary> /// Processes an HTTP Web request for a #Lua doc /// </summary> /// <param name="context"></param> public void ProcessRequest(HttpContext context) { // Create a new environment and add a table 'web' that has access to the HttpObjects LuaTypes.LuaTable env = LuaRuntime.CreateGlobalEnviroment(); LuaTypes.LuaTable Web = new SharpLua.LuaTypes.LuaTable(); Web.SetNameValue("context", SharpLua.ObjectToLua.ToLuaValue(context)); Web.SetNameValue("response", SharpLua.ObjectToLua.ToLuaValue(context.Response)); Web.SetNameValue("request", SharpLua.ObjectToLua.ToLuaValue(context.Request)); Web.SetNameValue("application", SharpLua.ObjectToLua.ToLuaValue(context.Application)); Web.SetNameValue("session", SharpLua.ObjectToLua.ToLuaValue(context.Session)); Web.SetNameValue("filepath", SharpLua.ObjectToLua.ToLuaValue(context.Request.PhysicalPath)); env.SetNameValue("web", Web); try { LuaRuntime.RunFile(context.Request.PhysicalPath, env); } catch (Exception e) { context.Response.Write("<h1>Error Loading #Lua File " + context.Request.PhysicalPath + "</h1><br />"); context.Response.Write("<br /> <font color='red'>"); context.Response.Write(e.ToString().Replace("\n", "<br />")); context.Response.Write("</font>"); } }
void Start() { t = LuaRuntime.CreateGlobalEnviroment(); t.SetNameValue("selfVehicle", new LuaUserdata(self, true)); t.SetNameValue("sceneManager", new LuaUserdata(SceneManager.instance, true)); if (initCodes != "") { LuaRuntime.Run(initCodes, t); } }
public static void Main(string[] cmd_args_1938475092347027340582734) // random name doesnt interfere with my variables { // Create a global environment LuaTable t = LuaRuntime.CreateGlobalEnviroment(); // to add an item, dont use AddValue, it sticks it into the back // instead, use SetNameValue t.SetNameValue("obj", new LuaString("sample object")); // we can set the MetaTable of item "obj", but first we must get it from // the global environment: LuaValue val = t.GetValue("obj"); // We can then print "val" Console.WriteLine(val.ToString()); // --> sample object // to register methods, use the Register function (using Addresses or delegates) t.Register("samplefunc", delegate(LuaValue[] args) { return(new LuaNumber(100)); }); // To run Lua, use the Run function in LuaRuntime // we pass "t" as the specified environment, otherwise it will // create a new environment to run in. LuaRuntime.Run("print(\"obj:\", obj, \"\nsamplefunc:\", samplefunc())", t); // we can also call .NET methods using Lua-created .NET object // such as: LuaRuntime.Run("obj2 = script.create(\"CSharpExampleProject.TestClass\")", t); LuaRuntime.Run("print(\"testint:\", obj2.testint, \"TestFunc:\", obj2.TestFunc())", t); // the reason for this is because script.create returns an advanced Userdata with // metatables that check any indexing or setting and map them to the .NET object // if it doesn't find it, it throws an error //LuaRuntime.Run("obj2.ThisValueDoesntExistInDotNet = \"hey\"", t); // the same value was printed twice, with different functions each time, proving its not actually there: //Console.WriteLine(LuaRuntime.Run("return \"Sample attemption at creating an object: \" .. tostring(obj2.ThisValueDoesntExistInDotNet)", t)); // Console.WriteLine was used to show that you can print the returned value of executed code //LuaRuntime.Run("print(obj2.ThisValueDoesntExistInDotNet)", t); // Lua can also create "classes" LuaRuntime.Run("c = class()", t); Console.WriteLine(LuaRuntime.Run("return c", t)); // You can also call functions defined in #Lua LuaFunction f = LuaRuntime.Run("return function() print\"a function says hai\" end", t) as LuaFunction; f.Invoke(new LuaValue[] { }); // Let you see the output Console.Write("Press any key to continue . . . "); Console.ReadKey(true); }
void Start() { port = part.port; luaEnv = LuaRuntime.CreateGlobalEnviroment(); LuaTable dbg = new LuaTable(); dbg.Register("send", dbgSend); dbg.Register("tojson", dbgToJSON); luaEnv.SetNameValue("debugatron", dbg); A8Console.OnConsoleEvent += A8ConsoleEvent; server = new TcpListener(IPAddress.Any, port); server.Start(); }
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)); } } }