Пример #1
0
        internal GlobalContext(ILua lua)
        {
            this.lua = lua;

            lua.GetField(-10002, "SERVER");
            isServerSide = lua.GetBool(-1);

            module_contexts = new Dictionary <string, Tuple <GmodNetModuleAssemblyLoadContext, List <GCHandle> > >();

            int managed_func_type_id = lua.CreateMetaTable("ManagedFunction");

            unsafe
            {
                lua.PushCFunction(&ManagedFunctionMetaMethods.ManagedDelegateGC);
            }
            lua.SetField(-2, "__gc");
            lua.Pop(1);

            lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
            lua.PushNumber(managed_func_type_id);
            lua.SetField(-2, ManagedFunctionMetaMethods.ManagedFunctionIdField);
            lua.Pop(1);

            lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
            lua.CreateTable();

            lua.PushManagedFunction(LoadModule);
            lua.SetField(-2, "load");

            lua.PushManagedFunction(UnloadModule);
            lua.SetField(-2, "unload");

            lua.SetField(-2, "dotnet");
            lua.Pop(1);
        }
Пример #2
0
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                lua.PushManagedClosure(lua =>
                {
                    if (lua.Top() != 3)
                    {
                        throw new Exception("Closure execution stack has incorect number of items.");
                    }

                    string one   = lua.GetString(1);
                    string two   = lua.GetString(2);
                    double three = lua.GetNumber(3);

                    lua.Pop(3);

                    lua.PushString(one + three);
                    lua.PushString(three + two);

                    if (lua.Top() != 2)
                    {
                        throw new Exception("Closure execution stack has incorrect number of items after executtion.");
                    }

                    return(2);
                }, 0);

                lua.PushString(random1);
                lua.PushString(random2);
                lua.PushNumber(random3);

                lua.MCall(3, 2);

                string ret_1 = lua.GetString(-2);
                string ret_2 = lua.GetString(-1);

                lua.Pop(2);

                if (ret_1 != random1 + random3)
                {
                    throw new Exception("First return string is incorrect.");
                }

                if (ret_2 != random3 + random2)
                {
                    throw new Exception("Second return string is incorrect.");
                }

                taskCompletion.TrySetResult(true);
            }
            catch (Exception e)
            {
                taskCompletion.TrySetException(new Exception[] { e });
            }

            return(taskCompletion.Task);
        }
Пример #3
0
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                string random_key1 = Guid.NewGuid().ToString();
                string random_key2 = Guid.NewGuid().ToString();
                string random_key3 = Guid.NewGuid().ToString();

                string random1 = Guid.NewGuid().ToString();
                string random2 = Guid.NewGuid().ToString();
                string random3 = Guid.NewGuid().ToString();

                lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                lua.PushString(random1);
                lua.SetField(-2, random_key1);

                lua.PushString(random2);
                lua.SetField(-2, random_key2);

                lua.PushString(random3);
                lua.SetField(-2, random_key3);

                lua.Pop(lua.Top());

                lua.PushGlobalTable();

                lua.GetField(-1, random_key1);
                if (lua.GetString(-1) != random1)
                {
                    throw new Exception("First random string is invalid");
                }
                lua.Pop(1);

                lua.GetField(-1, random_key2);
                if (lua.GetString(-1) != random2)
                {
                    throw new Exception("Second random string is invalid");
                }
                lua.Pop(1);

                lua.GetField(-1, random_key3);
                if (lua.GetString(-1) != random3)
                {
                    throw new Exception("Third random string is invalid");
                }
                lua.Pop(1);

                lua.Pop(1);

                taskCompletion.TrySetResult(true);
            }
            catch (Exception e)
            {
                taskCompletion.TrySetException(new Exception[] { e });
            }

            return(taskCompletion.Task);
        }
