diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5a03d229..4cc73b38 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,6 +18,7 @@ jobs: with: php-version: ${{ matrix.php }} ini-values: xdebug.max_nesting_level=3000 + extensions: encoding-pmmp/ext-encoding@1.0.0 - name: Load Composer package cache uses: actions/cache@v4 @@ -49,6 +50,7 @@ jobs: php-version: 8.2 ini-values: xdebug.max_nesting_level=3000 tools: php-cs-fixer:3 + extensions: encoding-pmmp/ext-encoding@1.0.0 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/composer.json b/composer.json index d7980a8a..a245798f 100644 --- a/composer.json +++ b/composer.json @@ -4,6 +4,7 @@ "type": "library", "require": { "php": "^8.1", + "ext-encoding": "~1.0.0", "ext-json": "*", "pocketmine/binaryutils": "^0.2.0", "pocketmine/color": "^0.2.0 || ^0.3.0", diff --git a/src/ActorEventPacket.php b/src/ActorEventPacket.php index c5c0ff50..2baea4c1 100644 --- a/src/ActorEventPacket.php +++ b/src/ActorEventPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\ActorEvent; class ActorEventPacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ @@ -36,16 +40,16 @@ public static function create(int $actorRuntimeId, int $eventId, int $eventData) return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorRuntimeId = $in->getActorRuntimeId(); - $this->eventId = $in->getByte(); - $this->eventData = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->eventId = Byte::readUnsigned($in); + $this->eventData = VarInt::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorRuntimeId($this->actorRuntimeId); - $out->putByte($this->eventId); - $out->putVarInt($this->eventData); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + Byte::writeUnsigned($out, $this->eventId); + VarInt::writeSignedInt($out, $this->eventData); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ActorPickRequestPacket.php b/src/ActorPickRequestPacket.php index f5bd4b6b..d49c9a46 100644 --- a/src/ActorPickRequestPacket.php +++ b/src/ActorPickRequestPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class ActorPickRequestPacket extends DataPacket implements ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::ACTOR_PICK_REQUEST_PACKET; @@ -34,16 +38,16 @@ public static function create(int $actorUniqueId, int $hotbarSlot, bool $addUser return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorUniqueId = $in->getLLong(); - $this->hotbarSlot = $in->getByte(); - $this->addUserData = $in->getBool(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorUniqueId = LE::readSignedLong($in); + $this->hotbarSlot = Byte::readUnsigned($in); + $this->addUserData = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLLong($this->actorUniqueId); - $out->putByte($this->hotbarSlot); - $out->putBool($this->addUserData); + protected function encodePayload(ByteBufferWriter $out) : void{ + LE::writeSignedLong($out, $this->actorUniqueId); + Byte::writeUnsigned($out, $this->hotbarSlot); + CommonTypes::putBool($out, $this->addUserData); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/AddActorPacket.php b/src/AddActorPacket.php index d1110fa1..4bc885aa 100644 --- a/src/AddActorPacket.php +++ b/src/AddActorPacket.php @@ -14,8 +14,12 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\entity\Attribute; use pocketmine\network\mcpe\protocol\types\entity\EntityLink; use pocketmine\network\mcpe\protocol\types\entity\MetadataProperty; @@ -85,60 +89,60 @@ public static function create( return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorUniqueId = $in->getActorUniqueId(); - $this->actorRuntimeId = $in->getActorRuntimeId(); - $this->type = $in->getString(); - $this->position = $in->getVector3(); - $this->motion = $in->getVector3(); - $this->pitch = $in->getLFloat(); - $this->yaw = $in->getLFloat(); - $this->headYaw = $in->getLFloat(); - $this->bodyYaw = $in->getLFloat(); - - $attrCount = $in->getUnsignedVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorUniqueId = CommonTypes::getActorUniqueId($in); + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->type = CommonTypes::getString($in); + $this->position = CommonTypes::getVector3($in); + $this->motion = CommonTypes::getVector3($in); + $this->pitch = LE::readFloat($in); + $this->yaw = LE::readFloat($in); + $this->headYaw = LE::readFloat($in); + $this->bodyYaw = LE::readFloat($in); + + $attrCount = VarInt::readUnsignedInt($in); for($i = 0; $i < $attrCount; ++$i){ - $id = $in->getString(); - $min = $in->getLFloat(); - $current = $in->getLFloat(); - $max = $in->getLFloat(); + $id = CommonTypes::getString($in); + $min = LE::readFloat($in); + $current = LE::readFloat($in); + $max = LE::readFloat($in); $this->attributes[] = new Attribute($id, $min, $max, $current, $current, []); } - $this->metadata = $in->getEntityMetadata(); + $this->metadata = CommonTypes::getEntityMetadata($in); $this->syncedProperties = PropertySyncData::read($in); - $linkCount = $in->getUnsignedVarInt(); + $linkCount = VarInt::readUnsignedInt($in); for($i = 0; $i < $linkCount; ++$i){ - $this->links[] = $in->getEntityLink(); + $this->links[] = CommonTypes::getEntityLink($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorUniqueId($this->actorUniqueId); - $out->putActorRuntimeId($this->actorRuntimeId); - $out->putString($this->type); - $out->putVector3($this->position); - $out->putVector3Nullable($this->motion); - $out->putLFloat($this->pitch); - $out->putLFloat($this->yaw); - $out->putLFloat($this->headYaw); - $out->putLFloat($this->bodyYaw); - - $out->putUnsignedVarInt(count($this->attributes)); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorUniqueId($out, $this->actorUniqueId); + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + CommonTypes::putString($out, $this->type); + CommonTypes::putVector3($out, $this->position); + CommonTypes::putVector3Nullable($out, $this->motion); + LE::writeFloat($out, $this->pitch); + LE::writeFloat($out, $this->yaw); + LE::writeFloat($out, $this->headYaw); + LE::writeFloat($out, $this->bodyYaw); + + VarInt::writeUnsignedInt($out, count($this->attributes)); foreach($this->attributes as $attribute){ - $out->putString($attribute->getId()); - $out->putLFloat($attribute->getMin()); - $out->putLFloat($attribute->getCurrent()); - $out->putLFloat($attribute->getMax()); + CommonTypes::putString($out, $attribute->getId()); + LE::writeFloat($out, $attribute->getMin()); + LE::writeFloat($out, $attribute->getCurrent()); + LE::writeFloat($out, $attribute->getMax()); } - $out->putEntityMetadata($this->metadata); + CommonTypes::putEntityMetadata($out, $this->metadata); $this->syncedProperties->write($out); - $out->putUnsignedVarInt(count($this->links)); + VarInt::writeUnsignedInt($out, count($this->links)); foreach($this->links as $link){ - $out->putEntityLink($link); + CommonTypes::putEntityLink($out, $link); } } diff --git a/src/AddBehaviorTreePacket.php b/src/AddBehaviorTreePacket.php index 55746242..a9b6f8f0 100644 --- a/src/AddBehaviorTreePacket.php +++ b/src/AddBehaviorTreePacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class AddBehaviorTreePacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::ADD_BEHAVIOR_TREE_PACKET; @@ -30,12 +32,12 @@ public static function create(string $behaviorTreeJson) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->behaviorTreeJson = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->behaviorTreeJson = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->behaviorTreeJson); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->behaviorTreeJson); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/AddItemActorPacket.php b/src/AddItemActorPacket.php index 5996ab1e..51b0fa97 100644 --- a/src/AddItemActorPacket.php +++ b/src/AddItemActorPacket.php @@ -14,8 +14,10 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\entity\MetadataProperty; use pocketmine\network\mcpe\protocol\types\inventory\ItemStackWrapper; @@ -59,24 +61,24 @@ public static function create( return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorUniqueId = $in->getActorUniqueId(); - $this->actorRuntimeId = $in->getActorRuntimeId(); - $this->item = $in->getItemStackWrapper(); - $this->position = $in->getVector3(); - $this->motion = $in->getVector3(); - $this->metadata = $in->getEntityMetadata(); - $this->isFromFishing = $in->getBool(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorUniqueId = CommonTypes::getActorUniqueId($in); + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->item = CommonTypes::getItemStackWrapper($in); + $this->position = CommonTypes::getVector3($in); + $this->motion = CommonTypes::getVector3($in); + $this->metadata = CommonTypes::getEntityMetadata($in); + $this->isFromFishing = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorUniqueId($this->actorUniqueId); - $out->putActorRuntimeId($this->actorRuntimeId); - $out->putItemStackWrapper($this->item); - $out->putVector3($this->position); - $out->putVector3Nullable($this->motion); - $out->putEntityMetadata($this->metadata); - $out->putBool($this->isFromFishing); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorUniqueId($out, $this->actorUniqueId); + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + CommonTypes::putItemStackWrapper($out, $this->item); + CommonTypes::putVector3($out, $this->position); + CommonTypes::putVector3Nullable($out, $this->motion); + CommonTypes::putEntityMetadata($out, $this->metadata); + CommonTypes::putBool($out, $this->isFromFishing); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/AddPaintingPacket.php b/src/AddPaintingPacket.php index 23a15ba9..b6e9bd35 100644 --- a/src/AddPaintingPacket.php +++ b/src/AddPaintingPacket.php @@ -14,8 +14,11 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class AddPaintingPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::ADD_PAINTING_PACKET; @@ -39,20 +42,20 @@ public static function create(int $actorUniqueId, int $actorRuntimeId, Vector3 $ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorUniqueId = $in->getActorUniqueId(); - $this->actorRuntimeId = $in->getActorRuntimeId(); - $this->position = $in->getVector3(); - $this->direction = $in->getVarInt(); - $this->title = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorUniqueId = CommonTypes::getActorUniqueId($in); + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->position = CommonTypes::getVector3($in); + $this->direction = VarInt::readSignedInt($in); + $this->title = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorUniqueId($this->actorUniqueId); - $out->putActorRuntimeId($this->actorRuntimeId); - $out->putVector3($this->position); - $out->putVarInt($this->direction); - $out->putString($this->title); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorUniqueId($out, $this->actorUniqueId); + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + CommonTypes::putVector3($out, $this->position); + VarInt::writeSignedInt($out, $this->direction); + CommonTypes::putString($out, $this->title); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/AddPlayerPacket.php b/src/AddPlayerPacket.php index 82375257..72e0c0fd 100644 --- a/src/AddPlayerPacket.php +++ b/src/AddPlayerPacket.php @@ -14,8 +14,12 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\DeviceOS; use pocketmine\network\mcpe\protocol\types\entity\EntityLink; use pocketmine\network\mcpe\protocol\types\entity\MetadataProperty; @@ -98,57 +102,57 @@ public static function create( return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->uuid = $in->getUUID(); - $this->username = $in->getString(); - $this->actorRuntimeId = $in->getActorRuntimeId(); - $this->platformChatId = $in->getString(); - $this->position = $in->getVector3(); - $this->motion = $in->getVector3(); - $this->pitch = $in->getLFloat(); - $this->yaw = $in->getLFloat(); - $this->headYaw = $in->getLFloat(); - $this->item = $in->getItemStackWrapper(); - $this->gameMode = $in->getVarInt(); - $this->metadata = $in->getEntityMetadata(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->uuid = CommonTypes::getUUID($in); + $this->username = CommonTypes::getString($in); + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->platformChatId = CommonTypes::getString($in); + $this->position = CommonTypes::getVector3($in); + $this->motion = CommonTypes::getVector3($in); + $this->pitch = LE::readFloat($in); + $this->yaw = LE::readFloat($in); + $this->headYaw = LE::readFloat($in); + $this->item = CommonTypes::getItemStackWrapper($in); + $this->gameMode = VarInt::readSignedInt($in); + $this->metadata = CommonTypes::getEntityMetadata($in); $this->syncedProperties = PropertySyncData::read($in); $this->abilitiesPacket = new UpdateAbilitiesPacket(); $this->abilitiesPacket->decodePayload($in); - $linkCount = $in->getUnsignedVarInt(); + $linkCount = VarInt::readUnsignedInt($in); for($i = 0; $i < $linkCount; ++$i){ - $this->links[$i] = $in->getEntityLink(); + $this->links[$i] = CommonTypes::getEntityLink($in); } - $this->deviceId = $in->getString(); - $this->buildPlatform = $in->getLInt(); + $this->deviceId = CommonTypes::getString($in); + $this->buildPlatform = LE::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUUID($this->uuid); - $out->putString($this->username); - $out->putActorRuntimeId($this->actorRuntimeId); - $out->putString($this->platformChatId); - $out->putVector3($this->position); - $out->putVector3Nullable($this->motion); - $out->putLFloat($this->pitch); - $out->putLFloat($this->yaw); - $out->putLFloat($this->headYaw); - $out->putItemStackWrapper($this->item); - $out->putVarInt($this->gameMode); - $out->putEntityMetadata($this->metadata); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putUUID($out, $this->uuid); + CommonTypes::putString($out, $this->username); + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + CommonTypes::putString($out, $this->platformChatId); + CommonTypes::putVector3($out, $this->position); + CommonTypes::putVector3Nullable($out, $this->motion); + LE::writeFloat($out, $this->pitch); + LE::writeFloat($out, $this->yaw); + LE::writeFloat($out, $this->headYaw); + CommonTypes::putItemStackWrapper($out, $this->item); + VarInt::writeSignedInt($out, $this->gameMode); + CommonTypes::putEntityMetadata($out, $this->metadata); $this->syncedProperties->write($out); $this->abilitiesPacket->encodePayload($out); - $out->putUnsignedVarInt(count($this->links)); + VarInt::writeUnsignedInt($out, count($this->links)); foreach($this->links as $link){ - $out->putEntityLink($link); + CommonTypes::putEntityLink($out, $link); } - $out->putString($this->deviceId); - $out->putLInt($this->buildPlatform); + CommonTypes::putString($out, $this->deviceId); + LE::writeSignedInt($out, $this->buildPlatform); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/AddVolumeEntityPacket.php b/src/AddVolumeEntityPacket.php index c655c9b1..17a11195 100644 --- a/src/AddVolumeEntityPacket.php +++ b/src/AddVolumeEntityPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; use pocketmine\network\mcpe\protocol\types\CacheableNbt; @@ -74,26 +77,26 @@ public function getDimension() : int{ return $this->dimension; } public function getEngineVersion() : string{ return $this->engineVersion; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->entityNetId = $in->getUnsignedVarInt(); - $this->data = new CacheableNbt($in->getNbtCompoundRoot()); - $this->jsonIdentifier = $in->getString(); - $this->instanceName = $in->getString(); - $this->minBound = $in->getBlockPosition(); - $this->maxBound = $in->getBlockPosition(); - $this->dimension = $in->getVarInt(); - $this->engineVersion = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->entityNetId = VarInt::readUnsignedInt($in); + $this->data = new CacheableNbt(CommonTypes::getNbtCompoundRoot($in)); + $this->jsonIdentifier = CommonTypes::getString($in); + $this->instanceName = CommonTypes::getString($in); + $this->minBound = CommonTypes::getBlockPosition($in); + $this->maxBound = CommonTypes::getBlockPosition($in); + $this->dimension = VarInt::readSignedInt($in); + $this->engineVersion = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt($this->entityNetId); - $out->put($this->data->getEncodedNbt()); - $out->putString($this->jsonIdentifier); - $out->putString($this->instanceName); - $out->putBlockPosition($this->minBound); - $out->putBlockPosition($this->maxBound); - $out->putVarInt($this->dimension); - $out->putString($this->engineVersion); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, $this->entityNetId); + $out->writeByteArray($this->data->getEncodedNbt()); + CommonTypes::putString($out, $this->jsonIdentifier); + CommonTypes::putString($out, $this->instanceName); + CommonTypes::putBlockPosition($out, $this->minBound); + CommonTypes::putBlockPosition($out, $this->maxBound); + VarInt::writeSignedInt($out, $this->dimension); + CommonTypes::putString($out, $this->engineVersion); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/AgentActionEventPacket.php b/src/AgentActionEventPacket.php index bedb53df..20e4a0ce 100644 --- a/src/AgentActionEventPacket.php +++ b/src/AgentActionEventPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\AgentActionType; /** @@ -46,16 +49,16 @@ public function getAction() : int{ return $this->action; } public function getResponseJson() : string{ return $this->responseJson; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->requestId = $in->getString(); - $this->action = $in->getLInt(); - $this->responseJson = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->requestId = CommonTypes::getString($in); + $this->action = LE::readUnsignedInt($in); + $this->responseJson = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->requestId); - $out->putLInt($this->action); - $out->putString($this->responseJson); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->requestId); + LE::writeUnsignedInt($out, $this->action); + CommonTypes::putString($out, $this->responseJson); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/AgentAnimationPacket.php b/src/AgentAnimationPacket.php index 4bb6916a..38896696 100644 --- a/src/AgentAnimationPacket.php +++ b/src/AgentAnimationPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class AgentAnimationPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::AGENT_ANIMATION_PACKET; @@ -39,14 +42,14 @@ public function getAnimationType() : int{ return $this->animationType; } public function getActorRuntimeId() : int{ return $this->actorRuntimeId; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->animationType = $in->getByte(); - $this->actorRuntimeId = $in->getActorRuntimeId(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->animationType = Byte::readUnsigned($in); + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->animationType); - $out->putActorRuntimeId($this->actorRuntimeId); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->animationType); + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/AnimateEntityPacket.php b/src/AnimateEntityPacket.php index b021ff8e..51d19fb9 100644 --- a/src/AnimateEntityPacket.php +++ b/src/AnimateEntityPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use function count; class AnimateEntityPacket extends DataPacket implements ClientboundPacket{ @@ -75,29 +79,29 @@ public function getBlendOutTime() : float{ return $this->blendOutTime; } */ public function getActorRuntimeIds() : array{ return $this->actorRuntimeIds; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->animation = $in->getString(); - $this->nextState = $in->getString(); - $this->stopExpression = $in->getString(); - $this->stopExpressionVersion = $in->getLInt(); - $this->controller = $in->getString(); - $this->blendOutTime = $in->getLFloat(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->animation = CommonTypes::getString($in); + $this->nextState = CommonTypes::getString($in); + $this->stopExpression = CommonTypes::getString($in); + $this->stopExpressionVersion = LE::readSignedInt($in); + $this->controller = CommonTypes::getString($in); + $this->blendOutTime = LE::readFloat($in); $this->actorRuntimeIds = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ - $this->actorRuntimeIds[] = $in->getActorRuntimeId(); + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ + $this->actorRuntimeIds[] = CommonTypes::getActorRuntimeId($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->animation); - $out->putString($this->nextState); - $out->putString($this->stopExpression); - $out->putLInt($this->stopExpressionVersion); - $out->putString($this->controller); - $out->putLFloat($this->blendOutTime); - $out->putUnsignedVarInt(count($this->actorRuntimeIds)); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->animation); + CommonTypes::putString($out, $this->nextState); + CommonTypes::putString($out, $this->stopExpression); + LE::writeSignedInt($out, $this->stopExpressionVersion); + CommonTypes::putString($out, $this->controller); + LE::writeFloat($out, $this->blendOutTime); + VarInt::writeUnsignedInt($out, count($this->actorRuntimeIds)); foreach($this->actorRuntimeIds as $id){ - $out->putActorRuntimeId($id); + CommonTypes::putActorRuntimeId($out, $id); } } diff --git a/src/AnimatePacket.php b/src/AnimatePacket.php index f2fef44e..12947c43 100644 --- a/src/AnimatePacket.php +++ b/src/AnimatePacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class AnimatePacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::ANIMATE_PACKET; @@ -42,19 +46,19 @@ public static function boatHack(int $actorRuntimeId, int $actionId, float $data) return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->action = $in->getVarInt(); - $this->actorRuntimeId = $in->getActorRuntimeId(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->action = VarInt::readSignedInt($in); + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); if(($this->action & 0x80) !== 0){ - $this->float = $in->getLFloat(); + $this->float = LE::readFloat($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->action); - $out->putActorRuntimeId($this->actorRuntimeId); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->action); + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); if(($this->action & 0x80) !== 0){ - $out->putLFloat($this->float); + LE::writeFloat($out, $this->float); } } diff --git a/src/AnvilDamagePacket.php b/src/AnvilDamagePacket.php index a19565c4..416f76b4 100644 --- a/src/AnvilDamagePacket.php +++ b/src/AnvilDamagePacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; class AnvilDamagePacket extends DataPacket implements ServerboundPacket{ @@ -39,14 +42,14 @@ public function getDamageAmount() : int{ public function getBlockPosition() : BlockPosition{ return $this->blockPosition; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->damageAmount = $in->getByte(); - $this->blockPosition = $in->getBlockPosition(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->damageAmount = Byte::readUnsigned($in); + $this->blockPosition = CommonTypes::getBlockPosition($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->damageAmount); - $out->putBlockPosition($this->blockPosition); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->damageAmount); + CommonTypes::putBlockPosition($out, $this->blockPosition); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/AutomationClientConnectPacket.php b/src/AutomationClientConnectPacket.php index 096c2944..da2f80df 100644 --- a/src/AutomationClientConnectPacket.php +++ b/src/AutomationClientConnectPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class AutomationClientConnectPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::AUTOMATION_CLIENT_CONNECT_PACKET; @@ -30,12 +32,12 @@ public static function create(string $serverUri) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->serverUri = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->serverUri = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->serverUri); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->serverUri); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/AvailableActorIdentifiersPacket.php b/src/AvailableActorIdentifiersPacket.php index 257c2999..5e298c46 100644 --- a/src/AvailableActorIdentifiersPacket.php +++ b/src/AvailableActorIdentifiersPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\CacheableNbt; class AvailableActorIdentifiersPacket extends DataPacket implements ClientboundPacket{ @@ -33,12 +35,12 @@ public static function create(CacheableNbt $identifiers) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->identifiers = new CacheableNbt($in->getNbtCompoundRoot()); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->identifiers = new CacheableNbt(CommonTypes::getNbtCompoundRoot($in)); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->put($this->identifiers->getEncodedNbt()); + protected function encodePayload(ByteBufferWriter $out) : void{ + $out->writeByteArray($this->identifiers->getEncodedNbt()); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/AvailableCommandsPacket.php b/src/AvailableCommandsPacket.php index ea3af9b5..cc2f4567 100644 --- a/src/AvailableCommandsPacket.php +++ b/src/AvailableCommandsPacket.php @@ -14,7 +14,13 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\DataDecodeException; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\command\ChainedSubCommandData; use pocketmine\network\mcpe\protocol\types\command\ChainedSubCommandValue; use pocketmine\network\mcpe\protocol\types\command\CommandData; @@ -23,7 +29,6 @@ use pocketmine\network\mcpe\protocol\types\command\CommandOverload; use pocketmine\network\mcpe\protocol\types\command\CommandParameter; use pocketmine\network\mcpe\protocol\types\command\CommandParameterTypes as ArgTypes; -use pocketmine\utils\BinaryDataException; use function array_search; use function count; use function dechex; @@ -128,28 +133,28 @@ public static function create(array $commandData, array $hardcodedEnums, array $ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ /** @var string[] $enumValues */ $enumValues = []; - for($i = 0, $enumValuesCount = $in->getUnsignedVarInt(); $i < $enumValuesCount; ++$i){ - $enumValues[] = $in->getString(); + for($i = 0, $enumValuesCount = VarInt::readUnsignedInt($in); $i < $enumValuesCount; ++$i){ + $enumValues[] = CommonTypes::getString($in); } /** @var string[] $chainedSubcommandValueNames */ $chainedSubcommandValueNames = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ - $chainedSubcommandValueNames[] = $in->getString(); + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ + $chainedSubcommandValueNames[] = CommonTypes::getString($in); } /** @var string[] $postfixes */ $postfixes = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ - $postfixes[] = $in->getString(); + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ + $postfixes[] = CommonTypes::getString($in); } /** @var CommandEnum[] $enums */ $enums = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $enums[] = $enum = $this->getEnum($enumValues, $in); //TODO: Bedrock may provide some enums which are not referenced by any command, and can't reasonably be //considered "hardcoded". This happens with various Edu command enums, and other enums which are probably @@ -162,28 +167,28 @@ protected function decodePayload(PacketSerializer $in) : void{ } $chainedSubCommandData = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ - $name = $in->getString(); + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ + $name = CommonTypes::getString($in); $values = []; - for($j = 0, $valueCount = $in->getUnsignedVarInt(); $j < $valueCount; ++$j){ - $valueName = $chainedSubcommandValueNames[$in->getLShort()]; - $valueType = $in->getLShort(); + for($j = 0, $valueCount = VarInt::readUnsignedInt($in); $j < $valueCount; ++$j){ + $valueName = $chainedSubcommandValueNames[LE::readUnsignedShort($in)]; + $valueType = LE::readUnsignedShort($in); $values[] = new ChainedSubCommandValue($valueName, $valueType); } $chainedSubCommandData[] = new ChainedSubCommandData($name, $values); } - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $this->commandData[] = $this->getCommandData($enums, $postfixes, $chainedSubCommandData, $in); } - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $this->softEnums[] = $this->getSoftEnum($in); } $this->initSoftEnumsInCommandData(); - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $this->enumConstraints[] = $this->getEnumConstraint($enums, $enumValues, $in); } } @@ -212,15 +217,15 @@ protected function initSoftEnumsInCommandData() : void{ * @param string[] $enumValueList * * @throws PacketDecodeException - * @throws BinaryDataException + * @throws DataDecodeException */ - protected function getEnum(array $enumValueList, PacketSerializer $in) : CommandEnum{ - $enumName = $in->getString(); + protected function getEnum(array $enumValueList, ByteBufferReader $in) : CommandEnum{ + $enumName = CommonTypes::getString($in); $enumValues = []; $listSize = count($enumValueList); - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $index = $this->getEnumValueIndex($listSize, $in); if(!isset($enumValueList[$index])){ throw new PacketDecodeException("Invalid enum value index $index"); @@ -233,15 +238,15 @@ protected function getEnum(array $enumValueList, PacketSerializer $in) : Command } /** - * @throws BinaryDataException + * @throws DataDecodeException */ - protected function getSoftEnum(PacketSerializer $in) : CommandEnum{ - $enumName = $in->getString(); + protected function getSoftEnum(ByteBufferReader $in) : CommandEnum{ + $enumName = CommonTypes::getString($in); $enumValues = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ //Get the enum value from the initial pile of mess - $enumValues[] = $in->getString(); + $enumValues[] = CommonTypes::getString($in); } return new CommandEnum($enumName, $enumValues, true); @@ -250,11 +255,11 @@ protected function getSoftEnum(PacketSerializer $in) : CommandEnum{ /** * @param int[] $enumValueMap */ - protected function putEnum(CommandEnum $enum, array $enumValueMap, PacketSerializer $out) : void{ - $out->putString($enum->getName()); + protected function putEnum(CommandEnum $enum, array $enumValueMap, ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $enum->getName()); $values = $enum->getValues(); - $out->putUnsignedVarInt(count($values)); + VarInt::writeUnsignedInt($out, count($values)); $listSize = count($enumValueMap); foreach($values as $value){ if(!isset($enumValueMap[$value])){ @@ -264,36 +269,36 @@ protected function putEnum(CommandEnum $enum, array $enumValueMap, PacketSeriali } } - protected function putSoftEnum(CommandEnum $enum, PacketSerializer $out) : void{ - $out->putString($enum->getName()); + protected function putSoftEnum(CommandEnum $enum, ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $enum->getName()); $values = $enum->getValues(); - $out->putUnsignedVarInt(count($values)); + VarInt::writeUnsignedInt($out, count($values)); foreach($values as $value){ - $out->putString($value); + CommonTypes::putString($out, $value); } } /** - * @throws BinaryDataException + * @throws DataDecodeException */ - protected function getEnumValueIndex(int $valueCount, PacketSerializer $in) : int{ + protected function getEnumValueIndex(int $valueCount, ByteBufferReader $in) : int{ if($valueCount < 256){ - return $in->getByte(); + return Byte::readUnsigned($in); }elseif($valueCount < 65536){ - return $in->getLShort(); + return LE::readUnsignedShort($in); }else{ - return $in->getLInt(); + return LE::readUnsignedInt($in); } } - protected function putEnumValueIndex(int $index, int $valueCount, PacketSerializer $out) : void{ + protected function putEnumValueIndex(int $index, int $valueCount, ByteBufferWriter $out) : void{ if($valueCount < 256){ - $out->putByte($index); + Byte::writeUnsigned($out, $index); }elseif($valueCount < 65536){ - $out->putLShort($index); + LE::writeUnsignedShort($out, $index); }else{ - $out->putLInt($index); + LE::writeUnsignedInt($out, $index); } } @@ -302,15 +307,15 @@ protected function putEnumValueIndex(int $index, int $valueCount, PacketSerializ * @param string[] $enumValues * * @throws PacketDecodeException - * @throws BinaryDataException + * @throws DataDecodeException */ - protected function getEnumConstraint(array $enums, array $enumValues, PacketSerializer $in) : CommandEnumConstraint{ + protected function getEnumConstraint(array $enums, array $enumValues, ByteBufferReader $in) : CommandEnumConstraint{ //wtf, what was wrong with an offset inside the enum? :( - $valueIndex = $in->getLInt(); + $valueIndex = LE::readUnsignedInt($in); if(!isset($enumValues[$valueIndex])){ throw new PacketDecodeException("Enum constraint refers to unknown enum value index $valueIndex"); } - $enumIndex = $in->getLInt(); + $enumIndex = LE::readUnsignedInt($in); if(!isset($enums[$enumIndex])){ throw new PacketDecodeException("Enum constraint refers to unknown enum index $enumIndex"); } @@ -321,8 +326,8 @@ protected function getEnumConstraint(array $enums, array $enumValues, PacketSeri } $constraintIds = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ - $constraintIds[] = $in->getByte(); + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ + $constraintIds[] = Byte::readUnsigned($in); } return new CommandEnumConstraint($enum, $valueOffset, $constraintIds); @@ -332,12 +337,12 @@ protected function getEnumConstraint(array $enums, array $enumValues, PacketSeri * @param int[] $enumIndexes string enum name -> int index * @param int[] $enumValueIndexes string value -> int index */ - protected function putEnumConstraint(CommandEnumConstraint $constraint, array $enumIndexes, array $enumValueIndexes, PacketSerializer $out) : void{ - $out->putLInt($enumValueIndexes[$constraint->getAffectedValue()]); - $out->putLInt($enumIndexes[$constraint->getEnum()->getName()]); - $out->putUnsignedVarInt(count($constraint->getConstraints())); + protected function putEnumConstraint(CommandEnumConstraint $constraint, array $enumIndexes, array $enumValueIndexes, ByteBufferWriter $out) : void{ + LE::writeUnsignedInt($out, $enumValueIndexes[$constraint->getAffectedValue()]); + LE::writeUnsignedInt($out, $enumIndexes[$constraint->getEnum()->getName()]); + VarInt::writeUnsignedInt($out, count($constraint->getConstraints())); foreach($constraint->getConstraints() as $v){ - $out->putByte($v); + Byte::writeUnsigned($out, $v); } } @@ -347,31 +352,31 @@ protected function putEnumConstraint(CommandEnumConstraint $constraint, array $e * @param ChainedSubCommandData[] $allChainedSubCommandData * * @throws PacketDecodeException - * @throws BinaryDataException + * @throws DataDecodeException */ - protected function getCommandData(array $enums, array $postfixes, array $allChainedSubCommandData, PacketSerializer $in) : CommandData{ - $name = $in->getString(); - $description = $in->getString(); - $flags = $in->getLShort(); - $permission = $in->getByte(); - $aliases = $enums[$in->getLInt()] ?? null; + protected function getCommandData(array $enums, array $postfixes, array $allChainedSubCommandData, ByteBufferReader $in) : CommandData{ + $name = CommonTypes::getString($in); + $description = CommonTypes::getString($in); + $flags = LE::readUnsignedShort($in); + $permission = Byte::readUnsigned($in); + $aliases = $enums[LE::readSignedInt($in)] ?? null; $chainedSubCommandData = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ - $index = $in->getLShort(); + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ + $index = LE::readUnsignedShort($in); $chainedSubCommandData[] = $allChainedSubCommandData[$index] ?? throw new PacketDecodeException("Unknown chained subcommand data index $index"); } $overloads = []; - for($overloadIndex = 0, $overloadCount = $in->getUnsignedVarInt(); $overloadIndex < $overloadCount; ++$overloadIndex){ + for($overloadIndex = 0, $overloadCount = VarInt::readUnsignedInt($in); $overloadIndex < $overloadCount; ++$overloadIndex){ $parameters = []; - $isChaining = $in->getBool(); - for($paramIndex = 0, $paramCount = $in->getUnsignedVarInt(); $paramIndex < $paramCount; ++$paramIndex){ + $isChaining = CommonTypes::getBool($in); + for($paramIndex = 0, $paramCount = VarInt::readUnsignedInt($in); $paramIndex < $paramCount; ++$paramIndex){ $parameter = new CommandParameter(); - $parameter->paramName = $in->getString(); - $parameter->paramType = $in->getLInt(); - $parameter->isOptional = $in->getBool(); - $parameter->flags = $in->getByte(); + $parameter->paramName = CommonTypes::getString($in); + $parameter->paramType = LE::readUnsignedInt($in); + $parameter->isOptional = CommonTypes::getBool($in); + $parameter->flags = Byte::readUnsigned($in); if(($parameter->paramType & self::ARG_FLAG_ENUM) !== 0){ $index = ($parameter->paramType & 0xffff); @@ -403,31 +408,31 @@ protected function getCommandData(array $enums, array $postfixes, array $allChai * @param int[] $postfixIndexes * @param int[] $chainedSubCommandDataIndexes */ - protected function putCommandData(CommandData $data, array $enumIndexes, array $softEnumIndexes, array $postfixIndexes, array $chainedSubCommandDataIndexes, PacketSerializer $out) : void{ - $out->putString($data->name); - $out->putString($data->description); - $out->putLShort($data->flags); - $out->putByte($data->permission); + protected function putCommandData(CommandData $data, array $enumIndexes, array $softEnumIndexes, array $postfixIndexes, array $chainedSubCommandDataIndexes, ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $data->name); + CommonTypes::putString($out, $data->description); + LE::writeUnsignedShort($out, $data->flags); + Byte::writeUnsigned($out, $data->permission); if($data->aliases !== null){ - $out->putLInt($enumIndexes[$data->aliases->getName()] ?? -1); + LE::writeSignedInt($out, $enumIndexes[$data->aliases->getName()] ?? -1); }else{ - $out->putLInt(-1); + LE::writeSignedInt($out, -1); } - $out->putUnsignedVarInt(count($data->chainedSubCommandData)); + VarInt::writeUnsignedInt($out, count($data->chainedSubCommandData)); foreach($data->chainedSubCommandData as $chainedSubCommandData){ $index = $chainedSubCommandDataIndexes[$chainedSubCommandData->getName()] ?? throw new \LogicException("Chained subcommand data {$chainedSubCommandData->getName()} does not have an index (this should be impossible)"); - $out->putLShort($index); + LE::writeUnsignedShort($out, $index); } - $out->putUnsignedVarInt(count($data->overloads)); + VarInt::writeUnsignedInt($out, count($data->overloads)); foreach($data->overloads as $overload){ - $out->putBool($overload->isChaining()); - $out->putUnsignedVarInt(count($overload->getParameters())); + CommonTypes::putBool($out, $overload->isChaining()); + VarInt::writeUnsignedInt($out, count($overload->getParameters())); foreach($overload->getParameters() as $parameter){ - $out->putString($parameter->paramName); + CommonTypes::putString($out, $parameter->paramName); if($parameter->enum !== null){ if($parameter->enum->isSoft()){ @@ -444,14 +449,14 @@ protected function putCommandData(CommandData $data, array $enumIndexes, array $ $type = $parameter->paramType; } - $out->putLInt($type); - $out->putBool($parameter->isOptional); - $out->putByte($parameter->flags); + LE::writeUnsignedInt($out, $type); + CommonTypes::putBool($out, $parameter->isOptional); + Byte::writeUnsigned($out, $parameter->flags); } } } - protected function encodePayload(PacketSerializer $out) : void{ + protected function encodePayload(ByteBufferWriter $out) : void{ /** * @var int[] $enumValueIndexes * @phpstan-var array $enumValueIndexes @@ -553,49 +558,49 @@ protected function encodePayload(PacketSerializer $out) : void{ } } - $out->putUnsignedVarInt(count($enumValueIndexes)); + VarInt::writeUnsignedInt($out, count($enumValueIndexes)); foreach($enumValueIndexes as $enumValue => $index){ - $out->putString((string) $enumValue); //stupid PHP key casting D: + CommonTypes::putString($out, (string) $enumValue); //stupid PHP key casting D: } - $out->putUnsignedVarInt(count($chainedSubCommandValueNameIndexes)); + VarInt::writeUnsignedInt($out, count($chainedSubCommandValueNameIndexes)); foreach($chainedSubCommandValueNameIndexes as $chainedSubCommandValueName => $index){ - $out->putString((string) $chainedSubCommandValueName); //stupid PHP key casting D: + CommonTypes::putString($out, (string) $chainedSubCommandValueName); //stupid PHP key casting D: } - $out->putUnsignedVarInt(count($postfixIndexes)); + VarInt::writeUnsignedInt($out, count($postfixIndexes)); foreach($postfixIndexes as $postfix => $index){ - $out->putString((string) $postfix); //stupid PHP key casting D: + CommonTypes::putString($out, (string) $postfix); //stupid PHP key casting D: } - $out->putUnsignedVarInt(count($enums)); + VarInt::writeUnsignedInt($out, count($enums)); foreach($enums as $enum){ $this->putEnum($enum, $enumValueIndexes, $out); } - $out->putUnsignedVarInt(count($allChainedSubCommandData)); + VarInt::writeUnsignedInt($out, count($allChainedSubCommandData)); foreach($allChainedSubCommandData as $chainedSubCommandData){ - $out->putString($chainedSubCommandData->getName()); - $out->putUnsignedVarInt(count($chainedSubCommandData->getValues())); + CommonTypes::putString($out, $chainedSubCommandData->getName()); + VarInt::writeUnsignedInt($out, count($chainedSubCommandData->getValues())); foreach($chainedSubCommandData->getValues() as $value){ $valueNameIndex = $chainedSubCommandValueNameIndexes[$value->getName()] ?? throw new \LogicException("Chained subcommand value name index for \"" . $value->getName() . "\" not found (this should never happen)"); - $out->putLShort($valueNameIndex); - $out->putLShort($value->getType()); + LE::writeUnsignedShort($out, $valueNameIndex); + LE::writeUnsignedShort($out, $value->getType()); } } - $out->putUnsignedVarInt(count($this->commandData)); + VarInt::writeUnsignedInt($out, count($this->commandData)); foreach($this->commandData as $data){ $this->putCommandData($data, $enumIndexes, $softEnumIndexes, $postfixIndexes, $chainedSubCommandDataIndexes, $out); } - $out->putUnsignedVarInt(count($softEnums)); + VarInt::writeUnsignedInt($out, count($softEnums)); foreach($softEnums as $enum){ $this->putSoftEnum($enum, $out); } - $out->putUnsignedVarInt(count($this->enumConstraints)); + VarInt::writeUnsignedInt($out, count($this->enumConstraints)); foreach($this->enumConstraints as $constraint){ $this->putEnumConstraint($constraint, $enumIndexes, $enumValueIndexes, $out); } diff --git a/src/AwardAchievementPacket.php b/src/AwardAchievementPacket.php index b651a64c..9b2afc1d 100644 --- a/src/AwardAchievementPacket.php +++ b/src/AwardAchievementPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; class AwardAchievementPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::AWARD_ACHIEVEMENT_PACKET; @@ -32,12 +34,12 @@ public static function create(int $achievementId) : self{ public function getAchievementId() : int{ return $this->achievementId; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->achievementId = $in->getLInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->achievementId = LE::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLInt($this->achievementId); + protected function encodePayload(ByteBufferWriter $out) : void{ + LE::writeSignedInt($out, $this->achievementId); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/BiomeDefinitionListPacket.php b/src/BiomeDefinitionListPacket.php index 2c2e5b1a..31ff7a87 100644 --- a/src/BiomeDefinitionListPacket.php +++ b/src/BiomeDefinitionListPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\biome\BiomeDefinitionData; use pocketmine\network\mcpe\protocol\types\biome\BiomeDefinitionEntry; use function array_map; @@ -122,25 +125,25 @@ public function buildDefinitionsFromData() : array{ ), $this->definitionData); } - protected function decodePayload(PacketSerializer $in) : void{ - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + protected function decodePayload(ByteBufferReader $in) : void{ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $this->definitionData[] = BiomeDefinitionData::read($in); } - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ - $this->strings[] = $in->getString(); + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ + $this->strings[] = CommonTypes::getString($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->definitionData)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->definitionData)); foreach($this->definitionData as $data){ $data->write($out); } - $out->putUnsignedVarInt(count($this->strings)); + VarInt::writeUnsignedInt($out, count($this->strings)); foreach($this->strings as $string){ - $out->putString($string); + CommonTypes::putString($out, $string); } } diff --git a/src/BlockActorDataPacket.php b/src/BlockActorDataPacket.php index aad2a095..b83b28c8 100644 --- a/src/BlockActorDataPacket.php +++ b/src/BlockActorDataPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; use pocketmine\network\mcpe\protocol\types\CacheableNbt; @@ -36,14 +38,14 @@ public static function create(BlockPosition $blockPosition, CacheableNbt $nbt) : return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->blockPosition = $in->getBlockPosition(); - $this->nbt = new CacheableNbt($in->getNbtCompoundRoot()); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->blockPosition = CommonTypes::getBlockPosition($in); + $this->nbt = new CacheableNbt(CommonTypes::getNbtCompoundRoot($in)); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putBlockPosition($this->blockPosition); - $out->put($this->nbt->getEncodedNbt()); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putBlockPosition($out, $this->blockPosition); + $out->writeByteArray($this->nbt->getEncodedNbt()); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/BlockEventPacket.php b/src/BlockEventPacket.php index 152154f6..104ce6dd 100644 --- a/src/BlockEventPacket.php +++ b/src/BlockEventPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; class BlockEventPacket extends DataPacket implements ClientboundPacket{ @@ -35,16 +38,16 @@ public static function create(BlockPosition $blockPosition, int $eventType, int return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->blockPosition = $in->getBlockPosition(); - $this->eventType = $in->getVarInt(); - $this->eventData = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->blockPosition = CommonTypes::getBlockPosition($in); + $this->eventType = VarInt::readSignedInt($in); + $this->eventData = VarInt::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putBlockPosition($this->blockPosition); - $out->putVarInt($this->eventType); - $out->putVarInt($this->eventData); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putBlockPosition($out, $this->blockPosition); + VarInt::writeSignedInt($out, $this->eventType); + VarInt::writeSignedInt($out, $this->eventData); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/BlockPickRequestPacket.php b/src/BlockPickRequestPacket.php index a9837262..a11c27fa 100644 --- a/src/BlockPickRequestPacket.php +++ b/src/BlockPickRequestPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; class BlockPickRequestPacket extends DataPacket implements ServerboundPacket{ @@ -35,16 +38,16 @@ public static function create(BlockPosition $blockPosition, bool $addUserData, i return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->blockPosition = $in->getSignedBlockPosition(); - $this->addUserData = $in->getBool(); - $this->hotbarSlot = $in->getByte(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->blockPosition = CommonTypes::getSignedBlockPosition($in); + $this->addUserData = CommonTypes::getBool($in); + $this->hotbarSlot = Byte::readUnsigned($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putSignedBlockPosition($this->blockPosition); - $out->putBool($this->addUserData); - $out->putByte($this->hotbarSlot); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putSignedBlockPosition($out, $this->blockPosition); + CommonTypes::putBool($out, $this->addUserData); + Byte::writeUnsigned($out, $this->hotbarSlot); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/BookEditPacket.php b/src/BookEditPacket.php index b623ea4a..e33509aa 100644 --- a/src/BookEditPacket.php +++ b/src/BookEditPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class BookEditPacket extends DataPacket implements ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::BOOK_EDIT_PACKET; @@ -35,56 +38,56 @@ class BookEditPacket extends DataPacket implements ServerboundPacket{ public string $author; public string $xuid; - protected function decodePayload(PacketSerializer $in) : void{ - $this->type = $in->getByte(); - $this->inventorySlot = $in->getByte(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->type = Byte::readUnsigned($in); + $this->inventorySlot = Byte::readUnsigned($in); switch($this->type){ case self::TYPE_REPLACE_PAGE: case self::TYPE_ADD_PAGE: - $this->pageNumber = $in->getByte(); - $this->text = $in->getString(); - $this->photoName = $in->getString(); + $this->pageNumber = Byte::readUnsigned($in); + $this->text = CommonTypes::getString($in); + $this->photoName = CommonTypes::getString($in); break; case self::TYPE_DELETE_PAGE: - $this->pageNumber = $in->getByte(); + $this->pageNumber = Byte::readUnsigned($in); break; case self::TYPE_SWAP_PAGES: - $this->pageNumber = $in->getByte(); - $this->secondaryPageNumber = $in->getByte(); + $this->pageNumber = Byte::readUnsigned($in); + $this->secondaryPageNumber = Byte::readUnsigned($in); break; case self::TYPE_SIGN_BOOK: - $this->title = $in->getString(); - $this->author = $in->getString(); - $this->xuid = $in->getString(); + $this->title = CommonTypes::getString($in); + $this->author = CommonTypes::getString($in); + $this->xuid = CommonTypes::getString($in); break; default: throw new PacketDecodeException("Unknown book edit type $this->type!"); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->type); - $out->putByte($this->inventorySlot); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->type); + Byte::writeUnsigned($out, $this->inventorySlot); switch($this->type){ case self::TYPE_REPLACE_PAGE: case self::TYPE_ADD_PAGE: - $out->putByte($this->pageNumber); - $out->putString($this->text); - $out->putString($this->photoName); + Byte::writeUnsigned($out, $this->pageNumber); + CommonTypes::putString($out, $this->text); + CommonTypes::putString($out, $this->photoName); break; case self::TYPE_DELETE_PAGE: - $out->putByte($this->pageNumber); + Byte::writeUnsigned($out, $this->pageNumber); break; case self::TYPE_SWAP_PAGES: - $out->putByte($this->pageNumber); - $out->putByte($this->secondaryPageNumber); + Byte::writeUnsigned($out, $this->pageNumber); + Byte::writeUnsigned($out, $this->secondaryPageNumber); break; case self::TYPE_SIGN_BOOK: - $out->putString($this->title); - $out->putString($this->author); - $out->putString($this->xuid); + CommonTypes::putString($out, $this->title); + CommonTypes::putString($out, $this->author); + CommonTypes::putString($out, $this->xuid); break; default: throw new \InvalidArgumentException("Unknown book edit type $this->type!"); diff --git a/src/BossEventPacket.php b/src/BossEventPacket.php index 647dc63d..0e86a20a 100644 --- a/src/BossEventPacket.php +++ b/src/BossEventPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BossBarColor; class BossEventPacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ @@ -111,70 +115,70 @@ public static function query(int $bossActorUniqueId, int $playerActorUniqueId) : return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->bossActorUniqueId = $in->getActorUniqueId(); - $this->eventType = $in->getUnsignedVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->bossActorUniqueId = CommonTypes::getActorUniqueId($in); + $this->eventType = VarInt::readUnsignedInt($in); switch($this->eventType){ case self::TYPE_REGISTER_PLAYER: case self::TYPE_UNREGISTER_PLAYER: case self::TYPE_QUERY: - $this->playerActorUniqueId = $in->getActorUniqueId(); + $this->playerActorUniqueId = CommonTypes::getActorUniqueId($in); break; /** @noinspection PhpMissingBreakStatementInspection */ case self::TYPE_SHOW: - $this->title = $in->getString(); - $this->filteredTitle = $in->getString(); - $this->healthPercent = $in->getLFloat(); + $this->title = CommonTypes::getString($in); + $this->filteredTitle = CommonTypes::getString($in); + $this->healthPercent = LE::readFloat($in); /** @noinspection PhpMissingBreakStatementInspection */ case self::TYPE_PROPERTIES: - $this->darkenScreen = match($raw = $in->getLShort()){ + $this->darkenScreen = match($raw = LE::readUnsignedShort($in)){ 0 => false, 1 => true, default => throw new PacketDecodeException("Invalid darkenScreen value $raw"), }; case self::TYPE_TEXTURE: - $this->color = $in->getUnsignedVarInt(); - $this->overlay = $in->getUnsignedVarInt(); + $this->color = VarInt::readUnsignedInt($in); + $this->overlay = VarInt::readUnsignedInt($in); break; case self::TYPE_HEALTH_PERCENT: - $this->healthPercent = $in->getLFloat(); + $this->healthPercent = LE::readFloat($in); break; case self::TYPE_TITLE: - $this->title = $in->getString(); - $this->filteredTitle = $in->getString(); + $this->title = CommonTypes::getString($in); + $this->filteredTitle = CommonTypes::getString($in); break; default: break; } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorUniqueId($this->bossActorUniqueId); - $out->putUnsignedVarInt($this->eventType); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorUniqueId($out, $this->bossActorUniqueId); + VarInt::writeUnsignedInt($out, $this->eventType); switch($this->eventType){ case self::TYPE_REGISTER_PLAYER: case self::TYPE_UNREGISTER_PLAYER: case self::TYPE_QUERY: - $out->putActorUniqueId($this->playerActorUniqueId); + CommonTypes::putActorUniqueId($out, $this->playerActorUniqueId); break; /** @noinspection PhpMissingBreakStatementInspection */ case self::TYPE_SHOW: - $out->putString($this->title); - $out->putString($this->filteredTitle); - $out->putLFloat($this->healthPercent); + CommonTypes::putString($out, $this->title); + CommonTypes::putString($out, $this->filteredTitle); + LE::writeFloat($out, $this->healthPercent); /** @noinspection PhpMissingBreakStatementInspection */ case self::TYPE_PROPERTIES: - $out->putLShort($this->darkenScreen ? 1 : 0); + LE::writeUnsignedShort($out, $this->darkenScreen ? 1 : 0); case self::TYPE_TEXTURE: - $out->putUnsignedVarInt($this->color); - $out->putUnsignedVarInt($this->overlay); + VarInt::writeUnsignedInt($out, $this->color); + VarInt::writeUnsignedInt($out, $this->overlay); break; case self::TYPE_HEALTH_PERCENT: - $out->putLFloat($this->healthPercent); + LE::writeFloat($out, $this->healthPercent); break; case self::TYPE_TITLE: - $out->putString($this->title); - $out->putString($this->filteredTitle); + CommonTypes::putString($out, $this->title); + CommonTypes::putString($out, $this->filteredTitle); break; default: break; diff --git a/src/CameraAimAssistPacket.php b/src/CameraAimAssistPacket.php index 6290c376..78cd702b 100644 --- a/src/CameraAimAssistPacket.php +++ b/src/CameraAimAssistPacket.php @@ -14,8 +14,12 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; use pocketmine\math\Vector2; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\camera\CameraAimAssistActionType; use pocketmine\network\mcpe\protocol\types\camera\CameraAimAssistTargetMode; @@ -55,22 +59,22 @@ public function getActionType() : CameraAimAssistActionType{ return $this->actio public function getShowDebugRender() : bool{ return $this->showDebugRender; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->presetId = $in->getString(); - $this->viewAngle = $in->getVector2(); - $this->distance = $in->getLFloat(); - $this->targetMode = CameraAimAssistTargetMode::fromPacket($in->getByte()); - $this->actionType = CameraAimAssistActionType::fromPacket($in->getByte()); - $this->showDebugRender = $in->getBool(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->presetId = CommonTypes::getString($in); + $this->viewAngle = CommonTypes::getVector2($in); + $this->distance = LE::readFloat($in); + $this->targetMode = CameraAimAssistTargetMode::fromPacket(Byte::readUnsigned($in)); + $this->actionType = CameraAimAssistActionType::fromPacket(Byte::readUnsigned($in)); + $this->showDebugRender = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->presetId); - $out->putVector2($this->viewAngle); - $out->putLFloat($this->distance); - $out->putByte($this->targetMode->value); - $out->putByte($this->actionType->value); - $out->putBool($this->showDebugRender); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->presetId); + CommonTypes::putVector2($out, $this->viewAngle); + LE::writeFloat($out, $this->distance); + Byte::writeUnsigned($out, $this->targetMode->value); + Byte::writeUnsigned($out, $this->actionType->value); + CommonTypes::putBool($out, $this->showDebugRender); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/CameraAimAssistPresetsPacket.php b/src/CameraAimAssistPresetsPacket.php index 7bcfd154..a1a6ce3f 100644 --- a/src/CameraAimAssistPresetsPacket.php +++ b/src/CameraAimAssistPresetsPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\types\camera\CameraAimAssistCategory; use pocketmine\network\mcpe\protocol\types\camera\CameraAimAssistPreset; use function count; @@ -53,32 +56,32 @@ public function getPresets() : array{ return $this->presets; } public function getOperation() : int{ return $this->operation; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ $this->categories = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $this->categories[] = CameraAimAssistCategory::read($in); } $this->presets = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $this->presets[] = CameraAimAssistPreset::read($in); } - $this->operation = $in->getByte(); + $this->operation = Byte::readUnsigned($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->categories)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->categories)); foreach($this->categories as $category){ $category->write($out); } - $out->putUnsignedVarInt(count($this->presets)); + VarInt::writeUnsignedInt($out, count($this->presets)); foreach($this->presets as $preset){ $preset->write($out); } - $out->putByte($this->operation); + Byte::writeUnsigned($out, $this->operation); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/CameraInstructionPacket.php b/src/CameraInstructionPacket.php index dd295aef..0ea2083e 100644 --- a/src/CameraInstructionPacket.php +++ b/src/CameraInstructionPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\camera\CameraFadeInstruction; use pocketmine\network\mcpe\protocol\types\camera\CameraFovInstruction; use pocketmine\network\mcpe\protocol\types\camera\CameraSetInstruction; @@ -56,22 +58,22 @@ public function getRemoveTarget() : ?bool{ return $this->removeTarget; } public function getFieldOfView() : ?CameraFovInstruction{ return $this->fieldOfView; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->set = $in->readOptional(fn() => CameraSetInstruction::read($in)); - $this->clear = $in->readOptional($in->getBool(...)); - $this->fade = $in->readOptional(fn() => CameraFadeInstruction::read($in)); - $this->target = $in->readOptional(fn() => CameraTargetInstruction::read($in)); - $this->removeTarget = $in->readOptional($in->getBool(...)); - $this->fieldOfView = $in->readOptional(fn() => CameraFovInstruction::read($in)); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->set = CommonTypes::readOptional($in, CameraSetInstruction::read(...)); + $this->clear = CommonTypes::readOptional($in, CommonTypes::getBool(...)); + $this->fade = CommonTypes::readOptional($in, CameraFadeInstruction::read(...)); + $this->target = CommonTypes::readOptional($in, CameraTargetInstruction::read(...)); + $this->removeTarget = CommonTypes::readOptional($in, CommonTypes::getBool(...)); + $this->fieldOfView = CommonTypes::readOptional($in, CameraFovInstruction::read(...)); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->writeOptional($this->set, fn(CameraSetInstruction $v) => $v->write($out)); - $out->writeOptional($this->clear, $out->putBool(...)); - $out->writeOptional($this->fade, fn(CameraFadeInstruction $v) => $v->write($out)); - $out->writeOptional($this->target, fn(CameraTargetInstruction $v) => $v->write($out)); - $out->writeOptional($this->removeTarget, $out->putBool(...)); - $out->writeOptional($this->fieldOfView, fn(CameraFovInstruction $v) => $v->write($out)); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::writeOptional($out, $this->set, fn(ByteBufferWriter $out, CameraSetInstruction $v) => $v->write($out)); + CommonTypes::writeOptional($out, $this->clear, CommonTypes::putBool(...)); + CommonTypes::writeOptional($out, $this->fade, fn(ByteBufferWriter $out, CameraFadeInstruction $v) => $v->write($out)); + CommonTypes::writeOptional($out, $this->target, fn(ByteBufferWriter $out, CameraTargetInstruction $v) => $v->write($out)); + CommonTypes::writeOptional($out, $this->removeTarget, CommonTypes::putBool(...)); + CommonTypes::writeOptional($out, $this->fieldOfView, fn(ByteBufferWriter $out, CameraFovInstruction $v) => $v->write($out)); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/CameraPacket.php b/src/CameraPacket.php index 15811fb2..19126dc0 100644 --- a/src/CameraPacket.php +++ b/src/CameraPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class CameraPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::CAMERA_PACKET; @@ -32,14 +34,14 @@ public static function create(int $cameraActorUniqueId, int $playerActorUniqueId return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->cameraActorUniqueId = $in->getActorUniqueId(); - $this->playerActorUniqueId = $in->getActorUniqueId(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->cameraActorUniqueId = CommonTypes::getActorUniqueId($in); + $this->playerActorUniqueId = CommonTypes::getActorUniqueId($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorUniqueId($this->cameraActorUniqueId); - $out->putActorUniqueId($this->playerActorUniqueId); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorUniqueId($out, $this->cameraActorUniqueId); + CommonTypes::putActorUniqueId($out, $this->playerActorUniqueId); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/CameraPresetsPacket.php b/src/CameraPresetsPacket.php index 01597d10..ca17504b 100644 --- a/src/CameraPresetsPacket.php +++ b/src/CameraPresetsPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\types\camera\CameraPreset; use function count; @@ -39,15 +41,15 @@ public static function create(array $presets) : self{ */ public function getPresets() : array{ return $this->presets; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ $this->presets = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; $i++){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; $i++){ $this->presets[] = CameraPreset::read($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->presets)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->presets)); foreach($this->presets as $preset){ $preset->write($out); } diff --git a/src/CameraShakePacket.php b/src/CameraShakePacket.php index b6209426..b97b306b 100644 --- a/src/CameraShakePacket.php +++ b/src/CameraShakePacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; class CameraShakePacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::CAMERA_SHAKE_PACKET; @@ -50,18 +53,18 @@ public function getShakeType() : int{ return $this->shakeType; } public function getShakeAction() : int{ return $this->shakeAction; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->intensity = $in->getLFloat(); - $this->duration = $in->getLFloat(); - $this->shakeType = $in->getByte(); - $this->shakeAction = $in->getByte(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->intensity = LE::readFloat($in); + $this->duration = LE::readFloat($in); + $this->shakeType = Byte::readUnsigned($in); + $this->shakeAction = Byte::readUnsigned($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLFloat($this->intensity); - $out->putLFloat($this->duration); - $out->putByte($this->shakeType); - $out->putByte($this->shakeAction); + protected function encodePayload(ByteBufferWriter $out) : void{ + LE::writeFloat($out, $this->intensity); + LE::writeFloat($out, $this->duration); + Byte::writeUnsigned($out, $this->shakeType); + Byte::writeUnsigned($out, $this->shakeAction); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ChangeDimensionPacket.php b/src/ChangeDimensionPacket.php index dd2f12ae..558b9c53 100644 --- a/src/ChangeDimensionPacket.php +++ b/src/ChangeDimensionPacket.php @@ -14,8 +14,12 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class ChangeDimensionPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::CHANGE_DIMENSION_PACKET; @@ -39,18 +43,18 @@ public static function create(int $dimension, Vector3 $position, bool $respawn, public function getLoadingScreenId() : ?int{ return $this->loadingScreenId; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->dimension = $in->getVarInt(); - $this->position = $in->getVector3(); - $this->respawn = $in->getBool(); - $this->loadingScreenId = $in->readOptional(fn() => $in->getLInt()); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->dimension = VarInt::readSignedInt($in); + $this->position = CommonTypes::getVector3($in); + $this->respawn = CommonTypes::getBool($in); + $this->loadingScreenId = CommonTypes::readOptional($in, LE::readUnsignedInt(...)); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->dimension); - $out->putVector3($this->position); - $out->putBool($this->respawn); - $out->writeOptional($this->loadingScreenId, $out->putLInt(...)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->dimension); + CommonTypes::putVector3($out, $this->position); + CommonTypes::putBool($out, $this->respawn); + CommonTypes::writeOptional($out, $this->loadingScreenId, LE::writeUnsignedInt(...)); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ChangeMobPropertyPacket.php b/src/ChangeMobPropertyPacket.php index 6e1418b7..82789e4a 100644 --- a/src/ChangeMobPropertyPacket.php +++ b/src/ChangeMobPropertyPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; /** * This appears to be some kind of debug packet. Does nothing in release mode. @@ -72,22 +76,22 @@ public function getIntValue() : int{ return $this->intValue; } public function getFloatValue() : float{ return $this->floatValue; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorUniqueId = $in->getActorUniqueId(); - $this->propertyName = $in->getString(); - $this->boolValue = $in->getBool(); - $this->stringValue = $in->getString(); - $this->intValue = $in->getVarInt(); - $this->floatValue = $in->getLFloat(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorUniqueId = CommonTypes::getActorUniqueId($in); + $this->propertyName = CommonTypes::getString($in); + $this->boolValue = CommonTypes::getBool($in); + $this->stringValue = CommonTypes::getString($in); + $this->intValue = VarInt::readSignedInt($in); + $this->floatValue = LE::readFloat($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorUniqueId($this->actorUniqueId); - $out->putString($this->propertyName); - $out->putBool($this->boolValue); - $out->putString($this->stringValue); - $out->putVarInt($this->intValue); - $out->putLFloat($this->floatValue); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorUniqueId($out, $this->actorUniqueId); + CommonTypes::putString($out, $this->propertyName); + CommonTypes::putBool($out, $this->boolValue); + CommonTypes::putString($out, $this->stringValue); + VarInt::writeSignedInt($out, $this->intValue); + LE::writeFloat($out, $this->floatValue); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ChunkRadiusUpdatedPacket.php b/src/ChunkRadiusUpdatedPacket.php index c8f6b2b1..613c5863 100644 --- a/src/ChunkRadiusUpdatedPacket.php +++ b/src/ChunkRadiusUpdatedPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; class ChunkRadiusUpdatedPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::CHUNK_RADIUS_UPDATED_PACKET; @@ -30,12 +32,12 @@ public static function create(int $radius) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->radius = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->radius = VarInt::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->radius); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->radius); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ClientCacheBlobStatusPacket.php b/src/ClientCacheBlobStatusPacket.php index a53d84f4..2db23102 100644 --- a/src/ClientCacheBlobStatusPacket.php +++ b/src/ClientCacheBlobStatusPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; use function count; class ClientCacheBlobStatusPacket extends DataPacket implements ServerboundPacket{ @@ -51,25 +54,25 @@ public function getMissHashes() : array{ return $this->missHashes; } - protected function decodePayload(PacketSerializer $in) : void{ - $missCount = $in->getUnsignedVarInt(); - $hitCount = $in->getUnsignedVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $missCount = VarInt::readUnsignedInt($in); + $hitCount = VarInt::readUnsignedInt($in); for($i = 0; $i < $missCount; ++$i){ - $this->missHashes[] = $in->getLLong(); + $this->missHashes[] = LE::readUnsignedLong($in); } for($i = 0; $i < $hitCount; ++$i){ - $this->hitHashes[] = $in->getLLong(); + $this->hitHashes[] = LE::readUnsignedLong($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->missHashes)); - $out->putUnsignedVarInt(count($this->hitHashes)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->missHashes)); + VarInt::writeUnsignedInt($out, count($this->hitHashes)); foreach($this->missHashes as $hash){ - $out->putLLong($hash); + LE::writeUnsignedLong($out, $hash); } foreach($this->hitHashes as $hash){ - $out->putLLong($hash); + LE::writeUnsignedLong($out, $hash); } } diff --git a/src/ClientCacheMissResponsePacket.php b/src/ClientCacheMissResponsePacket.php index 9affe1c5..2e415808 100644 --- a/src/ClientCacheMissResponsePacket.php +++ b/src/ClientCacheMissResponsePacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\ChunkCacheBlob; use function count; @@ -41,19 +45,19 @@ public function getBlobs() : array{ return $this->blobs; } - protected function decodePayload(PacketSerializer $in) : void{ - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ - $hash = $in->getLLong(); - $payload = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ + $hash = LE::readUnsignedLong($in); + $payload = CommonTypes::getString($in); $this->blobs[] = new ChunkCacheBlob($hash, $payload); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->blobs)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->blobs)); foreach($this->blobs as $blob){ - $out->putLLong($blob->getHash()); - $out->putString($blob->getPayload()); + LE::writeUnsignedLong($out, $blob->getHash()); + CommonTypes::putString($out, $blob->getPayload()); } } diff --git a/src/ClientCacheStatusPacket.php b/src/ClientCacheStatusPacket.php index 6980234d..7962bdc9 100644 --- a/src/ClientCacheStatusPacket.php +++ b/src/ClientCacheStatusPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class ClientCacheStatusPacket extends DataPacket implements ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::CLIENT_CACHE_STATUS_PACKET; @@ -34,12 +36,12 @@ public function isEnabled() : bool{ return $this->enabled; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->enabled = $in->getBool(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->enabled = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putBool($this->enabled); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putBool($out, $this->enabled); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ClientCameraAimAssistPacket.php b/src/ClientCameraAimAssistPacket.php index 484e5df9..46b86551 100644 --- a/src/ClientCameraAimAssistPacket.php +++ b/src/ClientCameraAimAssistPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\camera\CameraAimAssistActionType; class ClientCameraAimAssistPacket extends DataPacket implements ServerboundPacket{ @@ -41,16 +44,16 @@ public function getActionType() : CameraAimAssistActionType{ return $this->actio public function getAllowAimAssist() : bool{ return $this->allowAimAssist; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->presetId = $in->getString(); - $this->actionType = CameraAimAssistActionType::fromPacket($in->getByte()); - $this->allowAimAssist = $in->getBool(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->presetId = CommonTypes::getString($in); + $this->actionType = CameraAimAssistActionType::fromPacket(Byte::readUnsigned($in)); + $this->allowAimAssist = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->presetId); - $out->putByte($this->actionType->value); - $out->putBool($this->allowAimAssist); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->presetId); + Byte::writeUnsigned($out, $this->actionType->value); + CommonTypes::putBool($out, $this->allowAimAssist); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ClientMovementPredictionSyncPacket.php b/src/ClientMovementPredictionSyncPacket.php index da386990..88842fd2 100644 --- a/src/ClientMovementPredictionSyncPacket.php +++ b/src/ClientMovementPredictionSyncPacket.php @@ -14,8 +14,11 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; use pocketmine\network\mcpe\protocol\serializer\BitSet; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags; class ClientMovementPredictionSyncPacket extends DataPacket implements ServerboundPacket{ @@ -117,34 +120,34 @@ public function getActorUniqueId() : int{ return $this->actorUniqueId; } public function getActorFlyingState() : bool{ return $this->actorFlyingState; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ $this->flags = BitSet::read($in, self::FLAG_LENGTH); - $this->scale = $in->getLFloat(); - $this->width = $in->getLFloat(); - $this->height = $in->getLFloat(); - $this->movementSpeed = $in->getLFloat(); - $this->underwaterMovementSpeed = $in->getLFloat(); - $this->lavaMovementSpeed = $in->getLFloat(); - $this->jumpStrength = $in->getLFloat(); - $this->health = $in->getLFloat(); - $this->hunger = $in->getLFloat(); - $this->actorUniqueId = $in->getActorUniqueId(); - $this->actorFlyingState = $in->getBool(); + $this->scale = LE::readFloat($in); + $this->width = LE::readFloat($in); + $this->height = LE::readFloat($in); + $this->movementSpeed = LE::readFloat($in); + $this->underwaterMovementSpeed = LE::readFloat($in); + $this->lavaMovementSpeed = LE::readFloat($in); + $this->jumpStrength = LE::readFloat($in); + $this->health = LE::readFloat($in); + $this->hunger = LE::readFloat($in); + $this->actorUniqueId = CommonTypes::getActorUniqueId($in); + $this->actorFlyingState = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ + protected function encodePayload(ByteBufferWriter $out) : void{ $this->flags->write($out); - $out->putLFloat($this->scale); - $out->putLFloat($this->width); - $out->putLFloat($this->height); - $out->putLFloat($this->movementSpeed); - $out->putLFloat($this->underwaterMovementSpeed); - $out->putLFloat($this->lavaMovementSpeed); - $out->putLFloat($this->jumpStrength); - $out->putLFloat($this->health); - $out->putLFloat($this->hunger); - $out->putActorUniqueId($this->actorUniqueId); - $out->putBool($this->actorFlyingState); + LE::writeFloat($out, $this->scale); + LE::writeFloat($out, $this->width); + LE::writeFloat($out, $this->height); + LE::writeFloat($out, $this->movementSpeed); + LE::writeFloat($out, $this->underwaterMovementSpeed); + LE::writeFloat($out, $this->lavaMovementSpeed); + LE::writeFloat($out, $this->jumpStrength); + LE::writeFloat($out, $this->health); + LE::writeFloat($out, $this->hunger); + CommonTypes::putActorUniqueId($out, $this->actorUniqueId); + CommonTypes::putBool($out, $this->actorFlyingState); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ClientToServerHandshakePacket.php b/src/ClientToServerHandshakePacket.php index d2e2f0ed..72ed90b3 100644 --- a/src/ClientToServerHandshakePacket.php +++ b/src/ClientToServerHandshakePacket.php @@ -14,7 +14,8 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; class ClientToServerHandshakePacket extends DataPacket implements ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::CLIENT_TO_SERVER_HANDSHAKE_PACKET; @@ -30,11 +31,11 @@ public function canBeSentBeforeLogin() : bool{ return true; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ //No payload } - protected function encodePayload(PacketSerializer $out) : void{ + protected function encodePayload(ByteBufferWriter $out) : void{ //No payload } diff --git a/src/ClientboundCloseFormPacket.php b/src/ClientboundCloseFormPacket.php index b8bf5b70..5aef33c5 100644 --- a/src/ClientboundCloseFormPacket.php +++ b/src/ClientboundCloseFormPacket.php @@ -14,7 +14,8 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; class ClientboundCloseFormPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::CLIENTBOUND_CLOSE_FORM_PACKET; @@ -26,11 +27,11 @@ public static function create() : self{ return new self; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ //No payload } - protected function encodePayload(PacketSerializer $out) : void{ + protected function encodePayload(ByteBufferWriter $out) : void{ //No payload } diff --git a/src/ClientboundControlSchemeSetPacket.php b/src/ClientboundControlSchemeSetPacket.php index d3a359f6..cfad7c60 100644 --- a/src/ClientboundControlSchemeSetPacket.php +++ b/src/ClientboundControlSchemeSetPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use pocketmine\network\mcpe\protocol\types\ControlScheme; class ClientboundControlSchemeSetPacket extends DataPacket implements ClientboundPacket{ @@ -33,12 +35,12 @@ public static function create(ControlScheme $scheme) : self{ public function getScheme() : ControlScheme{ return $this->scheme; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->scheme = ControlScheme::fromPacket($in->getByte()); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->scheme = ControlScheme::fromPacket(Byte::readUnsigned($in)); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->scheme->value); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->scheme->value); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ClientboundDebugRendererPacket.php b/src/ClientboundDebugRendererPacket.php index b10a6fb6..586313b4 100644 --- a/src/ClientboundDebugRendererPacket.php +++ b/src/ClientboundDebugRendererPacket.php @@ -14,8 +14,11 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class ClientboundDebugRendererPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::CLIENTBOUND_DEBUG_RENDERER_PACKET; @@ -70,42 +73,42 @@ public function getAlpha() : float{ return $this->alpha; } public function getDurationMillis() : int{ return $this->durationMillis; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->type = $in->getLInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->type = LE::readUnsignedInt($in); switch($this->type){ case self::TYPE_CLEAR: //NOOP break; case self::TYPE_ADD_CUBE: - $this->text = $in->getString(); - $this->position = $in->getVector3(); - $this->red = $in->getLFloat(); - $this->green = $in->getLFloat(); - $this->blue = $in->getLFloat(); - $this->alpha = $in->getLFloat(); - $this->durationMillis = $in->getLLong(); + $this->text = CommonTypes::getString($in); + $this->position = CommonTypes::getVector3($in); + $this->red = LE::readFloat($in); + $this->green = LE::readFloat($in); + $this->blue = LE::readFloat($in); + $this->alpha = LE::readFloat($in); + $this->durationMillis = LE::readUnsignedLong($in); break; default: throw new PacketDecodeException("Unknown type " . $this->type); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLInt($this->type); + protected function encodePayload(ByteBufferWriter $out) : void{ + LE::writeUnsignedInt($out, $this->type); switch($this->type){ case self::TYPE_CLEAR: //NOOP break; case self::TYPE_ADD_CUBE: - $out->putString($this->text); - $out->putVector3($this->position); - $out->putLFloat($this->red); - $out->putLFloat($this->green); - $out->putLFloat($this->blue); - $out->putLFloat($this->alpha); - $out->putLLong($this->durationMillis); + CommonTypes::putString($out, $this->text); + CommonTypes::putVector3($out, $this->position); + LE::writeFloat($out, $this->red); + LE::writeFloat($out, $this->green); + LE::writeFloat($out, $this->blue); + LE::writeFloat($out, $this->alpha); + LE::writeUnsignedLong($out, $this->durationMillis); break; default: throw new \InvalidArgumentException("Unknown type " . $this->type); diff --git a/src/ClientboundMapItemDataPacket.php b/src/ClientboundMapItemDataPacket.php index 260cc740..f5956d53 100644 --- a/src/ClientboundMapItemDataPacket.php +++ b/src/ClientboundMapItemDataPacket.php @@ -14,8 +14,13 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; use pocketmine\color\Color; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; use pocketmine\network\mcpe\protocol\types\DimensionIds; use pocketmine\network\mcpe\protocol\types\MapDecoration; @@ -50,56 +55,56 @@ class ClientboundMapItemDataPacket extends DataPacket implements ClientboundPack public int $yOffset = 0; public ?MapImage $colors = null; - protected function decodePayload(PacketSerializer $in) : void{ - $this->mapId = $in->getActorUniqueId(); - $this->type = $in->getUnsignedVarInt(); - $this->dimensionId = $in->getByte(); - $this->isLocked = $in->getBool(); - $this->origin = $in->getSignedBlockPosition(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->mapId = CommonTypes::getActorUniqueId($in); + $this->type = VarInt::readUnsignedInt($in); + $this->dimensionId = Byte::readUnsigned($in); + $this->isLocked = CommonTypes::getBool($in); + $this->origin = CommonTypes::getSignedBlockPosition($in); if(($this->type & self::BITFLAG_MAP_CREATION) !== 0){ - $count = $in->getUnsignedVarInt(); + $count = VarInt::readUnsignedInt($in); for($i = 0; $i < $count; ++$i){ - $this->parentMapIds[] = $in->getActorUniqueId(); + $this->parentMapIds[] = CommonTypes::getActorUniqueId($in); } } if(($this->type & (self::BITFLAG_MAP_CREATION | self::BITFLAG_DECORATION_UPDATE | self::BITFLAG_TEXTURE_UPDATE)) !== 0){ //Decoration bitflag or colour bitflag - $this->scale = $in->getByte(); + $this->scale = Byte::readUnsigned($in); } if(($this->type & self::BITFLAG_DECORATION_UPDATE) !== 0){ - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $object = new MapTrackedObject(); - $object->type = $in->getLInt(); + $object->type = LE::readUnsignedInt($in); if($object->type === MapTrackedObject::TYPE_BLOCK){ - $object->blockPosition = $in->getBlockPosition(); + $object->blockPosition = CommonTypes::getBlockPosition($in); }elseif($object->type === MapTrackedObject::TYPE_ENTITY){ - $object->actorUniqueId = $in->getActorUniqueId(); + $object->actorUniqueId = CommonTypes::getActorUniqueId($in); }else{ throw new PacketDecodeException("Unknown map object type $object->type"); } $this->trackedEntities[] = $object; } - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ - $icon = $in->getByte(); - $rotation = $in->getByte(); - $xOffset = $in->getByte(); - $yOffset = $in->getByte(); - $label = $in->getString(); - $color = Color::fromRGBA(Binary::flipIntEndianness($in->getUnsignedVarInt())); + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ + $icon = Byte::readUnsigned($in); + $rotation = Byte::readUnsigned($in); + $xOffset = Byte::readUnsigned($in); + $yOffset = Byte::readUnsigned($in); + $label = CommonTypes::getString($in); + $color = Color::fromRGBA(Binary::flipIntEndianness(VarInt::readUnsignedInt($in))); $this->decorations[] = new MapDecoration($icon, $rotation, $xOffset, $yOffset, $label, $color); } } if(($this->type & self::BITFLAG_TEXTURE_UPDATE) !== 0){ - $width = $in->getVarInt(); - $height = $in->getVarInt(); - $this->xOffset = $in->getVarInt(); - $this->yOffset = $in->getVarInt(); + $width = VarInt::readSignedInt($in); + $height = VarInt::readSignedInt($in); + $this->xOffset = VarInt::readSignedInt($in); + $this->yOffset = VarInt::readSignedInt($in); - $count = $in->getUnsignedVarInt(); + $count = VarInt::readUnsignedInt($in); if($count !== $width * $height){ throw new PacketDecodeException("Expected colour count of " . ($height * $width) . " (height $height * width $width), got $count"); } @@ -108,8 +113,8 @@ protected function decodePayload(PacketSerializer $in) : void{ } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorUniqueId($this->mapId); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorUniqueId($out, $this->mapId); $type = 0; if(($parentMapIdsCount = count($this->parentMapIds)) > 0){ @@ -122,53 +127,53 @@ protected function encodePayload(PacketSerializer $out) : void{ $type |= self::BITFLAG_TEXTURE_UPDATE; } - $out->putUnsignedVarInt($type); - $out->putByte($this->dimensionId); - $out->putBool($this->isLocked); - $out->putSignedBlockPosition($this->origin); + VarInt::writeUnsignedInt($out, $type); + Byte::writeUnsigned($out, $this->dimensionId); + CommonTypes::putBool($out, $this->isLocked); + CommonTypes::putSignedBlockPosition($out, $this->origin); if(($type & self::BITFLAG_MAP_CREATION) !== 0){ - $out->putUnsignedVarInt($parentMapIdsCount); + VarInt::writeUnsignedInt($out, $parentMapIdsCount); foreach($this->parentMapIds as $parentMapId){ - $out->putActorUniqueId($parentMapId); + CommonTypes::putActorUniqueId($out, $parentMapId); } } if(($type & (self::BITFLAG_MAP_CREATION | self::BITFLAG_TEXTURE_UPDATE | self::BITFLAG_DECORATION_UPDATE)) !== 0){ - $out->putByte($this->scale); + Byte::writeUnsigned($out, $this->scale); } if(($type & self::BITFLAG_DECORATION_UPDATE) !== 0){ - $out->putUnsignedVarInt(count($this->trackedEntities)); + VarInt::writeUnsignedInt($out, count($this->trackedEntities)); foreach($this->trackedEntities as $object){ - $out->putLInt($object->type); + LE::writeUnsignedInt($out, $object->type); if($object->type === MapTrackedObject::TYPE_BLOCK){ - $out->putBlockPosition($object->blockPosition); + CommonTypes::putBlockPosition($out, $object->blockPosition); }elseif($object->type === MapTrackedObject::TYPE_ENTITY){ - $out->putActorUniqueId($object->actorUniqueId); + CommonTypes::putActorUniqueId($out, $object->actorUniqueId); }else{ throw new \InvalidArgumentException("Unknown map object type $object->type"); } } - $out->putUnsignedVarInt($decorationCount); + VarInt::writeUnsignedInt($out, $decorationCount); foreach($this->decorations as $decoration){ - $out->putByte($decoration->getIcon()); - $out->putByte($decoration->getRotation()); - $out->putByte($decoration->getXOffset()); - $out->putByte($decoration->getYOffset()); - $out->putString($decoration->getLabel()); - $out->putUnsignedVarInt(Binary::flipIntEndianness($decoration->getColor()->toRGBA())); + Byte::writeUnsigned($out, $decoration->getIcon()); + Byte::writeUnsigned($out, $decoration->getRotation()); + Byte::writeUnsigned($out, $decoration->getXOffset()); + Byte::writeUnsigned($out, $decoration->getYOffset()); + CommonTypes::putString($out, $decoration->getLabel()); + VarInt::writeUnsignedInt($out, Binary::flipIntEndianness($decoration->getColor()->toRGBA())); } } if($this->colors !== null){ - $out->putVarInt($this->colors->getWidth()); - $out->putVarInt($this->colors->getHeight()); - $out->putVarInt($this->xOffset); - $out->putVarInt($this->yOffset); + VarInt::writeSignedInt($out, $this->colors->getWidth()); + VarInt::writeSignedInt($out, $this->colors->getHeight()); + VarInt::writeSignedInt($out, $this->xOffset); + VarInt::writeSignedInt($out, $this->yOffset); - $out->putUnsignedVarInt($this->colors->getWidth() * $this->colors->getHeight()); //list count, but we handle it as a 2D array... thanks for the confusion mojang + VarInt::writeUnsignedInt($out, $this->colors->getWidth() * $this->colors->getHeight()); //list count, but we handle it as a 2D array... thanks for the confusion mojang $this->colors->encode($out); } diff --git a/src/CodeBuilderPacket.php b/src/CodeBuilderPacket.php index 07521ff2..8dcf68cc 100644 --- a/src/CodeBuilderPacket.php +++ b/src/CodeBuilderPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class CodeBuilderPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::CODE_BUILDER_PACKET; @@ -40,14 +42,14 @@ public function openCodeBuilder() : bool{ return $this->openCodeBuilder; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->url = $in->getString(); - $this->openCodeBuilder = $in->getBool(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->url = CommonTypes::getString($in); + $this->openCodeBuilder = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->url); - $out->putBool($this->openCodeBuilder); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->url); + CommonTypes::putBool($out, $this->openCodeBuilder); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/CodeBuilderSourcePacket.php b/src/CodeBuilderSourcePacket.php index 3d408b26..c2f0a760 100644 --- a/src/CodeBuilderSourcePacket.php +++ b/src/CodeBuilderSourcePacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; class CodeBuilderSourcePacket extends DataPacket implements ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::CODE_BUILDER_SOURCE_PACKET; @@ -40,16 +42,16 @@ public function getCategory() : int{ return $this->category; } public function getCodeStatus() : int{ return $this->codeStatus; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->operation = $in->getByte(); - $this->category = $in->getByte(); - $this->codeStatus = $in->getByte(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->operation = Byte::readUnsigned($in); + $this->category = Byte::readUnsigned($in); + $this->codeStatus = Byte::readUnsigned($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->operation); - $out->putByte($this->category); - $out->putByte($this->codeStatus); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->operation); + Byte::writeUnsigned($out, $this->category); + Byte::writeUnsigned($out, $this->codeStatus); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/CommandBlockUpdatePacket.php b/src/CommandBlockUpdatePacket.php index 9d8e5496..3d05a136 100644 --- a/src/CommandBlockUpdatePacket.php +++ b/src/CommandBlockUpdatePacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; class CommandBlockUpdatePacket extends DataPacket implements ServerboundPacket{ @@ -37,47 +41,47 @@ class CommandBlockUpdatePacket extends DataPacket implements ServerboundPacket{ public int $tickDelay; public bool $executeOnFirstTick; - protected function decodePayload(PacketSerializer $in) : void{ - $this->isBlock = $in->getBool(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->isBlock = CommonTypes::getBool($in); if($this->isBlock){ - $this->blockPosition = $in->getBlockPosition(); - $this->commandBlockMode = $in->getUnsignedVarInt(); - $this->isRedstoneMode = $in->getBool(); - $this->isConditional = $in->getBool(); + $this->blockPosition = CommonTypes::getBlockPosition($in); + $this->commandBlockMode = VarInt::readUnsignedInt($in); + $this->isRedstoneMode = CommonTypes::getBool($in); + $this->isConditional = CommonTypes::getBool($in); }else{ //Minecart with command block - $this->minecartActorRuntimeId = $in->getActorRuntimeId(); + $this->minecartActorRuntimeId = CommonTypes::getActorRuntimeId($in); } - $this->command = $in->getString(); - $this->lastOutput = $in->getString(); - $this->name = $in->getString(); - $this->filteredName = $in->getString(); - $this->shouldTrackOutput = $in->getBool(); - $this->tickDelay = $in->getLInt(); - $this->executeOnFirstTick = $in->getBool(); + $this->command = CommonTypes::getString($in); + $this->lastOutput = CommonTypes::getString($in); + $this->name = CommonTypes::getString($in); + $this->filteredName = CommonTypes::getString($in); + $this->shouldTrackOutput = CommonTypes::getBool($in); + $this->tickDelay = LE::readUnsignedInt($in); + $this->executeOnFirstTick = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putBool($this->isBlock); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putBool($out, $this->isBlock); if($this->isBlock){ - $out->putBlockPosition($this->blockPosition); - $out->putUnsignedVarInt($this->commandBlockMode); - $out->putBool($this->isRedstoneMode); - $out->putBool($this->isConditional); + CommonTypes::putBlockPosition($out, $this->blockPosition); + VarInt::writeUnsignedInt($out, $this->commandBlockMode); + CommonTypes::putBool($out, $this->isRedstoneMode); + CommonTypes::putBool($out, $this->isConditional); }else{ - $out->putActorRuntimeId($this->minecartActorRuntimeId); + CommonTypes::putActorRuntimeId($out, $this->minecartActorRuntimeId); } - $out->putString($this->command); - $out->putString($this->lastOutput); - $out->putString($this->name); - $out->putString($this->filteredName); - $out->putBool($this->shouldTrackOutput); - $out->putLInt($this->tickDelay); - $out->putBool($this->executeOnFirstTick); + CommonTypes::putString($out, $this->command); + CommonTypes::putString($out, $this->lastOutput); + CommonTypes::putString($out, $this->name); + CommonTypes::putString($out, $this->filteredName); + CommonTypes::putBool($out, $this->shouldTrackOutput); + LE::writeUnsignedInt($out, $this->tickDelay); + CommonTypes::putBool($out, $this->executeOnFirstTick); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/CommandOutputPacket.php b/src/CommandOutputPacket.php index f2994b5d..144cfe08 100644 --- a/src/CommandOutputPacket.php +++ b/src/CommandOutputPacket.php @@ -14,10 +14,14 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\DataDecodeException; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\command\CommandOriginData; use pocketmine\network\mcpe\protocol\types\command\CommandOutputMessage; -use pocketmine\utils\BinaryDataException; use function count; class CommandOutputPacket extends DataPacket implements ClientboundPacket{ @@ -35,58 +39,58 @@ class CommandOutputPacket extends DataPacket implements ClientboundPacket{ public array $messages = []; public string $unknownString; - protected function decodePayload(PacketSerializer $in) : void{ - $this->originData = $in->getCommandOriginData(); - $this->outputType = $in->getByte(); - $this->successCount = $in->getUnsignedVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->originData = CommonTypes::getCommandOriginData($in); + $this->outputType = Byte::readUnsigned($in); + $this->successCount = VarInt::readUnsignedInt($in); - for($i = 0, $size = $in->getUnsignedVarInt(); $i < $size; ++$i){ + for($i = 0, $size = VarInt::readUnsignedInt($in); $i < $size; ++$i){ $this->messages[] = $this->getCommandMessage($in); } if($this->outputType === self::TYPE_DATA_SET){ - $this->unknownString = $in->getString(); + $this->unknownString = CommonTypes::getString($in); } } /** - * @throws BinaryDataException + * @throws DataDecodeException */ - protected function getCommandMessage(PacketSerializer $in) : CommandOutputMessage{ + protected function getCommandMessage(ByteBufferReader $in) : CommandOutputMessage{ $message = new CommandOutputMessage(); - $message->isInternal = $in->getBool(); - $message->messageId = $in->getString(); + $message->isInternal = CommonTypes::getBool($in); + $message->messageId = CommonTypes::getString($in); - for($i = 0, $size = $in->getUnsignedVarInt(); $i < $size; ++$i){ - $message->parameters[] = $in->getString(); + for($i = 0, $size = VarInt::readUnsignedInt($in); $i < $size; ++$i){ + $message->parameters[] = CommonTypes::getString($in); } return $message; } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putCommandOriginData($this->originData); - $out->putByte($this->outputType); - $out->putUnsignedVarInt($this->successCount); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putCommandOriginData($out, $this->originData); + Byte::writeUnsigned($out, $this->outputType); + VarInt::writeUnsignedInt($out, $this->successCount); - $out->putUnsignedVarInt(count($this->messages)); + VarInt::writeUnsignedInt($out, count($this->messages)); foreach($this->messages as $message){ $this->putCommandMessage($message, $out); } if($this->outputType === self::TYPE_DATA_SET){ - $out->putString($this->unknownString); + CommonTypes::putString($out, $this->unknownString); } } - protected function putCommandMessage(CommandOutputMessage $message, PacketSerializer $out) : void{ - $out->putBool($message->isInternal); - $out->putString($message->messageId); + protected function putCommandMessage(CommandOutputMessage $message, ByteBufferWriter $out) : void{ + CommonTypes::putBool($out, $message->isInternal); + CommonTypes::putString($out, $message->messageId); - $out->putUnsignedVarInt(count($message->parameters)); + VarInt::writeUnsignedInt($out, count($message->parameters)); foreach($message->parameters as $parameter){ - $out->putString($parameter); + CommonTypes::putString($out, $parameter); } } diff --git a/src/CommandRequestPacket.php b/src/CommandRequestPacket.php index a68f5861..d679959d 100644 --- a/src/CommandRequestPacket.php +++ b/src/CommandRequestPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\command\CommandOriginData; class CommandRequestPacket extends DataPacket implements ServerboundPacket{ @@ -37,18 +40,18 @@ public static function create(string $command, CommandOriginData $originData, bo return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->command = $in->getString(); - $this->originData = $in->getCommandOriginData(); - $this->isInternal = $in->getBool(); - $this->version = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->command = CommonTypes::getString($in); + $this->originData = CommonTypes::getCommandOriginData($in); + $this->isInternal = CommonTypes::getBool($in); + $this->version = VarInt::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->command); - $out->putCommandOriginData($this->originData); - $out->putBool($this->isInternal); - $out->putVarInt($this->version); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->command); + CommonTypes::putCommandOriginData($out, $this->originData); + CommonTypes::putBool($out, $this->isInternal); + VarInt::writeSignedInt($out, $this->version); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/CompletedUsingItemPacket.php b/src/CompletedUsingItemPacket.php index dd7220a6..36929c16 100644 --- a/src/CompletedUsingItemPacket.php +++ b/src/CompletedUsingItemPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; class CompletedUsingItemPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::COMPLETED_USING_ITEM_PACKET; @@ -50,14 +52,14 @@ public static function create(int $itemId, int $action) : self{ return $result; } - public function decodePayload(PacketSerializer $in) : void{ - $this->itemId = $in->getShort(); - $this->action = $in->getLInt(); + public function decodePayload(ByteBufferReader $in) : void{ + $this->itemId = LE::readSignedShort($in); + $this->action = LE::readSignedInt($in); } - public function encodePayload(PacketSerializer $out) : void{ - $out->putShort($this->itemId); - $out->putLInt($this->action); + public function encodePayload(ByteBufferWriter $out) : void{ + LE::writeSignedShort($out, $this->itemId); + LE::writeSignedInt($out, $this->action); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ContainerClosePacket.php b/src/ContainerClosePacket.php index 36fdd1a5..1a3a9cda 100644 --- a/src/ContainerClosePacket.php +++ b/src/ContainerClosePacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class ContainerClosePacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::CONTAINER_CLOSE_PACKET; @@ -34,16 +37,16 @@ public static function create(int $windowId, int $windowType, bool $server) : se return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->windowId = $in->getByte(); - $this->windowType = $in->getByte(); - $this->server = $in->getBool(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->windowId = Byte::readUnsigned($in); + $this->windowType = Byte::readUnsigned($in); + $this->server = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->windowId); - $out->putByte($this->windowType); - $out->putBool($this->server); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->windowId); + Byte::writeUnsigned($out, $this->windowType); + CommonTypes::putBool($out, $this->server); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ContainerOpenPacket.php b/src/ContainerOpenPacket.php index a2d2e9d6..233ec129 100644 --- a/src/ContainerOpenPacket.php +++ b/src/ContainerOpenPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; class ContainerOpenPacket extends DataPacket implements ClientboundPacket{ @@ -45,18 +48,18 @@ public static function entityInv(int $windowId, int $windowType, int $actorUniqu return self::create($windowId, $windowType, new BlockPosition(0, 0, 0), $actorUniqueId); } - protected function decodePayload(PacketSerializer $in) : void{ - $this->windowId = $in->getByte(); - $this->windowType = $in->getByte(); - $this->blockPosition = $in->getBlockPosition(); - $this->actorUniqueId = $in->getActorUniqueId(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->windowId = Byte::readUnsigned($in); + $this->windowType = Byte::readUnsigned($in); + $this->blockPosition = CommonTypes::getBlockPosition($in); + $this->actorUniqueId = CommonTypes::getActorUniqueId($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->windowId); - $out->putByte($this->windowType); - $out->putBlockPosition($this->blockPosition); - $out->putActorUniqueId($this->actorUniqueId); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->windowId); + Byte::writeUnsigned($out, $this->windowType); + CommonTypes::putBlockPosition($out, $this->blockPosition); + CommonTypes::putActorUniqueId($out, $this->actorUniqueId); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ContainerRegistryCleanupPacket.php b/src/ContainerRegistryCleanupPacket.php index 0d7ca58d..620333fe 100644 --- a/src/ContainerRegistryCleanupPacket.php +++ b/src/ContainerRegistryCleanupPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\types\inventory\FullContainerName; use function count; @@ -39,15 +41,15 @@ public static function create(array $removedContainers) : self{ */ public function getRemovedContainers() : array{ return $this->removedContainers; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ $this->removedContainers = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ $this->removedContainers[] = FullContainerName::read($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->removedContainers)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->removedContainers)); foreach($this->removedContainers as $container){ $container->write($out); } diff --git a/src/ContainerSetDataPacket.php b/src/ContainerSetDataPacket.php index d340cd02..62a2a31d 100644 --- a/src/ContainerSetDataPacket.php +++ b/src/ContainerSetDataPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; class ContainerSetDataPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::CONTAINER_SET_DATA_PACKET; @@ -44,16 +47,16 @@ public static function create(int $windowId, int $property, int $value) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->windowId = $in->getByte(); - $this->property = $in->getVarInt(); - $this->value = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->windowId = Byte::readUnsigned($in); + $this->property = VarInt::readSignedInt($in); + $this->value = VarInt::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->windowId); - $out->putVarInt($this->property); - $out->putVarInt($this->value); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->windowId); + VarInt::writeSignedInt($out, $this->property); + VarInt::writeSignedInt($out, $this->value); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/CorrectPlayerMovePredictionPacket.php b/src/CorrectPlayerMovePredictionPacket.php index 74562ba7..2a28d745 100644 --- a/src/CorrectPlayerMovePredictionPacket.php +++ b/src/CorrectPlayerMovePredictionPacket.php @@ -14,9 +14,14 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; use pocketmine\math\Vector2; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class CorrectPlayerMovePredictionPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::CORRECT_PLAYER_MOVE_PREDICTION_PACKET; @@ -69,25 +74,25 @@ public function getVehicleRotation() : Vector2{ return $this->vehicleRotation; } public function getVehicleAngularVelocity() : ?float{ return $this->vehicleAngularVelocity; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->predictionType = $in->getByte(); - $this->position = $in->getVector3(); - $this->delta = $in->getVector3(); - $this->vehicleRotation = new Vector2($in->getFloat(), $in->getFloat()); - $this->vehicleAngularVelocity = $in->readOptional($in->getFloat(...)); - $this->onGround = $in->getBool(); - $this->tick = $in->getUnsignedVarLong(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->predictionType = Byte::readUnsigned($in); + $this->position = CommonTypes::getVector3($in); + $this->delta = CommonTypes::getVector3($in); + $this->vehicleRotation = new Vector2(LE::readFloat($in), LE::readFloat($in)); + $this->vehicleAngularVelocity = CommonTypes::readOptional($in, LE::readFloat(...)); + $this->onGround = CommonTypes::getBool($in); + $this->tick = VarInt::readUnsignedLong($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->predictionType); - $out->putVector3($this->position); - $out->putVector3($this->delta); - $out->putFloat($this->vehicleRotation->getX()); - $out->putFloat($this->vehicleRotation->getY()); - $out->writeOptional($this->vehicleAngularVelocity, $out->putFloat(...)); - $out->putBool($this->onGround); - $out->putUnsignedVarLong($this->tick); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->predictionType); + CommonTypes::putVector3($out, $this->position); + CommonTypes::putVector3($out, $this->delta); + LE::writeFloat($out, $this->vehicleRotation->getX()); + LE::writeFloat($out, $this->vehicleRotation->getY()); + CommonTypes::writeOptional($out, $this->vehicleAngularVelocity, LE::writeFloat(...)); + CommonTypes::putBool($out, $this->onGround); + VarInt::writeUnsignedLong($out, $this->tick); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/CraftingDataPacket.php b/src/CraftingDataPacket.php index 93d812e8..e3d9aea1 100644 --- a/src/CraftingDataPacket.php +++ b/src/CraftingDataPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\recipe\FurnaceRecipe; use pocketmine\network\mcpe\protocol\types\recipe\MaterialReducerRecipe; use pocketmine\network\mcpe\protocol\types\recipe\MaterialReducerRecipeOutput; @@ -69,11 +72,11 @@ public static function create(array $recipesWithTypeIds, array $potionTypeRecipe return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $recipeCount = $in->getUnsignedVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $recipeCount = VarInt::readUnsignedInt($in); $previousType = "none"; for($i = 0; $i < $recipeCount; ++$i){ - $recipeType = $in->getVarInt(); + $recipeType = VarInt::readSignedInt($in); $this->recipesWithTypeIds[] = match($recipeType){ self::ENTRY_SHAPELESS, self::ENTRY_USER_DATA_SHAPELESS, self::ENTRY_SHAPELESS_CHEMISTRY => ShapelessRecipe::decode($recipeType, $in), @@ -86,66 +89,66 @@ protected function decodePayload(PacketSerializer $in) : void{ }; $previousType = $recipeType; } - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ - $inputId = $in->getVarInt(); - $inputMeta = $in->getVarInt(); - $ingredientId = $in->getVarInt(); - $ingredientMeta = $in->getVarInt(); - $outputId = $in->getVarInt(); - $outputMeta = $in->getVarInt(); + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ + $inputId = VarInt::readSignedInt($in); + $inputMeta = VarInt::readSignedInt($in); + $ingredientId = VarInt::readSignedInt($in); + $ingredientMeta = VarInt::readSignedInt($in); + $outputId = VarInt::readSignedInt($in); + $outputMeta = VarInt::readSignedInt($in); $this->potionTypeRecipes[] = new PotionTypeRecipe($inputId, $inputMeta, $ingredientId, $ingredientMeta, $outputId, $outputMeta); } - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ - $input = $in->getVarInt(); - $ingredient = $in->getVarInt(); - $output = $in->getVarInt(); + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ + $input = VarInt::readSignedInt($in); + $ingredient = VarInt::readSignedInt($in); + $output = VarInt::readSignedInt($in); $this->potionContainerRecipes[] = new PotionContainerChangeRecipe($input, $ingredient, $output); } - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ - $inputIdAndData = $in->getVarInt(); + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ + $inputIdAndData = VarInt::readSignedInt($in); [$inputId, $inputMeta] = [$inputIdAndData >> 16, $inputIdAndData & 0x7fff]; $outputs = []; - for($j = 0, $outputCount = $in->getUnsignedVarInt(); $j < $outputCount; ++$j){ - $outputItemId = $in->getVarInt(); - $outputItemCount = $in->getVarInt(); + for($j = 0, $outputCount = VarInt::readUnsignedInt($in); $j < $outputCount; ++$j){ + $outputItemId = VarInt::readSignedInt($in); + $outputItemCount = VarInt::readSignedInt($in); $outputs[] = new MaterialReducerRecipeOutput($outputItemId, $outputItemCount); } $this->materialReducerRecipes[] = new MaterialReducerRecipe($inputId, $inputMeta, $outputs); } - $this->cleanRecipes = $in->getBool(); + $this->cleanRecipes = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->recipesWithTypeIds)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->recipesWithTypeIds)); foreach($this->recipesWithTypeIds as $d){ - $out->putVarInt($d->getTypeId()); + VarInt::writeSignedInt($out, $d->getTypeId()); $d->encode($out); } - $out->putUnsignedVarInt(count($this->potionTypeRecipes)); + VarInt::writeUnsignedInt($out, count($this->potionTypeRecipes)); foreach($this->potionTypeRecipes as $recipe){ - $out->putVarInt($recipe->getInputItemId()); - $out->putVarInt($recipe->getInputItemMeta()); - $out->putVarInt($recipe->getIngredientItemId()); - $out->putVarInt($recipe->getIngredientItemMeta()); - $out->putVarInt($recipe->getOutputItemId()); - $out->putVarInt($recipe->getOutputItemMeta()); + VarInt::writeSignedInt($out, $recipe->getInputItemId()); + VarInt::writeSignedInt($out, $recipe->getInputItemMeta()); + VarInt::writeSignedInt($out, $recipe->getIngredientItemId()); + VarInt::writeSignedInt($out, $recipe->getIngredientItemMeta()); + VarInt::writeSignedInt($out, $recipe->getOutputItemId()); + VarInt::writeSignedInt($out, $recipe->getOutputItemMeta()); } - $out->putUnsignedVarInt(count($this->potionContainerRecipes)); + VarInt::writeUnsignedInt($out, count($this->potionContainerRecipes)); foreach($this->potionContainerRecipes as $recipe){ - $out->putVarInt($recipe->getInputItemId()); - $out->putVarInt($recipe->getIngredientItemId()); - $out->putVarInt($recipe->getOutputItemId()); + VarInt::writeSignedInt($out, $recipe->getInputItemId()); + VarInt::writeSignedInt($out, $recipe->getIngredientItemId()); + VarInt::writeSignedInt($out, $recipe->getOutputItemId()); } - $out->putUnsignedVarInt(count($this->materialReducerRecipes)); + VarInt::writeUnsignedInt($out, count($this->materialReducerRecipes)); foreach($this->materialReducerRecipes as $recipe){ - $out->putVarInt(($recipe->getInputItemId() << 16) | $recipe->getInputItemMeta()); - $out->putUnsignedVarInt(count($recipe->getOutputs())); + VarInt::writeSignedInt($out, ($recipe->getInputItemId() << 16) | $recipe->getInputItemMeta()); + VarInt::writeUnsignedInt($out, count($recipe->getOutputs())); foreach($recipe->getOutputs() as $output){ - $out->putVarInt($output->getItemId()); - $out->putVarInt($output->getCount()); + VarInt::writeSignedInt($out, $output->getItemId()); + VarInt::writeSignedInt($out, $output->getCount()); } } - $out->putBool($this->cleanRecipes); + CommonTypes::putBool($out, $this->cleanRecipes); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/CreatePhotoPacket.php b/src/CreatePhotoPacket.php index 703adf14..712c05e7 100644 --- a/src/CreatePhotoPacket.php +++ b/src/CreatePhotoPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class CreatePhotoPacket extends DataPacket implements ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::CREATE_PHOTO_PACKET; @@ -40,16 +43,16 @@ public function getPhotoName() : string{ return $this->photoName; } public function getPhotoItemName() : string{ return $this->photoItemName; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorUniqueId = $in->getLLong(); //why be consistent mojang ????? - $this->photoName = $in->getString(); - $this->photoItemName = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorUniqueId = LE::readSignedLong($in); //why be consistent mojang ????? + $this->photoName = CommonTypes::getString($in); + $this->photoItemName = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLLong($this->actorUniqueId); - $out->putString($this->photoName); - $out->putString($this->photoItemName); + protected function encodePayload(ByteBufferWriter $out) : void{ + LE::writeSignedLong($out, $this->actorUniqueId); + CommonTypes::putString($out, $this->photoName); + CommonTypes::putString($out, $this->photoItemName); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/CreativeContentPacket.php b/src/CreativeContentPacket.php index ba44b6c5..7e2d6c04 100644 --- a/src/CreativeContentPacket.php +++ b/src/CreativeContentPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\types\inventory\CreativeGroupEntry; use pocketmine\network\mcpe\protocol\types\inventory\CreativeItemEntry; use function count; @@ -50,25 +52,25 @@ public function getGroups() : array{ return $this->groups; } /** @return CreativeItemEntry[] */ public function getItems() : array{ return $this->items; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ $this->groups = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ $this->groups[] = CreativeGroupEntry::read($in); } $this->items = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ $this->items[] = CreativeItemEntry::read($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->groups)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->groups)); foreach($this->groups as $entry){ $entry->write($out); } - $out->putUnsignedVarInt(count($this->items)); + VarInt::writeUnsignedInt($out, count($this->items)); foreach($this->items as $entry){ $entry->write($out); } diff --git a/src/CurrentStructureFeaturePacket.php b/src/CurrentStructureFeaturePacket.php index 8558408d..6e11202b 100644 --- a/src/CurrentStructureFeaturePacket.php +++ b/src/CurrentStructureFeaturePacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class CurrentStructureFeaturePacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::CURRENT_STRUCTURE_FEATURE_PACKET; @@ -30,12 +32,12 @@ public static function create(string $currentStructureFeature) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->currentStructureFeature = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->currentStructureFeature = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->currentStructureFeature); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->currentStructureFeature); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/DataPacket.php b/src/DataPacket.php index e1934316..ec2e3085 100644 --- a/src/DataPacket.php +++ b/src/DataPacket.php @@ -14,8 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; -use pocketmine\utils\BinaryDataException; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\DataDecodeException; +use pmmp\encoding\VarInt; use function get_class; abstract class DataPacket implements Packet{ @@ -46,21 +48,21 @@ public function canBeSentBeforeLogin() : bool{ /** * @throws PacketDecodeException */ - final public function decode(PacketSerializer $in) : void{ + final public function decode(ByteBufferReader $in) : void{ try{ $this->decodeHeader($in); $this->decodePayload($in); - }catch(BinaryDataException | PacketDecodeException $e){ + }catch(DataDecodeException | PacketDecodeException $e){ throw PacketDecodeException::wrap($e, $this->getName()); } } /** - * @throws BinaryDataException + * @throws DataDecodeException * @throws PacketDecodeException */ - protected function decodeHeader(PacketSerializer $in) : void{ - $header = $in->getUnsignedVarInt(); + protected function decodeHeader(ByteBufferReader $in) : void{ + $header = VarInt::readUnsignedInt($in); $pid = $header & self::PID_MASK; if($pid !== static::NETWORK_ID){ //TODO: this means a logical error in the code, but how to prevent it from happening? @@ -75,17 +77,17 @@ protected function decodeHeader(PacketSerializer $in) : void{ * Decodes the packet body, without the packet ID or other generic header fields. * * @throws PacketDecodeException - * @throws BinaryDataException + * @throws DataDecodeException */ - abstract protected function decodePayload(PacketSerializer $in) : void; + abstract protected function decodePayload(ByteBufferReader $in) : void; - final public function encode(PacketSerializer $out) : void{ + final public function encode(ByteBufferWriter $out) : void{ $this->encodeHeader($out); $this->encodePayload($out); } - protected function encodeHeader(PacketSerializer $out) : void{ - $out->putUnsignedVarInt( + protected function encodeHeader(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, static::NETWORK_ID | ($this->senderSubId << self::SENDER_SUBCLIENT_ID_SHIFT) | ($this->recipientSubId << self::RECIPIENT_SUBCLIENT_ID_SHIFT) @@ -95,7 +97,7 @@ protected function encodeHeader(PacketSerializer $out) : void{ /** * Encodes the packet body, without the packet ID or other generic header fields. */ - abstract protected function encodePayload(PacketSerializer $out) : void; + abstract protected function encodePayload(ByteBufferWriter $out) : void; /** * @param string $name diff --git a/src/DeathInfoPacket.php b/src/DeathInfoPacket.php index 111586c5..e820bfcb 100644 --- a/src/DeathInfoPacket.php +++ b/src/DeathInfoPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use function count; /** @@ -43,21 +46,21 @@ public function getMessageTranslationKey() : string{ return $this->messageTransl /** @return string[] */ public function getMessageParameters() : array{ return $this->messageParameters; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->messageTranslationKey = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->messageTranslationKey = CommonTypes::getString($in); $this->messageParameters = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; $i++){ - $this->messageParameters[] = $in->getString(); + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; $i++){ + $this->messageParameters[] = CommonTypes::getString($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->messageTranslationKey); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->messageTranslationKey); - $out->putUnsignedVarInt(count($this->messageParameters)); + VarInt::writeUnsignedInt($out, count($this->messageParameters)); foreach($this->messageParameters as $parameter){ - $out->putString($parameter); + CommonTypes::putString($out, $parameter); } } diff --git a/src/DebugInfoPacket.php b/src/DebugInfoPacket.php index 2c43f504..1b0e1c1e 100644 --- a/src/DebugInfoPacket.php +++ b/src/DebugInfoPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class DebugInfoPacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::DEBUG_INFO_PACKET; @@ -36,14 +38,14 @@ public function getActorUniqueId() : int{ return $this->actorUniqueId; } public function getData() : string{ return $this->data; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorUniqueId = $in->getActorUniqueId(); - $this->data = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorUniqueId = CommonTypes::getActorUniqueId($in); + $this->data = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorUniqueId($this->actorUniqueId); - $out->putString($this->data); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorUniqueId($out, $this->actorUniqueId); + CommonTypes::putString($out, $this->data); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/DimensionDataPacket.php b/src/DimensionDataPacket.php index 60422a98..44ed0f36 100644 --- a/src/DimensionDataPacket.php +++ b/src/DimensionDataPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\DimensionData; use pocketmine\network\mcpe\protocol\types\DimensionNameIds; use function count; @@ -48,11 +51,11 @@ public static function create(array $definitions) : self{ */ public function getDefinitions() : array{ return $this->definitions; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ $this->definitions = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; $i++){ - $dimensionNameId = $in->getString(); + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; $i++){ + $dimensionNameId = CommonTypes::getString($in); $dimensionData = DimensionData::read($in); if(isset($this->definitions[$dimensionNameId])){ @@ -65,11 +68,11 @@ protected function decodePayload(PacketSerializer $in) : void{ } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->definitions)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->definitions)); foreach($this->definitions as $dimensionNameId => $definition){ - $out->putString((string) $dimensionNameId); //@phpstan-ignore-line + CommonTypes::putString($out, (string) $dimensionNameId); //@phpstan-ignore-line $definition->write($out); } } diff --git a/src/DisconnectPacket.php b/src/DisconnectPacket.php index c8724fc2..c37b4749 100644 --- a/src/DisconnectPacket.php +++ b/src/DisconnectPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class DisconnectPacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::DISCONNECT_PACKET; @@ -38,19 +41,19 @@ public function canBeSentBeforeLogin() : bool{ return true; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->reason = $in->getVarInt(); - $skipMessage = $in->getBool(); - $this->message = $skipMessage ? null : $in->getString(); - $this->filteredMessage = $skipMessage ? null : $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->reason = VarInt::readSignedInt($in); + $skipMessage = CommonTypes::getBool($in); + $this->message = $skipMessage ? null : CommonTypes::getString($in); + $this->filteredMessage = $skipMessage ? null : CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->reason); - $out->putBool($skipMessage = $this->message === null && $this->filteredMessage === null); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->reason); + CommonTypes::putBool($out, $skipMessage = $this->message === null && $this->filteredMessage === null); if(!$skipMessage){ - $out->putString($this->message ?? ""); - $out->putString($this->filteredMessage ?? ""); + CommonTypes::putString($out, $this->message ?? ""); + CommonTypes::putString($out, $this->filteredMessage ?? ""); } } diff --git a/src/EditorNetworkPacket.php b/src/EditorNetworkPacket.php index 17bd81d6..f1649066 100644 --- a/src/EditorNetworkPacket.php +++ b/src/EditorNetworkPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\CacheableNbt; /** @@ -43,14 +45,14 @@ public function getPayload() : CacheableNbt{ return $this->payload; } public function isRouteToManager() : bool{ return $this->isRouteToManager; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->isRouteToManager = $in->getBool(); - $this->payload = new CacheableNbt($in->getNbtCompoundRoot()); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->isRouteToManager = CommonTypes::getBool($in); + $this->payload = new CacheableNbt(CommonTypes::getNbtCompoundRoot($in)); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putBool($this->isRouteToManager); - $out->put($this->payload->getEncodedNbt()); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putBool($out, $this->isRouteToManager); + $out->writeByteArray($this->payload->getEncodedNbt()); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/EduUriResourcePacket.php b/src/EduUriResourcePacket.php index 5f1ae512..35d695bc 100644 --- a/src/EduUriResourcePacket.php +++ b/src/EduUriResourcePacket.php @@ -14,7 +14,8 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use pocketmine\network\mcpe\protocol\types\EducationUriResource; class EduUriResourcePacket extends DataPacket implements ClientboundPacket{ @@ -33,11 +34,11 @@ public static function create(EducationUriResource $resource) : self{ public function getResource() : EducationUriResource{ return $this->resource; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ $this->resource = EducationUriResource::read($in); } - protected function encodePayload(PacketSerializer $out) : void{ + protected function encodePayload(ByteBufferWriter $out) : void{ $this->resource->write($out); } diff --git a/src/EducationSettingsPacket.php b/src/EducationSettingsPacket.php index 717651b0..cadbc802 100644 --- a/src/EducationSettingsPacket.php +++ b/src/EducationSettingsPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\EducationSettingsAgentCapabilities; use pocketmine\network\mcpe\protocol\types\EducationSettingsExternalLinkSettings; @@ -91,30 +93,30 @@ public function getHasQuiz() : bool{ public function getLinkSettings() : ?EducationSettingsExternalLinkSettings{ return $this->linkSettings; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->codeBuilderDefaultUri = $in->getString(); - $this->codeBuilderTitle = $in->getString(); - $this->canResizeCodeBuilder = $in->getBool(); - $this->disableLegacyTitleBar = $in->getBool(); - $this->postProcessFilter = $in->getString(); - $this->screenshotBorderResourcePath = $in->getString(); - $this->agentCapabilities = $in->readOptional(fn() => EducationSettingsAgentCapabilities::read($in)); - $this->codeBuilderOverrideUri = $in->readOptional($in->getString(...)); - $this->hasQuiz = $in->getBool(); - $this->linkSettings = $in->readOptional(fn() => EducationSettingsExternalLinkSettings::read($in)); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->codeBuilderDefaultUri = CommonTypes::getString($in); + $this->codeBuilderTitle = CommonTypes::getString($in); + $this->canResizeCodeBuilder = CommonTypes::getBool($in); + $this->disableLegacyTitleBar = CommonTypes::getBool($in); + $this->postProcessFilter = CommonTypes::getString($in); + $this->screenshotBorderResourcePath = CommonTypes::getString($in); + $this->agentCapabilities = CommonTypes::readOptional($in, EducationSettingsAgentCapabilities::read(...)); + $this->codeBuilderOverrideUri = CommonTypes::readOptional($in, CommonTypes::getString(...)); + $this->hasQuiz = CommonTypes::getBool($in); + $this->linkSettings = CommonTypes::readOptional($in, EducationSettingsExternalLinkSettings::read(...)); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->codeBuilderDefaultUri); - $out->putString($this->codeBuilderTitle); - $out->putBool($this->canResizeCodeBuilder); - $out->putBool($this->disableLegacyTitleBar); - $out->putString($this->postProcessFilter); - $out->putString($this->screenshotBorderResourcePath); - $out->writeOptional($this->agentCapabilities, fn(EducationSettingsAgentCapabilities $v) => $v->write($out)); - $out->writeOptional($this->codeBuilderOverrideUri, $out->putString(...)); - $out->putBool($this->hasQuiz); - $out->writeOptional($this->linkSettings, fn(EducationSettingsExternalLinkSettings $v) => $v->write($out)); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->codeBuilderDefaultUri); + CommonTypes::putString($out, $this->codeBuilderTitle); + CommonTypes::putBool($out, $this->canResizeCodeBuilder); + CommonTypes::putBool($out, $this->disableLegacyTitleBar); + CommonTypes::putString($out, $this->postProcessFilter); + CommonTypes::putString($out, $this->screenshotBorderResourcePath); + CommonTypes::writeOptional($out, $this->agentCapabilities, fn(ByteBufferWriter $out, EducationSettingsAgentCapabilities $v) => $v->write($out)); + CommonTypes::writeOptional($out, $this->codeBuilderOverrideUri, CommonTypes::putString(...)); + CommonTypes::putBool($out, $this->hasQuiz); + CommonTypes::writeOptional($out, $this->linkSettings, fn(ByteBufferWriter $out, EducationSettingsExternalLinkSettings $v) => $v->write($out)); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/EmoteListPacket.php b/src/EmoteListPacket.php index 3c7e91f3..6b2d2c6e 100644 --- a/src/EmoteListPacket.php +++ b/src/EmoteListPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use Ramsey\Uuid\UuidInterface; use function count; @@ -41,19 +44,19 @@ public function getPlayerActorRuntimeId() : int{ return $this->playerActorRuntim /** @return UuidInterface[] */ public function getEmoteIds() : array{ return $this->emoteIds; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->playerActorRuntimeId = $in->getActorRuntimeId(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->playerActorRuntimeId = CommonTypes::getActorRuntimeId($in); $this->emoteIds = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ - $this->emoteIds[] = $in->getUUID(); + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ + $this->emoteIds[] = CommonTypes::getUUID($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorRuntimeId($this->playerActorRuntimeId); - $out->putUnsignedVarInt(count($this->emoteIds)); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->playerActorRuntimeId); + VarInt::writeUnsignedInt($out, count($this->emoteIds)); foreach($this->emoteIds as $emoteId){ - $out->putUUID($emoteId); + CommonTypes::putUUID($out, $emoteId); } } diff --git a/src/EmotePacket.php b/src/EmotePacket.php index 52b072be..46f3d933 100644 --- a/src/EmotePacket.php +++ b/src/EmotePacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class EmotePacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::EMOTE_PACKET; @@ -61,22 +65,22 @@ public function getFlags() : int{ return $this->flags; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorRuntimeId = $in->getActorRuntimeId(); - $this->emoteId = $in->getString(); - $this->emoteLengthTicks = $in->getUnsignedVarInt(); - $this->xboxUserId = $in->getString(); - $this->platformChatId = $in->getString(); - $this->flags = $in->getByte(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->emoteId = CommonTypes::getString($in); + $this->emoteLengthTicks = VarInt::readUnsignedInt($in); + $this->xboxUserId = CommonTypes::getString($in); + $this->platformChatId = CommonTypes::getString($in); + $this->flags = Byte::readUnsigned($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorRuntimeId($this->actorRuntimeId); - $out->putString($this->emoteId); - $out->putUnsignedVarInt($this->emoteLengthTicks); - $out->putString($this->xboxUserId); - $out->putString($this->platformChatId); - $out->putByte($this->flags); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + CommonTypes::putString($out, $this->emoteId); + VarInt::writeUnsignedInt($out, $this->emoteLengthTicks); + CommonTypes::putString($out, $this->xboxUserId); + CommonTypes::putString($out, $this->platformChatId); + Byte::writeUnsigned($out, $this->flags); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/FeatureRegistryPacket.php b/src/FeatureRegistryPacket.php index 39950929..c1c95971 100644 --- a/src/FeatureRegistryPacket.php +++ b/src/FeatureRegistryPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\types\FeatureRegistryPacketEntry; use function count; @@ -40,14 +42,14 @@ public static function create(array $entries) : self{ /** @return FeatureRegistryPacketEntry[] */ public function getEntries() : array{ return $this->entries; } - protected function decodePayload(PacketSerializer $in) : void{ - for($this->entries = [], $i = 0, $count = $in->getUnsignedVarInt(); $i < $count; $i++){ + protected function decodePayload(ByteBufferReader $in) : void{ + for($this->entries = [], $i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; $i++){ $this->entries[] = FeatureRegistryPacketEntry::read($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->entries)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->entries)); foreach($this->entries as $entry){ $entry->write($out); } diff --git a/src/GameRulesChangedPacket.php b/src/GameRulesChangedPacket.php index aee0fcf8..f671bcd8 100644 --- a/src/GameRulesChangedPacket.php +++ b/src/GameRulesChangedPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\GameRule; class GameRulesChangedPacket extends DataPacket implements ClientboundPacket{ @@ -37,12 +39,12 @@ public static function create(array $gameRules) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->gameRules = $in->getGameRules(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->gameRules = CommonTypes::getGameRules($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putGameRules($this->gameRules); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putGameRules($out, $this->gameRules); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/GameTestRequestPacket.php b/src/GameTestRequestPacket.php index 21df6bb8..07473bd4 100644 --- a/src/GameTestRequestPacket.php +++ b/src/GameTestRequestPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; class GameTestRequestPacket extends DataPacket implements ServerboundPacket{ @@ -73,24 +77,24 @@ public function getTestsPerRow() : int{ return $this->testsPerRow; } public function getTestName() : string{ return $this->testName; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->maxTestsPerBatch = $in->getVarInt(); - $this->repeatCount = $in->getVarInt(); - $this->rotation = $in->getByte(); - $this->stopOnFailure = $in->getBool(); - $this->testPosition = $in->getSignedBlockPosition(); - $this->testsPerRow = $in->getVarInt(); - $this->testName = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->maxTestsPerBatch = VarInt::readSignedInt($in); + $this->repeatCount = VarInt::readSignedInt($in); + $this->rotation = Byte::readUnsigned($in); + $this->stopOnFailure = CommonTypes::getBool($in); + $this->testPosition = CommonTypes::getSignedBlockPosition($in); + $this->testsPerRow = VarInt::readSignedInt($in); + $this->testName = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->maxTestsPerBatch); - $out->putVarInt($this->repeatCount); - $out->putByte($this->rotation); - $out->putBool($this->stopOnFailure); - $out->putSignedBlockPosition($this->testPosition); - $out->putVarInt($this->testsPerRow); - $out->putString($this->testName); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->maxTestsPerBatch); + VarInt::writeSignedInt($out, $this->repeatCount); + Byte::writeUnsigned($out, $this->rotation); + CommonTypes::putBool($out, $this->stopOnFailure); + CommonTypes::putSignedBlockPosition($out, $this->testPosition); + VarInt::writeSignedInt($out, $this->testsPerRow); + CommonTypes::putString($out, $this->testName); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/GameTestResultsPacket.php b/src/GameTestResultsPacket.php index c04db34f..1ed429e6 100644 --- a/src/GameTestResultsPacket.php +++ b/src/GameTestResultsPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class GameTestResultsPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::GAME_TEST_RESULTS_PACKET; @@ -40,16 +42,16 @@ public function getError() : string{ return $this->error; } public function getTestName() : string{ return $this->testName; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->success = $in->getBool(); - $this->error = $in->getString(); - $this->testName = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->success = CommonTypes::getBool($in); + $this->error = CommonTypes::getString($in); + $this->testName = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putBool($this->success); - $out->putString($this->error); - $out->putString($this->testName); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putBool($out, $this->success); + CommonTypes::putString($out, $this->error); + CommonTypes::putString($out, $this->testName); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/GuiDataPickItemPacket.php b/src/GuiDataPickItemPacket.php index 800b4a08..f79c7357 100644 --- a/src/GuiDataPickItemPacket.php +++ b/src/GuiDataPickItemPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class GuiDataPickItemPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::GUI_DATA_PICK_ITEM_PACKET; @@ -34,16 +37,16 @@ public static function create(string $itemDescription, string $itemEffects, int return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->itemDescription = $in->getString(); - $this->itemEffects = $in->getString(); - $this->hotbarSlot = $in->getLInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->itemDescription = CommonTypes::getString($in); + $this->itemEffects = CommonTypes::getString($in); + $this->hotbarSlot = LE::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->itemDescription); - $out->putString($this->itemEffects); - $out->putLInt($this->hotbarSlot); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->itemDescription); + CommonTypes::putString($out, $this->itemEffects); + LE::writeSignedInt($out, $this->hotbarSlot); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/HurtArmorPacket.php b/src/HurtArmorPacket.php index eb8e4f89..2e28252a 100644 --- a/src/HurtArmorPacket.php +++ b/src/HurtArmorPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; class HurtArmorPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::HURT_ARMOR_PACKET; @@ -34,16 +36,16 @@ public static function create(int $cause, int $health, int $armorSlotFlags) : se return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->cause = $in->getVarInt(); - $this->health = $in->getVarInt(); - $this->armorSlotFlags = $in->getUnsignedVarLong(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->cause = VarInt::readSignedInt($in); + $this->health = VarInt::readSignedInt($in); + $this->armorSlotFlags = VarInt::readUnsignedLong($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->cause); - $out->putVarInt($this->health); - $out->putUnsignedVarLong($this->armorSlotFlags); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->cause); + VarInt::writeSignedInt($out, $this->health); + VarInt::writeUnsignedLong($out, $this->armorSlotFlags); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/InteractPacket.php b/src/InteractPacket.php index 5bd71518..3b691c01 100644 --- a/src/InteractPacket.php +++ b/src/InteractPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class InteractPacket extends DataPacket implements ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::INTERACT_PACKET; @@ -30,26 +34,26 @@ class InteractPacket extends DataPacket implements ServerboundPacket{ public float $y; public float $z; - protected function decodePayload(PacketSerializer $in) : void{ - $this->action = $in->getByte(); - $this->targetActorRuntimeId = $in->getActorRuntimeId(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->action = Byte::readUnsigned($in); + $this->targetActorRuntimeId = CommonTypes::getActorRuntimeId($in); if($this->action === self::ACTION_MOUSEOVER || $this->action === self::ACTION_LEAVE_VEHICLE){ //TODO: should this be a vector3? - $this->x = $in->getLFloat(); - $this->y = $in->getLFloat(); - $this->z = $in->getLFloat(); + $this->x = LE::readFloat($in); + $this->y = LE::readFloat($in); + $this->z = LE::readFloat($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->action); - $out->putActorRuntimeId($this->targetActorRuntimeId); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->action); + CommonTypes::putActorRuntimeId($out, $this->targetActorRuntimeId); if($this->action === self::ACTION_MOUSEOVER || $this->action === self::ACTION_LEAVE_VEHICLE){ - $out->putLFloat($this->x); - $out->putLFloat($this->y); - $out->putLFloat($this->z); + LE::writeFloat($out, $this->x); + LE::writeFloat($out, $this->y); + LE::writeFloat($out, $this->z); } } diff --git a/src/InventoryContentPacket.php b/src/InventoryContentPacket.php index 6e42e260..53480125 100644 --- a/src/InventoryContentPacket.php +++ b/src/InventoryContentPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\inventory\FullContainerName; use pocketmine\network\mcpe\protocol\types\inventory\ItemStackWrapper; use function count; @@ -41,24 +44,24 @@ public static function create(int $windowId, array $items, FullContainerName $co return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->windowId = $in->getUnsignedVarInt(); - $count = $in->getUnsignedVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->windowId = VarInt::readUnsignedInt($in); + $count = VarInt::readUnsignedInt($in); for($i = 0; $i < $count; ++$i){ - $this->items[] = $in->getItemStackWrapper(); + $this->items[] = CommonTypes::getItemStackWrapper($in); } $this->containerName = FullContainerName::read($in); - $this->storage = $in->getItemStackWrapper(); + $this->storage = CommonTypes::getItemStackWrapper($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt($this->windowId); - $out->putUnsignedVarInt(count($this->items)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, $this->windowId); + VarInt::writeUnsignedInt($out, count($this->items)); foreach($this->items as $item){ - $out->putItemStackWrapper($item); + CommonTypes::putItemStackWrapper($out, $item); } $this->containerName->write($out); - $out->putItemStackWrapper($this->storage); + CommonTypes::putItemStackWrapper($out, $this->storage); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/InventorySlotPacket.php b/src/InventorySlotPacket.php index c75627fb..8acd7110 100644 --- a/src/InventorySlotPacket.php +++ b/src/InventorySlotPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\inventory\FullContainerName; use pocketmine\network\mcpe\protocol\types\inventory\ItemStackWrapper; @@ -40,20 +43,20 @@ public static function create(int $windowId, int $inventorySlot, FullContainerNa return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->windowId = $in->getUnsignedVarInt(); - $this->inventorySlot = $in->getUnsignedVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->windowId = VarInt::readUnsignedInt($in); + $this->inventorySlot = VarInt::readUnsignedInt($in); $this->containerName = FullContainerName::read($in); - $this->storage = $in->getItemStackWrapper(); - $this->item = $in->getItemStackWrapper(); + $this->storage = CommonTypes::getItemStackWrapper($in); + $this->item = CommonTypes::getItemStackWrapper($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt($this->windowId); - $out->putUnsignedVarInt($this->inventorySlot); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, $this->windowId); + VarInt::writeUnsignedInt($out, $this->inventorySlot); $this->containerName->write($out); - $out->putItemStackWrapper($this->storage); - $out->putItemStackWrapper($this->item); + CommonTypes::putItemStackWrapper($out, $this->storage); + CommonTypes::putItemStackWrapper($out, $this->item); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/InventoryTransactionPacket.php b/src/InventoryTransactionPacket.php index 22ab580f..54de31ea 100644 --- a/src/InventoryTransactionPacket.php +++ b/src/InventoryTransactionPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\inventory\InventoryTransactionChangedSlotsHack; use pocketmine\network\mcpe\protocol\types\inventory\MismatchTransactionData; use pocketmine\network\mcpe\protocol\types\inventory\NormalTransactionData; @@ -53,16 +56,16 @@ public static function create(int $requestId, array $requestChangedSlots, Transa return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->requestId = $in->readLegacyItemStackRequestId(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->requestId = CommonTypes::readLegacyItemStackRequestId($in); $this->requestChangedSlots = []; if($this->requestId !== 0){ - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ $this->requestChangedSlots[] = InventoryTransactionChangedSlotsHack::read($in); } } - $transactionType = $in->getUnsignedVarInt(); + $transactionType = VarInt::readUnsignedInt($in); $this->trData = match($transactionType){ NormalTransactionData::ID => new NormalTransactionData(), @@ -76,16 +79,16 @@ protected function decodePayload(PacketSerializer $in) : void{ $this->trData->decode($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->writeLegacyItemStackRequestId($this->requestId); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::writeLegacyItemStackRequestId($out, $this->requestId); if($this->requestId !== 0){ - $out->putUnsignedVarInt(count($this->requestChangedSlots)); + VarInt::writeUnsignedInt($out, count($this->requestChangedSlots)); foreach($this->requestChangedSlots as $changedSlots){ $changedSlots->write($out); } } - $out->putUnsignedVarInt($this->trData->getTypeId()); + VarInt::writeUnsignedInt($out, $this->trData->getTypeId()); $this->trData->encode($out); } diff --git a/src/ItemRegistryPacket.php b/src/ItemRegistryPacket.php index fb46f739..a91fc7d1 100644 --- a/src/ItemRegistryPacket.php +++ b/src/ItemRegistryPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\CacheableNbt; use pocketmine\network\mcpe\protocol\types\ItemTypeEntry; use function count; @@ -45,26 +49,26 @@ public static function create(array $entries) : self{ */ public function getEntries() : array{ return $this->entries; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ $this->entries = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ - $stringId = $in->getString(); - $numericId = $in->getSignedLShort(); - $isComponentBased = $in->getBool(); - $version = $in->getVarInt(); - $nbt = $in->getNbtCompoundRoot(); + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ + $stringId = CommonTypes::getString($in); + $numericId = LE::readSignedShort($in); + $isComponentBased = CommonTypes::getBool($in); + $version = VarInt::readSignedInt($in); + $nbt = CommonTypes::getNbtCompoundRoot($in); $this->entries[] = new ItemTypeEntry($stringId, $numericId, $isComponentBased, $version, new CacheableNbt($nbt)); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->entries)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->entries)); foreach($this->entries as $entry){ - $out->putString($entry->getStringId()); - $out->putLShort($entry->getNumericId()); - $out->putBool($entry->isComponentBased()); - $out->putVarInt($entry->getVersion()); - $out->put($entry->getComponentNbt()->getEncodedNbt()); + CommonTypes::putString($out, $entry->getStringId()); + LE::writeSignedShort($out, $entry->getNumericId()); + CommonTypes::putBool($out, $entry->isComponentBased()); + VarInt::writeSignedInt($out, $entry->getVersion()); + $out->writeByteArray($entry->getComponentNbt()->getEncodedNbt()); } } diff --git a/src/ItemStackRequestPacket.php b/src/ItemStackRequestPacket.php index bc26ddab..e9cf505b 100644 --- a/src/ItemStackRequestPacket.php +++ b/src/ItemStackRequestPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\types\inventory\stackrequest\ItemStackRequest; use function count; @@ -37,15 +39,15 @@ public static function create(array $requests) : self{ /** @return ItemStackRequest[] */ public function getRequests() : array{ return $this->requests; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ $this->requests = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ $this->requests[] = ItemStackRequest::read($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->requests)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->requests)); foreach($this->requests as $request){ $request->write($out); } diff --git a/src/ItemStackResponsePacket.php b/src/ItemStackResponsePacket.php index 72b17eff..5e44d7c4 100644 --- a/src/ItemStackResponsePacket.php +++ b/src/ItemStackResponsePacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\types\inventory\stackresponse\ItemStackResponse; use function count; @@ -37,15 +39,15 @@ public static function create(array $responses) : self{ /** @return ItemStackResponse[] */ public function getResponses() : array{ return $this->responses; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ $this->responses = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ $this->responses[] = ItemStackResponse::read($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->responses)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->responses)); foreach($this->responses as $response){ $response->write($out); } diff --git a/src/JigsawStructureDataPacket.php b/src/JigsawStructureDataPacket.php index c6841565..43e715c5 100644 --- a/src/JigsawStructureDataPacket.php +++ b/src/JigsawStructureDataPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\CacheableNbt; class JigsawStructureDataPacket extends DataPacket implements ClientboundPacket{ @@ -36,12 +38,12 @@ public static function create(CacheableNbt $nbt) : self{ /** @phpstan-return CacheableNbt<\pocketmine\nbt\tag\CompoundTag> */ public function getNbt() : CacheableNbt{ return $this->nbt; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->nbt = new CacheableNbt($in->getNbtCompoundRoot()); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->nbt = new CacheableNbt(CommonTypes::getNbtCompoundRoot($in)); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->put($this->nbt->getEncodedNbt()); + protected function encodePayload(ByteBufferWriter $out) : void{ + $out->writeByteArray($this->nbt->getEncodedNbt()); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/LabTablePacket.php b/src/LabTablePacket.php index 1a90d418..78c71931 100644 --- a/src/LabTablePacket.php +++ b/src/LabTablePacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; class LabTablePacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ @@ -39,16 +42,16 @@ public static function create(int $actionType, BlockPosition $blockPosition, int return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actionType = $in->getByte(); - $this->blockPosition = $in->getSignedBlockPosition(); - $this->reactionType = $in->getByte(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actionType = Byte::readUnsigned($in); + $this->blockPosition = CommonTypes::getSignedBlockPosition($in); + $this->reactionType = Byte::readUnsigned($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->actionType); - $out->putSignedBlockPosition($this->blockPosition); - $out->putByte($this->reactionType); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->actionType); + CommonTypes::putSignedBlockPosition($out, $this->blockPosition); + Byte::writeUnsigned($out, $this->reactionType); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/LecternUpdatePacket.php b/src/LecternUpdatePacket.php index 326f07fd..3b347ba2 100644 --- a/src/LecternUpdatePacket.php +++ b/src/LecternUpdatePacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; class LecternUpdatePacket extends DataPacket implements ServerboundPacket{ @@ -35,16 +38,16 @@ public static function create(int $page, int $totalPages, BlockPosition $blockPo return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->page = $in->getByte(); - $this->totalPages = $in->getByte(); - $this->blockPosition = $in->getBlockPosition(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->page = Byte::readUnsigned($in); + $this->totalPages = Byte::readUnsigned($in); + $this->blockPosition = CommonTypes::getBlockPosition($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->page); - $out->putByte($this->totalPages); - $out->putBlockPosition($this->blockPosition); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->page); + Byte::writeUnsigned($out, $this->totalPages); + CommonTypes::putBlockPosition($out, $this->blockPosition); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/LegacyTelemetryEventPacket.php b/src/LegacyTelemetryEventPacket.php index 9ef424f4..c9f386e4 100644 --- a/src/LegacyTelemetryEventPacket.php +++ b/src/LegacyTelemetryEventPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class LegacyTelemetryEventPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::LEGACY_TELEMETRY_EVENT_PACKET; @@ -49,18 +53,18 @@ class LegacyTelemetryEventPacket extends DataPacket implements ClientboundPacket public int $eventData; public int $type; - protected function decodePayload(PacketSerializer $in) : void{ - $this->playerRuntimeId = $in->getActorRuntimeId(); - $this->eventData = $in->getVarInt(); - $this->type = $in->getByte(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->playerRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->eventData = VarInt::readSignedInt($in); + $this->type = Byte::readUnsigned($in); //TODO: nice confusing mess } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorRuntimeId($this->playerRuntimeId); - $out->putVarInt($this->eventData); - $out->putByte($this->type); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->playerRuntimeId); + VarInt::writeSignedInt($out, $this->eventData); + Byte::writeUnsigned($out, $this->type); //TODO: also nice confusing mess } diff --git a/src/LessonProgressPacket.php b/src/LessonProgressPacket.php index c53e150d..8f080ac6 100644 --- a/src/LessonProgressPacket.php +++ b/src/LessonProgressPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; /** * Handled only in Education mode. Used to fire telemetry reporting on the client. @@ -47,16 +50,16 @@ public function getScore() : int{ return $this->score; } public function getActivityId() : string{ return $this->activityId; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->action = $in->getVarInt(); - $this->score = $in->getVarInt(); - $this->activityId = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->action = VarInt::readSignedInt($in); + $this->score = VarInt::readSignedInt($in); + $this->activityId = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->action); - $out->putVarInt($this->score); - $out->putString($this->activityId); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->action); + VarInt::writeSignedInt($out, $this->score); + CommonTypes::putString($out, $this->activityId); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/LevelChunkPacket.php b/src/LevelChunkPacket.php index d2824b41..ffa8ddc8 100644 --- a/src/LevelChunkPacket.php +++ b/src/LevelChunkPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\ChunkPosition; use pocketmine\network\mcpe\protocol\types\DimensionIds; use pocketmine\utils\Limits; @@ -89,59 +93,59 @@ public function getExtraPayload() : string{ return $this->extraPayload; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ $this->chunkPosition = ChunkPosition::read($in); - $this->dimensionId = $in->getVarInt(); + $this->dimensionId = VarInt::readSignedInt($in); - $subChunkCountButNotReally = $in->getUnsignedVarInt(); + $subChunkCountButNotReally = VarInt::readUnsignedInt($in); if($subChunkCountButNotReally === self::CLIENT_REQUEST_FULL_COLUMN_FAKE_COUNT){ $this->clientSubChunkRequestsEnabled = true; $this->subChunkCount = PHP_INT_MAX; }elseif($subChunkCountButNotReally === self::CLIENT_REQUEST_TRUNCATED_COLUMN_FAKE_COUNT){ $this->clientSubChunkRequestsEnabled = true; - $this->subChunkCount = $in->getLShort(); + $this->subChunkCount = LE::readUnsignedShort($in); }else{ $this->clientSubChunkRequestsEnabled = false; $this->subChunkCount = $subChunkCountButNotReally; } - $cacheEnabled = $in->getBool(); + $cacheEnabled = CommonTypes::getBool($in); if($cacheEnabled){ $this->usedBlobHashes = []; - $count = $in->getUnsignedVarInt(); + $count = VarInt::readUnsignedInt($in); if($count > self::MAX_BLOB_HASHES){ throw new PacketDecodeException("Expected at most " . self::MAX_BLOB_HASHES . " blob hashes, got " . $count); } for($i = 0; $i < $count; ++$i){ - $this->usedBlobHashes[] = $in->getLLong(); + $this->usedBlobHashes[] = LE::readUnsignedLong($in); } } - $this->extraPayload = $in->getString(); + $this->extraPayload = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ + protected function encodePayload(ByteBufferWriter $out) : void{ $this->chunkPosition->write($out); - $out->putVarInt($this->dimensionId); + VarInt::writeSignedInt($out, $this->dimensionId); if($this->clientSubChunkRequestsEnabled){ if($this->subChunkCount === PHP_INT_MAX){ - $out->putUnsignedVarInt(self::CLIENT_REQUEST_FULL_COLUMN_FAKE_COUNT); + VarInt::writeUnsignedInt($out, self::CLIENT_REQUEST_FULL_COLUMN_FAKE_COUNT); }else{ - $out->putUnsignedVarInt(self::CLIENT_REQUEST_TRUNCATED_COLUMN_FAKE_COUNT); - $out->putLShort($this->subChunkCount); + VarInt::writeUnsignedInt($out, self::CLIENT_REQUEST_TRUNCATED_COLUMN_FAKE_COUNT); + LE::writeUnsignedShort($out, $this->subChunkCount); } }else{ - $out->putUnsignedVarInt($this->subChunkCount); + VarInt::writeUnsignedInt($out, $this->subChunkCount); } - $out->putBool($this->usedBlobHashes !== null); + CommonTypes::putBool($out, $this->usedBlobHashes !== null); if($this->usedBlobHashes !== null){ - $out->putUnsignedVarInt(count($this->usedBlobHashes)); + VarInt::writeUnsignedInt($out, count($this->usedBlobHashes)); foreach($this->usedBlobHashes as $hash){ - $out->putLLong($hash); + LE::writeUnsignedLong($out, $hash); } } - $out->putString($this->extraPayload); + CommonTypes::putString($out, $this->extraPayload); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/LevelEventGenericPacket.php b/src/LevelEventGenericPacket.php index d1970c79..e8547da1 100644 --- a/src/LevelEventGenericPacket.php +++ b/src/LevelEventGenericPacket.php @@ -14,11 +14,13 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\nbt\NBT; use pocketmine\nbt\NbtDataException; use pocketmine\nbt\tag\Tag; use pocketmine\network\mcpe\protocol\serializer\NetworkNbtSerializer; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; class LevelEventGenericPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::LEVEL_EVENT_GENERIC_PACKET; @@ -44,20 +46,20 @@ public function getEventData() : Tag{ return $this->eventData; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->eventId = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->eventId = VarInt::readSignedInt($in); $offset = $in->getOffset(); try{ - $this->eventData = (new NetworkNbtSerializer())->readHeadless($in->getBuffer(), NBT::TAG_Compound, $offset); + $this->eventData = (new NetworkNbtSerializer())->readHeadless($in->getData(), NBT::TAG_Compound, $offset); }catch(NbtDataException $e){ throw PacketDecodeException::wrap($e); } $in->setOffset($offset); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->eventId); - $out->put((new NetworkNbtSerializer())->writeHeadless($this->eventData)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->eventId); + $out->writeByteArray((new NetworkNbtSerializer())->writeHeadless($this->eventData)); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/LevelEventPacket.php b/src/LevelEventPacket.php index d86d7ef1..98d0db8b 100644 --- a/src/LevelEventPacket.php +++ b/src/LevelEventPacket.php @@ -14,8 +14,11 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\LevelEvent; class LevelEventPacket extends DataPacket implements ClientboundPacket{ @@ -41,16 +44,16 @@ public static function standardParticle(int $particleId, int $data, Vector3 $pos return self::create(LevelEvent::ADD_PARTICLE_MASK | $particleId, $data, $position); } - protected function decodePayload(PacketSerializer $in) : void{ - $this->eventId = $in->getVarInt(); - $this->position = $in->getVector3(); - $this->eventData = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->eventId = VarInt::readSignedInt($in); + $this->position = CommonTypes::getVector3($in); + $this->eventData = VarInt::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->eventId); - $out->putVector3Nullable($this->position); - $out->putVarInt($this->eventData); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->eventId); + CommonTypes::putVector3Nullable($out, $this->position); + VarInt::writeSignedInt($out, $this->eventData); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/LevelSoundEventPacket.php b/src/LevelSoundEventPacket.php index d22a20a9..9683350c 100644 --- a/src/LevelSoundEventPacket.php +++ b/src/LevelSoundEventPacket.php @@ -14,8 +14,12 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\LevelSoundEvent; class LevelSoundEventPacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ @@ -57,24 +61,24 @@ public static function nonActorSound(int $sound, Vector3 $position, bool $disabl return self::create($sound, $position, $extraData, ":", false, $disableRelativeVolume, -1); } - protected function decodePayload(PacketSerializer $in) : void{ - $this->sound = $in->getUnsignedVarInt(); - $this->position = $in->getVector3(); - $this->extraData = $in->getVarInt(); - $this->entityType = $in->getString(); - $this->isBabyMob = $in->getBool(); - $this->disableRelativeVolume = $in->getBool(); - $this->actorUniqueId = $in->getLLong(); //WHY IS THIS NON-STANDARD? + protected function decodePayload(ByteBufferReader $in) : void{ + $this->sound = VarInt::readUnsignedInt($in); + $this->position = CommonTypes::getVector3($in); + $this->extraData = VarInt::readSignedInt($in); + $this->entityType = CommonTypes::getString($in); + $this->isBabyMob = CommonTypes::getBool($in); + $this->disableRelativeVolume = CommonTypes::getBool($in); + $this->actorUniqueId = LE::readSignedLong($in); //WHY IS THIS NON-STANDARD? } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt($this->sound); - $out->putVector3($this->position); - $out->putVarInt($this->extraData); - $out->putString($this->entityType); - $out->putBool($this->isBabyMob); - $out->putBool($this->disableRelativeVolume); - $out->putLLong($this->actorUniqueId); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, $this->sound); + CommonTypes::putVector3($out, $this->position); + VarInt::writeSignedInt($out, $this->extraData); + CommonTypes::putString($out, $this->entityType); + CommonTypes::putBool($out, $this->isBabyMob); + CommonTypes::putBool($out, $this->disableRelativeVolume); + LE::writeSignedLong($out, $this->actorUniqueId); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/LoginPacket.php b/src/LoginPacket.php index 788fc470..956a795a 100644 --- a/src/LoginPacket.php +++ b/src/LoginPacket.php @@ -14,8 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; -use pocketmine\utils\BinaryStream; +use pmmp\encoding\BE; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use function strlen; class LoginPacket extends DataPacket implements ServerboundPacket{ @@ -40,46 +43,36 @@ public function canBeSentBeforeLogin() : bool{ return true; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->protocol = $in->getInt(); - $this->decodeConnectionRequest($in->getString()); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->protocol = BE::readUnsignedInt($in); + $this->decodeConnectionRequest(CommonTypes::getString($in)); } protected function decodeConnectionRequest(string $binary) : void{ - $connRequestReader = new BinaryStream($binary); - - $authInfoJsonLength = $connRequestReader->getLInt(); - if($authInfoJsonLength <= 0){ - //technically this is always positive; the problem results because getLInt() is implicitly signed - //this is inconsistent with many other methods, but we can't do anything about that for now - throw new PacketDecodeException("Length of auth info JSON must be positive"); - } - $this->authInfoJson = $connRequestReader->get($authInfoJsonLength); - - $clientDataJwtLength = $connRequestReader->getLInt(); - if($clientDataJwtLength <= 0){ - //technically this is always positive; the problem results because getLInt() is implicitly signed - //this is inconsistent with many other methods, but we can't do anything about that for now - throw new PacketDecodeException("Length of clientData JWT must be positive"); - } - $this->clientDataJwt = $connRequestReader->get($clientDataJwtLength); + $connRequestReader = new ByteBufferReader($binary); + + $authInfoJsonLength = LE::readUnsignedInt($connRequestReader); + $this->authInfoJson = $connRequestReader->readByteArray($authInfoJsonLength); + + $clientDataJwtLength = LE::readUnsignedInt($connRequestReader); + $this->clientDataJwt = $connRequestReader->readByteArray($clientDataJwtLength); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putInt($this->protocol); - $out->putString($this->encodeConnectionRequest()); + protected function encodePayload(ByteBufferWriter $out) : void{ + BE::writeUnsignedInt($out, $this->protocol); + CommonTypes::putString($out, $this->encodeConnectionRequest()); } protected function encodeConnectionRequest() : string{ - $connRequestWriter = new BinaryStream(); + $connRequestWriter = new ByteBufferWriter(); - $connRequestWriter->putLInt(strlen($this->authInfoJson)); - $connRequestWriter->put($this->authInfoJson); + LE::writeUnsignedInt($connRequestWriter, strlen($this->authInfoJson)); + $connRequestWriter->writeByteArray($this->authInfoJson); - $connRequestWriter->putLInt(strlen($this->clientDataJwt)); - $connRequestWriter->put($this->clientDataJwt); + LE::writeUnsignedInt($connRequestWriter, strlen($this->clientDataJwt)); + $connRequestWriter->writeByteArray($this->clientDataJwt); - return $connRequestWriter->getBuffer(); + return $connRequestWriter->getData(); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/MapCreateLockedCopyPacket.php b/src/MapCreateLockedCopyPacket.php index 1be06e68..fadedcae 100644 --- a/src/MapCreateLockedCopyPacket.php +++ b/src/MapCreateLockedCopyPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class MapCreateLockedCopyPacket extends DataPacket implements ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::MAP_CREATE_LOCKED_COPY_PACKET; @@ -32,14 +34,14 @@ public static function create(int $originalMapId, int $newMapId) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->originalMapId = $in->getActorUniqueId(); - $this->newMapId = $in->getActorUniqueId(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->originalMapId = CommonTypes::getActorUniqueId($in); + $this->newMapId = CommonTypes::getActorUniqueId($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorUniqueId($this->originalMapId); - $out->putActorUniqueId($this->newMapId); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorUniqueId($out, $this->originalMapId); + CommonTypes::putActorUniqueId($out, $this->newMapId); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/MapInfoRequestPacket.php b/src/MapInfoRequestPacket.php index 23ffb756..670260a5 100644 --- a/src/MapInfoRequestPacket.php +++ b/src/MapInfoRequestPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\MapImage; use pocketmine\network\mcpe\protocol\types\MapInfoRequestPacketClientPixel; use function count; @@ -37,11 +40,11 @@ public static function create(int $mapId, array $clientPixels) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->mapId = $in->getActorUniqueId(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->mapId = CommonTypes::getActorUniqueId($in); $this->clientPixels = []; - $count = $in->getLInt(); + $count = LE::readUnsignedInt($in); if($count > MapImage::MAX_HEIGHT * MapImage::MAX_WIDTH){ throw new PacketDecodeException("Too many pixels"); } @@ -50,10 +53,10 @@ protected function decodePayload(PacketSerializer $in) : void{ } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorUniqueId($this->mapId); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorUniqueId($out, $this->mapId); - $out->putLInt(count($this->clientPixels)); + LE::writeUnsignedInt($out, count($this->clientPixels)); foreach($this->clientPixels as $pixel){ $pixel->write($out); } diff --git a/src/MobArmorEquipmentPacket.php b/src/MobArmorEquipmentPacket.php index 947173a0..d6860243 100644 --- a/src/MobArmorEquipmentPacket.php +++ b/src/MobArmorEquipmentPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\inventory\ItemStackWrapper; class MobArmorEquipmentPacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ @@ -43,22 +45,22 @@ public static function create(int $actorRuntimeId, ItemStackWrapper $head, ItemS return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorRuntimeId = $in->getActorRuntimeId(); - $this->head = $in->getItemStackWrapper(); - $this->chest = $in->getItemStackWrapper(); - $this->legs = $in->getItemStackWrapper(); - $this->feet = $in->getItemStackWrapper(); - $this->body = $in->getItemStackWrapper(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->head = CommonTypes::getItemStackWrapper($in); + $this->chest = CommonTypes::getItemStackWrapper($in); + $this->legs = CommonTypes::getItemStackWrapper($in); + $this->feet = CommonTypes::getItemStackWrapper($in); + $this->body = CommonTypes::getItemStackWrapper($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorRuntimeId($this->actorRuntimeId); - $out->putItemStackWrapper($this->head); - $out->putItemStackWrapper($this->chest); - $out->putItemStackWrapper($this->legs); - $out->putItemStackWrapper($this->feet); - $out->putItemStackWrapper($this->body); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + CommonTypes::putItemStackWrapper($out, $this->head); + CommonTypes::putItemStackWrapper($out, $this->chest); + CommonTypes::putItemStackWrapper($out, $this->legs); + CommonTypes::putItemStackWrapper($out, $this->feet); + CommonTypes::putItemStackWrapper($out, $this->body); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/MobEffectPacket.php b/src/MobEffectPacket.php index b10485ef..39af6607 100644 --- a/src/MobEffectPacket.php +++ b/src/MobEffectPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class MobEffectPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::MOB_EFFECT_PACKET; @@ -62,24 +66,24 @@ public static function remove(int $actorRuntimeId, int $effectId, int $tick) : s return self::create($actorRuntimeId, self::EVENT_REMOVE, $effectId, 0, false, 0, $tick); } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorRuntimeId = $in->getActorRuntimeId(); - $this->eventId = $in->getByte(); - $this->effectId = $in->getVarInt(); - $this->amplifier = $in->getVarInt(); - $this->particles = $in->getBool(); - $this->duration = $in->getVarInt(); - $this->tick = $in->getUnsignedVarLong(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->eventId = Byte::readUnsigned($in); + $this->effectId = VarInt::readSignedInt($in); + $this->amplifier = VarInt::readSignedInt($in); + $this->particles = CommonTypes::getBool($in); + $this->duration = VarInt::readSignedInt($in); + $this->tick = VarInt::readUnsignedLong($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorRuntimeId($this->actorRuntimeId); - $out->putByte($this->eventId); - $out->putVarInt($this->effectId); - $out->putVarInt($this->amplifier); - $out->putBool($this->particles); - $out->putVarInt($this->duration); - $out->putUnsignedVarLong($this->tick); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + Byte::writeUnsigned($out, $this->eventId); + VarInt::writeSignedInt($out, $this->effectId); + VarInt::writeSignedInt($out, $this->amplifier); + CommonTypes::putBool($out, $this->particles); + VarInt::writeSignedInt($out, $this->duration); + VarInt::writeUnsignedLong($out, $this->tick); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/MobEquipmentPacket.php b/src/MobEquipmentPacket.php index feebdb57..a105262e 100644 --- a/src/MobEquipmentPacket.php +++ b/src/MobEquipmentPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\inventory\ItemStackWrapper; class MobEquipmentPacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ @@ -39,20 +42,20 @@ public static function create(int $actorRuntimeId, ItemStackWrapper $item, int $ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorRuntimeId = $in->getActorRuntimeId(); - $this->item = $in->getItemStackWrapper(); - $this->inventorySlot = $in->getByte(); - $this->hotbarSlot = $in->getByte(); - $this->windowId = $in->getByte(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->item = CommonTypes::getItemStackWrapper($in); + $this->inventorySlot = Byte::readUnsigned($in); + $this->hotbarSlot = Byte::readUnsigned($in); + $this->windowId = Byte::readUnsigned($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorRuntimeId($this->actorRuntimeId); - $out->putItemStackWrapper($this->item); - $out->putByte($this->inventorySlot); - $out->putByte($this->hotbarSlot); - $out->putByte($this->windowId); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + CommonTypes::putItemStackWrapper($out, $this->item); + Byte::writeUnsigned($out, $this->inventorySlot); + Byte::writeUnsigned($out, $this->hotbarSlot); + Byte::writeUnsigned($out, $this->windowId); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ModalFormRequestPacket.php b/src/ModalFormRequestPacket.php index ad88d158..39462f93 100644 --- a/src/ModalFormRequestPacket.php +++ b/src/ModalFormRequestPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class ModalFormRequestPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::MODAL_FORM_REQUEST_PACKET; @@ -32,14 +35,14 @@ public static function create(int $formId, string $formData) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->formId = $in->getUnsignedVarInt(); - $this->formData = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->formId = VarInt::readUnsignedInt($in); + $this->formData = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt($this->formId); - $out->putString($this->formData); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, $this->formId); + CommonTypes::putString($out, $this->formData); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ModalFormResponsePacket.php b/src/ModalFormResponsePacket.php index c39a84ef..79b57259 100644 --- a/src/ModalFormResponsePacket.php +++ b/src/ModalFormResponsePacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class ModalFormResponsePacket extends DataPacket implements ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::MODAL_FORM_RESPONSE_PACKET; @@ -46,17 +50,17 @@ public static function cancel(int $formId, int $cancelReason) : self{ return self::create($formId, null, $cancelReason); } - protected function decodePayload(PacketSerializer $in) : void{ - $this->formId = $in->getUnsignedVarInt(); - $this->formData = $in->readOptional($in->getString(...)); - $this->cancelReason = $in->readOptional($in->getByte(...)); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->formId = VarInt::readUnsignedInt($in); + $this->formData = CommonTypes::readOptional($in, CommonTypes::getString(...)); + $this->cancelReason = CommonTypes::readOptional($in, Byte::readUnsigned(...)); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt($this->formId); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, $this->formId); - $out->writeOptional($this->formData, $out->putString(...)); - $out->writeOptional($this->cancelReason, $out->putByte(...)); + CommonTypes::writeOptional($out, $this->formData, CommonTypes::putString(...)); + CommonTypes::writeOptional($out, $this->cancelReason, Byte::writeUnsigned(...)); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/MotionPredictionHintsPacket.php b/src/MotionPredictionHintsPacket.php index 006981d4..d8e8f06a 100644 --- a/src/MotionPredictionHintsPacket.php +++ b/src/MotionPredictionHintsPacket.php @@ -14,8 +14,10 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class MotionPredictionHintsPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::MOTION_PREDICTION_HINTS_PACKET; @@ -41,16 +43,16 @@ public function getMotion() : Vector3{ return $this->motion; } public function isOnGround() : bool{ return $this->onGround; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorRuntimeId = $in->getActorRuntimeId(); - $this->motion = $in->getVector3(); - $this->onGround = $in->getBool(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->motion = CommonTypes::getVector3($in); + $this->onGround = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorRuntimeId($this->actorRuntimeId); - $out->putVector3($this->motion); - $out->putBool($this->onGround); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + CommonTypes::putVector3($out, $this->motion); + CommonTypes::putBool($out, $this->onGround); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/MoveActorAbsolutePacket.php b/src/MoveActorAbsolutePacket.php index 5f48e3c4..927363ad 100644 --- a/src/MoveActorAbsolutePacket.php +++ b/src/MoveActorAbsolutePacket.php @@ -14,8 +14,11 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class MoveActorAbsolutePacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::MOVE_ACTOR_ABSOLUTE_PACKET; @@ -45,22 +48,22 @@ public static function create(int $actorRuntimeId, Vector3 $position, float $pit return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorRuntimeId = $in->getActorRuntimeId(); - $this->flags = $in->getByte(); - $this->position = $in->getVector3(); - $this->pitch = $in->getRotationByte(); - $this->yaw = $in->getRotationByte(); - $this->headYaw = $in->getRotationByte(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->flags = Byte::readUnsigned($in); + $this->position = CommonTypes::getVector3($in); + $this->pitch = CommonTypes::getRotationByte($in); + $this->yaw = CommonTypes::getRotationByte($in); + $this->headYaw = CommonTypes::getRotationByte($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorRuntimeId($this->actorRuntimeId); - $out->putByte($this->flags); - $out->putVector3($this->position); - $out->putRotationByte($this->pitch); - $out->putRotationByte($this->yaw); - $out->putRotationByte($this->headYaw); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + Byte::writeUnsigned($out, $this->flags); + CommonTypes::putVector3($out, $this->position); + CommonTypes::putRotationByte($out, $this->pitch); + CommonTypes::putRotationByte($out, $this->yaw); + CommonTypes::putRotationByte($out, $this->headYaw); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/MoveActorDeltaPacket.php b/src/MoveActorDeltaPacket.php index 9f218b68..788572b5 100644 --- a/src/MoveActorDeltaPacket.php +++ b/src/MoveActorDeltaPacket.php @@ -14,8 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; -use pocketmine\utils\BinaryDataException; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\DataDecodeException; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class MoveActorDeltaPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::MOVE_ACTOR_DELTA_PACKET; @@ -39,29 +42,25 @@ class MoveActorDeltaPacket extends DataPacket implements ClientboundPacket{ public float $yaw = 0.0; public float $headYaw = 0.0; - /** - * @throws BinaryDataException - */ - private function maybeReadCoord(int $flag, PacketSerializer $in) : float{ + /** @throws DataDecodeException */ + private function maybeReadCoord(int $flag, ByteBufferReader $in) : float{ if(($this->flags & $flag) !== 0){ - return $in->getLFloat(); + return LE::readFloat($in); } return 0; } - /** - * @throws BinaryDataException - */ - private function maybeReadRotation(int $flag, PacketSerializer $in) : float{ + /** @throws DataDecodeException */ + private function maybeReadRotation(int $flag, ByteBufferReader $in) : float{ if(($this->flags & $flag) !== 0){ - return $in->getRotationByte(); + return CommonTypes::getRotationByte($in); } return 0.0; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorRuntimeId = $in->getActorRuntimeId(); - $this->flags = $in->getLShort(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->flags = LE::readUnsignedShort($in); $this->xPos = $this->maybeReadCoord(self::FLAG_HAS_X, $in); $this->yPos = $this->maybeReadCoord(self::FLAG_HAS_Y, $in); $this->zPos = $this->maybeReadCoord(self::FLAG_HAS_Z, $in); @@ -70,21 +69,21 @@ protected function decodePayload(PacketSerializer $in) : void{ $this->headYaw = $this->maybeReadRotation(self::FLAG_HAS_HEAD_YAW, $in); } - private function maybeWriteCoord(int $flag, float $val, PacketSerializer $out) : void{ + private function maybeWriteCoord(int $flag, float $val, ByteBufferWriter $out) : void{ if(($this->flags & $flag) !== 0){ - $out->putLFloat($val); + LE::writeFloat($out, $val); } } - private function maybeWriteRotation(int $flag, float $val, PacketSerializer $out) : void{ + private function maybeWriteRotation(int $flag, float $val, ByteBufferWriter $out) : void{ if(($this->flags & $flag) !== 0){ - $out->putRotationByte($val); + CommonTypes::putRotationByte($out, $val); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorRuntimeId($this->actorRuntimeId); - $out->putLShort($this->flags); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + LE::writeUnsignedShort($out, $this->flags); $this->maybeWriteCoord(self::FLAG_HAS_X, $this->xPos, $out); $this->maybeWriteCoord(self::FLAG_HAS_Y, $this->yPos, $out); $this->maybeWriteCoord(self::FLAG_HAS_Z, $this->zPos, $out); diff --git a/src/MovePlayerPacket.php b/src/MovePlayerPacket.php index eafc8412..2c6b9fbd 100644 --- a/src/MovePlayerPacket.php +++ b/src/MovePlayerPacket.php @@ -14,8 +14,13 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class MovePlayerPacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::MOVE_PLAYER_PACKET; @@ -82,36 +87,36 @@ public static function simple( return self::create($actorRuntimeId, $position, $pitch, $yaw, $headYaw, $mode, $onGround, $ridingActorRuntimeId, 0, 0, $tick); } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorRuntimeId = $in->getActorRuntimeId(); - $this->position = $in->getVector3(); - $this->pitch = $in->getLFloat(); - $this->yaw = $in->getLFloat(); - $this->headYaw = $in->getLFloat(); - $this->mode = $in->getByte(); - $this->onGround = $in->getBool(); - $this->ridingActorRuntimeId = $in->getActorRuntimeId(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->position = CommonTypes::getVector3($in); + $this->pitch = LE::readFloat($in); + $this->yaw = LE::readFloat($in); + $this->headYaw = LE::readFloat($in); + $this->mode = Byte::readUnsigned($in); + $this->onGround = CommonTypes::getBool($in); + $this->ridingActorRuntimeId = CommonTypes::getActorRuntimeId($in); if($this->mode === MovePlayerPacket::MODE_TELEPORT){ - $this->teleportCause = $in->getLInt(); - $this->teleportItem = $in->getLInt(); + $this->teleportCause = LE::readSignedInt($in); + $this->teleportItem = LE::readSignedInt($in); } - $this->tick = $in->getUnsignedVarLong(); + $this->tick = VarInt::readUnsignedLong($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorRuntimeId($this->actorRuntimeId); - $out->putVector3($this->position); - $out->putLFloat($this->pitch); - $out->putLFloat($this->yaw); - $out->putLFloat($this->headYaw); //TODO - $out->putByte($this->mode); - $out->putBool($this->onGround); - $out->putActorRuntimeId($this->ridingActorRuntimeId); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + CommonTypes::putVector3($out, $this->position); + LE::writeFloat($out, $this->pitch); + LE::writeFloat($out, $this->yaw); + LE::writeFloat($out, $this->headYaw); //TODO + Byte::writeUnsigned($out, $this->mode); + CommonTypes::putBool($out, $this->onGround); + CommonTypes::putActorRuntimeId($out, $this->ridingActorRuntimeId); if($this->mode === MovePlayerPacket::MODE_TELEPORT){ - $out->putLInt($this->teleportCause); - $out->putLInt($this->teleportItem); + LE::writeSignedInt($out, $this->teleportCause); + LE::writeSignedInt($out, $this->teleportItem); } - $out->putUnsignedVarLong($this->tick); + VarInt::writeUnsignedLong($out, $this->tick); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/MovementEffectPacket.php b/src/MovementEffectPacket.php index 642015dc..1cb575eb 100644 --- a/src/MovementEffectPacket.php +++ b/src/MovementEffectPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\MovementEffectType; class MovementEffectPacket extends DataPacket implements ClientboundPacket{ @@ -45,18 +48,18 @@ public function getDuration() : int{ return $this->duration; } public function getTick() : int{ return $this->tick; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorRuntimeId = $in->getActorRuntimeId(); - $this->effectType = MovementEffectType::fromPacket($in->getUnsignedVarInt()); - $this->duration = $in->getUnsignedVarInt(); - $this->tick = $in->getUnsignedVarLong(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->effectType = MovementEffectType::fromPacket(VarInt::readUnsignedInt($in)); + $this->duration = VarInt::readUnsignedInt($in); + $this->tick = VarInt::readUnsignedLong($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorRuntimeId($this->actorRuntimeId); - $out->putUnsignedVarInt($this->effectType->value); - $out->putUnsignedVarInt($this->duration); - $out->putUnsignedVarLong($this->tick); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + VarInt::writeUnsignedInt($out, $this->effectType->value); + VarInt::writeUnsignedInt($out, $this->duration); + VarInt::writeUnsignedLong($out, $this->tick); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/MultiplayerSettingsPacket.php b/src/MultiplayerSettingsPacket.php index 7d672a6b..14afb854 100644 --- a/src/MultiplayerSettingsPacket.php +++ b/src/MultiplayerSettingsPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; class MultiplayerSettingsPacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::MULTIPLAYER_SETTINGS_PACKET; @@ -38,12 +40,12 @@ public function getAction() : int{ return $this->action; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->action = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->action = VarInt::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->action); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->action); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/NetworkChunkPublisherUpdatePacket.php b/src/NetworkChunkPublisherUpdatePacket.php index 097274ae..87b829c2 100644 --- a/src/NetworkChunkPublisherUpdatePacket.php +++ b/src/NetworkChunkPublisherUpdatePacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; use pocketmine\network\mcpe\protocol\types\ChunkPosition; use function count; @@ -41,11 +45,11 @@ public static function create(BlockPosition $blockPosition, int $radius, array $ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->blockPosition = $in->getSignedBlockPosition(); - $this->radius = $in->getUnsignedVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->blockPosition = CommonTypes::getSignedBlockPosition($in); + $this->radius = VarInt::readUnsignedInt($in); - $count = $in->getLInt(); + $count = LE::readUnsignedInt($in); if($count > self::MAX_SAVED_CHUNKS){ throw new PacketDecodeException("Expected at most " . self::MAX_SAVED_CHUNKS . " saved chunks, got " . $count); } @@ -54,11 +58,11 @@ protected function decodePayload(PacketSerializer $in) : void{ } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putSignedBlockPosition($this->blockPosition); - $out->putUnsignedVarInt($this->radius); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putSignedBlockPosition($out, $this->blockPosition); + VarInt::writeUnsignedInt($out, $this->radius); - $out->putLInt(count($this->savedChunks)); + LE::writeUnsignedInt($out, count($this->savedChunks)); foreach($this->savedChunks as $chunk){ $chunk->write($out); } diff --git a/src/NetworkSettingsPacket.php b/src/NetworkSettingsPacket.php index e626f8a3..b0a65b80 100644 --- a/src/NetworkSettingsPacket.php +++ b/src/NetworkSettingsPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\CompressionAlgorithm; /** @@ -66,20 +70,20 @@ public function getClientThrottleThreshold() : int{ return $this->clientThrottle public function getClientThrottleScalar() : float{ return $this->clientThrottleScalar; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->compressionThreshold = $in->getLShort(); - $this->compressionAlgorithm = $in->getLShort(); - $this->enableClientThrottling = $in->getBool(); - $this->clientThrottleThreshold = $in->getByte(); - $this->clientThrottleScalar = $in->getLFloat(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->compressionThreshold = LE::readUnsignedShort($in); + $this->compressionAlgorithm = LE::readUnsignedShort($in); + $this->enableClientThrottling = CommonTypes::getBool($in); + $this->clientThrottleThreshold = Byte::readUnsigned($in); + $this->clientThrottleScalar = LE::readFloat($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLShort($this->compressionThreshold); - $out->putLShort($this->compressionAlgorithm); - $out->putBool($this->enableClientThrottling); - $out->putByte($this->clientThrottleThreshold); - $out->putLFloat($this->clientThrottleScalar); + protected function encodePayload(ByteBufferWriter $out) : void{ + LE::writeUnsignedShort($out, $this->compressionThreshold); + LE::writeUnsignedShort($out, $this->compressionAlgorithm); + CommonTypes::putBool($out, $this->enableClientThrottling); + Byte::writeUnsigned($out, $this->clientThrottleThreshold); + LE::writeFloat($out, $this->clientThrottleScalar); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/NetworkStackLatencyPacket.php b/src/NetworkStackLatencyPacket.php index 28c76ed9..fd957ba0 100644 --- a/src/NetworkStackLatencyPacket.php +++ b/src/NetworkStackLatencyPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class NetworkStackLatencyPacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::NETWORK_STACK_LATENCY_PACKET; @@ -40,14 +43,14 @@ public static function response(int $timestamp) : self{ return self::create($timestamp, false); } - protected function decodePayload(PacketSerializer $in) : void{ - $this->timestamp = $in->getLLong(); - $this->needResponse = $in->getBool(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->timestamp = LE::readUnsignedLong($in); + $this->needResponse = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLLong($this->timestamp); - $out->putBool($this->needResponse); + protected function encodePayload(ByteBufferWriter $out) : void{ + LE::writeUnsignedLong($out, $this->timestamp); + CommonTypes::putBool($out, $this->needResponse); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/NpcDialoguePacket.php b/src/NpcDialoguePacket.php index 98dab95f..56a1f694 100644 --- a/src/NpcDialoguePacket.php +++ b/src/NpcDialoguePacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class NpcDialoguePacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::NPC_DIALOGUE_PACKET; @@ -55,22 +59,22 @@ public function getNpcName() : string{ return $this->npcName; } public function getActionJson() : string{ return $this->actionJson; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->npcActorUniqueId = $in->getLLong(); //WHY NOT USING STANDARD METHODS, MOJANG - $this->actionType = $in->getVarInt(); - $this->dialogue = $in->getString(); - $this->sceneName = $in->getString(); - $this->npcName = $in->getString(); - $this->actionJson = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->npcActorUniqueId = LE::readSignedLong($in); //WHY NOT USING STANDARD METHODS, MOJANG + $this->actionType = VarInt::readSignedInt($in); + $this->dialogue = CommonTypes::getString($in); + $this->sceneName = CommonTypes::getString($in); + $this->npcName = CommonTypes::getString($in); + $this->actionJson = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLLong($this->npcActorUniqueId); - $out->putVarInt($this->actionType); - $out->putString($this->dialogue); - $out->putString($this->sceneName); - $out->putString($this->npcName); - $out->putString($this->actionJson); + protected function encodePayload(ByteBufferWriter $out) : void{ + LE::writeSignedLong($out, $this->npcActorUniqueId); + VarInt::writeSignedInt($out, $this->actionType); + CommonTypes::putString($out, $this->dialogue); + CommonTypes::putString($out, $this->sceneName); + CommonTypes::putString($out, $this->npcName); + CommonTypes::putString($out, $this->actionJson); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/NpcRequestPacket.php b/src/NpcRequestPacket.php index 08fbe915..96ff5011 100644 --- a/src/NpcRequestPacket.php +++ b/src/NpcRequestPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class NpcRequestPacket extends DataPacket implements ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::NPC_REQUEST_PACKET; @@ -46,20 +49,20 @@ public static function create(int $actorRuntimeId, int $requestType, string $com return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorRuntimeId = $in->getActorRuntimeId(); - $this->requestType = $in->getByte(); - $this->commandString = $in->getString(); - $this->actionIndex = $in->getByte(); - $this->sceneName = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->requestType = Byte::readUnsigned($in); + $this->commandString = CommonTypes::getString($in); + $this->actionIndex = Byte::readUnsigned($in); + $this->sceneName = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorRuntimeId($this->actorRuntimeId); - $out->putByte($this->requestType); - $out->putString($this->commandString); - $out->putByte($this->actionIndex); - $out->putString($this->sceneName); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + Byte::writeUnsigned($out, $this->requestType); + CommonTypes::putString($out, $this->commandString); + Byte::writeUnsigned($out, $this->actionIndex); + CommonTypes::putString($out, $this->sceneName); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/OnScreenTextureAnimationPacket.php b/src/OnScreenTextureAnimationPacket.php index 6dfc9d4c..f0140d63 100644 --- a/src/OnScreenTextureAnimationPacket.php +++ b/src/OnScreenTextureAnimationPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; class OnScreenTextureAnimationPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::ON_SCREEN_TEXTURE_ANIMATION_PACKET; @@ -30,12 +32,12 @@ public static function create(int $effectId) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->effectId = $in->getLInt(); //unsigned + protected function decodePayload(ByteBufferReader $in) : void{ + $this->effectId = LE::readUnsignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLInt($this->effectId); + protected function encodePayload(ByteBufferWriter $out) : void{ + LE::writeUnsignedInt($out, $this->effectId); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/OpenSignPacket.php b/src/OpenSignPacket.php index 914a7f1a..df12f205 100644 --- a/src/OpenSignPacket.php +++ b/src/OpenSignPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; /** @@ -40,14 +42,14 @@ public function getBlockPosition() : BlockPosition{ return $this->blockPosition; public function isFront() : bool{ return $this->front; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->blockPosition = $in->getBlockPosition(); - $this->front = $in->getBool(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->blockPosition = CommonTypes::getBlockPosition($in); + $this->front = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putBlockPosition($this->blockPosition); - $out->putBool($this->front); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putBlockPosition($out, $this->blockPosition); + CommonTypes::putBool($out, $this->front); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/Packet.php b/src/Packet.php index feed87e8..cb25b1a4 100644 --- a/src/Packet.php +++ b/src/Packet.php @@ -14,7 +14,8 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; interface Packet{ @@ -27,9 +28,9 @@ public function canBeSentBeforeLogin() : bool; /** * @throws PacketDecodeException */ - public function decode(PacketSerializer $in) : void; + public function decode(ByteBufferReader $in) : void; - public function encode(PacketSerializer $out) : void; + public function encode(ByteBufferWriter $out) : void; /** * Performs handling for this packet. Usually you'll want an appropriately named method in the session handler for diff --git a/src/PacketPool.php b/src/PacketPool.php index 5d88e864..b4fe7f27 100644 --- a/src/PacketPool.php +++ b/src/PacketPool.php @@ -14,8 +14,8 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\utils\Binary; -use pocketmine\utils\BinaryDataException; +use pmmp\encoding\DataDecodeException; +use pmmp\encoding\VarInt; class PacketPool{ protected static ?PacketPool $instance = null; @@ -251,10 +251,9 @@ public function getPacketById(int $pid) : ?Packet{ } /** - * @throws BinaryDataException + * @throws DataDecodeException */ public function getPacket(string $buffer) : ?Packet{ - $offset = 0; - return $this->getPacketById(Binary::readUnsignedVarInt($buffer, $offset) & DataPacket::PID_MASK); + return $this->getPacketById(VarInt::unpackUnsignedInt($buffer) & DataPacket::PID_MASK); } } diff --git a/src/PacketViolationWarningPacket.php b/src/PacketViolationWarningPacket.php index 3b5d74bf..65c39412 100644 --- a/src/PacketViolationWarningPacket.php +++ b/src/PacketViolationWarningPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class PacketViolationWarningPacket extends DataPacket implements ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::PACKET_VIOLATION_WARNING_PACKET; @@ -50,18 +53,18 @@ public function getPacketId() : int{ return $this->packetId; } public function getMessage() : string{ return $this->message; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->type = $in->getVarInt(); - $this->severity = $in->getVarInt(); - $this->packetId = $in->getVarInt(); - $this->message = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->type = VarInt::readSignedInt($in); + $this->severity = VarInt::readSignedInt($in); + $this->packetId = VarInt::readSignedInt($in); + $this->message = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->type); - $out->putVarInt($this->severity); - $out->putVarInt($this->packetId); - $out->putString($this->message); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->type); + VarInt::writeSignedInt($out, $this->severity); + VarInt::writeSignedInt($out, $this->packetId); + CommonTypes::putString($out, $this->message); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/PhotoTransferPacket.php b/src/PhotoTransferPacket.php index 1b116032..26df13a8 100644 --- a/src/PhotoTransferPacket.php +++ b/src/PhotoTransferPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class PhotoTransferPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::PHOTO_TRANSFER_PACKET; @@ -50,24 +54,24 @@ public static function create( return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->photoName = $in->getString(); - $this->photoData = $in->getString(); - $this->bookId = $in->getString(); - $this->type = $in->getByte(); - $this->sourceType = $in->getByte(); - $this->ownerActorUniqueId = $in->getLLong(); //............... - $this->newPhotoName = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->photoName = CommonTypes::getString($in); + $this->photoData = CommonTypes::getString($in); + $this->bookId = CommonTypes::getString($in); + $this->type = Byte::readUnsigned($in); + $this->sourceType = Byte::readUnsigned($in); + $this->ownerActorUniqueId = LE::readSignedLong($in); //............... + $this->newPhotoName = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->photoName); - $out->putString($this->photoData); - $out->putString($this->bookId); - $out->putByte($this->type); - $out->putByte($this->sourceType); - $out->putLLong($this->ownerActorUniqueId); - $out->putString($this->newPhotoName); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->photoName); + CommonTypes::putString($out, $this->photoData); + CommonTypes::putString($out, $this->bookId); + Byte::writeUnsigned($out, $this->type); + Byte::writeUnsigned($out, $this->sourceType); + LE::writeSignedLong($out, $this->ownerActorUniqueId); + CommonTypes::putString($out, $this->newPhotoName); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/PlaySoundPacket.php b/src/PlaySoundPacket.php index c14b0d3f..7a0dbb61 100644 --- a/src/PlaySoundPacket.php +++ b/src/PlaySoundPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; class PlaySoundPacket extends DataPacket implements ClientboundPacket{ @@ -41,21 +44,21 @@ public static function create(string $soundName, float $x, float $y, float $z, f return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->soundName = $in->getString(); - $blockPosition = $in->getBlockPosition(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->soundName = CommonTypes::getString($in); + $blockPosition = CommonTypes::getBlockPosition($in); $this->x = $blockPosition->getX() / 8; $this->y = $blockPosition->getY() / 8; $this->z = $blockPosition->getZ() / 8; - $this->volume = $in->getLFloat(); - $this->pitch = $in->getLFloat(); + $this->volume = LE::readFloat($in); + $this->pitch = LE::readFloat($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->soundName); - $out->putBlockPosition(new BlockPosition((int) ($this->x * 8), (int) ($this->y * 8), (int) ($this->z * 8))); - $out->putLFloat($this->volume); - $out->putLFloat($this->pitch); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->soundName); + CommonTypes::putBlockPosition($out, new BlockPosition((int) ($this->x * 8), (int) ($this->y * 8), (int) ($this->z * 8))); + LE::writeFloat($out, $this->volume); + LE::writeFloat($out, $this->pitch); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/PlayStatusPacket.php b/src/PlayStatusPacket.php index 5541a21b..46f1ddb2 100644 --- a/src/PlayStatusPacket.php +++ b/src/PlayStatusPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\BE; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; class PlayStatusPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::PLAY_STATUS_PACKET; @@ -41,16 +43,16 @@ public static function create(int $status) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->status = $in->getInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->status = BE::readUnsignedInt($in); } public function canBeSentBeforeLogin() : bool{ return true; } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putInt($this->status); + protected function encodePayload(ByteBufferWriter $out) : void{ + BE::writeUnsignedInt($out, $this->status); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/PlayerActionPacket.php b/src/PlayerActionPacket.php index 717ff310..8a56f0a1 100644 --- a/src/PlayerActionPacket.php +++ b/src/PlayerActionPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; use pocketmine\network\mcpe\protocol\types\PlayerAction; @@ -41,20 +44,20 @@ public static function create(int $actorRuntimeId, int $action, BlockPosition $b return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorRuntimeId = $in->getActorRuntimeId(); - $this->action = $in->getVarInt(); - $this->blockPosition = $in->getBlockPosition(); - $this->resultPosition = $in->getBlockPosition(); - $this->face = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->action = VarInt::readSignedInt($in); + $this->blockPosition = CommonTypes::getBlockPosition($in); + $this->resultPosition = CommonTypes::getBlockPosition($in); + $this->face = VarInt::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorRuntimeId($this->actorRuntimeId); - $out->putVarInt($this->action); - $out->putBlockPosition($this->blockPosition); - $out->putBlockPosition($this->resultPosition); - $out->putVarInt($this->face); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + VarInt::writeSignedInt($out, $this->action); + CommonTypes::putBlockPosition($out, $this->blockPosition); + CommonTypes::putBlockPosition($out, $this->resultPosition); + VarInt::writeSignedInt($out, $this->face); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/PlayerArmorDamagePacket.php b/src/PlayerArmorDamagePacket.php index b5a10e4b..c868018c 100644 --- a/src/PlayerArmorDamagePacket.php +++ b/src/PlayerArmorDamagePacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; class PlayerArmorDamagePacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::PLAYER_ARMOR_DAMAGE_PACKET; @@ -54,15 +57,15 @@ public function getFeetSlotDamage() : ?int{ return $this->feetSlotDamage; } public function getBodySlotDamage() : ?int{ return $this->bodySlotDamage; } - private function maybeReadDamage(int $flags, int $flag, PacketSerializer $in) : ?int{ + private function maybeReadDamage(int $flags, int $flag, ByteBufferReader $in) : ?int{ if(($flags & (1 << $flag)) !== 0){ - return $in->getVarInt(); + return VarInt::readSignedInt($in); } return null; } - protected function decodePayload(PacketSerializer $in) : void{ - $flags = $in->getByte(); + protected function decodePayload(ByteBufferReader $in) : void{ + $flags = Byte::readUnsigned($in); $this->headSlotDamage = $this->maybeReadDamage($flags, self::FLAG_HEAD, $in); $this->chestSlotDamage = $this->maybeReadDamage($flags, self::FLAG_CHEST, $in); @@ -75,14 +78,14 @@ private function composeFlag(?int $field, int $flag) : int{ return $field !== null ? (1 << $flag) : 0; } - private function maybeWriteDamage(?int $field, PacketSerializer $out) : void{ + private function maybeWriteDamage(?int $field, ByteBufferWriter $out) : void{ if($field !== null){ - $out->putVarInt($field); + VarInt::writeSignedInt($out, $field); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte( + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->composeFlag($this->headSlotDamage, self::FLAG_HEAD) | $this->composeFlag($this->chestSlotDamage, self::FLAG_CHEST) | $this->composeFlag($this->legsSlotDamage, self::FLAG_LEGS) | diff --git a/src/PlayerAuthInputPacket.php b/src/PlayerAuthInputPacket.php index 3430847e..76ca5b54 100644 --- a/src/PlayerAuthInputPacket.php +++ b/src/PlayerAuthInputPacket.php @@ -14,10 +14,14 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; use pocketmine\math\Vector2; use pocketmine\math\Vector3; use pocketmine\network\mcpe\protocol\serializer\BitSet; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\InputMode; use pocketmine\network\mcpe\protocol\types\InteractionMode; use pocketmine\network\mcpe\protocol\types\inventory\stackrequest\ItemStackRequest; @@ -259,20 +263,20 @@ public function getCameraOrientation() : Vector3{ return $this->cameraOrientatio public function getRawMove() : Vector2{ return $this->rawMove; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->pitch = $in->getLFloat(); - $this->yaw = $in->getLFloat(); - $this->position = $in->getVector3(); - $this->moveVecX = $in->getLFloat(); - $this->moveVecZ = $in->getLFloat(); - $this->headYaw = $in->getLFloat(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->pitch = LE::readFloat($in); + $this->yaw = LE::readFloat($in); + $this->position = CommonTypes::getVector3($in); + $this->moveVecX = LE::readFloat($in); + $this->moveVecZ = LE::readFloat($in); + $this->headYaw = LE::readFloat($in); $this->inputFlags = BitSet::read($in, PlayerAuthInputFlags::NUMBER_OF_FLAGS); - $this->inputMode = $in->getUnsignedVarInt(); - $this->playMode = $in->getUnsignedVarInt(); - $this->interactionMode = $in->getUnsignedVarInt(); - $this->interactRotation = $in->getVector2(); - $this->tick = $in->getUnsignedVarLong(); - $this->delta = $in->getVector3(); + $this->inputMode = VarInt::readUnsignedInt($in); + $this->playMode = VarInt::readUnsignedInt($in); + $this->interactionMode = VarInt::readUnsignedInt($in); + $this->interactRotation = CommonTypes::getVector2($in); + $this->tick = VarInt::readUnsignedLong($in); + $this->delta = CommonTypes::getVector3($in); if($this->inputFlags->get(PlayerAuthInputFlags::PERFORM_ITEM_INTERACTION)){ $this->itemInteractionData = ItemInteractionData::read($in); } @@ -281,9 +285,9 @@ protected function decodePayload(PacketSerializer $in) : void{ } if($this->inputFlags->get(PlayerAuthInputFlags::PERFORM_BLOCK_ACTIONS)){ $this->blockActions = []; - $max = $in->getVarInt(); + $max = VarInt::readSignedInt($in); for($i = 0; $i < $max; ++$i){ - $actionType = $in->getVarInt(); + $actionType = VarInt::readSignedInt($in); $this->blockActions[] = match(true){ PlayerBlockActionWithBlockInfo::isValidActionType($actionType) => PlayerBlockActionWithBlockInfo::read($in, $actionType), $actionType === PlayerAction::STOP_BREAK => new PlayerBlockActionStopBreak(), @@ -294,26 +298,26 @@ protected function decodePayload(PacketSerializer $in) : void{ if($this->inputFlags->get(PlayerAuthInputFlags::IN_CLIENT_PREDICTED_VEHICLE)){ $this->vehicleInfo = PlayerAuthInputVehicleInfo::read($in); } - $this->analogMoveVecX = $in->getLFloat(); - $this->analogMoveVecZ = $in->getLFloat(); - $this->cameraOrientation = $in->getVector3(); - $this->rawMove = $in->getVector2(); + $this->analogMoveVecX = LE::readFloat($in); + $this->analogMoveVecZ = LE::readFloat($in); + $this->cameraOrientation = CommonTypes::getVector3($in); + $this->rawMove = CommonTypes::getVector2($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLFloat($this->pitch); - $out->putLFloat($this->yaw); - $out->putVector3($this->position); - $out->putLFloat($this->moveVecX); - $out->putLFloat($this->moveVecZ); - $out->putLFloat($this->headYaw); + protected function encodePayload(ByteBufferWriter $out) : void{ + LE::writeFloat($out, $this->pitch); + LE::writeFloat($out, $this->yaw); + CommonTypes::putVector3($out, $this->position); + LE::writeFloat($out, $this->moveVecX); + LE::writeFloat($out, $this->moveVecZ); + LE::writeFloat($out, $this->headYaw); $this->inputFlags->write($out); - $out->putUnsignedVarInt($this->inputMode); - $out->putUnsignedVarInt($this->playMode); - $out->putUnsignedVarInt($this->interactionMode); - $out->putVector2($this->interactRotation); - $out->putUnsignedVarLong($this->tick); - $out->putVector3($this->delta); + VarInt::writeUnsignedInt($out, $this->inputMode); + VarInt::writeUnsignedInt($out, $this->playMode); + VarInt::writeUnsignedInt($out, $this->interactionMode); + CommonTypes::putVector2($out, $this->interactRotation); + VarInt::writeUnsignedLong($out, $this->tick); + CommonTypes::putVector3($out, $this->delta); if($this->itemInteractionData !== null){ $this->itemInteractionData->write($out); } @@ -321,19 +325,19 @@ protected function encodePayload(PacketSerializer $out) : void{ $this->itemStackRequest->write($out); } if($this->blockActions !== null){ - $out->putVarInt(count($this->blockActions)); + VarInt::writeSignedInt($out, count($this->blockActions)); foreach($this->blockActions as $blockAction){ - $out->putVarInt($blockAction->getActionType()); + VarInt::writeSignedInt($out, $blockAction->getActionType()); $blockAction->write($out); } } if($this->vehicleInfo !== null){ $this->vehicleInfo->write($out); } - $out->putLFloat($this->analogMoveVecX); - $out->putLFloat($this->analogMoveVecZ); - $out->putVector3($this->cameraOrientation); - $out->putVector2($this->rawMove); + LE::writeFloat($out, $this->analogMoveVecX); + LE::writeFloat($out, $this->analogMoveVecZ); + CommonTypes::putVector3($out, $this->cameraOrientation); + CommonTypes::putVector2($out, $this->rawMove); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/PlayerAuthInputVehicleInfo.php b/src/PlayerAuthInputVehicleInfo.php index 95762640..c77e691e 100644 --- a/src/PlayerAuthInputVehicleInfo.php +++ b/src/PlayerAuthInputVehicleInfo.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class PlayerAuthInputVehicleInfo{ @@ -30,17 +33,17 @@ public function getVehicleRotationZ() : float{ return $this->vehicleRotationZ; } public function getPredictedVehicleActorUniqueId() : int{ return $this->predictedVehicleActorUniqueId; } - public static function read(PacketSerializer $in) : self{ - $vehicleRotationX = $in->getLFloat(); - $vehicleRotationZ = $in->getLFloat(); - $predictedVehicleActorUniqueId = $in->getActorUniqueId(); + public static function read(ByteBufferReader $in) : self{ + $vehicleRotationX = LE::readFloat($in); + $vehicleRotationZ = LE::readFloat($in); + $predictedVehicleActorUniqueId = CommonTypes::getActorUniqueId($in); return new self($vehicleRotationX, $vehicleRotationZ, $predictedVehicleActorUniqueId); } - public function write(PacketSerializer $out) : void{ - $out->putLFloat($this->vehicleRotationX); - $out->putLFloat($this->vehicleRotationZ); - $out->putActorUniqueId($this->predictedVehicleActorUniqueId); + public function write(ByteBufferWriter $out) : void{ + LE::writeFloat($out, $this->vehicleRotationX); + LE::writeFloat($out, $this->vehicleRotationZ); + CommonTypes::putActorUniqueId($out, $this->predictedVehicleActorUniqueId); } } diff --git a/src/PlayerEnchantOptionsPacket.php b/src/PlayerEnchantOptionsPacket.php index 2c2773f5..24d341b3 100644 --- a/src/PlayerEnchantOptionsPacket.php +++ b/src/PlayerEnchantOptionsPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\types\EnchantOption; use function count; @@ -39,15 +41,15 @@ public static function create(array $options) : self{ */ public function getOptions() : array{ return $this->options; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ $this->options = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ $this->options[] = EnchantOption::read($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->options)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->options)); foreach($this->options as $option){ $option->write($out); } diff --git a/src/PlayerFogPacket.php b/src/PlayerFogPacket.php index fa034647..51d6cee7 100644 --- a/src/PlayerFogPacket.php +++ b/src/PlayerFogPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use function count; class PlayerFogPacket extends DataPacket implements ClientboundPacket{ @@ -43,17 +46,17 @@ public static function create(array $fogLayers) : self{ */ public function getFogLayers() : array{ return $this->fogLayers; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ $this->fogLayers = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ - $this->fogLayers[] = $in->getString(); + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ + $this->fogLayers[] = CommonTypes::getString($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->fogLayers)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->fogLayers)); foreach($this->fogLayers as $fogLayer){ - $out->putString($fogLayer); + CommonTypes::putString($out, $fogLayer); } } diff --git a/src/PlayerHotbarPacket.php b/src/PlayerHotbarPacket.php index 54150954..1bd242c9 100644 --- a/src/PlayerHotbarPacket.php +++ b/src/PlayerHotbarPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\inventory\ContainerIds; class PlayerHotbarPacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ @@ -35,16 +39,16 @@ public static function create(int $selectedHotbarSlot, int $windowId, bool $sele return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->selectedHotbarSlot = $in->getUnsignedVarInt(); - $this->windowId = $in->getByte(); - $this->selectHotbarSlot = $in->getBool(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->selectedHotbarSlot = VarInt::readUnsignedInt($in); + $this->windowId = Byte::readUnsigned($in); + $this->selectHotbarSlot = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt($this->selectedHotbarSlot); - $out->putByte($this->windowId); - $out->putBool($this->selectHotbarSlot); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, $this->selectedHotbarSlot); + Byte::writeUnsigned($out, $this->windowId); + CommonTypes::putBool($out, $this->selectHotbarSlot); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/PlayerListPacket.php b/src/PlayerListPacket.php index 76d7ffda..ca72c856 100644 --- a/src/PlayerListPacket.php +++ b/src/PlayerListPacket.php @@ -14,8 +14,13 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; use pocketmine\color\Color; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\PlayerListEntry; use function count; @@ -54,60 +59,60 @@ public static function remove(array $entries) : self{ return self::create(self::TYPE_REMOVE, $entries); } - protected function decodePayload(PacketSerializer $in) : void{ - $this->type = $in->getByte(); - $count = $in->getUnsignedVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->type = Byte::readUnsigned($in); + $count = VarInt::readUnsignedInt($in); for($i = 0; $i < $count; ++$i){ $entry = new PlayerListEntry(); if($this->type === self::TYPE_ADD){ - $entry->uuid = $in->getUUID(); - $entry->actorUniqueId = $in->getActorUniqueId(); - $entry->username = $in->getString(); - $entry->xboxUserId = $in->getString(); - $entry->platformChatId = $in->getString(); - $entry->buildPlatform = $in->getLInt(); - $entry->skinData = $in->getSkin(); - $entry->isTeacher = $in->getBool(); - $entry->isHost = $in->getBool(); - $entry->isSubClient = $in->getBool(); - $entry->color = Color::fromARGB($in->getLInt()); + $entry->uuid = CommonTypes::getUUID($in); + $entry->actorUniqueId = CommonTypes::getActorUniqueId($in); + $entry->username = CommonTypes::getString($in); + $entry->xboxUserId = CommonTypes::getString($in); + $entry->platformChatId = CommonTypes::getString($in); + $entry->buildPlatform = LE::readSignedInt($in); + $entry->skinData = CommonTypes::getSkin($in); + $entry->isTeacher = CommonTypes::getBool($in); + $entry->isHost = CommonTypes::getBool($in); + $entry->isSubClient = CommonTypes::getBool($in); + $entry->color = Color::fromARGB(LE::readUnsignedInt($in)); }else{ - $entry->uuid = $in->getUUID(); + $entry->uuid = CommonTypes::getUUID($in); } $this->entries[$i] = $entry; } if($this->type === self::TYPE_ADD){ for($i = 0; $i < $count; ++$i){ - $this->entries[$i]->skinData->setVerified($in->getBool()); + $this->entries[$i]->skinData->setVerified(CommonTypes::getBool($in)); } } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->type); - $out->putUnsignedVarInt(count($this->entries)); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->type); + VarInt::writeUnsignedInt($out, count($this->entries)); foreach($this->entries as $entry){ if($this->type === self::TYPE_ADD){ - $out->putUUID($entry->uuid); - $out->putActorUniqueId($entry->actorUniqueId); - $out->putString($entry->username); - $out->putString($entry->xboxUserId); - $out->putString($entry->platformChatId); - $out->putLInt($entry->buildPlatform); - $out->putSkin($entry->skinData); - $out->putBool($entry->isTeacher); - $out->putBool($entry->isHost); - $out->putBool($entry->isSubClient); - $out->putLInt(($entry->color ?? new Color(255, 255, 255))->toARGB()); + CommonTypes::putUUID($out, $entry->uuid); + CommonTypes::putActorUniqueId($out, $entry->actorUniqueId); + CommonTypes::putString($out, $entry->username); + CommonTypes::putString($out, $entry->xboxUserId); + CommonTypes::putString($out, $entry->platformChatId); + LE::writeSignedInt($out, $entry->buildPlatform); + CommonTypes::putSkin($out, $entry->skinData); + CommonTypes::putBool($out, $entry->isTeacher); + CommonTypes::putBool($out, $entry->isHost); + CommonTypes::putBool($out, $entry->isSubClient); + LE::writeUnsignedInt($out, ($entry->color ?? new Color(255, 255, 255))->toARGB()); }else{ - $out->putUUID($entry->uuid); + CommonTypes::putUUID($out, $entry->uuid); } } if($this->type === self::TYPE_ADD){ foreach($this->entries as $entry){ - $out->putBool($entry->skinData->isVerified()); + CommonTypes::putBool($out, $entry->skinData->isVerified()); } } } diff --git a/src/PlayerLocationPacket.php b/src/PlayerLocationPacket.php index 06c99732..28d5a595 100644 --- a/src/PlayerLocationPacket.php +++ b/src/PlayerLocationPacket.php @@ -14,8 +14,11 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\PlayerLocationType; class PlayerLocationPacket extends DataPacket implements ClientboundPacket{ @@ -50,24 +53,24 @@ public function getActorUniqueId() : int{ return $this->actorUniqueId; } public function getPosition() : ?Vector3{ return $this->position; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->type = PlayerLocationType::fromPacket($in->getLInt()); - $this->actorUniqueId = $in->getActorUniqueId(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->type = PlayerLocationType::fromPacket(LE::readUnsignedInt($in)); + $this->actorUniqueId = CommonTypes::getActorUniqueId($in); if($this->type === PlayerLocationType::PLAYER_LOCATION_COORDINATES){ - $this->position = $in->getVector3(); + $this->position = CommonTypes::getVector3($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLInt($this->type->value); - $out->putActorUniqueId($this->actorUniqueId); + protected function encodePayload(ByteBufferWriter $out) : void{ + LE::writeUnsignedInt($out, $this->type->value); + CommonTypes::putActorUniqueId($out, $this->actorUniqueId); if($this->type === PlayerLocationType::PLAYER_LOCATION_COORDINATES){ if($this->position === null){ // this should never be the case throw new \LogicException("PlayerLocationPacket with type PLAYER_LOCATION_COORDINATES require a position to be provided"); } - $out->putVector3($this->position); + CommonTypes::putVector3($out, $this->position); } } diff --git a/src/PlayerSkinPacket.php b/src/PlayerSkinPacket.php index a8698d07..e4e35abb 100644 --- a/src/PlayerSkinPacket.php +++ b/src/PlayerSkinPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\skin\SkinData; use Ramsey\Uuid\UuidInterface; @@ -38,20 +40,20 @@ public static function create(UuidInterface $uuid, string $oldSkinName, string $ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->uuid = $in->getUUID(); - $this->skin = $in->getSkin(); - $this->newSkinName = $in->getString(); - $this->oldSkinName = $in->getString(); - $this->skin->setVerified($in->getBool()); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->uuid = CommonTypes::getUUID($in); + $this->skin = CommonTypes::getSkin($in); + $this->newSkinName = CommonTypes::getString($in); + $this->oldSkinName = CommonTypes::getString($in); + $this->skin->setVerified(CommonTypes::getBool($in)); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUUID($this->uuid); - $out->putSkin($this->skin); - $out->putString($this->newSkinName); - $out->putString($this->oldSkinName); - $out->putBool($this->skin->isVerified()); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putUUID($out, $this->uuid); + CommonTypes::putSkin($out, $this->skin); + CommonTypes::putString($out, $this->newSkinName); + CommonTypes::putString($out, $this->oldSkinName); + CommonTypes::putBool($out, $this->skin->isVerified()); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/PlayerStartItemCooldownPacket.php b/src/PlayerStartItemCooldownPacket.php index c4bc43ae..a6599e9f 100644 --- a/src/PlayerStartItemCooldownPacket.php +++ b/src/PlayerStartItemCooldownPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class PlayerStartItemCooldownPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::PLAYER_START_ITEM_COOLDOWN_PACKET; @@ -36,14 +39,14 @@ public function getItemCategory() : string{ return $this->itemCategory; } public function getCooldownTicks() : int{ return $this->cooldownTicks; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->itemCategory = $in->getString(); - $this->cooldownTicks = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->itemCategory = CommonTypes::getString($in); + $this->cooldownTicks = VarInt::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->itemCategory); - $out->putVarInt($this->cooldownTicks); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->itemCategory); + VarInt::writeSignedInt($out, $this->cooldownTicks); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/PlayerToggleCrafterSlotRequestPacket.php b/src/PlayerToggleCrafterSlotRequestPacket.php index e8e55fe4..9e1d1719 100644 --- a/src/PlayerToggleCrafterSlotRequestPacket.php +++ b/src/PlayerToggleCrafterSlotRequestPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; class PlayerToggleCrafterSlotRequestPacket extends DataPacket implements ServerboundPacket{ @@ -41,21 +45,21 @@ public function getSlot() : int{ return $this->slot; } public function isDisabled() : bool{ return $this->disabled; } - protected function decodePayload(PacketSerializer $in) : void{ - $x = $in->getLInt(); - $y = $in->getLInt(); - $z = $in->getLInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $x = LE::readSignedInt($in); + $y = LE::readSignedInt($in); + $z = LE::readSignedInt($in); $this->position = new BlockPosition($x, $y, $z); - $this->slot = $in->getByte(); - $this->disabled = $in->getBool(); + $this->slot = Byte::readUnsigned($in); + $this->disabled = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLInt($this->position->getX()); - $out->putLInt($this->position->getY()); - $out->putLInt($this->position->getZ()); - $out->putByte($this->slot); - $out->putBool($this->disabled); + protected function encodePayload(ByteBufferWriter $out) : void{ + LE::writeSignedInt($out, $this->position->getX()); + LE::writeSignedInt($out, $this->position->getY()); + LE::writeSignedInt($out, $this->position->getZ()); + Byte::writeUnsigned($out, $this->slot); + CommonTypes::putBool($out, $this->disabled); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/PlayerUpdateEntityOverridesPacket.php b/src/PlayerUpdateEntityOverridesPacket.php index 7db4b4f2..fba7b487 100644 --- a/src/PlayerUpdateEntityOverridesPacket.php +++ b/src/PlayerUpdateEntityOverridesPacket.php @@ -14,7 +14,12 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\OverrideUpdateType; class PlayerUpdateEntityOverridesPacket extends DataPacket implements ClientboundPacket{ @@ -65,31 +70,31 @@ public function getIntOverrideValue() : ?int{ return $this->intOverrideValue; } public function getFloatOverrideValue() : ?float{ return $this->floatOverrideValue; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorRuntimeId = $in->getActorRuntimeId(); - $this->propertyIndex = $in->getUnsignedVarInt(); - $this->updateType = OverrideUpdateType::fromPacket($in->getByte()); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->propertyIndex = VarInt::readUnsignedInt($in); + $this->updateType = OverrideUpdateType::fromPacket(Byte::readUnsigned($in)); if($this->updateType === OverrideUpdateType::SET_INT_OVERRIDE){ - $this->intOverrideValue = $in->getLInt(); + $this->intOverrideValue = LE::readSignedInt($in); }elseif($this->updateType === OverrideUpdateType::SET_FLOAT_OVERRIDE){ - $this->floatOverrideValue = $in->getLFloat(); + $this->floatOverrideValue = LE::readFloat($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorRuntimeId($this->actorRuntimeId); - $out->putUnsignedVarInt($this->propertyIndex); - $out->putByte($this->updateType->value); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + VarInt::writeUnsignedInt($out, $this->propertyIndex); + Byte::writeUnsigned($out, $this->updateType->value); if($this->updateType === OverrideUpdateType::SET_INT_OVERRIDE){ if($this->intOverrideValue === null){ // this should never be the case throw new \LogicException("PlayerUpdateEntityOverridesPacket with type SET_INT_OVERRIDE requires intOverrideValue to be provided"); } - $out->putLInt($this->intOverrideValue); + LE::writeSignedInt($out, $this->intOverrideValue); }elseif($this->updateType === OverrideUpdateType::SET_FLOAT_OVERRIDE){ if($this->floatOverrideValue === null){ // this should never be the case throw new \LogicException("PlayerUpdateEntityOverridesPacket with type SET_FLOAT_OVERRIDE requires floatOverrideValue to be provided"); } - $out->putLFloat($this->floatOverrideValue); + LE::writeFloat($out, $this->floatOverrideValue); } } diff --git a/src/PlayerVideoCapturePacket.php b/src/PlayerVideoCapturePacket.php index 87118ed4..306d4a86 100644 --- a/src/PlayerVideoCapturePacket.php +++ b/src/PlayerVideoCapturePacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class PlayerVideoCapturePacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::PLAYER_VIDEO_CAPTURE_PACKET; @@ -48,16 +51,16 @@ public function getFrameRate() : ?int{ return $this->frameRate; } public function getFilePrefix() : ?string{ return $this->filePrefix; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->recording = $in->getBool(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->recording = CommonTypes::getBool($in); if($this->recording){ - $this->frameRate = $in->getLInt(); - $this->filePrefix = $in->getString(); + $this->frameRate = LE::readUnsignedInt($in); + $this->filePrefix = CommonTypes::getString($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putBool($this->recording); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putBool($out, $this->recording); if($this->recording){ if($this->frameRate === null){ // this should never be the case throw new \LogicException("PlayerUpdateEntityOverridesPacket with recording=true require a frame rate to be provided"); @@ -67,8 +70,8 @@ protected function encodePayload(PacketSerializer $out) : void{ throw new \LogicException("PlayerUpdateEntityOverridesPacket with recording=true require a file prefix to be provided"); } - $out->putLInt($this->frameRate); - $out->putString($this->filePrefix); + LE::writeUnsignedInt($out, $this->frameRate); + CommonTypes::putString($out, $this->filePrefix); } } diff --git a/src/PositionTrackingDBClientRequestPacket.php b/src/PositionTrackingDBClientRequestPacket.php index 55802506..2bf67311 100644 --- a/src/PositionTrackingDBClientRequestPacket.php +++ b/src/PositionTrackingDBClientRequestPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; class PositionTrackingDBClientRequestPacket extends DataPacket implements ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::POSITION_TRACKING_D_B_CLIENT_REQUEST_PACKET; @@ -38,14 +41,14 @@ public function getAction() : int{ return $this->action; } public function getTrackingId() : int{ return $this->trackingId; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->action = $in->getByte(); - $this->trackingId = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->action = Byte::readUnsigned($in); + $this->trackingId = VarInt::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->action); - $out->putVarInt($this->trackingId); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->action); + VarInt::writeSignedInt($out, $this->trackingId); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/PositionTrackingDBServerBroadcastPacket.php b/src/PositionTrackingDBServerBroadcastPacket.php index c3d3ca67..48f2a27b 100644 --- a/src/PositionTrackingDBServerBroadcastPacket.php +++ b/src/PositionTrackingDBServerBroadcastPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\CacheableNbt; class PositionTrackingDBServerBroadcastPacket extends DataPacket implements ClientboundPacket{ @@ -48,16 +52,16 @@ public function getTrackingId() : int{ return $this->trackingId; } /** @phpstan-return CacheableNbt<\pocketmine\nbt\tag\CompoundTag> */ public function getNbt() : CacheableNbt{ return $this->nbt; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->action = $in->getByte(); - $this->trackingId = $in->getVarInt(); - $this->nbt = new CacheableNbt($in->getNbtCompoundRoot()); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->action = Byte::readUnsigned($in); + $this->trackingId = VarInt::readSignedInt($in); + $this->nbt = new CacheableNbt(CommonTypes::getNbtCompoundRoot($in)); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->action); - $out->putVarInt($this->trackingId); - $out->put($this->nbt->getEncodedNbt()); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->action); + VarInt::writeSignedInt($out, $this->trackingId); + $out->writeByteArray($this->nbt->getEncodedNbt()); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/PurchaseReceiptPacket.php b/src/PurchaseReceiptPacket.php index e6166c3b..29595315 100644 --- a/src/PurchaseReceiptPacket.php +++ b/src/PurchaseReceiptPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use function count; class PurchaseReceiptPacket extends DataPacket implements ServerboundPacket{ @@ -33,17 +36,17 @@ public static function create(array $entries) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $count = $in->getUnsignedVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $count = VarInt::readUnsignedInt($in); for($i = 0; $i < $count; ++$i){ - $this->entries[] = $in->getString(); + $this->entries[] = CommonTypes::getString($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->entries)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->entries)); foreach($this->entries as $entry){ - $out->putString($entry); + CommonTypes::putString($out, $entry); } } diff --git a/src/RefreshEntitlementsPacket.php b/src/RefreshEntitlementsPacket.php index 60bf913e..c22c7dbc 100644 --- a/src/RefreshEntitlementsPacket.php +++ b/src/RefreshEntitlementsPacket.php @@ -14,7 +14,8 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; class RefreshEntitlementsPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::REFRESH_ENTITLEMENTS_PACKET; @@ -26,11 +27,11 @@ public static function create() : self{ return new self; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ //NOOP } - protected function encodePayload(PacketSerializer $out) : void{ + protected function encodePayload(ByteBufferWriter $out) : void{ //NOOP } diff --git a/src/RemoveActorPacket.php b/src/RemoveActorPacket.php index 4398ca46..3bec4a24 100644 --- a/src/RemoveActorPacket.php +++ b/src/RemoveActorPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class RemoveActorPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::REMOVE_ACTOR_PACKET; @@ -30,12 +32,12 @@ public static function create(int $actorUniqueId) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorUniqueId = $in->getActorUniqueId(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorUniqueId = CommonTypes::getActorUniqueId($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorUniqueId($this->actorUniqueId); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorUniqueId($out, $this->actorUniqueId); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/RemoveObjectivePacket.php b/src/RemoveObjectivePacket.php index 063714f9..d6d83cc6 100644 --- a/src/RemoveObjectivePacket.php +++ b/src/RemoveObjectivePacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class RemoveObjectivePacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::REMOVE_OBJECTIVE_PACKET; @@ -30,12 +32,12 @@ public static function create(string $objectiveName) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->objectiveName = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->objectiveName = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->objectiveName); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->objectiveName); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/RemoveVolumeEntityPacket.php b/src/RemoveVolumeEntityPacket.php index e14cafa2..52c2ddcd 100644 --- a/src/RemoveVolumeEntityPacket.php +++ b/src/RemoveVolumeEntityPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; class RemoveVolumeEntityPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::REMOVE_VOLUME_ENTITY_PACKET; @@ -36,14 +38,14 @@ public function getEntityNetId() : int{ return $this->entityNetId; } public function getDimension() : int{ return $this->dimension; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->entityNetId = $in->getUnsignedVarInt(); - $this->dimension = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->entityNetId = VarInt::readUnsignedInt($in); + $this->dimension = VarInt::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt($this->entityNetId); - $out->putVarInt($this->dimension); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, $this->entityNetId); + VarInt::writeSignedInt($out, $this->dimension); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/RequestAbilityPacket.php b/src/RequestAbilityPacket.php index efae5629..5b45ce60 100644 --- a/src/RequestAbilityPacket.php +++ b/src/RequestAbilityPacket.php @@ -14,7 +14,12 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use function is_bool; use function is_float; @@ -49,15 +54,15 @@ public function getAbilityId() : int{ return $this->abilityId; } public function getAbilityValue() : float|bool{ return $this->abilityValue; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->abilityId = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->abilityId = VarInt::readSignedInt($in); - $valueType = $in->getByte(); + $valueType = Byte::readUnsigned($in); //what is the point of having a type ID if you just write all the types anyway ??? mojang ... //only one of these values is ever used; the other(s) are discarded - $boolValue = $in->getBool(); - $floatValue = $in->getLFloat(); + $boolValue = CommonTypes::getBool($in); + $floatValue = LE::readFloat($in); $this->abilityValue = match($valueType){ self::VALUE_TYPE_BOOL => $boolValue, @@ -66,17 +71,17 @@ protected function decodePayload(PacketSerializer $in) : void{ }; } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->abilityId); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->abilityId); [$valueType, $boolValue, $floatValue] = match(true){ is_bool($this->abilityValue) => [self::VALUE_TYPE_BOOL, $this->abilityValue, 0.0], is_float($this->abilityValue) => [self::VALUE_TYPE_FLOAT, false, $this->abilityValue], default => throw new \LogicException("Unreachable") }; - $out->putByte($valueType); - $out->putBool($boolValue); - $out->putLFloat($floatValue); + Byte::writeUnsigned($out, $valueType); + CommonTypes::putBool($out, $boolValue); + LE::writeFloat($out, $floatValue); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/RequestChunkRadiusPacket.php b/src/RequestChunkRadiusPacket.php index 18297fd6..649f983b 100644 --- a/src/RequestChunkRadiusPacket.php +++ b/src/RequestChunkRadiusPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; class RequestChunkRadiusPacket extends DataPacket implements ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::REQUEST_CHUNK_RADIUS_PACKET; @@ -32,14 +35,14 @@ public static function create(int $radius, int $maxRadius) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->radius = $in->getVarInt(); - $this->maxRadius = $in->getByte(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->radius = VarInt::readSignedInt($in); + $this->maxRadius = Byte::readUnsigned($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->radius); - $out->putByte($this->maxRadius); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->radius); + Byte::writeUnsigned($out, $this->maxRadius); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/RequestNetworkSettingsPacket.php b/src/RequestNetworkSettingsPacket.php index 1c5feef8..4cdd7f2b 100644 --- a/src/RequestNetworkSettingsPacket.php +++ b/src/RequestNetworkSettingsPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\BE; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; /** * This is the first packet sent in a game session. It contains the client's protocol version. @@ -41,12 +43,12 @@ public function canBeSentBeforeLogin() : bool{ public function getProtocolVersion() : int{ return $this->protocolVersion; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->protocolVersion = $in->getInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->protocolVersion = BE::readUnsignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putInt($this->protocolVersion); + protected function encodePayload(ByteBufferWriter $out) : void{ + BE::writeUnsignedInt($out, $this->protocolVersion); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/RequestPermissionsPacket.php b/src/RequestPermissionsPacket.php index af8f46d7..849e2253 100644 --- a/src/RequestPermissionsPacket.php +++ b/src/RequestPermissionsPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\types\PlayerPermissions; /** @@ -56,16 +59,16 @@ public function getPlayerPermission() : int{ return $this->playerPermission; } public function getCustomFlags() : int{ return $this->customFlags; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->targetActorUniqueId = $in->getLLong(); - $this->playerPermission = $in->getVarInt(); - $this->customFlags = $in->getLShort(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->targetActorUniqueId = LE::readSignedLong($in); + $this->playerPermission = VarInt::readSignedInt($in); + $this->customFlags = LE::readUnsignedShort($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLLong($this->targetActorUniqueId); - $out->putVarInt($this->playerPermission); - $out->putLShort($this->customFlags); + protected function encodePayload(ByteBufferWriter $out) : void{ + LE::writeSignedLong($out, $this->targetActorUniqueId); + VarInt::writeSignedInt($out, $this->playerPermission); + LE::writeUnsignedShort($out, $this->customFlags); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ResourcePackChunkDataPacket.php b/src/ResourcePackChunkDataPacket.php index c5e83972..c3c9dec5 100644 --- a/src/ResourcePackChunkDataPacket.php +++ b/src/ResourcePackChunkDataPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class ResourcePackChunkDataPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::RESOURCE_PACK_CHUNK_DATA_PACKET; @@ -36,18 +39,18 @@ public static function create(string $packId, int $chunkIndex, int $offset, stri return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->packId = $in->getString(); - $this->chunkIndex = $in->getLInt(); - $this->offset = $in->getLLong(); - $this->data = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->packId = CommonTypes::getString($in); + $this->chunkIndex = LE::readUnsignedInt($in); + $this->offset = LE::readUnsignedLong($in); + $this->data = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->packId); - $out->putLInt($this->chunkIndex); - $out->putLLong($this->offset); - $out->putString($this->data); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->packId); + LE::writeUnsignedInt($out, $this->chunkIndex); + LE::writeUnsignedLong($out, $this->offset); + CommonTypes::putString($out, $this->data); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ResourcePackChunkRequestPacket.php b/src/ResourcePackChunkRequestPacket.php index 72cd1b88..4dc4016f 100644 --- a/src/ResourcePackChunkRequestPacket.php +++ b/src/ResourcePackChunkRequestPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class ResourcePackChunkRequestPacket extends DataPacket implements ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::RESOURCE_PACK_CHUNK_REQUEST_PACKET; @@ -32,14 +35,14 @@ public static function create(string $packId, int $chunkIndex) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->packId = $in->getString(); - $this->chunkIndex = $in->getLInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->packId = CommonTypes::getString($in); + $this->chunkIndex = LE::readUnsignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->packId); - $out->putLInt($this->chunkIndex); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->packId); + LE::writeUnsignedInt($out, $this->chunkIndex); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ResourcePackClientResponsePacket.php b/src/ResourcePackClientResponsePacket.php index 099a9059..10fe96fb 100644 --- a/src/ResourcePackClientResponsePacket.php +++ b/src/ResourcePackClientResponsePacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use function count; class ResourcePackClientResponsePacket extends DataPacket implements ServerboundPacket{ @@ -40,20 +44,20 @@ public static function create(int $status, array $packIds) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->status = $in->getByte(); - $entryCount = $in->getLShort(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->status = Byte::readUnsigned($in); + $entryCount = LE::readUnsignedShort($in); $this->packIds = []; while($entryCount-- > 0){ - $this->packIds[] = $in->getString(); + $this->packIds[] = CommonTypes::getString($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->status); - $out->putLShort(count($this->packIds)); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->status); + LE::writeUnsignedShort($out, count($this->packIds)); foreach($this->packIds as $id){ - $out->putString($id); + CommonTypes::putString($out, $id); } } diff --git a/src/ResourcePackDataInfoPacket.php b/src/ResourcePackDataInfoPacket.php index c0092a3a..04d95015 100644 --- a/src/ResourcePackDataInfoPacket.php +++ b/src/ResourcePackDataInfoPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\resourcepacks\ResourcePackType; class ResourcePackDataInfoPacket extends DataPacket implements ClientboundPacket{ @@ -51,24 +55,24 @@ public static function create( return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->packId = $in->getString(); - $this->maxChunkSize = $in->getLInt(); - $this->chunkCount = $in->getLInt(); - $this->compressedPackSize = $in->getLLong(); - $this->sha256 = $in->getString(); - $this->isPremium = $in->getBool(); - $this->packType = $in->getByte(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->packId = CommonTypes::getString($in); + $this->maxChunkSize = LE::readUnsignedInt($in); + $this->chunkCount = LE::readUnsignedInt($in); + $this->compressedPackSize = LE::readUnsignedLong($in); + $this->sha256 = CommonTypes::getString($in); + $this->isPremium = CommonTypes::getBool($in); + $this->packType = Byte::readUnsigned($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->packId); - $out->putLInt($this->maxChunkSize); - $out->putLInt($this->chunkCount); - $out->putLLong($this->compressedPackSize); - $out->putString($this->sha256); - $out->putBool($this->isPremium); - $out->putByte($this->packType); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->packId); + LE::writeUnsignedInt($out, $this->maxChunkSize); + LE::writeUnsignedInt($out, $this->chunkCount); + LE::writeUnsignedLong($out, $this->compressedPackSize); + CommonTypes::putString($out, $this->sha256); + CommonTypes::putBool($out, $this->isPremium); + Byte::writeUnsigned($out, $this->packType); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ResourcePackStackPacket.php b/src/ResourcePackStackPacket.php index 285268ff..d0e37267 100644 --- a/src/ResourcePackStackPacket.php +++ b/src/ResourcePackStackPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\Experiments; use pocketmine\network\mcpe\protocol\types\resourcepacks\ResourcePackStackEntry; use function count; @@ -47,39 +50,39 @@ public static function create(array $resourcePackStack, array $behaviorPackStack return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->mustAccept = $in->getBool(); - $behaviorPackCount = $in->getUnsignedVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->mustAccept = CommonTypes::getBool($in); + $behaviorPackCount = VarInt::readUnsignedInt($in); while($behaviorPackCount-- > 0){ $this->behaviorPackStack[] = ResourcePackStackEntry::read($in); } - $resourcePackCount = $in->getUnsignedVarInt(); + $resourcePackCount = VarInt::readUnsignedInt($in); while($resourcePackCount-- > 0){ $this->resourcePackStack[] = ResourcePackStackEntry::read($in); } - $this->baseGameVersion = $in->getString(); + $this->baseGameVersion = CommonTypes::getString($in); $this->experiments = Experiments::read($in); - $this->useVanillaEditorPacks = $in->getBool(); + $this->useVanillaEditorPacks = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putBool($this->mustAccept); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putBool($out, $this->mustAccept); - $out->putUnsignedVarInt(count($this->behaviorPackStack)); + VarInt::writeUnsignedInt($out, count($this->behaviorPackStack)); foreach($this->behaviorPackStack as $entry){ $entry->write($out); } - $out->putUnsignedVarInt(count($this->resourcePackStack)); + VarInt::writeUnsignedInt($out, count($this->resourcePackStack)); foreach($this->resourcePackStack as $entry){ $entry->write($out); } - $out->putString($this->baseGameVersion); + CommonTypes::putString($out, $this->baseGameVersion); $this->experiments->write($out); - $out->putBool($this->useVanillaEditorPacks); + CommonTypes::putBool($out, $this->useVanillaEditorPacks); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ResourcePacksInfoPacket.php b/src/ResourcePacksInfoPacket.php index 23045191..d3029494 100644 --- a/src/ResourcePacksInfoPacket.php +++ b/src/ResourcePacksInfoPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\resourcepacks\ResourcePackInfoEntry; use Ramsey\Uuid\UuidInterface; use function count; @@ -61,28 +64,28 @@ public function getWorldTemplateVersion() : string{ return $this->worldTemplateV public function isForceDisablingVibrantVisuals() : bool{ return $this->forceDisableVibrantVisuals; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->mustAccept = $in->getBool(); - $this->hasAddons = $in->getBool(); - $this->hasScripts = $in->getBool(); - $this->forceDisableVibrantVisuals = $in->getBool(); - $this->worldTemplateId = $in->getUUID(); - $this->worldTemplateVersion = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->mustAccept = CommonTypes::getBool($in); + $this->hasAddons = CommonTypes::getBool($in); + $this->hasScripts = CommonTypes::getBool($in); + $this->forceDisableVibrantVisuals = CommonTypes::getBool($in); + $this->worldTemplateId = CommonTypes::getUUID($in); + $this->worldTemplateVersion = CommonTypes::getString($in); - $resourcePackCount = $in->getLShort(); + $resourcePackCount = LE::readUnsignedShort($in); while($resourcePackCount-- > 0){ $this->resourcePackEntries[] = ResourcePackInfoEntry::read($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putBool($this->mustAccept); - $out->putBool($this->hasAddons); - $out->putBool($this->hasScripts); - $out->putBool($this->forceDisableVibrantVisuals); - $out->putUUID($this->worldTemplateId); - $out->putString($this->worldTemplateVersion); - $out->putLShort(count($this->resourcePackEntries)); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putBool($out, $this->mustAccept); + CommonTypes::putBool($out, $this->hasAddons); + CommonTypes::putBool($out, $this->hasScripts); + CommonTypes::putBool($out, $this->forceDisableVibrantVisuals); + CommonTypes::putUUID($out, $this->worldTemplateId); + CommonTypes::putString($out, $this->worldTemplateVersion); + LE::writeUnsignedShort($out, count($this->resourcePackEntries)); foreach($this->resourcePackEntries as $entry){ $entry->write($out); } diff --git a/src/RespawnPacket.php b/src/RespawnPacket.php index e3974eb1..d14c32a1 100644 --- a/src/RespawnPacket.php +++ b/src/RespawnPacket.php @@ -14,8 +14,11 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class RespawnPacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::RESPAWN_PACKET; @@ -39,16 +42,16 @@ public static function create(Vector3 $position, int $respawnState, int $actorRu return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->position = $in->getVector3(); - $this->respawnState = $in->getByte(); - $this->actorRuntimeId = $in->getActorRuntimeId(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->position = CommonTypes::getVector3($in); + $this->respawnState = Byte::readUnsigned($in); + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVector3($this->position); - $out->putByte($this->respawnState); - $out->putActorRuntimeId($this->actorRuntimeId); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putVector3($out, $this->position); + Byte::writeUnsigned($out, $this->respawnState); + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ScriptMessagePacket.php b/src/ScriptMessagePacket.php index 9fa69a02..acdad5f0 100644 --- a/src/ScriptMessagePacket.php +++ b/src/ScriptMessagePacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class ScriptMessagePacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::SCRIPT_MESSAGE_PACKET; @@ -36,14 +38,14 @@ public function getMessageId() : string{ return $this->messageId; } public function getValue() : string{ return $this->value; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->messageId = $in->getString(); - $this->value = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->messageId = CommonTypes::getString($in); + $this->value = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->messageId); - $out->putString($this->value); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->messageId); + CommonTypes::putString($out, $this->value); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ServerPlayerPostMovePositionPacket.php b/src/ServerPlayerPostMovePositionPacket.php index 103e6c6f..8484e822 100644 --- a/src/ServerPlayerPostMovePositionPacket.php +++ b/src/ServerPlayerPostMovePositionPacket.php @@ -14,8 +14,10 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class ServerPlayerPostMovePositionPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::SERVER_PLAYER_POST_MOVE_POSITION_PACKET; @@ -33,12 +35,12 @@ public static function create(Vector3 $position) : self{ public function getPosition() : Vector3{ return $this->position; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->position = $in->getVector3(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->position = CommonTypes::getVector3($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVector3($this->position); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putVector3($out, $this->position); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ServerScriptDebugDrawerPacket.php b/src/ServerScriptDebugDrawerPacket.php index 2a2f59e8..f0a8164c 100644 --- a/src/ServerScriptDebugDrawerPacket.php +++ b/src/ServerScriptDebugDrawerPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\types\PacketShapeData; use function count; @@ -44,15 +46,15 @@ public static function create(array $shapes) : self{ */ public function getShapes() : array{ return $this->shapes; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ $this->shapes = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ $this->shapes[] = PacketShapeData::read($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->shapes)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->shapes)); foreach($this->shapes as $shape){ $shape->write($out); } diff --git a/src/ServerSettingsRequestPacket.php b/src/ServerSettingsRequestPacket.php index 45043134..dce58607 100644 --- a/src/ServerSettingsRequestPacket.php +++ b/src/ServerSettingsRequestPacket.php @@ -14,7 +14,8 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; class ServerSettingsRequestPacket extends DataPacket implements ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::SERVER_SETTINGS_REQUEST_PACKET; @@ -26,11 +27,11 @@ public static function create() : self{ return new self; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ //No payload } - protected function encodePayload(PacketSerializer $out) : void{ + protected function encodePayload(ByteBufferWriter $out) : void{ //No payload } diff --git a/src/ServerSettingsResponsePacket.php b/src/ServerSettingsResponsePacket.php index c6032a35..1a334458 100644 --- a/src/ServerSettingsResponsePacket.php +++ b/src/ServerSettingsResponsePacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class ServerSettingsResponsePacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::SERVER_SETTINGS_RESPONSE_PACKET; @@ -32,14 +35,14 @@ public static function create(int $formId, string $formData) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->formId = $in->getUnsignedVarInt(); - $this->formData = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->formId = VarInt::readUnsignedInt($in); + $this->formData = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt($this->formId); - $out->putString($this->formData); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, $this->formId); + CommonTypes::putString($out, $this->formData); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ServerStatsPacket.php b/src/ServerStatsPacket.php index 8e66cfc5..ebae6085 100644 --- a/src/ServerStatsPacket.php +++ b/src/ServerStatsPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; /** * Relays server performance statistics to the client. @@ -40,14 +42,14 @@ public function getServerTime() : float{ return $this->serverTime; } public function getNetworkTime() : float{ return $this->networkTime; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->serverTime = $in->getLFloat(); - $this->networkTime = $in->getLFloat(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->serverTime = LE::readFloat($in); + $this->networkTime = LE::readFloat($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLFloat($this->serverTime); - $out->putLFloat($this->networkTime); + protected function encodePayload(ByteBufferWriter $out) : void{ + LE::writeFloat($out, $this->serverTime); + LE::writeFloat($out, $this->networkTime); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ServerToClientHandshakePacket.php b/src/ServerToClientHandshakePacket.php index d985828f..1cb93b7e 100644 --- a/src/ServerToClientHandshakePacket.php +++ b/src/ServerToClientHandshakePacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class ServerToClientHandshakePacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::SERVER_TO_CLIENT_HANDSHAKE_PACKET; @@ -35,12 +37,12 @@ public function canBeSentBeforeLogin() : bool{ return true; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->jwt = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->jwt = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->jwt); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->jwt); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ServerboundDiagnosticsPacket.php b/src/ServerboundDiagnosticsPacket.php index d2681839..1cfe9344 100644 --- a/src/ServerboundDiagnosticsPacket.php +++ b/src/ServerboundDiagnosticsPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; class ServerboundDiagnosticsPacket extends DataPacket implements ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::SERVERBOUND_DIAGNOSTICS_PACKET; @@ -74,28 +76,28 @@ public function getAvgRemainderTimePercent() : float{ return $this->avgRemainder public function getAvgUnaccountedTimePercent() : float{ return $this->avgUnaccountedTimePercent; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->avgFps = $in->getLFloat(); - $this->avgServerSimTickTimeMS = $in->getLFloat(); - $this->avgClientSimTickTimeMS = $in->getLFloat(); - $this->avgBeginFrameTimeMS = $in->getLFloat(); - $this->avgInputTimeMS = $in->getLFloat(); - $this->avgRenderTimeMS = $in->getLFloat(); - $this->avgEndFrameTimeMS = $in->getLFloat(); - $this->avgRemainderTimePercent = $in->getLFloat(); - $this->avgUnaccountedTimePercent = $in->getLFloat(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->avgFps = LE::readFloat($in); + $this->avgServerSimTickTimeMS = LE::readFloat($in); + $this->avgClientSimTickTimeMS = LE::readFloat($in); + $this->avgBeginFrameTimeMS = LE::readFloat($in); + $this->avgInputTimeMS = LE::readFloat($in); + $this->avgRenderTimeMS = LE::readFloat($in); + $this->avgEndFrameTimeMS = LE::readFloat($in); + $this->avgRemainderTimePercent = LE::readFloat($in); + $this->avgUnaccountedTimePercent = LE::readFloat($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLFloat($this->avgFps); - $out->putLFloat($this->avgServerSimTickTimeMS); - $out->putLFloat($this->avgClientSimTickTimeMS); - $out->putLFloat($this->avgBeginFrameTimeMS); - $out->putLFloat($this->avgInputTimeMS); - $out->putLFloat($this->avgRenderTimeMS); - $out->putLFloat($this->avgEndFrameTimeMS); - $out->putLFloat($this->avgRemainderTimePercent); - $out->putLFloat($this->avgUnaccountedTimePercent); + protected function encodePayload(ByteBufferWriter $out) : void{ + LE::writeFloat($out, $this->avgFps); + LE::writeFloat($out, $this->avgServerSimTickTimeMS); + LE::writeFloat($out, $this->avgClientSimTickTimeMS); + LE::writeFloat($out, $this->avgBeginFrameTimeMS); + LE::writeFloat($out, $this->avgInputTimeMS); + LE::writeFloat($out, $this->avgRenderTimeMS); + LE::writeFloat($out, $this->avgEndFrameTimeMS); + LE::writeFloat($out, $this->avgRemainderTimePercent); + LE::writeFloat($out, $this->avgUnaccountedTimePercent); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ServerboundLoadingScreenPacket.php b/src/ServerboundLoadingScreenPacket.php index e16e9821..36f73cb5 100644 --- a/src/ServerboundLoadingScreenPacket.php +++ b/src/ServerboundLoadingScreenPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\hud\LoadingScreenType; class ServerboundLoadingScreenPacket extends DataPacket implements ServerboundPacket{ @@ -37,14 +41,14 @@ public function getLoadingScreenType() : LoadingScreenType{ return $this->loadin public function getLoadingScreenId() : ?int{ return $this->loadingScreenId; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->loadingScreenType = LoadingScreenType::fromPacket($in->getVarInt()); - $this->loadingScreenId = $in->readOptional(fn() => $in->getLInt()); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->loadingScreenType = LoadingScreenType::fromPacket(VarInt::readSignedInt($in)); + $this->loadingScreenId = CommonTypes::readOptional($in, LE::readUnsignedInt(...)); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->loadingScreenType->value); - $out->writeOptional($this->loadingScreenId, $out->putLInt(...)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->loadingScreenType->value); + CommonTypes::writeOptional($out, $this->loadingScreenId, LE::writeUnsignedInt(...)); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SetActorDataPacket.php b/src/SetActorDataPacket.php index db698af2..efc89543 100644 --- a/src/SetActorDataPacket.php +++ b/src/SetActorDataPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\entity\MetadataProperty; use pocketmine\network\mcpe\protocol\types\entity\PropertySyncData; @@ -44,18 +47,18 @@ public static function create(int $actorRuntimeId, array $metadata, PropertySync return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorRuntimeId = $in->getActorRuntimeId(); - $this->metadata = $in->getEntityMetadata(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->metadata = CommonTypes::getEntityMetadata($in); $this->syncedProperties = PropertySyncData::read($in); - $this->tick = $in->getUnsignedVarLong(); + $this->tick = VarInt::readUnsignedLong($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorRuntimeId($this->actorRuntimeId); - $out->putEntityMetadata($this->metadata); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + CommonTypes::putEntityMetadata($out, $this->metadata); $this->syncedProperties->write($out); - $out->putUnsignedVarLong($this->tick); + VarInt::writeUnsignedLong($out, $this->tick); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SetActorLinkPacket.php b/src/SetActorLinkPacket.php index 47159d81..425e4e28 100644 --- a/src/SetActorLinkPacket.php +++ b/src/SetActorLinkPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\entity\EntityLink; class SetActorLinkPacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ @@ -31,12 +33,12 @@ public static function create(EntityLink $link) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->link = $in->getEntityLink(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->link = CommonTypes::getEntityLink($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putEntityLink($this->link); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putEntityLink($out, $this->link); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SetActorMotionPacket.php b/src/SetActorMotionPacket.php index 364d6f98..a4efab42 100644 --- a/src/SetActorMotionPacket.php +++ b/src/SetActorMotionPacket.php @@ -14,8 +14,11 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class SetActorMotionPacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::SET_ACTOR_MOTION_PACKET; @@ -35,16 +38,16 @@ public static function create(int $actorRuntimeId, Vector3 $motion, int $tick) : return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorRuntimeId = $in->getActorRuntimeId(); - $this->motion = $in->getVector3(); - $this->tick = $in->getUnsignedVarLong(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->motion = CommonTypes::getVector3($in); + $this->tick = VarInt::readUnsignedLong($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorRuntimeId($this->actorRuntimeId); - $out->putVector3($this->motion); - $out->putUnsignedVarLong($this->tick); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + CommonTypes::putVector3($out, $this->motion); + VarInt::writeUnsignedLong($out, $this->tick); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SetCommandsEnabledPacket.php b/src/SetCommandsEnabledPacket.php index a01302aa..6d5a554e 100644 --- a/src/SetCommandsEnabledPacket.php +++ b/src/SetCommandsEnabledPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class SetCommandsEnabledPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::SET_COMMANDS_ENABLED_PACKET; @@ -30,12 +32,12 @@ public static function create(bool $enabled) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->enabled = $in->getBool(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->enabled = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putBool($this->enabled); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putBool($out, $this->enabled); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SetDefaultGameTypePacket.php b/src/SetDefaultGameTypePacket.php index 0aeb2f3d..7458991d 100644 --- a/src/SetDefaultGameTypePacket.php +++ b/src/SetDefaultGameTypePacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; class SetDefaultGameTypePacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::SET_DEFAULT_GAME_TYPE_PACKET; @@ -30,12 +32,12 @@ public static function create(int $gamemode) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->gamemode = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->gamemode = VarInt::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt($this->gamemode); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->gamemode); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SetDifficultyPacket.php b/src/SetDifficultyPacket.php index 268836a7..549d60a3 100644 --- a/src/SetDifficultyPacket.php +++ b/src/SetDifficultyPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; class SetDifficultyPacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::SET_DIFFICULTY_PACKET; @@ -30,12 +32,12 @@ public static function create(int $difficulty) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->difficulty = $in->getUnsignedVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->difficulty = VarInt::readUnsignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt($this->difficulty); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, $this->difficulty); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SetDisplayObjectivePacket.php b/src/SetDisplayObjectivePacket.php index 8a46749f..486e7795 100644 --- a/src/SetDisplayObjectivePacket.php +++ b/src/SetDisplayObjectivePacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class SetDisplayObjectivePacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::SET_DISPLAY_OBJECTIVE_PACKET; @@ -45,20 +48,20 @@ public static function create(string $displaySlot, string $objectiveName, string return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->displaySlot = $in->getString(); - $this->objectiveName = $in->getString(); - $this->displayName = $in->getString(); - $this->criteriaName = $in->getString(); - $this->sortOrder = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->displaySlot = CommonTypes::getString($in); + $this->objectiveName = CommonTypes::getString($in); + $this->displayName = CommonTypes::getString($in); + $this->criteriaName = CommonTypes::getString($in); + $this->sortOrder = VarInt::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->displaySlot); - $out->putString($this->objectiveName); - $out->putString($this->displayName); - $out->putString($this->criteriaName); - $out->putVarInt($this->sortOrder); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->displaySlot); + CommonTypes::putString($out, $this->objectiveName); + CommonTypes::putString($out, $this->displayName); + CommonTypes::putString($out, $this->criteriaName); + VarInt::writeSignedInt($out, $this->sortOrder); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SetHealthPacket.php b/src/SetHealthPacket.php index 559aa896..d0cd2d30 100644 --- a/src/SetHealthPacket.php +++ b/src/SetHealthPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; class SetHealthPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::SET_HEALTH_PACKET; @@ -30,12 +32,12 @@ public static function create(int $health) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->health = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->health = VarInt::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->health); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->health); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SetHudPacket.php b/src/SetHudPacket.php index 7a2094ae..ee463aa4 100644 --- a/src/SetHudPacket.php +++ b/src/SetHudPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\types\hud\HudElement; use pocketmine\network\mcpe\protocol\types\hud\HudVisibility; use function count; @@ -42,20 +44,20 @@ public function getHudElements() : array{ return $this->hudElements; } public function getVisibility() : HudVisibility{ return $this->visibility; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ $this->hudElements = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ - $this->hudElements[] = HudElement::fromPacket($in->getVarInt()); + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ + $this->hudElements[] = HudElement::fromPacket(VarInt::readSignedInt($in)); } - $this->visibility = HudVisibility::fromPacket($in->getVarInt()); + $this->visibility = HudVisibility::fromPacket(VarInt::readSignedInt($in)); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->hudElements)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->hudElements)); foreach($this->hudElements as $element){ - $out->putVarInt($element->value); + VarInt::writeSignedInt($out, $element->value); } - $out->putVarInt($this->visibility->value); + VarInt::writeSignedInt($out, $this->visibility->value); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SetLastHurtByPacket.php b/src/SetLastHurtByPacket.php index 6d6c626b..3fa81a73 100644 --- a/src/SetLastHurtByPacket.php +++ b/src/SetLastHurtByPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; class SetLastHurtByPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::SET_LAST_HURT_BY_PACKET; @@ -30,12 +32,12 @@ public static function create(int $entityTypeId) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->entityTypeId = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->entityTypeId = VarInt::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->entityTypeId); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->entityTypeId); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SetLocalPlayerAsInitializedPacket.php b/src/SetLocalPlayerAsInitializedPacket.php index 1bc327eb..7f9f2e6f 100644 --- a/src/SetLocalPlayerAsInitializedPacket.php +++ b/src/SetLocalPlayerAsInitializedPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class SetLocalPlayerAsInitializedPacket extends DataPacket implements ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::SET_LOCAL_PLAYER_AS_INITIALIZED_PACKET; @@ -30,12 +32,12 @@ public static function create(int $actorRuntimeId) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorRuntimeId = $in->getActorRuntimeId(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorRuntimeId($this->actorRuntimeId); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SetPlayerGameTypePacket.php b/src/SetPlayerGameTypePacket.php index a6dc8539..ee961c98 100644 --- a/src/SetPlayerGameTypePacket.php +++ b/src/SetPlayerGameTypePacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; class SetPlayerGameTypePacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::SET_PLAYER_GAME_TYPE_PACKET; @@ -30,12 +32,12 @@ public static function create(int $gamemode) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->gamemode = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->gamemode = VarInt::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->gamemode); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->gamemode); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SetPlayerInventoryOptionsPacket.php b/src/SetPlayerInventoryOptionsPacket.php index 7fab1cd6..cc686b75 100644 --- a/src/SetPlayerInventoryOptionsPacket.php +++ b/src/SetPlayerInventoryOptionsPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\inventory\InventoryLayout; use pocketmine\network\mcpe\protocol\types\inventory\InventoryLeftTab; use pocketmine\network\mcpe\protocol\types\inventory\InventoryRightTab; @@ -51,20 +54,20 @@ public function getInventoryLayout() : InventoryLayout{ return $this->inventoryL public function getCraftingLayout() : InventoryLayout{ return $this->craftingLayout; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->leftTab = InventoryLeftTab::fromPacket($in->getVarInt()); - $this->rightTab = InventoryRightTab::fromPacket($in->getVarInt()); - $this->filtering = $in->getBool(); - $this->inventoryLayout = InventoryLayout::fromPacket($in->getVarInt()); - $this->craftingLayout = InventoryLayout::fromPacket($in->getVarInt()); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->leftTab = InventoryLeftTab::fromPacket(VarInt::readSignedInt($in)); + $this->rightTab = InventoryRightTab::fromPacket(VarInt::readSignedInt($in)); + $this->filtering = CommonTypes::getBool($in); + $this->inventoryLayout = InventoryLayout::fromPacket(VarInt::readSignedInt($in)); + $this->craftingLayout = InventoryLayout::fromPacket(VarInt::readSignedInt($in)); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->leftTab->value); - $out->putVarInt($this->rightTab->value); - $out->putBool($this->filtering); - $out->putVarInt($this->inventoryLayout->value); - $out->putVarInt($this->craftingLayout->value); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->leftTab->value); + VarInt::writeSignedInt($out, $this->rightTab->value); + CommonTypes::putBool($out, $this->filtering); + VarInt::writeSignedInt($out, $this->inventoryLayout->value); + VarInt::writeSignedInt($out, $this->craftingLayout->value); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SetScorePacket.php b/src/SetScorePacket.php index fc46bb7f..4bac0d20 100644 --- a/src/SetScorePacket.php +++ b/src/SetScorePacket.php @@ -14,7 +14,12 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\ScorePacketEntry; use function count; @@ -39,22 +44,22 @@ public static function create(int $type, array $entries) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->type = $in->getByte(); - for($i = 0, $i2 = $in->getUnsignedVarInt(); $i < $i2; ++$i){ + protected function decodePayload(ByteBufferReader $in) : void{ + $this->type = Byte::readUnsigned($in); + for($i = 0, $i2 = VarInt::readUnsignedInt($in); $i < $i2; ++$i){ $entry = new ScorePacketEntry(); - $entry->scoreboardId = $in->getVarLong(); - $entry->objectiveName = $in->getString(); - $entry->score = $in->getLInt(); + $entry->scoreboardId = VarInt::readSignedLong($in); + $entry->objectiveName = CommonTypes::getString($in); + $entry->score = LE::readSignedInt($in); if($this->type !== self::TYPE_REMOVE){ - $entry->type = $in->getByte(); + $entry->type = Byte::readUnsigned($in); switch($entry->type){ case ScorePacketEntry::TYPE_PLAYER: case ScorePacketEntry::TYPE_ENTITY: - $entry->actorUniqueId = $in->getActorUniqueId(); + $entry->actorUniqueId = CommonTypes::getActorUniqueId($in); break; case ScorePacketEntry::TYPE_FAKE_PLAYER: - $entry->customName = $in->getString(); + $entry->customName = CommonTypes::getString($in); break; default: throw new PacketDecodeException("Unknown entry type $entry->type"); @@ -64,22 +69,22 @@ protected function decodePayload(PacketSerializer $in) : void{ } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->type); - $out->putUnsignedVarInt(count($this->entries)); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->type); + VarInt::writeUnsignedInt($out, count($this->entries)); foreach($this->entries as $entry){ - $out->putVarLong($entry->scoreboardId); - $out->putString($entry->objectiveName); - $out->putLInt($entry->score); + VarInt::writeSignedLong($out, $entry->scoreboardId); + CommonTypes::putString($out, $entry->objectiveName); + LE::writeSignedInt($out, $entry->score); if($this->type !== self::TYPE_REMOVE){ - $out->putByte($entry->type); + Byte::writeUnsigned($out, $entry->type); switch($entry->type){ case ScorePacketEntry::TYPE_PLAYER: case ScorePacketEntry::TYPE_ENTITY: - $out->putActorUniqueId($entry->actorUniqueId); + CommonTypes::putActorUniqueId($out, $entry->actorUniqueId); break; case ScorePacketEntry::TYPE_FAKE_PLAYER: - $out->putString($entry->customName); + CommonTypes::putString($out, $entry->customName); break; default: throw new \InvalidArgumentException("Unknown entry type $entry->type"); diff --git a/src/SetScoreboardIdentityPacket.php b/src/SetScoreboardIdentityPacket.php index 717d52bd..77723fa0 100644 --- a/src/SetScoreboardIdentityPacket.php +++ b/src/SetScoreboardIdentityPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\ScoreboardIdentityPacketEntry; use function count; @@ -39,26 +43,26 @@ public static function create(int $type, array $entries) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->type = $in->getByte(); - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + protected function decodePayload(ByteBufferReader $in) : void{ + $this->type = Byte::readUnsigned($in); + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $entry = new ScoreboardIdentityPacketEntry(); - $entry->scoreboardId = $in->getVarLong(); + $entry->scoreboardId = VarInt::readSignedLong($in); if($this->type === self::TYPE_REGISTER_IDENTITY){ - $entry->actorUniqueId = $in->getActorUniqueId(); + $entry->actorUniqueId = CommonTypes::getActorUniqueId($in); } $this->entries[] = $entry; } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->type); - $out->putUnsignedVarInt(count($this->entries)); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->type); + VarInt::writeUnsignedInt($out, count($this->entries)); foreach($this->entries as $entry){ - $out->putVarLong($entry->scoreboardId); + VarInt::writeSignedLong($out, $entry->scoreboardId); if($this->type === self::TYPE_REGISTER_IDENTITY){ - $out->putActorUniqueId($entry->actorUniqueId); + CommonTypes::putActorUniqueId($out, $entry->actorUniqueId); } } } diff --git a/src/SetSpawnPositionPacket.php b/src/SetSpawnPositionPacket.php index d8427716..f4a6a577 100644 --- a/src/SetSpawnPositionPacket.php +++ b/src/SetSpawnPositionPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; use pocketmine\utils\Limits; @@ -54,18 +57,18 @@ public static function worldSpawn(BlockPosition $spawnPosition, int $dimension) return self::create(self::TYPE_WORLD_SPAWN, $spawnPosition, $dimension, new BlockPosition(Limits::INT32_MIN, Limits::INT32_MIN, Limits::INT32_MIN)); } - protected function decodePayload(PacketSerializer $in) : void{ - $this->spawnType = $in->getVarInt(); - $this->spawnPosition = $in->getBlockPosition(); - $this->dimension = $in->getVarInt(); - $this->causingBlockPosition = $in->getBlockPosition(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->spawnType = VarInt::readSignedInt($in); + $this->spawnPosition = CommonTypes::getBlockPosition($in); + $this->dimension = VarInt::readSignedInt($in); + $this->causingBlockPosition = CommonTypes::getBlockPosition($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->spawnType); - $out->putBlockPosition($this->spawnPosition); - $out->putVarInt($this->dimension); - $out->putBlockPosition($this->causingBlockPosition); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->spawnType); + CommonTypes::putBlockPosition($out, $this->spawnPosition); + VarInt::writeSignedInt($out, $this->dimension); + CommonTypes::putBlockPosition($out, $this->causingBlockPosition); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SetTimePacket.php b/src/SetTimePacket.php index 58cca16d..c0e6d61b 100644 --- a/src/SetTimePacket.php +++ b/src/SetTimePacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; class SetTimePacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::SET_TIME_PACKET; @@ -27,12 +29,12 @@ public static function create(int $time) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->time = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->time = VarInt::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->time); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->time); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SetTitlePacket.php b/src/SetTitlePacket.php index e8be6ccb..d7fa41a7 100644 --- a/src/SetTitlePacket.php +++ b/src/SetTitlePacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class SetTitlePacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::SET_TITLE_PACKET; @@ -63,26 +66,26 @@ public static function create( return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->type = $in->getVarInt(); - $this->text = $in->getString(); - $this->fadeInTime = $in->getVarInt(); - $this->stayTime = $in->getVarInt(); - $this->fadeOutTime = $in->getVarInt(); - $this->xuid = $in->getString(); - $this->platformOnlineId = $in->getString(); - $this->filteredTitleText = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->type = VarInt::readSignedInt($in); + $this->text = CommonTypes::getString($in); + $this->fadeInTime = VarInt::readSignedInt($in); + $this->stayTime = VarInt::readSignedInt($in); + $this->fadeOutTime = VarInt::readSignedInt($in); + $this->xuid = CommonTypes::getString($in); + $this->platformOnlineId = CommonTypes::getString($in); + $this->filteredTitleText = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->type); - $out->putString($this->text); - $out->putVarInt($this->fadeInTime); - $out->putVarInt($this->stayTime); - $out->putVarInt($this->fadeOutTime); - $out->putString($this->xuid); - $out->putString($this->platformOnlineId); - $out->putString($this->filteredTitleText); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->type); + CommonTypes::putString($out, $this->text); + VarInt::writeSignedInt($out, $this->fadeInTime); + VarInt::writeSignedInt($out, $this->stayTime); + VarInt::writeSignedInt($out, $this->fadeOutTime); + CommonTypes::putString($out, $this->xuid); + CommonTypes::putString($out, $this->platformOnlineId); + CommonTypes::putString($out, $this->filteredTitleText); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SettingsCommandPacket.php b/src/SettingsCommandPacket.php index b0ce8353..6e7a9aa9 100644 --- a/src/SettingsCommandPacket.php +++ b/src/SettingsCommandPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class SettingsCommandPacket extends DataPacket implements ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::SETTINGS_COMMAND_PACKET; @@ -40,14 +42,14 @@ public function getSuppressOutput() : bool{ return $this->suppressOutput; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->command = $in->getString(); - $this->suppressOutput = $in->getBool(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->command = CommonTypes::getString($in); + $this->suppressOutput = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->command); - $out->putBool($this->suppressOutput); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->command); + CommonTypes::putBool($out, $this->suppressOutput); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ShowCreditsPacket.php b/src/ShowCreditsPacket.php index 086efe9c..022a7368 100644 --- a/src/ShowCreditsPacket.php +++ b/src/ShowCreditsPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class ShowCreditsPacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::SHOW_CREDITS_PACKET; @@ -35,14 +38,14 @@ public static function create(int $playerActorRuntimeId, int $status) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->playerActorRuntimeId = $in->getActorRuntimeId(); - $this->status = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->playerActorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->status = VarInt::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorRuntimeId($this->playerActorRuntimeId); - $out->putVarInt($this->status); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->playerActorRuntimeId); + VarInt::writeSignedInt($out, $this->status); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ShowProfilePacket.php b/src/ShowProfilePacket.php index 4ad834e1..adada116 100644 --- a/src/ShowProfilePacket.php +++ b/src/ShowProfilePacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class ShowProfilePacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::SHOW_PROFILE_PACKET; @@ -30,12 +32,12 @@ public static function create(string $xuid) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->xuid = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->xuid = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->xuid); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->xuid); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ShowStoreOfferPacket.php b/src/ShowStoreOfferPacket.php index bb063b4c..0dbb7102 100644 --- a/src/ShowStoreOfferPacket.php +++ b/src/ShowStoreOfferPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\ShowStoreOfferRedirectType; class ShowStoreOfferPacket extends DataPacket implements ClientboundPacket{ @@ -33,14 +36,14 @@ public static function create(string $offerId, ShowStoreOfferRedirectType $redir return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->offerId = $in->getString(); - $this->redirectType = ShowStoreOfferRedirectType::fromPacket($in->getByte()); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->offerId = CommonTypes::getString($in); + $this->redirectType = ShowStoreOfferRedirectType::fromPacket(Byte::readUnsigned($in)); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->offerId); - $out->putByte($this->redirectType->value); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->offerId); + Byte::writeUnsigned($out, $this->redirectType->value); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SimpleEventPacket.php b/src/SimpleEventPacket.php index 006706a4..5d57a9c2 100644 --- a/src/SimpleEventPacket.php +++ b/src/SimpleEventPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; class SimpleEventPacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::SIMPLE_EVENT_PACKET; @@ -34,12 +36,12 @@ public static function create(int $eventType) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->eventType = $in->getLShort(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->eventType = LE::readUnsignedShort($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLShort($this->eventType); + protected function encodePayload(ByteBufferWriter $out) : void{ + LE::writeUnsignedShort($out, $this->eventType); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SimulationTypePacket.php b/src/SimulationTypePacket.php index a0a04e13..29919119 100644 --- a/src/SimulationTypePacket.php +++ b/src/SimulationTypePacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; class SimulationTypePacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::SIMULATION_TYPE_PACKET; @@ -36,12 +38,12 @@ public static function create(int $type) : self{ public function getType() : int{ return $this->type; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->type = $in->getByte(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->type = Byte::readUnsigned($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->type); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->type); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SpawnExperienceOrbPacket.php b/src/SpawnExperienceOrbPacket.php index 7e2a5233..53868c88 100644 --- a/src/SpawnExperienceOrbPacket.php +++ b/src/SpawnExperienceOrbPacket.php @@ -14,8 +14,11 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class SpawnExperienceOrbPacket extends DataPacket implements ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::SPAWN_EXPERIENCE_ORB_PACKET; @@ -33,14 +36,14 @@ public static function create(Vector3 $position, int $amount) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->position = $in->getVector3(); - $this->amount = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->position = CommonTypes::getVector3($in); + $this->amount = VarInt::readSignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVector3($this->position); - $out->putVarInt($this->amount); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putVector3($out, $this->position); + VarInt::writeSignedInt($out, $this->amount); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SpawnParticleEffectPacket.php b/src/SpawnParticleEffectPacket.php index bd9f6477..c0f626d5 100644 --- a/src/SpawnParticleEffectPacket.php +++ b/src/SpawnParticleEffectPacket.php @@ -14,8 +14,11 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\DimensionIds; class SpawnParticleEffectPacket extends DataPacket implements ClientboundPacket{ @@ -40,22 +43,22 @@ public static function create(int $dimensionId, int $actorUniqueId, Vector3 $pos return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->dimensionId = $in->getByte(); - $this->actorUniqueId = $in->getActorUniqueId(); - $this->position = $in->getVector3(); - $this->particleName = $in->getString(); - $this->molangVariablesJson = $in->getBool() ? $in->getString() : null; + protected function decodePayload(ByteBufferReader $in) : void{ + $this->dimensionId = Byte::readUnsigned($in); + $this->actorUniqueId = CommonTypes::getActorUniqueId($in); + $this->position = CommonTypes::getVector3($in); + $this->particleName = CommonTypes::getString($in); + $this->molangVariablesJson = CommonTypes::getBool($in) ? CommonTypes::getString($in) : null; } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->dimensionId); - $out->putActorUniqueId($this->actorUniqueId); - $out->putVector3($this->position); - $out->putString($this->particleName); - $out->putBool($this->molangVariablesJson !== null); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->dimensionId); + CommonTypes::putActorUniqueId($out, $this->actorUniqueId); + CommonTypes::putVector3($out, $this->position); + CommonTypes::putString($out, $this->particleName); + CommonTypes::putBool($out, $this->molangVariablesJson !== null); if($this->molangVariablesJson !== null){ - $out->putString($this->molangVariablesJson); + CommonTypes::putString($out, $this->molangVariablesJson); } } diff --git a/src/StartGamePacket.php b/src/StartGamePacket.php index e51c3685..7c6b9adc 100644 --- a/src/StartGamePacket.php +++ b/src/StartGamePacket.php @@ -14,9 +14,13 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; use pocketmine\math\Vector3; use pocketmine\nbt\tag\CompoundTag; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPaletteEntry; use pocketmine\network\mcpe\protocol\types\CacheableNbt; use pocketmine\network\mcpe\protocol\types\LevelSettings; @@ -133,82 +137,82 @@ public static function create( return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorUniqueId = $in->getActorUniqueId(); - $this->actorRuntimeId = $in->getActorRuntimeId(); - $this->playerGamemode = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorUniqueId = CommonTypes::getActorUniqueId($in); + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->playerGamemode = VarInt::readSignedInt($in); - $this->playerPosition = $in->getVector3(); + $this->playerPosition = CommonTypes::getVector3($in); - $this->pitch = $in->getLFloat(); - $this->yaw = $in->getLFloat(); + $this->pitch = LE::readFloat($in); + $this->yaw = LE::readFloat($in); $this->levelSettings = LevelSettings::read($in); - $this->levelId = $in->getString(); - $this->worldName = $in->getString(); - $this->premiumWorldTemplateId = $in->getString(); - $this->isTrial = $in->getBool(); + $this->levelId = CommonTypes::getString($in); + $this->worldName = CommonTypes::getString($in); + $this->premiumWorldTemplateId = CommonTypes::getString($in); + $this->isTrial = CommonTypes::getBool($in); $this->playerMovementSettings = PlayerMovementSettings::read($in); - $this->currentTick = $in->getLLong(); + $this->currentTick = LE::readUnsignedLong($in); - $this->enchantmentSeed = $in->getVarInt(); + $this->enchantmentSeed = VarInt::readSignedInt($in); $this->blockPalette = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ - $blockName = $in->getString(); - $state = $in->getNbtCompoundRoot(); + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ + $blockName = CommonTypes::getString($in); + $state = CommonTypes::getNbtCompoundRoot($in); $this->blockPalette[] = new BlockPaletteEntry($blockName, new CacheableNbt($state)); } - $this->multiplayerCorrelationId = $in->getString(); - $this->enableNewInventorySystem = $in->getBool(); - $this->serverSoftwareVersion = $in->getString(); - $this->playerActorProperties = new CacheableNbt($in->getNbtCompoundRoot()); - $this->blockPaletteChecksum = $in->getLLong(); - $this->worldTemplateId = $in->getUUID(); - $this->enableClientSideChunkGeneration = $in->getBool(); - $this->blockNetworkIdsAreHashes = $in->getBool(); - $this->enableTickDeathSystems = $in->getBool(); + $this->multiplayerCorrelationId = CommonTypes::getString($in); + $this->enableNewInventorySystem = CommonTypes::getBool($in); + $this->serverSoftwareVersion = CommonTypes::getString($in); + $this->playerActorProperties = new CacheableNbt(CommonTypes::getNbtCompoundRoot($in)); + $this->blockPaletteChecksum = LE::readUnsignedLong($in); + $this->worldTemplateId = CommonTypes::getUUID($in); + $this->enableClientSideChunkGeneration = CommonTypes::getBool($in); + $this->blockNetworkIdsAreHashes = CommonTypes::getBool($in); + $this->enableTickDeathSystems = CommonTypes::getBool($in); $this->networkPermissions = NetworkPermissions::decode($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorUniqueId($this->actorUniqueId); - $out->putActorRuntimeId($this->actorRuntimeId); - $out->putVarInt($this->playerGamemode); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorUniqueId($out, $this->actorUniqueId); + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + VarInt::writeSignedInt($out, $this->playerGamemode); - $out->putVector3($this->playerPosition); + CommonTypes::putVector3($out, $this->playerPosition); - $out->putLFloat($this->pitch); - $out->putLFloat($this->yaw); + LE::writeFloat($out, $this->pitch); + LE::writeFloat($out, $this->yaw); $this->levelSettings->write($out); - $out->putString($this->levelId); - $out->putString($this->worldName); - $out->putString($this->premiumWorldTemplateId); - $out->putBool($this->isTrial); + CommonTypes::putString($out, $this->levelId); + CommonTypes::putString($out, $this->worldName); + CommonTypes::putString($out, $this->premiumWorldTemplateId); + CommonTypes::putBool($out, $this->isTrial); $this->playerMovementSettings->write($out); - $out->putLLong($this->currentTick); + LE::writeUnsignedLong($out, $this->currentTick); - $out->putVarInt($this->enchantmentSeed); + VarInt::writeSignedInt($out, $this->enchantmentSeed); - $out->putUnsignedVarInt(count($this->blockPalette)); + VarInt::writeUnsignedInt($out, count($this->blockPalette)); foreach($this->blockPalette as $entry){ - $out->putString($entry->getName()); - $out->put($entry->getStates()->getEncodedNbt()); + CommonTypes::putString($out, $entry->getName()); + $out->writeByteArray($entry->getStates()->getEncodedNbt()); } - $out->putString($this->multiplayerCorrelationId); - $out->putBool($this->enableNewInventorySystem); - $out->putString($this->serverSoftwareVersion); - $out->put($this->playerActorProperties->getEncodedNbt()); - $out->putLLong($this->blockPaletteChecksum); - $out->putUUID($this->worldTemplateId); - $out->putBool($this->enableClientSideChunkGeneration); - $out->putBool($this->blockNetworkIdsAreHashes); - $out->putBool($this->enableTickDeathSystems); + CommonTypes::putString($out, $this->multiplayerCorrelationId); + CommonTypes::putBool($out, $this->enableNewInventorySystem); + CommonTypes::putString($out, $this->serverSoftwareVersion); + $out->writeByteArray($this->playerActorProperties->getEncodedNbt()); + LE::writeUnsignedLong($out, $this->blockPaletteChecksum); + CommonTypes::putUUID($out, $this->worldTemplateId); + CommonTypes::putBool($out, $this->enableClientSideChunkGeneration); + CommonTypes::putBool($out, $this->blockNetworkIdsAreHashes); + CommonTypes::putBool($out, $this->enableTickDeathSystems); $this->networkPermissions->encode($out); } diff --git a/src/StopSoundPacket.php b/src/StopSoundPacket.php index 6ee20d3c..5e2f6853 100644 --- a/src/StopSoundPacket.php +++ b/src/StopSoundPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class StopSoundPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::STOP_SOUND_PACKET; @@ -34,16 +36,16 @@ public static function create(string $soundName, bool $stopAll, bool $stopLegacy return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->soundName = $in->getString(); - $this->stopAll = $in->getBool(); - $this->stopLegacyMusic = $in->getBool(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->soundName = CommonTypes::getString($in); + $this->stopAll = CommonTypes::getBool($in); + $this->stopLegacyMusic = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->soundName); - $out->putBool($this->stopAll); - $out->putBool($this->stopLegacyMusic); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->soundName); + CommonTypes::putBool($out, $this->stopAll); + CommonTypes::putBool($out, $this->stopLegacyMusic); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/StructureBlockUpdatePacket.php b/src/StructureBlockUpdatePacket.php index e0f5a03d..f2b79059 100644 --- a/src/StructureBlockUpdatePacket.php +++ b/src/StructureBlockUpdatePacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; use pocketmine\network\mcpe\protocol\types\StructureEditorData; @@ -38,18 +40,18 @@ public static function create(BlockPosition $blockPosition, StructureEditorData return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->blockPosition = $in->getBlockPosition(); - $this->structureEditorData = $in->getStructureEditorData(); - $this->isPowered = $in->getBool(); - $this->waterlogged = $in->getBool(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->blockPosition = CommonTypes::getBlockPosition($in); + $this->structureEditorData = CommonTypes::getStructureEditorData($in); + $this->isPowered = CommonTypes::getBool($in); + $this->waterlogged = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putBlockPosition($this->blockPosition); - $out->putStructureEditorData($this->structureEditorData); - $out->putBool($this->isPowered); - $out->putBool($this->waterlogged); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putBlockPosition($out, $this->blockPosition); + CommonTypes::putStructureEditorData($out, $this->structureEditorData); + CommonTypes::putBool($out, $this->isPowered); + CommonTypes::putBool($out, $this->waterlogged); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/StructureTemplateDataRequestPacket.php b/src/StructureTemplateDataRequestPacket.php index a6f40dcf..50cf8b36 100644 --- a/src/StructureTemplateDataRequestPacket.php +++ b/src/StructureTemplateDataRequestPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; use pocketmine\network\mcpe\protocol\types\StructureSettings; @@ -42,18 +45,18 @@ public static function create(string $structureTemplateName, BlockPosition $stru return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->structureTemplateName = $in->getString(); - $this->structureBlockPosition = $in->getBlockPosition(); - $this->structureSettings = $in->getStructureSettings(); - $this->requestType = $in->getByte(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->structureTemplateName = CommonTypes::getString($in); + $this->structureBlockPosition = CommonTypes::getBlockPosition($in); + $this->structureSettings = CommonTypes::getStructureSettings($in); + $this->requestType = Byte::readUnsigned($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->structureTemplateName); - $out->putBlockPosition($this->structureBlockPosition); - $out->putStructureSettings($this->structureSettings); - $out->putByte($this->requestType); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->structureTemplateName); + CommonTypes::putBlockPosition($out, $this->structureBlockPosition); + CommonTypes::putStructureSettings($out, $this->structureSettings); + Byte::writeUnsigned($out, $this->requestType); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/StructureTemplateDataResponsePacket.php b/src/StructureTemplateDataResponsePacket.php index 5704f1ac..dcc5ebf8 100644 --- a/src/StructureTemplateDataResponsePacket.php +++ b/src/StructureTemplateDataResponsePacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\CacheableNbt; class StructureTemplateDataResponsePacket extends DataPacket implements ClientboundPacket{ @@ -41,21 +44,21 @@ public static function create(string $structureTemplateName, ?CacheableNbt $nbt, return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->structureTemplateName = $in->getString(); - if($in->getBool()){ - $this->nbt = new CacheableNbt($in->getNbtCompoundRoot()); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->structureTemplateName = CommonTypes::getString($in); + if(CommonTypes::getBool($in)){ + $this->nbt = new CacheableNbt(CommonTypes::getNbtCompoundRoot($in)); } - $this->responseType = $in->getByte(); + $this->responseType = Byte::readUnsigned($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->structureTemplateName); - $out->putBool($this->nbt !== null); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->structureTemplateName); + CommonTypes::putBool($out, $this->nbt !== null); if($this->nbt !== null){ - $out->put($this->nbt->getEncodedNbt()); + $out->writeByteArray($this->nbt->getEncodedNbt()); } - $out->putByte($this->responseType); + Byte::writeUnsigned($out, $this->responseType); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SubChunkPacket.php b/src/SubChunkPacket.php index 7111a486..0e10102e 100644 --- a/src/SubChunkPacket.php +++ b/src/SubChunkPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\SubChunkPacketEntryWithCache as EntryWithBlobHash; use pocketmine\network\mcpe\protocol\types\SubChunkPacketEntryWithCacheList as ListWithBlobHashes; use pocketmine\network\mcpe\protocol\types\SubChunkPacketEntryWithoutCache as EntryWithoutBlobHash; @@ -48,12 +52,12 @@ public function getBaseSubChunkPosition() : SubChunkPosition{ return $this->base public function getEntries() : ListWithBlobHashes|ListWithoutBlobHashes{ return $this->entries; } - protected function decodePayload(PacketSerializer $in) : void{ - $cacheEnabled = $in->getBool(); - $this->dimension = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $cacheEnabled = CommonTypes::getBool($in); + $this->dimension = VarInt::readSignedInt($in); $this->baseSubChunkPosition = SubChunkPosition::read($in); - $count = $in->getLInt(); + $count = LE::readUnsignedInt($in); if($cacheEnabled){ $entries = []; for($i = 0; $i < $count; $i++){ @@ -69,12 +73,12 @@ protected function decodePayload(PacketSerializer $in) : void{ } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putBool($this->entries instanceof ListWithBlobHashes); - $out->putVarInt($this->dimension); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putBool($out, $this->entries instanceof ListWithBlobHashes); + VarInt::writeSignedInt($out, $this->dimension); $this->baseSubChunkPosition->write($out); - $out->putLInt(count($this->entries->getEntries())); + LE::writeUnsignedInt($out, count($this->entries->getEntries())); foreach($this->entries->getEntries() as $entry){ $entry->write($out); diff --git a/src/SubChunkRequestPacket.php b/src/SubChunkRequestPacket.php index 5b67b478..bd547aca 100644 --- a/src/SubChunkRequestPacket.php +++ b/src/SubChunkRequestPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\types\SubChunkPosition; use pocketmine\network\mcpe\protocol\types\SubChunkPositionOffset; use function count; @@ -53,21 +56,21 @@ public function getBasePosition() : SubChunkPosition{ return $this->basePosition */ public function getEntries() : array{ return $this->entries; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->dimension = $in->getVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->dimension = VarInt::readSignedInt($in); $this->basePosition = SubChunkPosition::read($in); $this->entries = []; - for($i = 0, $count = $in->getLInt(); $i < $count; $i++){ + for($i = 0, $count = LE::readUnsignedInt($in); $i < $count; $i++){ $this->entries[] = SubChunkPositionOffset::read($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->dimension); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->dimension); $this->basePosition->write($out); - $out->putLInt(count($this->entries)); + LE::writeUnsignedInt($out, count($this->entries)); foreach($this->entries as $entry){ $entry->write($out); } diff --git a/src/SubClientLoginPacket.php b/src/SubClientLoginPacket.php index c496d393..531d2963 100644 --- a/src/SubClientLoginPacket.php +++ b/src/SubClientLoginPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class SubClientLoginPacket extends DataPacket implements ServerboundPacket{ public const NETWORK_ID = ProtocolInfo::SUB_CLIENT_LOGIN_PACKET; @@ -30,12 +32,12 @@ public static function create(string $connectionRequestData) : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->connectionRequestData = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->connectionRequestData = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->connectionRequestData); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->connectionRequestData); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/SyncActorPropertyPacket.php b/src/SyncActorPropertyPacket.php index 56fca128..30f92309 100644 --- a/src/SyncActorPropertyPacket.php +++ b/src/SyncActorPropertyPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\CacheableNbt; class SyncActorPropertyPacket extends DataPacket implements ClientboundPacket{ @@ -36,12 +38,12 @@ public static function create(CacheableNbt $nbt) : self{ /** @phpstan-return CacheableNbt<\pocketmine\nbt\tag\CompoundTag> */ public function getNbt() : CacheableNbt{ return $this->nbt; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->nbt = new CacheableNbt($in->getNbtCompoundRoot()); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->nbt = new CacheableNbt(CommonTypes::getNbtCompoundRoot($in)); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->put($this->nbt->getEncodedNbt()); + protected function encodePayload(ByteBufferWriter $out) : void{ + $out->writeByteArray($this->nbt->getEncodedNbt()); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/TakeItemActorPacket.php b/src/TakeItemActorPacket.php index 127e8fdc..87b7b9db 100644 --- a/src/TakeItemActorPacket.php +++ b/src/TakeItemActorPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class TakeItemActorPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::TAKE_ITEM_ACTOR_PACKET; @@ -32,14 +34,14 @@ public static function create(int $takerActorRuntimeId, int $itemActorRuntimeId) return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->itemActorRuntimeId = $in->getActorRuntimeId(); - $this->takerActorRuntimeId = $in->getActorRuntimeId(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->itemActorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->takerActorRuntimeId = CommonTypes::getActorRuntimeId($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorRuntimeId($this->itemActorRuntimeId); - $out->putActorRuntimeId($this->takerActorRuntimeId); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->itemActorRuntimeId); + CommonTypes::putActorRuntimeId($out, $this->takerActorRuntimeId); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/TextPacket.php b/src/TextPacket.php index ceae96ca..9a5ec94f 100644 --- a/src/TextPacket.php +++ b/src/TextPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use function count; class TextPacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ @@ -95,72 +99,72 @@ public static function tip(string $message) : self{ return self::messageOnly(self::TYPE_TIP, $message); } - protected function decodePayload(PacketSerializer $in) : void{ - $this->type = $in->getByte(); - $this->needsTranslation = $in->getBool(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->type = Byte::readUnsigned($in); + $this->needsTranslation = CommonTypes::getBool($in); switch($this->type){ case self::TYPE_CHAT: case self::TYPE_WHISPER: /** @noinspection PhpMissingBreakStatementInspection */ case self::TYPE_ANNOUNCEMENT: - $this->sourceName = $in->getString(); + $this->sourceName = CommonTypes::getString($in); case self::TYPE_RAW: case self::TYPE_TIP: case self::TYPE_SYSTEM: case self::TYPE_JSON_WHISPER: case self::TYPE_JSON: case self::TYPE_JSON_ANNOUNCEMENT: - $this->message = $in->getString(); + $this->message = CommonTypes::getString($in); break; case self::TYPE_TRANSLATION: case self::TYPE_POPUP: case self::TYPE_JUKEBOX_POPUP: - $this->message = $in->getString(); - $count = $in->getUnsignedVarInt(); + $this->message = CommonTypes::getString($in); + $count = VarInt::readUnsignedInt($in); for($i = 0; $i < $count; ++$i){ - $this->parameters[] = $in->getString(); + $this->parameters[] = CommonTypes::getString($in); } break; } - $this->xboxUserId = $in->getString(); - $this->platformChatId = $in->getString(); - $this->filteredMessage = $in->getString(); + $this->xboxUserId = CommonTypes::getString($in); + $this->platformChatId = CommonTypes::getString($in); + $this->filteredMessage = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->type); - $out->putBool($this->needsTranslation); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->type); + CommonTypes::putBool($out, $this->needsTranslation); switch($this->type){ case self::TYPE_CHAT: case self::TYPE_WHISPER: /** @noinspection PhpMissingBreakStatementInspection */ case self::TYPE_ANNOUNCEMENT: - $out->putString($this->sourceName); + CommonTypes::putString($out, $this->sourceName); case self::TYPE_RAW: case self::TYPE_TIP: case self::TYPE_SYSTEM: case self::TYPE_JSON_WHISPER: case self::TYPE_JSON: case self::TYPE_JSON_ANNOUNCEMENT: - $out->putString($this->message); + CommonTypes::putString($out, $this->message); break; case self::TYPE_TRANSLATION: case self::TYPE_POPUP: case self::TYPE_JUKEBOX_POPUP: - $out->putString($this->message); - $out->putUnsignedVarInt(count($this->parameters)); + CommonTypes::putString($out, $this->message); + VarInt::writeUnsignedInt($out, count($this->parameters)); foreach($this->parameters as $p){ - $out->putString($p); + CommonTypes::putString($out, $p); } break; } - $out->putString($this->xboxUserId); - $out->putString($this->platformChatId); - $out->putString($this->filteredMessage); + CommonTypes::putString($out, $this->xboxUserId); + CommonTypes::putString($out, $this->platformChatId); + CommonTypes::putString($out, $this->filteredMessage); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/TickingAreasLoadStatusPacket.php b/src/TickingAreasLoadStatusPacket.php index c8390ce6..97cba12c 100644 --- a/src/TickingAreasLoadStatusPacket.php +++ b/src/TickingAreasLoadStatusPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class TickingAreasLoadStatusPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::TICKING_AREAS_LOAD_STATUS_PACKET; @@ -32,12 +34,12 @@ public static function create(bool $waitingForPreload) : self{ public function isWaitingForPreload() : bool{ return $this->waitingForPreload; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->waitingForPreload = $in->getBool(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->waitingForPreload = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putBool($this->waitingForPreload); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putBool($out, $this->waitingForPreload); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ToastRequestPacket.php b/src/ToastRequestPacket.php index e1d8f5c4..451d0a34 100644 --- a/src/ToastRequestPacket.php +++ b/src/ToastRequestPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; /** * Displays a toast notification on the client's screen (usually a little box at the top, like the one shown when @@ -40,14 +42,14 @@ public function getTitle() : string{ return $this->title; } public function getBody() : string{ return $this->body; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->title = $in->getString(); - $this->body = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->title = CommonTypes::getString($in); + $this->body = CommonTypes::getString($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->title); - $out->putString($this->body); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->title); + CommonTypes::putString($out, $this->body); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/TransferPacket.php b/src/TransferPacket.php index eaf54985..840e50e8 100644 --- a/src/TransferPacket.php +++ b/src/TransferPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class TransferPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::TRANSFER_PACKET; @@ -34,16 +37,16 @@ public static function create(string $address, int $port, bool $reloadWorld) : s return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->address = $in->getString(); - $this->port = $in->getLShort(); - $this->reloadWorld = $in->getBool(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->address = CommonTypes::getString($in); + $this->port = LE::readUnsignedShort($in); + $this->reloadWorld = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->address); - $out->putLShort($this->port); - $out->putBool($this->reloadWorld); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->address); + LE::writeUnsignedShort($out, $this->port); + CommonTypes::putBool($out, $this->reloadWorld); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/TrimDataPacket.php b/src/TrimDataPacket.php index 179e2c3f..4ea67b0a 100644 --- a/src/TrimDataPacket.php +++ b/src/TrimDataPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\types\TrimMaterial; use pocketmine\network\mcpe\protocol\types\TrimPattern; use function count; @@ -59,23 +61,23 @@ public function getTrimPatterns() : array{ return $this->trimPatterns; } */ public function getTrimMaterials() : array{ return $this->trimMaterials; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ $this->trimPatterns = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $this->trimPatterns[] = TrimPattern::read($in); } $this->trimMaterials = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $this->trimMaterials[] = TrimMaterial::read($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->trimPatterns)); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->trimPatterns)); foreach($this->trimPatterns as $trimPattern){ $trimPattern->write($out); } - $out->putUnsignedVarInt(count($this->trimMaterials)); + VarInt::writeUnsignedInt($out, count($this->trimMaterials)); foreach($this->trimMaterials as $trimMaterial){ $trimMaterial->write($out); } diff --git a/src/UnlockedRecipesPacket.php b/src/UnlockedRecipesPacket.php index 51326e3c..a2f466d2 100644 --- a/src/UnlockedRecipesPacket.php +++ b/src/UnlockedRecipesPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use function count; class UnlockedRecipesPacket extends DataPacket implements ClientboundPacket{ @@ -48,19 +52,19 @@ public function getType() : int{ return $this->type; } */ public function getRecipes() : array{ return $this->recipes; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->type = $in->getLInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->type = LE::readUnsignedInt($in); $this->recipes = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; $i++){ - $this->recipes[] = $in->getString(); + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; $i++){ + $this->recipes[] = CommonTypes::getString($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLInt($this->type); - $out->putUnsignedVarInt(count($this->recipes)); + protected function encodePayload(ByteBufferWriter $out) : void{ + LE::writeUnsignedInt($out, $this->type); + VarInt::writeUnsignedInt($out, count($this->recipes)); foreach($this->recipes as $recipe){ - $out->putString($recipe); + CommonTypes::putString($out, $recipe); } } diff --git a/src/UpdateAbilitiesPacket.php b/src/UpdateAbilitiesPacket.php index 7be99470..fdeb6f20 100644 --- a/src/UpdateAbilitiesPacket.php +++ b/src/UpdateAbilitiesPacket.php @@ -14,7 +14,8 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use pocketmine\network\mcpe\protocol\types\AbilitiesData; /** @@ -37,11 +38,11 @@ public static function create(AbilitiesData $data) : self{ public function getData() : AbilitiesData{ return $this->data; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ $this->data = AbilitiesData::decode($in); } - protected function encodePayload(PacketSerializer $out) : void{ + protected function encodePayload(ByteBufferWriter $out) : void{ $this->data->encode($out); } diff --git a/src/UpdateAdventureSettingsPacket.php b/src/UpdateAdventureSettingsPacket.php index 058522dc..b97b8c7f 100644 --- a/src/UpdateAdventureSettingsPacket.php +++ b/src/UpdateAdventureSettingsPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; /** * Updates "adventure settings". In vanilla, these flags apply to the whole world. This differs from abilities, which @@ -54,20 +56,20 @@ public function isShowNameTags() : bool{ return $this->showNameTags; } public function isAutoJump() : bool{ return $this->autoJump; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->noAttackingMobs = $in->getBool(); - $this->noAttackingPlayers = $in->getBool(); - $this->worldImmutable = $in->getBool(); - $this->showNameTags = $in->getBool(); - $this->autoJump = $in->getBool(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->noAttackingMobs = CommonTypes::getBool($in); + $this->noAttackingPlayers = CommonTypes::getBool($in); + $this->worldImmutable = CommonTypes::getBool($in); + $this->showNameTags = CommonTypes::getBool($in); + $this->autoJump = CommonTypes::getBool($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putBool($this->noAttackingMobs); - $out->putBool($this->noAttackingPlayers); - $out->putBool($this->worldImmutable); - $out->putBool($this->showNameTags); - $out->putBool($this->autoJump); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putBool($out, $this->noAttackingMobs); + CommonTypes::putBool($out, $this->noAttackingPlayers); + CommonTypes::putBool($out, $this->worldImmutable); + CommonTypes::putBool($out, $this->showNameTags); + CommonTypes::putBool($out, $this->autoJump); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/UpdateAttributesPacket.php b/src/UpdateAttributesPacket.php index 0f87684d..f645a3b0 100644 --- a/src/UpdateAttributesPacket.php +++ b/src/UpdateAttributesPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\entity\UpdateAttribute; use function count; @@ -38,21 +41,21 @@ public static function create(int $actorRuntimeId, array $entries, int $tick) : return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->actorRuntimeId = $in->getActorRuntimeId(); - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ + protected function decodePayload(ByteBufferReader $in) : void{ + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ $this->entries[] = UpdateAttribute::read($in); } - $this->tick = $in->getUnsignedVarLong(); + $this->tick = VarInt::readUnsignedLong($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putActorRuntimeId($this->actorRuntimeId); - $out->putUnsignedVarInt(count($this->entries)); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + VarInt::writeUnsignedInt($out, count($this->entries)); foreach($this->entries as $entry){ $entry->write($out); } - $out->putUnsignedVarLong($this->tick); + VarInt::writeUnsignedLong($out, $this->tick); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/UpdateBlockPacket.php b/src/UpdateBlockPacket.php index b777c7fd..a2ad9aa3 100644 --- a/src/UpdateBlockPacket.php +++ b/src/UpdateBlockPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; class UpdateBlockPacket extends DataPacket implements ClientboundPacket{ @@ -51,18 +54,18 @@ public static function create(BlockPosition $blockPosition, int $blockRuntimeId, return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->blockPosition = $in->getBlockPosition(); - $this->blockRuntimeId = $in->getUnsignedVarInt(); - $this->flags = $in->getUnsignedVarInt(); - $this->dataLayerId = $in->getUnsignedVarInt(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->blockPosition = CommonTypes::getBlockPosition($in); + $this->blockRuntimeId = VarInt::readUnsignedInt($in); + $this->flags = VarInt::readUnsignedInt($in); + $this->dataLayerId = VarInt::readUnsignedInt($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putBlockPosition($this->blockPosition); - $out->putUnsignedVarInt($this->blockRuntimeId); - $out->putUnsignedVarInt($this->flags); - $out->putUnsignedVarInt($this->dataLayerId); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putBlockPosition($out, $this->blockPosition); + VarInt::writeUnsignedInt($out, $this->blockRuntimeId); + VarInt::writeUnsignedInt($out, $this->flags); + VarInt::writeUnsignedInt($out, $this->dataLayerId); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/UpdateBlockSyncedPacket.php b/src/UpdateBlockSyncedPacket.php index dc1c706a..74830102 100644 --- a/src/UpdateBlockSyncedPacket.php +++ b/src/UpdateBlockSyncedPacket.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; class UpdateBlockSyncedPacket extends UpdateBlockPacket{ public const NETWORK_ID = ProtocolInfo::UPDATE_BLOCK_SYNCED_PACKET; @@ -26,16 +28,16 @@ class UpdateBlockSyncedPacket extends UpdateBlockPacket{ public int $actorUniqueId; public int $updateType; - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ parent::decodePayload($in); - $this->actorUniqueId = $in->getUnsignedVarLong(); - $this->updateType = $in->getUnsignedVarLong(); + $this->actorUniqueId = VarInt::readUnsignedLong($in); + $this->updateType = VarInt::readUnsignedLong($in); } - protected function encodePayload(PacketSerializer $out) : void{ + protected function encodePayload(ByteBufferWriter $out) : void{ parent::encodePayload($out); - $out->putUnsignedVarLong($this->actorUniqueId); - $out->putUnsignedVarLong($this->updateType); + VarInt::writeUnsignedLong($out, $this->actorUniqueId); + VarInt::writeUnsignedLong($out, $this->updateType); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/UpdateClientInputLocksPacket.php b/src/UpdateClientInputLocksPacket.php index add3e60b..7cb5368d 100644 --- a/src/UpdateClientInputLocksPacket.php +++ b/src/UpdateClientInputLocksPacket.php @@ -14,8 +14,11 @@ namespace pocketmine\network\mcpe\protocol; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class UpdateClientInputLocksPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::UPDATE_CLIENT_INPUT_LOCKS_PACKET; @@ -37,14 +40,14 @@ public function getFlags() : int{ return $this->flags; } public function getPosition() : Vector3{ return $this->position; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->flags = $in->getUnsignedVarInt(); - $this->position = $in->getVector3(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->flags = VarInt::readUnsignedInt($in); + $this->position = CommonTypes::getVector3($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putUnsignedVarInt($this->flags); - $out->putVector3($this->position); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, $this->flags); + CommonTypes::putVector3($out, $this->position); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/UpdateClientOptionsPacket.php b/src/UpdateClientOptionsPacket.php index 389fc462..12abb153 100644 --- a/src/UpdateClientOptionsPacket.php +++ b/src/UpdateClientOptionsPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\GraphicsMode; class UpdateClientOptionsPacket extends DataPacket implements ServerboundPacket{ @@ -33,12 +36,12 @@ public static function create(?GraphicsMode $graphicsMode) : self{ public function getGraphicsMode() : ?GraphicsMode{ return $this->graphicsMode; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->graphicsMode = $in->readOptional(fn() => GraphicsMode::fromPacket($in->getByte())); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->graphicsMode = CommonTypes::readOptional($in, fn() => GraphicsMode::fromPacket(Byte::readUnsigned($in))); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->writeOptional($this->graphicsMode, fn(GraphicsMode $v) => $out->putByte($v->value)); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::writeOptional($out, $this->graphicsMode, fn(ByteBufferWriter $out, GraphicsMode $v) => Byte::writeUnsigned($out, $v->value)); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/UpdateEquipPacket.php b/src/UpdateEquipPacket.php index 22413360..04cb347a 100644 --- a/src/UpdateEquipPacket.php +++ b/src/UpdateEquipPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\CacheableNbt; class UpdateEquipPacket extends DataPacket implements ClientboundPacket{ @@ -41,20 +45,20 @@ public static function create(int $windowId, int $windowType, int $windowSlotCou return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->windowId = $in->getByte(); - $this->windowType = $in->getByte(); - $this->windowSlotCount = $in->getVarInt(); - $this->actorUniqueId = $in->getActorUniqueId(); - $this->nbt = new CacheableNbt($in->getNbtCompoundRoot()); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->windowId = Byte::readUnsigned($in); + $this->windowType = Byte::readUnsigned($in); + $this->windowSlotCount = VarInt::readSignedInt($in); + $this->actorUniqueId = CommonTypes::getActorUniqueId($in); + $this->nbt = new CacheableNbt(CommonTypes::getNbtCompoundRoot($in)); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->windowId); - $out->putByte($this->windowType); - $out->putVarInt($this->windowSlotCount); - $out->putActorUniqueId($this->actorUniqueId); - $out->put($this->nbt->getEncodedNbt()); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->windowId); + Byte::writeUnsigned($out, $this->windowType); + VarInt::writeSignedInt($out, $this->windowSlotCount); + CommonTypes::putActorUniqueId($out, $this->actorUniqueId); + $out->writeByteArray($this->nbt->getEncodedNbt()); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/UpdatePlayerGameTypePacket.php b/src/UpdatePlayerGameTypePacket.php index 92651818..b8e8d116 100644 --- a/src/UpdatePlayerGameTypePacket.php +++ b/src/UpdatePlayerGameTypePacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\GameMode; class UpdatePlayerGameTypePacket extends DataPacket implements ClientboundPacket{ @@ -42,16 +45,16 @@ public function getPlayerActorUniqueId() : int{ return $this->playerActorUniqueI public function getTick() : int{ return $this->tick; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->gameMode = $in->getVarInt(); - $this->playerActorUniqueId = $in->getActorUniqueId(); - $this->tick = $in->getUnsignedVarLong(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->gameMode = VarInt::readSignedInt($in); + $this->playerActorUniqueId = CommonTypes::getActorUniqueId($in); + $this->tick = VarInt::readUnsignedLong($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putVarInt($this->gameMode); - $out->putActorUniqueId($this->playerActorUniqueId); - $out->putUnsignedVarLong($this->tick); + protected function encodePayload(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->gameMode); + CommonTypes::putActorUniqueId($out, $this->playerActorUniqueId); + VarInt::writeUnsignedLong($out, $this->tick); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/UpdateSoftEnumPacket.php b/src/UpdateSoftEnumPacket.php index e3e0950c..49815c92 100644 --- a/src/UpdateSoftEnumPacket.php +++ b/src/UpdateSoftEnumPacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use function count; class UpdateSoftEnumPacket extends DataPacket implements ClientboundPacket{ @@ -41,21 +45,21 @@ public static function create(string $enumName, array $values, int $type) : self return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->enumName = $in->getString(); - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ - $this->values[] = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->enumName = CommonTypes::getString($in); + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ + $this->values[] = CommonTypes::getString($in); } - $this->type = $in->getByte(); + $this->type = Byte::readUnsigned($in); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->enumName); - $out->putUnsignedVarInt(count($this->values)); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->enumName); + VarInt::writeUnsignedInt($out, count($this->values)); foreach($this->values as $v){ - $out->putString($v); + CommonTypes::putString($out, $v); } - $out->putByte($this->type); + Byte::writeUnsigned($out, $this->type); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/UpdateSubChunkBlocksPacket.php b/src/UpdateSubChunkBlocksPacket.php index e664a1f4..eff99ec7 100644 --- a/src/UpdateSubChunkBlocksPacket.php +++ b/src/UpdateSubChunkBlocksPacket.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; use pocketmine\network\mcpe\protocol\types\UpdateSubChunkBlocksPacketEntry; use function count; @@ -50,25 +53,25 @@ public function getLayer0Updates() : array{ return $this->layer0Updates; } /** @return UpdateSubChunkBlocksPacketEntry[] */ public function getLayer1Updates() : array{ return $this->layer1Updates; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->baseBlockPosition = $in->getBlockPosition(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->baseBlockPosition = CommonTypes::getBlockPosition($in); $this->layer0Updates = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $this->layer0Updates[] = UpdateSubChunkBlocksPacketEntry::read($in); } $this->layer1Updates = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $this->layer1Updates[] = UpdateSubChunkBlocksPacketEntry::read($in); } } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putBlockPosition($this->baseBlockPosition); - $out->putUnsignedVarInt(count($this->layer0Updates)); + protected function encodePayload(ByteBufferWriter $out) : void{ + CommonTypes::putBlockPosition($out, $this->baseBlockPosition); + VarInt::writeUnsignedInt($out, count($this->layer0Updates)); foreach($this->layer0Updates as $update){ $update->write($out); } - $out->putUnsignedVarInt(count($this->layer1Updates)); + VarInt::writeUnsignedInt($out, count($this->layer1Updates)); foreach($this->layer1Updates as $update){ $update->write($out); } diff --git a/src/UpdateTradePacket.php b/src/UpdateTradePacket.php index 8c66a1ec..618de2ed 100644 --- a/src/UpdateTradePacket.php +++ b/src/UpdateTradePacket.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\CacheableNbt; use pocketmine\network\mcpe\protocol\types\inventory\WindowTypes; @@ -63,30 +67,30 @@ public static function create( return $result; } - protected function decodePayload(PacketSerializer $in) : void{ - $this->windowId = $in->getByte(); - $this->windowType = $in->getByte(); - $this->windowSlotCount = $in->getVarInt(); - $this->tradeTier = $in->getVarInt(); - $this->traderActorUniqueId = $in->getActorUniqueId(); - $this->playerActorUniqueId = $in->getActorUniqueId(); - $this->displayName = $in->getString(); - $this->isV2Trading = $in->getBool(); - $this->isEconomyTrading = $in->getBool(); - $this->offers = new CacheableNbt($in->getNbtCompoundRoot()); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->windowId = Byte::readUnsigned($in); + $this->windowType = Byte::readUnsigned($in); + $this->windowSlotCount = VarInt::readSignedInt($in); + $this->tradeTier = VarInt::readSignedInt($in); + $this->traderActorUniqueId = CommonTypes::getActorUniqueId($in); + $this->playerActorUniqueId = CommonTypes::getActorUniqueId($in); + $this->displayName = CommonTypes::getString($in); + $this->isV2Trading = CommonTypes::getBool($in); + $this->isEconomyTrading = CommonTypes::getBool($in); + $this->offers = new CacheableNbt(CommonTypes::getNbtCompoundRoot($in)); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->windowId); - $out->putByte($this->windowType); - $out->putVarInt($this->windowSlotCount); - $out->putVarInt($this->tradeTier); - $out->putActorUniqueId($this->traderActorUniqueId); - $out->putActorUniqueId($this->playerActorUniqueId); - $out->putString($this->displayName); - $out->putBool($this->isV2Trading); - $out->putBool($this->isEconomyTrading); - $out->put($this->offers->getEncodedNbt()); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->windowId); + Byte::writeUnsigned($out, $this->windowType); + VarInt::writeSignedInt($out, $this->windowSlotCount); + VarInt::writeSignedInt($out, $this->tradeTier); + CommonTypes::putActorUniqueId($out, $this->traderActorUniqueId); + CommonTypes::putActorUniqueId($out, $this->playerActorUniqueId); + CommonTypes::putString($out, $this->displayName); + CommonTypes::putBool($out, $this->isV2Trading); + CommonTypes::putBool($out, $this->isEconomyTrading); + $out->writeByteArray($this->offers->getEncodedNbt()); } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/serializer/BitSet.php b/src/serializer/BitSet.php index b05fa969..c22a3cee 100644 --- a/src/serializer/BitSet.php +++ b/src/serializer/BitSet.php @@ -14,6 +14,8 @@ namespace pocketmine\network\mcpe\protocol\serializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader;use pmmp\encoding\ByteBufferWriter; use function array_pad; use function array_slice; use function array_values; @@ -86,14 +88,14 @@ private static function getExpectedPartsCount(int $length) : int{ return intdiv($length + self::INT_BITS - 1, self::INT_BITS); } - public static function read(PacketSerializer $in, int $length) : self{ + public static function read(ByteBufferReader $in, int $length) : self{ $result = [0]; $currentIndex = 0; $currentShift = 0; for($i = 0; $i < $length; $i += self::SHIFT){ - $b = $in->getByte(); + $b = Byte::readUnsigned($in); $bits = $b & 0x7f; $result[$currentIndex] |= $bits << $currentShift; //extra bits will be discarded @@ -113,7 +115,7 @@ public static function read(PacketSerializer $in, int $length) : self{ return new self($length, array_slice($result, 0, self::getExpectedPartsCount($length))); } - public function write(PacketSerializer $out) : void{ + public function write(ByteBufferWriter $out) : void{ $parts = $this->parts; $length = $this->length; @@ -132,7 +134,7 @@ public function write(PacketSerializer $out) : void{ $last = $i + self::SHIFT >= $length; $bits |= $last ? 0 : 0x80; - $out->putByte($bits); + Byte::writeUnsigned($out, $bits); if($last){ break; } diff --git a/src/serializer/CommonTypes.php b/src/serializer/CommonTypes.php new file mode 100644 index 00000000..1586e1bf --- /dev/null +++ b/src/serializer/CommonTypes.php @@ -0,0 +1,828 @@ + + * + * BedrockProtocol is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + */ + +declare(strict_types=1); + +namespace pocketmine\network\mcpe\protocol\serializer; + +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\DataDecodeException; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\math\Vector2; +use pocketmine\math\Vector3; +use pocketmine\nbt\NbtDataException; +use pocketmine\nbt\tag\CompoundTag; +use pocketmine\nbt\TreeRoot; +use pocketmine\network\mcpe\protocol\PacketDecodeException; +use pocketmine\network\mcpe\protocol\types\BlockPosition; +use pocketmine\network\mcpe\protocol\types\BoolGameRule; +use pocketmine\network\mcpe\protocol\types\command\CommandOriginData; +use pocketmine\network\mcpe\protocol\types\entity\BlockPosMetadataProperty; +use pocketmine\network\mcpe\protocol\types\entity\ByteMetadataProperty; +use pocketmine\network\mcpe\protocol\types\entity\CompoundTagMetadataProperty; +use pocketmine\network\mcpe\protocol\types\entity\EntityLink; +use pocketmine\network\mcpe\protocol\types\entity\FloatMetadataProperty; +use pocketmine\network\mcpe\protocol\types\entity\IntMetadataProperty; +use pocketmine\network\mcpe\protocol\types\entity\LongMetadataProperty; +use pocketmine\network\mcpe\protocol\types\entity\MetadataProperty; +use pocketmine\network\mcpe\protocol\types\entity\ShortMetadataProperty; +use pocketmine\network\mcpe\protocol\types\entity\StringMetadataProperty; +use pocketmine\network\mcpe\protocol\types\entity\Vec3MetadataProperty; +use pocketmine\network\mcpe\protocol\types\FloatGameRule; +use pocketmine\network\mcpe\protocol\types\GameRule; +use pocketmine\network\mcpe\protocol\types\IntGameRule; +use pocketmine\network\mcpe\protocol\types\inventory\ItemStack; +use pocketmine\network\mcpe\protocol\types\inventory\ItemStackWrapper; +use pocketmine\network\mcpe\protocol\types\recipe\ComplexAliasItemDescriptor; +use pocketmine\network\mcpe\protocol\types\recipe\IntIdMetaItemDescriptor; +use pocketmine\network\mcpe\protocol\types\recipe\ItemDescriptorType; +use pocketmine\network\mcpe\protocol\types\recipe\MolangItemDescriptor; +use pocketmine\network\mcpe\protocol\types\recipe\RecipeIngredient; +use pocketmine\network\mcpe\protocol\types\recipe\StringIdMetaItemDescriptor; +use pocketmine\network\mcpe\protocol\types\recipe\TagItemDescriptor; +use pocketmine\network\mcpe\protocol\types\skin\PersonaPieceTintColor; +use pocketmine\network\mcpe\protocol\types\skin\PersonaSkinPiece; +use pocketmine\network\mcpe\protocol\types\skin\SkinAnimation; +use pocketmine\network\mcpe\protocol\types\skin\SkinData; +use pocketmine\network\mcpe\protocol\types\skin\SkinImage; +use pocketmine\network\mcpe\protocol\types\StructureEditorData; +use pocketmine\network\mcpe\protocol\types\StructureSettings; +use pocketmine\utils\Binary; +use Ramsey\Uuid\Uuid; +use Ramsey\Uuid\UuidInterface; +use function count; +use function strlen; +use function strrev; +use function substr; + +final class CommonTypes{ + + private function __construct(){ + //NOOP + } + + /** @throws DataDecodeException */ + public static function getString(ByteBufferReader $in) : string{ + return $in->readByteArray(VarInt::readUnsignedInt($in)); + } + + public static function putString(ByteBufferWriter $out, string $v) : void{ + VarInt::writeUnsignedInt($out, strlen($v)); + $out->writeByteArray($v); + } + + /** @throws DataDecodeException */ + public static function getBool(ByteBufferReader $in) : bool{ + return Byte::readUnsigned($in) !== 0; + } + + public static function putBool(ByteBufferWriter $out, bool $v) : void{ + Byte::writeUnsigned($out, $v ? 1 : 0); + } + + /** @throws DataDecodeException */ + public static function getUUID(ByteBufferReader $in) : UuidInterface{ + //This is two little-endian longs: bytes 7-0 followed by bytes 15-8 + $p1 = strrev($in->readByteArray(8)); + $p2 = strrev($in->readByteArray(8)); + return Uuid::fromBytes($p1 . $p2); + } + + public static function putUUID(ByteBufferWriter $out, UuidInterface $uuid) : void{ + $bytes = $uuid->getBytes(); + $out->writeByteArray(strrev(substr($bytes, 0, 8))); + $out->writeByteArray(strrev(substr($bytes, 8, 8))); + } + + /** @throws DataDecodeException */ + public static function getSkin(ByteBufferReader $in) : SkinData{ + $skinId = self::getString($in); + $skinPlayFabId = self::getString($in); + $skinResourcePatch = self::getString($in); + $skinData = self::getSkinImage($in); + $animationCount = LE::readUnsignedInt($in); + $animations = []; + for($i = 0; $i < $animationCount; ++$i){ + $skinImage = self::getSkinImage($in); + $animationType = LE::readUnsignedInt($in); + $animationFrames = LE::readFloat($in); + $expressionType = LE::readUnsignedInt($in); + $animations[] = new SkinAnimation($skinImage, $animationType, $animationFrames, $expressionType); + } + $capeData = self::getSkinImage($in); + $geometryData = self::getString($in); + $geometryDataVersion = self::getString($in); + $animationData = self::getString($in); + $capeId = self::getString($in); + $fullSkinId = self::getString($in); + $armSize = self::getString($in); + $skinColor = self::getString($in); + $personaPieceCount = LE::readUnsignedInt($in); + $personaPieces = []; + for($i = 0; $i < $personaPieceCount; ++$i){ + $pieceId = self::getString($in); + $pieceType = self::getString($in); + $packId = self::getString($in); + $isDefaultPiece = self::getBool($in); + $productId = self::getString($in); + $personaPieces[] = new PersonaSkinPiece($pieceId, $pieceType, $packId, $isDefaultPiece, $productId); + } + $pieceTintColorCount = LE::readUnsignedInt($in); + $pieceTintColors = []; + for($i = 0; $i < $pieceTintColorCount; ++$i){ + $pieceType = self::getString($in); + $colorCount = LE::readUnsignedInt($in); + $colors = []; + for($j = 0; $j < $colorCount; ++$j){ + $colors[] = self::getString($in); + } + $pieceTintColors[] = new PersonaPieceTintColor( + $pieceType, + $colors + ); + } + + $premium = self::getBool($in); + $persona = self::getBool($in); + $capeOnClassic = self::getBool($in); + $isPrimaryUser = self::getBool($in); + $override = self::getBool($in); + + return new SkinData( + $skinId, + $skinPlayFabId, + $skinResourcePatch, + $skinData, + $animations, + $capeData, + $geometryData, + $geometryDataVersion, + $animationData, + $capeId, + $fullSkinId, + $armSize, + $skinColor, + $personaPieces, + $pieceTintColors, + true, + $premium, + $persona, + $capeOnClassic, + $isPrimaryUser, + $override, + ); + } + + public static function putSkin(ByteBufferWriter $out, SkinData $skin) : void{ + self::putString($out, $skin->getSkinId()); + self::putString($out, $skin->getPlayFabId()); + self::putString($out, $skin->getResourcePatch()); + self::putSkinImage($out, $skin->getSkinImage()); + LE::writeUnsignedInt($out, count($skin->getAnimations())); + foreach($skin->getAnimations() as $animation){ + self::putSkinImage($out, $animation->getImage()); + LE::writeUnsignedInt($out, $animation->getType()); + LE::writeFloat($out, $animation->getFrames()); + LE::writeUnsignedInt($out, $animation->getExpressionType()); + } + self::putSkinImage($out, $skin->getCapeImage()); + self::putString($out, $skin->getGeometryData()); + self::putString($out, $skin->getGeometryDataEngineVersion()); + self::putString($out, $skin->getAnimationData()); + self::putString($out, $skin->getCapeId()); + self::putString($out, $skin->getFullSkinId()); + self::putString($out, $skin->getArmSize()); + self::putString($out, $skin->getSkinColor()); + LE::writeUnsignedInt($out, count($skin->getPersonaPieces())); + foreach($skin->getPersonaPieces() as $piece){ + self::putString($out, $piece->getPieceId()); + self::putString($out, $piece->getPieceType()); + self::putString($out, $piece->getPackId()); + self::putBool($out, $piece->isDefaultPiece()); + self::putString($out, $piece->getProductId()); + } + LE::writeUnsignedInt($out, count($skin->getPieceTintColors())); + foreach($skin->getPieceTintColors() as $tint){ + self::putString($out, $tint->getPieceType()); + LE::writeUnsignedInt($out, count($tint->getColors())); + foreach($tint->getColors() as $color){ + self::putString($out, $color); + } + } + self::putBool($out, $skin->isPremium()); + self::putBool($out, $skin->isPersona()); + self::putBool($out, $skin->isPersonaCapeOnClassic()); + self::putBool($out, $skin->isPrimaryUser()); + self::putBool($out, $skin->isOverride()); + } + + /** @throws DataDecodeException */ + private static function getSkinImage(ByteBufferReader $in) : SkinImage{ + $width = LE::readUnsignedInt($in); + $height = LE::readUnsignedInt($in); + $data = self::getString($in); + try{ + return new SkinImage($height, $width, $data); + }catch(\InvalidArgumentException $e){ + throw new PacketDecodeException($e->getMessage(), 0, $e); + } + } + + private static function putSkinImage(ByteBufferWriter $out, SkinImage $image) : void{ + LE::writeUnsignedInt($out, $image->getWidth()); + LE::writeUnsignedInt($out, $image->getHeight()); + self::putString($out, $image->getData()); + } + + /** + * @return int[] + * @phpstan-return array{0: int, 1: int, 2: int} + * @throws DataDecodeException + */ + private static function getItemStackHeader(ByteBufferReader $in) : array{ + $id = VarInt::readSignedInt($in); + if($id === 0){ + return [0, 0, 0]; + } + + $count = LE::readUnsignedShort($in); + $meta = VarInt::readUnsignedInt($in); + + return [$id, $count, $meta]; + } + + private static function putItemStackHeader(ByteBufferWriter $out, ItemStack $itemStack) : bool{ + if($itemStack->getId() === 0){ + VarInt::writeSignedInt($out, 0); + return false; + } + + VarInt::writeSignedInt($out, $itemStack->getId()); + LE::writeUnsignedShort($out, $itemStack->getCount()); + VarInt::writeUnsignedInt($out, $itemStack->getMeta()); + + return true; + } + + /** @throws DataDecodeException */ + private static function getItemStackFooter(ByteBufferReader $in, int $id, int $meta, int $count) : ItemStack{ + $blockRuntimeId = VarInt::readSignedInt($in); + $rawExtraData = self::getString($in); + + return new ItemStack($id, $meta, $count, $blockRuntimeId, $rawExtraData); + } + + private static function putItemStackFooter(ByteBufferWriter $out, ItemStack $itemStack) : void{ + VarInt::writeSignedInt($out, $itemStack->getBlockRuntimeId()); + self::putString($out, $itemStack->getRawExtraData()); + } + + /** + * @throws PacketDecodeException + * @throws DataDecodeException + */ + public static function getItemStackWithoutStackId(ByteBufferReader $in) : ItemStack{ + [$id, $count, $meta] = self::getItemStackHeader($in); + + return $id !== 0 ? self::getItemStackFooter($in, $id, $meta, $count) : ItemStack::null(); + + } + + public static function putItemStackWithoutStackId(ByteBufferWriter $out, ItemStack $itemStack) : void{ + if(self::putItemStackHeader($out, $itemStack)){ + self::putItemStackFooter($out, $itemStack); + } + } + + /** @throws DataDecodeException */ + public static function getItemStackWrapper(ByteBufferReader $in) : ItemStackWrapper{ + [$id, $count, $meta] = self::getItemStackHeader($in); + if($id === 0){ + return new ItemStackWrapper(0, ItemStack::null()); + } + + $hasNetId = self::getBool($in); + $stackId = $hasNetId ? self::readServerItemStackId($in) : 0; + + $itemStack = self::getItemStackFooter($in, $id, $meta, $count); + + return new ItemStackWrapper($stackId, $itemStack); + } + + public static function putItemStackWrapper(ByteBufferWriter $out, ItemStackWrapper $itemStackWrapper) : void{ + $itemStack = $itemStackWrapper->getItemStack(); + if(self::putItemStackHeader($out, $itemStack)){ + $hasNetId = $itemStackWrapper->getStackId() !== 0; + self::putBool($out, $hasNetId); + if($hasNetId){ + self::writeServerItemStackId($out, $itemStackWrapper->getStackId()); + } + + self::putItemStackFooter($out, $itemStack); + } + } + + /** @throws DataDecodeException */ + public static function getRecipeIngredient(ByteBufferReader $in) : RecipeIngredient{ + $descriptorType = Byte::readUnsigned($in); + $descriptor = match($descriptorType){ + ItemDescriptorType::INT_ID_META => IntIdMetaItemDescriptor::read($in), + ItemDescriptorType::STRING_ID_META => StringIdMetaItemDescriptor::read($in), + ItemDescriptorType::TAG => TagItemDescriptor::read($in), + ItemDescriptorType::MOLANG => MolangItemDescriptor::read($in), + ItemDescriptorType::COMPLEX_ALIAS => ComplexAliasItemDescriptor::read($in), + default => null + }; + $count = VarInt::readSignedInt($in); + + return new RecipeIngredient($descriptor, $count); + } + + public static function putRecipeIngredient(ByteBufferWriter $out, RecipeIngredient $ingredient) : void{ + $type = $ingredient->getDescriptor(); + + Byte::writeUnsigned($out, $type?->getTypeId() ?? 0); + $type?->write($out); + + VarInt::writeSignedInt($out, $ingredient->getCount()); + } + + /** + * Decodes entity metadata from the stream. + * + * @return MetadataProperty[] + * @phpstan-return array + * + * @throws PacketDecodeException + * @throws DataDecodeException + */ + public static function getEntityMetadata(ByteBufferReader $in) : array{ + $count = VarInt::readUnsignedInt($in); + $data = []; + for($i = 0; $i < $count; ++$i){ + $key = VarInt::readUnsignedInt($in); + $type = VarInt::readUnsignedInt($in); + + $data[$key] = self::readMetadataProperty($in, $type); + } + + return $data; + } + + /** @throws DataDecodeException */ + private static function readMetadataProperty(ByteBufferReader $in, int $type) : MetadataProperty{ + return match($type){ + ByteMetadataProperty::ID => ByteMetadataProperty::read($in), + ShortMetadataProperty::ID => ShortMetadataProperty::read($in), + IntMetadataProperty::ID => IntMetadataProperty::read($in), + FloatMetadataProperty::ID => FloatMetadataProperty::read($in), + StringMetadataProperty::ID => StringMetadataProperty::read($in), + CompoundTagMetadataProperty::ID => CompoundTagMetadataProperty::read($in), + BlockPosMetadataProperty::ID => BlockPosMetadataProperty::read($in), + LongMetadataProperty::ID => LongMetadataProperty::read($in), + Vec3MetadataProperty::ID => Vec3MetadataProperty::read($in), + default => throw new PacketDecodeException("Unknown entity metadata type " . $type), + }; + } + + /** + * Writes entity metadata to the packet buffer. + * + * @param MetadataProperty[] $metadata + * + * @phpstan-param array $metadata + */ + public static function putEntityMetadata(ByteBufferWriter $out, array $metadata) : void{ + VarInt::writeUnsignedInt($out, count($metadata)); + foreach($metadata as $key => $d){ + VarInt::writeUnsignedInt($out, $key); + VarInt::writeUnsignedInt($out, $d->getTypeId()); + $d->write($out); + } + } + + /** @throws DataDecodeException */ + public static function getActorUniqueId(ByteBufferReader $in) : int{ + return VarInt::readSignedLong($in); + } + + public static function putActorUniqueId(ByteBufferWriter $out, int $eid) : void{ + VarInt::writeSignedLong($out, $eid); + } + + /** @throws DataDecodeException */ + public static function getActorRuntimeId(ByteBufferReader $in) : int{ + return VarInt::readUnsignedLong($in); + } + + public static function putActorRuntimeId(ByteBufferWriter $out, int $eid) : void{ + VarInt::writeUnsignedLong($out, $eid); + } + + /** + * Reads a block position with unsigned Y coordinate. + * + * @throws DataDecodeException + */ + public static function getBlockPosition(ByteBufferReader $in) : BlockPosition{ + $x = VarInt::readSignedInt($in); + $y = Binary::signInt(VarInt::readUnsignedInt($in)); //Y coordinate may be signed, but it's written unsigned :< + $z = VarInt::readSignedInt($in); + return new BlockPosition($x, $y, $z); + } + + /** + * Writes a block position with unsigned Y coordinate. + */ + public static function putBlockPosition(ByteBufferWriter $out, BlockPosition $blockPosition) : void{ + VarInt::writeSignedInt($out, $blockPosition->getX()); + VarInt::writeUnsignedInt($out, Binary::unsignInt($blockPosition->getY())); //Y coordinate may be signed, but it's written unsigned :< + VarInt::writeSignedInt($out, $blockPosition->getZ()); + } + + /** + * Reads a block position with a signed Y coordinate. + * + * @throws DataDecodeException + */ + public static function getSignedBlockPosition(ByteBufferReader $in) : BlockPosition{ + $x = VarInt::readSignedInt($in); + $y = VarInt::readSignedInt($in); + $z = VarInt::readSignedInt($in); + return new BlockPosition($x, $y, $z); + } + + /** + * Writes a block position with a signed Y coordinate. + */ + public static function putSignedBlockPosition(ByteBufferWriter $out, BlockPosition $blockPosition) : void{ + VarInt::writeSignedInt($out, $blockPosition->getX()); + VarInt::writeSignedInt($out, $blockPosition->getY()); + VarInt::writeSignedInt($out, $blockPosition->getZ()); + } + + /** + * Reads a floating-point Vector3 object with coordinates rounded to 4 decimal places. + * + * @throws DataDecodeException + */ + public static function getVector3(ByteBufferReader $in) : Vector3{ + $x = LE::readFloat($in); + $y = LE::readFloat($in); + $z = LE::readFloat($in); + return new Vector3($x, $y, $z); + } + + /** + * Reads a floating-point Vector2 object with coordinates rounded to 4 decimal places. + * + * @throws DataDecodeException + */ + public static function getVector2(ByteBufferReader $in) : Vector2{ + $x = LE::readFloat($in); + $y = LE::readFloat($in); + return new Vector2($x, $y); + } + + /** + * Writes a floating-point Vector3 object, or 3x zero if null is given. + * + * Note: ONLY use this where it is reasonable to allow not specifying the vector. + * For all other purposes, use the non-nullable version. + * + * @see CommonTypes::putVector3() + */ + public static function putVector3Nullable(ByteBufferWriter $out, ?Vector3 $vector) : void{ + if($vector !== null){ + self::putVector3($out, $vector); + }else{ + LE::writeFloat($out, 0.0); + LE::writeFloat($out, 0.0); + LE::writeFloat($out, 0.0); + } + } + + /** + * Writes a floating-point Vector3 object + */ + public static function putVector3(ByteBufferWriter $out, Vector3 $vector) : void{ + LE::writeFloat($out, $vector->x); + LE::writeFloat($out, $vector->y); + LE::writeFloat($out, $vector->z); + } + + /** + * Writes a floating-point Vector2 object + */ + public static function putVector2(ByteBufferWriter $out, Vector2 $vector2) : void{ + LE::writeFloat($out, $vector2->x); + LE::writeFloat($out, $vector2->y); + } + + /** @throws DataDecodeException */ + public static function getRotationByte(ByteBufferReader $in) : float{ + return Byte::readUnsigned($in) * (360 / 256); + } + + public static function putRotationByte(ByteBufferWriter $out, float $rotation) : void{ + Byte::writeUnsigned($out, (int) ($rotation / (360 / 256))); + } + + /** @throws DataDecodeException */ + private static function readGameRule(ByteBufferReader $in, int $type, bool $isPlayerModifiable) : GameRule{ + return match($type){ + BoolGameRule::ID => BoolGameRule::decode($in, $isPlayerModifiable), + IntGameRule::ID => IntGameRule::decode($in, $isPlayerModifiable), + FloatGameRule::ID => FloatGameRule::decode($in, $isPlayerModifiable), + default => throw new PacketDecodeException("Unknown gamerule type $type"), + }; + } + + /** + * Reads gamerules + * + * @return GameRule[] game rule name => value + * @phpstan-return array + * + * @throws PacketDecodeException + * @throws DataDecodeException + */ + public static function getGameRules(ByteBufferReader $in) : array{ + $count = VarInt::readUnsignedInt($in); + $rules = []; + for($i = 0; $i < $count; ++$i){ + $name = self::getString($in); + $isPlayerModifiable = self::getBool($in); + $type = VarInt::readUnsignedInt($in); + $rules[$name] = self::readGameRule($in, $type, $isPlayerModifiable); + } + + return $rules; + } + + /** + * Writes a gamerule array + * + * @param GameRule[] $rules + * @phpstan-param array $rules + */ + public static function putGameRules(ByteBufferWriter $out, array $rules) : void{ + VarInt::writeUnsignedInt($out, count($rules)); + foreach($rules as $name => $rule){ + self::putString($out, $name); + self::putBool($out, $rule->isPlayerModifiable()); + VarInt::writeUnsignedInt($out, $rule->getTypeId()); + $rule->encode($out); + } + } + + /** @throws DataDecodeException */ + public static function getEntityLink(ByteBufferReader $in) : EntityLink{ + $fromActorUniqueId = self::getActorUniqueId($in); + $toActorUniqueId = self::getActorUniqueId($in); + $type = Byte::readUnsigned($in); + $immediate = self::getBool($in); + $causedByRider = self::getBool($in); + $vehicleAngularVelocity = LE::readFloat($in); + return new EntityLink($fromActorUniqueId, $toActorUniqueId, $type, $immediate, $causedByRider, $vehicleAngularVelocity); + } + + public static function putEntityLink(ByteBufferWriter $out, EntityLink $link) : void{ + self::putActorUniqueId($out, $link->fromActorUniqueId); + self::putActorUniqueId($out, $link->toActorUniqueId); + Byte::writeUnsigned($out, $link->type); + self::putBool($out, $link->immediate); + self::putBool($out, $link->causedByRider); + LE::writeFloat($out, $link->vehicleAngularVelocity); + } + + /** @throws DataDecodeException */ + public static function getCommandOriginData(ByteBufferReader $in) : CommandOriginData{ + $result = new CommandOriginData(); + + $result->type = VarInt::readUnsignedInt($in); + $result->uuid = self::getUUID($in); + $result->requestId = self::getString($in); + + if($result->type === CommandOriginData::ORIGIN_DEV_CONSOLE or $result->type === CommandOriginData::ORIGIN_TEST){ + $result->playerActorUniqueId = VarInt::readSignedLong($in); + } + + return $result; + } + + public static function putCommandOriginData(ByteBufferWriter $out, CommandOriginData $data) : void{ + VarInt::writeUnsignedInt($out, $data->type); + self::putUUID($out, $data->uuid); + self::putString($out, $data->requestId); + + if($data->type === CommandOriginData::ORIGIN_DEV_CONSOLE or $data->type === CommandOriginData::ORIGIN_TEST){ + VarInt::writeSignedLong($out, $data->playerActorUniqueId); + } + } + + /** @throws DataDecodeException */ + public static function getStructureSettings(ByteBufferReader $in) : StructureSettings{ + $result = new StructureSettings(); + + $result->paletteName = self::getString($in); + + $result->ignoreEntities = self::getBool($in); + $result->ignoreBlocks = self::getBool($in); + $result->allowNonTickingChunks = self::getBool($in); + + $result->dimensions = self::getBlockPosition($in); + $result->offset = self::getBlockPosition($in); + + $result->lastTouchedByPlayerID = self::getActorUniqueId($in); + $result->rotation = Byte::readUnsigned($in); + $result->mirror = Byte::readUnsigned($in); + $result->animationMode = Byte::readUnsigned($in); + $result->animationSeconds = LE::readFloat($in); + $result->integrityValue = LE::readFloat($in); + $result->integritySeed = LE::readUnsignedInt($in); + $result->pivot = self::getVector3($in); + + return $result; + } + + public static function putStructureSettings(ByteBufferWriter $out, StructureSettings $structureSettings) : void{ + self::putString($out, $structureSettings->paletteName); + + self::putBool($out, $structureSettings->ignoreEntities); + self::putBool($out, $structureSettings->ignoreBlocks); + self::putBool($out, $structureSettings->allowNonTickingChunks); + + self::putBlockPosition($out, $structureSettings->dimensions); + self::putBlockPosition($out, $structureSettings->offset); + + self::putActorUniqueId($out, $structureSettings->lastTouchedByPlayerID); + Byte::writeUnsigned($out, $structureSettings->rotation); + Byte::writeUnsigned($out, $structureSettings->mirror); + Byte::writeUnsigned($out, $structureSettings->animationMode); + LE::writeFloat($out, $structureSettings->animationSeconds); + LE::writeFloat($out, $structureSettings->integrityValue); + LE::writeUnsignedInt($out, $structureSettings->integritySeed); + self::putVector3($out, $structureSettings->pivot); + } + + /** @throws DataDecodeException */ + public static function getStructureEditorData(ByteBufferReader $in) : StructureEditorData{ + $result = new StructureEditorData(); + + $result->structureName = self::getString($in); + $result->filteredStructureName = self::getString($in); + $result->structureDataField = self::getString($in); + + $result->includePlayers = self::getBool($in); + $result->showBoundingBox = self::getBool($in); + + $result->structureBlockType = VarInt::readSignedInt($in); + $result->structureSettings = self::getStructureSettings($in); + $result->structureRedstoneSaveMode = VarInt::readSignedInt($in); + + return $result; + } + + public static function putStructureEditorData(ByteBufferWriter $out, StructureEditorData $structureEditorData) : void{ + self::putString($out, $structureEditorData->structureName); + self::putString($out, $structureEditorData->filteredStructureName); + self::putString($out, $structureEditorData->structureDataField); + + self::putBool($out, $structureEditorData->includePlayers); + self::putBool($out, $structureEditorData->showBoundingBox); + + VarInt::writeSignedInt($out, $structureEditorData->structureBlockType); + self::putStructureSettings($out, $structureEditorData->structureSettings); + VarInt::writeSignedInt($out, $structureEditorData->structureRedstoneSaveMode); + } + + /** @throws PacketDecodeException */ + public static function getNbtRoot(ByteBufferReader $in) : TreeRoot{ + $offset = $in->getOffset(); + try{ + return (new NetworkNbtSerializer())->read($in->getData(), $offset, 512); + }catch(NbtDataException $e){ + throw PacketDecodeException::wrap($e, "Failed decoding NBT root"); + }finally{ + $in->setOffset($offset); + } + } + + public static function getNbtCompoundRoot(ByteBufferReader $in) : CompoundTag{ + try{ + return self::getNbtRoot($in)->mustGetCompoundTag(); + }catch(NbtDataException $e){ + throw PacketDecodeException::wrap($e, "Expected TAG_Compound NBT root"); + } + } + + /** @throws DataDecodeException */ + public static function readRecipeNetId(ByteBufferReader $in) : int{ + return VarInt::readUnsignedInt($in); + } + + public static function writeRecipeNetId(ByteBufferWriter $out, int $id) : void{ + VarInt::writeUnsignedInt($out, $id); + } + + /** @throws DataDecodeException */ + public static function readCreativeItemNetId(ByteBufferReader $in) : int{ + return VarInt::readUnsignedInt($in); + } + + public static function writeCreativeItemNetId(ByteBufferWriter $out, int $id) : void{ + VarInt::writeUnsignedInt($out, $id); + } + + /** + * This is a union of ItemStackRequestId, LegacyItemStackRequestId, and ServerItemStackId, used in serverbound + * packets to allow the client to refer to server known items, or items which may have been modified by a previous + * as-yet unacknowledged request from the client. + * + * - Server itemstack ID is positive + * - InventoryTransaction "legacy" request ID is negative and even + * - ItemStackRequest request ID is negative and odd + * - 0 refers to an empty itemstack (air) + * + * @throws DataDecodeException + */ + public static function readItemStackNetIdVariant(ByteBufferReader $in) : int{ + return VarInt::readSignedInt($in); + } + + /** + * This is a union of ItemStackRequestId, LegacyItemStackRequestId, and ServerItemStackId, used in serverbound + * packets to allow the client to refer to server known items, or items which may have been modified by a previous + * as-yet unacknowledged request from the client. + */ + public static function writeItemStackNetIdVariant(ByteBufferWriter $out, int $id) : void{ + VarInt::writeSignedInt($out, $id); + } + + /** @throws DataDecodeException */ + public static function readItemStackRequestId(ByteBufferReader $in) : int{ + return VarInt::readSignedInt($in); + } + + public static function writeItemStackRequestId(ByteBufferWriter $out, int $id) : void{ + VarInt::writeSignedInt($out, $id); + } + + /** @throws DataDecodeException */ + public static function readLegacyItemStackRequestId(ByteBufferReader $in) : int{ + return VarInt::readSignedInt($in); + } + + public static function writeLegacyItemStackRequestId(ByteBufferWriter $out, int $id) : void{ + VarInt::writeSignedInt($out, $id); + } + + /** @throws DataDecodeException */ + public static function readServerItemStackId(ByteBufferReader $in) : int{ + return VarInt::readSignedInt($in); + } + + public static function writeServerItemStackId(ByteBufferWriter $out, int $id) : void{ + VarInt::writeSignedInt($out, $id); + } + + /** + * @phpstan-template T + * @phpstan-param \Closure(ByteBufferReader) : T $reader + * @phpstan-return T|null + * @throws DataDecodeException + */ + public static function readOptional(ByteBufferReader $in, \Closure $reader) : mixed{ + if(self::getBool($in)){ + return $reader($in); + } + return null; + } + + /** + * @phpstan-template T + * @phpstan-param T|null $value + * @phpstan-param \Closure(ByteBufferWriter, T) : void $writer + */ + public static function writeOptional(ByteBufferWriter $out, mixed $value, \Closure $writer) : void{ + if($value !== null){ + self::putBool($out, true); + $writer($out, $value); + }else{ + self::putBool($out, false); + } + } +} diff --git a/src/serializer/PacketBatch.php b/src/serializer/PacketBatch.php index c02b5c7a..cad938b3 100644 --- a/src/serializer/PacketBatch.php +++ b/src/serializer/PacketBatch.php @@ -14,11 +14,13 @@ namespace pocketmine\network\mcpe\protocol\serializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\DataDecodeException; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\Packet; use pocketmine\network\mcpe\protocol\PacketDecodeException; use pocketmine\network\mcpe\protocol\PacketPool; -use pocketmine\utils\BinaryDataException; -use pocketmine\utils\BinaryStream; use function strlen; class PacketBatch{ @@ -31,13 +33,13 @@ private function __construct(){ * @phpstan-return \Generator * @throws PacketDecodeException */ - final public static function decodeRaw(BinaryStream $stream) : \Generator{ + final public static function decodeRaw(ByteBufferReader $in) : \Generator{ $c = 0; - while(!$stream->feof()){ + while($in->getUnreadLength() > 0){ try{ - $length = $stream->getUnsignedVarInt(); - $buffer = $stream->get($length); - }catch(BinaryDataException $e){ + $length = VarInt::readUnsignedInt($in); + $buffer = $in->readByteArray($length); + }catch(DataDecodeException $e){ throw new PacketDecodeException("Error decoding packet $c in batch: " . $e->getMessage(), 0, $e); } yield $buffer; @@ -49,10 +51,10 @@ final public static function decodeRaw(BinaryStream $stream) : \Generator{ * @param string[] $packets * @phpstan-param list $packets */ - final public static function encodeRaw(BinaryStream $stream, array $packets) : void{ + final public static function encodeRaw(ByteBufferWriter $out, array $packets) : void{ foreach($packets as $packet){ - $stream->putUnsignedVarInt(strlen($packet)); - $stream->put($packet); + VarInt::writeUnsignedInt($out, strlen($packet)); + $out->writeByteArray($packet); } } @@ -60,13 +62,15 @@ final public static function encodeRaw(BinaryStream $stream, array $packets) : v * @phpstan-return \Generator * @throws PacketDecodeException */ - final public static function decodePackets(BinaryStream $stream, PacketPool $packetPool) : \Generator{ + final public static function decodePackets(ByteBufferReader $in, PacketPool $packetPool) : \Generator{ $c = 0; - foreach(self::decodeRaw($stream) as $packetBuffer){ + foreach(self::decodeRaw($in) as $packetBuffer){ $packet = $packetPool->getPacket($packetBuffer); if($packet !== null){ try{ - $packet->decode(PacketSerializer::decoder($packetBuffer, 0)); + //TODO: this could use a view with a start and end offset to avoid extra string allocations + //currently ByteBufferReader doesn't support this + $packet->decode(new ByteBufferReader($packetBuffer)); }catch(PacketDecodeException $e){ throw new PacketDecodeException("Error decoding packet $c in batch: " . $e->getMessage(), 0, $e); } @@ -82,12 +86,14 @@ final public static function decodePackets(BinaryStream $stream, PacketPool $pac * @param Packet[] $packets * @phpstan-param list $packets */ - final public static function encodePackets(BinaryStream $stream, array $packets) : void{ + final public static function encodePackets(ByteBufferWriter $out, array $packets) : void{ foreach($packets as $packet){ - $serializer = PacketSerializer::encoder(); + $serializer = new ByteBufferWriter(); $packet->encode($serializer); - $stream->putUnsignedVarInt(strlen($serializer->getBuffer())); - $stream->put($serializer->getBuffer()); + //this may require a copy, so don't call it twice + $packetBuffer = $serializer->getData(); + VarInt::writeUnsignedInt($out, strlen($packetBuffer)); + $out->writeByteArray($packetBuffer); } } } diff --git a/src/serializer/PacketSerializer.php b/src/serializer/PacketSerializer.php deleted file mode 100644 index 00657b04..00000000 --- a/src/serializer/PacketSerializer.php +++ /dev/null @@ -1,819 +0,0 @@ - - * - * BedrockProtocol is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - */ - -declare(strict_types=1); - -namespace pocketmine\network\mcpe\protocol\serializer; - -use pocketmine\math\Vector2; -use pocketmine\math\Vector3; -use pocketmine\nbt\NbtDataException; -use pocketmine\nbt\tag\CompoundTag; -use pocketmine\nbt\TreeRoot; -use pocketmine\network\mcpe\protocol\PacketDecodeException; -use pocketmine\network\mcpe\protocol\types\BlockPosition; -use pocketmine\network\mcpe\protocol\types\BoolGameRule; -use pocketmine\network\mcpe\protocol\types\command\CommandOriginData; -use pocketmine\network\mcpe\protocol\types\entity\BlockPosMetadataProperty; -use pocketmine\network\mcpe\protocol\types\entity\ByteMetadataProperty; -use pocketmine\network\mcpe\protocol\types\entity\CompoundTagMetadataProperty; -use pocketmine\network\mcpe\protocol\types\entity\EntityLink; -use pocketmine\network\mcpe\protocol\types\entity\FloatMetadataProperty; -use pocketmine\network\mcpe\protocol\types\entity\IntMetadataProperty; -use pocketmine\network\mcpe\protocol\types\entity\LongMetadataProperty; -use pocketmine\network\mcpe\protocol\types\entity\MetadataProperty; -use pocketmine\network\mcpe\protocol\types\entity\ShortMetadataProperty; -use pocketmine\network\mcpe\protocol\types\entity\StringMetadataProperty; -use pocketmine\network\mcpe\protocol\types\entity\Vec3MetadataProperty; -use pocketmine\network\mcpe\protocol\types\FloatGameRule; -use pocketmine\network\mcpe\protocol\types\GameRule; -use pocketmine\network\mcpe\protocol\types\IntGameRule; -use pocketmine\network\mcpe\protocol\types\inventory\ItemStack; -use pocketmine\network\mcpe\protocol\types\inventory\ItemStackWrapper; -use pocketmine\network\mcpe\protocol\types\recipe\ComplexAliasItemDescriptor; -use pocketmine\network\mcpe\protocol\types\recipe\IntIdMetaItemDescriptor; -use pocketmine\network\mcpe\protocol\types\recipe\ItemDescriptorType; -use pocketmine\network\mcpe\protocol\types\recipe\MolangItemDescriptor; -use pocketmine\network\mcpe\protocol\types\recipe\RecipeIngredient; -use pocketmine\network\mcpe\protocol\types\recipe\StringIdMetaItemDescriptor; -use pocketmine\network\mcpe\protocol\types\recipe\TagItemDescriptor; -use pocketmine\network\mcpe\protocol\types\skin\PersonaPieceTintColor; -use pocketmine\network\mcpe\protocol\types\skin\PersonaSkinPiece; -use pocketmine\network\mcpe\protocol\types\skin\SkinAnimation; -use pocketmine\network\mcpe\protocol\types\skin\SkinData; -use pocketmine\network\mcpe\protocol\types\skin\SkinImage; -use pocketmine\network\mcpe\protocol\types\StructureEditorData; -use pocketmine\network\mcpe\protocol\types\StructureSettings; -use pocketmine\utils\Binary; -use pocketmine\utils\BinaryDataException; -use pocketmine\utils\BinaryStream; -use Ramsey\Uuid\Uuid; -use Ramsey\Uuid\UuidInterface; -use function count; -use function strlen; -use function strrev; -use function substr; - -class PacketSerializer extends BinaryStream{ - protected function __construct(string $buffer = "", int $offset = 0){ - //overridden to change visibility - parent::__construct($buffer, $offset); - } - - public static function encoder() : self{ - return new self(); - } - - public static function decoder(string $buffer, int $offset) : self{ - return new self($buffer, $offset); - } - - /** - * @throws BinaryDataException - */ - public function getString() : string{ - return $this->get($this->getUnsignedVarInt()); - } - - public function putString(string $v) : void{ - $this->putUnsignedVarInt(strlen($v)); - $this->put($v); - } - - /** - * @throws BinaryDataException - */ - public function getUUID() : UuidInterface{ - //This is two little-endian longs: bytes 7-0 followed by bytes 15-8 - $p1 = strrev($this->get(8)); - $p2 = strrev($this->get(8)); - return Uuid::fromBytes($p1 . $p2); - } - - public function putUUID(UuidInterface $uuid) : void{ - $bytes = $uuid->getBytes(); - $this->put(strrev(substr($bytes, 0, 8))); - $this->put(strrev(substr($bytes, 8, 8))); - } - - public function getSkin() : SkinData{ - $skinId = $this->getString(); - $skinPlayFabId = $this->getString(); - $skinResourcePatch = $this->getString(); - $skinData = $this->getSkinImage(); - $animationCount = $this->getLInt(); - $animations = []; - for($i = 0; $i < $animationCount; ++$i){ - $skinImage = $this->getSkinImage(); - $animationType = $this->getLInt(); - $animationFrames = $this->getLFloat(); - $expressionType = $this->getLInt(); - $animations[] = new SkinAnimation($skinImage, $animationType, $animationFrames, $expressionType); - } - $capeData = $this->getSkinImage(); - $geometryData = $this->getString(); - $geometryDataVersion = $this->getString(); - $animationData = $this->getString(); - $capeId = $this->getString(); - $fullSkinId = $this->getString(); - $armSize = $this->getString(); - $skinColor = $this->getString(); - $personaPieceCount = $this->getLInt(); - $personaPieces = []; - for($i = 0; $i < $personaPieceCount; ++$i){ - $pieceId = $this->getString(); - $pieceType = $this->getString(); - $packId = $this->getString(); - $isDefaultPiece = $this->getBool(); - $productId = $this->getString(); - $personaPieces[] = new PersonaSkinPiece($pieceId, $pieceType, $packId, $isDefaultPiece, $productId); - } - $pieceTintColorCount = $this->getLInt(); - $pieceTintColors = []; - for($i = 0; $i < $pieceTintColorCount; ++$i){ - $pieceType = $this->getString(); - $colorCount = $this->getLInt(); - $colors = []; - for($j = 0; $j < $colorCount; ++$j){ - $colors[] = $this->getString(); - } - $pieceTintColors[] = new PersonaPieceTintColor( - $pieceType, - $colors - ); - } - - $premium = $this->getBool(); - $persona = $this->getBool(); - $capeOnClassic = $this->getBool(); - $isPrimaryUser = $this->getBool(); - $override = $this->getBool(); - - return new SkinData( - $skinId, - $skinPlayFabId, - $skinResourcePatch, - $skinData, - $animations, - $capeData, - $geometryData, - $geometryDataVersion, - $animationData, - $capeId, - $fullSkinId, - $armSize, - $skinColor, - $personaPieces, - $pieceTintColors, - true, - $premium, - $persona, - $capeOnClassic, - $isPrimaryUser, - $override, - ); - } - - public function putSkin(SkinData $skin) : void{ - $this->putString($skin->getSkinId()); - $this->putString($skin->getPlayFabId()); - $this->putString($skin->getResourcePatch()); - $this->putSkinImage($skin->getSkinImage()); - $this->putLInt(count($skin->getAnimations())); - foreach($skin->getAnimations() as $animation){ - $this->putSkinImage($animation->getImage()); - $this->putLInt($animation->getType()); - $this->putLFloat($animation->getFrames()); - $this->putLInt($animation->getExpressionType()); - } - $this->putSkinImage($skin->getCapeImage()); - $this->putString($skin->getGeometryData()); - $this->putString($skin->getGeometryDataEngineVersion()); - $this->putString($skin->getAnimationData()); - $this->putString($skin->getCapeId()); - $this->putString($skin->getFullSkinId()); - $this->putString($skin->getArmSize()); - $this->putString($skin->getSkinColor()); - $this->putLInt(count($skin->getPersonaPieces())); - foreach($skin->getPersonaPieces() as $piece){ - $this->putString($piece->getPieceId()); - $this->putString($piece->getPieceType()); - $this->putString($piece->getPackId()); - $this->putBool($piece->isDefaultPiece()); - $this->putString($piece->getProductId()); - } - $this->putLInt(count($skin->getPieceTintColors())); - foreach($skin->getPieceTintColors() as $tint){ - $this->putString($tint->getPieceType()); - $this->putLInt(count($tint->getColors())); - foreach($tint->getColors() as $color){ - $this->putString($color); - } - } - $this->putBool($skin->isPremium()); - $this->putBool($skin->isPersona()); - $this->putBool($skin->isPersonaCapeOnClassic()); - $this->putBool($skin->isPrimaryUser()); - $this->putBool($skin->isOverride()); - } - - private function getSkinImage() : SkinImage{ - $width = $this->getLInt(); - $height = $this->getLInt(); - $data = $this->getString(); - try{ - return new SkinImage($height, $width, $data); - }catch(\InvalidArgumentException $e){ - throw new PacketDecodeException($e->getMessage(), 0, $e); - } - } - - private function putSkinImage(SkinImage $image) : void{ - $this->putLInt($image->getWidth()); - $this->putLInt($image->getHeight()); - $this->putString($image->getData()); - } - - /** - * @return int[] - * @phpstan-return array{0: int, 1: int, 2: int} - * @throws BinaryDataException - */ - private function getItemStackHeader() : array{ - $id = $this->getVarInt(); - if($id === 0){ - return [0, 0, 0]; - } - - $count = $this->getLShort(); - $meta = $this->getUnsignedVarInt(); - - return [$id, $count, $meta]; - } - - private function putItemStackHeader(ItemStack $itemStack) : bool{ - if($itemStack->getId() === 0){ - $this->putVarInt(0); - return false; - } - - $this->putVarInt($itemStack->getId()); - $this->putLShort($itemStack->getCount()); - $this->putUnsignedVarInt($itemStack->getMeta()); - - return true; - } - - private function getItemStackFooter(int $id, int $meta, int $count) : ItemStack{ - $blockRuntimeId = $this->getVarInt(); - $rawExtraData = $this->getString(); - - return new ItemStack($id, $meta, $count, $blockRuntimeId, $rawExtraData); - } - - private function putItemStackFooter(ItemStack $itemStack) : void{ - $this->putVarInt($itemStack->getBlockRuntimeId()); - $this->putString($itemStack->getRawExtraData()); - } - - /** - * @throws PacketDecodeException - * @throws BinaryDataException - */ - public function getItemStackWithoutStackId() : ItemStack{ - [$id, $count, $meta] = $this->getItemStackHeader(); - - return $id !== 0 ? $this->getItemStackFooter($id, $meta, $count) : ItemStack::null(); - - } - - public function putItemStackWithoutStackId(ItemStack $itemStack) : void{ - if($this->putItemStackHeader($itemStack)){ - $this->putItemStackFooter($itemStack); - } - } - - public function getItemStackWrapper() : ItemStackWrapper{ - [$id, $count, $meta] = $this->getItemStackHeader(); - if($id === 0){ - return new ItemStackWrapper(0, ItemStack::null()); - } - - $hasNetId = $this->getBool(); - $stackId = $hasNetId ? $this->readServerItemStackId() : 0; - - $itemStack = $this->getItemStackFooter($id, $meta, $count); - - return new ItemStackWrapper($stackId, $itemStack); - } - - public function putItemStackWrapper(ItemStackWrapper $itemStackWrapper) : void{ - $itemStack = $itemStackWrapper->getItemStack(); - if($this->putItemStackHeader($itemStack)){ - $hasNetId = $itemStackWrapper->getStackId() !== 0; - $this->putBool($hasNetId); - if($hasNetId){ - $this->writeServerItemStackId($itemStackWrapper->getStackId()); - } - - $this->putItemStackFooter($itemStack); - } - } - - public function getRecipeIngredient() : RecipeIngredient{ - $descriptorType = $this->getByte(); - $descriptor = match($descriptorType){ - ItemDescriptorType::INT_ID_META => IntIdMetaItemDescriptor::read($this), - ItemDescriptorType::STRING_ID_META => StringIdMetaItemDescriptor::read($this), - ItemDescriptorType::TAG => TagItemDescriptor::read($this), - ItemDescriptorType::MOLANG => MolangItemDescriptor::read($this), - ItemDescriptorType::COMPLEX_ALIAS => ComplexAliasItemDescriptor::read($this), - default => null - }; - $count = $this->getVarInt(); - - return new RecipeIngredient($descriptor, $count); - } - - public function putRecipeIngredient(RecipeIngredient $ingredient) : void{ - $type = $ingredient->getDescriptor(); - - $this->putByte($type?->getTypeId() ?? 0); - $type?->write($this); - - $this->putVarInt($ingredient->getCount()); - } - - /** - * Decodes entity metadata from the stream. - * - * @return MetadataProperty[] - * @phpstan-return array - * - * @throws PacketDecodeException - * @throws BinaryDataException - */ - public function getEntityMetadata() : array{ - $count = $this->getUnsignedVarInt(); - $data = []; - for($i = 0; $i < $count; ++$i){ - $key = $this->getUnsignedVarInt(); - $type = $this->getUnsignedVarInt(); - - $data[$key] = $this->readMetadataProperty($type); - } - - return $data; - } - - private function readMetadataProperty(int $type) : MetadataProperty{ - return match($type){ - ByteMetadataProperty::ID => ByteMetadataProperty::read($this), - ShortMetadataProperty::ID => ShortMetadataProperty::read($this), - IntMetadataProperty::ID => IntMetadataProperty::read($this), - FloatMetadataProperty::ID => FloatMetadataProperty::read($this), - StringMetadataProperty::ID => StringMetadataProperty::read($this), - CompoundTagMetadataProperty::ID => CompoundTagMetadataProperty::read($this), - BlockPosMetadataProperty::ID => BlockPosMetadataProperty::read($this), - LongMetadataProperty::ID => LongMetadataProperty::read($this), - Vec3MetadataProperty::ID => Vec3MetadataProperty::read($this), - default => throw new PacketDecodeException("Unknown entity metadata type " . $type), - }; - } - - /** - * Writes entity metadata to the packet buffer. - * - * @param MetadataProperty[] $metadata - * - * @phpstan-param array $metadata - */ - public function putEntityMetadata(array $metadata) : void{ - $this->putUnsignedVarInt(count($metadata)); - foreach($metadata as $key => $d){ - $this->putUnsignedVarInt($key); - $this->putUnsignedVarInt($d->getTypeId()); - $d->write($this); - } - } - - /** - * @throws BinaryDataException - */ - final public function getActorUniqueId() : int{ - return $this->getVarLong(); - } - - public function putActorUniqueId(int $eid) : void{ - $this->putVarLong($eid); - } - - /** - * @throws BinaryDataException - */ - final public function getActorRuntimeId() : int{ - return $this->getUnsignedVarLong(); - } - - public function putActorRuntimeId(int $eid) : void{ - $this->putUnsignedVarLong($eid); - } - - /** - * Reads a block position with unsigned Y coordinate. - * - * @throws BinaryDataException - */ - public function getBlockPosition() : BlockPosition{ - $x = $this->getVarInt(); - $y = Binary::signInt($this->getUnsignedVarInt()); //Y coordinate may be signed, but it's written unsigned :< - $z = $this->getVarInt(); - return new BlockPosition($x, $y, $z); - } - - /** - * Writes a block position with unsigned Y coordinate. - */ - public function putBlockPosition(BlockPosition $blockPosition) : void{ - $this->putVarInt($blockPosition->getX()); - $this->putUnsignedVarInt(Binary::unsignInt($blockPosition->getY())); //Y coordinate may be signed, but it's written unsigned :< - $this->putVarInt($blockPosition->getZ()); - } - - /** - * Reads a block position with a signed Y coordinate. - * - * @throws BinaryDataException - */ - public function getSignedBlockPosition() : BlockPosition{ - $x = $this->getVarInt(); - $y = $this->getVarInt(); - $z = $this->getVarInt(); - return new BlockPosition($x, $y, $z); - } - - /** - * Writes a block position with a signed Y coordinate. - */ - public function putSignedBlockPosition(BlockPosition $blockPosition) : void{ - $this->putVarInt($blockPosition->getX()); - $this->putVarInt($blockPosition->getY()); - $this->putVarInt($blockPosition->getZ()); - } - - /** - * Reads a floating-point Vector3 object with coordinates rounded to 4 decimal places. - * - * @throws BinaryDataException - */ - public function getVector3() : Vector3{ - $x = $this->getLFloat(); - $y = $this->getLFloat(); - $z = $this->getLFloat(); - return new Vector3($x, $y, $z); - } - - /** - * Reads a floating-point Vector2 object with coordinates rounded to 4 decimal places. - * - * @throws BinaryDataException - */ - public function getVector2() : Vector2{ - $x = $this->getLFloat(); - $y = $this->getLFloat(); - return new Vector2($x, $y); - } - - /** - * Writes a floating-point Vector3 object, or 3x zero if null is given. - * - * Note: ONLY use this where it is reasonable to allow not specifying the vector. - * For all other purposes, use the non-nullable version. - * - * @see PacketSerializer::putVector3() - */ - public function putVector3Nullable(?Vector3 $vector) : void{ - if($vector !== null){ - $this->putVector3($vector); - }else{ - $this->putLFloat(0.0); - $this->putLFloat(0.0); - $this->putLFloat(0.0); - } - } - - /** - * Writes a floating-point Vector3 object - */ - public function putVector3(Vector3 $vector) : void{ - $this->putLFloat($vector->x); - $this->putLFloat($vector->y); - $this->putLFloat($vector->z); - } - - /** - * Writes a floating-point Vector2 object - */ - public function putVector2(Vector2 $vector2) : void{ - $this->putLFloat($vector2->x); - $this->putLFloat($vector2->y); - } - - /** - * @throws BinaryDataException - */ - public function getRotationByte() : float{ - return ($this->getByte() * (360 / 256)); - } - - public function putRotationByte(float $rotation) : void{ - $this->putByte((int) ($rotation / (360 / 256))); - } - - private function readGameRule(int $type, bool $isPlayerModifiable) : GameRule{ - return match($type){ - BoolGameRule::ID => BoolGameRule::decode($this, $isPlayerModifiable), - IntGameRule::ID => IntGameRule::decode($this, $isPlayerModifiable), - FloatGameRule::ID => FloatGameRule::decode($this, $isPlayerModifiable), - default => throw new PacketDecodeException("Unknown gamerule type $type"), - }; - } - - /** - * Reads gamerules - * - * @return GameRule[] game rule name => value - * @phpstan-return array - * - * @throws PacketDecodeException - * @throws BinaryDataException - */ - public function getGameRules() : array{ - $count = $this->getUnsignedVarInt(); - $rules = []; - for($i = 0; $i < $count; ++$i){ - $name = $this->getString(); - $isPlayerModifiable = $this->getBool(); - $type = $this->getUnsignedVarInt(); - $rules[$name] = $this->readGameRule($type, $isPlayerModifiable); - } - - return $rules; - } - - /** - * Writes a gamerule array - * - * @param GameRule[] $rules - * @phpstan-param array $rules - */ - public function putGameRules(array $rules) : void{ - $this->putUnsignedVarInt(count($rules)); - foreach($rules as $name => $rule){ - $this->putString($name); - $this->putBool($rule->isPlayerModifiable()); - $this->putUnsignedVarInt($rule->getTypeId()); - $rule->encode($this); - } - } - - /** - * @throws BinaryDataException - */ - public function getEntityLink() : EntityLink{ - $fromActorUniqueId = $this->getActorUniqueId(); - $toActorUniqueId = $this->getActorUniqueId(); - $type = $this->getByte(); - $immediate = $this->getBool(); - $causedByRider = $this->getBool(); - $vehicleAngularVelocity = $this->getLFloat(); - return new EntityLink($fromActorUniqueId, $toActorUniqueId, $type, $immediate, $causedByRider, $vehicleAngularVelocity); - } - - public function putEntityLink(EntityLink $link) : void{ - $this->putActorUniqueId($link->fromActorUniqueId); - $this->putActorUniqueId($link->toActorUniqueId); - $this->putByte($link->type); - $this->putBool($link->immediate); - $this->putBool($link->causedByRider); - $this->putLFloat($link->vehicleAngularVelocity); - } - - /** - * @throws BinaryDataException - */ - public function getCommandOriginData() : CommandOriginData{ - $result = new CommandOriginData(); - - $result->type = $this->getUnsignedVarInt(); - $result->uuid = $this->getUUID(); - $result->requestId = $this->getString(); - - if($result->type === CommandOriginData::ORIGIN_DEV_CONSOLE or $result->type === CommandOriginData::ORIGIN_TEST){ - $result->playerActorUniqueId = $this->getVarLong(); - } - - return $result; - } - - public function putCommandOriginData(CommandOriginData $data) : void{ - $this->putUnsignedVarInt($data->type); - $this->putUUID($data->uuid); - $this->putString($data->requestId); - - if($data->type === CommandOriginData::ORIGIN_DEV_CONSOLE or $data->type === CommandOriginData::ORIGIN_TEST){ - $this->putVarLong($data->playerActorUniqueId); - } - } - - public function getStructureSettings() : StructureSettings{ - $result = new StructureSettings(); - - $result->paletteName = $this->getString(); - - $result->ignoreEntities = $this->getBool(); - $result->ignoreBlocks = $this->getBool(); - $result->allowNonTickingChunks = $this->getBool(); - - $result->dimensions = $this->getBlockPosition(); - $result->offset = $this->getBlockPosition(); - - $result->lastTouchedByPlayerID = $this->getActorUniqueId(); - $result->rotation = $this->getByte(); - $result->mirror = $this->getByte(); - $result->animationMode = $this->getByte(); - $result->animationSeconds = $this->getLFloat(); - $result->integrityValue = $this->getLFloat(); - $result->integritySeed = $this->getLInt(); - $result->pivot = $this->getVector3(); - - return $result; - } - - public function putStructureSettings(StructureSettings $structureSettings) : void{ - $this->putString($structureSettings->paletteName); - - $this->putBool($structureSettings->ignoreEntities); - $this->putBool($structureSettings->ignoreBlocks); - $this->putBool($structureSettings->allowNonTickingChunks); - - $this->putBlockPosition($structureSettings->dimensions); - $this->putBlockPosition($structureSettings->offset); - - $this->putActorUniqueId($structureSettings->lastTouchedByPlayerID); - $this->putByte($structureSettings->rotation); - $this->putByte($structureSettings->mirror); - $this->putByte($structureSettings->animationMode); - $this->putLFloat($structureSettings->animationSeconds); - $this->putLFloat($structureSettings->integrityValue); - $this->putLInt($structureSettings->integritySeed); - $this->putVector3($structureSettings->pivot); - } - - public function getStructureEditorData() : StructureEditorData{ - $result = new StructureEditorData(); - - $result->structureName = $this->getString(); - $result->filteredStructureName = $this->getString(); - $result->structureDataField = $this->getString(); - - $result->includePlayers = $this->getBool(); - $result->showBoundingBox = $this->getBool(); - - $result->structureBlockType = $this->getVarInt(); - $result->structureSettings = $this->getStructureSettings(); - $result->structureRedstoneSaveMode = $this->getVarInt(); - - return $result; - } - - public function putStructureEditorData(StructureEditorData $structureEditorData) : void{ - $this->putString($structureEditorData->structureName); - $this->putString($structureEditorData->filteredStructureName); - $this->putString($structureEditorData->structureDataField); - - $this->putBool($structureEditorData->includePlayers); - $this->putBool($structureEditorData->showBoundingBox); - - $this->putVarInt($structureEditorData->structureBlockType); - $this->putStructureSettings($structureEditorData->structureSettings); - $this->putVarInt($structureEditorData->structureRedstoneSaveMode); - } - - public function getNbtRoot() : TreeRoot{ - $offset = $this->getOffset(); - try{ - return (new NetworkNbtSerializer())->read($this->getBuffer(), $offset, 512); - }catch(NbtDataException $e){ - throw PacketDecodeException::wrap($e, "Failed decoding NBT root"); - }finally{ - $this->setOffset($offset); - } - } - - public function getNbtCompoundRoot() : CompoundTag{ - try{ - return $this->getNbtRoot()->mustGetCompoundTag(); - }catch(NbtDataException $e){ - throw PacketDecodeException::wrap($e, "Expected TAG_Compound NBT root"); - } - } - - public function readRecipeNetId() : int{ - return $this->getUnsignedVarInt(); - } - - public function writeRecipeNetId(int $id) : void{ - $this->putUnsignedVarInt($id); - } - - public function readCreativeItemNetId() : int{ - return $this->getUnsignedVarInt(); - } - - public function writeCreativeItemNetId(int $id) : void{ - $this->putUnsignedVarInt($id); - } - - /** - * This is a union of ItemStackRequestId, LegacyItemStackRequestId, and ServerItemStackId, used in serverbound - * packets to allow the client to refer to server known items, or items which may have been modified by a previous - * as-yet unacknowledged request from the client. - * - * - Server itemstack ID is positive - * - InventoryTransaction "legacy" request ID is negative and even - * - ItemStackRequest request ID is negative and odd - * - 0 refers to an empty itemstack (air) - */ - public function readItemStackNetIdVariant() : int{ - return $this->getVarInt(); - } - - /** - * This is a union of ItemStackRequestId, LegacyItemStackRequestId, and ServerItemStackId, used in serverbound - * packets to allow the client to refer to server known items, or items which may have been modified by a previous - * as-yet unacknowledged request from the client. - */ - public function writeItemStackNetIdVariant(int $id) : void{ - $this->putVarInt($id); - } - - public function readItemStackRequestId() : int{ - return $this->getVarInt(); - } - - public function writeItemStackRequestId(int $id) : void{ - $this->putVarInt($id); - } - - public function readLegacyItemStackRequestId() : int{ - return $this->getVarInt(); - } - - public function writeLegacyItemStackRequestId(int $id) : void{ - $this->putVarInt($id); - } - - public function readServerItemStackId() : int{ - return $this->getVarInt(); - } - - public function writeServerItemStackId(int $id) : void{ - $this->putVarInt($id); - } - - /** - * @phpstan-template T - * @phpstan-param \Closure() : T $reader - * @phpstan-return T|null - */ - public function readOptional(\Closure $reader) : mixed{ - if($this->getBool()){ - return $reader(); - } - return null; - } - - /** - * @phpstan-template T - * @phpstan-param T|null $value - * @phpstan-param \Closure(T) : void $writer - */ - public function writeOptional(mixed $value, \Closure $writer) : void{ - if($value !== null){ - $this->putBool(true); - $writer($value); - }else{ - $this->putBool(false); - } - } -} diff --git a/src/types/AbilitiesData.php b/src/types/AbilitiesData.php index 4ad28c1d..d8a49073 100644 --- a/src/types/AbilitiesData.php +++ b/src/types/AbilitiesData.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; use function count; final class AbilitiesData{ @@ -41,25 +44,25 @@ public function getTargetActorUniqueId() : int{ return $this->targetActorUniqueI */ public function getAbilityLayers() : array{ return $this->abilityLayers; } - public static function decode(PacketSerializer $in) : self{ - $targetActorUniqueId = $in->getLLong(); //WHY IS THIS NON-STANDARD? - $playerPermission = $in->getByte(); - $commandPermission = $in->getByte(); + public static function decode(ByteBufferReader $in) : self{ + $targetActorUniqueId = LE::readSignedLong($in); //WHY IS THIS NON-STANDARD? + $playerPermission = Byte::readUnsigned($in); + $commandPermission = Byte::readUnsigned($in); $abilityLayers = []; - for($i = 0, $len = $in->getByte(); $i < $len; $i++){ + for($i = 0, $len = Byte::readUnsigned($in); $i < $len; $i++){ $abilityLayers[] = AbilitiesLayer::decode($in); } return new self($commandPermission, $playerPermission, $targetActorUniqueId, $abilityLayers); } - public function encode(PacketSerializer $out) : void{ - $out->putLLong($this->targetActorUniqueId); - $out->putByte($this->playerPermission); - $out->putByte($this->commandPermission); + public function encode(ByteBufferWriter $out) : void{ + LE::writeSignedLong($out, $this->targetActorUniqueId); + Byte::writeUnsigned($out, $this->playerPermission); + Byte::writeUnsigned($out, $this->commandPermission); - $out->putByte(count($this->abilityLayers)); + Byte::writeUnsigned($out, count($this->abilityLayers)); foreach($this->abilityLayers as $abilityLayer){ $abilityLayer->encode($out); } diff --git a/src/types/AbilitiesLayer.php b/src/types/AbilitiesLayer.php index 43cc4d9f..a1837cfe 100644 --- a/src/types/AbilitiesLayer.php +++ b/src/types/AbilitiesLayer.php @@ -14,8 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; use pocketmine\network\mcpe\protocol\PacketDecodeException; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; final class AbilitiesLayer{ @@ -76,13 +78,13 @@ public function getVerticalFlySpeed() : ?float{ return $this->verticalFlySpeed; public function getWalkSpeed() : ?float{ return $this->walkSpeed; } - public static function decode(PacketSerializer $in) : self{ - $layerId = $in->getLShort(); - $setAbilities = $in->getLInt(); - $setAbilityValues = $in->getLInt(); - $flySpeed = $in->getLFloat(); - $verticalFlySpeed = $in->getLFloat(); - $walkSpeed = $in->getLFloat(); + public static function decode(ByteBufferReader $in) : self{ + $layerId = LE::readUnsignedShort($in); + $setAbilities = LE::readUnsignedInt($in); + $setAbilityValues = LE::readUnsignedInt($in); + $flySpeed = LE::readFloat($in); + $verticalFlySpeed = LE::readFloat($in); + $walkSpeed = LE::readFloat($in); $boolAbilities = []; for($i = 0; $i < self::NUMBER_OF_ABILITIES; $i++){ @@ -115,8 +117,8 @@ public static function decode(PacketSerializer $in) : self{ return new self($layerId, $boolAbilities, $flySpeed, $verticalFlySpeed, $walkSpeed); } - public function encode(PacketSerializer $out) : void{ - $out->putLShort($this->layerId); + public function encode(ByteBufferWriter $out) : void{ + LE::writeUnsignedShort($out, $this->layerId); $setAbilities = 0; $setAbilityValues = 0; @@ -134,10 +136,10 @@ public function encode(PacketSerializer $out) : void{ $setAbilities |= (1 << self::ABILITY_WALK_SPEED); } - $out->putLInt($setAbilities); - $out->putLInt($setAbilityValues); - $out->putLFloat($this->flySpeed ?? 0); - $out->putLFloat($this->verticalFlySpeed ?? 0); - $out->putLFloat($this->walkSpeed ?? 0); + LE::writeUnsignedInt($out, $setAbilities); + LE::writeUnsignedInt($out, $setAbilityValues); + LE::writeFloat($out, $this->flySpeed ?? 0); + LE::writeFloat($out, $this->verticalFlySpeed ?? 0); + LE::writeFloat($out, $this->walkSpeed ?? 0); } } diff --git a/src/types/BoolGameRule.php b/src/types/BoolGameRule.php index aedc5f6f..11b39032 100644 --- a/src/types/BoolGameRule.php +++ b/src/types/BoolGameRule.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class BoolGameRule extends GameRule{ use GetTypeIdFromConstTrait; @@ -32,11 +34,11 @@ public function getValue() : bool{ return $this->value; } - public function encode(PacketSerializer $out) : void{ - $out->putBool($this->value); + public function encode(ByteBufferWriter $out) : void{ + CommonTypes::putBool($out, $this->value); } - public static function decode(PacketSerializer $in, bool $isPlayerModifiable) : self{ - return new self($in->getBool(), $isPlayerModifiable); + public static function decode(ByteBufferReader $in, bool $isPlayerModifiable) : self{ + return new self(CommonTypes::getBool($in), $isPlayerModifiable); } } diff --git a/src/types/ChunkPosition.php b/src/types/ChunkPosition.php index 6f9c55fd..2efc25d1 100644 --- a/src/types/ChunkPosition.php +++ b/src/types/ChunkPosition.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; final class ChunkPosition{ @@ -27,15 +29,15 @@ public function getX() : int{ return $this->x; } public function getZ() : int{ return $this->z; } - public static function read(PacketSerializer $in) : self{ - $x = $in->getVarInt(); - $z = $in->getVarInt(); + public static function read(ByteBufferReader $in) : self{ + $x = VarInt::readSignedInt($in); + $z = VarInt::readSignedInt($in); return new self($x, $z); } - public function write(PacketSerializer $out) : void{ - $out->putVarInt($this->x); - $out->putVarInt($this->z); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->x); + VarInt::writeSignedInt($out, $this->z); } } diff --git a/src/types/DimensionData.php b/src/types/DimensionData.php index ff4f3967..02db5313 100644 --- a/src/types/DimensionData.php +++ b/src/types/DimensionData.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; final class DimensionData{ @@ -30,17 +32,17 @@ public function getMinHeight() : int{ return $this->minHeight; } public function getGenerator() : int{ return $this->generator; } - public static function read(PacketSerializer $in) : self{ - $maxHeight = $in->getVarInt(); - $minHeight = $in->getVarInt(); - $generator = $in->getVarInt(); + public static function read(ByteBufferReader $in) : self{ + $maxHeight = VarInt::readSignedInt($in); + $minHeight = VarInt::readSignedInt($in); + $generator = VarInt::readSignedInt($in); return new self($maxHeight, $minHeight, $generator); } - public function write(PacketSerializer $out) : void{ - $out->putVarInt($this->maxHeight); - $out->putVarInt($this->minHeight); - $out->putVarInt($this->generator); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->maxHeight); + VarInt::writeSignedInt($out, $this->minHeight); + VarInt::writeSignedInt($out, $this->generator); } } diff --git a/src/types/EducationSettingsAgentCapabilities.php b/src/types/EducationSettingsAgentCapabilities.php index 6c5501be..a0be72ee 100644 --- a/src/types/EducationSettingsAgentCapabilities.php +++ b/src/types/EducationSettingsAgentCapabilities.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class EducationSettingsAgentCapabilities{ public function __construct( @@ -23,12 +25,12 @@ public function __construct( public function getCanModifyBlocks() : ?bool{ return $this->canModifyBlocks; } - public static function read(PacketSerializer $in) : self{ - $canModifyBlocks = $in->readOptional($in->getBool(...)); + public static function read(ByteBufferReader $in) : self{ + $canModifyBlocks = CommonTypes::readOptional($in, CommonTypes::getBool(...)); return new self($canModifyBlocks); } - public function write(PacketSerializer $out) : void{ - $out->writeOptional($this->canModifyBlocks, $out->putBool(...)); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::writeOptional($out, $this->canModifyBlocks, CommonTypes::putBool(...)); } } diff --git a/src/types/EducationSettingsExternalLinkSettings.php b/src/types/EducationSettingsExternalLinkSettings.php index 1217b9a5..d5aa1e74 100644 --- a/src/types/EducationSettingsExternalLinkSettings.php +++ b/src/types/EducationSettingsExternalLinkSettings.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class EducationSettingsExternalLinkSettings{ public function __construct( @@ -26,14 +28,14 @@ public function getUrl() : string{ return $this->url; } public function getDisplayName() : string{ return $this->displayName; } - public static function read(PacketSerializer $in) : self{ - $url = $in->getString(); - $displayName = $in->getString(); + public static function read(ByteBufferReader $in) : self{ + $url = CommonTypes::getString($in); + $displayName = CommonTypes::getString($in); return new self($displayName, $url); } - public function write(PacketSerializer $out) : void{ - $out->putString($this->url); - $out->putString($this->displayName); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->url); + CommonTypes::putString($out, $this->displayName); } } diff --git a/src/types/EducationUriResource.php b/src/types/EducationUriResource.php index bd829ae8..d7b4d89f 100644 --- a/src/types/EducationUriResource.php +++ b/src/types/EducationUriResource.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class EducationUriResource{ public function __construct( @@ -26,14 +28,14 @@ public function getButtonName() : string{ return $this->buttonName; } public function getLinkUri() : string{ return $this->linkUri; } - public static function read(PacketSerializer $in) : self{ - $buttonName = $in->getString(); - $linkUri = $in->getString(); + public static function read(ByteBufferReader $in) : self{ + $buttonName = CommonTypes::getString($in); + $linkUri = CommonTypes::getString($in); return new self($buttonName, $linkUri); } - public function write(PacketSerializer $out) : void{ - $out->putString($this->buttonName); - $out->putString($this->linkUri); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->buttonName); + CommonTypes::putString($out, $this->linkUri); } } diff --git a/src/types/Enchant.php b/src/types/Enchant.php index f139dcaf..7e446e4f 100644 --- a/src/types/Enchant.php +++ b/src/types/Enchant.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; final class Enchant{ public function __construct( @@ -26,14 +28,14 @@ public function getId() : int{ return $this->id; } public function getLevel() : int{ return $this->level; } - public static function read(PacketSerializer $in) : self{ - $id = $in->getByte(); - $level = $in->getByte(); + public static function read(ByteBufferReader $in) : self{ + $id = Byte::readUnsigned($in); + $level = Byte::readUnsigned($in); return new self($id, $level); } - public function write(PacketSerializer $out) : void{ - $out->putByte($this->id); - $out->putByte($this->level); + public function write(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->id); + Byte::writeUnsigned($out, $this->level); } } diff --git a/src/types/EnchantOption.php b/src/types/EnchantOption.php index bd9b1a8b..97d62a16 100644 --- a/src/types/EnchantOption.php +++ b/src/types/EnchantOption.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use function count; final class EnchantOption{ @@ -53,9 +57,9 @@ public function getOptionId() : int{ return $this->optionId; } /** * @return Enchant[] */ - private static function readEnchantList(PacketSerializer $in) : array{ + private static function readEnchantList(ByteBufferReader $in) : array{ $result = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ $result[] = Enchant::read($in); } return $result; @@ -64,36 +68,36 @@ private static function readEnchantList(PacketSerializer $in) : array{ /** * @param Enchant[] $list */ - private static function writeEnchantList(PacketSerializer $out, array $list) : void{ - $out->putUnsignedVarInt(count($list)); + private static function writeEnchantList(ByteBufferWriter $out, array $list) : void{ + VarInt::writeUnsignedInt($out, count($list)); foreach($list as $item){ $item->write($out); } } - public static function read(PacketSerializer $in) : self{ - $cost = $in->getUnsignedVarInt(); + public static function read(ByteBufferReader $in) : self{ + $cost = VarInt::readUnsignedInt($in); - $slotFlags = $in->getLInt(); + $slotFlags = LE::readUnsignedInt($in); $equipActivatedEnchants = self::readEnchantList($in); $heldActivatedEnchants = self::readEnchantList($in); $selfActivatedEnchants = self::readEnchantList($in); - $name = $in->getString(); - $optionId = $in->readRecipeNetId(); + $name = CommonTypes::getString($in); + $optionId = CommonTypes::readRecipeNetId($in); return new self($cost, $slotFlags, $equipActivatedEnchants, $heldActivatedEnchants, $selfActivatedEnchants, $name, $optionId); } - public function write(PacketSerializer $out) : void{ - $out->putUnsignedVarInt($this->cost); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, $this->cost); - $out->putLInt($this->slotFlags); + LE::writeUnsignedInt($out, $this->slotFlags); self::writeEnchantList($out, $this->equipActivatedEnchantments); self::writeEnchantList($out, $this->heldActivatedEnchantments); self::writeEnchantList($out, $this->selfActivatedEnchantments); - $out->putString($this->name); - $out->writeRecipeNetId($this->optionId); + CommonTypes::putString($out, $this->name); + CommonTypes::writeRecipeNetId($out, $this->optionId); } } diff --git a/src/types/Experiments.php b/src/types/Experiments.php index 9f498f92..f14a3bd1 100644 --- a/src/types/Experiments.php +++ b/src/types/Experiments.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use function count; final class Experiments{ @@ -32,23 +35,23 @@ public function getExperiments() : array{ return $this->experiments; } public function hasPreviouslyUsedExperiments() : bool{ return $this->hasPreviouslyUsedExperiments; } - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ $experiments = []; - for($i = 0, $len = $in->getLInt(); $i < $len; ++$i){ - $experimentName = $in->getString(); - $enabled = $in->getBool(); + for($i = 0, $len = LE::readUnsignedInt($in); $i < $len; ++$i){ + $experimentName = CommonTypes::getString($in); + $enabled = CommonTypes::getBool($in); $experiments[$experimentName] = $enabled; } - $hasPreviouslyUsedExperiments = $in->getBool(); + $hasPreviouslyUsedExperiments = CommonTypes::getBool($in); return new self($experiments, $hasPreviouslyUsedExperiments); } - public function write(PacketSerializer $out) : void{ - $out->putLInt(count($this->experiments)); + public function write(ByteBufferWriter $out) : void{ + LE::writeUnsignedInt($out, count($this->experiments)); foreach($this->experiments as $experimentName => $enabled){ - $out->putString($experimentName); - $out->putBool($enabled); + CommonTypes::putString($out, $experimentName); + CommonTypes::putBool($out, $enabled); } - $out->putBool($this->hasPreviouslyUsedExperiments); + CommonTypes::putBool($out, $this->hasPreviouslyUsedExperiments); } } diff --git a/src/types/FeatureRegistryPacketEntry.php b/src/types/FeatureRegistryPacketEntry.php index 3fe8d844..ad84c12e 100644 --- a/src/types/FeatureRegistryPacketEntry.php +++ b/src/types/FeatureRegistryPacketEntry.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class FeatureRegistryPacketEntry{ @@ -27,15 +29,15 @@ public function getFeatureName() : string{ return $this->featureName; } public function getFeatureJson() : string{ return $this->featureJson; } - public static function read(PacketSerializer $in) : self{ - $featureName = $in->getString(); - $featureJson = $in->getString(); + public static function read(ByteBufferReader $in) : self{ + $featureName = CommonTypes::getString($in); + $featureJson = CommonTypes::getString($in); return new self($featureName, $featureJson); } - public function write(PacketSerializer $out) : void{ - $out->putString($this->featureName); - $out->putString($this->featureJson); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->featureName); + CommonTypes::putString($out, $this->featureJson); } } diff --git a/src/types/FloatGameRule.php b/src/types/FloatGameRule.php index 13bf42f9..ab0fd71a 100644 --- a/src/types/FloatGameRule.php +++ b/src/types/FloatGameRule.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; final class FloatGameRule extends GameRule{ use GetTypeIdFromConstTrait; @@ -32,11 +34,11 @@ public function getValue() : float{ return $this->value; } - public function encode(PacketSerializer $out) : void{ - $out->putLFloat($this->value); + public function encode(ByteBufferWriter $out) : void{ + LE::writeFloat($out, $this->value); } - public static function decode(PacketSerializer $in, bool $isPlayerModifiable) : self{ - return new self($in->getLFloat(), $isPlayerModifiable); + public static function decode(ByteBufferReader $in, bool $isPlayerModifiable) : self{ + return new self(LE::readFloat($in), $isPlayerModifiable); } } diff --git a/src/types/GameRule.php b/src/types/GameRule.php index fc90ca8c..eb40d552 100644 --- a/src/types/GameRule.php +++ b/src/types/GameRule.php @@ -14,7 +14,7 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferWriter; abstract class GameRule{ public function __construct( @@ -25,5 +25,5 @@ public function isPlayerModifiable() : bool{ return $this->isPlayerModifiable; } abstract public function getTypeId() : int; - abstract public function encode(PacketSerializer $out) : void; + abstract public function encode(ByteBufferWriter $out) : void; } diff --git a/src/types/IntGameRule.php b/src/types/IntGameRule.php index 80474546..f31524be 100644 --- a/src/types/IntGameRule.php +++ b/src/types/IntGameRule.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; final class IntGameRule extends GameRule{ use GetTypeIdFromConstTrait; @@ -32,11 +34,11 @@ public function getValue() : int{ return $this->value; } - public function encode(PacketSerializer $out) : void{ - $out->putUnsignedVarInt($this->value); + public function encode(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, $this->value); } - public static function decode(PacketSerializer $in, bool $isPlayerModifiable) : self{ - return new self($in->getUnsignedVarInt(), $isPlayerModifiable); + public static function decode(ByteBufferReader $in, bool $isPlayerModifiable) : self{ + return new self(VarInt::readUnsignedInt($in), $isPlayerModifiable); } } diff --git a/src/types/ItemInteractionData.php b/src/types/ItemInteractionData.php index d83766b0..3361ff86 100644 --- a/src/types/ItemInteractionData.php +++ b/src/types/ItemInteractionData.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\types\inventory\InventoryTransactionChangedSlotsHack; use pocketmine\network\mcpe\protocol\types\inventory\UseItemTransactionData; use function count; @@ -44,11 +46,11 @@ public function getTransactionData() : UseItemTransactionData{ return $this->transactionData; } - public static function read(PacketSerializer $in) : self{ - $requestId = $in->getVarInt(); + public static function read(ByteBufferReader $in) : self{ + $requestId = VarInt::readSignedInt($in); $requestChangedSlots = []; if($requestId !== 0){ - $len = $in->getUnsignedVarInt(); + $len = VarInt::readUnsignedInt($in); for($i = 0; $i < $len; ++$i){ $requestChangedSlots[] = InventoryTransactionChangedSlotsHack::read($in); } @@ -58,10 +60,10 @@ public static function read(PacketSerializer $in) : self{ return new ItemInteractionData($requestId, $requestChangedSlots, $transactionData); } - public function write(PacketSerializer $out) : void{ - $out->putVarInt($this->requestId); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->requestId); if($this->requestId !== 0){ - $out->putUnsignedVarInt(count($this->requestChangedSlots)); + VarInt::writeUnsignedInt($out, count($this->requestChangedSlots)); foreach($this->requestChangedSlots as $changedSlot){ $changedSlot->write($out); } diff --git a/src/types/LevelSettings.php b/src/types/LevelSettings.php index 73fb339b..be4d4854 100644 --- a/src/types/LevelSettings.php +++ b/src/types/LevelSettings.php @@ -14,10 +14,15 @@ namespace pocketmine\network\mcpe\protocol\types; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\DataDecodeException; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\PacketDecodeException; use pocketmine\network\mcpe\protocol\ProtocolInfo; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; -use pocketmine\utils\BinaryDataException; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class LevelSettings{ @@ -82,10 +87,10 @@ final class LevelSettings{ public string $ownerIdentifier = ""; /** - * @throws BinaryDataException + * @throws DataDecodeException * @throws PacketDecodeException */ - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ //TODO: in the future we'll use promoted properties + named arguments for decoding, but for now we stick with //this shitty way to limit BC breaks (needs more R&D) $result = new self; @@ -94,116 +99,116 @@ public static function read(PacketSerializer $in) : self{ } /** - * @throws BinaryDataException + * @throws DataDecodeException * @throws PacketDecodeException */ - private function internalRead(PacketSerializer $in) : void{ - $this->seed = $in->getLLong(); + private function internalRead(ByteBufferReader $in) : void{ + $this->seed = LE::readUnsignedLong($in); $this->spawnSettings = SpawnSettings::read($in); - $this->generator = $in->getVarInt(); - $this->worldGamemode = $in->getVarInt(); - $this->hardcore = $in->getBool(); - $this->difficulty = $in->getVarInt(); - $this->spawnPosition = $in->getBlockPosition(); - $this->hasAchievementsDisabled = $in->getBool(); - $this->editorWorldType = $in->getVarInt(); - $this->createdInEditorMode = $in->getBool(); - $this->exportedFromEditorMode = $in->getBool(); - $this->time = $in->getVarInt(); - $this->eduEditionOffer = $in->getVarInt(); - $this->hasEduFeaturesEnabled = $in->getBool(); - $this->eduProductUUID = $in->getString(); - $this->rainLevel = $in->getLFloat(); - $this->lightningLevel = $in->getLFloat(); - $this->hasConfirmedPlatformLockedContent = $in->getBool(); - $this->isMultiplayerGame = $in->getBool(); - $this->hasLANBroadcast = $in->getBool(); - $this->xboxLiveBroadcastMode = $in->getVarInt(); - $this->platformBroadcastMode = $in->getVarInt(); - $this->commandsEnabled = $in->getBool(); - $this->isTexturePacksRequired = $in->getBool(); - $this->gameRules = $in->getGameRules(); + $this->generator = VarInt::readSignedInt($in); + $this->worldGamemode = VarInt::readSignedInt($in); + $this->hardcore = CommonTypes::getBool($in); + $this->difficulty = VarInt::readSignedInt($in); + $this->spawnPosition = CommonTypes::getBlockPosition($in); + $this->hasAchievementsDisabled = CommonTypes::getBool($in); + $this->editorWorldType = VarInt::readSignedInt($in); + $this->createdInEditorMode = CommonTypes::getBool($in); + $this->exportedFromEditorMode = CommonTypes::getBool($in); + $this->time = VarInt::readSignedInt($in); + $this->eduEditionOffer = VarInt::readSignedInt($in); + $this->hasEduFeaturesEnabled = CommonTypes::getBool($in); + $this->eduProductUUID = CommonTypes::getString($in); + $this->rainLevel = LE::readFloat($in); + $this->lightningLevel = LE::readFloat($in); + $this->hasConfirmedPlatformLockedContent = CommonTypes::getBool($in); + $this->isMultiplayerGame = CommonTypes::getBool($in); + $this->hasLANBroadcast = CommonTypes::getBool($in); + $this->xboxLiveBroadcastMode = VarInt::readSignedInt($in); + $this->platformBroadcastMode = VarInt::readSignedInt($in); + $this->commandsEnabled = CommonTypes::getBool($in); + $this->isTexturePacksRequired = CommonTypes::getBool($in); + $this->gameRules = CommonTypes::getGameRules($in); $this->experiments = Experiments::read($in); - $this->hasBonusChestEnabled = $in->getBool(); - $this->hasStartWithMapEnabled = $in->getBool(); - $this->defaultPlayerPermission = $in->getVarInt(); - $this->serverChunkTickRadius = $in->getLInt(); - $this->hasLockedBehaviorPack = $in->getBool(); - $this->hasLockedResourcePack = $in->getBool(); - $this->isFromLockedWorldTemplate = $in->getBool(); - $this->useMsaGamertagsOnly = $in->getBool(); - $this->isFromWorldTemplate = $in->getBool(); - $this->isWorldTemplateOptionLocked = $in->getBool(); - $this->onlySpawnV1Villagers = $in->getBool(); - $this->disablePersona = $in->getBool(); - $this->disableCustomSkins = $in->getBool(); - $this->muteEmoteAnnouncements = $in->getBool(); - $this->vanillaVersion = $in->getString(); - $this->limitedWorldWidth = $in->getLInt(); - $this->limitedWorldLength = $in->getLInt(); - $this->isNewNether = $in->getBool(); + $this->hasBonusChestEnabled = CommonTypes::getBool($in); + $this->hasStartWithMapEnabled = CommonTypes::getBool($in); + $this->defaultPlayerPermission = VarInt::readSignedInt($in); + $this->serverChunkTickRadius = LE::readSignedInt($in); //doesn't make sense for this to be signed, but that's what the spec says + $this->hasLockedBehaviorPack = CommonTypes::getBool($in); + $this->hasLockedResourcePack = CommonTypes::getBool($in); + $this->isFromLockedWorldTemplate = CommonTypes::getBool($in); + $this->useMsaGamertagsOnly = CommonTypes::getBool($in); + $this->isFromWorldTemplate = CommonTypes::getBool($in); + $this->isWorldTemplateOptionLocked = CommonTypes::getBool($in); + $this->onlySpawnV1Villagers = CommonTypes::getBool($in); + $this->disablePersona = CommonTypes::getBool($in); + $this->disableCustomSkins = CommonTypes::getBool($in); + $this->muteEmoteAnnouncements = CommonTypes::getBool($in); + $this->vanillaVersion = CommonTypes::getString($in); + $this->limitedWorldWidth = LE::readSignedInt($in); //doesn't make sense for this to be signed, but that's what the spec says + $this->limitedWorldLength = LE::readSignedInt($in); //same as above + $this->isNewNether = CommonTypes::getBool($in); $this->eduSharedUriResource = EducationUriResource::read($in); - $this->experimentalGameplayOverride = $in->readOptional($in->getBool(...)); - $this->chatRestrictionLevel = $in->getByte(); - $this->disablePlayerInteractions = $in->getBool(); - $this->serverIdentifier = $in->getString(); - $this->worldIdentifier = $in->getString(); - $this->scenarioIdentifier = $in->getString(); - $this->ownerIdentifier = $in->getString(); + $this->experimentalGameplayOverride = CommonTypes::readOptional($in, CommonTypes::getBool(...)); + $this->chatRestrictionLevel = Byte::readUnsigned($in); + $this->disablePlayerInteractions = CommonTypes::getBool($in); + $this->serverIdentifier = CommonTypes::getString($in); + $this->worldIdentifier = CommonTypes::getString($in); + $this->scenarioIdentifier = CommonTypes::getString($in); + $this->ownerIdentifier = CommonTypes::getString($in); } - public function write(PacketSerializer $out) : void{ - $out->putLLong($this->seed); + public function write(ByteBufferWriter $out) : void{ + LE::writeUnsignedLong($out, $this->seed); $this->spawnSettings->write($out); - $out->putVarInt($this->generator); - $out->putVarInt($this->worldGamemode); - $out->putBool($this->hardcore); - $out->putVarInt($this->difficulty); - $out->putBlockPosition($this->spawnPosition); - $out->putBool($this->hasAchievementsDisabled); - $out->putVarInt($this->editorWorldType); - $out->putBool($this->createdInEditorMode); - $out->putBool($this->exportedFromEditorMode); - $out->putVarInt($this->time); - $out->putVarInt($this->eduEditionOffer); - $out->putBool($this->hasEduFeaturesEnabled); - $out->putString($this->eduProductUUID); - $out->putLFloat($this->rainLevel); - $out->putLFloat($this->lightningLevel); - $out->putBool($this->hasConfirmedPlatformLockedContent); - $out->putBool($this->isMultiplayerGame); - $out->putBool($this->hasLANBroadcast); - $out->putVarInt($this->xboxLiveBroadcastMode); - $out->putVarInt($this->platformBroadcastMode); - $out->putBool($this->commandsEnabled); - $out->putBool($this->isTexturePacksRequired); - $out->putGameRules($this->gameRules); + VarInt::writeSignedInt($out, $this->generator); + VarInt::writeSignedInt($out, $this->worldGamemode); + CommonTypes::putBool($out, $this->hardcore); + VarInt::writeSignedInt($out, $this->difficulty); + CommonTypes::putBlockPosition($out, $this->spawnPosition); + CommonTypes::putBool($out, $this->hasAchievementsDisabled); + VarInt::writeSignedInt($out, $this->editorWorldType); + CommonTypes::putBool($out, $this->createdInEditorMode); + CommonTypes::putBool($out, $this->exportedFromEditorMode); + VarInt::writeSignedInt($out, $this->time); + VarInt::writeSignedInt($out, $this->eduEditionOffer); + CommonTypes::putBool($out, $this->hasEduFeaturesEnabled); + CommonTypes::putString($out, $this->eduProductUUID); + LE::writeFloat($out, $this->rainLevel); + LE::writeFloat($out, $this->lightningLevel); + CommonTypes::putBool($out, $this->hasConfirmedPlatformLockedContent); + CommonTypes::putBool($out, $this->isMultiplayerGame); + CommonTypes::putBool($out, $this->hasLANBroadcast); + VarInt::writeSignedInt($out, $this->xboxLiveBroadcastMode); + VarInt::writeSignedInt($out, $this->platformBroadcastMode); + CommonTypes::putBool($out, $this->commandsEnabled); + CommonTypes::putBool($out, $this->isTexturePacksRequired); + CommonTypes::putGameRules($out, $this->gameRules); $this->experiments->write($out); - $out->putBool($this->hasBonusChestEnabled); - $out->putBool($this->hasStartWithMapEnabled); - $out->putVarInt($this->defaultPlayerPermission); - $out->putLInt($this->serverChunkTickRadius); - $out->putBool($this->hasLockedBehaviorPack); - $out->putBool($this->hasLockedResourcePack); - $out->putBool($this->isFromLockedWorldTemplate); - $out->putBool($this->useMsaGamertagsOnly); - $out->putBool($this->isFromWorldTemplate); - $out->putBool($this->isWorldTemplateOptionLocked); - $out->putBool($this->onlySpawnV1Villagers); - $out->putBool($this->disablePersona); - $out->putBool($this->disableCustomSkins); - $out->putBool($this->muteEmoteAnnouncements); - $out->putString($this->vanillaVersion); - $out->putLInt($this->limitedWorldWidth); - $out->putLInt($this->limitedWorldLength); - $out->putBool($this->isNewNether); + CommonTypes::putBool($out, $this->hasBonusChestEnabled); + CommonTypes::putBool($out, $this->hasStartWithMapEnabled); + VarInt::writeSignedInt($out, $this->defaultPlayerPermission); + LE::writeSignedInt($out, $this->serverChunkTickRadius); //doesn't make sense for this to be signed, but that's what the spec says + CommonTypes::putBool($out, $this->hasLockedBehaviorPack); + CommonTypes::putBool($out, $this->hasLockedResourcePack); + CommonTypes::putBool($out, $this->isFromLockedWorldTemplate); + CommonTypes::putBool($out, $this->useMsaGamertagsOnly); + CommonTypes::putBool($out, $this->isFromWorldTemplate); + CommonTypes::putBool($out, $this->isWorldTemplateOptionLocked); + CommonTypes::putBool($out, $this->onlySpawnV1Villagers); + CommonTypes::putBool($out, $this->disablePersona); + CommonTypes::putBool($out, $this->disableCustomSkins); + CommonTypes::putBool($out, $this->muteEmoteAnnouncements); + CommonTypes::putString($out, $this->vanillaVersion); + LE::writeSignedInt($out, $this->limitedWorldWidth); //doesn't make sense for this to be signed, but that's what the spec says + LE::writeSignedInt($out, $this->limitedWorldLength); //same as above + CommonTypes::putBool($out, $this->isNewNether); ($this->eduSharedUriResource ?? new EducationUriResource("", ""))->write($out); - $out->writeOptional($this->experimentalGameplayOverride, $out->putBool(...)); - $out->putByte($this->chatRestrictionLevel); - $out->putBool($this->disablePlayerInteractions); - $out->putString($this->serverIdentifier); - $out->putString($this->worldIdentifier); - $out->putString($this->scenarioIdentifier); - $out->putString($this->ownerIdentifier); + CommonTypes::writeOptional($out, $this->experimentalGameplayOverride, CommonTypes::putBool(...)); + Byte::writeUnsigned($out, $this->chatRestrictionLevel); + CommonTypes::putBool($out, $this->disablePlayerInteractions); + CommonTypes::putString($out, $this->serverIdentifier); + CommonTypes::putString($out, $this->worldIdentifier); + CommonTypes::putString($out, $this->scenarioIdentifier); + CommonTypes::putString($out, $this->ownerIdentifier); } } diff --git a/src/types/MapImage.php b/src/types/MapImage.php index 42f59936..ca77bbb3 100644 --- a/src/types/MapImage.php +++ b/src/types/MapImage.php @@ -14,11 +14,13 @@ namespace pocketmine\network\mcpe\protocol\types; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\DataDecodeException; +use pmmp\encoding\VarInt; use pocketmine\color\Color; use pocketmine\network\mcpe\protocol\PacketDecodeException; use pocketmine\utils\Binary; -use pocketmine\utils\BinaryDataException; -use pocketmine\utils\BinaryStream; use function count; final class MapImage{ @@ -72,26 +74,26 @@ public function getHeight() : int{ return $this->height; } */ public function getPixels() : array{ return $this->pixels; } - public function encode(BinaryStream $output) : void{ + public function encode(ByteBufferWriter $out) : void{ if($this->encodedPixelCache === null){ - $serializer = new BinaryStream(); + $serializer = new ByteBufferWriter(); for($y = 0; $y < $this->height; ++$y){ for($x = 0; $x < $this->width; ++$x){ //if mojang had any sense this would just be a regular LE int - $serializer->putUnsignedVarInt(Binary::flipIntEndianness($this->pixels[$y][$x]->toRGBA())); + VarInt::writeUnsignedInt($serializer, Binary::flipIntEndianness($this->pixels[$y][$x]->toRGBA())); } } - $this->encodedPixelCache = $serializer->getBuffer(); + $this->encodedPixelCache = $serializer->getData(); } - $output->put($this->encodedPixelCache); + $out->writeByteArray($this->encodedPixelCache); } /** * @throws PacketDecodeException - * @throws BinaryDataException + * @throws DataDecodeException */ - public static function decode(BinaryStream $input, int $height, int $width) : self{ + public static function decode(ByteBufferReader $in, int $height, int $width) : self{ if($width > self::MAX_WIDTH){ throw new PacketDecodeException("Image width must be at most " . self::MAX_WIDTH . " pixels wide"); } @@ -103,7 +105,7 @@ public static function decode(BinaryStream $input, int $height, int $width) : se for($y = 0; $y < $height; ++$y){ $row = []; for($x = 0; $x < $width; ++$x){ - $row[] = Color::fromRGBA(Binary::flipIntEndianness($input->getUnsignedVarInt())); + $row[] = Color::fromRGBA(Binary::flipIntEndianness(VarInt::readUnsignedInt($in))); } $pixels[] = $row; } diff --git a/src/types/MapInfoRequestPacketClientPixel.php b/src/types/MapInfoRequestPacketClientPixel.php index dc74b358..96641509 100644 --- a/src/types/MapInfoRequestPacketClientPixel.php +++ b/src/types/MapInfoRequestPacketClientPixel.php @@ -14,8 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; use pocketmine\color\Color; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; use function intdiv; final class MapInfoRequestPacketClientPixel{ @@ -34,9 +36,9 @@ public function getX() : int{ return $this->x; } public function getY() : int{ return $this->y; } - public static function read(PacketSerializer $in) : self{ - $color = $in->getLInt(); - $index = $in->getLShort(); + public static function read(ByteBufferReader $in) : self{ + $color = LE::readUnsignedInt($in); + $index = LE::readUnsignedShort($in); $x = $index % self::Y_INDEX_MULTIPLIER; $y = intdiv($index, self::Y_INDEX_MULTIPLIER); @@ -44,8 +46,8 @@ public static function read(PacketSerializer $in) : self{ return new self(Color::fromRGBA($color), $x, $y); } - public function write(PacketSerializer $out) : void{ - $out->putLInt($this->color->toRGBA()); - $out->putLShort($this->x + ($this->y * self::Y_INDEX_MULTIPLIER)); + public function write(ByteBufferWriter $out) : void{ + LE::writeUnsignedInt($out, $this->color->toRGBA()); + LE::writeUnsignedShort($out, $this->x + ($this->y * self::Y_INDEX_MULTIPLIER)); } } diff --git a/src/types/NetworkPermissions.php b/src/types/NetworkPermissions.php index 9503b925..fdcb1920 100644 --- a/src/types/NetworkPermissions.php +++ b/src/types/NetworkPermissions.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class NetworkPermissions{ public function __construct( @@ -25,12 +27,12 @@ public function disableClientSounds() : bool{ return $this->disableClientSounds; } - public static function decode(PacketSerializer $in) : self{ - $disableClientSounds = $in->getBool(); + public static function decode(ByteBufferReader $in) : self{ + $disableClientSounds = CommonTypes::getBool($in); return new self($disableClientSounds); } - public function encode(PacketSerializer $out) : void{ - $out->putBool($this->disableClientSounds); + public function encode(ByteBufferWriter $out) : void{ + CommonTypes::putBool($out, $this->disableClientSounds); } } diff --git a/src/types/PacketShapeData.php b/src/types/PacketShapeData.php index efad2929..51149073 100644 --- a/src/types/PacketShapeData.php +++ b/src/types/PacketShapeData.php @@ -14,9 +14,14 @@ namespace pocketmine\network\mcpe\protocol\types; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; use pocketmine\color\Color; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\ServerScriptDebugDrawerPacket; /** @@ -178,20 +183,20 @@ public function getArrowHeadRadius() : ?float{ return $this->arrowHeadRadius; } public function getSegments() : ?int{ return $this->segments; } - public static function read(PacketSerializer $in) : self{ - $networkId = $in->getUnsignedVarLong(); - $type = $in->readOptional(fn() => ScriptDebugShapeType::fromPacket($in->getByte())); - $location = $in->readOptional($in->getVector3(...)); - $scale = $in->readOptional($in->getLFloat(...)); - $rotation = $in->readOptional($in->getVector3(...)); - $totalTimeLeft = $in->readOptional($in->getLFloat(...)); - $color = $in->readOptional(fn() => Color::fromARGB($in->getLInt())); - $text = $in->readOptional($in->getString(...)); - $boxBound = $in->readOptional($in->getVector3(...)); - $lineEndLocation = $in->readOptional($in->getVector3(...)); - $arrowHeadLength = $in->readOptional($in->getLFloat(...)); - $arrowHeadRadius = $in->readOptional($in->getLFloat(...)); - $segments = $in->readOptional($in->getByte(...)); + public static function read(ByteBufferReader $in) : self{ + $networkId = VarInt::readUnsignedLong($in); + $type = CommonTypes::readOptional($in, fn() => ScriptDebugShapeType::fromPacket(Byte::readUnsigned($in))); + $location = CommonTypes::readOptional($in, CommonTypes::getVector3(...)); + $scale = CommonTypes::readOptional($in, LE::readFloat(...)); + $rotation = CommonTypes::readOptional($in, CommonTypes::getVector3(...)); + $totalTimeLeft = CommonTypes::readOptional($in, LE::readFloat(...)); + $color = CommonTypes::readOptional($in, fn() => Color::fromARGB(LE::readUnsignedInt($in))); + $text = CommonTypes::readOptional($in, CommonTypes::getString(...)); + $boxBound = CommonTypes::readOptional($in, CommonTypes::getVector3(...)); + $lineEndLocation = CommonTypes::readOptional($in, CommonTypes::getVector3(...)); + $arrowHeadLength = CommonTypes::readOptional($in, LE::readFloat(...)); + $arrowHeadRadius = CommonTypes::readOptional($in, LE::readFloat(...)); + $segments = CommonTypes::readOptional($in, Byte::readUnsigned(...)); return new self( $networkId, @@ -210,19 +215,19 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putUnsignedVarLong($this->networkId); - $out->writeOptional($this->type, fn(ScriptDebugShapeType $type) => $out->putByte($type->value)); - $out->writeOptional($this->location, $out->putVector3(...)); - $out->writeOptional($this->scale, $out->putLFloat(...)); - $out->writeOptional($this->rotation, $out->putVector3(...)); - $out->writeOptional($this->totalTimeLeft, $out->putLFloat(...)); - $out->writeOptional($this->color, fn(Color $color) => $out->putLInt($color->toARGB())); - $out->writeOptional($this->text, $out->putString(...)); - $out->writeOptional($this->boxBound, $out->putVector3(...)); - $out->writeOptional($this->lineEndLocation, $out->putVector3(...)); - $out->writeOptional($this->arrowHeadLength, $out->putLFloat(...)); - $out->writeOptional($this->arrowHeadRadius, $out->putLFloat(...)); - $out->writeOptional($this->segments, $out->putByte(...)); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedLong($out, $this->networkId); + CommonTypes::writeOptional($out, $this->type, fn(ByteBufferWriter $out, ScriptDebugShapeType $type) => Byte::writeUnsigned($out, $type->value)); + CommonTypes::writeOptional($out, $this->location, CommonTypes::putVector3(...)); + CommonTypes::writeOptional($out, $this->scale, LE::writeFloat(...)); + CommonTypes::writeOptional($out, $this->rotation, CommonTypes::putVector3(...)); + CommonTypes::writeOptional($out, $this->totalTimeLeft, LE::writeFloat(...)); + CommonTypes::writeOptional($out, $this->color, fn(ByteBufferWriter $out, Color $color) => LE::writeUnsignedInt($out, $color->toARGB())); + CommonTypes::writeOptional($out, $this->text, CommonTypes::putString(...)); + CommonTypes::writeOptional($out, $this->boxBound, CommonTypes::putVector3(...)); + CommonTypes::writeOptional($out, $this->lineEndLocation, CommonTypes::putVector3(...)); + CommonTypes::writeOptional($out, $this->arrowHeadLength, LE::writeFloat(...)); + CommonTypes::writeOptional($out, $this->arrowHeadRadius, LE::writeFloat(...)); + CommonTypes::writeOptional($out, $this->segments, Byte::writeUnsigned(...)); } } diff --git a/src/types/PlayerBlockAction.php b/src/types/PlayerBlockAction.php index 1c6ba029..5d51f249 100644 --- a/src/types/PlayerBlockAction.php +++ b/src/types/PlayerBlockAction.php @@ -14,12 +14,12 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferWriter; /** This is used for PlayerAuthInput packet when the flags include PERFORM_BLOCK_ACTIONS */ interface PlayerBlockAction{ public function getActionType() : int; - public function write(PacketSerializer $out) : void; + public function write(ByteBufferWriter $out) : void; } diff --git a/src/types/PlayerBlockActionStopBreak.php b/src/types/PlayerBlockActionStopBreak.php index a11495fe..5cc0d07c 100644 --- a/src/types/PlayerBlockActionStopBreak.php +++ b/src/types/PlayerBlockActionStopBreak.php @@ -14,7 +14,7 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferWriter; final class PlayerBlockActionStopBreak implements PlayerBlockAction{ @@ -22,7 +22,7 @@ public function getActionType() : int{ return PlayerAction::STOP_BREAK; } - public function write(PacketSerializer $out) : void{ + public function write(ByteBufferWriter $out) : void{ //NOOP } } diff --git a/src/types/PlayerBlockActionWithBlockInfo.php b/src/types/PlayerBlockActionWithBlockInfo.php index 1a837891..269f2615 100644 --- a/src/types/PlayerBlockActionWithBlockInfo.php +++ b/src/types/PlayerBlockActionWithBlockInfo.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; /** This is used for PlayerAuthInput packet when the flags include PERFORM_BLOCK_ACTIONS */ final class PlayerBlockActionWithBlockInfo implements PlayerBlockAction{ @@ -34,15 +37,15 @@ public function getBlockPosition() : BlockPosition{ return $this->blockPosition; public function getFace() : int{ return $this->face; } - public static function read(PacketSerializer $in, int $actionType) : self{ - $blockPosition = $in->getSignedBlockPosition(); - $face = $in->getVarInt(); + public static function read(ByteBufferReader $in, int $actionType) : self{ + $blockPosition = CommonTypes::getSignedBlockPosition($in); + $face = VarInt::readSignedInt($in); return new self($actionType, $blockPosition, $face); } - public function write(PacketSerializer $out) : void{ - $out->putSignedBlockPosition($this->blockPosition); - $out->putVarInt($this->face); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putSignedBlockPosition($out, $this->blockPosition); + VarInt::writeSignedInt($out, $this->face); } public static function isValidActionType(int $actionType) : bool{ diff --git a/src/types/PlayerMovementSettings.php b/src/types/PlayerMovementSettings.php index 9c8a72ef..4f71433e 100644 --- a/src/types/PlayerMovementSettings.php +++ b/src/types/PlayerMovementSettings.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class PlayerMovementSettings{ public function __construct( @@ -26,14 +29,14 @@ public function getRewindHistorySize() : int{ return $this->rewindHistorySize; } public function isServerAuthoritativeBlockBreaking() : bool{ return $this->serverAuthoritativeBlockBreaking; } - public static function read(PacketSerializer $in) : self{ - $rewindHistorySize = $in->getVarInt(); - $serverAuthBlockBreaking = $in->getBool(); + public static function read(ByteBufferReader $in) : self{ + $rewindHistorySize = VarInt::readSignedInt($in); + $serverAuthBlockBreaking = CommonTypes::getBool($in); return new self($rewindHistorySize, $serverAuthBlockBreaking); } - public function write(PacketSerializer $out) : void{ - $out->putVarInt($this->rewindHistorySize); - $out->putBool($this->serverAuthoritativeBlockBreaking); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->rewindHistorySize); + CommonTypes::putBool($out, $this->serverAuthoritativeBlockBreaking); } } diff --git a/src/types/SpawnSettings.php b/src/types/SpawnSettings.php index bb3d3899..bd2629a0 100644 --- a/src/types/SpawnSettings.php +++ b/src/types/SpawnSettings.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class SpawnSettings{ public const BIOME_TYPE_DEFAULT = 0; @@ -41,17 +45,17 @@ public function getDimension() : int{ return $this->dimension; } - public static function read(PacketSerializer $in) : self{ - $biomeType = $in->getLShort(); - $biomeName = $in->getString(); - $dimension = $in->getVarInt(); + public static function read(ByteBufferReader $in) : self{ + $biomeType = LE::readUnsignedShort($in); + $biomeName = CommonTypes::getString($in); + $dimension = VarInt::readSignedInt($in); return new self($biomeType, $biomeName, $dimension); } - public function write(PacketSerializer $out) : void{ - $out->putLShort($this->biomeType); - $out->putString($this->biomeName); - $out->putVarInt($this->dimension); + public function write(ByteBufferWriter $out) : void{ + LE::writeUnsignedShort($out, $this->biomeType); + CommonTypes::putString($out, $this->biomeName); + VarInt::writeSignedInt($out, $this->dimension); } } diff --git a/src/types/SubChunkPacketEntryCommon.php b/src/types/SubChunkPacketEntryCommon.php index 3b2de578..afe424e3 100644 --- a/src/types/SubChunkPacketEntryCommon.php +++ b/src/types/SubChunkPacketEntryCommon.php @@ -14,8 +14,11 @@ namespace pocketmine\network\mcpe\protocol\types; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use pocketmine\network\mcpe\protocol\PacketDecodeException; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class SubChunkPacketEntryCommon{ @@ -37,14 +40,14 @@ public function getHeightMap() : ?SubChunkPacketHeightMapInfo{ return $this->hei public function getRenderHeightMap() : ?SubChunkPacketHeightMapInfo{ return $this->renderHeightMap; } - public static function read(PacketSerializer $in, bool $cacheEnabled) : self{ + public static function read(ByteBufferReader $in, bool $cacheEnabled) : self{ $offset = SubChunkPositionOffset::read($in); - $requestResult = $in->getByte(); + $requestResult = Byte::readUnsigned($in); - $data = !$cacheEnabled || $requestResult !== SubChunkRequestResult::SUCCESS_ALL_AIR ? $in->getString() : ""; + $data = !$cacheEnabled || $requestResult !== SubChunkRequestResult::SUCCESS_ALL_AIR ? CommonTypes::getString($in) : ""; - $heightMapDataType = $in->getByte(); + $heightMapDataType = Byte::readUnsigned($in); $heightMapData = match ($heightMapDataType) { SubChunkPacketHeightMapType::NO_DATA => null, SubChunkPacketHeightMapType::DATA => SubChunkPacketHeightMapInfo::read($in), @@ -53,7 +56,7 @@ public static function read(PacketSerializer $in, bool $cacheEnabled) : self{ default => throw new PacketDecodeException("Unknown heightmap data type $heightMapDataType") }; - $renderHeightMapDataType = $in->getByte(); + $renderHeightMapDataType = Byte::readUnsigned($in); $renderHeightMapData = match ($renderHeightMapDataType) { SubChunkPacketHeightMapType::NO_DATA => null, SubChunkPacketHeightMapType::DATA => SubChunkPacketHeightMapInfo::read($in), @@ -72,36 +75,36 @@ public static function read(PacketSerializer $in, bool $cacheEnabled) : self{ ); } - public function write(PacketSerializer $out, bool $cacheEnabled) : void{ + public function write(ByteBufferWriter $out, bool $cacheEnabled) : void{ $this->offset->write($out); - $out->putByte($this->requestResult); + Byte::writeUnsigned($out, $this->requestResult); if(!$cacheEnabled || $this->requestResult !== SubChunkRequestResult::SUCCESS_ALL_AIR){ - $out->putString($this->terrainData); + CommonTypes::putString($out, $this->terrainData); } if($this->heightMap === null){ - $out->putByte(SubChunkPacketHeightMapType::NO_DATA); + Byte::writeUnsigned($out, SubChunkPacketHeightMapType::NO_DATA); }elseif($this->heightMap->isAllTooLow()){ - $out->putByte(SubChunkPacketHeightMapType::ALL_TOO_LOW); + Byte::writeUnsigned($out, SubChunkPacketHeightMapType::ALL_TOO_LOW); }elseif($this->heightMap->isAllTooHigh()){ - $out->putByte(SubChunkPacketHeightMapType::ALL_TOO_HIGH); + Byte::writeUnsigned($out, SubChunkPacketHeightMapType::ALL_TOO_HIGH); }else{ $heightMapData = $this->heightMap; //avoid PHPStan purity issue - $out->putByte(SubChunkPacketHeightMapType::DATA); + Byte::writeUnsigned($out, SubChunkPacketHeightMapType::DATA); $heightMapData->write($out); } if($this->renderHeightMap === null){ - $out->putByte(SubChunkPacketHeightMapType::ALL_COPIED); + Byte::writeUnsigned($out, SubChunkPacketHeightMapType::ALL_COPIED); }elseif($this->renderHeightMap->isAllTooLow()){ - $out->putByte(SubChunkPacketHeightMapType::ALL_TOO_LOW); + Byte::writeUnsigned($out, SubChunkPacketHeightMapType::ALL_TOO_LOW); }elseif($this->renderHeightMap->isAllTooHigh()){ - $out->putByte(SubChunkPacketHeightMapType::ALL_TOO_HIGH); + Byte::writeUnsigned($out, SubChunkPacketHeightMapType::ALL_TOO_HIGH); }else{ $renderHeightMapData = $this->renderHeightMap; //avoid PHPStan purity issue - $out->putByte(SubChunkPacketHeightMapType::DATA); + Byte::writeUnsigned($out, SubChunkPacketHeightMapType::DATA); $renderHeightMapData->write($out); } } diff --git a/src/types/SubChunkPacketEntryWithCache.php b/src/types/SubChunkPacketEntryWithCache.php index 2a179473..ca56c6ea 100644 --- a/src/types/SubChunkPacketEntryWithCache.php +++ b/src/types/SubChunkPacketEntryWithCache.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; final class SubChunkPacketEntryWithCache{ @@ -27,15 +29,15 @@ public function getBase() : SubChunkPacketEntryCommon{ return $this->base; } public function getUsedBlobHash() : int{ return $this->usedBlobHash; } - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ $base = SubChunkPacketEntryCommon::read($in, true); - $usedBlobHash = $in->getLLong(); + $usedBlobHash = LE::readUnsignedLong($in); return new self($base, $usedBlobHash); } - public function write(PacketSerializer $out) : void{ + public function write(ByteBufferWriter $out) : void{ $this->base->write($out, true); - $out->putLLong($this->usedBlobHash); + LE::writeUnsignedLong($out, $this->usedBlobHash); } } diff --git a/src/types/SubChunkPacketEntryWithoutCache.php b/src/types/SubChunkPacketEntryWithoutCache.php index 4e99c281..0a6a8094 100644 --- a/src/types/SubChunkPacketEntryWithoutCache.php +++ b/src/types/SubChunkPacketEntryWithoutCache.php @@ -14,7 +14,8 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; final class SubChunkPacketEntryWithoutCache{ @@ -24,11 +25,11 @@ public function __construct( public function getBase() : SubChunkPacketEntryCommon{ return $this->base; } - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ return new self(SubChunkPacketEntryCommon::read($in, false)); } - public function write(PacketSerializer $out) : void{ + public function write(ByteBufferWriter $out) : void{ $this->base->write($out, false); } } diff --git a/src/types/SubChunkPacketHeightMapInfo.php b/src/types/SubChunkPacketHeightMapInfo.php index 72c86e00..8bc35b32 100644 --- a/src/types/SubChunkPacketHeightMapInfo.php +++ b/src/types/SubChunkPacketHeightMapInfo.php @@ -14,8 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; -use pocketmine\utils\Binary; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use function array_fill; use function count; @@ -38,17 +39,17 @@ public function getHeight(int $x, int $z) : int{ return $this->heights[(($z & 0xf) << 4) | ($x & 0xf)]; } - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ $heights = []; for($i = 0; $i < 256; ++$i){ - $heights[] = Binary::signByte($in->getByte()); + $heights[] = Byte::readSigned($in); } return new self($heights); } - public function write(PacketSerializer $out) : void{ + public function write(ByteBufferWriter $out) : void{ for($i = 0; $i < 256; ++$i){ - $out->putByte(Binary::unsignByte($this->heights[$i])); + Byte::writeSigned($out, $this->heights[$i]); } } diff --git a/src/types/SubChunkPosition.php b/src/types/SubChunkPosition.php index fcdb517c..f292244a 100644 --- a/src/types/SubChunkPosition.php +++ b/src/types/SubChunkPosition.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; final class SubChunkPosition{ @@ -30,17 +32,17 @@ public function getY() : int{ return $this->y; } public function getZ() : int{ return $this->z; } - public static function read(PacketSerializer $in) : self{ - $x = $in->getVarInt(); - $y = $in->getVarInt(); - $z = $in->getVarInt(); + public static function read(ByteBufferReader $in) : self{ + $x = VarInt::readSignedInt($in); + $y = VarInt::readSignedInt($in); + $z = VarInt::readSignedInt($in); return new self($x, $y, $z); } - public function write(PacketSerializer $out) : void{ - $out->putVarInt($this->x); - $out->putVarInt($this->y); - $out->putVarInt($this->z); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->x); + VarInt::writeSignedInt($out, $this->y); + VarInt::writeSignedInt($out, $this->z); } } diff --git a/src/types/SubChunkPositionOffset.php b/src/types/SubChunkPositionOffset.php index 4c945324..10588722 100644 --- a/src/types/SubChunkPositionOffset.php +++ b/src/types/SubChunkPositionOffset.php @@ -14,8 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; -use pocketmine\utils\Binary; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use pocketmine\utils\Limits; final class SubChunkPositionOffset{ @@ -42,17 +43,17 @@ public function getYOffset() : int{ return $this->yOffset; } public function getZOffset() : int{ return $this->zOffset; } - public static function read(PacketSerializer $in) : self{ - $xOffset = Binary::signByte($in->getByte()); - $yOffset = Binary::signByte($in->getByte()); - $zOffset = Binary::signByte($in->getByte()); + public static function read(ByteBufferReader $in) : self{ + $xOffset = Byte::readSigned($in); + $yOffset = Byte::readSigned($in); + $zOffset = Byte::readSigned($in); return new self($xOffset, $yOffset, $zOffset); } - public function write(PacketSerializer $out) : void{ - $out->putByte($this->xOffset); - $out->putByte($this->yOffset); - $out->putByte($this->zOffset); + public function write(ByteBufferWriter $out) : void{ + Byte::writeSigned($out, $this->xOffset); + Byte::writeSigned($out, $this->yOffset); + Byte::writeSigned($out, $this->zOffset); } } diff --git a/src/types/TrimMaterial.php b/src/types/TrimMaterial.php index 76eccec7..b3ece0ae 100644 --- a/src/types/TrimMaterial.php +++ b/src/types/TrimMaterial.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class TrimMaterial{ @@ -30,16 +32,16 @@ public function getColor() : string{ return $this->color; } public function getItemId() : string{ return $this->itemId; } - public static function read(PacketSerializer $in) : self{ - $materialId = $in->getString(); - $color = $in->getString(); - $itemId = $in->getString(); + public static function read(ByteBufferReader $in) : self{ + $materialId = CommonTypes::getString($in); + $color = CommonTypes::getString($in); + $itemId = CommonTypes::getString($in); return new self($materialId, $color, $itemId); } - public function write(PacketSerializer $out) : void{ - $out->putString($this->materialId); - $out->putString($this->color); - $out->putString($this->itemId); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->materialId); + CommonTypes::putString($out, $this->color); + CommonTypes::putString($out, $this->itemId); } } diff --git a/src/types/TrimPattern.php b/src/types/TrimPattern.php index ddc0e919..1dd79d22 100644 --- a/src/types/TrimPattern.php +++ b/src/types/TrimPattern.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class TrimPattern{ @@ -27,14 +29,14 @@ public function getItemId() : string{ return $this->itemId; } public function getPatternId() : string{ return $this->patternId; } - public static function read(PacketSerializer $in) : self{ - $itemId = $in->getString(); - $patternId = $in->getString(); + public static function read(ByteBufferReader $in) : self{ + $itemId = CommonTypes::getString($in); + $patternId = CommonTypes::getString($in); return new self($itemId, $patternId); } - public function write(PacketSerializer $out) : void{ - $out->putString($this->itemId); - $out->putString($this->patternId); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->itemId); + CommonTypes::putString($out, $this->patternId); } } diff --git a/src/types/UpdateSubChunkBlocksPacketEntry.php b/src/types/UpdateSubChunkBlocksPacketEntry.php index b28a6c42..8f884158 100644 --- a/src/types/UpdateSubChunkBlocksPacketEntry.php +++ b/src/types/UpdateSubChunkBlocksPacketEntry.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\UpdateBlockPacket; final class UpdateSubChunkBlocksPacketEntry{ @@ -41,21 +44,21 @@ public function getSyncedUpdateActorUniqueId() : int{ return $this->syncedUpdate public function getSyncedUpdateType() : int{ return $this->syncedUpdateType; } - public static function read(PacketSerializer $in) : self{ - $blockPosition = $in->getBlockPosition(); - $blockRuntimeId = $in->getUnsignedVarInt(); - $updateFlags = $in->getUnsignedVarInt(); - $syncedUpdateActorUniqueId = $in->getUnsignedVarLong(); //this can't use the standard method because it's unsigned as opposed to the usual signed... !!!!!! - $syncedUpdateType = $in->getUnsignedVarInt(); //this isn't even consistent with UpdateBlockSyncedPacket?! + public static function read(ByteBufferReader $in) : self{ + $blockPosition = CommonTypes::getBlockPosition($in); + $blockRuntimeId = VarInt::readUnsignedInt($in); + $updateFlags = VarInt::readUnsignedInt($in); + $syncedUpdateActorUniqueId = VarInt::readUnsignedLong($in); //this can't use the standard method because it's unsigned as opposed to the usual signed... !!!!!! + $syncedUpdateType = VarInt::readUnsignedInt($in); //this isn't even consistent with UpdateBlockSyncedPacket?! return new self($blockPosition, $blockRuntimeId, $updateFlags, $syncedUpdateActorUniqueId, $syncedUpdateType); } - public function write(PacketSerializer $out) : void{ - $out->putBlockPosition($this->blockPosition); - $out->putUnsignedVarInt($this->blockRuntimeId); - $out->putUnsignedVarInt($this->flags); - $out->putUnsignedVarLong($this->syncedUpdateActorUniqueId); - $out->putUnsignedVarInt($this->syncedUpdateType); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putBlockPosition($out, $this->blockPosition); + VarInt::writeUnsignedInt($out, $this->blockRuntimeId); + VarInt::writeUnsignedInt($out, $this->flags); + VarInt::writeUnsignedLong($out, $this->syncedUpdateActorUniqueId); + VarInt::writeUnsignedInt($out, $this->syncedUpdateType); } } diff --git a/src/types/biome/BiomeDefinitionData.php b/src/types/biome/BiomeDefinitionData.php index dbc1f9c1..29d7659d 100644 --- a/src/types/biome/BiomeDefinitionData.php +++ b/src/types/biome/BiomeDefinitionData.php @@ -14,8 +14,12 @@ namespace pocketmine\network\mcpe\protocol\types\biome; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; use pocketmine\color\Color; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\biome\chunkgen\BiomeDefinitionChunkGenData; use function count; @@ -74,29 +78,29 @@ public function getTagIndexes() : ?array{ return $this->tagIndexes; } public function getChunkGenData() : ?BiomeDefinitionChunkGenData{ return $this->chunkGenData; } - public static function read(PacketSerializer $in) : self{ - $nameIndex = $in->getLShort(); - $id = $in->getLShort(); - $temperature = $in->getLFloat(); - $downfall = $in->getLFloat(); - $redSporeDensity = $in->getLFloat(); - $blueSporeDensity = $in->getLFloat(); - $ashDensity = $in->getLFloat(); - $whiteAshDensity = $in->getLFloat(); - $depth = $in->getLFloat(); - $scale = $in->getLFloat(); - $mapWaterColor = Color::fromARGB($in->getLInt()); - $rain = $in->getBool(); - $tags = $in->readOptional(function() use ($in) : array{ + public static function read(ByteBufferReader $in) : self{ + $nameIndex = LE::readUnsignedShort($in); + $id = LE::readUnsignedShort($in); + $temperature = LE::readFloat($in); + $downfall = LE::readFloat($in); + $redSporeDensity = LE::readFloat($in); + $blueSporeDensity = LE::readFloat($in); + $ashDensity = LE::readFloat($in); + $whiteAshDensity = LE::readFloat($in); + $depth = LE::readFloat($in); + $scale = LE::readFloat($in); + $mapWaterColor = Color::fromARGB(LE::readUnsignedInt($in)); + $rain = CommonTypes::getBool($in); + $tags = CommonTypes::readOptional($in, function() use ($in) : array{ $tagIndexes = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ - $tagIndexes[] = $in->getLShort(); + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ + $tagIndexes[] = LE::readUnsignedShort($in); } return $tagIndexes; }); - $chunkGenData = $in->readOptional(fn() => BiomeDefinitionChunkGenData::read($in)); + $chunkGenData = CommonTypes::readOptional($in, fn() => BiomeDefinitionChunkGenData::read($in)); return new self( $nameIndex, @@ -116,25 +120,25 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putLShort($this->nameIndex); - $out->putLShort($this->id); - $out->putLFloat($this->temperature); - $out->putLFloat($this->downfall); - $out->putLFloat($this->redSporeDensity); - $out->putLFloat($this->blueSporeDensity); - $out->putLFloat($this->ashDensity); - $out->putLFloat($this->whiteAshDensity); - $out->putLFloat($this->depth); - $out->putLFloat($this->scale); - $out->putLInt($this->mapWaterColor->toARGB()); - $out->putBool($this->rain); - $out->writeOptional($this->tagIndexes, function(array $tagIndexes) use ($out) : void{ - $out->putUnsignedVarInt(count($tagIndexes)); + public function write(ByteBufferWriter $out) : void{ + LE::writeUnsignedShort($out, $this->nameIndex); + LE::writeUnsignedShort($out, $this->id); + LE::writeFloat($out, $this->temperature); + LE::writeFloat($out, $this->downfall); + LE::writeFloat($out, $this->redSporeDensity); + LE::writeFloat($out, $this->blueSporeDensity); + LE::writeFloat($out, $this->ashDensity); + LE::writeFloat($out, $this->whiteAshDensity); + LE::writeFloat($out, $this->depth); + LE::writeFloat($out, $this->scale); + LE::writeUnsignedInt($out, $this->mapWaterColor->toARGB()); + CommonTypes::putBool($out, $this->rain); + CommonTypes::writeOptional($out, $this->tagIndexes, function(ByteBufferWriter $out, array $tagIndexes) : void{ + VarInt::writeUnsignedInt($out, count($tagIndexes)); foreach($tagIndexes as $tag){ - $out->putLShort($tag); + LE::writeUnsignedShort($out, $tag); } }); - $out->writeOptional($this->chunkGenData, fn(BiomeDefinitionChunkGenData $chunkGenData) => $chunkGenData->write($out)); + CommonTypes::writeOptional($out, $this->chunkGenData, fn(ByteBufferWriter $out, BiomeDefinitionChunkGenData $v) => $v->write($out)); } } diff --git a/src/types/biome/chunkgen/BiomeCappedSurfaceData.php b/src/types/biome/chunkgen/BiomeCappedSurfaceData.php index b15688b1..17416883 100644 --- a/src/types/biome/chunkgen/BiomeCappedSurfaceData.php +++ b/src/types/biome/chunkgen/BiomeCappedSurfaceData.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol\types\biome\chunkgen; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use function count; final class BiomeCappedSurfaceData{ @@ -47,20 +51,20 @@ public function getFoundationBlock() : ?int{ return $this->foundationBlock; } public function getBeachBlock() : ?int{ return $this->beachBlock; } - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ $floorBlocks = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ - $floorBlocks[] = $in->getLInt(); + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ + $floorBlocks[] = LE::readUnsignedInt($in); } $ceilingBlocks = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ - $ceilingBlocks[] = $in->getLInt(); + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ + $ceilingBlocks[] = LE::readUnsignedInt($in); } - $seaBlock = $in->readOptional($in->getLInt(...)); - $foundationBlock = $in->readOptional($in->getLInt(...)); - $beachBlock = $in->readOptional($in->getLInt(...)); + $seaBlock = CommonTypes::readOptional($in, LE::readUnsignedInt(...)); + $foundationBlock = CommonTypes::readOptional($in, LE::readUnsignedInt(...)); + $beachBlock = CommonTypes::readOptional($in, LE::readUnsignedInt(...)); return new self( $floorBlocks, @@ -71,19 +75,19 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->floorBlocks)); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->floorBlocks)); foreach($this->floorBlocks as $block){ - $out->putLInt($block); + LE::writeUnsignedInt($out, $block); } - $out->putUnsignedVarInt(count($this->ceilingBlocks)); + VarInt::writeUnsignedInt($out, count($this->ceilingBlocks)); foreach($this->ceilingBlocks as $block){ - $out->putLInt($block); + LE::writeUnsignedInt($out, $block); } - $out->writeOptional($this->seaBlock, $out->putLInt(...)); - $out->writeOptional($this->foundationBlock, $out->putLInt(...)); - $out->writeOptional($this->beachBlock, $out->putLInt(...)); + CommonTypes::writeOptional($out, $this->seaBlock, LE::writeUnsignedInt(...)); + CommonTypes::writeOptional($out, $this->foundationBlock, LE::writeUnsignedInt(...)); + CommonTypes::writeOptional($out, $this->beachBlock, LE::writeUnsignedInt(...)); } } diff --git a/src/types/biome/chunkgen/BiomeClimateData.php b/src/types/biome/chunkgen/BiomeClimateData.php index ecc21f76..e72e34ce 100644 --- a/src/types/biome/chunkgen/BiomeClimateData.php +++ b/src/types/biome/chunkgen/BiomeClimateData.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\biome\chunkgen; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; final class BiomeClimateData{ @@ -45,15 +47,15 @@ public function getSnowAccumulationMin() : float{ return $this->snowAccumulation public function getSnowAccumulationMax() : float{ return $this->snowAccumulationMax; } - public static function read(PacketSerializer $in) : self{ - $temperature = $in->getLFloat(); - $downfall = $in->getLFloat(); - $redSporeDensity = $in->getLFloat(); - $blueSporeDensity = $in->getLFloat(); - $ashDensity = $in->getLFloat(); - $whiteAshDensity = $in->getLFloat(); - $snowAccumulationMin = $in->getLFloat(); - $snowAccumulationMax = $in->getLFloat(); + public static function read(ByteBufferReader $in) : self{ + $temperature = LE::readFloat($in); + $downfall = LE::readFloat($in); + $redSporeDensity = LE::readFloat($in); + $blueSporeDensity = LE::readFloat($in); + $ashDensity = LE::readFloat($in); + $whiteAshDensity = LE::readFloat($in); + $snowAccumulationMin = LE::readFloat($in); + $snowAccumulationMax = LE::readFloat($in); return new self( $temperature, @@ -67,14 +69,14 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putLFloat($this->temperature); - $out->putLFloat($this->downfall); - $out->putLFloat($this->redSporeDensity); - $out->putLFloat($this->blueSporeDensity); - $out->putLFloat($this->ashDensity); - $out->putLFloat($this->whiteAshDensity); - $out->putLFloat($this->snowAccumulationMin); - $out->putFloat($this->snowAccumulationMax); + public function write(ByteBufferWriter $out) : void{ + LE::writeFloat($out, $this->temperature); + LE::writeFloat($out, $this->downfall); + LE::writeFloat($out, $this->redSporeDensity); + LE::writeFloat($out, $this->blueSporeDensity); + LE::writeFloat($out, $this->ashDensity); + LE::writeFloat($out, $this->whiteAshDensity); + LE::writeFloat($out, $this->snowAccumulationMin); + LE::writeFloat($out, $this->snowAccumulationMax); } } diff --git a/src/types/biome/chunkgen/BiomeConditionalTransformationData.php b/src/types/biome/chunkgen/BiomeConditionalTransformationData.php index 2df93964..7931d141 100644 --- a/src/types/biome/chunkgen/BiomeConditionalTransformationData.php +++ b/src/types/biome/chunkgen/BiomeConditionalTransformationData.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\biome\chunkgen; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; use function count; final class BiomeConditionalTransformationData{ @@ -37,14 +40,14 @@ public function getConditionJSON() : int{ return $this->conditionJSON; } public function getMinPassingNeighbors() : int{ return $this->minPassingNeighbors; } - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ $weightedBiomes = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $weightedBiomes[] = BiomeWeightedData::read($in); } - $conditionJSON = $in->getLShort(); - $minPassingNeighbors = $in->getLInt(); + $conditionJSON = LE::readSignedShort($in); + $minPassingNeighbors = LE::readUnsignedInt($in); return new self( $weightedBiomes, @@ -53,13 +56,13 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->weightedBiomes)); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->weightedBiomes)); foreach($this->weightedBiomes as $biome){ $biome->write($out); } - $out->putLShort($this->conditionJSON); - $out->putLInt($this->minPassingNeighbors); + LE::writeSignedShort($out, $this->conditionJSON); + LE::writeUnsignedInt($out, $this->minPassingNeighbors); } } diff --git a/src/types/biome/chunkgen/BiomeConsolidatedFeatureData.php b/src/types/biome/chunkgen/BiomeConsolidatedFeatureData.php index fd38f80a..75e86f76 100644 --- a/src/types/biome/chunkgen/BiomeConsolidatedFeatureData.php +++ b/src/types/biome/chunkgen/BiomeConsolidatedFeatureData.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\biome\chunkgen; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class BiomeConsolidatedFeatureData{ @@ -36,12 +39,12 @@ public function getPass() : int{ return $this->pass; } public function canUseInternal() : bool{ return $this->useInternal; } - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ $scatter = BiomeScatterParamData::read($in); - $feature = $in->getLShort(); - $identifier = $in->getLShort(); - $pass = $in->getLShort(); - $useInternal = $in->getBool(); + $feature = LE::readSignedShort($in); + $identifier = LE::readSignedShort($in); + $pass = LE::readSignedShort($in); + $useInternal = CommonTypes::getBool($in); return new self( $scatter, @@ -52,11 +55,11 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ + public function write(ByteBufferWriter $out) : void{ $this->scatter->write($out); - $out->putLShort($this->feature); - $out->putLShort($this->identifier); - $out->putLShort($this->pass); - $out->putBool($this->useInternal); + LE::writeSignedShort($out, $this->feature); + LE::writeSignedShort($out, $this->identifier); + LE::writeSignedShort($out, $this->pass); + CommonTypes::putBool($out, $this->useInternal); } } diff --git a/src/types/biome/chunkgen/BiomeConsolidatedFeaturesData.php b/src/types/biome/chunkgen/BiomeConsolidatedFeaturesData.php index f0a03132..0d616936 100644 --- a/src/types/biome/chunkgen/BiomeConsolidatedFeaturesData.php +++ b/src/types/biome/chunkgen/BiomeConsolidatedFeaturesData.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\biome\chunkgen; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use function count; final class BiomeConsolidatedFeaturesData{ @@ -31,18 +33,18 @@ public function __construct( */ public function getFeatures() : array{ return $this->features; } - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ $features = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $features[] = BiomeConsolidatedFeatureData::read($in); } return new self($features); } - public function write(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->features)); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->features)); foreach($this->features as $feature){ $feature->write($out); } diff --git a/src/types/biome/chunkgen/BiomeCoordinateData.php b/src/types/biome/chunkgen/BiomeCoordinateData.php index a61c2c8c..081ceb55 100644 --- a/src/types/biome/chunkgen/BiomeCoordinateData.php +++ b/src/types/biome/chunkgen/BiomeCoordinateData.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\biome\chunkgen; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; final class BiomeCoordinateData{ @@ -42,14 +45,14 @@ public function getGridStepSize() : int{ return $this->gridStepSize; } public function getDistribution() : int{ return $this->distribution; } - public static function read(PacketSerializer $in) : self{ - $minValueType = $in->getVarInt(); - $minValue = $in->getLShort(); - $maxValueType = $in->getVarInt(); - $maxValue = $in->getLShort(); - $gridOffset = $in->getLInt(); - $gridStepSize = $in->getLInt(); - $distribution = $in->getVarInt(); + public static function read(ByteBufferReader $in) : self{ + $minValueType = VarInt::readSignedInt($in); + $minValue = LE::readSignedShort($in); + $maxValueType = VarInt::readSignedInt($in); + $maxValue = LE::readSignedShort($in); + $gridOffset = LE::readUnsignedInt($in); + $gridStepSize = LE::readUnsignedInt($in); + $distribution = VarInt::readSignedInt($in); return new self( $minValueType, @@ -62,13 +65,13 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putVarInt($this->minValueType); - $out->putLShort($this->minValue); - $out->putVarInt($this->maxValueType); - $out->putLShort($this->maxValue); - $out->putLInt($this->gridOffset); - $out->putLInt($this->gridStepSize); - $out->putVarInt($this->distribution); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->minValueType); + LE::writeSignedShort($out, $this->minValue); + VarInt::writeSignedInt($out, $this->maxValueType); + LE::writeSignedShort($out, $this->maxValue); + LE::writeUnsignedInt($out, $this->gridOffset); + LE::writeUnsignedInt($out, $this->gridStepSize); + VarInt::writeSignedInt($out, $this->distribution); } } diff --git a/src/types/biome/chunkgen/BiomeDefinitionChunkGenData.php b/src/types/biome/chunkgen/BiomeDefinitionChunkGenData.php index 5ec3dedf..64f65854 100644 --- a/src/types/biome/chunkgen/BiomeDefinitionChunkGenData.php +++ b/src/types/biome/chunkgen/BiomeDefinitionChunkGenData.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\biome\chunkgen; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class BiomeDefinitionChunkGenData{ @@ -60,20 +62,20 @@ public function getMultinoiseGenRules() : ?BiomeMultinoiseGenRulesData{ return $ public function getLegacyWorldGenRules() : ?BiomeLegacyWorldGenRulesData{ return $this->legacyWorldGenRules; } - public static function read(PacketSerializer $in) : self{ - $climate = $in->readOptional(fn() => BiomeClimateData::read($in)); - $consolidatedFeatures = $in->readOptional(fn() => BiomeConsolidatedFeaturesData::read($in)); - $mountainParams = $in->readOptional(fn() => BiomeMountainParamsData::read($in)); - $surfaceMaterialAdjustment = $in->readOptional(fn() => BiomeSurfaceMaterialAdjustmentData::read($in)); - $surfaceMaterial = $in->readOptional(fn() => BiomeSurfaceMaterialData::read($in)); - $swampSurface = $in->getBool(); - $frozenOceanSurface = $in->getBool(); - $theEndSurface = $in->getBool(); - $mesaSurface = $in->readOptional(fn() => BiomeMesaSurfaceData::read($in)); - $cappedSurface = $in->readOptional(fn() => BiomeCappedSurfaceData::read($in)); - $overworldGenRules = $in->readOptional(fn() => BiomeOverworldGenRulesData::read($in)); - $multinoiseGenRules = $in->readOptional(fn() => BiomeMultinoiseGenRulesData::read($in)); - $legacyWorldGenRules = $in->readOptional(fn() => BiomeLegacyWorldGenRulesData::read($in)); + public static function read(ByteBufferReader $in) : self{ + $climate = CommonTypes::readOptional($in, fn() => BiomeClimateData::read($in)); + $consolidatedFeatures = CommonTypes::readOptional($in, fn() => BiomeConsolidatedFeaturesData::read($in)); + $mountainParams = CommonTypes::readOptional($in, fn() => BiomeMountainParamsData::read($in)); + $surfaceMaterialAdjustment = CommonTypes::readOptional($in, fn() => BiomeSurfaceMaterialAdjustmentData::read($in)); + $surfaceMaterial = CommonTypes::readOptional($in, fn() => BiomeSurfaceMaterialData::read($in)); + $swampSurface = CommonTypes::getBool($in); + $frozenOceanSurface = CommonTypes::getBool($in); + $theEndSurface = CommonTypes::getBool($in); + $mesaSurface = CommonTypes::readOptional($in, fn() => BiomeMesaSurfaceData::read($in)); + $cappedSurface = CommonTypes::readOptional($in, fn() => BiomeCappedSurfaceData::read($in)); + $overworldGenRules = CommonTypes::readOptional($in, fn() => BiomeOverworldGenRulesData::read($in)); + $multinoiseGenRules = CommonTypes::readOptional($in, fn() => BiomeMultinoiseGenRulesData::read($in)); + $legacyWorldGenRules = CommonTypes::readOptional($in, fn() => BiomeLegacyWorldGenRulesData::read($in)); return new self( $climate, @@ -92,19 +94,19 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->writeOptional($this->climate, fn(BiomeClimateData $climate) => $climate->write($out)); - $out->writeOptional($this->consolidatedFeatures, fn(BiomeConsolidatedFeaturesData $consolidatedFeatures) => $consolidatedFeatures->write($out)); - $out->writeOptional($this->mountainParams, fn(BiomeMountainParamsData $mountainParams) => $mountainParams->write($out)); - $out->writeOptional($this->surfaceMaterialAdjustment, fn(BiomeSurfaceMaterialAdjustmentData $surfaceMaterialAdjustment) => $surfaceMaterialAdjustment->write($out)); - $out->writeOptional($this->surfaceMaterial, fn(BiomeSurfaceMaterialData $surfaceMaterial) => $surfaceMaterial->write($out)); - $out->putBool($this->swampSurface); - $out->putBool($this->frozenOceanSurface); - $out->putBool($this->theEndSurface); - $out->writeOptional($this->mesaSurface, fn(BiomeMesaSurfaceData $mesaSurface) => $mesaSurface->write($out)); - $out->writeOptional($this->cappedSurface, fn(BiomeCappedSurfaceData $cappedSurface) => $cappedSurface->write($out)); - $out->writeOptional($this->overworldGenRules, fn(BiomeOverworldGenRulesData $overworldGenRules) => $overworldGenRules->write($out)); - $out->writeOptional($this->multinoiseGenRules, fn(BiomeMultinoiseGenRulesData $multinoiseGenRules) => $multinoiseGenRules->write($out)); - $out->writeOptional($this->legacyWorldGenRules, fn(BiomeLegacyWorldGenRulesData $legacyWorldGenRules) => $legacyWorldGenRules->write($out)); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::writeOptional($out, $this->climate, fn(ByteBufferWriter $out, BiomeClimateData $v) => $v->write($out)); + CommonTypes::writeOptional($out, $this->consolidatedFeatures, fn(ByteBufferWriter $out, BiomeConsolidatedFeaturesData $v) => $v->write($out)); + CommonTypes::writeOptional($out, $this->mountainParams, fn(ByteBufferWriter $out, BiomeMountainParamsData $v) => $v->write($out)); + CommonTypes::writeOptional($out, $this->surfaceMaterialAdjustment, fn(ByteBufferWriter $out, BiomeSurfaceMaterialAdjustmentData $v) => $v->write($out)); + CommonTypes::writeOptional($out, $this->surfaceMaterial, fn(ByteBufferWriter $out, BiomeSurfaceMaterialData $v) => $v->write($out)); + CommonTypes::putBool($out, $this->swampSurface); + CommonTypes::putBool($out, $this->frozenOceanSurface); + CommonTypes::putBool($out, $this->theEndSurface); + CommonTypes::writeOptional($out, $this->mesaSurface, fn(ByteBufferWriter $out, BiomeMesaSurfaceData $v) => $v->write($out)); + CommonTypes::writeOptional($out, $this->cappedSurface, fn(ByteBufferWriter $out, BiomeCappedSurfaceData $v) => $v->write($out)); + CommonTypes::writeOptional($out, $this->overworldGenRules, fn(ByteBufferWriter $out, BiomeOverworldGenRulesData $v) => $v->write($out)); + CommonTypes::writeOptional($out, $this->multinoiseGenRules, fn(ByteBufferWriter $out, BiomeMultinoiseGenRulesData $v) => $v->write($out)); + CommonTypes::writeOptional($out, $this->legacyWorldGenRules, fn(ByteBufferWriter $out, BiomeLegacyWorldGenRulesData $v) => $v->write($out)); } } diff --git a/src/types/biome/chunkgen/BiomeElementData.php b/src/types/biome/chunkgen/BiomeElementData.php index 5d4e80f4..c05dd1de 100644 --- a/src/types/biome/chunkgen/BiomeElementData.php +++ b/src/types/biome/chunkgen/BiomeElementData.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\biome\chunkgen; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; final class BiomeElementData{ @@ -45,14 +48,14 @@ public function getHeightMax() : int{ return $this->heightMax; } public function getSurfaceMaterial() : BiomeSurfaceMaterialData{ return $this->surfaceMaterial; } - public static function read(PacketSerializer $in) : self{ - $noiseFrequencyScale = $in->getLFloat(); - $noiseLowerBound = $in->getLFloat(); - $noiseUpperBound = $in->getLFloat(); - $heightMinType = $in->getVarInt(); - $heightMin = $in->getLShort(); - $heightMaxType = $in->getVarInt(); - $heightMax = $in->getLShort(); + public static function read(ByteBufferReader $in) : self{ + $noiseFrequencyScale = LE::readFloat($in); + $noiseLowerBound = LE::readFloat($in); + $noiseUpperBound = LE::readFloat($in); + $heightMinType = VarInt::readSignedInt($in); + $heightMin = LE::readSignedShort($in); + $heightMaxType = VarInt::readSignedInt($in); + $heightMax = LE::readSignedShort($in); $surfaceMaterial = BiomeSurfaceMaterialData::read($in); return new self( @@ -67,14 +70,14 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putLFloat($this->noiseFrequencyScale); - $out->putLFloat($this->noiseLowerBound); - $out->putLFloat($this->noiseUpperBound); - $out->putVarInt($this->heightMinType); - $out->putLShort($this->heightMin); - $out->putVarInt($this->heightMaxType); - $out->putLShort($this->heightMax); + public function write(ByteBufferWriter $out) : void{ + LE::writeFloat($out, $this->noiseFrequencyScale); + LE::writeFloat($out, $this->noiseLowerBound); + LE::writeFloat($out, $this->noiseUpperBound); + VarInt::writeSignedInt($out, $this->heightMinType); + LE::writeSignedShort($out, $this->heightMin); + VarInt::writeSignedInt($out, $this->heightMaxType); + LE::writeSignedShort($out, $this->heightMax); $this->surfaceMaterial->write($out); } } diff --git a/src/types/biome/chunkgen/BiomeLegacyWorldGenRulesData.php b/src/types/biome/chunkgen/BiomeLegacyWorldGenRulesData.php index a64640d0..66a68682 100644 --- a/src/types/biome/chunkgen/BiomeLegacyWorldGenRulesData.php +++ b/src/types/biome/chunkgen/BiomeLegacyWorldGenRulesData.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\biome\chunkgen; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use function count; final class BiomeLegacyWorldGenRulesData{ @@ -31,9 +33,9 @@ public function __construct( */ public function getLegacyPreHills() : array{ return $this->legacyPreHills; } - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ $legacyPreHills = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $legacyPreHills[] = BiomeConditionalTransformationData::read($in); } @@ -42,8 +44,8 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->legacyPreHills)); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->legacyPreHills)); foreach($this->legacyPreHills as $biome){ $biome->write($out); } diff --git a/src/types/biome/chunkgen/BiomeMesaSurfaceData.php b/src/types/biome/chunkgen/BiomeMesaSurfaceData.php index 293f45a3..956b3b2f 100644 --- a/src/types/biome/chunkgen/BiomeMesaSurfaceData.php +++ b/src/types/biome/chunkgen/BiomeMesaSurfaceData.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\biome\chunkgen; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class BiomeMesaSurfaceData{ @@ -33,11 +36,11 @@ public function hasBrycePillars() : bool{ return $this->brycePillars; } public function hasForest() : bool{ return $this->forest; } - public static function read(PacketSerializer $in) : self{ - $clayMaterial = $in->getLInt(); - $hardClayMaterial = $in->getLInt(); - $brycePillars = $in->getBool(); - $forest = $in->getBool(); + public static function read(ByteBufferReader $in) : self{ + $clayMaterial = LE::readUnsignedInt($in); + $hardClayMaterial = LE::readUnsignedInt($in); + $brycePillars = CommonTypes::getBool($in); + $forest = CommonTypes::getBool($in); return new self( $clayMaterial, @@ -47,10 +50,10 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putLInt($this->clayMaterial); - $out->putLInt($this->hardClayMaterial); - $out->putBool($this->brycePillars); - $out->putBool($this->forest); + public function write(ByteBufferWriter $out) : void{ + LE::writeUnsignedInt($out, $this->clayMaterial); + LE::writeUnsignedInt($out, $this->hardClayMaterial); + CommonTypes::putBool($out, $this->brycePillars); + CommonTypes::putBool($out, $this->forest); } } diff --git a/src/types/biome/chunkgen/BiomeMountainParamsData.php b/src/types/biome/chunkgen/BiomeMountainParamsData.php index 7e8748cd..735ef695 100644 --- a/src/types/biome/chunkgen/BiomeMountainParamsData.php +++ b/src/types/biome/chunkgen/BiomeMountainParamsData.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\biome\chunkgen; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class BiomeMountainParamsData{ @@ -39,13 +42,13 @@ public function hasEastSlopes() : bool{ return $this->eastSlopes; } public function hasTopSlideEnabled() : bool{ return $this->topSlideEnabled; } - public static function read(PacketSerializer $in) : self{ - $steepBlock = $in->getLInt(); - $northSlopes = $in->getBool(); - $southSlopes = $in->getBool(); - $westSlopes = $in->getBool(); - $eastSlopes = $in->getBool(); - $topSlideEnabled = $in->getBool(); + public static function read(ByteBufferReader $in) : self{ + $steepBlock = LE::readUnsignedInt($in); + $northSlopes = CommonTypes::getBool($in); + $southSlopes = CommonTypes::getBool($in); + $westSlopes = CommonTypes::getBool($in); + $eastSlopes = CommonTypes::getBool($in); + $topSlideEnabled = CommonTypes::getBool($in); return new self( $steepBlock, @@ -57,12 +60,12 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putLInt($this->steepBlock); - $out->putBool($this->northSlopes); - $out->putBool($this->southSlopes); - $out->putBool($this->westSlopes); - $out->putBool($this->eastSlopes); - $out->putBool($this->topSlideEnabled); + public function write(ByteBufferWriter $out) : void{ + LE::writeUnsignedInt($out, $this->steepBlock); + CommonTypes::putBool($out, $this->northSlopes); + CommonTypes::putBool($out, $this->southSlopes); + CommonTypes::putBool($out, $this->westSlopes); + CommonTypes::putBool($out, $this->eastSlopes); + CommonTypes::putBool($out, $this->topSlideEnabled); } } diff --git a/src/types/biome/chunkgen/BiomeMultinoiseGenRulesData.php b/src/types/biome/chunkgen/BiomeMultinoiseGenRulesData.php index 149e2acb..b7e25276 100644 --- a/src/types/biome/chunkgen/BiomeMultinoiseGenRulesData.php +++ b/src/types/biome/chunkgen/BiomeMultinoiseGenRulesData.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\biome\chunkgen; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; final class BiomeMultinoiseGenRulesData{ @@ -36,12 +38,12 @@ public function getWeirdness() : float{ return $this->weirdness; } public function getWeight() : float{ return $this->weight; } - public static function read(PacketSerializer $in) : self{ - $temperature = $in->getLFloat(); - $humidity = $in->getLFloat(); - $altitude = $in->getLFloat(); - $weirdness = $in->getLFloat(); - $weight = $in->getLFloat(); + public static function read(ByteBufferReader $in) : self{ + $temperature = LE::readFloat($in); + $humidity = LE::readFloat($in); + $altitude = LE::readFloat($in); + $weirdness = LE::readFloat($in); + $weight = LE::readFloat($in); return new self( $temperature, @@ -52,11 +54,11 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putLFloat($this->temperature); - $out->putLFloat($this->humidity); - $out->putLFloat($this->altitude); - $out->putLFloat($this->weirdness); - $out->putLFloat($this->weight); + public function write(ByteBufferWriter $out) : void{ + LE::writeFloat($out, $this->temperature); + LE::writeFloat($out, $this->humidity); + LE::writeFloat($out, $this->altitude); + LE::writeFloat($out, $this->weirdness); + LE::writeFloat($out, $this->weight); } } diff --git a/src/types/biome/chunkgen/BiomeOverworldGenRulesData.php b/src/types/biome/chunkgen/BiomeOverworldGenRulesData.php index f4f194f7..3011964e 100644 --- a/src/types/biome/chunkgen/BiomeOverworldGenRulesData.php +++ b/src/types/biome/chunkgen/BiomeOverworldGenRulesData.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\biome\chunkgen; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use function count; final class BiomeOverworldGenRulesData{ @@ -73,39 +75,39 @@ public function getPostShoreEdges() : array{ return $this->postShoreEdges; } */ public function getClimates() : array{ return $this->climates; } - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ $hillTransformations = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $hillTransformations[] = BiomeWeightedData::read($in); } $mutateTransformations = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $mutateTransformations[] = BiomeWeightedData::read($in); } $riverTransformations = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $riverTransformations[] = BiomeWeightedData::read($in); } $shoreTransformations = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $shoreTransformations[] = BiomeWeightedData::read($in); } $preHillsEdges = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $preHillsEdges[] = BiomeConditionalTransformationData::read($in); } $postShoreEdges = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $postShoreEdges[] = BiomeConditionalTransformationData::read($in); } $climates = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $climates[] = BiomeWeightedTemperatureData::read($in); } @@ -120,38 +122,38 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->hillTransformations)); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->hillTransformations)); foreach($this->hillTransformations as $transformation){ $transformation->write($out); } - $out->putUnsignedVarInt(count($this->mutateTransformations)); + VarInt::writeUnsignedInt($out, count($this->mutateTransformations)); foreach($this->mutateTransformations as $transformation){ $transformation->write($out); } - $out->putUnsignedVarInt(count($this->riverTransformations)); + VarInt::writeUnsignedInt($out, count($this->riverTransformations)); foreach($this->riverTransformations as $transformation){ $transformation->write($out); } - $out->putUnsignedVarInt(count($this->shoreTransformations)); + VarInt::writeUnsignedInt($out, count($this->shoreTransformations)); foreach($this->shoreTransformations as $transformation){ $transformation->write($out); } - $out->putUnsignedVarInt(count($this->preHillsEdges)); + VarInt::writeUnsignedInt($out, count($this->preHillsEdges)); foreach($this->preHillsEdges as $edge){ $edge->write($out); } - $out->putUnsignedVarInt(count($this->postShoreEdges)); + VarInt::writeUnsignedInt($out, count($this->postShoreEdges)); foreach($this->postShoreEdges as $edge){ $edge->write($out); } - $out->putUnsignedVarInt(count($this->climates)); + VarInt::writeUnsignedInt($out, count($this->climates)); foreach($this->climates as $climate){ $climate->write($out); } diff --git a/src/types/biome/chunkgen/BiomeScatterParamData.php b/src/types/biome/chunkgen/BiomeScatterParamData.php index 455d77a7..7f61dda5 100644 --- a/src/types/biome/chunkgen/BiomeScatterParamData.php +++ b/src/types/biome/chunkgen/BiomeScatterParamData.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\biome\chunkgen; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; use function count; final class BiomeScatterParamData{ @@ -52,18 +55,18 @@ public function getIterationsType() : int{ return $this->iterationsType; } public function getIterations() : int{ return $this->iterations; } - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ $coordinates = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $coordinates[] = BiomeCoordinateData::read($in); } - $evalOrder = $in->getVarInt(); - $chancePercentType = $in->getVarInt(); - $chancePercent = $in->getLShort(); - $chanceNumerator = $in->getLInt(); - $chanceDenominator = $in->getLInt(); - $iterationsType = $in->getVarInt(); - $iterations = $in->getLShort(); + $evalOrder = VarInt::readSignedInt($in); + $chancePercentType = VarInt::readSignedInt($in); + $chancePercent = LE::readSignedShort($in); + $chanceNumerator = LE::readSignedInt($in); + $chanceDenominator = LE::readSignedInt($in); + $iterationsType = VarInt::readSignedInt($in); + $iterations = LE::readSignedShort($in); return new self( $coordinates, @@ -77,17 +80,17 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->coordinates)); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->coordinates)); foreach($this->coordinates as $coordinate){ $coordinate->write($out); } - $out->putVarInt($this->evalOrder); - $out->putVarInt($this->chancePercentType); - $out->putLShort($this->chancePercent); - $out->putLInt($this->chanceNumerator); - $out->putLInt($this->chanceDenominator); - $out->putVarInt($this->iterationsType); - $out->putLShort($this->iterations); + VarInt::writeSignedInt($out, $this->evalOrder); + VarInt::writeSignedInt($out, $this->chancePercentType); + LE::writeSignedShort($out, $this->chancePercent); + LE::writeSignedInt($out, $this->chanceNumerator); + LE::writeSignedInt($out, $this->chanceDenominator); + VarInt::writeSignedInt($out, $this->iterationsType); + LE::writeSignedShort($out, $this->iterations); } } diff --git a/src/types/biome/chunkgen/BiomeSurfaceMaterialAdjustmentData.php b/src/types/biome/chunkgen/BiomeSurfaceMaterialAdjustmentData.php index 67ac1c14..f545beda 100644 --- a/src/types/biome/chunkgen/BiomeSurfaceMaterialAdjustmentData.php +++ b/src/types/biome/chunkgen/BiomeSurfaceMaterialAdjustmentData.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\biome\chunkgen; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use function count; final class BiomeSurfaceMaterialAdjustmentData{ @@ -31,18 +33,18 @@ public function __construct( */ public function getAdjustments() : array{ return $this->adjustments; } - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ $adjustments = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ $adjustments[] = BiomeElementData::read($in); } return new self($adjustments); } - public function write(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->adjustments)); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->adjustments)); foreach($this->adjustments as $adjustment){ $adjustment->write($out); } diff --git a/src/types/biome/chunkgen/BiomeSurfaceMaterialData.php b/src/types/biome/chunkgen/BiomeSurfaceMaterialData.php index 722d2aec..04e5bb52 100644 --- a/src/types/biome/chunkgen/BiomeSurfaceMaterialData.php +++ b/src/types/biome/chunkgen/BiomeSurfaceMaterialData.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\biome\chunkgen; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; final class BiomeSurfaceMaterialData{ @@ -39,13 +41,13 @@ public function getSeaBlock() : int{ return $this->seaBlock; } public function getSeaFloorDepth() : int{ return $this->seaFloorDepth; } - public static function read(PacketSerializer $in) : self{ - $topBlock = $in->getLInt(); - $midBlock = $in->getLInt(); - $seaFloorBlock = $in->getLInt(); - $foundationBlock = $in->getLInt(); - $seaBlock = $in->getLInt(); - $seaFloorDepth = $in->getLInt(); + public static function read(ByteBufferReader $in) : self{ + $topBlock = LE::readUnsignedInt($in); + $midBlock = LE::readUnsignedInt($in); + $seaFloorBlock = LE::readUnsignedInt($in); + $foundationBlock = LE::readUnsignedInt($in); + $seaBlock = LE::readUnsignedInt($in); + $seaFloorDepth = LE::readSignedInt($in); return new self( $topBlock, @@ -57,12 +59,12 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putLInt($this->topBlock); - $out->putLInt($this->midBlock); - $out->putLInt($this->seaFloorBlock); - $out->putLInt($this->foundationBlock); - $out->putLInt($this->seaBlock); - $out->putLInt($this->seaFloorDepth); + public function write(ByteBufferWriter $out) : void{ + LE::writeUnsignedInt($out, $this->topBlock); + LE::writeUnsignedInt($out, $this->midBlock); + LE::writeUnsignedInt($out, $this->seaFloorBlock); + LE::writeUnsignedInt($out, $this->foundationBlock); + LE::writeUnsignedInt($out, $this->seaBlock); + LE::writeSignedInt($out, $this->seaFloorDepth); } } diff --git a/src/types/biome/chunkgen/BiomeWeightedData.php b/src/types/biome/chunkgen/BiomeWeightedData.php index 9309cc61..fe28c799 100644 --- a/src/types/biome/chunkgen/BiomeWeightedData.php +++ b/src/types/biome/chunkgen/BiomeWeightedData.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\biome\chunkgen; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; final class BiomeWeightedData{ @@ -27,9 +29,9 @@ public function getBiome() : int{ return $this->biome; } public function getWeight() : int{ return $this->weight; } - public static function read(PacketSerializer $in) : self{ - $biome = $in->getLShort(); - $weight = $in->getLInt(); + public static function read(ByteBufferReader $in) : self{ + $biome = LE::readSignedShort($in); + $weight = LE::readUnsignedInt($in); return new self( $biome, @@ -37,8 +39,8 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putLShort($this->biome); - $out->putLInt($this->weight); + public function write(ByteBufferWriter $out) : void{ + LE::writeSignedShort($out, $this->biome); + LE::writeUnsignedInt($out, $this->weight); } } diff --git a/src/types/biome/chunkgen/BiomeWeightedTemperatureData.php b/src/types/biome/chunkgen/BiomeWeightedTemperatureData.php index be40c395..54a5ec63 100644 --- a/src/types/biome/chunkgen/BiomeWeightedTemperatureData.php +++ b/src/types/biome/chunkgen/BiomeWeightedTemperatureData.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\biome\chunkgen; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; final class BiomeWeightedTemperatureData{ @@ -27,9 +30,9 @@ public function getTemperature() : int{ return $this->temperature; } public function getWeight() : int{ return $this->weight; } - public static function read(PacketSerializer $in) : self{ - $temperature = $in->getVarInt(); - $weight = $in->getLInt(); + public static function read(ByteBufferReader $in) : self{ + $temperature = VarInt::readSignedInt($in); + $weight = LE::readUnsignedInt($in); return new self( $temperature, @@ -37,8 +40,8 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putVarInt($this->temperature); - $out->putLInt($this->weight); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->temperature); + LE::writeUnsignedInt($out, $this->weight); } } diff --git a/src/types/camera/CameraAimAssistCategory.php b/src/types/camera/CameraAimAssistCategory.php index 67e8af52..a0db4c67 100644 --- a/src/types/camera/CameraAimAssistCategory.php +++ b/src/types/camera/CameraAimAssistCategory.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\camera; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class CameraAimAssistCategory{ @@ -27,8 +29,8 @@ public function getName() : string{ return $this->name; } public function getPriorities() : CameraAimAssistCategoryPriorities{ return $this->priorities; } - public static function read(PacketSerializer $in) : self{ - $name = $in->getString(); + public static function read(ByteBufferReader $in) : self{ + $name = CommonTypes::getString($in); $priorities = CameraAimAssistCategoryPriorities::read($in); return new self( $name, @@ -36,8 +38,8 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putString($this->name); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->name); $this->priorities->write($out); } } diff --git a/src/types/camera/CameraAimAssistCategoryBlockPriority.php b/src/types/camera/CameraAimAssistCategoryBlockPriority.php index 9b4893e6..60d2c0d4 100644 --- a/src/types/camera/CameraAimAssistCategoryBlockPriority.php +++ b/src/types/camera/CameraAimAssistCategoryBlockPriority.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\camera; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class CameraAimAssistCategoryBlockPriority{ @@ -27,17 +30,17 @@ public function getIdentifier() : string{ return $this->identifier; } public function getPriority() : int{ return $this->priority; } - public static function read(PacketSerializer $in) : self{ - $identifier = $in->getString(); - $priority = $in->getLInt(); + public static function read(ByteBufferReader $in) : self{ + $identifier = CommonTypes::getString($in); + $priority = LE::readSignedInt($in); return new self( $identifier, $priority ); } - public function write(PacketSerializer $out) : void{ - $out->putString($this->identifier); - $out->putLInt($this->priority); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->identifier); + LE::writeSignedInt($out, $this->priority); } } diff --git a/src/types/camera/CameraAimAssistCategoryEntityPriority.php b/src/types/camera/CameraAimAssistCategoryEntityPriority.php index d6c7e1c8..0676b41e 100644 --- a/src/types/camera/CameraAimAssistCategoryEntityPriority.php +++ b/src/types/camera/CameraAimAssistCategoryEntityPriority.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\camera; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class CameraAimAssistCategoryEntityPriority{ @@ -27,17 +30,17 @@ public function getIdentifier() : string{ return $this->identifier; } public function getPriority() : int{ return $this->priority; } - public static function read(PacketSerializer $in) : self{ - $identifier = $in->getString(); - $priority = $in->getLInt(); + public static function read(ByteBufferReader $in) : self{ + $identifier = CommonTypes::getString($in); + $priority = LE::readSignedInt($in); return new self( $identifier, $priority ); } - public function write(PacketSerializer $out) : void{ - $out->putString($this->identifier); - $out->putLInt($this->priority); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->identifier); + LE::writeSignedInt($out, $this->priority); } } diff --git a/src/types/camera/CameraAimAssistCategoryPriorities.php b/src/types/camera/CameraAimAssistCategoryPriorities.php index 838b7629..82e05cc2 100644 --- a/src/types/camera/CameraAimAssistCategoryPriorities.php +++ b/src/types/camera/CameraAimAssistCategoryPriorities.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol\types\camera; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use function count; final class CameraAimAssistCategoryPriorities{ @@ -44,19 +48,19 @@ public function getDefaultEntityPriority() : ?int{ return $this->defaultEntityPr public function getDefaultBlockPriority() : ?int{ return $this->defaultBlockPriority; } - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ $entities = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ $entities[] = CameraAimAssistCategoryEntityPriority::read($in); } $blocks = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ $blocks[] = CameraAimAssistCategoryBlockPriority::read($in); } - $defaultEntityPriority = $in->readOptional(fn() => $in->getLInt()); - $defaultBlockPriority = $in->readOptional(fn() => $in->getLInt()); + $defaultEntityPriority = CommonTypes::readOptional($in, LE::readSignedInt(...)); + $defaultBlockPriority = CommonTypes::readOptional($in, LE::readSignedInt(...)); return new self( $entities, $blocks, @@ -65,18 +69,18 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->entities)); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->entities)); foreach($this->entities as $entity){ $entity->write($out); } - $out->putUnsignedVarInt(count($this->blocks)); + VarInt::writeUnsignedInt($out, count($this->blocks)); foreach($this->blocks as $block){ $block->write($out); } - $out->writeOptional($this->defaultEntityPriority, fn(int $v) => $out->putLInt($v)); - $out->writeOptional($this->defaultBlockPriority, fn(int $v) => $out->putLInt($v)); + CommonTypes::writeOptional($out, $this->defaultEntityPriority, LE::writeSignedInt(...)); + CommonTypes::writeOptional($out, $this->defaultBlockPriority, LE::writeSignedInt(...)); } } diff --git a/src/types/camera/CameraAimAssistPreset.php b/src/types/camera/CameraAimAssistPreset.php index e9914fd9..117f6560 100644 --- a/src/types/camera/CameraAimAssistPreset.php +++ b/src/types/camera/CameraAimAssistPreset.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\camera; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use function count; final class CameraAimAssistPreset{ @@ -54,26 +57,26 @@ public function getDefaultItemSettings() : ?string{ return $this->defaultItemSet public function getDefaultHandSettings() : ?string{ return $this->defaultHandSettings; } - public static function read(PacketSerializer $in) : self{ - $identifier = $in->getString(); + public static function read(ByteBufferReader $in) : self{ + $identifier = CommonTypes::getString($in); $exclusionList = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ - $exclusionList[] = $in->getString(); + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ + $exclusionList[] = CommonTypes::getString($in); } $liquidTargetingList = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ - $liquidTargetingList[] = $in->getString(); + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ + $liquidTargetingList[] = CommonTypes::getString($in); } $itemSettings = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ $itemSettings[] = CameraAimAssistPresetItemSettings::read($in); } - $defaultItemSettings = $in->readOptional(fn() => $in->getString()); - $defaultHandSettings = $in->readOptional(fn() => $in->getString()); + $defaultItemSettings = CommonTypes::readOptional($in, fn() => CommonTypes::getString($in)); + $defaultHandSettings = CommonTypes::readOptional($in, fn() => CommonTypes::getString($in)); return new self( $identifier, @@ -85,25 +88,25 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putString($this->identifier); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->identifier); - $out->putUnsignedVarInt(count($this->exclusionList)); + VarInt::writeUnsignedInt($out, count($this->exclusionList)); foreach($this->exclusionList as $exclusion){ - $out->putString($exclusion); + CommonTypes::putString($out, $exclusion); } - $out->putUnsignedVarInt(count($this->liquidTargetingList)); + VarInt::writeUnsignedInt($out, count($this->liquidTargetingList)); foreach($this->liquidTargetingList as $liquidTargeting){ - $out->putString($liquidTargeting); + CommonTypes::putString($out, $liquidTargeting); } - $out->putUnsignedVarInt(count($this->itemSettings)); + VarInt::writeUnsignedInt($out, count($this->itemSettings)); foreach($this->itemSettings as $itemSetting){ $itemSetting->write($out); } - $out->writeOptional($this->defaultItemSettings, fn(string $v) => $out->putString($v)); - $out->writeOptional($this->defaultHandSettings, fn(string $v) => $out->putString($v)); + CommonTypes::writeOptional($out, $this->defaultItemSettings, CommonTypes::putString(...)); + CommonTypes::writeOptional($out, $this->defaultHandSettings, CommonTypes::putString(...)); } } diff --git a/src/types/camera/CameraAimAssistPresetItemSettings.php b/src/types/camera/CameraAimAssistPresetItemSettings.php index 61e0e639..d3ebceac 100644 --- a/src/types/camera/CameraAimAssistPresetItemSettings.php +++ b/src/types/camera/CameraAimAssistPresetItemSettings.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\camera; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class CameraAimAssistPresetItemSettings{ @@ -27,17 +29,17 @@ public function getItemIdentifier() : string{ return $this->itemIdentifier; } public function getCategoryName() : string{ return $this->categoryName; } - public static function read(PacketSerializer $in) : self{ - $itemIdentifier = $in->getString(); - $categoryName = $in->getString(); + public static function read(ByteBufferReader $in) : self{ + $itemIdentifier = CommonTypes::getString($in); + $categoryName = CommonTypes::getString($in); return new self( $itemIdentifier, $categoryName ); } - public function write(PacketSerializer $out) : void{ - $out->putString($this->itemIdentifier); - $out->putString($this->categoryName); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->itemIdentifier); + CommonTypes::putString($out, $this->categoryName); } } diff --git a/src/types/camera/CameraFadeInstruction.php b/src/types/camera/CameraFadeInstruction.php index 01afed15..53c578ff 100644 --- a/src/types/camera/CameraFadeInstruction.php +++ b/src/types/camera/CameraFadeInstruction.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\camera; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\camera\CameraFadeInstructionColor as Color; use pocketmine\network\mcpe\protocol\types\camera\CameraFadeInstructionTime as Time; @@ -29,17 +31,17 @@ public function getTime() : ?Time{ return $this->time; } public function getColor() : ?Color{ return $this->color; } - public static function read(PacketSerializer $in) : self{ - $time = $in->readOptional(fn() => Time::read($in)); - $color = $in->readOptional(fn() => Color::read($in)); + public static function read(ByteBufferReader $in) : self{ + $time = CommonTypes::readOptional($in, fn() => Time::read($in)); + $color = CommonTypes::readOptional($in, fn() => Color::read($in)); return new self( $time, $color ); } - public function write(PacketSerializer $out) : void{ - $out->writeOptional($this->time, fn(Time $v) => $v->write($out)); - $out->writeOptional($this->color, fn(Color $v) => $v->write($out)); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::writeOptional($out, $this->time, fn(ByteBufferWriter $out, Time $v) => $v->write($out)); + CommonTypes::writeOptional($out, $this->color, fn(ByteBufferWriter $out, Color $v) => $v->write($out)); } } diff --git a/src/types/camera/CameraFadeInstructionColor.php b/src/types/camera/CameraFadeInstructionColor.php index 701bf476..c7b20814 100644 --- a/src/types/camera/CameraFadeInstructionColor.php +++ b/src/types/camera/CameraFadeInstructionColor.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\camera; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; final class CameraFadeInstructionColor{ @@ -30,16 +32,16 @@ public function getGreen() : float{ return $this->green; } public function getBlue() : float{ return $this->blue; } - public static function read(PacketSerializer $in) : self{ - $red = $in->getLFloat(); - $green = $in->getLFloat(); - $blue = $in->getLFloat(); + public static function read(ByteBufferReader $in) : self{ + $red = LE::readFloat($in); + $green = LE::readFloat($in); + $blue = LE::readFloat($in); return new self($red, $green, $blue); } - public function write(PacketSerializer $out) : void{ - $out->putLFloat($this->red); - $out->putLFloat($this->green); - $out->putLFloat($this->blue); + public function write(ByteBufferWriter $out) : void{ + LE::writeFloat($out, $this->red); + LE::writeFloat($out, $this->green); + LE::writeFloat($out, $this->blue); } } diff --git a/src/types/camera/CameraFadeInstructionTime.php b/src/types/camera/CameraFadeInstructionTime.php index 330a2e1e..fb4dd3f8 100644 --- a/src/types/camera/CameraFadeInstructionTime.php +++ b/src/types/camera/CameraFadeInstructionTime.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\camera; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; final class CameraFadeInstructionTime{ @@ -30,16 +32,16 @@ public function getStayTime() : float{ return $this->stayTime; } public function getFadeOutTime() : float{ return $this->fadeOutTime; } - public static function read(PacketSerializer $in) : self{ - $fadeInTime = $in->getLFloat(); - $stayTime = $in->getLFloat(); - $fadeOutTime = $in->getLFloat(); + public static function read(ByteBufferReader $in) : self{ + $fadeInTime = LE::readFloat($in); + $stayTime = LE::readFloat($in); + $fadeOutTime = LE::readFloat($in); return new self($fadeInTime, $stayTime, $fadeOutTime); } - public function write(PacketSerializer $out) : void{ - $out->putLFloat($this->fadeInTime); - $out->putLFloat($this->stayTime); - $out->putLFloat($this->fadeOutTime); + public function write(ByteBufferWriter $out) : void{ + LE::writeFloat($out, $this->fadeInTime); + LE::writeFloat($out, $this->stayTime); + LE::writeFloat($out, $this->fadeOutTime); } } diff --git a/src/types/camera/CameraFovInstruction.php b/src/types/camera/CameraFovInstruction.php index 760c9271..23ea79ed 100644 --- a/src/types/camera/CameraFovInstruction.php +++ b/src/types/camera/CameraFovInstruction.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol\types\camera; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class CameraFovInstruction{ @@ -39,11 +43,11 @@ public function getEaseType() : int{ return $this->easeType; } public function getClear() : bool{ return $this->clear; } - public static function read(PacketSerializer $in) : self{ - $fieldOfView = $in->getLFloat(); - $easeTime = $in->getLFloat(); - $easeType = $in->getByte(); - $clear = $in->getBool(); + public static function read(ByteBufferReader $in) : self{ + $fieldOfView = LE::readFloat($in); + $easeTime = LE::readFloat($in); + $easeType = Byte::readUnsigned($in); + $clear = CommonTypes::getBool($in); return new self( $fieldOfView, $easeTime, @@ -52,10 +56,10 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putLFloat($this->fieldOfView); - $out->putLFloat($this->easeTime); - $out->putByte($this->easeType); - $out->putBool($this->clear); + public function write(ByteBufferWriter $out) : void{ + LE::writeFloat($out, $this->fieldOfView); + LE::writeFloat($out, $this->easeTime); + Byte::writeUnsigned($out, $this->easeType); + CommonTypes::putBool($out, $this->clear); } } diff --git a/src/types/camera/CameraPreset.php b/src/types/camera/CameraPreset.php index 82135e59..a7f99245 100644 --- a/src/types/camera/CameraPreset.php +++ b/src/types/camera/CameraPreset.php @@ -14,9 +14,13 @@ namespace pocketmine\network\mcpe\protocol\types\camera; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; use pocketmine\math\Vector2; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\ControlScheme; final class CameraPreset{ @@ -92,29 +96,29 @@ public function getAimAssist() : ?CameraPresetAimAssist{ return $this->aimAssist public function getControlScheme() : ?ControlScheme{ return $this->controlScheme; } - public static function read(PacketSerializer $in) : self{ - $name = $in->getString(); - $parent = $in->getString(); - $xPosition = $in->readOptional($in->getLFloat(...)); - $yPosition = $in->readOptional($in->getLFloat(...)); - $zPosition = $in->readOptional($in->getLFloat(...)); - $pitch = $in->readOptional($in->getLFloat(...)); - $yaw = $in->readOptional($in->getLFloat(...)); - $rotationSpeed = $in->readOptional($in->getLFloat(...)); - $snapToTarget = $in->readOptional($in->getBool(...)); - $horizontalRotationLimit = $in->readOptional($in->getVector2(...)); - $verticalRotationLimit = $in->readOptional($in->getVector2(...)); - $continueTargeting = $in->readOptional($in->getBool(...)); - $blockListeningRadius = $in->readOptional($in->getLFloat(...)); - $viewOffset = $in->readOptional($in->getVector2(...)); - $entityOffset = $in->readOptional($in->getVector3(...)); - $radius = $in->readOptional($in->getLFloat(...)); - $yawLimitMin = $in->readOptional($in->getLFloat(...)); - $yawLimitMax = $in->readOptional($in->getLFloat(...)); - $audioListenerType = $in->readOptional($in->getByte(...)); - $playerEffects = $in->readOptional($in->getBool(...)); - $aimAssist = $in->readOptional(fn() => CameraPresetAimAssist::read($in)); - $controlScheme = $in->readOptional(fn() => ControlScheme::fromPacket($in->getByte())); + public static function read(ByteBufferReader $in) : self{ + $name = CommonTypes::getString($in); + $parent = CommonTypes::getString($in); + $xPosition = CommonTypes::readOptional($in, LE::readFloat(...)); + $yPosition = CommonTypes::readOptional($in, LE::readFloat(...)); + $zPosition = CommonTypes::readOptional($in, LE::readFloat(...)); + $pitch = CommonTypes::readOptional($in, LE::readFloat(...)); + $yaw = CommonTypes::readOptional($in, LE::readFloat(...)); + $rotationSpeed = CommonTypes::readOptional($in, LE::readFloat(...)); + $snapToTarget = CommonTypes::readOptional($in, CommonTypes::getBool(...)); + $horizontalRotationLimit = CommonTypes::readOptional($in, CommonTypes::getVector2(...)); + $verticalRotationLimit = CommonTypes::readOptional($in, CommonTypes::getVector2(...)); + $continueTargeting = CommonTypes::readOptional($in, CommonTypes::getBool(...)); + $blockListeningRadius = CommonTypes::readOptional($in, LE::readFloat(...)); + $viewOffset = CommonTypes::readOptional($in, CommonTypes::getVector2(...)); + $entityOffset = CommonTypes::readOptional($in, CommonTypes::getVector3(...)); + $radius = CommonTypes::readOptional($in, LE::readFloat(...)); + $yawLimitMin = CommonTypes::readOptional($in, LE::readFloat(...)); + $yawLimitMax = CommonTypes::readOptional($in, LE::readFloat(...)); + $audioListenerType = CommonTypes::readOptional($in, Byte::readUnsigned(...)); + $playerEffects = CommonTypes::readOptional($in, CommonTypes::getBool(...)); + $aimAssist = CommonTypes::readOptional($in, fn() => CameraPresetAimAssist::read($in)); + $controlScheme = CommonTypes::readOptional($in, fn() => ControlScheme::fromPacket(Byte::readUnsigned($in))); return new self( $name, @@ -142,28 +146,28 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putString($this->name); - $out->putString($this->parent); - $out->writeOptional($this->xPosition, $out->putLFloat(...)); - $out->writeOptional($this->yPosition, $out->putLFloat(...)); - $out->writeOptional($this->zPosition, $out->putLFloat(...)); - $out->writeOptional($this->pitch, $out->putLFloat(...)); - $out->writeOptional($this->yaw, $out->putLFloat(...)); - $out->writeOptional($this->rotationSpeed, $out->putLFloat(...)); - $out->writeOptional($this->snapToTarget, $out->putBool(...)); - $out->writeOptional($this->horizontalRotationLimit, $out->putVector2(...)); - $out->writeOptional($this->verticalRotationLimit, $out->putVector2(...)); - $out->writeOptional($this->continueTargeting, $out->putBool(...)); - $out->writeOptional($this->blockListeningRadius, $out->putLFloat(...)); - $out->writeOptional($this->viewOffset, $out->putVector2(...)); - $out->writeOptional($this->entityOffset, $out->putVector3(...)); - $out->writeOptional($this->radius, $out->putLFloat(...)); - $out->writeOptional($this->yawLimitMin, $out->putLFloat(...)); - $out->writeOptional($this->yawLimitMax, $out->putLFloat(...)); - $out->writeOptional($this->audioListenerType, $out->putByte(...)); - $out->writeOptional($this->playerEffects, $out->putBool(...)); - $out->writeOptional($this->aimAssist, fn(CameraPresetAimAssist $v) => $v->write($out)); - $out->writeOptional($this->controlScheme, fn(ControlScheme $v) => $out->putByte($v->value)); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->name); + CommonTypes::putString($out, $this->parent); + CommonTypes::writeOptional($out, $this->xPosition, LE::writeFloat(...)); + CommonTypes::writeOptional($out, $this->yPosition, LE::writeFloat(...)); + CommonTypes::writeOptional($out, $this->zPosition, LE::writeFloat(...)); + CommonTypes::writeOptional($out, $this->pitch, LE::writeFloat(...)); + CommonTypes::writeOptional($out, $this->yaw, LE::writeFloat(...)); + CommonTypes::writeOptional($out, $this->rotationSpeed, LE::writeFloat(...)); + CommonTypes::writeOptional($out, $this->snapToTarget, CommonTypes::putBool(...)); + CommonTypes::writeOptional($out, $this->horizontalRotationLimit, CommonTypes::putVector2(...)); + CommonTypes::writeOptional($out, $this->verticalRotationLimit, CommonTypes::putVector2(...)); + CommonTypes::writeOptional($out, $this->continueTargeting, CommonTypes::putBool(...)); + CommonTypes::writeOptional($out, $this->blockListeningRadius, LE::writeFloat(...)); + CommonTypes::writeOptional($out, $this->viewOffset, CommonTypes::putVector2(...)); + CommonTypes::writeOptional($out, $this->entityOffset, CommonTypes::putVector3(...)); + CommonTypes::writeOptional($out, $this->radius, LE::writeFloat(...)); + CommonTypes::writeOptional($out, $this->yawLimitMin, LE::writeFloat(...)); + CommonTypes::writeOptional($out, $this->yawLimitMax, LE::writeFloat(...)); + CommonTypes::writeOptional($out, $this->audioListenerType, Byte::writeUnsigned(...)); + CommonTypes::writeOptional($out, $this->playerEffects, CommonTypes::putBool(...)); + CommonTypes::writeOptional($out, $this->aimAssist, fn(ByteBufferWriter $out, CameraPresetAimAssist $v) => $v->write($out)); + CommonTypes::writeOptional($out, $this->controlScheme, fn(ByteBufferWriter $out, ControlScheme $v) => Byte::writeUnsigned($out, $v->value)); } } diff --git a/src/types/camera/CameraPresetAimAssist.php b/src/types/camera/CameraPresetAimAssist.php index 3114895f..b1417698 100644 --- a/src/types/camera/CameraPresetAimAssist.php +++ b/src/types/camera/CameraPresetAimAssist.php @@ -14,8 +14,12 @@ namespace pocketmine\network\mcpe\protocol\types\camera; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; use pocketmine\math\Vector2; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class CameraPresetAimAssist{ @@ -34,11 +38,11 @@ public function getViewAngle() : ?Vector2{ return $this->viewAngle; } public function getDistance() : ?float{ return $this->distance; } - public static function read(PacketSerializer $in) : self{ - $presetId = $in->readOptional($in->getString(...)); - $targetMode = $in->readOptional(fn() => CameraAimAssistTargetMode::fromPacket($in->getByte())); - $viewAngle = $in->readOptional($in->getVector2(...)); - $distance = $in->readOptional($in->getLFloat(...)); + public static function read(ByteBufferReader $in) : self{ + $presetId = CommonTypes::readOptional($in, CommonTypes::getString(...)); + $targetMode = CommonTypes::readOptional($in, fn() => CameraAimAssistTargetMode::fromPacket(Byte::readUnsigned($in))); + $viewAngle = CommonTypes::readOptional($in, CommonTypes::getVector2(...)); + $distance = CommonTypes::readOptional($in, LE::readFloat(...)); return new self( $presetId, @@ -48,10 +52,10 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->writeOptional($this->presetId, $out->putString(...)); - $out->writeOptional($this->targetMode, fn(CameraAimAssistTargetMode $v) => $out->putByte($v->value)); - $out->writeOptional($this->viewAngle, $out->putVector2(...)); - $out->writeOptional($this->distance, $out->putLFloat(...)); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::writeOptional($out, $this->presetId, CommonTypes::putString(...)); + CommonTypes::writeOptional($out, $this->targetMode, fn(ByteBufferWriter $out, CameraAimAssistTargetMode $v) => Byte::writeUnsigned($out, $v->value)); + CommonTypes::writeOptional($out, $this->viewAngle, CommonTypes::putVector2(...)); + CommonTypes::writeOptional($out, $this->distance, LE::writeFloat(...)); } } diff --git a/src/types/camera/CameraSetInstruction.php b/src/types/camera/CameraSetInstruction.php index b6ec9aa7..61d29c8a 100644 --- a/src/types/camera/CameraSetInstruction.php +++ b/src/types/camera/CameraSetInstruction.php @@ -14,9 +14,12 @@ namespace pocketmine\network\mcpe\protocol\types\camera; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; use pocketmine\math\Vector2; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class CameraSetInstruction{ @@ -50,16 +53,16 @@ public function getDefault() : ?bool{ return $this->default; } public function isIgnoringStartingValuesComponent() : bool{ return $this->ignoreStartingValuesComponent; } - public static function read(PacketSerializer $in) : self{ - $preset = $in->getLInt(); - $ease = $in->readOptional(fn() => CameraSetInstructionEase::read($in)); - $cameraPosition = $in->readOptional($in->getVector3(...)); - $rotation = $in->readOptional(fn() => CameraSetInstructionRotation::read($in)); - $facingPosition = $in->readOptional($in->getVector3(...)); - $viewOffset = $in->readOptional($in->getVector2(...)); - $entityOffset = $in->readOptional($in->getVector3(...)); - $default = $in->readOptional($in->getBool(...)); - $ignoreStartingValuesComponent = $in->getBool(); + public static function read(ByteBufferReader $in) : self{ + $preset = LE::readUnsignedInt($in); + $ease = CommonTypes::readOptional($in, CameraSetInstructionEase::read(...)); + $cameraPosition = CommonTypes::readOptional($in, CommonTypes::getVector3(...)); + $rotation = CommonTypes::readOptional($in, CameraSetInstructionRotation::read(...)); + $facingPosition = CommonTypes::readOptional($in, CommonTypes::getVector3(...)); + $viewOffset = CommonTypes::readOptional($in, CommonTypes::getVector2(...)); + $entityOffset = CommonTypes::readOptional($in, CommonTypes::getVector3(...)); + $default = CommonTypes::readOptional($in, CommonTypes::getBool(...)); + $ignoreStartingValuesComponent = CommonTypes::getBool($in); return new self( $preset, @@ -74,15 +77,15 @@ public static function read(PacketSerializer $in) : self{ ); } - public function write(PacketSerializer $out) : void{ - $out->putLInt($this->preset); - $out->writeOptional($this->ease, fn(CameraSetInstructionEase $v) => $v->write($out)); - $out->writeOptional($this->cameraPosition, $out->putVector3(...)); - $out->writeOptional($this->rotation, fn(CameraSetInstructionRotation $v) => $v->write($out)); - $out->writeOptional($this->facingPosition, $out->putVector3(...)); - $out->writeOptional($this->viewOffset, $out->putVector2(...)); - $out->writeOptional($this->entityOffset, $out->putVector3(...)); - $out->writeOptional($this->default, $out->putBool(...)); - $out->putBool($this->ignoreStartingValuesComponent); + public function write(ByteBufferWriter $out) : void{ + LE::writeUnsignedInt($out, $this->preset); + CommonTypes::writeOptional($out, $this->ease, fn(ByteBufferWriter $out, CameraSetInstructionEase $v) => $v->write($out)); + CommonTypes::writeOptional($out, $this->cameraPosition, CommonTypes::putVector3(...)); + CommonTypes::writeOptional($out, $this->rotation, fn(ByteBufferWriter $out, CameraSetInstructionRotation $v) => $v->write($out)); + CommonTypes::writeOptional($out, $this->facingPosition, CommonTypes::putVector3(...)); + CommonTypes::writeOptional($out, $this->viewOffset, CommonTypes::putVector2(...)); + CommonTypes::writeOptional($out, $this->entityOffset, CommonTypes::putVector3(...)); + CommonTypes::writeOptional($out, $this->default, CommonTypes::putBool(...)); + CommonTypes::putBool($out, $this->ignoreStartingValuesComponent); } } diff --git a/src/types/camera/CameraSetInstructionEase.php b/src/types/camera/CameraSetInstructionEase.php index 07722481..842f2ce7 100644 --- a/src/types/camera/CameraSetInstructionEase.php +++ b/src/types/camera/CameraSetInstructionEase.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\camera; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; final class CameraSetInstructionEase{ @@ -33,14 +36,14 @@ public function getType() : int{ return $this->type; } public function getDuration() : float{ return $this->duration; } - public static function read(PacketSerializer $in) : self{ - $type = $in->getByte(); - $duration = $in->getLFloat(); + public static function read(ByteBufferReader $in) : self{ + $type = Byte::readUnsigned($in); + $duration = LE::readFloat($in); return new self($type, $duration); } - public function write(PacketSerializer $out) : void{ - $out->putByte($this->type); - $out->putLFloat($this->duration); + public function write(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->type); + LE::writeFloat($out, $this->duration); } } diff --git a/src/types/camera/CameraSetInstructionRotation.php b/src/types/camera/CameraSetInstructionRotation.php index 4dbe8cfd..8ed1cc3d 100644 --- a/src/types/camera/CameraSetInstructionRotation.php +++ b/src/types/camera/CameraSetInstructionRotation.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\camera; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; final class CameraSetInstructionRotation{ @@ -27,14 +29,14 @@ public function getPitch() : float{ return $this->pitch; } public function getYaw() : float{ return $this->yaw; } - public static function read(PacketSerializer $in) : self{ - $pitch = $in->getLFloat(); - $yaw = $in->getLFloat(); + public static function read(ByteBufferReader $in) : self{ + $pitch = LE::readFloat($in); + $yaw = LE::readFloat($in); return new self($pitch, $yaw); } - public function write(PacketSerializer $out) : void{ - $out->putLFloat($this->pitch); - $out->putLFloat($this->yaw); + public function write(ByteBufferWriter $out) : void{ + LE::writeFloat($out, $this->pitch); + LE::writeFloat($out, $this->yaw); } } diff --git a/src/types/camera/CameraTargetInstruction.php b/src/types/camera/CameraTargetInstruction.php index 7e5c03fe..d8d86730 100644 --- a/src/types/camera/CameraTargetInstruction.php +++ b/src/types/camera/CameraTargetInstruction.php @@ -14,8 +14,11 @@ namespace pocketmine\network\mcpe\protocol\types\camera; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class CameraTargetInstruction{ @@ -28,17 +31,17 @@ public function getTargetCenterOffset() : ?Vector3{ return $this->targetCenterOf public function getActorUniqueId() : int{ return $this->actorUniqueId; } - public static function read(PacketSerializer $in) : self{ - $targetCenterOffset = $in->readOptional(fn() => $in->getVector3()); - $actorUniqueId = $in->getLLong(); //why be consistent mojang ????? + public static function read(ByteBufferReader $in) : self{ + $targetCenterOffset = CommonTypes::readOptional($in, CommonTypes::getVector3(...)); + $actorUniqueId = LE::readSignedLong($in); //why be consistent mojang ????? return new self( $targetCenterOffset, $actorUniqueId ); } - public function write(PacketSerializer $out) : void{ - $out->writeOptional($this->targetCenterOffset, fn(Vector3 $v) => $out->putVector3($v)); - $out->putLLong($this->actorUniqueId); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::writeOptional($out, $this->targetCenterOffset, CommonTypes::putVector3(...)); + LE::writeSignedLong($out, $this->actorUniqueId); } } diff --git a/src/types/entity/AttributeModifier.php b/src/types/entity/AttributeModifier.php index 175601a2..382857fe 100644 --- a/src/types/entity/AttributeModifier.php +++ b/src/types/entity/AttributeModifier.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\entity; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class AttributeModifier{ @@ -43,23 +46,23 @@ public function getOperand() : int{ return $this->operand; } public function isSerializable() : bool{ return $this->serializable; } - public static function read(PacketSerializer $in) : self{ - $id = $in->getString(); - $name = $in->getString(); - $amount = $in->getLFloat(); - $operation = $in->getLInt(); - $operand = $in->getLInt(); - $serializable = $in->getBool(); + public static function read(ByteBufferReader $in) : self{ + $id = CommonTypes::getString($in); + $name = CommonTypes::getString($in); + $amount = LE::readFloat($in); + $operation = LE::readSignedInt($in); + $operand = LE::readSignedInt($in); + $serializable = CommonTypes::getBool($in); return new self($id, $name, $amount, $operation, $operand, $serializable); } - public function write(PacketSerializer $out) : void{ - $out->putString($this->id); - $out->putString($this->name); - $out->putLFloat($this->amount); - $out->putLInt($this->operation); - $out->putLInt($this->operand); - $out->putBool($this->serializable); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->id); + CommonTypes::putString($out, $this->name); + LE::writeFloat($out, $this->amount); + LE::writeSignedInt($out, $this->operation); + LE::writeSignedInt($out, $this->operand); + CommonTypes::putBool($out, $this->serializable); } } diff --git a/src/types/entity/BlockPosMetadataProperty.php b/src/types/entity/BlockPosMetadataProperty.php index 389876f9..9f6276ba 100644 --- a/src/types/entity/BlockPosMetadataProperty.php +++ b/src/types/entity/BlockPosMetadataProperty.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\entity; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; @@ -31,12 +33,12 @@ public function getValue() : BlockPosition{ return $this->value; } - public static function read(PacketSerializer $in) : self{ - return new self($in->getSignedBlockPosition()); + public static function read(ByteBufferReader $in) : self{ + return new self(CommonTypes::getSignedBlockPosition($in)); } - public function write(PacketSerializer $out) : void{ - $out->putSignedBlockPosition($this->value); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putSignedBlockPosition($out, $this->value); } public function equals(MetadataProperty $other) : bool{ diff --git a/src/types/entity/ByteMetadataProperty.php b/src/types/entity/ByteMetadataProperty.php index 191262b6..3b043282 100644 --- a/src/types/entity/ByteMetadataProperty.php +++ b/src/types/entity/ByteMetadataProperty.php @@ -14,9 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\entity; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; -use pocketmine\utils\Binary; final class ByteMetadataProperty implements MetadataProperty{ use GetTypeIdFromConstTrait; @@ -32,11 +33,11 @@ protected function max() : int{ return 0x7f; } - public static function read(PacketSerializer $in) : self{ - return new self(Binary::signByte($in->getByte())); + public static function read(ByteBufferReader $in) : self{ + return new self(Byte::readSigned($in)); } - public function write(PacketSerializer $out) : void{ - $out->putByte($this->value); + public function write(ByteBufferWriter $out) : void{ + Byte::writeSigned($out, $this->value); } } diff --git a/src/types/entity/CompoundTagMetadataProperty.php b/src/types/entity/CompoundTagMetadataProperty.php index 9639fe8a..0e9750b5 100644 --- a/src/types/entity/CompoundTagMetadataProperty.php +++ b/src/types/entity/CompoundTagMetadataProperty.php @@ -14,8 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\entity; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use pocketmine\network\mcpe\protocol\PacketDecodeException; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\CacheableNbt; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; @@ -48,11 +50,11 @@ public function equals(MetadataProperty $other) : bool{ /** * @throws PacketDecodeException */ - public static function read(PacketSerializer $in) : self{ - return new self(new CacheableNbt($in->getNbtCompoundRoot())); + public static function read(ByteBufferReader $in) : self{ + return new self(new CacheableNbt(CommonTypes::getNbtCompoundRoot($in))); } - public function write(PacketSerializer $out) : void{ - $out->put($this->value->getEncodedNbt()); + public function write(ByteBufferWriter $out) : void{ + $out->writeByteArray($this->value->getEncodedNbt()); } } diff --git a/src/types/entity/FloatMetadataProperty.php b/src/types/entity/FloatMetadataProperty.php index c6de664d..e83473e7 100644 --- a/src/types/entity/FloatMetadataProperty.php +++ b/src/types/entity/FloatMetadataProperty.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\entity; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; final class FloatMetadataProperty implements MetadataProperty{ @@ -34,11 +36,11 @@ public function equals(MetadataProperty $other) : bool{ return $other instanceof self and $other->value === $this->value; } - public static function read(PacketSerializer $in) : self{ - return new self($in->getLFloat()); + public static function read(ByteBufferReader $in) : self{ + return new self(LE::readFloat($in)); } - public function write(PacketSerializer $out) : void{ - $out->putLFloat($this->value); + public function write(ByteBufferWriter $out) : void{ + LE::writeFloat($out, $this->value); } } diff --git a/src/types/entity/IntMetadataProperty.php b/src/types/entity/IntMetadataProperty.php index 43f37165..8cbee75c 100644 --- a/src/types/entity/IntMetadataProperty.php +++ b/src/types/entity/IntMetadataProperty.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\entity; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; final class IntMetadataProperty implements MetadataProperty{ @@ -31,11 +33,11 @@ protected function max() : int{ return 0x7fffffff; } - public static function read(PacketSerializer $in) : self{ - return new self($in->getVarInt()); + public static function read(ByteBufferReader $in) : self{ + return new self(VarInt::readSignedInt($in)); } - public function write(PacketSerializer $out) : void{ - $out->putVarInt($this->value); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->value); } } diff --git a/src/types/entity/LongMetadataProperty.php b/src/types/entity/LongMetadataProperty.php index 120f54e3..97036056 100644 --- a/src/types/entity/LongMetadataProperty.php +++ b/src/types/entity/LongMetadataProperty.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\entity; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; use const PHP_INT_MAX; use const PHP_INT_MIN; @@ -33,11 +35,11 @@ protected function max() : int{ return PHP_INT_MAX; } - public static function read(PacketSerializer $in) : self{ - return new self($in->getVarLong()); + public static function read(ByteBufferReader $in) : self{ + return new self(VarInt::readSignedLong($in)); } - public function write(PacketSerializer $out) : void{ - $out->putVarLong($this->value); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeSignedLong($out, $this->value); } } diff --git a/src/types/entity/MetadataProperty.php b/src/types/entity/MetadataProperty.php index 6fb1ee6d..54bc8a88 100644 --- a/src/types/entity/MetadataProperty.php +++ b/src/types/entity/MetadataProperty.php @@ -14,13 +14,13 @@ namespace pocketmine\network\mcpe\protocol\types\entity; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferWriter; interface MetadataProperty{ public function getTypeId() : int; - public function write(PacketSerializer $out) : void; + public function write(ByteBufferWriter $out) : void; public function equals(MetadataProperty $other) : bool; } diff --git a/src/types/entity/PropertySyncData.php b/src/types/entity/PropertySyncData.php index fea5ef47..71b120ec 100644 --- a/src/types/entity/PropertySyncData.php +++ b/src/types/entity/PropertySyncData.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\entity; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; use function count; final class PropertySyncData{ @@ -45,30 +48,30 @@ public function getFloatProperties() : array{ return $this->floatProperties; } - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ $intProperties = []; $floatProperties = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ - $intProperties[$in->getUnsignedVarInt()] = $in->getVarInt(); + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ + $intProperties[VarInt::readUnsignedInt($in)] = VarInt::readSignedInt($in); } - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){ - $floatProperties[$in->getUnsignedVarInt()] = $in->getLFloat(); + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ + $floatProperties[VarInt::readUnsignedInt($in)] = LE::readFloat($in); } return new self($intProperties, $floatProperties); } - public function write(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->intProperties)); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->intProperties)); foreach($this->intProperties as $key => $value){ - $out->putUnsignedVarInt($key); - $out->putVarInt($value); + VarInt::writeUnsignedInt($out, $key); + VarInt::writeSignedInt($out, $value); } - $out->putUnsignedVarInt(count($this->floatProperties)); + VarInt::writeUnsignedInt($out, count($this->floatProperties)); foreach($this->floatProperties as $key => $value){ - $out->putUnsignedVarInt($key); - $out->putLFloat($value); + VarInt::writeUnsignedInt($out, $key); + LE::writeFloat($out, $value); } } } diff --git a/src/types/entity/ShortMetadataProperty.php b/src/types/entity/ShortMetadataProperty.php index 14622efb..fa5561d9 100644 --- a/src/types/entity/ShortMetadataProperty.php +++ b/src/types/entity/ShortMetadataProperty.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\entity; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; final class ShortMetadataProperty implements MetadataProperty{ @@ -31,11 +33,11 @@ protected function max() : int{ return 0x7fff; } - public static function read(PacketSerializer $in) : self{ - return new self($in->getSignedLShort()); + public static function read(ByteBufferReader $in) : self{ + return new self(LE::readSignedShort($in)); } - public function write(PacketSerializer $out) : void{ - $out->putLShort($this->value); + public function write(ByteBufferWriter $out) : void{ + LE::writeSignedShort($out, $this->value); } } diff --git a/src/types/entity/StringMetadataProperty.php b/src/types/entity/StringMetadataProperty.php index 4fd201b4..7ed1351a 100644 --- a/src/types/entity/StringMetadataProperty.php +++ b/src/types/entity/StringMetadataProperty.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\entity; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; final class StringMetadataProperty implements MetadataProperty{ @@ -28,12 +30,12 @@ public function __construct( public function getValue() : string{ return $this->value; } - public static function read(PacketSerializer $in) : self{ - return new self($in->getString()); + public static function read(ByteBufferReader $in) : self{ + return new self(CommonTypes::getString($in)); } - public function write(PacketSerializer $out) : void{ - $out->putString($this->value); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->value); } public function equals(MetadataProperty $other) : bool{ diff --git a/src/types/entity/UpdateAttribute.php b/src/types/entity/UpdateAttribute.php index 15425433..0c21972b 100644 --- a/src/types/entity/UpdateAttribute.php +++ b/src/types/entity/UpdateAttribute.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol\types\entity; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use function count; final class UpdateAttribute{ @@ -51,33 +55,33 @@ public function getDefault() : float{ return $this->default; } */ public function getModifiers() : array{ return $this->modifiers; } - public static function read(PacketSerializer $in) : self{ - $min = $in->getLFloat(); - $max = $in->getLFloat(); - $current = $in->getLFloat(); - $defaultMin = $in->getLFloat(); - $defaultMax = $in->getLFloat(); - $default = $in->getLFloat(); - $id = $in->getString(); + public static function read(ByteBufferReader $in) : self{ + $min = LE::readFloat($in); + $max = LE::readFloat($in); + $current = LE::readFloat($in); + $defaultMin = LE::readFloat($in); + $defaultMax = LE::readFloat($in); + $default = LE::readFloat($in); + $id = CommonTypes::getString($in); $modifiers = []; - for($j = 0, $modifierCount = $in->getUnsignedVarInt(); $j < $modifierCount; $j++){ + for($j = 0, $modifierCount = VarInt::readUnsignedInt($in); $j < $modifierCount; $j++){ $modifiers[] = AttributeModifier::read($in); } return new self($id, $min, $max, $current, $defaultMin, $defaultMax, $default, $modifiers); } - public function write(PacketSerializer $out) : void{ - $out->putLFloat($this->min); - $out->putLFloat($this->max); - $out->putLFloat($this->current); - $out->putLFloat($this->defaultMin); - $out->putLFloat($this->defaultMax); - $out->putLFloat($this->default); - $out->putString($this->id); + public function write(ByteBufferWriter $out) : void{ + LE::writeFloat($out, $this->min); + LE::writeFloat($out, $this->max); + LE::writeFloat($out, $this->current); + LE::writeFloat($out, $this->defaultMin); + LE::writeFloat($out, $this->defaultMax); + LE::writeFloat($out, $this->default); + CommonTypes::putString($out, $this->id); - $out->putUnsignedVarInt(count($this->modifiers)); + VarInt::writeUnsignedInt($out, count($this->modifiers)); foreach($this->modifiers as $modifier){ $modifier->write($out); } diff --git a/src/types/entity/Vec3MetadataProperty.php b/src/types/entity/Vec3MetadataProperty.php index e0510db5..d96e7c05 100644 --- a/src/types/entity/Vec3MetadataProperty.php +++ b/src/types/entity/Vec3MetadataProperty.php @@ -14,8 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\entity; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; class Vec3MetadataProperty implements MetadataProperty{ @@ -33,12 +35,12 @@ public function getValue() : Vector3{ return clone $this->value; } - public static function read(PacketSerializer $in) : self{ - return new self($in->getVector3()); + public static function read(ByteBufferReader $in) : self{ + return new self(CommonTypes::getVector3($in)); } - public function write(PacketSerializer $out) : void{ - $out->putVector3($this->value); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putVector3($out, $this->value); } public function equals(MetadataProperty $other) : bool{ diff --git a/src/types/inventory/CreativeGroupEntry.php b/src/types/inventory/CreativeGroupEntry.php index b8bd8500..54e61bcc 100644 --- a/src/types/inventory/CreativeGroupEntry.php +++ b/src/types/inventory/CreativeGroupEntry.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\inventory; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class CreativeGroupEntry{ public function __construct( @@ -29,16 +32,16 @@ public function getCategoryName() : string{ return $this->categoryName; } public function getIcon() : ItemStack{ return $this->icon; } - public static function read(PacketSerializer $in) : self{ - $categoryId = $in->getLInt(); - $categoryName = $in->getString(); - $icon = $in->getItemStackWithoutStackId(); + public static function read(ByteBufferReader $in) : self{ + $categoryId = LE::readSignedInt($in); + $categoryName = CommonTypes::getString($in); + $icon = CommonTypes::getItemStackWithoutStackId($in); return new self($categoryId, $categoryName, $icon); } - public function write(PacketSerializer $out) : void{ - $out->putLInt($this->categoryId); - $out->putString($this->categoryName); - $out->putItemStackWithoutStackId($this->icon); + public function write(ByteBufferWriter $out) : void{ + LE::writeSignedInt($out, $this->categoryId); + CommonTypes::putString($out, $this->categoryName); + CommonTypes::putItemStackWithoutStackId($out, $this->icon); } } diff --git a/src/types/inventory/CreativeItemEntry.php b/src/types/inventory/CreativeItemEntry.php index 750be198..68629999 100644 --- a/src/types/inventory/CreativeItemEntry.php +++ b/src/types/inventory/CreativeItemEntry.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\inventory; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class CreativeItemEntry{ public function __construct( @@ -29,16 +32,16 @@ public function getItem() : ItemStack{ return $this->item; } public function getGroupId() : int{ return $this->groupId; } - public static function read(PacketSerializer $in) : self{ - $entryId = $in->readCreativeItemNetId(); - $item = $in->getItemStackWithoutStackId(); - $groupId = $in->getUnsignedVarInt(); + public static function read(ByteBufferReader $in) : self{ + $entryId = CommonTypes::readCreativeItemNetId($in); + $item = CommonTypes::getItemStackWithoutStackId($in); + $groupId = VarInt::readUnsignedInt($in); return new self($entryId, $item, $groupId); } - public function write(PacketSerializer $out) : void{ - $out->writeCreativeItemNetId($this->entryId); - $out->putItemStackWithoutStackId($this->item); - $out->putUnsignedVarInt($this->groupId); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::writeCreativeItemNetId($out, $this->entryId); + CommonTypes::putItemStackWithoutStackId($out, $this->item); + VarInt::writeUnsignedInt($out, $this->groupId); } } diff --git a/src/types/inventory/FullContainerName.php b/src/types/inventory/FullContainerName.php index 04227d0d..bfb063d7 100644 --- a/src/types/inventory/FullContainerName.php +++ b/src/types/inventory/FullContainerName.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol\types\inventory; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class FullContainerName{ public function __construct( @@ -26,14 +30,14 @@ public function getContainerId() : int{ return $this->containerId; } public function getDynamicId() : ?int{ return $this->dynamicId; } - public static function read(PacketSerializer $in) : self{ - $containerId = $in->getByte(); - $dynamicId = $in->readOptional($in->getLInt(...)); + public static function read(ByteBufferReader $in) : self{ + $containerId = Byte::readUnsigned($in); + $dynamicId = CommonTypes::readOptional($in, LE::readUnsignedInt(...)); return new self($containerId, $dynamicId); } - public function write(PacketSerializer $out) : void{ - $out->putByte($this->containerId); - $out->writeOptional($this->dynamicId, $out->putLInt(...)); + public function write(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->containerId); + CommonTypes::writeOptional($out, $this->dynamicId, LE::writeUnsignedInt(...)); } } diff --git a/src/types/inventory/InventoryTransactionChangedSlotsHack.php b/src/types/inventory/InventoryTransactionChangedSlotsHack.php index 329d7415..24026f3e 100644 --- a/src/types/inventory/InventoryTransactionChangedSlotsHack.php +++ b/src/types/inventory/InventoryTransactionChangedSlotsHack.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\inventory; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use function count; final class InventoryTransactionChangedSlotsHack{ @@ -31,20 +34,20 @@ public function getContainerId() : int{ return $this->containerId; } /** @return int[] */ public function getChangedSlotIndexes() : array{ return $this->changedSlotIndexes; } - public static function read(PacketSerializer $in) : self{ - $containerId = $in->getByte(); + public static function read(ByteBufferReader $in) : self{ + $containerId = Byte::readUnsigned($in); $changedSlots = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ - $changedSlots[] = $in->getByte(); + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ + $changedSlots[] = Byte::readUnsigned($in); } return new self($containerId, $changedSlots); } - public function write(PacketSerializer $out) : void{ - $out->putByte($this->containerId); - $out->putUnsignedVarInt(count($this->changedSlotIndexes)); + public function write(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->containerId); + VarInt::writeUnsignedInt($out, count($this->changedSlotIndexes)); foreach($this->changedSlotIndexes as $index){ - $out->putByte($index); + Byte::writeUnsigned($out, $index); } } } diff --git a/src/types/inventory/ItemStackExtraData.php b/src/types/inventory/ItemStackExtraData.php index 6def31b5..62769487 100644 --- a/src/types/inventory/ItemStackExtraData.php +++ b/src/types/inventory/ItemStackExtraData.php @@ -14,12 +14,15 @@ namespace pocketmine\network\mcpe\protocol\types\inventory; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; use pocketmine\nbt\LittleEndianNbtSerializer; use pocketmine\nbt\NbtDataException; use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\TreeRoot; use pocketmine\network\mcpe\protocol\PacketDecodeException; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; use function count; use function strlen; @@ -57,19 +60,19 @@ public function getNbt() : ?CompoundTag{ return $this->nbt; } - public static function read(PacketSerializer $in) : self{ - $nbtLen = $in->getLShort(); + public static function read(ByteBufferReader $in) : self{ + $nbtLen = LE::readSignedShort($in); /** @var CompoundTag|null $compound */ $compound = null; - if($nbtLen === 0xffff){ - $nbtDataVersion = $in->getByte(); + if($nbtLen === -1){ + $nbtDataVersion = Byte::readUnsigned($in); if($nbtDataVersion !== 1){ throw new PacketDecodeException("Unexpected NBT data version $nbtDataVersion"); } $offset = $in->getOffset(); try{ - $compound = (new LittleEndianNbtSerializer())->read($in->getBuffer(), $offset, 512)->mustGetCompoundTag(); + $compound = (new LittleEndianNbtSerializer())->read($in->getData(), $offset, 512)->mustGetCompoundTag(); }catch(NbtDataException $e){ throw PacketDecodeException::wrap($e, "Failed decoding NBT root"); }finally{ @@ -80,36 +83,37 @@ public static function read(PacketSerializer $in) : self{ } $canPlaceOn = []; - for($i = 0, $canPlaceOnCount = $in->getLInt(); $i < $canPlaceOnCount; ++$i){ - $canPlaceOn[] = $in->get($in->getLShort()); + //TODO: apparently this is not correct as of 1.21.50 + for($i = 0, $canPlaceOnCount = LE::readUnsignedInt($in); $i < $canPlaceOnCount; ++$i){ + $canPlaceOn[] = $in->readByteArray(LE::readUnsignedShort($in)); } $canDestroy = []; - for($i = 0, $canDestroyCount = $in->getLInt(); $i < $canDestroyCount; ++$i){ - $canDestroy[] = $in->get($in->getLShort()); + for($i = 0, $canDestroyCount = LE::readUnsignedInt($in); $i < $canDestroyCount; ++$i){ + $canDestroy[] = $in->readByteArray(LE::readUnsignedShort($in)); } return new self($compound, $canPlaceOn, $canDestroy); } - public function write(PacketSerializer $out) : void{ + public function write(ByteBufferWriter $out) : void{ if($this->nbt !== null){ - $out->putLShort(0xffff); - $out->putByte(1); //TODO: NBT data version (?) - $out->put((new LittleEndianNbtSerializer())->write(new TreeRoot($this->nbt))); + LE::writeSignedShort($out, 0xffff); + Byte::writeUnsigned($out, 1); //TODO: NBT data version (?) + $out->writeByteArray((new LittleEndianNbtSerializer())->write(new TreeRoot($this->nbt))); }else{ - $out->putLShort(0); + LE::writeSignedShort($out, 0); } - $out->putLInt(count($this->canPlaceOn)); + LE::writeUnsignedInt($out, count($this->canPlaceOn)); foreach($this->canPlaceOn as $entry){ - $out->putLShort(strlen($entry)); - $out->put($entry); + LE::writeUnsignedShort($out, strlen($entry)); + $out->writeByteArray($entry); } - $out->putLInt(count($this->canDestroy)); + LE::writeUnsignedInt($out, count($this->canDestroy)); foreach($this->canDestroy as $entry){ - $out->putLShort(strlen($entry)); - $out->put($entry); + LE::writeUnsignedShort($out, strlen($entry)); + $out->writeByteArray($entry); } } } diff --git a/src/types/inventory/ItemStackExtraDataShield.php b/src/types/inventory/ItemStackExtraDataShield.php index 3c7519cd..0ee1059e 100644 --- a/src/types/inventory/ItemStackExtraDataShield.php +++ b/src/types/inventory/ItemStackExtraDataShield.php @@ -14,8 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\inventory; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; use pocketmine\nbt\tag\CompoundTag; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; /** * Extension of ItemStackExtraData for shield items, which have an additional field for the blocking tick. @@ -37,15 +39,16 @@ public function __construct( public function getBlockingTick() : int{ return $this->blockingTick; } - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ $base = parent::read($in); - $blockingTick = $in->getLLong(); + //TODO: I don't know for sure if this is supposed to be signed or unsigned + $blockingTick = LE::readSignedLong($in); return new self($base->getNbt(), $base->getCanPlaceOn(), $base->getCanDestroy(), $blockingTick); } - public function write(PacketSerializer $out) : void{ + public function write(ByteBufferWriter $out) : void{ parent::write($out); - $out->putLLong($this->blockingTick); + LE::writeSignedLong($out, $this->blockingTick); } } diff --git a/src/types/inventory/MismatchTransactionData.php b/src/types/inventory/MismatchTransactionData.php index efe6bd24..1b98ccfd 100644 --- a/src/types/inventory/MismatchTransactionData.php +++ b/src/types/inventory/MismatchTransactionData.php @@ -14,9 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\inventory; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use pocketmine\network\mcpe\protocol\InventoryTransactionPacket; use pocketmine\network\mcpe\protocol\PacketDecodeException; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; use function count; @@ -25,13 +26,13 @@ class MismatchTransactionData extends TransactionData{ public const ID = InventoryTransactionPacket::TYPE_MISMATCH; - protected function decodeData(PacketSerializer $stream) : void{ + protected function decodeData(ByteBufferReader $in) : void{ if(count($this->actions) > 0){ throw new PacketDecodeException("Mismatch transaction type should not have any actions associated with it, but got " . count($this->actions)); } } - protected function encodeData(PacketSerializer $stream) : void{ + protected function encodeData(ByteBufferWriter $out) : void{ } diff --git a/src/types/inventory/NetworkInventoryAction.php b/src/types/inventory/NetworkInventoryAction.php index b014da27..ed5b7055 100644 --- a/src/types/inventory/NetworkInventoryAction.php +++ b/src/types/inventory/NetworkInventoryAction.php @@ -14,9 +14,12 @@ namespace pocketmine\network\mcpe\protocol\types\inventory; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\DataDecodeException; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\PacketDecodeException; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; -use pocketmine\utils\BinaryDataException; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class NetworkInventoryAction{ public const SOURCE_CONTAINER = 0; @@ -65,31 +68,31 @@ class NetworkInventoryAction{ /** * @return $this * - * @throws BinaryDataException + * @throws DataDecodeException * @throws PacketDecodeException */ - public function read(PacketSerializer $packet) : NetworkInventoryAction{ - $this->sourceType = $packet->getUnsignedVarInt(); + public function read(ByteBufferReader $in) : NetworkInventoryAction{ + $this->sourceType = VarInt::readUnsignedInt($in); switch($this->sourceType){ case self::SOURCE_CONTAINER: - $this->windowId = $packet->getVarInt(); + $this->windowId = VarInt::readSignedInt($in); break; case self::SOURCE_WORLD: - $this->sourceFlags = $packet->getUnsignedVarInt(); + $this->sourceFlags = VarInt::readUnsignedInt($in); break; case self::SOURCE_CREATIVE: break; case self::SOURCE_TODO: - $this->windowId = $packet->getVarInt(); + $this->windowId = VarInt::readSignedInt($in); break; default: throw new PacketDecodeException("Unknown inventory action source type $this->sourceType"); } - $this->inventorySlot = $packet->getUnsignedVarInt(); - $this->oldItem = $packet->getItemStackWrapper(); - $this->newItem = $packet->getItemStackWrapper(); + $this->inventorySlot = VarInt::readUnsignedInt($in); + $this->oldItem = CommonTypes::getItemStackWrapper($in); + $this->newItem = CommonTypes::getItemStackWrapper($in); return $this; } @@ -97,27 +100,27 @@ public function read(PacketSerializer $packet) : NetworkInventoryAction{ /** * @throws \InvalidArgumentException */ - public function write(PacketSerializer $packet) : void{ - $packet->putUnsignedVarInt($this->sourceType); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, $this->sourceType); switch($this->sourceType){ case self::SOURCE_CONTAINER: - $packet->putVarInt($this->windowId); + VarInt::writeSignedInt($out, $this->windowId); break; case self::SOURCE_WORLD: - $packet->putUnsignedVarInt($this->sourceFlags); + VarInt::writeUnsignedInt($out, $this->sourceFlags); break; case self::SOURCE_CREATIVE: break; case self::SOURCE_TODO: - $packet->putVarInt($this->windowId); + VarInt::writeSignedInt($out, $this->windowId); break; default: throw new \InvalidArgumentException("Unknown inventory action source type $this->sourceType"); } - $packet->putUnsignedVarInt($this->inventorySlot); - $packet->putItemStackWrapper($this->oldItem); - $packet->putItemStackWrapper($this->newItem); + VarInt::writeUnsignedInt($out, $this->inventorySlot); + CommonTypes::putItemStackWrapper($out, $this->oldItem); + CommonTypes::putItemStackWrapper($out, $this->newItem); } } diff --git a/src/types/inventory/NormalTransactionData.php b/src/types/inventory/NormalTransactionData.php index bc015b8f..ebd3cff3 100644 --- a/src/types/inventory/NormalTransactionData.php +++ b/src/types/inventory/NormalTransactionData.php @@ -14,8 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\inventory; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use pocketmine\network\mcpe\protocol\InventoryTransactionPacket; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; class NormalTransactionData extends TransactionData{ @@ -23,11 +24,11 @@ class NormalTransactionData extends TransactionData{ public const ID = InventoryTransactionPacket::TYPE_NORMAL; - protected function decodeData(PacketSerializer $stream) : void{ + protected function decodeData(ByteBufferReader $in) : void{ } - protected function encodeData(PacketSerializer $stream) : void{ + protected function encodeData(ByteBufferWriter $out) : void{ } diff --git a/src/types/inventory/ReleaseItemTransactionData.php b/src/types/inventory/ReleaseItemTransactionData.php index 79a39364..b9afb03f 100644 --- a/src/types/inventory/ReleaseItemTransactionData.php +++ b/src/types/inventory/ReleaseItemTransactionData.php @@ -14,9 +14,12 @@ namespace pocketmine\network\mcpe\protocol\types\inventory; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\math\Vector3; use pocketmine\network\mcpe\protocol\InventoryTransactionPacket; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; class ReleaseItemTransactionData extends TransactionData{ @@ -48,18 +51,18 @@ public function getHeadPosition() : Vector3{ return $this->headPosition; } - protected function decodeData(PacketSerializer $stream) : void{ - $this->actionType = $stream->getUnsignedVarInt(); - $this->hotbarSlot = $stream->getVarInt(); - $this->itemInHand = $stream->getItemStackWrapper(); - $this->headPosition = $stream->getVector3(); + protected function decodeData(ByteBufferReader $in) : void{ + $this->actionType = VarInt::readUnsignedInt($in); + $this->hotbarSlot = VarInt::readSignedInt($in); + $this->itemInHand = CommonTypes::getItemStackWrapper($in); + $this->headPosition = CommonTypes::getVector3($in); } - protected function encodeData(PacketSerializer $stream) : void{ - $stream->putUnsignedVarInt($this->actionType); - $stream->putVarInt($this->hotbarSlot); - $stream->putItemStackWrapper($this->itemInHand); - $stream->putVector3($this->headPosition); + protected function encodeData(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, $this->actionType); + VarInt::writeSignedInt($out, $this->hotbarSlot); + CommonTypes::putItemStackWrapper($out, $this->itemInHand); + CommonTypes::putVector3($out, $this->headPosition); } /** diff --git a/src/types/inventory/TransactionData.php b/src/types/inventory/TransactionData.php index 20cda371..209e93a2 100644 --- a/src/types/inventory/TransactionData.php +++ b/src/types/inventory/TransactionData.php @@ -14,9 +14,11 @@ namespace pocketmine\network\mcpe\protocol\types\inventory; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\DataDecodeException; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\PacketDecodeException; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; -use pocketmine\utils\BinaryDataException; use function count; abstract class TransactionData{ @@ -33,30 +35,30 @@ final public function getActions() : array{ abstract public function getTypeId() : int; /** - * @throws BinaryDataException + * @throws DataDecodeException * @throws PacketDecodeException */ - final public function decode(PacketSerializer $stream) : void{ - $actionCount = $stream->getUnsignedVarInt(); + final public function decode(ByteBufferReader $in) : void{ + $actionCount = VarInt::readUnsignedInt($in); for($i = 0; $i < $actionCount; ++$i){ - $this->actions[] = (new NetworkInventoryAction())->read($stream); + $this->actions[] = (new NetworkInventoryAction())->read($in); } - $this->decodeData($stream); + $this->decodeData($in); } /** - * @throws BinaryDataException + * @throws DataDecodeException * @throws PacketDecodeException */ - abstract protected function decodeData(PacketSerializer $stream) : void; + abstract protected function decodeData(ByteBufferReader $in) : void; - final public function encode(PacketSerializer $stream) : void{ - $stream->putUnsignedVarInt(count($this->actions)); + final public function encode(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->actions)); foreach($this->actions as $action){ - $action->write($stream); + $action->write($out); } - $this->encodeData($stream); + $this->encodeData($out); } - abstract protected function encodeData(PacketSerializer $stream) : void; + abstract protected function encodeData(ByteBufferWriter $out) : void; } diff --git a/src/types/inventory/UseItemOnEntityTransactionData.php b/src/types/inventory/UseItemOnEntityTransactionData.php index 4ffb49f4..b62bea80 100644 --- a/src/types/inventory/UseItemOnEntityTransactionData.php +++ b/src/types/inventory/UseItemOnEntityTransactionData.php @@ -14,9 +14,12 @@ namespace pocketmine\network\mcpe\protocol\types\inventory; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\math\Vector3; use pocketmine\network\mcpe\protocol\InventoryTransactionPacket; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; class UseItemOnEntityTransactionData extends TransactionData{ @@ -59,22 +62,22 @@ public function getClickPosition() : Vector3{ return $this->clickPosition; } - protected function decodeData(PacketSerializer $stream) : void{ - $this->actorRuntimeId = $stream->getActorRuntimeId(); - $this->actionType = $stream->getUnsignedVarInt(); - $this->hotbarSlot = $stream->getVarInt(); - $this->itemInHand = $stream->getItemStackWrapper(); - $this->playerPosition = $stream->getVector3(); - $this->clickPosition = $stream->getVector3(); + protected function decodeData(ByteBufferReader $in) : void{ + $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in); + $this->actionType = VarInt::readUnsignedInt($in); + $this->hotbarSlot = VarInt::readSignedInt($in); + $this->itemInHand = CommonTypes::getItemStackWrapper($in); + $this->playerPosition = CommonTypes::getVector3($in); + $this->clickPosition = CommonTypes::getVector3($in); } - protected function encodeData(PacketSerializer $stream) : void{ - $stream->putActorRuntimeId($this->actorRuntimeId); - $stream->putUnsignedVarInt($this->actionType); - $stream->putVarInt($this->hotbarSlot); - $stream->putItemStackWrapper($this->itemInHand); - $stream->putVector3($this->playerPosition); - $stream->putVector3($this->clickPosition); + protected function encodeData(ByteBufferWriter $out) : void{ + CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId); + VarInt::writeUnsignedInt($out, $this->actionType); + VarInt::writeSignedInt($out, $this->hotbarSlot); + CommonTypes::putItemStackWrapper($out, $this->itemInHand); + CommonTypes::putVector3($out, $this->playerPosition); + CommonTypes::putVector3($out, $this->clickPosition); } /** diff --git a/src/types/inventory/UseItemTransactionData.php b/src/types/inventory/UseItemTransactionData.php index 1fd97646..ab2ee95a 100644 --- a/src/types/inventory/UseItemTransactionData.php +++ b/src/types/inventory/UseItemTransactionData.php @@ -14,9 +14,12 @@ namespace pocketmine\network\mcpe\protocol\types\inventory; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\math\Vector3; use pocketmine\network\mcpe\protocol\InventoryTransactionPacket; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\BlockPosition; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; @@ -76,30 +79,30 @@ public function getBlockRuntimeId() : int{ public function getClientInteractPrediction() : PredictedResult{ return $this->clientInteractPrediction; } - protected function decodeData(PacketSerializer $stream) : void{ - $this->actionType = $stream->getUnsignedVarInt(); - $this->triggerType = TriggerType::fromPacket($stream->getUnsignedVarInt()); - $this->blockPosition = $stream->getBlockPosition(); - $this->face = $stream->getVarInt(); - $this->hotbarSlot = $stream->getVarInt(); - $this->itemInHand = $stream->getItemStackWrapper(); - $this->playerPosition = $stream->getVector3(); - $this->clickPosition = $stream->getVector3(); - $this->blockRuntimeId = $stream->getUnsignedVarInt(); - $this->clientInteractPrediction = PredictedResult::fromPacket($stream->getUnsignedVarInt()); + protected function decodeData(ByteBufferReader $in) : void{ + $this->actionType = VarInt::readUnsignedInt($in); + $this->triggerType = TriggerType::fromPacket(VarInt::readUnsignedInt($in)); + $this->blockPosition = CommonTypes::getBlockPosition($in); + $this->face = VarInt::readSignedInt($in); + $this->hotbarSlot = VarInt::readSignedInt($in); + $this->itemInHand = CommonTypes::getItemStackWrapper($in); + $this->playerPosition = CommonTypes::getVector3($in); + $this->clickPosition = CommonTypes::getVector3($in); + $this->blockRuntimeId = VarInt::readUnsignedInt($in); + $this->clientInteractPrediction = PredictedResult::fromPacket(VarInt::readUnsignedInt($in)); } - protected function encodeData(PacketSerializer $stream) : void{ - $stream->putUnsignedVarInt($this->actionType); - $stream->putUnsignedVarInt($this->triggerType->value); - $stream->putBlockPosition($this->blockPosition); - $stream->putVarInt($this->face); - $stream->putVarInt($this->hotbarSlot); - $stream->putItemStackWrapper($this->itemInHand); - $stream->putVector3($this->playerPosition); - $stream->putVector3($this->clickPosition); - $stream->putUnsignedVarInt($this->blockRuntimeId); - $stream->putUnsignedVarInt($this->clientInteractPrediction->value); + protected function encodeData(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, $this->actionType); + VarInt::writeUnsignedInt($out, $this->triggerType->value); + CommonTypes::putBlockPosition($out, $this->blockPosition); + VarInt::writeSignedInt($out, $this->face); + VarInt::writeSignedInt($out, $this->hotbarSlot); + CommonTypes::putItemStackWrapper($out, $this->itemInHand); + CommonTypes::putVector3($out, $this->playerPosition); + CommonTypes::putVector3($out, $this->clickPosition); + VarInt::writeUnsignedInt($out, $this->blockRuntimeId); + VarInt::writeUnsignedInt($out, $this->clientInteractPrediction->value); } /** diff --git a/src/types/inventory/stackrequest/BeaconPaymentStackRequestAction.php b/src/types/inventory/stackrequest/BeaconPaymentStackRequestAction.php index 5fc5e6f1..38a48137 100644 --- a/src/types/inventory/stackrequest/BeaconPaymentStackRequestAction.php +++ b/src/types/inventory/stackrequest/BeaconPaymentStackRequestAction.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; /** @@ -34,14 +36,14 @@ public function getPrimaryEffectId() : int{ return $this->primaryEffectId; } public function getSecondaryEffectId() : int{ return $this->secondaryEffectId; } - public static function read(PacketSerializer $in) : self{ - $primary = $in->getVarInt(); - $secondary = $in->getVarInt(); + public static function read(ByteBufferReader $in) : self{ + $primary = VarInt::readSignedInt($in); + $secondary = VarInt::readSignedInt($in); return new self($primary, $secondary); } - public function write(PacketSerializer $out) : void{ - $out->putVarInt($this->primaryEffectId); - $out->putVarInt($this->secondaryEffectId); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->primaryEffectId); + VarInt::writeSignedInt($out, $this->secondaryEffectId); } } diff --git a/src/types/inventory/stackrequest/CraftRecipeAutoStackRequestAction.php b/src/types/inventory/stackrequest/CraftRecipeAutoStackRequestAction.php index 228425dd..e5e75ce8 100644 --- a/src/types/inventory/stackrequest/CraftRecipeAutoStackRequestAction.php +++ b/src/types/inventory/stackrequest/CraftRecipeAutoStackRequestAction.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; use pocketmine\network\mcpe\protocol\types\recipe\RecipeIngredient; use function count; @@ -51,24 +54,24 @@ public function getRepetitions2() : int{ return $this->repetitions2; } */ public function getIngredients() : array{ return $this->ingredients; } - public static function read(PacketSerializer $in) : self{ - $recipeId = $in->readRecipeNetId(); - $repetitions = $in->getByte(); - $repetitions2 = $in->getByte(); //repetitions property is sent twice, mojang... + public static function read(ByteBufferReader $in) : self{ + $recipeId = CommonTypes::readRecipeNetId($in); + $repetitions = Byte::readUnsigned($in); + $repetitions2 = Byte::readUnsigned($in); //repetitions property is sent twice, mojang... $ingredients = []; - for($i = 0, $count = $in->getByte(); $i < $count; ++$i){ - $ingredients[] = $in->getRecipeIngredient(); + for($i = 0, $count = Byte::readUnsigned($in); $i < $count; ++$i){ + $ingredients[] = CommonTypes::getRecipeIngredient($in); } return new self($recipeId, $repetitions, $repetitions2, $ingredients); } - public function write(PacketSerializer $out) : void{ - $out->writeRecipeNetId($this->recipeId); - $out->putByte($this->repetitions); - $out->putByte($this->repetitions2); - $out->putByte(count($this->ingredients)); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::writeRecipeNetId($out, $this->recipeId); + Byte::writeUnsigned($out, $this->repetitions); + Byte::writeUnsigned($out, $this->repetitions2); + Byte::writeUnsigned($out, count($this->ingredients)); foreach($this->ingredients as $ingredient){ - $out->putRecipeIngredient($ingredient); + CommonTypes::putRecipeIngredient($out, $ingredient); } } } diff --git a/src/types/inventory/stackrequest/CraftRecipeOptionalStackRequestAction.php b/src/types/inventory/stackrequest/CraftRecipeOptionalStackRequestAction.php index c67eb391..a377048b 100644 --- a/src/types/inventory/stackrequest/CraftRecipeOptionalStackRequestAction.php +++ b/src/types/inventory/stackrequest/CraftRecipeOptionalStackRequestAction.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; /** @@ -38,14 +41,14 @@ public function getRecipeId() : int{ return $this->recipeId; } public function getFilterStringIndex() : int{ return $this->filterStringIndex; } - public static function read(PacketSerializer $in) : self{ - $recipeId = $in->readRecipeNetId(); - $filterStringIndex = $in->getLInt(); + public static function read(ByteBufferReader $in) : self{ + $recipeId = CommonTypes::readRecipeNetId($in); + $filterStringIndex = LE::readSignedInt($in); return new self($recipeId, $filterStringIndex); } - public function write(PacketSerializer $out) : void{ - $out->writeRecipeNetId($this->recipeId); - $out->putLInt($this->filterStringIndex); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::writeRecipeNetId($out, $this->recipeId); + LE::writeSignedInt($out, $this->filterStringIndex); } } diff --git a/src/types/inventory/stackrequest/CraftRecipeStackRequestAction.php b/src/types/inventory/stackrequest/CraftRecipeStackRequestAction.php index 87ebd06c..c06520b6 100644 --- a/src/types/inventory/stackrequest/CraftRecipeStackRequestAction.php +++ b/src/types/inventory/stackrequest/CraftRecipeStackRequestAction.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; /** @@ -34,14 +37,14 @@ public function getRecipeId() : int{ return $this->recipeId; } public function getRepetitions() : int{ return $this->repetitions; } - public static function read(PacketSerializer $in) : self{ - $recipeId = $in->readRecipeNetId(); - $repetitions = $in->getByte(); + public static function read(ByteBufferReader $in) : self{ + $recipeId = CommonTypes::readRecipeNetId($in); + $repetitions = Byte::readUnsigned($in); return new self($recipeId, $repetitions); } - public function write(PacketSerializer $out) : void{ - $out->writeRecipeNetId($this->recipeId); - $out->putByte($this->repetitions); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::writeRecipeNetId($out, $this->recipeId); + Byte::writeUnsigned($out, $this->repetitions); } } diff --git a/src/types/inventory/stackrequest/CraftingCreateSpecificResultStackRequestAction.php b/src/types/inventory/stackrequest/CraftingCreateSpecificResultStackRequestAction.php index 860d09c2..dbed3951 100644 --- a/src/types/inventory/stackrequest/CraftingCreateSpecificResultStackRequestAction.php +++ b/src/types/inventory/stackrequest/CraftingCreateSpecificResultStackRequestAction.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; use pocketmine\network\mcpe\protocol\types\inventory\ContainerUIIds; use pocketmine\network\mcpe\protocol\types\inventory\UIInventorySlotOffset; @@ -39,12 +41,12 @@ public function __construct( public function getResultIndex() : int{ return $this->resultIndex; } - public static function read(PacketSerializer $in) : self{ - $slot = $in->getByte(); + public static function read(ByteBufferReader $in) : self{ + $slot = Byte::readUnsigned($in); return new self($slot); } - public function write(PacketSerializer $out) : void{ - $out->putByte($this->resultIndex); + public function write(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->resultIndex); } } diff --git a/src/types/inventory/stackrequest/CreativeCreateStackRequestAction.php b/src/types/inventory/stackrequest/CreativeCreateStackRequestAction.php index 74d09ad8..ecfa5532 100644 --- a/src/types/inventory/stackrequest/CreativeCreateStackRequestAction.php +++ b/src/types/inventory/stackrequest/CreativeCreateStackRequestAction.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; /** @@ -34,14 +37,14 @@ public function getCreativeItemId() : int{ return $this->creativeItemId; } public function getRepetitions() : int{ return $this->repetitions; } - public static function read(PacketSerializer $in) : self{ - $creativeItemId = $in->readCreativeItemNetId(); - $repetitions = $in->getByte(); + public static function read(ByteBufferReader $in) : self{ + $creativeItemId = CommonTypes::readCreativeItemNetId($in); + $repetitions = Byte::readUnsigned($in); return new self($creativeItemId, $repetitions); } - public function write(PacketSerializer $out) : void{ - $out->writeCreativeItemNetId($this->creativeItemId); - $out->putByte($this->repetitions); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::writeCreativeItemNetId($out, $this->creativeItemId); + Byte::writeUnsigned($out, $this->repetitions); } } diff --git a/src/types/inventory/stackrequest/DeprecatedCraftingNonImplementedStackRequestAction.php b/src/types/inventory/stackrequest/DeprecatedCraftingNonImplementedStackRequestAction.php index 0dfb8031..d530f642 100644 --- a/src/types/inventory/stackrequest/DeprecatedCraftingNonImplementedStackRequestAction.php +++ b/src/types/inventory/stackrequest/DeprecatedCraftingNonImplementedStackRequestAction.php @@ -14,7 +14,8 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; /** @@ -26,11 +27,11 @@ final class DeprecatedCraftingNonImplementedStackRequestAction extends ItemStack public const ID = ItemStackRequestActionType::CRAFTING_NON_IMPLEMENTED_DEPRECATED_ASK_TY_LAING; - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ return new self; } - public function write(PacketSerializer $out) : void{ + public function write(ByteBufferWriter $out) : void{ //NOOP } } diff --git a/src/types/inventory/stackrequest/DeprecatedCraftingResultsStackRequestAction.php b/src/types/inventory/stackrequest/DeprecatedCraftingResultsStackRequestAction.php index eb133347..4dec8dd3 100644 --- a/src/types/inventory/stackrequest/DeprecatedCraftingResultsStackRequestAction.php +++ b/src/types/inventory/stackrequest/DeprecatedCraftingResultsStackRequestAction.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; use pocketmine\network\mcpe\protocol\types\inventory\ItemStack; use function count; @@ -41,20 +45,20 @@ public function getResults() : array{ return $this->results; } public function getIterations() : int{ return $this->iterations; } - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ $results = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ - $results[] = $in->getItemStackWithoutStackId(); + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ + $results[] = CommonTypes::getItemStackWithoutStackId($in); } - $iterations = $in->getByte(); + $iterations = Byte::readUnsigned($in); return new self($results, $iterations); } - public function write(PacketSerializer $out) : void{ - $out->putUnsignedVarInt(count($this->results)); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeUnsignedInt($out, count($this->results)); foreach($this->results as $result){ - $out->putItemStackWithoutStackId($result); + CommonTypes::putItemStackWithoutStackId($out, $result); } - $out->putByte($this->iterations); + Byte::writeUnsigned($out, $this->iterations); } } diff --git a/src/types/inventory/stackrequest/DisappearStackRequestActionTrait.php b/src/types/inventory/stackrequest/DisappearStackRequestActionTrait.php index 7b43eca9..5be3d5be 100644 --- a/src/types/inventory/stackrequest/DisappearStackRequestActionTrait.php +++ b/src/types/inventory/stackrequest/DisappearStackRequestActionTrait.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; trait DisappearStackRequestActionTrait{ final public function __construct( @@ -26,14 +28,14 @@ final public function getCount() : int{ return $this->count; } final public function getSource() : ItemStackRequestSlotInfo{ return $this->source; } - public static function read(PacketSerializer $in) : self{ - $count = $in->getByte(); + public static function read(ByteBufferReader $in) : self{ + $count = Byte::readUnsigned($in); $source = ItemStackRequestSlotInfo::read($in); return new self($count, $source); } - public function write(PacketSerializer $out) : void{ - $out->putByte($this->count); + public function write(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->count); $this->source->write($out); } } diff --git a/src/types/inventory/stackrequest/DropStackRequestAction.php b/src/types/inventory/stackrequest/DropStackRequestAction.php index 00644763..a5da35fa 100644 --- a/src/types/inventory/stackrequest/DropStackRequestAction.php +++ b/src/types/inventory/stackrequest/DropStackRequestAction.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; /** @@ -37,16 +40,16 @@ public function getSource() : ItemStackRequestSlotInfo{ return $this->source; } public function isRandomly() : bool{ return $this->randomly; } - public static function read(PacketSerializer $in) : self{ - $count = $in->getByte(); + public static function read(ByteBufferReader $in) : self{ + $count = Byte::readUnsigned($in); $source = ItemStackRequestSlotInfo::read($in); - $random = $in->getBool(); + $random = CommonTypes::getBool($in); return new self($count, $source, $random); } - public function write(PacketSerializer $out) : void{ - $out->putByte($this->count); + public function write(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->count); $this->source->write($out); - $out->putBool($this->randomly); + CommonTypes::putBool($out, $this->randomly); } } diff --git a/src/types/inventory/stackrequest/GrindstoneStackRequestAction.php b/src/types/inventory/stackrequest/GrindstoneStackRequestAction.php index e161d8f6..856331f2 100644 --- a/src/types/inventory/stackrequest/GrindstoneStackRequestAction.php +++ b/src/types/inventory/stackrequest/GrindstoneStackRequestAction.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; /** @@ -38,17 +42,17 @@ public function getRepairCost() : int{ return $this->repairCost; } public function getRepetitions() : int{ return $this->repetitions; } - public static function read(PacketSerializer $in) : self{ - $recipeId = $in->readRecipeNetId(); - $repairCost = $in->getVarInt(); //WHY!!!! - $repetitions = $in->getByte(); + public static function read(ByteBufferReader $in) : self{ + $recipeId = CommonTypes::readRecipeNetId($in); + $repairCost = VarInt::readSignedInt($in); //WHY!!!! + $repetitions = Byte::readUnsigned($in); return new self($recipeId, $repairCost, $repetitions); } - public function write(PacketSerializer $out) : void{ - $out->writeRecipeNetId($this->recipeId); - $out->putVarInt($this->repairCost); - $out->putByte($this->repetitions); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::writeRecipeNetId($out, $this->recipeId); + VarInt::writeSignedInt($out, $this->repairCost); + Byte::writeUnsigned($out, $this->repetitions); } } diff --git a/src/types/inventory/stackrequest/ItemStackRequest.php b/src/types/inventory/stackrequest/ItemStackRequest.php index dd82de79..6123c389 100644 --- a/src/types/inventory/stackrequest/ItemStackRequest.php +++ b/src/types/inventory/stackrequest/ItemStackRequest.php @@ -14,9 +14,14 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\DataDecodeException; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\PacketDecodeException; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; -use pocketmine\utils\BinaryDataException; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use function count; final class ItemStackRequest{ @@ -46,10 +51,10 @@ public function getFilterStrings() : array{ return $this->filterStrings; } public function getFilterStringCause() : int{ return $this->filterStringCause; } /** - * @throws BinaryDataException + * @throws DataDecodeException * @throws PacketDecodeException */ - private static function readAction(PacketSerializer $in, int $typeId) : ItemStackRequestAction{ + private static function readAction(ByteBufferReader $in, int $typeId) : ItemStackRequestAction{ return match($typeId){ TakeStackRequestAction::ID => TakeStackRequestAction::read($in), PlaceStackRequestAction::ID => PlaceStackRequestAction::read($in), @@ -73,32 +78,32 @@ private static function readAction(PacketSerializer $in, int $typeId) : ItemStac }; } - public static function read(PacketSerializer $in) : self{ - $requestId = $in->readItemStackRequestId(); + public static function read(ByteBufferReader $in) : self{ + $requestId = CommonTypes::readItemStackRequestId($in); $actions = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ - $typeId = $in->getByte(); + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ + $typeId = Byte::readUnsigned($in); $actions[] = self::readAction($in, $typeId); } $filterStrings = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ - $filterStrings[] = $in->getString(); + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ + $filterStrings[] = CommonTypes::getString($in); } - $filterStringCause = $in->getLInt(); + $filterStringCause = LE::readSignedInt($in); return new self($requestId, $actions, $filterStrings, $filterStringCause); } - public function write(PacketSerializer $out) : void{ - $out->writeItemStackRequestId($this->requestId); - $out->putUnsignedVarInt(count($this->actions)); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::writeItemStackRequestId($out, $this->requestId); + VarInt::writeUnsignedInt($out, count($this->actions)); foreach($this->actions as $action){ - $out->putByte($action->getTypeId()); + Byte::writeUnsigned($out, $action->getTypeId()); $action->write($out); } - $out->putUnsignedVarInt(count($this->filterStrings)); + VarInt::writeUnsignedInt($out, count($this->filterStrings)); foreach($this->filterStrings as $string){ - $out->putString($string); + CommonTypes::putString($out, $string); } - $out->putLInt($this->filterStringCause); + LE::writeSignedInt($out, $this->filterStringCause); } } diff --git a/src/types/inventory/stackrequest/ItemStackRequestAction.php b/src/types/inventory/stackrequest/ItemStackRequestAction.php index 9cf6252f..7f272ce7 100644 --- a/src/types/inventory/stackrequest/ItemStackRequestAction.php +++ b/src/types/inventory/stackrequest/ItemStackRequestAction.php @@ -14,11 +14,11 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferWriter; abstract class ItemStackRequestAction{ abstract public function getTypeId() : int; - abstract public function write(PacketSerializer $out) : void; + abstract public function write(ByteBufferWriter $out) : void; } diff --git a/src/types/inventory/stackrequest/ItemStackRequestSlotInfo.php b/src/types/inventory/stackrequest/ItemStackRequestSlotInfo.php index 09688f51..668d74b9 100644 --- a/src/types/inventory/stackrequest/ItemStackRequestSlotInfo.php +++ b/src/types/inventory/stackrequest/ItemStackRequestSlotInfo.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\inventory\FullContainerName; final class ItemStackRequestSlotInfo{ @@ -30,16 +33,16 @@ public function getSlotId() : int{ return $this->slotId; } public function getStackId() : int{ return $this->stackId; } - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ $containerName = FullContainerName::read($in); - $slotId = $in->getByte(); - $stackId = $in->readItemStackNetIdVariant(); + $slotId = Byte::readUnsigned($in); + $stackId = CommonTypes::readItemStackNetIdVariant($in); return new self($containerName, $slotId, $stackId); } - public function write(PacketSerializer $out) : void{ + public function write(ByteBufferWriter $out) : void{ $this->containerName->write($out); - $out->putByte($this->slotId); - $out->writeItemStackNetIdVariant($this->stackId); + Byte::writeUnsigned($out, $this->slotId); + CommonTypes::writeItemStackNetIdVariant($out, $this->stackId); } } diff --git a/src/types/inventory/stackrequest/LabTableCombineStackRequestAction.php b/src/types/inventory/stackrequest/LabTableCombineStackRequestAction.php index 1b05e049..104a0fd6 100644 --- a/src/types/inventory/stackrequest/LabTableCombineStackRequestAction.php +++ b/src/types/inventory/stackrequest/LabTableCombineStackRequestAction.php @@ -14,7 +14,8 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; /** @@ -26,11 +27,11 @@ final class LabTableCombineStackRequestAction extends ItemStackRequestAction{ public const ID = ItemStackRequestActionType::LAB_TABLE_COMBINE; - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ return new self; } - public function write(PacketSerializer $out) : void{ + public function write(ByteBufferWriter $out) : void{ //NOOP } } diff --git a/src/types/inventory/stackrequest/LoomStackRequestAction.php b/src/types/inventory/stackrequest/LoomStackRequestAction.php index dbf6f3f3..a027bd07 100644 --- a/src/types/inventory/stackrequest/LoomStackRequestAction.php +++ b/src/types/inventory/stackrequest/LoomStackRequestAction.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; /** @@ -34,14 +37,14 @@ public function getPatternId() : string{ return $this->patternId; } public function getRepetitions() : int{ return $this->repetitions; } - public static function read(PacketSerializer $in) : self{ - $patternId = $in->getString(); - $repetitions = $in->getByte(); + public static function read(ByteBufferReader $in) : self{ + $patternId = CommonTypes::getString($in); + $repetitions = Byte::readUnsigned($in); return new self($patternId, $repetitions); } - public function write(PacketSerializer $out) : void{ - $out->putString($this->patternId); - $out->putByte($this->repetitions); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->patternId); + Byte::writeUnsigned($out, $this->repetitions); } } diff --git a/src/types/inventory/stackrequest/MineBlockStackRequestAction.php b/src/types/inventory/stackrequest/MineBlockStackRequestAction.php index e794dfc2..e41f227c 100644 --- a/src/types/inventory/stackrequest/MineBlockStackRequestAction.php +++ b/src/types/inventory/stackrequest/MineBlockStackRequestAction.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; final class MineBlockStackRequestAction extends ItemStackRequestAction{ @@ -34,16 +37,16 @@ public function getPredictedDurability() : int{ return $this->predictedDurabilit public function getStackId() : int{ return $this->stackId; } - public static function read(PacketSerializer $in) : self{ - $hotbarSlot = $in->getVarInt(); - $predictedDurability = $in->getVarInt(); - $stackId = $in->readItemStackNetIdVariant(); + public static function read(ByteBufferReader $in) : self{ + $hotbarSlot = VarInt::readSignedInt($in); + $predictedDurability = VarInt::readSignedInt($in); + $stackId = CommonTypes::readItemStackNetIdVariant($in); return new self($hotbarSlot, $predictedDurability, $stackId); } - public function write(PacketSerializer $out) : void{ - $out->putVarInt($this->hotbarSlot); - $out->putVarInt($this->predictedDurability); - $out->writeItemStackNetIdVariant($this->stackId); + public function write(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->hotbarSlot); + VarInt::writeSignedInt($out, $this->predictedDurability); + CommonTypes::writeItemStackNetIdVariant($out, $this->stackId); } } diff --git a/src/types/inventory/stackrequest/SwapStackRequestAction.php b/src/types/inventory/stackrequest/SwapStackRequestAction.php index f5f706bc..e0020301 100644 --- a/src/types/inventory/stackrequest/SwapStackRequestAction.php +++ b/src/types/inventory/stackrequest/SwapStackRequestAction.php @@ -14,7 +14,8 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; /** @@ -34,13 +35,13 @@ public function getSlot1() : ItemStackRequestSlotInfo{ return $this->slot1; } public function getSlot2() : ItemStackRequestSlotInfo{ return $this->slot2; } - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ $slot1 = ItemStackRequestSlotInfo::read($in); $slot2 = ItemStackRequestSlotInfo::read($in); return new self($slot1, $slot2); } - public function write(PacketSerializer $out) : void{ + public function write(ByteBufferWriter $out) : void{ $this->slot1->write($out); $this->slot2->write($out); } diff --git a/src/types/inventory/stackrequest/TakeOrPlaceStackRequestActionTrait.php b/src/types/inventory/stackrequest/TakeOrPlaceStackRequestActionTrait.php index 03ab6628..6c99aff0 100644 --- a/src/types/inventory/stackrequest/TakeOrPlaceStackRequestActionTrait.php +++ b/src/types/inventory/stackrequest/TakeOrPlaceStackRequestActionTrait.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; trait TakeOrPlaceStackRequestActionTrait{ final public function __construct( @@ -29,15 +31,15 @@ final public function getSource() : ItemStackRequestSlotInfo{ return $this->sour final public function getDestination() : ItemStackRequestSlotInfo{ return $this->destination; } - public static function read(PacketSerializer $in) : self{ - $count = $in->getByte(); + public static function read(ByteBufferReader $in) : self{ + $count = Byte::readUnsigned($in); $src = ItemStackRequestSlotInfo::read($in); $dst = ItemStackRequestSlotInfo::read($in); return new self($count, $src, $dst); } - public function write(PacketSerializer $out) : void{ - $out->putByte($this->count); + public function write(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->count); $this->source->write($out); $this->destination->write($out); } diff --git a/src/types/inventory/stackresponse/ItemStackResponse.php b/src/types/inventory/stackresponse/ItemStackResponse.php index c698fa25..dc781643 100644 --- a/src/types/inventory/stackresponse/ItemStackResponse.php +++ b/src/types/inventory/stackresponse/ItemStackResponse.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackresponse; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use function count; final class ItemStackResponse{ @@ -44,23 +48,23 @@ public function getRequestId() : int{ return $this->requestId; } /** @return ItemStackResponseContainerInfo[] */ public function getContainerInfos() : array{ return $this->containerInfos; } - public static function read(PacketSerializer $in) : self{ - $result = $in->getByte(); - $requestId = $in->readItemStackRequestId(); + public static function read(ByteBufferReader $in) : self{ + $result = Byte::readUnsigned($in); + $requestId = CommonTypes::readItemStackRequestId($in); $containerInfos = []; if($result === self::RESULT_OK){ - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ $containerInfos[] = ItemStackResponseContainerInfo::read($in); } } return new self($result, $requestId, $containerInfos); } - public function write(PacketSerializer $out) : void{ - $out->putByte($this->result); - $out->writeItemStackRequestId($this->requestId); + public function write(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->result); + CommonTypes::writeItemStackRequestId($out, $this->requestId); if($this->result === self::RESULT_OK){ - $out->putUnsignedVarInt(count($this->containerInfos)); + VarInt::writeUnsignedInt($out, count($this->containerInfos)); foreach($this->containerInfos as $containerInfo){ $containerInfo->write($out); } diff --git a/src/types/inventory/stackresponse/ItemStackResponseContainerInfo.php b/src/types/inventory/stackresponse/ItemStackResponseContainerInfo.php index 6cd03642..c3720785 100644 --- a/src/types/inventory/stackresponse/ItemStackResponseContainerInfo.php +++ b/src/types/inventory/stackresponse/ItemStackResponseContainerInfo.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackresponse; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\types\inventory\FullContainerName; use function count; @@ -32,18 +34,18 @@ public function getContainerName() : FullContainerName{ return $this->containerN /** @return ItemStackResponseSlotInfo[] */ public function getSlots() : array{ return $this->slots; } - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ $containerName = FullContainerName::read($in); $slots = []; - for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ + for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){ $slots[] = ItemStackResponseSlotInfo::read($in); } return new self($containerName, $slots); } - public function write(PacketSerializer $out) : void{ + public function write(ByteBufferWriter $out) : void{ $this->containerName->write($out); - $out->putUnsignedVarInt(count($this->slots)); + VarInt::writeUnsignedInt($out, count($this->slots)); foreach($this->slots as $slot){ $slot->write($out); } diff --git a/src/types/inventory/stackresponse/ItemStackResponseSlotInfo.php b/src/types/inventory/stackresponse/ItemStackResponseSlotInfo.php index fa83941d..4613c138 100644 --- a/src/types/inventory/stackresponse/ItemStackResponseSlotInfo.php +++ b/src/types/inventory/stackresponse/ItemStackResponseSlotInfo.php @@ -14,7 +14,11 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackresponse; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class ItemStackResponseSlotInfo{ public function __construct( @@ -41,24 +45,24 @@ public function getFilteredCustomName() : string{ return $this->filteredCustomNa public function getDurabilityCorrection() : int{ return $this->durabilityCorrection; } - public static function read(PacketSerializer $in) : self{ - $slot = $in->getByte(); - $hotbarSlot = $in->getByte(); - $count = $in->getByte(); - $itemStackId = $in->readServerItemStackId(); - $customName = $in->getString(); - $filteredCustomName = $in->getString(); - $durabilityCorrection = $in->getVarInt(); + public static function read(ByteBufferReader $in) : self{ + $slot = Byte::readUnsigned($in); + $hotbarSlot = Byte::readUnsigned($in); + $count = Byte::readUnsigned($in); + $itemStackId = CommonTypes::readServerItemStackId($in); + $customName = CommonTypes::getString($in); + $filteredCustomName = CommonTypes::getString($in); + $durabilityCorrection = VarInt::readSignedInt($in); return new self($slot, $hotbarSlot, $count, $itemStackId, $customName, $filteredCustomName, $durabilityCorrection); } - public function write(PacketSerializer $out) : void{ - $out->putByte($this->slot); - $out->putByte($this->hotbarSlot); - $out->putByte($this->count); - $out->writeServerItemStackId($this->itemStackId); - $out->putString($this->customName); - $out->putString($this->filteredCustomName); - $out->putVarInt($this->durabilityCorrection); + public function write(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->slot); + Byte::writeUnsigned($out, $this->hotbarSlot); + Byte::writeUnsigned($out, $this->count); + CommonTypes::writeServerItemStackId($out, $this->itemStackId); + CommonTypes::putString($out, $this->customName); + CommonTypes::putString($out, $this->filteredCustomName); + VarInt::writeSignedInt($out, $this->durabilityCorrection); } } diff --git a/src/types/recipe/ComplexAliasItemDescriptor.php b/src/types/recipe/ComplexAliasItemDescriptor.php index 19a2b990..76e2828b 100644 --- a/src/types/recipe/ComplexAliasItemDescriptor.php +++ b/src/types/recipe/ComplexAliasItemDescriptor.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\recipe; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; final class ComplexAliasItemDescriptor implements ItemDescriptor{ @@ -28,13 +30,13 @@ public function __construct( public function getAlias() : string{ return $this->alias; } - public static function read(PacketSerializer $in) : self{ - $alias = $in->getString(); + public static function read(ByteBufferReader $in) : self{ + $alias = CommonTypes::getString($in); return new self($alias); } - public function write(PacketSerializer $out) : void{ - $out->putString($this->alias); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->alias); } } diff --git a/src/types/recipe/FurnaceRecipe.php b/src/types/recipe/FurnaceRecipe.php index b55e2f7e..d28e3876 100644 --- a/src/types/recipe/FurnaceRecipe.php +++ b/src/types/recipe/FurnaceRecipe.php @@ -14,8 +14,11 @@ namespace pocketmine\network\mcpe\protocol\types\recipe; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\CraftingDataPacket; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\inventory\ItemStack; final class FurnaceRecipe extends RecipeWithTypeId{ @@ -45,24 +48,24 @@ public function getBlockName() : string{ return $this->blockName; } - public static function decode(int $typeId, PacketSerializer $in) : self{ - $inputId = $in->getVarInt(); + public static function decode(int $typeId, ByteBufferReader $in) : self{ + $inputId = VarInt::readSignedInt($in); $inputData = null; if($typeId === CraftingDataPacket::ENTRY_FURNACE_DATA){ - $inputData = $in->getVarInt(); + $inputData = VarInt::readSignedInt($in); } - $output = $in->getItemStackWithoutStackId(); - $block = $in->getString(); + $output = CommonTypes::getItemStackWithoutStackId($in); + $block = CommonTypes::getString($in); return new self($typeId, $inputId, $inputData, $output, $block); } - public function encode(PacketSerializer $out) : void{ - $out->putVarInt($this->inputId); + public function encode(ByteBufferWriter $out) : void{ + VarInt::writeSignedInt($out, $this->inputId); if($this->getTypeId() === CraftingDataPacket::ENTRY_FURNACE_DATA){ - $out->putVarInt($this->inputMeta); + VarInt::writeSignedInt($out, $this->inputMeta); } - $out->putItemStackWithoutStackId($this->result); - $out->putString($this->blockName); + CommonTypes::putItemStackWithoutStackId($out, $this->result); + CommonTypes::putString($out, $this->blockName); } } diff --git a/src/types/recipe/IntIdMetaItemDescriptor.php b/src/types/recipe/IntIdMetaItemDescriptor.php index ac056c74..8c89d973 100644 --- a/src/types/recipe/IntIdMetaItemDescriptor.php +++ b/src/types/recipe/IntIdMetaItemDescriptor.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\recipe; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; final class IntIdMetaItemDescriptor implements ItemDescriptor{ @@ -35,10 +37,10 @@ public function getId() : int{ return $this->id; } public function getMeta() : int{ return $this->meta; } - public static function read(PacketSerializer $in) : self{ - $id = $in->getSignedLShort(); + public static function read(ByteBufferReader $in) : self{ + $id = LE::readSignedShort($in); if($id !== 0){ - $meta = $in->getSignedLShort(); + $meta = LE::readSignedShort($in); }else{ $meta = 0; } @@ -46,10 +48,10 @@ public static function read(PacketSerializer $in) : self{ return new self($id, $meta); } - public function write(PacketSerializer $out) : void{ - $out->putLShort($this->id); + public function write(ByteBufferWriter $out) : void{ + LE::writeSignedShort($out, $this->id); if($this->id !== 0){ - $out->putLShort($this->meta); + LE::writeSignedShort($out, $this->meta); } } } diff --git a/src/types/recipe/ItemDescriptor.php b/src/types/recipe/ItemDescriptor.php index 9890d4f8..9694c0a6 100644 --- a/src/types/recipe/ItemDescriptor.php +++ b/src/types/recipe/ItemDescriptor.php @@ -14,7 +14,7 @@ namespace pocketmine\network\mcpe\protocol\types\recipe; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferWriter; /** * Describes what items are accepted in a recipe input. @@ -22,5 +22,5 @@ interface ItemDescriptor{ public function getTypeId() : int; - public function write(PacketSerializer $out) : void; + public function write(ByteBufferWriter $out) : void; } diff --git a/src/types/recipe/MolangItemDescriptor.php b/src/types/recipe/MolangItemDescriptor.php index 624c9bbd..b196aadf 100644 --- a/src/types/recipe/MolangItemDescriptor.php +++ b/src/types/recipe/MolangItemDescriptor.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\recipe; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; final class MolangItemDescriptor implements ItemDescriptor{ @@ -31,15 +34,15 @@ public function getMolangExpression() : string{ return $this->molangExpression; public function getMolangVersion() : int{ return $this->molangVersion; } - public static function read(PacketSerializer $in) : self{ - $expression = $in->getString(); - $version = $in->getByte(); + public static function read(ByteBufferReader $in) : self{ + $expression = CommonTypes::getString($in); + $version = Byte::readUnsigned($in); return new self($expression, $version); } - public function write(PacketSerializer $out) : void{ - $out->putString($this->molangExpression); - $out->putByte($this->molangVersion); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->molangExpression); + Byte::writeUnsigned($out, $this->molangVersion); } } diff --git a/src/types/recipe/MultiRecipe.php b/src/types/recipe/MultiRecipe.php index f4091c5e..81586377 100644 --- a/src/types/recipe/MultiRecipe.php +++ b/src/types/recipe/MultiRecipe.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\recipe; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use Ramsey\Uuid\UuidInterface; final class MultiRecipe extends RecipeWithTypeId{ @@ -48,14 +50,14 @@ public function getRecipeNetId() : int{ return $this->recipeNetId; } - public static function decode(int $typeId, PacketSerializer $in) : self{ - $uuid = $in->getUUID(); - $recipeNetId = $in->readRecipeNetId(); + public static function decode(int $typeId, ByteBufferReader $in) : self{ + $uuid = CommonTypes::getUUID($in); + $recipeNetId = CommonTypes::readRecipeNetId($in); return new self($typeId, $uuid, $recipeNetId); } - public function encode(PacketSerializer $out) : void{ - $out->putUUID($this->recipeId); - $out->writeRecipeNetId($this->recipeNetId); + public function encode(ByteBufferWriter $out) : void{ + CommonTypes::putUUID($out, $this->recipeId); + CommonTypes::writeRecipeNetId($out, $this->recipeNetId); } } diff --git a/src/types/recipe/RecipeUnlockingRequirement.php b/src/types/recipe/RecipeUnlockingRequirement.php index 4955d130..4c352c1e 100644 --- a/src/types/recipe/RecipeUnlockingRequirement.php +++ b/src/types/recipe/RecipeUnlockingRequirement.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\recipe; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use function count; final class RecipeUnlockingRequirement{ @@ -33,27 +36,27 @@ public function __construct( */ public function getUnlockingIngredients() : ?array{ return $this->unlockingIngredients; } - public static function read(PacketSerializer $in) : self{ + public static function read(ByteBufferReader $in) : self{ //I don't know what the point of this structure is. It could easily have been a list instead. //It's basically just an optional list, which could have been done by an empty list wherever it's not needed. - $unlockingContext = $in->getBool(); + $unlockingContext = CommonTypes::getBool($in); $unlockingIngredients = null; if(!$unlockingContext){ $unlockingIngredients = []; - for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; $i++){ - $unlockingIngredients[] = $in->getRecipeIngredient(); + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; $i++){ + $unlockingIngredients[] = CommonTypes::getRecipeIngredient($in); } } return new self($unlockingIngredients); } - public function write(PacketSerializer $out) : void{ - $out->putBool($this->unlockingIngredients === null); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putBool($out, $this->unlockingIngredients === null); if($this->unlockingIngredients !== null){ - $out->putUnsignedVarInt(count($this->unlockingIngredients)); + VarInt::writeUnsignedInt($out, count($this->unlockingIngredients)); foreach($this->unlockingIngredients as $ingredient){ - $out->putRecipeIngredient($ingredient); + CommonTypes::putRecipeIngredient($out, $ingredient); } } } diff --git a/src/types/recipe/RecipeWithTypeId.php b/src/types/recipe/RecipeWithTypeId.php index d70a4ef8..e4f09989 100644 --- a/src/types/recipe/RecipeWithTypeId.php +++ b/src/types/recipe/RecipeWithTypeId.php @@ -14,7 +14,7 @@ namespace pocketmine\network\mcpe\protocol\types\recipe; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferWriter; abstract class RecipeWithTypeId{ protected function __construct( @@ -25,5 +25,5 @@ final public function getTypeId() : int{ return $this->typeId; } - abstract public function encode(PacketSerializer $out) : void; + abstract public function encode(ByteBufferWriter $out) : void; } diff --git a/src/types/recipe/ShapedRecipe.php b/src/types/recipe/ShapedRecipe.php index ec116f1a..1cdb6792 100644 --- a/src/types/recipe/ShapedRecipe.php +++ b/src/types/recipe/ShapedRecipe.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\recipe; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\inventory\ItemStack; use Ramsey\Uuid\UuidInterface; use function count; @@ -104,53 +107,53 @@ public function getRecipeNetId() : int{ return $this->recipeNetId; } - public static function decode(int $recipeType, PacketSerializer $in) : self{ - $recipeId = $in->getString(); - $width = $in->getVarInt(); - $height = $in->getVarInt(); + public static function decode(int $recipeType, ByteBufferReader $in) : self{ + $recipeId = CommonTypes::getString($in); + $width = VarInt::readSignedInt($in); + $height = VarInt::readSignedInt($in); $input = []; for($row = 0; $row < $height; ++$row){ for($column = 0; $column < $width; ++$column){ - $input[$row][$column] = $in->getRecipeIngredient(); + $input[$row][$column] = CommonTypes::getRecipeIngredient($in); } } $output = []; - for($k = 0, $resultCount = $in->getUnsignedVarInt(); $k < $resultCount; ++$k){ - $output[] = $in->getItemStackWithoutStackId(); + for($k = 0, $resultCount = VarInt::readUnsignedInt($in); $k < $resultCount; ++$k){ + $output[] = CommonTypes::getItemStackWithoutStackId($in); } - $uuid = $in->getUUID(); - $block = $in->getString(); - $priority = $in->getVarInt(); - $symmetric = $in->getBool(); + $uuid = CommonTypes::getUUID($in); + $block = CommonTypes::getString($in); + $priority = VarInt::readSignedInt($in); + $symmetric = CommonTypes::getBool($in); $unlockingRequirement = RecipeUnlockingRequirement::read($in); - $recipeNetId = $in->readRecipeNetId(); + $recipeNetId = CommonTypes::readRecipeNetId($in); return new self($recipeType, $recipeId, $input, $output, $uuid, $block, $priority, $symmetric, $unlockingRequirement, $recipeNetId); } - public function encode(PacketSerializer $out) : void{ - $out->putString($this->recipeId); - $out->putVarInt($this->getWidth()); - $out->putVarInt($this->getHeight()); + public function encode(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->recipeId); + VarInt::writeSignedInt($out, $this->getWidth()); + VarInt::writeSignedInt($out, $this->getHeight()); foreach($this->input as $row){ foreach($row as $ingredient){ - $out->putRecipeIngredient($ingredient); + CommonTypes::putRecipeIngredient($out, $ingredient); } } - $out->putUnsignedVarInt(count($this->output)); + VarInt::writeUnsignedInt($out, count($this->output)); foreach($this->output as $item){ - $out->putItemStackWithoutStackId($item); + CommonTypes::putItemStackWithoutStackId($out, $item); } - $out->putUUID($this->uuid); - $out->putString($this->blockName); - $out->putVarInt($this->priority); - $out->putBool($this->symmetric); + CommonTypes::putUUID($out, $this->uuid); + CommonTypes::putString($out, $this->blockName); + VarInt::writeSignedInt($out, $this->priority); + CommonTypes::putBool($out, $this->symmetric); $this->unlockingRequirement->write($out); - $out->writeRecipeNetId($this->recipeNetId); + CommonTypes::writeRecipeNetId($out, $this->recipeNetId); } } diff --git a/src/types/recipe/ShapelessRecipe.php b/src/types/recipe/ShapelessRecipe.php index 84adc01a..d922a822 100644 --- a/src/types/recipe/ShapelessRecipe.php +++ b/src/types/recipe/ShapelessRecipe.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\recipe; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\inventory\ItemStack; use Ramsey\Uuid\UuidInterface; use function count; @@ -78,43 +81,43 @@ public function getRecipeNetId() : int{ return $this->recipeNetId; } - public static function decode(int $recipeType, PacketSerializer $in) : self{ - $recipeId = $in->getString(); + public static function decode(int $recipeType, ByteBufferReader $in) : self{ + $recipeId = CommonTypes::getString($in); $input = []; - for($j = 0, $ingredientCount = $in->getUnsignedVarInt(); $j < $ingredientCount; ++$j){ - $input[] = $in->getRecipeIngredient(); + for($j = 0, $ingredientCount = VarInt::readUnsignedInt($in); $j < $ingredientCount; ++$j){ + $input[] = CommonTypes::getRecipeIngredient($in); } $output = []; - for($k = 0, $resultCount = $in->getUnsignedVarInt(); $k < $resultCount; ++$k){ - $output[] = $in->getItemStackWithoutStackId(); + for($k = 0, $resultCount = VarInt::readUnsignedInt($in); $k < $resultCount; ++$k){ + $output[] = CommonTypes::getItemStackWithoutStackId($in); } - $uuid = $in->getUUID(); - $block = $in->getString(); - $priority = $in->getVarInt(); + $uuid = CommonTypes::getUUID($in); + $block = CommonTypes::getString($in); + $priority = VarInt::readSignedInt($in); $unlockingRequirement = RecipeUnlockingRequirement::read($in); - $recipeNetId = $in->readRecipeNetId(); + $recipeNetId = CommonTypes::readRecipeNetId($in); return new self($recipeType, $recipeId, $input, $output, $uuid, $block, $priority, $unlockingRequirement, $recipeNetId); } - public function encode(PacketSerializer $out) : void{ - $out->putString($this->recipeId); - $out->putUnsignedVarInt(count($this->inputs)); + public function encode(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->recipeId); + VarInt::writeUnsignedInt($out, count($this->inputs)); foreach($this->inputs as $item){ - $out->putRecipeIngredient($item); + CommonTypes::putRecipeIngredient($out, $item); } - $out->putUnsignedVarInt(count($this->outputs)); + VarInt::writeUnsignedInt($out, count($this->outputs)); foreach($this->outputs as $item){ - $out->putItemStackWithoutStackId($item); + CommonTypes::putItemStackWithoutStackId($out, $item); } - $out->putUUID($this->uuid); - $out->putString($this->blockName); - $out->putVarInt($this->priority); + CommonTypes::putUUID($out, $this->uuid); + CommonTypes::putString($out, $this->blockName); + VarInt::writeSignedInt($out, $this->priority); $this->unlockingRequirement->write($out); - $out->writeRecipeNetId($this->recipeNetId); + CommonTypes::writeRecipeNetId($out, $this->recipeNetId); } } diff --git a/src/types/recipe/SmithingTransformRecipe.php b/src/types/recipe/SmithingTransformRecipe.php index 62cd4b46..4543eca9 100644 --- a/src/types/recipe/SmithingTransformRecipe.php +++ b/src/types/recipe/SmithingTransformRecipe.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\recipe; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\inventory\ItemStack; final class SmithingTransformRecipe extends RecipeWithTypeId{ @@ -46,14 +48,14 @@ public function getBlockName() : string{ return $this->blockName; } public function getRecipeNetId() : int{ return $this->recipeNetId; } - public static function decode(int $typeId, PacketSerializer $in) : self{ - $recipeId = $in->getString(); - $template = $in->getRecipeIngredient(); - $input = $in->getRecipeIngredient(); - $addition = $in->getRecipeIngredient(); - $output = $in->getItemStackWithoutStackId(); - $blockName = $in->getString(); - $recipeNetId = $in->readRecipeNetId(); + public static function decode(int $typeId, ByteBufferReader $in) : self{ + $recipeId = CommonTypes::getString($in); + $template = CommonTypes::getRecipeIngredient($in); + $input = CommonTypes::getRecipeIngredient($in); + $addition = CommonTypes::getRecipeIngredient($in); + $output = CommonTypes::getItemStackWithoutStackId($in); + $blockName = CommonTypes::getString($in); + $recipeNetId = CommonTypes::readRecipeNetId($in); return new self( $typeId, @@ -67,13 +69,13 @@ public static function decode(int $typeId, PacketSerializer $in) : self{ ); } - public function encode(PacketSerializer $out) : void{ - $out->putString($this->recipeId); - $out->putRecipeIngredient($this->template); - $out->putRecipeIngredient($this->input); - $out->putRecipeIngredient($this->addition); - $out->putItemStackWithoutStackId($this->output); - $out->putString($this->blockName); - $out->writeRecipeNetId($this->recipeNetId); + public function encode(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->recipeId); + CommonTypes::putRecipeIngredient($out, $this->template); + CommonTypes::putRecipeIngredient($out, $this->input); + CommonTypes::putRecipeIngredient($out, $this->addition); + CommonTypes::putItemStackWithoutStackId($out, $this->output); + CommonTypes::putString($out, $this->blockName); + CommonTypes::writeRecipeNetId($out, $this->recipeNetId); } } diff --git a/src/types/recipe/SmithingTrimRecipe.php b/src/types/recipe/SmithingTrimRecipe.php index bf2ae39a..56a451b8 100644 --- a/src/types/recipe/SmithingTrimRecipe.php +++ b/src/types/recipe/SmithingTrimRecipe.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\recipe; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; final class SmithingTrimRecipe extends RecipeWithTypeId{ @@ -42,13 +44,13 @@ public function getBlockName() : string{ return $this->blockName; } public function getRecipeNetId() : int{ return $this->recipeNetId; } - public static function decode(int $typeId, PacketSerializer $in) : self{ - $recipeId = $in->getString(); - $template = $in->getRecipeIngredient(); - $input = $in->getRecipeIngredient(); - $addition = $in->getRecipeIngredient(); - $blockName = $in->getString(); - $recipeNetId = $in->readRecipeNetId(); + public static function decode(int $typeId, ByteBufferReader $in) : self{ + $recipeId = CommonTypes::getString($in); + $template = CommonTypes::getRecipeIngredient($in); + $input = CommonTypes::getRecipeIngredient($in); + $addition = CommonTypes::getRecipeIngredient($in); + $blockName = CommonTypes::getString($in); + $recipeNetId = CommonTypes::readRecipeNetId($in); return new self( $typeId, @@ -61,12 +63,12 @@ public static function decode(int $typeId, PacketSerializer $in) : self{ ); } - public function encode(PacketSerializer $out) : void{ - $out->putString($this->recipeId); - $out->putRecipeIngredient($this->template); - $out->putRecipeIngredient($this->input); - $out->putRecipeIngredient($this->addition); - $out->putString($this->blockName); - $out->writeRecipeNetId($this->recipeNetId); + public function encode(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->recipeId); + CommonTypes::putRecipeIngredient($out, $this->template); + CommonTypes::putRecipeIngredient($out, $this->input); + CommonTypes::putRecipeIngredient($out, $this->addition); + CommonTypes::putString($out, $this->blockName); + CommonTypes::writeRecipeNetId($out, $this->recipeNetId); } } diff --git a/src/types/recipe/StringIdMetaItemDescriptor.php b/src/types/recipe/StringIdMetaItemDescriptor.php index 961668f8..07f0b505 100644 --- a/src/types/recipe/StringIdMetaItemDescriptor.php +++ b/src/types/recipe/StringIdMetaItemDescriptor.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\recipe; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; final class StringIdMetaItemDescriptor implements ItemDescriptor{ @@ -35,15 +38,15 @@ public function getId() : string{ return $this->id; } public function getMeta() : int{ return $this->meta; } - public static function read(PacketSerializer $in) : self{ - $stringId = $in->getString(); - $meta = $in->getLShort(); + public static function read(ByteBufferReader $in) : self{ + $stringId = CommonTypes::getString($in); + $meta = LE::readUnsignedShort($in); return new self($stringId, $meta); } - public function write(PacketSerializer $out) : void{ - $out->putString($this->id); - $out->putLShort($this->meta); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->id); + LE::writeUnsignedShort($out, $this->meta); } } diff --git a/src/types/recipe/TagItemDescriptor.php b/src/types/recipe/TagItemDescriptor.php index fc698134..c2c84354 100644 --- a/src/types/recipe/TagItemDescriptor.php +++ b/src/types/recipe/TagItemDescriptor.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\recipe; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait; final class TagItemDescriptor implements ItemDescriptor{ @@ -28,13 +30,13 @@ public function __construct( public function getTag() : string{ return $this->tag; } - public static function read(PacketSerializer $in) : self{ - $tag = $in->getString(); + public static function read(ByteBufferReader $in) : self{ + $tag = CommonTypes::getString($in); return new self($tag); } - public function write(PacketSerializer $out) : void{ - $out->putString($this->tag); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->tag); } } diff --git a/src/types/resourcepacks/ResourcePackInfoEntry.php b/src/types/resourcepacks/ResourcePackInfoEntry.php index 5752437d..3bcfb82a 100644 --- a/src/types/resourcepacks/ResourcePackInfoEntry.php +++ b/src/types/resourcepacks/ResourcePackInfoEntry.php @@ -14,7 +14,10 @@ namespace pocketmine\network\mcpe\protocol\types\resourcepacks; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use Ramsey\Uuid\UuidInterface; class ResourcePackInfoEntry{ @@ -65,30 +68,30 @@ public function isRtxCapable() : bool{ return $this->isRtxCapable; } public function getCdnUrl() : string{ return $this->cdnUrl; } - public function write(PacketSerializer $out) : void{ - $out->putUUID($this->packId); - $out->putString($this->version); - $out->putLLong($this->sizeBytes); - $out->putString($this->encryptionKey); - $out->putString($this->subPackName); - $out->putString($this->contentId); - $out->putBool($this->hasScripts); - $out->putBool($this->isAddonPack); - $out->putBool($this->isRtxCapable); - $out->putString($this->cdnUrl); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putUUID($out, $this->packId); + CommonTypes::putString($out, $this->version); + LE::writeUnsignedLong($out, $this->sizeBytes); + CommonTypes::putString($out, $this->encryptionKey); + CommonTypes::putString($out, $this->subPackName); + CommonTypes::putString($out, $this->contentId); + CommonTypes::putBool($out, $this->hasScripts); + CommonTypes::putBool($out, $this->isAddonPack); + CommonTypes::putBool($out, $this->isRtxCapable); + CommonTypes::putString($out, $this->cdnUrl); } - public static function read(PacketSerializer $in) : self{ - $uuid = $in->getUUID(); - $version = $in->getString(); - $sizeBytes = $in->getLLong(); - $encryptionKey = $in->getString(); - $subPackName = $in->getString(); - $contentId = $in->getString(); - $hasScripts = $in->getBool(); - $isAddonPack = $in->getBool(); - $rtxCapable = $in->getBool(); - $cdnUrl = $in->getString(); + public static function read(ByteBufferReader $in) : self{ + $uuid = CommonTypes::getUUID($in); + $version = CommonTypes::getString($in); + $sizeBytes = LE::readUnsignedLong($in); + $encryptionKey = CommonTypes::getString($in); + $subPackName = CommonTypes::getString($in); + $contentId = CommonTypes::getString($in); + $hasScripts = CommonTypes::getBool($in); + $isAddonPack = CommonTypes::getBool($in); + $rtxCapable = CommonTypes::getBool($in); + $cdnUrl = CommonTypes::getString($in); return new self($uuid, $version, $sizeBytes, $encryptionKey, $subPackName, $contentId, $hasScripts, $isAddonPack, $rtxCapable, $cdnUrl); } } diff --git a/src/types/resourcepacks/ResourcePackStackEntry.php b/src/types/resourcepacks/ResourcePackStackEntry.php index 31774565..406624fc 100644 --- a/src/types/resourcepacks/ResourcePackStackEntry.php +++ b/src/types/resourcepacks/ResourcePackStackEntry.php @@ -14,7 +14,9 @@ namespace pocketmine\network\mcpe\protocol\types\resourcepacks; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; class ResourcePackStackEntry{ public function __construct( @@ -35,16 +37,16 @@ public function getSubPackName() : string{ return $this->subPackName; } - public function write(PacketSerializer $out) : void{ - $out->putString($this->packId); - $out->putString($this->version); - $out->putString($this->subPackName); + public function write(ByteBufferWriter $out) : void{ + CommonTypes::putString($out, $this->packId); + CommonTypes::putString($out, $this->version); + CommonTypes::putString($out, $this->subPackName); } - public static function read(PacketSerializer $in) : self{ - $packId = $in->getString(); - $version = $in->getString(); - $subPackName = $in->getString(); + public static function read(ByteBufferReader $in) : self{ + $packId = CommonTypes::getString($in); + $version = CommonTypes::getString($in); + $subPackName = CommonTypes::getString($in); return new self($packId, $version, $subPackName); } } diff --git a/tests/phpstan/configs/architectural-issues.neon b/tests/phpstan/configs/architectural-issues.neon index c8ad029e..00302978 100644 --- a/tests/phpstan/configs/architectural-issues.neon +++ b/tests/phpstan/configs/architectural-issues.neon @@ -1,31 +1,31 @@ parameters: ignoreErrors: - - message: "#^Property pocketmine\\\\network\\\\mcpe\\\\protocol\\\\LevelChunkPacket\\:\\:\\$dimensionId \\(0\\|1\\|2\\) does not accept int\\.$#" + message: '#^Property pocketmine\\network\\mcpe\\protocol\\LevelChunkPacket\:\:\$dimensionId \(0\|1\|2\) does not accept int\.$#' + identifier: assign.propertyType count: 1 path: ../../../src/LevelChunkPacket.php - - message: "#^Parameter \\#1 \\$eid of method pocketmine\\\\network\\\\mcpe\\\\protocol\\\\serializer\\\\PacketSerializer\\:\\:putActorUniqueId\\(\\) expects int, int\\|null given\\.$#" + message: '#^Parameter \#2 \$eid of static method pocketmine\\network\\mcpe\\protocol\\serializer\\CommonTypes\:\:putActorUniqueId\(\) expects int, int\|null given\.$#' + identifier: argument.type count: 1 path: ../../../src/SetScorePacket.php - - message: "#^Parameter \\#1 \\$v of method pocketmine\\\\network\\\\mcpe\\\\protocol\\\\serializer\\\\PacketSerializer\\:\\:putString\\(\\) expects string, string\\|null given\\.$#" + message: '#^Parameter \#2 \$v of static method pocketmine\\network\\mcpe\\protocol\\serializer\\CommonTypes\:\:putString\(\) expects string, string\|null given\.$#' + identifier: argument.type count: 1 path: ../../../src/SetScorePacket.php - - message: "#^Parameter \\#1 \\$eid of method pocketmine\\\\network\\\\mcpe\\\\protocol\\\\serializer\\\\PacketSerializer\\:\\:putActorUniqueId\\(\\) expects int, int\\|null given\\.$#" + message: '#^Parameter \#2 \$eid of static method pocketmine\\network\\mcpe\\protocol\\serializer\\CommonTypes\:\:putActorUniqueId\(\) expects int, int\|null given\.$#' + identifier: argument.type count: 1 path: ../../../src/SetScoreboardIdentityPacket.php - - message: "#^Property pocketmine\\\\network\\\\mcpe\\\\protocol\\\\serializer\\\\PacketSerializer\\:\\:\\$context is never read, only written\\.$#" - count: 1 - path: ../../../src/serializer/PacketSerializer.php - - - - message: "#^Parameter \\#1 \\$v of method pocketmine\\\\utils\\\\BinaryStream\\:\\:putVarInt\\(\\) expects int, int\\|null given\\.$#" + message: '#^Parameter \#2 \$value of static method pmmp\\encoding\\VarInt\:\:writeSignedInt\(\) expects int, int\|null given\.$#' + identifier: argument.type count: 1 path: ../../../src/types/recipe/FurnaceRecipe.php diff --git a/tests/phpunit/BitSetTest.php b/tests/phpunit/BitSetTest.php index f3872cc6..47eff59c 100644 --- a/tests/phpunit/BitSetTest.php +++ b/tests/phpunit/BitSetTest.php @@ -15,8 +15,10 @@ namespace pocketmine\network\mcpe\protocol; use PHPUnit\Framework\TestCase; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\VarInt; use pocketmine\network\mcpe\protocol\serializer\BitSet; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; class BitSetTest extends TestCase{ @@ -26,11 +28,11 @@ public function testBitSet() : void{ $writeTest->set(0, true); $writeTest->set(64, true); - $packetSerializer = PacketSerializer::encoder(); - $writeTest->write($packetSerializer); + $out = new ByteBufferWriter(); + $writeTest->write($out); - $packetSerializer = PacketSerializer::decoder($packetSerializer->getBuffer(), 0); - $readTest = BitSet::read($packetSerializer, 65); + $in = new ByteBufferReader($out->getData()); + $readTest = BitSet::read($in, 65); self::assertEqualBitSets($writeTest, $readTest); } @@ -41,34 +43,34 @@ public function testBitSetConstructor() : void{ $test2->set(64, true); - $packetSerializer = PacketSerializer::encoder(); - $test->write($packetSerializer); + $out1 = new ByteBufferWriter(); + $test->write($out1); - $packetSerializer2 = PacketSerializer::encoder(); - $test2->write($packetSerializer2); + $out2 = new ByteBufferWriter(); + $test2->write($out2); - self::assertEquals($packetSerializer->getBuffer(), $packetSerializer2->getBuffer()); + self::assertEquals($out1->getData(), $out2->getData()); } public function testBitSetParts() : void{ $writeTest = new BitSet(128); $writeTest->set(127, true); - $packetSerializer = PacketSerializer::encoder(); - $writeTest->write($packetSerializer); + $out = new ByteBufferWriter(); + $writeTest->write($out); - $packetSerializer = PacketSerializer::decoder($packetSerializer->getBuffer(), 0); - $readTest = BitSet::read($packetSerializer, 128); + $in = new ByteBufferReader($out->getData()); + $readTest = BitSet::read($in, 128); self::assertEqualBitSets($writeTest, $readTest); } public function testVarUnsignedLongCompatibility() : void{ - $packetSerializer = PacketSerializer::encoder(); - $packetSerializer->putUnsignedVarLong(0 | 1 << 63); + $out = new ByteBufferWriter(); + VarInt::writeUnsignedLong($out, 1 << 63); - $packetSerializer = PacketSerializer::decoder($packetSerializer->getBuffer(), 0); - $readTest = BitSet::read($packetSerializer, 64); + $in = new ByteBufferReader($out->getData()); + $readTest = BitSet::read($in, 64); $expectedResult = new BitSet(64); $expectedResult->set(63, true); diff --git a/tests/phpunit/DataPacketTest.php b/tests/phpunit/DataPacketTest.php index 5f86f82f..57e379a7 100644 --- a/tests/phpunit/DataPacketTest.php +++ b/tests/phpunit/DataPacketTest.php @@ -15,7 +15,8 @@ namespace pocketmine\network\mcpe\protocol; use PHPUnit\Framework\TestCase; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; class DataPacketTest extends TestCase{ @@ -24,11 +25,11 @@ public function testHeaderFidelity() : void{ $pk->senderSubId = 3; $pk->recipientSubId = 2; - $serializer = PacketSerializer::encoder(); + $serializer = new ByteBufferWriter(); $pk->encode($serializer); $pk2 = new TestPacket(); - $pk2->decode(PacketSerializer::decoder($serializer->getBuffer(), 0)); + $pk2->decode(new ByteBufferReader($serializer->getData())); self::assertSame($pk2->senderSubId, 3); self::assertSame($pk2->recipientSubId, 2); } diff --git a/tests/phpunit/LoginPacketTest.php b/tests/phpunit/LoginPacketTest.php index 78a66c8e..014ebd24 100644 --- a/tests/phpunit/LoginPacketTest.php +++ b/tests/phpunit/LoginPacketTest.php @@ -15,26 +15,32 @@ namespace pocketmine\network\mcpe\protocol; use PHPUnit\Framework\TestCase; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\BE; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use pmmp\encoding\VarInt; +use pocketmine\network\mcpe\protocol\serializer\CommonTypes; use function strlen; class LoginPacketTest extends TestCase{ public function testInvalidChainDataJsonHandling() : void{ - $stream = PacketSerializer::encoder(); - $stream->putUnsignedVarInt(ProtocolInfo::LOGIN_PACKET); + $stream = new ByteBufferWriter(); + VarInt::writeUnsignedInt($stream, ProtocolInfo::LOGIN_PACKET); + BE::writeUnsignedInt($stream, ProtocolInfo::CURRENT_PROTOCOL); + $payload = '{"chain":[]'; //intentionally malformed - $stream->putInt(ProtocolInfo::CURRENT_PROTOCOL); + $stream2 = new ByteBufferWriter(); + LE::writeUnsignedInt($stream2, strlen($payload)); + $stream2->writeByteArray($payload); - $stream2 = PacketSerializer::encoder(); - $stream2->putLInt(strlen($payload)); - $stream2->put($payload); - $stream->putString($stream2->getBuffer()); + CommonTypes::putString($stream, $stream2->getData()); - $pk = PacketPool::getInstance()->getPacket($stream->getBuffer()); + $pk = PacketPool::getInstance()->getPacket($stream->getData()); self::assertInstanceOf(LoginPacket::class, $pk); $this->expectException(PacketDecodeException::class); - $pk->decode(PacketSerializer::decoder($stream->getBuffer(), 0)); //bang + $pk->decode(new ByteBufferReader($stream->getData())); //bang } } diff --git a/tests/phpunit/TestPacket.php b/tests/phpunit/TestPacket.php index 856cece0..e1677cfa 100644 --- a/tests/phpunit/TestPacket.php +++ b/tests/phpunit/TestPacket.php @@ -14,16 +14,17 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; class TestPacket extends DataPacket{ public const NETWORK_ID = 1023; - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ } - protected function encodePayload(PacketSerializer $out) : void{ + protected function encodePayload(ByteBufferWriter $out) : void{ } diff --git a/tools/generate-protocol-info.php b/tools/generate-protocol-info.php index 837123b9..c3f422ed 100644 --- a/tools/generate-protocol-info.php +++ b/tools/generate-protocol-info.php @@ -75,7 +75,8 @@ namespace pocketmine\network\mcpe\protocol; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; class %s extends DataPacket{ public const NETWORK_ID = ProtocolInfo::%s; @@ -89,11 +90,11 @@ public static function create() : self{ return $result; } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ //TODO } - protected function encodePayload(PacketSerializer $out) : void{ + protected function encodePayload(ByteBufferWriter $out) : void{ //TODO } @@ -177,8 +178,8 @@ interface PacketHandlerInterface{ namespace pocketmine\network\mcpe\protocol; -use pocketmine\utils\Binary; -use pocketmine\utils\BinaryDataException; +use pmmp\encoding\DataDecodeException; +use pmmp\encoding\VarInt; class PacketPool{ protected static ?PacketPool $instance = null; @@ -207,11 +208,10 @@ public function getPacketById(int $pid) : ?Packet{ } /** - * @throws BinaryDataException + * @throws DataDecodeException */ public function getPacket(string $buffer) : ?Packet{ - $offset = 0; - return $this->getPacketById(Binary::readUnsignedVarInt($buffer, $offset) & DataPacket::PID_MASK); + return $this->getPacketById(VarInt::unpackUnsignedInt($buffer) & DataPacket::PID_MASK); } }