Fixing SuperTux crashes when compiled on Mac OS X 10.9
[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_streambuf.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     IFileStreambuf ins(filename);
147     std::istream in(&ins);
148
149     sq_release(global_vm, &world_thread);
150     world_thread = create_thread(global_vm);
151     compile_and_run(object_to_vm(world_thread), in, filename);
152   } catch(std::exception& ) {
153     // fallback: try to load worldmap worldmap.stwm
154     using namespace worldmap;
155     g_screen_manager->push_screen(new WorldMap(basedir + "worldmap.stwm", get_player_status()));
156   }
157 }
158
159 void
160 World::save_state()
161 {
162   using namespace scripting;
163
164   lisp::Writer writer(savegame_filename);
165
166   writer.start_list("supertux-savegame");
167   writer.write("version", 1);
168
169   using namespace worldmap;
170   if(WorldMap::current() != NULL) {
171     std::ostringstream title;
172     title << WorldMap::current()->get_title();
173     title << " (" << WorldMap::current()->solved_level_count()
174           << "/" << WorldMap::current()->level_count() << ")";
175     writer.write("title", title.str());
176   }
177
178   writer.start_list("tux");
179   player_status->write(writer);
180   writer.end_list("tux");
181
182   writer.start_list("state");
183
184   sq_pushroottable(global_vm);
185   sq_pushstring(global_vm, "state", -1);
186   if(SQ_SUCCEEDED(sq_get(global_vm, -2))) {
187     scripting::save_squirrel_table(global_vm, -1, writer);
188     sq_pop(global_vm, 1);
189   }
190   sq_pop(global_vm, 1);
191   writer.end_list("state");
192
193   writer.end_list("supertux-savegame");
194 }
195
196 void
197 World::load_state()
198 {
199   using namespace scripting;
200
201   if(PHYSFS_exists(savegame_filename.c_str())) {
202     try {
203       lisp::Parser parser;
204       const lisp::Lisp* root = parser.parse(savegame_filename);
205
206       const lisp::Lisp* lisp = root->get_lisp("supertux-savegame");
207       if(lisp == NULL)
208         throw std::runtime_error("file is not a supertux-savegame file");
209
210       int version = 1;
211       lisp->get("version", version);
212       if(version != 1)
213         throw std::runtime_error("incompatible savegame version");
214
215       const lisp::Lisp* tux = lisp->get_lisp("tux");
216       if(tux == NULL)
217         throw std::runtime_error("No tux section in savegame");
218       player_status->read(*tux);
219
220       const lisp::Lisp* state = lisp->get_lisp("state");
221       if(state == NULL)
222         throw std::runtime_error("No state section in savegame");
223
224       sq_pushroottable(global_vm);
225       sq_pushstring(global_vm, "state", -1);
226       if(SQ_FAILED(sq_deleteslot(global_vm, -2, SQFalse)))
227         sq_pop(global_vm, 1);
228
229       sq_pushstring(global_vm, "state", -1);
230       sq_newtable(global_vm);
231       load_squirrel_table(global_vm, -1, *state);
232       if(SQ_FAILED(sq_createslot(global_vm, -3)))
233         throw std::runtime_error("Couldn't create state table");
234       sq_pop(global_vm, 1);
235     } catch(std::exception& e) {
236       log_fatal << "Couldn't load savegame: " << e.what() << std::endl;
237     }
238   }
239 }
240
241 const std::string&
242 World::get_level_filename(unsigned int i) const
243 {
244   return levels[i];
245 }
246
247 unsigned int
248 World::get_num_levels() const
249 {
250   return levels.size();
251 }
252
253 const std::string&
254 World::get_basedir() const
255 {
256   return basedir;
257 }
258
259 const std::string&
260 World::get_title() const
261 {
262   return title;
263 }
264
265 /* EOF */