renamed all .h to .hpp
[supertux.git] / src / level_subset.cpp
index 5cd4fc9..b7d590e 100644 (file)
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //  02111-1307, USA.
-
 #include <config.h>
 
 #include <sstream>
 #include <stdexcept>
 #include <assert.h>
 #include <unistd.h>
-#include "app/setup.h"
-#include "level.h"
-#include "resources.h"
-#include "app/globals.h"
-#include "video/surface.h"
-#include "level_subset.h"
-#include "lisp/parser.h"
-#include "lisp/lisp.h"
-
-using namespace SuperTux;
+#include <physfs.h>
+#include "level.hpp"
+#include "resources.hpp"
+#include "file_system.hpp"
+#include "video/surface.hpp"
+#include "level_subset.hpp"
+#include "lisp/parser.hpp"
+#include "lisp/lisp.hpp"
+#include "lisp/writer.hpp"
 
 static bool has_suffix(const std::string& data, const std::string& suffix)
 {
@@ -84,82 +82,55 @@ void LevelSubset::load(const std::string& subset)
 {
   name = subset;
   
-  // Check in which directory our subset is located (ie. ~/.supertux/
-  // or SUPERTUX_DATADIR)
-  std::string filename = get_resource_filename(
-      std::string("levels/") + subset + "/info");
-  if(filename == "") {
-    std::stringstream msg;
-    msg << "Couldn't find level subset '" << subset << "'.";
-    throw new std::runtime_error(msg.str());
-  }
+  std::string infofile = subset + "/info";
   try {
-    read_info_file(filename);
+    read_info_file(infofile);
   } catch(std::exception& e) {
     std::stringstream msg;
-    msg << "Couldn't parse info file '" << filename << "': " << e.what();
-    throw new std::runtime_error(msg.str());
+    msg << "Couldn't parse info file '" << infofile << "': " << e.what();
+    throw std::runtime_error(msg.str());
   }
 
-  if (levels.empty())
-    { // Level info file doesn't define any levels, so read the
-      // directory to see what we can find
-      std::set<std::string> files;
-  
-      filename = datadir + "/levels/" + subset + "/";
-      files = FileSystem::read_directory(filename);
+  // test is a worldmap exists
+  has_worldmap = false;
+  std::string worldmap = subset + "/worldmap.stwm";
+  if(PHYSFS_exists(worldmap.c_str())) {
+    has_worldmap = true;
+  }
 
-      filename = st_dir + "/levels/" + subset + "/";
-      std::set<std::string> user_files = FileSystem::read_directory(filename);
-      files.insert(user_files.begin(), user_files.end());
-  
-      for(std::set<std::string>::iterator i = files.begin(); i != files.end(); ++i)
-        {
-          if (has_suffix(*i, ".stl"))
-            levels.push_back(get_resource_filename(
-                  std::string("levels/" + subset+ "/" + *i)));
-        }
+  if (levels.empty()) { 
+    // Level info file doesn't define any levels, so read the
+    // directory to see what we can find
+      
+    std::string path = subset + "/";
+    char** files = PHYSFS_enumerateFiles(path.c_str());
+    if(!files) {
+      std::cerr << "Warning: Couldn't read subset dir '" 
+                << path << "'.\n";
+      return;
+    }
+
+    for(const char* const* filename = files; *filename != 0; ++filename) {
+      if(has_suffix(*filename, ".stl")) {
+        levels.push_back(path + *filename);
+      }
     }
+    PHYSFS_freeList(files);
+  }
 }
 
 void
 LevelSubset::save()
 {
-  FILE* fi;
-  std::string filename;
-
   /* Save data file: */
-  filename = "/levels/" + name + "/";
-
-  FileSystem::fcreatedir(filename.c_str());
-  filename = std::string(st_dir) + "/levels/" + name + "/info";
-  if(!FileSystem::fwriteable(filename.c_str()))
-    filename = datadir + "/levels/" + name + "/info";
-  if(FileSystem::fwriteable(filename.c_str()))
-    {
-      fi = fopen(filename.c_str(), "w");
-      if (fi == NULL)
-        {
-          perror(filename.c_str());
-        }
-
-      /* Write header: */
-      fprintf(fi,";; SuperTux-Level-Subset\n");
-      fprintf(fi,"(supertux-level-subset\n");
-
-      /* Save title info: */
-      fprintf(fi,"  (title \"%s\")\n", title.c_str());
-
-      /* Save the description: */
-      fprintf(fi,"  (description \"%s\")\n", description.c_str());
-
-      /* Save the hide from Contrbis menu boolean: */
-      fprintf(fi,"  (hide-from-contribs %s)\n", hide_from_contribs ? "#t" : "#f");
-
-      fprintf( fi,")");
-      fclose(fi);
-    }
+  std::string filename = name + "/info";
+  lisp::Writer writer(filename);
+
+  writer.start_list("supertux-level-subset");
+  writer.write_string("title", title);
+  writer.write_string("description", description);
+  writer.write_bool("hide-from-contribs", hide_from_contribs);
+  writer.end_list("supertux-level-subset");
 }
 
 void
@@ -175,6 +146,12 @@ LevelSubset::get_level_filename(unsigned int num)
   return levels[num];
 }
 
+std::string
+LevelSubset::get_worldmap_filename()
+{
+  return std::string(name + "/worldmap.stwm");
+}
+
 int
 LevelSubset::get_num_levels() const
 {