PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
PluginDescription.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\plugin;
25
30use function array_map;
31use function array_values;
32use function get_debug_type;
33use function is_array;
34use function is_string;
35use function preg_match;
36use function str_replace;
37use function stripos;
38use function yaml_parse;
39
41 private const KEY_NAME = "name";
42 private const KEY_VERSION = "version";
43 private const KEY_MAIN = "main";
44 private const KEY_SRC_NAMESPACE_PREFIX = "src-namespace-prefix";
45 private const KEY_API = "api";
46 private const KEY_MCPE_PROTOCOL = "mcpe-protocol";
47 private const KEY_OS = "os";
48 private const KEY_DEPEND = "depend";
49 private const KEY_SOFTDEPEND = "softdepend";
50 private const KEY_LOADBEFORE = "loadbefore";
51 private const KEY_EXTENSIONS = "extensions";
52 private const KEY_WEBSITE = "website";
53 private const KEY_DESCRIPTION = "description";
54 private const KEY_LOGGER_PREFIX = "prefix";
55 private const KEY_LOAD = "load";
56 private const KEY_AUTHOR = "author";
57 private const KEY_AUTHORS = "authors";
58 private const KEY_PERMISSIONS = "permissions";
59
60 private const KEY_COMMANDS = "commands";
61 private const KEY_COMMAND_PERMISSION = "permission";
62 private const KEY_COMMAND_DESCRIPTION = self::KEY_DESCRIPTION;
63 private const KEY_COMMAND_USAGE = "usage";
64 private const KEY_COMMAND_ALIASES = "aliases";
65 private const KEY_COMMAND_PERMISSION_MESSAGE = "permission-message";
66
71 private array $map;
72
73 private string $name;
74 private string $main;
75 private string $srcNamespacePrefix = "";
77 private array $api;
79 private array $compatibleMcpeProtocols = [];
81 private array $compatibleOperatingSystems = [];
86 private array $extensions = [];
88 private array $depend = [];
90 private array $softDepend = [];
92 private array $loadBefore = [];
93 private string $version;
98 private array $commands = [];
99 private string $description = "";
101 private array $authors = [];
102 private string $website = "";
103 private string $prefix = "";
104 private PluginEnableOrder $order;
105
110 private array $permissions = [];
111
115 public function __construct(array|string $yamlString){
116 if(is_string($yamlString)){
117 $map = yaml_parse($yamlString);
118 if($map === false){
119 throw new PluginDescriptionParseException("YAML parsing error in plugin manifest");
120 }
121 if(!is_array($map)){
122 throw new PluginDescriptionParseException("Invalid structure of plugin manifest, expected array but have " . get_debug_type($map));
123 }
124 }else{
125 $map = $yamlString;
126 }
127 $this->loadMap($map);
128 }
129
134 private function loadMap(array $plugin) : void{
135 $this->map = $plugin;
136
137 $this->name = $plugin[self::KEY_NAME];
138 if(preg_match('/^[A-Za-z0-9 _.-]+$/', $this->name) === 0){
139 throw new PluginDescriptionParseException("Invalid Plugin name");
140 }
141 $this->name = str_replace(" ", "_", $this->name);
142 $this->version = (string) $plugin[self::KEY_VERSION];
143 $this->main = $plugin[self::KEY_MAIN];
144 if(stripos($this->main, "pocketmine\\") === 0){
145 throw new PluginDescriptionParseException("Invalid Plugin main, cannot start within the PocketMine namespace");
146 }
147
148 $this->srcNamespacePrefix = $plugin[self::KEY_SRC_NAMESPACE_PREFIX] ?? "";
149
150 $this->api = array_map("\strval", (array) ($plugin[self::KEY_API] ?? []));
151 $this->compatibleMcpeProtocols = array_map("\intval", (array) ($plugin[self::KEY_MCPE_PROTOCOL] ?? []));
152 $this->compatibleOperatingSystems = array_map("\strval", (array) ($plugin[self::KEY_OS] ?? []));
153
154 if(isset($plugin[self::KEY_COMMANDS]) && is_array($plugin[self::KEY_COMMANDS])){
155 foreach(Utils::promoteKeys($plugin[self::KEY_COMMANDS]) as $commandName => $commandData){
156 if(!is_string($commandName)){
157 throw new PluginDescriptionParseException("Invalid Plugin commands, key must be the name of the command");
158 }
159 if(!is_array($commandData)){
160 throw new PluginDescriptionParseException("Command $commandName has invalid properties");
161 }
162 if(!isset($commandData[self::KEY_COMMAND_PERMISSION]) || !is_string($commandData[self::KEY_COMMAND_PERMISSION])){
163 throw new PluginDescriptionParseException("Command $commandName does not have a valid permission set");
164 }
165 $this->commands[$commandName] = new PluginDescriptionCommandEntry(
166 $commandData[self::KEY_COMMAND_DESCRIPTION] ?? null,
167 $commandData[self::KEY_COMMAND_USAGE] ?? null,
168 $commandData[self::KEY_COMMAND_ALIASES] ?? [],
169 $commandData[self::KEY_COMMAND_PERMISSION],
170 $commandData[self::KEY_COMMAND_PERMISSION_MESSAGE] ?? null
171 );
172 }
173 }
174
175 if(isset($plugin[self::KEY_DEPEND])){
176 $this->depend = (array) $plugin[self::KEY_DEPEND];
177 }
178 if(isset($plugin[self::KEY_EXTENSIONS])){
179 $extensions = (array) $plugin[self::KEY_EXTENSIONS];
180 $isLinear = $extensions === array_values($extensions);
181 foreach(Utils::promoteKeys($extensions) as $k => $v){
182 if($isLinear){
183 $k = $v;
184 $v = "*";
185 }
186 $this->extensions[(string) $k] = array_map('strval', is_array($v) ? $v : [$v]);
187 }
188 }
189
190 $this->softDepend = (array) ($plugin[self::KEY_SOFTDEPEND] ?? $this->softDepend);
191
192 $this->loadBefore = (array) ($plugin[self::KEY_LOADBEFORE] ?? $this->loadBefore);
193
194 $this->website = (string) ($plugin[self::KEY_WEBSITE] ?? $this->website);
195
196 $this->description = (string) ($plugin[self::KEY_DESCRIPTION] ?? $this->description);
197
198 $this->prefix = (string) ($plugin[self::KEY_LOGGER_PREFIX] ?? $this->prefix);
199
200 if(isset($plugin[self::KEY_LOAD])){
201 $order = PluginEnableOrder::fromString($plugin[self::KEY_LOAD]);
202 if($order === null){
203 throw new PluginDescriptionParseException("Invalid Plugin \"" . self::KEY_LOAD . "\"");
204 }
205 $this->order = $order;
206 }else{
207 $this->order = PluginEnableOrder::POSTWORLD;
208 }
209
210 $this->authors = [];
211 if(isset($plugin[self::KEY_AUTHOR])){
212 if(is_array($plugin[self::KEY_AUTHOR])){
213 $this->authors = $plugin[self::KEY_AUTHOR];
214 }else{
215 $this->authors[] = $plugin[self::KEY_AUTHOR];
216 }
217 }
218 if(isset($plugin[self::KEY_AUTHORS])){
219 foreach($plugin[self::KEY_AUTHORS] as $author){
220 $this->authors[] = $author;
221 }
222 }
223
224 if(isset($plugin[self::KEY_PERMISSIONS])){
225 try{
226 $this->permissions = PermissionParser::loadPermissions($plugin[self::KEY_PERMISSIONS]);
227 }catch(PermissionParserException $e){
228 throw new PluginDescriptionParseException("Invalid Plugin \"" . self::KEY_PERMISSIONS . "\": " . $e->getMessage(), 0, $e);
229 }
230 }
231 }
232
233 public function getFullName() : string{
234 return $this->name . " v" . $this->version;
235 }
236
240 public function getCompatibleApis() : array{
241 return $this->api;
242 }
243
247 public function getCompatibleMcpeProtocols() : array{
248 return $this->compatibleMcpeProtocols;
249 }
250
254 public function getCompatibleOperatingSystems() : array{
255 return $this->compatibleOperatingSystems;
256 }
257
261 public function getAuthors() : array{
262 return $this->authors;
263 }
264
265 public function getPrefix() : string{
266 return $this->prefix;
267 }
268
273 public function getCommands() : array{
274 return $this->commands;
275 }
276
281 public function getRequiredExtensions() : array{
282 return $this->extensions;
283 }
284
288 public function getDepend() : array{
289 return $this->depend;
290 }
291
292 public function getDescription() : string{
293 return $this->description;
294 }
295
299 public function getLoadBefore() : array{
300 return $this->loadBefore;
301 }
302
303 public function getMain() : string{
304 return $this->main;
305 }
306
307 public function getSrcNamespacePrefix() : string{ return $this->srcNamespacePrefix; }
308
309 public function getName() : string{
310 return $this->name;
311 }
312
313 public function getOrder() : PluginEnableOrder{
314 return $this->order;
315 }
316
321 public function getPermissions() : array{
322 return $this->permissions;
323 }
324
328 public function getSoftDepend() : array{
329 return $this->softDepend;
330 }
331
332 public function getVersion() : string{
333 return $this->version;
334 }
335
336 public function getWebsite() : string{
337 return $this->website;
338 }
339
344 public function getMap() : array{
345 return $this->map;
346 }
347}
__construct(array|string $yamlString)