Skip to content

Commit 5e7b870

Browse files
author
toby7002
committed
Update checkForUpdates function
1 parent d1a9e96 commit 5e7b870

File tree

3 files changed

+126
-5
lines changed

3 files changed

+126
-5
lines changed

src/thebigcrafter/Hydrogen/Hydrogen.php

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,63 @@
1313

1414
use pocketmine\plugin\Plugin;
1515
use pocketmine\Server;
16+
use pocketmine\utils\InternetException;
1617
use thebigcrafter\Hydrogen\future\Future;
1718
use thebigcrafter\Hydrogen\future\FutureState;
1819
use thebigcrafter\Hydrogen\tasks\CheckUpdatesTask;
20+
use thebigcrafter\Hydrogen\utils\Internet;
1921

2022
class Hydrogen
2123
{
2224

2325
/**
2426
* Notify if an update is available on Poggit.
2527
*/
26-
public static function checkForUpdates(Plugin $plugin) : void
28+
public static function checkForUpdates(Plugin $plugin): void
2729
{
28-
Server::getInstance()->getAsyncPool()->submitTask(new CheckUpdatesTask($plugin->getName(), $plugin->getDescription()->getVersion()));
30+
31+
$logger = Server::getInstance()->getLogger();
32+
$highestVersion = $plugin->getDescription()->getVersion();
33+
$artifactUrl = "";
34+
35+
try {
36+
$res = Internet::fetch("https://poggit.pmmp.io/releases.min.json?name=" . $plugin->getName())->await();
37+
} catch (InternetException $e) {
38+
Server::getInstance()->getLogger()->debug($e);
39+
}
40+
41+
$releases = (array) json_decode($res, true);
42+
43+
if ($releases !== null) {
44+
/**
45+
* @var array{'version': string, 'artifact_url': string} $release
46+
*/
47+
foreach ($releases as $release) {
48+
if (version_compare($highestVersion, $release["version"], ">=")) {
49+
continue;
50+
}
51+
52+
$highestVersion = $release["version"];
53+
$artifactUrl = $release["artifact_url"];
54+
}
55+
}
56+
57+
if ($highestVersion !== $plugin->getDescription()->getVersion()) {
58+
$artifactUrl .= "/{$plugin->getDescription()->getName()}_{$highestVersion}.phar";
59+
$logger->notice("{$plugin->getDescription()->getName()} v{$highestVersion} is available for download at {$artifactUrl}");
60+
}
61+
2962
}
3063

3164
/**
3265
* Creates a new fiber asynchronously using the given closure, returning a Future that is completed with the
3366
* eventual return value of the passed function or will fail if the closure throws an exception.
3467
*/
35-
public static function async(\Closure $closure, mixed ...$args) : Future
68+
public static function async(\Closure $closure, mixed ...$args): Future
3669
{
3770
static $run = null;
3871

39-
$run ??= static function (FutureState $state, \Closure $closure, array $args) : void {
72+
$run ??= static function (FutureState $state, \Closure $closure, array $args): void {
4073
$s = $state;
4174
$c = $closure;
4275

@@ -56,4 +89,4 @@ public static function async(\Closure $closure, mixed ...$args) : Future
5689
return new Future($state);
5790
}
5891

59-
}
92+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace thebigcrafter\Hydrogen\future;
6+
use thebigcrafter\Hydrogen\trait\ForbidCloning;
7+
use thebigcrafter\Hydrogen\trait\ForbidSerialization;
8+
9+
class DeferredFuture
10+
{
11+
use ForbidCloning;
12+
use ForbidSerialization;
13+
14+
private readonly FutureState $state;
15+
16+
private readonly Future $future;
17+
18+
public function __construct()
19+
{
20+
$this->state = new FutureState();
21+
$this->future = new Future($this->state);
22+
}
23+
24+
/**
25+
* Completes the operation with a result value.
26+
*
27+
* @param T $value Result of the operation.
28+
*/
29+
public function complete(mixed $value = null): void
30+
{
31+
$this->state->complete($value);
32+
}
33+
34+
/**
35+
* Marks the operation as failed.
36+
*
37+
* @param \Throwable $throwable Throwable to indicate the error.
38+
*/
39+
public function error(\Throwable $throwable): void
40+
{
41+
$this->state->error($throwable);
42+
}
43+
44+
/**
45+
* @return bool True if the operation has completed.
46+
*/
47+
public function isComplete(): bool
48+
{
49+
return $this->state->isComplete();
50+
}
51+
52+
/**
53+
* @return Future<T> The future associated with this Deferred.
54+
*/
55+
public function getFuture(): Future
56+
{
57+
return $this->future;
58+
}
59+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace thebigcrafter\Hydrogen\utils;
6+
7+
use pocketmine\utils\InternetException;
8+
use thebigcrafter\Hydrogen\EventLoop;
9+
use thebigcrafter\Hydrogen\future\DeferredFuture;
10+
11+
class Internet
12+
{
13+
public static function fetch(string $url)
14+
{
15+
$deferred = new DeferredFuture();
16+
17+
EventLoop::defer(function () use($deferred, $url) {
18+
$res = \pocketmine\utils\Internet::getURL($url);
19+
20+
if ($res instanceof InternetException) {
21+
throw $res;
22+
}
23+
24+
$deferred->complete($res->getBody());
25+
});
26+
27+
return $deferred->getFuture();
28+
}
29+
}

0 commit comments

Comments
 (0)