Add the option for multiple quicksave slots
With more than 1 quicksave slot, slots will be created each time you quicksave until the maximum number (configured in settings) of quicksaves has been reached. After that, the oldest quicksave slot will be replaced each time you quicksave. Saves are numbered sequentially, unless the maximum number of slots is 1, in which case it is not numbered.pull/390/head
parent
2dff3aab22
commit
97924d97c7
@ -0,0 +1,73 @@
|
||||
#include "quicksavemanager.hpp"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
MWState::QuickSaveManager::QuickSaveManager(std::string &saveName, int maxSaves){
|
||||
this->saveName = saveName;
|
||||
this->maxSaves = maxSaves;
|
||||
this->oldestSlotVisited = NULL;
|
||||
this->oldestSlotId = 0;
|
||||
this->slotsVisited = 0;
|
||||
}
|
||||
|
||||
void MWState::QuickSaveManager::visitSave(const Slot *saveSlot){
|
||||
int slotId;
|
||||
if(tryExtractSlotId(saveSlot->mProfile.mDescription, slotId)){
|
||||
++slotsVisited;
|
||||
if(isOldestSave(saveSlot)){
|
||||
oldestSlotVisited = saveSlot;
|
||||
oldestSlotId = slotId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool MWState::QuickSaveManager::isOldestSave(const Slot *compare){
|
||||
if(oldestSlotVisited == NULL)
|
||||
return true;
|
||||
return (compare->mTimeStamp < oldestSlotVisited->mTimeStamp);
|
||||
}
|
||||
|
||||
bool MWState::QuickSaveManager::tryExtractSlotId(const std::string &slotName, int &extractedId){
|
||||
std::istringstream formattedExtractor = std::istringstream(slotName);
|
||||
|
||||
std::string nameToTest;
|
||||
formattedExtractor >> nameToTest;
|
||||
if(nameToTest == saveName){
|
||||
//Only try to extract the id if maxSaves > 1
|
||||
//With maxSaves == 1, we don't append the slotId to the name
|
||||
if(formattedExtractor >> extractedId)
|
||||
return (isSlotIdValid(extractedId));
|
||||
else if(maxSaves == 1)
|
||||
return formattedExtractor.eof();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool MWState::QuickSaveManager::isSlotIdValid(int slotId){
|
||||
return (slotId > 0 && slotId <= maxSaves);
|
||||
}
|
||||
|
||||
bool MWState::QuickSaveManager::shouldCreateNewSlot(){
|
||||
return (slotsVisited < maxSaves);
|
||||
}
|
||||
|
||||
const MWState::Slot *MWState::QuickSaveManager::getNextQuickSaveSlot(){
|
||||
if(shouldCreateNewSlot())
|
||||
return NULL;
|
||||
return oldestSlotVisited;
|
||||
}
|
||||
|
||||
std::string MWState::QuickSaveManager::getNextQuickSaveName(){
|
||||
std::ostringstream nameFormatter;
|
||||
nameFormatter << saveName;
|
||||
//Only print the number if there will be more than 1
|
||||
if(maxSaves > 1)
|
||||
nameFormatter << " " << calcNextSlotId();
|
||||
return nameFormatter.str();
|
||||
}
|
||||
|
||||
int MWState::QuickSaveManager::calcNextSlotId(){
|
||||
if(shouldCreateNewSlot())
|
||||
return (slotsVisited + 1);
|
||||
return oldestSlotId;
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
#ifndef GAME_STATE_QUICKSAVEMANAGER_H
|
||||
#define GAME_STATE_QUICKSAVEMANAGER_H
|
||||
|
||||
#include "character.hpp"
|
||||
#include "../mwbase/statemanager.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace MWState{
|
||||
class QuickSaveManager{
|
||||
std::string saveName;
|
||||
int maxSaves;
|
||||
int slotsVisited;
|
||||
int oldestSlotId;
|
||||
const Slot *oldestSlotVisited;
|
||||
private:
|
||||
bool tryExtractSlotId(const std::string &slotName, int &extractedIdll);
|
||||
bool isSlotIdValid(int slotId);
|
||||
bool shouldCreateNewSlot();
|
||||
bool isOldestSave(const Slot *compare);
|
||||
int calcNextSlotId();
|
||||
public:
|
||||
QuickSaveManager(std::string &saveName, int maxSaves);
|
||||
///< A utility class to manage multiple quicksave slots
|
||||
///
|
||||
/// \param saveName The name of the save ("QuickSave", "AutoSave", etc)
|
||||
/// \param maxSaves The maximum number of save slots to use before recycling old ones
|
||||
|
||||
void visitSave(const Slot *saveSlot);
|
||||
///< Visits the given \a slot \a
|
||||
|
||||
const Slot *getNextQuickSaveSlot();
|
||||
///< Get the slot that the next quicksave should use.
|
||||
///
|
||||
///\return Either the oldest quicksave slot visited, or NULL if a new slot can be made
|
||||
|
||||
std::string getNextQuickSaveName();
|
||||
///< Get the name that the next quicksave should use ("QuickSave 1", "AutoSave 10", etc)
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue