Skip to content

Commit

Permalink
Merge branch 'bennydiamond-Fix_Crc32'
Browse files Browse the repository at this point in the history
  • Loading branch information
mjungnickel18 committed Jun 7, 2020
2 parents b2c4102 + bcb877b commit 7c8adc4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
1 change: 1 addition & 0 deletions Maploader/World/SubChunkData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class SubChunkData
{
public byte[] Key { get; set; }
public byte[] Data { get; set; }
public int DataLength { get; set; }
public UInt32 Crc32 { get; set; }
public byte Index { get; set; }
public bool FoundInDb { get; set; }
Expand Down
31 changes: 18 additions & 13 deletions Maploader/World/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,17 @@ public ChunkData GetOverworldChunkData(int x, int z)
for (byte subChunkIdx = 0; subChunkIdx < 15; subChunkIdx++)
{
key[9] = subChunkIdx;

var data = db.Get(key);
UIntPtr length;
var data = db.Get(key, out length);
if (data != null)
{
var subChunkData = new SubChunkData()
{
Index = subChunkIdx,
Data = data,
DataLength = (int)length,
Key = key,
Crc32 = Force.Crc32.Crc32CAlgorithm.Compute(data)
Crc32 = Force.Crc32.Crc32CAlgorithm.Compute(data, 0, (int)length)
};
ret.SubChunks.Add(subChunkData);
}
Expand Down Expand Up @@ -132,8 +133,8 @@ public Chunk GetChunk(int x, int z)
for (byte subChunkIdx = 0; subChunkIdx < 15; subChunkIdx++)
{
key[9] = subChunkIdx;

var data = db.Get(key);
UIntPtr length;
var data = db.Get(key, out length);
if (data != null)
{
subChunks[subChunkIdx] = data;
Expand Down Expand Up @@ -493,16 +494,17 @@ public ChunkData GetChunkData(GroupedChunkSubKeys groupedChunkSubKeys)
foreach (var kvp in groupedChunkSubKeys.Subchunks)
{
var key = kvp.Value;

var data = db.Get(key.Key);
UIntPtr length;
var data = db.Get(key.Key, out length);
if (data != null)
{
var subChunkData = new SubChunkData()
{
Index = kvp.Key,
Data = data,
DataLength = (int)length,
Key = kvp.Value.Key,
Crc32 = Force.Crc32.Crc32CAlgorithm.Compute(data),
Crc32 = Force.Crc32.Crc32CAlgorithm.Compute(data, 0, (int)length),
};
ret.SubChunks.Add(subChunkData);
}
Expand Down Expand Up @@ -531,16 +533,17 @@ public ChunkData GetChunkData(IGrouping<ulong, LevelDbWorldKey2> groupedChunkSub
foreach (var kvp in groupedChunkSubKeys)
{
var key = kvp;

var data = db.Get(key.Key);
UIntPtr length;
var data = db.Get(key.Key, out length);
if (data != null)
{
var subChunkData = new SubChunkData()
{
Index = key.SubChunkId,
Data = data,
DataLength = (int)length,
Key = key.Key,
// Crc32 = Force.Crc32.Crc32CAlgorithm.Compute(data),
// Crc32 = Force.Crc32.Crc32CAlgorithm.Compute(data, 0, (int)length),
};
ret.SubChunks.Add(subChunkData);
}
Expand All @@ -566,15 +569,17 @@ public ChunkData GetChunkData(int x, int z)
var key = CreateKey(x, z);
key[9] = (byte)kvp;

var data = db.Get(key);
UIntPtr length;
var data = db.Get(key, out length);
if (data != null)
{
var subChunkData = new SubChunkData()
{
Index = (byte)kvp,
Data = data,
DataLength = (int)length,
Key = key,
Crc32 = Force.Crc32.Crc32CAlgorithm.Compute(data),
Crc32 = Force.Crc32.Crc32CAlgorithm.Compute(data, 0, (int)length),
};
ret.SubChunks.Add(subChunkData);
}
Expand Down
11 changes: 6 additions & 5 deletions leveldb-sharp-std/DB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public class DB : IDisposable, IEnumerable<KeyValuePair<byte[], byte[]>>

public byte[] this[byte[] key] {
get {
return Get(null, key);
UIntPtr unusedLength;
return Get(null, key, out unusedLength);
}
set {
Put(null, key, value);
Expand Down Expand Up @@ -183,18 +184,18 @@ public void Write(WriteBatch writeBatch)
Write(null, writeBatch);
}

public byte[] Get(ReadOptions options, byte[] key)
public byte[] Get(ReadOptions options, byte[] key, out UIntPtr length)
{
CheckDisposed();
if (options == null) {
options = new ReadOptions();
}
return Native.leveldb_get(Handle, options.Handle, key);
return Native.leveldb_get(Handle, options.Handle, key, out length);
}

public byte[] Get(byte[] key)
public byte[] Get(byte[] key, out UIntPtr length)
{
return Get(null, key);
return Get(null, key, out length);
}

IEnumerator IEnumerable.GetEnumerator()
Expand Down
4 changes: 2 additions & 2 deletions leveldb-sharp-std/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,15 @@ public static extern IntPtr leveldb_get(IntPtr db,

public static byte[] leveldb_get(IntPtr db,
IntPtr readOptions,
byte[] key)
byte[] key,
out UIntPtr valueLength)
{


unsafe
{
fixed (byte* p = key)
{
UIntPtr valueLength;
string error;
var keyLength = (UIntPtr) key.Length;
IntPtr keyPtr = (IntPtr)p;
Expand Down

0 comments on commit 7c8adc4

Please sign in to comment.