From 5e7b870bfc1fe2a7de8ccc2cf7e0a17f7f5a373c Mon Sep 17 00:00:00 2001 From: toby7002 <144540995+toby7002@users.noreply.github.com> Date: Wed, 27 Sep 2023 22:03:20 +0700 Subject: [PATCH] Update checkForUpdates function --- src/thebigcrafter/Hydrogen/Hydrogen.php | 43 ++++++++++++-- .../Hydrogen/future/DeferredFuture.php | 59 +++++++++++++++++++ src/thebigcrafter/Hydrogen/utils/Internet.php | 29 +++++++++ 3 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 src/thebigcrafter/Hydrogen/future/DeferredFuture.php create mode 100644 src/thebigcrafter/Hydrogen/utils/Internet.php diff --git a/src/thebigcrafter/Hydrogen/Hydrogen.php b/src/thebigcrafter/Hydrogen/Hydrogen.php index 135a6ff..b5ac66e 100644 --- a/src/thebigcrafter/Hydrogen/Hydrogen.php +++ b/src/thebigcrafter/Hydrogen/Hydrogen.php @@ -13,9 +13,11 @@ use pocketmine\plugin\Plugin; use pocketmine\Server; +use pocketmine\utils\InternetException; use thebigcrafter\Hydrogen\future\Future; use thebigcrafter\Hydrogen\future\FutureState; use thebigcrafter\Hydrogen\tasks\CheckUpdatesTask; +use thebigcrafter\Hydrogen\utils\Internet; class Hydrogen { @@ -23,20 +25,51 @@ class Hydrogen /** * Notify if an update is available on Poggit. */ - public static function checkForUpdates(Plugin $plugin) : void + public static function checkForUpdates(Plugin $plugin): void { - Server::getInstance()->getAsyncPool()->submitTask(new CheckUpdatesTask($plugin->getName(), $plugin->getDescription()->getVersion())); + + $logger = Server::getInstance()->getLogger(); + $highestVersion = $plugin->getDescription()->getVersion(); + $artifactUrl = ""; + + try { + $res = Internet::fetch("https://poggit.pmmp.io/releases.min.json?name=" . $plugin->getName())->await(); + } catch (InternetException $e) { + Server::getInstance()->getLogger()->debug($e); + } + + $releases = (array) json_decode($res, true); + + if ($releases !== null) { + /** + * @var array{'version': string, 'artifact_url': string} $release + */ + foreach ($releases as $release) { + if (version_compare($highestVersion, $release["version"], ">=")) { + continue; + } + + $highestVersion = $release["version"]; + $artifactUrl = $release["artifact_url"]; + } + } + + if ($highestVersion !== $plugin->getDescription()->getVersion()) { + $artifactUrl .= "/{$plugin->getDescription()->getName()}_{$highestVersion}.phar"; + $logger->notice("{$plugin->getDescription()->getName()} v{$highestVersion} is available for download at {$artifactUrl}"); + } + } /** * Creates a new fiber asynchronously using the given closure, returning a Future that is completed with the * eventual return value of the passed function or will fail if the closure throws an exception. */ - public static function async(\Closure $closure, mixed ...$args) : Future + public static function async(\Closure $closure, mixed ...$args): Future { static $run = null; - $run ??= static function (FutureState $state, \Closure $closure, array $args) : void { + $run ??= static function (FutureState $state, \Closure $closure, array $args): void { $s = $state; $c = $closure; @@ -56,4 +89,4 @@ public static function async(\Closure $closure, mixed ...$args) : Future return new Future($state); } -} +} \ No newline at end of file diff --git a/src/thebigcrafter/Hydrogen/future/DeferredFuture.php b/src/thebigcrafter/Hydrogen/future/DeferredFuture.php new file mode 100644 index 0000000..027998f --- /dev/null +++ b/src/thebigcrafter/Hydrogen/future/DeferredFuture.php @@ -0,0 +1,59 @@ +state = new FutureState(); + $this->future = new Future($this->state); + } + + /** + * Completes the operation with a result value. + * + * @param T $value Result of the operation. + */ + public function complete(mixed $value = null): void + { + $this->state->complete($value); + } + + /** + * Marks the operation as failed. + * + * @param \Throwable $throwable Throwable to indicate the error. + */ + public function error(\Throwable $throwable): void + { + $this->state->error($throwable); + } + + /** + * @return bool True if the operation has completed. + */ + public function isComplete(): bool + { + return $this->state->isComplete(); + } + + /** + * @return Future The future associated with this Deferred. + */ + public function getFuture(): Future + { + return $this->future; + } +} \ No newline at end of file diff --git a/src/thebigcrafter/Hydrogen/utils/Internet.php b/src/thebigcrafter/Hydrogen/utils/Internet.php new file mode 100644 index 0000000..deca8d1 --- /dev/null +++ b/src/thebigcrafter/Hydrogen/utils/Internet.php @@ -0,0 +1,29 @@ +complete($res->getBody()); + }); + + return $deferred->getFuture(); + } +} \ No newline at end of file