Skip to content

Commit

Permalink
Game Save|Refactor: Began transforming saveinfo_t into a object
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Jun 25, 2012
1 parent bc6da41 commit 73884e9
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 112 deletions.
37 changes: 18 additions & 19 deletions doomsday/plugins/common/include/common.h
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
/**\file
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
/**
* @file common.h
*
*\author Copyright © 2011 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright &copy; 2011-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright &copy; 2012 Daniel Swanson <danij@dengine.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/

#ifndef LIBCOMMON_GAME_INCLUDES
#define LIBCOMMON_GAME_INCLUDES

#define WEAPONBOTTOM (128) // from p_pspr.c

#define IS_NETWORK_SERVER (DD_GetInteger(DD_SERVER) && DD_GetInteger(DD_NETGAME))
#define IS_NETWORK_SERVER (DD_GetInteger(DD_SERVER) && DD_GetInteger(DD_NETGAME))
#define IS_NETWORK_CLIENT (DD_GetInteger(DD_CLIENT) && DD_GetInteger(DD_NETGAME))

#if __JDOOM__
# include "jdoom.h"
Expand Down
4 changes: 2 additions & 2 deletions doomsday/plugins/common/include/p_saveio.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ short SV_ReadShort(void);
long SV_ReadLong(void);
float SV_ReadFloat(void);

void SV_SaveInfo_Write(saveheader_t* info);
void SV_SaveInfo_Read(saveheader_t* info);
void SV_SaveInfo_Write(saveinfo_t* info);
void SV_SaveInfo_Read(saveinfo_t* info);

void SV_MaterialArchive_Write(MaterialArchive* arc);
void SV_MaterialArchive_Read(MaterialArchive* arc, int version);
Expand Down
166 changes: 85 additions & 81 deletions doomsday/plugins/common/src/p_saveg.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,59 @@ static void initSaveInfo(saveinfo_t* info)
if(!info) return;
Str_Init(&info->filePath);
Str_Init(&info->name);

memset(&info->header, 0, sizeof(info->header));
}

static void clearSaveInfo(saveinfo_t* info)
{
if(!info) return;
Str_Free(&info->filePath);
Str_Free(&info->name);
}

static void setNameOfSaveInfo(saveinfo_t* info, const char* newName)
{
if(!info) return;
dd_snprintf(info->header.name, SAVESTRINGSIZE, "%s", newName);
}

static void configureSaveInfoHeader(saveinfo_t* saveInfo, uint gameId)
{
saveheader_t* hdr = &saveInfo->header;

hdr->magic = IS_NETWORK_CLIENT? MY_CLIENT_SAVE_MAGIC : MY_SAVE_MAGIC;
hdr->version = MY_SAVE_VERSION;
hdr->gameMode = gameMode;
hdr->gameId = gameId;

hdr->map = gameMap+1;
#if __JHEXEN__
hdr->episode = 1;
#else
hdr->episode = gameEpisode+1;
#endif
#if __JHEXEN__
hdr->skill = gameSkill;
hdr->randomClasses = randomClassParm;
#else
hdr->skill = gameSkill;
if(fastParm) hdr->skill |= 0x80; // Set high byte.
#endif
hdr->deathmatch = deathmatch;
hdr->noMonsters = noMonstersParm;

#if __JHEXEN__
hdr->randomClasses = randomClassParm;
#else
hdr->respawnMonsters = respawnMonsters;
hdr->mapTime = mapTime;
{ int i;
for(i = 0; i < MAXPLAYERS; i++)
{
hdr->players[i] = players[i].plr->inGame;
}}
#endif
}

static boolean saveInfoIsValidForCurrentGameSession(saveinfo_t* info)
Expand Down Expand Up @@ -458,13 +511,6 @@ static void updateSaveInfo(saveinfo_t* info, ddstring_t* savePath)
}
}

static void clearSaveInfo(saveinfo_t* info)
{
if(!info) return;
Str_Free(&info->filePath);
Str_Free(&info->name);
}

/**
* Compose the (possibly relative) path to the game-save associated
* with the logical save @a slot.
Expand Down Expand Up @@ -629,19 +675,20 @@ boolean SV_Recognise(saveinfo_t* info)
SV_HxSavePtr()->b = saveBuffer;
#endif

SV_SaveInfo_Read(&info->header);
SV_SaveInfo_Read(info);
if(MY_SAVE_MAGIC == info->header.magic)
{
Str_Set(&info->name, info->header.name);
}

#if __JHEXEN__
Z_Free(saveBuffer);
#else
SV_CloseFile();
#endif

if(MY_SAVE_MAGIC == info->header.magic)
{
Str_Set(&info->name, info->header.name);
return true;
}
return (MY_SAVE_MAGIC == info->header.magic) ||
(MY_CLIENT_SAVE_MAGIC == info->header.magic);
}
return false;
}
Expand Down Expand Up @@ -4919,49 +4966,6 @@ void SV_Shutdown(void)
inited = false;
}

static void writeSaveHeader(const char* saveName, uint gameId)
{
saveheader_t hdr;

memset(&hdr, 0, sizeof(hdr));

hdr.magic = MY_SAVE_MAGIC;
hdr.version = MY_SAVE_VERSION;
hdr.gameMode = gameMode;
dd_snprintf(hdr.name, SAVESTRINGSIZE, "%s", saveName);

hdr.map = gameMap+1;
#if __JHEXEN__
hdr.episode = 1;
#else
hdr.episode = gameEpisode+1;
#endif
#if __JHEXEN__
hdr.skill = gameSkill;
hdr.randomClasses = randomClassParm;
#else
hdr.skill = gameSkill;
if(fastParm) hdr.skill |= 0x80; // Set high byte.
#endif
hdr.deathmatch = deathmatch;
hdr.noMonsters = noMonstersParm;

#if __JHEXEN__
hdr.randomClasses = randomClassParm;
#else
hdr.respawnMonsters = respawnMonsters;
hdr.mapTime = mapTime;
hdr.gameId = gameId;
{ int i;
for(i = 0; i < MAXPLAYERS; i++)
{
hdr.players[i] = players[i].plr->inGame;
}}
#endif

SV_SaveInfo_Write(&hdr);
}

static boolean SV_LoadGame2(saveinfo_t* saveInfo)
{
int i;
Expand All @@ -4974,8 +4978,10 @@ static boolean SV_LoadGame2(saveinfo_t* saveInfo)
// Read the header again.
/// @todo Seek past the header straight to the game state.
{
saveheader_t tmp;
saveinfo_t tmp;
initSaveInfo(&tmp);
SV_SaveInfo_Read(&tmp);
clearSaveInfo(&tmp);
}
hdr = &saveInfo->header;

Expand Down Expand Up @@ -5222,11 +5228,11 @@ boolean SV_LoadGame(int slot)

void SV_SaveGameClient(uint gameId)
{
#if !__JHEXEN__ // unsupported in jHexen
#if !__JHEXEN__ // unsupported in libhexen
player_t* pl = &players[CONSOLEPLAYER];
mobj_t* mo = pl->plr->mo;
ddstring_t gameSavePath;
saveheader_t hdr;
saveinfo_t saveInfo;

errorIfNotInited("SV_SaveGameClient");

Expand All @@ -5246,18 +5252,9 @@ void SV_SaveGameClient(uint gameId)
Str_Free(&gameSavePath);

// Prepare the header.
memset(&hdr, 0, sizeof(hdr));
hdr.magic = MY_CLIENT_SAVE_MAGIC;
hdr.version = MY_SAVE_VERSION;
hdr.skill = gameSkill;
hdr.episode = gameEpisode+1;
hdr.map = gameMap+1;
hdr.deathmatch = deathmatch;
hdr.noMonsters = noMonstersParm;
hdr.respawnMonsters = respawnMonsters;
hdr.mapTime = mapTime;
hdr.gameId = gameId;
SV_SaveInfo_Write(&hdr);
initSaveInfo(&saveInfo);
configureSaveInfoHeader(&saveInfo, gameId);
SV_SaveInfo_Write(&saveInfo);

// Some important information.
// Our position and look angles.
Expand All @@ -5281,19 +5278,17 @@ void SV_SaveGameClient(uint gameId)
materialArchive = NULL;

SV_CloseFile();

// Update our game save info.
SV_UpdateAllSaveInfo();
clearSaveInfo(&saveInfo);
#endif
}

void SV_LoadGameClient(uint gameId)
{
#if !__JHEXEN__ // unsupported in jHexen
#if !__JHEXEN__ // unsupported in libhexen
player_t* cpl = players + CONSOLEPLAYER;
mobj_t* mo = cpl->plr->mo;
ddstring_t gameSavePath;
saveheader_t clientSaveHeader;
saveinfo_t saveInfo;

errorIfNotInited("SV_LoadGameClient");

Expand All @@ -5313,10 +5308,13 @@ void SV_LoadGameClient(uint gameId)
}
Str_Free(&gameSavePath);

SV_SaveInfo_Read(&clientSaveHeader);
hdr = &clientSaveHeader;
initSaveInfo(&saveInfo);
SV_SaveInfo_Read(&saveInfo);

hdr = &saveInfo.header;
if(hdr->magic != MY_CLIENT_SAVE_MAGIC)
{
clearSaveInfo(&saveInfo);
SV_CloseFile();
Con_Message("SV_LoadGameClient: Bad magic!\n");
return;
Expand Down Expand Up @@ -5362,6 +5360,7 @@ void SV_LoadGameClient(uint gameId)
MaterialArchive_Delete(materialArchive);
materialArchive = NULL;

clearSaveInfo(&saveInfo);
SV_CloseFile();
#endif
}
Expand Down Expand Up @@ -5416,7 +5415,8 @@ typedef struct savegameparams_s {
static int saveGameWorker(void* parameters)
{
savegameworkerparams_t* parm = parameters;
uint gameId;
uint gameId = SV_GenerateGameId();
saveinfo_t saveInfo;

#if _DEBUG
VERBOSE( Con_Message("SV_SaveGame: Attempting save game to \"%s\".\n", Str_Text(parm->path)) )
Expand All @@ -5431,8 +5431,10 @@ static int saveGameWorker(void* parameters)
playerHeaderOK = false; // Uninitialized.

// Write the game session header.
gameId = SV_GenerateGameId();
writeSaveHeader(parm->name, gameId);
initSaveInfo(&saveInfo);
setNameOfSaveInfo(&saveInfo, parm->name);
configureSaveInfoHeader(&saveInfo, gameId);
SV_SaveInfo_Write(&saveInfo);

#if __JHEXEN__
P_ArchiveGlobalScriptData();
Expand Down Expand Up @@ -5499,6 +5501,8 @@ static int saveGameWorker(void* parameters)
SV_CloseFile();
#endif

clearSaveInfo(&saveInfo);

Con_BusyWorkerEnd();
return SV_OK;
}
Expand Down
Loading

0 comments on commit 73884e9

Please sign in to comment.