Skip to content

Commit

Permalink
Refactor|libcommon: Extracted high level savegame management into new…
Browse files Browse the repository at this point in the history
… C++ class 'SaveSlots'

At present the associated SaveInfos will continue to be owned by this
class. In the future these should be referenced from an index provided
by libdeng2's file system, which has none of the restrictions implicit
in the original games' slot-based mechanism.

Ultimately, SaveSlots will amount to little more than a mapping from
file paths into a finite set of 'slots'.

Todo: Cleanup
  • Loading branch information
danij-deng committed Feb 10, 2014
1 parent 4a8ff0e commit d7502c9
Show file tree
Hide file tree
Showing 5 changed files with 581 additions and 425 deletions.
2 changes: 2 additions & 0 deletions doomsday/plugins/common/common.pri
Expand Up @@ -68,6 +68,7 @@ HEADERS += \
$$common_inc/polyobjs.h \
$$common_inc/r_common.h \
$$common_inc/saveinfo.h \
$$common_inc/saveslots.h \
$$common_inc/thinkerinfo.h \
$$common_inc/x_hair.h \
$$common_inc/xgclass.h
Expand Down Expand Up @@ -131,5 +132,6 @@ SOURCES += \
$$common_src/polyobjs.cpp \
$$common_src/r_common.c \
$$common_src/saveinfo.cpp \
$$common_src/saveslots.cpp \
$$common_src/thinkerinfo.cpp \
$$common_src/x_hair.c
67 changes: 2 additions & 65 deletions doomsday/plugins/common/include/p_saveg.h
Expand Up @@ -43,58 +43,13 @@ void SV_Initialize(void);
/// Shutdown this module.
void SV_Shutdown(void);

/**
* Force an update of the cached game-save info. To be called (sparingly) at
* strategic points when an update is necessary (e.g., the game-save paths
* have changed).
*
* @note It is not necessary to call this after a game-save is made, this
* module will do so automatically.
*/
void SV_UpdateAllSaveInfo(void);
dd_bool SV_RecogniseGameState(Str const *path, SaveInfo *info);

/**
* Lookup a save slot by searching for a match on game-save name. Search is in
* ascending logical slot order 0...N (where N is the number of available save
* slots in the current game).
*
* @param name Name of the game-save to look for. Case insensitive.
*
* @return Logical slot number of the found game-save else @c -1
*/
void SV_UpdateAllSaveInfo(void);
int SV_SlotForSaveName(char const *name);

/**
* Parse @a str and determine whether it references a logical game-save slot.
*
* @param str String to be parsed. Parse is divided into three passes.
* Pass 1: Check for a known game-save name which matches this.
* Search is in ascending logical slot order 0..N (where N
* is the number of available save slots).
* Pass 2: Check for keyword identifiers.
* <auto> = The "auto save" slot.
* <last> = The last used slot.
* <quick> = The currently nominated "quick save" slot.
* Pass 3: Check for a logical save slot number.
*
* @return Save slot identifier of the slot else @c -1
*/
int SV_ParseSlotIdentifier(char const *str);

/**
* Returns @c true iff @a slot is a valid logical save slot.
*/
dd_bool SV_IsValidSlot(int slot);

/**
* Returns @c true iff @a slot is user-writable save slot (i.e., its not one
* of the special slots such as @em auto).
*/
dd_bool SV_IsUserWritableSlot(int slot);

/**
* Returns @c true iff a game-save is present for logical save @a slot.
*/
dd_bool SV_IsSlotUsed(int slot);

#if __JHEXEN__
Expand All @@ -105,27 +60,9 @@ dd_bool SV_IsSlotUsed(int slot);
dd_bool SV_HxHaveMapStateForSlot(int slot, uint map);
#endif

/**
* Returns the save info for save @a slot. Always returns SaveInfo even if
* supplied with an invalid or unused slot identifer (a null object).
*/
SaveInfo *SV_SaveInfoForSlot(int slot);

