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 <cassert> |
---|
35 | #include <string> |
---|
36 | #include <list> |
---|
37 | #include <map> |
---|
38 | |
---|
39 | #include "util/OrxEnum.h" |
---|
40 | #include "util/Singleton.h" |
---|
41 | |
---|
42 | namespace orxonox |
---|
43 | { |
---|
44 | // Use int as config file type to have an arbitrary number of files |
---|
45 | struct ConfigFileType : OrxEnum<ConfigFileType> |
---|
46 | { |
---|
47 | OrxEnumConstructors(ConfigFileType); |
---|
48 | |
---|
49 | static const int NoType = 0; |
---|
50 | static const int Settings = 1; |
---|
51 | static const int JoyStickCalibration = 2; |
---|
52 | |
---|
53 | static const int numberOfReservedTypes = 1024; |
---|
54 | }; |
---|
55 | |
---|
56 | _CoreExport bool config(const std::string& classname, const std::string& varname, const std::string& value); |
---|
57 | _CoreExport bool tconfig(const std::string& classname, const std::string& varname, const std::string& value); |
---|
58 | _CoreExport void reloadConfig(); |
---|
59 | _CoreExport void saveConfig(); |
---|
60 | _CoreExport void cleanConfig(); |
---|
61 | _CoreExport void loadSettings(const std::string& filename); |
---|
62 | |
---|
63 | |
---|
64 | ///////////////////// |
---|
65 | // ConfigFileEntry // |
---|
66 | ///////////////////// |
---|
67 | class _CoreExport ConfigFileEntry |
---|
68 | { |
---|
69 | public: |
---|
70 | virtual ~ConfigFileEntry() {}; |
---|
71 | virtual void setValue(const std::string& value) = 0; |
---|
72 | virtual std::string getValue() const = 0; |
---|
73 | virtual const std::string& getName() const = 0; |
---|
74 | virtual void setComment(const std::string& comment) = 0; |
---|
75 | virtual unsigned int getIndex() const { return 0; } |
---|
76 | virtual void setString(bool bString) = 0; |
---|
77 | virtual std::string getFileEntry() const = 0; |
---|
78 | }; |
---|
79 | |
---|
80 | |
---|
81 | ////////////////////////// |
---|
82 | // ConfigFileEntryValue // |
---|
83 | ////////////////////////// |
---|
84 | class _CoreExport ConfigFileEntryValue : public ConfigFileEntry |
---|
85 | { |
---|
86 | public: |
---|
87 | 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) {} |
---|
88 | inline virtual ~ConfigFileEntryValue() {} |
---|
89 | |
---|
90 | inline virtual const std::string& getName() const |
---|
91 | { return this->name_; } |
---|
92 | |
---|
93 | inline virtual void setComment(const std::string& comment) |
---|
94 | { this->additionalComment_ = comment; } |
---|
95 | |
---|
96 | virtual void setValue(const std::string& value); |
---|
97 | virtual std::string getValue() const; |
---|
98 | |
---|
99 | inline bool isString() const |
---|
100 | { return this->bString_; } |
---|
101 | inline void setString(bool bString) |
---|
102 | { this->bString_ = bString; } |
---|
103 | |
---|
104 | virtual std::string getFileEntry() const; |
---|
105 | |
---|
106 | protected: |
---|
107 | std::string name_; |
---|
108 | std::string value_; |
---|
109 | bool bString_; |
---|
110 | std::string additionalComment_; |
---|
111 | }; |
---|
112 | |
---|
113 | |
---|
114 | /////////////////////////////// |
---|
115 | // ConfigFileEntryVectorValue // |
---|
116 | /////////////////////////////// |
---|
117 | class _CoreExport ConfigFileEntryVectorValue : public ConfigFileEntryValue |
---|
118 | { |
---|
119 | public: |
---|
120 | 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) {} |
---|
121 | inline virtual ~ConfigFileEntryVectorValue() {} |
---|
122 | |
---|
123 | inline virtual unsigned int getIndex() const |
---|
124 | { return this->index_; } |
---|
125 | |
---|
126 | virtual std::string getFileEntry() const; |
---|
127 | |
---|
128 | private: |
---|
129 | unsigned int index_; |
---|
130 | }; |
---|
131 | |
---|
132 | |
---|
133 | //////////////////////////// |
---|
134 | // ConfigFileEntryComment // |
---|
135 | //////////////////////////// |
---|
136 | class _CoreExport ConfigFileEntryComment : public ConfigFileEntry |
---|
137 | { |
---|
138 | public: |
---|
139 | inline ConfigFileEntryComment(const std::string& comment) : comment_(comment) {} |
---|
140 | inline virtual ~ConfigFileEntryComment() {} |
---|
141 | |
---|
142 | inline virtual const std::string& getName() const |
---|
143 | { return this->comment_; } |
---|
144 | |
---|
145 | inline virtual void setComment(const std::string& comment) |
---|
146 | { this->comment_ = comment; } |
---|
147 | |
---|
148 | inline virtual void setValue(const std::string& value) |
---|
149 | {} |
---|
150 | inline virtual std::string getValue() const |
---|
151 | { return this->comment_; } |
---|
152 | |
---|
153 | inline void setString(bool bString) {} |
---|
154 | |
---|
155 | inline virtual std::string getFileEntry() const |
---|
156 | { return this->comment_; } |
---|
157 | |
---|
158 | private: |
---|
159 | std::string comment_; |
---|
160 | }; |
---|
161 | |
---|
162 | |
---|
163 | /////////////////////// |
---|
164 | // ConfigFileSection // |
---|
165 | /////////////////////// |
---|
166 | class _CoreExport ConfigFileSection |
---|
167 | { |
---|
168 | friend class ConfigFile; |
---|
169 | |
---|
170 | public: |
---|
171 | inline ConfigFileSection(const std::string& name, const std::string& additionalComment = "") : name_(name), additionalComment_(additionalComment), bUpdated_(false) {} |
---|
172 | ~ConfigFileSection(); |
---|
173 | |
---|
174 | inline const std::string& getName() const |
---|
175 | { return this->name_; } |
---|
176 | |
---|
177 | inline void setComment(const std::string& comment) |
---|
178 | { this->additionalComment_ = comment; } |
---|
179 | |
---|
180 | inline void setValue(const std::string& name, const std::string& value, bool bString) |
---|
181 | { this->getEntry(name, value, bString)->setValue(value); } |
---|
182 | inline std::string getValue(const std::string& name, const std::string& fallback, bool bString) |
---|
183 | { return this->getEntry(name, fallback, bString)->getValue(); } |
---|
184 | |
---|
185 | inline void setValue(const std::string& name, unsigned int index, const std::string& value, bool bString) |
---|
186 | { this->getEntry(name, index, value, bString)->setValue(value); } |
---|
187 | inline std::string getValue(const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
188 | { return this->getEntry(name, index, fallback, bString)->getValue(); } |
---|
189 | |
---|
190 | void deleteVectorEntries(const std::string& name, unsigned int startindex = 0); |
---|
191 | unsigned int getVectorSize(const std::string& name); |
---|
192 | |
---|
193 | std::string getFileEntry() const; |
---|
194 | |
---|
195 | private: |
---|
196 | std::list<ConfigFileEntry*>& getEntries() |
---|
197 | { return this->entries_; } |
---|
198 | std::list<ConfigFileEntry*>::const_iterator getEntriesBegin() const |
---|
199 | { return this->entries_.begin(); } |
---|
200 | std::list<ConfigFileEntry*>::const_iterator getEntriesEnd() const |
---|
201 | { return this->entries_.end(); } |
---|
202 | |
---|
203 | std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, const std::string& fallback, bool bString); |
---|
204 | std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString); |
---|
205 | |
---|
206 | inline ConfigFileEntry* getEntry(const std::string& name, const std::string& fallback, bool bString) |
---|
207 | { return (*this->getEntryIterator(name, fallback, bString)); } |
---|
208 | inline ConfigFileEntry* getEntry(const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
209 | { return (*this->getEntryIterator(name, index, fallback, bString)); } |
---|
210 | |
---|
211 | std::string name_; |
---|
212 | std::string additionalComment_; |
---|
213 | std::list<ConfigFileEntry*> entries_; |
---|
214 | bool bUpdated_; |
---|
215 | }; |
---|
216 | |
---|
217 | |
---|
218 | //////////////// |
---|
219 | // ConfigFile // |
---|
220 | //////////////// |
---|
221 | class _CoreExport ConfigFile |
---|
222 | { |
---|
223 | public: |
---|
224 | inline ConfigFile(const std::string& filename, ConfigFileType type) |
---|
225 | : filename_(filename) |
---|
226 | , type_(type) |
---|
227 | , bUpdated_(false) |
---|
228 | { } |
---|
229 | ~ConfigFile(); |
---|
230 | |
---|
231 | void load(bool bCreateIfNotExisting = true); |
---|
232 | void save() const; |
---|
233 | void saveAs(const std::string& filename); |
---|
234 | void clean(bool bCleanComments = false); |
---|
235 | void clear(); |
---|
236 | |
---|
237 | const std::string& getFilename() { return this->filename_; } |
---|
238 | |
---|
239 | inline void setValue(const std::string& section, const std::string& name, const std::string& value, bool bString) |
---|
240 | { this->getSection(section)->setValue(name, value, bString); this->save(); } |
---|
241 | inline std::string getValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString) |
---|
242 | { std::string output = this->getSection(section)->getValue(name, fallback, bString); this->saveIfUpdated(); return output; } |
---|
243 | |
---|
244 | inline void setValue(const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString) |
---|
245 | { this->getSection(section)->setValue(name, index, value, bString); this->save(); } |
---|
246 | inline std::string getValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
247 | { std::string output = this->getSection(section)->getValue(name, index, fallback, bString); this->saveIfUpdated(); return output; } |
---|
248 | |
---|
249 | inline void deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex = 0) |
---|
250 | { this->getSection(section)->deleteVectorEntries(name, startindex); } |
---|
251 | inline unsigned int getVectorSize(const std::string& section, const std::string& name) |
---|
252 | { return this->getSection(section)->getVectorSize(name); } |
---|
253 | |
---|
254 | void updateConfigValues(); |
---|
255 | |
---|
256 | private: |
---|
257 | ConfigFileSection* getSection(const std::string& section); |
---|
258 | void saveIfUpdated(); |
---|
259 | |
---|
260 | std::string filename_; |
---|
261 | ConfigFileType type_; |
---|
262 | std::list<ConfigFileSection*> sections_; |
---|
263 | bool bUpdated_; |
---|
264 | }; |
---|
265 | |
---|
266 | |
---|
267 | /////////////////////// |
---|
268 | // ConfigFileManager // |
---|
269 | /////////////////////// |
---|
270 | class _CoreExport ConfigFileManager : public Singleton<ConfigFileManager> |
---|
271 | { |
---|
272 | friend class Singleton<ConfigFileManager>; |
---|
273 | public: |
---|
274 | ConfigFileManager(); |
---|
275 | ~ConfigFileManager(); |
---|
276 | |
---|
277 | void load(); |
---|
278 | void save(); |
---|
279 | void clean(bool bCleanComments = false); |
---|
280 | |
---|
281 | void setFilename(ConfigFileType type, const std::string& filename); |
---|
282 | const std::string& getFilename(ConfigFileType type); |
---|
283 | |
---|
284 | ConfigFileType getNewConfigFileType() { return mininmalFreeType_++; } |
---|
285 | |
---|
286 | void load(ConfigFileType type); |
---|
287 | void save(ConfigFileType type); |
---|
288 | void saveAs(ConfigFileType type, const std::string& saveFilename); |
---|
289 | void clean(ConfigFileType type, bool bCleanComments = false); |
---|
290 | |
---|
291 | inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& value, bool bString) |
---|
292 | { this->getFile(type)->setValue(section, name, value, bString); } |
---|
293 | inline std::string getValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& fallback, bool bString) |
---|
294 | { return this->getFile(type)->getValue(section, name, fallback, bString); } |
---|
295 | |
---|
296 | inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString) |
---|
297 | { this->getFile(type)->setValue(section, name, index, value, bString); } |
---|
298 | inline std::string getValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
299 | { return this->getFile(type)->getValue(section, name, index, fallback, bString); } |
---|
300 | |
---|
301 | inline void deleteVectorEntries(ConfigFileType type, const std::string& section, const std::string& name, unsigned int startindex = 0) |
---|
302 | { this->getFile(type)->deleteVectorEntries(section, name, startindex); } |
---|
303 | inline unsigned int getVectorSize(ConfigFileType type, const std::string& section, const std::string& name) |
---|
304 | { return this->getFile(type)->getVectorSize(section, name); } |
---|
305 | |
---|
306 | void updateConfigValues(); |
---|
307 | void updateConfigValues(ConfigFileType type); |
---|
308 | |
---|
309 | static std::string DEFAULT_CONFIG_FILE; |
---|
310 | |
---|
311 | private: |
---|
312 | ConfigFileManager(const ConfigFileManager&); |
---|
313 | |
---|
314 | ConfigFile* getFile(ConfigFileType type); |
---|
315 | |
---|
316 | std::map<ConfigFileType, ConfigFile*> configFiles_; |
---|
317 | unsigned int mininmalFreeType_; |
---|
318 | |
---|
319 | static ConfigFileManager* singletonPtr_s; |
---|
320 | }; |
---|
321 | } |
---|
322 | |
---|
323 | #endif /* _ConfigFileManager_H__ */ |
---|