public static Int32 Close(GLua LUA) { LUA.GetGlobal("print"); LUA.Push("Hello! Module is being unloaded"); LUA.Call(1, 0); return(0); }
public static Int32 Open(GLua LUA) { LUA.GetGlobal("print"); LUA.Push("Hello! Module has been loaded"); LUA.Call(1, 0); return(0); }
// Arguments in C# methods are optional. // To elaborate, a Lua script can execute the below function // with two arguments, despite the fact that there is only one // C# argument. This is because we must check for the existance // of the Lua arguments within this C# method. So if Lua // executes this method without argument 1 of type string and // argument 2 of type function, a Lua error is emitted to VM. // If you choose to add arguments to a C# function, the GLua // type marshals will attempt to cast the Lua arguments to // its equivalent C# type. If a cast fails, the C# argument uses // defaults. private static void SecretPhrase() { int callback = -1; Console.WriteLine("Finding the secret phrase..."); // This will freeze the server for five second because we aren't asynchronous System.Threading.Thread.Sleep(5000); // Push the callback function to the stack Lua.Push(2); // Push argument 1 to the stack Lua.Push("rusty bullet hole"); // Create the Lua reference after all arguments are passed callback = Lua.ReferenceCreate(); // Push our completed reference back to the stack Lua.ReferencePush(callback); // Call the callback function with our secret phrase as argument 1 Lua.Call(1, 0); }
public static int Open(ref lua_State state) { GLua = GLua.Get(state); GLua.CreateTable(); GLua.Push(new Func <string, object>((string str) => { GLua.Push <string>("hello there"); GLua.Call(1, 2); Console.WriteLine(GLua.Get <string>(5)); GLua.Pop(); GLua.Push <string>("hello there"); GLua.Call(1, 2); Console.WriteLine(GLua.Get <string>(5)); return(0); })); GLua.SetField(-2, "test"); GLua.Push(new Func <string, string, int, object>((string path, string name, int boundObjectAmout) => { int res = instances.Count; try { instances.Add(NodeInstance.InstantiateSync(path, name, boundObjectAmout)); return(res); } catch (Exception e) { return(e.ToString()); } })); GLua.SetField(-2, "instantiateSync"); GLua.Push(new Func <string, string, int, object>((string path, string name, int boundObjectAmout) => { int res = instances.Count; NodeInstance.InstantiateAsync(path, name, boundObjectAmout).ContinueWith(taskResult => { if (taskResult.IsFaulted) { newInstancesExceptionsAsync.Add(res, string.Join(",", taskResult.Exception.InnerExceptions.Select(x => x.ToString()).ToArray())); } else { newInstancesAsync.Add(res, taskResult.Result); } }); return(res); })); GLua.SetField(-2, "instantiateAsync"); GLua.Push(new Func <int, bool>((int ptr) => { if (newInstancesAsync.ContainsKey(ptr)) { newInstancesAsync.Remove(ptr); return(true); } return(false); })); GLua.SetField(-2, "checkForNewAsyncInstances"); GLua.Push(new Func <int, object>((int ptr) => { if (newInstancesExceptionsAsync.ContainsKey(ptr)) { string str = newInstancesExceptionsAsync[ptr]; newInstancesExceptionsAsync.Remove(ptr); return(str); } return(false); })); GLua.SetField(-2, "checkForNewAsyncInstancesExceptions"); GLua.Push(new Func <int, string>((int instance) => { return(JsonConvert.SerializeObject(instances[instance].Methods)); })); GLua.SetField(-2, "getInstanceMethods"); GLua.Push(new Func <int, string, string, string>((int instance, string funcName, string args) => { Func <object> getHandler = new Func <object>(() => { GLua.Push <string>("hello there"); GLua.Call(1, 2); Console.WriteLine(GLua.Get <string>(5)); GLua.Pop(); return(0); }); try { object result = instances[instance].Call(funcName, JsonConvert.DeserializeObject <object[]>(args)); if (result == null) { return(null); } return(JsonConvert.SerializeObject(result)); } catch (Exception e) { Console.WriteLine(e); return("Error: " + e.Message); } })); GLua.SetField(-2, "callInstanceMethod"); GLua.Push(new Func <int, object>((int instance) => { instances[instance] = null; return(null); })); GLua.SetField(-2, "removeInstance"); GLua.Push(new Func <int, object>((int instance) => { instances[instance] = null; return(null); })); GLua.SetField(-2, "getInstanceObject"); GLua.Push(new Func <int, object>((int instance) => { instances[instance] = null; return(null); })); GLua.SetField(-2, "removeInstance"); GLua.Push(new Func <double>(() => { return(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds); })); GLua.SetField(-2, "getUnixTime"); GLua.SetField(GLua.LUA_GLOBALSINDEX, WRAPPER_OBJECT_NAME); return(0); }