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

            this.lua_extructor = lua_extructor;

            try
            {
                // Create metatable
                lua.CreateTable();
                lua.PushManagedFunction(this.eq_func);
                lua.SetField(-2, "__eq");

                // Create first table to compare
                lua.CreateTable();
                lua.PushNumber(1);
                lua.SetField(-2, "A");
                lua.Push(-2);
                lua.SetMetaTable(-2);

                // Create second table to compare
                lua.CreateTable();
                lua.PushNumber(2);
                lua.SetField(-2, "A");
                lua.Push(-3);
                lua.SetMetaTable(-2);

                // Get compare results
                bool equal_result     = lua.Equal(-1, -2);
                bool raw_equal_result = lua.RawEqual(-1, -2);

                lua.Pop(3);

                if (!equal_result)
                {
                    throw new EqualityTestException("ILua.Equal returned false but must return true");
                }

                if (raw_equal_result)
                {
                    throw new EqualityTestException("ILua.RawEqual returned true but must return false");
                }

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

            return(taskCompletion.Task);
        }
Пример #2
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);
        }
Пример #3
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);
        }
        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);
        }
Пример #5
0
        static int TestFunc(IntPtr lua_state)
        {
            ILua lua = GmodInterop.GetLuaFromState(lua_state);

            lua.PushNumber(random_number);

            lua.PushString(random_string);

            return(2);
        }
Пример #6
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);
        }
Пример #7
0
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                Random rand = new Random();

                int first  = rand.Next(1, 10000000);
                int second = rand.Next(1, 10000000);
                int third  = rand.Next(1, 10000000);

                lua.PushNumber(first);
                lua.PushNumber(second);
                lua.PushNumber(third);

                lua.Insert(-2);

                int received_first  = (int)lua.GetNumber(-3);
                int received_second = (int)lua.GetNumber(-2);
                int received_third  = (int)lua.GetNumber(-1);

                lua.Pop(3);

                if (!(received_first == first && received_second == third && received_third == second))
                {
                    throw new InsertTestException("Received numbers are invalid");
                }

                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 _)
        {
            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);
        }
Пример #9
0
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _)
        {
            taskSource = new TaskCompletionSource <bool>();

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

                double[] Random_numbers = new double[10];

                Random rand = new Random();

                for (int i = 0; i < 10; i++)
                {
                    Random_numbers[i] = rand.NextDouble();
                }

                lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);

                for (int i = 0; i < 10; i++)
                {
                    lua.PushNumber(Random_numbers[i]);
                    lua.SetField(-2, LuaNumId + "Num" + i.ToString());
                }

                for (int i = 0; i < 10; i++)
                {
                    lua.GetField(-1, LuaNumId + "Num" + i.ToString());
                    double tmp = lua.GetNumber(-1);
                    lua.Pop(1);

                    if (tmp != Random_numbers[i])
                    {
                        throw new PushNumberException(i, Random_numbers[i], tmp);
                    }
                }

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

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

            try
            {
                lua.PushNumber(1);

                lua.PushString("Dima is the best boy.");

                lua.PushBool(true);

                lua.PushNil();

                if (!lua.IsType(-4, TYPES.NUMBER))
                {
                    throw new Exception("IsType returned false on NUMBER type");
                }

                if (!lua.IsType(-3, TYPES.STRING))
                {
                    throw new Exception("IsType returned false on STRING type");
                }

                if (!lua.IsType(-2, TYPES.BOOL))
                {
                    throw new Exception("IsType returned false on BOOL type");
                }

                if (!lua.IsType(-1, TYPES.NIL))
                {
                    throw new Exception("IsType returned false on NIL type");
                }

                lua.Pop(4);

                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
            {
                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);
        }
