Пример #1
0
        private void zLIBZToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.AddExtension     = true;
            dialog.CheckFileExists  = true;
            dialog.CheckPathExists  = true;
            dialog.Multiselect      = false;
            dialog.RestoreDirectory = true;
            dialog.Title            = LangManager.GetString("nbtViewer_selectNBTFormatFile");

            DialogResult result = dialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                this.cacheData.NBTViewerCache.Rows.Clear();
                try
                {
                    CompoundTag tag = NBTIO.ReadZLIBFile(dialog.FileName, this.Endian);
                    this._Load(tag);
                }
                catch (Exception exc)
                {
                    MessageBox.Show(exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        public ChunkData Read(int x, int z)
        {
            const int width = 32;
            const int depth = 32;

            using (FileStream regionFile = File.OpenRead(RegionPath))
            {
                byte[] buffer = new byte[8192];

                regionFile.Read(buffer, 0, 8192);

                int xi = (x % width);
                if (xi < 0)
                {
                    xi += 32;
                }
                int zi = (z % depth);
                if (zi < 0)
                {
                    zi += 32;
                }
                int tableOffset = (xi + zi * width) * 4;

                regionFile.Seek(tableOffset, SeekOrigin.Begin);

                byte[] offsetBuffer = new byte[4];
                regionFile.Read(offsetBuffer, 0, 3);
                Array.Reverse(offsetBuffer);
                int offset = BitConverter.ToInt32(offsetBuffer, 0) << 4;

                byte[] bytes = BitConverter.GetBytes(offset >> 4);
                Array.Reverse(bytes);
                if (offset != 0 && offsetBuffer[0] != bytes[0] && offsetBuffer[1] != bytes[1] &&
                    offsetBuffer[2] != bytes[2])
                {
                    throw new FormatException();
                }

                int length = regionFile.ReadByte();

                if (offset == 0 || length == 0)
                {
                    return(null);
                }

                regionFile.Seek(offset, SeekOrigin.Begin);
                byte[] waste = new byte[4];
                regionFile.Read(waste, 0, 4);
                int compressionMode = regionFile.ReadByte();

                if (compressionMode != 0x02)
                {
                    throw new FormatException();
                }

                CompoundTag tag = NBTIO.ReadZLIBFile(new BinaryReader(regionFile).ReadBytes((int)(regionFile.Length - regionFile.Position)), NBTEndian.BIG_ENDIAN);
                return(new ChunkData(RegionPosition, new Tuple <int, int>(x, z), tag));
            }
        }
Пример #3
0
        public Chunk GetChunk(int chunkX, int chunkZ)
        {
            int    regionX = chunkX >> 5;
            int    regionZ = chunkZ >> 5;
            string key     = $"{regionX}:{regionZ}";

            lock (this.Files)
            {
                RegionFile file = null;
                if (!this.Files.ContainsKey(key))
                {
                    file = new RegionFile(this.WorldName, regionX, regionZ);
                    this.Files.Add(key, file);

                    Chunk chunk = new Chunk(chunkX, chunkZ);
                    return(chunk);
                }
                else
                {
                    file = this.Files[key];
                    if (file.IsFileCreated)
                    {
                        byte[] data = file.GetChunkBytes(chunkX, chunkZ);
                        if (data == null)
                        {
                            Chunk chunk = new Chunk(chunkX, chunkZ);
                            return(chunk);
                        }

                        return(this.ChunkFormat.NBTDeserialize(NBTIO.ReadZLIBFile(data, NBT.Data.NBTEndian.BIG_ENDIAN)));
                    }
                    else
                    {
                        Chunk chunk = new Chunk(chunkX, chunkZ);
                        return(chunk);
                    }
                }
            }
        }
Пример #4
0
        public Chunk GetChunk(int chunkX, int chunkZ)
        {
            int width = 32;
            int depth = 32;

            int rx = chunkX >> 5;
            int rz = chunkZ >> 5;
            Tuple <int, int> regionPos = new Tuple <int, int>(rx, rz);
            Tuple <int, int> chunkPos  = new Tuple <int, int>(chunkX, chunkZ);

            if (this._chunks.ContainsKey(chunkPos))
            {
                return(this._chunks[chunkPos]);
            }

            string filePath = Path.Combine(this.WorldPath, $@"region/r.{rx}.{rz}.mca");

            if (!File.Exists(filePath))
            {
                return(new Chunk(this.World, chunkX, chunkZ));
            }

            if (!_files.ContainsKey(regionPos))
            {
                this._files[regionPos] = File.OpenRead(filePath);
            }

            FileStream regionFile = this._files[regionPos];

            byte[] buffer = new byte[8192];

            regionFile.Read(buffer, 0, 8192);

            int xi = (chunkX % width);

            if (xi < 0)
            {
                xi += 32;
            }
            int zi = (chunkZ % depth);

            if (zi < 0)
            {
                zi += 32;
            }
            int tableOffset = (xi + zi * width) * 4;

            regionFile.Seek(tableOffset, SeekOrigin.Begin);

            byte[] offsetBuffer = new byte[4];
            regionFile.Read(offsetBuffer, 0, 3);
            Array.Reverse(offsetBuffer);
            int offset = BitConverter.ToInt32(offsetBuffer, 0) << 4;

            byte[] bytes = BitConverter.GetBytes(offset >> 4);
            Array.Reverse(bytes);
            if (offset != 0 && offsetBuffer[0] != bytes[0] && offsetBuffer[1] != bytes[1] &&
                offsetBuffer[2] != bytes[2])
            {
                throw new FormatException();
            }

            int length = regionFile.ReadByte();

            if (offset == 0 || length == 0)
            {
                return(new Chunk(this.World, chunkX, chunkZ));
            }

            regionFile.Seek(offset, SeekOrigin.Begin);
            byte[] waste = new byte[4];
            regionFile.Read(waste, 0, 4);
            int compressionMode = regionFile.ReadByte();

            if (compressionMode != 0x02)
            {
                throw new FormatException();
            }

            CompoundTag tag = NBTIO.ReadZLIBFile(new BinaryReader(regionFile).ReadBytes((int)(regionFile.Length - regionFile.Position)));

            return(this.ChunkFormat.NBTDeserialize(tag));
        }