Пример #4
0
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                /*
                 * //Test PushUserData
                 * lua.PushUserData((IntPtr)this.RandomInt);
                 *
                 * if(lua.GetUserType(-1, (int)TYPES.USERDATA) != (IntPtr)this.RandomInt)
                 * {
                 *  throw new Exception("GetUserType returned incorrect pointer after PushUserData");
                 * }
                 *
                 * lua.Pop(1);
                 */

                //Test PushUserType
                this.Type_Id = lua.CreateMetaTable("UserDataTestType");
                lua.Pop(1);

                lua.PushUserType((IntPtr)RandomInt2, this.Type_Id);

                if (lua.GetUserType(-1, this.Type_Id) != (IntPtr)this.RandomInt2)
                {
                    throw new Exception("GetUserType returned incorrect pointer after PushUserType");
                }

                //Test SetUserType
                lua.SetUserType(-1, (IntPtr)this.RandomInt3);
                if (lua.GetUserType(-1, this.Type_Id) != (IntPtr)this.RandomInt3)
                {
                    throw new Exception("GetUserType returned incorrect pointer after SetUserType");
                }

                //Additional test for GetType and IsType
                if (lua.GetType(-1) != this.Type_Id)
                {
                    throw new Exception("GetType returned incorrect type id on usertype");
                }

                if (!lua.IsType(-1, this.Type_Id))
                {
                    throw new Exception("IsType returned false on usertype");
                }

                lua.Pop(1);

                taskCompletion.TrySetResult(true);
            }
            catch (Exception e)
            {
                taskCompletion.TrySetException(new Exception[] { e });
            }

            return(taskCompletion.Task);
        }
Пример #5
0
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _)
        {
            task_completion = new TaskCompletionSource <bool>();

            Random rand = new Random();

            int[] random_numbers = new int[1000];
            for (int i = 0; i < 1000; i++)
            {
                random_numbers[i] = rand.Next(2);
            }

            bool[] random_bools = new bool[1000];
            for (int i = 0; i < 1000; i++)
            {
                if (random_numbers[i] == 0)
                {
                    random_bools[i] = false;
                }
                else
                {
                    random_bools[i] = true;
                }
            }


            try
            {
                lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                for (int i = 0; i < 1000; i++)
                {
                    lua.PushBool(random_bools[i]);
                    lua.SetField(-2, this.uuid + "Bool" + i.ToString());
                }
                for (int i = 0; i < 1000; i++)
                {
                    lua.GetField(-1, this.uuid + "Bool" + i.ToString());
                    bool tmp = lua.GetBool(-1);
                    lua.Pop(1);

                    if (tmp != random_bools[i])
                    {
                        throw new PushBoolException(i, random_bools[i], tmp);
                    }
                }
                lua.Pop(1);

                task_completion.TrySetResult(true);
            }
            catch (Exception e)
            {
                task_completion.TrySetException(new Exception[] { e });
            }

            return(task_completion.Task);
        }
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                int type1 = lua.CreateMetaTable(Guid.NewGuid().ToString());
                int type2 = lua.CreateMetaTable(Guid.NewGuid().ToString());
                lua.Pop(2);

                lua.PushUserType((IntPtr)1, type1);

                if (lua.GetUserType(-1, type2) != IntPtr.Zero)
                {
                    throw new Exception("GetUserType returned non-zero pointer in the first test.");
                }

                if (lua.GetUserType(-1, type1) != (IntPtr)1)
                {
                    throw new Exception("GetUserType returned invalid pointer in the second test.");
                }

                lua.Pop(1);

                lua.PushUserType((IntPtr)2, type2);

                if (lua.GetUserType(-1, type1) != IntPtr.Zero)
                {
                    throw new Exception("GetUserType returned non-zero pointer in the third test.");
                }

                if (lua.GetUserType(-1, type2) != (IntPtr)2)
                {
                    throw new Exception("GetUserType returned invalid pointer in the fourth test.");
                }

                lua.Pop(1);

                lua.PushString(Guid.NewGuid().ToString());

                if (lua.GetUserType(-1, type1) != IntPtr.Zero)
                {
                    throw new Exception("GetUserType returned invalid pointer in the fifth test.");
                }

                lua.Pop(1);

                taskCompletion.TrySetResult(true);
            }
            catch (Exception e)
            {
                taskCompletion.TrySetException(new Exception[] { e });
            }

            return(taskCompletion.Task);
        }
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                lua.PushManagedFunction((lua) =>
                {
                    int stack_items = lua.Top();
                    if (stack_items != 3)
                    {
                        throw new Exception("The number of items on the execution stack is incorrect");
                    }

                    string first  = lua.GetString(1);
                    string second = lua.GetString(2);
                    double third  = lua.GetNumber(3);

                    lua.Pop(3);

                    lua.PushString(first + third);
                    lua.PushString(third + second);

                    return(2);
                });

                lua.PushString(random_string_1);
                lua.PushString(random_string_2);
                lua.PushNumber(random_number);

                lua.MCall(3, 2);

                string ret_1 = lua.GetString(-2);
                string ret_2 = lua.GetString(-1);

                lua.Pop(2);

                if (ret_1 != random_string_1 + random_number)
                {
                    throw new Exception("First return string is incorrect");
                }

                if (ret_2 != random_number + random_string_2)
                {
                    throw new Exception("Second return string is incorrect");
                }

                taskCompletion.TrySetResult(true);
            }
            catch (Exception e)
            {
                taskCompletion.TrySetException(new Exception[] { e });
            }

            return(taskCompletion.Task);
        }
