-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
toby7002
committed
Oct 1, 2023
1 parent
1ee16c4
commit ec82f9a
Showing
6 changed files
with
156 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of oh-my-pmmp. | ||
* (c) thebigcrafter <[email protected]> | ||
* 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; | ||
|
||
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?fields=name,version,artifact_url,html_url,license,downloads,score,api,deps,description_url,icon_url,tagline"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of oh-my-pmmp. | ||
* (c) thebigcrafter <[email protected]> | ||
* 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\commands; | ||
|
||
class OMPCommand {} | ||
class OMPCommand {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,61 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of oh-my-pmmp. | ||
* (c) thebigcrafter <[email protected]> | ||
* 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\lang; | ||
|
||
use Exception; | ||
use thebigcrafter\Sulfur\Filesystem; | ||
use function array_key_exists; | ||
use function file_exists; | ||
use function json_decode; | ||
|
||
class OMPLanguage | ||
{ | ||
/** @var array<string, string> $languages */ | ||
private static array $languages; | ||
|
||
private string $default; | ||
|
||
public static function loadLanguageFile(string $name, string $path): void | ||
{ | ||
if (!file_exists($path)) { | ||
throw new Exception('File not found.'); | ||
} | ||
self::$languages[$name] = json_decode(\thebigcrafter\omp\utils\Filesystem::readFile($path)->await() ?: '', true); /** @phpstan-ignore-line */ | ||
} | ||
|
||
public function __construct(string $default) | ||
{ | ||
if (!array_key_exists($default, self::$languages)) { | ||
throw new Exception('Language not found'); | ||
} | ||
|
||
$this->default = $default; | ||
} | ||
|
||
public function setDefaultLanguage(string $name): void | ||
{ | ||
if (!array_key_exists($name, self::$languages)) { | ||
throw new Exception('Language not found'); | ||
} | ||
|
||
$this->default = $name; | ||
} | ||
|
||
public function get(string $key): string | ||
{ | ||
if (!array_key_exists($key, self::$languages[$this->default])) { /** @phpstan-ignore-line */ | ||
throw new Exception("$key not found"); | ||
} | ||
|
||
return self::$languages[$this->default][$key]; /** @phpstan-ignore-line */ | ||
} | ||
} | ||
/** @var array<string, string> $languages */ | ||
private static array $languages; | ||
|
||
private string $default; | ||
|
||
public static function loadLanguageFile(string $name, string $path) : void | ||
{ | ||
if (!file_exists($path)) { | ||
throw new Exception('File not found.'); | ||
} | ||
|
||
self::$languages[$name] = json_decode(\thebigcrafter\omp\utils\Filesystem::readFile($path)->await() ?: '', true); /** @phpstan-ignore-line */ | ||
} | ||
|
||
public function __construct(string $default) | ||
{ | ||
if (!array_key_exists($default, self::$languages)) { | ||
throw new Exception('Language not found'); | ||
} | ||
|
||
$this->default = $default; | ||
} | ||
|
||
public function setDefaultLanguage(string $name) : void | ||
{ | ||
if (!array_key_exists($name, self::$languages)) { | ||
throw new Exception('Language not found'); | ||
} | ||
|
||
$this->default = $name; | ||
} | ||
|
||
public function get(string $key) : string | ||
{ | ||
if (!array_key_exists($key, self::$languages[$this->default])) { /** @phpstan-ignore-line */ | ||
throw new Exception("$key not found"); | ||
} | ||
|
||
return self::$languages[$this->default][$key]; /** @phpstan-ignore-line */ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,35 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of oh-my-pmmp. | ||
* (c) thebigcrafter <[email protected]> | ||
* 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\utils; | ||
|
||
use Exception; | ||
use thebigcrafter\Fluorine\Fluorine; | ||
use thebigcrafter\Iodine\DeferredFuture; | ||
use thebigcrafter\Iodine\Future; | ||
use thebigcrafter\Iodine\Iodine; | ||
use thebigcrafter\Sulfur\Path; | ||
use function file_exists; | ||
use function file_get_contents; | ||
|
||
class Filesystem | ||
{ | ||
/** | ||
* @return Future<mixed> | ||
*/ | ||
public static function readFile(string $path): Future | ||
{ | ||
if (!file_exists($path)) { | ||
throw new Exception("File not found"); | ||
} | ||
/** | ||
* @return Future<mixed> | ||
*/ | ||
public static function readFile(string $path) : Future | ||
{ | ||
if (!file_exists($path)) { | ||
throw new Exception("File not found"); | ||
} | ||
|
||
return Iodine::async(function () use ($path) { | ||
return file_get_contents($path); | ||
}); | ||
} | ||
} | ||
return Iodine::async(function () use ($path) { | ||
return file_get_contents($path); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,69 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of oh-my-pmmp. | ||
* (c) thebigcrafter <[email protected]> | ||
* 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\utils; | ||
|
||
use pocketmine\utils\InternetException; | ||
use pocketmine\utils\InternetRequestResult; | ||
use thebigcrafter\Fluorine\Fluorine; | ||
use thebigcrafter\Iodine\DeferredFuture; | ||
use thebigcrafter\Iodine\Future; | ||
use thebigcrafter\Iodine\Iodine; | ||
use function get_headers; | ||
use function is_numeric; | ||
use function round; | ||
|
||
final class Internet | ||
{ | ||
/** | ||
* @return Future<mixed> | ||
*/ | ||
public static function fetch(string $url): Future | ||
{ | ||
return Iodine::async(function () use ($url) { | ||
try { | ||
$res = \pocketmine\utils\Internet::getURL($url); | ||
|
||
if ($res instanceof InternetRequestResult) { | ||
return $res->getBody(); | ||
} | ||
} catch (InternetException $e) { | ||
throw $e; | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @return Future<mixed> | ||
*/ | ||
public static function getRemoteFileSize(string $url): Future | ||
{ | ||
return Iodine::async(function () use ($url) { | ||
$headers = get_headers($url, true); | ||
|
||
if (isset($headers['Content-Length']) && is_numeric($headers['Content-Length'])) { | ||
$bytes = (int) $headers['Content-Length']; | ||
return self::formatFileSize($bytes); | ||
} | ||
|
||
return 'Unknown'; // Default value if Content-Length header is not present or not numeric | ||
}); | ||
} | ||
|
||
private static function formatFileSize(int $bytes): string | ||
{ | ||
if ($bytes < 1024) { | ||
return $bytes . ' B'; | ||
} elseif ($bytes < 1048576) { | ||
return round($bytes / 1024, 2) . ' KB'; | ||
} else { | ||
return round($bytes / 1024 / 1024, 2) . ' MB'; | ||
} | ||
} | ||
} | ||
/** | ||
* @return Future<mixed> | ||
*/ | ||
public static function fetch(string $url) : Future | ||
{ | ||
return Iodine::async(function () use ($url) { | ||
try { | ||
$res = \pocketmine\utils\Internet::getURL($url); | ||
|
||
if ($res instanceof InternetRequestResult) { | ||
return $res->getBody(); | ||
} | ||
} catch (InternetException $e) { | ||
throw $e; | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @return Future<mixed> | ||
*/ | ||
public static function getRemoteFileSize(string $url) : Future | ||
{ | ||
return Iodine::async(function () use ($url) { | ||
$headers = get_headers($url, true); | ||
|
||
if (isset($headers['Content-Length']) && is_numeric($headers['Content-Length'])) { | ||
$bytes = (int) $headers['Content-Length']; | ||
return self::formatFileSize($bytes); | ||
} | ||
|
||
return 'Unknown'; // Default value if Content-Length header is not present or not numeric | ||
}); | ||
} | ||
|
||
private static function formatFileSize(int $bytes) : string | ||
{ | ||
if ($bytes < 1024) { | ||
return $bytes . ' B'; | ||
} elseif ($bytes < 1048576) { | ||
return round($bytes / 1024, 2) . ' KB'; | ||
} else { | ||
return round($bytes / 1024 / 1024, 2) . ' MB'; | ||
} | ||
} | ||
} |