diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index 3e694d8be..e583ddb1e 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -142,7 +142,7 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run } $this->server->mapName = $this->getProperty("level-name"); $this->server->mapDir = FILE_PATH."worlds/".$this->server->mapName."/"; - if($this->server->mapName === false or trim($this->server->mapName) === "" or !file_exists($this->server->mapDir."chunks.dat")){ + if($this->server->mapName === false or trim($this->server->mapName) === "" or (!file_exists($this->server->mapDir."chunks.dat") and !file_exists($this->server->mapDir."chunks.dat.gz"))){ if($this->server->mapName === false or trim($this->server->mapName) === ""){ $this->server->mapName = "world"; } diff --git a/src/classes/ChunkParser.class.php b/src/classes/ChunkParser.class.php index e20d0d40a..223a9e44d 100644 --- a/src/classes/ChunkParser.class.php +++ b/src/classes/ChunkParser.class.php @@ -59,11 +59,16 @@ class ChunkParser{ } public function loadFile($file){ - if(!file_exists($file)){ + if(ZLIB_EXTENSION === true and file_exists($file.".gz")){ + $this->raw = gzinflate(file_get_contents($file.".gz")); + @unlink($file.".gz"); + file_put_contents($file, $this->raw); + }elseif(!file_exists($file)){ return false; + }else{ + $this->raw = file_get_contents($file); } $this->file = $file; - $this->raw = file_get_contents($file); $this->chunkLength = $this->sectorLength * ord($this->raw{0}); return true; } @@ -147,8 +152,9 @@ class ChunkParser{ console("[DEBUG] Chunks loaded!", true, true, 2); } - public function saveMap(){ + public function saveMap($final = false){ console("[DEBUG] Saving chunks...", true, true, 2); + $fp = fopen($this->file, "r+b"); flock($fp, LOCK_EX); foreach($this->map as $x => $d){ @@ -159,6 +165,12 @@ class ChunkParser{ } flock($fp, LOCK_UN); fclose($fp); + if(ZLIB_EXTENSION === true){ + file_put_contents($this->file .".gz", gzdeflate(file_get_contents($this->file), 9)); + if($final === true){ + @unlink($this->file); + } + } } public function getFloor($x, $z){ diff --git a/src/classes/PocketMinecraftServer.class.php b/src/classes/PocketMinecraftServer.class.php index bab6a24a6..48264d22f 100644 --- a/src/classes/PocketMinecraftServer.class.php +++ b/src/classes/PocketMinecraftServer.class.php @@ -154,7 +154,7 @@ class PocketMinecraftServer extends stdClass{ public function close($reason = "stop"){ if($this->stop !== true){ $this->chat(false, "Stopping server..."); - $this->save(); + $this->save(true); $this->stop = true; $this->trigger("server.close"); $this->interface->close(); @@ -295,10 +295,10 @@ class PocketMinecraftServer extends stdClass{ } } - public function save(){ + public function save($final = false){ if($this->mapName !== false){ file_put_contents($this->mapDir."level.dat", serialize($this->level)); - $this->map->saveMap(); + $this->map->saveMap($final); console("[INFO] Saving entities..."); foreach($this->entities as $entity){ diff --git a/src/common/dependencies.php b/src/common/dependencies.php index e948064af..730d179fc 100644 --- a/src/common/dependencies.php +++ b/src/common/dependencies.php @@ -71,6 +71,13 @@ if(!extension_loaded("sqlite3") and @dl((PHP_SHLIB_SUFFIX === "dll" ? "php_":"") ++$errors; } +if(!extension_loaded("zlib") and @dl((PHP_SHLIB_SUFFIX === "dll" ? "php_":"") . "zlib." . PHP_SHLIB_SUFFIX) === false){ + console("[ERROR] Unable to find Zlib extension. Compressed worlds won't be loaded", true, true, 0); + define("ZLIB_EXTENSION", false); +}else{ + define("ZLIB_EXTENSION", true); +} + if($errors > 0){ die(); }