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/>.
19 #include "lisp/parser.hpp"
20 #include "lisp/writer.hpp"
21 #include "physfs/ifile_streambuf.hpp"
22 #include "scripting/serialize.hpp"
23 #include "scripting/squirrel_util.hpp"
24 #include "supertux/globals.hpp"
25 #include "supertux/screen_fade.hpp"
26 #include "supertux/screen_manager.hpp"
27 #include "supertux/player_status.hpp"
28 #include "supertux/world.hpp"
29 #include "util/file_system.hpp"
30 #include "util/reader.hpp"
31 #include "util/string_util.hpp"
32 #include "worldmap/worldmap.hpp"
34 World* World::current_ = NULL;
49 player_status.reset(new PlayerStatus());
52 hide_from_contribs = false;
53 sq_resetobject(&world_thread);
58 sq_release(scripting::global_vm, &world_thread);
64 World::set_savegame_filename(const std::string& filename)
66 this->savegame_filename = filename;
67 // make sure the savegame directory exists
68 std::string dirname = FileSystem::dirname(filename);
69 if(!PHYSFS_exists(dirname.c_str())) {
70 if(!PHYSFS_mkdir(dirname.c_str())) {
71 std::ostringstream msg;
72 msg << "Couldn't create directory for savegames '"
73 << dirname << "': " <<PHYSFS_getLastError();
74 throw std::runtime_error(msg.str());
78 if(!PHYSFS_isDirectory(dirname.c_str())) {
79 std::ostringstream msg;
80 msg << "Savegame path '" << dirname << "' is not a directory";
81 throw std::runtime_error(msg.str());
86 World::load(const std::string& filename)
88 basedir = FileSystem::dirname(filename);
89 worldname = basedir + "worldmap.stwm";
92 const lisp::Lisp* root = parser.parse(filename);
94 const lisp::Lisp* info = root->get_lisp("supertux-world");
96 info = root->get_lisp("supertux-level-subset");
98 throw std::runtime_error("File is not a world or levelsubset file");
100 hide_from_contribs = false;
103 info->get("title", title);
104 info->get("description", description);
105 info->get("levelset", is_levelset);
106 info->get("hide-from-contribs", hide_from_contribs);
108 // Level info file doesn't define any levels, so read the
109 // directory to see what we can find
111 std::string path = basedir;
112 char** files = PHYSFS_enumerateFiles(path.c_str());
114 log_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
118 for(const char* const* filename = files; *filename != 0; ++filename) {
119 if(StringUtil::has_suffix(*filename, ".stl")) {
121 level.fullpath = path + *filename;
122 level.name = *filename;
123 levels.push_back(level);
126 PHYSFS_freeList(files);
128 std::sort(levels.begin(), levels.end(),
129 [](const Level& lhs, const Level& rhs)
131 return StringUtil::numeric_less(lhs.fullpath, rhs.fullpath);
140 // create new squirrel table for persistent game state
141 HSQUIRRELVM vm = scripting::global_vm;
143 sq_pushroottable(vm);
144 sq_pushstring(vm, "state", -1);
146 if(SQ_FAILED(sq_createslot(vm, -3)))
147 throw scripting::SquirrelError(vm, "Couldn't create state table");
152 std::string filename = basedir + "/world.nut";
154 IFileStreambuf ins(filename);
155 std::istream in(&ins);
157 sq_release(scripting::global_vm, &world_thread);
158 world_thread = scripting::create_thread(scripting::global_vm);
159 scripting::compile_and_run(scripting::object_to_vm(world_thread), in, filename);
160 } catch(std::exception& ) {
161 // fallback: try to load worldmap worldmap.stwm
162 using namespace worldmap;
163 g_screen_manager->push_screen(std::unique_ptr<Screen>(new WorldMap(basedir + "worldmap.stwm", get_player_status())));
170 using namespace scripting;
172 lisp::Writer writer(savegame_filename);
174 writer.start_list("supertux-savegame");
175 writer.write("version", 1);
177 using namespace worldmap;
178 if(WorldMap::current() != NULL) {
179 std::ostringstream title;
180 title << WorldMap::current()->get_title();
181 title << " (" << WorldMap::current()->solved_level_count()
182 << "/" << WorldMap::current()->level_count() << ")";
183 writer.write("title", title.str());
186 writer.start_list("tux");
187 player_status->write(writer);
188 writer.end_list("tux");
190 writer.start_list("state");
192 sq_pushroottable(global_vm);
193 sq_pushstring(global_vm, "state", -1);
194 if(SQ_SUCCEEDED(sq_get(global_vm, -2))) {
195 scripting::save_squirrel_table(global_vm, -1, writer);
196 sq_pop(global_vm, 1);
198 sq_pop(global_vm, 1);
199 writer.end_list("state");
201 writer.end_list("supertux-savegame");
207 using namespace scripting;
209 if(!PHYSFS_exists(savegame_filename.c_str()))
211 log_info << savegame_filename << ": doesn't exist, not loading state" << std::endl;
217 const lisp::Lisp* root = parser.parse(savegame_filename);
219 const lisp::Lisp* lisp = root->get_lisp("supertux-savegame");
221 throw std::runtime_error("file is not a supertux-savegame file");
224 lisp->get("version", version);
226 throw std::runtime_error("incompatible savegame version");
228 const lisp::Lisp* tux = lisp->get_lisp("tux");
230 throw std::runtime_error("No tux section in savegame");
231 player_status->read(*tux);
233 const lisp::Lisp* state = lisp->get_lisp("state");
235 throw std::runtime_error("No state section in savegame");
237 sq_pushroottable(global_vm);
238 sq_pushstring(global_vm, "state", -1);
239 if(SQ_FAILED(sq_deleteslot(global_vm, -2, SQFalse)))
240 sq_pop(global_vm, 1);
242 sq_pushstring(global_vm, "state", -1);
243 sq_newtable(global_vm);
244 load_squirrel_table(global_vm, -1, *state);
245 if(SQ_FAILED(sq_createslot(global_vm, -3)))
246 throw std::runtime_error("Couldn't create state table");
247 sq_pop(global_vm, 1);
248 } catch(std::exception& e) {
249 log_fatal << "Couldn't load savegame: " << e.what() << std::endl;
255 World::get_level_filename(unsigned int i) const
257 return levels[i].fullpath;
261 World::get_num_levels() const
263 return levels.size();
267 World::get_num_solved_levels() const
269 int num_solved_levels = 0;
271 HSQUIRRELVM vm = scripting::global_vm;
272 int oldtop = sq_gettop(vm);
274 sq_pushroottable(vm);
275 sq_pushstring(vm, "state", -1);
276 if(SQ_FAILED(sq_get(vm, -2)))
278 log_warning << "failed to get 'state' table" << std::endl;
282 sq_pushstring(vm, "worlds", -1);
283 if(SQ_FAILED(sq_get(vm, -2)))
285 log_warning << "failed to get 'state.worlds' table" << std::endl;
289 sq_pushstring(vm, worldname.c_str(), -1);
290 if(SQ_FAILED(sq_get(vm, -2)))
292 log_warning << "failed to get state.worlds['" << worldname << "']" << std::endl;
296 sq_pushstring(vm, "levels", -1);
297 if(SQ_FAILED(sq_get(vm, -2)))
299 log_warning << "failed to get state.worlds['" << worldname << "'].levels" << std::endl;
303 for(auto level : levels)
305 sq_pushstring(vm, level.name.c_str(), -1);
306 if(SQ_FAILED(sq_get(vm, -2)))
308 log_warning << "failed to get state.worlds['" << worldname << "'].levels['"
309 << level.name << "']" << std::endl;
313 bool solved = scripting::read_bool(vm, "solved");
316 num_solved_levels += 1;
326 sq_settop(vm, oldtop);
328 return num_solved_levels;
332 World::get_basedir() const
338 World::get_title() const