[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 | |
---|
[7363] | 29 | /** |
---|
| 30 | @file |
---|
| 31 | @ingroup Config ConfigFile |
---|
| 32 | */ |
---|
| 33 | |
---|
[1505] | 34 | #ifndef _ConfigFileManager_H__ |
---|
| 35 | #define _ConfigFileManager_H__ |
---|
| 36 | |
---|
| 37 | #include "CorePrereqs.h" |
---|
| 38 | |
---|
| 39 | #include <list> |
---|
| 40 | #include <map> |
---|
[6536] | 41 | #include <set> |
---|
| 42 | #include <string> |
---|
| 43 | #include <boost/array.hpp> |
---|
[1505] | 44 | |
---|
[3370] | 45 | #include "util/Singleton.h" |
---|
[1505] | 46 | |
---|
[6536] | 47 | namespace orxonox // tolua_export |
---|
| 48 | { // tolua_export |
---|
[2103] | 49 | |
---|
[1505] | 50 | ///////////////////// |
---|
| 51 | // ConfigFileEntry // |
---|
| 52 | ///////////////////// |
---|
| 53 | class _CoreExport ConfigFileEntry |
---|
| 54 | { |
---|
| 55 | public: |
---|
| 56 | virtual ~ConfigFileEntry() {}; |
---|
| 57 | virtual void setValue(const std::string& value) = 0; |
---|
[6425] | 58 | virtual const std::string& getValue() const = 0; |
---|
[1505] | 59 | virtual const std::string& getName() const = 0; |
---|
| 60 | virtual void setComment(const std::string& comment) = 0; |
---|
| 61 | virtual unsigned int getIndex() const { return 0; } |
---|
| 62 | virtual void setString(bool bString) = 0; |
---|
[6425] | 63 | virtual const std::string& getFileEntry() const = 0; |
---|
[1505] | 64 | }; |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | ////////////////////////// |
---|
| 68 | // ConfigFileEntryValue // |
---|
| 69 | ////////////////////////// |
---|
| 70 | class _CoreExport ConfigFileEntryValue : public ConfigFileEntry |
---|
| 71 | { |
---|
| 72 | public: |
---|
[6417] | 73 | inline ConfigFileEntryValue(const std::string& name, const std::string& value = "", bool bString = false, const std::string& additionalComment = "") |
---|
| 74 | : name_(name) |
---|
| 75 | , value_(value) |
---|
[6427] | 76 | , additionalComment_(additionalComment) |
---|
[6417] | 77 | , bString_(bString) |
---|
[6425] | 78 | { this->update(); } |
---|
| 79 | |
---|
[1505] | 80 | inline virtual ~ConfigFileEntryValue() {} |
---|
| 81 | |
---|
| 82 | inline virtual const std::string& getName() const |
---|
| 83 | { return this->name_; } |
---|
| 84 | |
---|
| 85 | inline virtual void setComment(const std::string& comment) |
---|
[6425] | 86 | { this->additionalComment_ = comment; this->update(); } |
---|
[1505] | 87 | |
---|
[6425] | 88 | inline virtual void setValue(const std::string& value) |
---|
| 89 | { this->value_ = value; this->update(); } |
---|
| 90 | inline virtual const std::string& getValue() const |
---|
| 91 | { return this->value_; } |
---|
[1505] | 92 | |
---|
[6425] | 93 | inline void virtual setString(bool bString) |
---|
| 94 | { this->bString_ = bString; this->update(); } |
---|
[1505] | 95 | |
---|
[6425] | 96 | inline virtual const std::string& getFileEntry() const |
---|
| 97 | { return this->fileEntry_; } |
---|
[1505] | 98 | |
---|
[6425] | 99 | inline virtual const std::string& getKeyString() const |
---|
| 100 | { return this->name_; } |
---|
| 101 | |
---|
[1505] | 102 | protected: |
---|
[6425] | 103 | virtual void update(); |
---|
| 104 | |
---|
| 105 | const std::string name_; |
---|
[1505] | 106 | std::string value_; |
---|
[6425] | 107 | std::string additionalComment_; |
---|
| 108 | std::string fileEntry_; |
---|
[1505] | 109 | bool bString_; |
---|
| 110 | }; |
---|
| 111 | |
---|
| 112 | |
---|
[6425] | 113 | //////////////////////////////// |
---|
[1505] | 114 | // ConfigFileEntryVectorValue // |
---|
[6425] | 115 | //////////////////////////////// |
---|
[1505] | 116 | class _CoreExport ConfigFileEntryVectorValue : public ConfigFileEntryValue |
---|
| 117 | { |
---|
| 118 | public: |
---|
[6425] | 119 | inline ConfigFileEntryVectorValue(const std::string& name, unsigned int index, const std::string& value = "", bool bString = false, const std::string& additionalComment = "") |
---|
| 120 | : ConfigFileEntryValue(name, value, bString, additionalComment) |
---|
| 121 | , index_(index) |
---|
| 122 | { this->update(); /*No virtual calls in base class ctor*/ } |
---|
[1505] | 123 | |
---|
[6425] | 124 | inline ~ConfigFileEntryVectorValue() {} |
---|
| 125 | |
---|
| 126 | inline unsigned int getIndex() const |
---|
[1505] | 127 | { return this->index_; } |
---|
| 128 | |
---|
[6425] | 129 | inline const std::string& getKeyString() const |
---|
| 130 | { return this->keyString_; } |
---|
[1505] | 131 | |
---|
| 132 | private: |
---|
[6425] | 133 | void update(); |
---|
| 134 | |
---|
[1505] | 135 | unsigned int index_; |
---|
[6425] | 136 | std::string keyString_; |
---|
[1505] | 137 | }; |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | //////////////////////////// |
---|
| 141 | // ConfigFileEntryComment // |
---|
| 142 | //////////////////////////// |
---|
| 143 | class _CoreExport ConfigFileEntryComment : public ConfigFileEntry |
---|
| 144 | { |
---|
| 145 | public: |
---|
| 146 | inline ConfigFileEntryComment(const std::string& comment) : comment_(comment) {} |
---|
| 147 | inline virtual ~ConfigFileEntryComment() {} |
---|
| 148 | |
---|
| 149 | inline virtual const std::string& getName() const |
---|
| 150 | { return this->comment_; } |
---|
| 151 | |
---|
| 152 | inline virtual void setComment(const std::string& comment) |
---|
| 153 | { this->comment_ = comment; } |
---|
| 154 | |
---|
| 155 | inline virtual void setValue(const std::string& value) |
---|
| 156 | {} |
---|
[6425] | 157 | inline virtual const std::string& getValue() const |
---|
| 158 | { return BLANKSTRING; } |
---|
[1505] | 159 | |
---|
[6425] | 160 | inline void setString(bool bString) |
---|
| 161 | {} |
---|
[1505] | 162 | |
---|
[6425] | 163 | inline virtual const std::string& getFileEntry() const |
---|
[1505] | 164 | { return this->comment_; } |
---|
| 165 | |
---|
[6425] | 166 | inline virtual const std::string& getKeyString() const |
---|
| 167 | { return BLANKSTRING; } |
---|
| 168 | |
---|
[1505] | 169 | private: |
---|
| 170 | std::string comment_; |
---|
| 171 | }; |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | /////////////////////// |
---|
| 175 | // ConfigFileSection // |
---|
| 176 | /////////////////////// |
---|
| 177 | class _CoreExport ConfigFileSection |
---|
| 178 | { |
---|
| 179 | friend class ConfigFile; |
---|
[6536] | 180 | friend class SettingsConfigFile; |
---|
[1505] | 181 | |
---|
| 182 | public: |
---|
[6417] | 183 | inline ConfigFileSection(const std::string& name, const std::string& additionalComment = "") |
---|
| 184 | : name_(name) |
---|
| 185 | , additionalComment_(additionalComment) |
---|
| 186 | , bUpdated_(false) |
---|
| 187 | {} |
---|
[1505] | 188 | ~ConfigFileSection(); |
---|
| 189 | |
---|
| 190 | inline const std::string& getName() const |
---|
| 191 | { return this->name_; } |
---|
| 192 | |
---|
| 193 | inline void setComment(const std::string& comment) |
---|
| 194 | { this->additionalComment_ = comment; } |
---|
| 195 | |
---|
| 196 | inline void setValue(const std::string& name, const std::string& value, bool bString) |
---|
[6536] | 197 | { this->getOrCreateEntry(name, value, bString)->setValue(value); } |
---|
| 198 | inline const std::string& getValue(const std::string& name, bool bString) |
---|
| 199 | { |
---|
| 200 | ConfigFileEntry* entry = this->getEntry(name); |
---|
| 201 | if (entry) |
---|
| 202 | { |
---|
| 203 | entry->setString(bString); |
---|
| 204 | return entry->getValue(); |
---|
| 205 | } |
---|
| 206 | return BLANKSTRING; |
---|
| 207 | } |
---|
| 208 | inline const std::string& getOrCreateValue(const std::string& name, const std::string& fallback, bool bString) |
---|
| 209 | { return this->getOrCreateEntry(name, fallback, bString)->getValue(); } |
---|
[1505] | 210 | |
---|
| 211 | inline void setValue(const std::string& name, unsigned int index, const std::string& value, bool bString) |
---|
[6536] | 212 | { this->getOrCreateEntry(name, index, value, bString)->setValue(value); } |
---|
| 213 | inline const std::string& getValue(const std::string& name, unsigned int index, bool bString) |
---|
| 214 | { |
---|
| 215 | ConfigFileEntry* entry = this->getEntry(name, index); |
---|
| 216 | if (entry) |
---|
| 217 | { |
---|
| 218 | entry->setString(bString); |
---|
| 219 | return entry->getValue(); |
---|
| 220 | } |
---|
| 221 | return BLANKSTRING; |
---|
| 222 | } |
---|
| 223 | inline const std::string& getOrCreateValue(const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
| 224 | { return this->getOrCreateEntry(name, index, fallback, bString)->getValue(); } |
---|
[1505] | 225 | |
---|
| 226 | void deleteVectorEntries(const std::string& name, unsigned int startindex = 0); |
---|
[6536] | 227 | unsigned int getVectorSize(const std::string& name) const; |
---|
[1505] | 228 | |
---|
| 229 | std::string getFileEntry() const; |
---|
| 230 | |
---|
| 231 | private: |
---|
| 232 | std::list<ConfigFileEntry*>& getEntries() |
---|
| 233 | { return this->entries_; } |
---|
| 234 | std::list<ConfigFileEntry*>::const_iterator getEntriesBegin() const |
---|
| 235 | { return this->entries_.begin(); } |
---|
| 236 | std::list<ConfigFileEntry*>::const_iterator getEntriesEnd() const |
---|
| 237 | { return this->entries_.end(); } |
---|
| 238 | |
---|
[6536] | 239 | std::list<ConfigFileEntry*>::iterator getOrCreateEntryIterator(const std::string& name, const std::string& fallback, bool bString); |
---|
| 240 | std::list<ConfigFileEntry*>::iterator getOrCreateEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString); |
---|
[1505] | 241 | |
---|
[6536] | 242 | ConfigFileEntry* getEntry(const std::string& name) const; |
---|
| 243 | inline ConfigFileEntry* getOrCreateEntry(const std::string& name, const std::string& fallback, bool bString) |
---|
| 244 | { return (*this->getOrCreateEntryIterator(name, fallback, bString)); } |
---|
| 245 | ConfigFileEntry* getEntry(const std::string& name, unsigned int index) const; |
---|
| 246 | inline ConfigFileEntry* getOrCreateEntry(const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
| 247 | { return (*this->getOrCreateEntryIterator(name, index, fallback, bString)); } |
---|
[1505] | 248 | |
---|
| 249 | std::string name_; |
---|
| 250 | std::string additionalComment_; |
---|
| 251 | std::list<ConfigFileEntry*> entries_; |
---|
| 252 | bool bUpdated_; |
---|
| 253 | }; |
---|
| 254 | |
---|
| 255 | |
---|
| 256 | //////////////// |
---|
| 257 | // ConfigFile // |
---|
| 258 | //////////////// |
---|
| 259 | class _CoreExport ConfigFile |
---|
| 260 | { |
---|
| 261 | public: |
---|
[6536] | 262 | ConfigFile(const std::string& filename, bool bCopyFallbackFile = true); |
---|
| 263 | virtual ~ConfigFile(); |
---|
[1505] | 264 | |
---|
[6536] | 265 | virtual void load(); |
---|
| 266 | virtual void save() const; |
---|
| 267 | virtual void saveAs(const std::string& filename) const; |
---|
| 268 | virtual void clear(); |
---|
[1505] | 269 | |
---|
[6536] | 270 | inline const std::string& getFilename() |
---|
| 271 | { return this->filename_; } |
---|
[2103] | 272 | |
---|
[1505] | 273 | inline void setValue(const std::string& section, const std::string& name, const std::string& value, bool bString) |
---|
[6536] | 274 | { |
---|
| 275 | this->getOrCreateSection(section)->setValue(name, value, bString); |
---|
| 276 | this->save(); |
---|
| 277 | } |
---|
| 278 | inline const std::string& getValue(const std::string& section, const std::string& name, bool bString) |
---|
| 279 | { |
---|
| 280 | ConfigFileSection* sectionPtr = this->getSection(section); |
---|
| 281 | return (sectionPtr ? sectionPtr->getValue(name, bString) : BLANKSTRING); |
---|
| 282 | } |
---|
| 283 | const std::string& getOrCreateValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString); |
---|
[1505] | 284 | |
---|
| 285 | inline void setValue(const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString) |
---|
[6536] | 286 | { |
---|
| 287 | this->getOrCreateSection(section)->setValue(name, index, value, bString); |
---|
| 288 | this->save(); |
---|
| 289 | } |
---|
| 290 | inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index, bool bString) |
---|
| 291 | { |
---|
| 292 | ConfigFileSection* sectionPtr = this->getSection(section); |
---|
| 293 | return (sectionPtr ? sectionPtr->getValue(name, index, bString) : BLANKSTRING); |
---|
| 294 | } |
---|
| 295 | const std::string& getOrCreateValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString); |
---|
[1505] | 296 | |
---|
[6536] | 297 | void deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex = 0); |
---|
| 298 | inline unsigned int getVectorSize(const std::string& section, const std::string& name) const |
---|
| 299 | { |
---|
| 300 | ConfigFileSection* sectionPtr = this->getSection(section); |
---|
| 301 | return (sectionPtr ? sectionPtr->getVectorSize(name) : 0); |
---|
| 302 | } |
---|
[1505] | 303 | |
---|
[6536] | 304 | static const char* DEFAULT_CONFIG_FOLDER; |
---|
[2103] | 305 | |
---|
[6536] | 306 | protected: |
---|
| 307 | ConfigFileSection* getSection(const std::string& section) const; |
---|
| 308 | ConfigFileSection* getOrCreateSection(const std::string& section); |
---|
| 309 | |
---|
| 310 | std::list<ConfigFileSection*> sections_; |
---|
| 311 | |
---|
[1505] | 312 | private: |
---|
| 313 | void saveIfUpdated(); |
---|
[6536] | 314 | const std::string filename_; |
---|
| 315 | const bool bCopyFallbackFile_; |
---|
[1505] | 316 | bool bUpdated_; |
---|
| 317 | }; |
---|
| 318 | |
---|
| 319 | |
---|
[6536] | 320 | //////////////////////// |
---|
| 321 | // SettingsConfigFile // |
---|
| 322 | //////////////////////// |
---|
| 323 | class _CoreExport SettingsConfigFile // tolua_export |
---|
| 324 | : public ConfigFile, public Singleton<SettingsConfigFile> |
---|
| 325 | { // tolua_export |
---|
| 326 | friend class Singleton<SettingsConfigFile>; |
---|
| 327 | |
---|
| 328 | public: |
---|
| 329 | typedef std::multimap<std::string, std::pair<std::string, ConfigValueContainer*> > ContainerMap; |
---|
| 330 | |
---|
| 331 | SettingsConfigFile(const std::string& filename); |
---|
| 332 | ~SettingsConfigFile(); |
---|
| 333 | |
---|
| 334 | void load(); // tolua_export |
---|
| 335 | void setFilename(const std::string& filename); // tolua_export |
---|
| 336 | void clean(bool bCleanComments = false); // tolua_export |
---|
| 337 | |
---|
[7284] | 338 | void config(const std::string& section, const std::string& entry, const std::string& value); // tolua_export |
---|
| 339 | void tconfig(const std::string& section, const std::string& entry, const std::string& value); // tolua_export |
---|
[6536] | 340 | std::string getConfig(const std::string& section, const std::string& entry); // tolua_export |
---|
| 341 | |
---|
| 342 | void addConfigValueContainer(ConfigValueContainer* container); |
---|
| 343 | void removeConfigValueContainer(ConfigValueContainer* container); |
---|
| 344 | |
---|
| 345 | inline const std::set<std::string>& getSectionNames() |
---|
| 346 | { return this->sectionNames_; } |
---|
| 347 | inline ContainerMap::const_iterator getContainerLowerBound(const std::string section) |
---|
| 348 | { return this->containers_.lower_bound(section); } |
---|
| 349 | inline ContainerMap::const_iterator getContainerUpperBound(const std::string section) |
---|
| 350 | { return this->containers_.upper_bound(section); } |
---|
| 351 | |
---|
| 352 | static SettingsConfigFile& getInstance() { return Singleton<SettingsConfigFile>::getInstance(); } // tolua_export |
---|
| 353 | |
---|
| 354 | private: |
---|
| 355 | void updateConfigValues(); |
---|
| 356 | bool configImpl(const std::string& section, const std::string& entry, const std::string& value, bool (ConfigValueContainer::*function)(const MultiType&)); |
---|
| 357 | |
---|
| 358 | ContainerMap containers_; |
---|
| 359 | std::set<std::string> sectionNames_; |
---|
| 360 | static SettingsConfigFile* singletonPtr_s; |
---|
| 361 | }; // tolua_export |
---|
| 362 | |
---|
| 363 | |
---|
[1505] | 364 | /////////////////////// |
---|
| 365 | // ConfigFileManager // |
---|
| 366 | /////////////////////// |
---|
[3370] | 367 | class _CoreExport ConfigFileManager : public Singleton<ConfigFileManager> |
---|
[1505] | 368 | { |
---|
[3370] | 369 | friend class Singleton<ConfigFileManager>; |
---|
[1505] | 370 | public: |
---|
[2103] | 371 | ConfigFileManager(); |
---|
| 372 | ~ConfigFileManager(); |
---|
[1505] | 373 | |
---|
[6536] | 374 | void setFilename(ConfigFileType::Value type, const std::string& filename); |
---|
[1505] | 375 | |
---|
[6536] | 376 | inline ConfigFile* getConfigFile(ConfigFileType::Value type) |
---|
| 377 | { |
---|
| 378 | // Check array bounds |
---|
| 379 | return configFiles_.at(type); |
---|
| 380 | } |
---|
[2103] | 381 | |
---|
[1505] | 382 | private: |
---|
[2103] | 383 | ConfigFileManager(const ConfigFileManager&); |
---|
[1505] | 384 | |
---|
[6536] | 385 | boost::array<ConfigFile*, 3> configFiles_; |
---|
[3370] | 386 | static ConfigFileManager* singletonPtr_s; |
---|
[1505] | 387 | }; |
---|
[6417] | 388 | } // tolua_export |
---|
[1505] | 389 | |
---|
| 390 | #endif /* _ConfigFileManager_H__ */ |
---|