From 90a83deba8a7af0c43ec34aff4f86a24fdfc21cb Mon Sep 17 00:00:00 2001 From: danij Date: Tue, 31 Jul 2012 19:49:44 +0100 Subject: [PATCH] Refactor|Game Save: SaveInfos are now freely swappable (in Hexen) as intended --- doomsday/plugins/common/include/saveinfo.h | 9 ++++-- doomsday/plugins/common/src/p_saveg.c | 32 ++++++---------------- doomsday/plugins/common/src/saveinfo.c | 22 ++++++++++++--- 3 files changed, 33 insertions(+), 30 deletions(-) diff --git a/doomsday/plugins/common/include/saveinfo.h b/doomsday/plugins/common/include/saveinfo.h index f75cd2d29b..43bb436ef7 100644 --- a/doomsday/plugins/common/include/saveinfo.h +++ b/doomsday/plugins/common/include/saveinfo.h @@ -54,14 +54,17 @@ typedef struct saveinfo_s { } SaveInfo; SaveInfo* SaveInfo_New(void); +SaveInfo* SaveInfo_NewCopy(const SaveInfo* other); void SaveInfo_Delete(SaveInfo* info); -uint SaveInfo_GameId(SaveInfo* info); +SaveInfo* SaveInfo_Copy(SaveInfo* self, const SaveInfo* other); -const saveheader_t* SaveInfo_Header(SaveInfo* info); +uint SaveInfo_GameId(const SaveInfo* info); -const ddstring_t* SaveInfo_Name(SaveInfo* info); +const saveheader_t* SaveInfo_Header(const SaveInfo* info); + +const ddstring_t* SaveInfo_Name(const SaveInfo* info); void SaveInfo_SetGameId(SaveInfo* info, uint newGameId); diff --git a/doomsday/plugins/common/src/p_saveg.c b/doomsday/plugins/common/src/p_saveg.c index ffa8a5296c..0d0e9f55dd 100644 --- a/doomsday/plugins/common/src/p_saveg.c +++ b/doomsday/plugins/common/src/p_saveg.c @@ -557,7 +557,6 @@ static SaveInfo* findSaveInfoForSlot(int slot) return saveInfo[slot]; } -#if !__JHEXEN__ static void replaceSaveInfo(int slot, SaveInfo* newInfo) { SaveInfo** destAdr; @@ -579,7 +578,6 @@ static void replaceSaveInfo(int slot, SaveInfo* newInfo) if(*destAdr) SaveInfo_Delete(*destAdr); *destAdr = newInfo; } -#endif void SV_Register(void) { @@ -609,7 +607,6 @@ AutoStr* SV_ComposeSlotIdentifier(int slot) void SV_ClearSlot(int slot) { - SaveInfo* info; AutoStr* path; errorIfNotInited("SV_ClearSlot"); @@ -639,11 +636,7 @@ void SV_ClearSlot(int slot) SV_RemoveFile(path); // Update save info for this slot. - info = findSaveInfoForSlot(slot); - if(info) - { - updateSaveInfo(path, info); - } + updateSaveInfo(path, findSaveInfoForSlot(slot)); } boolean SV_IsValidSlot(int slot) @@ -851,7 +844,6 @@ boolean SV_HxHaveMapSaveForSlot(int slot, uint map) void SV_CopySlot(int sourceSlot, int destSlot) { AutoStr* src, *dst; - SaveInfo* info; errorIfNotInited("SV_CopySlot"); @@ -886,12 +878,8 @@ void SV_CopySlot(int sourceSlot, int destSlot) dst = composeGameSavePathForSlot(destSlot); SV_CopyFile(src, dst); - // Update save info for the destination slot. - info = findSaveInfoForSlot(destSlot); - if(info) - { - updateSaveInfo(dst, info); - } + // Copy saveinfo too. + replaceSaveInfo(destSlot, SaveInfo_NewCopy(findSaveInfoForSlot(sourceSlot))); } #if __JHEXEN__ @@ -4325,7 +4313,7 @@ assert(thInfo->Write); * Clients do not save all data for all thinkers (the server will send * it to us anyway so saving it would just bloat client save games). * - * \note Some thinker classes are NEVER saved by clients. + * @note Some thinker classes are NEVER saved by clients. */ static void P_ArchiveThinkers(boolean savePlayers) { @@ -5335,7 +5323,8 @@ boolean SV_LoadGame(int slot) #if __JHEXEN__ // Copy all needed save files to the base slot. - /// @todo Why do this BEFORE loading?? + /// @todo Why do this BEFORE loading?? (G_NewGame() does not load the serialized map state) + /// @todo Does any caller ever attempt to load the base slot?? (Doesn't seem logical) if(slot != BASE_SLOT) { SV_CopySlot(slot, BASE_SLOT); @@ -5664,15 +5653,12 @@ boolean SV_SaveGame(int slot, const char* name) if(!saveError) { -#if !__JHEXEN__ // Swap the save info. - replaceSaveInfo(slot, info); -#else - // We no longer need the info. - SaveInfo_Delete(info); + replaceSaveInfo(logicalSlot, info); +#if !__JHEXEN__ // Copy base slot to destination slot. - SV_CopySlot(BASE_SLOT, slot); + SV_CopySlot(logicalSlot, slot); #endif // The "last" save slot is now this. diff --git a/doomsday/plugins/common/src/saveinfo.c b/doomsday/plugins/common/src/saveinfo.c index d8b15d26f7..870f1cac01 100644 --- a/doomsday/plugins/common/src/saveinfo.c +++ b/doomsday/plugins/common/src/saveinfo.c @@ -41,6 +41,11 @@ SaveInfo* SaveInfo_New(void) return info; } +SaveInfo* SaveInfo_NewCopy(const SaveInfo* other) +{ + return SaveInfo_Copy(SaveInfo_New(), other); +} + void SaveInfo_Delete(SaveInfo* info) { DENG_ASSERT(info); @@ -48,20 +53,29 @@ void SaveInfo_Delete(SaveInfo* info) free(info); } -uint SaveInfo_GameId(SaveInfo* info) +SaveInfo* SaveInfo_Copy(SaveInfo* self, const SaveInfo* other) +{ + DENG_ASSERT(self); + if(!other) return self; + Str_Copy(&self->name, SaveInfo_Name(other)); + self->gameId = SaveInfo_GameId(other); + memcpy(&self->header, SaveInfo_Header(other), sizeof(self->header)); + return self; +} + +uint SaveInfo_GameId(const SaveInfo* info) { DENG_ASSERT(info); return info->gameId; } -const saveheader_t* SaveInfo_Header(SaveInfo* info) +const saveheader_t* SaveInfo_Header(const SaveInfo* info) { DENG_ASSERT(info); - //if(!SaveInfo_IsLoadable(info)) return NULL; return &info->header; } -const ddstring_t* SaveInfo_Name(SaveInfo* info) +const ddstring_t* SaveInfo_Name(const SaveInfo* info) { DENG_ASSERT(info); return &info->name;