-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSavegame.cpp
360 lines (296 loc) · 8.74 KB
/
Savegame.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
Greyout - a colourful platformer about love
Greyout is Copyright (c)2011-2014 Janek Schäfer
This file is part of Greyout.
Greyout 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.
Greyout 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 this program. If not, see <http://www.gnu.org/licenses/>.
Please direct any feedback, questions or comments to
Janek Schäfer (foxblock), foxblock_at_gmail_dot_com
*/
#include "Savegame.h"
#include <fstream>
#include "StringUtility.h"
#include "fileTypeDefines.h"
#define SAVE_VERSION 2
Savegame* Savegame::self = NULL;
Savegame::Savegame()
{
autoSave = true;
filename = "";
encrypt = true;
}
Savegame::~Savegame()
{
if (filename[0] != 0)
save();
clear();
}
Savegame* Savegame::getSavegame()
{
if (not self)
self = new Savegame();
return self;
}
///---public---
bool Savegame::setFile(CRstring filename)
{
string line;
ifstream file(filename.c_str());
if (file.fail())
{
printf("Failed to open file for read: \"%s\"\n",filename.c_str());
// file does not exists -> try to open for write
ofstream file2(filename.c_str());
if (file2.fail())
{
printf("Failed to reserve save file!\n");
return false;
}
this->filename = filename;
if (file2.is_open())
file2.close();
return true;
}
// check save file version and check for encryption
getline(file,line);
line = StringUtility::stripLineEndings(line);
if (line[0] == 0 || line.size() > 2 || StringUtility::stringToInt(line) != SAVE_VERSION)
{
printf("Old save file detected! Contents will be overwritten on next save operation!\n");
file.close();
this->filename = filename;
return true;
}
getline(file,line);
line = StringUtility::stripLineEndings(line);
bool encrypted = StringUtility::stringToBool(line);
// parse file line by line
while (getline(file,line))
{
line = StringUtility::stripLineEndings(line);
if (encrypted)
line = crypt.decryptBuffer(line);
if (line.substr(0,COMMENT_STRING.length()) == COMMENT_STRING) // comment line - disregard
continue;
vector<string> tokens;
StringUtility::tokenize(line,tokens,VALUE_STRING);
if (tokens.size() == 2)
{
data[tokens[0]] = tokens[1];
}
}
if (file.is_open())
file.close();
printf("Save file successfully loaded! \"%s\"\n",filename.c_str());
this->filename = filename;
return true;
}
void Savegame::setEncryption(CRbool useEncryption)
{
encrypt = useEncryption;
}
bool Savegame::save()
{
string line;
ofstream file(filename.c_str());
if (file.fail())
{
printf("Failed to open file for write: \"%s\"\n",filename.c_str());
return false;
}
// write file version and encryption status
file << SAVE_VERSION << "\n" << (encrypt ? "true" : "false") << endl;
for (map<string,string>::const_iterator iter = data.begin(); iter != data.end(); ++iter)
{
line = iter->first + VALUE_STRING + iter->second;
if (encrypt)
line = crypt.encryptBuffer(line);
file << line << "\n";
}
if (file.is_open())
{
file.close();
printf("Game (and the world) saved!\n");
return true;
}
return false;
}
bool Savegame::clear()
{
data.clear();
tempData.clear();
levelDataCache.clear();
chapterDataCache.clear();
if (autoSave)
return save();
return true;
}
string Savegame::getData(CRstring key) const
{
map<string,string>::const_iterator iter = data.find(key);
if (iter != data.end())
return iter->second;
return "";
}
bool Savegame::writeData(CRstring key, CRstring value, CRbool overwrite)
{
if (not overwrite)
{
map<string,string>::const_iterator iter = data.find(key);
if (iter != data.end())
return false;
}
#ifdef _DEBUG
printf("Savegame: Writing data \"%s\" to key \"%s\", old data: \"%s\"\n",value.c_str(),key.c_str(),data[key].c_str());
#endif // _DEBUG
data[key] = value;
if (autoSave)
return save();
return true;
}
bool Savegame::hasData(CRstring key) const
{
map<string,string>::const_iterator iter = data.find(key);
return (iter != data.end());
}
string Savegame::getTempData(CRstring key) const
{
map<string,string>::const_iterator iter = tempData.find(key);
if (iter != tempData.end())
return iter->second;
return "";
}
bool Savegame::writeTempData(CRstring key, CRstring value, CRbool overwrite)
{
if (not overwrite)
{
map<string,string>::const_iterator iter = tempData.find(key);
if (iter != tempData.end())
return false;
}
tempData[key] = value;
return true;
}
bool Savegame::hasTempData(CRstring key) const
{
map<string,string>::const_iterator iter = tempData.find(key);
return (iter != tempData.end());
}
int Savegame::getChapterProgress(CRstring chapterFile)
{
return getChapterStats(chapterFile).progress;
}
bool Savegame::setChapterProgress(CRstring chapterFile, CRint progress, CRbool overwrite)
{
ChapterStats temp = {progress,-1};
return setChapterStats(chapterFile,temp,overwrite);
}
Savegame::ChapterStats Savegame::getChapterStats(CRstring chapterFile)
{
map<string,ChapterStats>::iterator iter = chapterDataCache.find(chapterFile);
if (iter != chapterDataCache.end())
return iter->second;
string value = getData(chapterFile);
vector<string> tokens;
StringUtility::tokenize(value,tokens,DELIMIT_STRING);
ChapterStats result = {0,-1};
if (tokens.size() >= 2)
{
result.progress = StringUtility::stringToInt(tokens.front());
result.bestSpeedrunTime = StringUtility::stringToInt(tokens[1]);
}
chapterDataCache[chapterFile] = result;
return result;
}
bool Savegame::setChapterStats(CRstring chapterFile, ChapterStats newStats, CRbool overwrite)
{
ChapterStats stats = getChapterStats(chapterFile);
if (not overwrite)
{
// preserve saved progress
if (newStats.progress < stats.progress)
newStats.progress = stats.progress;
// preserve saved speedrun time
if ((newStats.bestSpeedrunTime > stats.bestSpeedrunTime && stats.bestSpeedrunTime > 0) || newStats.bestSpeedrunTime < 0)
newStats.bestSpeedrunTime = stats.bestSpeedrunTime;
}
chapterDataCache[chapterFile] = newStats;
string temp = StringUtility::intToString(newStats.progress) + DELIMIT_STRING +
StringUtility::intToString(newStats.bestSpeedrunTime);
return writeData(chapterFile,temp,true);
}
bool Savegame::setLevelStats(CRstring levelFile, LevelStats newStats, CRbool overwrite)
{
LevelStats stats = getLevelStats(levelFile);
if (not overwrite)
{
// preserve saved speedrun time
if ((newStats.bestSpeedrunTime > stats.bestSpeedrunTime && stats.bestSpeedrunTime > 0) || newStats.bestSpeedrunTime < 0)
newStats.bestSpeedrunTime = stats.bestSpeedrunTime;
// increase counters
if (stats.totalDeaths > 0)
newStats.totalDeaths += stats.totalDeaths;
if (stats.totalResets > 0)
newStats.totalResets += stats.totalResets;
if (stats.timesAttempted > 0)
newStats.timesAttempted += stats.timesAttempted;
if (stats.timesCompleted > 0)
newStats.timesCompleted += stats.timesCompleted;
if (stats.totalTimeOnLevel > 0)
newStats.totalTimeOnLevel += stats.totalTimeOnLevel;
}
levelDataCache[levelFile] = newStats;
string temp = StringUtility::intToString(newStats.bestSpeedrunTime) + DELIMIT_STRING +
StringUtility::intToString(newStats.totalDeaths) + DELIMIT_STRING +
StringUtility::intToString(newStats.totalResets) + DELIMIT_STRING +
StringUtility::intToString(newStats.timesAttempted) + DELIMIT_STRING +
StringUtility::intToString(newStats.timesCompleted) + DELIMIT_STRING +
StringUtility::intToString(newStats.totalTimeOnLevel);
return writeData(levelFile,temp,true);
}
Savegame::LevelStats Savegame::getLevelStats(CRstring levelFile)
{
map<string,LevelStats>::iterator iter = levelDataCache.find(levelFile);
if (iter != levelDataCache.end())
return iter->second;
string value = getData(levelFile);
vector<string> tokens;
StringUtility::tokenize(value,tokens,DELIMIT_STRING);
LevelStats result = {-1,0,0,0,0,0};
for (int I = 0; I < tokens.size(); ++I)
{
switch (I)
{
case 0:
result.bestSpeedrunTime = StringUtility::stringToInt(tokens.front());
break;
case 1:
result.totalDeaths = StringUtility::stringToInt(tokens[I]);
break;
case 2:
result.totalResets = StringUtility::stringToInt(tokens[I]);
break;
case 3:
result.timesAttempted = StringUtility::stringToInt(tokens[I]);
break;
case 4:
result.timesCompleted = StringUtility::stringToInt(tokens[I]);
break;
case 5:
result.totalTimeOnLevel = StringUtility::stringToInt(tokens[I]);
break;
}
}
levelDataCache[levelFile] = result;
return result;
}
///---protected---
///---private---