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