/
gamestatefolder.cpp
470 lines (416 loc) · 15.2 KB
/
gamestatefolder.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
/** @file gamestatefolder.cpp Archived game state.
*
* @authors Copyright © 2016-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2014 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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; 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</small>
*/
#include "doomsday/GameStateFolder"
#include "doomsday/AbstractSession"
#include "doomsday/DataBundle"
#include <de/App>
#include <de/ArchiveFolder>
#include <de/ArrayValue>
#include <de/Info>
#include <de/LogBuffer>
#include <de/NativePath>
#include <de/NumberValue>
#include <de/PackageLoader>
#include <de/TextValue>
#include <de/Writer>
#include <de/ZipArchive>
using namespace de;
static String const BLOCK_GROUP = "group";
static String const BLOCK_GAMERULE = "gamerule";
/// @todo Refactor this to use ScriptedInfo. -jk
static Value *makeValueFromInfoValue(de::Info::Element::Value const &v)
{
String const text = v;
if (!text.compareWithoutCase("True"))
{
return new NumberValue(true, NumberValue::Boolean);
}
else if (!text.compareWithoutCase("False"))
{
return new NumberValue(false, NumberValue::Boolean);
}
else
{
return new TextValue(text);
}
}
DENG2_PIMPL(GameStateFolder)
{
Metadata metadata; ///< Cached.
bool needCacheMetadata;
Impl(Public *i)
: Base(i)
, needCacheMetadata(true)
{}
bool readMetadata(Metadata &metadata)
{
try
{
Block raw;
self().locate<File const>("Info") >> raw;
metadata.parse(String::fromUtf8(raw));
// So far so good.
return true;
}
catch (IByteArray::OffsetError const &)
{
LOG_RES_WARNING("Archive in %s is truncated") << self().description();
}
catch (IIStream::InputError const &)
{
LOG_RES_WARNING("%s cannot be read") << self().description();
}
catch (Archive::FormatError const &)
{
LOG_RES_WARNING("Archive in %s is invalid") << self().description();
}
catch (Folder::NotFoundError const &)
{
LOG_RES_WARNING("%s does not appear to be a .save package") << self().description();
}
return 0;
}
DENG2_PIMPL_AUDIENCE(MetadataChange)
};
DENG2_AUDIENCE_METHOD(GameStateFolder, MetadataChange)
GameStateFolder::GameStateFolder(File &sourceArchiveFile, String const &name)
: ArchiveFolder(sourceArchiveFile, name)
, d(new Impl(this))
{}
GameStateFolder::~GameStateFolder()
{
DENG2_FOR_AUDIENCE2(Deletion, i) i->fileBeingDeleted(*this);
audienceForDeletion().clear();
deindex();
//Session::savedIndex().remove(path());
}
/*void GameStateFolder::populate(PopulationBehaviors behavior)
{
ArchiveFolder::populate(behavior);
//Session::savedIndex().add(*this);
}*/
void GameStateFolder::readMetadata()
{
LOGDEV_VERBOSE("Updating GameStateFolder metadata %p") << this;
// Determine if a .save package exists in the repository and if so, read the metadata.
Metadata newMetadata;
if (!d->readMetadata(newMetadata))
{
// Unrecognized or the file could not be accessed (perhaps its a network path?).
// Return the session to the "null/invalid" state.
newMetadata.set("userDescription", "");
newMetadata.set("sessionId", duint32(0));
}
cacheMetadata(newMetadata);
}
GameStateFolder::Metadata const &GameStateFolder::metadata() const
{
if (d->needCacheMetadata)
{
const_cast<GameStateFolder *>(this)->readMetadata();
}
return d->metadata;
}
void GameStateFolder::cacheMetadata(Metadata const &copied)
{
d->metadata = copied;
d->needCacheMetadata = false;
DENG2_FOR_AUDIENCE2(MetadataChange, i)
{
i->gameStateFolderMetadataChanged(*this);
}
}
String GameStateFolder::stateFilePath(String const &path) //static
{
if (!path.fileName().isEmpty())
{
return path + "State";
}
return "";
}
bool GameStateFolder::isPackageAffectingGameplay(String const &packageId) // static
{
/**
* @todo The rules here could be more sophisticated when it comes to checking what
* exactly do the data bundles contain. Also, packages should be checked for any
* gameplay-affecting assets. (2016-07-06: Currently there are none.)
*/
if (auto const *bundle = DataBundle::bundleForPackage(packageId))
{
// Collections can be configured, so we need to list the actual files in use
// rather than just the collection itself.
return bundle->format() != DataBundle::Collection;
}
if (File const *selected = PackageLoader::get().select(packageId))
{
auto const &meta = Package::metadata(*selected);
if (meta.has("dataFiles") && meta.geta("dataFiles").size() > 0)
{
// Data files are assumed to affect gameplay.
return true;
}
}
return false;
}
File *GameStateFolder::Interpreter::interpretFile(File *sourceData) const
{
try
{
if (ZipArchive::recognize(*sourceData))
{
// It is a ZIP archive: we will represent it as a folder.
if (sourceData->extension() == ".save")
{
/// @todo fixme: Don't assume this is a save package.
LOG_RES_XVERBOSE("Interpreted %s as a GameStateFolder", sourceData->description());
std::unique_ptr<ArchiveFolder> package;
package.reset(new GameStateFolder(*sourceData, sourceData->name()));
// Archive opened successfully, give ownership of the source to the folder.
package->setSource(sourceData);
return package.release();
}
}
}
catch (Error const &er)
{
// Even though it was recognized as an archive, the file
// contents may still prove to be corrupted.
LOG_RES_WARNING("Failed to read archive in %s: %s")
<< sourceData->description()
<< er.asText();
}
return nullptr;
}
//---------------------------------------------------------------------------------------
DENG2_PIMPL_NOREF(GameStateFolder::MapStateReader)
{
GameStateFolder const *session; ///< Saved session being read. Not owned.
Impl(GameStateFolder const &session) : session(&session)
{}
};
GameStateFolder::MapStateReader::MapStateReader(GameStateFolder const &session)
: d(new Impl(session))
{}
GameStateFolder::MapStateReader::~MapStateReader()
{}
GameStateFolder::Metadata const &GameStateFolder::MapStateReader::metadata() const
{
return d->session->metadata();
}
Folder const &GameStateFolder::MapStateReader::folder() const
{
return *d->session;
}
//---------------------------------------------------------------------------------------
void GameStateFolder::Metadata::parse(String const &source)
{
try
{
clear();
Info info;
info.setAllowDuplicateBlocksOfType(QStringList() << BLOCK_GROUP << BLOCK_GAMERULE);
info.parse(source);
// Rebuild the game rules subrecord.
Record &rules = addSubrecord("gameRules");
foreach (Info::Element const *elem, info.root().contentsInOrder())
{
if (Info::KeyElement const *key = maybeAs<Info::KeyElement>(elem))
{
QScopedPointer<Value> v(makeValueFromInfoValue(key->value()));
add(key->name()) = v.take();
continue;
}
if (Info::ListElement const *list = maybeAs<Info::ListElement>(elem))
{
QScopedPointer<ArrayValue> arr(new ArrayValue);
foreach (Info::Element::Value const &v, list->values())
{
*arr << makeValueFromInfoValue(v);
}
addArray(list->name(), arr.take());
continue;
}
if (Info::BlockElement const *block = maybeAs<Info::BlockElement>(elem))
{
// Perhaps a ruleset group?
if (block->blockType() == BLOCK_GROUP)
{
foreach (Info::Element const *grpElem, block->contentsInOrder())
{
if (!grpElem->isBlock()) continue;
// Perhaps a gamerule?
Info::BlockElement const &ruleBlock = grpElem->as<Info::BlockElement>();
if (ruleBlock.blockType() == BLOCK_GAMERULE)
{
QScopedPointer<Value> v(makeValueFromInfoValue(ruleBlock.keyValue("value")));
rules.add(ruleBlock.name()) = v.take();
}
}
}
continue;
}
}
// Ensure the map URI has the "Maps" scheme set.
if (!gets("mapUri").beginsWith("Maps:", Qt::CaseInsensitive))
{
set("mapUri", String("Maps:") + gets("mapUri"));
}
// Ensure the episode is known. Earlier versions of the savegame format did not save
// this info explicitly. The assumption was that the episode was inferred by / encoded
// in the map URI. If the episode is not present in the metadata then we'll assume it
// is encoded in the map URI and extract it.
if (!has("episode"))
{
String const mapUriPath = gets("mapUri").substr(5);
if (mapUriPath.beginsWith("MAP", Qt::CaseInsensitive))
{
set("episode", "1");
}
else if (mapUriPath.at(0).toLower() == 'e' && mapUriPath.at(2).toLower() == 'm')
{
set("episode", mapUriPath.substr(1, 1));
}
else
{
// Hmm, very odd...
throw Error("GameStateFolder::metadata::parse", "Failed to extract episode id from map URI \"" + gets("mapUri") + "\"");
}
}
if (info.root().contains("packages"))
{
Info::ListElement const &list = info.root().find("packages")->as<Info::ListElement>();
auto *pkgs = new ArrayValue;
for (auto const &value : list.values())
{
*pkgs << new TextValue(value.text);
}
set("packages", pkgs);
}
else
{
set("packages", new ArrayValue);
}
// Ensure we have a valid description.
if (gets("userDescription").isEmpty())
{
set("userDescription", "UNNAMED");
}
//qDebug() << "Parsed save metadata:\n" << asText();
}
catch (Error const &er)
{
LOG_WARNING(er.asText());
}
}
String GameStateFolder::Metadata::asStyledText() const
{
String currentMapText = String(
_E(Ta)_E(l) " Episode: " _E(.)_E(Tb) "%1\n"
_E(Ta)_E(l) " Uri: " _E(.)_E(Tb) "%2")
.arg(gets("episode"))
.arg(gets("mapUri"));
// Is the time in the current map known?
if (has("mapTime"))
{
int time = geti("mapTime") / 35 /*TICRATE*/;
int const hours = time / 3600; time -= hours * 3600;
int const minutes = time / 60; time -= minutes * 60;
int const seconds = time;
currentMapText += String("\n" _E(Ta)_E(l) " Time: " _E(.)_E(Tb) "%1:%2:%3" )
.arg(hours, 2, 10, QChar('0'))
.arg(minutes, 2, 10, QChar('0'))
.arg(seconds, 2, 10, QChar('0'));
}
QStringList rules = gets("gameRules", "None").split("\n", QString::SkipEmptyParts);
rules.replaceInStrings(QRegExp("\\s*(.*)\\s*:\\s*([^ ].*)\\s*"), _E(l) "\\1: " _E(.) "\\2");
String gameRulesText = String::join(toStringList(rules), "\n - ");
auto const &pkgs = geta("packages");
StringList pkgIds;
for (auto const *val : pkgs.elements())
{
pkgIds << Package::splitToHumanReadable(val->asText());
}
return String(_E(1) "%1\n" _E(.)
_E(Ta)_E(l) " Game: " _E(.)_E(Tb) "%2\n"
_E(Ta)_E(l) " Session ID: " _E(.)_E(Tb)_E(m) "0x%3" _E(.) "\n"
_E(T`)_E(D) "Current map:\n" _E(.) "%4\n"
_E(T`)_E(D) "Game rules:\n" _E(.) " - %5\n"
_E(T`)_E(D) "Packages:\n" _E(.) " - %6")
.arg(gets("userDescription", ""))
.arg(gets("gameIdentityKey", ""))
.arg(getui("sessionId", 0), 0, 16)
.arg(currentMapText)
.arg(gameRulesText)
.arg(String::join(pkgIds, "\n - "));
}
/*
* See the Doomsday Wiki for an example of the syntax:
* http://dengine.net/dew/index.php?title=Info
*/
String GameStateFolder::Metadata::asTextWithInfoSyntax() const
{
/// @todo Use a more generic Record => Info conversion logic.
String text;
QTextStream os(&text);
os.setCodec("UTF-8");
if (has("gameIdentityKey")) os << "gameIdentityKey: " << gets("gameIdentityKey");
if (has("packages"))
{
os << "\npackages " << geta("packages").asTextUsingInfoSyntax();
}
if (has("episode")) os << "\nepisode: " << gets("episode");
if (has("mapTime")) os << "\nmapTime: " << String::number(geti("mapTime"));
if (has("mapUri")) os << "\nmapUri: " << gets("mapUri");
if (has("players"))
{
os << "\nplayers <";
ArrayValue const &playersArray = geta("players");
DENG2_FOR_EACH_CONST(ArrayValue::Elements, i, playersArray.elements())
{
Value const *value = *i;
if (i != playersArray.elements().begin()) os << ", ";
os << (value->as<NumberValue>().isTrue()? "True" : "False");
}
os << ">";
}
if (has("visitedMaps"))
{
os << "\nvisitedMaps " << geta("visitedMaps").asTextUsingInfoSyntax();
}
if (has("sessionId")) os << "\nsessionId: " << String::number(geti("sessionId"));
if (has("userDescription")) os << "\nuserDescription: " << gets("userDescription");
if (hasSubrecord("gameRules"))
{
os << "\n" << BLOCK_GROUP << " ruleset {";
Record const &rules = subrecord("gameRules");
DENG2_FOR_EACH_CONST(Record::Members, i, rules.members())
{
Value const &value = i.value()->value();
String valueAsText = value.asText();
if (is<Value::Text>(value))
{
valueAsText = "\"" + valueAsText.replace("\"", "''") + "\"";
}
os << "\n " << BLOCK_GAMERULE << " \"" << i.key() << "\""
<< " { value = " << valueAsText << " }";
}
os << "\n}";
}
return text;
}