-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsavesaccess.cpp
More file actions
609 lines (510 loc) · 17.8 KB
/
savesaccess.cpp
File metadata and controls
609 lines (510 loc) · 17.8 KB
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#include "savesaccess.h"
#include <QtWidgets/QApplication>
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QWidget>
#include <QDebug>
SavesAccess::SavesAccess(QObject *parent) :
QObject(parent),
savedGameModel(Q_NULLPTR),
resourceModel(Q_NULLPTR)
{
}
// Sets the path and loads the game.
void SavesAccess::loadSavedGame(QString gameName)
{
// Set the game name.
selectedSaveName = gameName;
// Pull the resources into the list.
loadResourceFile();
loadUnitFile();
}
void SavesAccess::saveSavedGame()
{
saveResourceFile();
saveUnitFile();
//saveTimeToFile();
//saveGameOverviewToFile();
}
void SavesAccess::openFileDialog()
{
// TODO: Set the parent of this dialog so it returns properly.
// Not sure if this is the cause of the weird Windows behavior
// or not. Something to try.
QFileDialog fileDialog(0,"Timber and Stone saves.sav file");
fileDialog.setDirectory(rootSavesDirectory);
fileDialog.setNameFilter("Saves File (saves.sav)");
// Open the file dialog so the user can select the saves.sav file.
if (fileDialog.exec())
{
rootSavesDirectory.setPath(fileDialog.selectedFiles().first());
rootSavesDirectory.cdUp(); // This trims off the saves.sav file.
}
}
// NOTE: Qml access this for types that are explicitly typed in.
void SavesAccess::setFilePath(QString path)
{
// Save the path to the file.
rootSavesDirectory.setPath(path);
rootSavesDirectory.cdUp();
}
QString SavesAccess::getSavesPath()
{
return (rootSavesDirectory.absolutePath() + "/" + "saves.sav");
}
bool SavesAccess::pathIsValid()
{
// Return True if both the directory path is valid
// and the saves.sav file exists.
QFile savedGameFile(getSavesPath());
QFileInfo fileInfo(savedGameFile);
if (savedGameFile.exists() && (fileInfo.fileName() == "saves.sav"))
{
return true;
}
else
{
return false;
}
}
void SavesAccess::loadGamesList()
{
// Save the file name.
QFile savedGameFile(rootSavesDirectory.absolutePath()
+ "/" + "saves.sav");
if (savedGameModel == Q_NULLPTR)
{
qDebug() << "SavedAccess model hasn't been set up yet.";
return;
}
// Open file and make sure it went okay.
if (!savedGameFile.exists() || !savedGameFile.open(QFile::ReadOnly | QFile::Text))
{
// TODO: Make this error apparent on the interface somehow.
qDebug() << "Can't open savedGameFile.";
return;
}
else
{
QTextStream in(&savedGameFile);
in.setCodec("UTF-8");
QStringList strings;
// Read the number of games in the file, but
// don't do anything with it.
in.readLine().toLongLong();
// Remove all the games.
savedGameModel->clear();
// Add the games from the saves.sav file.
while (!in.atEnd())
{
strings = in.readLine().split("/", QString::KeepEmptyParts);
// Add the games to the list.
savedGameModel->appendRow(new SavedGame(strings[0],
strings[3],
strings[4],
strings[1].toLongLong(),
strings[2].toLongLong()));
}
}
// It's important to always close the file after reading/writing, so this
// app can remain running while Timber and Stone saves as it wishes.
savedGameFile.close();
}
void SavesAccess::setSavedGameListModel(SavedGameListModel * model)
{
savedGameModel = model;
}
// =====================
// RESOURCES
// =====================
void SavesAccess::setResourceListModel(ResourceListModel * model)
{
resourceModel = model;
}
void SavesAccess::loadResourceFile()
{
QFile resourceFile(rootSavesDirectory.absolutePath()
+ "/" + selectedSaveName
+ "/" + "re.sav");
if (resourceModel == Q_NULLPTR)
{
qDebug() << "Resource model hasn't been set up yet.";
return;
}
// Open file and make sure it went okay.
if (!resourceFile.exists() || !resourceFile.open(QFile::ReadOnly))
{
QString message = "I can't load the re.sav file! Is it missing? Saving is disabled.";
emit fileLoadStatusChanged(true, message);
return;
}
else
{
// Pull in the list of resource assets (name, group, etc)
QFile assetFile(QCoreApplication::applicationDirPath() + "/resource_list.csv");
if (assetFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
// Clear the resource model
resourceModel->clear();
QTextStream assetStream(&assetFile);
while (!assetStream.atEnd())
{
// Grab the next section and decide the size, etc.
QString assetString;
assetString = assetStream.readLine();
QStringList assetData;
assetData = assetString.split(',');
// Temporary array to hold the bytes we read from the file.
QByteArray byteArray;
byteArray.clear();
unsigned char byte; // Most Significant Byte
resourceFile.read((char *)&byte, 1);
if( byte >= 0xe0)
{
byteArray.append(byte);
// Another byte exists!
resourceFile.read((char *)&byte, 1);
byteArray.append(byte);
}
else
{
byteArray.append(byte);
}
resourceFile.read((char *)&byte, 1);
byteArray.append(byte);
resourceModel->appendRow(new Resource(assetData[0],
assetData[1],
Utils::toInt(byteArray))); // resource quantity
}
if (!resourceFile.atEnd())
{
// Read the rest of the file into an array, and keep it for later.
resourceExtraData.clear();
resourceExtraData = resourceFile.readAll();
}
}
else
{
QString message = "I can't load the resource_list.csv file! Is it missing? Saving is disabled.";
emit fileLoadStatusChanged(true, message);
}
assetFile.close();
resourceFile.close();
}
}
void SavesAccess::saveResourceFile()
{
//
// Ha ha ha! Man, I'm hilarious.
//
// if (qrand()%1000 < 10) {
// // NOTE: This doesn't mess things up. Just save again and chance are
// // good that it'll save next time.
// QString message = "Failed to save game. The only loss of resources is one saved game.";
// emit fileSaveStatusChanged(true, message);
// return;
// }
if (resourceModel == Q_NULLPTR
|| resourceModel->rowCount() <= 0)
{
qDebug() << "The resource model hasn't been populated.";
return;
}
QFile resourceFile(rootSavesDirectory.absolutePath()
+ "/" + selectedSaveName
+ "/" + "re.sav");
// Open file for write, and make sure it went okay.
if (!resourceFile.open(QIODevice::WriteOnly))
{
QString message = "Saving failed! Check to make sure the re.sav file isn't open in another program.";
emit fileSaveStatusChanged(true, message);
return;
}
else
{
QByteArray binaryData;
// Write the data to the resource file. Clobber the old one.
for (int ndx = 0;
ndx < resourceModel->rowCount();
ndx++)
{
long quantity = resourceModel->index(ndx).data(Resource::QuantityRole).toLongLong();
if (quantity > 60000)
{
qDebug() << "Resource quantities above 65,278 will fail to load in the game. Reduced to 60,000.";
quantity = 60000;
// Update the resource. setData() requires an "id", not an index.
resourceModel->setData(ndx+1,60000);
}
binaryData = Utils::toBinary(quantity);
resourceFile.write(binaryData);
}
// Write out the unexpected data to the file too.
resourceFile.write(resourceExtraData);
resourceFile.close();
}
}
// =====================
// UNITS (human, neutral mobs, violent mobs)
// =====================
void SavesAccess::setHumanModel(HumanListModel * model)
{
humanModel = model;
}
void SavesAccess::setNeutralMobModel(NeutralMobListModel * model)
{
neutralMobModel = model;
}
void SavesAccess::setViolentMobModel(ViolentMobListModel * model)
{
violentMobModel = model;
}
void SavesAccess::loadUnitFile()
{
bool errorOccured(false);
QString message;
// Compose the file name.
QFile unitFile(rootSavesDirectory.absolutePath()
+ "/" + selectedSaveName
+ "/" + "un.sav");
if ((humanModel == Q_NULLPTR) |
(neutralMobModel == Q_NULLPTR) |
(violentMobModel == Q_NULLPTR) )
{
qDebug() << "Human, neutral, violent mob models haven't been set up yet.";
return;
}
// Open file and make sure it went okay.
if (!unitFile.exists() || !unitFile.open(QFile::ReadOnly))
{
// TODO: Make this error apparent on the interface somehow.
QString message = "The unit file (un.sav) seems to be missing! Saving disabled.";
emit fileLoadStatusChanged(true, message);
return;
}
else
{
QTextStream unitStream(&unitFile);
QString unitString;
QStringList unitData;
//
// Load in all the Humans
//
humanModel->clear();
int numberOfHumans = unitStream.readLine().toInt();
for (int i=0; i<numberOfHumans; i++)
{
unitString = unitStream.readLine();
unitData = unitString.split('/');
Human * newHuman = Human::build(unitData);
if (newHuman != Q_NULLPTR)
{
humanModel->appendRow(newHuman);
}
else
{
errorOccured = true;
qDebug() << "Note: There are" << unitData.size() << "fields in the Human string (not legal!).";
}
}
//
// Load in all the Neutral Mobs
//
neutralMobModel->clear();
int numberOfNeutralMobs = unitStream.readLine().toInt();
for (int i=0; i<numberOfNeutralMobs; i++)
{
unitString = unitStream.readLine();
unitData = unitString.split('/');
NeutralMob * newMob = NeutralMob::build(unitData);
if (newMob != Q_NULLPTR)
{
neutralMobModel->appendRow(newMob);
}
else
{
errorOccured = true;
qDebug() << "Note: There are" << unitData.size() << "fields in the Neutral Mob string (not legal!).";
}
}
//
// Load in all the Violent Mobs
//
violentMobModel->clear();
int numberOfViolentMobs = unitStream.readLine().toInt();
for (int i=0; i<numberOfViolentMobs; i++)
{
unitString = unitStream.readLine();
unitData = unitString.split('/');
ViolentMob * newMob = ViolentMob::build(unitData);
if (newMob != Q_NULLPTR)
{
violentMobModel->appendRow(newMob);
}
else
{
errorOccured = true;
qDebug() << "Note: There are" << unitData.size() << "fields in the Violent Mob string (not legal!).";
}
}
qDebug() << "From" << selectedSaveName
<< ": Loaded" << humanModel->rowCount() << "of" << numberOfHumans << "humans,"
<< neutralMobModel->rowCount() << "of" << numberOfNeutralMobs << "animals, and"
<< violentMobModel->rowCount() << "of" << numberOfViolentMobs << "bad guys.";
if (errorOccured) {
message = "This saved-game version is not supported! Saving is disabled and data may be innacurate.";
} else {
message = "Game \"" + selectedSaveName + "\" loaded properly.";
}
emit fileLoadStatusChanged(errorOccured, message);
unitFile.close();
}
}
void SavesAccess::saveUnitFile()
{
// Compile all the units into the unit file.
if ( humanModel == Q_NULLPTR
|| neutralMobModel == Q_NULLPTR
|| violentMobModel == Q_NULLPTR )
{
qDebug() << "The unit models haven't been set up.";
return;
}
QFile unitFile(rootSavesDirectory.absolutePath()
+ "/" + selectedSaveName
+ "/" + "un.sav");
// Open file for write, and make sure it went okay.
if (!unitFile.open(QIODevice::WriteOnly))
{
QString message = "Saving failed! Check to make sure the un.sav file isn't open in another program.";
emit fileSaveStatusChanged(true, message);
return;
}
else
{
QTextStream unitStream(&unitFile);
// Save the humans, then the neutral mobs, then the violent mobs.
// Humans
unitStream << humanModel->rowCount() << endl;
unitStream.flush();
for (QList<ListItem*>::iterator itr = humanModel->getList().begin(); itr != humanModel->getList().end(); itr++)
{
Human* human = (Human*)*itr;
human->writeToFile(unitFile);
}
// Neutral Mobs
unitStream << neutralMobModel->rowCount() << endl;
unitStream.flush();
for (QList<ListItem*>::iterator itr = neutralMobModel->getList().begin(); itr != neutralMobModel->getList().end(); itr++)
{
NeutralMob* mob = (NeutralMob*)*itr;
unitStream << mob->type() << "/"
<< mob->posX() << "/"
<< mob->posY() << "/"
<< mob->posZ() << "/"
<< mob->rotation() << "/";
// Write all the unknown floats
for (int i=0; i<4; i++)
{
unitStream << mob->unknown_float(i) << "/";
}
// Write all the options
for (int i=0; i<3; i++)
{
unitStream << QString(mob->option(i)?"True":"False") << "/";
}
unitStream << endl;
}
// Violent Mobs
unitStream << violentMobModel->rowCount() << endl;
unitStream.flush();
for (QList<ListItem*>::iterator itr = violentMobModel->getList().begin(); itr != violentMobModel->getList().end(); itr++)
{
ViolentMob* mob = (ViolentMob*)*itr;
unitStream << mob->type() << "/"
<< mob->posX() << "/"
<< mob->posY() << "/"
<< mob->posZ() << "/"
<< mob->rotation() << "/"
<< mob->health() << "/"
<< mob->subtype() << "/"
<< QString(mob->leader() ? "True" : "False") << "/";
unitStream << endl;
}
}
QString message = "Saved.";
emit fileSaveStatusChanged(false, message);
unitFile.close();
}
void SavesAccess::writeToMatlab(int squareSize)
{
qDebug() << "Opening file...";
// Create a new file for storing all these points. Comma seperated.
QFile matlabFile(rootSavesDirectory.absolutePath()
+ "/" + selectedSaveName
+ "/" + "cd.dat");
matlabFile.open(QFile::WriteOnly);
QFile worldFile(rootSavesDirectory.absolutePath()
+ "/" + selectedSaveName
+ "/" + "cd.sav");
worldFile.open(QFile::ReadOnly);
// Go until you get all the blocks
for( int blockNumber = 0; blockNumber < (squareSize * squareSize * 48); blockNumber++ )
{
// Grab a line in the file
QByteArray lineOfBytes = worldFile.readLine();
int i;
i=0;
while( i < lineOfBytes.length())
{
// Get the most significant byte
unsigned char byte = lineOfBytes.at(i);
i++;
// Temporary array to hold the bytes we read from the file.
QByteArray byteArray;
byteArray.clear();
if( byte >= 0xe0)
{
byteArray.append(byte);
// Another byte exists!
byte = lineOfBytes.at(i);
i++;
byteArray.append(byte);
}
else
{
byteArray.append(byte);
}
byte = lineOfBytes.at(i);
i++;
byteArray.append(byte);
// Add the byte to the array.
if( Utils::toInt(byteArray) != 125
&& Utils::toInt(byteArray) != 126
&& Utils::toInt(byteArray) != 714
&& Utils::toInt(byteArray) != 0 )
{
matlabFile.write(Utils::toBinary(1));
}
else
{
matlabFile.write(byteArray);
}
}
// First Block done!
//QByteArray newline("\n");
//matlabFile.write(newline);
}
// The remaining lines are numbers.
QByteArray tempBytes;
tempBytes = worldFile.readLine(); matlabFile.write(tempBytes);
tempBytes = worldFile.readLine(); matlabFile.write(tempBytes);
tempBytes = worldFile.readLine(); matlabFile.write(tempBytes);
tempBytes = worldFile.readLine(); matlabFile.write(tempBytes);
tempBytes = worldFile.readLine(); matlabFile.write(tempBytes);
tempBytes = worldFile.readLine(); matlabFile.write(tempBytes);
tempBytes = worldFile.readLine(); matlabFile.write(tempBytes);
matlabFile.close();
worldFile.close();
qDebug() << "Closed file.";
}
Q_DECLARE_METATYPE(SavesAccess*)