-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
save_index.cpp
440 lines (368 loc) · 13.2 KB
/
save_index.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
/*
Copyright (C) 2003 - 2016 by Jörg Hinrichs, refactored from various
places formerly created by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
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 2 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.
See the COPYING file for more details.
*/
#include <boost/iostreams/filter/gzip.hpp>
#include "save_index.hpp"
#include "format_time_summary.hpp"
#include "formula/string_utils.hpp"
#include "game_end_exceptions.hpp"
#include "game_errors.hpp"
#include "game_preferences.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include "serialization/binary_or_text.hpp"
#include "serialization/parser.hpp"
#include "team.hpp"
#include "filesystem.hpp"
#include "config.hpp"
static lg::log_domain log_engine("engine");
#define LOG_SAVE LOG_STREAM(info, log_engine)
#define ERR_SAVE LOG_STREAM(err, log_engine)
static lg::log_domain log_enginerefac("enginerefac");
#define LOG_RG LOG_STREAM(info, log_enginerefac)
void replace_underbar2space(std::string &name) {
std::replace(name.begin(),name.end(),'_',' ');
}
void replace_space2underbar(std::string &name) {
std::replace(name.begin(),name.end(),' ','_');
}
namespace savegame {
void extract_summary_from_config(config &, config &);
void save_index_class::rebuild(const std::string& name) {
std::string filename = name;
replace_space2underbar(filename);
time_t modified = filesystem::file_modified_time(filesystem::get_saves_dir() + "/" + filename);
rebuild(name, modified);
}
void save_index_class::rebuild(const std::string& name, const time_t& modified) {
log_scope("load_summary_from_file");
config& summary = data(name);
try {
config full;
std::string dummy;
read_save_file(name, full, &dummy);
extract_summary_from_config(full, summary);
} catch(game::load_game_failed&) {
summary["corrupt"] = true;
}
summary["mod_time"] = std::to_string(static_cast<int>(modified));
write_save_index();
}
void save_index_class::remove(const std::string& name) {
config& root = data();
root.remove_attribute(name);
write_save_index();
}
void save_index_class::set_modified(const std::string& name, const time_t& modified) {
modified_[name] = modified;
}
config& save_index_class::get(const std::string& name) {
config& result = data(name);
time_t m = modified_[name];
config::attribute_value& mod_time = result["mod_time"];
if (mod_time.empty() || static_cast<time_t>(mod_time.to_int()) != m) {
rebuild(name, m);
}
return result;
}
void save_index_class::write_save_index() {
log_scope("write_save_index()");
try {
filesystem::scoped_ostream stream = filesystem::ostream_file(filesystem::get_save_index_file());
if (preferences::save_compression_format() != compression::NONE) {
// TODO: maybe allow writing this using bz2 too?
write_gz(*stream, data());
} else {
write(*stream, data());
}
} catch(filesystem::io_exception& e) {
ERR_SAVE << "error writing to save index file: '" << e.what() << "'" << std::endl;
}
}
save_index_class::save_index_class()
: loaded_(false)
, data_()
, modified_()
{
}
config& save_index_class::data(const std::string& name) {
config& cfg = data();
if (config& sv = cfg.find_child("save", "save", name)) {
return sv;
}
config& res = cfg.add_child("save");
res["save"] = name;
return res;
}
config& save_index_class::data() {
if(loaded_ == false) {
try {
filesystem::scoped_istream stream = filesystem::istream_file(filesystem::get_save_index_file());
try {
read_gz(data_, *stream);
} catch (boost::iostreams::gzip_error&) {
stream->seekg(0);
read(data_, *stream);
}
} catch(filesystem::io_exception& e) {
ERR_SAVE << "error reading save index: '" << e.what() << "'" << std::endl;
} catch(config::error& e) {
ERR_SAVE << "error parsing save index config file:\n" << e.message << std::endl;
data_.clear();
}
loaded_ = true;
}
return data_;
}
save_index_class save_index_manager;
class filename_filter {
public:
filename_filter(const std::string& filter) : filter_(filter) {
}
bool operator()(const std::string& filename) const {
return filename.end() == std::search(filename.begin(), filename.end(),
filter_.begin(), filter_.end());
}
private:
std::string filter_;
};
/** Get a list of available saves. */
std::vector<save_info> get_saves_list(const std::string* dir, const std::string* filter)
{
create_save_info creator(dir);
std::vector<std::string> filenames;
filesystem::get_files_in_dir(creator.dir,&filenames);
if (filter) {
// Replace the spaces in the filter
std::string filter_replaced(filter->begin(), filter->end());
replace_space2underbar(filter_replaced);
filenames.erase(std::remove_if(filenames.begin(), filenames.end(),
filename_filter(filter_replaced)),
filenames.end());
}
std::vector<save_info> result;
std::transform(filenames.begin(), filenames.end(),
std::back_inserter(result), creator);
std::sort(result.begin(),result.end(),save_info_less_time());
return result;
}
const config& save_info::summary() const {
return save_index_manager.get(name());
}
std::string save_info::format_time_local() const
{
if(tm* tm_l = localtime(&modified())) {
const std::string format = preferences::use_twelve_hour_clock_format() ? _("%a %b %d %I:%M %p %Y") : _("%a %b %d %H:%M %Y");
return translation::strftime(format, tm_l);
}
LOG_SAVE << "localtime() returned null for time " << this->modified() << ", save " << name();
return "";
}
std::string save_info::format_time_summary() const
{
time_t t = modified();
return util::format_time_summary(t);
}
bool save_info_less_time::operator() (const save_info& a, const save_info& b) const {
if (a.modified() > b.modified()) {
return true;
} else if (a.modified() < b.modified()) {
return false;
// Special funky case; for files created in the same second,
// a replay file sorts less than a non-replay file. Prevents
// a timing-dependent bug where it may look like, at the end
// of a scenario, the replay and the autosave for the next
// scenario are displayed in the wrong order.
} else if (a.name().find(_(" replay"))==std::string::npos && b.name().find(_(" replay"))!=std::string::npos) {
return true;
} else if (a.name().find(_(" replay"))!=std::string::npos && b.name().find(_(" replay"))==std::string::npos) {
return false;
} else {
return a.name() > b.name();
}
}
static std::istream* find_save_file(const std::string &name, const std::string &alt_name, const std::vector<std::string> &suffixes) {
for (const std::string &suf : suffixes) {
std::istream *file_stream = filesystem::istream_file(filesystem::get_saves_dir() + "/" + name + suf);
if (file_stream->fail()) {
delete file_stream;
file_stream = filesystem::istream_file(filesystem::get_saves_dir() + "/" + alt_name + suf);
}
if (!file_stream->fail())
return file_stream;
else
delete file_stream;
}
LOG_SAVE << "Could not open supplied filename '" << name << "'\n";
throw game::load_game_failed();
}
void read_save_file(const std::string& name, config& cfg, std::string* error_log)
{
std::string modified_name = name;
replace_space2underbar(modified_name);
static const std::vector<std::string> suffixes = {"", ".gz", ".bz2"};
filesystem::scoped_istream file_stream = find_save_file(modified_name, name, suffixes);
cfg.clear();
try{
/*
* Test the modified name, since it might use a .gz
* file even when not requested.
*/
if(filesystem::is_gzip_file(modified_name)) {
read_gz(cfg, *file_stream);
} else if(filesystem::is_bzip2_file(modified_name)) {
read_bz2(cfg, *file_stream);
} else {
read(cfg, *file_stream);
}
} catch(const std::ios_base::failure& e) {
LOG_SAVE << e.what();
if(error_log) {
*error_log += e.what();
}
throw game::load_game_failed();
} catch(const config::error &err) {
LOG_SAVE << err.message;
if(error_log) {
*error_log += err.message;
}
throw game::load_game_failed();
}
if(cfg.empty()) {
LOG_SAVE << "Could not parse file data into config\n";
throw game::load_game_failed();
}
}
void remove_old_auto_saves(const int autosavemax, const int infinite_auto_saves)
{
const std::string auto_save = _("Auto-Save");
int countdown = autosavemax;
if (countdown == infinite_auto_saves)
return;
std::vector<save_info> games = get_saves_list(nullptr, &auto_save);
for (std::vector<save_info>::iterator i = games.begin(); i != games.end(); ++i) {
if (countdown-- <= 0) {
LOG_SAVE << "Deleting savegame '" << i->name() << "'\n";
delete_game(i->name());
}
}
}
void delete_game(const std::string& name)
{
std::string modified_name = name;
replace_space2underbar(modified_name);
filesystem::delete_file(filesystem::get_saves_dir() + "/" + name);
filesystem::delete_file(filesystem::get_saves_dir() + "/" + modified_name);
save_index_manager.remove(name);
}
create_save_info::create_save_info(const std::string* d)
: dir(d ? *d : filesystem::get_saves_dir())
{
}
save_info create_save_info::operator()(const std::string& filename) const
{
std::string name = filename;
replace_underbar2space(name);
time_t modified = filesystem::file_modified_time(dir + "/" + filename);
save_index_manager.set_modified(name, modified);
return save_info(name, modified);
}
void extract_summary_from_config(config& cfg_save, config& cfg_summary)
{
const config& cfg_snapshot = cfg_save.child("snapshot");
//Servergenerated replays contain [scenario] and no [replay_start]
const config& cfg_replay_start = cfg_save.child("replay_start") ? cfg_save.child("replay_start") : cfg_save.child("scenario") ;
const config& cfg_replay = cfg_save.child("replay");
const bool has_replay = cfg_replay && !cfg_replay.empty();
const bool has_snapshot = cfg_snapshot && cfg_snapshot.has_child("side");
cfg_summary["replay"] = has_replay;
cfg_summary["snapshot"] = has_snapshot;
cfg_summary["label"] = cfg_save["label"];
cfg_summary["campaign_type"] = cfg_save["campaign_type"];
if(cfg_save.has_child("carryover_sides_start")){
cfg_summary["scenario"] = cfg_save.child("carryover_sides_start")["next_scenario"];
} else {
cfg_summary["scenario"] = cfg_save["scenario"];
}
cfg_summary["difficulty"] = cfg_save["difficulty"];
cfg_summary["random_mode"] = cfg_save["random_mode"];
cfg_summary["campaign"] = cfg_save["campaign"];
cfg_summary["version"] = cfg_save["version"];
cfg_summary["corrupt"] = "";
if(has_snapshot) {
cfg_summary["turn"] = cfg_snapshot["turn_at"];
if(cfg_snapshot["turns"] != "-1") {
cfg_summary["turn"] = cfg_summary["turn"].str() + "/" + cfg_snapshot["turns"].str();
}
}
// Ensure we don't get duplicate [leader] tags
cfg_summary.clear_children("leader");
// Find the human leaders so we can display their icons and names in the load menu.
config leader_config;
bool shrouded = false;
if(const config& snapshot = *(has_snapshot ? &cfg_snapshot : &cfg_replay_start)) {
for(const config &side : snapshot.child_range("side")) {
std::string leader;
std::string leader_image;
std::string leader_image_tc_modifier;
std::string leader_name;
if(side["controller"] != team::CONTROLLER::enum_to_string(team::CONTROLLER::HUMAN)) {
continue;
}
if(side["shroud"].to_bool()) {
shrouded = true;
}
for(const config &u : side.child_range("unit")) {
if(!u["canrecruit"].to_bool()) {
continue;
}
leader = u["id"].str();
leader_name = u["name"].str();
leader_image = u["image"].str();
leader_image_tc_modifier = "~RC(" + u["flag_rgb"].str() + ">" + u["side"].str() + ")";
break;
}
// We need a binary path-independent path to the leader image here so it can be displayed
// for campaign-specific units even when the campaign isn't loaded yet.
std::string leader_image_path = filesystem::get_independent_image_path(leader_image);
// If the image path was found, we append the leader TC modifier. If it's not (such as in
// the case where the binary path hasn't been loaded yet, perhaps due to save_index being
// deleted), the unaltered image path is used and will be parsed by get_independent_image_path
// at runtime.
if(!leader_image_path.empty()) {
leader_image_path += leader_image_tc_modifier;
leader_image = leader_image_path;
}
leader_config["leader"] = leader;
leader_config["leader_name"] = leader_name;
leader_config["leader_image"] = leader_image;
leader_config["leader_image_tc_modifier"] = leader_image_tc_modifier;
cfg_summary.add_child("leader", leader_config);
}
}
if(!shrouded) {
if(has_snapshot) {
if (!cfg_snapshot.find_child("side", "shroud", "yes") && cfg_snapshot.has_attribute("map_data")) {
cfg_summary["map_data"] = cfg_snapshot["map_data"].str();
} else {
ERR_SAVE << "Not saving map because there is shroud" << std::endl;
}
} else if(has_replay) {
if (!cfg_replay_start.find_child("side","shroud","yes") && cfg_replay_start.has_attribute("map_data")) {
cfg_summary["map_data"] = cfg_replay_start["map_data"];
} else {
ERR_SAVE << "Not saving map because there is shroud" << std::endl;
}
}
}
}
} // end namespace savegame