X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Flevel_subset.cpp;h=19edf4772766d1d00fe400717b609b78e889b856;hb=44c614038ecdc2d1b69a6fee255d9c0ebc2a5098;hp=a35c6890d1c311bd9997b09732b3ac993f9ad8ab;hpb=163f2a127efc850055bdee49ac57a54e37896a88;p=supertux.git diff --git a/src/level_subset.cpp b/src/level_subset.cpp index a35c6890d..19edf4772 100644 --- a/src/level_subset.cpp +++ b/src/level_subset.cpp @@ -18,13 +18,21 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. +#include + +#include +#include #include -#include "setup.h" +#include +#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,12 +57,15 @@ 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) @@ -64,6 +75,8 @@ void LevelSubset::read_info_file(const std::string& info_file) 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 files; + std::set 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 user_files = FileSystem::read_directory(filename); + files.insert(user_files.begin(), user_files.end()); - for(std::vector::iterator i = files.begin(); i != files.end(); ++i) + for(std::set::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 */