Skip to content

Commit

Permalink
Game Save|Refactor: Cleaning up save state recognition
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Jul 2, 2012
1 parent 2cdadd1 commit c817463
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 111 deletions.
7 changes: 2 additions & 5 deletions doomsday/plugins/common/include/p_saveio.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,7 @@ short SV_ReadShort(void);
long SV_ReadLong(void);
float SV_ReadFloat(void);

void SV_SaveInfo_Write(SaveInfo* info);
void SV_SaveInfo_Read(SaveInfo* info);

void SV_MaterialArchive_Write(MaterialArchive* arc);
void SV_MaterialArchive_Read(MaterialArchive* arc, int version);
Writer* SV_NewWriter(void);
Reader* SV_NewReader(void);

#endif /* LIBCOMMON_SAVESTATE_INPUT_OUTPUT_H */
93 changes: 88 additions & 5 deletions doomsday/plugins/common/src/p_saveg.c
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,29 @@ boolean SV_IsUserWritableSlot(int slot)
return SV_IsValidSlot(slot);
}

boolean SV_Recognise(SaveInfo* info)
static void SV_SaveInfo_Read(SaveInfo* info)
{
Reader* svReader = SV_NewReader();
#if __JHEXEN__
// Read the magic byte to determine the high-level format.
int magic = Reader_ReadInt32(svReader);
SV_HxSavePtr()->b -= 4; // Rewind the stream.

if((!IS_NETWORK_CLIENT && magic != MY_SAVE_MAGIC) ||
( IS_NETWORK_CLIENT && magic != MY_CLIENT_SAVE_MAGIC))
{
// Perhaps the old v9 format?
SaveInfo_Read_Hx_v9(info, svReader);
}
else
#endif
{
SaveInfo_Read(info, svReader);
}
Reader_Delete(svReader);
}

static boolean recogniseSaveState(SaveInfo* info)
{
#if __JHEXEN__
byte* saveBuffer;
Expand Down Expand Up @@ -616,6 +638,53 @@ boolean SV_Recognise(SaveInfo* info)
return false;
}

#if __JDOOM__
static boolean recogniseSaveState_Dm_v19(SaveInfo* info)
{
if(!info || Str_IsEmpty(SaveInfo_FilePath(info))) return false;
if(SV_OpenFile(Str_Text(SaveInfo_FilePath(info)), "r"))
{
Reader* svReader = SV_NewReader();
SaveInfo_Read_Dm_v19(info, svReader);
SV_CloseFile();
Reader_Delete(svReader);
svReader = NULL;
return true;
}
return false;
}
#endif

#if __JHERETIC__
static boolean recogniseSaveState_Hr_v13(SaveInfo* info)
{
if(!info || Str_IsEmpty(SaveInfo_FilePath(info))) return false;
if(SV_OpenFile(Str_Text(SaveInfo_FilePath(info)), "r"))
{
Reader* svReader = SV_NewReader();
SaveInfo_Read_Hr_v13(info, svReader);
SV_CloseFile();
Reader_Delete(svReader);
svReader = NULL;
return true;
}
return false;
}
#endif

boolean SV_Recognise(SaveInfo* info)
{
if(recogniseSaveState(info)) return true;
// Perhaps an original game save?
#if __JDOOM__
if(recogniseSaveState_Dm_v19(info)) return true;
#endif
#if __JHERETIC__
if(recogniseSaveState_Hr_v13(info)) return true;
#endif
return false;
}

