From 37f2dafae1701bc840f5a57185a6b38f0cf8b72c Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 9 Aug 2023 16:16:11 +0100 Subject: [PATCH] PluginBase: make saveResource() use copy() instead of overengineered streams garbage --- src/plugin/PluginBase.php | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/plugin/PluginBase.php b/src/plugin/PluginBase.php index 6ef4748cf..1b537a11c 100644 --- a/src/plugin/PluginBase.php +++ b/src/plugin/PluginBase.php @@ -30,19 +30,16 @@ use pocketmine\command\PluginCommand; use pocketmine\lang\KnownTranslationFactory; use pocketmine\scheduler\TaskScheduler; use pocketmine\Server; -use pocketmine\utils\AssumptionFailedError; use pocketmine\utils\Config; use pocketmine\utils\Utils; use Symfony\Component\Filesystem\Path; +use function copy; use function count; use function dirname; -use function fclose; use function file_exists; -use function fopen; use function mkdir; use function rtrim; use function str_contains; -use function stream_copy_to_stream; use function strtolower; use function trim; use const DIRECTORY_SEPARATOR; @@ -251,26 +248,21 @@ abstract class PluginBase implements Plugin, CommandExecutor{ return false; } - $out = Path::join($this->dataFolder, $filename); - if(file_exists($out) && !$replace){ + $source = Path::join($this->resourceFolder, $filename); + if(!file_exists($source)){ return false; } - if(($resource = $this->getResource($filename)) === null){ + $destination = Path::join($this->dataFolder, $filename); + if(file_exists($destination) && !$replace){ return false; } - if(!file_exists(dirname($out))){ - mkdir(dirname($out), 0755, true); + if(!file_exists(dirname($destination))){ + mkdir(dirname($destination), 0755, true); } - $fp = fopen($out, "wb"); - if($fp === false) throw new AssumptionFailedError("fopen() should not fail with wb flags"); - - $ret = stream_copy_to_stream($resource, $fp) > 0; - fclose($fp); - fclose($resource); - return $ret; + return copy($source, $destination); } /**