fixed background drawing problems introduced with my last commit
[supertux.git] / src / level_subset.cpp
index 25785d1..19edf47 100644 (file)
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //  02111-1307, USA.
 
+#include <config.h>
+
+#include <sstream>
+#include <stdexcept>
 #include <assert.h>
-#include "setup.h"
+#include <unistd.h>
+#include "app/setup.h"
 #include "level.h"
-#include "globals.h"
-#include "screen/surface.h"
+#include "resources.h"
+#include "app/globals.h"
+#include "video/surface.h"
 #include "level_subset.h"
 
+using namespace SuperTux;
+
 static bool has_suffix(const std::string& data, const std::string& suffix)
 {
   if (data.length() >= suffix.length())
@@ -49,21 +57,26 @@ void LevelSubset::create(const std::string& subset_name)
   new_subset.name = subset_name;
   new_subset.title = "Unknown Title";
   new_subset.description = "No description so far.";
+  new_subset.hide_from_contribs = false;
   new_subset.save();
 }
 
 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);
-      reader.read_string("description", description);
+      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
     {
@@ -73,49 +86,39 @@ void LevelSubset::read_info_file(const std::string& info_file)
   lisp_free(root_obj);
 }
 
-void LevelSubset::load(const char* subset)
+void LevelSubset::load(const std::string& subset)
 {
   name = subset;
   
   // Check in which directory our subset is located (ie. ~/.supertux/
   // or SUPERTUX_DATADIR)
-  char filename[1024];
-  snprintf(filename, 1024, "%s/levels/%s/", st_dir, subset);
-  if (access(filename, R_OK) == 0)
-    {
-      directory = filename;
-    }
-  else
-    {
-      snprintf(filename, 1024, "%s/levels/%s/", datadir.c_str(), subset);
-      if (access(filename, R_OK) == 0)
-        directory = filename;
-      else
-        std::cout << "Error: LevelSubset: couldn't find subset: " << subset << std::endl;
-    }
+  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());
+  }
   
-  read_info_file(directory + "info");
+  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::vector<std::string> files;
+      std::set<std::string> files;
   
-      snprintf(filename, 1024, "%s/levels/%s/", st_dir, subset);
-      if(access(filename, R_OK) == 0)
-        {
-          files = read_directory(filename);
-        }
-      else
-        {
-          snprintf(filename, 1024, "%s/levels/%s/", datadir.c_str(), subset);
-          files = read_directory(filename);
-        }
+      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::vector<std::string>::iterator i = files.begin(); i != files.end(); ++i)
+      for(std::set<std::string>::iterator i = files.begin(); i != files.end(); ++i)
         {
           if (has_suffix(*i, ".stl"))
-            levels.push_back(*i);
+            levels.push_back(get_resource_filename(
+                  std::string("levels/" + subset+ "/" + *i)));
         }
     }
 }
@@ -129,11 +132,11 @@ LevelSubset::save()
   /* Save data file: */
   filename = "/levels/" + name + "/";
 
-  fcreatedir(filename.c_str());
+  FileSystem::fcreatedir(filename.c_str());
   filename = std::string(st_dir) + "/levels/" + name + "/info";
-  if(!fwriteable(filename.c_str()))
+  if(!FileSystem::fwriteable(filename.c_str()))
     filename = datadir + "/levels/" + name + "/info";
-  if(fwriteable(filename.c_str()))
+  if(FileSystem::fwriteable(filename.c_str()))
     {
       fi = fopen(filename.c_str(), "w");
       if (fi == NULL)
@@ -151,6 +154,9 @@ LevelSubset::save()
       /* 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);
     }
@@ -166,8 +172,7 @@ std::string
 LevelSubset::get_level_filename(unsigned int num)
 {
   assert(num < levels.size());
-
-  return directory + levels[num];
+  return levels[num];
 }
 
 int
@@ -175,5 +180,3 @@ LevelSubset::get_num_levels() const
 {
   return levels.size();
 }
-
-/* EOF */