Skip to content

Commit

Permalink
libdeng2|SavedSession: Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Mar 12, 2014
1 parent 047865b commit 1ba2d4b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 37 deletions.
30 changes: 15 additions & 15 deletions doomsday/libdeng2/include/de/game/savedsession.h
Expand Up @@ -23,13 +23,13 @@
#include "../Observers"
#include "../Path"
#include "../Record"
#include "../game/SavedSessionRepository"
#include "../String"

namespace de {
namespace game {

class MapStateReader;
class SavedSessionRepository;

/**
* Logical component representing a serialized game state on disk.
Expand Down Expand Up @@ -131,6 +131,14 @@ class DENG2_PUBLIC SavedSession
*/
void setRepository(SavedSessionRepository *newRepository);

/**
* Determines whether a file package exists for the saved session. However, it may not be
* compatible with the current game session.
*
* @see isLoadable()
*/
bool hasFile() const;

/**
* Determines whether a file package exists for the saved session and if so, reads the
* session metadata. A repository must be configured for this.
Expand All @@ -142,9 +150,9 @@ class DENG2_PUBLIC SavedSession
bool recognizeFile();

/**
* Attempt to update the status of the saved session status from the file package in the
* repository. If the file path is invalid, unreachable, or the package is not recognized
* then the saved session is returned to a valid but non-loadable state.
* Attempt to update the status of the saved session from the file package in the repository.
* If the file path is invalid, unreachable, or the package is not recognized then the saved
* session is returned to a valid but non-loadable state.
*
* @see setRepository(), isLoadable()
*/
Expand All @@ -158,20 +166,12 @@ class DENG2_PUBLIC SavedSession
void removeFile();

/**
* Returns the full path to the saved session file package in the repository.
*/
Path filePath() const;

/**
* Determines whether a game state exists for the saved session. However, it may not be
* compatible with the current game session.
*
* @see isLoadable()
* Composes the full path to the saved session file package in the repository (FYI).
*/
bool hasGameState() const;
inline Path filePath() const { return repository().folder().path() / fileName(); }

/**
* Determines whether a map state exists for the saved session.
* Determines whether a serialized map state exists for the saved session.
*
* @param mapUri Unique map identifier.
*/
Expand Down
11 changes: 6 additions & 5 deletions doomsday/libdeng2/include/de/game/savedsessionrepository.h
Expand Up @@ -21,17 +21,15 @@

#include "../Error"
#include "../Folder"
#include "../game/MapStateReader"
#include "../game/SavedSession"
#include "../Path"
#include "../String"

/// Game map state reader instantiator function ptr.
typedef de::game::MapStateReader *(*MapStateReaderMakeFunc)(de::game::SavedSession const &session);

namespace de {
namespace game {

class MapStateReader;
class SavedSession;

/**
* Centralized saved session repository. The saved game state file structure is automatically
* initialized when the current game changes.
Expand All @@ -47,6 +45,9 @@ class DENG2_PUBLIC SavedSessionRepository
/// Referenced session is not loadable. @ingroup errors
DENG2_ERROR(UnloadableSessionError);

/// Game map state reader instantiator function ptr.
typedef MapStateReader *(*MapStateReaderMakeFunc)(SavedSession const &session);

public:
SavedSessionRepository();

Expand Down
39 changes: 22 additions & 17 deletions doomsday/libdeng2/src/game/savedsession.cpp
Expand Up @@ -21,6 +21,7 @@
#include "de/App"
#include "de/ArrayValue"
#include "de/game/Game"
#include "de/game/MapStateReader"
#include "de/game/SavedSessionRepository"
#include "de/Info"
#include "de/Log"
Expand Down Expand Up @@ -217,7 +218,7 @@ DENG2_PIMPL(SavedSession)
Status const oldStatus = status;

status = Unused;
if(self.hasGameState())
if(self.hasFile())
{
status = Incompatible;
// Game identity key missmatch?
Expand Down Expand Up @@ -247,6 +248,10 @@ DENG2_AUDIENCE_METHOD(SavedSession, MetadataChange)
SavedSession::SavedSession(String const &fileName) : d(new Instance(this))
{
d->fileName = fileName;
if(d->fileName.fileNameExtension().isEmpty())
{
d->fileName += ".save";
}
}

SavedSession::SavedSession(SavedSession const &other) : d(new Instance(this, *other.d))
Expand All @@ -263,7 +268,7 @@ bool SavedSession::recognizeFile()
if(!d->repo) return false;
if(d->repo->folder().has(fileName()))
{
// Attempt to read the session metadata and recognize the map state.
// Attempt to read the session metadata.
PackageFolder &pack = d->repo->folder().locate<PackageFolder>(fileName());
if(SessionMetadata *metadata = Instance::readMetadata(pack))
{
Expand Down Expand Up @@ -328,51 +333,51 @@ static String metadataAsStyledText(SavedSession::Metadata const &metadata)
_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["userDescription"].value().asText())
.arg(metadata["gameIdentityKey"].value().asText())
.arg(metadata["mapUri"].value().asText())
.arg(metadata["version"].value().asNumber())
.arg(metadata["sessionId"].value().asNumber())
.arg(metadata["gameRules"].value().asText());
.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(NativePath(filePath()).pretty())
.arg(d->repo? NativePath(filePath()).pretty() : "None")
.arg(statusAsText());
}

Path SavedSession::filePath() const
{
return repository().folder().path() / fileName();
}

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

void SavedSession::setFileName(String newName)
{
if(newName.fileNameExtension().isEmpty())
{
newName += ".save";
}

if(d->fileName != newName)
{
d->fileName = newName;
d->needUpdateStatus = true;
}
}

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

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

0 comments on commit 1ba2d4b

Please sign in to comment.