Ejemplo n.º 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;
                }
                }
            }
        }
Ejemplo n.º 2
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);
        }