Ejemplo n.º 1
0
        internal override T Query <T>(TagQuery query, bool bypassCheck)
        {
            TagQueryToken token = null;

            if (!bypassCheck)
            {
                token = query.Next();

                if (token != null && !token.Name.Equals(Name))
                {
                    return(null);
                }
            }

            TagQueryToken nextToken = query.Peek();

            if (nextToken != null)
            {
                NbtTag nextTag = Get(nextToken.Name);
                if (nextTag == null)
                {
                    return(null);
                }

                return(nextTag.Query <T>(query));
            }

            return((T)((NbtTag)this));
        }
Ejemplo n.º 2
0
        internal override void SetQuery <T>(TagQuery query, T val, bool bypassCheck)
        {
            TagQueryToken token = null;

            if (!bypassCheck)
            {
                token = query.Next();

                if (token != null && !token.Name.Equals(Name))
                {
                    return;
                }
            }

            TagQueryToken nextToken = query.Peek();

            if (nextToken != null)
            {
                NbtTag nextTag = Get(nextToken.Name);
                if (nextTag == null)
                {
                    return;
                }

                nextTag.SetQuery <T>(query, val, false);
                Set(nextToken.Name, nextTag);
                return;
            }
        }
Ejemplo n.º 3
0
        internal override T Query <T>(TagQuery query, bool bypassCheck)
        {
            TagQueryToken token = query.Next();

            if (!bypassCheck)
            {
                if (token != null && !token.Name.Equals(Name))
                {
                    return(null);
                }
            }

            var nextToken = query.Peek();

            if (nextToken != null)
            {
                // Make sure this token is an integer because NbtLists don't have
                // named tag items
                int tagIndex;
                if (!int.TryParse(nextToken.Name, out tagIndex))
                {
                    throw new NbtQueryException(
                              string.Format("Attempt to query by name on a list tag that doesn't support names. ({0})",
                                            Name));
                }

                var indexedTag = Get(tagIndex);
                if (indexedTag == null)
                {
                    return(null);
                }

                if (query.TokensLeft() > 1)
                {
                    // Pop the index token so the current token is the next
                    // named token to continue the query
                    query.Next();

                    // Bypass the name check because the tag won't have one
                    return(indexedTag.Query <T>(query, true));
                }

                return((T)indexedTag);
            }

            return((T)((NbtTag)this));
        }
Ejemplo n.º 4
0
        public void MovingBetweenTokens()
        {
            var           query = new TagQuery();
            TagQueryToken token;

            query.SetQuery("/first/second/third/fourth/fifth/sixth");
            Assert.AreEqual(6, query.Count());
            Assert.AreEqual(6, query.TokensLeft());
            token = query.Previous();
            Assert.IsNull(token);
            Assert.AreEqual(6, query.TokensLeft());
            token = query.Next();
            Assert.AreEqual("first", token.Name);
            Assert.AreEqual(5, query.TokensLeft());
            token = query.Next();
            Assert.AreEqual("second", token.Name);
            Assert.AreEqual(4, query.TokensLeft());
            token = query.Next();
            Assert.AreEqual("third", token.Name);
            Assert.AreEqual(3, query.TokensLeft());
            token = query.Previous();
            Assert.AreEqual("second", token.Name);
            Assert.AreEqual(4, query.TokensLeft());
            token = query.Next();
            Assert.AreEqual("third", token.Name);
            Assert.AreEqual(3, query.TokensLeft());
            token = query.Peek();
            Assert.AreEqual("fourth", token.Name);
            Assert.AreEqual(3, query.TokensLeft());
            token = query.Next();
            Assert.AreEqual("fourth", token.Name);
            Assert.AreEqual(2, query.TokensLeft());
            token = query.Next();
            Assert.AreEqual("fifth", token.Name);
            Assert.AreEqual(1, query.TokensLeft());
            token = query.Next();
            Assert.AreEqual("sixth", token.Name);
            Assert.AreEqual(0, query.TokensLeft());
            token = query.Next();
            Assert.IsNull(token);
            Assert.AreEqual(0, query.TokensLeft());
            token = query.Previous();
            Assert.AreEqual("fifth", token.Name);
            Assert.AreEqual(1, query.TokensLeft());
            query.MoveFirst();
            Assert.AreEqual(6, query.TokensLeft());
            token = query.Next();
            Assert.AreEqual("first", token.Name);
            Assert.AreEqual(5, query.TokensLeft());
        }
