Skip to content

Commit

Permalink
libcommon|SaveInfo: Delineate all possible saved game session statuse…
Browse files Browse the repository at this point in the history
…s in SaveInfo
  • Loading branch information
danij-deng committed Feb 21, 2014
1 parent 325b29b commit a27f010
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 57 deletions.
14 changes: 1 addition & 13 deletions doomsday/plugins/common/include/p_savedef.h
@@ -1,4 +1,4 @@
/** @file common/p_savedef.h Common game-save state management.
/** @file p_savedef.h Common game-save state management.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2013 Daniel Swanson <danij@dengine.net>
Expand Down Expand Up @@ -67,18 +67,6 @@
# define BASE_SLOT 6
# define AUTO_SLOT 7

typedef union saveptr_u {
byte *b;
short *w;
int *l;
float *f;
} saveptr_t;

typedef struct targetplraddress_s {
void **address;
struct targetplraddress_s *next;
} targetplraddress_t;

#endif

#if !__JHEXEN__
Expand Down
30 changes: 17 additions & 13 deletions doomsday/plugins/common/include/p_saveg.h
@@ -1,4 +1,4 @@
/** @file p_saveg.h Common game-save state management.
/** @file p_saveg.h Common game-save state management.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2013 Daniel Swanson <danij@dengine.net>
Expand All @@ -22,12 +22,15 @@
#define LIBCOMMON_SAVESTATE_H

#include "common.h"
#include "p_savedef.h" /// @todo remove me
#include "saveinfo.h"

DENG_EXTERN_C int saveToRealPlayerNum[MAXPLAYERS];

#if __JHEXEN__
typedef struct targetplraddress_s {
void **address;
struct targetplraddress_s *next;
} targetplraddress_t;

DENG_EXTERN_C targetplraddress_t *targetPlayerAddrs;
#endif

Expand Down Expand Up @@ -74,10 +77,6 @@ void SV_LoadGameClient(uint gameId);
* present for logical save @a slotNumber.
*/
dd_bool SV_HxHaveMapStateForSlot(int slotNumber, uint map);

void SV_HxSaveHubMap(void);

void SV_HxLoadHubMap(void);
#endif

/**
Expand Down Expand Up @@ -120,10 +119,12 @@ void SV_ClearTargetPlayers(void);
} // extern "C"
#endif

#ifdef __cplusplus
# ifdef __cplusplus
#include "gamestatereader.h"
#include <de/Path>

class MapStateReader;
class MapStateWriter;
class SaveInfo;
class SaveSlots;

/**
Expand All @@ -147,14 +148,17 @@ void SV_DeclareGameStateReader(GameStateRecognizeFunc recognizer, GameStateReade
*/
bool SV_RecognizeGameState(SaveInfo &info);

class MapStateReader;
class MapStateWriter;

void SV_WriteLine(Line *line, MapStateWriter *msw);
void SV_ReadLine(Line *line, MapStateReader *msr);

void SV_WriteSector(Sector *sec, MapStateWriter *msw);
void SV_ReadSector(Sector *sec, MapStateReader *msr);
#endif // __cplusplus

# if __JHEXEN__
void SV_HxSaveHubMap(void);

void SV_HxLoadHubMap(void);
# endif // __JHEXEN__
# endif // __cplusplus

#endif // LIBCOMMON_SAVESTATE_H
12 changes: 10 additions & 2 deletions doomsday/plugins/common/include/p_saveio.h
Expand Up @@ -21,11 +21,10 @@
#ifndef LIBCOMMON_SAVESTATE_INPUT_OUTPUT_H
#define LIBCOMMON_SAVESTATE_INPUT_OUTPUT_H

#include "saveinfo.h"
#include "api_materialarchive.h"
#include "lzss.h"
#include "p_savedef.h"
#include <de/Path>
#include "lzss.h"

