From 9ce97703901ffd7d478d02318c2354be06b1fe93 Mon Sep 17 00:00:00 2001 From: danij Date: Tue, 25 Mar 2014 21:24:35 +0000 Subject: [PATCH] libcommon: Cleaned up logic for suggesting of user savegame descriptions --- doomsday/plugins/common/include/g_common.h | 15 ++-- doomsday/plugins/common/src/g_game.cpp | 84 +++++++++------------- doomsday/plugins/common/src/hu_menu.cpp | 8 +-- 3 files changed, 46 insertions(+), 61 deletions(-) diff --git a/doomsday/plugins/common/include/g_common.h b/doomsday/plugins/common/include/g_common.h index e598fd4e66..639b81549c 100644 --- a/doomsday/plugins/common/include/g_common.h +++ b/doomsday/plugins/common/include/g_common.h @@ -149,11 +149,6 @@ uint G_LogicalMapNumber(uint episode, uint map); /// @return Logical map number. uint G_CurrentLogicalMapNumber(void); -/** - * Automatically generate a useful savegame description (map name, map time, etc...). - */ -AutoStr *G_GenerateUserSaveDescription(void); - int G_Ruleset_Skill(); #if !__JHEXEN__ byte G_Ruleset_Fast(); @@ -231,7 +226,15 @@ bool G_SaveSession(de::String slotId, de::String *userDescription = 0); */ bool G_LoadSession(de::String slotId); -uint G_GenerateSessionId(void); +/** + * Chooses a default user description for a saved session. + * + * @param slotId Unique identifier of a saved slot from which the existing description should + * be re-used. Use a zero-length string to disable. + * @param autogenerate @c true= generate a useful description (map name, map time, etc...) if none + * exists for the referenced save @a slotId. + */ +de::String G_DefaultSavedSessionUserDescription(de::String const &slotId, bool autogenerate = true); /** * Configures @a metadata according to the current game session configuration. diff --git a/doomsday/plugins/common/src/g_game.cpp b/doomsday/plugins/common/src/g_game.cpp index 61879315e9..12d495ed6e 100644 --- a/doomsday/plugins/common/src/g_game.cpp +++ b/doomsday/plugins/common/src/g_game.cpp @@ -2894,11 +2894,7 @@ static void restorePlayersInHub(playerbackup_t playerBackup[MAXPLAYERS], uint ma } #endif // __JHEXEN__ -/** - * @param slotId Unique identifier of a saved slot from which the existing description should - * be re-used. Use a zero-length string to disable. - */ -static de::String defaultSavedSessionUserDescription(de::String const &slotId) +de::String G_DefaultSavedSessionUserDescription(de::String const &slotId, bool autogenerate) { // If the slot is already in use then choose existing description. if(G_SaveSlots().has(slotId)) @@ -2910,13 +2906,40 @@ static de::String defaultSavedSessionUserDescription(de::String const &slotId) } } - /// Autogenerate a suitable description. @todo Why is this optional? - if(gaSaveSessionGenerateDescription) + if(!autogenerate) return ""; + + // Autogenerate a suitable description. + de::String description; + + // Include the source file name, for custom maps. + AutoStr const *mapPath = Uri_Compose(gameMapUri); + if(P_MapIsCustom(Str_Text(mapPath))) { - return Str_Text(G_GenerateUserSaveDescription()); + de::String const &mapSourcePath = de::String(Str_Text(P_MapSourceFile(Str_Text(mapPath)))); + description += mapSourcePath.fileNameWithoutExtension() + ":"; } - return ""; + // Include the map title. + de::String mapTitle = de::String(P_MapTitle(0/*current map*/)); + // No map title? Use the identifier. (Some tricksy modders provide us with an empty title). + /// @todo Move this logic engine-side. + if(mapTitle.isEmpty() || mapTitle.at(0) == ' ') + { + mapTitle = Str_Text(mapPath); + } + description += mapTitle; + + // Include the game time also. + int time = mapTime / TICRATE; + int const hours = time / 3600; time -= hours * 3600; + int const minutes = time / 60; time -= minutes * 60; + int const seconds = time; + description += de::String("%1:%2:%3") + .arg(hours, 2, 10, QChar('0')) + .arg(minutes, 2, 10, QChar('0')) + .arg(seconds, 2, 10, QChar('0')); + + return description; } struct savegamesessionworker_params_t @@ -2961,7 +2984,7 @@ static int saveGameSessionWorker(void *context) } else // We'll generate a suitable description automatically. { - metadata.set("userDescription", defaultSavedSessionUserDescription(parm.slotId)); + metadata.set("userDescription", G_DefaultSavedSessionUserDescription(parm.slotId)); } // In networked games the server tells the clients to save also. @@ -3354,45 +3377,6 @@ bool G_SaveSession(de::String slotId, de::String *userDescription) return true; } -AutoStr *G_GenerateUserSaveDescription() -{ - int time = mapTime / TICRATE; - - int const hours = time / 3600; time -= hours * 3600; - int const minutes = time / 60; time -= minutes * 60; - int const seconds = time; - - AutoStr *mapPath = Uri_Compose(gameMapUri); - char const *mapTitle = P_MapTitle(0/*current map*/); - - // Still no map title? Use the identifier. - // Some tricksy modders provide us with an empty title... - /// @todo Move this logic engine-side. - if(!mapTitle || !mapTitle[0] || mapTitle[0] == ' ') - { - mapTitle = Str_Text(mapPath); - } - - char baseNameBuf[256]; - char const *baseName = 0; - if(P_MapIsCustom(Str_Text(mapPath))) - { - F_ExtractFileBase(baseNameBuf, Str_Text(P_MapSourceFile(Str_Text(mapPath))), 256); - baseName = baseNameBuf; - } - - AutoStr *str = AutoStr_New(); - Str_Appendf(str, "%s%s%s %02i:%02i:%02i", (baseName? baseName : ""), - (baseName? ":" : ""), mapTitle, hours, minutes, seconds); - - return str; -} - -uint G_GenerateSessionId() -{ - return Timer_RealMilliseconds() + (mapTime << 24); -} - void G_ApplyCurrentSessionMetadata(de::game::SessionMetadata &metadata) { metadata.clear(); @@ -3416,7 +3400,7 @@ void G_ApplyCurrentSessionMetadata(de::game::SessionMetadata &metadata) metadata.set("players", array); // Takes ownership. #endif - metadata.set("sessionId", G_GenerateSessionId()); + metadata.set("sessionId", uint(Timer_RealMilliseconds() + (mapTime << 24))); } /** diff --git a/doomsday/plugins/common/src/hu_menu.cpp b/doomsday/plugins/common/src/hu_menu.cpp index 317d98d94d..bca7696ea5 100644 --- a/doomsday/plugins/common/src/hu_menu.cpp +++ b/doomsday/plugins/common/src/hu_menu.cpp @@ -5847,15 +5847,13 @@ int Hu_MenuCvarList(mn_object_t *obj, mn_actionid_t action, void *parameters) return 0; } -int Hu_MenuSaveSlotEdit(mn_object_t *obj, mn_actionid_t action, void * /*context*/) +int Hu_MenuSaveSlotEdit(mn_object_t *ob, mn_actionid_t action, void * /*context*/) { if(MNA_ACTIVE != action) return 1; - - // Are we suggesting a new name? if(cfg.menuGameSaveSuggestDescription) { - AutoStr *suggestion = G_GenerateUserSaveDescription(); - MNEdit_SetText(obj, MNEDIT_STF_NO_ACTION, Str_Text(suggestion)); + de::String const description = G_DefaultSavedSessionUserDescription("" /*don't reuse the existing description*/); + MNEdit_SetText(ob, MNEDIT_STF_NO_ACTION, description.toLatin1().constData()); } return 0; }