mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-11 14:05:35 +00:00
Extract a ResourceLoader unit from PluginBase
this will allow addressing the resource accessing crash issue for script plugins, once fully implemented.
This commit is contained in:
parent
67321bb3f6
commit
b5805c2d0a
83
src/pocketmine/plugin/DiskResourceProvider.php
Normal file
83
src/pocketmine/plugin/DiskResourceProvider.php
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
<?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\plugin;
|
||||||
|
|
||||||
|
use function file_exists;
|
||||||
|
use function fopen;
|
||||||
|
use function is_dir;
|
||||||
|
use function rtrim;
|
||||||
|
use function str_replace;
|
||||||
|
use function strlen;
|
||||||
|
use function substr;
|
||||||
|
use const DIRECTORY_SEPARATOR;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides resources from the given plugin directory on disk. The path may be prefixed with a specific access protocol
|
||||||
|
* to enable special types of access.
|
||||||
|
*/
|
||||||
|
class DiskResourceProvider implements ResourceProvider{
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
private $file;
|
||||||
|
|
||||||
|
public function __construct(string $path){
|
||||||
|
$this->file = rtrim($path, "/\\") . "/";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an embedded resource on the plugin file.
|
||||||
|
* WARNING: You must close the resource given using fclose()
|
||||||
|
*
|
||||||
|
* @param string $filename
|
||||||
|
*
|
||||||
|
* @return null|resource Resource data, or null
|
||||||
|
*/
|
||||||
|
public function getResource(string $filename){
|
||||||
|
$filename = rtrim(str_replace("\\", "/", $filename), "/");
|
||||||
|
if(file_exists($this->file . "/resources/" . $filename)){
|
||||||
|
return fopen($this->file . "/resources/" . $filename, "rb");
|
||||||
|
}
|
||||||
|
|
||||||
|
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 . "resources/")){
|
||||||
|
foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->file . "resources/")) as $resource){
|
||||||
|
if($resource->isFile()){
|
||||||
|
$path = str_replace(DIRECTORY_SEPARATOR, "/", substr((string) $resource, strlen($this->file . "resources/")));
|
||||||
|
$resources[$path] = $resource;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $resources;
|
||||||
|
}
|
||||||
|
}
|
@ -34,7 +34,7 @@ use pocketmine\Server;
|
|||||||
*/
|
*/
|
||||||
interface Plugin{
|
interface Plugin{
|
||||||
|
|
||||||
public function __construct(PluginLoader $loader, Server $server, PluginDescription $description, string $dataFolder, string $file);
|
public function __construct(PluginLoader $loader, Server $server, PluginDescription $description, string $dataFolder, string $file, ResourceProvider $resourceProvider);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
|
@ -39,18 +39,13 @@ use function fopen;
|
|||||||
use function gettype;
|
use function gettype;
|
||||||
use function is_array;
|
use function is_array;
|
||||||
use function is_bool;
|
use function is_bool;
|
||||||
use function is_dir;
|
|
||||||
use function is_string;
|
use function is_string;
|
||||||
use function mkdir;
|
use function mkdir;
|
||||||
use function rtrim;
|
use function rtrim;
|
||||||
use function str_replace;
|
|
||||||
use function stream_copy_to_stream;
|
use function stream_copy_to_stream;
|
||||||
use function strlen;
|
|
||||||
use function strpos;
|
use function strpos;
|
||||||
use function strtolower;
|
use function strtolower;
|
||||||
use function substr;
|
|
||||||
use function trim;
|
use function trim;
|
||||||
use const DIRECTORY_SEPARATOR;
|
|
||||||
|
|
||||||
abstract class PluginBase implements Plugin, CommandExecutor{
|
abstract class PluginBase implements Plugin, CommandExecutor{
|
||||||
|
|
||||||
@ -81,15 +76,20 @@ abstract class PluginBase implements Plugin, CommandExecutor{
|
|||||||
/** @var TaskScheduler */
|
/** @var TaskScheduler */
|
||||||
private $scheduler;
|
private $scheduler;
|
||||||
|
|
||||||
public function __construct(PluginLoader $loader, Server $server, PluginDescription $description, string $dataFolder, string $file){
|
/** @var ResourceProvider */
|
||||||
|
private $resourceProvider;
|
||||||
|
|
||||||
|
public function __construct(PluginLoader $loader, Server $server, PluginDescription $description, string $dataFolder, string $file, ResourceProvider $resourceProvider){
|
||||||
$this->loader = $loader;
|
$this->loader = $loader;
|
||||||
$this->server = $server;
|
$this->server = $server;
|
||||||
$this->description = $description;
|
$this->description = $description;
|
||||||
$this->dataFolder = rtrim($dataFolder, "\\/") . "/";
|
$this->dataFolder = rtrim($dataFolder, "\\/") . "/";
|
||||||
|
//TODO: this is accessed externally via reflection, not unused
|
||||||
$this->file = rtrim($file, "\\/") . "/";
|
$this->file = rtrim($file, "\\/") . "/";
|
||||||
$this->configFile = $this->dataFolder . "config.yml";
|
$this->configFile = $this->dataFolder . "config.yml";
|
||||||
$this->logger = new PluginLogger($this);
|
$this->logger = new PluginLogger($this);
|
||||||
$this->scheduler = new TaskScheduler($this->getFullName());
|
$this->scheduler = new TaskScheduler($this->getFullName());
|
||||||
|
$this->resourceProvider = $resourceProvider;
|
||||||
|
|
||||||
$this->onLoad();
|
$this->onLoad();
|
||||||
|
|
||||||
@ -263,12 +263,7 @@ abstract class PluginBase implements Plugin, CommandExecutor{
|
|||||||
* @return null|resource Resource data, or null
|
* @return null|resource Resource data, or null
|
||||||
*/
|
*/
|
||||||
public function getResource(string $filename){
|
public function getResource(string $filename){
|
||||||
$filename = rtrim(str_replace("\\", "/", $filename), "/");
|
return $this->resourceProvider->getResource($filename);
|
||||||
if(file_exists($this->file . "resources/" . $filename)){
|
|
||||||
return fopen($this->file . "resources/" . $filename, "rb");
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -309,17 +304,7 @@ abstract class PluginBase implements Plugin, CommandExecutor{
|
|||||||
* @return \SplFileInfo[]
|
* @return \SplFileInfo[]
|
||||||
*/
|
*/
|
||||||
public function getResources() : array{
|
public function getResources() : array{
|
||||||
$resources = [];
|
return $this->resourceProvider->getResources();
|
||||||
if(is_dir($this->file . "resources/")){
|
|
||||||
foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->file . "resources/")) as $resource){
|
|
||||||
if($resource->isFile()){
|
|
||||||
$path = str_replace(DIRECTORY_SEPARATOR, "/", substr((string) $resource, strlen($this->file . "resources/")));
|
|
||||||
$resources[$path] = $resource;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $resources;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -183,7 +183,7 @@ class PluginManager{
|
|||||||
* @var Plugin $plugin
|
* @var Plugin $plugin
|
||||||
* @see Plugin::__construct()
|
* @see Plugin::__construct()
|
||||||
*/
|
*/
|
||||||
$plugin = new $mainClass($loader, $this->server, $description, $dataFolder, $prefixed);
|
$plugin = new $mainClass($loader, $this->server, $description, $dataFolder, $prefixed, new DiskResourceProvider($prefixed . "/"));
|
||||||
$this->plugins[$plugin->getDescription()->getName()] = $plugin;
|
$this->plugins[$plugin->getDescription()->getName()] = $plugin;
|
||||||
|
|
||||||
return $plugin;
|
return $plugin;
|
||||||
|
43
src/pocketmine/plugin/ResourceProvider.php
Normal file
43
src/pocketmine/plugin/ResourceProvider.php
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
<?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\plugin;
|
||||||
|
|
||||||
|
interface ResourceProvider{
|
||||||
|
/**
|
||||||
|
* Gets an embedded resource on the plugin file.
|
||||||
|
* WARNING: You must close the resource given using fclose()
|
||||||
|
*
|
||||||
|
* @param string $filename
|
||||||
|
*
|
||||||
|
* @return null|resource Resource data, or null
|
||||||
|
*/
|
||||||
|
public function getResource(string $filename);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all the resources packaged with the plugin in the form ["path/in/resources" => SplFileInfo]
|
||||||
|
*
|
||||||
|
* @return \SplFileInfo[]
|
||||||
|
*/
|
||||||
|
public function getResources() : array;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user