Ejemplo n.º 1
0
        /// <summary>
        /// Inserts/replaces a new chunk on a specified location.
        /// </summary>
        /// <param name="location">The region location of the chunk.</param>
        /// <param name="chunk">The chunk to be added.</param>
        public void InsertChunk(MCPoint location, NBTFile chunk)
        {
            int offset = location.X + (location.Y * 32);

            chunks[offset] = chunk;

            chunkChanged[offset] = true;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Opens an existing NBT file from a stream.
        /// </summary>
        /// <param name="stream">The stream to get the NBT file from.</param>
        /// <param name="version">The compression version of the NBT, specify '1' for the original gzip compression, '2' for the mcregion zlib compression.</param>
        /// <returns>An opened NBT file.</returns>
        public static NBTFile OpenFile(Stream stream, int version)
        {
            NBTFile file = new NBTFile();

            Stream compressStream;

            if (version == 1)
            {
                compressStream = new GZipStream(stream, CompressionMode.Decompress);
            }
            else
            {
                stream.ReadByte();
                stream.ReadByte();

                compressStream = new DeflateStream(stream, CompressionMode.Decompress);
            }

            BinaryReader reader = new BinaryReader(compressStream);
            {
                Encoding textEncoding = Encoding.UTF8;

                file.NamedNBT = reader.ReadByte() == 10;
                file.RootName = textEncoding.GetString(reader.ReadBytes(EndiannessConverter.ToInt16(reader.ReadInt16())));

                if (file.NamedNBT)
                {
                    byte type;

                    while ((type = reader.ReadByte()) != 0)
                    {
                        string name = textEncoding.GetString(reader.ReadBytes(EndiannessConverter.ToInt16(reader.ReadInt16())));

                        file.InsertTag(new NBTTag(name, type, file.ReadPayload(ref reader, type)));
                    }
                }
                else
                {
                    byte type = reader.ReadByte();
                    int size = EndiannessConverter.ToInt32(reader.ReadInt32());

                    for (int i = 0; i < size; i++)
                    {
                        file.InsertTag(new NBTTag("", type, file.ReadPayload(ref reader, type)));
                    }
                }
            }
            reader.Dispose();
            compressStream.Dispose();

            return file;
        }