X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Flevel_subset.cpp;h=2366f8cf18469704b09af5574421638f62da56a7;hb=b74f30dbef5d28594b4ec531ed16894bc2ec7a3e;hp=fda54170e58ce52d006b938c255398678768a9de;hpb=e68610f027ad6f35025d9aca87202ceb4501e97e;p=supertux.git diff --git a/src/level_subset.cpp b/src/level_subset.cpp index fda54170e..2366f8cf1 100644 --- a/src/level_subset.cpp +++ b/src/level_subset.cpp @@ -17,16 +17,19 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. +#include +#include +#include #include #include -#include "app/setup.h" #include "level.h" -#include "app/globals.h" +#include "resources.h" +#include "file_system.h" #include "video/surface.h" #include "level_subset.h" - -using namespace SuperTux; +#include "lisp/parser.h" +#include "lisp/lisp.h" static bool has_suffix(const std::string& data, const std::string& suffix) { @@ -58,27 +61,19 @@ 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); + lisp::Parser parser; + std::auto_ptr root (parser.parse(info_file)); - if (lisp_symbol_p(cur) && strcmp(lisp_symbol(cur), "supertux-level-subset") == 0) - { - LispReader reader(lisp_cdr(root_obj)); + const lisp::Lisp* info = root->get_lisp("supertux-level-subset"); + if(!info) + throw std::runtime_error("File is not a levelsubset 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 - { - std::cout << "LevelSubset: parse error in info file: " << info_file << std::endl; - } + hide_from_contribs = false; - lisp_free(root_obj); + 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) @@ -87,16 +82,29 @@ void LevelSubset::load(const std::string& 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; - } - - read_info_file(filename); + 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()); + } + + try { + read_info_file(filename); + } catch(std::exception& e) { + std::stringstream msg; + msg << "Couldn't parse info file '" << filename << "': " << e.what(); + throw new std::runtime_error(msg.str()); + } + + // test is a worldmap exists + has_worldmap = false; + std::string worldmap = get_resource_filename( + std::string("levels/") + subset + "/worldmap.stwm"); + if(worldmap != "") { + has_worldmap = true; + } if (levels.empty()) { // Level info file doesn't define any levels, so read the @@ -106,14 +114,15 @@ void LevelSubset::load(const std::string& subset) filename = datadir + "/levels/" + subset + "/"; files = FileSystem::read_directory(filename); - filename = st_dir + "/levels/" + subset + "/"; + filename = user_dir + "/levels/" + subset + "/"; std::set user_files = FileSystem::read_directory(filename); files.insert(user_files.begin(), user_files.end()); for(std::set::iterator i = files.begin(); i != files.end(); ++i) { if (has_suffix(*i, ".stl")) - levels.push_back(subset+ "/" + *i); + levels.push_back(get_resource_filename( + std::string("levels/" + subset+ "/" + *i))); } } } @@ -128,7 +137,7 @@ LevelSubset::save() filename = "/levels/" + name + "/"; FileSystem::fcreatedir(filename.c_str()); - filename = std::string(st_dir) + "/levels/" + name + "/info"; + filename = std::string(user_dir) + "/levels/" + name + "/info"; if(!FileSystem::fwriteable(filename.c_str())) filename = datadir + "/levels/" + name + "/info"; if(FileSystem::fwriteable(filename.c_str())) @@ -150,7 +159,7 @@ LevelSubset::save() 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," (hide-from-contribs %s)\n", hide_from_contribs ? "#t" : "#f"); fprintf( fi,")"); fclose(fi); @@ -170,10 +179,14 @@ LevelSubset::get_level_filename(unsigned int num) return levels[num]; } +std::string +LevelSubset::get_worldmap_filename() +{ + return std::string("/levels/" + name + "/worldmap.stwm"); +} + int LevelSubset::get_num_levels() const { return levels.size(); } - -/* EOF */