Skip to content

Commit

Permalink
libdeng2|SavedSession: Updated wrt saved session file format changes
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Mar 9, 2014
1 parent 1630a51 commit aec5c7e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 43 deletions.
11 changes: 1 addition & 10 deletions doomsday/libdeng2/include/de/game/savedsession.h
Expand Up @@ -134,23 +134,14 @@ class DENG2_PUBLIC SavedSession
*/
void updateFromRepository();

void deleteFilesInRepository();
void deleteFileInRepository();

/**
* Returns the name of the resource file (with extension) containing the game state.
*/
String fileName() const;
void setFileName(String newName);

/**
* Returns the name of the resource file (with extension) containing the map state.
*
* @param mapUri Unique map identifier.
*
* @see fileName()
*/
String fileNameForMap(String mapUriStr) const;

/**
* Provides read-only access to a copy of the deserialized saved session metadata.
*/
Expand Down
31 changes: 11 additions & 20 deletions doomsday/libdeng2/src/game/savedsession.cpp
Expand Up @@ -232,19 +232,17 @@ void SavedSession::setFileName(String newName)
}
}

String SavedSession::fileNameForMap(String mapUriStr) const
{
return d->fileName + mapUriStr + ".save";
}

bool SavedSession::hasGameState() const
{
return repository().folder().has(fileName());
}

bool SavedSession::hasMapState(String mapUriStr) const
{
return repository().folder().has(fileNameForMap(mapUriStr));
if(mapUriStr.isEmpty()) return false;
String mapFileName = d->fileName + mapUriStr + ".save";
/// @todo Open the .save file and check the index.
return repository().folder().has(mapFileName);
}

void SavedSession::updateFromRepository()
Expand Down Expand Up @@ -273,23 +271,16 @@ void SavedSession::updateFromRepository()
d->updateStatusIfNeeded();
}

void SavedSession::deleteFilesInRepository()
void SavedSession::deleteFileInRepository()
{
FS::FoundFiles files;
App::fileSystem().findAll(repository().folder().path(), files);
DENG2_FOR_EACH(FS::FoundFiles, i, files)
try
{
if(File *file = (*i)->maybeAs<File>())
{
if(file->name().fileNameExtension() == ".save" &&
file->name().beginsWith(d->fileName))
{
// Remove this file.
delete file;
d->needUpdateStatus = true;
}
}
File *file = &App::fileSystem().find<File>(repository().folder().path() / d->fileName + ".save");
delete file;
d->needUpdateStatus = true;
}
catch(FileSystem::NotFoundError const &)
{} // Ignore this error.

/// Force a status update. @todo necessary?
updateFromRepository();
Expand Down
26 changes: 13 additions & 13 deletions doomsday/plugins/common/src/g_game.cpp
Expand Up @@ -1389,12 +1389,12 @@ int G_DoLoadMap(loadmap_params_t *p)
try
{
SaveSlot &sslot = G_SaveSlots()["base"];
de::Path const stateFilePath = sslot.mapStateFilePath(gameMapUri);
de::Path const mapStateFilePath(Str_Text(Uri_Compose(gameMapUri)));

if(!SV_OpenFile(stateFilePath, false/*for read*/))
if(!SV_OpenFile(mapStateFilePath, false/*for read*/))
{
Reader_Delete(reader);
throw de::Error("G_DoLoadMap", "Failed opening \"" + de::NativePath(stateFilePath).pretty() + "\" for read");
throw de::Error("G_DoLoadMap", "Failed opening \"" + de::NativePath(mapStateFilePath).pretty() + "\" for read");
}

MapStateReader(sslot.saveMetadata()["version"].value().asNumber()).read(reader);
Expand Down Expand Up @@ -2952,13 +2952,13 @@ static int saveGameStateWorker(void *context)
{
saveRepo.folder().verifyWriteAccess();

de::Path const stateFilePath = sslot.stateFilePath();
de::Path const mapStateFilePath = sslot.mapStateFilePath(gameMapUri);
de::Path const filePath = sslot.filePath();
de::Path const mapStateFilePath(Str_Text(Uri_Resolved(gameMapUri)));

App_Log(DE2_LOG_VERBOSE, "Attempting save game to \"%s\"",
de::NativePath(stateFilePath).pretty().toLatin1().constData());
de::NativePath(filePath).pretty().toLatin1().constData());

GameStateWriter().write(stateFilePath, mapStateFilePath, *metadata);
GameStateWriter().write(filePath, mapStateFilePath, *metadata);

// Swap the save info.
sslot.replaceSavedSession(session);
Expand Down Expand Up @@ -3021,8 +3021,8 @@ void G_DoLeaveMap()
if(!gameRules.deathmatch)
{
// Save current map.
SaveSlot &sslot = G_SaveSlots()["base"];
de::Path const mapStateFilePath = sslot.mapStateFilePath(gameMapUri);
// SaveSlot &sslot = G_SaveSlots()["base"];
de::Path const mapStateFilePath(Str_Text(Uri_Compose(gameMapUri)));

if(!SV_OpenFile(mapStateFilePath, true/*for write*/))
{
Expand Down Expand Up @@ -3260,15 +3260,15 @@ void G_DoLoadSession(de::String slotId)
#endif

SaveSlot &sslot = G_SaveSlots()[logicalSlot];
de::Path const stateFilePath = sslot.stateFilePath();
de::Path const mapStateFilePath = sslot.mapStateFilePath(gameMapUri);
de::Path const filePath = sslot.filePath();
de::Path const mapStateFilePath(Str_Text(Uri_Resolved(gameMapUri)));

// Attempt to recognize and load the saved game state.
App_Log(DE2_LOG_VERBOSE, "Attempting load save game from \"%s\"",
de::NativePath(stateFilePath).pretty().toLatin1().constData());
de::NativePath(filePath).pretty().toLatin1().constData());

sslot.savedSession()
.gameStateReader()->read(stateFilePath, mapStateFilePath,
.gameStateReader()->read(filePath, mapStateFilePath,
sslot.saveMetadata());

// Make note of the last used save slot.
Expand Down

0 comments on commit aec5c7e

Please sign in to comment.