1 // $Id: level_subset.cpp 3118 2006-03-25 17:29:08Z sommer $
4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "file_system.hpp"
28 #include "lisp/parser.hpp"
29 #include "lisp/lisp.hpp"
30 #include "physfs/physfs_stream.hpp"
31 #include "script_manager.hpp"
32 #include "scripting/wrapper_util.hpp"
33 #include "scripting/serialize.hpp"
36 static bool has_suffix(const std::string& data, const std::string& suffix)
38 if (data.length() >= suffix.length())
39 return data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0;
47 hide_from_contribs = false;
55 World::set_savegame_filename(const std::string& filename)
57 this->savegame_filename = filename;
61 World::load(const std::string& filename)
63 basedir = FileSystem::dirname(filename);
66 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
68 const lisp::Lisp* info = root->get_lisp("supertux-world");
70 info = root->get_lisp("supertux-level-subset");
72 throw std::runtime_error("File is not a world or levelsubset file");
74 hide_from_contribs = false;
77 info->get("title", title);
78 info->get("description", description);
79 info->get("levelset", is_levelset);
80 info->get_vector("levels", levels);
81 info->get("hide-from-contribs", hide_from_contribs);
83 // Level info file doesn't define any levels, so read the
84 // directory to see what we can find
86 std::string path = basedir + "/";
87 char** files = PHYSFS_enumerateFiles(path.c_str());
89 msg_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
93 for(const char* const* filename = files; *filename != 0; ++filename) {
94 if(has_suffix(*filename, ".stl")) {
95 levels.push_back(path + *filename);
98 PHYSFS_freeList(files);
104 // create new squirrel table for persisten game state
105 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
107 sq_pushroottable(vm);
108 sq_pushstring(vm, "state", -1);
110 if(SQ_FAILED(sq_createslot(vm, -3)))
111 throw Scripting::SquirrelError(vm, "Couldn't create state table");
114 std::string filename = basedir + "/world.nut";
115 IFileStream in(filename);
117 HSQUIRRELVM new_vm = ScriptManager::instance->create_thread();
118 Scripting::compile_and_run(new_vm, in, filename);
124 lisp::Writer writer(savegame_filename);
126 writer.start_list("supertux-savegame");
127 writer.write_int("version", 1);
129 writer.start_list("tux");
130 player_status->write(writer);
131 writer.end_list("tux");
133 writer.start_list("state");
134 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
135 sq_pushroottable(vm);
136 sq_pushstring(vm, "state", -1);
137 if(SQ_SUCCEEDED(sq_get(vm, -2))) {
138 Scripting::save_squirrel_table(vm, -1, writer);
142 writer.end_list("state");
144 writer.end_list("supertux-savegame");
148 World::get_level_filename(unsigned int i) const
154 World::get_num_levels() const
156 return levels.size();