Esempio n. 1
0
 public void Push(GLua GLua, object obj)
 {
     if (obj is Delegate dele)
     {
         CFunc cfunc = (IntPtr L) => {
             List <object> args = new List <object>();
             foreach (var param in dele.Method.GetParameters())
             {
                 args.Add(GLua.Get(param.ParameterType, param.Position + 1));
             }
             var rtn = dele.DynamicInvoke(args.ToArray());
             if (rtn != null)
             {
                 GLua.Push(rtn, rtn.GetType());
                 return(1);
             }
             return(0);
         };
         GCHandle gch = GCHandle.Alloc(cfunc);
         GLua.AddLuaReference(gch);
         GLua.PushCFunction(cfunc);
     }
     else
     {
         throw new Exception("wrapper func must be castable to delegate");
     }
 }
Esempio n. 2
0
        public static int Open(ref lua_State state)
        {
            ClientConsole.RerouteConsole();
            ClientConsole.Color = new Color(0, 150, 255);
            Lua = GLua.Get(state);

            Lua.CreateTable();

            Lua.Push <Action <string> >(Dump);
            Lua.SetField(-2, "Dump");

            Lua.SetField(GLua.LUA_GLOBALSINDEX, "csluaview");

            var container = NativeInterface.Load <INetworkStringTableContainer>("engine", StringTableInterfaceName.CLIENT);
            var tablePtr  = container.FindTable("client_lua_files");
            var table     = JIT.ConvertInstance <INetworkStringTable>(tablePtr);

            Console.WriteLine($"dotnet table ptr: {tablePtr.ToString("X8")}");
            //var path0 = table.GetString(0); //hangs here

            //for (int i = 0; i < table.GetNumStrings(); i++)
            //{
            //}

            //var stringTable = StringTable.FindTable<int>("client_lua_files");
            //var luaFiles = stringTable.Select(s => new LuaFile { Path = s.String, CRC = s.UserData }).ToList();

            Console.WriteLine("DotNet Clientside Lua Viewer Loaded!");
            return(0);
        }
Esempio n. 3
0
        public void Push(GLua GLua, Object obj)
        {
            if (obj is Delegate dele)
            {
                Int32 cfunc(IntPtr L)
                {
                    List <Object> args = new List <Object>();

                    foreach (ParameterInfo param in dele.Method.GetParameters())
                    {
                        args.Add(GLua.Get(param.ParameterType, param.Position + 1));
                    }
                    Object rtn = dele.DynamicInvoke(args.ToArray());

                    if (rtn != null)
                    {
                        GLua.Push(rtn, rtn.GetType());
                        return(1);
                    }
                    return(0);
                }

#pragma warning disable IDE0009 // Member access should be qualified. will post issue on forums
                GLua.PushCFunction(cfunc);
#pragma warning restore IDE0009
            }
            else
            {
                throw new Exception("wrapper func must be castable to delegate");
            }
        }
Esempio n. 4
0
 public static Int32 Close(GLua LUA)
 {
     LUA.GetGlobal("print");
     LUA.Push("Hello! Module is being unloaded");
     LUA.Call(1, 0);
     return(0);
 }
Esempio n. 5
0
 public static Int32 Open(GLua LUA)
 {
     LUA.GetGlobal("print");
     LUA.Push("Hello! Module has been loaded");
     LUA.Call(1, 0);
     return(0);
 }
Esempio n. 6
0
        public static int Open(ref lua_State state)
        {
            GLua = GLua.Get(state);

            GLua.CreateTable();

            GLua.Push("woop was here");
            GLua.SetField(-2, "var");

            GLua.Push <Func <string, int, string> >(DoAThing);
            GLua.SetField(-2, "wrapped");

            GLua.SetField(GLua.LUA_GLOBALSINDEX, "dotnet");

            Console.WriteLine("DotNet loaded");
            return(0);
        }
Esempio n. 7
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);
        }
Esempio n. 8
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);
        }