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_stream.hpp"
22 #include "scripting/serialize.hpp"
23 #include "scripting/squirrel_util.hpp"
24 #include "supertux/globals.hpp"
25 #include "supertux/screen_manager.hpp"
26 #include "supertux/player_status.hpp"
27 #include "supertux/world.hpp"
28 #include "util/file_system.hpp"
29 #include "util/reader.hpp"
30 #include "util/string_util.hpp"
31 #include "worldmap/worldmap.hpp"
33 World* World::current_ = NULL;
47 player_status.reset(new PlayerStatus());
50 hide_from_contribs = false;
51 sq_resetobject(&world_thread);
56 sq_release(scripting::global_vm, &world_thread);
62 World::set_savegame_filename(const std::string& filename)
64 this->savegame_filename = filename;
65 // make sure the savegame directory exists
66 std::string dirname = FileSystem::dirname(filename);
67 if(!PHYSFS_exists(dirname.c_str())) {
68 if(!PHYSFS_mkdir(dirname.c_str())) {
69 std::ostringstream msg;
70 msg << "Couldn't create directory for savegames '"
71 << dirname << "': " <<PHYSFS_getLastError();
72 throw std::runtime_error(msg.str());
76 if(!PHYSFS_isDirectory(dirname.c_str())) {
77 std::ostringstream msg;
78 msg << "Savegame path '" << dirname << "' is not a directory";
79 throw std::runtime_error(msg.str());
84 World::load(const std::string& filename)
86 basedir = FileSystem::dirname(filename);
89 const lisp::Lisp* root = parser.parse(filename);
91 const lisp::Lisp* info = root->get_lisp("supertux-world");
93 info = root->get_lisp("supertux-level-subset");
95 throw std::runtime_error("File is not a world or levelsubset file");
97 hide_from_contribs = false;
100 info->get("title", title);
101 info->get("description", description);
102 info->get("levelset", is_levelset);
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(StringUtil::has_suffix(*filename, ".stl")) {
117 levels.push_back(path + *filename);
120 PHYSFS_freeList(files);
122 std::sort(levels.begin(), levels.end(), StringUtil::numeric_less);
128 using namespace scripting;
132 // create new squirrel table for persistent game state
133 HSQUIRRELVM vm = scripting::global_vm;
135 sq_pushroottable(vm);
136 sq_pushstring(vm, "state", -1);
138 if(SQ_FAILED(sq_createslot(vm, -3)))
139 throw scripting::SquirrelError(vm, "Couldn't create state table");
144 std::string filename = basedir + "/world.nut";
146 IFileStream in(filename);
148 sq_release(global_vm, &world_thread);
149 world_thread = create_thread(global_vm);
150 compile_and_run(object_to_vm(world_thread), in, filename);
151 } catch(std::exception& ) {
152 // fallback: try to load worldmap worldmap.stwm
153 using namespace worldmap;
154 g_screen_manager->push_screen(new WorldMap(basedir + "worldmap.stwm", get_player_status()));
161 using namespace scripting;
163 lisp::Writer writer(savegame_filename);
165 writer.start_list("supertux-savegame");
166 writer.write("version", 1);
168 using namespace worldmap;
169 if(WorldMap::current() != NULL) {
170 std::ostringstream title;
171 title << WorldMap::current()->get_title();
172 title << " (" << WorldMap::current()->solved_level_count()
173 << "/" << WorldMap::current()->level_count() << ")";
174 writer.write("title", title.str());
177 writer.start_list("tux");
178 player_status->write(writer);
179 writer.end_list("tux");
181 writer.start_list("state");
183 sq_pushroottable(global_vm);
184 sq_pushstring(global_vm, "state", -1);
185 if(SQ_SUCCEEDED(sq_get(global_vm, -2))) {
186 scripting::save_squirrel_table(global_vm, -1, writer);
187 sq_pop(global_vm, 1);
189 sq_pop(global_vm, 1);
190 writer.end_list("state");
192 writer.end_list("supertux-savegame");
198 using namespace scripting;
200 if(PHYSFS_exists(savegame_filename.c_str())) {
203 const lisp::Lisp* root = parser.parse(savegame_filename);
205 const lisp::Lisp* lisp = root->get_lisp("supertux-savegame");
207 throw std::runtime_error("file is not a supertux-savegame file");
210 lisp->get("version", version);
212 throw std::runtime_error("incompatible savegame version");
214 const lisp::Lisp* tux = lisp->get_lisp("tux");
216 throw std::runtime_error("No tux section in savegame");
217 player_status->read(*tux);
219 const lisp::Lisp* state = lisp->get_lisp("state");
221 throw std::runtime_error("No state section in savegame");
223 sq_pushroottable(global_vm);
224 sq_pushstring(global_vm, "state", -1);
225 if(SQ_FAILED(sq_deleteslot(global_vm, -2, SQFalse)))
226 sq_pop(global_vm, 1);
228 sq_pushstring(global_vm, "state", -1);
229 sq_newtable(global_vm);
230 load_squirrel_table(global_vm, -1, *state);
231 if(SQ_FAILED(sq_createslot(global_vm, -3)))
232 throw std::runtime_error("Couldn't create state table");
233 sq_pop(global_vm, 1);
234 } catch(std::exception& e) {
235 log_fatal << "Couldn't load savegame: " << e.what() << std::endl;
241 World::get_level_filename(unsigned int i) const
247 World::get_num_levels() const
249 return levels.size();
253 World::get_basedir() const
259 World::get_title() const