Skip to content

Commit

Permalink
Refactor: Current Game is publicaly visible via theGame global pointer
Browse files Browse the repository at this point in the history
Make the current Game instance available via a global pointer within
the engine, removing the need for DD_CurrentGame
  • Loading branch information
danij-deng committed Dec 29, 2011
1 parent 6912c7b commit 926a7f9
Show file tree
Hide file tree
Showing 18 changed files with 54 additions and 60 deletions.
6 changes: 3 additions & 3 deletions doomsday/engine/portable/include/dd_main.h
Expand Up @@ -54,6 +54,9 @@ extern finaleid_t titleFinale;
extern GETGAMEAPI GetGameAPI;
#endif

/// Currently active game.
extern Game* theGame;

int DD_EarlyInit(void);
int DD_Main(void);
void DD_CheckTimeDemo(void);
Expand Down Expand Up @@ -98,9 +101,6 @@ const char* value_Str(int val);
/// @return @c true iff there is presently a game loaded.
boolean DD_GameLoaded(void);

/// @return Currently active Game record (always succeeds).
Game* DD_CurrentGame(void);

/// @return Current number of Game records.
int DD_GameCount(void);

Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/cl_main.c
Expand Up @@ -134,7 +134,7 @@ void Cl_SendHello(void)

// The game mode is included in the hello packet.
memset(buf, 0, sizeof(buf));
strncpy(buf, Str_Text(Game_IdentityKey(DD_CurrentGame())), sizeof(buf) - 1);
strncpy(buf, Str_Text(Game_IdentityKey(theGame)), sizeof(buf) - 1);

#ifdef _DEBUG
Con_Message("Cl_SendHello: game mode = %s\n", buf);
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/con_config.c
Expand Up @@ -272,7 +272,7 @@ boolean Con_WriteState(const char* fileName, const char* bindingsFileName)
*/
void Con_SaveDefaults(void)
{
Con_WriteState(cfgFile, (!isDedicated? Str_Text(Game_BindingConfig(DD_CurrentGame())) : 0));
Con_WriteState(cfgFile, (!isDedicated? Str_Text(Game_BindingConfig(theGame)) : 0));
}

D_CMD(WriteConsole)
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/dam_main.c
Expand Up @@ -406,7 +406,7 @@ ddstring_t* DAM_ComposeCacheDir(const char* sourcePath)

if(!sourcePath || !sourcePath[0]) return NULL;

gameIdentityKey = Game_IdentityKey(DD_CurrentGame());
gameIdentityKey = Game_IdentityKey(theGame);
mapPathIdentifier = calculateIdentifierForMapPath(sourcePath);
Str_Init(&mapFileName);
F_FileName(&mapFileName, sourcePath);
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/dd_help.c
Expand Up @@ -326,7 +326,7 @@ void DD_ReadGameHelp(void)
return; // Nothing to do.

Str_Init(&helpFileName);
Str_Appendf(&helpFileName, "%sconhelp.txt", Str_Text(Game_DataPath(DD_CurrentGame())));
Str_Appendf(&helpFileName, "%sconhelp.txt", Str_Text(Game_DataPath(theGame)));
F_ExpandBasePath(&helpFileName, &helpFileName);
DH_ReadStrings(Str_Text(&helpFileName));
Str_Free(&helpFileName);
Expand Down
60 changes: 27 additions & 33 deletions doomsday/engine/portable/src/dd_main.c
Expand Up @@ -116,10 +116,12 @@ finaleid_t titleFinale = 0;
static ddstring_t** gameResourceFileList = 0;
static size_t numGameResourceFileList = 0;

// Game records and associated found-file lists.
static Game* nullGame; // Special "null-game" object.
Game* theGame = NULL; // Currently active game.

// Game collection.
static Game** games = 0;
static int gamesCount = 0, currentGameIndex = 0;
static int gamesCount = 0;
static Game* nullGame; // Special "null-game" object.

