don't delete wrapper.cpp when doing jam clean
[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 "level.h"
27 #include "resources.h"
28 #include "file_system.h"
29 #include "video/surface.h"
30 #include "level_subset.h"
31 #include "lisp/parser.h"
32 #include "lisp/lisp.h"
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 LevelSubset::LevelSubset()
43   : levels(0)
44 {
45 }
46
47 LevelSubset::~LevelSubset()
48 {
49 }
50
51 void LevelSubset::create(const std::string& subset_name)
52 {
53   Level new_lev;
54   LevelSubset new_subset;
55   new_subset.name = subset_name;
56   new_subset.title = "Unknown Title";
57   new_subset.description = "No description so far.";
58   new_subset.hide_from_contribs = false;
59   new_subset.save();
60 }
61
62 void LevelSubset::read_info_file(const std::string& info_file)
63 {
64   lisp::Parser parser;
65   std::auto_ptr<lisp::Lisp> root (parser.parse(info_file));
66
67   const lisp::Lisp* info = root->get_lisp("supertux-level-subset");
68   if(!info)
69     throw std::runtime_error("File is not a levelsubset file");
70
71   hide_from_contribs = false;
72
73   info->get("title", title);
74   info->get("description", description);
75   info->get_vector("levels", levels);
76   info->get("hide-from-contribs", hide_from_contribs);
77 }
78
79 void LevelSubset::load(const std::string& subset)
80 {
81   name = subset;
82   
83   // Check in which directory our subset is located (ie. ~/.supertux/
84   // or SUPERTUX_DATADIR)
85   std::string filename = get_resource_filename(
86       std::string("levels/") + subset + "/info");
87   if(filename == "") {
88     std::stringstream msg;
89     msg << "Couldn't find level subset '" << subset << "'.";
90     throw new std::runtime_error(msg.str());
91   }
92  
93   try {
94     read_info_file(filename);
95   } catch(std::exception& e) {
96     std::stringstream msg;
97     msg << "Couldn't parse info file '" << filename << "': " << e.what();
98     throw new std::runtime_error(msg.str());
99   }
100
101   // test is a worldmap exists
102   has_worldmap = false;
103   std::string worldmap = get_resource_filename(
104       std::string("levels/") + subset + "/worldmap.stwm");
105   if(worldmap != "") {
106     has_worldmap = true;
107   }
108
109   if (levels.empty())
110     { // Level info file doesn't define any levels, so read the
111       // directory to see what we can find
112       std::set<std::string> files;
113   
114       filename = datadir + "/levels/" + subset + "/";
115       files = FileSystem::read_directory(filename);
116
117       filename = user_dir + "/levels/" + subset + "/";
118       std::set<std::string> user_files = FileSystem::read_directory(filename);
119       files.insert(user_files.begin(), user_files.end());
120   
121       for(std::set<std::string>::iterator i = files.begin(); i != files.end(); ++i)
122         {
123           if (has_suffix(*i, ".stl"))
124             levels.push_back(get_resource_filename(
125                   std::string("levels/" + subset+ "/" + *i)));
126         }
127     }
128 }
129
130 void
131 LevelSubset::save()
132 {
133   FILE* fi;
134   std::string filename;
135
136   /* Save data file: */
137   filename = "/levels/" + name + "/";
138
139   FileSystem::fcreatedir(filename.c_str());
140   filename = std::string(user_dir) + "/levels/" + name + "/info";
141   if(!FileSystem::fwriteable(filename.c_str()))
142     filename = datadir + "/levels/" + name + "/info";
143   if(FileSystem::fwriteable(filename.c_str()))
144     {
145       fi = fopen(filename.c_str(), "w");
146       if (fi == NULL)
147         {
148           perror(filename.c_str());
149         }
150
151       /* Write header: */
152       fprintf(fi,";; SuperTux-Level-Subset\n");
153       fprintf(fi,"(supertux-level-subset\n");
154
155       /* Save title info: */
156       fprintf(fi,"  (title \"%s\")\n", title.c_str());
157
158       /* Save the description: */
159       fprintf(fi,"  (description \"%s\")\n", description.c_str());
160
161       /* Save the hide from Contrbis menu boolean: */
162       fprintf(fi,"  (hide-from-contribs %s)\n", hide_from_contribs ? "#t" : "#f");
163
164       fprintf( fi,")");
165       fclose(fi);
166     }
167 }
168
169 void
170 LevelSubset::add_level(const std::string& name)
171 {
172   levels.push_back(name);
173 }
174
175 std::string
176 LevelSubset::get_level_filename(unsigned int num)
177 {
178   assert(num < levels.size());
179   return levels[num];
180 }
181
182 std::string
183 LevelSubset::get_worldmap_filename()
184 {
185   return std::string("/levels/" + name + "/worldmap.stwm");
186 }
187
188 int
189 LevelSubset::get_num_levels() const
190 {
191   return levels.size();
192 }