Skip to content

Commit 25abb3a

Browse files
committed
Verify that all packets have a designated direction
1 parent db67098 commit 25abb3a

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

src/PacketPool.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
use pmmp\encoding\DataDecodeException;
1818
use pmmp\encoding\VarInt;
19+
use function array_filter;
20+
use function is_object;
1921

2022
class PacketPool{
2123
protected static ?PacketPool $instance = null;
@@ -273,4 +275,12 @@ public function getPacketById(int $pid) : ?Packet{
273275
public function getPacket(string $buffer) : ?Packet{
274276
return $this->getPacketById(VarInt::unpackUnsignedInt($buffer) & DataPacket::PID_MASK);
275277
}
278+
279+
/**
280+
* @return Packet[]
281+
* @phpstan-return array<int, Packet>
282+
*/
283+
public function getAll() : array{
284+
return array_filter($this->pool->toArray(), is_object(...));
285+
}
276286
}

tests/phpunit/PacketPoolTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
*
5+
* ____ _ _ __ __ _ __ __ ____
6+
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7+
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8+
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9+
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Lesser General Public License as published by
13+
* the Free Software Foundation, either version 3 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* @author PocketMine Team
17+
* @link http://www.pocketmine.net/
18+
*
19+
*
20+
*/
21+
22+
declare(strict_types=1);
23+
24+
namespace phpunit;
25+
26+
use PHPUnit\Framework\TestCase;
27+
use pocketmine\network\mcpe\protocol\ClientboundPacket;
28+
use pocketmine\network\mcpe\protocol\PacketPool;
29+
use pocketmine\network\mcpe\protocol\ServerboundPacket;
30+
31+
final class PacketPoolTest extends TestCase{
32+
33+
private PacketPool $pool;
34+
35+
public function setUp() : void{
36+
$this->pool = new PacketPool();
37+
}
38+
39+
public function testPacketDirectionDesignations() : void{
40+
foreach($this->pool->getAll() as $packet){
41+
self::assertTrue($packet instanceof ClientboundPacket || $packet instanceof ServerboundPacket, $packet->getName() . " must implement ClientboundPacket, ServerboundPacket, or both\n");
42+
}
43+
}
44+
}

tools/generate-protocol-info.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ interface PacketHandlerInterface{
180180
181181
use pmmp\encoding\DataDecodeException;
182182
use pmmp\encoding\VarInt;
183+
use function array_filter;
184+
use function is_object;
183185
184186
class PacketPool{
185187
protected static ?PacketPool $instance = null;
@@ -213,6 +215,14 @@ public function getPacketById(int $pid) : ?Packet{
213215
public function getPacket(string $buffer) : ?Packet{
214216
return $this->getPacketById(VarInt::unpackUnsignedInt($buffer) & DataPacket::PID_MASK);
215217
}
218+
219+
/**
220+
* @return Packet[]
221+
* @phpstan-return array<int, Packet>
222+
*/
223+
public function getAll() : array{
224+
return array_filter($this->pool->toArray(), is_object(...));
225+
}
216226
}
217227

218228
CODE;

0 commit comments

Comments
 (0)