Example #1
0
        private static void SerializeList(TagNodeList tag, StringBuilder str, int level, bool singleLine = false)
        {
            if (tag.Count == 0)
            {
                str.Append("[ ]");
                return;
            }

            if (!singleLine)
            {
                str.AppendLine();
            }

            AddLine(str, "[", level, singleLine);

            bool first = true;

            foreach (TagNode item in tag)
            {
                if (!first)
                {
                    str.Append(",");
                    str.AppendLine();
                }

                Serialize(item, level + 1, str, true, singleLine);

                first = false;
            }
            if (!singleLine)
            {
                str.AppendLine();
            }
            Add(str, "]", level, singleLine);
        }
Example #2
0
        private static void SerializeList(TagNodeList tag, StringBuilder str, int level)
        {
            if (tag.Count == 0)
            {
                str.Append("[ ]");
                return;
            }

            str.AppendLine();
            AddLine(str, "[", level);

            IEnumerator <TagNode> en = tag.GetEnumerator();
            bool first = true;

            while (en.MoveNext())
            {
                if (!first)
                {
                    str.Append(",");
                }

                TagNode item = en.Current;

                if (item.GetTagType() == TagType.TAG_COMPOUND)
                {
                    SerializeCompound(item as TagNodeCompound, str, level + 1);
                }
                else if (item.GetTagType() == TagType.TAG_LIST)
                {
                    SerializeList(item as TagNodeList, str, level + 1);
                }
                else
                {
                    if (!first)
                    {
                        str.AppendLine();
                    }
                    Indent(str, level + 1);
                    if (item.GetTagType() == TagType.TAG_INT_ARRAY)
                    {
                        SerializeIntArray(item as TagNodeIntArray, str, level);
                    }
                    else if (item.GetTagType() == TagType.TAG_BYTE_ARRAY)
                    {
                        SerializeByteArray(item as TagNodeByteArray, str, level);
                    }
                    else
                    {
                        SerializeScaler(item, str);
                    }
                }


                first = false;
            }

            str.AppendLine();
            Add(str, "]", level);
        }
Example #3
0
        /// <summary>
        /// Makes a deep copy of the node.
        /// </summary>
        /// <returns>A new list node containing new subnodes representing the same data.</returns>
        public override TagNode Copy()
        {
            TagNodeList list = new TagNodeList(_type);

            foreach (TagNode item in _items)
            {
                list.Add(item.Copy());
            }
            return(list);
        }
Example #4
0
        /// <summary>
        /// Constructs a default <see cref="TagNodeList"/> satisfying the constraints of this node.
        /// </summary>
        /// <returns>A <see cref="TagNodeList"/> with a sensible default value.  If a length is specified, default child <see cref="TagNode"/> objects of the necessary type will be created and added to the <see cref="TagNodeList"/>.</returns>
        public override TagNode BuildDefaultTree()
        {
            if (_length == 0)
            {
                return(new TagNodeList(_type));
            }

            TagNodeList list = new TagNodeList(_type);

            for (int i = 0; i < _length; i++)
            {
                list.Add(_subschema.BuildDefaultTree());
            }

            return(list);
        }
        private void WriteList(TagNodeList val)
        {
            byte[] lenBytes = BitConverter.GetBytes(val.Count);

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(lenBytes);
            }

            _stream.WriteByte((byte)val.ValueType);
            _stream.Write(lenBytes, 0, 4);

            foreach (TagNode v in val)
            {
                WriteValue(v);
            }
        }
Example #6
0
        private bool VerifyList(TagNode tag, SchemaNodeList schema)
        {
            TagNodeList ltag = tag as TagNodeList;

            if (ltag == null)
            {
                if (!OnInvalidTagType(new TagEventArgs(schema, tag)))
                {
                    return(false);
                }
            }
            if (ltag.Count > 0 && ltag.ValueType != schema.Type)
            {
                if (!OnInvalidTagValue(new TagEventArgs(schema, tag)))
                {
                    return(false);
                }
            }
            if (schema.Length > 0 && ltag.Count != schema.Length)
            {
                if (!OnInvalidTagValue(new TagEventArgs(schema, tag)))
                {
                    return(false);
                }
            }

            // Patch up empty lists
            //if (schema.Length == 0) {
            //    tag = new NBT_List(schema.Type);
            //}

            bool pass = true;

            // If a subschema is set, test all items in list against it

            if (schema.SubSchema != null)
            {
                foreach (TagNode v in ltag)
                {
                    pass = Verify(tag, v, schema.SubSchema) && pass;
                }
            }

            return(pass);
        }
Example #7
0
        public override bool Verify(NbtVerifier verifier, TagNode tag)
        {
            TagNodeList ltag = tag as TagNodeList;

            if (ltag == null)
            {
                if (!verifier.OnInvalidTagType(new TagEventArgs(this, tag)))
                {
                    return(false);
                }
            }
            if (ltag.Count > 0 && ltag.ValueType != Type)
            {
                if (!verifier.OnInvalidTagValue(new TagEventArgs(this, tag)))
                {
                    return(false);
                }
            }
            if (Length > 0 && ltag.Count != Length)
            {
                if (!verifier.OnInvalidTagValue(new TagEventArgs(this, tag)))
                {
                    return(false);
                }
            }

            // Patch up empty lists
            //if (schema.Length == 0) {
            //    tag = new NBT_List(schema.Type);
            //}

            bool pass = true;

            // If a subschema is set, test all items in list against it

            if (SubSchema != null)
            {
                foreach (TagNode v in ltag)
                {
                    pass = SubSchema.Verify(verifier, v) && pass;
                }
            }

            return(pass);
        }
Example #8
0
        private TagNode ReadList()
        {
            var gzByte = _stream.ReadByte();

            if (gzByte == -1)
            {
                throw new NBTException(NBTException.MSG_GZIP_ENDOFSTREAM);
            }

            var val = new TagNodeList((TagType)gzByte);

            if (val.ValueType > (TagType)Enum.GetValues(typeof(TagType)).GetUpperBound(0))
            {
                throw new NBTException(NBTException.MSG_READ_TYPE);
            }

            var lenBytes = new byte[4];

            _stream.Read(lenBytes, 0, 4);

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(lenBytes);
            }

            var length = BitConverter.ToInt32(lenBytes, 0);

            if (length < 0)
            {
                throw new NBTException(NBTException.MSG_READ_NEG);
            }

            if (val.ValueType == TagType.TAG_END)
            {
                return(new TagNodeList(TagType.TAG_BYTE));
            }

            for (var i = 0; i < length; i++)
            {
                val.Add(ReadValue(val.ValueType));
            }

            return(val);
        }