Skip to content

Commit

Permalink
libcommon: Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Feb 21, 2014
1 parent a331343 commit 325b29b
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 50 deletions.
3 changes: 1 addition & 2 deletions doomsday/plugins/common/include/gamestatewriter.h
Expand Up @@ -24,7 +24,6 @@
#include "common.h"
#include "saveinfo.h"
#include <de/Error>
#include <de/Path>

/**
* Native saved game state writer.
Expand All @@ -41,7 +40,7 @@ class GameStateWriter
public:
GameStateWriter();

void write(SaveInfo &info, de::Path path);
void write(SaveInfo &info);

private:
DENG2_PRIVATE(d)
Expand Down
6 changes: 3 additions & 3 deletions doomsday/plugins/common/src/gamestatereader.cpp
Expand Up @@ -114,7 +114,7 @@ DENG2_PIMPL(GameStateReader)
SV_HxReleaseSaveBuffer();

// Open the map state file.
de::Path path = SV_SavePath() / SV_SaveSlots()[BASE_SLOT].saveInfo().fileNameForMap(gameMap);
de::Path path = SV_SavePath() / saveInfo->fileNameForMap(gameMap);
if(!SV_OpenMapSaveFile(path))
{
throw FileAccessError("GameStateReader", "Failed opening \"" + de::NativePath(path).pretty() + "\"");
Expand Down Expand Up @@ -279,7 +279,7 @@ GameStateReader::~GameStateReader()

bool GameStateReader::recognize(SaveInfo &info) // static
{
de::Path path = SV_SavePath() / info.fileName();
de::Path const path = SV_SavePath() / info.fileName();

if(!SV_ExistingFile(path)) return false;
if(!SV_OpenGameSaveFile(path, false/*for reading*/)) return false;
Expand Down Expand Up @@ -327,7 +327,7 @@ IGameStateReader *GameStateReader::make() // static

void GameStateReader::read(SaveInfo &info)
{
de::Path path = SV_SavePath() / info.fileName();
de::Path const path = SV_SavePath() / info.fileName();

d->saveInfo = &info;

Expand Down
6 changes: 4 additions & 2 deletions doomsday/plugins/common/src/gamestatewriter.cpp
Expand Up @@ -89,7 +89,7 @@ DENG2_PIMPL(GameStateWriter)
SV_CloseFile();

// Open the map state file.
SV_OpenFile(SV_SavePath() / SV_SaveSlots()[BASE_SLOT].saveInfo().fileNameForMap(gameMap), "wp");
SV_OpenFile(SV_SavePath() / saveInfo->fileNameForMap(gameMap), "wp");
#endif

MapStateWriter(*thingArchive).write(writer);
Expand Down Expand Up @@ -130,8 +130,10 @@ DENG2_PIMPL(GameStateWriter)
GameStateWriter::GameStateWriter() : d(new Instance(this))
{}

void GameStateWriter::write(SaveInfo &info, de::Path path)
void GameStateWriter::write(SaveInfo &info)
{
de::Path const path = SV_SavePath() / info.fileName();

d->saveInfo = &info;

// In networked games the server tells the clients to save their games.
Expand Down
71 changes: 33 additions & 38 deletions doomsday/plugins/common/src/p_saveg.cpp
Expand Up @@ -858,38 +858,30 @@ dd_bool SV_LoadGame(int slotNumber)
int const logicalSlot = slotNumber;
#endif

if(!SV_SaveSlots().isKnownSlot(slotNumber))
{
return false;
}

if(SV_SavePath().isEmpty())
{
App_Log(DE2_RES_ERROR, "Game not loaded: path \"%s\" is unreachable",
de::NativePath(SV_SavePath()).pretty().toLatin1().constData());
de::NativePath(SV_SavePath()).pretty().toLatin1().constData());
return false;
}

#if __JHEXEN__
// Copy all needed save files to the base slot.
/// @todo Why do this BEFORE loading?? (G_NewGame() does not load the serialized map state)
/// @todo Does any caller ever attempt to load the base slot?? (Doesn't seem logical)
if(slotNumber != BASE_SLOT)
try
{
SV_SaveSlots().copySlot(slotNumber, BASE_SLOT);
}
#if __JHEXEN__
// Copy all needed save files to the base slot.
if(slotNumber != BASE_SLOT)
{
SV_SaveSlots().copySlot(slotNumber, BASE_SLOT);
}
#endif

de::Path path = SV_SavePath() / SV_SaveSlots()[logicalSlot].saveInfo().fileName();
App_Log(DE2_LOG_VERBOSE, "Attempting load save game from \"%s\"",
de::NativePath(path).pretty().toLatin1().constData());

try
{
SaveInfo &info = SV_SaveSlots()[logicalSlot].saveInfo();
SaveInfo &saveInfo = SV_SaveSlots()[logicalSlot].saveInfo();

// Attempt to recognize and load the saved game state.
gameStateReaderFor(info)->read(info);
App_Log(DE2_LOG_VERBOSE, "Attempting load save game from \"%s\"",
de::NativePath(SV_SavePath() / saveInfo.fileName()).pretty().toLatin1().constData());

gameStateReaderFor(saveInfo)->read(saveInfo);

// Make note of the last used save slot.
Con_SetInteger2("game-save-last-slot", slotNumber, SVF_WRITE_OVERRIDE);
Expand All @@ -899,7 +891,7 @@ dd_bool SV_LoadGame(int slotNumber)
catch(de::Error const &er)
{
App_Log(DE2_RES_WARNING, "Error loading save slot #%i:\n%s",
slotNumber, er.asText().toLatin1().constData());
slotNumber, er.asText().toLatin1().constData());
}

return false;
Expand All @@ -920,6 +912,7 @@ dd_bool SV_SaveGame(int slotNumber, char const *description)
DENG2_ASSERT(!"Invalid slot specified");
return false;
}

