oops forgot 2 files
[supertux.git] / src / level_subset.cpp
index 0a67f1d..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 "app/globals.h"
-#include "video/surface.h"
-#include "level_subset.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)
 {
@@ -60,103 +63,74 @@ void LevelSubset::create(const std::string& subset_name)
 
 void LevelSubset::read_info_file(const std::string& info_file)
 {
-  lisp_object_t* root_obj = lisp_read_from_file(info_file);
-  if (root_obj == NULL)
-    return;
-  lisp_object_t* cur = lisp_car(root_obj);
-
-  if (lisp_symbol_p(cur) && strcmp(lisp_symbol(cur), "supertux-level-subset") == 0)
-    {
-      LispReader reader(lisp_cdr(root_obj));
-
-      reader.read_string("title", title, true);
-      reader.read_string("description", description, true);
-      reader.read_string_vector("levels", levels);
-      hide_from_contribs = false;
-      reader.read_bool("hide-from-contribs", hide_from_contribs);
-    }
-  else
-    {
-      std::cout << "LevelSubset: parse error in info file: " << info_file << std::endl;
-    }
+  lisp::Parser parser;
+  std::auto_ptr<lisp::Lisp> root (parser.parse(info_file));
 
-  lisp_free(root_obj);
+  const lisp::Lisp* info = root->get_lisp("supertux-level-subset");
+  if(!info)
+    throw std::runtime_error("File is not a levelsubset file");
+
+  hide_from_contribs = false;
+
+  info->get("title", title);
+  info->get("description", description);
+  info->get_vector("levels", levels);
+  info->get("hide-from-contribs", hide_from_contribs);
 }
 
 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;
-  filename = st_dir + "/levels/" + subset + "/info";
-  if (access(filename.c_str(), R_OK) != 0)
-    {
-      filename = datadir + "/levels/" + subset + "/info";
-      if (access(filename.c_str(), R_OK) != 0)
-        std::cout << "Error: LevelSubset: couldn't find subset: " << subset << std::endl;
+  std::string infofile = subset + "/info";
+  try {
+    read_info_file(infofile);
+  } catch(std::exception& e) {
+    std::stringstream msg;
+    msg << "Couldn't parse info file '" << infofile << "': " << e.what();
+    throw std::runtime_error(msg.str());
+  }
+
+  // test is a worldmap exists
+  has_worldmap = false;
+  std::string worldmap = subset + "/worldmap.stwm";
+  if(PHYSFS_exists(worldmap.c_str())) {
+    has_worldmap = true;
+  }
+
+  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;
     }
-  
-  read_info_file(filename);
-
-  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);
 
-      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(subset+ "/" + *i);
-        }
+    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
@@ -172,10 +146,14 @@ 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
 {
   return levels.size();
 }
-
-/* EOF */