forked from equalpants/pigmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
region.cpp
276 lines (239 loc) · 7.72 KB
/
region.cpp
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2011 Michael J. Nelson
//
// This file is part of pigmap.
//
// pigmap is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// pigmap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with pigmap. If not, see <http://www.gnu.org/licenses/>.
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include "region.h"
#include "utils.h"
using namespace std;
struct fcloser
{
FILE *f;
fcloser(FILE *ff) : f(ff) {}
~fcloser() {fclose(f);}
};
FILE* openRegionFile(const RegionIdx& ri, const string& inputpath, bool& anvil)
{
string filename = inputpath + "/region/" + ri.toAnvilFileName();
FILE *anvilfile = fopen(filename.c_str(), "rb");
if (anvilfile != NULL)
{
anvil = true;
return anvilfile;
}
filename = inputpath + "/region/" + ri.toOldFileName();
anvil = false;
return fopen(filename.c_str(), "rb");
}
int RegionFileReader::loadFromFile(const RegionIdx& ri, const string& inputpath)
{
// open file
FILE *f = openRegionFile(ri, inputpath, anvil);
if (f == NULL)
return -1;
fcloser fc(f);
// get file length
fseek(f, 0, SEEK_END);
size_t length = (size_t)ftell(f);
fseek(f, 0, SEEK_SET);
if (length < 4096)
return -2;
// read the header
size_t count = fread(&(offsets[0]), 4096, 1, f);
if (count < 1)
return -2;
// read the rest of the file
chunkdata.resize(length - 4096);
if (length > 4096)
{
count = fread(&(chunkdata[0]), length - 4096, 1, f);
if (count < 1)
return -2;
}
return 0;
}
int RegionFileReader::loadHeaderOnly(const RegionIdx& ri, const string& inputpath)
{
// open file
FILE *f = openRegionFile(ri, inputpath, anvil);
if (f == NULL)
return -1;
fcloser fc(f);
// read the header
size_t count = fread(&(offsets[0]), 4096, 1, f);
if (count < 1)
return -2;
return 0;
}
int RegionFileReader::decompressChunk(const ChunkOffset& co, vector<uint8_t>& buf)
{
// see if chunk is present
if (!containsChunk(co))
return -1;
// attempt to decompress chunk data into buffer
int idx = getIdx(co);
uint32_t sector = getSectorOffset(idx);
if ((sector - 1) * 4096 >= chunkdata.size())
return -2;
uint8_t *chunkstart = &(chunkdata[(sector - 1) * 4096]);
uint32_t datasize = fromBigEndian(*((uint32_t*)chunkstart));
bool okay = readGzOrZlib(chunkstart + 5, datasize - 1, buf);
if (!okay)
return -2;
return 0;
}
int RegionFileReader::getContainedChunks(const RegionIdx& ri, const string84& inputpath, vector<ChunkIdx>& chunks)
{
chunks.clear();
int result = loadHeaderOnly(ri, inputpath.s);
if (0 != result)
return result;
for (RegionChunkIterator it(ri); !it.end; it.advance())
if (containsChunk(it.current))
chunks.push_back(it.current);
return 0;
}
RegionChunkIterator::RegionChunkIterator(const RegionIdx& ri)
: end(false), current(ri.baseChunk()), basechunk(ri.baseChunk())
{
}
void RegionChunkIterator::advance()
{
current.x++;
if (current.x >= basechunk.x + 32)
{
current.x = basechunk.x;
current.z++;
}
if (current.z >= basechunk.z + 32)
end = true;
}
RegionCacheStats& RegionCacheStats::operator+=(const RegionCacheStats& rcs)
{
hits += rcs.hits;
misses += rcs.misses;
read += rcs.read;
skipped += rcs.skipped;
missing += rcs.missing;
reqmissing += rcs.reqmissing;
corrupt += rcs.corrupt;
return *this;
}
int RegionCache::getDecompressedChunk(const PosChunkIdx& ci, vector<uint8_t>& buf, bool& anvil)
{
PosRegionIdx ri = ci.toChunkIdx().getRegionIdx();
int e = getEntryNum(ri);
int state = regiontable.getDiskState(ri);
if (state == RegionSet::REGION_UNKNOWN)
stats.misses++;
else
stats.hits++;
// if we already tried and failed to read this region, don't try again
if (state == RegionSet::REGION_CORRUPTED || state == RegionSet::REGION_MISSING)
{
// actually, it shouldn't even be possible to get here, since the disk state
// flags for all chunks in the region should have been set the first time we failed
cerr << "cache invariant failure! tried to read already-failed region" << endl;
return -1;
}
// if the region is in the cache, try to extract the chunk from it
if (state == RegionSet::REGION_CACHED)
{
// try the "real" cache entry, then the extra readbuf
if (entries[e].ri == ri)
{
anvil = entries[e].regionfile.anvil;
return entries[e].regionfile.decompressChunk(ci.toChunkIdx(), buf);
}
else if (readbuf.ri == ri)
{
anvil = readbuf.regionfile.anvil;
return readbuf.regionfile.decompressChunk(ci.toChunkIdx(), buf);
}
// if it wasn't in one of those two places, it shouldn't have been marked as cached
cerr << "grievous region cache failure!" << endl;
cerr << "[" << ri.x << "," << ri.z << "] [" << entries[e].ri.x << "," << entries[e].ri.z << "] [" << readbuf.ri.x << "," << readbuf.ri.z << "]" << endl;
exit(-1);
}
// if this is a full render and the region is not required, we already know it doesn't exist
bool req = regiontable.isRequired(ri);
if (fullrender && !req)
{
stats.skipped++;
regiontable.setDiskState(ri, RegionSet::REGION_MISSING);
for (RegionChunkIterator it(ri.toRegionIdx()); !it.end; it.advance())
chunktable.setDiskState(it.current, ChunkSet::CHUNK_MISSING);
return -1;
}
// okay, we actually have to read the region from disk, if it's there
readRegionFile(ri);
// check whether the read succeeded; try to extract the chunk if so
state = regiontable.getDiskState(ri);
if (state == RegionSet::REGION_CORRUPTED)
{
stats.corrupt++;
return -1;
}
if (state == RegionSet::REGION_MISSING)
{
if (req)
stats.reqmissing++;
else
stats.missing++;
return -1;
}
// since we've actually just done a read, the region should now be in a real cache entry, not the readbuf
if (state != RegionSet::REGION_CACHED || entries[e].ri != ri)
{
cerr << "grievous region cache failure!" << endl;
cerr << "[" << ri.x << "," << ri.z << "] [" << entries[e].ri.x << "," << entries[e].ri.z << "]" << endl;
exit(-1);
}
stats.read++;
anvil = entries[e].regionfile.anvil;
return entries[e].regionfile.decompressChunk(ci.toChunkIdx(), buf);
}
void RegionCache::readRegionFile(const PosRegionIdx& ri)
{
// forget the data in the readbuf
if (readbuf.ri.valid())
regiontable.setDiskState(readbuf.ri, RegionSet::REGION_UNKNOWN);
readbuf.ri = PosRegionIdx(-1,-1);
// read the region file from disk, if it's there
int result = readbuf.regionfile.loadFromFile(ri.toRegionIdx(), inputpath);
if (result == -1)
{
regiontable.setDiskState(ri, RegionSet::REGION_MISSING);
for (RegionChunkIterator it(ri.toRegionIdx()); !it.end; it.advance())
chunktable.setDiskState(it.current, ChunkSet::CHUNK_MISSING);
return;
}
if (result == -2)
{
regiontable.setDiskState(ri, RegionSet::REGION_CORRUPTED);
for (RegionChunkIterator it(ri.toRegionIdx()); !it.end; it.advance())
chunktable.setDiskState(it.current, ChunkSet::CHUNK_MISSING);
return;
}
// read was successful; evict current tenant of chunk's cache slot (swap it into the readbuf)
int e = getEntryNum(ri);
entries[e].regionfile.swap(readbuf.regionfile);
swap(entries[e].ri, readbuf.ri);
// mark the entry as vaild and the region as cached
entries[e].ri = ri;
regiontable.setDiskState(ri, RegionSet::REGION_CACHED);
}