Skip to content

Commit

Permalink
Merge remote-tracking branch 'nethergames/stable' into stable
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.json
#	composer.lock
#	src/network/mcpe/ChunkRequestTask.php
#	src/network/mcpe/serializer/ChunkSerializer.php
  • Loading branch information
DavyCraft648 committed Feb 7, 2024
2 parents 45c3f00 + 1ca275e commit 3b12f8e
Show file tree
Hide file tree
Showing 44 changed files with 193 additions and 109 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
"composer-runtime-api": "^2.0",
"adhocore/json-comment": "~1.2.0",
"pocketmine/netresearch-jsonmapper": "~v4.2.1000",
"pocketmine/bedrock-block-upgrade-schema": "~3.5.0+bedrock-1.20.60",
"pocketmine/bedrock-block-upgrade-schema": "~3.5.0+bedrock-1.20.60",
"nethergamesmc/bedrock-data": "dev-master",
"pocketmine/bedrock-item-upgrade-schema": "~1.7.0+bedrock-1.20.60",
"pocketmine/bedrock-item-upgrade-schema": "~1.7.0+bedrock-1.20.60",
"nethergamesmc/bedrock-protocol": "dev-master",
"pocketmine/binaryutils": "^0.2.1",
"pocketmine/callback-validator": "^1.0.2",
Expand Down
9 changes: 5 additions & 4 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -1410,13 +1410,14 @@ public function broadcastTitle(string $title, string $subtitle = "", int $fadeIn
*
* @param bool|null $sync Compression on the main thread (true) or workers (false). Default is automatic (null).
*/
public function prepareBatch(string $buffer, Compressor $compressor, ?bool $sync = null, ?TimingsHandler $timings = null) : CompressBatchPromise|string{
public function prepareBatch(string $buffer, PacketSerializerContext $packetSerializerContext, Compressor $compressor, ?bool $sync = null, ?TimingsHandler $timings = null) : CompressBatchPromise|string{
$timings ??= Timings::$playerNetworkSendCompress;
try{
$timings->startTiming();

$threshold = $compressor->getCompressionThreshold();
if($threshold === null || strlen($buffer) < $compressor->getCompressionThreshold()){
$protocolId = $packetSerializerContext->getProtocolId();
if(($threshold === null || strlen($buffer) < $compressor->getCompressionThreshold()) && $protocolId >= ProtocolInfo::PROTOCOL_1_20_60){
$compressionType = CompressionAlgorithm::NONE;
$compressed = $buffer;

Expand All @@ -1425,7 +1426,7 @@ public function prepareBatch(string $buffer, Compressor $compressor, ?bool $sync

if(!$sync && strlen($buffer) >= $this->networkCompressionAsyncThreshold){
$promise = new CompressBatchPromise();
$task = new CompressBatchTask($buffer, $promise, $compressor);
$task = new CompressBatchTask($buffer, $promise, $compressor, $protocolId);
$this->asyncPool->submitTask($task);
return $promise;
}
Expand All @@ -1434,7 +1435,7 @@ public function prepareBatch(string $buffer, Compressor $compressor, ?bool $sync
$compressed = $compressor->compress($buffer);
}

return chr($compressionType) . $compressed;
return ($protocolId >= ProtocolInfo::PROTOCOL_1_20_60 ? chr($compressionType) : '') . $compressed;
}finally{
$timings->stopTiming();
}
Expand Down
5 changes: 5 additions & 0 deletions src/data/bedrock/BedrockDataFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@ private function __construct(){
public const BLOCK_ID_TO_ITEM_ID_MAP_1_20_0_JSON = BEDROCK_DATA_PATH . '/block_id_to_item_id_map-1.20.0.json';
public const BLOCK_ID_TO_ITEM_ID_MAP_1_20_10_JSON = BEDROCK_DATA_PATH . '/block_id_to_item_id_map-1.20.10.json';
public const BLOCK_ID_TO_ITEM_ID_MAP_1_20_40_JSON = BEDROCK_DATA_PATH . '/block_id_to_item_id_map-1.20.40.json';
public const BLOCK_ID_TO_ITEM_ID_MAP_1_20_50_JSON = BEDROCK_DATA_PATH . '/block_id_to_item_id_map-1.20.50.json';
public const BLOCK_ID_TO_ITEM_ID_MAP_JSON = BEDROCK_DATA_PATH . '/block_id_to_item_id_map.json';
public const BLOCK_STATE_META_MAP_1_20_0_JSON = BEDROCK_DATA_PATH . '/block_state_meta_map-1.20.0.json';
public const BLOCK_STATE_META_MAP_1_20_10_JSON = BEDROCK_DATA_PATH . '/block_state_meta_map-1.20.10.json';
public const BLOCK_STATE_META_MAP_1_20_30_JSON = BEDROCK_DATA_PATH . '/block_state_meta_map-1.20.30.json';
public const BLOCK_STATE_META_MAP_1_20_40_JSON = BEDROCK_DATA_PATH . '/block_state_meta_map-1.20.40.json';
public const BLOCK_STATE_META_MAP_1_20_50_JSON = BEDROCK_DATA_PATH . '/block_state_meta_map-1.20.50.json';
public const BLOCK_STATE_META_MAP_JSON = BEDROCK_DATA_PATH . '/block_state_meta_map.json';
public const CANONICAL_BLOCK_STATES_1_20_0_NBT = BEDROCK_DATA_PATH . '/canonical_block_states-1.20.0.nbt';
public const CANONICAL_BLOCK_STATES_1_20_10_NBT = BEDROCK_DATA_PATH . '/canonical_block_states-1.20.10.nbt';
public const CANONICAL_BLOCK_STATES_1_20_30_NBT = BEDROCK_DATA_PATH . '/canonical_block_states-1.20.30.nbt';
public const CANONICAL_BLOCK_STATES_1_20_40_NBT = BEDROCK_DATA_PATH . '/canonical_block_states-1.20.40.nbt';
public const CANONICAL_BLOCK_STATES_1_20_50_NBT = BEDROCK_DATA_PATH . '/canonical_block_states-1.20.50.nbt';
public const CANONICAL_BLOCK_STATES_NBT = BEDROCK_DATA_PATH . '/canonical_block_states.nbt';
public const COMMAND_ARG_TYPES_JSON = BEDROCK_DATA_PATH . '/command_arg_types.json';
public const CREATIVEITEMS_JSON = BEDROCK_DATA_PATH . '/creativeitems.json';
Expand All @@ -60,10 +63,12 @@ private function __construct(){
public const R12_TO_CURRENT_BLOCK_MAP_1_20_10_BIN = BEDROCK_DATA_PATH . '/r12_to_current_block_map-1.20.10.bin';
public const R12_TO_CURRENT_BLOCK_MAP_1_20_30_BIN = BEDROCK_DATA_PATH . '/r12_to_current_block_map-1.20.30.bin';
public const R12_TO_CURRENT_BLOCK_MAP_1_20_40_BIN = BEDROCK_DATA_PATH . '/r12_to_current_block_map-1.20.40.bin';
public const R12_TO_CURRENT_BLOCK_MAP_1_20_50_BIN = BEDROCK_DATA_PATH . '/r12_to_current_block_map-1.20.50.bin';
public const R12_TO_CURRENT_BLOCK_MAP_BIN = BEDROCK_DATA_PATH . '/r12_to_current_block_map.bin';
public const R16_TO_CURRENT_ITEM_MAP_JSON = BEDROCK_DATA_PATH . '/r16_to_current_item_map.json';
public const REQUIRED_ITEM_LIST_1_20_0_JSON = BEDROCK_DATA_PATH . '/required_item_list-1.20.0.json';
public const REQUIRED_ITEM_LIST_1_20_10_JSON = BEDROCK_DATA_PATH . '/required_item_list-1.20.10.json';
public const REQUIRED_ITEM_LIST_1_20_40_JSON = BEDROCK_DATA_PATH . '/required_item_list-1.20.40.json';
public const REQUIRED_ITEM_LIST_1_20_50_JSON = BEDROCK_DATA_PATH . '/required_item_list-1.20.50.json';
public const REQUIRED_ITEM_LIST_JSON = BEDROCK_DATA_PATH . '/required_item_list.json';
}
1 change: 1 addition & 0 deletions src/data/bedrock/ItemTagToIdMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ final class ItemTagToIdMap{

private const PATHS = [
ProtocolInfo::CURRENT_PROTOCOL => "",
ProtocolInfo::PROTOCOL_1_20_50 => "",
ProtocolInfo::PROTOCOL_1_20_40 => "-1.20.0",
ProtocolInfo::PROTOCOL_1_20_30 => "-1.20.0",
ProtocolInfo::PROTOCOL_1_20_10 => "-1.20.0",
Expand Down
29 changes: 22 additions & 7 deletions src/network/mcpe/CachedChunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@
use BadFunctionCallException;
use pocketmine\network\mcpe\compression\Compressor;
use pocketmine\network\mcpe\protocol\LevelChunkPacket;
use pocketmine\network\mcpe\protocol\ProtocolInfo;
use pocketmine\network\mcpe\protocol\serializer\PacketBatch;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializerContext;
use pocketmine\network\mcpe\protocol\types\ChunkPosition;
use pocketmine\network\mcpe\protocol\types\DimensionIds;
use pocketmine\utils\BinaryStream;
use function chr;
use function count;
use function strlen;

Expand Down Expand Up @@ -79,14 +82,18 @@ public function getHashMap() : array{
return $map;
}

public function compressPackets(int $chunkX, int $chunkZ, string $chunkData, Compressor $compressor, PacketSerializerContext $encoderContext) : void{
/**
* @phpstan-param DimensionIds::* $dimensionId
*/
public function compressPackets(int $chunkX, int $chunkZ, int $dimensionId, string $chunkData, Compressor $compressor, PacketSerializerContext $encoderContext, int $protocolId) : void{
$protocolAddition = $protocolId >= ProtocolInfo::PROTOCOL_1_20_60 ? chr($compressor->getNetworkId()) : '';
$stream = new BinaryStream();
PacketBatch::encodePackets($stream, $encoderContext, [$this->createPacket($chunkX, $chunkZ, $chunkData)]);
$this->packet = $compressor->compress($stream->getBuffer());
PacketBatch::encodePackets($stream, $encoderContext, [$this->createPacket($chunkX, $chunkZ, $dimensionId, $chunkData)]);
$this->packet = $protocolAddition . $compressor->compress($stream->getBuffer());

$stream = new BinaryStream();
PacketBatch::encodePackets($stream, $encoderContext, [$this->createCachablePacket($chunkX, $chunkZ, $chunkData)]);
$this->cachablePacket = $compressor->compress($stream->getBuffer());
PacketBatch::encodePackets($stream, $encoderContext, [$this->createCachablePacket($chunkX, $chunkZ, $dimensionId, $chunkData)]);
$this->cachablePacket = $protocolAddition . $compressor->compress($stream->getBuffer());
}

public function getCacheablePacket() : string{
Expand All @@ -105,7 +112,10 @@ public function getPacket() : string{
return $this->packet;
}

private function createPacket(int $chunkX, int $chunkZ, string $chunkData) : LevelChunkPacket{
/**
* @phpstan-param DimensionIds::* $dimensionId
*/
private function createPacket(int $chunkX, int $chunkZ, int $dimensionId, string $chunkData) : LevelChunkPacket{
$stream = new BinaryStream();

foreach($this->blobs as $subChunk){
Expand All @@ -116,16 +126,21 @@ private function createPacket(int $chunkX, int $chunkZ, string $chunkData) : Lev

return LevelChunkPacket::create(
new ChunkPosition($chunkX, $chunkZ),
$dimensionId,
count($this->hashes),
false,
null,
$stream->getBuffer()
);
}

private function createCachablePacket(int $chunkX, int $chunkZ, string $chunkData) : LevelChunkPacket{
/**
* @phpstan-param DimensionIds::* $dimensionId
*/
private function createCachablePacket(int $chunkX, int $chunkZ, int $dimensionId, string $chunkData) : LevelChunkPacket{
return LevelChunkPacket::create(
new ChunkPosition($chunkX, $chunkZ),
$dimensionId,
count($this->hashes),
false,
$this->getHashes(),
Expand Down
7 changes: 4 additions & 3 deletions src/network/mcpe/ChunkRequestTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
use pocketmine\utils\Binary;
use pocketmine\world\format\Chunk;
use pocketmine\world\format\io\FastChunkSerializer;
use function chr;
/** @phpstan-ignore-next-line */
use function xxhash64;

Expand Down Expand Up @@ -75,14 +74,14 @@ public function onRun() : void{
$converter = TypeConverter::getInstance($this->mappingProtocol);
$encoderContext = new PacketSerializerContext($converter->getItemTypeDictionary(), $this->mappingProtocol);

foreach(ChunkSerializer::serializeSubChunks($chunk, $converter->getBlockTranslator(), $encoderContext) as $subChunk){
foreach(ChunkSerializer::serializeSubChunks($chunk, $this->dimensionId, $converter->getBlockTranslator(), $encoderContext) as $subChunk){
/** @phpstan-ignore-next-line */
$cache->addSubChunk(Binary::readLong(xxhash64($subChunk)), $subChunk);
}

$encoder = PacketSerializer::encoder($encoderContext);
$biomeEncoder = clone $encoder;
ChunkSerializer::serializeBiomes($chunk, $biomeEncoder);
ChunkSerializer::serializeBiomes($chunk, $this->dimensionId, $biomeEncoder);
/** @phpstan-ignore-next-line */
$cache->setBiomes(Binary::readLong(xxhash64($chunkBuffer = $biomeEncoder->getBuffer())), $chunkBuffer);

Expand All @@ -92,9 +91,11 @@ public function onRun() : void{
$cache->compressPackets(
$this->chunkX,
$this->chunkZ,
$this->dimensionId,
$chunkDataEncoder->getBuffer(),
$this->compressor->deserialize(),
$encoderContext,
$this->mappingProtocol
);

$this->setResult($cache);
Expand Down
31 changes: 21 additions & 10 deletions src/network/mcpe/NetworkSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,21 +413,32 @@ public function handleEncoded(string $payload) : void{

if($this->enableCompression){
Timings::$playerNetworkReceiveDecompress->startTiming();
$compressionType = ord($payload[0]);
$compressed = substr($payload, 1);
if($compressionType === CompressionAlgorithm::NONE){
$decompressed = $compressed;
}elseif($compressionType === $this->compressor->getNetworkId()){
if($this->protocolId >= ProtocolInfo::PROTOCOL_1_20_60){
$compressionType = ord($payload[0]);
$compressed = substr($payload, 1);
if($compressionType === CompressionAlgorithm::NONE){
$decompressed = $compressed;
}elseif($compressionType === $this->compressor->getNetworkId()){
try{
$decompressed = $this->compressor->decompress($compressed);
}catch(DecompressionException $e){
$this->logger->debug("Failed to decompress packet: " . base64_encode($compressed));
throw PacketHandlingException::wrap($e, "Compressed packet batch decode error");
}finally{
Timings::$playerNetworkReceiveDecompress->stopTiming();
}
}else{
throw new PacketHandlingException("Packet compressed with unexpected compression type $compressionType");
}
}else{
try{
$decompressed = $this->compressor->decompress($compressed);
$decompressed = $this->compressor->decompress($payload);
}catch(DecompressionException $e){
$this->logger->debug("Failed to decompress packet: " . base64_encode($compressed));
$this->logger->debug("Failed to decompress packet: " . base64_encode($payload));
throw PacketHandlingException::wrap($e, "Compressed packet batch decode error");
}finally{
Timings::$playerNetworkReceiveDecompress->stopTiming();
}
}else{
throw new PacketHandlingException("Packet compressed with unexpected compression type $compressionType");
}
}else{
$decompressed = $payload;
Expand Down Expand Up @@ -592,7 +603,7 @@ private function flushSendBuffer(bool $immediate = false) : void{
PacketBatch::encodeRaw($stream, $this->sendBuffer);

if($this->enableCompression){
$batch = $this->server->prepareBatch($stream->getBuffer(), $this->compressor, $syncMode, Timings::$playerNetworkSendCompressSessionBuffer);
$batch = $this->server->prepareBatch($stream->getBuffer(), $this->packetSerializerContext, $this->compressor, $syncMode, Timings::$playerNetworkSendCompressSessionBuffer);
}else{
$batch = $stream->getBuffer();
}
Expand Down
2 changes: 1 addition & 1 deletion src/network/mcpe/StandardPacketBroadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function broadcastPackets(array $recipients, array $packets) : void{
PacketBatch::encodeRaw($stream, $packetBuffers);
$batchBuffer = $stream->getBuffer();

$batch = $this->server->prepareBatch($batchBuffer, $compressor, timings: Timings::$playerNetworkSendCompressBroadcast);
$batch = $this->server->prepareBatch($batchBuffer, $this->protocolContext, $compressor, timings: Timings::$playerNetworkSendCompressBroadcast);
foreach($compressorTargets as $target){
$target->queueCompressed($batch);
}
Expand Down
7 changes: 5 additions & 2 deletions src/network/mcpe/compression/CompressBatchTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace pocketmine\network\mcpe\compression;

use pocketmine\network\mcpe\protocol\ProtocolInfo;
use pocketmine\scheduler\AsyncTask;
use pocketmine\thread\NonThreadSafeValue;
use function chr;
Expand All @@ -37,15 +38,17 @@ class CompressBatchTask extends AsyncTask{
public function __construct(
private string $data,
CompressBatchPromise $promise,
Compressor $compressor
Compressor $compressor,
private int $protocolId
){
$this->compressor = new NonThreadSafeValue($compressor);
$this->storeLocal(self::TLS_KEY_PROMISE, $promise);
}

public function onRun() : void{
$compressor = $this->compressor->deserialize();
$this->setResult(chr($compressor->getNetworkId()) . $compressor->compress($this->data));
$protocolAddition = $this->protocolId >= ProtocolInfo::PROTOCOL_1_20_60 ? chr($compressor->getNetworkId()) : '';
$this->setResult($protocolAddition . $compressor->compress($this->data));
}

public function onCompletion() : void{
Expand Down
4 changes: 4 additions & 0 deletions src/network/mcpe/convert/BlockTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ final class BlockTranslator{
self::CANONICAL_BLOCK_STATES_PATH => '',
self::BLOCK_STATE_META_MAP_PATH => '',
],
ProtocolInfo::PROTOCOL_1_20_50 => [
self::CANONICAL_BLOCK_STATES_PATH => '-1.20.50',
self::BLOCK_STATE_META_MAP_PATH => '-1.20.50',
],
ProtocolInfo::PROTOCOL_1_20_40 => [
self::CANONICAL_BLOCK_STATES_PATH => '-1.20.40',
self::BLOCK_STATE_META_MAP_PATH => '-1.20.40',
Expand Down
2 changes: 2 additions & 0 deletions src/network/mcpe/convert/ItemTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ public function fromNetworkId(int $networkId, int $networkMeta, int $networkBloc

public static function getItemSchemaId(int $protocolId) : int{
return match($protocolId){
ProtocolInfo::PROTOCOL_1_20_60 => 161,

ProtocolInfo::PROTOCOL_1_20_50 => 151,

ProtocolInfo::PROTOCOL_1_20_40,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ final class ItemTypeDictionaryFromDataHelper{

private const PATHS = [
ProtocolInfo::CURRENT_PROTOCOL => "",
ProtocolInfo::PROTOCOL_1_20_50 => "-1.20.50",
ProtocolInfo::PROTOCOL_1_20_40 => "-1.20.40",
ProtocolInfo::PROTOCOL_1_20_30 => "-1.20.40",
ProtocolInfo::PROTOCOL_1_20_10 => "-1.20.10",
Expand Down
Loading

0 comments on commit 3b12f8e

Please sign in to comment.