typedef enum savestatesegment_e {
ASEG_MAP_HEADER = 102, // Hexen only
Expand All @@ -44,6 +43,15 @@ typedef enum savestatesegment_e {
ASEG_WORLDSCRIPTDATA // Hexen only
} savestatesegment_t;

#if __JHEXEN__
typedef union saveptr_u {
byte *b;
short *w;
int *l;
float *f;
} saveptr_t;
#endif

void SV_InitIO();
void SV_ShutdownIO();

Expand Down
35 changes: 28 additions & 7 deletions doomsday/plugins/common/include/saveinfo.h
Expand Up @@ -20,9 +20,7 @@

#ifndef LIBCOMMON_SAVEINFO
#define LIBCOMMON_SAVEINFO
#ifdef __cplusplus

#include "doomsday.h"
#include "common.h"
#include <de/Path>
#include <de/String>
Expand All @@ -35,6 +33,13 @@
class SaveInfo
{
public:
/// Logical game session status:
enum SessionStatus {
Loadable,
Incompatible,
Unused
};

#if !__JHEXEN__
// Info data about players present (or not) in the game session.
typedef byte Players[MAXPLAYERS];
Expand All @@ -49,8 +54,17 @@ class SaveInfo

SaveInfo &operator = (SaveInfo const &other);

/**
* Determines the logical status of the saved game session.
*
* @see statusAsText()
*/
SessionStatus status() const;

/**
* Returns a textual representation of the current status of the saved game session.
*
* @see status()
*/
de::String statusAsText() const;

Expand All @@ -60,17 +74,25 @@ class SaveInfo
de::String description() const;

/**
* Determines whether the saved game session is compatibile with the current game session
* (and @em should therefore be loadable).
* Determines whether a saved game session exists. However, it may not be compatible with
* the current game session.
*
* @see gameSessionIsLoadable()
*/
bool haveGameSession() const;

/**
* Determines whether a saved game session exists and is compatibile with the current game
* session (and @em should therefore be loadable).
*/
bool isLoadable() const;
bool gameSessionIsLoadable() const;

/**
* Attempt to update the save info from the named saved game session file. If the save path
* is invalid, unreachable, or the game state is not recognized -- the save info is returned
* to a valid but non-loadable state.
*
* @see isLoadable()
* @see gameSessionIsLoadable()
*/
void updateFromFile();

Expand Down Expand Up @@ -166,5 +188,4 @@ class SaveInfo
DENG2_PRIVATE(d)
};

#endif // __cplusplus
#endif // LIBCOMMON_SAVEINFO
3 changes: 2 additions & 1 deletion doomsday/plugins/common/include/saveslots.h
Expand Up @@ -23,10 +23,11 @@
#ifdef __cplusplus

#include "common.h"
#include "saveinfo.h"
#include <de/Error>
#include <de/Path>

class SaveInfo;

/**
* Maps saved games into a finite set of "save slots".
*
Expand Down
15 changes: 10 additions & 5 deletions doomsday/plugins/common/src/p_saveg.cpp
Expand Up @@ -1108,15 +1108,20 @@ void SV_LoadGameClient(uint sessionId)
#if __JHEXEN__
void SV_HxSaveHubMap()
{
de::Path const path = SV_SavePath() / SV_SaveSlots()[BASE_SLOT].saveInfo().fileNameForMap(gameMap);
SV_OpenFile(path, "wp");
SaveInfo &saveInfo = SV_SaveSlots()[BASE_SLOT].saveInfo();
de::Path const path = SV_SavePath() / saveInfo.fileNameForMap(gameMap);

if(!SV_OpenFile(path, "wp"))
{
throw de::Error("SV_HxSaveHubMap", "Failed opening \"" + de::NativePath(path).pretty() + "\" for write");
}

Writer *writer = SV_NewWriter();

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

Writer *writer = SV_NewWriter();

MapStateWriter(thingArchive).write(writer);

// Close the output file
Expand All @@ -1133,14 +1138,14 @@ void SV_HxLoadHubMap()

Reader *reader = SV_NewReader();

// Been here before, load the previous map state.
try
{
SaveInfo &saveInfo = SV_SaveSlots()[BASE_SLOT].saveInfo();
de::Path const path = SV_SavePath() / saveInfo.fileNameForMap(gameMap);

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

Expand Down
44 changes: 31 additions & 13 deletions doomsday/plugins/common/src/saveinfo.cpp
Expand Up @@ -23,6 +23,7 @@

#include "g_common.h"
#include "p_tick.h"
#include "p_savedef.h"
#include "p_saveg.h" /// SV_RecognizeGameState, @todo remove me
#include "p_saveio.h"
#include <de/NativePath>
Expand Down Expand Up @@ -146,19 +147,19 @@ SaveInfo *SaveInfo::newWithCurrentSessionMetadata(de::String const &fileName,
return info;
}

SaveInfo *SaveInfo::fromReader(Reader *reader) // static
{
SaveInfo *info = new SaveInfo;
info->read(reader);
return info;
}

SaveInfo &SaveInfo::operator = (SaveInfo const &other)
{
d.reset(new Instance(*other.d));
return *this;
}

SaveInfo::SessionStatus SaveInfo::status() const
{
if(!haveGameSession()) return Unused;
if(!gameSessionIsLoadable()) return Incompatible;
return Loadable;
}

de::String SaveInfo::fileName() const
{
return d->fileName + de::String("." SAVEGAMEEXTENSION);
Expand Down Expand Up @@ -283,8 +284,15 @@ void SaveInfo::applyCurrentSessionMetadata()
#endif
}

bool SaveInfo::isLoadable() const
bool SaveInfo::haveGameSession() const
{
return SV_ExistingFile(SV_SavePath() / fileName());
}

bool SaveInfo::gameSessionIsLoadable() const
{
if(!haveGameSession()) return false;

// Game identity key missmatch?
GameInfo gameInfo;
DD_GameInfo(&gameInfo);
Expand Down Expand Up @@ -477,20 +485,23 @@ void SaveInfo::read(Reader *reader)

de::String SaveInfo::statusAsText() const
{
if(isLoadable()) return "Loadable";
/// @todo Delineate all possible statuses (logic in @ref SaveSlots).
return "Incompatible/Unused";
static de::String const statusTexts[] = {
"Loadable",
"Incompatible",
"Unused",
};
return statusTexts[int(status())];
}

de::String SaveInfo::description() const
{
AutoStr *currentMapUriAsText = Uri_ToString(mapUri());

return de::String(_E(b) "%1\n" _E(.)
_E(l) "IdentityKey: " _E(.)_E(i) "%2 " _E(.)
_E(l) "IdentityKey: " _E(.)_E(i) "%2 " _E(.)
_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) "Version: " _E(.)_E(i) "%5 " _E(.)
_E(l) "Session id: " _E(.)_E(i) "%6\n" _E(.)
_E(D) "Status: " _E(.) "%7")
.arg(userDescription())
Expand All @@ -511,3 +522,10 @@ void SaveInfo::setMagic(int newMagic)
{
d->magic = newMagic;
}

SaveInfo *SaveInfo::fromReader(Reader *reader) // static
{
SaveInfo *info = new SaveInfo;
info->read(reader);
return info;
}
3 changes: 2 additions & 1 deletion doomsday/plugins/common/src/saveslots.cpp
Expand Up @@ -23,6 +23,7 @@

#include "p_savedef.h"
#include "p_saveio.h"
#include "saveinfo.h"
#include <de/NativePath>
#include <de/String>
#include <vector>
Expand Down Expand Up @@ -64,7 +65,7 @@ bool SaveSlots::Slot::isUsed() const
{
if(SV_SavePath().isEmpty()) return false;
if(!hasSaveInfo()) return false;
return SV_ExistingFile(SV_SavePath() / saveInfo().fileName()) && saveInfo().isLoadable();
return saveInfo().gameSessionIsLoadable();
}

bool SaveSlots::Slot::hasSaveInfo() const
Expand Down
1 change: 0 additions & 1 deletion doomsday/plugins/doom/include/doomv9gamestatereader.h
Expand Up @@ -26,7 +26,6 @@
# error "Using jDoom headers without __JDOOM__"
#endif

#include "saveinfo.h"
#include "gamestatereader.h"

/**
Expand Down
Expand Up @@ -26,7 +26,6 @@
# error "Using jHeretic headers without __JHERETIC__"
#endif

#include "saveinfo.h"
#include "gamestatereader.h"

/**
Expand Down

0 comments on commit a27f010

Please sign in to comment.