Skip to content

Commit

Permalink
Refactor|Game Save: SaveInfos are now freely swappable (in Hexen) as …
Browse files Browse the repository at this point in the history
…intended
  • Loading branch information
danij-deng committed Jul 31, 2012
1 parent d6e62ae commit 90a83de
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
9 changes: 6 additions & 3 deletions doomsday/plugins/common/include/saveinfo.h
Expand Up @@ -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);

Expand Down
32 changes: 9 additions & 23 deletions doomsday/plugins/common/src/p_saveg.c
Expand Up @@ -557,7 +557,6 @@ static SaveInfo* findSaveInfoForSlot(int slot)
return saveInfo[slot];
}

#if !__JHEXEN__
static void replaceSaveInfo(int slot, SaveInfo* newInfo)
{
SaveInfo** destAdr;
Expand All @@ -579,7 +578,6 @@ static void replaceSaveInfo(int slot, SaveInfo* newInfo)
if(*destAdr) SaveInfo_Delete(*destAdr);
*destAdr = newInfo;
}
#endif

void SV_Register(void)
{
Expand Down Expand Up @@ -609,7 +607,6 @@ AutoStr* SV_ComposeSlotIdentifier(int slot)

void SV_ClearSlot(int slot)
{
SaveInfo* info;
AutoStr* path;

errorIfNotInited("SV_ClearSlot");
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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__
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
22 changes: 18 additions & 4 deletions doomsday/plugins/common/src/saveinfo.c
Expand Up @@ -41,27 +41,41 @@ 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);
Str_Free(&info->name);
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;
Expand Down

0 comments on commit 90a83de

Please sign in to comment.