/
savedsession.cpp
454 lines (393 loc) · 12.8 KB
/
savedsession.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
/** @file savedsession.cpp Saved (game) session.
*
* @authors Copyright © 2014 Daniel Swanson <danij@dengine.net>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser 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 Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/game/Savedsession"
#include "de/App"
#include "de/ArrayValue"
#include "de/game/Game"
#include "de/game/MapStateReader"
#include "de/game/SavedSessionRepository"
#include "de/Info"
#include "de/Log"
#include "de/NumberValue"
#include "de/NativePath"
#include "de/PackageFolder"
namespace de {
namespace game {
static String const BLOCK_GROUP = "group";
static String const BLOCK_GAMERULE = "gamerule";
void SavedSession::Metadata::parse(String const &source)
{
clear();
/// @todo Info keys are converted to lowercase when parsed?
try
{
Info info;
info.setAllowDuplicateBlocksOfType(QStringList() << BLOCK_GROUP << BLOCK_GAMERULE);
info.parse(source);
// Rebuild the game rules subrecord.
Record &rules = addRecord("gameRules");
foreach(Info::Element const *elem, info.root().contentsInOrder())
{
if(!elem->isBlock()) continue;
// Perhaps a ruleset group?
Info::BlockElement const &groupBlock = elem->as<Info::BlockElement>();
if(groupBlock.blockType() == BLOCK_GROUP)
{
foreach(Info::Element const *grpElem, groupBlock.contentsInOrder())
{
if(!grpElem->isBlock()) continue;
// Perhaps a gamerule?
Info::BlockElement const &ruleBlock = grpElem->as<Info::BlockElement>();
if(ruleBlock.blockType() == BLOCK_GAMERULE)
{
rules.set(ruleBlock.name(), ruleBlock.keyValue("value").text);
}
}
}
}
// Apply the other known values.
String value;
if(info.findValueForKey("gameidentitykey", value))
{
set("gameIdentityKey", value);
}
if(info.findValueForKey("maptime", value))
{
set("mapTime", value.toInt());
}
if(info.findValueForKey("mapuri", value))
{
set("mapUri", value);
}
if(info.findValueForKey("sessionid", value))
{
set("sessionId", value.toInt());
}
if(info.findValueForKey("userdescription", value))
{
set("userDescription", value);
}
if(info.findValueForKey("version", value))
{
set("version", value.toInt());
}
}
catch(de::Error const &er)
{
LOG_WARNING(er.asText());
}
}
String SavedSession::Metadata::asTextWithInfoSyntax() const
{
String text;
QTextStream os(&text);
os.setCodec("UTF-8");
if(has("gameIdentityKey")) os << "gameIdentityKey: " << gets("gameIdentityKey");
if(has("mapTime")) os << "\nmapTime: " << String::number(geti("mapTime"));
if(has("mapUri")) os << "\nmapUri: " << gets("mapUri");
if(has("players")) os << "\nplayers: " << geta("players").asText();
if(has("sessionId")) os << "\nsessionId: " << String::number(geti("sessionId"));
if(has("userDescription")) os << "\nuserDescription: " << gets("userDescription");
if(has("version")) os << "\nversion: " << String::number(geti("version"));
if(hasSubrecord("gameRules"))
{
os << "\n" << BLOCK_GROUP << " ruleset {";
Record const &rules = subrecord("gameRules");
for(Record::Members::const_iterator i = rules.members().begin();
i != rules.members().end(); ++i)
{
os << "\n " << BLOCK_GAMERULE << " \"" << i.key() << "\""
<< " { value= \"" << i.value()->value().asText().replace("\"", "''") << "\" }";
}
os << "\n}";
}
return text;
}
DENG2_PIMPL(SavedSession)
{
SavedSessionRepository *repo; ///< The owning repository (if any).
String fileName; ///< Name of the game session file.
QScopedPointer<Metadata> metadata;
Status status;
bool needUpdateStatus;
Instance(Public *i)
: Base(i)
, repo (0)
, metadata (new Metadata)
, status (Unused)
, needUpdateStatus(true)
{}
Instance(Public *i, Instance const &other)
: Base(i)
, repo (other.repo)
, fileName (other.fileName)
, status (other.status)
, needUpdateStatus(other.needUpdateStatus)
{
DENG2_ASSERT(!other.metadata.isNull());
metadata.reset(new Metadata(*other.metadata));
}
static SessionMetadata *readMetadata(PackageFolder const &pack)
{
if(!pack.has("Info")) return 0;
try
{
File const &file = pack.locate<File const>("Info");
Block raw;
file >> raw;
QScopedPointer<SessionMetadata> metadata(new SessionMetadata);
metadata->parse(String::fromUtf8(raw));
// Future version?
if(metadata->geti("version") > 14) return 0;
// So far so good.
return metadata.take();
}
catch(IByteArray::OffsetError const &)
{
LOG_RES_WARNING("Archive in %s is truncated") << pack.description();
}
catch(IIStream::InputError const &)
{
LOG_RES_WARNING("%s cannot be read") << pack.description();
}
catch(Archive::FormatError const &)
{
LOG_RES_WARNING("Archive in %s is invalid") << pack.description();
}
return 0;
}
void notifyMetadataChanged()
{
DENG2_FOR_PUBLIC_AUDIENCE2(MetadataChange, i)
{
i->savedSessionMetadataChanged(self);
}
}
PackageFolder *tryLocatePackage()
{
if(!repo) return false;
return repo->folder().tryLocate<PackageFolder>(fileName);
}
void updateStatusIfNeeded()
{
if(!needUpdateStatus) return;
needUpdateStatus = false;
LOGDEV_XVERBOSE("Updating SavedSession %p status") << thisPublic;
Status const oldStatus = status;
status = Unused;
if(self.hasFile())
{
status = Incompatible;
// Game identity key missmatch?
if(!metadata->gets("gameIdentityKey", "").compareWithoutCase(App::game().id()))
{
/// @todo Validate loaded add-ons and checksum the definition database.
status = Loadable; // It's good!
}
}
if(status != oldStatus)
{
DENG2_FOR_PUBLIC_AUDIENCE2(StatusChange, i)
{
i->savedSessionStatusChanged(self);
}
}
}
DENG2_PIMPL_AUDIENCE(StatusChange)
DENG2_PIMPL_AUDIENCE(MetadataChange)
};
DENG2_AUDIENCE_METHOD(SavedSession, StatusChange)
DENG2_AUDIENCE_METHOD(SavedSession, MetadataChange)
SavedSession::SavedSession(String const &fileName) : d(new Instance(this))
{
d->fileName = fileName;
if(!d->fileName.isEmpty() && d->fileName.fileNameExtension().isEmpty())
{
d->fileName += ".save";
}
}
SavedSession::SavedSession(SavedSession const &other) : d(new Instance(this, *other.d))
{}
SavedSession &SavedSession::operator = (SavedSession const &other)
{
d.reset(new Instance(this, *other.d));
return *this;
}
SavedSessionRepository &SavedSession::repository() const
{
if(d->repo)
{
return *d->repo;
}
/// @throw MissingRepositoryError No repository is configured.
throw MissingRepositoryError("SavedSession::repository", "No repository is configured");
}
void SavedSession::setRepository(SavedSessionRepository *newRepository)
{
d->repo = newRepository;
}
SavedSession::Status SavedSession::status() const
{
d->updateStatusIfNeeded();
return d->status;
}
String SavedSession::statusAsText() const
{
static String const statusTexts[] = {
"Loadable",
"Incompatible",
"Unused",
};
return statusTexts[int(status())];
}
static String metadataAsStyledText(SavedSession::Metadata const &metadata)
{
return String(_E(b) "%1\n" _E(.)
_E(l) "IdentityKey: " _E(.)_E(i) "%2 " _E(.)
_E(l) "Current map: " _E(.)_E(i) "%3\n" _E(.)
_E(l) "Version: " _E(.)_E(i) "%4 " _E(.)
_E(l) "Session id: " _E(.)_E(i) "%5\n" _E(.)
_E(D) "Game rules:\n" _E(.) " %6")
.arg(metadata.gets("userDescription", ""))
.arg(metadata.gets("gameIdentityKey", ""))
.arg(metadata.gets("mapUri", ""))
.arg(metadata.geti("version", 14))
.arg(metadata.geti("sessionId", 0))
.arg(metadata.gets("gameRules", ""));
}
String SavedSession::description() const
{
return metadataAsStyledText(metadata()) + "\n" +
String(_E(l) "Source file: " _E(.)_E(i) "\"%1\"\n" _E(.)
_E(D) "Status: " _E(.) "%2")
.arg(d->repo? NativePath(d->repo->folder().path() / d->fileName).pretty() : "None")
.arg(statusAsText());
}
String SavedSession::fileName() const
{
return d->fileName;
}
void SavedSession::setFileName(String newName)
{
if(!newName.isEmpty() && newName.fileNameExtension().isEmpty())
{
newName += ".save";
}
if(d->fileName != newName)
{
d->fileName = newName;
d->needUpdateStatus = true;
}
}
bool SavedSession::hasFile() const
{
if(!d->repo) return false;
return d->repo->folder().has(fileName());
}
PackageFolder &SavedSession::locateFile()
{
if(PackageFolder *pack = d->tryLocatePackage())
{
return *pack;
}
/// @throw MissingFileError Failed to locate the source file package.
throw MissingFileError("SavedSession::locateFile", "Source file for " + d->fileName + " could not be located");
}
PackageFolder const &SavedSession::locateFile() const
{
return const_cast<SavedSession *>(this)->locateFile();
}
bool SavedSession::recognizeFile()
{
if(PackageFolder *pack = d->tryLocatePackage())
{
// Attempt to read the session metadata.
if(SessionMetadata *metadata = Instance::readMetadata(*pack))
{
replaceMetadata(metadata);
return true;
}
}
return 0; // Unrecognized
}
void SavedSession::updateFromFile()
{
LOGDEV_VERBOSE("Updating SavedSession %p from the repository") << this;
if(recognizeFile())
{
// Ensure we have a valid description.
if(d->metadata->gets("userDescription", "").isEmpty())
{
d->metadata->set("userDescription", "UNNAMED");
d->notifyMetadataChanged();
}
}
else
{
// Unrecognized or the file could not be accessed (perhaps its a network path?).
// Return the session to the "null/invalid" state.
d->metadata->set("userDescription", "");
d->metadata->set("sessionId", duint32(0));
d->notifyMetadataChanged();
}
d->updateStatusIfNeeded();
}
void SavedSession::removeFile()
{
if(hasFile())
{
d->repo->folder().removeFile(fileName());
d->needUpdateStatus = true;
}
/// Force a status update. @todo necessary?
updateFromFile();
}
bool SavedSession::hasMapState(String mapUriStr) const
{
if(!mapUriStr.isEmpty())
{
if(PackageFolder const *pack = d->tryLocatePackage())
{
return pack->has(Path("maps") / mapUriStr);
}
}
return false;
}
std::auto_ptr<MapStateReader> SavedSession::mapStateReader()
{
if(recognizeFile())
{
std::auto_ptr<MapStateReader> p(repository().makeReader(*this));
return p;
}
/// @throw UnrecognizedMapStateError The game state format was not recognized.
throw UnrecognizedMapStateError("SavedSession::mapStateReader", "Unrecognized map state format");
}
SavedSession::Metadata const &SavedSession::metadata() const
{
DENG2_ASSERT(!d->metadata.isNull());
return *d->metadata;
}
void SavedSession::replaceMetadata(Metadata *newMetadata)
{
DENG2_ASSERT(newMetadata != 0);
d->metadata.reset(newMetadata);
d->needUpdateStatus = true;
}
} // namespace game
} // namespace de