4 // Copyright (C) 2004 SuperTux Development Team, see AUTHORS for details
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 #include "resources.h"
29 #include "file_system.h"
30 #include "video/surface.h"
31 #include "level_subset.h"
32 #include "lisp/parser.h"
33 #include "lisp/lisp.h"
34 #include "lisp/writer.h"
36 static bool has_suffix(const std::string& data, const std::string& suffix)
38 if (data.length() >= suffix.length())
39 return data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0;
44 LevelSubset::LevelSubset()
49 LevelSubset::~LevelSubset()
53 void LevelSubset::create(const std::string& subset_name)
56 LevelSubset new_subset;
57 new_subset.name = subset_name;
58 new_subset.title = "Unknown Title";
59 new_subset.description = "No description so far.";
60 new_subset.hide_from_contribs = false;
64 void LevelSubset::read_info_file(const std::string& info_file)
67 std::auto_ptr<lisp::Lisp> root (parser.parse(info_file));
69 const lisp::Lisp* info = root->get_lisp("supertux-level-subset");
71 throw std::runtime_error("File is not a levelsubset file");
73 hide_from_contribs = false;
75 info->get("title", title);
76 info->get("description", description);
77 info->get_vector("levels", levels);
78 info->get("hide-from-contribs", hide_from_contribs);
81 void LevelSubset::load(const std::string& subset)
85 std::string infofile = subset + "/info";
87 read_info_file(infofile);
88 } catch(std::exception& e) {
89 std::stringstream msg;
90 msg << "Couldn't parse info file '" << infofile << "': " << e.what();
91 throw std::runtime_error(msg.str());
94 // test is a worldmap exists
96 std::string worldmap = subset + "/worldmap.stwm";
97 if(PHYSFS_exists(worldmap.c_str())) {
101 if (levels.empty()) {
102 // Level info file doesn't define any levels, so read the
103 // directory to see what we can find
105 std::string path = subset + "/";
106 char** files = PHYSFS_enumerateFiles(path.c_str());
108 std::cerr << "Warning: Couldn't read subset dir '"
113 for(const char* const* filename = files; *filename != 0; ++filename) {
114 if(has_suffix(*filename, ".stl")) {
115 levels.push_back(path + *filename);
118 PHYSFS_freeList(files);
125 /* Save data file: */
126 std::string filename = name + "/info";
127 lisp::Writer writer(filename);
129 writer.start_list("supertux-level-subset");
130 writer.write_string("title", title);
131 writer.write_string("description", description);
132 writer.write_bool("hide-from-contribs", hide_from_contribs);
133 writer.end_list("supertux-level-subset");
137 LevelSubset::add_level(const std::string& name)
139 levels.push_back(name);
143 LevelSubset::get_level_filename(unsigned int num)
145 assert(num < levels.size());
150 LevelSubset::get_worldmap_filename()
152 return std::string(name + "/worldmap.stwm");
156 LevelSubset::get_num_levels() const
158 return levels.size();