From c557381c44efee68b3c8bbb1d721b63788787f48 Mon Sep 17 00:00:00 2001 From: danij Date: Fri, 22 Jun 2012 04:40:06 +0100 Subject: [PATCH] libcommon|Refactor: Use SV_Header_Read() to read Hexen save headers Todo: Presently the whole save file is buffered in order to read the save header. This is unnecessary with further refactoring... --- doomsday/plugins/common/include/p_saveio.h | 4 +- doomsday/plugins/common/src/p_saveio.c | 53 ++++++++++------------ 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/doomsday/plugins/common/include/p_saveio.h b/doomsday/plugins/common/include/p_saveio.h index a4e1cd87d4..7c6c7128f3 100644 --- a/doomsday/plugins/common/include/p_saveio.h +++ b/doomsday/plugins/common/include/p_saveio.h @@ -36,10 +36,8 @@ typedef struct saveheader_s { int magic; #endif int version; -#if !__JHEXEN__ - int gameMode; + int gameMode; // Presently unused in Hexen. char name[SAVESTRINGSIZE]; -#endif byte skill; byte episode; byte map; diff --git a/doomsday/plugins/common/src/p_saveio.c b/doomsday/plugins/common/src/p_saveio.c index a019d68aba..671895ad65 100644 --- a/doomsday/plugins/common/src/p_saveio.c +++ b/doomsday/plugins/common/src/p_saveio.c @@ -341,41 +341,39 @@ static boolean readGameSaveHeader(gamesaveinfo_t* info) { boolean found = false; #if __JHEXEN__ - LZFILE* fp; + byte* saveBuffer; #endif assert(inited && info); if(Str_IsEmpty(&info->filePath)) return false; #if __JHEXEN__ - fp = lzOpen(Str_Text(&info->filePath), "rp"); - if(fp) - { - // Read the header. - char versionText[HXS_VERSION_TEXT_LENGTH]; - char nameBuffer[SAVESTRINGSIZE]; - lzRead(nameBuffer, SAVESTRINGSIZE, fp); - lzRead(versionText, HXS_VERSION_TEXT_LENGTH, fp); - lzClose(fp); fp = NULL; - if(!strncmp(versionText, HXS_VERSION_TEXT, 8)) - { - const int ver = atoi(&versionText[8]); - if(ver <= MY_SAVE_VERSION) - { - Str_Set(&info->name, nameBuffer); - found = true; - } - } - } + /// @todo Do not buffer the whole file. + if(M_ReadFile(Str_Text(&info->filePath), (char**)&saveBuffer)) #else if(SV_OpenFile(Str_Text(&info->filePath), "rp")) +#endif { saveheader_t* hdr = SV_SaveHeader(); +#if __JHEXEN__ + // Set the save pointer. + SV_HxSavePtr()->b = saveBuffer; +#endif + SV_Header_Read(hdr); + +#if __JHEXEN__ + Z_Free(saveBuffer); +#else SV_CloseFile(); +#endif +#if __JHEXEN__ + if(!strncmp((const char*) hdr->magic, HXS_VERSION_TEXT, 8)) +#else if(MY_SAVE_MAGIC == hdr->magic) +#endif { Str_Set(&info->name, hdr->name); found = true; @@ -383,7 +381,7 @@ static boolean readGameSaveHeader(gamesaveinfo_t* info) } // If not found or not recognized try other supported formats. -#if !__JDOOM64__ +#if __JDOOM__ || __JHERETIC__ if(!found) { // Perhaps a DOOM(2).EXE v19 saved game? @@ -397,7 +395,6 @@ static boolean readGameSaveHeader(gamesaveinfo_t* info) found = true; } } -# endif #endif // Ensure we have a non-empty name. @@ -856,15 +853,15 @@ static void srd(Reader* r, char* data, int len) #if __JHEXEN__ void SV_Header_Read(saveheader_t* hdr) { - // Skip the name field. - SV_HxSavePtr()->b += SAVESTRINGSIZE; + char verText[HXS_VERSION_TEXT_LENGTH]; - memcpy(hdr->magic, SV_HxSavePtr()->b, 8); - - hdr->version = atoi((const char*) (SV_HxSavePtr()->b + 8)); - SV_HxSavePtr()->b += HXS_VERSION_TEXT_LENGTH; + SV_Read(&hdr->name, SAVESTRINGSIZE); + SV_Read(&verText, HXS_VERSION_TEXT_LENGTH); + hdr->version = atoi(&verText[8]); + memcpy(hdr->magic, verText, 8); SV_AssertSegment(ASEG_GAME_HEADER); + hdr->episode = 1; hdr->map = SV_ReadByte(); hdr->skill = SV_ReadByte();