mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-20 11:39:43 +00:00
support php8019 and some cleanup (by @D-aHTuCT)
This commit is contained in:
parent
cf0f9df267
commit
b60f0877e1
376 changed files with 6528 additions and 13472 deletions
|
|
@ -1,56 +1,24 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
class PocketChunkParser{
|
||||
|
||||
var $sectorLength = 4096;
|
||||
var $chunkLength = 86016; //16 * 16 * 16
|
||||
var $map; //21 * $sectorLength
|
||||
private $location, $raw = b"", $file;
|
||||
var $sectorLength = 4096; //16 * 16 * 16
|
||||
var $chunkLength = 86016; //21 * $sectorLength
|
||||
var $map;
|
||||
|
||||
function __construct(){
|
||||
$map = array();
|
||||
}
|
||||
|
||||
private function loadLocationTable(){
|
||||
$this->location = array();
|
||||
console("[DEBUG] Loading Chunk Location table...", true, true, 2);
|
||||
for($offset = 0; $offset < 0x1000; $offset += 4){
|
||||
$data = Utils::readLInt(substr($this->raw, $offset, 4));
|
||||
$sectors = $data & 0xff;
|
||||
if($sectors === 0){
|
||||
continue;
|
||||
}
|
||||
$sectorLocation = $data >> 8;
|
||||
$this->location[$offset >> 2] = $sectorLocation * $this->sectorLength;//$this->getOffset($X, $Z, $sectors);
|
||||
}
|
||||
$map = [];
|
||||
}
|
||||
|
||||
public function loadFile($file){
|
||||
if(file_exists($file.".gz")){
|
||||
$this->raw = gzinflate(file_get_contents($file.".gz"));
|
||||
if(file_exists($file . ".gz")){
|
||||
$this->raw = gzinflate(file_get_contents($file . ".gz"));
|
||||
$r = @gzinflate($this->raw);
|
||||
if($r !== false and $r != ""){
|
||||
$this->raw = $r;
|
||||
}
|
||||
@unlink($file.".gz");
|
||||
@unlink($file . ".gz");
|
||||
file_put_contents($file, $this->raw);
|
||||
}elseif(!file_exists($file)){
|
||||
return false;
|
||||
|
|
@ -69,51 +37,14 @@ class PocketChunkParser{
|
|||
return true;
|
||||
}
|
||||
|
||||
private function getOffset($X, $Z){
|
||||
return $this->location[$X + ($Z << 5)];
|
||||
}
|
||||
|
||||
public function getChunk($X, $Z){
|
||||
$X = (int) $X;
|
||||
$Z = (int) $Z;
|
||||
return substr($this->raw, $this->getOffset($X, $Z), $this->chunkLength);
|
||||
}
|
||||
|
||||
public function writeChunk($X, $Z){
|
||||
$X = (int) $X;
|
||||
$Z = (int) $Z;
|
||||
if(!isset($this->map[$X][$Z])){
|
||||
return false;
|
||||
}
|
||||
$chunk = "";
|
||||
foreach($this->map[$X][$Z] as $section => $data){
|
||||
for($i = 0; $i < 256; ++$i){
|
||||
$chunk .= $data[$i];
|
||||
}
|
||||
}
|
||||
return Utils::writeLInt(strlen($chunk)).$chunk;
|
||||
}
|
||||
|
||||
public function parseChunk($X, $Z){
|
||||
$X = (int) $X;
|
||||
$Z = (int) $Z;
|
||||
$offset = $this->getOffset($X, $Z);
|
||||
$len = Utils::readLInt(substr($this->raw, $offset, 4));
|
||||
$offset += 4;
|
||||
$chunk = array(
|
||||
0 => array(), //Block
|
||||
1 => array(), //Data
|
||||
2 => array(), //SkyLight
|
||||
3 => array(), //BlockLight
|
||||
);
|
||||
foreach($chunk as $section => &$data){
|
||||
$l = $section === 0 ? 128:64;
|
||||
for($i = 0; $i < 256; ++$i){
|
||||
$data[$i] = substr($this->raw, $offset, $l);
|
||||
$offset += $l;
|
||||
}
|
||||
}
|
||||
return $chunk;
|
||||
private function getOffset($X, $Z){
|
||||
return $this->location[$X + ($Z << 5)];
|
||||
}
|
||||
|
||||
public function loadMap(){
|
||||
|
|
@ -123,7 +54,7 @@ class PocketChunkParser{
|
|||
$this->loadLocationTable();
|
||||
console("[DEBUG] Loading chunks...", true, true, 2);
|
||||
for($x = 0; $x < 16; ++$x){
|
||||
$this->map[$x] = array();
|
||||
$this->map[$x] = [];
|
||||
for($z = 0; $z < 16; ++$z){
|
||||
$this->map[$x][$z] = $this->parseChunk($x, $z);
|
||||
}
|
||||
|
|
@ -132,6 +63,42 @@ class PocketChunkParser{
|
|||
console("[DEBUG] Chunks loaded!", true, true, 2);
|
||||
}
|
||||
|
||||
private function loadLocationTable(){
|
||||
$this->location = [];
|
||||
console("[DEBUG] Loading Chunk Location table...", true, true, 2);
|
||||
for($offset = 0; $offset < 0x1000; $offset += 4){
|
||||
$data = Utils::readLInt(substr($this->raw, $offset, 4));
|
||||
$sectors = $data & 0xff;
|
||||
if($sectors === 0){
|
||||
continue;
|
||||
}
|
||||
$sectorLocation = $data >> 8;
|
||||
$this->location[$offset >> 2] = $sectorLocation * $this->sectorLength;//$this->getOffset($X, $Z, $sectors);
|
||||
}
|
||||
}
|
||||
|
||||
public function parseChunk($X, $Z){
|
||||
$X = (int) $X;
|
||||
$Z = (int) $Z;
|
||||
$offset = $this->getOffset($X, $Z);
|
||||
$len = Utils::readLInt(substr($this->raw, $offset, 4));
|
||||
$offset += 4;
|
||||
$chunk = [
|
||||
0 => [], //Block
|
||||
1 => [], //Data
|
||||
2 => [], //SkyLight
|
||||
3 => [], //BlockLight
|
||||
];
|
||||
foreach($chunk as $section => &$data){
|
||||
$l = $section === 0 ? 128 : 64;
|
||||
for($i = 0; $i < 256; ++$i){
|
||||
$data[$i] = substr($this->raw, $offset, $l);
|
||||
$offset += $l;
|
||||
}
|
||||
}
|
||||
return $chunk;
|
||||
}
|
||||
|
||||
public function saveMap($final = false){
|
||||
console("[DEBUG] Saving chunks...", true, true, 2);
|
||||
|
||||
|
|
@ -146,14 +113,29 @@ class PocketChunkParser{
|
|||
flock($fp, LOCK_UN);
|
||||
fclose($fp);
|
||||
$original = filesize($this->file);
|
||||
file_put_contents($this->file .".gz", gzdeflate(gzdeflate(file_get_contents($this->file),9),9)); //Double compression for flat maps
|
||||
$compressed = filesize($this->file .".gz");
|
||||
console("[DEBUG] Saved chunks.dat.gz with ".round(($compressed/$original)*100, 2)."% (".round($compressed/1024, 2)."KB) of the original size", true, true, 2);
|
||||
file_put_contents($this->file . ".gz", gzdeflate(gzdeflate(file_get_contents($this->file), 9), 9)); //Double compression for flat maps
|
||||
$compressed = filesize($this->file . ".gz");
|
||||
console("[DEBUG] Saved chunks.dat.gz with " . round(($compressed / $original) * 100, 2) . "% (" . round($compressed / 1024, 2) . "KB) of the original size", true, true, 2);
|
||||
if($final === true){
|
||||
@unlink($this->file);
|
||||
}
|
||||
}
|
||||
|
||||
public function writeChunk($X, $Z){
|
||||
$X = (int) $X;
|
||||
$Z = (int) $Z;
|
||||
if(!isset($this->map[$X][$Z])){
|
||||
return false;
|
||||
}
|
||||
$chunk = "";
|
||||
foreach($this->map[$X][$Z] as $section => $data){
|
||||
for($i = 0; $i < 256; ++$i){
|
||||
$chunk .= $data[$i];
|
||||
}
|
||||
}
|
||||
return Utils::writeLInt(strlen($chunk)) . $chunk;
|
||||
}
|
||||
|
||||
public function getFloor($x, $z){
|
||||
$X = $x >> 4;
|
||||
$Z = $z >> 4;
|
||||
|
|
@ -184,7 +166,7 @@ class PocketChunkParser{
|
|||
}else{
|
||||
$meta = $meta >> 4;
|
||||
}
|
||||
return array($block, $meta);
|
||||
return [$block, $meta];
|
||||
}
|
||||
|
||||
public function getChunkColumn($X, $Z, $x, $z, $type = 0){
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue