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);
        }
Beispiel #2
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);
        }
        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);
        }
Beispiel #4
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();
        }
Beispiel #5
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);
        }