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