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/physfs_stream.hpp"
20 #include "scripting/serialize.hpp"
21 #include "scripting/squirrel_util.hpp"
22 #include "supertux/mainloop.hpp"
23 #include "supertux/player_status.hpp"
24 #include "supertux/world.hpp"
25 #include "util/file_system.hpp"
26 #include "worldmap/worldmap.hpp"
28 static bool has_suffix(const std::string& data, const std::string& suffix)
30 if (data.length() >= suffix.length())
31 return data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0;
36 World* World::current_ = NULL;
41 hide_from_contribs = false;
42 sq_resetobject(&world_thread);
47 sq_release(Scripting::global_vm, &world_thread);
53 World::set_savegame_filename(const std::string& filename)
55 this->savegame_filename = filename;
56 // make sure the savegame directory exists
57 std::string dirname = FileSystem::dirname(filename);
58 if(!PHYSFS_exists(dirname.c_str())) {
59 if(PHYSFS_mkdir(dirname.c_str())) {
60 std::ostringstream msg;
61 msg << "Couldn't create directory for savegames '"
62 << dirname << "': " <<PHYSFS_getLastError();
63 throw std::runtime_error(msg.str());
67 if(!PHYSFS_isDirectory(dirname.c_str())) {
68 std::ostringstream msg;
69 msg << "Savegame path '" << dirname << "' is not a directory";
70 throw std::runtime_error(msg.str());
75 World::load(const std::string& filename)
77 basedir = FileSystem::dirname(filename);
80 const lisp::Lisp* root = parser.parse(filename);
82 const lisp::Lisp* info = root->get_lisp("supertux-world");
84 info = root->get_lisp("supertux-level-subset");
86 throw std::runtime_error("File is not a world or levelsubset file");
88 hide_from_contribs = false;
91 info->get("title", title);
92 info->get("description", description);
93 info->get("levelset", is_levelset);
94 info->get("levels", levels);
95 info->get("hide-from-contribs", hide_from_contribs);
97 // Level info file doesn't define any levels, so read the
98 // directory to see what we can find
100 std::string path = basedir;
101 char** files = PHYSFS_enumerateFiles(path.c_str());
103 log_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
107 for(const char* const* filename = files; *filename != 0; ++filename) {
108 if(has_suffix(*filename, ".stl")) {
109 levels.push_back(path + *filename);
112 PHYSFS_freeList(files);
118 using namespace Scripting;
122 // create new squirrel table for persistent game state
123 HSQUIRRELVM vm = Scripting::global_vm;
125 sq_pushroottable(vm);
126 sq_pushstring(vm, "state", -1);
128 if(SQ_FAILED(sq_createslot(vm, -3)))
129 throw Scripting::SquirrelError(vm, "Couldn't create state table");
134 std::string filename = basedir + "/world.nut";
136 IFileStream in(filename);
138 sq_release(global_vm, &world_thread);
139 world_thread = create_thread(global_vm);
140 compile_and_run(object_to_vm(world_thread), in, filename);
141 } catch(std::exception& ) {
142 // fallback: try to load worldmap worldmap.stwm
143 using namespace WorldMapNS;
144 g_main_loop->push_screen(new WorldMap(basedir + "worldmap.stwm"));
151 using namespace Scripting;
153 lisp::Writer writer(savegame_filename);
155 writer.start_list("supertux-savegame");
156 writer.write("version", 1);
158 using namespace WorldMapNS;
159 if(WorldMap::current() != NULL) {
160 std::ostringstream title;
161 title << WorldMap::current()->get_title();
162 title << " (" << WorldMap::current()->solved_level_count()
163 << "/" << WorldMap::current()->level_count() << ")";
164 writer.write("title", title.str());
167 writer.start_list("tux");
168 player_status->write(writer);
169 writer.end_list("tux");
171 writer.start_list("state");
173 sq_pushroottable(global_vm);
174 sq_pushstring(global_vm, "state", -1);
175 if(SQ_SUCCEEDED(sq_get(global_vm, -2))) {
176 Scripting::save_squirrel_table(global_vm, -1, writer);
177 sq_pop(global_vm, 1);
179 sq_pop(global_vm, 1);
180 writer.end_list("state");
182 writer.end_list("supertux-savegame");
188 using namespace Scripting;
192 const lisp::Lisp* root = parser.parse(savegame_filename);
194 const lisp::Lisp* lisp = root->get_lisp("supertux-savegame");
196 throw std::runtime_error("file is not a supertux-savegame file");
199 lisp->get("version", version);
201 throw std::runtime_error("incompatible savegame version");
203 const lisp::Lisp* tux = lisp->get_lisp("tux");
205 throw std::runtime_error("No tux section in savegame");
206 player_status->read(*tux);
208 const lisp::Lisp* state = lisp->get_lisp("state");
210 throw std::runtime_error("No state section in savegame");
212 sq_pushroottable(global_vm);
213 sq_pushstring(global_vm, "state", -1);
214 if(SQ_FAILED(sq_deleteslot(global_vm, -2, SQFalse)))
215 sq_pop(global_vm, 1);
217 sq_pushstring(global_vm, "state", -1);
218 sq_newtable(global_vm);
219 load_squirrel_table(global_vm, -1, state);
220 if(SQ_FAILED(sq_createslot(global_vm, -3)))
221 throw std::runtime_error("Couldn't create state table");
222 sq_pop(global_vm, 1);
223 } catch(std::exception& e) {
224 log_debug << "Couldn't load savegame: " << e.what() << std::endl;
229 World::get_level_filename(unsigned int i) const
235 World::get_num_levels() const
237 return levels.size();
241 World::get_basedir() const