public function readChunk($x, $z)
{
$index = self::getChunkOffset($x, $z);
if ($index < 0 or $index >= 4096) {
return null;
}
$this->lastUsed = time();
if (!$this->isChunkGenerated($index)) {
return null;
}
fseek($this->filePointer, $this->locationTable[$index][0] << 12);
$length = Binary::readInt(fread($this->filePointer, 4));
$compression = ord(fgetc($this->filePointer));
if ($length <= 0 or $length > self::MAX_SECTOR_LENGTH) {
//Not yet generated / corrupted
if ($length >= self::MAX_SECTOR_LENGTH) {
$this->locationTable[$index][0] = ++$this->lastSector;
$this->locationTable[$index][1] = 1;
MainLogger::getLogger()->error("Corrupted chunk header detected");
}
return null;
}
if ($length > $this->locationTable[$index][1] << 12) {
//Invalid chunk, bigger than defined number of sectors
MainLogger::getLogger()->error("Corrupted bigger chunk detected");
$this->locationTable[$index][1] = $length >> 12;
$this->writeLocationIndex($index);
} elseif ($compression !== self::COMPRESSION_ZLIB and $compression !== self::COMPRESSION_GZIP) {
MainLogger::getLogger()->error("Invalid compression type");
return null;
}
$chunk = $this->unserializeChunk(fread($this->filePointer, $length - 1));
if ($chunk instanceof FullChunk) {
return $chunk;
} else {
MainLogger::getLogger()->error("Corrupted chunk detected");
return null;
}
}