Пример #8
0
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                int stack_state = lua.Top();

                lua.PushString(random1);
                lua.PushString(random2);
                lua.PushManagedClosure(lua =>
                {
                    if (lua.Top() != 1)
                    {
                        throw new Exception("Managed closure execution stack has incorrect number of items");
                    }

                    double num = lua.GetNumber(1);

                    lua.Pop(1);

                    string first  = lua.GetString(GmodInterop.GetUpvalueIndex(1));
                    string second = lua.GetString(GmodInterop.GetUpvalueIndex(2));

                    lua.PushString(first + num + second);

                    return(1);
                }, 2);

                if (lua.Top() != stack_state + 1)
                {
                    throw new Exception("Wrong number of items left on the stack");
                }

                lua.PushNumber(random3);

                lua.MCall(1, 1);

                string ret = lua.GetString(-1);
                lua.Pop(1);

                if (ret != random1 + random3 + random2)
                {
                    throw new Exception("Return string is incorrect");
                }

                taskCompletion.TrySetResult(true);
            }
            catch (Exception e)
            {
                taskCompletion.TrySetException(new Exception[] { e });
            }

            return(taskCompletion.Task);
        }
Пример #9
0
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _)
        {
            try
            {
                Random rand = new Random();

                int random_number = rand.Next(1, 500);

                lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                lua.GetField(-1, "tonumber");
                lua.PushString(random_number.ToString());
                if (lua.PCall(1, 1, 0) != 0)
                {
                    throw new PCallTest_NoError_Exception("PCall returned nonzero error code");
                }
                double recieved_num = lua.GetNumber(-1);
                if (recieved_num != (double)random_number)
                {
                    throw new PCallTest_NoError_Exception("Recieved value is invalid");
                }
                lua.Pop(2);

                string random_error_msg = Guid.NewGuid().ToString();

                lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                lua.GetField(-1, "assert");
                lua.PushBool(false);
                lua.PushString(random_error_msg);
                if (lua.PCall(2, 0, 0) == 0)
                {
                    throw new PCallTest_Exception("assert(false, ...) didn't throw an error");
                }
                string res_err_msg = lua.GetString(-1);
                if (res_err_msg != random_error_msg)
                {
                    throw new PCallTest_Exception("Received error message is invalid");
                }
                lua.Pop(2);

                taskCompletion.TrySetResult(true);
            }
            catch (Exception e)
            {
                taskCompletion.TrySetException(new Exception[] { e });
            }

            return(taskCompletion.Task);
        }
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                int stack_state = lua.Top();

                custom_type_id = lua.CreateMetaTable(Guid.NewGuid().ToString());
                lua.PushManagedFunction(lua =>
                {
                    long value = (long)lua.GetUserType(1, custom_type_id);
                    lua.Pop(1);
                    lua.PushString((value * 2).ToString());
                    return(1);
                });
                lua.SetField(-2, "__call");
                lua.Pop(1);

                lua.PushUserType((IntPtr)random_integer, custom_type_id);
                lua.MCall(0, 1);
                string ret_string = lua.GetString(-1);
                lua.Pop(1);

                lua.PushMetaTable(custom_type_id);
                lua.PushNil();
                lua.SetField(-2, "__call");
                lua.Pop(1);

                if ((stack_state - lua.Top()) != 0)
                {
                    throw new Exception("Lua stack has some values left");
                }

                if (ret_string != (random_integer * 2).ToString())
                {
                    throw new Exception("Return string is incorrect");
                }

                taskCompletion.TrySetResult(true);
            }
            catch (Exception e)
            {
                taskCompletion.TrySetException(new Exception[] { e });
            }

            return(taskCompletion.Task);
        }