/**
* Compose the textual identifier/name for save @a slot.
*
* @return Name/identifier associated with slot @a slot.
*/
AutoStr *SV_ComposeSlotIdentifier(int slot);

/**
* Deletes all save game files associated with a slot number.
*/
void SV_ClearSlot(int slot);

/**
* Copies all the save game files from one slot to another.
*/
void SV_CopySlot(int sourceSlot, int destSlot);

/**
Expand Down
136 changes: 136 additions & 0 deletions doomsday/plugins/common/include/saveslots.h
@@ -0,0 +1,136 @@
/** @file saveslots.h Map of logical game save slots.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/

#ifndef LIBCOMMON_SAVESLOTS_H
#define LIBCOMMON_SAVESLOTS_H

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

/**
* Maps saved games into a finite set of "save slots".
*
* @todo At present the associated SaveInfos are actually owned by this class. In the future
* these should be referenced from another container which has none of the restrictions of
* the slot-based mechanism.
*
* @ingroup libcommon
*/
class SaveSlots
{
public:
SaveSlots();

void clearSaveInfo();

/// Re-build game-save info by re-scanning the save paths and populating the list.
void buildSaveInfo();

/**
* Force an update of the cached game-save info. To be called (sparingly) at strategic
* points when an update is necessary (e.g., the game-save paths have changed).
*
* @note It is not necessary to call this after a game-save is made, this module will do
* so automatically.
*/
void updateAllSaveInfo();

/**
* Composes the textual identifier/name for save @a slot.
*/
AutoStr *composeSlotIdentifier(int slot);

/**
* Parse @a str and determine whether it references a logical game-save slot.
*
* @param str String to be parsed. Parse is divided into three passes.
* Pass 1: Check for a known game-save name which matches this.
* Search is in ascending logical slot order 0..N (where N is the number
* of available save slots).
* Pass 2: Check for keyword identifiers.
* <auto> = The "auto save" slot.
* <last> = The last used slot.
* <quick> = The currently nominated "quick save" slot.
* Pass 3: Check for a logical save slot number.
*
* @return Save slot identifier of the slot else @c -1
*/
int parseSlotIdentifier(char const *str);

/**
* Lookup a save slot by searching for a match on game-save name. Search is in ascending
* logical slot order 0...N (where N is the number of available save slots).
*
* @param name Name of the game-save to look for. Case insensitive.
*
* @return Logical slot number of the found game-save else @c -1
*/
int slotForSaveName(char const *description);

/**
* Returns @c true iff a game-save is present for logical save @a slot.
*/
bool slotInUse(int slot);

/**
* Returns @c true iff @a slot is a valid logical save slot.
*/
bool isValidSlot(int slot);

/**
* Returns @c true iff @a slot is user-writable save slot (i.e., its not one of the special
* slots such as @em auto).
*/
bool isUserWritableSlot(int slot);

/**
* Returns the save info for save @a slot. Always returns SaveInfo even if supplied with an
* invalid or unused slot identifer (a null object).
*/
SaveInfo *findSaveInfoForSlot(int slot);

void replaceSaveInfo(int slot, SaveInfo *newInfo);

/**
* Deletes all save game files associated with a slot number.
*/
void clearSlot(int slot);

/**
* Copies all the save game files from one slot to another.
*/
void copySlot(int sourceSlot, int destSlot);

/**
* Compose the (possibly relative) path to save state associated with the logical save @a slot.
*
* @param slot Logical save slot identifier.
* @param map If @c >= 0 include this logical map index in the composed path.
*
* @return The composed path if reachable (else a zero-length string).
*/
AutoStr *composeGameSavePathForSlot(int slot, int map = -1);

private:
DENG2_PRIVATE(d)
};

#endif // LIBCOMMON_MAPSTATEREADER_H

0 comments on commit d7502c9

Please sign in to comment.