Improve ReadBlockStatesField

This commit is contained in:
BruceChen 2022-08-29 23:39:03 +08:00
parent 003c4c3ab8
commit 68b9c81c0b
3 changed files with 25 additions and 25 deletions

View file

@ -46,6 +46,12 @@ namespace MinecraftClient.Protocol.Handlers
return result; return result;
} }
public void ReadDataReverse(Queue<byte> cache, Span<byte> dest)
{
for (int i = (dest.Length - 1); i >= 0; --i)
dest[i] = cache.Dequeue();
}
/// <summary> /// <summary>
/// Remove some data from the cache /// Remove some data from the cache
/// </summary> /// </summary>

View file

@ -554,12 +554,8 @@ namespace MinecraftClient.Protocol.Handlers
int dataSize = dataTypes.ReadNextVarInt(packetData); // Size int dataSize = dataTypes.ReadNextVarInt(packetData); // Size
Parallel.Invoke(() => if (pTerrain.ProcessChunkColumnData(chunkX, chunkZ, verticalStripBitmask, packetData, cancellationToken))
{ Interlocked.Decrement(ref handler.GetWorld().chunkLoadNotCompleted);
bool loaded = pTerrain.ProcessChunkColumnData(chunkX, chunkZ, verticalStripBitmask, packetData, cancellationToken);
if (loaded)
Interlocked.Decrement(ref handler.GetWorld().chunkLoadNotCompleted);
});
// Block Entity data: ignored // Block Entity data: ignored
// Light data: ignored // Light data: ignored
@ -578,12 +574,9 @@ namespace MinecraftClient.Protocol.Handlers
int compressedDataSize = dataTypes.ReadNextInt(packetData); int compressedDataSize = dataTypes.ReadNextInt(packetData);
byte[] compressed = dataTypes.ReadData(compressedDataSize, packetData); byte[] compressed = dataTypes.ReadData(compressedDataSize, packetData);
byte[] decompressed = ZlibUtils.Decompress(compressed); byte[] decompressed = ZlibUtils.Decompress(compressed);
Parallel.Invoke(() =>
{ if (pTerrain.ProcessChunkColumnData(chunkX, chunkZ, chunkMask, addBitmap, currentDimension == 0, chunksContinuous, currentDimension, new Queue<byte>(decompressed), cancellationToken))
bool loaded = pTerrain.ProcessChunkColumnData(chunkX, chunkZ, chunkMask, addBitmap, currentDimension == 0, chunksContinuous, currentDimension, new Queue<byte>(decompressed), cancellationToken); Interlocked.Decrement(ref handler.GetWorld().chunkLoadNotCompleted);
if (loaded)
Interlocked.Decrement(ref handler.GetWorld().chunkLoadNotCompleted);
});
} }
else else
{ {
@ -607,12 +600,9 @@ namespace MinecraftClient.Protocol.Handlers
else dataTypes.DropData(1024 * 4, packetData); // Biomes - 1.15 and above else dataTypes.DropData(1024 * 4, packetData); // Biomes - 1.15 and above
} }
int dataSize = dataTypes.ReadNextVarInt(packetData); int dataSize = dataTypes.ReadNextVarInt(packetData);
Parallel.Invoke(() =>
{ if (pTerrain.ProcessChunkColumnData(chunkX, chunkZ, chunkMask, 0, false, chunksContinuous, currentDimension, packetData, cancellationToken))
bool loaded = pTerrain.ProcessChunkColumnData(chunkX, chunkZ, chunkMask, 0, false, chunksContinuous, currentDimension, packetData, cancellationToken); Interlocked.Decrement(ref handler.GetWorld().chunkLoadNotCompleted);
if (loaded)
Interlocked.Decrement(ref handler.GetWorld().chunkLoadNotCompleted);
});
} }
} }
} }

View file

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
//using System.Linq; //using System.Linq;
@ -69,6 +70,8 @@ namespace MinecraftClient.Protocol.Handlers
// Indirect Mode: For block states with bits per entry <= 4, 4 bits are used to represent a block. // Indirect Mode: For block states with bits per entry <= 4, 4 bits are used to represent a block.
if (bitsPerEntry < 4) bitsPerEntry = 4; if (bitsPerEntry < 4) bitsPerEntry = 4;
int entryPerLong = 64 / bitsPerEntry; // entryPerLong = sizeof(long) / bitsPerEntry
// Direct Mode: Bit mask covering bitsPerEntry bits // Direct Mode: Bit mask covering bitsPerEntry bits
// EG, if bitsPerEntry = 5, valueMask = 00011111 in binary // EG, if bitsPerEntry = 5, valueMask = 00011111 in binary
uint valueMask = (uint)((1 << bitsPerEntry) - 1); uint valueMask = (uint)((1 << bitsPerEntry) - 1);
@ -76,15 +79,16 @@ namespace MinecraftClient.Protocol.Handlers
int paletteLength = 0; // Assume zero when length is absent int paletteLength = 0; // Assume zero when length is absent
if (usePalette) paletteLength = dataTypes.ReadNextVarInt(cache); if (usePalette) paletteLength = dataTypes.ReadNextVarInt(cache);
int[] palette = new int[paletteLength]; Span<uint> palette = paletteLength < 256 ? stackalloc uint[paletteLength] : new uint[paletteLength];
for (int i = 0; i < paletteLength; i++) for (int i = 0; i < paletteLength; i++)
palette[i] = dataTypes.ReadNextVarInt(cache); palette[i] = (uint)dataTypes.ReadNextVarInt(cache);
// Block IDs are packed in the array of 64-bits integers //// Block IDs are packed in the array of 64-bits integers
ulong[] dataArray = dataTypes.ReadNextULongArray(cache); dataTypes.SkipNextVarInt(cache);
Span<byte> entryDataByte = stackalloc byte[8];
dataTypes.ReadDataReverse(cache, entryDataByte); // read long
Chunk chunk = new(); Chunk chunk = new();
int longIndex = 0;
int startOffset = 0 - bitsPerEntry; int startOffset = 0 - bitsPerEntry;
for (int blockY = 0; blockY < Chunk.SizeY; blockY++) for (int blockY = 0; blockY < Chunk.SizeY; blockY++)
{ {
@ -103,12 +107,12 @@ namespace MinecraftClient.Protocol.Handlers
// When overlapping, move forward to the beginning of the next Long // When overlapping, move forward to the beginning of the next Long
startOffset = 0; startOffset = 0;
longIndex++; dataTypes.ReadDataReverse(cache, entryDataByte); // read long
} }
// NOTICE: In the future a single ushort may not store the entire block id; // NOTICE: In the future a single ushort may not store the entire block id;
// the Block class may need to change if block state IDs go beyond 65535 // the Block class may need to change if block state IDs go beyond 65535
ushort blockId = (ushort)((dataArray[longIndex] >> startOffset) & valueMask); ushort blockId = (ushort)((MemoryMarshal.Read<long>(entryDataByte) >> startOffset) & valueMask);
// Map small IDs to actual larger block IDs // Map small IDs to actual larger block IDs
if (usePalette) if (usePalette)