Skip to content

Commit

Permalink
Refactor|libcommon: Removed need for a global ThingArchive for writin…
Browse files Browse the repository at this point in the history
…g/reading savegames

Todo: MapStateWriter should fully encapsulate it.
  • Loading branch information
danij-deng committed Feb 13, 2014
1 parent 6801b18 commit 59bbdd4
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 56 deletions.
2 changes: 1 addition & 1 deletion doomsday/plugins/common/include/mapstatereader.h
Expand Up @@ -42,7 +42,7 @@ class MapStateReader
/**
* @param saveVersion Logical saved state version number.
*/
MapStateReader(ThingArchive &thingArchive, int saveVersion);
MapStateReader(int saveVersion, int thingArchiveSize = 0);

/**
* Deserialize the saved map state using the specified @a reader.
Expand Down
5 changes: 1 addition & 4 deletions doomsday/plugins/common/include/thingarchive.h
Expand Up @@ -37,10 +37,9 @@ class ThingArchive
#endif

public:
ThingArchive();
ThingArchive(int version = 0);

int version() const;
void setVersion(int newVersion);

bool excludePlayers() const;

Expand Down Expand Up @@ -79,6 +78,4 @@ class ThingArchive
DENG2_PRIVATE(d)
};

extern ThingArchive theThingArchive;

#endif // LIBCOMMON_P_ACTOR_H
29 changes: 13 additions & 16 deletions doomsday/plugins/common/src/gamestatereader.cpp
Expand Up @@ -68,7 +68,7 @@ DENG2_PIMPL(GameStateReader)
#endif
}

void readMap()
void readMap(int thingArchiveSize)
{
#if __JHEXEN__ // The map state is actually read from a separate file.
// Close the game state file.
Expand All @@ -79,20 +79,16 @@ DENG2_PIMPL(GameStateReader)
SV_OpenMapSaveFile(saveSlots->composeSavePathForSlot(BASE_SLOT, gameMap + 1));
#endif

MapStateReader(theThingArchive, saveInfo->version()).read(reader);

#if __JHEXEN__
Z_Free(saveBuffer);
#endif

MapStateReader(saveInfo->version(), thingArchiveSize).read(reader);
#if !__JHEXEN__
SV_ReadConsistencyBytes();
SV_CloseFile();
#endif

theThingArchive.clear();
#if __JHEXEN__
Z_Free(saveBuffer);
SV_ClearTargetPlayers();
#else
SV_CloseFile();
#endif
}

Expand Down Expand Up @@ -241,13 +237,13 @@ bool GameStateReader::recognize(SaveInfo &info, Str const *path) // static

#if __JHEXEN__
/// @todo Do not buffer the whole file.
byte *saveBuffer;
size_t fileSize = M_ReadFile(Str_Text(path), (char **) &saveBuffer);
byte *readBuffer;
size_t fileSize = M_ReadFile(Str_Text(path), (char **) &readBuffer);
if(!fileSize) return false;

// Set the save pointer.
SV_HxSavePtr()->b = saveBuffer;
SV_HxSetSaveEndPtr(saveBuffer + fileSize);
SV_HxSavePtr()->b = readBuffer;
SV_HxSetSaveEndPtr(readBuffer + fileSize);
#else
if(!SV_OpenFile(path, "rp")) return false;
#endif
Expand All @@ -257,7 +253,7 @@ bool GameStateReader::recognize(SaveInfo &info, Str const *path) // static
Reader_Delete(reader);

#if __JHEXEN__
Z_Free(saveBuffer);
Z_Free(readBuffer);
#else
SV_CloseFile();
#endif
Expand Down Expand Up @@ -319,12 +315,13 @@ void GameStateReader::read(SaveInfo &info, Str const *path)
mapTime = d->saveInfo->mapTime();
#endif

int thingArchiveSize = 0;
#if !__JHEXEN__
theThingArchive.initForLoad(d->saveInfo->version() >= 5? Reader_ReadInt32(d->reader) : 1024 /* num elements */);
thingArchiveSize = (d->saveInfo->version() >= 5? Reader_ReadInt32(d->reader) : 1024);
#endif

d->readPlayers();
d->readMap();
d->readMap(thingArchiveSize);