Пример #11
0
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                lua.PushAngle(new System.Numerics.Vector3(0));
                if (!lua.GetMetaTable(-1))
                {
                    throw new GetMetaTableTestException("GetMetaTable returned false");
                }
                lua.GetField(-1, "IsZero");
                lua.Push(-3);
                lua.MCall(1, 1);
                bool received_bool = lua.GetBool(-1);
                lua.Pop(3);
                if (!received_bool)
                {
                    throw new GetMetaTableTestException("Meta function returned False but must return True");
                }

                taskCompletion.TrySetResult(true);
            }
            catch (Exception e)
            {
                taskCompletion.TrySetException(new Exception[] { e });
            }

            return(taskCompletion.Task);
        }
Пример #12
0
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                lua.PushManagedFunction((lua) =>
                {
                    lua.PushString(random_string);
                    return(1);
                });
                lua.MCall(0, 1);
                string ret_string = lua.GetString(-1);
                lua.Pop(1);

                if (ret_string != random_string)
                {
                    throw new Exception("Return string is invalid");
                }

                taskCompletion.TrySetResult(true);
            }
            catch (Exception e)
            {
                taskCompletion.TrySetException(new Exception[] { e });
            }

            return(taskCompletion.Task);
        }
Пример #13
0
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _)
        {
            this.lua_extructor = lua_extructor;

            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                string closure_upvalue = Guid.NewGuid().ToString();

                lua.PushString(closure_upvalue);

                lua.PushCClosure(closure_ptr, 1);

                lua.MCall(0, 1);

                string received_string = lua.GetString(-1);

                lua.Pop(1);

                if (received_string != (closure_upvalue + string_to_add))
                {
                    throw new PushCClosureTestException("Received string is invalid");
                }

                taskCompletion.TrySetResult(true);
            }
            catch (Exception e)
            {
                taskCompletion.TrySetException(new Exception[] { e });
            }

            return(taskCompletion.Task);
        }
Пример #14
0
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                string key   = Guid.NewGuid().ToString();
                string value = Guid.NewGuid().ToString();

                lua.CreateTable();
                lua.PushString(value);
                lua.SetField(-2, key);

                lua.PushNil();
                lua.Next(-2);

                string received_key   = lua.GetString(-2);
                string received_value = lua.GetString(-1);
                lua.Pop(3);

                if (received_key != key || received_value != value)
                {
                    throw new NextTestException("Received key value pair is invalid");
                }

                taskCompletion.TrySetResult(true);
            }
            catch (Exception e)
            {
                taskCompletion.TrySetException(new Exception[] { e });
            }

            return(taskCompletion.Task);
        }
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                lua.PushManagedClosure(l =>
                {
                    if (l.Top() != 0)
                    {
                        throw new Exception("Closure execution stack is non-empty.");
                    }
                    l.PushString(random);
                    return(1);
                }, 0);
                lua.MCall(0, 1);
                string ret_string = lua.GetString(-1);
                lua.Pop(1);

                if (ret_string != random)
                {
                    throw new Exception("Return string is incorrect.");
                }

                taskCompletion.TrySetResult(true);
            }
            catch (Exception e)
            {
                taskCompletion.TrySetException(new Exception[] { e });
            }

            return(taskCompletion.Task);
        }
Пример #16
0
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                IntPtr func_int_ptr;

                unsafe
                {
                    func_int_ptr = (IntPtr)(delegate * unmanaged[Cdecl] < IntPtr, int >) & TestFunc;
                }

                lua.PushCFunction(func_int_ptr);

                lua.MCall(0, 1);

                string ret_string = lua.GetString(-1);

                lua.Pop(1);

                if (ret_string != random)
                {
                    throw new Exception("Return string is incorrect");
                }

                taskCompletion.TrySetResult(true);
            }
            catch (Exception e)
            {
                taskCompletion.TrySetException(new Exception[] { e });
            }

            return(taskCompletion.Task);
        }
