Moved savegame name generation into World class and made it automatic, moved folder...
authorIngo Ruhnke <grumbel@gmail.com>
Sun, 10 Aug 2014 21:25:05 +0000 (23:25 +0200)
committerIngo Ruhnke <grumbel@gmail.com>
Mon, 11 Aug 2014 22:21:35 +0000 (00:21 +0200)
src/supertux/game_manager.cpp
src/supertux/menu/contrib_menu.cpp
src/supertux/menu/main_menu.cpp
src/supertux/world.cpp
src/supertux/world.hpp

index d6f02a0..ba5e72f 100644 (file)
@@ -43,7 +43,7 @@ GameManager::start_level(std::unique_ptr<World> world, int index)
 {
   m_world = std::move(world);
 
-  std::unique_ptr<Screen> screen(new GameSession(m_world->get_level_filename(index), 
+  std::unique_ptr<Screen> 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> 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)
index d957e1e..e36b1a0 100644 (file)
@@ -50,18 +50,11 @@ ContribMenu::ContribMenu() :
   {
     try
     {
-      std::unique_ptr<World> world = World::load(*it + "/info");
+      std::unique_ptr<World> 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<Menu>(new ContribWorldMenu(std::move(m_contrib_worlds[index]))));
     }
index 65ed61d..dd4195a 100644 (file)
@@ -57,7 +57,7 @@ MainMenu::check_menu()
   {
     case MNID_STARTGAME:
       {
-        std::unique_ptr<World> world = World::load("levels/world1/info");
+        std::unique_ptr<World> world = World::load("levels/world1");
         GameManager::current()->start_game(std::move(world));
       }
       break;
index fea2e9a..b87aab5 100644 (file)
 #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"
 #include "worldmap/worldmap.hpp"
 
 std::unique_ptr<World>
-World::load(const std::string& filename)
+World::load(const std::string& directory)
 {
   std::unique_ptr<World> 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 << "': " <<PHYSFS_getLastError();
-      throw std::runtime_error(msg.str());
-    }
-  }
-
-  if(!PHYSFS_isDirectory(dirname.c_str())) {
-    std::ostringstream msg;
-    msg << "Savegame path '" << dirname << "' is not a directory";
-    throw std::runtime_error(msg.str());
-  }
-}
-
-void
 World::load_(const std::string& filename)
 {
   m_basedir = FileSystem::dirname(filename);
@@ -177,6 +165,27 @@ World::run()
 void
 World::save_state()
 {
+  { // make sure the savegame directory exists
+    std::string dirname = FileSystem::dirname(m_savegame_filename);
+    if(!PHYSFS_exists(dirname.c_str()))
+    {
+      if(!PHYSFS_mkdir(dirname.c_str()))
+      {
+        std::ostringstream msg;
+        msg << "Couldn't create directory for savegames '"
+            << dirname << "': " <<PHYSFS_getLastError();
+        throw std::runtime_error(msg.str());
+      }
+    }
+
+    if(!PHYSFS_isDirectory(dirname.c_str()))
+    {
+      std::ostringstream msg;
+      msg << "Savegame path '" << dirname << "' is not a directory";
+      throw std::runtime_error(msg.str());
+    }
+  }
+
   HSQUIRRELVM vm = scripting::global_vm;
 
   lisp::Writer writer(m_savegame_filename);
index 5d305bc..fd2171b 100644 (file)
@@ -34,13 +34,16 @@ private:
   void load_(const std::string& filename);
 
 public:
-  static std::unique_ptr<World> load(const std::string& filename);
+  /**
+      Load a World
+
+      @param directory  Directory containing the info file, e.g. "levels/world1"
+  */
+  static std::unique_ptr<World> load(const std::string& directory);
 
 public:
   ~World();
 
-  void set_savegame_filename(const std::string& filename);
-
   void save_state();
   void load_state();