Beispiel #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;
        }
Beispiel #2
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;
        }
Beispiel #3
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);
        }
Beispiel #4
0
        /// <summary>
        /// Opens the region file from a stream.
        /// </summary>
        /// <param name="stream">The stream the region file will read from.</param>
        /// <returns>The parsed region file.</returns>
        public static RegionFile OpenRegion(Stream stream)
        {
#if DEBUG
            DateTime wStart;
#endif
            RegionFile region = new RegionFile();

            using (BinaryReader reader = new BinaryReader(stream))
            {
                int[] sectors = new int[1024];
                int[] tstamps = new int[1024];

                for (int i = 0; i < 1024; i++)
                {
                    sectors[i] = reader.ReadInt32();
                }

                for (int i = 0; i < 1024; i++)
                {
                    tstamps[i] = reader.ReadInt32();
                }

                Thread offsetThread = new Thread(new ThreadStart(() =>
                {
                    int sector = 0;

                    lock (sectors)
                        for (int i = 0; i < 1024; i++)
                        {
                            sector = EndiannessConverter.ToInt32(sectors[i]);

                            region.offsets[i] = new McrOffset()
                            {
                                SectorSize   = (byte)(sector & 0xFF),
                                SectorOffset = sector >> 8,
                            };
                        }

                    sectors = null;
                }));
                offsetThread.Name = "offset calculator thread";
                offsetThread.Start();

                offsetThread.Join();

                Thread tstampThread = new Thread(() =>
                {
                    int tstamp = 0;
                    lock (tstamps)
                        for (int i = 0; i < 1024; i++)
                        {
                            tstamp            = EndiannessConverter.ToInt32(tstamps[i]);
                            region.tstamps[i] = new McrtStamp
                            {
                                Timestamp = tstamp,
                            };
                        }

                    tstamps = null;
                })
                {
                    Name = "timestamp calculator thread"
                };
                tstampThread.Start();
                tstampThread.Join();
#if DEBUG
                wStart = DateTime.Now;
#endif
                byte[][] chunkBuffer = new byte[1024][];
                {
                    int       length;
                    McrOffset offset;

                    for (int i = 0; i < 1024; i++)
                    {
                        offset = region.offsets[i];

                        if (offset.SectorOffset != 0)
                        {
                            stream.Seek(offset.SectorOffset * 4096, SeekOrigin.Begin);

                            length = EndiannessConverter.ToInt32(reader.ReadInt32());
                            reader.ReadByte();

                            chunkBuffer[i] = reader.ReadBytes(length - 1);
                        }
                    }
                }

                int      chunkSlice    = 1024 / MaxTHREADS;
                Thread[] workerThreads = new Thread[MaxTHREADS];
                {
                    for (int i = 0; i < MaxTHREADS; i++)
                    {
                        byte[][] chunkWorkerBuffer = new byte[chunkSlice][];
                        Array.Copy(chunkBuffer, i * chunkSlice, chunkWorkerBuffer, 0, chunkSlice);

                        int index = i;

                        workerThreads[i] = new Thread(new ThreadStart(() =>
                        {
#if DEBUG
                            DateTime start = DateTime.Now;
#endif

                            int offset            = index * (1024 / MaxTHREADS);
                            MemoryStream mmStream = null;

                            for (int n = 0; n < chunkWorkerBuffer.Length; n++)
                            {
                                byte[] chunk = chunkWorkerBuffer[n];

                                if (chunk == null)
                                {
                                    continue;
                                }

                                mmStream = new MemoryStream(chunk);
                                region.chunks[n + offset] = NBTFile.OpenFile(mmStream, 2);
                                mmStream.Dispose();
                            }

                            chunkWorkerBuffer = null;

#if DEBUG
                            Console.WriteLine("Thread worker " + (index + 1) + " is complete! Took " + (int)(DateTime.Now - start).TotalMilliseconds + "ms to process.");
                        }));

                        workerThreads[i].Name = "chunk worker thread " + (index + 1);
#else
                        }));
Beispiel #5
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;
        }