Пример #1
0
        /// <summary>
        /// Pushes the arguments onto the Lua stack.
        /// </summary>
        /// <param name="NL"></param>
        /// <param name="args"></param>
        private static void PushArguments(IntPtr NL, params object[] args)
        {
            foreach (var arg in args)
            {
                switch (arg)
                {
                case byte v: Melua.lua_pushinteger(NL, v); break;

                case bool v: Melua.lua_pushboolean(NL, v); break;

                case short v: Melua.lua_pushinteger(NL, v); break;

                case int v: Melua.lua_pushinteger(NL, v); break;

                case float v: Melua.lua_pushnumber(NL, v); break;

                case double v: Melua.lua_pushnumber(NL, v); break;

                case string v: Melua.lua_pushstring(NL, v); break;

                default:
                {
                    Log.Warning("ScriptManager.PushArguments: Invalid argument type '{0}', pushing 'int 0' instead.", arg.GetType().Name);
                    Melua.lua_pushinteger(NL, 0);
                    break;
                }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Sends dialog input message, showing a message and a text field,
        /// for the user to put in a string.
        /// </summary>
        /// <remarks>
        /// Parameters:
        /// - string message
        ///
        /// Result:
        /// The string put in by the user.
        /// Returns empty string on error.
        /// </remarks>
        /// <param name="L"></param>
        /// <returns></returns>
        private int input(IntPtr L)
        {
            // Check arguments and return empty string on error
            var argc = Melua.lua_gettop(L);

            if (argc == 0)
            {
                Log.Warning("input: No arguments.");
                Melua.lua_pushstring(L, "");
                return(1);
            }

            var conn = this.GetConnectionFromState(L);

            // Get message
            var msg = Melua.luaL_checkstring(L, 1);

            Melua.lua_pop(L, 1);

            this.HandleCustomCode(conn, ref msg);
            this.AttachNpcName(conn, ref msg);

            Send.ZC_DIALOG_STRINGINPUT(conn, msg);

            return(Melua.lua_yield(L, 1));
        }
Пример #3
0
        /// <summary>
        /// Returns table, containing information about the current date/time.
        /// </summary>
        /// <remarks>
        /// Result:
        /// {
        ///		integer year,     -- Current year
        ///		integer month,    -- Current month
        ///		integer day,      -- Current day
        ///		integer weekday,  -- Day of the week (0-6), starting on Sunday
        ///		integer yearday,  -- Day of the current year
        ///		integer hour,     -- Current hours (0-23)
        ///		integer min,      -- Current minutes (0-59)
        ///		integer sec,      -- Current seconds (0-59)
        ///		integer msec,     -- Current milliseconds (0-999)
        ///		boolean isdst,    -- Is Daylight Saving Time?
        ///		integer unixts,   -- Unix timestamp
        /// }
        /// </remarks>
        /// <param name="L"></param>
        /// <returns></returns>
        private int gettime(IntPtr L)
        {
            var now = DateTime.Now;

            // TODO: Could a general table generation like this be cached?

            Melua.lua_newtable(L);

            Melua.lua_pushstring(L, "year");
            Melua.lua_pushinteger(L, now.Year);
            Melua.lua_settable(L, -3);

            Melua.lua_pushstring(L, "month");
            Melua.lua_pushinteger(L, now.Month);
            Melua.lua_settable(L, -3);

            Melua.lua_pushstring(L, "day");
            Melua.lua_pushinteger(L, now.Day);
            Melua.lua_settable(L, -3);

            Melua.lua_pushstring(L, "weekday");
            Melua.lua_pushinteger(L, (int)now.DayOfWeek);
            Melua.lua_settable(L, -3);

            Melua.lua_pushstring(L, "yearday");
            Melua.lua_pushinteger(L, now.DayOfYear);
            Melua.lua_settable(L, -3);

            Melua.lua_pushstring(L, "hour");
            Melua.lua_pushinteger(L, now.Hour);
            Melua.lua_settable(L, -3);

            Melua.lua_pushstring(L, "min");
            Melua.lua_pushinteger(L, now.Minute);
            Melua.lua_settable(L, -3);

            Melua.lua_pushstring(L, "sec");
            Melua.lua_pushinteger(L, now.Second);
            Melua.lua_settable(L, -3);

            Melua.lua_pushstring(L, "msec");
            Melua.lua_pushinteger(L, now.Millisecond);
            Melua.lua_settable(L, -3);

            Melua.lua_pushstring(L, "isdst");
            Melua.lua_pushboolean(L, now.IsDaylightSavingTime());
            Melua.lua_settable(L, -3);

            Melua.lua_pushstring(L, "unixts");
            Melua.lua_pushinteger(L, (int)(now.ToUniversalTime().Subtract(UnixEpoch)).TotalSeconds);
            Melua.lua_settable(L, -3);

            return(1);
        }
Пример #4
0
        public void tonumber()
        {
            var L = Melua.luaL_newstate();

            Melua.lua_pushstring(L, "123");
            Assert.Equal(123, Melua.lua_tonumber(L, -1));

            Melua.lua_pushstring(L, "0x123");
            Assert.Equal(0x123, Melua.lua_tonumber(L, -1));

            Melua.lua_pushstring(L, "0x12AB34");
            Assert.Equal(0x12AB34, Melua.lua_tonumber(L, -1));
        }
Пример #5
0
        public void typename()
        {
            var L = Melua.luaL_newstate();

            Melua.lua_pushinteger(L, 123);
            Assert.Equal("number", Melua.luaL_typename(L, -1));

            Melua.lua_pushstring(L, "123");
            Assert.Equal("string", Melua.luaL_typename(L, -1));

            Melua.lua_newtable(L);
            Assert.Equal("table", Melua.luaL_typename(L, -1));
        }
Пример #6
0
        public void type()
        {
            var L = Melua.luaL_newstate();

            Melua.lua_pushinteger(L, 123);
            Assert.Equal(Melua.LUA_TNUMBER, Melua.lua_type(L, -1));

            Melua.lua_pushstring(L, "123");
            Assert.Equal(Melua.LUA_TSTRING, Melua.lua_type(L, -1));

            Melua.lua_newtable(L);
            Assert.Equal(Melua.LUA_TTABLE, Melua.lua_type(L, -1));
        }
Пример #7
0
        /// <summary>
        /// Returns a table with information about the player.
        /// </summary>
        /// <remarks>
        /// Result:
        /// {
        ///		string  name,     -- Character's name
        ///		string  teamName, -- Character's team name
        ///		integer gender,   -- Character's gender
        ///		integer level,    -- Character's level
        ///		integer hp,       -- Character's HP
        ///		integer maxHp,    -- Character's max HP
        ///		integer sp,       -- Character's SP
        ///		integer maxSp,    -- Character's max SP
        ///		integer stamina,  -- Character's stamina
        ///		integer hair,     -- Character's hair
        /// }
        /// </remarks>
        /// <param name="L"></param>
        /// <returns></returns>
        private int getpc(IntPtr L)
        {
            var conn      = this.GetConnectionFromState(L);
            var character = conn.SelectedCharacter;

            Melua.lua_newtable(L);

            Melua.lua_pushstring(L, "name");
            Melua.lua_pushstring(L, character.Name);
            Melua.lua_settable(L, -3);

            Melua.lua_pushstring(L, "teamName");
            Melua.lua_pushstring(L, character.TeamName);
            Melua.lua_settable(L, -3);

            Melua.lua_pushstring(L, "gender");
            Melua.lua_pushinteger(L, (int)character.Gender);
            Melua.lua_settable(L, -3);

            Melua.lua_pushstring(L, "level");
            Melua.lua_pushinteger(L, character.Level);
            Melua.lua_settable(L, -3);

            Melua.lua_pushstring(L, "hp");
            Melua.lua_pushinteger(L, character.Hp);
            Melua.lua_settable(L, -3);

            Melua.lua_pushstring(L, "maxHp");
            Melua.lua_pushinteger(L, character.MaxHp);
            Melua.lua_settable(L, -3);

            Melua.lua_pushstring(L, "sp");
            Melua.lua_pushinteger(L, character.Sp);
            Melua.lua_settable(L, -3);

            Melua.lua_pushstring(L, "maxSp");
            Melua.lua_pushinteger(L, character.MaxSp);
            Melua.lua_settable(L, -3);

            Melua.lua_pushstring(L, "stamina");
            Melua.lua_pushinteger(L, character.Stamina);
            Melua.lua_settable(L, -3);

            Melua.lua_pushstring(L, "hair");
            Melua.lua_pushinteger(L, character.Hair);
            Melua.lua_settable(L, -3);

            return(1);
        }
Пример #8
0
        public void tostring()
        {
            var L = Melua.luaL_newstate();

            Melua.lua_pushinteger(L, 123);
            var str = Melua.lua_tostring(L, -1);

            Assert.Equal("123", str);

            Melua.lua_newtable(L);
            Assert.Equal(null, Melua.lua_tostring(L, -1));

            Melua.lua_pushstring(L, "foobar");
            Assert.Equal("foobar", Melua.lua_tostring(L, -1));
        }
Пример #9
0
        /// <summary>
        /// Returns a table with information about the NPC.
        /// </summary>
        /// <remarks>
        /// Result:
        /// {
        ///		string name,        -- NPCs name
        ///		string dialogName,  -- Name of NPCs main dialog function
        /// }
        /// </remarks>
        /// <param name="L"></param>
        /// <returns></returns>
        private int getnpc(IntPtr L)
        {
            var conn      = this.GetConnectionFromState(L);
            var character = conn.ScriptState.CurrentNpc;

            Melua.lua_newtable(L);

            Melua.lua_pushstring(L, "name");
            Melua.lua_pushstring(L, character.Name);
            Melua.lua_settable(L, -3);

            Melua.lua_pushstring(L, "dialogName");
            Melua.lua_pushstring(L, character.DialogName);
            Melua.lua_settable(L, -3);

            return(1);
        }
Пример #10
0
        /// <summary>
        /// Warps player to given location.
        /// </summary>
        /// <remarks>
        /// Parameters:
        /// - string mapName
        /// - number x
        /// - number y
        /// - number z
        /// </remarks>
        /// <param name="L"></param>
        /// <returns></returns>
        private int warp(IntPtr L)
        {
            var conn = this.GetConnectionFromState(L);

            var mapName = Melua.luaL_checkstring(L, 1);
            var x       = (float)Melua.luaL_checknumber(L, 2);
            var y       = (float)Melua.luaL_checknumber(L, 3);
            var z       = (float)Melua.luaL_checknumber(L, 4);

            Melua.lua_pop(L, 4);

            try
            {
                conn.SelectedCharacter.Warp(mapName, x, y, z);
            }
            catch (ArgumentException ex)
            {
                Melua.lua_pushstring(L, ex.Message);
                Melua.lua_error(L);
            }

            return(0);
        }
Пример #11
0
        /// <summary>
        /// Resumes script after yielding.
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="argument"></param>
        public void Resume(ChannelConnection conn, params object[] arguments)
        {
            if (conn.ScriptState.LuaThread == null)
            {
                Send.ZC_DIALOG_CLOSE(conn);
                Log.Warning("ScriptManager: Resume on empty ScriptState from user '{0}'.", conn.Account.Name);
                return;
            }

            var NL   = conn.ScriptState.LuaThread.L;
            var argc = (arguments != null ? arguments.Length : 0);

            // Reset current shop in case we came from one.
            conn.ScriptState.CurrentShop = null;

            if (argc != 0)
            {
                foreach (var arg in arguments)
                {
                    if (arg is byte)
                    {
                        Melua.lua_pushinteger(NL, (byte)arg);
                    }
                    else if (arg is short)
                    {
                        Melua.lua_pushinteger(NL, (short)arg);
                    }
                    else if (arg is int)
                    {
                        Melua.lua_pushinteger(NL, (int)arg);
                    }
                    else if (arg is string)
                    {
                        Melua.lua_pushstring(NL, (string)arg);
                    }
                    else
                    {
                        Log.Warning("ScriptManager.Resume: Invalid argument type '{0}'.", arg.GetType().Name);
                        Melua.lua_pushinteger(NL, 0);
                    }
                }

                // If arguments were passed, we can assume we're coming from
                // a selection handler, which's windows don't disappear when
                // sending the next message. So let's close it before
                // continuing.
                Send.ZC_DIALOG_CLOSE(conn);
            }

            var result = Melua.lua_resume(NL, argc);

            // Log error if result is not success or yield
            if (result != 0 && result != Melua.LUA_YIELD)
            {
                Log.Error("ScriptManager.Call: Error while resuming script for {0}.\n{1}", conn.Account.Name, Melua.lua_tostring(NL, -1));
                result = 0;                 // Set to 0 to close dialog on error
            }

            // Close dialog if end of function was reached
            if (result == 0)
            {
                // Only close from here if the end was reached after an
                // argument-less resume, since close is already called
                // from the argument handling to get rid of the selection
                // dialog.
                if (argc == 0)
                {
                    Send.ZC_DIALOG_CLOSE(conn);
                }

                conn.ScriptState.Reset();
            }
        }
Пример #12
0
        /// <summary>
        /// Gets or sets a scripting variable.
        /// </summary>
        /// <remarks>
        /// Scripting variables are separate from Lua variables and exist
        /// across script and playing sessions. How the variable is saved
        /// depends on the used prefix.
        ///
        /// Variable names may contain the following characters, apart from
        /// the prefixes, and must start with a character:
        /// abcdefghijklmnopqrstuvwxyz0123456789_
        ///
        /// Prefixes:
        /// ""   - Permanent variable attached to the character.
        /// "@"  - Temporary variable attached to the character.
        /// "#"  - Permanent variable attached to the account.
        /// "$"  - Permanent global variable.
        /// "$@" - Temporary global variable.
        ///
        /// Parameters:
        /// - string variableName
        /// - (optional) T value
        ///
        /// Result:
        /// - T value
        /// </remarks>
        /// <param name="L"></param>
        /// <returns></returns>
        private int var(IntPtr L)
        {
            var conn      = this.GetConnectionFromState(L);
            var character = conn.SelectedCharacter;

            // Get parameters
            var argc = Melua.lua_gettop(L);
            var name = Melua.luaL_checkstring(L, 1).Trim();

            object value = null;

            if (argc == 2)
            {
                if (Melua.lua_isnumber(L, 2))
                {
                    value = Melua.lua_tonumber(L, 2);
                }
                else if (Melua.lua_isstring(L, 2))
                {
                    value = Melua.lua_tostring(L, 2);
                }
                else if (Melua.lua_isboolean(L, 2))
                {
                    value = Melua.lua_toboolean(L, 2);
                }
                else
                {
                    return(Melua.melua_error(L, "Unsupported variable type."));
                }
            }

            Melua.lua_pop(L, argc);

            // Get variable manager and trim name
            VariableManager vars;

            if (name.StartsWith("$@"))
            {
                vars = this.Variables.Temp;
                name = name.Substring(2);
            }
            else if (name.StartsWith("$"))
            {
                vars = this.Variables.Perm;
                name = name.Substring(1);
            }
            else if (name.StartsWith("#"))
            {
                vars = conn.Account.Variables.Perm;
                name = name.Substring(1);
            }
            else if (name.StartsWith("@"))
            {
                vars = character.Variables.Temp;
                name = name.Substring(1);
            }
            else
            {
                vars = character.Variables.Perm;
            }

            // Check name syntax, if we want to add more prefixes later on,
            // we can't have special characters in names.
            if (!VarNameCheck.IsMatch(name))
            {
                return(Melua.melua_error(L, "Invalid variable name."));
            }

            // Update or get value
            if (value == null)
            {
                value = vars[name];
            }
            else
            {
                vars[name] = value;
            }

            // Push return value
            if (value == null)
            {
                Melua.lua_pushnil(L);
            }
            else if (value is string)
            {
                Melua.lua_pushstring(L, (string)value);
            }
            else if (value is double)
            {
                Melua.lua_pushnumber(L, (double)value);
            }
            else if (value is float)
            {
                Melua.lua_pushnumber(L, (float)value);
            }
            else if (value is int)
            {
                Melua.lua_pushinteger(L, (int)value);
            }
            else if (value is bool)
            {
                Melua.lua_pushboolean(L, (bool)value);
            }
            else
            {
                return(Melua.melua_error(L, "Unsupported variable type '{0}'.", value.GetType().Name));
            }

            return(1);
        }
Пример #13
0
        public void userdata()
        {
            var L = Melua.luaL_newstate();

            Melua.melua_opensafelibs(L);

            var n1 = 0;

            // Ctor
            Melua.luaL_register(L, "Test", new[]
            {
                new MeluaLib.Melua.LuaLib("new", NL =>
                {
                    var test = new UserDataTest()
                    {
                        N1 = 1234
                    };
                    var size = Marshal.SizeOf(test);

                    var ptr = Melua.lua_newuserdata(L, size);
                    Melua.luaL_getmetatable(L, "Melua.Test");
                    Melua.lua_setmetatable(L, -2);

                    Marshal.StructureToPtr(test, ptr, true);

                    return(1);
                })
            });

            // Meta table for test userdata type
            Melua.luaL_newmetatable(L, "Melua.Test");
            Melua.lua_pushstring(L, "__index");
            Melua.lua_pushvalue(L, -2);
            Melua.lua_settable(L, -3);

            Melua.luaL_register(L, null, new[]
            {
                new MeluaLib.Melua.LuaLib("setN1", _ =>
                {
                    var ptr = Melua.luaL_checkudata(L, 1, "Melua.Test");
                    var val = Melua.luaL_checkinteger(L, 2);

                    // Either marshal back and forth or use unsafe
                    var test = (UserDataTest)Marshal.PtrToStructure(ptr, typeof(UserDataTest));
                    test.N1  = val;
                    Marshal.StructureToPtr(test, ptr, true);

                    //unsafe
                    //{
                    //	var test = (UserDataTest*)ptr;
                    //	test->N1 = val;
                    //}

                    return(0);
                }),
                new MeluaLib.Melua.LuaLib("getN1", _ =>
                {
                    var ptr  = Melua.luaL_checkudata(L, 1, "Melua.Test");
                    var test = (UserDataTest)Marshal.PtrToStructure(ptr, typeof(UserDataTest));

                    Melua.lua_pushinteger(L, test.N1);

                    return(1);
                })
            });

            // Test method
            Melua.melua_register(L, "testgetn1", _ =>
            {
                n1 = Melua.lua_tointeger(L, -1);

                return(0);
            });

            var result = Melua.luaL_dostring(L, @"
local t = Test.new()
t:setN1(5678)
testgetn1(t:getN1())
");

            if (result != 0)
            {
                throw new Exception(Melua.lua_tostring(L, -1));
            }

            Assert.Equal(n1, 5678);
        }