From 1dc3d42b782b02fa855dfe86f9dc3783e774bb64 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Thu, 10 Apr 2014 16:52:53 +0200 Subject: [PATCH] Working Anvil region format parsing --- src/pocketmine/level/format/anvil/Chunk.php | 61 ++++++++++ .../level/format/anvil/RegionLoader.php | 110 +++++++++++++++++- src/pocketmine/nbt/NBT.php | 27 +++-- .../nbt/tag/{Byte_Array.php => ByteArray.php} | 4 +- src/pocketmine/nbt/tag/Enum.php | 8 +- .../nbt/tag/{Int_Array.php => IntArray.php} | 4 +- 6 files changed, 188 insertions(+), 26 deletions(-) create mode 100644 src/pocketmine/level/format/anvil/Chunk.php rename src/pocketmine/nbt/tag/{Byte_Array.php => ByteArray.php} (93%) rename src/pocketmine/nbt/tag/{Int_Array.php => IntArray.php} (94%) diff --git a/src/pocketmine/level/format/anvil/Chunk.php b/src/pocketmine/level/format/anvil/Chunk.php new file mode 100644 index 000000000..462f193f1 --- /dev/null +++ b/src/pocketmine/level/format/anvil/Chunk.php @@ -0,0 +1,61 @@ +nbt = $nbt; + + if($this->nbt->Entities instanceof Enum){ + $this->nbt->Entities->setTagType(NBT::TAG_Compound); + }else{ + $this->nbt->Entities = new Enum("Entities", array()); + $this->nbt->Entities->setTagType(NBT::TAG_Compound); + } + + if($this->nbt->TileEntities instanceof Enum){ + $this->nbt->TileEntities->setTagType(NBT::TAG_Compound); + }else{ + $this->nbt->TileEntities = new Enum("TileEntities", array()); + $this->nbt->TileEntities->setTagType(NBT::TAG_Compound); + } + + if($this->nbt->TileTicks instanceof Enum){ + $this->nbt->TileTicks->setTagType(NBT::TAG_Compound); + }else{ + $this->nbt->TileTicks = new Enum("TileTicks", array()); + $this->nbt->TileTicks->setTagType(NBT::TAG_Compound); + } + + parent::__construct($level, $this->nbt["xPos"], $this->nbt["zPos"], $sections); + } +} \ No newline at end of file diff --git a/src/pocketmine/level/format/anvil/RegionLoader.php b/src/pocketmine/level/format/anvil/RegionLoader.php index 41f360170..c463acae5 100644 --- a/src/pocketmine/level/format/anvil/RegionLoader.php +++ b/src/pocketmine/level/format/anvil/RegionLoader.php @@ -21,20 +21,34 @@ namespace pocketmine\level\format\anvil; +use pocketmine\level\Level; +use pocketmine\nbt\NBT; +use pocketmine\nbt\tag\Byte; +use pocketmine\nbt\tag\ByteArray; +use pocketmine\nbt\tag\Compound; +use pocketmine\nbt\tag\Enum; +use pocketmine\nbt\tag\Int; +use pocketmine\nbt\tag\IntArray; +use pocketmine\nbt\tag\Long; use pocketmine\utils\Binary; class RegionLoader{ + const VERSION = 1; const COMPRESSION_GZIP = 1; const COMPRESSION_ZLIB = 2; + public static $COMPRESSION_LEVEL = 7; protected $x; protected $z; protected $filePath; protected $filePointer; + protected $lastSector; protected $locationTable = array(); public function __construct($path, $regionX, $regionZ){ - $this->filePath = $path . "r.$regionX.$regionZ.mca"; + $this->x = $regionX; + $this->z = $regionZ; + $this->filePath = $path . "region/r.$regionX.$regionZ.mca"; touch($this->filePath); $this->filePointer = fopen($this->filePath, "r+b"); flock($this->filePointer, LOCK_EX); @@ -56,9 +70,91 @@ class RegionLoader{ } } + public function readChunk($x, $z, $generate = true){ + $index = self::getChunkOffset($x, $z); + if($index < 0 or $index >= 4096){ + return false; + } + + if($this->locationTable[$index][0] === 0 or $this->locationTable[$index][1] === 0){ + if($generate === true){ + //Allocate space + $this->locationTable[$index][0] = ++$this->lastSector; + $this->locationTable[$index][1] = 1; + fseek($this->filePointer, $this->locationTable[$index][0] << 12); + fwrite($this->filePointer, str_pad(Binary::writeInt(-1) . chr(self::COMPRESSION_ZLIB), 4096, "\x00", STR_PAD_RIGHT)); + $this->writeLocationIndex($index); + }else{ + return false; + } + } + + fseek($this->filePointer, $this->locationTable[$index][0] << 12); + $length = Binary::readInt(fread($this->filePointer, 4)); + $compression = ord(fgetc($this->filePointer)); + + if($length <= 0){ //Not yet generated + $this->generateChunk($x, $z); + } + + if($length > ($this->locationTable[$index][1] << 12)){ //Invalid chunk, bigger than defined number of sectors + trigger_error("Corrupted bigger chunk detected", E_USER_WARNING); + $this->locationTable[$index][1] = $length >> 12; + $this->writeLocationIndex($index); + }elseif($compression !== self::COMPRESSION_ZLIB and $compression !== self::COMPRESSION_GZIP){ + trigger_error("Invalid compression type", E_USER_WARNING); + return false; + } + + $nbt = new NBT(NBT::BIG_ENDIAN); + $nbt->readCompressed(fread($this->filePointer, $length - 1), $compression); + $chunk = $nbt->getData()->Level; + + if(!$chunk instanceof Compound){ + return false; + } + } + + public function generateChunk($x, $z){ + $nbt = new Compound("Level", array()); + $nbt->xPos = new Int("xPos", ($this->getX() * 32) + $x); + $nbt->zPos = new Int("xPos", ($this->getZ() * 32) + $z); + $nbt->LastUpdate = new Long("LastUpdate", 0); + $nbt->LightPopulated = new Byte("LightPopulated", 0); + $nbt->TerrainPopulated = new Byte("TerrainPopulated", 0); + $nbt->V = new Byte("V", self::VERSION); + $nbt->InhabitedTime = new Long("InhabitedTime", 0); + $nbt->Biomes = new ByteArray("Biomes", str_repeat(Binary::writeByte(-1), 256)); + $nbt->HeightMap = new IntArray("HeightMap", array_fill(0, 256, 127)); + $nbt->Sections = new Enum("Sections", array()); + $nbt->Sections->setTagType(NBT::TAG_Compound); + $nbt->Entities = new Enum("Entities", array()); + $nbt->Entities->setTagType(NBT::TAG_Compound); + $nbt->TileEntities = new Enum("TileEntities", array()); + $nbt->TileEntities->setTagType(NBT::TAG_Compound); + $nbt->TileTicks = new Enum("TileTicks", array()); + $nbt->TileTicks->setTagType(NBT::TAG_Compound); + $writer = new NBT(NBT::BIG_ENDIAN); + $writer->setData(new Compound("", array($nbt))); + $chunkData = $writer->writeCompressed(self::COMPRESSION_ZLIB, self::$COMPRESSION_LEVEL); + $length = strlen($chunkData) + 1; + $sectors = ($length + 4) >> 12; + $index = self::getChunkOffset($x, $z); + if($this->locationTable[$index][1] < $sectors){ + $this->locationTable[$index][0] = $this->lastSector += $sectors; //The GC will clean this shift later + } + $this->locationTable[$index][1] = $sectors; + + fseek($this->filePointer, $this->locationTable[$index][0] << 12); + fwrite($this->filePointer, str_pad(Binary::writeInt($length) . chr(self::COMPRESSION_ZLIB) . $chunkData, $sectors << 12, "\x00", STR_PAD_RIGHT)); + } + + protected static function getChunkOffset($x, $z){ + return $x + ($z << 5); + } + private function cleanGarbage(){ $sectors = array(); - $maxSector = 1; foreach($this->locationTable as $index => $data){ //Calculate file usage if($data[0] === 0 or $data[1] === 0){ $this->locationTable[$index] = array(0, 0); @@ -66,13 +162,10 @@ class RegionLoader{ } for($i = 0; $i < $data[1]; ++$i){ $sectors[$data[0]] = $index; - if($data[0] > $maxSector){ - $maxSector = $data[0]; - } } } - if(count($sectors) === ($maxSector - 2)){ //No collection needed + if(count($sectors) === ($this->lastSector - 2)){ //No collection needed return 0; } @@ -101,9 +194,13 @@ class RegionLoader{ private function loadLocationTable(){ fseek($this->filePointer, 0); + $this->lastSector = 1; for($i = 0; $i < 1024; ++$i){ $index = Binary::readInt(fread($this->filePointer, 4)); $this->locationTable[$i] = array(($index & ~0xff) >> 8, $index & 0xff); + if(($this->locationTable[$i][0] + $this->locationTable[$i][1] - 1) > $this->lastSector){ + $this->lastSector = $this->locationTable[$i][0] + $this->locationTable[$i][1] - 1; + } } } @@ -122,6 +219,7 @@ class RegionLoader{ private function createBlank(){ fseek($this->filePointer, 0); ftruncate($this->filePointer, 0); + $this->lastSector = 1; for($i = 0; $i < 1024; ++$i){ $this->locationTable[$i] = array(0, 0); fwrite($this->filePointer, Binary::writeInt(0)); diff --git a/src/pocketmine/nbt/NBT.php b/src/pocketmine/nbt/NBT.php index 61cdf2edf..814f59d53 100644 --- a/src/pocketmine/nbt/NBT.php +++ b/src/pocketmine/nbt/NBT.php @@ -25,14 +25,14 @@ namespace pocketmine\nbt; use pocketmine\nbt\tag\Byte; -use pocketmine\nbt\tag\Byte_Array; +use pocketmine\nbt\tag\ByteArray; use pocketmine\nbt\tag\Compound; use pocketmine\nbt\tag\Double; use pocketmine\nbt\tag\End; use pocketmine\nbt\tag\Enum; use pocketmine\nbt\tag\Float; use pocketmine\nbt\tag\Int; -use pocketmine\nbt\tag\Int_Array; +use pocketmine\nbt\tag\IntArray; use pocketmine\nbt\tag\Long; use pocketmine\nbt\tag\NamedTAG; use pocketmine\nbt\tag\Short; @@ -45,6 +45,9 @@ use pocketmine\utils\Utils; * Named Binary Tag encoder/decoder */ class NBT{ + const COMPRESSION_GZIP = 1; + const COMPRESSION_ZLIB = 2; + const LITTLE_ENDIAN = 0; const BIG_ENDIAN = 1; const TAG_End = 0; @@ -54,11 +57,11 @@ class NBT{ const TAG_Long = 4; const TAG_Float = 5; const TAG_Double = 6; - const TAG_Byte_Array = 7; + const TAG_ByteArray = 7; const TAG_String = 8; const TAG_Enum = 9; const TAG_Compound = 10; - const TAG_Int_Array = 11; + const TAG_IntArray = 11; private $buffer; private $offset; @@ -104,8 +107,8 @@ class NBT{ $this->buffer = ""; } - public function readCompressed($buffer){ - $this->read(\gzdecode($buffer)); + public function readCompressed($buffer, $compression = self::COMPRESSION_ZLIB){ + $this->read($compression === self::COMPRESSION_ZLIB ? zlib_decode($buffer) : zlib_decode($buffer)); } public function write(){ @@ -119,9 +122,9 @@ class NBT{ } } - public function writeCompressed(){ + public function writeCompressed($compression = self::COMPRESSION_ZLIB, $level = 7){ if(($write = $this->write()) !== false){ - return \gzencode($write, 9); + return $compression === self::COMPRESSION_ZLIB ? zlib_encode($write, 15, $level) : zlib_encode($write, 31, $level); } return false; @@ -153,8 +156,8 @@ class NBT{ $tag = new Double($this->getString()); $tag->read($this); break; - case NBT::TAG_Byte_Array: - $tag = new Byte_Array($this->getString()); + case NBT::TAG_ByteArray: + $tag = new ByteArray($this->getString()); $tag->read($this); break; case NBT::TAG_String: @@ -169,8 +172,8 @@ class NBT{ $tag = new Compound($this->getString()); $tag->read($this); break; - case NBT::TAG_Int_Array: - $tag = new Int_Array($this->getString()); + case NBT::TAG_IntArray: + $tag = new IntArray($this->getString()); $tag->read($this); break; diff --git a/src/pocketmine/nbt/tag/Byte_Array.php b/src/pocketmine/nbt/tag/ByteArray.php similarity index 93% rename from src/pocketmine/nbt/tag/Byte_Array.php rename to src/pocketmine/nbt/tag/ByteArray.php index ad43191a1..08387696b 100644 --- a/src/pocketmine/nbt/tag/Byte_Array.php +++ b/src/pocketmine/nbt/tag/ByteArray.php @@ -23,10 +23,10 @@ namespace pocketmine\nbt\tag; use pocketmine\nbt\NBT; -class Byte_Array extends NamedTag{ +class ByteArray extends NamedTag{ public function getType(){ - return NBT::TAG_Byte_Array; + return NBT::TAG_ByteArray; } public function read(NBT $nbt){ diff --git a/src/pocketmine/nbt/tag/Enum.php b/src/pocketmine/nbt/tag/Enum.php index ecef94a9a..f53e9d2f9 100644 --- a/src/pocketmine/nbt/tag/Enum.php +++ b/src/pocketmine/nbt/tag/Enum.php @@ -111,8 +111,8 @@ class Enum extends NamedTag implements \ArrayAccess{ $tag->read($nbt); $this->{$i} = $tag; break; - case NBT::TAG_Byte_Array: - $tag = new Byte_Array(false); + case NBT::TAG_ByteArray: + $tag = new ByteArray(false); $tag->read($nbt); $this->{$i} = $tag; break; @@ -131,8 +131,8 @@ class Enum extends NamedTag implements \ArrayAccess{ $tag->read($nbt); $this->{$i} = $tag; break; - case NBT::TAG_Int_Array: - $tag = new Int_Array(false); + case NBT::TAG_IntArray: + $tag = new IntArray(false); $tag->read($nbt); $this->{$i} = $tag; break; diff --git a/src/pocketmine/nbt/tag/Int_Array.php b/src/pocketmine/nbt/tag/IntArray.php similarity index 94% rename from src/pocketmine/nbt/tag/Int_Array.php rename to src/pocketmine/nbt/tag/IntArray.php index 76fa777ee..b47ba7a88 100644 --- a/src/pocketmine/nbt/tag/Int_Array.php +++ b/src/pocketmine/nbt/tag/IntArray.php @@ -23,10 +23,10 @@ namespace pocketmine\nbt\tag; use pocketmine\nbt\NBT; -class Int_Array extends NamedTag{ +class IntArray extends NamedTag{ public function getType(){ - return NBT::TAG_Int_Array; + return NBT::TAG_IntArray; } public function read(NBT $nbt){