Example #1
0
    public void WriteChunkToFile(Streaming.IChunkIO chunk)
    {
        if (_chunkWrite != null)
        {
            ++_modifyCount;

            long ofs  = 0;
            long size = 0;

            if ((chunk.flags & EChunkFlags.SOLID) != 0)
            {
                ofs = _chunkWrite.BaseStream.Position;
                WriteChunkDataToFile(_chunkWrite, chunk);
                size = _chunkWrite.BaseStream.Position - ofs;
            }

            var chunkFile = new ChunkFile_t()
            {
                pos         = chunk.chunkPos,
                ofs         = (uint)ofs,
                size        = (uint)size,
                flags       = (uint)chunk.flags,
                modifyCount = _modifyCount
            };
            _chunkFiles[chunkFile.pos] = chunkFile;
        }
    }
Example #2
0
    void LoadIndexFile(string path)
    {
        _chunkFiles = new Dictionary <WorldChunkPos_t, ChunkFile_t>();

        bool valid = false;
        var  file  = File.Open(path + ".cix", FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
        long fpos  = 0;

        using (var reader = new BinaryReader(file, Encoding.UTF8)) {
            Header_t header = default(Header_t);
            try {
                header.version    = reader.ReadInt32();
                header.chunkCount = reader.ReadInt32();
                valid             = header.version == VERSION;

                if (valid)
                {
                    for (int i = 0; i < header.chunkCount; ++i)
                    {
                        var cf = ChunkFile_t.Read(reader);
                        _chunkFiles.Add(cf.pos, cf);
                    }
                    fpos = reader.BaseStream.Position;
                }
            } catch (Exception) {
                valid = false;
            }
        }

        file       = File.Open(path + ".cix", valid ? FileMode.Open : FileMode.Create, FileAccess.Write, FileShare.None);
        _indexFile = new BinaryWriter(file);
        if (valid)
        {
            _indexFile.BaseStream.Position = fpos;
        }
        else
        {
            _chunkFiles.Clear();
            WriteIndexFile();
            try {
                File.Delete(path + ".cdf");
            } catch (Exception e) {
                Debug.LogException(e);
            }
        }
    }