SaveInfo* SV_SaveInfoForSlot(int slot)
{
errorIfNotInited("SV_SaveInfoForSlot");
Expand Down Expand Up @@ -2774,7 +2843,10 @@ static void P_ArchiveWorld(void)
{
uint i;

SV_MaterialArchive_Write(materialArchive);
{ Writer* svWriter = SV_NewWriter();
MaterialArchive_Write(materialArchive, svWriter);
Writer_Delete(svWriter);
}

SV_BeginSegment(ASEG_WORLD);
for(i = 0; i < numsectors; ++i)
Expand Down Expand Up @@ -2807,7 +2879,11 @@ static void P_UnArchiveWorld(void)
#if !__JHEXEN__
if(hdr->version >= 4)
#endif
SV_MaterialArchive_Read(materialArchive, matArchiveVer);
{
Reader* svReader = SV_NewReader();
MaterialArchive_Read(materialArchive, matArchiveVer, svReader);
Reader_Delete(svReader);
}

SV_AssertSegment(ASEG_WORLD);
// Load sectors.
Expand Down Expand Up @@ -5141,7 +5217,11 @@ void SV_SaveGameClient(uint gameId)
saveInfo = SaveInfo_New();
SaveInfo_SetGameId(saveInfo, gameId);
SaveInfo_Configure(saveInfo);
SV_SaveInfo_Write(saveInfo);

{Writer* svWriter = SV_NewWriter();
SaveInfo_Write(saveInfo, svWriter);
Writer_Delete(svWriter);
}

// Some important information.
// Our position and look angles.
Expand Down Expand Up @@ -5299,7 +5379,10 @@ static int saveGameWorker(void* parameters)
playerHeaderOK = false; // Uninitialized.

// Write the game session header.
SV_SaveInfo_Write(saveInfo);
{ Writer* svWriter = SV_NewWriter();
SaveInfo_Write(saveInfo, svWriter);
Writer_Delete(svWriter);
}

#if __JHEXEN__
P_ArchiveGlobalScriptData();
Expand Down
41 changes: 4 additions & 37 deletions doomsday/plugins/common/src/p_saveio.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,18 +371,9 @@ static void swd(Writer* w, const char* data, int len)
SV_Write(data, len);
}

void SV_SaveInfo_Write(SaveInfo* info)
Writer* SV_NewWriter(void)
{
Writer* svWriter = Writer_NewWithCallbacks(swi8, swi16, swi32, swf, swd);
SaveInfo_Write(info, svWriter);
Writer_Delete(svWriter);
}

void SV_MaterialArchive_Write(MaterialArchive* arc)
{
Writer* svWriter = Writer_NewWithCallbacks(swi8, swi16, swi32, swf, swd);
MaterialArchive_Write(arc, svWriter);
Writer_Delete(svWriter);
return Writer_NewWithCallbacks(swi8, swi16, swi32, swf, swd);
}

static char sri8(Reader* r)
Expand Down Expand Up @@ -415,31 +406,7 @@ static void srd(Reader* r, char* data, int len)
SV_Read(data, len);
}

void SV_SaveInfo_Read(SaveInfo* info)
{
Reader* svReader = Reader_NewWithCallbacks(sri8, sri16, sri32, srf, srd);
#if __JHEXEN__
// Read the magic byte to determine the high-level format.
int magic = Reader_ReadInt32(svReader);
saveptr.b -= 4; // Rewind the stream.

if((!IS_NETWORK_CLIENT && magic != MY_SAVE_MAGIC) ||
( IS_NETWORK_CLIENT && magic != MY_CLIENT_SAVE_MAGIC))
{
// Perhaps the old v9 format?
SaveInfo_Read_Hx_v9(info, svReader);
}
else
#endif
{
SaveInfo_Read(info, svReader);
}
Reader_Delete(svReader);
}

void SV_MaterialArchive_Read(MaterialArchive* arc, int version)
Reader* SV_NewReader(void)
{
Reader* svReader = Reader_NewWithCallbacks(sri8, sri16, sri32, srf, srd);
MaterialArchive_Read(arc, version, svReader);
Reader_Delete(svReader);
return Reader_NewWithCallbacks(sri8, sri16, sri32, srf, srd);
}
17 changes: 1 addition & 16 deletions doomsday/plugins/common/src/saveinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,6 @@ boolean SaveInfo_IsLoadable(SaveInfo* info)
return true; // It's good!
}

static boolean recogniseAndReadHeader(SaveInfo* info)
{
if(SV_Recognise(info)) return true;

// Perhaps an original game save?
#if __JDOOM__
if(SV_v19_Recognise(info)) return true;
#endif
#if __JHERETIC__
if(SV_v13_Recognise(info)) return true;
#endif

return false;
}

void SaveInfo_Update(SaveInfo* info)
{
assert(info);
Expand All @@ -194,7 +179,7 @@ void SaveInfo_Update(SaveInfo* info)
return;
}

