Update CMake to 3.2.1 in .travis.yml
[supertux.git] / src / supertux / gameconfig.cpp
1 //  SuperTux -  A Jump'n Run
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 "supertux/gameconfig.hpp"
18
19 #include <stdexcept>
20
21 #include "addon/addon_manager.hpp"
22 #include "control/input_manager.hpp"
23 #include "lisp/writer.hpp"
24 #include "lisp/list_iterator.hpp"
25 #include "lisp/parser.hpp"
26 #include "util/reader.hpp"
27 #include "util/log.hpp"
28 #include "supertux/globals.hpp"
29
30 Config::Config() :
31   profile(1),
32   fullscreen_size(0, 0),
33   fullscreen_refresh_rate(0),
34   window_size(1280, 800),
35   aspect_size(0, 0), // auto detect
36   magnification(0.0f),
37   use_fullscreen(false),
38   video(VideoSystem::AUTO_VIDEO),
39   try_vsync(true),
40   show_fps(false),
41   sound_enabled(true),
42   music_enabled(true),
43   console_enabled(false),
44   random_seed(0), // set by time(), by default (unless in config)
45   start_level(),
46   enable_script_debugger(false),
47   start_demo(),
48   record_demo(),
49   locale(),
50   keyboard_config(),
51   joystick_config(),
52   addons(),
53   developer_mode(false)
54 {
55 }
56
57 Config::~Config()
58 {}
59
60 void
61 Config::load()
62 {
63   lisp::Parser parser;
64   const lisp::Lisp* root = parser.parse("config");
65
66   const lisp::Lisp* config_lisp = root->get_lisp("supertux-config");
67   if(!config_lisp)
68   {
69     throw std::runtime_error("File is not a supertux-config file");
70   }
71
72   config_lisp->get("profile", profile);
73   config_lisp->get("show_fps", show_fps);
74   config_lisp->get("console", console_enabled);
75   config_lisp->get("developer", developer_mode);
76   config_lisp->get("locale", locale);
77   config_lisp->get("random_seed", random_seed);
78
79   const lisp::Lisp* config_video_lisp = config_lisp->get_lisp("video");
80   if(config_video_lisp)
81   {
82     config_video_lisp->get("fullscreen", use_fullscreen);
83     std::string video_string;
84     config_video_lisp->get("video", video_string);
85     video = VideoSystem::get_video_system(video_string);
86     config_video_lisp->get("vsync", try_vsync);
87
88     config_video_lisp->get("fullscreen_width",  fullscreen_size.width);
89     config_video_lisp->get("fullscreen_height", fullscreen_size.height);
90     config_video_lisp->get("fullscreen_refresh_rate", fullscreen_refresh_rate);
91
92     config_video_lisp->get("window_width",  window_size.width);
93     config_video_lisp->get("window_height", window_size.height);
94
95     config_video_lisp->get("aspect_width",  aspect_size.width);
96     config_video_lisp->get("aspect_height", aspect_size.height);
97
98     config_video_lisp->get("magnification", magnification);
99   }
100
101   const lisp::Lisp* config_audio_lisp = config_lisp->get_lisp("audio");
102   if(config_audio_lisp) {
103     config_audio_lisp->get("sound_enabled", sound_enabled);
104     config_audio_lisp->get("music_enabled", music_enabled);
105   }
106
107   const lisp::Lisp* config_control_lisp = config_lisp->get_lisp("control");
108   if (config_control_lisp)
109   {
110     const lisp::Lisp* keymap_lisp = config_control_lisp->get_lisp("keymap");
111     if (keymap_lisp)
112     {
113       keyboard_config.read(*keymap_lisp);
114     }
115
116     const lisp::Lisp* joystick_lisp = config_control_lisp->get_lisp("joystick");
117     if (joystick_lisp)
118     {
119       joystick_config.read(*joystick_lisp);
120     }
121   }
122
123   const lisp::Lisp* config_addons_lisp = config_lisp->get_lisp("addons");
124   if (config_addons_lisp)
125   {
126     lisp::ListIterator iter(config_addons_lisp);
127     while(iter.next())
128     {
129       const std::string& token = iter.item();
130       if (token == "addon")
131       {
132         std::string id;
133         bool enabled = false;
134         if (iter.lisp()->get("id", id) &&
135             iter.lisp()->get("enabled", enabled))
136         {
137           addons.push_back({id, enabled});
138         }
139       }
140       else
141       {
142         log_warning << "Unknown token in config file: " << token << std::endl;
143       }
144     }
145   }
146 }
147
148 void
149 Config::save()
150 {
151   lisp::Writer writer("config");
152
153   writer.start_list("supertux-config");
154
155   writer.write("profile", profile);
156   writer.write("show_fps", show_fps);
157   writer.write("console", console_enabled);
158   writer.write("developer", developer_mode);
159   writer.write("locale", locale);
160
161   writer.start_list("video");
162   writer.write("fullscreen", use_fullscreen);
163   writer.write("video", VideoSystem::get_video_string(video));
164   writer.write("vsync", try_vsync);
165
166   writer.write("fullscreen_width",  fullscreen_size.width);
167   writer.write("fullscreen_height", fullscreen_size.height);
168   writer.write("fullscreen_refresh_rate", fullscreen_refresh_rate);
169
170   writer.write("window_width",  window_size.width);
171   writer.write("window_height", window_size.height);
172
173   writer.write("aspect_width",  aspect_size.width);
174   writer.write("aspect_height", aspect_size.height);
175
176   writer.write("magnification", magnification);
177
178   writer.end_list("video");
179
180   writer.start_list("audio");
181   writer.write("sound_enabled", sound_enabled);
182   writer.write("music_enabled", music_enabled);
183   writer.end_list("audio");
184
185   writer.start_list("control");
186   {
187     writer.start_list("keymap");
188     keyboard_config.write(writer);
189     writer.end_list("keymap");
190
191     writer.start_list("joystick");
192     joystick_config.write(writer);
193     writer.end_list("joystick");
194   }
195   writer.end_list("control");
196
197   writer.start_list("addons");
198   for(auto addon : addons)
199   {
200     writer.start_list("addon");
201     writer.write("id", addon.id);
202     writer.write("enabled", addon.enabled);
203     writer.end_list("addon");
204   }
205   writer.end_list("addons");
206
207   writer.end_list("supertux-config");
208 }
209
210 /* EOF */