1 // $Id: level_subset.cpp 3118 2006-03-25 17:29:08Z sommer $
4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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
26 #include "file_system.hpp"
27 #include "lisp/parser.hpp"
28 #include "lisp/lisp.hpp"
29 #include "physfs/physfs_stream.hpp"
30 #include "scripting/script_interpreter.hpp"
33 static bool has_suffix(const std::string& data, const std::string& suffix)
35 if (data.length() >= suffix.length())
36 return data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0;
44 hide_from_contribs = false;
52 World::load(const std::string& filename)
54 basedir = FileSystem::dirname(filename);
57 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
59 const lisp::Lisp* info = root->get_lisp("supertux-world");
61 info = root->get_lisp("supertux-level-subset");
63 throw std::runtime_error("File is not a world or levelsubset file");
65 hide_from_contribs = false;
68 info->get("title", title);
69 info->get("description", description);
70 info->get("levelset", is_levelset);
71 info->get_vector("levels", levels);
72 info->get("hide-from-contribs", hide_from_contribs);
74 // Level info file doesn't define any levels, so read the
75 // directory to see what we can find
77 std::string path = basedir + "/";
78 char** files = PHYSFS_enumerateFiles(path.c_str());
80 msg_warning("Couldn't read subset dir '"
85 for(const char* const* filename = files; *filename != 0; ++filename) {
86 if(has_suffix(*filename, ".stl")) {
87 levels.push_back(path + *filename);
90 PHYSFS_freeList(files);
97 std::string filename = basedir + "/world.nut";
98 std::auto_ptr<ScriptInterpreter> interpeter (new ScriptInterpreter(basedir));
99 IFileStream in(filename);
101 interpeter->run_script(in, filename, true);
102 } catch(std::exception& e) {
103 msg_warning("Problem running world script: " << e.what());
108 World::get_level_filename(unsigned int i) const
114 World::get_num_levels() const
116 return levels.size();