Filesystem: added fileGetContents to reduce ErrorToExceptionHandler boilerplate code

This commit is contained in:
Dylan K. Taylor
2022-12-25 17:13:51 +00:00
parent b1c0eae1f6
commit 0d169b4e80
16 changed files with 53 additions and 51 deletions

View File

@ -39,7 +39,6 @@ use pocketmine\world\generator\GeneratorManager;
use pocketmine\world\World;
use pocketmine\world\WorldCreationOptions;
use Symfony\Component\Filesystem\Path;
use function file_get_contents;
use function file_put_contents;
use function strlen;
use function substr;
@ -111,9 +110,10 @@ class BedrockWorldData extends BaseNbtWorldData{
}
protected function load() : CompoundTag{
$rawLevelData = @file_get_contents($this->dataPath);
if($rawLevelData === false){
throw new CorruptedWorldException("Failed to read level.dat (permission denied or doesn't exist)");
try{
$rawLevelData = Filesystem::fileGetContents($this->dataPath);
}catch(\RuntimeException $e){
throw new CorruptedWorldException($e->getMessage(), 0, $e);
}
if(strlen($rawLevelData) <= 8){
throw new CorruptedWorldException("Truncated level.dat");

View File

@ -37,7 +37,6 @@ use pocketmine\world\World;
use pocketmine\world\WorldCreationOptions;
use Symfony\Component\Filesystem\Path;
use function ceil;
use function file_get_contents;
use function file_put_contents;
use function microtime;
use function zlib_decode;
@ -75,9 +74,10 @@ class JavaWorldData extends BaseNbtWorldData{
}
protected function load() : CompoundTag{
$rawLevelData = @file_get_contents($this->dataPath);
if($rawLevelData === false){
throw new CorruptedWorldException("Failed to read level.dat (permission denied or doesn't exist)");
try{
$rawLevelData = Filesystem::fileGetContents($this->dataPath);
}catch(\RuntimeException $e){
throw new CorruptedWorldException($e->getMessage(), 0, $e);
}
$nbt = new BigEndianNbtSerializer();
$decompressed = @zlib_decode($rawLevelData);