示例#1
0
        private void WriteCompound(TagNodeCompound val)
        {
            foreach (var item in val)
            {
                WriteTag(item.Key, item.Value);
            }

            WriteTag(null, _nulltag);
        }
示例#2
0
 /// <summary>
 /// Rebuild the internal NBT tree from a source data stream.
 /// </summary>
 /// <param name="s">An open, readable data stream containing NBT data.</param>
 public void ReadFrom(Stream s)
 {
     if (s != null)
     {
         _stream = s;
         _root   = ReadRoot();
         _stream = null;
     }
 }
示例#3
0
        private void WriteCompound(TagNodeCompound val)
        {
            foreach (KeyValuePair <string, TagNode> item in val)
            {
                WriteTag(item.Key, item.Value);
            }

            WriteTag(null, _nulltag);
        }
示例#4
0
        /// <summary>
        /// Makes a deep copy of the node.
        /// </summary>
        /// <returns>A new compound node containing new subnodes representing the same data.</returns>
        public override TagNode Copy()
        {
            var list = new TagNodeCompound();

            foreach (var(key, value) in _tags)
            {
                list[key] = value.Copy();
            }
            return(list);
        }
示例#5
0
        /// <summary>
        /// Makes a deep copy of the node.
        /// </summary>
        /// <returns>A new compound node containing new subnodes representing the same data.</returns>
        public override TagNode Copy()
        {
            TagNodeCompound list = new TagNodeCompound();

            foreach (KeyValuePair <string, TagNode> item in _tags)
            {
                list[item.Key] = item.Value.Copy();
            }
            return(list);
        }
        /// <summary>
        /// Constructs a default <see cref="TagNodeCompound"/> satisfying the constraints of this node.
        /// </summary>
        /// <returns>A <see cref="TagNodeCompound"/> with a sensible default value.  A default child <see cref="TagNode"/> is created for every <see cref="SchemaNode"/> contained in this <see cref="SchemaNodeCompound"/>.</returns>
        public override TagNode BuildDefaultTree()
        {
            var list = new TagNodeCompound();

            foreach (var node in _subnodes)
            {
                list[node.Name] = node.BuildDefaultTree();
            }

            return(list);
        }
示例#7
0
        private TagNode ReadCompound()
        {
            TagNodeCompound val = new TagNodeCompound();

            while (ReadTag(val))
            {
                ;
            }

            return(val);
        }
示例#8
0
        /// <summary>
        /// Copies all the elements of <paramref name="tree"/> into this <see cref="TagNodeCompound"/> if they do not already exist.
        /// </summary>
        /// <param name="tree">The source <see cref="TagNodeCompound"/> to copy elements from.</param>
        public void MergeFrom(TagNodeCompound tree)
        {
            foreach (var node in tree)
            {
                if (_tags.ContainsKey(node.Key))
                {
                    continue;
                }

                _tags.Add(node.Key, node.Value);
            }
        }
示例#9
0
        private static void SerializeCompound(TagNodeCompound tag, StringBuilder str, int level)
        {
            if (tag.Count == 0)
            {
                str.Append("{ }");
                return;
            }

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

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

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

                KeyValuePair <string, TagNode> item = en.Current;
                Add(str, "\"" + item.Key + "\": ", level + 1);

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

                first = false;
            }

            str.AppendLine();
            Add(str, "}", level);
        }
示例#10
0
        private bool ReadTag(TagNodeCompound parent)
        {
            TagType type = (TagType)_stream.ReadByte();

            if (type != TagType.TAG_END)
            {
                string name = ReadString().ToTagString().Data;
                parent[name] = ReadValue(type);
                return(true);
            }

            return(false);
        }
示例#11
0
        private bool VerifyCompound(TagNode tag, SchemaNodeCompound schema)
        {
            TagNodeCompound ctag = tag as TagNodeCompound;

            if (ctag == null)
            {
                if (!OnInvalidTagType(new TagEventArgs(schema, tag)))
                {
                    return(false);
                }
            }

            bool pass = true;

            Dictionary <string, TagNode> _scratch = new Dictionary <string, TagNode>();

            foreach (SchemaNode node in schema)
            {
                TagNode value;
                ctag.TryGetValue(node.Name, out value);

                if (value == null)
                {
                    if ((node.Options & SchemaOptions.CREATE_ON_MISSING) == SchemaOptions.CREATE_ON_MISSING)
                    {
                        _scratch[node.Name] = node.BuildDefaultTree();
                        continue;
                    }
                    else if ((node.Options & SchemaOptions.OPTIONAL) == SchemaOptions.OPTIONAL)
                    {
                        continue;
                    }
                }

                pass = Verify(tag, value, node) && pass;
            }

            foreach (KeyValuePair <string, TagNode> item in _scratch)
            {
                ctag[item.Key] = item.Value;
            }

            _scratch.Clear();

            return(pass);
        }
示例#12
0
        private static void SerializeCompound(TagNodeCompound 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 (KeyValuePair <string, TagNode> item in tag)
            {
                if (!first)
                {
                    str.Append(",");
                    if (!singleLine)
                    {
                        str.AppendLine();
                    }
                }

                Add(str, "\"" + Escape(item.Key) + "\": ", level + 1, singleLine);

                Serialize(tag, level + 1, str, false, singleLine);

                first = false;
            }
            if (!singleLine)
            {
                str.AppendLine();
            }
            Add(str, "}", level, singleLine);
        }
示例#13
0
 /// <summary>
 /// Constructs a wrapper around another NBT tree and gives it a name.
 /// </summary>
 /// <param name="tree">The root node of an NBT tree.</param>
 /// <param name="name">The name for the root node.</param>
 public NbtTree(TagNodeCompound tree, string name)
 {
     _root     = tree;
     _rootName = name;
 }
示例#14
0
 /// <summary>
 /// Constructs a wrapper around another NBT tree.
 /// </summary>
 /// <param name="tree">The root node of an NBT tree.</param>
 public NbtTree(TagNodeCompound tree)
 {
     _root = tree;
 }
示例#15
0
 /// <summary>
 /// Constructs a wrapper around a new NBT tree with an empty root node.
 /// </summary>
 public NbtTree()
 {
     _root = new TagNodeCompound();
 }