Skip to content

Commit

Permalink
libcommon|MapStateWriter: Replaced map state data Writer
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
danij-deng committed Mar 21, 2014
1 parent ca0248a commit 175cb57
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 70 deletions.
12 changes: 7 additions & 5 deletions doomsday/plugins/common/include/p_saveio.h
Expand Up @@ -21,8 +21,9 @@
#ifndef LIBCOMMON_SAVESTATE_INPUT_OUTPUT_H
#define LIBCOMMON_SAVESTATE_INPUT_OUTPUT_H

#include <de/game/SavedSession>
#include <de/Path>
//#include <de/game/SavedSession>
//#include <de/Path>
#include <de/File>
#include <de/Reader>
#include <de/Writer>
#include <de/reader.h>
Expand Down Expand Up @@ -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);
Expand All @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions doomsday/plugins/common/src/g_game.cpp
Expand Up @@ -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");
}
Expand All @@ -3019,7 +3019,7 @@ void G_DoLeaveMap()
MapStateWriter(thingArchive).write(writer);

Writer_Delete(writer);
SV_CloseFile_LZSS();
SV_CloseFile();
}
}
else // Entering new hub.
Expand Down
8 changes: 4 additions & 4 deletions doomsday/plugins/common/src/gamesessionwriter.cpp
Expand Up @@ -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");
}
Expand All @@ -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<Folder>("/savegame").replaceFile(d->session.path() + ".save");
de::Writer(outFile) << arch;
Expand Down
2 changes: 1 addition & 1 deletion doomsday/plugins/common/src/mapstatereader.cpp
Expand Up @@ -738,7 +738,7 @@ void MapStateReader::read(String const &mapUriStr)
game::SessionMetadata const &metadata = session().metadata();

File const &mapStateFile = pack.locate<File>(Path("maps") / mapUriStr + "State");
SV_OpenFile(mapStateFile);
SV_OpenFileForRead(mapStateFile);
d->reader = SV_NewReader();

/*magic*/ Reader_ReadInt32(d->reader);
Expand Down
8 changes: 4 additions & 4 deletions doomsday/plugins/common/src/p_saveg.cpp
Expand Up @@ -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::File>(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");
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
87 changes: 33 additions & 54 deletions doomsday/plugins/common/src/p_saveio.cpp
Expand Up @@ -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)
{
Expand All @@ -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__
Expand All @@ -190,7 +159,6 @@ void SV_AssertSegment(int segmentId)
DENG_UNUSED(segmentId);
#endif
}
#endif

void SV_BeginSegment(int segType)
{
Expand All @@ -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);
Expand Down Expand Up @@ -242,7 +209,6 @@ void SV_WriteSessionMetadata(de::game::SessionMetadata const &metadata, Writer *

Writer_WriteInt32(writer, metadata["sessionId"].value().asNumber());
}
#endif

void SV_WriteConsistencyBytes()
{
Expand All @@ -251,44 +217,57 @@ void SV_WriteConsistencyBytes()
#endif
}

/*void SV_ReadConsistencyBytes()
void SV_ReadConsistencyBytes()
{
#if !__JHEXEN__
if(SV_ReadByte() != CONSISTENCY)
{
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()
Expand Down

0 comments on commit 175cb57

Please sign in to comment.