diff --git a/src/thebigcrafter/omp/commands/subcommands/DisableCommand.php b/src/thebigcrafter/omp/commands/subcommands/DisableCommand.php index 19c3acb..538bf1b 100644 --- a/src/thebigcrafter/omp/commands/subcommands/DisableCommand.php +++ b/src/thebigcrafter/omp/commands/subcommands/DisableCommand.php @@ -16,11 +16,12 @@ use CortexPE\Commando\args\RawStringArgument; use CortexPE\Commando\BaseSubCommand; use pocketmine\command\CommandSender; -use Symfony\Component\Filesystem\Filesystem; +use SOFe\AwaitGenerator\Await; use Symfony\Component\Filesystem\Path; use thebigcrafter\omp\Language; use thebigcrafter\omp\OhMyPMMP; use thebigcrafter\omp\Utils; +use thebigcrafter\omp\utils\Filesystem; class DisableCommand extends BaseSubCommand { @@ -36,7 +37,6 @@ protected function prepare() : void */ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void { - $fs = new Filesystem(); $name = $args["name"]; $oldPluginFilePath = Utils::getPluginFilePath($name); $newPluginFilePath = Path::join(OhMyPMMP::getInstance()->getServer()->getPluginPath(), "..", "disabled_plugins", "$name.phar"); @@ -46,7 +46,7 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v return; } - $fs->rename($oldPluginFilePath, $newPluginFilePath); + Await::g2c(Filesystem::rename($oldPluginFilePath, $newPluginFilePath)); $sender->sendMessage(Language::translate("commands.disable.successfully", ["name" => $name])); } diff --git a/src/thebigcrafter/omp/commands/subcommands/EnableCommand.php b/src/thebigcrafter/omp/commands/subcommands/EnableCommand.php index 8f92126..f18a343 100644 --- a/src/thebigcrafter/omp/commands/subcommands/EnableCommand.php +++ b/src/thebigcrafter/omp/commands/subcommands/EnableCommand.php @@ -16,11 +16,12 @@ use CortexPE\Commando\args\RawStringArgument; use CortexPE\Commando\BaseSubCommand; use pocketmine\command\CommandSender; -use Symfony\Component\Filesystem\Filesystem; +use SOFe\AwaitGenerator\Await; use Symfony\Component\Filesystem\Path; use thebigcrafter\omp\Language; use thebigcrafter\omp\OhMyPMMP; use thebigcrafter\omp\Utils; +use thebigcrafter\omp\utils\Filesystem; class EnableCommand extends BaseSubCommand { @@ -36,17 +37,16 @@ protected function prepare() : void */ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void { - $fs = new Filesystem(); $name = $args["name"]; $newPluginFilePath = Path::join(Utils::getPluginsFolder(), "$name.phar"); $oldPluginFilePath = Path::join(OhMyPMMP::getInstance()->getServer()->getPluginPath(), "..", "disabled_plugins", "$name.phar"); - if (!$fs->exists($oldPluginFilePath)) { + if (!Filesystem::exists($oldPluginFilePath)) { $sender->sendMessage(Language::translate("commands.enable.failed", ["name" => $name])); return; } - $fs->rename($oldPluginFilePath, $newPluginFilePath); + Await::g2c(Filesystem::rename($oldPluginFilePath, $newPluginFilePath)); $sender->sendMessage(Language::translate("commands.enable.successfully", ["name" => $name])); } diff --git a/src/thebigcrafter/omp/commands/subcommands/InstallCommand.php b/src/thebigcrafter/omp/commands/subcommands/InstallCommand.php index 2c02d83..5c003dc 100644 --- a/src/thebigcrafter/omp/commands/subcommands/InstallCommand.php +++ b/src/thebigcrafter/omp/commands/subcommands/InstallCommand.php @@ -18,11 +18,12 @@ use pocketmine\command\CommandSender; use pocketmine\utils\Internet; use pocketmine\utils\InternetRequestResult; +use SOFe\AwaitGenerator\Await; use Symfony\Component\Filesystem\Path; +use thebigcrafter\omp\helpers\PharHelper; use thebigcrafter\omp\Language; use thebigcrafter\omp\OhMyPMMP; use thebigcrafter\omp\pool\PoggitPluginsPool; -use function file_put_contents; use function is_null; class InstallCommand extends BaseSubCommand @@ -70,7 +71,7 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v $pharPath = Path::join(OhMyPMMP::getInstance()->getServer()->getDataPath(), "plugins", "$name.phar"); - file_put_contents($pharPath, $res->getBody()); + Await::g2c(PharHelper::writePhar($pharPath, $res->getBody())); $sender->sendMessage(Language::translate("commands.install.successfully", ["name" => $name, "version" => $latestVersion])); } diff --git a/src/thebigcrafter/omp/helpers/PharHelper.php b/src/thebigcrafter/omp/helpers/PharHelper.php new file mode 100644 index 0000000..28a4313 --- /dev/null +++ b/src/thebigcrafter/omp/helpers/PharHelper.php @@ -0,0 +1,36 @@ + + * + * This source file is subject to the GPL-3.0 license that is bundled + * with this source code in the file LICENSE. + */ + +declare(strict_types=1); + +namespace thebigcrafter\omp\helpers; + +use Closure; +use Exception; +use Generator; +use SOFe\AwaitGenerator\Await; +use function file_put_contents; + +class PharHelper +{ + public static function writePhar(string $path, string $content) : Generator + { + return yield from Await::promise(function (Closure $resolve, Closure $reject) use ($path, $content) { + $exec = file_put_contents($path, $content); + + if ($exec === false) { + $reject(new Exception("Cannot create Phar file")); + } + + $resolve($exec); + }); + } +} diff --git a/src/thebigcrafter/omp/tasks/CheckForUpdates.php b/src/thebigcrafter/omp/tasks/CheckForUpdates.php index a8abf82..316104b 100644 --- a/src/thebigcrafter/omp/tasks/CheckForUpdates.php +++ b/src/thebigcrafter/omp/tasks/CheckForUpdates.php @@ -1,25 +1,25 @@ - * - * This source file is subject to the GPL-3.0 license that is bundled - * with this source code in the file LICENSE. - */ - -declare(strict_types=1); - -namespace thebigcrafter\omp\tasks; - + +/* + * This file is part of oh-my-pmmp. + * + * (c) thebigcrafter + * + * This source file is subject to the GPL-3.0 license that is bundled + * with this source code in the file LICENSE. + */ + +declare(strict_types=1); + +namespace thebigcrafter\omp\tasks; + use pocketmine\scheduler\AsyncTask; use pocketmine\utils\Internet; use thebigcrafter\omp\Language; use thebigcrafter\omp\OhMyPMMP; use function json_decode; -use function version_compare; - +use function version_compare; + final class CheckForUpdates extends AsyncTask { private string $highestVersion; @@ -28,33 +28,33 @@ public function __construct(private readonly string $name, private string $curre { $this->highestVersion = $currentVersion; $this->artifactUrl = ""; - } - + } + public function onRun() : void { - $res = Internet::getURL("https://poggit.pmmp.io/releases.min.json?name=" . $this->name); - - $releases = (array) json_decode($res->getBody(), true); - + $res = Internet::getURL("https://poggit.pmmp.io/releases.min.json?name=" . $this->name); + + $releases = (array) json_decode($res->getBody(), true); + if ($releases !== null) { - /** - * @var array{'version': string, 'artifact_url': string} $release + /** + * @var array{'version': string, 'artifact_url': string} $release */ foreach ($releases as $release) { if (version_compare($this->highestVersion, $release["version"], ">")) { continue; - } - + } + $this->highestVersion = $release["version"]; $this->artifactUrl = $release["artifact_url"]; } - } - + } + if ($this->highestVersion !== $this->currentVersion) { $this->setResult(false); } - } - + } + public function onCompletion() : void { if (!$this->getResult()) { @@ -67,4 +67,4 @@ public function onCompletion() : void ); } } -} +} diff --git a/src/thebigcrafter/omp/utils/Filesystem.php b/src/thebigcrafter/omp/utils/Filesystem.php index e8b7f79..2139d8a 100644 --- a/src/thebigcrafter/omp/utils/Filesystem.php +++ b/src/thebigcrafter/omp/utils/Filesystem.php @@ -33,6 +33,19 @@ public static function remove(string $path) : Generator }); } + public static function rename(string $origin, string $target) : Generator + { + return yield from Await::promise(function (Closure $resolve, Closure $reject) use ($origin, $target) { + $fs = new \Symfony\Component\Filesystem\Filesystem(); + try { + // @phpstan-ignore-next-line + $resolve($fs->rename($origin, $target)); + } catch (IOException $e) { + $reject($e); + } + }); + } + // @phpstan-ignore-next-line public static function exists(iterable|string $files) : bool {