Skip to content

Commit

Permalink
libdeng2|Session|libcommon|GameSession: game::Session::Profile record…
Browse files Browse the repository at this point in the history
…s the game identity key

The unique identity key for the current game session is now available
from the profile.
  • Loading branch information
danij-deng committed Apr 5, 2014
1 parent 792a3a2 commit 7a8b3da
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 50 deletions.
5 changes: 3 additions & 2 deletions doomsday/client/src/dd_main.cpp
Expand Up @@ -1421,7 +1421,7 @@ bool App_ChangeGame(Game &game, bool allowReload)

if(!game.isNull())
{
LOG_MSG("Selecting game '%s'...") << game.identityKey();
LOG_MSG("Selecting game '%s'...") << game.id();
}
else if(!isReload)
{
Expand All @@ -1439,7 +1439,7 @@ bool App_ChangeGame(Game &game, bool allowReload)
// Re-initialize subsystems needed even when in ringzero.
if(!DD_ExchangeGamePluginEntryPoints(game.pluginId()))
{
LOG_WARNING("Game plugin '%s' is invalid") << game.identityKey();
LOG_WARNING("Game plugin for '%s' is invalid") << game.id();
LOGDEV_WARNING("Failed exchanging entrypoints with plugin %i")
<< int(game.pluginId());
return false;
Expand All @@ -1450,6 +1450,7 @@ bool App_ChangeGame(Game &game, bool allowReload)

// This is now the current game.
App::app().setGame(game);
game::Session::profile().gameId = game.id();

#ifdef __CLIENT__
ClientWindow::main().setWindowTitle(DD_ComposeMainWindowTitle());
Expand Down
33 changes: 18 additions & 15 deletions doomsday/libdeng2/include/de/game/session.h
Expand Up @@ -46,6 +46,24 @@ class DENG2_PUBLIC Session
public:
virtual ~Session() {}

/**
* Configuration profile.
*/
struct Profile
{
// Unique identifier of the game this profile is used with.
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();

/**
* Determines whether the currently configured game session is in progress. Usually this
* will not be the case during title sequences (for example).
Expand Down Expand Up @@ -78,21 +96,6 @@ class DENG2_PUBLIC Session
*/
virtual void load(String const &saveName) = 0;

/**
* Session configuration profile.
*/
struct Profile
{
// 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 configuration profile for the game session.
*/
static Profile &profile();

protected: // Saved session management -------------------------------------------------------

/**
Expand Down
2 changes: 1 addition & 1 deletion doomsday/libdeng2/src/game/session.cpp
Expand Up @@ -29,7 +29,7 @@ namespace game {
static Session::Profile currentProfile;
static Session::SavedIndex sharedSavedIndex;

Session::Profile &Session::profile()
Session::Profile &Session::profile() //static
{
/// @todo None current profiles should be stored persistently when the game changes.
return currentProfile;
Expand Down
5 changes: 0 additions & 5 deletions doomsday/plugins/common/include/g_common.h
Expand Up @@ -233,11 +233,6 @@ D_CMD( CCmdExitLevel );

class SaveSlots;

/**
* Returns the game identity key (from the engine).
*/
de::String G_IdentityKey();

/**
* Chooses a default user description for a saved session.
*
Expand Down
10 changes: 10 additions & 0 deletions doomsday/plugins/common/include/gamesession.h
Expand Up @@ -58,6 +58,11 @@ class GameSession : public de::game::Session
bool savingPossible();
bool loadingPossible();

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

/**
* Determines whether saved game progress will be restored when the current map is reloaded,
* according to the current game state and user configuration.
Expand Down Expand Up @@ -110,6 +115,11 @@ class GameSession : public de::game::Session

public: // Saved session management ----------------------------------------------------------

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

/**
* Save the current game state to a new @em user saved session.
*
Expand Down
4 changes: 2 additions & 2 deletions doomsday/plugins/common/src/d_netcl.cpp
Expand Up @@ -86,10 +86,10 @@ void NetCl_UpdateGameState(Reader *msg)
/// @todo Automatically load the server's game if it is available.
/// However, note that this can only occur if the server changes its game
/// while a netgame is running (which currently will end the netgame).
if(G_IdentityKey().compare(gsGameIdentity))
if(COMMON_GAMESESSION->gameId().compare(gsGameIdentity))
{
App_Log(DE2_NET_ERROR, "Game mismatch: server's identity key (%s) is different to yours (%s)",
gsGameIdentity, G_IdentityKey().toLatin1().constData());
gsGameIdentity, COMMON_GAMESESSION->gameId().toLatin1().constData());
DD_Execute(false, "net disconnect");
return;
}
Expand Down
9 changes: 5 additions & 4 deletions doomsday/plugins/common/src/d_netsv.cpp
Expand Up @@ -23,6 +23,7 @@
#include "d_netsv.h"

#include "d_net.h"
#include "gamesession.h"
#include "player.h"
#include "p_user.h"
#include "p_map.h"
Expand Down Expand Up @@ -678,11 +679,11 @@ void NetSv_SendGameState(int flags, int to)
{
if(!IS_NETWORK_SERVER) return;

de::String const identityKey = G_IdentityKey();
de::String const gameId = COMMON_GAMESESSION->gameId();

// Print a short message that describes the game state.
App_Log(DE2_NET_NOTE, "Sending game setup: %s %s %s",
identityKey.toLatin1().constData(),
gameId.toLatin1().constData(),
Str_Text(Uri_Resolved(gameMapUri)),
gameConfigString);

Expand All @@ -696,8 +697,8 @@ void NetSv_SendGameState(int flags, int to)
Writer_WriteByte(writer, flags);

// Game identity key.
Writer_WriteByte(writer, identityKey.length());
Writer_Write(writer, identityKey.toLatin1().constData(), identityKey.length());
Writer_WriteByte(writer, gameId.length());
Writer_Write(writer, gameId.toLatin1().constData(), gameId.length());

// The current map.
Uri_Write(gameMapUri, writer);
Expand Down
22 changes: 5 additions & 17 deletions doomsday/plugins/common/src/g_game.cpp
Expand Up @@ -567,10 +567,9 @@ gameaction_t G_GameAction()
return gameAction;
}

/// @return Absolute path to a saved session in /home/savegames
static inline de::String composeSavedSessionPathForSlot(int slot)
{
return de::String("/home/savegames") / G_IdentityKey() / SAVEGAMENAME + de::String::number(slot) + ".save";
return COMMON_GAMESESSION->savePath() / de::String(SAVEGAMENAME"%1.save").arg(slot);
}

static void initSaveSlots()
Expand Down Expand Up @@ -1066,7 +1065,7 @@ static de::game::SavedSession *savedSessionByUserDescription(de::String descript
{
if(!description.isEmpty())
{
de::Folder &saveFolder = DENG2_APP->rootFolder().locate<de::Folder>(de::String("/home/savegames") / G_IdentityKey());
de::Folder &saveFolder = DENG2_APP->rootFolder().locate<de::Folder>(COMMON_GAMESESSION->savePath());
DENG2_FOR_EACH_CONST(de::Folder::Contents, i, saveFolder.contents())
{
if(de::game::SavedSession *session = i->second->maybeAs<de::game::SavedSession>())
Expand All @@ -1091,7 +1090,7 @@ de::String G_SaveSlotIdFromUserInput(de::String str)
}

// Perhaps a saved session file name?
de::String savePath = de::String("/home/savegames") / G_IdentityKey() / str + ".save";
de::String savePath = COMMON_GAMESESSION->savePath() / str + ".save";
if(SaveSlot *sslot = G_SaveSlots().slot(DENG2_APP->rootFolder().tryLocate<de::game::SavedSession const>(savePath)))
{
return sslot->id();
Expand Down Expand Up @@ -2578,7 +2577,7 @@ void G_ApplyCurrentSessionMetadata(de::game::SessionMetadata &metadata)
{
metadata.clear();

metadata.set("gameIdentityKey", G_IdentityKey());
metadata.set("gameIdentityKey", COMMON_GAMESESSION->gameId());
metadata.set("userDescription", ""); // Applied later.
metadata.set("mapUri", Str_Text(Uri_Compose(gameMapUri)));
#if !__JHEXEN__
Expand Down Expand Up @@ -2680,17 +2679,6 @@ void G_QuitGame()
Hu_MsgStart(MSG_YESNO, endString, quitGameConfirmed, 0, NULL);
}

de::String G_IdentityKey()
{
GameInfo gameInfo;
if(DD_GameInfo(&gameInfo))
{
return Str_Text(gameInfo.identityKey);
}
/// @throw Error GameInfo is unavailable.
throw de::Error("G_IdentityKey", "Failed retrieving GameInfo");
}

uint G_LogicalMapNumber(uint episode, uint map)
{
#if __JHEXEN__
Expand Down Expand Up @@ -3146,7 +3134,7 @@ void G_ScreenShot()
*/
static de::String composeScreenshotFileName()
{
de::String name = G_IdentityKey() + "-";
de::String name = COMMON_GAMESESSION->gameId() + "-";
int const numPos = name.length();
for(int i = 0; i < 1e6; ++i) // Stop eventually...
{
Expand Down
5 changes: 2 additions & 3 deletions doomsday/plugins/common/src/gamesession.cpp
Expand Up @@ -59,9 +59,8 @@ DENG2_PIMPL(GameSession)
singleton = thisPublic;
}

String userSavePath(String const &fileName)
{
return String("/home/savegames/%1/%2.save").arg(G_IdentityKey()).arg(fileName);
inline String userSavePath(String const &fileName) {
return self.savedPath() / fileName + ".save";
}

void cleanupInternalSave()
Expand Down
2 changes: 1 addition & 1 deletion doomsday/plugins/common/src/saveslots.cpp
Expand Up @@ -74,7 +74,7 @@ DENG2_PIMPL_NOREF(SaveSlots::Slot)
{
status = Incompatible;
// Game identity key missmatch?
if(!session->metadata().gets("gameIdentityKey", "").compareWithoutCase(G_IdentityKey()))
if(!session->metadata().gets("gameIdentityKey").compareWithoutCase(COMMON_GAMESESSION->gameId()))
{
/// @todo Validate loaded add-ons and checksum the definition database.
status = Loadable; // It's good!
Expand Down

0 comments on commit 7a8b3da

Please sign in to comment.