1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Fabian 'x3n' Landau |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #ifndef _ConfigFileManager_H__ |
---|
30 | #define _ConfigFileManager_H__ |
---|
31 | |
---|
32 | #include "CorePrereqs.h" |
---|
33 | |
---|
34 | #include <iostream> |
---|
35 | #include <string> |
---|
36 | #include <list> |
---|
37 | #include <map> |
---|
38 | |
---|
39 | #include "util/Math.h" |
---|
40 | |
---|
41 | namespace orxonox |
---|
42 | { |
---|
43 | enum _CoreExport ConfigFileType |
---|
44 | { |
---|
45 | CFT_Settings, |
---|
46 | CFT_Keybindings, |
---|
47 | CFT_JoyStickCalibration |
---|
48 | }; |
---|
49 | |
---|
50 | |
---|
51 | bool config(const std::string& classname, const std::string& varname, const std::string& value); |
---|
52 | bool tconfig(const std::string& classname, const std::string& varname, const std::string& value); |
---|
53 | void reloadConfig(); |
---|
54 | void saveConfig(); |
---|
55 | void cleanConfig(); |
---|
56 | void loadSettings(const std::string& filename); |
---|
57 | void loadKeybindings(const std::string& filename); |
---|
58 | |
---|
59 | |
---|
60 | ///////////////////// |
---|
61 | // ConfigFileEntry // |
---|
62 | ///////////////////// |
---|
63 | class _CoreExport ConfigFileEntry |
---|
64 | { |
---|
65 | public: |
---|
66 | virtual ~ConfigFileEntry() {}; |
---|
67 | virtual void setValue(const std::string& value) = 0; |
---|
68 | virtual std::string getValue() const = 0; |
---|
69 | virtual const std::string& getName() const = 0; |
---|
70 | virtual void setComment(const std::string& comment) = 0; |
---|
71 | virtual unsigned int getIndex() const { return 0; } |
---|
72 | virtual void setString(bool bString) = 0; |
---|
73 | virtual std::string getFileEntry() const = 0; |
---|
74 | }; |
---|
75 | |
---|
76 | |
---|
77 | ////////////////////////// |
---|
78 | // ConfigFileEntryValue // |
---|
79 | ////////////////////////// |
---|
80 | class _CoreExport ConfigFileEntryValue : public ConfigFileEntry |
---|
81 | { |
---|
82 | public: |
---|
83 | inline ConfigFileEntryValue(const std::string& name, const std::string& value = "", bool bString = false, const std::string& additionalComment = "") : name_(name), value_(value), bString_(bString), additionalComment_(additionalComment) {} |
---|
84 | inline virtual ~ConfigFileEntryValue() {} |
---|
85 | |
---|
86 | inline virtual const std::string& getName() const |
---|
87 | { return this->name_; } |
---|
88 | |
---|
89 | inline virtual void setComment(const std::string& comment) |
---|
90 | { this->additionalComment_ = comment; } |
---|
91 | |
---|
92 | virtual void setValue(const std::string& value); |
---|
93 | virtual std::string getValue() const; |
---|
94 | |
---|
95 | inline bool isString() const |
---|
96 | { return this->bString_; } |
---|
97 | inline void setString(bool bString) |
---|
98 | { this->bString_ = bString; } |
---|
99 | |
---|
100 | virtual std::string getFileEntry() const; |
---|
101 | |
---|
102 | protected: |
---|
103 | std::string name_; |
---|
104 | std::string value_; |
---|
105 | bool bString_; |
---|
106 | std::string additionalComment_; |
---|
107 | }; |
---|
108 | |
---|
109 | |
---|
110 | /////////////////////////////// |
---|
111 | // ConfigFileEntryVectorValue // |
---|
112 | /////////////////////////////// |
---|
113 | class _CoreExport ConfigFileEntryVectorValue : public ConfigFileEntryValue |
---|
114 | { |
---|
115 | public: |
---|
116 | inline ConfigFileEntryVectorValue(const std::string& name, unsigned int index, const std::string& value = "", bool bString = false, const std::string& additionalComment = "") : ConfigFileEntryValue(name, value, bString, additionalComment), index_(index) {} |
---|
117 | inline virtual ~ConfigFileEntryVectorValue() {} |
---|
118 | |
---|
119 | inline virtual unsigned int getIndex() const |
---|
120 | { return this->index_; } |
---|
121 | |
---|
122 | virtual std::string getFileEntry() const; |
---|
123 | |
---|
124 | private: |
---|
125 | unsigned int index_; |
---|
126 | }; |
---|
127 | |
---|
128 | |
---|
129 | //////////////////////////// |
---|
130 | // ConfigFileEntryComment // |
---|
131 | //////////////////////////// |
---|
132 | class _CoreExport ConfigFileEntryComment : public ConfigFileEntry |
---|
133 | { |
---|
134 | public: |
---|
135 | inline ConfigFileEntryComment(const std::string& comment) : comment_(comment) {} |
---|
136 | inline virtual ~ConfigFileEntryComment() {} |
---|
137 | |
---|
138 | inline virtual const std::string& getName() const |
---|
139 | { return this->comment_; } |
---|
140 | |
---|
141 | inline virtual void setComment(const std::string& comment) |
---|
142 | { this->comment_ = comment; } |
---|
143 | |
---|
144 | inline virtual void setValue(const std::string& value) |
---|
145 | {} |
---|
146 | inline virtual std::string getValue() const |
---|
147 | { return this->comment_; } |
---|
148 | |
---|
149 | inline void setString(bool bString) {} |
---|
150 | |
---|
151 | inline virtual std::string getFileEntry() const |
---|
152 | { return this->comment_; } |
---|
153 | |
---|
154 | private: |
---|
155 | std::string comment_; |
---|
156 | }; |
---|
157 | |
---|
158 | |
---|
159 | /////////////////////// |
---|
160 | // ConfigFileSection // |
---|
161 | /////////////////////// |
---|
162 | class _CoreExport ConfigFileSection |
---|
163 | { |
---|
164 | friend class ConfigFile; |
---|
165 | |
---|
166 | public: |
---|
167 | inline ConfigFileSection(const std::string& name, const std::string& additionalComment = "") : name_(name), additionalComment_(additionalComment), bUpdated_(false) {} |
---|
168 | ~ConfigFileSection(); |
---|
169 | |
---|
170 | inline const std::string& getName() const |
---|
171 | { return this->name_; } |
---|
172 | |
---|
173 | inline void setComment(const std::string& comment) |
---|
174 | { this->additionalComment_ = comment; } |
---|
175 | |
---|
176 | inline void setValue(const std::string& name, const std::string& value, bool bString) |
---|
177 | { this->getEntry(name, value, bString)->setValue(value); } |
---|
178 | inline std::string getValue(const std::string& name, const std::string& fallback, bool bString) |
---|
179 | { return this->getEntry(name, fallback, bString)->getValue(); } |
---|
180 | |
---|
181 | inline void setValue(const std::string& name, unsigned int index, const std::string& value, bool bString) |
---|
182 | { this->getEntry(name, index, value, bString)->setValue(value); } |
---|
183 | inline std::string getValue(const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
184 | { return this->getEntry(name, index, fallback, bString)->getValue(); } |
---|
185 | |
---|
186 | void deleteVectorEntries(const std::string& name, unsigned int startindex = 0); |
---|
187 | unsigned int getVectorSize(const std::string& name); |
---|
188 | |
---|
189 | std::string getFileEntry() const; |
---|
190 | |
---|
191 | private: |
---|
192 | std::list<ConfigFileEntry*>& getEntries() |
---|
193 | { return this->entries_; } |
---|
194 | std::list<ConfigFileEntry*>::const_iterator getEntriesBegin() const |
---|
195 | { return this->entries_.begin(); } |
---|
196 | std::list<ConfigFileEntry*>::const_iterator getEntriesEnd() const |
---|
197 | { return this->entries_.end(); } |
---|
198 | |
---|
199 | std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, const std::string& fallback, bool bString); |
---|
200 | std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString); |
---|
201 | |
---|
202 | inline ConfigFileEntry* getEntry(const std::string& name, const std::string& fallback, bool bString) |
---|
203 | { return (*this->getEntryIterator(name, fallback, bString)); } |
---|
204 | inline ConfigFileEntry* getEntry(const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
205 | { return (*this->getEntryIterator(name, index, fallback, bString)); } |
---|
206 | |
---|
207 | std::string name_; |
---|
208 | std::string additionalComment_; |
---|
209 | std::list<ConfigFileEntry*> entries_; |
---|
210 | bool bUpdated_; |
---|
211 | }; |
---|
212 | |
---|
213 | |
---|
214 | //////////////// |
---|
215 | // ConfigFile // |
---|
216 | //////////////// |
---|
217 | class _CoreExport ConfigFile |
---|
218 | { |
---|
219 | public: |
---|
220 | inline ConfigFile(const std::string& filename) : filename_(filename), bUpdated_(false) {} |
---|
221 | ~ConfigFile(); |
---|
222 | |
---|
223 | void load(bool bCreateIfNotExisting = true); |
---|
224 | void save() const; |
---|
225 | void save(const std::string& filename); |
---|
226 | void clean(bool bCleanComments = false); |
---|
227 | |
---|
228 | inline void setValue(const std::string& section, const std::string& name, const std::string& value, bool bString) |
---|
229 | { this->getSection(section)->setValue(name, value, bString); this->save(); } |
---|
230 | inline std::string getValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString) |
---|
231 | { std::string output = this->getSection(section)->getValue(name, fallback, bString); this->saveIfUpdated(); return output; } |
---|
232 | |
---|
233 | inline void setValue(const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString) |
---|
234 | { this->getSection(section)->setValue(name, index, value, bString); this->save(); } |
---|
235 | inline std::string getValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
236 | { std::string output = this->getSection(section)->getValue(name, index, fallback, bString); this->saveIfUpdated(); return output; } |
---|
237 | |
---|
238 | inline void deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex = 0) |
---|
239 | { this->getSection(section)->deleteVectorEntries(name, startindex); } |
---|
240 | inline unsigned int getVectorSize(const std::string& section, const std::string& name) |
---|
241 | { return this->getSection(section)->getVectorSize(name); } |
---|
242 | |
---|
243 | private: |
---|
244 | ConfigFileSection* getSection(const std::string& section); |
---|
245 | void saveIfUpdated(); |
---|
246 | |
---|
247 | std::string filename_; |
---|
248 | std::list<ConfigFileSection*> sections_; |
---|
249 | bool bUpdated_; |
---|
250 | }; |
---|
251 | |
---|
252 | |
---|
253 | /////////////////////// |
---|
254 | // ConfigFileManager // |
---|
255 | /////////////////////// |
---|
256 | class _CoreExport ConfigFileManager |
---|
257 | { |
---|
258 | public: |
---|
259 | static ConfigFileManager& getInstance(); |
---|
260 | |
---|
261 | void setFile(ConfigFileType type, const std::string& filename, bool bCreateIfNotExisting = true); |
---|
262 | |
---|
263 | void load(bool bCreateIfNotExisting = true); |
---|
264 | void save(); |
---|
265 | void clean(bool bCleanComments = false); |
---|
266 | |
---|
267 | void load(ConfigFileType type, bool bCreateIfNotExisting = true); |
---|
268 | void save(ConfigFileType type); |
---|
269 | void save(ConfigFileType type, const std::string& filename); |
---|
270 | void clean(ConfigFileType type, bool bCleanComments = false); |
---|
271 | |
---|
272 | inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& value, bool bString) |
---|
273 | { this->getFile(type)->setValue(section, name, value, bString); } |
---|
274 | inline std::string getValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& fallback, bool bString) |
---|
275 | { return this->getFile(type)->getValue(section, name, fallback, bString); } |
---|
276 | |
---|
277 | inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString) |
---|
278 | { this->getFile(type)->setValue(section, name, index, value, bString); } |
---|
279 | inline std::string getValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
280 | { return this->getFile(type)->getValue(section, name, index, fallback, bString); } |
---|
281 | |
---|
282 | inline void deleteVectorEntries(ConfigFileType type, const std::string& section, const std::string& name, unsigned int startindex = 0) |
---|
283 | { this->getFile(type)->deleteVectorEntries(section, name, startindex); } |
---|
284 | inline unsigned int getVectorSize(ConfigFileType type, const std::string& section, const std::string& name) |
---|
285 | { return this->getFile(type)->getVectorSize(section, name); } |
---|
286 | |
---|
287 | void updateConfigValues() const; |
---|
288 | void updateConfigValues(ConfigFileType type) const; |
---|
289 | |
---|
290 | private: |
---|
291 | ConfigFileManager(); |
---|
292 | ConfigFileManager(const ConfigFileManager& other); |
---|
293 | ~ConfigFileManager(); |
---|
294 | |
---|
295 | ConfigFile* getFile(ConfigFileType type); |
---|
296 | |
---|
297 | std::string getFilePath(const std::string& name) const; |
---|
298 | |
---|
299 | std::map<ConfigFileType, ConfigFile*> configFiles_; |
---|
300 | }; |
---|
301 | } |
---|
302 | |
---|
303 | #endif /* _ConfigFileManager_H__ */ |
---|