Check that savegame exists before trying to load it.
[supertux.git] / src / supertux / world.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include <algorithm>
18
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"
32
33 World* World::current_ = NULL;
34
35 World::World() :
36   levels(),
37   basedir(),
38   savegame_filename(),
39   state_table(),
40   world_thread(),
41   title(),
42   description(),
43   player_status(),
44   hide_from_contribs(),
45   is_levelset()
46 {
47   player_status.reset(new PlayerStatus());
48
49   is_levelset = true;
50   hide_from_contribs = false;
51   sq_resetobject(&world_thread);
52 }
53
54 World::~World()
55 {
56   sq_release(scripting::global_vm, &world_thread);
57   if(current_ == this)
58     current_ = NULL;
59 }
60
61 void
62 World::set_savegame_filename(const std::string& filename)
63 {
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());
73     }
74   }
75
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());
80   }
81 }
82
83 void
84 World::load(const std::string& filename)
85 {
86   basedir = FileSystem::dirname(filename);
87
88   lisp::Parser parser;
89   const lisp::Lisp* root = parser.parse(filename);
90
91   const lisp::Lisp* info = root->get_lisp("supertux-world");
92   if(info == NULL)
93     info = root->get_lisp("supertux-level-subset");
94   if(info == NULL)
95     throw std::runtime_error("File is not a world or levelsubset file");
96
97   hide_from_contribs = false;
98   is_levelset = true;
99
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);
104
105   // Level info file doesn't define any levels, so read the
106   // directory to see what we can find
107
108   std::string path = basedir;
109   char** files = PHYSFS_enumerateFiles(path.c_str());
110   if(!files) {
111     log_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
112     return;
113   }
114
115   for(const char* const* filename = files; *filename != 0; ++filename) {
116     if(StringUtil::has_suffix(*filename, ".stl")) {
117       levels.push_back(path + *filename);
118     }
119   }
120   PHYSFS_freeList(files);
121
122   std::sort(levels.begin(), levels.end(), StringUtil::numeric_less);
123 }
124
125 void
126 World::run()
127 {
128   using namespace scripting;
129
130   current_ = this;
131
132   // create new squirrel table for persistent game state
133   HSQUIRRELVM vm = scripting::global_vm;
134
135   sq_pushroottable(vm);
136   sq_pushstring(vm, "state", -1);
137   sq_newtable(vm);
138   if(SQ_FAILED(sq_createslot(vm, -3)))
139     throw scripting::SquirrelError(vm, "Couldn't create state table");
140   sq_pop(vm, 1);
141
142   load_state();
143
144   std::string filename = basedir + "/world.nut";
145   try {
146     IFileStream in(filename);
147
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()));
155   }
156 }
157
158 void
159 World::save_state()
160 {
161   using namespace scripting;
162
163   lisp::Writer writer(savegame_filename);
164
165   writer.start_list("supertux-savegame");
166   writer.write("version", 1);
167
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());
175   }
176
177   writer.start_list("tux");
178   player_status->write(writer);
179   writer.end_list("tux");
180
181   writer.start_list("state");
182
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);
188   }
189   sq_pop(global_vm, 1);
190   writer.end_list("state");
191
192   writer.end_list("supertux-savegame");
193 }
194
195 void
196 World::load_state()
197 {
198   using namespace scripting;
199
200   if(PHYSFS_exists(savegame_filename)) {
201     try {
202       lisp::Parser parser;
203       const lisp::Lisp* root = parser.parse(savegame_filename);
204
205       const lisp::Lisp* lisp = root->get_lisp("supertux-savegame");
206       if(lisp == NULL)
207         throw std::runtime_error("file is not a supertux-savegame file");
208
209       int version = 1;
210       lisp->get("version", version);
211       if(version != 1)
212         throw std::runtime_error("incompatible savegame version");
213
214       const lisp::Lisp* tux = lisp->get_lisp("tux");
215       if(tux == NULL)
216         throw std::runtime_error("No tux section in savegame");
217       player_status->read(*tux);
218
219       const lisp::Lisp* state = lisp->get_lisp("state");
220       if(state == NULL)
221         throw std::runtime_error("No state section in savegame");
222
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);
227
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;
236     }
237   }
238 }
239
240 const std::string&
241 World::get_level_filename(unsigned int i) const
242 {
243   return levels[i];
244 }
245
246 unsigned int
247 World::get_num_levels() const
248 {
249   return levels.size();
250 }
251
252 const std::string&
253 World::get_basedir() const
254 {
255   return basedir;
256 }
257
258 const std::string&
259 World::get_title() const
260 {
261   return title;
262 }
263
264 /* EOF */