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
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
9 changed files with 20 additions and 39 deletions

View File

@ -23,13 +23,12 @@ declare(strict_types=1);
namespace pocketmine\data\bedrock; namespace pocketmine\data\bedrock;
use pocketmine\errorhandler\ErrorToExceptionHandler;
use pocketmine\utils\AssumptionFailedError; use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\Filesystem;
use pocketmine\utils\SingletonTrait; use pocketmine\utils\SingletonTrait;
use pocketmine\utils\Utils; use pocketmine\utils\Utils;
use Symfony\Component\Filesystem\Path; use Symfony\Component\Filesystem\Path;
use function array_keys; use function array_keys;
use function file_get_contents;
use function gettype; use function gettype;
use function is_array; use function is_array;
use function is_string; use function is_string;
@ -46,7 +45,7 @@ final class ItemTagToIdMap{
use SingletonTrait; use SingletonTrait;
private static function make() : self{ private static function make() : self{
$map = json_decode(ErrorToExceptionHandler::trapAndRemoveFalse(fn() => file_get_contents(Path::join(BEDROCK_DATA_PATH, 'item_tags.json'))), true, flags: JSON_THROW_ON_ERROR); $map = json_decode(Filesystem::fileGetContents(Path::join(BEDROCK_DATA_PATH, 'item_tags.json')), true, flags: JSON_THROW_ON_ERROR);
if(!is_array($map)){ if(!is_array($map)){
throw new AssumptionFailedError("Invalid item tag map, expected array"); throw new AssumptionFailedError("Invalid item tag map, expected array");
} }

View File

@ -27,16 +27,15 @@ use pocketmine\data\bedrock\block\upgrade\model\BlockStateUpgradeSchemaModel;
use pocketmine\data\bedrock\block\upgrade\model\BlockStateUpgradeSchemaModelBlockRemap; use pocketmine\data\bedrock\block\upgrade\model\BlockStateUpgradeSchemaModelBlockRemap;
use pocketmine\data\bedrock\block\upgrade\model\BlockStateUpgradeSchemaModelTag; use pocketmine\data\bedrock\block\upgrade\model\BlockStateUpgradeSchemaModelTag;
use pocketmine\data\bedrock\block\upgrade\model\BlockStateUpgradeSchemaModelValueRemap; use pocketmine\data\bedrock\block\upgrade\model\BlockStateUpgradeSchemaModelValueRemap;
use pocketmine\errorhandler\ErrorToExceptionHandler;
use pocketmine\nbt\tag\ByteTag; use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\IntTag; use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\StringTag; use pocketmine\nbt\tag\StringTag;
use pocketmine\nbt\tag\Tag; use pocketmine\nbt\tag\Tag;
use pocketmine\utils\Filesystem;
use pocketmine\utils\Utils; use pocketmine\utils\Utils;
use Symfony\Component\Filesystem\Path; use Symfony\Component\Filesystem\Path;
use function array_map; use function array_map;
use function count; use function count;
use function file_get_contents;
use function get_debug_type; use function get_debug_type;
use function gettype; use function gettype;
use function implode; use function implode;
@ -275,11 +274,7 @@ final class BlockStateUpgradeSchemaUtils{
$fullPath = Path::join($path, $filename); $fullPath = Path::join($path, $filename);
try{ $raw = Filesystem::fileGetContents($fullPath);
$raw = ErrorToExceptionHandler::trapAndRemoveFalse(fn() => file_get_contents($fullPath));
}catch(\ErrorException $e){
throw new \RuntimeException("Loading schema file $fullPath: " . $e->getMessage(), 0, $e);
}
try{ try{
$schema = self::loadSchemaFromString($raw, $priority); $schema = self::loadSchemaFromString($raw, $priority);

View File

@ -24,11 +24,10 @@ declare(strict_types=1);
namespace pocketmine\data\bedrock\item; namespace pocketmine\data\bedrock\item;
use pocketmine\utils\AssumptionFailedError; use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\Filesystem;
use pocketmine\utils\SingletonTrait; use pocketmine\utils\SingletonTrait;
use pocketmine\utils\Utils;
use Symfony\Component\Filesystem\Path; use Symfony\Component\Filesystem\Path;
use function array_flip; use function array_flip;
use function file_get_contents;
use function is_array; use function is_array;
use function json_decode; use function json_decode;
use const JSON_THROW_ON_ERROR; use const JSON_THROW_ON_ERROR;
@ -42,7 +41,7 @@ final class BlockItemIdMap{
private static function make() : self{ private static function make() : self{
$map = json_decode( $map = json_decode(
Utils::assumeNotFalse(file_get_contents(Path::join(BEDROCK_DATA_PATH, 'block_id_to_item_id_map.json')), "Missing required resource file"), Filesystem::fileGetContents(Path::join(BEDROCK_DATA_PATH, 'block_id_to_item_id_map.json')),
associative: true, associative: true,
flags: JSON_THROW_ON_ERROR flags: JSON_THROW_ON_ERROR
); );

View File

@ -24,9 +24,8 @@ declare(strict_types=1);
namespace pocketmine\data\bedrock\item\upgrade; namespace pocketmine\data\bedrock\item\upgrade;
use pocketmine\data\bedrock\item\upgrade\model\ItemIdMetaUpgradeSchemaModel; use pocketmine\data\bedrock\item\upgrade\model\ItemIdMetaUpgradeSchemaModel;
use pocketmine\errorhandler\ErrorToExceptionHandler; use pocketmine\utils\Filesystem;
use Symfony\Component\Filesystem\Path; use Symfony\Component\Filesystem\Path;
use function file_get_contents;
use function gettype; use function gettype;
use function is_object; use function is_object;
use function json_decode; use function json_decode;
@ -60,11 +59,7 @@ final class ItemIdMetaUpgradeSchemaUtils{
$fullPath = Path::join($path, $filename); $fullPath = Path::join($path, $filename);
try{ $raw = Filesystem::fileGetContents($fullPath);
$raw = ErrorToExceptionHandler::trapAndRemoveFalse(fn() => file_get_contents($fullPath));
}catch(\ErrorException $e){
throw new \RuntimeException("Loading schema file $fullPath: " . $e->getMessage(), 0, $e);
}
try{ try{
$schema = self::loadSchemaFromString($raw, $priority); $schema = self::loadSchemaFromString($raw, $priority);

View File

@ -24,10 +24,10 @@ declare(strict_types=1);
namespace pocketmine\data\bedrock\item\upgrade; namespace pocketmine\data\bedrock\item\upgrade;
use pocketmine\utils\AssumptionFailedError; use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\Filesystem;
use pocketmine\utils\SingletonTrait; use pocketmine\utils\SingletonTrait;
use pocketmine\utils\Utils; use pocketmine\utils\Utils;
use Symfony\Component\Filesystem\Path; use Symfony\Component\Filesystem\Path;
use function file_get_contents;
use function is_array; use function is_array;
use function is_string; use function is_string;
use function json_decode; use function json_decode;
@ -47,7 +47,7 @@ final class R12ItemIdToBlockIdMap{
private static function make() : self{ private static function make() : self{
$map = json_decode( $map = json_decode(
Utils::assumeNotFalse(file_get_contents(Path::join(BEDROCK_ITEM_UPGRADE_SCHEMA_PATH, '1.12.0_item_id_to_block_id_map.json')), "Missing required resource file"), Filesystem::fileGetContents(Path::join(BEDROCK_ITEM_UPGRADE_SCHEMA_PATH, '1.12.0_item_id_to_block_id_map.json')),
associative: true, associative: true,
flags: JSON_THROW_ON_ERROR flags: JSON_THROW_ON_ERROR
); );

View File

@ -32,9 +32,8 @@ use pocketmine\data\bedrock\block\upgrade\BlockIdMetaUpgrader;
use pocketmine\data\bedrock\block\upgrade\BlockStateUpgrader; use pocketmine\data\bedrock\block\upgrade\BlockStateUpgrader;
use pocketmine\data\bedrock\block\upgrade\BlockStateUpgradeSchemaUtils; use pocketmine\data\bedrock\block\upgrade\BlockStateUpgradeSchemaUtils;
use pocketmine\data\bedrock\block\upgrade\LegacyBlockIdToStringIdMap; use pocketmine\data\bedrock\block\upgrade\LegacyBlockIdToStringIdMap;
use pocketmine\errorhandler\ErrorToExceptionHandler; use pocketmine\utils\Filesystem;
use Symfony\Component\Filesystem\Path; use Symfony\Component\Filesystem\Path;
use function file_get_contents;
use const pocketmine\BEDROCK_BLOCK_UPGRADE_SCHEMA_PATH; use const pocketmine\BEDROCK_BLOCK_UPGRADE_SCHEMA_PATH;
/** /**
@ -69,10 +68,10 @@ final class GlobalBlockStateHandlers{
)); ));
self::$blockDataUpgrader = new BlockDataUpgrader( self::$blockDataUpgrader = new BlockDataUpgrader(
BlockIdMetaUpgrader::loadFromString( BlockIdMetaUpgrader::loadFromString(
ErrorToExceptionHandler::trapAndRemoveFalse(fn() => file_get_contents(Path::join( Filesystem::fileGetContents(Path::join(
BEDROCK_BLOCK_UPGRADE_SCHEMA_PATH, BEDROCK_BLOCK_UPGRADE_SCHEMA_PATH,
'1.12.0_to_1.18.10_blockstate_map.bin' '1.12.0_to_1.18.10_blockstate_map.bin'
))), )),
LegacyBlockIdToStringIdMap::getInstance(), LegacyBlockIdToStringIdMap::getInstance(),
$blockStateUpgrader $blockStateUpgrader
), ),

View File

@ -23,18 +23,17 @@ declare(strict_types=1);
namespace pocketmine\tools\generate_block_palette_spec; namespace pocketmine\tools\generate_block_palette_spec;
use pocketmine\errorhandler\ErrorToExceptionHandler;
use pocketmine\nbt\NbtException; use pocketmine\nbt\NbtException;
use pocketmine\nbt\tag\ByteTag; use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\IntTag; use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\StringTag; use pocketmine\nbt\tag\StringTag;
use pocketmine\network\mcpe\convert\BlockStateDictionary; use pocketmine\network\mcpe\convert\BlockStateDictionary;
use pocketmine\utils\AssumptionFailedError; use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\Filesystem;
use pocketmine\utils\Utils; use pocketmine\utils\Utils;
use function array_values; use function array_values;
use function count; use function count;
use function dirname; use function dirname;
use function file_get_contents;
use function file_put_contents; use function file_put_contents;
use function fwrite; use function fwrite;
use function get_class; use function get_class;
@ -54,7 +53,7 @@ if(count($argv) !== 3){
[, $inputFile, $outputFile] = $argv; [, $inputFile, $outputFile] = $argv;
try{ try{
$states = BlockStateDictionary::loadPaletteFromString(ErrorToExceptionHandler::trapAndRemoveFalse(fn() => file_get_contents($inputFile))); $states = BlockStateDictionary::loadPaletteFromString(Filesystem::fileGetContents($inputFile));
}catch(NbtException){ }catch(NbtException){
fwrite(STDERR, "Invalid block palette file $argv[1]\n"); fwrite(STDERR, "Invalid block palette file $argv[1]\n");
exit(1); 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\BlockStateUpgradeSchemaBlockRemap;
use pocketmine\data\bedrock\block\upgrade\BlockStateUpgradeSchemaUtils; use pocketmine\data\bedrock\block\upgrade\BlockStateUpgradeSchemaUtils;
use pocketmine\data\bedrock\block\upgrade\BlockStateUpgradeSchemaValueRemap; use pocketmine\data\bedrock\block\upgrade\BlockStateUpgradeSchemaValueRemap;
use pocketmine\errorhandler\ErrorToExceptionHandler;
use pocketmine\nbt\tag\Tag; use pocketmine\nbt\tag\Tag;
use pocketmine\network\mcpe\protocol\serializer\NetworkNbtSerializer; use pocketmine\network\mcpe\protocol\serializer\NetworkNbtSerializer;
use pocketmine\utils\AssumptionFailedError; use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\Filesystem;
use pocketmine\utils\Utils; use pocketmine\utils\Utils;
use function array_key_first; use function array_key_first;
use function count; use function count;
use function dirname; use function dirname;
use function file_get_contents;
use function file_put_contents; use function file_put_contents;
use function fwrite; use function fwrite;
use function json_encode; use function json_encode;
@ -59,11 +58,7 @@ class BlockStateMapping{
* @phpstan-return array<string, list<BlockStateMapping>> * @phpstan-return array<string, list<BlockStateMapping>>
*/ */
function loadUpgradeTable(string $file, bool $reverse) : array{ function loadUpgradeTable(string $file, bool $reverse) : array{
try{ $contents = Filesystem::fileGetContents($file);
$contents = ErrorToExceptionHandler::trapAndRemoveFalse(fn() => file_get_contents($file));
}catch(\ErrorException $e){
throw new \RuntimeException("Failed loading mapping table file $file: " . $e->getMessage(), 0, $e);
}
$data = (new NetworkNbtSerializer())->readMultiple($contents); $data = (new NetworkNbtSerializer())->readMultiple($contents);
$result = []; $result = [];

View File

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