PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
ResourcePacksPacketHandler.php
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
22declare(strict_types=1);
23
24namespace pocketmine\network\mcpe\handler;
25
40use Ramsey\Uuid\Uuid;
41use function array_keys;
42use function array_map;
43use function ceil;
44use function count;
45use function implode;
46use function strpos;
47use function strtolower;
48use function substr;
49
55 private const PACK_CHUNK_SIZE = 256 * 1024; //256KB
56
61 private const MAX_CONCURRENT_CHUNK_REQUESTS = 1;
62
67 private array $resourcePacksById = [];
68
70 private array $downloadedChunks = [];
71
73 private \SplQueue $requestQueue;
74
75 private int $activeRequests = 0;
76
85 public function __construct(
86 private NetworkSession $session,
87 private array $resourcePackStack,
88 private array $encryptionKeys,
89 private bool $mustAccept,
90 private \Closure $completionCallback
91 ){
92 $this->requestQueue = new \SplQueue();
93 foreach($resourcePackStack as $pack){
94 $this->resourcePacksById[$pack->getPackId()] = $pack;
95 }
96 }
97
98 private function getPackById(string $id) : ?ResourcePack{
99 return $this->resourcePacksById[strtolower($id)] ?? null;
100 }
101
102 public function setUp() : void{
103 $resourcePackEntries = array_map(function(ResourcePack $pack) : ResourcePackInfoEntry{
104 //TODO: more stuff
105
106 return new ResourcePackInfoEntry(
107 Uuid::fromString($pack->getPackId()),
108 $pack->getPackVersion(),
109 $pack->getPackSize(),
110 $this->encryptionKeys[$pack->getPackId()] ?? "",
111 "",
112 $pack->getPackId(),
113 false
114 );
115 }, $this->resourcePackStack);
116 //TODO: support forcing server packs
117 $this->session->sendDataPacket(ResourcePacksInfoPacket::create(
118 resourcePackEntries: $resourcePackEntries,
119 mustAccept: $this->mustAccept,
120 hasAddons: false,
121 hasScripts: false,
122 worldTemplateId: Uuid::fromString(Uuid::NIL),
123 worldTemplateVersion: ""
124 ));
125 $this->session->getLogger()->debug("Waiting for client to accept resource packs");
126 }
127
128 private function disconnectWithError(string $error) : void{
129 $this->session->disconnectWithError(
130 reason: "Error downloading resource packs: " . $error,
131 disconnectScreenMessage: KnownTranslationFactory::disconnectionScreen_resourcePack()
132 );
133 }
134
135 public function handleResourcePackClientResponse(ResourcePackClientResponsePacket $packet) : bool{
136 switch($packet->status){
137 case ResourcePackClientResponsePacket::STATUS_REFUSED:
138 //TODO: add lang strings for this
139 $this->session->disconnect("Refused resource packs", "You must accept resource packs to join this server.", true);
140 break;
141 case ResourcePackClientResponsePacket::STATUS_SEND_PACKS:
142 foreach($packet->packIds as $uuid){
143 //dirty hack for mojang's dirty hack for versions
144 $splitPos = strpos($uuid, "_");
145 if($splitPos !== false){
146 $uuid = substr($uuid, 0, $splitPos);
147 }
148 $pack = $this->getPackById($uuid);
149
150 if(!($pack instanceof ResourcePack)){
151 //Client requested a resource pack but we don't have it available on the server
152 $this->disconnectWithError("Unknown pack $uuid requested, available packs: " . implode(", ", array_keys($this->resourcePacksById)));
153 return false;
154 }
155
156 $this->session->sendDataPacket(ResourcePackDataInfoPacket::create(
157 $pack->getPackId(),
158 self::PACK_CHUNK_SIZE,
159 (int) ceil($pack->getPackSize() / self::PACK_CHUNK_SIZE),
160 $pack->getPackSize(),
161 $pack->getSha256(),
162 false,
163 ResourcePackType::RESOURCES //TODO: this might be an addon (not behaviour pack), needed to properly support client-side custom items
164 ));
165 }
166 $this->session->getLogger()->debug("Player requested download of " . count($packet->packIds) . " resource packs");
167
168 break;
169 case ResourcePackClientResponsePacket::STATUS_HAVE_ALL_PACKS:
170 $stack = array_map(static function(ResourcePack $pack) : ResourcePackStackEntry{
171 return new ResourcePackStackEntry($pack->getPackId(), $pack->getPackVersion(), ""); //TODO: subpacks
172 }, $this->resourcePackStack);
173
174 //we support chemistry blocks by default, the client should already have this installed
175 $stack[] = new ResourcePackStackEntry("0fba4063-dba1-4281-9b89-ff9390653530", "1.0.0", "");
176
177 //we don't force here, because it doesn't have user-facing effects
178 //but it does have an annoying side-effect when true: it makes
179 //the client remove its own non-server-supplied resource packs.
180 $this->session->sendDataPacket(ResourcePackStackPacket::create($stack, [], false, ProtocolInfo::MINECRAFT_VERSION_NETWORK, new Experiments([], false), false));
181 $this->session->getLogger()->debug("Applying resource pack stack");
182 break;
183 case ResourcePackClientResponsePacket::STATUS_COMPLETED:
184 $this->session->getLogger()->debug("Resource packs sequence completed");
185 ($this->completionCallback)();
186 break;
187 default:
188 return false;
189 }
190
191 return true;
192 }
193
194 public function handleResourcePackChunkRequest(ResourcePackChunkRequestPacket $packet) : bool{
195 $pack = $this->getPackById($packet->packId);
196 if(!($pack instanceof ResourcePack)){
197 $this->disconnectWithError("Invalid request for chunk $packet->chunkIndex of unknown pack $packet->packId, available packs: " . implode(", ", array_keys($this->resourcePacksById)));
198 return false;
199 }
200
201 $packId = $pack->getPackId(); //use this because case may be different
202
203 if(isset($this->downloadedChunks[$packId][$packet->chunkIndex])){
204 $this->disconnectWithError("Duplicate request for chunk $packet->chunkIndex of pack $packet->packId");
205 return false;
206 }
207
208 $offset = $packet->chunkIndex * self::PACK_CHUNK_SIZE;
209 if($offset < 0 || $offset >= $pack->getPackSize()){
210 $this->disconnectWithError("Invalid out-of-bounds request for chunk $packet->chunkIndex of $packet->packId: offset $offset, file size " . $pack->getPackSize());
211 return false;
212 }
213
214 if(!isset($this->downloadedChunks[$packId])){
215 $this->downloadedChunks[$packId] = [$packet->chunkIndex => true];
216 }else{
217 $this->downloadedChunks[$packId][$packet->chunkIndex] = true;
218 }
219
220 $this->requestQueue->enqueue([$pack, $packet->chunkIndex]);
221 $this->processChunkRequestQueue();
222
223 return true;
224 }
225
226 private function processChunkRequestQueue() : void{
227 if($this->activeRequests >= self::MAX_CONCURRENT_CHUNK_REQUESTS || $this->requestQueue->isEmpty()){
228 return;
229 }
234 [$pack, $chunkIndex] = $this->requestQueue->dequeue();
235
236 $packId = $pack->getPackId();
237 $offset = $chunkIndex * self::PACK_CHUNK_SIZE;
238 $chunkData = $pack->getPackChunk($offset, self::PACK_CHUNK_SIZE);
239 $this->activeRequests++;
240 $this->session
241 ->sendDataPacketWithReceipt(ResourcePackChunkDataPacket::create($packId, $chunkIndex, $offset, $chunkData))
242 ->onCompletion(
243 function() : void{
244 $this->activeRequests--;
245 $this->processChunkRequestQueue();
246 },
247 function() : void{
248 //this may have been rejected because of a disconnection - this will do nothing in that case
249 $this->disconnectWithError("Plugin interrupted sending of resource packs");
250 }
251 );
252 }
253}
__construct(private NetworkSession $session, private array $resourcePackStack, private array $encryptionKeys, private bool $mustAccept, private \Closure $completionCallback)