[chunk index => hasSent] */ private $downloadedChunks = []; public function __construct(NetworkSession $session, ResourcePackManager $resourcePackManager){ $this->session = $session; $this->resourcePackManager = $resourcePackManager; } public function setUp() : void{ $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"); } private function disconnectWithError(string $error) : void{ $this->session->getLogger()->error("Error downloading resource packs: " . $error); $this->session->disconnect("disconnectionScreen.resourcePack"); } public function handleResourcePackClientResponse(ResourcePackClientResponsePacket $packet) : bool{ switch($packet->status){ case ResourcePackClientResponsePacket::STATUS_REFUSED: //TODO: add lang strings for this $this->session->disconnect("You must accept resource packs to join this server.", true); break; case ResourcePackClientResponsePacket::STATUS_SEND_PACKS: foreach($packet->packIds as $uuid){ //dirty hack for mojang's dirty hack for versions $splitPos = strpos($uuid, "_"); if($splitPos !== false){ $uuid = substr($uuid, 0, $splitPos); } $pack = $this->resourcePackManager->getPackById($uuid); if(!($pack instanceof ResourcePack)){ //Client requested a resource pack but we don't have it available on the server $this->disconnectWithError("Unknown pack $uuid requested, available packs: " . implode(", ", $this->resourcePackManager->getPackIdList())); return false; } $this->session->sendDataPacket(ResourcePackDataInfoPacket::create( $pack->getPackId(), self::PACK_CHUNK_SIZE, (int) ceil($pack->getPackSize() / self::PACK_CHUNK_SIZE), $pack->getPackSize(), $pack->getSha256() )); } $this->session->getLogger()->debug("Player requested download of " . count($packet->packIds) . " resource packs"); break; case ResourcePackClientResponsePacket::STATUS_HAVE_ALL_PACKS: $stack = array_map(static function(ResourcePack $pack){ return new ResourcePackStackEntry($pack->getPackId(), $pack->getPackVersion(), ""); //TODO: subpacks }, $this->resourcePackManager->getResourceStack()); //we support chemistry blocks by default, the client should already have this installed $stack[] = new ResourcePackStackEntry("0fba4063-dba1-4281-9b89-ff9390653530", "1.0.0", ""); //we don't force here, because it doesn't have user-facing effects //but it does have an annoying side-effect when true: it makes //the client remove its own non-server-supplied resource packs. $this->session->sendDataPacket(ResourcePackStackPacket::create($stack, [], false, false)); $this->session->getLogger()->debug("Applying resource pack stack"); break; case ResourcePackClientResponsePacket::STATUS_COMPLETED: $this->session->getLogger()->debug("Resource packs sequence completed"); $this->session->onResourcePacksDone(); break; default: return false; } return true; } public function handleResourcePackChunkRequest(ResourcePackChunkRequestPacket $packet) : bool{ $pack = $this->resourcePackManager->getPackById($packet->packId); if(!($pack instanceof ResourcePack)){ $this->disconnectWithError("Invalid request for chunk $packet->chunkIndex of unknown pack $packet->packId, available packs: " . implode(", ", $this->resourcePackManager->getPackIdList())); return false; } $packId = $pack->getPackId(); //use this because case may be different if(isset($this->downloadedChunks[$packId][$packet->chunkIndex])){ $this->disconnectWithError("Duplicate request for chunk $packet->chunkIndex of pack $packet->packId"); return false; } $offset = $packet->chunkIndex * self::PACK_CHUNK_SIZE; if($offset < 0 or $offset >= $pack->getPackSize()){ $this->disconnectWithError("Invalid out-of-bounds request for chunk $packet->chunkIndex of $packet->packId: offset $offset, file size " . $pack->getPackSize()); return false; } if(!isset($this->downloadedChunks[$packId])){ $this->downloadedChunks[$packId] = [$packet->chunkIndex => true]; }else{ $this->downloadedChunks[$packId][$packet->chunkIndex] = true; } $this->session->sendDataPacket(ResourcePackChunkDataPacket::create($packId, $packet->chunkIndex, $offset, $pack->getPackChunk($offset, self::PACK_CHUNK_SIZE))); return true; } }