Пример #12
0
        public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context)
        {
            // create usertype identifier
            UserType_Id = lua.CreateMetaTable("Example_UserType");
            // set meta-methods
            // see http://www.tutorialspoint.com/lua/lua_metatables.htm for all meta-methods
            // also checkout https://www.lua.org/pil/13.html for explanation about meta-methods/tables

            // __index method
            // __index is called when you trying to index usertype ex:
            // MyCSString.Length
            // MyCSString:ToCharArray()
            // FindMetaTable("Example_UserType").ToCharArray(MyCSString)
            lua.PushManagedFunction((lua) =>
            {
                IntPtr ptr = lua.GetUserType(1, UserType_Id);
                if (ptr != IntPtr.Zero)
                {
                    string indexingname = lua.GetString(2);
                    switch (indexingname)
                    {
                    // pushing simple number
                    case "Length":
                        GCHandle gCHandle = GCHandle.FromIntPtr(ptr);
                        string csString   = (string)gCHandle.Target;
                        lua.PushNumber(csString.Length);
                        break;

                    // pushing function
                    case "ToCharArray":
                        lua.PushManagedFunction((lua) =>
                        {
                            IntPtr ptr        = lua.GetUserType(1, UserType_Id);
                            GCHandle gCHandle = GCHandle.FromIntPtr(ptr);
                            string csString   = (string)gCHandle.Target;
                            lua.CreateTable();
                            char[] charArray = csString.ToCharArray();
                            for (int i = 0; i < charArray.Length; i++)
                            {
                                lua.PushNumber(i);
                                lua.PushString(charArray[i].ToString());
                                lua.SetTable(-3);
                            }
                            return(1);
                        });
                        break;

                    case "Clone":
                        lua.PushManagedFunction((lua) =>
                        {
                            IntPtr ptr1        = lua.GetUserType(1, UserType_Id);
                            GCHandle gCHandle1 = GCHandle.FromIntPtr(ptr1);
                            string csString1   = (string)gCHandle1.Target;
                            string csString2   = (string)csString1.Clone();
                            GCHandle gCHandle2 = GCHandle.Alloc(csString2, GCHandleType.Weak);
                            IntPtr ptr2        = GCHandle.ToIntPtr(gCHandle2);
                            lua.PushUserType(ptr2, UserType_Id);
                            return(1);
                        });
                        break;

                    case "IndexOf":
                        lua.PushManagedFunction((lua) =>
                        {
                            IntPtr ptr        = lua.GetUserType(1, UserType_Id);
                            GCHandle gCHandle = GCHandle.FromIntPtr(ptr);
                            string csString   = (string)gCHandle.Target;

                            string toFind;
                            if (lua.IsType(2, TYPES.STRING))
                            {
                                toFind = lua.GetString(2);
                            }
                            else if (lua.IsType(2, UserType_Id))
                            {
                                IntPtr ptr2        = lua.GetUserType(2, UserType_Id);
                                GCHandle gCHandle2 = GCHandle.FromIntPtr(ptr2);
                                toFind             = (string)gCHandle2.Target;
                            }
                            else
                            {
                                return(0);
                            }

                            int indexOf = csString.IndexOf(toFind);
                            lua.PushNumber(indexOf);
                            return(1);
                        });
                        break;

                    case "Contains":
                        lua.PushManagedFunction((lua) =>
                        {
                            IntPtr ptr        = lua.GetUserType(1, UserType_Id);
                            GCHandle gCHandle = GCHandle.FromIntPtr(ptr);
                            string csString   = (string)gCHandle.Target;

                            string toFind;
                            if (lua.IsType(2, TYPES.STRING))
                            {
                                toFind = lua.GetString(2);
                            }
                            else if (lua.IsType(2, UserType_Id))
                            {
                                IntPtr ptr2        = lua.GetUserType(2, UserType_Id);
                                GCHandle gCHandle2 = GCHandle.FromIntPtr(ptr2);
                                toFind             = (string)gCHandle2.Target;
                            }
                            else
                            {
                                return(0);
                            }

                            bool contains = csString.Contains(toFind);
                            lua.PushBool(contains);
                            return(1);
                        });
                        break;

                    default:
                        lua.PushNil();
                        break;
                    }
                }
                else
                {
                    Console.WriteLine("nil passed to __index");
                    lua.PushNil();
                }
                // function will return 1 result
                return(1);
            });
            lua.SetField(-2, "__index");

            lua.PushManagedFunction((lua) =>
            {
                // 1 - stands for 1st passed to function argument
                IntPtr ptr = lua.GetUserType(1, UserType_Id);
                if (ptr != IntPtr.Zero)
                {
                    GCHandle gCHandle = GCHandle.FromIntPtr(ptr);
                    string csString   = (string)gCHandle.Target;
                    lua.PushString(csString);
                }
                else
                {
                    Console.WriteLine("nil passed to __tostring");
                    lua.PushNil();
                }
                return(1);
            });
            lua.SetField(-2, "__tostring");

            // equal (==) method
            lua.PushManagedFunction((lua) =>
            {
                IntPtr ptr1 = lua.GetUserType(1, UserType_Id);
                IntPtr ptr2 = lua.GetUserType(2, UserType_Id);
                // if we have same pointers, then objects are same
                if (ptr1 == ptr2)
                {
                    lua.PushBool(true);
                }
                // check if both pointers not zero
                else if (ptr1 != IntPtr.Zero && ptr2 != IntPtr.Zero)
                {
                    GCHandle gCHandle1 = GCHandle.FromIntPtr(ptr1);
                    GCHandle gCHandle2 = GCHandle.FromIntPtr(ptr2);
                    string csString1   = (string)gCHandle1.Target;
                    string csString2   = (string)gCHandle2.Target;
                    lua.PushBool(csString1 == csString2);
                }
                // some of pointers is Zero, we'll not compare them
                else
                {
                    lua.PushBool(false);
                }
                return(1);
            });
            lua.SetField(-2, "__eq");

            // Dispose() in lua
            lua.PushManagedFunction((lua) =>
            {
                // 1 - stands for 1st passed to function argument
                IntPtr ptr = lua.GetUserType(1, UserType_Id);
                if (ptr != IntPtr.Zero)
                {
                    GCHandle gCHandle = GCHandle.FromIntPtr(ptr);
                    string csString   = (string)gCHandle.Target;
                    Console.WriteLine($"csString ({csString}) is garbage collected");
                    gCHandle.Free();
                }
                else
                {
                    Console.WriteLine("nil passed to __gc");
                }
                return(0);
            });
            lua.SetField(-2, "__gc");
            lua.Pop();

            // now we need to somehow create it
            lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
            lua.PushManagedFunction((lua) =>
            {
                string csString   = lua.GetString(1);
                GCHandle gCHandle = GCHandle.Alloc(csString, GCHandleType.Weak);
                IntPtr ptr        = GCHandle.ToIntPtr(gCHandle);
                lua.PushUserType(ptr, UserType_Id);
                return(1);
            });
            lua.SetField(-2, "CreateCSString");
            lua.Pop();
        }
