PopulationTask no longer depends on a World object

this means it's now possible to test generation offline without too much hassle.
World::generateChunkCallback() has been removed from public API.
This commit is contained in:
Dylan K. Taylor 2021-11-02 15:40:51 +00:00
parent facfd7c04a
commit 8b3565b75d
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 50 additions and 35 deletions

View File

@ -2889,7 +2889,22 @@ class World implements ChunkManager{
} }
} }
$task = new PopulationTask($this, $chunkX, $chunkZ, $this->loadChunk($chunkX, $chunkZ), $temporaryChunkLoader, $chunkPopulationLockId); $centerChunk = $this->loadChunk($chunkX, $chunkZ);
$adjacentChunks = $this->getAdjacentChunks($chunkX, $chunkZ);
$task = new PopulationTask(
$this->worldId,
$chunkX,
$chunkZ,
$centerChunk,
$adjacentChunks,
function(Chunk $centerChunk, array $adjacentChunks) use ($chunkPopulationLockId, $chunkX, $chunkZ, $temporaryChunkLoader) : void{
if(!$this->isLoaded()){
return;
}
$this->generateChunkCallback($chunkPopulationLockId, $chunkX, $chunkZ, $centerChunk, $adjacentChunks, $temporaryChunkLoader);
}
);
$workerId = $this->workerPool->selectWorker(); $workerId = $this->workerPool->selectWorker();
if(!isset($this->generatorRegisteredWorkers[$workerId])){ if(!isset($this->generatorRegisteredWorkers[$workerId])){
$this->registerGeneratorToWorker($workerId); $this->registerGeneratorToWorker($workerId);
@ -2904,7 +2919,7 @@ class World implements ChunkManager{
* @param Chunk[] $adjacentChunks chunkHash => chunk * @param Chunk[] $adjacentChunks chunkHash => chunk
* @phpstan-param array<int, Chunk> $adjacentChunks * @phpstan-param array<int, Chunk> $adjacentChunks
*/ */
public function generateChunkCallback(ChunkLockId $chunkLockId, int $x, int $z, Chunk $chunk, array $adjacentChunks, ChunkLoader $temporaryChunkLoader) : void{ private function generateChunkCallback(ChunkLockId $chunkLockId, int $x, int $z, Chunk $chunk, array $adjacentChunks, ChunkLoader $temporaryChunkLoader) : void{
Timings::$generationCallback->startTiming(); Timings::$generationCallback->startTiming();
$dirtyChunks = 0; $dirtyChunks = 0;

View File

@ -26,8 +26,6 @@ namespace pocketmine\world\generator;
use pocketmine\data\bedrock\BiomeIds; use pocketmine\data\bedrock\BiomeIds;
use pocketmine\scheduler\AsyncTask; use pocketmine\scheduler\AsyncTask;
use pocketmine\utils\AssumptionFailedError; use pocketmine\utils\AssumptionFailedError;
use pocketmine\world\ChunkLoader;
use pocketmine\world\ChunkLockId;
use pocketmine\world\format\BiomeArray; use pocketmine\world\format\BiomeArray;
use pocketmine\world\format\Chunk; use pocketmine\world\format\Chunk;
use pocketmine\world\format\io\FastChunkSerializer; use pocketmine\world\format\io\FastChunkSerializer;
@ -37,10 +35,11 @@ use function array_map;
use function igbinary_serialize; use function igbinary_serialize;
use function igbinary_unserialize; use function igbinary_unserialize;
/**
* @phpstan-type OnCompletion \Closure(Chunk $centerChunk, array<int, Chunk> $adjacentChunks) : void
*/
class PopulationTask extends AsyncTask{ class PopulationTask extends AsyncTask{
private const TLS_KEY_WORLD = "world"; private const TLS_KEY_ON_COMPLETION = "onCompletion";
private const TLS_KEY_CHUNK_LOADER = "chunkLoader";
private const TLS_KEY_LOCK_ID = "chunkLockId";
/** @var int */ /** @var int */
public $worldId; public $worldId;
@ -54,20 +53,23 @@ class PopulationTask extends AsyncTask{
private string $adjacentChunks; private string $adjacentChunks;
public function __construct(World $world, int $chunkX, int $chunkZ, ?Chunk $chunk, ChunkLoader $temporaryChunkLoader, ChunkLockId $chunkLockId){ /**
$this->worldId = $world->getId(); * @param Chunk[]|null[] $adjacentChunks
* @phpstan-param array<int, Chunk|null> $adjacentChunks
* @phpstan-param OnCompletion $onCompletion
*/
public function __construct(int $worldId, int $chunkX, int $chunkZ, ?Chunk $chunk, array $adjacentChunks, \Closure $onCompletion){
$this->worldId = $worldId;
$this->chunkX = $chunkX; $this->chunkX = $chunkX;
$this->chunkZ = $chunkZ; $this->chunkZ = $chunkZ;
$this->chunk = $chunk !== null ? FastChunkSerializer::serializeTerrain($chunk) : null; $this->chunk = $chunk !== null ? FastChunkSerializer::serializeTerrain($chunk) : null;
$this->adjacentChunks = igbinary_serialize(array_map( $this->adjacentChunks = igbinary_serialize(array_map(
fn(?Chunk $c) => $c !== null ? FastChunkSerializer::serializeTerrain($c) : null, fn(?Chunk $c) => $c !== null ? FastChunkSerializer::serializeTerrain($c) : null,
$world->getAdjacentChunks($chunkX, $chunkZ) $adjacentChunks
)) ?? throw new AssumptionFailedError("igbinary_serialize() returned null"); )) ?? throw new AssumptionFailedError("igbinary_serialize() returned null");
$this->storeLocal(self::TLS_KEY_WORLD, $world); $this->storeLocal(self::TLS_KEY_ON_COMPLETION, $onCompletion);
$this->storeLocal(self::TLS_KEY_CHUNK_LOADER, $temporaryChunkLoader);
$this->storeLocal(self::TLS_KEY_LOCK_ID, $chunkLockId);
} }
public function onRun() : void{ public function onRun() : void{
@ -128,13 +130,12 @@ class PopulationTask extends AsyncTask{
} }
public function onCompletion() : void{ public function onCompletion() : void{
/** @var World $world */ /**
$world = $this->fetchLocal(self::TLS_KEY_WORLD); * @var \Closure $onCompletion
/** @var ChunkLoader $temporaryChunkLoader */ * @phpstan-var OnCompletion $onCompletion
$temporaryChunkLoader = $this->fetchLocal(self::TLS_KEY_CHUNK_LOADER); */
/** @var ChunkLockId $lockId */ $onCompletion = $this->fetchLocal(self::TLS_KEY_ON_COMPLETION);
$lockId = $this->fetchLocal(self::TLS_KEY_LOCK_ID);
if($world->isLoaded()){
$chunk = $this->chunk !== null ? $chunk = $this->chunk !== null ?
FastChunkSerializer::deserializeTerrain($this->chunk) : FastChunkSerializer::deserializeTerrain($this->chunk) :
throw new AssumptionFailedError("Center chunk should never be null"); throw new AssumptionFailedError("Center chunk should never be null");
@ -151,7 +152,6 @@ class PopulationTask extends AsyncTask{
} }
} }
$world->generateChunkCallback($lockId, $this->chunkX, $this->chunkZ, $chunk, $adjacentChunks, $temporaryChunkLoader); $onCompletion($chunk, $adjacentChunks);
}
} }
} }