// CODE --------------------------------------------------------------------

Expand Down Expand Up @@ -248,7 +250,7 @@ static Game* addGame(Game* game)

boolean DD_GameLoaded(void)
{
return !DD_IsNullGame(DD_CurrentGame());
return !DD_IsNullGame(theGame);
}

int DD_GameCount(void)
Expand All @@ -270,10 +272,10 @@ Game* DD_GameByIdentityKey(const char* identityKey)
return NULL;
}

Game* DD_CurrentGame(void)
gameid_t DD_GameId(Game* game)
{
if(currentGameIndex <= 0) return nullGame;
return games[currentGameIndex-1];
if(!game || game == nullGame) return 0; // Invalid id.
return (gameid_t)gameIndex(game);
}

boolean DD_IsNullGame(const Game* game)
Expand Down Expand Up @@ -303,7 +305,7 @@ boolean DD_GameInfo(GameInfo* info)

if(DD_GameLoaded())
{
populateGameInfo(info, DD_CurrentGame());
populateGameInfo(info, theGame);
return true;
}

Expand Down Expand Up @@ -403,7 +405,7 @@ gameid_t DD_DefineGame(const GameDef* def)
if(game)
{
Game_SetPluginId(game, DD_PluginIdForActiveHook());
return (gameid_t)gameIndex(game);
return DD_GameId(game);
}
return 0; // Invalid id.
}
Expand All @@ -428,7 +430,7 @@ void DD_DestroyGames(void)
Game_Delete(nullGame);
nullGame = NULL;
}
currentGameIndex = 0;
theGame = NULL;
}

