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
27 #include "resources.h"
28 #include "file_system.h"
29 #include "video/surface.h"
30 #include "level_subset.h"
31 #include "lisp/parser.h"
32 #include "lisp/lisp.h"
34 static bool has_suffix(const std::string& data, const std::string& suffix)
36 if (data.length() >= suffix.length())
37 return data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0;
42 LevelSubset::LevelSubset()
47 LevelSubset::~LevelSubset()
51 void LevelSubset::create(const std::string& subset_name)
54 LevelSubset new_subset;
55 new_subset.name = subset_name;
56 new_subset.title = "Unknown Title";
57 new_subset.description = "No description so far.";
58 new_subset.hide_from_contribs = false;
62 void LevelSubset::read_info_file(const std::string& info_file)
65 std::auto_ptr<lisp::Lisp> root (parser.parse(info_file));
67 const lisp::Lisp* info = root->get_lisp("supertux-level-subset");
69 throw std::runtime_error("File is not a levelsubset file");
71 hide_from_contribs = false;
73 info->get("title", title);
74 info->get("description", description);
75 info->get_vector("levels", levels);
76 info->get("hide-from-contribs", hide_from_contribs);
79 void LevelSubset::load(const std::string& subset)
83 // Check in which directory our subset is located (ie. ~/.supertux/
84 // or SUPERTUX_DATADIR)
85 std::string filename = get_resource_filename(
86 std::string("levels/") + subset + "/info");
88 std::stringstream msg;
89 msg << "Couldn't find level subset '" << subset << "'.";
90 throw new std::runtime_error(msg.str());
94 read_info_file(filename);
95 } catch(std::exception& e) {
96 std::stringstream msg;
97 msg << "Couldn't parse info file '" << filename << "': " << e.what();
98 throw new std::runtime_error(msg.str());
101 // test is a worldmap exists
102 has_worldmap = false;
103 std::string worldmap = get_resource_filename(
104 std::string("levels/") + subset + "/worldmap.stwm");
110 { // Level info file doesn't define any levels, so read the
111 // directory to see what we can find
112 std::set<std::string> files;
114 filename = datadir + "/levels/" + subset + "/";
115 files = FileSystem::read_directory(filename);
117 filename = user_dir + "/levels/" + subset + "/";
118 std::set<std::string> user_files = FileSystem::read_directory(filename);
119 files.insert(user_files.begin(), user_files.end());
121 for(std::set<std::string>::iterator i = files.begin(); i != files.end(); ++i)
123 if (has_suffix(*i, ".stl"))
124 levels.push_back(get_resource_filename(
125 std::string("levels/" + subset+ "/" + *i)));
134 std::string filename;
136 /* Save data file: */
137 filename = "/levels/" + name + "/";
139 FileSystem::fcreatedir(filename.c_str());
140 filename = std::string(user_dir) + "/levels/" + name + "/info";
141 if(!FileSystem::fwriteable(filename.c_str()))
142 filename = datadir + "/levels/" + name + "/info";
143 if(FileSystem::fwriteable(filename.c_str()))
145 fi = fopen(filename.c_str(), "w");
148 perror(filename.c_str());
152 fprintf(fi,";; SuperTux-Level-Subset\n");
153 fprintf(fi,"(supertux-level-subset\n");
155 /* Save title info: */
156 fprintf(fi," (title \"%s\")\n", title.c_str());
158 /* Save the description: */
159 fprintf(fi," (description \"%s\")\n", description.c_str());
161 /* Save the hide from Contrbis menu boolean: */
162 fprintf(fi," (hide-from-contribs %s)\n", hide_from_contribs ? "#t" : "#f");
170 LevelSubset::add_level(const std::string& name)
172 levels.push_back(name);
176 LevelSubset::get_level_filename(unsigned int num)
178 assert(num < levels.size());
183 LevelSubset::get_worldmap_filename()
185 return std::string("/levels/" + name + "/worldmap.stwm");
189 LevelSubset::get_num_levels() const
191 return levels.size();