if(!description || !description[0])
{
DENG2_ASSERT(!"Empty description specified for slot");
Expand All @@ -929,22 +922,22 @@ dd_bool SV_SaveGame(int slotNumber, char const *description)
if(SV_SavePath().isEmpty())
{
App_Log(DE2_RES_WARNING, "Cannot save game: path \"%s\" is unreachable",
de::NativePath(SV_SavePath()).pretty().toLatin1().constData());
de::NativePath(SV_SavePath()).pretty().toLatin1().constData());
return false;
}

SaveInfo *info = SaveInfo::newWithCurrentSessionMetadata(SV_SaveSlots()[logicalSlot].fileName(), description);

de::Path path = SV_SavePath() / info->fileName();
App_Log(DE2_LOG_VERBOSE, "Attempting save game to \"%s\"",
de::NativePath(path).pretty().toLatin1().constData());

SaveInfo *saveInfo = SaveInfo::newWithCurrentSessionMetadata(
SV_SaveSlots()[logicalSlot].fileName(),
description);
try
{
GameStateWriter().write(*info, path);
App_Log(DE2_LOG_VERBOSE, "Attempting save game to \"%s\"",
de::NativePath(SV_SavePath() / saveInfo->fileName()).pretty().toLatin1().constData());

GameStateWriter().write(*saveInfo);

// Swap the save info.
SV_SaveSlots()[logicalSlot].replaceSaveInfo(info);
SV_SaveSlots()[logicalSlot].replaceSaveInfo(saveInfo);

#if __JHEXEN__
// Copy base slot to destination slot.
Expand All @@ -959,11 +952,11 @@ dd_bool SV_SaveGame(int slotNumber, char const *description)
catch(de::Error const &er)
{
App_Log(DE2_RES_WARNING, "Error writing to save slot #%i:\n%s",
slotNumber, er.asText().toLatin1().constData());
slotNumber, er.asText().toLatin1().constData());
}

// Discard the useless save info.
delete info;
delete saveInfo;

