From 39658ef63f0b307b30d37f15148d73199ec89efc Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Sun, 18 Aug 2013 21:31:47 +0200 Subject: [PATCH] Automatic recovery of corrupted maps --- src/pmf/Level.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/pmf/Level.php b/src/pmf/Level.php index e35d51368..bf7499951 100644 --- a/src/pmf/Level.php +++ b/src/pmf/Level.php @@ -147,11 +147,22 @@ class PMFLevel extends PMF{ if(($this->levelData["width"] !== 16 and $this->levelData["width"] !== 32) or $this->levelData["height"] !== 8){ return false; } - $this->levelData["extra"] = gzinflate($this->read(Utils::readShort($this->read(2), false))); //Additional custom plugin data - if($this->levelData["extra"] === false){ - return false; + $lastseek = ftell($this->fp); + if(($len = $this->read(2)) === false or ($this->levelData["extra"] = @gzinflate($this->read(Utils::readShort($len, false)))) === false){ //Corruption protection + console("[NOTICE] Empty/corrupt location table detected, forcing recovery"); + fseek($this->fp, $lastseek); + $c = gzdeflate(""); + $this->write(Utils::writeShort(strlen($c)).$c); + $this->payloadOffset = ftell($this->fp); + $this->levelData["extra"] = ""; + $cnt = pow($this->levelData["width"], 2); + for($index = 0; $index < $cnt; ++$index){ + $this->write("\x00\xFF"); //Force index recreation + } + fseek($this->fp, $this->payloadOffset); + }else{ + $this->payloadOffset = ftell($this->fp); } - $this->payloadOffset = ftell($this->fp); return $this->readLocationTable(); }