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