0
0
mirror of https://github.com/pmmp/BedrockProtocol.git synced 2024-11-27 04:59:08 +00:00
BedrockProtocol/tools/generate-entity-ids.php
2024-03-13 14:35:54 +00:00

92 lines
2.4 KiB
PHP

<?php
/*
* This file is part of BedrockProtocol.
* Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
*
* BedrockProtocol is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
declare(strict_types=1);
namespace pocketmine\network\mcpe\protocol\tools\generate_entity_ids;
use function count;
use function dirname;
use function explode;
use function fclose;
use function file_get_contents;
use function fopen;
use function fwrite;
use function is_array;
use function json_decode;
use function ksort;
use function strtoupper;
if(count($argv) !== 2){
fwrite(STDERR, "Required arguments: path to entity ID mapping file\n");
fwrite(STDERR, "Hint: Input file is a JSON file like entity_id_map.json in pmmp/BedrockData\n");
exit(1);
}
$jsonRaw = file_get_contents($argv[1]);
if($jsonRaw === false){
fwrite(STDERR, "Failed to read entity ID mapping file\n");
exit(1);
}
$list = json_decode($jsonRaw, true, flags: JSON_THROW_ON_ERROR);
if(!is_array($list)){
fwrite(STDERR, "Failed to decode entity ID mapping file, expected a JSON object\n");
exit(1);
}
ksort($list, SORT_STRING);
$output = fopen(dirname(__DIR__) . "/src/types/entity/EntityIds.php", "wb");
if($output === false){
throw new \RuntimeException("Failed to open output file");
}
fwrite($output, <<<'CODE'
<?php
/*
* This file is part of BedrockProtocol.
* Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
*
* BedrockProtocol is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
declare(strict_types=1);
namespace pocketmine\network\mcpe\protocol\types\entity;
/**
* This file is automatically generated; do NOT edit it by hand.
* Regenerate it by running tools/generate-entity-ids.php
*/
final class EntityIds{
private function __construct(){
//NOOP
}
CODE);
foreach($list as $stringId => $legacyId){
$constantName = strtoupper(explode(":", $stringId, 2)[1]);
fwrite($output, "\tpublic const $constantName = \"$stringId\";\n");
}
fwrite($output, "}\n");
fclose($output);
echo "Successfully regenerated EntityIds" . PHP_EOL;