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
27 #include "file_system.hpp"
28 #include "lisp/parser.hpp"
29 #include "lisp/lisp.hpp"
30 #include "physfs/physfs_stream.hpp"
31 #include "script_manager.hpp"
32 #include "scripting/wrapper_util.hpp"
33 #include "scripting/serialize.hpp"
35 #include "worldmap.hpp"
36 #include "mainloop.hpp"
38 static bool has_suffix(const std::string& data, const std::string& suffix)
40 if (data.length() >= suffix.length())
41 return data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0;
46 World* World::current_ = NULL;
51 hide_from_contribs = false;
61 World::set_savegame_filename(const std::string& filename)
63 this->savegame_filename = filename;
67 World::load(const std::string& filename)
69 basedir = FileSystem::dirname(filename);
72 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
74 const lisp::Lisp* info = root->get_lisp("supertux-world");
76 info = root->get_lisp("supertux-level-subset");
78 throw std::runtime_error("File is not a world or levelsubset file");
80 hide_from_contribs = false;
83 info->get("title", title);
84 info->get("description", description);
85 info->get("levelset", is_levelset);
86 info->get_vector("levels", levels);
87 info->get("hide-from-contribs", hide_from_contribs);
89 // Level info file doesn't define any levels, so read the
90 // directory to see what we can find
92 std::string path = basedir + "/";
93 char** files = PHYSFS_enumerateFiles(path.c_str());
95 msg_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
99 for(const char* const* filename = files; *filename != 0; ++filename) {
100 if(has_suffix(*filename, ".stl")) {
101 levels.push_back(path + *filename);
104 PHYSFS_freeList(files);
112 // create new squirrel table for persisten game state
113 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
115 sq_pushroottable(vm);
116 sq_pushstring(vm, "state", -1);
118 if(SQ_FAILED(sq_createslot(vm, -3)))
119 throw Scripting::SquirrelError(vm, "Couldn't create state table");
124 std::string filename = basedir + "/world.nut";
126 IFileStream in(filename);
128 HSQUIRRELVM new_vm = ScriptManager::instance->create_thread();
129 Scripting::compile_and_run(new_vm, in, filename);
130 } catch(std::exception& e) {
131 using namespace WorldMapNS;
132 // fallback try to load worldmap
133 std::auto_ptr<WorldMap> worldmap (new WorldMap);
134 worldmap->loadmap(basedir + "worldmap.stwm");
135 main_loop->push_screen(worldmap.release());
142 lisp::Writer writer(savegame_filename);
144 writer.start_list("supertux-savegame");
145 writer.write_int("version", 1);
147 using namespace WorldMapNS;
148 if(WorldMap::current() != NULL) {
149 std::ostringstream title;
150 title << WorldMap::current()->get_title();
151 title << " (" << WorldMap::current()->solved_level_count()
152 << "/" << WorldMap::current()->level_count() << ")";
153 writer.write_string("title", title.str());
156 writer.start_list("tux");
157 player_status->write(writer);
158 writer.end_list("tux");
160 writer.start_list("state");
161 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
162 sq_pushroottable(vm);
163 sq_pushstring(vm, "state", -1);
164 if(SQ_SUCCEEDED(sq_get(vm, -2))) {
165 Scripting::save_squirrel_table(vm, -1, writer);
169 writer.end_list("state");
171 writer.end_list("supertux-savegame");
179 std::auto_ptr<lisp::Lisp> root (parser.parse(savegame_filename));
181 const lisp::Lisp* lisp = root->get_lisp("supertux-savegame");
183 throw std::runtime_error("file is not a supertux-savegame file");
186 lisp->get("version", version);
188 throw std::runtime_error("incompatible savegame version");
190 const lisp::Lisp* tux = lisp->get_lisp("tux");
192 throw std::runtime_error("No tux section in savegame");
193 player_status->read(*tux);
195 const lisp::Lisp* state = lisp->get_lisp("state");
197 throw std::runtime_error("No state section in savegame");
199 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
200 sq_pushroottable(vm);
201 sq_pushstring(vm, "state", -1);
202 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
205 sq_pushstring(vm, "state", -1);
207 Scripting::load_squirrel_table(vm, -1, state);
208 if(SQ_FAILED(sq_createslot(vm, -3)))
209 throw std::runtime_error("Couldn't create state table");
211 } catch(std::exception& e) {
212 msg_debug << "Couldn't load savegame: " << e.what() << std::endl;
217 World::get_level_filename(unsigned int i) const
223 World::get_num_levels() const
225 return levels.size();
229 World::get_basedir() const