Ejemplo n.º 1
0
        public NBTag Append(string name, params NBTag[] tags)
        {
            NBTCompound compound = new NBTCompound {
                Name = name
            };

            foreach (NBTag tag in tags)
            {
                compound.Tags.Add(tag.Name, tag);
            }
            return(Append(compound));
        }
Ejemplo n.º 2
0
        public static NBTag ReadTag([NotNull] BinaryReader reader, NBTType type, string name, NBTag parent)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            if (name == null && type != NBTType.End)
            {
                name = ReadString(reader);
            }
            switch (type)
            {
            case NBTType.End:
                return(new NBTag(NBTType.End, parent));

            case NBTType.Byte:
                return(new NBTag(NBTType.Byte, name, reader.ReadByte(), parent));

            case NBTType.Short:
                return(new NBTag(NBTType.Short, name, IPAddress.NetworkToHostOrder(reader.ReadInt16()), parent));

            case NBTType.Int:
                return(new NBTag(NBTType.Int, name, IPAddress.NetworkToHostOrder(reader.ReadInt32()), parent));

            case NBTType.Long:
                return(new NBTag(NBTType.Long, name, IPAddress.NetworkToHostOrder(reader.ReadInt64()), parent));

            case NBTType.Float:
                return(new NBTag(NBTType.Float, name, reader.ReadSingle(), parent));

            case NBTType.Double:
                return(new NBTag(NBTType.Double, name, reader.ReadDouble(), parent));

            case NBTType.Bytes:
                int bytesLength = IPAddress.NetworkToHostOrder(reader.ReadInt32());
                return(new NBTag(NBTType.Bytes, name, reader.ReadBytes(bytesLength), parent));

            case NBTType.String:
                return(new NBTag(NBTType.String, name, ReadString(reader), parent));


            case NBTType.List:
                NBTList list = new NBTList {
                    Type     = NBTType.List,
                    Name     = name,
                    Parent   = parent,
                    ListType = (NBTType)reader.ReadByte()
                };
                int listLength = IPAddress.NetworkToHostOrder(reader.ReadInt32());
                list.Tags = new NBTag[listLength];
                for (int i = 0; i < listLength; i++)
                {
                    list.Tags[i] = ReadTag(reader, list.ListType, "", list);
                }
                return(list);

            case NBTType.Compound:
                NBTCompound compound = new NBTCompound {
                    Type   = NBTType.Compound,
                    Name   = name,
                    Parent = parent
                };
                while (true)
                {
                    NBTag childTag = ReadTag(reader, (NBTType)reader.ReadByte(), null, compound);
                    if (childTag.Type == NBTType.End)
                    {
                        break;
                    }
                    if (childTag.Name == null)
                    {
                        continue;
                    }
                    if (compound.Tags.ContainsKey(childTag.Name))
                    {
                        throw new IOException("NBT parsing error: null names and duplicate names are not allowed within a compound tags.");
                    }
                    else
                    {
                        compound.Tags.Add(childTag.Name, childTag);
                    }
                }
                return(compound);

            default:
                throw new IOException("NBT parsing error: unknown tag type.");
            }
        }