PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
GenerationRequestManager.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 
22 namespace pocketmine\level\generator;
23 
29 
31 
33  protected $server;
35  protected $generationThread;
36 
37  private $internalThreaded;
38  private $externalThreaded;
39 
43  public function __construct(Server $server){
44  $this->server = $server;
45  $this->internalThreaded = new \Threaded();
46  $this->externalThreaded = new \Threaded();
47  $this->generationThread = new GenerationThread($this->internalThreaded, $this->externalThreaded, $server->getLogger(), $server->getLoader());
48  }
49 
55  public function openLevel(Level $level, $generator, array $options = []){
56  $buffer = chr(GenerationManager::PACKET_OPEN_LEVEL) . Binary::writeInt($level->getId()) . Binary::writeInt($level->getSeed()) .
57  Binary::writeShort(strlen($generator)) . $generator . serialize($options);
58 
59  $this->generationThread->pushMainToThreadPacket($buffer);
60  }
61 
65  public function closeLevel(Level $level){
66  $buffer = chr(GenerationManager::PACKET_CLOSE_LEVEL) . Binary::writeInt($level->getId());
67  $this->generationThread->pushMainToThreadPacket($buffer);
68  }
69 
70  public function addNamespace($namespace, $path){
71  $buffer = chr(GenerationManager::PACKET_ADD_NAMESPACE) . Binary::writeShort(strlen($namespace)) . $namespace . $path;
72  $this->generationThread->pushMainToThreadPacket($buffer);
73  }
74 
75  protected function sendChunk($levelID, FullChunk $chunk){
76  $buffer = chr(GenerationManager::PACKET_SEND_CHUNK) . Binary::writeInt($levelID) . chr(strlen($class = get_class($chunk))) . $class . $chunk->toBinary();
77  $this->generationThread->pushMainToThreadPacket($buffer);
78  }
79 
80  public function requestChunk(Level $level, $chunkX, $chunkZ){
81  $buffer = chr(GenerationManager::PACKET_REQUEST_CHUNK) . Binary::writeInt($level->getId()) . Binary::writeInt($chunkX) . Binary::writeInt($chunkZ);
82  $this->generationThread->pushMainToThreadPacket($buffer);
83  }
84 
85  protected function handleRequest($levelID, $chunkX, $chunkZ){
86  if(($level = $this->server->getLevel($levelID)) instanceof Level){
87  $chunk = $level->getChunk($chunkX, $chunkZ, true);
88  if($chunk instanceof FullChunk){
89  $this->sendChunk($levelID, $chunk);
90  }else{
91  throw new ChunkException("Invalid Chunk given");
92  }
93  }else{
94  $buffer = chr(GenerationManager::PACKET_CLOSE_LEVEL) . Binary::writeInt($levelID);
95  $this->generationThread->pushMainToThreadPacket($buffer);
96  }
97  }
98 
99  protected function receiveChunk($levelID, FullChunk $chunk){
100  if(($level = $this->server->getLevel($levelID)) instanceof Level){
101  $level->generateChunkCallback($chunk->getX(), $chunk->getZ(), $chunk);
102  }else{
103  $buffer = chr(GenerationManager::PACKET_CLOSE_LEVEL) . Binary::writeInt($levelID);
104  $this->generationThread->pushMainToThreadPacket($buffer);
105  }
106  }
107 
108  public function process(){
109  $this->handlePackets();
110  }
111 
112  public function handlePackets(){
113  while(strlen($packet = $this->generationThread->readThreadToMainPacket()) > 0){
114  $pid = ord($packet{0});
115  $offset = 1;
116 
117  if($pid === GenerationManager::PACKET_REQUEST_CHUNK){
118  $levelID = Binary::readInt(substr($packet, $offset, 4));
119  $offset += 4;
120  $chunkX = Binary::readInt(substr($packet, $offset, 4));
121  $offset += 4;
122  $chunkZ = Binary::readInt(substr($packet, $offset, 4));
123  $this->handleRequest($levelID, $chunkX, $chunkZ);
124  }elseif($pid === GenerationManager::PACKET_SEND_CHUNK){
125  $levelID = Binary::readInt(substr($packet, $offset, 4));
126  $offset += 4;
127  $len = ord($packet{$offset++});
129  $class = substr($packet, $offset, $len);
130  $offset += $len;
131  $level = $this->server->getLevel($levelID);
132  if($level instanceof Level){
133  $chunk = $class::fromBinary(substr($packet, $offset), $level->getProvider());
134  $this->receiveChunk($levelID, $chunk);
135  }
136  }
137  }
138  }
139 
140  public function shutdown(){
141  $buffer = chr(GenerationManager::PACKET_SHUTDOWN);
142  $this->generationThread->pushMainToThreadPacket($buffer);
143  }
144 
145 
146 }
getChunk($x, $z, $create=false)
Definition: Level.php:1732
static writeShort($value)
Definition: Binary.php:285
openLevel(Level $level, $generator, array $options=[])