Ejemplo n.º 1
0
        /// <summary>
        /// 加载实体
        /// </summary>
        /// <param name="entitys"></param>
        public void LoadEntity(ChunkPos chunkPos, ref List <DataTag> entityDataList)
        {
            var entityDataName = chunkPos.IsGlobal() ? "global" : chunkPos.x + "." + chunkPos.z;

            var loadPath = _dir + "e." + entityDataName + ".dat";

            if (!chunkPos.IsAvailable())
            {
                return;
            }

            // 如果未创建实体信息则生成
            if (!File.Exists(loadPath))
            {
                var worldSize = GameManager.WorldManager.worldInfo.worldSize;
                if (!chunkPos.IsGlobal())
                {
                    if (
                        !(chunkPos.x < 0 || chunkPos.x > worldSize.x ||
                          chunkPos.z < 0 || chunkPos.z > worldSize.z))
                    {
                        GameManager.WorldManager.worldGenerator.GenChunkEntity(chunkPos, ref entityDataList);
                    }
                }
                else
                {
                    GameManager.WorldManager.worldGenerator.GenGlobalEntity(ref entityDataList);
                }
                return;
            }

            var entityStream         = File.OpenRead(loadPath);
            var decompressFileStream = new GZipStream(entityStream, CompressionMode.Decompress);
            var reader = new BinaryReader(decompressFileStream);

            // 加载实体
            var entityCount = reader.ReadInt32();

            for (var i = 0; i < entityCount; ++i)
            {
                var entityData = DataTag.Empty();
                entityData.Load(reader);

                entityDataList.Add(entityData);
            }

            reader.Close();
            decompressFileStream.Close();
            entityStream.Close();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 保存实体
        /// </summary>
        /// <param name="entitys"></param>
        public void SaveEntity(ChunkPos chunkPos, List <DataTag> entityDataList)
        {
            if (!chunkPos.IsAvailable())
            {
                return;
            }

            if (!chunkPos.IsGlobal())
            {
                SaveEntity(chunkPos.x + "." + chunkPos.z, entityDataList);
            }
            else
            {
                SaveEntity("global", entityDataList);
            }
        }
Ejemplo n.º 3
0
        public Task AsyncUnloadTask()
        {
            // 卸载网格
            if (ChunkPos.IsAvailable())
            {
                _chunkMeshGenerator?.Unload();
            }

            if (_unloadTask != null && !_unloadTask.IsCompleted)
            {
                return(_unloadTask);
            }

            var needSave    = IsLoaded;
            var oldChunkPos = ChunkPos;

            IsLoaded = false;
            _isDirty = false;

            // 卸载Chunk
            _unloadTask = new Task(() =>
            {
                if (!oldChunkPos.IsAvailable())
                {
                    return;
                }

                if (needSave)
                {
                    GameManager.WorldManager.worldLoader.SaveBlock(oldChunkPos, _blocks);
                }

                for (var x = 0; x < ContainerSize.x; ++x)
                {
                    for (var y = 0; y < ContainerSize.y; ++y)
                    {
                        for (var z = 0; z < ContainerSize.z; ++z)
                        {
                            _blocks[x, y, z].blockProxy = null;
                        }
                    }
                }
            });
            _unloadTask.Start();

            return(_unloadTask);
        }
Ejemplo n.º 4
0
        public void LoadBlock(ChunkPos chunkPos, ref Block[,,] blocks)
        {
            if (_dir == "")
            {
                Debug.LogError("Not load world");
                return;
            }

            if (!chunkPos.IsAvailable())
            {
                return;
            }

            var chunkFilePath    = _dir + "c." + chunkPos.x + "." + chunkPos.z + ".dat";
            var chunkTmpFilePath = chunkFilePath + ".tmp";

            while (File.Exists(chunkTmpFilePath))
            {
                Thread.Yield();
            }

            var chunkSize = GameManager.WorldManager.ChunkSize;

            // Chunk不存在
            var isChunkExists = File.Exists(chunkFilePath);

            if (!isChunkExists)
            {
                var worldSize = GameManager.WorldManager.worldInfo.worldSize;
                if (chunkPos.x < 0 || chunkPos.x > worldSize.x ||
                    chunkPos.z < 0 || chunkPos.z > worldSize.z)
                {
                    for (var x = 0; x < chunkSize.x; ++x)
                    {
                        for (var y = 0; y < chunkSize.y; ++y)
                        {
                            for (var z = 0; z < chunkSize.z; ++z)
                            {
                                blocks[x, y, z].ReadFrom(null, null);
                            }
                        }
                    }
                }
                else
                {
                    GameManager.WorldManager.worldGenerator.GenChunk(chunkPos, ref blocks);
                }

                return;
            }

            var chunkStream          = File.OpenRead(chunkFilePath);
            var decompressFileStream = new GZipStream(chunkStream, CompressionMode.Decompress);
            var reader = new BinaryReader(decompressFileStream);

            // 读取索引表
            var indexSize  = reader.ReadInt32();
            var blockIndex = new List <IBlock>(indexSize);

            for (var i = 0; i < indexSize; ++i)
            {
                var blockName = reader.ReadString();
                blockIndex.Add(GameManager.ProxyManager.Get <IBlock>(blockName));
            }

            for (var x = 0; x < chunkSize.x; ++x)
            {
                for (var y = 0; y < chunkSize.y; ++y)
                {
                    for (var z = 0; z < chunkSize.z; ++z)
                    {
                        blocks[x, y, z].ReadFrom(reader, blockIndex);
                    }
                }
            }

            reader.Close();
            decompressFileStream.Close();
            chunkStream.Close();
        }
Ejemplo n.º 5
0
        public void SaveBlock(ChunkPos chunkPos, Block[,,] blocks)
        {
            if (_dir == "")
            {
                Debug.LogError("Not load world");
                return;
            }

            if (!chunkPos.IsAvailable())
            {
                return;
            }

            var chunkSize = GameManager.WorldManager.ChunkSize;

            var savePath           = _dir + "c." + chunkPos.x + "." + chunkPos.z + ".dat";
            var saveTmpPath        = savePath + ".tmp";
            var chunkStream        = File.Create(saveTmpPath);
            var compressFileStream = new GZipStream(chunkStream, CompressionMode.Compress);
            var writer             = new BinaryWriter(compressFileStream);

            // 创建索引表
            List <string> blockIndex = new List <string>();

            for (var x = 0; x < chunkSize.x; ++x)
            {
                for (var y = 0; y < chunkSize.y; ++y)
                {
                    for (var z = 0; z < chunkSize.z; ++z)
                    {
                        var blockData = blocks[x, y, z].blockProxy;
                        var blockName = blockData == null ? "island.block:air" : blockData.Name;
                        if (!blockIndex.Contains(blockName))
                        {
                            blockIndex.Add(blockName);
                        }
                    }
                }
            }

            // 写入索引表
            writer.Write(blockIndex.Count);

            foreach (var str in blockIndex)
            {
                writer.Write(str);
            }

            // 写入Block信息
            for (var x = 0; x < chunkSize.x; ++x)
            {
                for (var y = 0; y < chunkSize.y; ++y)
                {
                    for (var z = 0; z < chunkSize.z; ++z)
                    {
                        blocks[x, y, z].WriteTo(writer, blockIndex);
                    }
                }
            }

            writer.Close();
            compressFileStream.Close();
            chunkStream.Close();

            // 复制文件
            File.Copy(saveTmpPath, savePath, true);

            // 删除临时文件
            File.Delete(saveTmpPath);
        }