Skip to content

Commit

Permalink
Add protocolId to PacketSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
dries-c committed Mar 1, 2024
1 parent 4260884 commit 6793560
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 31 deletions.
30 changes: 17 additions & 13 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,6 @@ class Server{
*/
private array $broadcastSubscribers = [];

/** @var array<int, PacketSerializerContext> */
private array $packetSerializerContexts = [];
/** @var array<int, PacketBroadcaster> */
private array $packetBroadcasters = [];
/** @var array<string, EntityEventBroadcaster> */
Expand Down Expand Up @@ -606,8 +604,7 @@ public function createPlayer(NetworkSession $session, PlayerInfo $playerInfo, bo
$playerPromiseResolver->reject();
};

$promise = Promise::all($promises->toArray());
$promise->onCompletion(function () use ($playerPos, $world, $createPlayer, $playerCreationRejected) : void{
$playerCreationSucceeded = function () use ($playerPos, $world, $createPlayer, $playerCreationRejected) : void{
if($playerPos === null){ //new player or no valid position due to world not being loaded
$world->requestSafeSpawn()->onCompletion(
function(Position $spawn) use ($createPlayer, $world) : void{
Expand All @@ -620,9 +617,17 @@ function() use ($playerCreationRejected) : void{
}else{ //returning player with a valid position - safe spawn not required
$createPlayer($playerPos);
}
}, function () use ($playerCreationRejected) : void{
$playerCreationRejected("Failed to create player");
});
};

if(count($promises = $promises->toArray()) > 0){
$promise = Promise::all($promises);

Check failure on line 623 in src/Server.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Parameter #1 $promises of static method pocketmine\promise\Promise<mixed>::all() expects non-empty-array<int, pocketmine\promise\Promise<null>>, array<int, pocketmine\promise\Promise<null>> given.

Check failure on line 623 in src/Server.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Parameter #1 $promises of static method pocketmine\promise\Promise<mixed>::all() expects non-empty-array<int, pocketmine\promise\Promise<null>>, array<int, pocketmine\promise\Promise<null>> given.

Check failure on line 623 in src/Server.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Parameter #1 $promises of static method pocketmine\promise\Promise<mixed>::all() expects non-empty-array<int, pocketmine\promise\Promise<null>>, array<int, pocketmine\promise\Promise<null>> given.

$promise->onCompletion($playerCreationSucceeded, function () use ($playerCreationRejected) : void{
$playerCreationRejected("Failed to create player");
});
}else{
$playerCreationSucceeded();
}

return $playerPromiseResolver->getPromise();
}
Expand Down Expand Up @@ -1255,7 +1260,7 @@ private function startupPrepareNetworkInterfaces() : bool{
$useQuery = $this->configGroup->getConfigBool(ServerProperties::ENABLE_QUERY, true);

$typeConverter = TypeConverter::getInstance();
$packetBroadcaster = $this->getPacketBroadcaster($typeConverter);
$packetBroadcaster = $this->getPacketBroadcaster(ProtocolInfo::CURRENT_PROTOCOL);
$entityEventBroadcaster = $this->getEntityEventBroadcaster($packetBroadcaster, $typeConverter);

if(
Expand Down Expand Up @@ -1914,12 +1919,11 @@ private function tick() : void{
$this->nextTick += self::TARGET_SECONDS_PER_TICK;
}
}
public function getPacketSerializerContext(TypeConverter $typeConverter) : PacketSerializerContext{
return $this->packetSerializerContexts[spl_object_id($typeConverter)] ??= new PacketSerializerContext($typeConverter->getItemTypeDictionary(), $typeConverter->getProtocolId());
}
public function getPacketBroadcaster(PacketSerializerContext $packetSerializerContext) : PacketBroadcaster{
return $this->packetBroadcasters[spl_object_id($packetSerializerContext)] ??= new StandardPacketBroadcaster($this, $packetSerializerContext);

public function getPacketBroadcaster(int $protocolId) : PacketBroadcaster{
return $this->packetBroadcasters[$protocolId] ??= new StandardPacketBroadcaster($this, $protocolId);
}

public function getEntityEventBroadcaster(PacketBroadcaster $packetBroadcaster, TypeConverter $typeConverter) : EntityEventBroadcaster{
return $this->entityEventBroadcasters[spl_object_id($packetBroadcaster) . ':' . spl_object_id($typeConverter)] ??= new StandardEntityEventBroadcaster($packetBroadcaster, $typeConverter);
}
Expand Down
7 changes: 3 additions & 4 deletions src/network/mcpe/CachedChunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
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;
Expand Down Expand Up @@ -85,14 +84,14 @@ public function getHashMap() : array{
/**
* @phpstan-param DimensionIds::* $dimensionId
*/
public function compressPackets(int $chunkX, int $chunkZ, int $dimensionId, string $chunkData, Compressor $compressor, PacketSerializerContext $encoderContext, int $protocolId) : void{
public function compressPackets(int $chunkX, int $chunkZ, int $dimensionId, string $chunkData, Compressor $compressor, int $protocolId) : void{
$protocolAddition = $protocolId >= ProtocolInfo::PROTOCOL_1_20_60 ? chr($compressor->getNetworkId()) : '';
$stream = new BinaryStream();
PacketBatch::encodePackets($stream, $encoderContext, [$this->createPacket($chunkX, $chunkZ, $dimensionId, $chunkData)]);
PacketBatch::encodePackets($stream, $protocolId, [$this->createPacket($chunkX, $chunkZ, $dimensionId, $chunkData)]);
$this->packet = $protocolAddition . $compressor->compress($stream->getBuffer());

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

Expand Down
4 changes: 0 additions & 4 deletions src/network/mcpe/ChunkRequestTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
use pocketmine\network\mcpe\compression\Compressor;
use pocketmine\network\mcpe\convert\TypeConverter;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializerContext;
use pocketmine\network\mcpe\protocol\LevelChunkPacket;
use pocketmine\network\mcpe\protocol\serializer\PacketBatch;
use pocketmine\network\mcpe\protocol\types\ChunkPosition;
use pocketmine\network\mcpe\protocol\types\DimensionIds;
use pocketmine\network\mcpe\serializer\ChunkSerializer;
use pocketmine\scheduler\AsyncTask;
Expand Down
4 changes: 2 additions & 2 deletions src/network/mcpe/InventoryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,8 @@ private function itemStackExtraDataEqual(ItemStack $left, ItemStack $right) : bo
}

$typeConverter = $this->session->getTypeConverter();
$leftExtraData = $typeConverter->deserializeItemStackExtraData($left->getRawExtraData(), $left->getId());
$rightExtraData = $typeConverter->deserializeItemStackExtraData($right->getRawExtraData(), $right->getId());
$leftExtraData = $typeConverter->deserializeItemStackExtraData($this->session->getProtocolId(), $left->getRawExtraData(), $left->getId());
$rightExtraData = $typeConverter->deserializeItemStackExtraData($this->session->getProtocolId(), $right->getRawExtraData(), $right->getId());

$leftNbt = $leftExtraData->getNbt();
$rightNbt = $rightExtraData->getNbt();
Expand Down
3 changes: 1 addition & 2 deletions src/network/mcpe/NetworkSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,7 @@ public function setProtocolId(int $protocolId) : void{
$this->protocolId = $protocolId;

$this->typeConverter = TypeConverter::getInstance($protocolId);
$this->packetSerializerContext = $this->server->getPacketSerializerContext($this->typeConverter);
$this->broadcaster = $this->server->getPacketBroadcaster($this->packetSerializerContext);
$this->broadcaster = $this->server->getPacketBroadcaster($protocolId);
$this->entityEventBroadcaster = $this->server->getEntityEventBroadcaster($this->broadcaster, $this->typeConverter);
}

Expand Down
7 changes: 4 additions & 3 deletions src/network/mcpe/StandardPacketBroadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@

final class StandardPacketBroadcaster implements PacketBroadcaster{
public function __construct(
private Server $server
private Server $server,
private int $protocolId
){}

public function broadcastPackets(array $recipients, array $packets) : void{
Expand Down Expand Up @@ -66,7 +67,7 @@ public function broadcastPackets(array $recipients, array $packets) : void{
$totalLength = 0;
$packetBuffers = [];
foreach($packets as $packet){
$buffer = NetworkSession::encodePacketTimed(PacketSerializer::encoder(), $packet);
$buffer = NetworkSession::encodePacketTimed(PacketSerializer::encoder($this->protocolId), $packet);
//varint length prefix + packet buffer
$totalLength += (((int) log(strlen($buffer), 128)) + 1) + strlen($buffer);
$packetBuffers[] = $buffer;
Expand All @@ -82,7 +83,7 @@ public function broadcastPackets(array $recipients, array $packets) : void{
PacketBatch::encodeRaw($stream, $packetBuffers);
$batchBuffer = $stream->getBuffer();

$batch = $this->server->prepareBatch($batchBuffer, $this->protocolContext, $compressor, timings: Timings::$playerNetworkSendCompressBroadcast);
$batch = $this->server->prepareBatch($batchBuffer, $this->protocolId, $compressor, timings: Timings::$playerNetworkSendCompressBroadcast);
foreach($compressorTargets as $target){
$target->queueCompressed($batch);
}
Expand Down
2 changes: 1 addition & 1 deletion src/network/mcpe/convert/TypeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
use pocketmine\crafting\TagWildcardRecipeIngredient;
use pocketmine\data\bedrock\item\BlockItemIdMap;
use pocketmine\data\bedrock\item\downgrade\ItemIdMetaDowngrader;
use pocketmine\event\server\TypeConverterConstructEvent;
use pocketmine\data\bedrock\item\ItemTypeNames;
use pocketmine\event\server\TypeConverterConstructEvent;
use pocketmine\item\Item;
use pocketmine\item\VanillaItems;
use pocketmine\nbt\NbtException;
Expand Down
5 changes: 3 additions & 2 deletions tools/generate-bedrock-data-from-packets.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use pocketmine\network\mcpe\protocol\CraftingDataPacket;
use pocketmine\network\mcpe\protocol\CreativeContentPacket;
use pocketmine\network\mcpe\protocol\PacketPool;
use pocketmine\network\mcpe\protocol\ProtocolInfo;
use pocketmine\network\mcpe\protocol\serializer\ItemTypeDictionary;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
use pocketmine\network\mcpe\protocol\StartGamePacket;
Expand Down Expand Up @@ -173,7 +174,7 @@ private function itemStackToJson(ItemStack $itemStack) : ItemStackData{

$rawExtraData = $itemStack->getRawExtraData();
if($rawExtraData !== ""){
$decoder = PacketSerializer::decoder($rawExtraData, 0);
$decoder = PacketSerializer::decoder(ProtocolInfo::CURRENT_PROTOCOL, $rawExtraData, 0);
$extraData = $itemStringId === ItemTypeNames::SHIELD ? ItemStackExtraDataShield::read($decoder) : ItemStackExtraData::read($decoder);
$nbt = $extraData->getNbt();
if($nbt !== null && count($nbt) > 0){
Expand Down Expand Up @@ -577,7 +578,7 @@ function main(array $argv) : int{
fwrite(STDERR, "Unknown packet on line " . ($lineNum + 1) . ": " . $parts[1]);
continue;
}
$serializer = PacketSerializer::decoder($raw, 0);
$serializer = PacketSerializer::decoder(ProtocolInfo::CURRENT_PROTOCOL, $raw, 0);

$pk->decode($serializer);
$pk->handle($handler);
Expand Down

0 comments on commit 6793560

Please sign in to comment.