Skip to content

Commit 5ad5f12

Browse files
authored
Migrate encode & decode to use ext-encoding (really big commit) (#322)
- PacketSerializer is now gone - All places which previously used ByteBufferReader and ByteBufferWriter instead of PacketSerializer/BinaryStream - All helper methods previously in PacketSerializer are now provided as CommonTypes:: static methods instead BinaryUtils is still required for now since there's a couple of places where it's still irreplaceable. But all encoding and decoding is now done using ext-encoding, which should yield significant performance improvements. Stable version of NBT is still used for the time being as we don't want to be forced to fully switch over to ext-encoding right away. Doing network only is safer for now since there'll be no lasting damage done if there are growing pains. Proper integration of ext-encoding+NBT will come later. For now, this is enough to net some gains.
1 parent af7f48e commit 5ad5f12

File tree

357 files changed

+5670
-4726
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

357 files changed

+5670
-4726
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ jobs:
1818
with:
1919
php-version: ${{ matrix.php }}
2020
ini-values: xdebug.max_nesting_level=3000
21+
extensions: encoding-pmmp/[email protected]
2122

2223
- name: Load Composer package cache
2324
uses: actions/cache@v4
@@ -49,6 +50,7 @@ jobs:
4950
php-version: 8.2
5051
ini-values: xdebug.max_nesting_level=3000
5152
tools: php-cs-fixer:3
53+
extensions: encoding-pmmp/[email protected]
5254
env:
5355
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5456

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"type": "library",
55
"require": {
66
"php": "^8.1",
7+
"ext-encoding": "~1.0.0",
78
"ext-json": "*",
89
"pocketmine/binaryutils": "^0.2.0",
910
"pocketmine/color": "^0.2.0 || ^0.3.0",

src/ActorEventPacket.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515
namespace pocketmine\network\mcpe\protocol;
1616

17-
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
17+
use pmmp\encoding\Byte;
18+
use pmmp\encoding\ByteBufferReader;
19+
use pmmp\encoding\ByteBufferWriter;
20+
use pmmp\encoding\VarInt;
21+
use pocketmine\network\mcpe\protocol\serializer\CommonTypes;
1822
use pocketmine\network\mcpe\protocol\types\ActorEvent;
1923

2024
class ActorEventPacket extends DataPacket implements ClientboundPacket, ServerboundPacket{
@@ -36,16 +40,16 @@ public static function create(int $actorRuntimeId, int $eventId, int $eventData)
3640
return $result;
3741
}
3842

39-
protected function decodePayload(PacketSerializer $in) : void{
40-
$this->actorRuntimeId = $in->getActorRuntimeId();
41-
$this->eventId = $in->getByte();
42-
$this->eventData = $in->getVarInt();
43+
protected function decodePayload(ByteBufferReader $in) : void{
44+
$this->actorRuntimeId = CommonTypes::getActorRuntimeId($in);
45+
$this->eventId = Byte::readUnsigned($in);
46+
$this->eventData = VarInt::readSignedInt($in);
4347
}
4448

45-
protected function encodePayload(PacketSerializer $out) : void{
46-
$out->putActorRuntimeId($this->actorRuntimeId);
47-
$out->putByte($this->eventId);
48-
$out->putVarInt($this->eventData);
49+
protected function encodePayload(ByteBufferWriter $out) : void{
50+
CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId);
51+
Byte::writeUnsigned($out, $this->eventId);
52+
VarInt::writeSignedInt($out, $this->eventData);
4953
}
5054

5155
public function handle(PacketHandlerInterface $handler) : bool{

src/ActorPickRequestPacket.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515
namespace pocketmine\network\mcpe\protocol;
1616

17-
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
17+
use pmmp\encoding\Byte;
18+
use pmmp\encoding\ByteBufferReader;
19+
use pmmp\encoding\ByteBufferWriter;
20+
use pmmp\encoding\LE;
21+
use pocketmine\network\mcpe\protocol\serializer\CommonTypes;
1822

1923
class ActorPickRequestPacket extends DataPacket implements ServerboundPacket{
2024
public const NETWORK_ID = ProtocolInfo::ACTOR_PICK_REQUEST_PACKET;
@@ -34,16 +38,16 @@ public static function create(int $actorUniqueId, int $hotbarSlot, bool $addUser
3438
return $result;
3539
}
3640

37-
protected function decodePayload(PacketSerializer $in) : void{
38-
$this->actorUniqueId = $in->getLLong();
39-
$this->hotbarSlot = $in->getByte();
40-
$this->addUserData = $in->getBool();
41+
protected function decodePayload(ByteBufferReader $in) : void{
42+
$this->actorUniqueId = LE::readSignedLong($in);
43+
$this->hotbarSlot = Byte::readUnsigned($in);
44+
$this->addUserData = CommonTypes::getBool($in);
4145
}
4246

43-
protected function encodePayload(PacketSerializer $out) : void{
44-
$out->putLLong($this->actorUniqueId);
45-
$out->putByte($this->hotbarSlot);
46-
$out->putBool($this->addUserData);
47+
protected function encodePayload(ByteBufferWriter $out) : void{
48+
LE::writeSignedLong($out, $this->actorUniqueId);
49+
Byte::writeUnsigned($out, $this->hotbarSlot);
50+
CommonTypes::putBool($out, $this->addUserData);
4751
}
4852

4953
public function handle(PacketHandlerInterface $handler) : bool{

src/AddActorPacket.php

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414

1515
namespace pocketmine\network\mcpe\protocol;
1616

17+
use pmmp\encoding\ByteBufferReader;
18+
use pmmp\encoding\ByteBufferWriter;
19+
use pmmp\encoding\LE;
20+
use pmmp\encoding\VarInt;
1721
use pocketmine\math\Vector3;
18-
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
22+
use pocketmine\network\mcpe\protocol\serializer\CommonTypes;
1923
use pocketmine\network\mcpe\protocol\types\entity\Attribute;
2024
use pocketmine\network\mcpe\protocol\types\entity\EntityLink;
2125
use pocketmine\network\mcpe\protocol\types\entity\MetadataProperty;
@@ -85,60 +89,60 @@ public static function create(
8589
return $result;
8690
}
8791

88-
protected function decodePayload(PacketSerializer $in) : void{
89-
$this->actorUniqueId = $in->getActorUniqueId();
90-
$this->actorRuntimeId = $in->getActorRuntimeId();
91-
$this->type = $in->getString();
92-
$this->position = $in->getVector3();
93-
$this->motion = $in->getVector3();
94-
$this->pitch = $in->getLFloat();
95-
$this->yaw = $in->getLFloat();
96-
$this->headYaw = $in->getLFloat();
97-
$this->bodyYaw = $in->getLFloat();
98-
99-
$attrCount = $in->getUnsignedVarInt();
92+
protected function decodePayload(ByteBufferReader $in) : void{
93+
$this->actorUniqueId = CommonTypes::getActorUniqueId($in);
94+
$this->actorRuntimeId = CommonTypes::getActorRuntimeId($in);
95+
$this->type = CommonTypes::getString($in);
96+
$this->position = CommonTypes::getVector3($in);
97+
$this->motion = CommonTypes::getVector3($in);
98+
$this->pitch = LE::readFloat($in);
99+
$this->yaw = LE::readFloat($in);
100+
$this->headYaw = LE::readFloat($in);
101+
$this->bodyYaw = LE::readFloat($in);
102+
103+
$attrCount = VarInt::readUnsignedInt($in);
100104
for($i = 0; $i < $attrCount; ++$i){
101-
$id = $in->getString();
102-
$min = $in->getLFloat();
103-
$current = $in->getLFloat();
104-
$max = $in->getLFloat();
105+
$id = CommonTypes::getString($in);
106+
$min = LE::readFloat($in);
107+
$current = LE::readFloat($in);
108+
$max = LE::readFloat($in);
105109
$this->attributes[] = new Attribute($id, $min, $max, $current, $current, []);
106110
}
107111

108-
$this->metadata = $in->getEntityMetadata();
112+
$this->metadata = CommonTypes::getEntityMetadata($in);
109113
$this->syncedProperties = PropertySyncData::read($in);
110114

111-
$linkCount = $in->getUnsignedVarInt();
115+
$linkCount = VarInt::readUnsignedInt($in);
112116
for($i = 0; $i < $linkCount; ++$i){
113-
$this->links[] = $in->getEntityLink();
117+
$this->links[] = CommonTypes::getEntityLink($in);
114118
}
115119
}
116120

117-
protected function encodePayload(PacketSerializer $out) : void{
118-
$out->putActorUniqueId($this->actorUniqueId);
119-
$out->putActorRuntimeId($this->actorRuntimeId);
120-
$out->putString($this->type);
121-
$out->putVector3($this->position);
122-
$out->putVector3Nullable($this->motion);
123-
$out->putLFloat($this->pitch);
124-
$out->putLFloat($this->yaw);
125-
$out->putLFloat($this->headYaw);
126-
$out->putLFloat($this->bodyYaw);
127-
128-
$out->putUnsignedVarInt(count($this->attributes));
121+
protected function encodePayload(ByteBufferWriter $out) : void{
122+
CommonTypes::putActorUniqueId($out, $this->actorUniqueId);
123+
CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId);
124+
CommonTypes::putString($out, $this->type);
125+
CommonTypes::putVector3($out, $this->position);
126+
CommonTypes::putVector3Nullable($out, $this->motion);
127+
LE::writeFloat($out, $this->pitch);
128+
LE::writeFloat($out, $this->yaw);
129+
LE::writeFloat($out, $this->headYaw);
130+
LE::writeFloat($out, $this->bodyYaw);
131+
132+
VarInt::writeUnsignedInt($out, count($this->attributes));
129133
foreach($this->attributes as $attribute){
130-
$out->putString($attribute->getId());
131-
$out->putLFloat($attribute->getMin());
132-
$out->putLFloat($attribute->getCurrent());
133-
$out->putLFloat($attribute->getMax());
134+
CommonTypes::putString($out, $attribute->getId());
135+
LE::writeFloat($out, $attribute->getMin());
136+
LE::writeFloat($out, $attribute->getCurrent());
137+
LE::writeFloat($out, $attribute->getMax());
134138
}
135139

136-
$out->putEntityMetadata($this->metadata);
140+
CommonTypes::putEntityMetadata($out, $this->metadata);
137141
$this->syncedProperties->write($out);
138142

139-
$out->putUnsignedVarInt(count($this->links));
143+
VarInt::writeUnsignedInt($out, count($this->links));
140144
foreach($this->links as $link){
141-
$out->putEntityLink($link);
145+
CommonTypes::putEntityLink($out, $link);
142146
}
143147
}
144148

src/AddBehaviorTreePacket.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
namespace pocketmine\network\mcpe\protocol;
1616

17-
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
17+
use pmmp\encoding\ByteBufferReader;
18+
use pmmp\encoding\ByteBufferWriter;
19+
use pocketmine\network\mcpe\protocol\serializer\CommonTypes;
1820

1921
class AddBehaviorTreePacket extends DataPacket implements ClientboundPacket{
2022
public const NETWORK_ID = ProtocolInfo::ADD_BEHAVIOR_TREE_PACKET;
@@ -30,12 +32,12 @@ public static function create(string $behaviorTreeJson) : self{
3032
return $result;
3133
}
3234

33-
protected function decodePayload(PacketSerializer $in) : void{
34-
$this->behaviorTreeJson = $in->getString();
35+
protected function decodePayload(ByteBufferReader $in) : void{
36+
$this->behaviorTreeJson = CommonTypes::getString($in);
3537
}
3638

37-
protected function encodePayload(PacketSerializer $out) : void{
38-
$out->putString($this->behaviorTreeJson);
39+
protected function encodePayload(ByteBufferWriter $out) : void{
40+
CommonTypes::putString($out, $this->behaviorTreeJson);
3941
}
4042

4143
public function handle(PacketHandlerInterface $handler) : bool{

src/AddItemActorPacket.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414

1515
namespace pocketmine\network\mcpe\protocol;
1616

17+
use pmmp\encoding\ByteBufferReader;
18+
use pmmp\encoding\ByteBufferWriter;
1719
use pocketmine\math\Vector3;
18-
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
20+
use pocketmine\network\mcpe\protocol\serializer\CommonTypes;
1921
use pocketmine\network\mcpe\protocol\types\entity\MetadataProperty;
2022
use pocketmine\network\mcpe\protocol\types\inventory\ItemStackWrapper;
2123

@@ -59,24 +61,24 @@ public static function create(
5961
return $result;
6062
}
6163

62-
protected function decodePayload(PacketSerializer $in) : void{
63-
$this->actorUniqueId = $in->getActorUniqueId();
64-
$this->actorRuntimeId = $in->getActorRuntimeId();
65-
$this->item = $in->getItemStackWrapper();
66-
$this->position = $in->getVector3();
67-
$this->motion = $in->getVector3();
68-
$this->metadata = $in->getEntityMetadata();
69-
$this->isFromFishing = $in->getBool();
64+
protected function decodePayload(ByteBufferReader $in) : void{
65+
$this->actorUniqueId = CommonTypes::getActorUniqueId($in);
66+
$this->actorRuntimeId = CommonTypes::getActorRuntimeId($in);
67+
$this->item = CommonTypes::getItemStackWrapper($in);
68+
$this->position = CommonTypes::getVector3($in);
69+
$this->motion = CommonTypes::getVector3($in);
70+
$this->metadata = CommonTypes::getEntityMetadata($in);
71+
$this->isFromFishing = CommonTypes::getBool($in);
7072
}
7173

72-
protected function encodePayload(PacketSerializer $out) : void{
73-
$out->putActorUniqueId($this->actorUniqueId);
74-
$out->putActorRuntimeId($this->actorRuntimeId);
75-
$out->putItemStackWrapper($this->item);
76-
$out->putVector3($this->position);
77-
$out->putVector3Nullable($this->motion);
78-
$out->putEntityMetadata($this->metadata);
79-
$out->putBool($this->isFromFishing);
74+
protected function encodePayload(ByteBufferWriter $out) : void{
75+
CommonTypes::putActorUniqueId($out, $this->actorUniqueId);
76+
CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId);
77+
CommonTypes::putItemStackWrapper($out, $this->item);
78+
CommonTypes::putVector3($out, $this->position);
79+
CommonTypes::putVector3Nullable($out, $this->motion);
80+
CommonTypes::putEntityMetadata($out, $this->metadata);
81+
CommonTypes::putBool($out, $this->isFromFishing);
8082
}
8183

8284
public function handle(PacketHandlerInterface $handler) : bool{

src/AddPaintingPacket.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414

1515
namespace pocketmine\network\mcpe\protocol;
1616

17+
use pmmp\encoding\ByteBufferReader;
18+
use pmmp\encoding\ByteBufferWriter;
19+
use pmmp\encoding\VarInt;
1720
use pocketmine\math\Vector3;
18-
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
21+
use pocketmine\network\mcpe\protocol\serializer\CommonTypes;
1922

2023
class AddPaintingPacket extends DataPacket implements ClientboundPacket{
2124
public const NETWORK_ID = ProtocolInfo::ADD_PAINTING_PACKET;
@@ -39,20 +42,20 @@ public static function create(int $actorUniqueId, int $actorRuntimeId, Vector3 $
3942
return $result;
4043
}
4144

42-
protected function decodePayload(PacketSerializer $in) : void{
43-
$this->actorUniqueId = $in->getActorUniqueId();
44-
$this->actorRuntimeId = $in->getActorRuntimeId();
45-
$this->position = $in->getVector3();
46-
$this->direction = $in->getVarInt();
47-
$this->title = $in->getString();
45+
protected function decodePayload(ByteBufferReader $in) : void{
46+
$this->actorUniqueId = CommonTypes::getActorUniqueId($in);
47+
$this->actorRuntimeId = CommonTypes::getActorRuntimeId($in);
48+
$this->position = CommonTypes::getVector3($in);
49+
$this->direction = VarInt::readSignedInt($in);
50+
$this->title = CommonTypes::getString($in);
4851
}
4952

50-
protected function encodePayload(PacketSerializer $out) : void{
51-
$out->putActorUniqueId($this->actorUniqueId);
52-
$out->putActorRuntimeId($this->actorRuntimeId);
53-
$out->putVector3($this->position);
54-
$out->putVarInt($this->direction);
55-
$out->putString($this->title);
53+
protected function encodePayload(ByteBufferWriter $out) : void{
54+
CommonTypes::putActorUniqueId($out, $this->actorUniqueId);
55+
CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId);
56+
CommonTypes::putVector3($out, $this->position);
57+
VarInt::writeSignedInt($out, $this->direction);
58+
CommonTypes::putString($out, $this->title);
5659
}
5760

5861
public function handle(PacketHandlerInterface $handler) : bool{

0 commit comments

Comments
 (0)