Major rewrite of scripting support:
[supertux.git] / src / world.cpp
1 //  $Id: level_subset.cpp 3118 2006-03-25 17:29:08Z sommer $
2 // 
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 // 
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
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include <physfs.h>
23 #include <stdexcept>
24
25 #include "world.hpp"
26 #include "file_system.hpp"
27 #include "lisp/parser.hpp"
28 #include "lisp/lisp.hpp"
29 #include "physfs/physfs_stream.hpp"
30 #include "script_manager.hpp"
31 #include "scripting/wrapper_util.hpp"
32 #include "msg.hpp"
33
34 static bool has_suffix(const std::string& data, const std::string& suffix)
35 {
36   if (data.length() >= suffix.length())
37     return data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0;
38   else
39     return false;
40 }
41
42 World::World()
43 {
44   is_levelset = true;
45   hide_from_contribs = false;
46 }
47
48 World::~World()
49 {
50 }
51
52 void
53 World::load(const std::string& filename)
54 {
55   basedir = FileSystem::dirname(filename);
56   
57   lisp::Parser parser;
58   std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
59
60   const lisp::Lisp* info = root->get_lisp("supertux-world");
61   if(info == NULL)
62     info = root->get_lisp("supertux-level-subset");
63   if(info == NULL)
64     throw std::runtime_error("File is not a world or levelsubset file");
65
66   hide_from_contribs = false;
67   is_levelset = true;
68
69   info->get("title", title);
70   info->get("description", description);
71   info->get("levelset", is_levelset);
72   info->get_vector("levels", levels);
73   info->get("hide-from-contribs", hide_from_contribs);
74
75   // Level info file doesn't define any levels, so read the
76   // directory to see what we can find
77       
78   std::string path = basedir + "/";
79   char** files = PHYSFS_enumerateFiles(path.c_str());
80   if(!files) {
81     msg_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
82     return;
83   }
84
85   for(const char* const* filename = files; *filename != 0; ++filename) {
86     if(has_suffix(*filename, ".stl")) {
87       levels.push_back(path + *filename);
88     }
89   }
90   PHYSFS_freeList(files);
91 }
92
93 void
94 World::run()
95 {
96   std::string filename = basedir + "/world.nut";
97   IFileStream in(filename);
98
99   HSQUIRRELVM vm = script_manager->create_thread();
100   Scripting::compile_and_run(vm, in, filename);
101 }
102
103 const std::string&
104 World::get_level_filename(unsigned int i) const
105 {
106   return levels[i];
107 }
108
109 unsigned int
110 World::get_num_levels() const
111 {
112   return levels.size();
113 }
114