Skip to content

Commit

Permalink
libcommon: Convenient method for returning the game identity key
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Feb 28, 2014
1 parent 541da1f commit 2e5a0d9
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 63 deletions.
12 changes: 10 additions & 2 deletions doomsday/plugins/common/include/g_common.h
Expand Up @@ -59,8 +59,6 @@ gameaction_t G_GameAction(void);

void G_SetGameAction(gameaction_t action);

AutoStr *G_IdentityKeyForLegacyGamemode(int gamemode, int saveVersion);

uint G_GenerateSessionId(void);

/**
Expand Down Expand Up @@ -172,6 +170,16 @@ D_CMD( CCmdExitLevel );
class GameStateReaderFactory;
class SaveSlots;

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

/**
* Translates a legacy game mode identifier to a game identity key.
*/
de::String G_IdentityKeyForLegacyGamemode(int gamemode, int saveVersion);

/**
* @param mapUri Map identifier.
* @param mapEntrance Logical map entry point number.
Expand Down
6 changes: 2 additions & 4 deletions doomsday/plugins/common/src/d_netcl.cpp
Expand Up @@ -83,12 +83,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).
GameInfo gameInfo;
DD_GameInfo(&gameInfo);
if(Str_Compare(gameInfo.identityKey, gsGameIdentity))
if(G_IdentityKey().compare(gsGameIdentity))
{
App_Log(DE2_NET_ERROR, "Game mismatch: server's identity key (%s) is different to yours (%s)",
gsGameIdentity, Str_Text(gameInfo.identityKey));
gsGameIdentity, G_IdentityKey().toLatin1().constData());
DD_Execute(false, "net disconnect");
return;
}
Expand Down
13 changes: 6 additions & 7 deletions doomsday/plugins/common/src/d_netsv.cpp
Expand Up @@ -678,14 +678,13 @@ void NetSv_SendGameState(int flags, int to)
{
if(!IS_NETWORK_SERVER) return;

GameInfo gameInfo;
DD_GameInfo(&gameInfo);
de::String const identityKey = G_IdentityKey();

// Print a short message that describes the game state.
AutoStr *str = Uri_Resolved(gameMapUri);

App_Log(DE2_NET_NOTE, "Sending game setup: %s %s %s",
Str_Text(gameInfo.identityKey), Str_Text(str), gameConfigString);
identityKey.toLatin1().constData(),
Str_Text(Uri_Resolved(gameMapUri)),
gameConfigString);

// Send an update to all the players in the game.
for(int i = 0; i < MAXPLAYERS; ++i)
Expand All @@ -697,8 +696,8 @@ void NetSv_SendGameState(int flags, int to)
Writer_WriteByte(writer, flags);

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

// The current map.
Uri_Write(gameMapUri, writer);
Expand Down
60 changes: 27 additions & 33 deletions doomsday/plugins/common/src/g_game.cpp
Expand Up @@ -481,14 +481,7 @@ de::Path G_ChooseRootSaveDirectory()
}

// Use the default.
GameInfo gameInfo;
if(DD_GameInfo(&gameInfo))
{
return de::Path(SAVEGAME_DEFAULT_DIR) / Str_Text(gameInfo.identityKey);
}

/// @throw Error GameInfo is unavailable.
throw de::Error("G_ChooseRootSaveDirectory", "Failed retrieving GameInfo");
return de::Path(SAVEGAME_DEFAULT_DIR) / G_IdentityKey();
}

static void initGameSaveSystem()
Expand Down Expand Up @@ -3559,7 +3552,18 @@ void G_QuitGame()
Hu_MsgStart(MSG_YESNO, endString, G_QuitGameResponse, 0, NULL);
}

AutoStr *G_IdentityKeyForLegacyGamemode(int gamemode, int saveVersion)
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");
}

de::String G_IdentityKeyForLegacyGamemode(int gamemode, int saveVersion)
{
static char const *identityKeys[] = {
#if __JDOOM__
Expand Down Expand Up @@ -3619,9 +3623,7 @@ AutoStr *G_IdentityKeyForLegacyGamemode(int gamemode, int saveVersion)
*/
if(gamemode == doom2 && (gameModeBits & GM_ANY_DOOM2))
{
GameInfo gameInfo;
DD_GameInfo(&gameInfo);
return gameInfo.identityKey;
return G_IdentityKey();
}
/// kludge end.
# endif
Expand All @@ -3631,7 +3633,7 @@ AutoStr *G_IdentityKeyForLegacyGamemode(int gamemode, int saveVersion)
#endif

DENG2_ASSERT(gamemode >= 0 && (unsigned)gamemode < sizeof(identityKeys) / sizeof(identityKeys[0]));
return AutoStr_FromTextStd(identityKeys[gamemode]);
return identityKeys[gamemode];
}

uint G_GenerateSessionId()
Expand Down Expand Up @@ -4070,45 +4072,37 @@ void G_ScreenShot()
}