if(!recogniseAndReadHeader(info))
if(!SV_Recognise(info))
{
// Not a loadable save.
return;
Expand Down
2 changes: 1 addition & 1 deletion doomsday/plugins/jdoom/include/p_oldsvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

#include "p_saveio.h"

boolean SV_v19_Recognise(SaveInfo* saveInfo);
void SaveInfo_Read_Dm_v19(SaveInfo* info, Reader* reader);

/**
* @return @c 0 on success else error code.
Expand Down
33 changes: 10 additions & 23 deletions doomsday/plugins/jdoom/src/p_oldsvg.c
Original file line number Diff line number Diff line change
Expand Up @@ -911,36 +911,36 @@ int SV_v19_LoadGame(SaveInfo* info)
return 0; // Success!
}

void SaveInfo_Read_Dm_v19(SaveInfo* info)
void SaveInfo_Read_Dm_v19(SaveInfo* info, Reader* reader)
{
saveheader_t* hdr = &info->header;
char nameBuffer[V19_SAVESTRINGSIZE];
char vcheck[VERSIONSIZE];
int i;
assert(info);

SV_Read(nameBuffer, V19_SAVESTRINGSIZE);
Reader_Read(reader, nameBuffer, V19_SAVESTRINGSIZE);
nameBuffer[V19_SAVESTRINGSIZE - 1] = 0;
Str_Set(&info->name, nameBuffer);

// Check version.
SV_Read(vcheck, VERSIONSIZE);
Reader_Read(reader, vcheck, VERSIONSIZE);
hdr->version = atoi(&vcheck[8]);
hdr->version = MY_SAVE_VERSION; /// @todo Remove me.

hdr->skill = SV_ReadByte();
hdr->episode = SV_ReadByte()-1;
hdr->map = SV_ReadByte()-1;
hdr->skill = Reader_ReadByte(reader);
hdr->episode = Reader_ReadByte(reader)-1;
hdr->map = Reader_ReadByte(reader)-1;
for(i = 0; i < 4; ++i)
{
hdr->players[i] = SV_ReadByte();
hdr->players[i] = Reader_ReadByte(reader);
}
memset(&hdr->players[4], 0, sizeof(*hdr->players) * (MAXPLAYERS-4));

// Get the map time.
{ int a = SV_ReadByte();
int b = SV_ReadByte();
int c = SV_ReadByte();
{ int a = Reader_ReadByte(reader);
int b = Reader_ReadByte(reader);
int c = Reader_ReadByte(reader);
hdr->mapTime = (a << 16) + (b << 8) + c;
}

Expand All @@ -954,16 +954,3 @@ void SaveInfo_Read_Dm_v19(SaveInfo* info)

info->gameId = 0; // None.
}

boolean SV_v19_Recognise(SaveInfo* info)
{
if(!info || Str_IsEmpty(SaveInfo_FilePath(info))) return false;

if(SV_OpenFile(Str_Text(SaveInfo_FilePath(info)), "r"))
{
SaveInfo_Read_Dm_v19(info);
SV_CloseFile();
return true;
}
return false;
}
2 changes: 1 addition & 1 deletion doomsday/plugins/jheretic/include/p_oldsvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

#include "p_saveio.h"

boolean SV_v13_Recognise(SaveInfo* saveInfo);
void SaveInfo_Read_Hr_v13(SaveInfo* info, Reader* reader);

/**
* @return @c 0 on success else error code.
Expand Down
33 changes: 10 additions & 23 deletions doomsday/plugins/jheretic/src/p_oldsvg.c
Original file line number Diff line number Diff line change
Expand Up @@ -920,36 +920,36 @@ int SV_v13_LoadGame(SaveInfo* info)
return 0; // Success!
}

void SaveInfo_Read_Hr_v13(SaveInfo* info)
void SaveInfo_Read_Hr_v13(SaveInfo* info, Reader* reader)
{
saveheader_t* hdr = &info->header;
char nameBuffer[V13_SAVESTRINGSIZE];
char vcheck[VERSIONSIZE];
int i;
assert(info);

SV_Read(nameBuffer, V13_SAVESTRINGSIZE);
Reader_Read(reader, nameBuffer, V13_SAVESTRINGSIZE);
nameBuffer[V13_SAVESTRINGSIZE - 1] = 0;
Str_Set(&info->name, nameBuffer);

// Check version.
SV_Read(vcheck, VERSIONSIZE);
Reader_Read(reader, vcheck, VERSIONSIZE);
hdr->version = atoi(&vcheck[8]);
hdr->version = MY_SAVE_VERSION; /// @todo Remove me.

hdr->skill = SV_ReadByte();
hdr->episode = SV_ReadByte()-1;
hdr->map = SV_ReadByte()-1;
hdr->skill = Reader_ReadByte(reader);
hdr->episode = Reader_ReadByte(reader)-1;
hdr->map = Reader_ReadByte(reader)-1;
for(i = 0; i < 4; ++i)
{
hdr->players[i] = SV_ReadByte();
hdr->players[i] = Reader_ReadByte(reader);
}
memset(&hdr->players[4], 0, sizeof(*hdr->players) * (MAXPLAYERS-4));

// Get the map time.
{ int a = SV_ReadByte();
int b = SV_ReadByte();
int c = SV_ReadByte();
{ int a = Reader_ReadByte(reader);
int b = Reader_ReadByte(reader);
int c = Reader_ReadByte(reader);
hdr->mapTime = (a << 16) + (b << 8) + c;
}

Expand All @@ -963,16 +963,3 @@ void SaveInfo_Read_Hr_v13(SaveInfo* info)

info->gameId = 0; // None.
}

boolean SV_v13_Recognise(SaveInfo* info)
{
if(!info || Str_IsEmpty(SaveInfo_FilePath(info))) return false;

if(SV_OpenFile(Str_Text(SaveInfo_FilePath(info)), "r"))
{
SaveInfo_Read_Hr_v13(info);
SV_CloseFile();
return true;
}
return false;
}

0 comments on commit c817463

Please sign in to comment.