Exemplo n.º 1
0
        private int removeelements(IntPtr L)
        {
            if (!IsXmlFileLoaded())
            {
                return(Melua.melua_error(L, "No XML file loaded."));
            }

            var selectors = new HashSet <string>();

            if (Melua.lua_isstring(L, -1))
            {
                var selector = Melua.luaL_checkstring(L, 1);
                Melua.lua_pop(L, 1);

                if (!IsXPathValid(selector))
                {
                    return(Melua.melua_error(L, "Invalid XPath."));
                }

                selectors.Add(selector);
            }
            else if (Melua.lua_istable(L, -1))
            {
                Melua.lua_pushnil(L);
                while (Melua.lua_next(L, -2) != 0)
                {
                    var selector = Melua.luaL_checkstring(L, -1);
                    Melua.lua_pop(L, 1);

                    if (!IsXPathValid(selector))
                    {
                        return(Melua.melua_error(L, "Invalid XPath."));
                    }

                    selectors.Add(selector);
                }
            }
            else
            {
                return(Melua.melua_error(L, "Invalid argument type '{0}'.", Melua.luaL_typename(L, -1)));
            }

            var modder = _loadedXmlModder;

            foreach (var selector in selectors)
            {
                _modPack.AddMod(new XmlElementRemover(modder, selector));
            }

            return(0);
        }
Exemplo n.º 2
0
        private int setattributes(IntPtr L)
        {
            if (!IsXmlFileLoaded())
            {
                return(Melua.melua_error(L, "No XML file loaded."));
            }

            var modder = _loadedXmlModder;

            var selector = Melua.luaL_checkstring(L, 1);

            if (!IsXPathValid(selector))
            {
                return(Melua.melua_error(L, "Invalid XPath."));
            }

            if (Melua.lua_isstring(L, 2) && Melua.lua_isstring(L, 3))
            {
                var attributeName  = Melua.lua_tostring(L, 2);
                var attributeValue = Melua.lua_tostring(L, 3);
                Melua.lua_pop(L, 2);

                _modPack.AddMod(new XmlAttributeSetter(modder, selector, attributeName, attributeValue));
            }
            else if (Melua.lua_istable(L, 2))
            {
                Melua.lua_pushnil(L);
                while (Melua.lua_next(L, -2) != 0)
                {
                    var attributeName  = Melua.luaL_checkstring(L, -2);
                    var attributeValue = Melua.luaL_checkstring(L, -1);
                    Melua.lua_pop(L, 1);

                    _modPack.AddMod(new XmlAttributeSetter(modder, selector, attributeName, attributeValue));
                }

                Melua.lua_pop(L, 1);
            }
            else
            {
                return(Melua.melua_error(L, "Invalid argument type."));
            }

            Melua.lua_pop(L, 1);

            return(0);
        }
Exemplo n.º 3
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);
        }