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