-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreadchunk.php
137 lines (108 loc) · 3.18 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
ini_set('display_errors', true);
ini_set('memory_limit', '200M');
$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;
}
}
function jsonMultipleOut($liststr) {
global $CHUNK_DIR;
$key = md5($liststr);
$combfile = $CHUNK_DIR. $key. '.json.gz';
if (file_exists($combfile)) {
echo file_get_contents($combfile);
return true;
}
$list = json_decode($liststr);
$combined = array();
$count = count($list);
for ($i=0; $i<$count; $i++) {
$chunk = $list[$i];
if (array_key_exists('x', $chunk)) {
$posx = $chunk->x;
$posz = $chunk->z;
$chunkFile = $CHUNK_DIR . "c.$posx.$posz.json";
if(file_exists($chunkFile)) {
$chunkData = json_decode(file_get_contents($chunkFile));
} else {
$chunkData = readChunk($posx, $posz);
$cf = fopen($chunkFile, 'wb');
fwrite($cf, json_encode($chunkData));
fclose($cf);
}
$combined[] = $chunkData;
}
}
$gz = gzencode(json_encode($combined));
echo $gz;
$ccf = fopen($combfile, 'wb');
fwrite($ccf, $gz);
fclose($ccf);
return true;
}
?>