-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathBlockLoader.cpp
340 lines (297 loc) · 11.6 KB
/
BlockLoader.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "stdafx.h"
#include "BlockLoader.h"
#include <Vorb/io/IOManager.h>
#include <Vorb/io/Keg.h>
#include "BlockPack.h"
#include "Chunk.h"
#include "Errors.h"
#include "GameManager.h"
#include "VoxelBits.h"
#define BLOCK_MAPPING_PATH "BlockMapping.ini"
#define BLOCK_DATA_PATH "BlockData.yml"
#define BINARY_CACHE_PATH "BlockCache.bin"
bool BlockLoader::loadBlocks(const vio::IOManager& iom, BlockPack* pack) {
// Load existing mapping if there is one
tryLoadMapping(iom, BLOCK_MAPPING_PATH, pack);
// Check for binary cache
vio::Path binPath;
bool useCache = false;
if (iom.resolvePath(BINARY_CACHE_PATH, binPath)) {
vio::Path dataPath;
if (!iom.resolvePath(BLOCK_DATA_PATH, dataPath)) return false;
if (binPath.getLastModTime() >= dataPath.getLastModTime()) {
useCache = true;
}
}
// Clear CA physics cache
CaPhysicsType::clearTypes();
GameBlockPostProcess bpp(&iom, &CaPhysicsType::typesCache);
pack->onBlockAddition += bpp.del;
if (useCache) {
if (!BlockLoader::loadBinary(iom, BINARY_CACHE_PATH, pack)) {
printf("Failed to load binary cache %s\n", BINARY_CACHE_PATH);
if (!BlockLoader::load(iom, BLOCK_DATA_PATH, pack)) {
pack->onBlockAddition -= bpp.del;
return false;
}
}
} else {
if (!BlockLoader::load(iom, BLOCK_DATA_PATH, pack)) {
pack->onBlockAddition -= bpp.del;
return false;
}
}
pack->onBlockAddition -= bpp.del;
saveMapping(iom, BLOCK_MAPPING_PATH, pack);
if (!useCache) {
// saveBinary(iom, BINARY_CACHE_PATH, pack);
}
return true;
}
// Conditional keg write
#define COND_WRITE_KEG(key, var) if (b.var != d.var) { writer.push(keg::WriterParam::KEY) << nString(key); writer.push(keg::WriterParam::VALUE) << b.var; }
bool BlockLoader::saveBlocks(const nString& filePath, BlockPack* pack) {
// Open the portal to Hell
std::ofstream file(filePath);
if (file.fail()) return false;
BlockPack& blocks = *pack;
std::map<nString, const Block*> sortMap;
const std::vector<Block>& blockList = blocks.getBlockList();
for (size_t i = 0; i < blockList.size(); i++) {
const Block& b = blockList[i];
if (b.active) {
sortMap[b.sID] = &b;
}
}
// Default block
Block d;
// Emit data
keg::YAMLWriter writer;
writer.push(keg::WriterParam::BEGIN_MAP);
for (auto& it : sortMap) {
const Block& b = *it.second;
// Write the block name first
writer.push(keg::WriterParam::KEY) << b.sID;
// Write the block data now
writer.push(keg::WriterParam::VALUE);
writer.push(keg::WriterParam::BEGIN_MAP);
COND_WRITE_KEG("allowsLight", allowLight);
COND_WRITE_KEG("collide", collide);
COND_WRITE_KEG("crushable", isCrushable);
COND_WRITE_KEG("explosionPower", explosivePower);
COND_WRITE_KEG("explosionPowerLoss", explosionPowerLoss);
COND_WRITE_KEG("explosionRays", explosionRays);
COND_WRITE_KEG("explosionResistance", explosionResistance);
COND_WRITE_KEG("flammability", flammability);
COND_WRITE_KEG("floatingAction", floatingAction);
if (b.colorFilter != d.colorFilter) { writer.push(keg::WriterParam::KEY) << nString("lightColorFilter"); writer.push(keg::WriterParam::VALUE) << keg::kegf32v3(b.colorFilter); }
if (b.meshType != d.meshType) {
writer.push(keg::WriterParam::KEY) << nString("meshType");
switch (b.meshType) {
case MeshType::NONE:
writer.push(keg::WriterParam::VALUE) << nString("none");
break;
case MeshType::BLOCK:
writer.push(keg::WriterParam::VALUE) << nString("cube");
break;
case MeshType::LEAVES:
writer.push(keg::WriterParam::VALUE) << nString("leaves");
break;
case MeshType::TRIANGLE:
writer.push(keg::WriterParam::VALUE) << nString("triangle");
break;
case MeshType::CROSSFLORA:
writer.push(keg::WriterParam::VALUE) << nString("cross");
break;
case MeshType::LIQUID:
writer.push(keg::WriterParam::VALUE) << nString("liquid");
break;
case MeshType::FLAT:
writer.push(keg::WriterParam::VALUE) << nString("flat");
break;
}
}
COND_WRITE_KEG("moveMod", moveMod);
COND_WRITE_KEG("name", name);
switch (b.occlude) {
case BlockOcclusion::NONE:
writer.push(keg::WriterParam::KEY) << nString("occlusion");
writer.push(keg::WriterParam::VALUE) << nString("none");
break;
case BlockOcclusion::ALL:
writer.push(keg::WriterParam::KEY) << nString("occlusion");
writer.push(keg::WriterParam::VALUE) << nString("all");
break;
case BlockOcclusion::SELF:
writer.push(keg::WriterParam::KEY) << nString("occlusion");
writer.push(keg::WriterParam::VALUE) << nString("self");
break;
case BlockOcclusion::SELF_ONLY:
writer.push(keg::WriterParam::KEY) << nString("occlusion");
writer.push(keg::WriterParam::VALUE) << nString("selfOnly");
break;
}
COND_WRITE_KEG("sinkID", sinkID);
COND_WRITE_KEG("spawnerID", spawnerID);
COND_WRITE_KEG("supportive", isSupportive);
COND_WRITE_KEG("waterBreak", waterBreak);
//keg::write((ui8*)b, writer, keg::getGlobalEnvironment(), &KEG_GLOBAL_TYPE(Block));
writer.push(keg::WriterParam::END_MAP);
}
writer.push(keg::WriterParam::END_MAP);
file << writer.c_str();
file.flush();
file.close();
return true;
}
bool BlockLoader::load(const vio::IOManager& iom, const cString filePath, BlockPack* pack) {
// Read file
nString data;
iom.readFileToString(filePath, data);
if (data.empty()) return false;
// Convert to YAML
keg::ReadContext context;
context.env = keg::getGlobalEnvironment();
context.reader.init(data.c_str());
keg::Node node = context.reader.getFirst();
if (keg::getType(node) != keg::NodeType::MAP) {
context.reader.dispose();
return false;
}
// Load all block nodes
std::vector<Block> loadedBlocks;
auto f = makeFunctor([&] (Sender, const nString& key, keg::Node value) {
// Add a block
loadedBlocks.emplace_back();
Block& b = loadedBlocks.back();
// Set sID to key
b.sID = key;
// Load data
keg::parse((ui8*)&b, value, context, &KEG_GLOBAL_TYPE(Block));
});
context.reader.forAllInMap(node, &f);
context.reader.dispose();
// Add blocks to pack
for (auto& b : loadedBlocks) {
pack->append(b);
}
return true;
}
GameBlockPostProcess::GameBlockPostProcess(const vio::IOManager* iom, CaPhysicsTypeDict* caCache) :
m_iom(iom),
m_caCache(caCache) {
del = makeDelegate(this, &GameBlockPostProcess::invoke);
}
void GameBlockPostProcess::invoke(Sender s, ui16 id) {
Block& block = ((BlockPack*)s)->operator[](id);
block.active = true;
// Pack light color
block.lightColorPacked =
((ui16)block.lightColor.r << LAMP_RED_SHIFT) |
((ui16)block.lightColor.g << LAMP_GREEN_SHIFT) |
(ui16)block.lightColor.b;
// Ca Physics
if (block.caFilePath.length()) {
// Check if this physics type was already loaded
auto it = m_caCache->find(block.caFilePath);
if (it == m_caCache->end()) {
// TODO(Ben): Why new?
// TODO(Ben): This isn't getting stored...
CaPhysicsType* newType = new CaPhysicsType();
// Load in the data
if (newType->loadFromYml(block.caFilePath, m_iom)) {
block.caIndex = newType->getCaIndex();
block.caAlg = newType->getCaAlg();
} else {
delete newType;
}
} else {
block.caIndex = it->second->getCaIndex();
block.caAlg = it->second->getCaAlg();
}
}
}
bool BlockLoader::saveMapping(const vio::IOManager& iom, const cString filePath, BlockPack* pack) {
vio::FileStream fs = iom.openFile(filePath, vio::FileOpenFlags::WRITE_ONLY_CREATE);
if (!fs.isOpened()) pError("Failed to open block mapping file for save");
for (auto& b : pack->getBlockMap()) {
fs.write("%s: %d\n", b.first.c_str(), b.second);
}
return true;
}
bool BlockLoader::tryLoadMapping(const vio::IOManager& iom, const cString filePath, BlockPack* pack) {
vio::Path path;
if (!iom.resolvePath(filePath, path)) return false;
std::ifstream file(path.getCString());
if (file.fail()) return false;
// TODO(Ben): Handle comments
nString token;
BlockID id;
while (std::getline(file, token, ':')) {
// Read the id
file >> id;
pack->reserveID(token, id);
// Get the newline
char nl;
file.get(nl);
}
return true;
}
bool BlockLoader::saveBinary(const vio::IOManager& iom, const cString filePath, BlockPack* pack) {
vio::FileStream fs = iom.openFile(filePath, vio::FileOpenFlags::WRITE_ONLY_CREATE | vio::FileOpenFlags::BINARY);
if (!fs.isOpened()) return false;
ui32 size = pack->getBlockMap().size();
ui32 blockSize = sizeof(Block);
// Write sizes
fs.write(1, sizeof(ui32), &size);
fs.write(1, sizeof(ui32), &blockSize);
// TODO(Ben): This isn't complete.
const std::vector<Block>& blockList = pack->getBlockList();
for (auto& b : pack->getBlockMap()) {
const Block& block = blockList[b.second];
fs.write("%s", block.name.c_str()); fs.write(1, 1, "\0");
fs.write(1, sizeof(bool), &block.powderMove);
fs.write(1, sizeof(bool), &block.collide);
fs.write(1, sizeof(bool), &block.waterBreak);
fs.write(1, sizeof(bool), &block.blockLight);
fs.write(1, sizeof(bool), &block.useable);
fs.write(1, sizeof(bool), &block.allowLight);
fs.write(1, sizeof(bool), &block.isCrushable);
fs.write(1, sizeof(bool), &block.isSupportive);
}
return true;
}
void readStr(vio::FileStream& fs, char* buf) {
int i = 0;
do {
fs.read(1, sizeof(char), buf + i);
} while (buf[i++] != 0);
}
bool BlockLoader::loadBinary(const vio::IOManager& iom, const cString filePath, BlockPack* pack) {
vio::FileStream fs = iom.openFile(filePath, vio::FileOpenFlags::READ_ONLY_EXISTING | vio::FileOpenFlags::BINARY);
if (!fs.isOpened()) return false;
ui32 size;
ui32 blockSize;
// Read sizes
fs.read(1, sizeof(ui32), &size);
fs.read(1, sizeof(ui32), &blockSize);
// Make sure block size didn't change. DEBUG MODE CHANGES THE SIZE!!!
//if (blockSize != sizeof(Block)) return false;
char buf[512];
// TODO(Ben): This isn't complete.
for (ui32 i = 0; i < size; i++) {
Block b;
readStr(fs, buf);
b.name = buf;
fs.read(1, sizeof(bool), &b.powderMove);
fs.read(1, sizeof(bool), &b.collide);
fs.read(1, sizeof(bool), &b.waterBreak);
fs.read(1, sizeof(bool), &b.blockLight);
fs.read(1, sizeof(bool), &b.useable);
fs.read(1, sizeof(bool), &b.allowLight);
fs.read(1, sizeof(bool), &b.isCrushable);
fs.read(1, sizeof(bool), &b.isSupportive);
pack->append(b);
}
return true;
}