Deglobalize ItemTypeDictionary usage, at least for the protocol

while this is a bit hacky outside of the protocol namespace, it makes it much easier to use the protocol library for alternative purposes, such as for a client or MITM proxy.
It also removes all but one remaining core dependency of the protocol library, making it very close to being able to be separated from the server core entirely.
This commit is contained in:
Dylan K. Taylor
2021-07-14 14:26:32 +01:00
parent 0c2917f2aa
commit 1ad38d499c
12 changed files with 108 additions and 36 deletions

View File

@ -24,7 +24,9 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe\protocol;
use PHPUnit\Framework\TestCase;
use pocketmine\network\mcpe\convert\GlobalItemTypeDictionary;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializerContext;
class DataPacketTest extends TestCase{
@ -32,11 +34,13 @@ class DataPacketTest extends TestCase{
$pk = new TestPacket();
$pk->senderSubId = 3;
$pk->recipientSubId = 2;
$serializer = new PacketSerializer();
$context = new PacketSerializerContext(GlobalItemTypeDictionary::getInstance()->getDictionary());
$serializer = PacketSerializer::encoder($context);
$pk->encode($serializer);
$pk2 = new TestPacket();
$pk2->decode(new PacketSerializer($serializer->getBuffer()));
$pk2->decode(PacketSerializer::decoder($serializer->getBuffer(), 0, $context));
self::assertSame($pk2->senderSubId, 3);
self::assertSame($pk2->recipientSubId, 2);
}

View File

@ -24,18 +24,21 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe\protocol;
use PHPUnit\Framework\TestCase;
use pocketmine\network\mcpe\convert\GlobalItemTypeDictionary;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializerContext;
use function strlen;
class LoginPacketTest extends TestCase{
public function testInvalidChainDataJsonHandling() : void{
$stream = new PacketSerializer();
$context = new PacketSerializerContext(GlobalItemTypeDictionary::getInstance()->getDictionary());
$stream = PacketSerializer::encoder($context);
$stream->putUnsignedVarInt(ProtocolInfo::LOGIN_PACKET);
$payload = '{"chain":[]'; //intentionally malformed
$stream->putInt(ProtocolInfo::CURRENT_PROTOCOL);
$stream2 = new PacketSerializer();
$stream2 = PacketSerializer::encoder($context);
$stream2->putLInt(strlen($payload));
$stream2->put($payload);
$stream->putString($stream2->getBuffer());
@ -43,6 +46,6 @@ class LoginPacketTest extends TestCase{
$pk = PacketPool::getInstance()->getPacket($stream->getBuffer());
$this->expectException(PacketDecodeException::class);
$pk->decode(new PacketSerializer($stream->getBuffer())); //bang
$pk->decode(PacketSerializer::decoder($stream->getBuffer(), 0, $context)); //bang
}
}

View File

@ -24,9 +24,11 @@ declare(strict_types=1);
namespace pocketmine\mcpe\protocol\serializer;
use PHPUnit\Framework\TestCase;
use pocketmine\network\mcpe\convert\GlobalItemTypeDictionary;
use pocketmine\network\mcpe\protocol\PacketDecodeException;
use pocketmine\network\mcpe\protocol\PacketPool;
use pocketmine\network\mcpe\protocol\serializer\PacketBatch;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializerContext;
use pocketmine\network\mcpe\protocol\TestPacket;
use function array_fill;
@ -34,21 +36,23 @@ class PacketBatchTest extends TestCase{
public function testDecodeTooBig() : void{
$limit = 10;
$write = PacketBatch::fromPackets(...array_fill(0, $limit + 1, new TestPacket()));
$decoderContext = new PacketSerializerContext(GlobalItemTypeDictionary::getInstance()->getDictionary());
$write = PacketBatch::fromPackets($decoderContext, ...array_fill(0, $limit + 1, new TestPacket()));
$read = new PacketBatch($write->getBuffer());
$this->expectException(PacketDecodeException::class);
$readCount = 0;
foreach($read->getPackets(PacketPool::getInstance(), $limit) as $packet){
foreach($read->getPackets(PacketPool::getInstance(), $decoderContext, $limit) as $packet){
$readCount++;
}
}
public function testDecodeAtLimit() : void{
$limit = 10;
$write = PacketBatch::fromPackets(...array_fill(0, $limit, new TestPacket()));
$decoderContext = new PacketSerializerContext(GlobalItemTypeDictionary::getInstance()->getDictionary());
$write = PacketBatch::fromPackets($decoderContext, ...array_fill(0, $limit, new TestPacket()));
$read = new PacketBatch($write->getBuffer());
$readCount = 0;
foreach($read->getPackets(PacketPool::getInstance(), $limit) as $packet){
foreach($read->getPackets(PacketPool::getInstance(), $decoderContext, $limit) as $packet){
$readCount++;
}
self::assertSame($limit, $readCount);