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 "script_manager.hpp"
31 #include "scripting/wrapper_util.hpp"
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;
45 hide_from_contribs = false;
53 World::load(const std::string& filename)
55 basedir = FileSystem::dirname(filename);
58 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
60 const lisp::Lisp* info = root->get_lisp("supertux-world");
62 info = root->get_lisp("supertux-level-subset");
64 throw std::runtime_error("File is not a world or levelsubset file");
66 hide_from_contribs = false;
69 info->get("title", title);
70 info->get("description", description);
71 info->get("levelset", is_levelset);
72 info->get_vector("levels", levels);
73 info->get("hide-from-contribs", hide_from_contribs);
75 // Level info file doesn't define any levels, so read the
76 // directory to see what we can find
78 std::string path = basedir + "/";
79 char** files = PHYSFS_enumerateFiles(path.c_str());
81 msg_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
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);
96 std::string filename = basedir + "/world.nut";
97 IFileStream in(filename);
99 HSQUIRRELVM vm = script_manager->create_thread();
100 Scripting::compile_and_run(vm, in, filename);
104 World::get_level_filename(unsigned int i) const
110 World::get_num_levels() const
112 return levels.size();