Ejemplo n.º 1
0
    public Dictionary <Xyz, ushort[]> GetChunksFromDatabase(List <Xyz> chunks, string filename)
    {
        if (chunks == null)
        {
            return(null);
        }

        if (!GameStorePath.IsValidName(filename))
        {
            Console.WriteLine("Invalid backup filename: " + filename);
            return(null);
        }
        if (!Directory.Exists(GameStorePath.gamepathbackup))
        {
            Directory.CreateDirectory(GameStorePath.gamepathbackup);
        }
        string finalFilename = Path.Combine(GameStorePath.gamepathbackup, filename + MapManipulator.BinSaveExtension);

        Dictionary <Xyz, ushort[]> deserializedChunks = new Dictionary <Xyz, ushort[]>();
        Dictionary <Xyz, byte[]>   serializedChunks   = ChunkDb.GetChunksFromFile(d_ChunkDb, chunks, finalFilename);

        foreach (var k in serializedChunks)
        {
            ServerChunk c = null;
            if (k.Value != null)
            {
                c = DeserializeChunk(k.Value);
            }
            deserializedChunks.Add(k.Key, c.data);
        }
        return(deserializedChunks);
    }
Ejemplo n.º 2
0
    public ushort[] GetChunkFromDatabase(int x, int y, int z, string filename)
    {
        if (MapUtil.IsValidPos(d_Map, x, y, z))
        {
            if (!GameStorePath.IsValidName(filename))
            {
                Console.WriteLine("Invalid backup filename: " + filename);
                return(null);
            }
            if (!Directory.Exists(GameStorePath.gamepathbackup))
            {
                Directory.CreateDirectory(GameStorePath.gamepathbackup);
            }
            string finalFilename = Path.Combine(GameStorePath.gamepathbackup, filename + MapManipulator.BinSaveExtension);

            x = x / chunksize;
            y = y / chunksize;
            z = z / chunksize;

            byte[] serializedChunk = ChunkDb.GetChunkFromFile(d_ChunkDb, x, y, z, finalFilename);
            if (serializedChunk != null)
            {
                ServerChunk c = DeserializeChunk(serializedChunk);
                return(c.data);
            }
        }
        return(null);
    }
Ejemplo n.º 3
0
    public void DeleteChunks(List <Vector3i> chunkPositions)
    {
        List <Xyz> chunks = new List <Xyz>();

        foreach (Vector3i pos in chunkPositions)
        {
            if (MapUtil.IsValidPos(d_Map, pos.x, pos.y, pos.z))
            {
                int x = pos.x / chunksize;
                int y = pos.y / chunksize;
                int z = pos.z / chunksize;
                d_Map.SetChunkValid(x, y, z, null);
                chunks.Add(new Xyz()
                {
                    X = x, Y = y, Z = z
                });
            }
        }
        if (chunks.Count != 0)
        {
            ChunkDb.DeleteChunks(d_ChunkDb, chunks);
            // force to update chunks at clients
            foreach (var k in clients)
            {
                //todo wrong
                //k.Value.chunksseen.Clear();
                Array.Clear(k.Value.chunksseen, 0, k.Value.chunksseen.Length);
            }
        }
    }
Ejemplo n.º 4
0
    public ServerChunk GetChunk(int x, int y, int z)
    {
        x = x / chunksize;
        y = y / chunksize;
        z = z / chunksize;
        ServerChunk chunk = GetChunkValid(x, y, z);

        if (chunk == null)
        {
            wasChunkGenerated = true;
            byte[] serializedChunk = ChunkDb.GetChunk(d_ChunkDb, x, y, z);
            if (serializedChunk != null)
            {
                SetChunkValid(x, y, z, DeserializeChunk(serializedChunk));
                //todo get heightmap from disk
                UpdateChunkHeight(x, y, z);
                return(GetChunkValid(x, y, z));
            }

            // get chunk
            ushort[] newchunk = new ushort[chunksize * chunksize * chunksize];
            for (int i = 0; i < server.modEventHandlers.getchunk.Count; i++)
            {
                server.modEventHandlers.getchunk[i](x, y, z, newchunk);
            }
            SetChunkValid(x, y, z, new ServerChunk()
            {
                data = newchunk
            });
            GetChunkValid(x, y, z).DirtyForSaving = true;
            UpdateChunkHeight(x, y, z);
            return(GetChunkValid(x, y, z));
        }
        return(chunk);
    }
Ejemplo n.º 5
0
 public void DeleteChunk(int x, int y, int z)
 {
     if (MapUtil.IsValidPos(d_Map, x, y, z))
     {
         x = x / chunksize;
         y = y / chunksize;
         z = z / chunksize;
         ChunkDb.DeleteChunk(d_ChunkDb, x, y, z);
         d_Map.SetChunkValid(x, y, z, null);
         // update related chunk at clients
         foreach (var k in clients)
         {
             //todo wrong
             //k.Value.chunksseen.Clear();
             Array.Clear(k.Value.chunksseen, 0, k.Value.chunksseen.Length);
         }
     }
 }
Ejemplo n.º 6
0
        public byte[] GetChunk(int x, int y, int z)
        {
            x = x / chunksize;
            y = y / chunksize;
            z = z / chunksize;
            Chunk chunk = chunks[x, y, z];

            if (chunk == null)
            {
                byte[] serializedChunk = ChunkDb.GetChunk(d_ChunkDb, x, y, z);
                if (serializedChunk != null)
                {
                    chunks[x, y, z] = DeserializeChunk(serializedChunk);
                    //todo get heightmap from disk
                    UpdateChunkHeight(x, y, z);
                    return(chunks[x, y, z].data);
                }

                // update chunk size and get chunk
                d_Generator.ChunkSize = chunksize;
                byte[, ,] newchunk    = d_Generator.GetChunk(x, y, z);
                if (newchunk != null)
                {
                    chunks[x, y, z] = new Chunk()
                    {
                        data = MapUtil.ToFlatMap(newchunk)
                    };
                }
                else
                {
                    chunks[x, y, z] = new Chunk()
                    {
                        data = new byte[chunksize * chunksize * chunksize]
                    };
                }
                chunks[x, y, z].DirtyForSaving = true;
                UpdateChunkHeight(x, y, z);
                return(chunks[x, y, z].data);
            }
            return(chunk.data);
        }