PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
generate-entity-ids.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_entity_ids;
16
17use function count;
18use function dirname;
19use function explode;
20use function fclose;
21use function file_get_contents;
22use function fopen;
23use function fwrite;
24use function is_array;
25use function json_decode;
26use function ksort;
27use function strtoupper;
28
29if(count($argv) !== 2){
30 fwrite(STDERR, "Required arguments: path to entity ID mapping file\n");
31 fwrite(STDERR, "Hint: Input file is a JSON file like entity_id_map.json in pmmp/BedrockData\n");
32 exit(1);
33}
34
35$jsonRaw = file_get_contents($argv[1]);
36if($jsonRaw === false){
37 fwrite(STDERR, "Failed to read entity ID mapping file\n");
38 exit(1);
39}
40
41$list = json_decode($jsonRaw, true, flags: JSON_THROW_ON_ERROR);
42if(!is_array($list)){
43 fwrite(STDERR, "Failed to decode entity ID mapping file, expected a JSON object\n");
44 exit(1);
45}
46ksort($list, SORT_STRING);
47
48$output = fopen(dirname(__DIR__) . "/src/types/entity/EntityIds.php", "wb");
49if($output === false){
50 throw new \RuntimeException("Failed to open output file");
51}
52
53fwrite($output, <<<'CODE'
54<?php
55
56/*
57 * This file is part of BedrockProtocol.
58 * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
59 *
60 * BedrockProtocol is free software: you can redistribute it and/or modify
61 * it under the terms of the GNU Lesser General Public License as published by
62 * the Free Software Foundation, either version 3 of the License, or
63 * (at your option) any later version.
64 */
65
66declare(strict_types=1);
67
68namespace pocketmine\network\mcpe\protocol\types\entity;
69
74final class EntityIds{
75
76 private function __construct(){
77 //NOOP
78 }
79
80
81CODE);
82
83foreach($list as $stringId => $legacyId){
84 $constantName = strtoupper(explode(":", $stringId, 2)[1]);
85 fwrite($output, "\tpublic const $constantName = \"$stringId\";\n");
86}
87
88fwrite($output, "}\n");
89fclose($output);
90
91echo "Successfully regenerated EntityIds" . PHP_EOL;