added basic save slot management and connected main menu save to save function (bypassing the save GUI for now)
parent
4c61deca8d
commit
5e64888227
@ -0,0 +1,101 @@
|
||||
|
||||
#include "character.hpp"
|
||||
|
||||
#include <ctime>
|
||||
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
bool MWState::operator< (const Slot& left, const Slot& right)
|
||||
{
|
||||
return left.mTimeStamp<right.mTimeStamp;
|
||||
}
|
||||
|
||||
|
||||
void MWState::Character::addSlot (const boost::filesystem::path& path)
|
||||
{
|
||||
Slot slot;
|
||||
slot.mPath = path;
|
||||
slot.mTimeStamp = boost::filesystem::last_write_time (path);
|
||||
|
||||
/// \todo load profile
|
||||
|
||||
mSlots.push_back (slot);
|
||||
}
|
||||
|
||||
void MWState::Character::addSlot (const ESM::SavedGame& profile)
|
||||
{
|
||||
Slot slot;
|
||||
|
||||
std::ostringstream stream;
|
||||
stream << mNext++;
|
||||
|
||||
slot.mPath = mPath / stream.str();
|
||||
slot.mProfile = profile;
|
||||
slot.mTimeStamp = std::time (0);
|
||||
|
||||
mSlots.push_back (slot);
|
||||
}
|
||||
|
||||
MWState::Character::Character (const boost::filesystem::path& saves)
|
||||
: mPath (saves), mNext (0)
|
||||
{
|
||||
if (!boost::filesystem::is_directory (mPath))
|
||||
{
|
||||
boost::filesystem::create_directories (mPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (boost::filesystem::directory_iterator iter (mPath);
|
||||
iter!=boost::filesystem::directory_iterator(); ++iter)
|
||||
{
|
||||
boost::filesystem::path slotPath = *iter;
|
||||
|
||||
try
|
||||
{
|
||||
addSlot (slotPath);
|
||||
}
|
||||
catch (...) {} // ignoring bad saved game files for now
|
||||
|
||||
std::istringstream stream (slotPath.filename().string());
|
||||
|
||||
int index = 0;
|
||||
|
||||
if ((stream >> index) && index>=mNext)
|
||||
mNext = index+1;
|
||||
}
|
||||
|
||||
std::sort (mSlots.begin(), mSlots.end());
|
||||
}
|
||||
}
|
||||
|
||||
const MWState::Slot *MWState::Character::createSlot (const ESM::SavedGame& profile)
|
||||
{
|
||||
addSlot (profile);
|
||||
|
||||
return &mSlots.back();
|
||||
}
|
||||
|
||||
const MWState::Slot *MWState::Character::updateSlot (const Slot *slot, const ESM::SavedGame& profile)
|
||||
{
|
||||
int index = slot - &mSlots[0];
|
||||
|
||||
if (index<0 || index>=static_cast<int> (mSlots.size()))
|
||||
{
|
||||
// sanity check; not entirely reliable
|
||||
throw std::logic_error ("slot not found");
|
||||
}
|
||||
|
||||
Slot newSlot = *slot;
|
||||
newSlot.mProfile = profile;
|
||||
newSlot.mTimeStamp = std::time (0);
|
||||
|
||||
mSlots.erase (mSlots.begin()+index);
|
||||
|
||||
mSlots.push_back (newSlot);
|
||||
|
||||
return &mSlots.back();
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
#ifndef GAME_STATE_CHARACTER_H
|
||||
#define GAME_STATE_CHARACTER_H
|
||||
|
||||
#include <boost/filesystem/path.hpp>
|
||||
|
||||
#include <components/esm/savedgame.hpp>
|
||||
|
||||
namespace MWState
|
||||
{
|
||||
struct Slot
|
||||
{
|
||||
boost::filesystem::path mPath;
|
||||
ESM::SavedGame mProfile;
|
||||
std::time_t mTimeStamp;
|
||||
};
|
||||
|
||||
bool operator< (const Slot& left, const Slot& right);
|
||||
|
||||
class Character
|
||||
{
|
||||
boost::filesystem::path mPath;
|
||||
std::vector<Slot> mSlots;
|
||||
int mNext;
|
||||
|
||||
void addSlot (const boost::filesystem::path& path);
|
||||
|
||||
void addSlot (const ESM::SavedGame& profile);
|
||||
|
||||
public:
|
||||
|
||||
Character (const boost::filesystem::path& saves);
|
||||
|
||||
const Slot *createSlot (const ESM::SavedGame& profile);
|
||||
///< Create new slot.
|
||||
///
|
||||
/// \attention The ownership of the slot is not transferred.
|
||||
|
||||
const Slot *updateSlot (const Slot *slot, const ESM::SavedGame& profile);
|
||||
/// \note Slot must belong to this character.
|
||||
///
|
||||
/// \attention The \æ slot pointer will be invalidated by this call.
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,57 @@
|
||||
|
||||
#include "charactermanager.hpp"
|
||||
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
MWState::CharacterManager::CharacterManager (const boost::filesystem::path& saves)
|
||||
: mPath (saves), mNext (0), mCurrent (0)
|
||||
{
|
||||
if (!boost::filesystem::is_directory (mPath))
|
||||
{
|
||||
boost::filesystem::create_directories (mPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (boost::filesystem::directory_iterator iter (mPath);
|
||||
iter!=boost::filesystem::directory_iterator(); ++iter)
|
||||
{
|
||||
boost::filesystem::path characterDir = *iter;
|
||||
|
||||
if (boost::filesystem::is_directory (characterDir))
|
||||
{
|
||||
Character character (characterDir);
|
||||
mCharacters.push_back (character);
|
||||
}
|
||||
|
||||
std::istringstream stream (characterDir.filename().string());
|
||||
|
||||
int index = 0;
|
||||
|
||||
if ((stream >> index) && index>=mNext)
|
||||
mNext = index+1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MWState::Character *MWState::CharacterManager::getCurrentCharacter()
|
||||
{
|
||||
if (!mCurrent)
|
||||
throw std::logic_error ("no character selected");
|
||||
|
||||
return mCurrent;
|
||||
}
|
||||
|
||||
void MWState::CharacterManager::createCharacter()
|
||||
{
|
||||
std::ostringstream stream;
|
||||
stream << mNext++;
|
||||
|
||||
boost::filesystem::path path = mPath / stream.str();
|
||||
|
||||
mCharacters.push_back (Character (path));
|
||||
|
||||
mCurrent = &mCharacters.back();
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
#ifndef GAME_STATE_CHARACTERMANAGER_H
|
||||
#define GAME_STATE_CHARACTERMANAGER_H
|
||||
|
||||
#include <boost/filesystem/path.hpp>
|
||||
|
||||
#include "character.hpp"
|
||||
|
||||
namespace MWState
|
||||
{
|
||||
class CharacterManager
|
||||
{
|
||||
boost::filesystem::path mPath;
|
||||
int mNext;
|
||||
std::vector<Character> mCharacters;
|
||||
Character *mCurrent;
|
||||
|
||||
private:
|
||||
|
||||
CharacterManager (const CharacterManager&);
|
||||
///< Not implemented
|
||||
|
||||
CharacterManager& operator= (const CharacterManager&);
|
||||
///< Not implemented
|
||||
|
||||
public:
|
||||
|
||||
CharacterManager (const boost::filesystem::path& saves);
|
||||
|
||||
Character *getCurrentCharacter();
|
||||
///< Must not be called, if there is no current character.
|
||||
|
||||
void createCharacter();
|
||||
///< Create new character within saved game management
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue