public NetChunkData(int roleId, WorldPos pos)
 {
     this.roleId = roleId;
     worldPos    = pos;
     isExist     = false;
     data        = new ChunkByteData();
 }
Ejemplo n.º 2
0
        public void SaveChunkData(int x, int z, ChunkByteData chunkByteData)
        {
            byte[] chunkSize = new byte[4];
            Serialization.WriteIntToByteArr(chunkSize, (int)chunkByteData.data.Length);
            int singleChunkLength = (int)chunkByteData.data.Length + 5;

            int blockLength = ByteToSector(singleChunkLength);

            ClearChunkData(x, z);

            int  blockOffset = GetChunkCanSavedSector(blockLength);
            long blockOffsetStartByteIndex = SectorToByte(blockOffset);

            _fs.Position = blockOffsetStartByteIndex;
            //写入区块长度
            _fs.Write(chunkSize, 0, chunkSize.Length);
            //写入压缩格式
            _fs.WriteByte((byte)chunkByteData.compressType);
            //写入区块压缩数据
            _fs.Write(chunkByteData.data, 0, (int)chunkByteData.data.Length);

            int startPosition = (x * REGION_DEPTH + z) * 4;

            _fs.Position = startPosition;
            byte[] blockOffsetByte = new byte[4];
            Serialization.WriteIntToByteArr(blockOffsetByte, blockOffset);
            _fs.Write(blockOffsetByte, 0, 3);
            _fs.WriteByte((byte)blockLength);

            //更新可存储标志,并将块后面不足一个扇区的部分写0x00
            for (int i = blockOffset; i < blockOffset + blockLength; i++)
            {
                _useSector[i] = true;
            }
            long chunkEnd  = blockOffsetStartByteIndex + singleChunkLength;
            long sectorEnd = blockOffsetStartByteIndex + SectorToByte(blockLength);

            if (chunkEnd < sectorEnd)
            {
                _fs.Position = chunkEnd;
                byte[] extendByte = Enumerable.Repeat((byte)0x00, (int)(sectorEnd - chunkEnd)).ToArray();
                _fs.Write(extendByte, 0, extendByte.Length);
            }
            //截取文件最后没有使用的扇区
            int lastUseSectorIndex = GetLastUseSectorIndex();

            if (lastUseSectorIndex != -1 && lastUseSectorIndex != _useSector.Count - 1)
            {
                int truncatedIndex = lastUseSectorIndex + 1;
                _useSector.RemoveRange(truncatedIndex, _useSector.Count - truncatedIndex);
                _fs.SetLength(SectorToByte(truncatedIndex));
            }
            //更新headData数据
            _headData[x, z].BlockOffset     = blockOffset;
            _headData[x, z].BlockLength     = (byte)blockLength;
            _headData[x, z].Length          = chunkByteData.data.Length;
            _headData[x, z].status          = ChunkDataStatus.OK;
            _headData[x, z].CompressionType = chunkByteData.compressType;
        }
Ejemplo n.º 3
0
        public bool GetChunkData(int x, int z, ChunkByteData chunkByteData)
        {
            if (_headData[x, z].status == ChunkDataStatus.Not_Create)
            {
                return(false);
            }
            _fs.Position = SectorToByte(_headData[x, z].BlockOffset) + 5;

            byte[] data = new byte[_headData[x, z].Length];
            _fs.Read(data, 0, data.Length);
            chunkByteData.data         = data;
            chunkByteData.compressType = _headData[x, z].CompressionType;
            return(true);
        }