oops
[supertux.git] / src / level_subset.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 SuperTux Development Team, see AUTHORS for details
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 <sstream>
23 #include <stdexcept>
24 #include <assert.h>
25 #include <unistd.h>
26 #include <physfs.h>
27 #include "level.hpp"
28 #include "resources.hpp"
29 #include "file_system.hpp"
30 #include "video/surface.hpp"
31 #include "level_subset.hpp"
32 #include "lisp/parser.hpp"
33 #include "lisp/lisp.hpp"
34 #include "lisp/writer.hpp"
35
36 static bool has_suffix(const std::string& data, const std::string& suffix)
37 {
38   if (data.length() >= suffix.length())
39     return data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0;
40   else
41     return false;
42 }
43
44 LevelSubset::LevelSubset()
45   : levels(0)
46 {
47 }
48
49 LevelSubset::~LevelSubset()
50 {
51 }
52
53 void LevelSubset::create(const std::string& subset_name)
54 {
55   Level new_lev;
56   LevelSubset new_subset;
57   new_subset.name = subset_name;
58   new_subset.title = "Unknown Title";
59   new_subset.description = "No description so far.";
60   new_subset.hide_from_contribs = false;
61   new_subset.save();
62 }
63
64 void LevelSubset::read_info_file(const std::string& info_file)
65 {
66   lisp::Parser parser;
67   std::auto_ptr<lisp::Lisp> root (parser.parse(info_file));
68
69   const lisp::Lisp* info = root->get_lisp("supertux-level-subset");
70   if(!info)
71     throw std::runtime_error("File is not a levelsubset file");
72
73   hide_from_contribs = false;
74
75   info->get("title", title);
76   info->get("description", description);
77   info->get_vector("levels", levels);
78   info->get("hide-from-contribs", hide_from_contribs);
79 }
80
81 void LevelSubset::load(const std::string& subset)
82 {
83   name = subset;
84   
85   std::string infofile = subset + "/info";
86   try {
87     read_info_file(infofile);
88   } catch(std::exception& e) {
89     std::stringstream msg;
90     msg << "Couldn't parse info file '" << infofile << "': " << e.what();
91     throw std::runtime_error(msg.str());
92   }
93
94   // test is a worldmap exists
95   has_worldmap = false;
96   std::string worldmap = subset + "/worldmap.stwm";
97   if(PHYSFS_exists(worldmap.c_str())) {
98     has_worldmap = true;
99   }
100
101   if (levels.empty()) { 
102     // Level info file doesn't define any levels, so read the
103     // directory to see what we can find
104       
105     std::string path = subset + "/";
106     char** files = PHYSFS_enumerateFiles(path.c_str());
107     if(!files) {
108       std::cerr << "Warning: Couldn't read subset dir '" 
109                 << path << "'.\n";
110       return;
111     }
112
113     for(const char* const* filename = files; *filename != 0; ++filename) {
114       if(has_suffix(*filename, ".stl")) {
115         levels.push_back(path + *filename);
116       }
117     }
118     PHYSFS_freeList(files);
119   }
120 }
121
122 void
123 LevelSubset::save()
124 {
125   /* Save data file: */
126   std::string filename = name + "/info";
127   lisp::Writer writer(filename);
128
129   writer.start_list("supertux-level-subset");
130   writer.write_string("title", title);
131   writer.write_string("description", description);
132   writer.write_bool("hide-from-contribs", hide_from_contribs);
133   writer.end_list("supertux-level-subset");
134 }
135
136 void
137 LevelSubset::add_level(const std::string& name)
138 {
139   levels.push_back(name);
140 }
141
142 std::string
143 LevelSubset::get_level_filename(unsigned int num)
144 {
145   assert(num < levels.size());
146   return levels[num];
147 }
148
149 std::string
150 LevelSubset::get_worldmap_filename()
151 {
152   return std::string(name + "/worldmap.stwm");
153 }
154
155 int
156 LevelSubset::get_num_levels() const
157 {
158   return levels.size();
159 }