/**
Expand Down Expand Up @@ -557,15 +559,15 @@ static boolean isRequiredResource(Game* game, const char* absolutePath)

static void locateGameResources(Game* game)
{
int oldGameIndex = currentGameIndex;
Game* oldGame = theGame;
uint i;

if(!game) return;

if(DD_CurrentGame() != game)
if(theGame != game)
{
/// \kludge Temporarily switch Game.
currentGameIndex = gameIndex(game);
theGame = game;
// Re-init the resource locator using the search paths of this Game.
F_ResetAllResourceNamespaces();
}
Expand All @@ -587,10 +589,10 @@ static void locateGameResources(Game* game)
}
}

if(currentGameIndex != oldGameIndex)
if(theGame != oldGame)
{
/// \kludge Restore the old Game.
currentGameIndex = oldGameIndex;
theGame = oldGame;
// Re-init the resource locator using the search paths of this Game.
F_ResetAllResourceNamespaces();
}
Expand Down Expand Up @@ -731,7 +733,7 @@ void DD_PrintGame(Game* game, int flags)
}

if(flags & PGF_STATUS)
Con_Printf("Status: %s\n", DD_CurrentGame() == game? "Loaded" :
Con_Printf("Status: %s\n", theGame == game? "Loaded" :
allGameResourcesFound(game)? "Complete/Playable" :
"Incomplete/Not playable");
}
Expand Down Expand Up @@ -786,7 +788,7 @@ static int addFilesFromAutoData(boolean loadFiles)
for(i = 0; extensions[i]; ++i)
{
Str_Clear(&pattern);
Str_Appendf(&pattern, "%sauto/*.%s", Str_Text(Game_DataPath(DD_CurrentGame())), extensions[i]);
Str_Appendf(&pattern, "%sauto/*.%s", Str_Text(Game_DataPath(theGame)), extensions[i]);
F_AllResourcePaths2(Str_Text(&pattern), autoDataAdder, (void*)&data);
}
Str_Free(&pattern);
Expand Down Expand Up @@ -908,7 +910,7 @@ static int DD_ChangeGameWorker(void* paramaters)

// Now that resources have been located we can begin to initialize the game.
if(!DD_IsNullGame(p->game) && gx.PreInit)
gx.PreInit((gameid_t)gameIndex(p->game));
gx.PreInit(DD_GameId(p->game));

/**
* Parse the game's main config file.
Expand Down Expand Up @@ -1025,7 +1027,7 @@ boolean DD_ChangeGame2(Game* game, boolean allowReload)
assert(game);

// Ignore attempts to re-load the current game?
if(DD_CurrentGame() == game)
if(theGame == game)
{
if(!allowReload)
{
Expand Down Expand Up @@ -1100,7 +1102,7 @@ boolean DD_ChangeGame2(Game* game, boolean allowReload)
Con_ClearDatabases();

// The current game is now the special "null-game".
currentGameIndex = 0;
theGame = nullGame;

Con_InitDatabases();
DD_Register();
Expand Down Expand Up @@ -1147,14 +1149,7 @@ boolean DD_ChangeGame2(Game* game, boolean allowReload)
}

// This is now the current game.
if(!DD_IsNullGame(game))
{
currentGameIndex = gameIndex(game);
}
else
{
currentGameIndex = 0;
}
theGame = game;

DD_ComposeMainWindowTitle(buf);
Sys_SetWindowTitle(windowIDX, buf);
Expand Down Expand Up @@ -1318,8 +1313,7 @@ int DD_EarlyInit(void)
if(Str_RAt(&defsPath, 0) != '/')
Str_AppendChar(&defsPath, '/');

nullGame = Game_New("null-game", &dataPath, &defsPath, "doomsday.cfg", 0, 0);
currentGameIndex = 0;
theGame = nullGame = Game_New("null-game", &dataPath, &defsPath, "doomsday.cfg", 0, 0);

Str_Free(&defsPath);
Str_Free(&dataPath);
Expand Down Expand Up @@ -2378,7 +2372,7 @@ D_CMD(Unload)
continue;

// Do not attempt to unload a resource required by the current game.
if(isRequiredResource(DD_CurrentGame(), Str_Text(&foundPath)))
if(isRequiredResource(theGame, Str_Text(&foundPath)))
{
Con_Message("\"%s\" is required by the current game and cannot be unloaded in isolation.\n",
F_PrettyPath(Str_Text(&foundPath)));
Expand Down Expand Up @@ -2410,7 +2404,7 @@ D_CMD(ReloadGame)
Con_Message("No game is presently loaded.\n");
return true;
}
DD_ChangeGame2(DD_CurrentGame(), true);
DD_ChangeGame2(theGame, true);
return true;
}

Expand Down Expand Up @@ -2442,7 +2436,7 @@ D_CMD(ListGames)
{
Game* game = gamePtrs[i];

Con_Printf(" %s %-16s %s (%s)\n", DD_CurrentGame() == game? "*" :
Con_Printf(" %s %-16s %s (%s)\n", theGame == game? "*" :
!allGameResourcesFound(game)? "!" : " ",
Str_Text(Game_IdentityKey(game)), Str_Text(Game_Title(game)),
Str_Text(Game_Author(game)));
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/dd_pinit.c
Expand Up @@ -114,7 +114,7 @@ void DD_ComposeMainWindowTitle(char* title)
if(DD_GameLoaded() && gx.GetVariable)
{
sprintf(title, DOOMSDAY_NICENAME " " DOOMSDAY_VERSION_TEXT "%s - %s (%s %s)",
(isDedicated? " (Dedicated)" : ""), Str_Text(Game_Title(DD_CurrentGame())),
(isDedicated? " (Dedicated)" : ""), Str_Text(Game_Title(theGame)),
(char*) gx.GetVariable(DD_PLUGIN_NAME), (char*) gx.GetVariable(DD_PLUGIN_VERSION_SHORT));
}
else
Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/portable/src/def_main.c
Expand Up @@ -829,7 +829,7 @@ static void readAllDefinitions(void)
// Now any definition files required by the game on load.
if(DD_GameLoaded())
{
Game* game = DD_CurrentGame();
Game* game = theGame;
AbstractResource* const* records = Game_Resources(game, RC_DEFINITION, 0);
AbstractResource* const* recordIt;

Expand Down Expand Up @@ -858,7 +858,7 @@ static void readAllDefinitions(void)
{
ddstring_t pattern;
Str_Init(&pattern);
Str_Appendf(&pattern, "%sauto/*.ded", Str_Text(Game_DefsPath(DD_CurrentGame())));
Str_Appendf(&pattern, "%sauto/*.ded", Str_Text(Game_DefsPath(theGame)));
F_AllResourcePaths(Str_Text(&pattern), autoDefsReader);
Str_Free(&pattern);
}
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/def_read.c
Expand Up @@ -697,7 +697,7 @@ static boolean DED_CheckCondition(const char* cond, boolean expected)
}
else if(isalnum(cond[0]))
{ // A game mode.
value = !stricmp(cond, Str_Text(Game_IdentityKey(DD_CurrentGame())));
value = !stricmp(cond, Str_Text(Game_IdentityKey(theGame)));
}

return value == expected;
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/edit_bias.c
Expand Up @@ -423,7 +423,7 @@ static boolean SBE_Save(const char* name)

// Since there can be quite a lot of these, make sure we'll skip
// the ones that are definitely not suitable.
fprintf(file, "SkipIf Not %s\n", Str_Text(Game_IdentityKey(DD_CurrentGame())));
fprintf(file, "SkipIf Not %s\n", Str_Text(Game_IdentityKey(theGame)));

s = SB_GetSource(0);
{ int i;
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/finaleinterpreter.c
Expand Up @@ -1354,7 +1354,7 @@ DEFFC(If)
else if(!strnicmp(token, "mode:", 5))
{
if(DD_GameLoaded())
val = !stricmp(token + 5, Str_Text(Game_IdentityKey(DD_CurrentGame())));
val = !stricmp(token + 5, Str_Text(Game_IdentityKey(theGame)));
else
val = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/p_data.c
Expand Up @@ -150,7 +150,7 @@ const char* P_GenerateUniqueMapId(const char* mapID)
Str_Init(&fileName);
F_FileName(&fileName, F_LumpSourceFile(lumpNum));
dd_snprintf(uid, 255, "%s|%s|%s|%s", mapID, Str_Text(&fileName),
(!F_LumpIsCustom(lumpNum) ? "iwad" : "pwad"), Str_Text(Game_IdentityKey(DD_CurrentGame())));
(!F_LumpIsCustom(lumpNum) ? "iwad" : "pwad"), Str_Text(Game_IdentityKey(theGame)));
strlwr(uid);

Str_Free(&fileName);
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/rend_console.c
Expand Up @@ -287,7 +287,7 @@ void Rend_ConsoleUpdateTitle(void)
if(DD_GameLoaded())
{
dd_snprintf(secondaryTitleText, sizeof(secondaryTitleText)-1, "%s %s", (char*) gx.GetVariable(DD_PLUGIN_NAME), (char*) gx.GetVariable(DD_PLUGIN_VERSION_SHORT));
strncpy(statusText, Str_Text(Game_Title(DD_CurrentGame())), sizeof(statusText) - 1);
strncpy(statusText, Str_Text(Game_Title(theGame)), sizeof(statusText) - 1);
return;
}

Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/portable/src/sv_main.c
Expand Up @@ -94,7 +94,7 @@ void Sv_GetInfo(serverinfo_t* info)
// Let's figure out what we want to tell about ourselves.
info->version = DOOMSDAY_VERSION;
dd_snprintf(info->plugin, sizeof(info->plugin) - 1, "%s %s", (char*) gx.GetVariable(DD_PLUGIN_NAME), (char*) gx.GetVariable(DD_PLUGIN_VERSION_SHORT));
strncpy(info->gameIdentityKey, Str_Text(Game_IdentityKey(DD_CurrentGame())), sizeof(info->gameIdentityKey) - 1);
strncpy(info->gameIdentityKey, Str_Text(Game_IdentityKey(theGame)), sizeof(info->gameIdentityKey) - 1);
strncpy(info->gameConfig, gx.GetVariable(DD_GAME_CONFIG), sizeof(info->gameConfig) - 1);
strncpy(info->name, serverName, sizeof(info->name) - 1);
strncpy(info->description, serverInfo, sizeof(info->description) - 1);
Expand Down Expand Up @@ -418,7 +418,7 @@ void Sv_HandlePacket(void)
{
// Check the game mode (max 16 chars).
Reader_Read(msgReader, buf, 16);
if(strnicmp(buf, Str_Text(Game_IdentityKey(DD_CurrentGame())), 16))
if(strnicmp(buf, Str_Text(Game_IdentityKey(theGame)), 16))
{
Con_Printf(" Bad Game ID: %-.16s\n", buf);
N_TerminateClient(from);
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/sys_reslocator.c
Expand Up @@ -1236,7 +1236,7 @@ boolean F_MapResourcePath(resourcenamespaceid_t rni, ddstring_t* path)
if(nameLen <= pathLen && Str_At(path, nameLen) == '/' &&
!strnicmp(Str_Text(&info->name), Str_Text(path), nameLen))
{
Str_Prepend(path, Str_Text(Game_DataPath(DD_CurrentGame())));
Str_Prepend(path, Str_Text(Game_DataPath(theGame)));
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/portable/src/ui_mpi.c
Expand Up @@ -481,11 +481,11 @@ void MPIUpdateServerList(void)
N_MasterGet(i, &info);

// Is this suitable?
if(info.version != DOOMSDAY_VERSION || stricmp(info.gameIdentityKey, Str_Text(Game_IdentityKey(DD_CurrentGame()))) || !info.canJoin)
if(info.version != DOOMSDAY_VERSION || stricmp(info.gameIdentityKey, Str_Text(Game_IdentityKey(theGame))) || !info.canJoin)
{
Con_Message("Server %s filtered out:\n", info.name);
Con_Message(" remote = %i, local = %i\n", info.version, DOOMSDAY_VERSION);
Con_Message(" remote = %s, local = %s\n", info.gameIdentityKey, Str_Text(Game_IdentityKey(DD_CurrentGame())));
Con_Message(" remote = %s, local = %s\n", info.gameIdentityKey, Str_Text(Game_IdentityKey(theGame)));
Con_Message(" can join = %i\n", info.canJoin);
continue;
}
Expand Down
6 changes: 3 additions & 3 deletions doomsday/engine/portable/src/uri.c
Expand Up @@ -126,22 +126,22 @@ static ddstring_t* resolveUri(const Uri* uri)
else if(!Str_CompareIgnoreCase(&part, "Game.DataPath"))
{
/// \note DataPath already has ending '/'.
Game* game = DD_CurrentGame();
Game* game = theGame;
if(DD_IsNullGame(game))
goto parseEnded;
Str_PartAppend(dest, Str_Text(Game_DataPath(game)), 0, Str_Length(Game_DataPath(game))-1);
}
else if(!Str_CompareIgnoreCase(&part, "Game.DefsPath"))
{
/// \note DefsPath already has ending '/'.
Game* game = DD_CurrentGame();
Game* game = theGame;
if(DD_IsNullGame(game))
goto parseEnded;
Str_PartAppend(dest, Str_Text(Game_DefsPath(game)), 0, Str_Length(Game_DefsPath(game))-1);
}
else if(!Str_CompareIgnoreCase(&part, "Game.IdentityKey"))
{
Game* game = DD_CurrentGame();
Game* game = theGame;
if(DD_IsNullGame(game))
goto parseEnded;
Str_Append(dest, Str_Text(Game_IdentityKey(game)));
Expand Down

0 comments on commit 926a7f9

Please sign in to comment.