PocketMine-MP 5.39.3 git-21ae710729750cd637333d673bbbbbc598fc659e
Loading...
Searching...
No Matches
ResourcePackManager.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\resourcepacks;
25
29use Ramsey\Uuid\UuidInterface;
30use Symfony\Component\Filesystem\Path;
31use function array_map;
32use function copy;
33use function count;
34use function file_exists;
35use function gettype;
36use function is_array;
37use function is_dir;
38use function is_float;
39use function is_int;
40use function is_string;
41use function mkdir;
42use function rtrim;
43use function strlen;
44use function strtolower;
45use const DIRECTORY_SEPARATOR;
46
48 private string $path;
49 private bool $serverForceResources = false;
50
55 private array $resourcePacks = [];
56
61 private array $uuidList = [];
62
67 private array $encryptionKeys = [];
68
72 public function __construct(string $path, \Logger $logger){
73 $this->path = $path;
74
75 if(!file_exists($this->path)){
76 $logger->debug("Resource packs path $path does not exist, creating directory");
77 mkdir($this->path);
78 }elseif(!is_dir($this->path)){
79 throw new \InvalidArgumentException("Resource packs path $path exists and is not a directory");
80 }
81
82 $resourcePacksYml = Path::join($this->path, "resource_packs.yml");
83 if(!file_exists($resourcePacksYml)){
84 copy(Path::join(\pocketmine\RESOURCE_PATH, "resource_packs.yml"), $resourcePacksYml);
85 }
86
87 $resourcePacksConfig = new Config($resourcePacksYml, Config::YAML, []);
88
89 $this->serverForceResources = (bool) $resourcePacksConfig->get("force_resources", false);
90
91 $logger->info("Loading resource packs...");
92
93 $resourceStack = $resourcePacksConfig->get("resource_stack", []);
94 if(!is_array($resourceStack)){
95 throw new \InvalidArgumentException("\"resource_stack\" key should contain a list of pack names");
96 }
97
98 foreach(Utils::promoteKeys($resourceStack) as $pos => $pack){
99 if(!is_string($pack) && !is_int($pack) && !is_float($pack)){
100 $logger->critical("Found invalid entry in resource pack list at offset $pos of type " . gettype($pack));
101 continue;
102 }
103 $pack = (string) $pack;
104 try{
105 $newPack = $this->loadPackFromPath(Path::join($this->path, $pack));
106
107 $printableId = $newPack->getPackId()->toString();
108 $this->uuidList[$printableId] = $newPack;
109 $this->resourcePacks[] = $newPack;
110
111 $keyPath = Path::join($this->path, $pack . ".key");
112 if(file_exists($keyPath)){
113 try{
114 $key = Filesystem::fileGetContents($keyPath);
115 }catch(\RuntimeException $e){
116 throw new ResourcePackException("Could not read encryption key file: " . $e->getMessage(), 0, $e);
117 }
118 $key = rtrim($key, "\r\n");
119 if(strlen($key) !== 32){
120 throw new ResourcePackException("Invalid encryption key length, must be exactly 32 bytes");
121 }
122 $this->encryptionKeys[$printableId] = $key;
123 }
124 }catch(ResourcePackException $e){
125 $logger->critical("Could not load resource pack \"$pack\": " . $e->getMessage());
126 }
127 }
128
129 $logger->debug("Successfully loaded " . count($this->resourcePacks) . " resource packs");
130 }
131
132 private function loadPackFromPath(string $packPath) : ResourcePack{
133 if(!file_exists($packPath)){
134 throw new ResourcePackException("File or directory not found");
135 }
136 if(is_dir($packPath)){
137 throw new ResourcePackException("Directory resource packs are unsupported");
138 }
139
140 //Detect the type of resource pack.
141 $info = new \SplFileInfo($packPath);
142 switch($info->getExtension()){
143 case "zip":
144 case "mcpack":
145 return new ZippedResourcePack($packPath);
146 }
147
148 throw new ResourcePackException("Format not recognized");
149 }
150
154 public function getPath() : string{
155 return $this->path . DIRECTORY_SEPARATOR;
156 }
157
161 public function resourcePacksRequired() : bool{
162 return $this->serverForceResources;
163 }
164
168 public function setResourcePacksRequired(bool $value) : void{
169 $this->serverForceResources = $value;
170 }
171
177 public function getResourceStack() : array{
178 return $this->resourcePacks;
179 }
180
189 public function setResourceStack(array $resourceStack) : void{
190 $uuidList = [];
191 $resourcePacks = [];
192 foreach($resourceStack as $pack){
193 $printableId = $pack->getPackId()->toString();
194 if(isset($uuidList[$printableId])){
195 throw new \InvalidArgumentException("Cannot load two resource pack with the same UUID ($printableId)");
196 }
197 $uuidList[$printableId] = $pack;
198 $resourcePacks[] = $pack;
199 }
200 $this->resourcePacks = $resourcePacks;
201 $this->uuidList = $uuidList;
202 }
203
207 public function getPackById(UuidInterface|string $id) : ?ResourcePack{
208 return $this->uuidList[$id instanceof UuidInterface ? $id->toString() : strtolower($id)] ?? null;
209 }
210
215 public function getPackIdList() : array{
216 return array_map(fn(ResourcePack $p) => $p->getPackId(), $this->uuidList);
217 }
218
222 public function getPackEncryptionKey(UuidInterface $id) : ?string{
223 return $this->encryptionKeys[$id->toString()] ?? null;
224 }
225
230 public function setPackEncryptionKey(UuidInterface $id, ?string $key) : void{
231 $printableId = $id->toString();
232 if($key === null){
233 //allow deprovisioning keys for resource packs that have been removed
234 unset($this->encryptionKeys[$printableId]);
235 }elseif(isset($this->uuidList[$printableId])){
236 if(strlen($key) !== 32){
237 throw new \InvalidArgumentException("Encryption key must be exactly 32 bytes long");
238 }
239 $this->encryptionKeys[$printableId] = $key;
240 }else{
241 throw new \InvalidArgumentException("Unknown pack ID $printableId");
242 }
243 }
244}
setPackEncryptionKey(UuidInterface $id, ?string $key)
debug($message)
info($message)
critical($message)