return false;
}
Expand Down Expand Up @@ -1115,7 +1108,8 @@ void SV_LoadGameClient(uint sessionId)
#if __JHEXEN__
void SV_HxSaveHubMap()
{
SV_OpenFile(SV_SavePath() / SV_SaveSlots()[BASE_SLOT].saveInfo().fileNameForMap(gameMap), "wp");
de::Path const path = SV_SavePath() / SV_SaveSlots()[BASE_SLOT].saveInfo().fileNameForMap(gameMap);
SV_OpenFile(path, "wp");

// Set the mobj archive numbers
ThingArchive thingArchive;
Expand All @@ -1142,21 +1136,22 @@ void SV_HxLoadHubMap()
// Been here before, load the previous map state.
try
{
SaveSlot &sslot = SV_SaveSlots()[BASE_SLOT];
de::Path path = SV_SavePath() / sslot.saveInfo().fileNameForMap(gameMap);
SaveInfo &saveInfo = SV_SaveSlots()[BASE_SLOT].saveInfo();
de::Path const path = SV_SavePath() / saveInfo.fileNameForMap(gameMap);

if(!SV_OpenMapSaveFile(path))
{
throw de::Error("SV_HxLoadHubMap", "Failed opening \"" + de::NativePath(path).pretty() + "\" for read");
}

MapStateReader(sslot.saveInfo().version()).read(reader);
MapStateReader(saveInfo.version()).read(reader);

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

Reader_Delete(reader);
Expand Down
2 changes: 1 addition & 1 deletion doomsday/plugins/common/src/saveinfo.cpp
Expand Up @@ -491,7 +491,7 @@ de::String SaveInfo::description() const
_E(l) "Current map: " _E(.)_E(i) "%3\n" _E(.)
_E(l) "Source file: " _E(.)_E(i) "%4\n" _E(.)
_E(l) "Version: " _E(.)_E(i) "%5 " _E(.)
_E(l) "Session id: " _E(.)_E(i) "%6\n" _E(.)
_E(l) "Session id: " _E(.)_E(i) "%6\n" _E(.)
_E(D) "Status: " _E(.) "%7")
.arg(userDescription())
.arg(gameIdentityKey())
Expand Down
4 changes: 2 additions & 2 deletions doomsday/plugins/doom/src/doomv9gamestatereader.cpp
Expand Up @@ -930,7 +930,7 @@ DoomV9GameStateReader::~DoomV9GameStateReader()

bool DoomV9GameStateReader::recognize(SaveInfo &info) // static
{
de::Path path = SV_SavePath() / info.fileName();
de::Path const path = SV_SavePath() / info.fileName();

if(!SV_ExistingFile(path)) return false;
if(!SV_OpenFile_Dm_v19(path)) return false;
Expand Down Expand Up @@ -962,7 +962,7 @@ IGameStateReader *DoomV9GameStateReader::make()

void DoomV9GameStateReader::read(SaveInfo &info)
{
de::Path path = SV_SavePath() / info.fileName();
de::Path const path = SV_SavePath() / info.fileName();

if(!SV_OpenFile_Dm_v19(path))
{
Expand Down
4 changes: 2 additions & 2 deletions doomsday/plugins/heretic/src/hereticv13gamestatereader.cpp
Expand Up @@ -944,7 +944,7 @@ HereticV13GameStateReader::~HereticV13GameStateReader()

bool HereticV13GameStateReader::recognize(SaveInfo &info) // static
{
de::Path path = SV_SavePath() / info.fileName();
de::Path const path = SV_SavePath() / info.fileName();

if(!SV_ExistingFile(path)) return false;
if(!SV_OpenFile_Hr_v13(path)) return false;
Expand Down Expand Up @@ -976,7 +976,7 @@ IGameStateReader *HereticV13GameStateReader::make()

void HereticV13GameStateReader::read(SaveInfo &info)
{
de::Path path = SV_SavePath() / info.fileName();
de::Path const path = SV_SavePath() / info.fileName();

if(!SV_OpenFile_Hr_v13(path))
{
Expand Down

0 comments on commit 325b29b

Please sign in to comment.