mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-23 00:55:57 +00:00
move filesystem-related functions to pocketmine\utils\Filesystem
This commit is contained in:
parent
40da5059c9
commit
3eea2442a7
@ -26,6 +26,7 @@ namespace pocketmine;
|
||||
use pocketmine\network\mcpe\protocol\ProtocolInfo;
|
||||
use pocketmine\plugin\PluginBase;
|
||||
use pocketmine\plugin\PluginManager;
|
||||
use pocketmine\utils\Filesystem;
|
||||
use pocketmine\utils\Utils;
|
||||
use pocketmine\utils\VersionString;
|
||||
use raklib\RakLib;
|
||||
@ -203,7 +204,7 @@ class CrashDump{
|
||||
$error = (array) error_get_last();
|
||||
$error["trace"] = Utils::currentTrace(3); //Skipping CrashDump->baseCrash, CrashDump->construct, Server->crashDump
|
||||
$error["fullFile"] = $error["file"];
|
||||
$error["file"] = Utils::cleanPath($error["file"]);
|
||||
$error["file"] = Filesystem::cleanPath($error["file"]);
|
||||
try{
|
||||
$error["type"] = \ErrorUtils::errorTypeToString($error["type"]);
|
||||
}catch(\InvalidArgumentException $e){
|
||||
@ -262,7 +263,7 @@ class CrashDump{
|
||||
}
|
||||
|
||||
private function determinePluginFromFile(string $filePath, bool $crashFrame) : bool{
|
||||
$frameCleanPath = Utils::cleanPath($filePath); //this will be empty in phar stub
|
||||
$frameCleanPath = Filesystem::cleanPath($filePath); //this will be empty in phar stub
|
||||
if(strpos($frameCleanPath, "plugins") === 0 and file_exists($filePath)){
|
||||
$this->addLine();
|
||||
if($crashFrame){
|
||||
@ -277,7 +278,7 @@ class CrashDump{
|
||||
$file = $reflection->getProperty("file");
|
||||
$file->setAccessible(true);
|
||||
foreach($this->server->getPluginManager()->getPlugins() as $plugin){
|
||||
$filePath = Utils::cleanPath($file->getValue($plugin));
|
||||
$filePath = Filesystem::cleanPath($file->getValue($plugin));
|
||||
if(strpos($frameCleanPath, $filePath) === 0){
|
||||
$this->data["plugin"] = $plugin->getName();
|
||||
$this->addLine("BAD PLUGIN: " . $plugin->getDescription()->getFullName());
|
||||
|
@ -83,6 +83,7 @@ use pocketmine\timings\Timings;
|
||||
use pocketmine\timings\TimingsHandler;
|
||||
use pocketmine\updater\AutoUpdater;
|
||||
use pocketmine\utils\Config;
|
||||
use pocketmine\utils\Filesystem;
|
||||
use pocketmine\utils\Internet;
|
||||
use pocketmine\utils\MainLogger;
|
||||
use pocketmine\utils\Process;
|
||||
@ -1651,7 +1652,7 @@ class Server{
|
||||
|
||||
$errstr = preg_replace('/\s+/', ' ', trim($errstr));
|
||||
|
||||
$errfile = Utils::cleanPath($errfile);
|
||||
$errfile = Filesystem::cleanPath($errfile);
|
||||
|
||||
$this->logger->logException($e, $trace);
|
||||
|
||||
|
@ -30,6 +30,7 @@ use pocketmine\network\mcpe\protocol\ProtocolInfo;
|
||||
use pocketmine\network\Network;
|
||||
use pocketmine\Server;
|
||||
use pocketmine\snooze\SleeperNotifier;
|
||||
use pocketmine\utils\Filesystem;
|
||||
use pocketmine\utils\Utils;
|
||||
use raklib\protocol\EncapsulatedPacket;
|
||||
use raklib\protocol\PacketReliability;
|
||||
@ -156,7 +157,7 @@ class RakLibInterface implements ServerInstance, AdvancedNetworkInterface{
|
||||
$logger->error("Bad packet (error ID $errorId): " . $e->getMessage());
|
||||
|
||||
//intentionally doesn't use logException, we don't want spammy packet error traces to appear in release mode
|
||||
$logger->debug("Origin: " . Utils::cleanPath($e->getFile()) . "(" . $e->getLine() . ")");
|
||||
$logger->debug("Origin: " . Filesystem::cleanPath($e->getFile()) . "(" . $e->getLine() . ")");
|
||||
foreach(Utils::printableTrace($e->getTrace()) as $frame){
|
||||
$logger->debug($frame);
|
||||
}
|
||||
|
78
src/utils/Filesystem.php
Normal file
78
src/utils/Filesystem.php
Normal file
@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\utils;
|
||||
|
||||
use function is_dir;
|
||||
use function is_file;
|
||||
use function ltrim;
|
||||
use function rmdir;
|
||||
use function rtrim;
|
||||
use function scandir;
|
||||
use function str_replace;
|
||||
use function strpos;
|
||||
use function unlink;
|
||||
use const SCANDIR_SORT_NONE;
|
||||
|
||||
final class Filesystem{
|
||||
|
||||
private function __construct(){
|
||||
//NOOP
|
||||
}
|
||||
|
||||
public static function recursiveUnlink(string $dir) : void{
|
||||
if(is_dir($dir)){
|
||||
$objects = scandir($dir, SCANDIR_SORT_NONE);
|
||||
foreach($objects as $object){
|
||||
if($object !== "." and $object !== ".."){
|
||||
if(is_dir($dir . "/" . $object)){
|
||||
self::recursiveUnlink($dir . "/" . $object);
|
||||
}else{
|
||||
unlink($dir . "/" . $object);
|
||||
}
|
||||
}
|
||||
}
|
||||
rmdir($dir);
|
||||
}elseif(is_file($dir)){
|
||||
unlink($dir);
|
||||
}
|
||||
}
|
||||
|
||||
public static function cleanPath($path){
|
||||
$result = str_replace(["\\", ".php", "phar://"], ["/", "", ""], $path);
|
||||
|
||||
//remove relative paths
|
||||
//TODO: make these paths dynamic so they can be unit-tested against
|
||||
static $cleanPaths = [
|
||||
\pocketmine\PLUGIN_PATH => "plugins", //this has to come BEFORE \pocketmine\PATH because it's inside that by default on src installations
|
||||
\pocketmine\PATH => ""
|
||||
];
|
||||
foreach($cleanPaths as $cleanPath => $replacement){
|
||||
$cleanPath = rtrim(str_replace(["\\", "phar://"], ["/", ""], $cleanPath), "/");
|
||||
if(strpos($result, $cleanPath) === 0){
|
||||
$result = ltrim(str_replace($cleanPath, $replacement, $result), "/");
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
@ -167,7 +167,7 @@ class MainLogger extends \AttachableThreadedLogger{
|
||||
//pass
|
||||
}
|
||||
$errstr = preg_replace('/\s+/', ' ', trim($errstr));
|
||||
$errfile = Utils::cleanPath($errfile);
|
||||
$errfile = Filesystem::cleanPath($errfile);
|
||||
|
||||
$message = get_class($e) . ": \"$errstr\" ($errno) in \"$errfile\" at line $errline";
|
||||
$stack = Utils::printableTrace($trace);
|
||||
|
@ -50,8 +50,6 @@ use function getenv;
|
||||
use function gettype;
|
||||
use function implode;
|
||||
use function is_array;
|
||||
use function is_dir;
|
||||
use function is_file;
|
||||
use function is_object;
|
||||
use function is_readable;
|
||||
use function is_string;
|
||||
@ -68,10 +66,7 @@ use function preg_grep;
|
||||
use function preg_match;
|
||||
use function preg_match_all;
|
||||
use function preg_replace;
|
||||
use function rmdir;
|
||||
use function scandir;
|
||||
use function str_pad;
|
||||
use function str_replace;
|
||||
use function str_split;
|
||||
use function stripos;
|
||||
use function strlen;
|
||||
@ -79,13 +74,11 @@ use function strpos;
|
||||
use function substr;
|
||||
use function sys_get_temp_dir;
|
||||
use function trim;
|
||||
use function unlink;
|
||||
use function xdebug_get_function_stack;
|
||||
use const PHP_EOL;
|
||||
use const PHP_INT_MAX;
|
||||
use const PHP_INT_SIZE;
|
||||
use const PHP_MAXPATHLEN;
|
||||
use const SCANDIR_SORT_NONE;
|
||||
use const STR_PAD_LEFT;
|
||||
use const STR_PAD_RIGHT;
|
||||
|
||||
@ -123,7 +116,7 @@ class Utils{
|
||||
//non-class function
|
||||
return $func->getName();
|
||||
}
|
||||
return "closure@" . self::cleanPath($func->getFileName()) . "#L" . $func->getStartLine();
|
||||
return "closure@" . Filesystem::cleanPath($func->getFileName()) . "#L" . $func->getStartLine();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -137,7 +130,7 @@ class Utils{
|
||||
public static function getNiceClassName(object $obj) : string{
|
||||
$reflect = new \ReflectionClass($obj);
|
||||
if($reflect->isAnonymous()){
|
||||
return "anonymous@" . self::cleanPath($reflect->getFileName()) . "#L" . $reflect->getStartLine();
|
||||
return "anonymous@" . Filesystem::cleanPath($reflect->getFileName()) . "#L" . $reflect->getStartLine();
|
||||
}
|
||||
|
||||
return $reflect->getName();
|
||||
@ -442,7 +435,7 @@ class Utils{
|
||||
return gettype($value) . " " . Utils::printable((string) $value);
|
||||
}, $args));
|
||||
}
|
||||
$messages[] = "#$i " . (isset($trace[$i]["file"]) ? self::cleanPath($trace[$i]["file"]) : "") . "(" . (isset($trace[$i]["line"]) ? $trace[$i]["line"] : "") . "): " . (isset($trace[$i]["class"]) ? $trace[$i]["class"] . (($trace[$i]["type"] === "dynamic" or $trace[$i]["type"] === "->") ? "->" : "::") : "") . $trace[$i]["function"] . "(" . Utils::printable($params) . ")";
|
||||
$messages[] = "#$i " . (isset($trace[$i]["file"]) ? Filesystem::cleanPath($trace[$i]["file"]) : "") . "(" . (isset($trace[$i]["line"]) ? $trace[$i]["line"] : "") . "): " . (isset($trace[$i]["class"]) ? $trace[$i]["class"] . (($trace[$i]["type"] === "dynamic" or $trace[$i]["type"] === "->") ? "->" : "::") : "") . $trace[$i]["function"] . "(" . Utils::printable($params) . ")";
|
||||
}
|
||||
return $messages;
|
||||
}
|
||||
@ -475,24 +468,6 @@ class Utils{
|
||||
return self::printableTrace(self::currentTrace(++$skipFrames));
|
||||
}
|
||||
|
||||
public static function cleanPath($path){
|
||||
$result = str_replace(["\\", ".php", "phar://"], ["/", "", ""], $path);
|
||||
|
||||
//remove relative paths
|
||||
//TODO: make these paths dynamic so they can be unit-tested against
|
||||
static $cleanPaths = [
|
||||
\pocketmine\PLUGIN_PATH => "plugins", //this has to come BEFORE \pocketmine\PATH because it's inside that by default on src installations
|
||||
\pocketmine\PATH => ""
|
||||
];
|
||||
foreach($cleanPaths as $cleanPath => $replacement){
|
||||
$cleanPath = rtrim(str_replace(["\\", "phar://"], ["/", ""], $cleanPath), "/");
|
||||
if(strpos($result, $cleanPath) === 0){
|
||||
$result = ltrim(str_replace($cleanPath, $replacement, $result), "/");
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts one-line tags from the doc-comment
|
||||
*
|
||||
@ -543,24 +518,6 @@ class Utils{
|
||||
}
|
||||
}
|
||||
|
||||
public static function recursiveUnlink(string $dir) : void{
|
||||
if(is_dir($dir)){
|
||||
$objects = scandir($dir, SCANDIR_SORT_NONE);
|
||||
foreach($objects as $object){
|
||||
if($object !== "." and $object !== ".."){
|
||||
if(is_dir($dir . "/" . $object)){
|
||||
self::recursiveUnlink($dir . "/" . $object);
|
||||
}else{
|
||||
unlink($dir . "/" . $object);
|
||||
}
|
||||
}
|
||||
}
|
||||
rmdir($dir);
|
||||
}elseif(is_file($dir)){
|
||||
unlink($dir);
|
||||
}
|
||||
}
|
||||
|
||||
public static function checkUTF8(string $string) : void{
|
||||
if(!mb_check_encoding($string, 'UTF-8')){
|
||||
throw new \InvalidArgumentException("Text must be valid UTF-8");
|
||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\world\format\io;
|
||||
|
||||
use pocketmine\utils\Filesystem;
|
||||
use pocketmine\utils\Utils;
|
||||
use pocketmine\world\generator\GeneratorManager;
|
||||
use function basename;
|
||||
@ -98,7 +99,7 @@ class FormatConverter{
|
||||
$convertedOutput = rtrim($this->oldProvider->getPath(), "/\\") . "_converted" . DIRECTORY_SEPARATOR;
|
||||
if(file_exists($convertedOutput)){
|
||||
$this->logger->info("Found previous conversion attempt, deleting...");
|
||||
Utils::recursiveUnlink($convertedOutput);
|
||||
Filesystem::recursiveUnlink($convertedOutput);
|
||||
}
|
||||
$this->newProvider::generate($convertedOutput, $data->getName(), $data->getSeed(), GeneratorManager::getGenerator($data->getGenerator()), $data->getGeneratorOptions());
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user