diff --git a/apps/openmw/mwbase/statemanager.hpp b/apps/openmw/mwbase/statemanager.hpp index 79ddbfa2a..48a95f029 100644 --- a/apps/openmw/mwbase/statemanager.hpp +++ b/apps/openmw/mwbase/statemanager.hpp @@ -78,8 +78,8 @@ namespace MWBase /** Used for quickload **/ virtual void quickLoad()=0; - virtual MWState::Character *getCurrentCharacter (bool create = true) = 0; - ///< \param create Create a new character, if there is no current character. + virtual MWState::Character *getCurrentCharacter () = 0; + ///< @note May return null. virtual CharacterIterator characterBegin() = 0; ///< Any call to SaveGame and getCurrentCharacter can invalidate the returned diff --git a/apps/openmw/mwgui/savegamedialog.cpp b/apps/openmw/mwgui/savegamedialog.cpp index 9de2374ad..946eac4d9 100644 --- a/apps/openmw/mwgui/savegamedialog.cpp +++ b/apps/openmw/mwgui/savegamedialog.cpp @@ -132,7 +132,7 @@ namespace MWGui if (mgr->characterBegin() == mgr->characterEnd()) return; - mCurrentCharacter = mgr->getCurrentCharacter (false); + mCurrentCharacter = mgr->getCurrentCharacter(); std::string directory = Misc::StringUtils::lowerCase (Settings::Manager::getString ("character", "Saves")); @@ -202,7 +202,7 @@ namespace MWGui if (!load) { - mCurrentCharacter = MWBase::Environment::get().getStateManager()->getCurrentCharacter (false); + mCurrentCharacter = MWBase::Environment::get().getStateManager()->getCurrentCharacter(); } center(); diff --git a/apps/openmw/mwstate/charactermanager.cpp b/apps/openmw/mwstate/charactermanager.cpp index f2d031576..90b25ab5a 100644 --- a/apps/openmw/mwstate/charactermanager.cpp +++ b/apps/openmw/mwstate/charactermanager.cpp @@ -32,11 +32,8 @@ MWState::CharacterManager::CharacterManager (const boost::filesystem::path& save } } -MWState::Character *MWState::CharacterManager::getCurrentCharacter (bool create, const std::string& name) +MWState::Character *MWState::CharacterManager::getCurrentCharacter () { - if (!mCurrent && create) - createCharacter(name); - return mCurrent; } @@ -56,7 +53,7 @@ void MWState::CharacterManager::deleteSlot(const MWState::Character *character, } } -void MWState::CharacterManager::createCharacter(const std::string& name) +MWState::Character* MWState::CharacterManager::createCharacter(const std::string& name) { std::ostringstream stream; @@ -82,8 +79,7 @@ void MWState::CharacterManager::createCharacter(const std::string& name) } mCharacters.push_back (Character (path, mGame)); - - mCurrent = &mCharacters.back(); + return &mCharacters.back(); } std::list::iterator MWState::CharacterManager::findCharacter(const MWState::Character* character) diff --git a/apps/openmw/mwstate/charactermanager.hpp b/apps/openmw/mwstate/charactermanager.hpp index 64f71af99..2daf73401 100644 --- a/apps/openmw/mwstate/charactermanager.hpp +++ b/apps/openmw/mwstate/charactermanager.hpp @@ -31,13 +31,12 @@ namespace MWState CharacterManager (const boost::filesystem::path& saves, const std::string& game); - Character *getCurrentCharacter (bool create, const std::string& name); - ///< \param create Create a new character, if there is no current character. - /// \param name The character name to use in case a new character is created. + Character *getCurrentCharacter (); + ///< @note May return null void deleteSlot(const MWState::Character *character, const MWState::Slot *slot); - void createCharacter(const std::string& name); + Character* createCharacter(const std::string& name); ///< Create new character within saved game management /// \param name Name for the character (does not need to be unique) diff --git a/apps/openmw/mwstate/statemanagerimp.cpp b/apps/openmw/mwstate/statemanagerimp.cpp index 115b47a5d..e3c8f3df5 100644 --- a/apps/openmw/mwstate/statemanagerimp.cpp +++ b/apps/openmw/mwstate/statemanagerimp.cpp @@ -109,7 +109,7 @@ void MWState::StateManager::askLoadRecent() if( !mAskLoadRecent ) { - const MWState::Character* character = getCurrentCharacter(false); + const MWState::Character* character = getCurrentCharacter(); if(!character || character->begin() == character->end())//no saves { MWBase::Environment::get().getWindowManager()->pushGuiMode (MWGui::GM_MainMenu); @@ -173,6 +173,16 @@ void MWState::StateManager::endGame() void MWState::StateManager::saveGame (const std::string& description, const Slot *slot) { + MWState::Character* character = getCurrentCharacter(); + if (!character) + { + MWWorld::ConstPtr player = MWMechanics::getPlayer(); + std::string name = player.get()->mBase->mName; + + character = mCharacterManager.createCharacter(name); + mCharacterManager.setCurrentCharacter(character); + } + try { ESM::SavedGame profile; @@ -204,9 +214,9 @@ void MWState::StateManager::saveGame (const std::string& description, const Slot writeScreenshot(profile.mScreenshot); if (!slot) - slot = getCurrentCharacter(true)->createSlot (profile); + slot = character->createSlot (profile); else - slot = getCurrentCharacter(true)->updateSlot (slot, profile); + slot = character->updateSlot (slot, profile); // Write to a memory stream first. If there is an exception during the save process, we don't want to trash the // existing save file we are overwriting. @@ -290,7 +300,10 @@ void MWState::StateManager::saveGame (const std::string& description, const Slot // If no file was written, clean up the slot if (slot && !boost::filesystem::exists(slot->mPath)) - getCurrentCharacter(true)->deleteSlot(slot); + { + character->deleteSlot(slot); + character->cleanup(); + } } } @@ -306,13 +319,16 @@ void MWState::StateManager::quickSave (std::string name) } const Slot* slot = NULL; - Character* mCurrentCharacter = getCurrentCharacter(true); //Get current character + Character* currentCharacter = getCurrentCharacter(); //Get current character //Find quicksave slot - for (Character::SlotIterator it = mCurrentCharacter->begin(); it != mCurrentCharacter->end(); ++it) + if (currentCharacter) { - if (it->mProfile.mDescription == name) - slot = &*it; + for (Character::SlotIterator it = currentCharacter->begin(); it != currentCharacter->end(); ++it) + { + if (it->mProfile.mDescription == name) + slot = &*it; + } } saveGame(name, slot); @@ -334,7 +350,7 @@ void MWState::StateManager::loadGame(const std::string& filepath) } } - MWState::Character* character = getCurrentCharacter(false); + MWState::Character* character = getCurrentCharacter(); loadGame(character, filepath); } @@ -506,7 +522,7 @@ void MWState::StateManager::loadGame (const Character *character, const std::str void MWState::StateManager::quickLoad() { - if (Character* currentCharacter = getCurrentCharacter (false)) + if (Character* currentCharacter = getCurrentCharacter ()) { if (currentCharacter->begin() == currentCharacter->end()) return; @@ -519,12 +535,9 @@ void MWState::StateManager::deleteGame(const MWState::Character *character, cons mCharacterManager.deleteSlot(character, slot); } -MWState::Character *MWState::StateManager::getCurrentCharacter (bool create) +MWState::Character *MWState::StateManager::getCurrentCharacter () { - MWWorld::ConstPtr player = MWMechanics::getPlayer(); - std::string name = player.get()->mBase->mName; - - return mCharacterManager.getCurrentCharacter (create, name); + return mCharacterManager.getCurrentCharacter(); } MWState::StateManager::CharacterIterator MWState::StateManager::characterBegin() @@ -545,7 +558,7 @@ void MWState::StateManager::update (float duration) if (mAskLoadRecent) { int iButton = MWBase::Environment::get().getWindowManager()->readPressedButton(); - MWState::Character *curCharacter = getCurrentCharacter(false); + MWState::Character *curCharacter = getCurrentCharacter(); if(iButton==0 && curCharacter) { mAskLoadRecent = false; diff --git a/apps/openmw/mwstate/statemanagerimp.hpp b/apps/openmw/mwstate/statemanagerimp.hpp index 44ee98be7..5defcdea3 100644 --- a/apps/openmw/mwstate/statemanagerimp.hpp +++ b/apps/openmw/mwstate/statemanagerimp.hpp @@ -73,8 +73,8 @@ namespace MWState virtual void loadGame (const Character *character, const std::string &filepath); ///< Load a saved game file belonging to the given character. - virtual Character *getCurrentCharacter (bool create); - ///< \param create Create a new character, if there is no current character. + virtual Character *getCurrentCharacter (); + ///< @note May return null. virtual CharacterIterator characterBegin(); ///< Any call to SaveGame and getCurrentCharacter can invalidate the returned