|
| 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\build\generate_known_translation_apis; |
| 25 | + |
| 26 | +use function array_fill_keys; |
| 27 | +use function count; |
| 28 | +use function explode; |
| 29 | +use function file_get_contents; |
| 30 | +use function fwrite; |
| 31 | +use function in_array; |
| 32 | +use function is_array; |
| 33 | +use function json_decode; |
| 34 | +use function json_encode; |
| 35 | +use function parse_ini_file; |
| 36 | +use function preg_match_all; |
| 37 | +use function str_starts_with; |
| 38 | +use const INI_SCANNER_RAW; |
| 39 | +use const JSON_PRETTY_PRINT; |
| 40 | +use const JSON_THROW_ON_ERROR; |
| 41 | +use const PHP_EOL; |
| 42 | +use const PHP_INT_MAX; |
| 43 | +use const STDERR; |
| 44 | + |
| 45 | +/** |
| 46 | + * @param string[] $baseLanguageDef |
| 47 | + * @param string[] $altLanguageDef |
| 48 | + * |
| 49 | + * @phpstan-param array<string, string> $baseLanguageDef |
| 50 | + * @phpstan-param array<string, string> $altLanguageDef |
| 51 | + * |
| 52 | + * @return bool true if everything is OK, false otherwise |
| 53 | + */ |
| 54 | +function verify_translations(array $baseLanguageDef, string $altLanguageName, array $altLanguageDef) : bool{ |
| 55 | + $parameterRegex = '/{%(.+?)}/'; |
| 56 | + |
| 57 | + $ok = true; |
| 58 | + foreach($baseLanguageDef as $key => $baseString){ |
| 59 | + if(!isset($altLanguageDef[$key])){ |
| 60 | + continue; |
| 61 | + } |
| 62 | + $altString = $altLanguageDef[$key]; |
| 63 | + $baseParams = preg_match_all($parameterRegex, $baseString, $baseMatches); |
| 64 | + $altParams = preg_match_all($parameterRegex, $altString, $altMatches); |
| 65 | + if($baseParams === false || $altParams === false){ |
| 66 | + throw new \Error("preg_match_all() should not have failed here"); |
| 67 | + } |
| 68 | + foreach($baseMatches[1] as $paramName){ |
| 69 | + if(!in_array($paramName, $altMatches[1], true)){ |
| 70 | + fwrite(STDERR, "$altLanguageName: missing parameter %$paramName in string $key" . PHP_EOL); |
| 71 | + $ok = false; |
| 72 | + } |
| 73 | + } |
| 74 | + foreach($altMatches[1] as $paramName){ |
| 75 | + if(!in_array($paramName, $baseMatches[1], true)){ |
| 76 | + fwrite(STDERR, "$altLanguageName: unexpected extra parameter %$paramName in string $key" . PHP_EOL); |
| 77 | + $ok = false; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + foreach($altLanguageDef as $key => $altString){ |
| 82 | + if(!isset($baseLanguageDef[$key])){ |
| 83 | + fwrite(STDERR, "$altLanguageName: unexpected extra string $key with no base in eng.ini" . PHP_EOL); |
| 84 | + $ok = false; |
| 85 | + } |
| 86 | + } |
| 87 | + return $ok; |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * @return string[]|null |
| 92 | + * @phpstan-return array<string, string>|null |
| 93 | + */ |
| 94 | +function parse_language_file(string $path, string $code) : ?array{ |
| 95 | + $lang = parse_ini_file($path . "/" . "$code.ini", false, INI_SCANNER_RAW); |
| 96 | + if($lang === false){ |
| 97 | + return null; |
| 98 | + } |
| 99 | + return $lang; |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * @return string[] |
| 104 | + * @phpstan-return array<string, string> |
| 105 | + */ |
| 106 | +function parse_mojang_language_defs(string $contents) : array{ |
| 107 | + $result = []; |
| 108 | + foreach(explode("\n", $contents, limit: PHP_INT_MAX) as $line){ |
| 109 | + $stripped = explode("##", $line, 2)[0]; |
| 110 | + $kv = explode("=", $stripped, 2); |
| 111 | + if(count($kv) !== 2){ |
| 112 | + continue; |
| 113 | + } |
| 114 | + $result[$kv[0]] = $kv[1]; |
| 115 | + } |
| 116 | + |
| 117 | + return $result; |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * @param string[] $pocketmine |
| 122 | + * @param string[] $mojang |
| 123 | + * @param string[] $knownBadKeys |
| 124 | + * @phpstan-param array<string, string> $pocketmine |
| 125 | + * @phpstan-param array<string, string> $mojang |
| 126 | + * @phpstan-param array<string, bool> $knownBadKeys |
| 127 | + * |
| 128 | + * @return string[] |
| 129 | + * @phpstan-return list<string> |
| 130 | + */ |
| 131 | +function verify_keys(array $pocketmine, array $mojang, array $knownBadKeys) : array{ |
| 132 | + $wrong = []; |
| 133 | + foreach($pocketmine as $k => $v){ |
| 134 | + if(str_starts_with($k, "pocketmine.")){ |
| 135 | + continue; |
| 136 | + } |
| 137 | + |
| 138 | + if(!isset($mojang[$k]) && !isset($knownBadKeys[$k])){ |
| 139 | + $wrong[] = $k; |
| 140 | + } |
| 141 | + } |
| 142 | + foreach($knownBadKeys as $k => $_){ |
| 143 | + if(!isset($pocketmine[$k])){ |
| 144 | + fwrite(STDERR, "known-bad-keys.json contains key \"$k\" which does not exist in eng.ini\n"); |
| 145 | + } |
| 146 | + } |
| 147 | + return $wrong; |
| 148 | +} |
| 149 | + |
| 150 | +if(count($argv) !== 2){ |
| 151 | + fwrite(STDERR, "Required arguments: path\n"); |
| 152 | + exit(1); |
| 153 | +} |
| 154 | +$eng = parse_language_file($argv[1], "eng"); |
| 155 | +if($eng === null){ |
| 156 | + fwrite(STDERR, "Failed to parse eng.ini\n"); |
| 157 | + exit(1); |
| 158 | +} |
| 159 | + |
| 160 | +$mojangRaw = file_get_contents("https://raw.githubusercontent.com/Mojang/bedrock-samples/refs/heads/main/resource_pack/texts/en_US.lang"); |
| 161 | +if($mojangRaw === false){ |
| 162 | + fwrite(STDERR, "Failed to fetch official Mojang sources for verification\n"); |
| 163 | + exit(1); |
| 164 | +} |
| 165 | +$mojang = parse_mojang_language_defs($mojangRaw); |
| 166 | + |
| 167 | +$knownBadKeysRaw = file_get_contents($argv[1] . "/known-bad-keys.json"); |
| 168 | +$knownBadKeysDecoded = $knownBadKeysRaw !== false ? json_decode($knownBadKeysRaw, associative: true, flags: JSON_THROW_ON_ERROR) : []; |
| 169 | + |
| 170 | +if(!is_array($knownBadKeysDecoded)){ |
| 171 | + fwrite(STDERR, "known-bad-keys.json should contain an array of strings\n"); |
| 172 | + exit(1); |
| 173 | +} |
| 174 | +$knownBadKeys = []; |
| 175 | +foreach($knownBadKeysDecoded as $key){ |
| 176 | + if(!is_string($key)){ |
| 177 | + fwrite(STDERR, "known-bad-keys.json should contain an array of strings\n"); |
| 178 | + exit(1); |
| 179 | + } |
| 180 | + $knownBadKeys[] = $key; |
| 181 | +} |
| 182 | + |
| 183 | +$badKeys = verify_keys($eng, $mojang, array_fill_keys($knownBadKeys, true)); |
| 184 | +if(count($badKeys) !== 0){ |
| 185 | + fwrite(STDERR, "The following non-\"pocketmine.\" keys are not matched by Mojang sources and are not whitelisted:\n"); |
| 186 | + fwrite(STDERR, json_encode($badKeys, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR) . "\n"); |
| 187 | + fwrite(STDERR, "Keys must either match Mojang sources, or be prefixed with \"pocketmine.\"\n"); |
| 188 | + fwrite(STDERR, "Failure to do so will cause these to be shown incorrectly on clients, as the server won't translate them\n"); |
| 189 | + exit(1); |
| 190 | +} |
| 191 | + |
| 192 | +$exit = 0; |
| 193 | +/** |
| 194 | + * @var string[] $match |
| 195 | + * @phpstan-var array{0: string, 1: string} $match |
| 196 | + */ |
| 197 | +foreach(new \RegexIterator(new \FilesystemIterator($argv[1], \FilesystemIterator::CURRENT_AS_PATHNAME), "/([a-z]+)\.ini$/", \RegexIterator::GET_MATCH) as $match){ |
| 198 | + $code = $match[1]; |
| 199 | + if($code === "eng"){ |
| 200 | + continue; |
| 201 | + } |
| 202 | + $otherLang = parse_language_file($argv[1], $code); |
| 203 | + if($otherLang === null){ |
| 204 | + fwrite(STDERR, "Error parsing $code.ini\n"); |
| 205 | + $exit = 1; |
| 206 | + continue; |
| 207 | + } |
| 208 | + if(!verify_translations($eng, $code, $otherLang)){ |
| 209 | + fwrite(STDERR, "Errors found in $code.ini\n"); |
| 210 | + $exit = 1; |
| 211 | + continue; |
| 212 | + } |
| 213 | + |
| 214 | + echo "Everything OK in $code.ini\n"; |
| 215 | +} |
| 216 | +exit($exit); |
0 commit comments