/
savegame.cpp
481 lines (412 loc) · 10.8 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
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
// SuperTux
// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
// 2014 Ingo Ruhnke <grumbel@gmx.de>
//
// This program 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.
//
// This program 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/>.
#include "supertux/savegame.hpp"
#include "lisp/lisp.hpp"
#include "lisp/parser.hpp"
#include "lisp/writer.hpp"
#include "physfs/ifile_streambuf.hpp"
#include "scripting/serialize.hpp"
#include "scripting/squirrel_util.hpp"
#include "scripting/squirrel_util.hpp"
#include "supertux/player_status.hpp"
#include "util/file_system.hpp"
#include "util/log.hpp"
#include "worldmap/worldmap.hpp"
namespace {
void get_table_entry(HSQUIRRELVM vm, const std::string& name)
{
sq_pushstring(vm, name.c_str(), -1);
if(SQ_FAILED(sq_get(vm, -2)))
{
throw std::runtime_error("failed to get '" + name + "' table entry");
}
else
{
// successfully placed result on stack
}
}
void get_or_create_table_entry(HSQUIRRELVM vm, const std::string& name)
{
sq_pushstring(vm, name.c_str(), -1);
if(SQ_FAILED(sq_get(vm, -2)))
{
sq_pushstring(vm, name.c_str(), -1);
sq_newtable(vm);
if(SQ_FAILED(sq_newslot(vm, -3, SQFalse)))
{
throw std::runtime_error("failed to create '" + name + "' table entry");
}
else
{
get_table_entry(vm, name);
}
}
else
{
// successfully placed result on stack
}
}
std::vector<std::string> get_table_keys(HSQUIRRELVM vm)
{
std::vector<std::string> worlds;
sq_pushnull(vm);
while(SQ_SUCCEEDED(sq_next(vm, -2)))
{
//here -1 is the value and -2 is the key
const char* result;
if(SQ_FAILED(sq_getstring(vm, -2, &result)))
{
std::ostringstream msg;
msg << "Couldn't get string value for key";
throw scripting::SquirrelError(vm, msg.str());
}
else
{
worlds.push_back(result);
}
// pops key and val before the next iteration
sq_pop(vm, 2);
}
return worlds;
}
std::vector<LevelState> get_level_states(HSQUIRRELVM vm)
{
std::vector<LevelState> results;
sq_pushnull(vm);
while(SQ_SUCCEEDED(sq_next(vm, -2)))
{
//here -1 is the value and -2 is the key
const char* result;
if(SQ_FAILED(sq_getstring(vm, -2, &result)))
{
std::ostringstream msg;
msg << "Couldn't get string value";
throw scripting::SquirrelError(vm, msg.str());
}
else
{
LevelState level_state;
level_state.filename = result;
scripting::get_bool(vm, "solved", level_state.solved);
scripting::get_bool(vm, "perfect", level_state.perfect);
results.push_back(level_state);
}
// pops key and val before the next iteration
sq_pop(vm, 2);
}
return results;
}
} // namespace
void
LevelsetState::store_level_state(const LevelState& in_state)
{
auto it = std::find_if(level_states.begin(), level_states.end(),
[&in_state](const LevelState& state)
{
return state.filename == in_state.filename;
});
if (it != level_states.end())
{
*it = in_state;
}
else
{
level_states.push_back(in_state);
}
}
LevelState
LevelsetState::get_level_state(const std::string& filename)
{
auto it = std::find_if(level_states.begin(), level_states.end(),
[filename](const LevelState& state)
{
return state.filename == filename;
});
if (it != level_states.end())
{
return *it;
}
else
{
log_warning << "failed to retrieve level state for " << filename << std::endl;
LevelState state;
state.filename = filename;
return state;
}
}
Savegame::Savegame(const std::string& filename) :
m_filename(filename),
m_player_status(new PlayerStatus)
{
}
Savegame::~Savegame()
{
}
void
Savegame::load()
{
if (m_filename.empty())
{
log_debug << "no filename set for savegame, skipping load" << std::endl;
return;
}
clear_state_table();
if(!PHYSFS_exists(m_filename.c_str()))
{
log_info << m_filename << ": doesn't exist, not loading state" << std::endl;
}
else
{
log_debug << "loading savegame from " << m_filename << std::endl;
try
{
HSQUIRRELVM vm = scripting::global_vm;
lisp::Parser parser;
const lisp::Lisp* root = parser.parse(m_filename);
const lisp::Lisp* lisp = root->get_lisp("supertux-savegame");
if(lisp == NULL)
{
throw std::runtime_error("file is not a supertux-savegame file");
}
else
{
int version = 1;
lisp->get("version", version);
if(version != 1)
{
throw std::runtime_error("incompatible savegame version");
}
else
{
const lisp::Lisp* tux = lisp->get_lisp("tux");
if(tux == NULL)
{
throw std::runtime_error("No tux section in savegame");
}
{
m_player_status->read(*tux);
}
const lisp::Lisp* state = lisp->get_lisp("state");
if(state == NULL)
{
throw std::runtime_error("No state section in savegame");
}
else
{
sq_pushroottable(vm);
get_table_entry(vm, "state");
scripting::load_squirrel_table(vm, -1, *state);
sq_pop(vm, 2);
}
}
}
}
catch(const std::exception& e)
{
log_fatal << "Couldn't load savegame: " << e.what() << std::endl;
}
}
}
void
Savegame::clear_state_table()
{
HSQUIRRELVM vm = scripting::global_vm;
// delete existing state table, if it exists
sq_pushroottable(vm);
{
/*sq_pushstring(vm, "state", -1);
if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
{
sq_pop(vm, 1);
}*/
// create a new empty state table
sq_pushstring(vm, "state", -1);
sq_newtable(vm);
if(SQ_FAILED(sq_newslot(vm, -3, SQFalse)))
{
throw std::runtime_error("Couldn't create state table");
}
}
sq_pop(vm, 1);
}
void
Savegame::save()
{
if (m_filename.empty())
{
log_debug << "no filename set for savegame, skipping save" << std::endl;
return;
}
log_debug << "saving savegame to " << m_filename << std::endl;
{ // make sure the savegame directory exists
std::string dirname = FileSystem::dirname(m_filename);
if(!PHYSFS_exists(dirname.c_str()))
{
if(!PHYSFS_mkdir(dirname.c_str()))
{
std::ostringstream msg;
msg << "Couldn't create directory for savegames '"
<< dirname << "': " <<PHYSFS_getLastError();
throw std::runtime_error(msg.str());
}
}
if(!PHYSFS_isDirectory(dirname.c_str()))
{
std::ostringstream msg;
msg << "Savegame path '" << dirname << "' is not a directory";
throw std::runtime_error(msg.str());
}
}
HSQUIRRELVM vm = scripting::global_vm;
lisp::Writer writer(m_filename);
writer.start_list("supertux-savegame");
writer.write("version", 1);
using namespace worldmap;
if(WorldMap::current() != NULL)
{
std::ostringstream title;
title << WorldMap::current()->get_title();
title << " (" << WorldMap::current()->solved_level_count()
<< "/" << WorldMap::current()->level_count() << ")";
writer.write("title", title.str());
}
writer.start_list("tux");
m_player_status->write(writer);
writer.end_list("tux");
writer.start_list("state");
sq_pushroottable(vm);
sq_pushstring(vm, "state", -1);
if(SQ_SUCCEEDED(sq_get(vm, -2)))
{
scripting::save_squirrel_table(vm, -1, writer);
sq_pop(vm, 1);
}
sq_pop(vm, 1);
writer.end_list("state");
writer.end_list("supertux-savegame");
}
std::vector<std::string>
Savegame::get_worldmaps()
{
std::vector<std::string> worlds;
HSQUIRRELVM vm = scripting::global_vm;
int oldtop = sq_gettop(vm);
try
{
sq_pushroottable(vm);
get_table_entry(vm, "state");
get_table_entry(vm, "worlds");
worlds = get_table_keys(vm);
}
catch(const std::exception& err)
{
log_warning << err.what() << std::endl;
}
sq_settop(vm, oldtop);
return worlds;
}
WorldmapState
Savegame::get_worldmap_state(const std::string& name)
{
WorldmapState result;
HSQUIRRELVM vm = scripting::global_vm;
int oldtop = sq_gettop(vm);
try
{
sq_pushroottable(vm);
get_table_entry(vm, "state");
get_table_entry(vm, "worlds");
get_table_entry(vm, name);
get_table_entry(vm, "levels");
result.level_states = get_level_states(vm);
}
catch(const std::exception& err)
{
log_warning << err.what() << std::endl;
}
sq_settop(vm, oldtop);
return result;
}
std::vector<std::string>
Savegame::get_levelsets()
{
std::vector<std::string> results;
HSQUIRRELVM vm = scripting::global_vm;
int oldtop = sq_gettop(vm);
try
{
sq_pushroottable(vm);
get_table_entry(vm, "state");
get_table_entry(vm, "levelsets");
results = get_table_keys(vm);
}
catch(const std::exception& err)
{
log_warning << err.what() << std::endl;
}
sq_settop(vm, oldtop);
return results;
}
LevelsetState
Savegame::get_levelset_state(const std::string& basedir)
{
LevelsetState result;
HSQUIRRELVM vm = scripting::global_vm;
int oldtop = sq_gettop(vm);
try
{
sq_pushroottable(vm);
get_table_entry(vm, "state");
get_table_entry(vm, "levelsets");
get_table_entry(vm, basedir);
get_table_entry(vm, "levels");
result.level_states = get_level_states(vm);
}
catch(const std::exception& err)
{
log_warning << err.what() << std::endl;
}
sq_settop(vm, oldtop);
return result;
}
void
Savegame::set_levelset_state(const std::string& basedir,
const std::string& level_filename,
bool solved)
{
LevelsetState state = get_levelset_state(basedir);
HSQUIRRELVM vm = scripting::global_vm;
int oldtop = sq_gettop(vm);
try
{
sq_pushroottable(vm);
get_table_entry(vm, "state");
get_or_create_table_entry(vm, "levelsets");
get_or_create_table_entry(vm, basedir);
get_or_create_table_entry(vm, "levels");
get_or_create_table_entry(vm, level_filename);
bool old_solved = false;
scripting::get_bool(vm, "solved", old_solved);
scripting::store_bool(vm, "solved", solved || old_solved);
}
catch(const std::exception& err)
{
log_warning << err.what() << std::endl;
}
sq_settop(vm, oldtop);
}
/* EOF */