Пример #13
0
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _)
        {
            try
            {
                Random rand = new Random();

                int    curr_type;
                string curr_name;

                lua.PushNumber(rand.NextDouble());
                curr_type = lua.GetType(-1);
                curr_name = lua.GetTypeName(curr_type);
                if (curr_type != (int)TYPES.NUMBER || curr_name != "number")
                {
                    throw new CheckTypeException("number");
                }
                lua.Pop(1);


                lua.PushString(Guid.NewGuid().ToString());
                curr_type = lua.GetType(-1);
                curr_name = lua.GetTypeName(curr_type);
                if (curr_type != (int)TYPES.STRING || curr_name != "string")
                {
                    throw new CheckTypeException("string");
                }
                lua.Pop(1);


                lua.PushBool(true);
                curr_type = lua.GetType(-1);
                curr_name = lua.GetTypeName(curr_type);
                if (curr_type != (int)TYPES.BOOL || curr_name != "bool")
                {
                    throw new CheckTypeException("bool");
                }
                lua.Pop(1);


                lua.PushNil();
                curr_type = lua.GetType(-1);
                curr_name = lua.GetTypeName(curr_type);
                if (curr_type != (int)TYPES.NIL || curr_name != "nil")
                {
                    throw new CheckTypeException("nil");
                }
                lua.Pop(1);


                lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                curr_type = lua.GetType(-1);
                curr_name = lua.GetTypeName(curr_type);
                if (curr_type != (int)TYPES.TABLE || curr_name != "table")
                {
                    throw new CheckTypeException("table");
                }
                lua.Pop(1);


                lua.PushVector(new Vector3((float)rand.NextDouble()));
                curr_type = lua.GetType(-1);
                curr_name = lua.GetTypeName(curr_type);
                if (curr_type != (int)TYPES.Vector || curr_name != "vector")
                {
                    throw new CheckTypeException("vector");
                }
                lua.Pop(1);


                lua.PushAngle(new Vector3((float)rand.NextDouble()));
                curr_type = lua.GetType(-1);
                curr_name = lua.GetTypeName(curr_type);
                if (curr_type != (int)TYPES.ANGLE || curr_name != "angle")
                {
                    throw new CheckTypeException("angle");
                }
                lua.Pop(1);


                lua.PushManagedFunction(this.test_func);
                curr_type = lua.GetType(-1);
                curr_name = lua.GetTypeName(curr_type);
                if (curr_type != (int)TYPES.FUNCTION || curr_name != "function")
                {
                    throw new CheckTypeException("function");
                }
                lua.Pop(1);



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

            return(taskCompletion.Task);
        }
Пример #14
0
        void IModule.Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context)
        {
            if (is_serverside)
            {
                throw new Exception("Must be loaded from client");
            }

            string platformIdentifier = OperatingSystem.IsWindows() ? "win-x64" : "linux-x64";

            assembly_context.SetCustomNativeLibraryResolver((ctx, str) =>
            {
                if (str.Contains("sourcesdkc"))
                {
                    if (sourcesdkc == IntPtr.Zero)
                    {
                        Console.WriteLine("loading sourcesdkc");
                        sourcesdkc = NativeLibrary.Load($"./garrysmod/lua/bin/Modules/GetRenderTargetExample/runtimes/{platformIdentifier}/native/sourcesdkc");
                        Console.WriteLine($"loaded sourcesdkc: {sourcesdkc != IntPtr.Zero}");
                    }
                    return(sourcesdkc);
                }
                return(IntPtr.Zero);
            });

            if (interfaceh.Sys_LoadInterface("vguimatsurface", ISurface.VGUI_SURFACE_INTERFACE_VERSION, out _, out IntPtr isurfacePtr))
            {
                surface = new(isurfacePtr);
            }
            else
            {
                throw new Exception("failed loading vguimatsurface");
            }

            if (interfaceh.Sys_LoadInterface("materialsystem", IMaterialSystem.MATERIAL_SYSTEM_INTERFACE_VERSION, out _, out IntPtr materialSystemPtr))
            {
                materialSystem = new(materialSystemPtr);
            }
            else
            {
                Console.WriteLine("failed loading materialsystem");
            }

            lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
            lua.GetField(-1, "GetRenderTarget");
            lua.PushString("ExampleRTwithAlpha");
            lua.PushNumber(512);
            lua.PushNumber(512);
            lua.MCall(3, 1);
            rt = lua.GetUserType(-1, (int)TYPES.TEXTURE);
            lua.Pop();

            lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
            lua.GetField(-1, "CreateMaterial");
            lua.PushString("ExampleRTwithAlpha_Mat");
            lua.PushString("UnlitGeneric");
            lua.CreateTable();
            {
                lua.PushString("$basetexture");
                lua.PushString("ExampleRTwithAlpha");
                lua.SetTable(-3);

                lua.PushString("$translucent");
                lua.PushString("1");
                lua.SetTable(-3);
            }
            lua.MCall(3, 1);
            mat = lua.GetUserType(-1, (int)TYPES.MATERIAL);
            lua.Pop();

            lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
            lua.GetField(-1, "hook");
            lua.GetField(-1, "Add");
            lua.PushString("HUDPaint");
            lua.PushString("ExampleRTwithAlpha_Render");
            lua.PushManagedFunction(Render);
            lua.MCall(3, 0);
            lua.Pop();
        }
