Skip to content

Commit

Permalink
Finish plugin caching
Browse files Browse the repository at this point in the history
  • Loading branch information
toby7002 committed Oct 1, 2023
1 parent bfc31a7 commit 23c7a42
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 3 deletions.
2 changes: 1 addition & 1 deletion resources/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"text.cache.running": "§eTask is running! Please wait until it is finished",
"text.cache.failed": "§cCould not get Poggit plugins list from Poggit: {%reason}",
"text.cache.successfully": "§a({%count}) Poggit plugins has been cached successfully",
"text.cache.successfully": "§a{{count}} Poggit plugins has been cached successfully",

"command.version.api_version": "§aPocketMine-MP API v{{version}}",
"command.version.php_version": "§aPHP v{{version}}",
Expand Down
9 changes: 9 additions & 0 deletions src/thebigcrafter/omp/OhMyPMMP.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
use pocketmine\plugin\PluginBase;
use pocketmine\utils\SingletonTrait;
use thebigcrafter\Fluorine\Fluorine;
use thebigcrafter\Iodine\Iodine;
use thebigcrafter\omp\cache\PluginsPool;
use thebigcrafter\omp\commands\OMPCommand;
use thebigcrafter\omp\lang\OMPLanguage;
use thebigcrafter\omp\tasks\CachePlugins;
use thebigcrafter\omp\trait\Language;
use thebigcrafter\Sulfur\Path;
use function is_dir;
Expand All @@ -36,6 +39,7 @@ public function onEnable() : void
{
$this->saveDefaultConfig();
$this->loadLanguage();
$this->cachePlugins();
$this->getServer()->getCommandMap()->register("OhMyPMMP", new OMPCommand($this, "ohmypmmp", "Oh My PMMP", ["omp", "oh-my-pmmp"]));

Fluorine::run();
Expand All @@ -62,4 +66,9 @@ public function loadLanguage() : void
OMPLanguage::loadLanguageFile($selectedLanguage, $selectedLanguagePath);
self::setLanguage(new OMPLanguage($selectedLanguage));
}
public function cachePlugins(): void {
Iodine::async(function() {
CachePlugins::run()->await();
})->await();
}
}
2 changes: 1 addition & 1 deletion src/thebigcrafter/omp/Vars.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
namespace thebigcrafter\omp;

final class Vars {
public const POGGIT_REPO_URL = "https://poggit.pmmp.io/releases.min.json?fields=name,version,artifact_url,html_url,license,downloads,score,api,deps,description_url,icon_url,tagline";
public const POGGIT_REPO_URL = "https://poggit.pmmp.io/releases.min.json";
}
83 changes: 83 additions & 0 deletions src/thebigcrafter/omp/cache/PluginCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace thebigcrafter\omp\cache;

class PluginCache
{
/**
* @param string $name
* @param PluginVersion[] $versions
* @param string $tagline
* @param int $downloads
* @param int $score
* @param string $license
*/
public function __construct(private string $name, private array $versions, private string $tagline, private int $downloads, private int $score, private string $license)
{
}

public function getName(): string {
return $this->name;
}

/**
* Return a string list (["1.0.0", "1.0.1"])
* @return string[]
*/
public function getVersions(): array {
$versions = [];

foreach($this->versions as $version) {
$versions[] = $version->getVersion();
}

return $versions;
}

public function getShortDescription(): string {
return $this->tagline;
}

public function getDownloads(): int {
return $this->downloads;
}

public function getScore(): int {
return $this->score;
}

public function getLicense(): string {
return $this->license;
}

/**
* @param string $version Use "latest" to get the latest version
* @return PluginVersion|null
*/
public function getVersion(string $version) : ?PluginVersion {
if($version === "latest") {
$version = $this->getLatestVersion();
}

if(isset($this->versions[$version])) {
return $this->versions[$version];
}
return null;
}

public function getLatestVersion() : string {
$versions = $this->versions;

sort($versions);

return $versions[count($versions) - 1]->getVersion();
}

public function getHomepageByVersion(string $version) : string {
return "https://poggit.pmmp.io/p/" . $this->getName() . "/" . $version;
}

public function pushVersion(PluginVersion $version) : void {
$this->versions[$version->getVersion()] = $version;
}
}
48 changes: 48 additions & 0 deletions src/thebigcrafter/omp/cache/PluginVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace thebigcrafter\omp\cache;

use thebigcrafter\Iodine\Future;
use thebigcrafter\omp\utils\Internet;

