public void ReplaceChunkData(ChunkIdx _chunkIdx, Dictionary <IntVector4, long> chunkModifyInfos) { //清空已存在的数据 Clear(); //if (info.DefChunkStreamInMenu == null) // return; foreach (KeyValuePair <IntVector4, long> ite in chunkModifyInfos) { IntVector4 cpos = ite.Key; if (cpos.XYZ.Equals(_chunkIdx.m_gobalIdx)) { bool breplace = false; #if false for (int _j = 0; _j <= LODOctreeMan.MaxLod; _j++) #endif { byte[] bytes = VFVoxelTerrain.self.SaveLoad.TryGetChunkData(cpos, false); m_voxels = new VoxelType[VoxelTerrainConstants.VOXEL_ARRAY_LENGTH]; int m = 0; for (int _i = 0; _i != VoxelTerrainConstants.VOXEL_ARRAY_LENGTH; ++_i) { VoxelType _vt = new VoxelType(); _vt.m_vol = bytes[m++]; _vt.m_type = bytes[m++]; m_voxels[_i] = _vt; } breplace = true; } if (!breplace) { Debug.LogError("ReplaceChunkData() is error!"); return; } Debug.Log("ReplaceChunkData() - done!!!"); return; } } }
public void ReplaceModifyChunk(string _strVoxelmap, int _pieceCount, int _iMaxTier, int _chunkCount, List <IntVector4> _ltChunk, Dictionary <IntVector4, long> chunkModifyInfos) { string _strSrcVoxelMap = VFVoxelTerrain.MapDataPath_Zip + _strVoxelmap; using (FileStream _fsSrcVM = new FileStream(_strSrcVoxelMap, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (BinaryReader _brSrcVM = new BinaryReader(_fsSrcVM)) { int _fileSize = (int)_fsSrcVM.Length; //int _iMaxSize = _pieceCount * _iMaxTier * _pieceCount; int _iSize = _pieceCount * _iMaxTier; int _jSize = _iMaxTier; // Here Ignore lod() use 0 //计算并保存修改的chunk在当前voxelMap中的对应的piece和chunk位置 Dictionary <IntVector4, List <ChunkIdx> > _mapChunk = new Dictionary <IntVector4, List <ChunkIdx> >(); foreach (IntVector4 _idx in _ltChunk) { int _offsetX = _idx.x % (VoxelTerrainConstants._mapFileWidth >> VoxelTerrainConstants._shift); int _offsetZ = _idx.z % (VoxelTerrainConstants._mapFileWidth >> VoxelTerrainConstants._shift); IntVector4 _pieceIdx = new IntVector4(); _pieceIdx.x = _offsetX / VoxelTerrainConstants._ChunksPerPiecePerAxis; _pieceIdx.y = _idx.y / VoxelTerrainConstants._ChunksPerPiecePerAxis; _pieceIdx.z = _offsetZ / VoxelTerrainConstants._ChunksPerPiecePerAxis; ChunkIdx _chunkIdx = new ChunkIdx(); _chunkIdx.m_gobalIdx = _idx.XYZ; // Here Ignore lod() use 0 _chunkIdx.m_localIdx = new IntVector3(); _chunkIdx.m_localIdx.x = _offsetX % VoxelTerrainConstants._ChunksPerPiecePerAxis; _chunkIdx.m_localIdx.z = _offsetZ % VoxelTerrainConstants._ChunksPerPiecePerAxis; _chunkIdx.m_localIdx.y = _idx.y % VoxelTerrainConstants._ChunksPerPiecePerAxis; List <ChunkIdx> _chunkList; if (_mapChunk.TryGetValue(_pieceIdx, out _chunkList)) { _chunkList.Add(_chunkIdx); } else { _chunkList = new List <ChunkIdx>(); _chunkList.Add(_chunkIdx); _mapChunk.Add(_pieceIdx, _chunkList); } } //读取每个piece的offset并且初始化 int _iPieceSize = _pieceCount * _iMaxTier * _pieceCount; m_pieceData = new VoxelPieceData[_iPieceSize]; for (int _i = 0; _i != _iPieceSize; ++_i) { m_pieceData[_i] = new VoxelPieceData(); //依次读取每个piece的chunk数据 m_pieceData[_i].m_iOffsetPiece = _brSrcVM.ReadInt32(); } //拷贝和整合修改后的数据 if (!Directory.Exists(GameConfig.MergedUserDataPath)) { Directory.CreateDirectory(GameConfig.MergedUserDataPath); } string _strDstVoxelMap = GameConfig.MergedUserDataPath + _strVoxelmap; using (FileStream _fsDstVM = new FileStream(_strDstVoxelMap, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)) { using (BinaryWriter _bwDstVM = new BinaryWriter(_fsDstVM)) { //偏移_fsDstVM中的piece数据区 _fsDstVM.Seek(_iPieceSize * 4, SeekOrigin.Begin); //写入pieceData for (int _i = 0; _i != _iPieceSize; ++_i) { IntVector4 _pieceIdx = new IntVector4(); int _temp = _i % _iSize; _pieceIdx.x = _i / _iSize; _pieceIdx.z = _temp / _jSize; _pieceIdx.y = _temp % _jSize; //确定当前piece的bytes大小 int _iPieceBytes = 0; if (_i != (m_pieceData.Length - 1)) { _iPieceBytes = m_pieceData[_i + 1].m_iOffsetPiece - m_pieceData[_i].m_iOffsetPiece; } else { _iPieceBytes = _fileSize - m_pieceData[_i].m_iOffsetPiece; } //设置源文件pieceData偏移位置 _fsSrcVM.Seek(m_pieceData[_i].m_iOffsetPiece, SeekOrigin.Begin); //记录目标文件pieceData当前位置 m_pieceData[_i].m_iOffsetPiece = (int)_fsDstVM.Position; //载入pieceData数据 List <ChunkIdx> _chunkList; if (_mapChunk.TryGetValue(_pieceIdx, out _chunkList)) { string _strLastFileMerged = ""; foreach (ChunkIdx _chunkIdx in _chunkList) { _strLastFileMerged += "(" + _chunkIdx.m_gobalIdx.x + "," + _chunkIdx.m_gobalIdx.z + "," + _chunkIdx.m_gobalIdx.y + ")"; } EditorUtility.DisplayProgressBar("Merging files...", _strVoxelmap + _strLastFileMerged, _i / (float)_iPieceSize); //载入解压后的pieceData m_pieceData[_i].LoadPieceData(_fsSrcVM, _brSrcVM, _chunkCount, _iPieceBytes); //替换其中修改后的chunkData m_pieceData[_i].ReplaceChunkData(_chunkList, _chunkCount, chunkModifyInfos); m_pieceData[_i].CompressAndSave(_bwDstVM, _chunkCount); } else { EditorUtility.DisplayProgressBar("Merging files...", _strVoxelmap + ":" + _i, _i / (float)_iPieceSize); //直接读取压缩pieceData并存入_fsDstVM m_pieceData[_i].LoadZipPieceData(_brSrcVM, _iPieceBytes); m_pieceData[_i].SavePieceData(_bwDstVM); } //清理pieceData m_pieceData[_i].Clear(); } //写入pieceData偏移数据 _fsDstVM.Seek(0, SeekOrigin.Begin); foreach (VoxelPieceData _vpd in m_pieceData) { _bwDstVM.Write(_vpd.m_iOffsetPiece); } } } } } }