Ejemplo n.º 5
0
        internal override void SetQuery <T>(TagQuery query, T val, bool bypassCheck)
        {
            if (bypassCheck)
            {
                return;
            }

            TagQueryToken token = query.Next();

            if (token.Name.Equals(Name))
            {
                if (query.Peek() != null)
                {
                    throw new LibNbt.Exceptions.NbtQueryException(string.Format("Attempt through non list type tag: {0}", Name));
                }
                Value = (string)Convert.ChangeType(val, typeof(string));
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Queries the tag to easily find a tag in a structure.
        /// </summary>
        /// <typeparam name="T">Type of the tag to return.</typeparam>
        /// <param name="query">Tokenized query</param>
        /// <param name="bypassCheck">Bypass the name check when querying non-named queries.
        /// NbtList elements are an example.</param>
        /// <returns>The tag that was queried for.</returns>
        internal virtual T Query <T>(TagQuery query, bool bypassCheck) where T : NbtTag
        {
            if (bypassCheck)
            {
                return((T)this);
            }

            TagQueryToken token = query.Next();

            if (token.Name.Equals(Name))
            {
                if (query.Peek() != null)
                {
                    throw new NbtQueryException(string.Format("Attempt through non list type tag: {0}", Name));
                }

                return((T)this);
            }

            return(null);
        }
Ejemplo n.º 7
0
        public void Tokenization()
        {
            var           query = new TagQuery();
            TagQueryToken token;

            // Expected: first
            query = new TagQuery("/first");
            Assert.AreEqual(1, query.Count());
            Assert.AreEqual(1, query.TokensLeft());
            token = query.Next();
            Assert.AreEqual("first", token.Name);
            Assert.AreSame(query, token.Query);
            Assert.AreEqual(0, query.TokensLeft());
            token = query.Next();
            Assert.IsNull(token);
            Assert.AreEqual(0, query.TokensLeft());

            // Expected: first
            query.SetQuery("/first");
            Assert.AreEqual(1, query.Count());
            Assert.AreEqual(1, query.TokensLeft());
            token = query.Next();
            Assert.AreEqual("first", token.Name);
            Assert.AreSame(query, token.Query);
            Assert.AreEqual(0, query.TokensLeft());
            token = query.Next();
            Assert.IsNull(token);
            Assert.AreEqual(0, query.TokensLeft());

            // Expected: first, second, third
            query.SetQuery("/first/second/third");
            Assert.AreEqual(3, query.Count());
            Assert.AreEqual(3, query.TokensLeft());
            token = query.Next();
            Assert.AreEqual("first", token.Name);
            Assert.AreSame(query, token.Query);
            Assert.AreEqual(2, query.TokensLeft());
            token = query.Next();
            Assert.AreEqual("second", token.Name);
            Assert.AreSame(query, token.Query);
            Assert.AreEqual(1, query.TokensLeft());
            token = query.Next();
            Assert.AreEqual("third", token.Name);
            Assert.AreSame(query, token.Query);
            Assert.AreEqual(0, query.TokensLeft());
            token = query.Next();
            Assert.IsNull(token);
            Assert.AreEqual(0, query.TokensLeft());

            // Expected: first/slash, second\backslash, third!, extended/ÅÄÖ
            query.SetQuery(@"/first\/slash/second\\backslash/third!/extended\/ÅÄÖ");
            Assert.AreEqual(4, query.Count());
            Assert.AreEqual(4, query.TokensLeft());
            token = query.Next();
            Assert.AreEqual("first/slash", token.Name);
            Assert.AreSame(query, token.Query);
            Assert.AreEqual(3, query.TokensLeft());
            token = query.Next();
            Assert.AreEqual(@"second\backslash", token.Name);
            Assert.AreSame(query, token.Query);
            Assert.AreEqual(2, query.TokensLeft());
            token = query.Next();
            Assert.AreEqual("third!", token.Name);
            Assert.AreSame(query, token.Query);
            Assert.AreEqual(1, query.TokensLeft());
            token = query.Next();
            Assert.AreEqual("extended/ÅÄÖ", token.Name);
            Assert.AreSame(query, token.Query);
            Assert.AreEqual(0, query.TokensLeft());
            token = query.Next();
            Assert.IsNull(token);
            Assert.AreEqual(0, query.TokensLeft());
        }