示例#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>
        /// 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);
        }
示例#3
0
        /// Returns true if character has items with the given id.
        /// </summary>
        /// <remarks>
        /// Parameters:
        /// - int itemId
        ///
        /// Result:
        /// - bool hasItem?
        /// </remarks>
        /// <param name="L"></param>
        /// <returns></returns>
        private int hasitem(IntPtr L)
        {
            var conn      = this.GetConnectionFromState(L);
            var character = conn.SelectedCharacter;

            var itemId = Melua.luaL_checkinteger(L, 1);

            Melua.lua_pop(L, 1);

            var result = character.Inventory.HasItem(itemId);

            Melua.lua_pushboolean(L, result);

            return(1);
        }
示例#4
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);
        }