Skip to content

Commit

Permalink
Refactor|libdoomsday|libcommon: AbstractSession has URI and inProgres…
Browse files Browse the repository at this point in the history
…s properties

AbstractSession, at libcommon level, knows about the current map URI
and whether the session is in progress.

DoomsdayApp has new methods that get called when saving and loading
games, so that engine-side state can be included.
  • Loading branch information
skyjake committed Dec 1, 2016
1 parent 9736b68 commit 6632182
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 77 deletions.
69 changes: 43 additions & 26 deletions doomsday/apps/libdoomsday/include/doomsday/abstractsession.h
Expand Up @@ -20,6 +20,7 @@
#define LIBDOOMSDAY_SESSION_H

#include "libdoomsday.h"
#include "uri.h"
#include <de/Error>
#include <de/Observers>
#include <de/String>
Expand All @@ -42,38 +43,21 @@ class LIBDOOMSDAY_PUBLIC AbstractSession
DENG2_ERROR(InProgressError);

public:
virtual ~AbstractSession() {}
AbstractSession();

/**
* Configuration profile.
* @todo Remove this. Could just point to a GameProfile instead. -jk
*/
struct Profile
{
// Unique identifier of the game this profile is used with.
de::String gameId;

// List of resource files (specified via the command line or in a cfg, or found using
// the default search algorithm (e.g., /auto and DOOMWADDIR)).
QStringList resourceFiles;
};
virtual ~AbstractSession();

/**
* Returns the current configuration profile for the game session.
* Determines whether the currently configured game session is in progress. Usually this
* will not be the case during title sequences (for example).
*/
static Profile &profile();

/// Convenient method of looking up the game identity key from the game session profile.
static inline de::String gameId() { return profile().gameId; }

/// Compose the absolute path of the @em user saved session folder for the game session.
static inline de::String savePath() { return de::String("/home/savegames") / profile().gameId; }
bool hasBegun() const;

/**
* Determines whether the currently configured game session is in progress. Usually this
* will not be the case during title sequences (for example).
* Returns the current map URI for the game session in progress. If the session has not
* yet begun then an empty URI is returned.
*/
virtual bool hasBegun() const = 0;
de::Uri mapUri() const;

/**
* Determines whether the game state currently allows the session to be saved.
Expand Down Expand Up @@ -101,7 +85,37 @@ class LIBDOOMSDAY_PUBLIC AbstractSession
*/
virtual void load(de::String const &saveName) = 0;

protected: // Saved session management: ---------------------------------------------------
public:
/**
* Configuration profile.
* @todo Remove this. Could just point to a GameProfile instead. -jk
*/
struct Profile
{
// Unique identifier of the game this profile is used with.
de::String gameId;

// List of resource files (specified via the command line or in a cfg, or found using
// the default search algorithm (e.g., /auto and DOOMWADDIR)).
QStringList resourceFiles;
};

/**
* Returns the current configuration profile for the game session.
*/
static Profile &profile();

/// Convenient method of looking up the game identity key from the game session profile.
static inline de::String gameId() { return profile().gameId; }

/// Compose the absolute path of the @em user saved session folder for the game session.
static inline de::String savePath() { return de::String("/home/savegames") / profile().gameId; }

protected:
void setMapUri(de::Uri const &uri);
void setInProgress(bool inProgress);

//- Saved session management ------------------------------------------------------------

/**
* Makes a copy of the saved session specified.
Expand All @@ -115,6 +129,9 @@ class LIBDOOMSDAY_PUBLIC AbstractSession
* Removes the saved session at @a path.
*/
static void removeSaved(de::String const &path);

private:
DENG2_PRIVATE(d)
};

#endif // LIBDOOMSDAY_SESSION_H
22 changes: 22 additions & 0 deletions doomsday/apps/libdoomsday/include/doomsday/doomsdayapp.h
Expand Up @@ -36,6 +36,8 @@ namespace res { class Bundles; }
class Game;
class Games;
class SaveGames;
class GameStateFolder;
class AbstractSession;

