Improve the flexibility of WorldProvider registration

WorldProviders now have the following requirements removed:
- __construct() is no longer required to have a specific signature
- static isValid() no longer needs to be implemented (you will still need it for registering, but it can be declared anywhere now)
- static generate() no longer needs to be implemented

This paves the way for more interesting types of world providers that use something other than local disk to store chunks (e.g. a mysql database).

WorldProviderManager no longer accepts class-string<WorldProvider>. Instead, WorldProviderManagerEntry is required, with 2 or 3 callbacks:
- ReadOnlyWorldProviderManager must provide a callback for isValid, and a callback for fromPath
- WritableWorldProviderManagerEntry must provide the same, and also a generate() callback

In practice, this requires zero changes to the WorldProviders themselves, since a WorldProviderManagerEntry can be created like this:
`new WritableWorldProviderManagerEntry(\Closure::fromCallable([LevelDB::class, 'isValid']), fn(string ) => new LevelDB(), \Closure::fromCallable([LevelDB::class, 'generate']))`

This provides identical functionality to before for the provider itself; only registration is changed.
This commit is contained in:
Dylan K. Taylor
2021-07-13 16:53:17 +01:00
parent 654fc9a2a6
commit 676bacbee1
11 changed files with 158 additions and 184 deletions

View File

@@ -34,7 +34,6 @@ use pocketmine\timings\Timings;
use pocketmine\world\format\io\exception\CorruptedWorldException;
use pocketmine\world\format\io\exception\UnsupportedWorldFormatException;
use pocketmine\world\format\io\FormatConverter;
use pocketmine\world\format\io\WorldProvider;
use pocketmine\world\format\io\WorldProviderManager;
use pocketmine\world\format\io\WritableWorldProvider;
use pocketmine\world\generator\GeneratorManager;
@@ -205,11 +204,7 @@ class WorldManager{
$providerClass = array_shift($providers);
try{
/**
* @var WorldProvider $provider
* @see WorldProvider::__construct()
*/
$provider = new $providerClass($path);
$provider = $providerClass->fromPath($path);
}catch(CorruptedWorldException $e){
$this->server->getLogger()->error($this->server->getLanguage()->translateString(KnownTranslationKeys::POCKETMINE_LEVEL_LOADERROR, [$name, "Corruption detected: " . $e->getMessage()]));
return false;
@@ -255,14 +250,12 @@ class WorldManager{
return false;
}
$providerClass = $this->providerManager->getDefault();
$providerEntry = $this->providerManager->getDefault();
$path = $this->getWorldPath($name);
/** @var WritableWorldProvider $providerClass */
$providerClass::generate($path, $name, $options);
$providerEntry->generate($path, $name, $options);
/** @see WritableWorldProvider::__construct() */
$world = new World($this->server, $name, new $providerClass($path), $this->server->getAsyncPool());
$world = new World($this->server, $name, $providerEntry->fromPath($path), $this->server->getAsyncPool());
$this->worlds[$world->getId()] = $world;
$world->setAutoSave($this->autoSave);