// Notify the players that weren't in the savegame.
d->kickMissingPlayers();
Expand Down
15 changes: 8 additions & 7 deletions doomsday/plugins/common/src/gamestatewriter.cpp
Expand Up @@ -25,16 +25,19 @@
#include "mapstatewriter.h"
#include "p_saveio.h"
#include "p_saveg.h" /// @todo remove me
#include "thingarchive.h"
#include <de/String>

DENG2_PIMPL(GameStateWriter)
{
SaveInfo *saveInfo; ///< Info for the save state to be written.
ThingArchive *thingArchive;
Writer *writer;

Instance(Public *i)
: Base(i)
, saveInfo(0)
, thingArchive(0)
, writer(0)
{}

Expand Down Expand Up @@ -71,14 +74,10 @@ DENG2_PIMPL(GameStateWriter)
SV_OpenFile(saveSlots->composeSavePathForSlot(BASE_SLOT, gameMap + 1), "wp");
#endif

MapStateWriter(theThingArchive).write(writer);
MapStateWriter(*thingArchive).write(writer);

SV_WriteConsistencyBytes(); // To be absolutely sure...
SV_CloseFile();

#if !__JHEXEN___
theThingArchive.clear();
#endif
}

void writePlayers()
Expand Down Expand Up @@ -135,13 +134,15 @@ void GameStateWriter::write(SaveInfo *saveInfo, Str const *path)
d->writeWorldACScriptData();

// Set the mobj archive numbers.
theThingArchive.initForSave(false/*do not exclude players*/);
d->thingArchive = new ThingArchive;
d->thingArchive->initForSave(false/*do not exclude players*/);
#if !__JHEXEN__
Writer_WriteInt32(d->writer, theThingArchive.size());
Writer_WriteInt32(d->writer, d->thingArchive->size());
#endif

d->writePlayers();
d->writeMap();

delete d->thingArchive; d->thingArchive = 0;
Writer_Delete(d->writer); d->writer = 0;
}
28 changes: 22 additions & 6 deletions doomsday/plugins/common/src/mapstatereader.cpp
Expand Up @@ -34,6 +34,8 @@ DENG2_PIMPL(MapStateReader)
Reader *reader;
int saveVersion;
int mapVersion;
int thingArchiveSize;

ThingArchive *thingArchive;
MaterialArchive *materialArchive;
dmu_lib::SideArchive *sideArchive;
Expand All @@ -43,6 +45,7 @@ DENG2_PIMPL(MapStateReader)
, reader(0)
, saveVersion(0)
, mapVersion(0)
, thingArchiveSize(thingArchiveSize)
, thingArchive(0)
, materialArchive(0)
, sideArchive(0)
Expand Down Expand Up @@ -84,12 +87,22 @@ DENG2_PIMPL(MapStateReader)
#endif
}

int thingArchiveVersion()
{
#if !__JHEXEN__
return 0;
#else
return mapVersion >= 4? 1 : 0;
#endif
}

void beginMapSegment()
{
checkMapSegment();

#if __JHEXEN__
thingArchive->setVersion(mapVersion >= 4? 1 : 0);
thingArchive = new ThingArchive(thingArchiveVersion());
#if !__JHEXEN__
thingArchive->initForLoad(thingArchiveSize);
#endif

#if __JHEXEN__
Expand Down Expand Up @@ -119,6 +132,8 @@ DENG2_PIMPL(MapStateReader)

delete sideArchive; sideArchive = 0;
MaterialArchive_Delete(materialArchive); materialArchive = 0;

delete thingArchive; thingArchive = 0;
}

void readElements()
Expand Down Expand Up @@ -525,12 +540,13 @@ DENG2_PIMPL(MapStateReader)
}
};

MapStateReader::MapStateReader(ThingArchive &thingArchive, int saveVersion)
MapStateReader::MapStateReader(int saveVersion, int thingArchiveSize)
: d(new Instance(this))
{
d->thingArchive = &thingArchive;
d->saveVersion = saveVersion;
d->mapVersion = saveVersion; // Default: mapVersion == saveVersion
d->saveVersion = saveVersion;
d->mapVersion = saveVersion; // Default: mapVersion == saveVersion

d->thingArchiveSize = thingArchiveSize;
}