Пример #15
0
        public int Render(ILua lua)
        {
            //render.PushRenderTarget(textureRT)
            //cam.Start2D()
            // render.Clear(0, 0, 0, 0)

            lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
            lua.GetField(-1, "CurTime");
            lua.MCall(0, 1);
            int CurTime = (int)lua.GetNumber(-1);

            lua.Pop();

            // PushRenderTarget
            {
                lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                lua.GetField(-1, "render");
                lua.GetField(-1, "PushRenderTarget");
                lua.PushUserType(rt, (int)TYPES.TEXTURE);
                lua.MCall(1, 0);
                lua.Pop();
            }

            // Start 2D
            {
                lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                lua.GetField(-1, "cam");
                lua.GetField(-1, "Start2D");
                lua.MCall(0, 0);
                lua.Pop();
            }

            // Clear
            {
                lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                lua.GetField(-1, "render");
                lua.GetField(-1, "Clear");
                lua.PushNumber(0);
                lua.PushNumber(0);
                lua.PushNumber(0);
                lua.PushNumber(0);
                lua.MCall(4, 0);
                lua.Pop();
            }
            // Draw Rects
            {
                if (surface is not null)
                {
                    surface.DrawSetColor(255, 255, 255, 255);
                    surface.DrawFilledRect(20, (100 + (((int)Math.Sin(CurTime)) * 50)), 50, 50);
                    surface.DrawSetColor(255, 0, 0, 100);
                    surface.DrawFilledRect(120, (100 + (((int)Math.Sin(CurTime)) * 50)), 50, 50);
                }
                else
                {
                    lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                    lua.GetField(-1, "surface");
                    lua.GetField(-1, "SetDrawColor");
                    lua.PushNumber(255);
                    lua.PushNumber(255);
                    lua.PushNumber(255);
                    lua.PushNumber(255);
                    lua.MCall(4, 0);
                    lua.Pop(2);

                    lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                    lua.GetField(-1, "surface");
                    lua.GetField(-1, "DrawRect");
                    lua.PushNumber(20);
                    lua.PushNumber((100 + (((int)Math.Sin(CurTime)) * 50)));
                    lua.PushNumber(50);
                    lua.PushNumber(50);
                    lua.MCall(4, 0);
                    lua.Pop(2);

                    lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                    lua.GetField(-1, "surface");
                    lua.GetField(-1, "SetDrawColor");
                    lua.PushNumber(255);
                    lua.PushNumber(0);
                    lua.PushNumber(0);
                    lua.PushNumber(100);
                    lua.MCall(4, 0);
                    lua.Pop(2);

                    lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                    lua.GetField(-1, "surface");
                    lua.GetField(-1, "DrawRect");
                    lua.PushNumber(120);
                    lua.PushNumber((100 + (((int)Math.Sin(CurTime)) * 50)));
                    lua.PushNumber(50);
                    lua.PushNumber(50);
                    lua.MCall(4, 0);
                    lua.Pop(2);
                }
            }

            // End2D
            {
                lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                lua.GetField(-1, "cam");
                lua.GetField(-1, "End2D");
                lua.MCall(0, 0);
                lua.Pop();
            }

            // Pop
            {
                lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                lua.GetField(-1, "render");
                lua.GetField(-1, "PopRenderTarget");
                lua.MCall(0, 0);
                lua.Pop();
            }

            // Draw it on screen
            {
                if (surface is not null)
                {
                    surface.DrawSetColor(255, 255, 255, 255);
                }
                else
                {
                    lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                    lua.GetField(-1, "surface");
                    lua.GetField(-1, "SetDrawColor");
                    lua.PushNumber(255);
                    lua.PushNumber(255);
                    lua.PushNumber(255);
                    lua.PushNumber(255);
                    lua.MCall(4, 0);
                    lua.Pop();
                }

                lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                lua.GetField(-1, "surface");
                lua.GetField(-1, "SetMaterial");
                lua.PushUserType(mat, (int)TYPES.MATERIAL);
                lua.MCall(1, 0);
                lua.Pop();

                if (surface is not null)
                {
                    surface.DrawTexturedRect(50, 50, 512, 512);
                }
                else
                {
                    lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                    lua.GetField(-1, "surface");
                    lua.GetField(-1, "DrawTexturedRect");
                    lua.PushNumber(50);
                    lua.PushNumber(50);
                    lua.PushNumber(512);
                    lua.PushNumber(512);
                    lua.MCall(4, 0);
                    lua.Pop();
                }
            }

            return(0);
        }