Beispiel #1
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 _)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

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

                lua.PushString(random_string);

                int reference = lua.ReferenceCreate();

                if (lua.GetType(-1) == (int)TYPES.STRING && lua.GetString(-1) == random_string)
                {
                    throw new ReferenceTestException("String wasn't poped from the stack");
                }

                lua.ReferencePush(reference);

                if (lua.GetType(-1) == (int)TYPES.STRING && lua.GetString(-1) != random_string)
                {
                    throw new ReferenceTestException("Reference wasn't pushed to the stack");
                }

                lua.Pop(1);

                lua.ReferenceFree(reference);

                lua.ReferencePush(reference);

                if (lua.GetType(-1) == (int)TYPES.STRING && lua.GetString(-1) == random_string)
                {
                    throw new ReferenceTestException("Reference wasn't freed");
                }

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

            return(taskCompletion.Task);
        }
        int SpawnFunc(ILua lua)
        {
            try
            {
                lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                lua.GetField(-1, "ents");
                lua.GetField(-1, "Create");
                lua.PushString("prop_physics");
                lua.MCall(1, 1);

                if (lua.GetType(-1) != (int)TYPES.ENTITY)
                {
                    throw new SpawnEntityException("ents.Create returned a value which type is not Entity");
                }

                lua.GetField(-1, "SetModel");
                lua.Push(-2);
                lua.PushString("models/props_c17/oildrum001.mdl");
                lua.MCall(2, 0);

                lua.GetField(-1, "SetPos");
                lua.Push(-2);
                lua.PushVector(new System.Numerics.Vector3(0));
                lua.MCall(2, 0);

                lua.GetField(-1, "Spawn");
                lua.Push(-2);
                lua.MCall(1, 0);

                lua.GetField(-1, "IsValid");
                lua.Push(-2);
                lua.MCall(1, 1);
                if (!lua.GetBool(-1))
                {
                    throw new SpawnEntityException("Entity is not valid");
                }

                lua.Pop(lua.Top());

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

            return(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);
        }
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                this.lua_extructor = lua_extructor;

                // Create new type
                this.type_id = lua.CreateMetaTable("SetTableAndRawSetTestType");
                lua.PushManagedFunction(this.newIndexImpl);
                lua.SetField(-2, "__newindex");
                lua.Pop(1);

                // Create test table
                lua.CreateTable();
                lua.PushString(random1);
                lua.SetField(-2, "Val");
                lua.PushMetaTable(this.type_id);
                lua.SetMetaTable(-2);

                // Test SetTable
                lua.PushString("Val");
                lua.PushString(random1 + random1);
                lua.SetTable(-3);
                lua.GetField(-1, "Val");
                string received_string = lua.GetString(-1);
                lua.Pop(1);
                if (received_string != random1 + random1)
                {
                    throw new Exception("SetTable didn't set a value for an existing key");
                }

                lua.PushString("ArbitraryKey");
                lua.PushString("ArbitraryString");
                lua.SetTable(-3);
                lua.GetField(-1, "ArbitraryKey");
                int received_type = lua.GetType(-1);
                lua.Pop(1);
                if (received_type != (int)TYPES.NIL)
                {
                    throw new Exception("SetTable ignored overriden __newindex");
                }

                lua.PushString("Val2");
                lua.PushString(random2);
                lua.RawSet(-3);
                lua.GetField(-1, "Val2");
                string received_string2 = lua.GetString(-1);
                lua.Pop(1);
                if (received_string2 != random2)
                {
                    throw new Exception("RawSet didn't set a value to a key");
                }

                lua.Pop(1);

                lua.PushMetaTable(type_id);
                lua.PushNil();
                lua.SetField(-2, "__newindex");
                lua.Pop(1);

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

            return(taskCompletion.Task);
        }
Beispiel #6
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);
        }
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                this.lua_extructor = lua_extructor;

                // Create new metatable and populate it
                this.TypeId = lua.CreateMetaTable("GetTableAndRawGetMetaTable");
                lua.PushManagedFunction(this.indexDelegate);
                lua.SetField(-2, "__index");
                lua.Pop(1);

                // Create a test table
                lua.CreateTable();
                lua.PushString(this.RandomString2);
                lua.SetField(-2, "TestVal");
                lua.PushMetaTable(this.TypeId);
                lua.SetMetaTable(-2);

                // Test GetTable
                lua.PushString("TestVal");
                lua.GetTable(-2);
                string receivedString1 = lua.GetString(-1);
                if (receivedString1 != this.RandomString2)
                {
                    throw new Exception("GetTable returned invalid string on existing key");
                }
                lua.Pop(1);

                lua.PushString("ArbitraryString");
                lua.GetTable(-2);
                string receivedString11 = lua.GetString(-1);
                if (receivedString11 != this.RandomString1)
                {
                    throw new Exception("GetTable returned invalid string on non-existing key");
                }
                lua.Pop(1);

                // Test RawGet
                lua.PushString("TestVal");
                lua.RawGet(-2);
                string receivedString2 = lua.GetString(-1);
                if (receivedString2 != this.RandomString2)
                {
                    throw new Exception("RawGet returned invalid string on existing key");
                }
                lua.Pop(1);

                lua.PushString("ArbitraryString");
                lua.RawGet(-2);
                int received_type = lua.GetType(-1);
                if (received_type != (int)TYPES.NIL)
                {
                    throw new Exception("RawGet didn't return NIL on non-existing key");
                }
                lua.Pop(1);

                lua.Pop(lua.Top());

                lua.PushMetaTable(TypeId);
                lua.PushNil();
                lua.SetField(-2, "__index");
                lua.Pop(1);

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

            return(taskCompletion.Task);
        }