From d87b6f9ff76ed0d3052770a35a204eecdc5512e6 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 8 Aug 2019 19:36:54 +0100 Subject: [PATCH] Remove resource pack packets dependency on ResourcePack, now supports decoding --- .../handler/ResourcePacksPacketHandler.php | 14 +- .../mcpe/protocol/ResourcePackStackPacket.php | 30 ++--- .../mcpe/protocol/ResourcePacksInfoPacket.php | 46 ++----- .../protocol/types/ResourcePackInfoEntry.php | 125 ++++++++++++++++++ .../types/ResourcePackStackEntry.php} | 47 +++++-- 5 files changed, 197 insertions(+), 65 deletions(-) create mode 100644 src/network/mcpe/protocol/types/ResourcePackInfoEntry.php rename src/{resourcepacks/ResourcePackInfoEntry.php => network/mcpe/protocol/types/ResourcePackStackEntry.php} (51%) diff --git a/src/network/mcpe/handler/ResourcePacksPacketHandler.php b/src/network/mcpe/handler/ResourcePacksPacketHandler.php index a6b3bd9ae..5fe682fc0 100644 --- a/src/network/mcpe/handler/ResourcePacksPacketHandler.php +++ b/src/network/mcpe/handler/ResourcePacksPacketHandler.php @@ -30,8 +30,11 @@ use pocketmine\network\mcpe\protocol\ResourcePackClientResponsePacket; use pocketmine\network\mcpe\protocol\ResourcePackDataInfoPacket; use pocketmine\network\mcpe\protocol\ResourcePacksInfoPacket; use pocketmine\network\mcpe\protocol\ResourcePackStackPacket; +use pocketmine\network\mcpe\protocol\types\ResourcePackInfoEntry; +use pocketmine\network\mcpe\protocol\types\ResourcePackStackEntry; use pocketmine\resourcepacks\ResourcePack; use pocketmine\resourcepacks\ResourcePackManager; +use function array_map; use function ceil; use function count; use function implode; @@ -60,7 +63,11 @@ class ResourcePacksPacketHandler extends PacketHandler{ } public function setUp() : void{ - $this->session->sendDataPacket(ResourcePacksInfoPacket::create($this->resourcePackManager->getResourceStack(), [], $this->resourcePackManager->resourcePacksRequired(), false)); + $resourcePackEntries = array_map(static function(ResourcePack $pack){ + //TODO: more stuff + return new ResourcePackInfoEntry($pack->getPackId(), $pack->getPackVersion(), $pack->getPackSize(), "", "", "", false); + }, $this->resourcePackManager->getResourceStack()); + $this->session->sendDataPacket(ResourcePacksInfoPacket::create($resourcePackEntries, [], $this->resourcePackManager->resourcePacksRequired(), false)); $this->session->getLogger()->debug("Waiting for client to accept resource packs"); } @@ -102,7 +109,10 @@ class ResourcePacksPacketHandler extends PacketHandler{ break; case ResourcePackClientResponsePacket::STATUS_HAVE_ALL_PACKS: - $this->session->sendDataPacket(ResourcePackStackPacket::create($this->resourcePackManager->getResourceStack(), [], $this->resourcePackManager->resourcePacksRequired(), false)); + $stack = array_map(static function(ResourcePack $pack){ + return new ResourcePackStackEntry($pack->getPackId(), $pack->getPackVersion(), ""); //TODO: subpacks + }, $this->resourcePackManager->getResourceStack()); + $this->session->sendDataPacket(ResourcePackStackPacket::create($stack, [], $this->resourcePackManager->resourcePacksRequired(), false)); $this->session->getLogger()->debug("Applying resource pack stack"); break; case ResourcePackClientResponsePacket::STATUS_COMPLETED: diff --git a/src/network/mcpe/protocol/ResourcePackStackPacket.php b/src/network/mcpe/protocol/ResourcePackStackPacket.php index ec8e604ea..958bdc75f 100644 --- a/src/network/mcpe/protocol/ResourcePackStackPacket.php +++ b/src/network/mcpe/protocol/ResourcePackStackPacket.php @@ -28,7 +28,7 @@ namespace pocketmine\network\mcpe\protocol; use pocketmine\network\mcpe\handler\PacketHandler; -use pocketmine\resourcepacks\ResourcePack; +use pocketmine\network\mcpe\protocol\types\ResourcePackStackEntry; use function count; class ResourcePackStackPacket extends DataPacket implements ClientboundPacket{ @@ -37,19 +37,19 @@ class ResourcePackStackPacket extends DataPacket implements ClientboundPacket{ /** @var bool */ public $mustAccept = false; - /** @var ResourcePack[] */ + /** @var ResourcePackStackEntry[] */ public $behaviorPackStack = []; - /** @var ResourcePack[] */ + /** @var ResourcePackStackEntry[] */ public $resourcePackStack = []; /** @var bool */ public $isExperimental = false; /** - * @param ResourcePack[] $resourcePacks - * @param ResourcePack[] $behaviorPacks - * @param bool $mustAccept - * @param bool $isExperimental + * @param ResourcePackStackEntry[] $resourcePacks + * @param ResourcePackStackEntry[] $behaviorPacks + * @param bool $mustAccept + * @param bool $isExperimental * * @return ResourcePackStackPacket */ @@ -66,16 +66,12 @@ class ResourcePackStackPacket extends DataPacket implements ClientboundPacket{ $this->mustAccept = $this->getBool(); $behaviorPackCount = $this->getUnsignedVarInt(); while($behaviorPackCount-- > 0){ - $this->getString(); - $this->getString(); - $this->getString(); + $this->behaviorPackStack[] = ResourcePackStackEntry::read($this); } $resourcePackCount = $this->getUnsignedVarInt(); while($resourcePackCount-- > 0){ - $this->getString(); - $this->getString(); - $this->getString(); + $this->resourcePackStack[] = ResourcePackStackEntry::read($this); } $this->isExperimental = $this->getBool(); @@ -86,16 +82,12 @@ class ResourcePackStackPacket extends DataPacket implements ClientboundPacket{ $this->putUnsignedVarInt(count($this->behaviorPackStack)); foreach($this->behaviorPackStack as $entry){ - $this->putString($entry->getPackId()); - $this->putString($entry->getPackVersion()); - $this->putString(""); //TODO: subpack name + $entry->write($this); } $this->putUnsignedVarInt(count($this->resourcePackStack)); foreach($this->resourcePackStack as $entry){ - $this->putString($entry->getPackId()); - $this->putString($entry->getPackVersion()); - $this->putString(""); //TODO: subpack name + $entry->write($this); } $this->putBool($this->isExperimental); diff --git a/src/network/mcpe/protocol/ResourcePacksInfoPacket.php b/src/network/mcpe/protocol/ResourcePacksInfoPacket.php index 5da110a68..6724d0b89 100644 --- a/src/network/mcpe/protocol/ResourcePacksInfoPacket.php +++ b/src/network/mcpe/protocol/ResourcePacksInfoPacket.php @@ -27,7 +27,7 @@ namespace pocketmine\network\mcpe\protocol; use pocketmine\network\mcpe\handler\PacketHandler; -use pocketmine\resourcepacks\ResourcePack; +use pocketmine\network\mcpe\protocol\types\ResourcePackInfoEntry; use function count; class ResourcePacksInfoPacket extends DataPacket implements ClientboundPacket{ @@ -37,16 +37,16 @@ class ResourcePacksInfoPacket extends DataPacket implements ClientboundPacket{ public $mustAccept = false; //if true, forces client to use selected resource packs /** @var bool */ public $hasScripts = false; //if true, causes disconnect for any platform that doesn't support scripts yet - /** @var ResourcePack[] */ + /** @var ResourcePackInfoEntry[] */ public $behaviorPackEntries = []; - /** @var ResourcePack[] */ + /** @var ResourcePackInfoEntry[] */ public $resourcePackEntries = []; /** - * @param ResourcePack[] $resourcePacks - * @param ResourcePack[] $behaviorPacks - * @param bool $mustAccept - * @param bool $hasScripts + * @param ResourcePackInfoEntry[] $resourcePacks + * @param ResourcePackInfoEntry[] $behaviorPacks + * @param bool $mustAccept + * @param bool $hasScripts * * @return ResourcePacksInfoPacket */ @@ -64,24 +64,12 @@ class ResourcePacksInfoPacket extends DataPacket implements ClientboundPacket{ $this->hasScripts = $this->getBool(); $behaviorPackCount = $this->getLShort(); while($behaviorPackCount-- > 0){ - $this->getString(); - $this->getString(); - $this->getLLong(); - $this->getString(); - $this->getString(); - $this->getString(); - $this->getBool(); + $this->behaviorPackEntries[] = ResourcePackInfoEntry::read($this); } $resourcePackCount = $this->getLShort(); while($resourcePackCount-- > 0){ - $this->getString(); - $this->getString(); - $this->getLLong(); - $this->getString(); - $this->getString(); - $this->getString(); - $this->getBool(); + $this->resourcePackEntries[] = ResourcePackInfoEntry::read($this); } } @@ -90,23 +78,11 @@ class ResourcePacksInfoPacket extends DataPacket implements ClientboundPacket{ $this->putBool($this->hasScripts); $this->putLShort(count($this->behaviorPackEntries)); foreach($this->behaviorPackEntries as $entry){ - $this->putString($entry->getPackId()); - $this->putString($entry->getPackVersion()); - $this->putLLong($entry->getPackSize()); - $this->putString(""); //TODO: encryption key - $this->putString(""); //TODO: subpack name - $this->putString(""); //TODO: content identity - $this->putBool(false); //TODO: has scripts (?) + $entry->write($this); } $this->putLShort(count($this->resourcePackEntries)); foreach($this->resourcePackEntries as $entry){ - $this->putString($entry->getPackId()); - $this->putString($entry->getPackVersion()); - $this->putLLong($entry->getPackSize()); - $this->putString(""); //TODO: encryption key - $this->putString(""); //TODO: subpack name - $this->putString(""); //TODO: content identity - $this->putBool(false); //TODO: seems useless for resource packs + $entry->write($this); } } diff --git a/src/network/mcpe/protocol/types/ResourcePackInfoEntry.php b/src/network/mcpe/protocol/types/ResourcePackInfoEntry.php new file mode 100644 index 000000000..f091b651f --- /dev/null +++ b/src/network/mcpe/protocol/types/ResourcePackInfoEntry.php @@ -0,0 +1,125 @@ +packId = $packId; + $this->version = $version; + $this->sizeBytes = $sizeBytes; + $this->encryptionKey = $encryptionKey; + $this->subPackName = $subPackName; + $this->contentId = $contentId; + $this->hasScripts = $hasScripts; + } + + /** + * @return string + */ + public function getPackId() : string{ + return $this->packId; + } + + /** + * @return string + */ + public function getVersion() : string{ + return $this->version; + } + + /** + * @return int + */ + public function getSizeBytes() : int{ + return $this->sizeBytes; + } + + /** + * @return string + */ + public function getEncryptionKey() : string{ + return $this->encryptionKey; + } + + /** + * @return string + */ + public function getSubPackName() : string{ + return $this->subPackName; + } + + /** + * @return string + */ + public function getContentId() : string{ + return $this->contentId; + } + + /** + * @return bool + */ + public function hasScripts() : bool{ + return $this->hasScripts; + } + + public function write(NetworkBinaryStream $out) : void{ + $out->putString($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); + } + + public static function read(NetworkBinaryStream $in) : self{ + return new self( + $uuid = $in->getString(), + $version = $in->getString(), + $sizeBytes = $in->getLLong(), + $encryptionKey = $in->getString(), + $subPackName = $in->getString(), + $contentId = $in->getString(), + $hasScripts = $in->getBool() + ); + } +} diff --git a/src/resourcepacks/ResourcePackInfoEntry.php b/src/network/mcpe/protocol/types/ResourcePackStackEntry.php similarity index 51% rename from src/resourcepacks/ResourcePackInfoEntry.php rename to src/network/mcpe/protocol/types/ResourcePackStackEntry.php index 3b6e7a3e9..689a698c1 100644 --- a/src/resourcepacks/ResourcePackInfoEntry.php +++ b/src/network/mcpe/protocol/types/ResourcePackStackEntry.php @@ -21,28 +21,57 @@ declare(strict_types=1); -namespace pocketmine\resourcepacks; +namespace pocketmine\network\mcpe\protocol\types; -class ResourcePackInfoEntry{ - protected $packId; //UUID - protected $version; - protected $packSize; +use pocketmine\network\mcpe\serializer\NetworkBinaryStream; - public function __construct(string $packId, string $version, int $packSize = 0){ +class ResourcePackStackEntry{ + + /** @var string */ + private $packId; + /** @var string */ + private $version; + /** @var string */ + private $subPackName; + + public function __construct(string $packId, string $version, string $subPackName){ $this->packId = $packId; $this->version = $version; - $this->packSize = $packSize; + $this->subPackName = $subPackName; } + /** + * @return string + */ public function getPackId() : string{ return $this->packId; } + /** + * @return string + */ public function getVersion() : string{ return $this->version; } - public function getPackSize() : int{ - return $this->packSize; + /** + * @return string + */ + public function getSubPackName() : string{ + return $this->subPackName; + } + + public function write(NetworkBinaryStream $out) : void{ + $out->putString($this->packId); + $out->putString($this->version); + $out->putString($this->subPackName); + } + + public static function read(NetworkBinaryStream $in) : self{ + return new self( + $packId = $in->getString(), + $version = $in->getString(), + $subPackName = $in->getString() + ); } }