forked from runvnc/MC-Chunk-Loader
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreadchunk.php
94 lines (72 loc) · 2.19 KB
/
readchunk.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
ini_set('display_errors', true);
$wf = $_SERVER['SCRIPT_FILENAME'];
$pos = strrpos($wf, '/');
$wd = substr($wf, 0, $pos);
$CHUNK_DIR = $wd . '/chunks/';
$REGION_DIR = $wd . '/world/region/';
function floormod($a, $b) { return (($a % $b) + $b) % $b; }
function readChunk($posx, $posz) {
global $REGION_DIR;
// calculate region file to read
$regionX = floor($posx / 32);
$regionZ = floor($posz / 32);
// open region file, seek to header info
$file = gzopen($REGION_DIR . "r.$regionX.$regionZ.mcr", 'r');
$chunkHeaderLoc = 4 * (floormod($posx, 32) + floormod($posz, 32) * 32);
gzseek($file, $chunkHeaderLoc);
$info = unpack('C*', gzread($file, 4));
$chunkDataLoc = ($info[1]<<16)|($info[2]<<8)|($info[3]);
// if chunk hasn't been generated, return empty
if($chunkDataLoc == 0) {
return array();
}
// seek to data, write to gz and return
gzseek($file, $chunkDataLoc * 4096);
$info = unpack('C*', gzread($file, 4));
$chunkLength = ($info[1]<<32)|($info[2]<<16)|($info[3]<<8)|($info[4]);
// read to skip over compression byte
gzread($file, 1);
// skip first two bytes for deflate
gzread($file, 2);
// leave off last four bytes for deflate
$chunkLength -= 4;
$contents = gzread($file, $chunkLength - 1);
$contents = gzinflate($contents);
$data = array_merge(unpack("C*", $contents));
return $data;
}
function jsonFileOut($path) {
if (file_exists($path.'.json.gz')) {
echo file_get_contents($path.'.json.gz');
return true;
} else {
$zd = gzopen($path, "r");
$contents = gzread($zd, 9990000);
gzclose($zd);
$json = json_encode(array_merge(unpack("C*", $contents)));
$gz = gzencode($json);
$zd2 = fopen($path.'.json.gz', 'wb');
fwrite($zd2, $gz);
fclose($zd2);
echo $gz;
return true;
}
}
function jsonChunkOut($posx, $posz) {
global $CHUNK_DIR;
$chunkFile = $CHUNK_DIR . "c.$posx.$posz.json.gz";
if(file_exists($chunkFile)) {
echo file_get_contents($chunkFile);
return true;
} else {
$chunkData = readChunk($posx, $posz);
$gz = gzencode(json_encode($chunkData));
$cf = fopen($chunkFile, 'wb');
fwrite($cf, $gz);
fclose($cf);
echo $gz;
return true;
}
}
?>