void MapStateReader::read(Reader *reader)
Expand Down
1 change: 1 addition & 0 deletions doomsday/plugins/common/src/mapstatewriter.cpp
Expand Up @@ -73,6 +73,7 @@ DENG2_PIMPL(MapStateWriter)
void endMapSegment()
{
endSegment();

MaterialArchive_Delete(materialArchive); materialArchive = 0;
}

Expand Down
20 changes: 8 additions & 12 deletions doomsday/plugins/common/src/p_saveg.cpp
Expand Up @@ -244,7 +244,7 @@ void SV_TranslateLegacyMobjFlags(mobj_t *mo, int ver)

playerheader_t *SV_GetPlayerHeader()
{
DENG_ASSERT(playerHeaderOK);
//DENG_ASSERT(playerHeaderOK);
return &playerHeader;
}

Expand Down Expand Up @@ -1096,7 +1096,8 @@ void SV_SaveGameClient(uint sessionId)

players[CONSOLEPLAYER].write(writer);

MapStateWriter(theThingArchive).write(writer);
ThingArchive thingArchive;
MapStateWriter(thingArchive).write(writer);
/// @todo No consistency bytes in client saves?

SV_CloseFile();
Expand Down Expand Up @@ -1179,7 +1180,7 @@ void SV_LoadGameClient(uint sessionId)

cpl->read(reader);

MapStateReader(theThingArchive, info->version()).read(reader);
MapStateReader(info->version()).read(reader);

SV_CloseFile();
Reader_Delete(reader);
Expand All @@ -1197,11 +1198,12 @@ void SV_HxSaveHubMap()
SV_OpenFile(saveSlots->composeSavePathForSlot(BASE_SLOT, gameMap + 1), "wp");

// Set the mobj archive numbers
theThingArchive.initForSave(true /*exclude players*/);
ThingArchive thingArchive;
thingArchive.initForSave(true/*exclude players*/);

Writer *writer = SV_NewWriter();

MapStateWriter(theThingArchive).write(writer);
MapStateWriter(thingArchive).write(writer);

// Close the output file
SV_CloseFile();
Expand All @@ -1228,21 +1230,15 @@ void SV_HxLoadHubMap()
{
SV_OpenMapSaveFile(saveSlots->composeSavePathForSlot(BASE_SLOT, gameMap + 1));

MapStateReader(theThingArchive, info->version()).read(reader);
MapStateReader(info->version()).read(reader);

#if __JHEXEN__
Z_Free(saveBuffer);
#endif
}
catch(de::Error const &er)
{
App_Log(DE2_RES_WARNING, "Error loading hub map save state:\n%s", er.asText().toLatin1().constData());
}

Reader_Delete(reader);

#if __JHEXEN__
theThingArchive.clear();
#endif
}
#endif
2 changes: 1 addition & 1 deletion doomsday/plugins/common/src/saveinfo.cpp
Expand Up @@ -94,12 +94,12 @@ SaveInfo::SaveInfo(SaveInfo const &other)
, _magic (other._magic)
, _version (other._version)
, _gameMode (other._gameMode)
, _mapUri (Uri_Dup(other._mapUri))
#if !__JHEXEN__
, _mapTime (other._mapTime)
#endif
{
Str_Copy(Str_InitStd(&_description), &other._description);
Uri_Copy(_mapUri, other._mapUri);
#if !__JHEXEN__
std::memcpy(&_players, &other._players, sizeof(_players));
#endif
Expand Down
13 changes: 4 additions & 9 deletions doomsday/plugins/common/src/thingarchive.cpp
Expand Up @@ -67,17 +67,14 @@ DENG2_PIMPL(ThingArchive)
}
};

ThingArchive::ThingArchive() : d(new Instance(this))
{}

int ThingArchive::version() const
ThingArchive::ThingArchive(int version) : d(new Instance(this))
{
return d->version;
d->version = version;
}

void ThingArchive::setVersion(int newVersion)
int ThingArchive::version() const
{
d->version = newVersion;
return d->version;
}

bool ThingArchive::excludePlayers() const
Expand Down Expand Up @@ -236,5 +233,3 @@ mobj_t *ThingArchive::mobj(SerialId serialId, void *address)

return d->things[serialId];
}

ThingArchive theThingArchive; /// @todo remove me

0 comments on commit 59bbdd4

Please sign in to comment.