Fix some more things to do with last commit

This commit is contained in:
UnknownShadow200 2017-11-08 22:33:17 +11:00
parent 4ad3c9f3ab
commit eee5c60ab3
3 changed files with 29 additions and 21 deletions

View File

@ -86,7 +86,7 @@ namespace MCGalaxy.DB {
int count = Math.Min(remaining, BulkEntries); int count = Math.Min(remaining, BulkEntries);
if (count > 0) { if (count > 0) {
BlockDBFile.ReadFully(s, bulk, count * EntrySize); BlockDBFile.ReadFully(s, bulk, 0, count * EntrySize);
} }
return count; return count;
} }
@ -99,7 +99,7 @@ namespace MCGalaxy.DB {
if (count > 0) { if (count > 0) {
pos -= count * EntrySize; pos -= count * EntrySize;
s.Position = pos; s.Position = pos;
BlockDBFile.ReadFully(s, bulk, count * EntrySize); BlockDBFile.ReadFully(s, bulk, 0, count * EntrySize);
s.Position = pos; // set correct position for next backward read s.Position = pos; // set correct position for next backward read
} }
return count; return count;

View File

@ -22,9 +22,9 @@ using MCGalaxy.Util;
namespace MCGalaxy.DB { namespace MCGalaxy.DB {
public unsafe sealed class BlockDBFile_V2 : BlockDBFile { public unsafe sealed class BlockDBFile_V2 : BlockDBFile {
const int BlockSize = 4096; const int BlockSize = BlockDBFile.BulkEntries;
/* TODO: Last chunk may only be partial. need to prepend these entries when compressing. */ /* TODO: Last chunk in file may only be partially filled. need to prepend these entries when compressing more. */
public override void WriteEntries(Stream s, FastList<BlockDBEntry> entries) { public override void WriteEntries(Stream s, FastList<BlockDBEntry> entries) {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -36,7 +36,7 @@ namespace MCGalaxy.DB {
public override long CountEntries(Stream s) { public override long CountEntries(Stream s) {
byte[] data = new byte[8]; byte[] data = new byte[8];
s.Position = 16; s.Position = 16;
BlockDBFile.ReadFully(s, data, data.Length); BlockDBFile.ReadFully(s, data, 0, data.Length);
uint lo = (uint)ReadInt32(data, 0); uint lo = (uint)ReadInt32(data, 0);
uint hi = (uint)ReadInt32(data, 4); uint hi = (uint)ReadInt32(data, 4);
@ -44,32 +44,40 @@ namespace MCGalaxy.DB {
} }
public unsafe override int ReadForward(Stream s, byte[] bulk, BlockDBEntry* entriesPtr) { public unsafe override int ReadForward(Stream s, byte[] bulk, BlockDBEntry* entriesPtr) {
long remaining = s.Length - s.Position; long pos = s.Position;
// Version 2 expects all chunks to be aligned to 4096 bytes
if (pos < BlockSize) { s.Position = BlockSize; pos = BlockSize; }
long remaining = s.Length - pos;
if (remaining == 0) return 0; if (remaining == 0) return 0;
int bytes = (int)Math.Min(remaining, BlockSize);
/* TODO: BULK POINTS TO ENTRIESPTR, FIX THIS */ int bytes = (int)Math.Min(remaining, BlockSize);
BlockDBFile.ReadFully(s, bulk, bytes); int offset = bulk.Length - BlockSize;
return DecompressChunk(bulk, entriesPtr);
// NOTE: bulk and entriesPtr point to same thing
// But we read into the end of the bulk array, thus the entriesPtr pointing
// to start of array never ends up overlapping with the data being read
BlockDBFile.ReadFully(s, bulk, offset, bytes);
return DecompressChunk(bulk, offset, entriesPtr);
} }
public unsafe override int ReadBackward(Stream s, byte[] bulk, BlockDBEntry* entriesPtr) { public unsafe override int ReadBackward(Stream s, byte[] bulk, BlockDBEntry* entriesPtr) {
long pos = s.Position; long pos = s.Position;
if (pos > BlockSize) { if (pos > BlockSize) {
int bytes = (int)Math.Min(pos - BlockSize, BlockSize); int bytes = (int)Math.Min(pos - BlockSize, BlockSize);
int offset = bulk.Length - BlockSize;
pos -= bytes; pos -= bytes;
s.Position = pos; s.Position = pos;
BlockDBFile.ReadFully(s, bulk, bytes); BlockDBFile.ReadFully(s, bulk, offset, bytes);
s.Position = pos; // set correct position for next backward read s.Position = pos; // set correct position for next backward read
return DecompressChunk(bulk, entriesPtr); return DecompressChunk(bulk, offset, entriesPtr);
} }
return 0; return 0;
} }
unsafe static int DecompressChunk(byte[] bulk, BlockDBEntry* ptr) { unsafe static int DecompressChunk(byte[] bulk, int idx, BlockDBEntry* ptr) {
byte comp = bulk[0]; byte comp = bulk[idx]; idx++;
int count = bulk[1] | (bulk[2] << 8); int count = bulk[idx] | (bulk[idx + 1] << 8); idx += 2;
int idx = 3;
int playerID = 0; int playerID = 0;
if ((comp & 0x01) < 0x01) { playerID = ReadInt32(bulk, idx); idx += 4; } if ((comp & 0x01) < 0x01) { playerID = ReadInt32(bulk, idx); idx += 4; }

View File

@ -27,7 +27,7 @@ namespace MCGalaxy.DB {
public const byte Version = 1; public const byte Version = 1;
public const int EntrySize = 16; public const int EntrySize = 16;
public const int HeaderEntries = 1; public const int HeaderEntries = 1;
public const int BulkEntries = 2048; public const int BulkEntries = 4096;
public static BlockDBFile V1 = new BlockDBFile_V1(); public static BlockDBFile V1 = new BlockDBFile_V1();
@ -48,7 +48,7 @@ namespace MCGalaxy.DB {
public static BlockDBFile ReadHeader(Stream s, out Vec3U16 dims) { public static BlockDBFile ReadHeader(Stream s, out Vec3U16 dims) {
dims = default(Vec3U16); dims = default(Vec3U16);
byte[] header = new byte[EntrySize * HeaderEntries]; byte[] header = new byte[EntrySize * HeaderEntries];
ReadFully(s, header, header.Length); ReadFully(s, header, 0, header.Length);
// Check constants are expected // Check constants are expected
// TODO: check 8 byte string identifier // TODO: check 8 byte string identifier
@ -194,10 +194,10 @@ namespace MCGalaxy.DB {
array[index++] = (byte)(value >> 8); array[index++] = (byte)(value >> 8);
} }
internal static void ReadFully(Stream stream, byte[] dst, int count) { internal static void ReadFully(Stream stream, byte[] dst, int offset, int count) {
int total = 0; int total = 0;
do { do {
int read = stream.Read(dst, total, count - total); int read = stream.Read(dst, offset + total, count - total);
if (read == 0) throw new EndOfStreamException(); if (read == 0) throw new EndOfStreamException();
total += read; total += read;
} while (total < count); } while (total < count);