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/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;
64 // make sure the savegame directory exists
65 std::string dirname = FileSystem::dirname(filename);
66 if(!PHYSFS_exists(dirname.c_str())) {
67 if(PHYSFS_mkdir(dirname.c_str())) {
68 std::ostringstream msg;
69 msg << "Couldn't create directory for savegames '"
70 << dirname << "': " <<PHYSFS_getLastError();
71 throw std::runtime_error(msg.str());
75 if(!PHYSFS_isDirectory(dirname.c_str())) {
76 std::ostringstream msg;
77 msg << "Savegame path '" << dirname << "' is not a directory";
78 throw std::runtime_error(msg.str());
83 World::load(const std::string& filename)
85 basedir = FileSystem::dirname(filename);
88 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
90 const lisp::Lisp* info = root->get_lisp("supertux-world");
92 info = root->get_lisp("supertux-level-subset");
94 throw std::runtime_error("File is not a world or levelsubset file");
96 hide_from_contribs = false;
99 info->get("title", title);
100 info->get("description", description);
101 info->get("levelset", is_levelset);
102 info->get_vector("levels", levels);
103 info->get("hide-from-contribs", hide_from_contribs);
105 // Level info file doesn't define any levels, so read the
106 // directory to see what we can find
108 std::string path = basedir + "/";
109 char** files = PHYSFS_enumerateFiles(path.c_str());
111 log_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
115 for(const char* const* filename = files; *filename != 0; ++filename) {
116 if(has_suffix(*filename, ".stl")) {
117 levels.push_back(path + *filename);
120 PHYSFS_freeList(files);
128 // create new squirrel table for persisten game state
129 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
131 sq_pushroottable(vm);
132 sq_pushstring(vm, "state", -1);
134 if(SQ_FAILED(sq_createslot(vm, -3)))
135 throw Scripting::SquirrelError(vm, "Couldn't create state table");
140 std::string filename = basedir + "/world.nut";
142 IFileStream in(filename);
144 HSQUIRRELVM new_vm = ScriptManager::instance->create_thread();
145 Scripting::compile_and_run(new_vm, in, filename);
146 } catch(std::exception& e) {
147 using namespace WorldMapNS;
148 // fallback try to load worldmap
149 std::auto_ptr<WorldMap> worldmap (new WorldMap);
150 worldmap->loadmap(basedir + "worldmap.stwm");
151 main_loop->push_screen(worldmap.release());
158 lisp::Writer writer(savegame_filename);
160 writer.start_list("supertux-savegame");
161 writer.write_int("version", 1);
163 using namespace WorldMapNS;
164 if(WorldMap::current() != NULL) {
165 std::ostringstream title;
166 title << WorldMap::current()->get_title();
167 title << " (" << WorldMap::current()->solved_level_count()
168 << "/" << WorldMap::current()->level_count() << ")";
169 writer.write_string("title", title.str());
172 writer.start_list("tux");
173 player_status->write(writer);
174 writer.end_list("tux");
176 writer.start_list("state");
177 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
178 sq_pushroottable(vm);
179 sq_pushstring(vm, "state", -1);
180 if(SQ_SUCCEEDED(sq_get(vm, -2))) {
181 Scripting::save_squirrel_table(vm, -1, writer);
185 writer.end_list("state");
187 writer.end_list("supertux-savegame");
195 std::auto_ptr<lisp::Lisp> root (parser.parse(savegame_filename));
197 const lisp::Lisp* lisp = root->get_lisp("supertux-savegame");
199 throw std::runtime_error("file is not a supertux-savegame file");
202 lisp->get("version", version);
204 throw std::runtime_error("incompatible savegame version");
206 const lisp::Lisp* tux = lisp->get_lisp("tux");
208 throw std::runtime_error("No tux section in savegame");
209 player_status->read(*tux);
211 const lisp::Lisp* state = lisp->get_lisp("state");
213 throw std::runtime_error("No state section in savegame");
215 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
216 sq_pushroottable(vm);
217 sq_pushstring(vm, "state", -1);
218 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
221 sq_pushstring(vm, "state", -1);
223 Scripting::load_squirrel_table(vm, -1, state);
224 if(SQ_FAILED(sq_createslot(vm, -3)))
225 throw std::runtime_error("Couldn't create state table");
227 } catch(std::exception& e) {
228 log_debug << "Couldn't load savegame: " << e.what() << std::endl;
233 World::get_level_filename(unsigned int i) const
239 World::get_num_levels() const
241 return levels.size();
245 World::get_basedir() const