Пример #1
0
        static int TestFunc(IntPtr lua_state)
        {
            ILua lua = GmodInterop.GetLuaFromState(lua_state);

            lua.Pop(lua.Top());

            lua.PushString(random);

            return(1);
        }
Пример #2
0
        static int TestFunc(IntPtr lua_state)
        {
            ILua lua = GmodInterop.GetLuaFromState(lua_state);

            lua.PushNumber(random_number);

            lua.PushString(random_string);

            return(2);
        }
        static int ClosureTestFunc(IntPtr lua_state)
        {
            ILua lua = GmodInterop.GetLuaFromState(lua_state);

            lua.Pop(lua.Top());

            string upvalue = lua.GetString(GmodInterop.GetUpvalueIndex(1, false));

            lua.PushString(upvalue + string_to_add);

            return(1);
        }
        internal static int ManagedDelegateGC(IntPtr lua_state)
        {
            ILua lua = GmodInterop.GetLuaFromState(lua_state);

            lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
            lua.GetField(-1, ManagedFunctionIdField);
            int managed_delegate_type_id = (int)lua.GetNumber(-1);

            lua.Pop(2);

            IntPtr managed_delegate_handle = lua.GetUserType(1, managed_delegate_type_id);

            GCHandle.FromIntPtr(managed_delegate_handle).Free();

            return(0);
        }
        internal static int ManagedDelegateExecutor(IntPtr lua_state)
        {
            ILua lua = GmodInterop.GetLuaFromState(lua_state);

            try
            {
                lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                lua.GetField(-1, ManagedFunctionIdField);
                int managed_delegate_type_id = (int)lua.GetNumber(-1);
                lua.Pop(2);

                IntPtr managed_delegate_handle = lua.GetUserType(GmodInterop.GetUpvalueIndex(1, false), managed_delegate_type_id);

                Func <ILua, int> managed_delegate = (Func <ILua, int>)GCHandle.FromIntPtr(managed_delegate_handle).Target;

                return(Math.Max(0, managed_delegate(lua)));
            }
            catch (Exception e)
            {
                lua.Pop(lua.Top());
                lua.PushString(".NET Exception: " + e.ToString());
                return(-1);
            }
        }