PocketMine-MP 5.28.3 git-d5a1007c80fcee27feb2251cf5dcf1ad5a59a85c
Loading...
Searching...
No Matches
AsyncGeneratorExecutor.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\world\generator\executor;
25
29use function array_key_exists;
30
32 private static int $nextAsyncContextId = 1;
33
34 private readonly \Logger $logger;
35
37 private readonly \Closure $workerStartHook;
38
39 private readonly int $asyncContextId;
40
45 private array $generatorRegisteredWorkers = [];
46
47 public function __construct(
48 \Logger $logger,
49 private readonly AsyncPool $workerPool,
50 private readonly GeneratorExecutorSetupParameters $setupParameters,
51 int $asyncContextId = null
52 ){
53 $this->logger = new \PrefixedLogger($logger, "AsyncGeneratorExecutor");
54
55 //TODO: we only allow setting this for PM5 because of PopulationTask uses in plugins
56 $this->asyncContextId = $asyncContextId ?? self::$nextAsyncContextId++;
57
58 $this->workerStartHook = function(int $workerId) : void{
59 if(array_key_exists($workerId, $this->generatorRegisteredWorkers)){
60 $this->logger->debug("Worker $workerId with previously registered generator restarted, flagging as unregistered");
61 unset($this->generatorRegisteredWorkers[$workerId]);
62 }
63 };
64 $this->workerPool->addWorkerStartHook($this->workerStartHook);
65 }
66
67 private function registerGeneratorToWorker(int $worker) : void{
68 $this->logger->debug("Registering generator on worker $worker");
69 $this->workerPool->submitTaskToWorker(new AsyncGeneratorRegisterTask($this->setupParameters, $this->asyncContextId), $worker);
70 $this->generatorRegisteredWorkers[$worker] = true;
71 }
72
73 private function unregisterGenerator() : void{
74 foreach($this->workerPool->getRunningWorkers() as $i){
75 if(isset($this->generatorRegisteredWorkers[$i])){
76 $this->workerPool->submitTaskToWorker(new AsyncGeneratorUnregisterTask($this->asyncContextId), $i);
77 }
78 }
79 $this->generatorRegisteredWorkers = [];
80 }
81
82 public function populate(int $chunkX, int $chunkZ, ?Chunk $centerChunk, array $adjacentChunks, \Closure $onCompletion) : void{
83 $task = new PopulationTask(
84 $this->asyncContextId,
85 $chunkX,
86 $chunkZ,
87 $centerChunk,
88 $adjacentChunks,
89 $onCompletion
90 );
91 $workerId = $this->workerPool->selectWorker();
92 if(!isset($this->workerPool->getRunningWorkers()[$workerId]) && isset($this->generatorRegisteredWorkers[$workerId])){
93 $this->logger->debug("Selected worker $workerId previously had generator registered, but is now offline");
94 unset($this->generatorRegisteredWorkers[$workerId]);
95 }
96 if(!isset($this->generatorRegisteredWorkers[$workerId])){
97 $this->registerGeneratorToWorker($workerId);
98 }
99 $this->workerPool->submitTaskToWorker($task, $workerId);
100 }
101
102 public function shutdown() : void{
103 $this->unregisterGenerator();
104 $this->workerPool->removeWorkerStartHook($this->workerStartHook);
105 }
106}
populate(int $chunkX, int $chunkZ, ?Chunk $centerChunk, array $adjacentChunks, \Closure $onCompletion)