From: Ingo Ruhnke Date: Sun, 10 Aug 2014 21:25:05 +0000 (+0200) Subject: Moved savegame name generation into World class and made it automatic, moved folder... X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=4d8c468db5cda62a1abafb22db11278a80c86ded;p=supertux.git Moved savegame name generation into World class and made it automatic, moved folder creation into World::save_state() --- diff --git a/src/supertux/game_manager.cpp b/src/supertux/game_manager.cpp index d6f02a06c..ba5e72f8d 100644 --- a/src/supertux/game_manager.cpp +++ b/src/supertux/game_manager.cpp @@ -43,7 +43,7 @@ GameManager::start_level(std::unique_ptr world, int index) { m_world = std::move(world); - std::unique_ptr screen(new GameSession(m_world->get_level_filename(index), + std::unique_ptr screen(new GameSession(m_world->get_level_filename(index), m_world->get_player_status())); g_screen_manager->push_screen(std::move(screen)); } @@ -55,16 +55,8 @@ GameManager::start_game(std::unique_ptr world) MenuManager::instance().clear_menu_stack(); - std::string basename = m_world->get_basedir(); - basename = basename.substr(0, basename.length()-1); - std::string worlddirname = FileSystem::basename(basename); - std::ostringstream stream; - stream << "profile" << g_config->profile << "/" << worlddirname << ".stsg"; - std::string slotfile = stream.str(); - try { - m_world->set_savegame_filename(slotfile); m_world->run(); } catch(std::exception& e) diff --git a/src/supertux/menu/contrib_menu.cpp b/src/supertux/menu/contrib_menu.cpp index d957e1e1a..e36b1a0f6 100644 --- a/src/supertux/menu/contrib_menu.cpp +++ b/src/supertux/menu/contrib_menu.cpp @@ -50,18 +50,11 @@ ContribMenu::ContribMenu() : { try { - std::unique_ptr world = World::load(*it + "/info"); + std::unique_ptr world = World::load(*it); if (!world->hide_from_contribs()) { - { // FIXME: yuck, this should be easier - std::ostringstream stream; - std::string worlddirname = FileSystem::basename(*it); - stream << "profile" << g_config->profile << "/" << worlddirname << ".stsg"; - std::string slotfile = stream.str(); - world->set_savegame_filename(stream.str()); - world->load_state(); - } + world->load_state(); std::ostringstream title; title << world->get_title() << " (" << world->get_num_solved_levels() << "/" << world->get_num_levels() << ")"; @@ -97,7 +90,7 @@ ContribMenu::check_menu() // so it might be ok GameManager::current()->start_game(std::move(m_contrib_worlds[index])); } - else + else { MenuManager::instance().push_menu(std::unique_ptr(new ContribWorldMenu(std::move(m_contrib_worlds[index])))); } diff --git a/src/supertux/menu/main_menu.cpp b/src/supertux/menu/main_menu.cpp index 65ed61dc3..dd4195a3c 100644 --- a/src/supertux/menu/main_menu.cpp +++ b/src/supertux/menu/main_menu.cpp @@ -57,7 +57,7 @@ MainMenu::check_menu() { case MNID_STARTGAME: { - std::unique_ptr world = World::load("levels/world1/info"); + std::unique_ptr world = World::load("levels/world1"); GameManager::current()->start_game(std::move(world)); } break; diff --git a/src/supertux/world.cpp b/src/supertux/world.cpp index fea2e9ae9..b87aab55f 100644 --- a/src/supertux/world.cpp +++ b/src/supertux/world.cpp @@ -21,10 +21,11 @@ #include "physfs/ifile_streambuf.hpp" #include "scripting/serialize.hpp" #include "scripting/squirrel_util.hpp" +#include "supertux/gameconfig.hpp" #include "supertux/globals.hpp" +#include "supertux/player_status.hpp" #include "supertux/screen_fade.hpp" #include "supertux/screen_manager.hpp" -#include "supertux/player_status.hpp" #include "supertux/world.hpp" #include "util/file_system.hpp" #include "util/reader.hpp" @@ -32,10 +33,20 @@ #include "worldmap/worldmap.hpp" std::unique_ptr -World::load(const std::string& filename) +World::load(const std::string& directory) { std::unique_ptr world(new World); - world->load_(filename); + + world->load_(directory + "/info"); + + { // generate savegame filename + std::string worlddirname = FileSystem::basename(directory); + std::ostringstream stream; + stream << "profile" << g_config->profile << "/" << worlddirname << ".stsg"; + std::string slotfile = stream.str(); + world->m_savegame_filename = stream.str(); + } + return std::move(world); } @@ -60,29 +71,6 @@ World::~World() } void -World::set_savegame_filename(const std::string& filename) -{ - m_savegame_filename = filename; - - // make sure the savegame directory exists - std::string dirname = FileSystem::dirname(filename); - if(!PHYSFS_exists(dirname.c_str())) { - if(!PHYSFS_mkdir(dirname.c_str())) { - std::ostringstream msg; - msg << "Couldn't create directory for savegames '" - << dirname << "': " < load(const std::string& filename); + /** + Load a World + + @param directory Directory containing the info file, e.g. "levels/world1" + */ + static std::unique_ptr load(const std::string& directory); public: ~World(); - void set_savegame_filename(const std::string& filename); - void save_state(); void load_state();