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