/**
* Find an unused screenshot file name. Uses the game's identity key as the
* file name base.
* @return Composed file name.
* Find an unused screenshot file name. Uses the game's identity key as the file name base.
*/
static AutoStr *composeScreenshotFileName()
static de::String composeScreenshotFileName()
{
GameInfo gameInfo;
if(!DD_GameInfo(&gameInfo))
{
Con_Error("composeScreenshotFileName: Failed retrieving GameInfo.");
return 0; // Unreachable.
}

AutoStr *name = Str_Appendf(AutoStr_NewStd(), "%s-", Str_Text(gameInfo.identityKey));
int numPos = Str_Length(name);
de::String name = G_IdentityKey() + "-";
int const numPos = name.length();
for(int i = 0; i < 1e6; ++i) // Stop eventually...
{
Str_Appendf(name, "%03i.png", i);
if(!F_FileExists(Str_Text(name))) break;
Str_Truncate(name, numPos);
name += de::String("%1.png").arg(i, 3, 10, QChar('0'));
if(!F_FileExists(name.toUtf8().constData())) break;
name.truncate(numPos);
}
return name;
}

void G_DoScreenShot()
{
AutoStr *fileName = composeScreenshotFileName();
if(fileName && M_ScreenShot(Str_Text(fileName), 24))
de::String fileName = composeScreenshotFileName();
if(M_ScreenShot(fileName.toUtf8().constData(), 24))
{
/// @todo Do not use the console player's message log for this notification.
/// The engine should implement it's own notification UI system for
/// this sort of thing.
AutoStr* msg = Str_Appendf(AutoStr_NewStd(), "Saved screenshot: %s", F_PrettyPath(Str_Text(fileName)));
AutoStr *msg = Str_Appendf(AutoStr_NewStd(), "Saved screenshot: %s",
de::NativePath(fileName).pretty().toLatin1().constData());
P_SetMessage(players + CONSOLEPLAYER, LMF_NO_HIDE, Str_Text(msg));
return;
}

App_Log(DE2_RES_ERROR, "Failed to write screenshot \"%s\"",
fileName? F_PrettyPath(Str_Text(fileName)) : "(null)");
de::NativePath(fileName).pretty().toLatin1().constData());
}

D_CMD(OpenLoadMenu)
Expand Down
15 changes: 4 additions & 11 deletions doomsday/plugins/common/src/saveinfo.cpp
Expand Up @@ -39,13 +39,6 @@ namespace internal
return false;
#endif
}

static de::String currentGameIdentityKey()
{
GameInfo gameInfo;
DD_GameInfo(&gameInfo);
return Str_Text(gameInfo.identityKey);
}
}

using namespace de;
Expand Down Expand Up @@ -128,7 +121,7 @@ DENG2_PIMPL(SaveInfo)
{
status = Incompatible;
// Game identity key missmatch?
if(!gameIdentityKey.compareWithoutCase(currentGameIdentityKey()))
if(!gameIdentityKey.compareWithoutCase(G_IdentityKey()))
{
/// @todo Validate loaded add-ons and checksum the definition database.
status = Loadable; // It's good!
Expand Down Expand Up @@ -161,7 +154,7 @@ DENG2_PIMPL(SaveInfo)
version = String(&verText[8]).toInt();

/// @note Kludge: Assume the current game.
gameIdentityKey = currentGameIdentityKey();
gameIdentityKey = G_IdentityKey();
/// Kludge end.

/*Skip junk*/ SV_Seek(4);
Expand Down Expand Up @@ -349,7 +342,7 @@ void SaveInfo::applyCurrentSessionMetadata()
LOG_AS("SaveInfo");
d->magic = IS_NETWORK_CLIENT? MY_CLIENT_SAVE_MAGIC : MY_SAVE_MAGIC;
d->version = MY_SAVE_VERSION;
d->gameIdentityKey = currentGameIdentityKey();
d->gameIdentityKey = G_IdentityKey();
Uri_Copy(d->mapUri, gameMapUri);
#if !__JHEXEN__
d->mapTime = ::mapTime;
Expand Down Expand Up @@ -467,7 +460,7 @@ void SaveInfo::read(reader_s *reader)
{
// Translate gamemode identifiers from older save versions.
int oldGamemode = Reader_ReadInt32(reader);
d->gameIdentityKey = Str_Text(G_IdentityKeyForLegacyGamemode(oldGamemode, d->version));
d->gameIdentityKey = G_IdentityKeyForLegacyGamemode(oldGamemode, d->version);
}

if(d->version >= 10)
Expand Down
4 changes: 1 addition & 3 deletions doomsday/plugins/doom/src/doomv9gamestatereader.cpp
Expand Up @@ -659,9 +659,7 @@ static void SaveInfo_Read_Dm_v19(SaveInfo *info, Reader *reader)
info->setMagic(0); // Initialize with *something*.

/// @note Kludge: Assume the current game mode.
GameInfo gameInfo;
DD_GameInfo(&gameInfo);
info->setGameIdentityKey(Str_Text(gameInfo.identityKey));
info->setGameIdentityKey(G_IdentityKey());
/// Kludge end.

info->setSessionId(0); // None.
Expand Down
4 changes: 1 addition & 3 deletions doomsday/plugins/heretic/src/hereticv13gamestatereader.cpp
Expand Up @@ -681,9 +681,7 @@ static void SaveInfo_Read_Hr_v13(SaveInfo *info, Reader *reader)
info->setMagic(0); // Initialize with *something*.

/// @note Kludge: Assume the current game mode.
GameInfo gameInfo;
DD_GameInfo(&gameInfo);
info->setGameIdentityKey(Str_Text(gameInfo.identityKey));
info->setGameIdentityKey(G_IdentityKey());
/// Kludge end.

info->setSessionId(0); // None.
Expand Down

0 comments on commit 2e5a0d9

Please sign in to comment.