-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdatabase.cpp
251 lines (237 loc) · 6.97 KB
/
database.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
#include "server.hpp"
inline bool file_exists(const std::string& name) {
return ( access( name.c_str(), F_OK ) != -1 );
}
/* Database class functions */
Database::Database(const std::string& dir)
: dir(dir),
created_dir(file_exists(dir)),
changedPropsOrProtects(false) {
if (created_dir) {
std::string prop;
std::ifstream file(dir + "props.txt");
while (file.good()) {
std::getline(file, prop/*, '\0'*/);
if (prop.size() > 0) {
size_t keylen = prop.find_first_of(' ');
if (keylen != std::string::npos) {
worldProps[prop.substr(0, keylen)] = prop.substr(keylen + 1);
}
}
prop.clear();
}
file.close();
/*for (auto & kv : worldProps) {
std::cout << "'" << kv.first << "' = '" << kv.second << "'" << std::endl;
}*/
file.open(dir + "pchunks.bin", std::ios::binary);
if(file.good()){
file.seekg(0, std::fstream::end);
const size_t size = file.tellg();
if (size % 8) {
std::cerr << "Protection file corrupted, at: " << dir << ", ignoring." << std::endl;
} else {
const size_t itemsonfile = size / 8;
uint64_t * rankedarr = new uint64_t[itemsonfile];
file.seekg(0);
file.read((char*)rankedarr, size);
for (size_t i = 0; i < itemsonfile; i++) {
rankedChunks.emplace(rankedarr[i]);
}
delete[] rankedarr;
}
}
file.close();
}
}
Database::~Database() {
for(const auto& hdl : handles){
delete hdl.second;
}
handles.clear();
save();
}
void Database::save() {
if (!changedPropsOrProtects) return;
changedPropsOrProtects = false;
if (worldProps.size() > 0) {
std::ofstream file(dir + "props.txt", std::ios_base::trunc);
for (auto & kv : worldProps) {
file << kv.first << " " << kv.second << std::endl;
}
file.flush();
file.close();
} else {
std::remove((dir + "props.txt").c_str());
}
if (rankedChunks.size() > 0) {
std::ofstream file(dir + "pchunks.bin", std::ios_base::binary | std::ios_base::trunc);
if (file.good()) {
uint64_t * rankarr = new uint64_t[rankedChunks.size()];
size_t filesize = 0;
for (uint64_t pos : rankedChunks) {
rankarr[filesize++] = pos;
}
file.write((char*)rankarr, filesize * sizeof(uint64_t));
delete[] rankarr;
}
file.close();
} else {
std::remove((dir + "pchunks.bin").c_str());
}
}
void Database::setChunkProtection(int32_t x, int32_t y, bool state) {
union {
struct {
int32_t x;
int32_t y;
} p;
uint64_t pos;
} s;
s.p.x = x;
s.p.y = y;
//uint64_t p = (*(uint64_t *)&x) << 32 | (*(uint64_t *)&y);
if (state) {
rankedChunks.emplace(s.pos);
} else {
rankedChunks.erase(s.pos);
}
changedPropsOrProtects = true;
}
bool Database::getChunkProtection(int32_t x, int32_t y) {
//uint64_t p = (*(uint64_t *)&x) << 32 | (*(uint64_t *)&y);
union {
struct {
int32_t x;
int32_t y;
} p;
uint64_t pos;
} s;
s.p.x = x;
s.p.y = y;
return rankedChunks.find(s.pos) != rankedChunks.end();
}
std::string Database::getProp(std::string key, std::string defval) {
auto search = worldProps.find(key);
if (search != worldProps.end()) {
return search->second;
}
return defval;
}
void Database::setProp(std::string key, std::string value) {
if (!value.size()) {
worldProps.erase(key);
} else {
worldProps[key] = value;
}
changedPropsOrProtects = true;
}
std::fstream * Database::get_handle(const int32_t x, const int32_t y, const bool create) {
if(!created_dir && create){
created_dir = (mkdir(dir.c_str(), 0700) == 0);
if(!created_dir){
std::cerr << "Could not create directory! (" << strerror(errno) << ")" << std::endl;
return nullptr;
}
}
const int32_t rx = x >> 5;
const int32_t ry = y >> 5;
const std::string mkey(key(rx, ry));
const auto nsearch = nonexistant.find(mkey);
if(nsearch != nonexistant.end() && !create){
return nullptr;
} else if(create){
nonexistant.erase(mkey);
}
const auto search = handles.find(mkey);
std::fstream * handle = nullptr;
if(search == handles.end()){
const std::string path(dir + "r." + std::to_string(rx) + "." + std::to_string(ry) + ".pxr");
if(file_exists(path)){
handle = new std::fstream(path, std::fstream::in | std::fstream::out | std::fstream::binary);
/* std::cout << "Read file: '" << path << "'" << std::endl; */
} else if(create){
handle = new std::fstream(path, std::fstream::in | std::fstream::out | std::fstream::binary | std::fstream::trunc);
if(handle && handle->good()){
uint8_t zero[3072]; /* There must be a better way of doing this */
memset(&zero, 0, sizeof(zero));
handle->write((char *)&zero, sizeof(zero));
/* std::cout << "Made file: '" << path << "'" << std::endl; */
}
} else {
/* We tried reading, didn't find the file, don't try to read it again. */
if(nonexistant.size() >= 512){
nonexistant.clear();
}
nonexistant.emplace(mkey);
}
if(handle && handle->good()){
if(handles.size() > WORLD_MAX_FILE_HANDLES){
auto it = handles.begin();
/* ugly, get first element inserted */
for(auto it2 = handles.begin(); ++it2 != handles.end(); ++it);
/* look at the inline key func for explanation */
/*std::cout << "Closed file handle to: '" << dir << "r."
<< *((int32_t *)it->first.c_str()) << "."
<< *((int32_t *)(it->first.c_str() + sizeof(int32_t))) << ".pxr'" << std::endl;*/
delete it->second;
handles.erase(it);
}
handles[mkey] = handle;
} else if(handle) {
std::cerr << "Could not create/read file in '" << path << "'! (" << strerror(errno) << ")" << std::endl;
delete handle;
handle = nullptr;
}
} else {
handle = search->second;
if(handle && !handle->good()){
std::cerr << "A file handle has gone bad: '" << dir << "/r." << rx << "." << ry << ".pxr'" << std::endl;
delete handle;
handles.erase(search);
/* oh boy, not sure if this will fix anything */
handle = get_handle(x, y, create);
}
}
return handle;
}
bool Database::get_chunk(const int32_t x, const int32_t y, char * const arr) {
std::fstream * const file = get_handle(x, y, false);
if(!file || !file->good()){
return false;
}
file->seekg(0, std::fstream::end);
const size_t size = file->tellg();
const uint32_t lookup = 3 * ((x & 31) + (y & 31) * 32);
file->seekg(lookup);
uint32_t chunkpos = 0;
file->read(((char *)&chunkpos) + 1, 3);
if(chunkpos == 0 || size < 3072 || size - chunkpos < 768){
return false;
}
file->seekg(chunkpos);
file->read(arr, 768);
return true;
}
void Database::set_chunk(const int32_t x, const int32_t y, const char * const arr) {
std::fstream * const file = get_handle(x, y, true);
if(!file || !file->good()){
std::cerr << "Could not save chunk X: " << x << ", Y: " << y << std::endl;
return;
}
file->seekg(0, std::fstream::end);
const size_t size = file->tellg();
const uint32_t lookup = 3 * ((x & 31) + (y & 31) * 32);
file->seekg(lookup);
uint32_t chunkpos = 0;
file->read(((char *)&chunkpos) + 1, 3);
/* TODO: check for corruption */
if(chunkpos == 0){
file->seekp(lookup);
file->write(((char *)&size) + 1, 3);
chunkpos = size;
}
file->seekp(chunkpos);
file->write(arr, 768);
file->flush();
}