| 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 <cassert> | 
|---|
| 35 | #include <string> | 
|---|
| 36 | #include <list> | 
|---|
| 37 | #include <map> | 
|---|
| 38 |  | 
|---|
| 39 | #include "util/OrxEnum.h" | 
|---|
| 40 | #include "util/Singleton.h" | 
|---|
| 41 |  | 
|---|
| 42 | // tolua_begin | 
|---|
| 43 | namespace orxonox | 
|---|
| 44 | { | 
|---|
| 45 |     // tolua_end | 
|---|
| 46 |     // Use int as config file type to have an arbitrary number of files | 
|---|
| 47 |     struct ConfigFileType : OrxEnum<ConfigFileType> | 
|---|
| 48 |     { | 
|---|
| 49 |         OrxEnumConstructors(ConfigFileType); | 
|---|
| 50 |  | 
|---|
| 51 |         static const int NoType              = 0; | 
|---|
| 52 |         static const int Settings            = 1; | 
|---|
| 53 |         static const int JoyStickCalibration = 2; | 
|---|
| 54 |         static const int CommandHistory      = 3; | 
|---|
| 55 |  | 
|---|
| 56 |         static const int numberOfReservedTypes = 1024; | 
|---|
| 57 |     }; | 
|---|
| 58 |  | 
|---|
| 59 |     _CoreExport bool config(const std::string& classname, const std::string& varname, const std::string& value); // tolua_export | 
|---|
| 60 |     _CoreExport const std::string& getConfig(const std::string& classname, const std::string& varname); // tolua_export | 
|---|
| 61 |     _CoreExport bool tconfig(const std::string& classname, const std::string& varname, const std::string& value); | 
|---|
| 62 |     _CoreExport void reloadConfig(); | 
|---|
| 63 |     _CoreExport void saveConfig(); | 
|---|
| 64 |     _CoreExport void cleanConfig(); | 
|---|
| 65 |     _CoreExport void loadSettings(const std::string& filename); | 
|---|
| 66 |  | 
|---|
| 67 |  | 
|---|
| 68 |     ///////////////////// | 
|---|
| 69 |     // ConfigFileEntry // | 
|---|
| 70 |     ///////////////////// | 
|---|
| 71 |     class _CoreExport ConfigFileEntry | 
|---|
| 72 |     { | 
|---|
| 73 |         public: | 
|---|
| 74 |             virtual ~ConfigFileEntry() {}; | 
|---|
| 75 |             virtual void setValue(const std::string& value) = 0; | 
|---|
| 76 |             virtual const std::string& getValue() const = 0; | 
|---|
| 77 |             virtual const std::string& getName() const = 0; | 
|---|
| 78 |             virtual void setComment(const std::string& comment) = 0; | 
|---|
| 79 |             virtual unsigned int getIndex() const { return 0; } | 
|---|
| 80 |             virtual void setString(bool bString) = 0; | 
|---|
| 81 |             virtual const std::string& getFileEntry() const = 0; | 
|---|
| 82 |     }; | 
|---|
| 83 |  | 
|---|
| 84 |  | 
|---|
| 85 |     ////////////////////////// | 
|---|
| 86 |     // ConfigFileEntryValue // | 
|---|
| 87 |     ////////////////////////// | 
|---|
| 88 |     class _CoreExport ConfigFileEntryValue : public ConfigFileEntry | 
|---|
| 89 |     { | 
|---|
| 90 |         public: | 
|---|
| 91 |             inline ConfigFileEntryValue(const std::string& name, const std::string& value = "", bool bString = false, const std::string& additionalComment = "") | 
|---|
| 92 |                 : name_(name) | 
|---|
| 93 |                 , value_(value) | 
|---|
| 94 |                 , additionalComment_(additionalComment) | 
|---|
| 95 |                 , bString_(bString) | 
|---|
| 96 |                 { this->update(); } | 
|---|
| 97 |  | 
|---|
| 98 |             inline virtual ~ConfigFileEntryValue() {} | 
|---|
| 99 |  | 
|---|
| 100 |             inline virtual const std::string& getName() const | 
|---|
| 101 |                 { return this->name_; } | 
|---|
| 102 |  | 
|---|
| 103 |             inline virtual void setComment(const std::string& comment) | 
|---|
| 104 |                 { this->additionalComment_ = comment; this->update(); } | 
|---|
| 105 |  | 
|---|
| 106 |             inline virtual void setValue(const std::string& value) | 
|---|
| 107 |                 { this->value_ = value; this->update(); } | 
|---|
| 108 |             inline virtual const std::string& getValue() const | 
|---|
| 109 |                 { return this->value_; } | 
|---|
| 110 |  | 
|---|
| 111 |             inline void virtual setString(bool bString) | 
|---|
| 112 |                 { this->bString_ = bString; this->update(); } | 
|---|
| 113 |  | 
|---|
| 114 |             inline virtual const std::string& getFileEntry() const | 
|---|
| 115 |                 { return this->fileEntry_; } | 
|---|
| 116 |  | 
|---|
| 117 |             inline virtual const std::string& getKeyString() const | 
|---|
| 118 |                 { return this->name_; } | 
|---|
| 119 |  | 
|---|
| 120 |         protected: | 
|---|
| 121 |             virtual void update(); | 
|---|
| 122 |  | 
|---|
| 123 |             const std::string name_; | 
|---|
| 124 |             std::string value_; | 
|---|
| 125 |             std::string additionalComment_; | 
|---|
| 126 |             std::string fileEntry_; | 
|---|
| 127 |             bool bString_; | 
|---|
| 128 |     }; | 
|---|
| 129 |  | 
|---|
| 130 |  | 
|---|
| 131 |     //////////////////////////////// | 
|---|
| 132 |     // ConfigFileEntryVectorValue // | 
|---|
| 133 |     //////////////////////////////// | 
|---|
| 134 |     class _CoreExport ConfigFileEntryVectorValue : public ConfigFileEntryValue | 
|---|
| 135 |     { | 
|---|
| 136 |         public: | 
|---|
| 137 |             inline ConfigFileEntryVectorValue(const std::string& name, unsigned int index, const std::string& value = "", bool bString = false, const std::string& additionalComment = "") | 
|---|
| 138 |                 : ConfigFileEntryValue(name, value, bString, additionalComment) | 
|---|
| 139 |                 , index_(index) | 
|---|
| 140 |                 { this->update(); /*No virtual calls in base class ctor*/ } | 
|---|
| 141 |  | 
|---|
| 142 |             inline ~ConfigFileEntryVectorValue() {} | 
|---|
| 143 |  | 
|---|
| 144 |             inline unsigned int getIndex() const | 
|---|
| 145 |                 { return this->index_; } | 
|---|
| 146 |  | 
|---|
| 147 |             inline const std::string& getKeyString() const | 
|---|
| 148 |                 { return this->keyString_; } | 
|---|
| 149 |  | 
|---|
| 150 |         private: | 
|---|
| 151 |             void update(); | 
|---|
| 152 |  | 
|---|
| 153 |             unsigned int index_; | 
|---|
| 154 |             std::string keyString_; | 
|---|
| 155 |     }; | 
|---|
| 156 |  | 
|---|
| 157 |  | 
|---|
| 158 |     //////////////////////////// | 
|---|
| 159 |     // ConfigFileEntryComment // | 
|---|
| 160 |     //////////////////////////// | 
|---|
| 161 |     class _CoreExport ConfigFileEntryComment : public ConfigFileEntry | 
|---|
| 162 |     { | 
|---|
| 163 |         public: | 
|---|
| 164 |             inline ConfigFileEntryComment(const std::string& comment) : comment_(comment) {} | 
|---|
| 165 |             inline virtual ~ConfigFileEntryComment() {} | 
|---|
| 166 |  | 
|---|
| 167 |             inline virtual const std::string& getName() const | 
|---|
| 168 |                 { return this->comment_; } | 
|---|
| 169 |  | 
|---|
| 170 |             inline virtual void setComment(const std::string& comment) | 
|---|
| 171 |                 { this->comment_ = comment; } | 
|---|
| 172 |  | 
|---|
| 173 |             inline virtual void setValue(const std::string& value) | 
|---|
| 174 |                 {} | 
|---|
| 175 |             inline virtual const std::string& getValue() const | 
|---|
| 176 |                 { return BLANKSTRING; } | 
|---|
| 177 |  | 
|---|
| 178 |             inline void setString(bool bString) | 
|---|
| 179 |                 {} | 
|---|
| 180 |  | 
|---|
| 181 |             inline virtual const std::string& getFileEntry() const | 
|---|
| 182 |                 { return this->comment_; } | 
|---|
| 183 |  | 
|---|
| 184 |             inline virtual const std::string& getKeyString() const | 
|---|
| 185 |                 { return BLANKSTRING; } | 
|---|
| 186 |  | 
|---|
| 187 |         private: | 
|---|
| 188 |             std::string comment_; | 
|---|
| 189 |     }; | 
|---|
| 190 |  | 
|---|
| 191 |  | 
|---|
| 192 |     /////////////////////// | 
|---|
| 193 |     // ConfigFileSection // | 
|---|
| 194 |     /////////////////////// | 
|---|
| 195 |     class _CoreExport ConfigFileSection | 
|---|
| 196 |     { | 
|---|
| 197 |         friend class ConfigFile; | 
|---|
| 198 |  | 
|---|
| 199 |         public: | 
|---|
| 200 |             inline ConfigFileSection(const std::string& name, const std::string& additionalComment = "") | 
|---|
| 201 |                 : name_(name) | 
|---|
| 202 |                 , additionalComment_(additionalComment) | 
|---|
| 203 |                 , bUpdated_(false) | 
|---|
| 204 |                 {} | 
|---|
| 205 |             ~ConfigFileSection(); | 
|---|
| 206 |  | 
|---|
| 207 |             inline const std::string& getName() const | 
|---|
| 208 |                 { return this->name_; } | 
|---|
| 209 |  | 
|---|
| 210 |             inline void setComment(const std::string& comment) | 
|---|
| 211 |                 { this->additionalComment_ = comment; } | 
|---|
| 212 |  | 
|---|
| 213 |             inline void setValue(const std::string& name, const std::string& value, bool bString) | 
|---|
| 214 |                 { this->getEntry(name, value, bString)->setValue(value); } | 
|---|
| 215 |             inline const std::string& getValue(const std::string& name, const std::string& fallback, bool bString) | 
|---|
| 216 |                 { return this->getEntry(name, fallback, bString)->getValue(); } | 
|---|
| 217 |  | 
|---|
| 218 |             inline void setValue(const std::string& name, unsigned int index, const std::string& value, bool bString) | 
|---|
| 219 |                 { this->getEntry(name, index, value, bString)->setValue(value); } | 
|---|
| 220 |             inline const std::string& getValue(const std::string& name, unsigned int index, const std::string& fallback, bool bString) | 
|---|
| 221 |                 { return this->getEntry(name, index, fallback, bString)->getValue(); } | 
|---|
| 222 |  | 
|---|
| 223 |             void deleteVectorEntries(const std::string& name, unsigned int startindex = 0); | 
|---|
| 224 |             unsigned int getVectorSize(const std::string& name); | 
|---|
| 225 |  | 
|---|
| 226 |             std::string getFileEntry() const; | 
|---|
| 227 |  | 
|---|
| 228 |         private: | 
|---|
| 229 |             std::list<ConfigFileEntry*>& getEntries() | 
|---|
| 230 |                 { return this->entries_; } | 
|---|
| 231 |             std::list<ConfigFileEntry*>::const_iterator getEntriesBegin() const | 
|---|
| 232 |                 { return this->entries_.begin(); } | 
|---|
| 233 |             std::list<ConfigFileEntry*>::const_iterator getEntriesEnd() const | 
|---|
| 234 |                 { return this->entries_.end(); } | 
|---|
| 235 |  | 
|---|
| 236 |             std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, const std::string& fallback, bool bString); | 
|---|
| 237 |             std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString); | 
|---|
| 238 |  | 
|---|
| 239 |             inline ConfigFileEntry* getEntry(const std::string& name, const std::string& fallback, bool bString) | 
|---|
| 240 |                 { return (*this->getEntryIterator(name, fallback, bString)); } | 
|---|
| 241 |             inline ConfigFileEntry* getEntry(const std::string& name, unsigned int index, const std::string& fallback, bool bString) | 
|---|
| 242 |                 { return (*this->getEntryIterator(name, index, fallback, bString)); } | 
|---|
| 243 |  | 
|---|
| 244 |             std::string name_; | 
|---|
| 245 |             std::string additionalComment_; | 
|---|
| 246 |             std::list<ConfigFileEntry*> entries_; | 
|---|
| 247 |             bool bUpdated_; | 
|---|
| 248 |     }; | 
|---|
| 249 |  | 
|---|
| 250 |  | 
|---|
| 251 |     //////////////// | 
|---|
| 252 |     // ConfigFile // | 
|---|
| 253 |     //////////////// | 
|---|
| 254 |     class _CoreExport ConfigFile | 
|---|
| 255 |     { | 
|---|
| 256 |         public: | 
|---|
| 257 |             inline ConfigFile(const std::string& filename, ConfigFileType type) | 
|---|
| 258 |                 : filename_(filename) | 
|---|
| 259 |                 , type_(type) | 
|---|
| 260 |                 , bUpdated_(false) | 
|---|
| 261 |             { } | 
|---|
| 262 |             ~ConfigFile(); | 
|---|
| 263 |  | 
|---|
| 264 |             void load(bool bCreateIfNotExisting = true); | 
|---|
| 265 |             void save() const; | 
|---|
| 266 |             void saveAs(const std::string& filename); | 
|---|
| 267 |             void clean(bool bCleanComments = false); | 
|---|
| 268 |             void clear(); | 
|---|
| 269 |  | 
|---|
| 270 |             const std::string& getFilename() { return this->filename_; } | 
|---|
| 271 |  | 
|---|
| 272 |             inline void setValue(const std::string& section, const std::string& name, const std::string& value, bool bString) | 
|---|
| 273 |                 { this->getSection(section)->setValue(name, value, bString); this->save(); } | 
|---|
| 274 |             inline const std::string& getValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString) | 
|---|
| 275 |                 { const std::string& output = this->getSection(section)->getValue(name, fallback, bString); this->saveIfUpdated(); return output; } | 
|---|
| 276 |  | 
|---|
| 277 |             inline void setValue(const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString) | 
|---|
| 278 |                 { this->getSection(section)->setValue(name, index, value, bString); this->save(); } | 
|---|
| 279 |             inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString) | 
|---|
| 280 |                 { const std::string& output = this->getSection(section)->getValue(name, index, fallback, bString); this->saveIfUpdated(); return output; } | 
|---|
| 281 |  | 
|---|
| 282 |             inline void deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex = 0) | 
|---|
| 283 |                 { this->getSection(section)->deleteVectorEntries(name, startindex); } | 
|---|
| 284 |             inline unsigned int getVectorSize(const std::string& section, const std::string& name) | 
|---|
| 285 |                 { return this->getSection(section)->getVectorSize(name); } | 
|---|
| 286 |  | 
|---|
| 287 |             void updateConfigValues(); | 
|---|
| 288 |  | 
|---|
| 289 |         private: | 
|---|
| 290 |             ConfigFileSection* getSection(const std::string& section); | 
|---|
| 291 |             void saveIfUpdated(); | 
|---|
| 292 |  | 
|---|
| 293 |             std::string filename_; | 
|---|
| 294 |             ConfigFileType type_; | 
|---|
| 295 |             std::list<ConfigFileSection*> sections_; | 
|---|
| 296 |             bool bUpdated_; | 
|---|
| 297 |     }; | 
|---|
| 298 |  | 
|---|
| 299 |  | 
|---|
| 300 |     /////////////////////// | 
|---|
| 301 |     // ConfigFileManager // | 
|---|
| 302 |     /////////////////////// | 
|---|
| 303 |     class _CoreExport ConfigFileManager : public Singleton<ConfigFileManager> | 
|---|
| 304 |     { | 
|---|
| 305 |         friend class Singleton<ConfigFileManager>; | 
|---|
| 306 |         public: | 
|---|
| 307 |             ConfigFileManager(); | 
|---|
| 308 |             ~ConfigFileManager(); | 
|---|
| 309 |  | 
|---|
| 310 |             void load(); | 
|---|
| 311 |             void save(); | 
|---|
| 312 |             void clean(bool bCleanComments = false); | 
|---|
| 313 |  | 
|---|
| 314 |             void setFilename(ConfigFileType type, const std::string& filename); | 
|---|
| 315 |             const std::string& getFilename(ConfigFileType type); | 
|---|
| 316 |  | 
|---|
| 317 |             ConfigFileType getNewConfigFileType() { return mininmalFreeType_++; } | 
|---|
| 318 |  | 
|---|
| 319 |             void load(ConfigFileType type); | 
|---|
| 320 |             void save(ConfigFileType type); | 
|---|
| 321 |             void saveAs(ConfigFileType type, const std::string& saveFilename); | 
|---|
| 322 |             void clean(ConfigFileType type, bool bCleanComments = false); | 
|---|
| 323 |  | 
|---|
| 324 |             inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& value, bool bString) | 
|---|
| 325 |                 { this->getFile(type)->setValue(section, name, value, bString); } | 
|---|
| 326 |             inline const std::string& getValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& fallback, bool bString) | 
|---|
| 327 |                 { return this->getFile(type)->getValue(section, name, fallback, bString); } | 
|---|
| 328 |  | 
|---|
| 329 |             inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString) | 
|---|
| 330 |                 { this->getFile(type)->setValue(section, name, index, value, bString); } | 
|---|
| 331 |             inline const std::string& getValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString) | 
|---|
| 332 |                 { return this->getFile(type)->getValue(section, name, index, fallback, bString); } | 
|---|
| 333 |  | 
|---|
| 334 |             inline void deleteVectorEntries(ConfigFileType type, const std::string& section, const std::string& name, unsigned int startindex = 0) | 
|---|
| 335 |                 { this->getFile(type)->deleteVectorEntries(section, name, startindex); } | 
|---|
| 336 |             inline unsigned int getVectorSize(ConfigFileType type, const std::string& section, const std::string& name) | 
|---|
| 337 |                 { return this->getFile(type)->getVectorSize(section, name); } | 
|---|
| 338 |  | 
|---|
| 339 |             void updateConfigValues(); | 
|---|
| 340 |             void updateConfigValues(ConfigFileType type); | 
|---|
| 341 |  | 
|---|
| 342 |             static std::string DEFAULT_CONFIG_FILE; | 
|---|
| 343 |  | 
|---|
| 344 |         private: | 
|---|
| 345 |             ConfigFileManager(const ConfigFileManager&); | 
|---|
| 346 |  | 
|---|
| 347 |             ConfigFile* getFile(ConfigFileType type); | 
|---|
| 348 |  | 
|---|
| 349 |             std::map<ConfigFileType, ConfigFile*> configFiles_; | 
|---|
| 350 |             unsigned int mininmalFreeType_; | 
|---|
| 351 |  | 
|---|
| 352 |             static ConfigFileManager* singletonPtr_s; | 
|---|
| 353 |     }; | 
|---|
| 354 | } // tolua_export | 
|---|
| 355 |  | 
|---|
| 356 | #endif /* _ConfigFileManager_H__ */ | 
|---|