PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/pocketmine/level/format/mcregion/McRegion.php

https://gitlab.com/koutyan777/Genisys
PHP | 332 lines | 88 code | 27 blank | 217 comment | 7 complexity | 7d164290fe6cf77cf6205aba719e503f MD5 | raw file
  1. <?php
  2. /*
  3. *
  4. * ____ _ _ __ __ _ __ __ ____
  5. * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
  6. * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
  7. * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
  8. * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * @author PocketMine Team
  16. * @link http://www.pocketmine.net/
  17. *
  18. *
  19. */
  20. namespace pocketmine\level\format\mcregion;
  21. use pocketmine\level\format\FullChunk;
  22. use pocketmine\level\format\generic\BaseLevelProvider;
  23. use pocketmine\level\generator\Generator;
  24. use pocketmine\level\Level;
  25. use pocketmine\nbt\NBT;
  26. use pocketmine\nbt\tag\ByteTag;
  27. use pocketmine\nbt\tag\CompoundTag;
  28. use pocketmine\nbt\tag\IntTag;
  29. use pocketmine\nbt\tag\LongTag;
  30. use pocketmine\nbt\tag\StringTag;
  31. use pocketmine\tile\Spawnable;
  32. use pocketmine\utils\BinaryStream;
  33. use pocketmine\utils\ChunkException;
  34. class McRegion extends BaseLevelProvider{
  35. /** @var RegionLoader[] */
  36. protected $regions = [];
  37. /** @var Chunk[] */
  38. protected $chunks = [];
  39. public static function getProviderName(){
  40. return "mcregion";
  41. }
  42. public static function getProviderOrder(){
  43. return self::ORDER_ZXY;
  44. }
  45. public static function usesChunkSection(){
  46. return false;
  47. }
  48. public static function isValid($path){
  49. $isValid = (file_exists($path . "/level.dat") and is_dir($path . "/region/"));
  50. if($isValid){
  51. $files = glob($path . "/region/*.mc*");
  52. foreach($files as $f){
  53. if(strpos($f, ".mca") !== false){ //Anvil
  54. $isValid = false;
  55. break;
  56. }
  57. }
  58. }
  59. return $isValid;
  60. }
  61. public static function generate($path, $name, $seed, $generator, array $options = []){
  62. if(!file_exists($path)){
  63. mkdir($path, 0777, true);
  64. }
  65. if(!file_exists($path . "/region")){
  66. mkdir($path . "/region", 0777);
  67. }
  68. //TODO, add extra details
  69. $levelData = new CompoundTag("Data", [
  70. "hardcore" => new ByteTag("hardcore", 0),
  71. "initialized" => new ByteTag("initialized", 1),
  72. "GameType" => new IntTag("GameType", 0),
  73. "generatorVersion" => new IntTag("generatorVersion", 1), //2 in MCPE
  74. "SpawnX" => new IntTag("SpawnX", 128),
  75. "SpawnY" => new IntTag("SpawnY", 70),
  76. "SpawnZ" => new IntTag("SpawnZ", 128),
  77. "version" => new IntTag("version", 19133),
  78. "DayTime" => new IntTag("DayTime", 0),
  79. "LastPlayed" => new LongTag("LastPlayed", microtime(true) * 1000),
  80. "RandomSeed" => new LongTag("RandomSeed", $seed),
  81. "SizeOnDisk" => new LongTag("SizeOnDisk", 0),
  82. "Time" => new LongTag("Time", 0),
  83. "generatorName" => new StringTag("generatorName", Generator::getGeneratorName($generator)),
  84. "generatorOptions" => new StringTag("generatorOptions", isset($options["preset"]) ? $options["preset"] : ""),
  85. "LevelName" => new StringTag("LevelName", $name),
  86. "GameRules" => new CompoundTag("GameRules", [])
  87. ]);
  88. $nbt = new NBT(NBT::BIG_ENDIAN);
  89. $nbt->setData(new CompoundTag("", [
  90. "Data" => $levelData
  91. ]));
  92. $buffer = $nbt->writeCompressed();
  93. file_put_contents($path . "level.dat", $buffer);
  94. }
  95. public static function getRegionIndex($chunkX, $chunkZ, &$x, &$z){
  96. $x = $chunkX >> 5;
  97. $z = $chunkZ >> 5;
  98. }
  99. public function requestChunkTask($x, $z){
  100. $chunk = $this->getChunk($x, $z, false);
  101. if(!($chunk instanceof Chunk)){
  102. throw new ChunkException("Invalid Chunk sent");
  103. }
  104. if($this->getServer()->asyncChunkRequest){
  105. $task = new ChunkRequestTask($this->getLevel(), $chunk);
  106. $this->getServer()->getScheduler()->scheduleAsyncTask($task);
  107. }else{
  108. $tiles = "";
  109. if(count($chunk->getTiles()) > 0){
  110. $nbt = new NBT(NBT::LITTLE_ENDIAN);
  111. $list = [];
  112. foreach($chunk->getTiles() as $tile){
  113. if($tile instanceof Spawnable){
  114. $list[] = $tile->getSpawnCompound();
  115. }
  116. }
  117. $nbt->setData($list);
  118. $tiles = $nbt->write();
  119. }
  120. $extraData = new BinaryStream();
  121. $extraData->putLInt(count($chunk->getBlockExtraDataArray()));
  122. foreach($chunk->getBlockExtraDataArray() as $key => $value){
  123. $extraData->putLInt($key);
  124. $extraData->putLShort($value);
  125. }
  126. $ordered = $chunk->getBlockIdArray() .
  127. $chunk->getBlockDataArray() .
  128. $chunk->getBlockSkyLightArray() .
  129. $chunk->getBlockLightArray() .
  130. pack("C*", ...$chunk->getHeightMapArray()) .
  131. pack("N*", ...$chunk->getBiomeColorArray()) .
  132. $extraData->getBuffer() .
  133. $tiles;
  134. $this->getLevel()->chunkRequestCallback($x, $z, $ordered);
  135. }
  136. return null;
  137. }
  138. public function unloadChunks(){
  139. foreach($this->chunks as $chunk){
  140. $this->unloadChunk($chunk->getX(), $chunk->getZ(), false);
  141. }
  142. $this->chunks = [];
  143. }
  144. public function getGenerator(){
  145. return $this->levelData["generatorName"];
  146. }
  147. public function getGeneratorOptions(){
  148. return ["preset" => $this->levelData["generatorOptions"]];
  149. }
  150. public function getLoadedChunks(){
  151. return $this->chunks;
  152. }
  153. public function isChunkLoaded($x, $z){
  154. return isset($this->chunks[Level::chunkHash($x, $z)]);
  155. }
  156. public function saveChunks(){
  157. foreach($this->chunks as $chunk){
  158. $this->saveChunk($chunk->getX(), $chunk->getZ());
  159. }
  160. }
  161. public function doGarbageCollection(){
  162. $limit = time() - 300;
  163. foreach($this->regions as $index => $region){
  164. if($region->lastUsed <= $limit){
  165. $region->close();
  166. unset($this->regions[$index]);
  167. }
  168. }
  169. }
  170. public function loadChunk($chunkX, $chunkZ, $create = false){
  171. $index = Level::chunkHash($chunkX, $chunkZ);
  172. if(isset($this->chunks[$index])){
  173. return true;
  174. }
  175. $regionX = $regionZ = null;
  176. self::getRegionIndex($chunkX, $chunkZ, $regionX, $regionZ);
  177. $this->loadRegion($regionX, $regionZ);
  178. $this->level->timings->syncChunkLoadDataTimer->startTiming();
  179. $chunk = $this->getRegion($regionX, $regionZ)->readChunk($chunkX - $regionX * 32, $chunkZ - $regionZ * 32);
  180. if($chunk === null and $create){
  181. $chunk = $this->getEmptyChunk($chunkX, $chunkZ);
  182. }
  183. $this->level->timings->syncChunkLoadDataTimer->stopTiming();
  184. if($chunk !== null){
  185. $this->chunks[$index] = $chunk;
  186. return true;
  187. }else{
  188. return false;
  189. }
  190. }
  191. public function getEmptyChunk($chunkX, $chunkZ){
  192. return Chunk::getEmptyChunk($chunkX, $chunkZ, $this);
  193. }
  194. public function unloadChunk($x, $z, $safe = true){
  195. $chunk = isset($this->chunks[$index = Level::chunkHash($x, $z)]) ? $this->chunks[$index] : null;
  196. if($chunk instanceof FullChunk and $chunk->unload(false, $safe)){
  197. unset($this->chunks[$index]);
  198. return true;
  199. }
  200. return false;
  201. }
  202. public function saveChunk($x, $z){
  203. if($this->isChunkLoaded($x, $z)){
  204. $this->getRegion($x >> 5, $z >> 5)->writeChunk($this->getChunk($x, $z));
  205. return true;
  206. }
  207. return false;
  208. }
  209. /**
  210. * @param $x
  211. * @param $z
  212. *
  213. * @return RegionLoader
  214. */
  215. protected function getRegion($x, $z){
  216. return isset($this->regions[$index = Level::chunkHash($x, $z)]) ? $this->regions[$index] : null;
  217. }
  218. /**
  219. * @param int $chunkX
  220. * @param int $chunkZ
  221. * @param bool $create
  222. *
  223. * @return Chunk
  224. */
  225. public function getChunk($chunkX, $chunkZ, $create = false){
  226. $index = Level::chunkHash($chunkX, $chunkZ);
  227. if(isset($this->chunks[$index])){
  228. return $this->chunks[$index];
  229. }else{
  230. $this->loadChunk($chunkX, $chunkZ, $create);
  231. return isset($this->chunks[$index]) ? $this->chunks[$index] : null;
  232. }
  233. }
  234. public function setChunk($chunkX, $chunkZ, FullChunk $chunk){
  235. if(!($chunk instanceof Chunk)){
  236. throw new ChunkException("Invalid Chunk class");
  237. }
  238. $chunk->setProvider($this);
  239. self::getRegionIndex($chunkX, $chunkZ, $regionX, $regionZ);
  240. $this->loadRegion($regionX, $regionZ);
  241. $chunk->setX($chunkX);
  242. $chunk->setZ($chunkZ);
  243. if(isset($this->chunks[$index = Level::chunkHash($chunkX, $chunkZ)]) and $this->chunks[$index] !== $chunk){
  244. $this->unloadChunk($chunkX, $chunkZ, false);
  245. }
  246. $this->chunks[$index] = $chunk;
  247. }
  248. public static function createChunkSection($Y){
  249. return null;
  250. }
  251. public function isChunkGenerated($chunkX, $chunkZ){
  252. if(($region = $this->getRegion($chunkX >> 5, $chunkZ >> 5)) !== null){
  253. return $region->chunkExists($chunkX - $region->getX() * 32, $chunkZ - $region->getZ() * 32) and $this->getChunk($chunkX - $region->getX() * 32, $chunkZ - $region->getZ() * 32, true)->isGenerated();
  254. }
  255. return false;
  256. }
  257. public function isChunkPopulated($chunkX, $chunkZ){
  258. $chunk = $this->getChunk($chunkX, $chunkZ);
  259. if($chunk !== null){
  260. return $chunk->isPopulated();
  261. }else{
  262. return false;
  263. }
  264. }
  265. protected function loadRegion($x, $z){
  266. if(!isset($this->regions[$index = Level::chunkHash($x, $z)])){
  267. $this->regions[$index] = new RegionLoader($this, $x, $z);
  268. }
  269. }
  270. public function close(){
  271. $this->unloadChunks();
  272. foreach($this->regions as $index => $region){
  273. $region->close();
  274. unset($this->regions[$index]);
  275. }
  276. $this->level = null;
  277. }
  278. }