2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "lisp/parser.hpp"
18 #include "lisp/writer.hpp"
19 #include "physfs/ifile_stream.hpp"
20 #include "scripting/serialize.hpp"
21 #include "scripting/squirrel_util.hpp"
22 #include "supertux/globals.hpp"
23 #include "supertux/screen_manager.hpp"
24 #include "supertux/player_status.hpp"
25 #include "supertux/world.hpp"
26 #include "util/file_system.hpp"
27 #include "util/reader.hpp"
28 #include "util/string_util.hpp"
29 #include "worldmap/worldmap.hpp"
31 World* World::current_ = NULL;
45 player_status.reset(new PlayerStatus());
48 hide_from_contribs = false;
49 sq_resetobject(&world_thread);
54 sq_release(scripting::global_vm, &world_thread);
60 World::set_savegame_filename(const std::string& filename)
62 this->savegame_filename = filename;
63 // make sure the savegame directory exists
64 std::string dirname = FileSystem::dirname(filename);
65 if(!PHYSFS_exists(dirname.c_str())) {
66 if(PHYSFS_mkdir(dirname.c_str())) {
67 std::ostringstream msg;
68 msg << "Couldn't create directory for savegames '"
69 << dirname << "': " <<PHYSFS_getLastError();
70 throw std::runtime_error(msg.str());
74 if(!PHYSFS_isDirectory(dirname.c_str())) {
75 std::ostringstream msg;
76 msg << "Savegame path '" << dirname << "' is not a directory";
77 throw std::runtime_error(msg.str());
82 World::load(const std::string& filename)
84 basedir = FileSystem::dirname(filename);
87 const lisp::Lisp* root = parser.parse(filename);
89 const lisp::Lisp* info = root->get_lisp("supertux-world");
91 info = root->get_lisp("supertux-level-subset");
93 throw std::runtime_error("File is not a world or levelsubset file");
95 hide_from_contribs = false;
98 info->get("title", title);
99 info->get("description", description);
100 info->get("levelset", is_levelset);
101 info->get("levels", levels);
102 info->get("hide-from-contribs", hide_from_contribs);
104 // Level info file doesn't define any levels, so read the
105 // directory to see what we can find
107 std::string path = basedir;
108 char** files = PHYSFS_enumerateFiles(path.c_str());
110 log_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
114 for(const char* const* filename = files; *filename != 0; ++filename) {
115 if(StringUtil::has_suffix(*filename, ".stl")) {
116 levels.push_back(path + *filename);
119 PHYSFS_freeList(files);
125 using namespace scripting;
129 // create new squirrel table for persistent game state
130 HSQUIRRELVM vm = scripting::global_vm;
132 sq_pushroottable(vm);
133 sq_pushstring(vm, "state", -1);
135 if(SQ_FAILED(sq_createslot(vm, -3)))
136 throw scripting::SquirrelError(vm, "Couldn't create state table");
141 std::string filename = basedir + "/world.nut";
143 IFileStream in(filename);
145 sq_release(global_vm, &world_thread);
146 world_thread = create_thread(global_vm);
147 compile_and_run(object_to_vm(world_thread), in, filename);
148 } catch(std::exception& ) {
149 // fallback: try to load worldmap worldmap.stwm
150 using namespace worldmap;
151 g_screen_manager->push_screen(new WorldMap(basedir + "worldmap.stwm", get_player_status()));
158 using namespace scripting;
160 lisp::Writer writer(savegame_filename);
162 writer.start_list("supertux-savegame");
163 writer.write("version", 1);
165 using namespace worldmap;
166 if(WorldMap::current() != NULL) {
167 std::ostringstream title;
168 title << WorldMap::current()->get_title();
169 title << " (" << WorldMap::current()->solved_level_count()
170 << "/" << WorldMap::current()->level_count() << ")";
171 writer.write("title", title.str());
174 writer.start_list("tux");
175 player_status->write(writer);
176 writer.end_list("tux");
178 writer.start_list("state");
180 sq_pushroottable(global_vm);
181 sq_pushstring(global_vm, "state", -1);
182 if(SQ_SUCCEEDED(sq_get(global_vm, -2))) {
183 scripting::save_squirrel_table(global_vm, -1, writer);
184 sq_pop(global_vm, 1);
186 sq_pop(global_vm, 1);
187 writer.end_list("state");
189 writer.end_list("supertux-savegame");
195 using namespace scripting;
199 const lisp::Lisp* root = parser.parse(savegame_filename);
201 const lisp::Lisp* lisp = root->get_lisp("supertux-savegame");
203 throw std::runtime_error("file is not a supertux-savegame file");
206 lisp->get("version", version);
208 throw std::runtime_error("incompatible savegame version");
210 const lisp::Lisp* tux = lisp->get_lisp("tux");
212 throw std::runtime_error("No tux section in savegame");
213 player_status->read(*tux);
215 const lisp::Lisp* state = lisp->get_lisp("state");
217 throw std::runtime_error("No state section in savegame");
219 sq_pushroottable(global_vm);
220 sq_pushstring(global_vm, "state", -1);
221 if(SQ_FAILED(sq_deleteslot(global_vm, -2, SQFalse)))
222 sq_pop(global_vm, 1);
224 sq_pushstring(global_vm, "state", -1);
225 sq_newtable(global_vm);
226 load_squirrel_table(global_vm, -1, *state);
227 if(SQ_FAILED(sq_createslot(global_vm, -3)))
228 throw std::runtime_error("Couldn't create state table");
229 sq_pop(global_vm, 1);
230 } catch(std::exception& e) {
231 log_debug << "Couldn't load savegame: " << e.what() << std::endl;
236 World::get_level_filename(unsigned int i) const
242 World::get_num_levels() const
244 return levels.size();
248 World::get_basedir() const
254 World::get_title() const