Пример #17
0
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _)
        {
            this.lua_extructor = lua_extructor;

            try
            {
                lua.CreateTable();
                lua.CreateTable();
                lua.PushManagedFunction(MetaToStringDelegate);
                lua.SetField(-2, "__tostring");
                lua.SetMetaTable(-2);

                lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                lua.GetField(-1, "tostring");
                lua.Push(-3);
                lua.MCall(1, 1);

                string get_val = lua.GetString(-1);

                lua.Pop(2);

                if (get_val != to_str_msg)
                {
                    throw new CreateMetaTableException("Recieved string is incorrect");
                }

                taskCompletion.TrySetResult(true);
            }
            catch (Exception e)
            {
                taskCompletion.TrySetException(new Exception[] { e });
            }

            return(taskCompletion.Task);
        }
Пример #18
0
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                Random rand = new Random();

                int random_number = rand.Next(1, 500);

                lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                lua.GetField(-1, "tonumber");
                lua.PushString(random_number.ToString());
                try
                {
                    lua.MCall(1, 1);
                }
                catch (GmodLuaException)
                {
                    throw new MCallTestException("Exception was thrown by MCall, but it is not expected.");
                }
                int received_number = (int)lua.GetNumber(-1);
                if (received_number != random_number)
                {
                    throw new MCallTestException("Numbers mismatch.");
                }
                lua.Pop(2);

                string random_error_message = Guid.NewGuid().ToString();

                lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                lua.GetField(-1, "assert");
                lua.PushBool(false);
                lua.PushString(random_error_message);
                try
                {
                    lua.MCall(2, 0);
                }
                catch (GmodLuaException e)
                {
                    if (e.Message != random_error_message)
                    {
                        throw new MCallTestException("Error message is invalid");
                    }

                    taskCompletion.TrySetResult(true);
                }
                catch (Exception)
                {
                    throw new MCallTestException("Wrong exception type was thrown");
                }
            }
            catch (Exception e)
            {
                taskCompletion.TrySetException(new Exception[] { e });
            }

            return(taskCompletion.Task);
        }
Пример #19
0
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                int initial_number_of_values = lua.Top();

                Random rand = new Random();

                int count = rand.Next(5, 16);

                for (int i = 0; i < count; i++)
                {
                    lua.PushNumber(1);
                }

                int first_get = lua.Top();

                lua.Pop(1);

                int second_get = lua.Top();

                if (first_get != initial_number_of_values + count || second_get != initial_number_of_values + count - 1)
                {
                    throw new PopTopException("Test failed");
                }

                lua.Pop(second_get - initial_number_of_values);

                int last_get = lua.Top();

                if (last_get != initial_number_of_values)
                {
                    throw new PopTopException("Test failed on last check");
                }

                taskCompletion.TrySetResult(true);
            }
            catch (Exception e)
            {
                taskCompletion.TrySetException(new Exception[] { e });
            }

            return(taskCompletion.Task);
        }
Пример #20
0
 public void Unload(ILua lua)
 {
     lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
     lua.GetField(-1, "print");
     lua.PushString("Goodbye World!");
     lua.MCall(1, 0);
     lua.Pop();
 }
Пример #21
0
 public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context)
 {
     lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
     lua.GetField(-1, "print");
     lua.PushString("Hello World!");
     lua.MCall(1, 0);
     lua.Pop();
 }
Пример #22
0
 public static void PushGlobalFunction(ILua lua, string key, Func <ILua, int> value)
 {
     lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
     lua.GetField(-1, "CNode");
     lua.PushManagedFunction(value);
     lua.SetField(-2, key);
     lua.Pop();
 }
Пример #23
0
 public static void PushGlobalVector(ILua lua, string key, Vector3 value)
 {
     lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
     lua.GetField(-1, "CNode");
     lua.PushVector(value);
     lua.SetField(-2, key);
     lua.Pop();
 }
Пример #24
0
 internal static void PrintToConsole(this ILua lua, string message)
 {
     lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
     lua.GetField(-1, "print");
     lua.PushString(message);
     lua.MCall(1, 0);
     lua.Pop(1);
 }
