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