Skip to content

Commit

Permalink
libcommon|SaveSlots: Identify logical save slots with unique string i…
Browse files Browse the repository at this point in the history
…dentifiers

Each save slot is referenced with a unique string identifier rather
than a numerical index. This greatly simplifies the SaveSlots API,
facilitates a better OO design and allows for additional slots to be
added at any time.
  • Loading branch information
danij-deng committed Feb 23, 2014
1 parent ebd4e73 commit f2a632b
Show file tree
Hide file tree
Showing 17 changed files with 532 additions and 560 deletions.
39 changes: 39 additions & 0 deletions doomsday/plugins/common/include/g_common.h
Expand Up @@ -154,6 +154,8 @@ D_CMD( CCmdExitLevel );
#endif

#if __cplusplus
#include <de/String>

class GameStateReaderFactory;
class SaveSlots;

Expand All @@ -162,6 +164,43 @@ class SaveSlots;
*/
SaveSlots &G_SaveSlots();

/**
* 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 description which matches this.
* Search is in logical save slot creation order.
* 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 unique save slot identifier.
*
* @return The parsed slot id if found; otherwise a zero-length string.
*/
de::String G_SaveSlotIdFromUserInput(de::String str);

/**
* To be called to schedule a save game-save action.
*
* @param slotId Unique identifier of the save slot to use.
* @param userDescription New user description for the game-save. Can be @c NULL in which
* case the name will not change if the slot has already been used.
* If an empty string a new name will be generated automatically.
*
* @return @c true iff @a slotId is valid and saving is presently possible.
*/
bool G_SaveGame(de::String slotId, de::String *userDescription = 0);

/**
* To be called to schedule a load game-save action.
*
* @param slotId Unique identifier of the save slot to use.
*
* @return @c true iff @a slotId is in use and loading is presently possible.
*/
bool G_LoadGame(de::String slotId);

/**
* Returns the game's GameStateReaderFactory.
*/
Expand Down
5 changes: 2 additions & 3 deletions doomsday/plugins/common/include/hu_lib.h
Expand Up @@ -622,9 +622,8 @@ typedef struct mndata_edit_s {
ddstring_t oldtext; // If the current edit is canceled...
uint maxLength;
uint maxVisibleChars;
const char* emptyString; // Drawn when editfield is empty/null.
void* data1;
int data2;
char const *emptyString; // Drawn when editfield is empty/null.
void *data1;
} mndata_edit_t;

#ifdef __cplusplus
Expand Down
93 changes: 38 additions & 55 deletions doomsday/plugins/common/include/saveslots.h
Expand Up @@ -53,12 +53,23 @@ class SaveSlots
DENG2_ERROR(MissingInfoError);

public:
Slot(de::String const &fileName = "");
Slot(de::String id, bool userWritable, de::String const &fileName = "");

/**
* Returns the unique identifier/name for the save slot.
*/
de::String const &id() const;

/**
* Returns @c true iff the save slot is user-writable (i.e., not a special slot,
* such as the @em auto and @em base slots).
*/
bool isUserWritable() const;

/**
* Returns the save game file name bound to the logical save slot.
*/
de::String fileName() const;
de::String const &fileName() const;

/**
* Change the save game file name bound to the logical save slot.
Expand Down Expand Up @@ -103,10 +114,16 @@ class SaveSlots
};

public:
SaveSlots();

/**
* @param numSlots Number of logical slots.
* Add a new logical save slot.
*
* @param id Unique identifier for this slot.
* @param userWritable @c true= allow the user to write to this slot.
* @param fileName File name to bind to this slot.
*/
SaveSlots(int numSlots);
void addSlot(de::String id, bool userWritable, de::String fileName);

/**
* Returns the total number of logical save slots.
Expand All @@ -117,49 +134,21 @@ class SaveSlots
inline int size() const { return slotCount(); }

/**
* Returns @c true iff @a value is interpretable as logical slot number (in range).
*
* @see slotCount()
*/
bool isKnownSlot(int value) const;

/**
* Composes the textual, symbolic identifier/name for save @a slotNumber.
*
* @see parseSlotIdentifier()
*/
de::String slotIdentifier(int slotNumber) const;

/**
* 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 The parsed slot number if valid; otherwise @c -1
*
* @see slotIdentifier()
* Returns @c true iff @a value is interpretable as logical slot identifier.
*/
int parseSlotIdentifier(de::String str) const;
bool isKnownSlot(de::String value) const;

/// @see slot()
inline Slot &operator [] (int slotNumber) {
return slot(slotNumber);
inline Slot &operator [] (de::String slotId) {
return slot(slotId);
}

/**
* Returns the logical save slot associated with @a slotNumber.
* Returns the logical save slot associated with @a slotId.
*
* @see isKnownSlot()
*/
Slot &slot(int slotNumber) const;
Slot &slot(de::String slotId) const;

/**
* Clears save info for all logical save slots.
Expand All @@ -176,32 +165,26 @@ class SaveSlots
void updateAll();

/**
* Lookup a save slot by searching for a match on game-save description. The search is in
* ascending logical slot order 0...N (where N is the number of available save slots).
* Deletes all save game files associated with the specified save @a slotId.
*
* @param description Description of the game-save to look for (not case sensitive).
*
* @return Logical slot number of the found game-save else @c -1
* @see isKnownSlot()
*/
int findSlotWithUserSaveDescription(de::String description) const;
void clearSlot(de::String slotId);

/**
* Returns @c true iff save @a slotNumber is user-writable (i.e., not a special slot, such
* as the @em auto and @em base slots).
* Copies all the save game files from one slot to another.
*/
bool slotIsUserWritable(int slotNumber) const;
void copySlot(de::String sourceSlotId, de::String destSlotId);

/**
* Deletes all save game files associated with the specified save @a slotNumber.
* Lookup a save slot by searching for a match on game-save description. The search is in
* ascending logical slot order 0...N (where N is the number of available save slots).
*
* @see isKnownSlot()
*/
void clearSlot(int slotNumber);

/**
* Copies all the save game files from one slot to another.
* @param description Description of the game-save to look for (not case sensitive).
*
* @return Unique identifier of the found slot; otherwise a zero-length string.
*/
void copySlot(int sourceSlotNumber, int destSlotNumber);
de::String findSlotWithUserSaveDescription(de::String description) const;

/**
* Register the console commands and variables of this module.
Expand Down

0 comments on commit f2a632b

Please sign in to comment.