Пример #25
0
        int MetaToString(ILua lua)
        {
            lua.Pop(lua.Top());

            lua.PushString(to_str_msg);

            return(1);
        }
Пример #26
0
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                string table_name = Guid.NewGuid().ToString();
                string field_name = Guid.NewGuid().ToString();

                Random rand     = new Random();
                double rand_num = rand.NextDouble();

                lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                lua.CreateTable();
                lua.PushNumber(rand_num);
                lua.SetField(-2, field_name);
                lua.SetField(-2, table_name);

                lua.Pop(1);

                lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                lua.GetField(-1, table_name);
                if (lua.GetType(-1) != (int)TYPES.TABLE)
                {
                    throw new CreateTableException("Type check failed");
                }
                lua.GetField(-1, field_name);
                double get_num = lua.GetNumber(-1);
                if (get_num != rand_num)
                {
                    throw new CreateTableException("Wrong number recieved");
                }

                lua.Pop(3);

                taskCompletion.TrySetResult(true);
            }
            catch (Exception e)
            {
                taskCompletion.TrySetException(new Exception[] { e });
            }

            return(taskCompletion.Task);
        }
Пример #27
0
 public void Unload(ILua lua)
 {
     lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
     lua.GetField(-1, "hook");
     lua.GetField(-1, "Remove");
     lua.PushString("Tick");
     lua.PushString(this.OnTickIdentifier);
     lua.MCall(2, 0);
     lua.Pop(2);
 }
Пример #28
0
        static int TestFunc(IntPtr lua_state)
        {
            ILua lua = GmodInterop.GetLuaFromState(lua_state);

            lua.Pop(lua.Top());

            lua.PushString(random);

            return(1);
        }
Пример #29
0
        void SetPlayerList(ILua lua)
        {
            if (!getPlayersTasks.IsEmpty)
            {
                List <string> players            = new List <string>();
                Exception     executionException = null;

                try
                {
                    lua.PushGlobalTable();
                    lua.GetField(-1, "player");
                    lua.GetField(-1, "GetAll");
                    lua.MCall(0, 1);
                    lua.PushNil();
                    while (lua.Next(-2) != 0)
                    {
                        lua.GetField(-1, "Nick");
                        lua.Push(-2);
                        lua.MCall(1, 1);
                        players.Add(lua.GetString(-1));
                        lua.Pop(2);
                    }
                    lua.Pop(lua.Top());
                }
                catch (Exception e)
                {
                    executionException = e;
                }

                TaskCompletionSource <List <string> > task;
                while (getPlayersTasks.TryDequeue(out task))
                {
                    if (executionException == null)
                    {
                        task.SetResult(players);
                    }
                    else
                    {
                        task.SetException(executionException);
                    }
                }
            }
        }
Пример #30
0
        int UnloadModule(ILua lua)
        {
            try
            {
                string module_name = lua.GetString(1);

                if (String.IsNullOrEmpty(module_name))
                {
                    throw new Exception("Module name is empty or null");
                }

                if (!module_contexts.ContainsKey(module_name))
                {
                    throw new Exception($"There is no loaded module with name { module_name }");
                }

                lua.PrintToConsole($"Unloading module { module_name } ...");

                WeakReference <GmodNetModuleAssemblyLoadContext> context_weak_reference = UnloadHelper(module_name);

                for (int i = 0; context_weak_reference.TryGetTarget(out _); i++)
                {
                    lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                    lua.GetField(-1, "collectgarbage");
                    lua.MCall(0, 0);
                    lua.Pop(1);

                    GC.Collect();
                    GC.WaitForPendingFinalizers();

                    if (i >= 300)
                    {
                        throw new Exception($"Module {module_name} can't be unloaded: there are remaining references or background threads still executing. " +
                                            $"Module resources can't be freed. Memory leak could occur. Game restart may be required.");
                    }
                }

                lua.PrintToConsole($"Module {module_name} was unloaded.");

                lua.PushBool(true);

                return(1);
            }
            catch (Exception e)
            {
                lua.PrintToConsole("Unable to unload module: exception was thrown");
                lua.PrintToConsole(e.ToString());

                lua.PushBool(false);

                return(1);
            }
        }