class PluginVersion
{
public function __construct(private string $version, private string $artifact_url,private string $description_url, private array $api, private array $deps, private string $changelog_url)
{
}

public function getVersion(): string
{
return $this->version;
}

public function getArtifactUrl(): string
{
return $this->artifact_url;
}

public function getApi(): array
{
return $this->api;
}

public function getDepends(): array
{
return $this->deps;
}

/**
* @return Future<mixed>
*/
public function getSize(): Future {
return Internet::getRemoteFileSize($this->getArtifactUrl());
}

public function getDescriptionUrl(): string {
return $this->description_url;
}

public function getChangelogUrl(): string {
return $this->changelog_url;
}
}
62 changes: 62 additions & 0 deletions src/thebigcrafter/omp/cache/PluginsPool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace thebigcrafter\omp\cache;

use Generator;
use SplObjectStorage;

final class PluginsPool {
/** @var ?SplObjectStorage<PluginCache, mixed> $storage */
private static ?SplObjectStorage $storage = null;

/**
* @return SplObjectStorage<PluginCache, mixed>
*/
public static function getStorage() : SplObjectStorage{
if(self::$storage === null){
self::$storage = new SplObjectStorage(); /** @phpstan-ignore-line */
}

return self::$storage; /** @phpstan-ignore-line */
}

/**
* @param SplObjectStorage<PluginCache, mixed> $storage
* @return void
*/
public static function setStorage(SplObjectStorage $storage) : void{
self::$storage = $storage;
}

public static function add(PluginCache $pluginCache) : void {
self::getStorage()->attach($pluginCache);
}

/**
* @param PluginCache[] $array
*/
public static function addMultiple(array $array) : void {
foreach ($array as $plugin) {
if ($plugin instanceof PluginCache) {
self::getStorage()->attach($plugin);
}
}
}

public static function getNamePlugins() : Generator {
foreach (self::$storage as $pluginCache) {
yield $pluginCache->getName();
}
}

public static function getPluginCacheByName(string $name) : ?PluginCache {
/** @var SplObjectStorage<PluginCache, mixed> $pluginCache */
foreach(self::getStorage() as $pluginCache) {
if($pluginCache->getName() == $name) {
return $pluginCache;
}
}
return null;
}

}
2 changes: 1 addition & 1 deletion src/thebigcrafter/omp/lang/OMPLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function setDefaultLanguage(string $name) : void
*/
public function translate(string $key, array $placeholders = []) : string
{
if (!array_key_exists($key, self::$languages[$this->default])) { /** @phpstan-ignore-line */
if (!array_key_exists($key, (array) self::$languages[$this->default])) {
throw new Exception("$key not found");
}

Expand Down
62 changes: 62 additions & 0 deletions src/thebigcrafter/omp/tasks/CachePlugins.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace thebigcrafter\omp\tasks;

use thebigcrafter\Iodine\Future;
use thebigcrafter\Iodine\Iodine;
use thebigcrafter\omp\cache\PluginCache;
use thebigcrafter\omp\cache\PluginsPool;
use thebigcrafter\omp\cache\PluginVersion;
use thebigcrafter\omp\OhMyPMMP;
use thebigcrafter\omp\utils\Internet;
use thebigcrafter\omp\Vars;

class CachePlugins
{
private static bool $hasCached = false;

public static function hasCached() : bool {
return self::$hasCached;
}

public static function setHasCached(bool $status) : void {
self::$hasCached = $status;
}

public static function run() : Future {
return Iodine::async(function() {
$res = Internet::fetch(Vars::POGGIT_REPO_URL)->await();
$plugins = json_decode($res, true);

sort($plugins);
$pluginCaches = [];

foreach ($plugins as $plugin) {
$name = $plugin["name"];
$license = $plugin["license"] ?? "None";
$downloads = (int) $plugin["downloads"];
$artifactUrl = $plugin["artifact_url"];
/** @var array<array{from: string, to: string}> $api */
$api = (array) $plugin["api"];
/** @var array{from: string, to: string} $apiShift */
$apiShift = array_merge(...$api);
/** @var array<array{name: string, version: string, depRelId: int, isHard: bool}> $deps */
$deps = (array) $plugin["deps"];
$score = (int) $plugin["score"];

if (!isset($pluginCaches[$name])) {
$pluginCaches[$name] = new PluginCache($name, [], $plugin["tagline"], $downloads, $score, $license);
}
$description_url = $plugin["description_url"];
$changelog_url = $plugin["changelog_url"];
$version = new PluginVersion($plugin["version"], $artifactUrl, $description_url, $apiShift, $deps, (string) $changelog_url);
$pluginCache = $pluginCaches[$name];
$pluginCache->pushVersion($version);
}

PluginsPool::addMultiple($pluginCaches);
self::setHasCached(true);
OhMyPMMP::getInstance()->getLogger()->info(OhMyPMMP::getLanguage()->translate("text.cache.successfully", ["count" => count($pluginCaches)]));
});
}
}

0 comments on commit 23c7a42

Please sign in to comment.