Use Filesystem::fileGetContents() in more places

This commit is contained in:
Dylan K. Taylor
2022-12-25 18:26:53 +00:00
parent c89df7eb1c
commit 8fd4918429
9 changed files with 20 additions and 39 deletions

View File

@ -23,18 +23,17 @@ declare(strict_types=1);
namespace pocketmine\tools\generate_block_palette_spec;
use pocketmine\errorhandler\ErrorToExceptionHandler;
use pocketmine\nbt\NbtException;
use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\network\mcpe\convert\BlockStateDictionary;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\Filesystem;
use pocketmine\utils\Utils;
use function array_values;
use function count;
use function dirname;
use function file_get_contents;
use function file_put_contents;
use function fwrite;
use function get_class;
@ -54,7 +53,7 @@ if(count($argv) !== 3){
[, $inputFile, $outputFile] = $argv;
try{
$states = BlockStateDictionary::loadPaletteFromString(ErrorToExceptionHandler::trapAndRemoveFalse(fn() => file_get_contents($inputFile)));
$states = BlockStateDictionary::loadPaletteFromString(Filesystem::fileGetContents($inputFile));
}catch(NbtException){
fwrite(STDERR, "Invalid block palette file $argv[1]\n");
exit(1);

View File

@ -28,15 +28,14 @@ use pocketmine\data\bedrock\block\upgrade\BlockStateUpgradeSchema;
use pocketmine\data\bedrock\block\upgrade\BlockStateUpgradeSchemaBlockRemap;
use pocketmine\data\bedrock\block\upgrade\BlockStateUpgradeSchemaUtils;
use pocketmine\data\bedrock\block\upgrade\BlockStateUpgradeSchemaValueRemap;
use pocketmine\errorhandler\ErrorToExceptionHandler;
use pocketmine\nbt\tag\Tag;
use pocketmine\network\mcpe\protocol\serializer\NetworkNbtSerializer;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\Filesystem;
use pocketmine\utils\Utils;
use function array_key_first;
use function count;
use function dirname;
use function file_get_contents;
use function file_put_contents;
use function fwrite;
use function json_encode;
@ -59,11 +58,7 @@ class BlockStateMapping{
* @phpstan-return array<string, list<BlockStateMapping>>
*/
function loadUpgradeTable(string $file, bool $reverse) : array{
try{
$contents = ErrorToExceptionHandler::trapAndRemoveFalse(fn() => file_get_contents($file));
}catch(\ErrorException $e){
throw new \RuntimeException("Failed loading mapping table file $file: " . $e->getMessage(), 0, $e);
}
$contents = Filesystem::fileGetContents($file);
$data = (new NetworkNbtSerializer())->readMultiple($contents);
$result = [];

View File

@ -30,10 +30,10 @@ declare(strict_types=1);
namespace pocketmine\tools\generate_item_upgrade_schema;
use pocketmine\errorhandler\ErrorToExceptionHandler;
use pocketmine\utils\Filesystem;
use Symfony\Component\Filesystem\Path;
use function count;
use function dirname;
use function file_get_contents;
use function file_put_contents;
use function is_array;
use function json_decode;
@ -55,7 +55,7 @@ if(count($argv) !== 4){
[, $mappingTableFile, $upgradeSchemasDir, $outputFile] = $argv;
$target = json_decode(ErrorToExceptionHandler::trapAndRemoveFalse(fn() => file_get_contents($mappingTableFile)), true, JSON_THROW_ON_ERROR);
$target = json_decode(Filesystem::fileGetContents($mappingTableFile), true, JSON_THROW_ON_ERROR);
if(!is_array($target)){
\GlobalLogger::get()->error("Invalid mapping table file");
exit(1);
@ -69,7 +69,7 @@ foreach($files as $file){
continue;
}
\GlobalLogger::get()->info("Processing schema file $file");
$data = json_decode(ErrorToExceptionHandler::trapAndRemoveFalse(fn() => file_get_contents(Path::join($upgradeSchemasDir, $file))), associative: true, flags: JSON_THROW_ON_ERROR);
$data = json_decode(Filesystem::fileGetContents(Path::join($upgradeSchemasDir, $file)), associative: true, flags: JSON_THROW_ON_ERROR);
if(!is_array($data)){
\GlobalLogger::get()->error("Invalid schema file $file");
exit(1);