Removed obsolete comment
[supertux.git] / src / supertux / world.cpp
index 05470f8..b6734cc 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 "supertux/savegame.hpp"
 #include "util/file_system.hpp"
 #include "util/reader.hpp"
 #include "util/string_util.hpp"
 #include "worldmap/worldmap.hpp"
 
-World* World::current_ = NULL;
-
-World::World() :
-  worldname(),
-  levels(),
-  basedir(),
-  savegame_filename(),
-  state_table(),
-  world_thread(),
-  title(),
-  description(),
-  player_status(),
-  hide_from_contribs(),
-  is_levelset()
+std::unique_ptr<World>
+World::load(const std::string& directory)
 {
-  player_status.reset(new PlayerStatus());
+  std::unique_ptr<World> world(new World);
+
+  world->load_(directory);
 
-  is_levelset = true;
-  hide_from_contribs = false;
-  sq_resetobject(&world_thread);
+  { // 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);
 }
 
-World::~World()
+World::World() :
+  m_basedir(),
+  m_worldmap_filename(),
+  m_savegame_filename(),
+  m_title(),
+  m_description(),
+  m_hide_from_contribs(false),
+  m_is_levelset(true)
 {
-  sq_release(scripting::global_vm, &world_thread);
-  if(current_ == this)
-    current_ = NULL;
 }
 
-void
-World::set_savegame_filename(const std::string& filename)
+World::~World()
 {
-  this->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)
+World::load_(const std::string& directory)
 {
-  basedir = FileSystem::dirname(filename);
-  worldname = basedir + "worldmap.stwm";
+  m_basedir = directory;
+  m_worldmap_filename = m_basedir + "/worldmap.stwm";
 
   lisp::Parser parser;
-  const lisp::Lisp* root = parser.parse(filename);
+  const lisp::Lisp* root = parser.parse(m_basedir + "/info");
 
   const lisp::Lisp* info = root->get_lisp("supertux-world");
   if(info == NULL)
@@ -97,247 +81,25 @@ World::load(const std::string& filename)
   if(info == NULL)
     throw std::runtime_error("File is not a world or levelsubset file");
 
-  hide_from_contribs = false;
-  is_levelset = true;
-
-  info->get("title", title);
-  info->get("description", description);
-  info->get("levelset", is_levelset);
-  info->get("hide-from-contribs", hide_from_contribs);
-
-  // Level info file doesn't define any levels, so read the
-  // directory to see what we can find
-
-  std::string path = basedir;
-  char** files = PHYSFS_enumerateFiles(path.c_str());
-  if(!files) {
-    log_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
-    return;
-  }
-
-  for(const char* const* filename = files; *filename != 0; ++filename) {
-    if(StringUtil::has_suffix(*filename, ".stl")) {
-      Level level;
-      level.fullpath = path + *filename;
-      level.name = *filename;
-      levels.push_back(level);
-    }
-  }
-  PHYSFS_freeList(files);
-
-  std::sort(levels.begin(), levels.end(),
-            [](const Level& lhs, const Level& rhs)
-            {
-              return StringUtil::numeric_less(lhs.fullpath, rhs.fullpath);
-            });
-}
-
-void
-World::run()
-{
-  current_ = this;
-
-  // create new squirrel table for persistent game state
-  HSQUIRRELVM vm = scripting::global_vm;
-
-  sq_pushroottable(vm);
-  sq_pushstring(vm, "state", -1);
-  sq_newtable(vm);
-  if(SQ_FAILED(sq_createslot(vm, -3)))
-    throw scripting::SquirrelError(vm, "Couldn't create state table");
-  sq_pop(vm, 1);
-
-  load_state();
-
-  std::string filename = basedir + "/world.nut";
-  try {
-    IFileStreambuf ins(filename);
-    std::istream in(&ins);
-
-    sq_release(scripting::global_vm, &world_thread);
-    world_thread = scripting::create_thread(scripting::global_vm);
-    scripting::compile_and_run(scripting::object_to_vm(world_thread), in, filename);
-  } catch(std::exception& ) {
-    // fallback: try to load worldmap worldmap.stwm
-    using namespace worldmap;
-    g_screen_manager->push_screen(std::unique_ptr<Screen>(new WorldMap(basedir + "worldmap.stwm", get_player_status())));
-  }
-}
-
-void
-World::save_state()
-{
-  using namespace scripting;
-
-  lisp::Writer writer(savegame_filename);
-
-  writer.start_list("supertux-savegame");
-  writer.write("version", 1);
-
-  using namespace worldmap;
-  if(WorldMap::current() != NULL) {
-    std::ostringstream title;
-    title << WorldMap::current()->get_title();
-    title << " (" << WorldMap::current()->solved_level_count()
-          << "/" << WorldMap::current()->level_count() << ")";
-    writer.write("title", title.str());
-  }
-
-  writer.start_list("tux");
-  player_status->write(writer);
-  writer.end_list("tux");
-
-  writer.start_list("state");
-
-  sq_pushroottable(global_vm);
-  sq_pushstring(global_vm, "state", -1);
-  if(SQ_SUCCEEDED(sq_get(global_vm, -2))) {
-    scripting::save_squirrel_table(global_vm, -1, writer);
-    sq_pop(global_vm, 1);
-  }
-  sq_pop(global_vm, 1);
-  writer.end_list("state");
-
-  writer.end_list("supertux-savegame");
-}
-
-void
-World::load_state()
-{
-  using namespace scripting;
-
-  if(!PHYSFS_exists(savegame_filename.c_str()))
-  {
-    log_info << savegame_filename << ": doesn't exist, not loading state" << std::endl;
-  }
-  else
-  {
-    try {
-      lisp::Parser parser;
-      const lisp::Lisp* root = parser.parse(savegame_filename);
-
-      const lisp::Lisp* lisp = root->get_lisp("supertux-savegame");
-      if(lisp == NULL)
-        throw std::runtime_error("file is not a supertux-savegame file");
-
-      int version = 1;
-      lisp->get("version", version);
-      if(version != 1)
-        throw std::runtime_error("incompatible savegame version");
-
-      const lisp::Lisp* tux = lisp->get_lisp("tux");
-      if(tux == NULL)
-        throw std::runtime_error("No tux section in savegame");
-      player_status->read(*tux);
-
-      const lisp::Lisp* state = lisp->get_lisp("state");
-      if(state == NULL)
-        throw std::runtime_error("No state section in savegame");
-
-      sq_pushroottable(global_vm);
-      sq_pushstring(global_vm, "state", -1);
-      if(SQ_FAILED(sq_deleteslot(global_vm, -2, SQFalse)))
-        sq_pop(global_vm, 1);
-
-      sq_pushstring(global_vm, "state", -1);
-      sq_newtable(global_vm);
-      load_squirrel_table(global_vm, -1, *state);
-      if(SQ_FAILED(sq_createslot(global_vm, -3)))
-        throw std::runtime_error("Couldn't create state table");
-      sq_pop(global_vm, 1);
-    } catch(std::exception& e) {
-      log_fatal << "Couldn't load savegame: " << e.what() << std::endl;
-    }
-  }
-}
-
-const std::string&
-World::get_level_filename(unsigned int i) const
-{
-  return levels[i].fullpath;
-}
-
-unsigned int
-World::get_num_levels() const
-{
-  return levels.size();
-}
-
-int
-World::get_num_solved_levels() const
-{
-  int num_solved_levels = 0;
-
-  HSQUIRRELVM vm = scripting::global_vm;
-  int oldtop = sq_gettop(vm);
-
-  sq_pushroottable(vm);
-  sq_pushstring(vm, "state", -1);
-  if(SQ_FAILED(sq_get(vm, -2)))
-  {
-    log_warning << "failed to get 'state' table" << std::endl;
-  }
-  else
-  {
-    sq_pushstring(vm, "worlds", -1);
-    if(SQ_FAILED(sq_get(vm, -2)))
-    {
-      log_warning << "failed to get 'state.worlds' table" << std::endl;
-    }
-    else
-    {
-      sq_pushstring(vm, worldname.c_str(), -1);
-      if(SQ_FAILED(sq_get(vm, -2)))
-      {
-        log_warning << "failed to get state.worlds['" << worldname << "']" << std::endl;
-      }
-      else
-      {
-        sq_pushstring(vm, "levels", -1);
-        if(SQ_FAILED(sq_get(vm, -2)))
-        {
-          log_warning << "failed to get state.worlds['" << worldname << "'].levels" << std::endl;
-        }
-        else
-        {
-          for(auto level : levels)
-          {
-            sq_pushstring(vm, level.name.c_str(), -1);
-            if(SQ_FAILED(sq_get(vm, -2)))
-            {
-              log_warning << "failed to get state.worlds['" << worldname << "'].levels['"
-                          << level.name << "']" << std::endl;
-            }
-            else
-            {
-              bool solved = scripting::read_bool(vm, "solved");
-              if (solved)
-              {
-                num_solved_levels += 1;
-              }
-              sq_pop(vm, 1);
-            }
-          }
-        }
-      }
-    }
-  }
-
-  sq_settop(vm, oldtop);
+  m_hide_from_contribs = false;
+  m_is_levelset = true;
 
-  return num_solved_levels;
+  info->get("title", m_title);
+  info->get("description", m_description);
+  info->get("levelset", m_is_levelset);
+  info->get("hide-from-contribs", m_hide_from_contribs);
 }
 
-const std::string&
+std::string
 World::get_basedir() const
 {
-  return basedir;
+  return m_basedir;
 }
 
-const std::string&
+std::string
 World::get_title() const
 {
-  return title;
+  return m_title;
 }
 
 /* EOF */