/**
* Common application-level state and components.
Expand Down Expand Up @@ -138,6 +140,26 @@ class LIBDOOMSDAY_PUBLIC DoomsdayApp
de::String const &userMessageIfIncompatible,
std::function<void ()> finalizeFunc) = 0;

/**
* Saves application state to a save folder.
*
* When saving a game session to disk, this method should be called so the
* application gets a chance to include its state in the save as well.
*
* @param toFolder Folder where the game state is being written.
*/
virtual void gameSessionWasSaved(AbstractSession const &session, GameStateFolder &toFolder);

/**
* Loads application state from a save folder.
*
* When loading a game session from disk, this method should be called so that
* the application can restore its state from the save.
*
* @param fromFolder Folder where the game state is being read.
*/
virtual void gameSessionWasLoaded(AbstractSession const &session, GameStateFolder const &fromFolder);

public:
static DoomsdayApp & app();
static res::Bundles & bundles();
Expand Down
39 changes: 37 additions & 2 deletions doomsday/apps/libdoomsday/src/abstractsession.cpp
Expand Up @@ -23,26 +23,61 @@
#include <de/FileSystem>
#include <de/Log>
#include <de/Writer>
#include <doomsday/uri.h>

using namespace de;

static AbstractSession::Profile currentProfile;

DENG2_PIMPL_NOREF(AbstractSession)
{
bool inProgress = false; ///< @c true: session is in progress / internal.save exists.

de::Uri mapUri;
};

AbstractSession::AbstractSession()
: d(new Impl)
{}

AbstractSession::~AbstractSession()
{}

void AbstractSession::setInProgress(bool inProgress)
{
d->inProgress = inProgress;
}

AbstractSession::Profile &AbstractSession::profile() //static
{
/// @todo None current profiles should be stored persistently when the game changes.
return currentProfile;
}

void AbstractSession::removeSaved(String const &path) //static
bool AbstractSession::hasBegun() const
{
return d->inProgress;
}

de::Uri AbstractSession::mapUri() const
{
return hasBegun()? d->mapUri : de::Uri("Maps:", RC_NULL);
}

void AbstractSession::setMapUri(Uri const &uri)
{
d->mapUri = uri;
}

void AbstractSession::removeSaved(String const &path) // static
{
if (App::rootFolder().has(path))
{
App::rootFolder().removeFile(path);
}
}

void AbstractSession::copySaved(String const &destPath, String const &sourcePath) //static
void AbstractSession::copySaved(String const &destPath, String const &sourcePath) // static
{
if (!destPath.compareWithoutCase(sourcePath)) return;

Expand Down
10 changes: 10 additions & 0 deletions doomsday/apps/libdoomsday/src/doomsdayapp.cpp
Expand Up @@ -674,6 +674,16 @@ void DoomsdayApp::reset()
d->currentProfile = nullptr;
}

void DoomsdayApp::gameSessionWasSaved(AbstractSession const &, GameStateFolder &)
{
//qDebug() << "App saving to" << toFolder.description();
}

void DoomsdayApp::gameSessionWasLoaded(AbstractSession const &, GameStateFolder const &)
{
//qDebug() << "App loading from" << fromFolder.description();
}

void DoomsdayApp::setGame(Game const &game)
{
app().d->currentGame = const_cast<Game *>(&game);
Expand Down
7 changes: 0 additions & 7 deletions doomsday/apps/plugins/common/include/gamesession.h
Expand Up @@ -56,7 +56,6 @@ class GameSession : public AbstractSession
GameSession();
virtual ~GameSession();

bool hasBegun() const;
bool isSavingPossible();
bool isLoadingPossible();

Expand Down Expand Up @@ -85,12 +84,6 @@ class GameSession : public AbstractSession
*/
de::Record const &mapInfo() const;

/**
* Returns the current map URI for the game session in progress. If the session has not
* yet begun then an empty URI is returned.
*/
de::Uri mapUri() const;

/**
* Returns the player entry point for the current map, for the game session in progress.
* The entry point determines where players will be reborn.
Expand Down

0 comments on commit 6632182

Please sign in to comment.