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