From 175cb57c52c5f1ccbc1a94407c2b47aaf2b32fc4 Mon Sep 17 00:00:00 2001 From: danij Date: Fri, 21 Mar 2014 01:21:45 +0000 Subject: [PATCH] libcommon|MapStateWriter: Replaced map state data Writer Avoiding the need to update all of the existing serialization logic the new writer takes advantage of the callback nature of libdeng1's Writer, using this as a C API which wraps an instance of de::Writer that takes the source de::File as input. As of this commit, libcommon is no longer dependent on the external LZSS library. Todo for later: Update all map state serialization logic to use the native serialization components provided by libdeng2. --- doomsday/plugins/common/include/p_saveio.h | 12 +-- doomsday/plugins/common/src/g_game.cpp | 4 +- .../plugins/common/src/gamesessionwriter.cpp | 8 +- .../plugins/common/src/mapstatereader.cpp | 2 +- doomsday/plugins/common/src/p_saveg.cpp | 8 +- doomsday/plugins/common/src/p_saveio.cpp | 87 +++++++------------ 6 files changed, 51 insertions(+), 70 deletions(-) diff --git a/doomsday/plugins/common/include/p_saveio.h b/doomsday/plugins/common/include/p_saveio.h index 744aa86757..a98f9fec75 100644 --- a/doomsday/plugins/common/include/p_saveio.h +++ b/doomsday/plugins/common/include/p_saveio.h @@ -21,8 +21,9 @@ #ifndef LIBCOMMON_SAVESTATE_INPUT_OUTPUT_H #define LIBCOMMON_SAVESTATE_INPUT_OUTPUT_H -#include -#include +//#include +//#include +#include #include #include #include @@ -50,7 +51,8 @@ typedef enum savestatesegment_e { */ void SV_CloseFile(); -bool SV_OpenFile(de::File const &file); +bool SV_OpenFileForRead(de::File const &file); +bool SV_OpenFileForWrite(de::File &file); #if 0 bool SV_OpenFile_LZSS(de::Path filePath); @@ -69,11 +71,11 @@ void SV_BeginSegment(int segmentId); void SV_EndSegment(); void SV_WriteSessionMetadata(de::game::SessionMetadata const &metadata, Writer *writer); -#endif void SV_WriteConsistencyBytes(); -//void SV_ReadConsistencyBytes(); +void SV_ReadConsistencyBytes(); +#endif Writer *SV_NewWriter(); diff --git a/doomsday/plugins/common/src/g_game.cpp b/doomsday/plugins/common/src/g_game.cpp index 5775c6f4ad..f333a5783e 100644 --- a/doomsday/plugins/common/src/g_game.cpp +++ b/doomsday/plugins/common/src/g_game.cpp @@ -3005,7 +3005,7 @@ void G_DoLeaveMap() // SaveSlot &sslot = G_SaveSlots()["base"]; de::Path const mapStateFilePath(Str_Text(Uri_Compose(gameMapUri))); - if(!SV_OpenFile_LZSS(mapStateFilePath)) + if(!SV_OpenFileForWrite(mapStateFilePath)) { throw de::Error("G_DoLeaveMap", "Failed opening \"" + de::NativePath(mapStateFilePath).pretty() + "\" for write"); } @@ -3019,7 +3019,7 @@ void G_DoLeaveMap() MapStateWriter(thingArchive).write(writer); Writer_Delete(writer); - SV_CloseFile_LZSS(); + SV_CloseFile(); } } else // Entering new hub. diff --git a/doomsday/plugins/common/src/gamesessionwriter.cpp b/doomsday/plugins/common/src/gamesessionwriter.cpp index 860b2fc925..30aeb27fbe 100644 --- a/doomsday/plugins/common/src/gamesessionwriter.cpp +++ b/doomsday/plugins/common/src/gamesessionwriter.cpp @@ -134,7 +134,7 @@ void GameSessionWriter::write(Path const &stateFilePath, Path const &mapStateFil NetSv_SaveGame(metadata->geti("sessionId")); #endif - if(!SV_OpenFile(stateFilePath)) + if(!SV_OpenFileForWrite(stateFilePath)) { throw FileAccessError("GameSessionWriter", "Failed opening \"" + NativePath(stateFilePath).pretty() + "\" for write"); } @@ -160,16 +160,16 @@ void GameSessionWriter::write(Path const &stateFilePath, Path const &mapStateFil { // The map state is actually written to a separate file. // Close the game state file. - //SV_CloseFile(); + SV_CloseFile(); // Open the map state file. - SV_OpenFile(mapStateFilePath); + SV_OpenFileForWrite(mapStateFilePath); } d->writeMap(); d->writeConsistencyBytes(); // To be absolutely sure... - //SV_CloseFile(); + SV_CloseFile(); File &outFile = App::rootFolder().locate("/savegame").replaceFile(d->session.path() + ".save"); de::Writer(outFile) << arch; diff --git a/doomsday/plugins/common/src/mapstatereader.cpp b/doomsday/plugins/common/src/mapstatereader.cpp index 2d59a343eb..f44bb400a8 100644 --- a/doomsday/plugins/common/src/mapstatereader.cpp +++ b/doomsday/plugins/common/src/mapstatereader.cpp @@ -738,7 +738,7 @@ void MapStateReader::read(String const &mapUriStr) game::SessionMetadata const &metadata = session().metadata(); File const &mapStateFile = pack.locate(Path("maps") / mapUriStr + "State"); - SV_OpenFile(mapStateFile); + SV_OpenFileForRead(mapStateFile); d->reader = SV_NewReader(); /*magic*/ Reader_ReadInt32(d->reader); diff --git a/doomsday/plugins/common/src/p_saveg.cpp b/doomsday/plugins/common/src/p_saveg.cpp index c207f07ac6..f4dfa069b1 100644 --- a/doomsday/plugins/common/src/p_saveg.cpp +++ b/doomsday/plugins/common/src/p_saveg.cpp @@ -55,7 +55,7 @@ SV_MapStateReader(de::game::SavedSession const &session, de::String mapUriStr) { de::PackageFolder const &pack = session.locateFile(); de::File const &mapStateFile = pack.locate(de::Path("maps") / mapUriStr + "State"); - if(!SV_OpenFile(mapStateFile)) + if(!SV_OpenFileForRead(mapStateFile)) { /// @throw de::Error The serialized map state file could not be opened for read. throw de::Error("SV_MapStateReader", "Failed to open \"" + mapStateFile.path() + "\" for read"); @@ -843,7 +843,7 @@ void SV_SaveGameClient(uint /*sessionId*/) session->replaceMetadata(metadata); de::Path path = de::String("/savegame") / "client" / session->path(); - if(!SV_OpenFile_LZSS(path)) + if(!SV_OpenFileForWrite(path)) { App_Log(DE2_RES_WARNING, "SV_SaveGameClient: Failed opening \"%s\" for writing", path.toString().toLatin1().constData()); @@ -877,7 +877,7 @@ void SV_SaveGameClient(uint /*sessionId*/) MapStateWriter(thingArchive).write(writer); /// @todo No consistency bytes in client saves? - SV_CloseFile_LZSS(); + SV_CloseFile(); Writer_Delete(writer); delete session; #else @@ -905,7 +905,7 @@ void SV_LoadGameClient(uint /*sessionId*/) session->replaceMetadata(metadata); de::Path path = de::String("/savegame") / "client" / session->path(); - if(!SV_OpenFile(path)) + if(!SV_OpenFileForRead(path)) { delete session; App_Log(DE2_RES_WARNING, "SV_LoadGameClient: Failed opening \"%s\" for reading", diff --git a/doomsday/plugins/common/src/p_saveio.cpp b/doomsday/plugins/common/src/p_saveio.cpp index ecc1d20637..f08ea3782a 100644 --- a/doomsday/plugins/common/src/p_saveio.cpp +++ b/doomsday/plugins/common/src/p_saveio.cpp @@ -106,15 +106,23 @@ de::Reader &SV_RawReader() void SV_CloseFile() { delete reader; reader = 0; + delete writer; writer = 0; } -bool SV_OpenFile(de::File const &file) +bool SV_OpenFileForRead(de::File const &file) { SV_CloseFile(); reader = new de::Reader(file); return true; } +bool SV_OpenFileForWrite(de::File &file) +{ + SV_CloseFile(); + writer = new de::Writer(file); + return true; +} + #if 0 bool SV_OpenFile_LZSS(de::Path path) { @@ -134,46 +142,7 @@ void SV_CloseFile_LZSS() savefile = 0; } } -#endif - -void SV_Write(void const *data, int len) -{ - //lzWrite((void *)data, len, savefile); -} - -void SV_WriteByte(byte val) -{ - //lzPutC(val, savefile); -} -#if __JHEXEN__ -void SV_WriteShort(unsigned short val) -#else -void SV_WriteShort(short val) -#endif -{ - //lzPutW(val, savefile); -} - -#if __JHEXEN__ -void SV_WriteLong(unsigned int val) -#else -void SV_WriteLong(long val) -#endif -{ - //lzPutL(val, savefile); -} - -void SV_WriteFloat(float val) -{ - DENG2_ASSERT(sizeof(val) == 4); - - int32_t temp = 0; - std::memcpy(&temp, &val, 4); - //lzPutL(temp, savefile); -} - -#if 0 void SV_AssertSegment(int segmentId) { #if __JHEXEN__ @@ -190,7 +159,6 @@ void SV_AssertSegment(int segmentId) DENG_UNUSED(segmentId); #endif } -#endif void SV_BeginSegment(int segType) { @@ -206,7 +174,6 @@ void SV_EndSegment() SV_BeginSegment(ASEG_END); } -#if 0 void SV_WriteSessionMetadata(de::game::SessionMetadata const &metadata, Writer *writer) { DENG2_ASSERT(writer != 0); @@ -242,7 +209,6 @@ void SV_WriteSessionMetadata(de::game::SessionMetadata const &metadata, Writer * Writer_WriteInt32(writer, metadata["sessionId"].value().asNumber()); } -#endif void SV_WriteConsistencyBytes() { @@ -251,7 +217,7 @@ void SV_WriteConsistencyBytes() #endif } -/*void SV_ReadConsistencyBytes() +void SV_ReadConsistencyBytes() { #if !__JHEXEN__ if(SV_ReadByte() != CONSISTENCY) @@ -259,36 +225,49 @@ void SV_WriteConsistencyBytes() Con_Error("Corrupt save game: Consistency test failed."); } #endif -}*/ +} +#endif -static void swi8(Writer *w, char i) +static void swi8(writer_s *w, char val) { if(!w) return; - SV_WriteByte(i); + DENG2_ASSERT(writer); + *writer << val; } -static void swi16(Writer *w, short i) +static void swi16(Writer *w, short val) { if(!w) return; - SV_WriteShort(i); + DENG2_ASSERT(writer); + *writer << val; } -static void swi32(Writer *w, int i) +static void swi32(Writer *w, int val) { if(!w) return; - SV_WriteLong(i); + DENG2_ASSERT(writer); + *writer << val; } -static void swf(Writer *w, float i) +static void swf(Writer *w, float val) { if(!w) return; - SV_WriteFloat(i); + DENG2_ASSERT(writer); + DENG2_ASSERT(sizeof(float) == 4); + + int32_t temp = 0; + std::memcpy(&temp, &val, 4); + *writer << val; } static void swd(Writer *w, char const *data, int len) { if(!w) return; - SV_Write(data, len); + DENG2_ASSERT(writer); + if(data) + { + *writer << de::Block(data, len); + } } Writer *SV_NewWriter()