X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Flevel_subset.cpp;h=b7d590e30701ded2c87b6b92fe2d90b640b3ce37;hb=c3f9468882de1fd44f41ccfa7dae6d776bf306a7;hp=19edf4772766d1d00fe400717b609b78e889b856;hpb=5a542dea3c6043703683b68fcaa774f6cb0d9127;p=supertux.git diff --git a/src/level_subset.cpp b/src/level_subset.cpp index 19edf4772..b7d590e30 100644 --- a/src/level_subset.cpp +++ b/src/level_subset.cpp @@ -17,21 +17,21 @@ // 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 "resources.h" -#include "app/globals.h" -#include "video/surface.h" -#include "level_subset.h" - -using namespace SuperTux; +#include +#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) { @@ -63,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 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 = get_resource_filename( - std::string("levels/") + subset + "/info"); - if(filename == "") { + std::string infofile = subset + "/info"; + try { + read_info_file(infofile); + } catch(std::exception& e) { std::stringstream msg; - msg << "Couldn't find level subset '" << subset << "'."; - throw new std::runtime_error(msg.str()); + msg << "Couldn't parse info file '" << infofile << "': " << e.what(); + throw std::runtime_error(msg.str()); } - - 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 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 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(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 {