diff --git a/src/plugin/DiskResourceProvider.php b/src/plugin/DiskResourceProvider.php deleted file mode 100644 index efdc9cd17..000000000 --- a/src/plugin/DiskResourceProvider.php +++ /dev/null @@ -1,83 +0,0 @@ -file = rtrim(str_replace(DIRECTORY_SEPARATOR, "/", $path), "/") . "/"; - } - - /** - * Gets an embedded resource on the plugin file. - * WARNING: You must close the resource given using fclose() - * - * @return null|resource Resource data, or null - */ - public function getResource(string $filename){ - $filename = rtrim(str_replace(DIRECTORY_SEPARATOR, "/", $filename), "/"); - if(file_exists($this->file . $filename)){ - $resource = fopen($this->file . $filename, "rb"); - if($resource === false) throw new AssumptionFailedError("fopen() should not fail on a file which exists"); - return $resource; - } - - return null; - } - - /** - * Returns all the resources packaged with the plugin in the form ["path/in/resources" => SplFileInfo] - * - * @return \SplFileInfo[] - */ - public function getResources() : array{ - $resources = []; - if(is_dir($this->file)){ - /** @var \SplFileInfo $resource */ - foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->file)) as $resource){ - if($resource->isFile()){ - $path = str_replace(DIRECTORY_SEPARATOR, "/", substr((string) $resource, strlen($this->file))); - $resources[$path] = $resource; - } - } - } - - return $resources; - } -} diff --git a/src/plugin/Plugin.php b/src/plugin/Plugin.php index 49b393e5a..ae64d443b 100644 --- a/src/plugin/Plugin.php +++ b/src/plugin/Plugin.php @@ -34,7 +34,7 @@ use pocketmine\Server; */ interface Plugin{ - public function __construct(PluginLoader $loader, Server $server, PluginDescription $description, string $dataFolder, string $file, ResourceProvider $resourceProvider); + public function __construct(PluginLoader $loader, Server $server, PluginDescription $description, string $dataFolder, string $file, string $resourceFolder); public function isEnabled() : bool; diff --git a/src/plugin/PluginBase.php b/src/plugin/PluginBase.php index c8f3e1c39..a4f9bcf71 100644 --- a/src/plugin/PluginBase.php +++ b/src/plugin/PluginBase.php @@ -39,11 +39,15 @@ use function dirname; use function fclose; use function file_exists; use function fopen; +use function is_dir; use function mkdir; use function rtrim; use function str_contains; +use function str_replace; use function stream_copy_to_stream; +use function strlen; use function strtolower; +use function substr; use function trim; use const DIRECTORY_SEPARATOR; @@ -62,11 +66,13 @@ abstract class PluginBase implements Plugin, CommandExecutor{ private PluginDescription $description, private string $dataFolder, private string $file, - private ResourceProvider $resourceProvider + private string $resourceFolder, ){ $this->dataFolder = rtrim($dataFolder, "/" . DIRECTORY_SEPARATOR) . "/"; //TODO: this is accessed externally via reflection, not unused $this->file = rtrim($file, "/" . DIRECTORY_SEPARATOR) . "/"; + $this->resourceFolder = rtrim(str_replace(DIRECTORY_SEPARATOR, "/", $resourceFolder), "/") . "/"; + $this->configFile = Path::join($this->dataFolder, "config.yml"); $prefix = $this->getDescription()->getPrefix(); @@ -215,7 +221,14 @@ abstract class PluginBase implements Plugin, CommandExecutor{ * @return null|resource Resource data, or null */ public function getResource(string $filename){ - return $this->resourceProvider->getResource($filename); + $filename = rtrim(str_replace(DIRECTORY_SEPARATOR, "/", $filename), "/"); + if(file_exists($this->resourceFolder . $filename)){ + $resource = fopen($this->resourceFolder . $filename, "rb"); + if($resource === false) throw new AssumptionFailedError("fopen() should not fail on a file which exists"); + return $resource; + } + + return null; } /** @@ -254,7 +267,18 @@ abstract class PluginBase implements Plugin, CommandExecutor{ * @return \SplFileInfo[] */ public function getResources() : array{ - return $this->resourceProvider->getResources(); + $resources = []; + if(is_dir($this->resourceFolder)){ + /** @var \SplFileInfo $resource */ + foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resourceFolder)) as $resource){ + if($resource->isFile()){ + $path = str_replace(DIRECTORY_SEPARATOR, "/", substr((string) $resource, strlen($this->resourceFolder))); + $resources[$path] = $resource; + } + } + } + + return $resources; } public function getConfig() : Config{ diff --git a/src/plugin/PluginManager.php b/src/plugin/PluginManager.php index 8e0109eaf..b065f0de9 100644 --- a/src/plugin/PluginManager.php +++ b/src/plugin/PluginManager.php @@ -213,7 +213,7 @@ class PluginManager{ * @var Plugin $plugin * @see Plugin::__construct() */ - $plugin = new $mainClass($loader, $this->server, $description, $dataFolder, $prefixed, new DiskResourceProvider($prefixed . "/resources/")); + $plugin = new $mainClass($loader, $this->server, $description, $dataFolder, $prefixed, $prefixed . "/resources/"); $this->plugins[$plugin->getDescription()->getName()] = $plugin; return $plugin; diff --git a/src/plugin/ResourceProvider.php b/src/plugin/ResourceProvider.php deleted file mode 100644 index 3594d7eee..000000000 --- a/src/plugin/ResourceProvider.php +++ /dev/null @@ -1,41 +0,0 @@ - SplFileInfo] - * - * @return \SplFileInfo[] - */ - public function getResources() : array; -}