|
| 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 | + |
| 22 | +declare(strict_types=1); |
| 23 | + |
| 24 | +namespace pocketmine\network\mcpe\protocol\tools\generate_command_parameter_types; |
| 25 | + |
| 26 | +use function count; |
| 27 | +use function file_get_contents; |
| 28 | +use function fwrite; |
| 29 | +use function strtoupper; |
| 30 | +use function uasort; |
| 31 | + |
| 32 | +if(count($argv) !== 2){ |
| 33 | + echo "Usage: php generate-command-parameter-types.php <input-file> \n"; |
| 34 | + echo "Hint: Input file is a JSON file like command_arg_types.json in pmmp/BedrockData\n"; |
| 35 | + exit(1); |
| 36 | +} |
| 37 | + |
| 38 | +$jsonRaw = file_get_contents($argv[1]); |
| 39 | +if($jsonRaw === false){ |
| 40 | + echo "Failed to read input file $argv[1]\n"; |
| 41 | + exit(1); |
| 42 | +} |
| 43 | + |
| 44 | +$list = json_decode($jsonRaw, true, flags: JSON_THROW_ON_ERROR); |
| 45 | +if(!is_array($list)){ |
| 46 | + echo "Failed to decode input file $argv[1], expected a JSON object\n"; |
| 47 | + exit(1); |
| 48 | +} |
| 49 | + |
| 50 | +$output = fopen(dirname(__DIR__) . "/src/types/command/CommandParameterTypes.php", "wb"); |
| 51 | +if($output === false){ |
| 52 | + throw new \RuntimeException("Failed to open output file"); |
| 53 | +} |
| 54 | + |
| 55 | +fwrite($output, <<<'CODE' |
| 56 | +<?php |
| 57 | +
|
| 58 | +/* |
| 59 | + * This file is part of BedrockProtocol. |
| 60 | + * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol> |
| 61 | + * |
| 62 | + * BedrockProtocol is free software: you can redistribute it and/or modify |
| 63 | + * it under the terms of the GNU Lesser General Public License as published by |
| 64 | + * the Free Software Foundation, either version 3 of the License, or |
| 65 | + * (at your option) any later version. |
| 66 | + */ |
| 67 | +
|
| 68 | +declare(strict_types=1); |
| 69 | +
|
| 70 | +namespace pocketmine\network\mcpe\protocol\types\command; |
| 71 | +
|
| 72 | +/** |
| 73 | + * This file is automatically generated; do NOT edit it by hand. |
| 74 | + * Regenerate it by running tools/generate-command-parameter-types.php |
| 75 | + */ |
| 76 | +final class CommandParameterTypes{ |
| 77 | +
|
| 78 | + private function __construct(){ |
| 79 | + //NOOP |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | +CODE); |
| 84 | + |
| 85 | +uasort($list, function(array $left, array $right) : int{ |
| 86 | + return $left["id"] <=> $right["id"]; |
| 87 | +}); |
| 88 | + |
| 89 | +foreach($list as $name => $properties){ |
| 90 | + if($properties["description"] === "unknown"){ |
| 91 | + echo "Skipping $name - description unknown, assuming internal type\n"; |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + fwrite($output, "\tpublic const " . strtoupper($name) . " = " . $properties["id"] . "; // " . $properties["description"] . "\n"); |
| 96 | +} |
| 97 | + |
| 98 | +fwrite($output, "}\n"); |
| 99 | +fclose($output); |
| 100 | + |
| 101 | +echo "Done. Don't forget to run CS fixer after generating code.\n"; |
0 commit comments