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