Пример #1
0
        public void WriteTag([NotNull] BinaryWriter writer, bool writeType)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writeType)
            {
                writer.Write((byte)Type);
            }
            if (Type == NBTType.End)
            {
                return;
            }
            if (writeType)
            {
                WriteString(Name, writer);
            }
            switch (Type)
            {
            case NBTType.Byte:
                writer.Write((byte)Payload);
                return;

            case NBTType.Short:
                writer.Write(IPAddress.HostToNetworkOrder((short)Payload));
                return;

            case NBTType.Int:
                writer.Write(IPAddress.HostToNetworkOrder((int)Payload));
                return;

            case NBTType.Long:
                writer.Write(IPAddress.HostToNetworkOrder((long)Payload));
                return;

            case NBTType.Float:
                writer.Write((float)Payload);
                return;

            case NBTType.Double:
                writer.Write((double)Payload);
                return;

            case NBTType.Bytes:
                writer.Write(IPAddress.HostToNetworkOrder(((byte[])Payload).Length));
                writer.Write((byte[])Payload);
                return;

            case NBTType.String:
                WriteString((string)Payload, writer);
                return;


            case NBTType.List:
                NBTList list = (NBTList)this;
                writer.Write((byte)list.ListType);
                writer.Write(IPAddress.HostToNetworkOrder(list.Tags.Length));

                for (int i = 0; i < list.Tags.Length; i++)
                {
                    list.Tags[i].WriteTag(writer, false);
                }
                return;

            case NBTType.Compound:
                foreach (NBTag tag in this)
                {
                    tag.WriteTag(writer);
                }
                writer.Write((byte)NBTType.End);
                return;

            default:
                return;
            }
        }
Пример #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:
                NBTag       childTag;
                NBTCompound compound = new NBTCompound {
                    Type   = NBTType.Compound,
                    Name   = name,
                    Parent = parent
                };
                while (true)
                {
                    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.");
            }
        }