Esempio n. 1
0
 private async Task LoadChunk(FileStream saveFile, int x, int y, int seed)
 {
     byte[] row = await this.ReferenceTableLookup(saveFile, 0, x, y);
     if (row == null)
     {
         this.chunks.Add(new Chunk(x, y).Generate(seed));
     }
     else
     {
         Chunk chunk = new Chunk(x, y);
         byte[] buffer = new byte[4 * Chunk.size * Chunk.size];
         await saveFile.ReadAsync(buffer, BitConverter.ToInt32(row, 0), buffer.Length);
         for (int j = 0; j < Chunk.size; j++)
         {
             for (int k = 0; k < Chunk.size; k++)
             {
                 chunk.SetTile(k, j, BitConverter.ToInt32(buffer, (j * Chunk.size + k) * 4));
             }
         }
         this.chunks.Add(chunk);
     }
 }
Esempio n. 2
0
 private async Task UnloadChunk(FileStream saveFile, Chunk chunk)
 {
     byte[] buffer = new byte[Chunk.size * Chunk.size * 4];
     byte[] row = await this.ReferenceTableLookup(saveFile, 0, chunk.X, chunk.Y);
     for (int j = 0; j < Chunk.size; j++)
     {
         for (int k = 0; k < Chunk.size; k++)
         {
             BitConverter.GetBytes(chunk.GetTile(k, j)).CopyTo(buffer, (j * Chunk.size + k) * 4);
         }
     }
     
     if (row == null)
     {
         byte[] temp = new byte[4];
         int offset = 0;
         do
         {
             await saveFile.ReadAsync(temp, offset, 1);
             if (temp[0] == 255)
             {
                 await saveFile.ReadAsync(temp, offset + 1 + 256 * 16, 4);
                 offset = BitConverter.ToInt32(temp, 0);
             }
         }
         while (temp[0] == 255);
         byte[] refRow = new byte[16];
         BitConverter.GetBytes(chunk.X).CopyTo(refRow, 0);
         BitConverter.GetBytes(chunk.Y).CopyTo(refRow, 4);
         BitConverter.GetBytes(saveFile.Length).CopyTo(refRow, 8);
         await saveFile.WriteAsync(buffer, (int)saveFile.Length, buffer.Length);
         //TODO: Fix reference point
         BitConverter.GetBytes(0).CopyTo(refRow, 12);
         await saveFile.WriteAsync(refRow, offset + temp[0] * 16, 16);
     }
     else
     {
         await saveFile.WriteAsync(buffer, BitConverter.ToInt32(row, 0), buffer.Length);
     }
     saveFile.Close();
 }