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