[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" |
---|
[2541] | 40 | #include "util/OrxEnum.h" |
---|
[1505] | 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
[2541] | 44 | // Use int as config file type to have an arbitrary number of files |
---|
| 45 | struct ConfigFileType : OrxEnum<ConfigFileType> |
---|
[1505] | 46 | { |
---|
[2541] | 47 | OrxEnumConstructors(ConfigFileType); |
---|
[2103] | 48 | |
---|
[2541] | 49 | static const int NoType = 0; |
---|
| 50 | static const int Settings = 1; |
---|
| 51 | static const int JoyStickCalibration = 2; |
---|
[2103] | 52 | |
---|
[2541] | 53 | static const int numberOfReservedTypes = 1024; |
---|
[1505] | 54 | }; |
---|
| 55 | |
---|
| 56 | bool config(const std::string& classname, const std::string& varname, const std::string& value); |
---|
| 57 | bool tconfig(const std::string& classname, const std::string& varname, const std::string& value); |
---|
| 58 | void reloadConfig(); |
---|
| 59 | void saveConfig(); |
---|
| 60 | void cleanConfig(); |
---|
| 61 | 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: |
---|
[2103] | 224 | inline ConfigFile(const std::string& filename, ConfigFileType type) |
---|
| 225 | : filename_(filename) |
---|
| 226 | , type_(type) |
---|
| 227 | , bUpdated_(false) |
---|
| 228 | { } |
---|
[1505] | 229 | ~ConfigFile(); |
---|
| 230 | |
---|
| 231 | void load(bool bCreateIfNotExisting = true); |
---|
| 232 | void save() const; |
---|
[2103] | 233 | void saveAs(const std::string& filename); |
---|
[1505] | 234 | void clean(bool bCleanComments = false); |
---|
[2103] | 235 | void clear(); |
---|
[1505] | 236 | |
---|
[2103] | 237 | const std::string& getFilename() { return this->filename_; } |
---|
| 238 | |
---|
[1505] | 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 | |
---|
[2103] | 254 | void updateConfigValues(); |
---|
| 255 | |
---|
[1505] | 256 | private: |
---|
| 257 | ConfigFileSection* getSection(const std::string& section); |
---|
| 258 | void saveIfUpdated(); |
---|
| 259 | |
---|
| 260 | std::string filename_; |
---|
[2103] | 261 | ConfigFileType type_; |
---|
[1505] | 262 | std::list<ConfigFileSection*> sections_; |
---|
| 263 | bool bUpdated_; |
---|
| 264 | }; |
---|
| 265 | |
---|
| 266 | |
---|
| 267 | /////////////////////// |
---|
| 268 | // ConfigFileManager // |
---|
| 269 | /////////////////////// |
---|
| 270 | class _CoreExport ConfigFileManager |
---|
| 271 | { |
---|
| 272 | public: |
---|
[2103] | 273 | ConfigFileManager(); |
---|
| 274 | ~ConfigFileManager(); |
---|
[1505] | 275 | |
---|
[2103] | 276 | void load(); |
---|
[1505] | 277 | void save(); |
---|
| 278 | void clean(bool bCleanComments = false); |
---|
| 279 | |
---|
[2103] | 280 | void setFilename(ConfigFileType type, const std::string& filename); |
---|
| 281 | const std::string& getFilename(ConfigFileType type); |
---|
| 282 | |
---|
| 283 | ConfigFileType getNewConfigFileType() { return mininmalFreeType_++; } |
---|
| 284 | |
---|
| 285 | void load(ConfigFileType type); |
---|
[1505] | 286 | void save(ConfigFileType type); |
---|
[2103] | 287 | void saveAs(ConfigFileType type, const std::string& saveFilename); |
---|
[1505] | 288 | void clean(ConfigFileType type, bool bCleanComments = false); |
---|
| 289 | |
---|
| 290 | inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& value, bool bString) |
---|
| 291 | { this->getFile(type)->setValue(section, name, value, bString); } |
---|
| 292 | inline std::string getValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& fallback, bool bString) |
---|
| 293 | { return this->getFile(type)->getValue(section, name, fallback, bString); } |
---|
| 294 | |
---|
| 295 | inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString) |
---|
| 296 | { this->getFile(type)->setValue(section, name, index, value, bString); } |
---|
| 297 | inline std::string getValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
| 298 | { return this->getFile(type)->getValue(section, name, index, fallback, bString); } |
---|
| 299 | |
---|
| 300 | inline void deleteVectorEntries(ConfigFileType type, const std::string& section, const std::string& name, unsigned int startindex = 0) |
---|
| 301 | { this->getFile(type)->deleteVectorEntries(section, name, startindex); } |
---|
| 302 | inline unsigned int getVectorSize(ConfigFileType type, const std::string& section, const std::string& name) |
---|
| 303 | { return this->getFile(type)->getVectorSize(section, name); } |
---|
| 304 | |
---|
[2103] | 305 | void updateConfigValues(); |
---|
| 306 | void updateConfigValues(ConfigFileType type); |
---|
[1505] | 307 | |
---|
[2103] | 308 | static ConfigFileManager& getInstance() { assert(singletonRef_s); return *singletonRef_s; } |
---|
| 309 | |
---|
[1505] | 310 | private: |
---|
[2103] | 311 | ConfigFileManager(const ConfigFileManager&); |
---|
[1505] | 312 | |
---|
| 313 | ConfigFile* getFile(ConfigFileType type); |
---|
| 314 | |
---|
[2103] | 315 | std::map<ConfigFileType, ConfigFile*> configFiles_; |
---|
| 316 | unsigned int mininmalFreeType_; |
---|
[1505] | 317 | |
---|
[2103] | 318 | static ConfigFileManager* singletonRef_s; |
---|
[1505] | 319 | }; |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | #endif /* _ConfigFileManager_H__ */ |
---|