[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 | #include "ConfigFileManager.h" |
---|
[2103] | 30 | |
---|
[2710] | 31 | #include <boost/filesystem.hpp> |
---|
| 32 | |
---|
[1505] | 33 | #include "util/Convert.h" |
---|
[3196] | 34 | #include "util/Math.h" |
---|
[7163] | 35 | #include "util/StringUtils.h" |
---|
[2103] | 36 | #include "ConfigValueContainer.h" |
---|
[5929] | 37 | #include "PathConfig.h" |
---|
[7284] | 38 | #include "command/ConsoleCommand.h" |
---|
[1505] | 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
| 42 | ////////////////////////// |
---|
| 43 | // ConfigFileEntryValue // |
---|
| 44 | ////////////////////////// |
---|
| 45 | |
---|
[6425] | 46 | void ConfigFileEntryValue::update() |
---|
[1505] | 47 | { |
---|
[6425] | 48 | // Make sure we remove the quotes when bString changes |
---|
| 49 | if (this->bString_) |
---|
| 50 | this->value_ = stripEnclosingQuotes(this->value_); |
---|
| 51 | // Assemble the entry line |
---|
| 52 | this->fileEntry_ = this->getKeyString() + " = "; |
---|
[6536] | 53 | if (this->bString_ && !this->value_.empty()) |
---|
[6425] | 54 | this->fileEntry_ += '"' + addSlashes(this->value_) + '"'; |
---|
[1505] | 55 | else |
---|
[6425] | 56 | this->fileEntry_ += this->value_; |
---|
| 57 | if (!this->additionalComment_.empty()) |
---|
| 58 | this->fileEntry_ += ' ' + this->additionalComment_; |
---|
[1505] | 59 | } |
---|
| 60 | |
---|
| 61 | |
---|
[2103] | 62 | //////////////////////////////// |
---|
[1505] | 63 | // ConfigFileEntryVectorValue // |
---|
[2103] | 64 | //////////////////////////////// |
---|
[6425] | 65 | void ConfigFileEntryVectorValue::update() |
---|
[1505] | 66 | { |
---|
[6425] | 67 | this->keyString_ = this->name_ + '[' + multi_cast<std::string>(this->index_) + ']'; |
---|
| 68 | ConfigFileEntryValue::update(); |
---|
[1505] | 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | /////////////////////// |
---|
| 73 | // ConfigFileSection // |
---|
| 74 | /////////////////////// |
---|
| 75 | ConfigFileSection::~ConfigFileSection() |
---|
| 76 | { |
---|
| 77 | for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ) |
---|
| 78 | delete (*(it++)); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | void ConfigFileSection::deleteVectorEntries(const std::string& name, unsigned int startindex) |
---|
| 82 | { |
---|
| 83 | for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ) |
---|
| 84 | { |
---|
| 85 | if (((*it)->getName() == name) && ((*it)->getIndex() >= startindex)) |
---|
| 86 | { |
---|
| 87 | delete (*it); |
---|
| 88 | this->entries_.erase(it++); |
---|
| 89 | } |
---|
| 90 | else |
---|
| 91 | { |
---|
| 92 | ++it; |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
[6536] | 97 | unsigned int ConfigFileSection::getVectorSize(const std::string& name) const |
---|
[1505] | 98 | { |
---|
| 99 | unsigned int size = 0; |
---|
| 100 | for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) |
---|
| 101 | if ((*it)->getName() == name) |
---|
| 102 | if ((*it)->getIndex() > size) |
---|
| 103 | size = (*it)->getIndex(); |
---|
[2662] | 104 | if (size == 0) |
---|
| 105 | return 0; |
---|
| 106 | else |
---|
| 107 | return (size + 1); |
---|
[1505] | 108 | } |
---|
| 109 | |
---|
| 110 | std::string ConfigFileSection::getFileEntry() const |
---|
| 111 | { |
---|
[6417] | 112 | if (this->additionalComment_.empty()) |
---|
| 113 | return ('[' + this->name_ + ']'); |
---|
[1505] | 114 | else |
---|
[6417] | 115 | return ('[' + this->name_ + "] " + this->additionalComment_); |
---|
[1505] | 116 | } |
---|
| 117 | |
---|
[6536] | 118 | ConfigFileEntry* ConfigFileSection::getEntry(const std::string& name) const |
---|
[1505] | 119 | { |
---|
[6536] | 120 | for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) |
---|
| 121 | { |
---|
| 122 | if ((*it)->getName() == name) |
---|
| 123 | return *it; |
---|
| 124 | } |
---|
| 125 | return NULL; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | ConfigFileEntry* ConfigFileSection::getEntry(const std::string& name, unsigned int index) const |
---|
| 129 | { |
---|
| 130 | for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) |
---|
| 131 | { |
---|
| 132 | if (((*it)->getName() == name) && ((*it)->getIndex() == index)) |
---|
| 133 | return *it; |
---|
| 134 | } |
---|
| 135 | return NULL; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | std::list<ConfigFileEntry*>::iterator ConfigFileSection::getOrCreateEntryIterator(const std::string& name, const std::string& fallback, bool bString) |
---|
| 139 | { |
---|
[1505] | 140 | for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) |
---|
| 141 | { |
---|
| 142 | if ((*it)->getName() == name) |
---|
| 143 | { |
---|
| 144 | (*it)->setString(bString); |
---|
| 145 | return it; |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | this->bUpdated_ = true; |
---|
| 150 | |
---|
[6536] | 151 | return this->entries_.insert(this->entries_.end(), new ConfigFileEntryValue(name, fallback, bString)); |
---|
[1505] | 152 | } |
---|
| 153 | |
---|
[6536] | 154 | std::list<ConfigFileEntry*>::iterator ConfigFileSection::getOrCreateEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
[1505] | 155 | { |
---|
| 156 | for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) |
---|
| 157 | { |
---|
| 158 | if (((*it)->getName() == name) && ((*it)->getIndex() == index)) |
---|
| 159 | { |
---|
| 160 | (*it)->setString(bString); |
---|
| 161 | return it; |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | this->bUpdated_ = true; |
---|
| 166 | |
---|
| 167 | if (index == 0) |
---|
[6536] | 168 | return this->entries_.insert(this->entries_.end(), new ConfigFileEntryVectorValue(name, index, fallback, bString)); |
---|
[1505] | 169 | else |
---|
[6536] | 170 | return this->entries_.insert(++this->getOrCreateEntryIterator(name, index - 1, "", bString), new ConfigFileEntryVectorValue(name, index, fallback, bString)); |
---|
[1505] | 171 | } |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | //////////////// |
---|
| 175 | // ConfigFile // |
---|
| 176 | //////////////// |
---|
[6536] | 177 | |
---|
| 178 | const char* ConfigFile::DEFAULT_CONFIG_FOLDER = "defaultConfig"; |
---|
| 179 | |
---|
| 180 | ConfigFile::ConfigFile(const std::string& filename, bool bCopyFallbackFile) |
---|
| 181 | : filename_(filename) |
---|
| 182 | , bCopyFallbackFile_(bCopyFallbackFile) |
---|
| 183 | , bUpdated_(false) |
---|
| 184 | { |
---|
| 185 | } |
---|
| 186 | |
---|
[1505] | 187 | ConfigFile::~ConfigFile() |
---|
| 188 | { |
---|
[2103] | 189 | this->clear(); |
---|
[1505] | 190 | } |
---|
| 191 | |
---|
[6536] | 192 | void ConfigFile::load() |
---|
[1505] | 193 | { |
---|
[2710] | 194 | // Be sure we start from new in the memory |
---|
[2103] | 195 | this->clear(); |
---|
[1505] | 196 | |
---|
[6536] | 197 | boost::filesystem::path filepath(this->filename_); |
---|
| 198 | if (!filepath.is_complete()) |
---|
[2710] | 199 | { |
---|
[6536] | 200 | filepath = PathConfig::getConfigPath() / filepath; |
---|
| 201 | if (this->bCopyFallbackFile_) |
---|
[2710] | 202 | { |
---|
[6536] | 203 | // Look for default file in the data folder |
---|
| 204 | if (!boost::filesystem::exists(filepath)) |
---|
| 205 | { |
---|
| 206 | boost::filesystem::path defaultFilepath(PathConfig::getDataPath() / DEFAULT_CONFIG_FOLDER / this->filename_); |
---|
| 207 | if (boost::filesystem::exists(defaultFilepath)) |
---|
| 208 | { |
---|
| 209 | // Try to copy default file from the data folder |
---|
| 210 | try |
---|
| 211 | { |
---|
| 212 | boost::filesystem::copy_file(defaultFilepath, filepath); |
---|
| 213 | COUT(3) << "Copied " << this->filename_ << " from the default config folder." << std::endl; |
---|
| 214 | } |
---|
| 215 | catch (const boost::filesystem::filesystem_error& ex) |
---|
| 216 | { COUT(1) << "Error in ConfigFile: " << ex.what() << std::endl; } |
---|
| 217 | } |
---|
| 218 | } |
---|
[2710] | 219 | } |
---|
| 220 | } |
---|
[2103] | 221 | |
---|
[1505] | 222 | // Open the file |
---|
| 223 | std::ifstream file; |
---|
[2759] | 224 | file.open(filepath.string().c_str(), std::fstream::in); |
---|
[2710] | 225 | if (file.is_open()) |
---|
[1505] | 226 | { |
---|
[2710] | 227 | ConfigFileSection* newsection = 0; |
---|
[1505] | 228 | |
---|
[2710] | 229 | while (file.good() && !file.eof()) |
---|
[1505] | 230 | { |
---|
[3198] | 231 | std::string line; |
---|
| 232 | std::getline(file, line); |
---|
[1505] | 233 | |
---|
[6417] | 234 | const std::string& temp = getStripped(line); |
---|
[2710] | 235 | if (!isEmpty(temp) && !isComment(temp)) |
---|
[1505] | 236 | { |
---|
[2710] | 237 | size_t pos1 = temp.find('['); |
---|
| 238 | if (pos1 == 0) pos1 = line.find('['); else pos1 = std::string::npos; |
---|
| 239 | size_t pos2 = line.find(']'); |
---|
[1505] | 240 | |
---|
[2710] | 241 | if (pos1 != std::string::npos && pos2 != std::string::npos && pos2 > pos1 + 1) |
---|
[1505] | 242 | { |
---|
[2710] | 243 | // New section |
---|
[6417] | 244 | const std::string& comment = line.substr(pos2 + 1); |
---|
[2710] | 245 | if (isComment(comment)) |
---|
| 246 | newsection = new ConfigFileSection(line.substr(pos1 + 1, pos2 - pos1 - 1), comment); |
---|
[1505] | 247 | else |
---|
[2710] | 248 | newsection = new ConfigFileSection(line.substr(pos1 + 1, pos2 - pos1 - 1)); |
---|
| 249 | this->sections_.insert(this->sections_.end(), newsection); |
---|
| 250 | continue; |
---|
| 251 | } |
---|
| 252 | } |
---|
[1505] | 253 | |
---|
[2710] | 254 | if (newsection != 0) |
---|
| 255 | { |
---|
| 256 | if (isComment(line)) |
---|
| 257 | { |
---|
| 258 | // New comment |
---|
| 259 | newsection->getEntries().insert(newsection->getEntries().end(), new ConfigFileEntryComment(removeTrailingWhitespaces(line))); |
---|
| 260 | continue; |
---|
| 261 | } |
---|
| 262 | else |
---|
| 263 | { |
---|
| 264 | size_t pos1 = line.find('='); |
---|
| 265 | |
---|
| 266 | if (pos1 != std::string::npos && pos1 > 0) |
---|
[1505] | 267 | { |
---|
[2710] | 268 | // New entry |
---|
| 269 | size_t pos2 = line.find('['); |
---|
| 270 | size_t pos3 = line.find(']'); |
---|
| 271 | size_t commentposition = getNextCommentPosition(line, pos1 + 1); |
---|
| 272 | while (isBetweenQuotes(line, commentposition)) |
---|
[1505] | 273 | { |
---|
[2710] | 274 | commentposition = getNextCommentPosition(line, commentposition + 1); |
---|
[1505] | 275 | } |
---|
[6417] | 276 | std::string value, comment; |
---|
[2710] | 277 | if (commentposition == std::string::npos) |
---|
| 278 | { |
---|
[7284] | 279 | value = line.substr(pos1 + 1); |
---|
[2710] | 280 | } |
---|
| 281 | else |
---|
| 282 | { |
---|
[7284] | 283 | value = line.substr(pos1 + 1, commentposition - pos1 - 1); |
---|
[2710] | 284 | comment = removeTrailingWhitespaces(line.substr(commentposition)); |
---|
| 285 | } |
---|
| 286 | |
---|
[7284] | 287 | value = removeTrailingWhitespaces(value); |
---|
| 288 | value = removeSlashes(value); |
---|
| 289 | |
---|
[2710] | 290 | if (pos2 != std::string::npos && pos3 != std::string::npos && pos3 > pos2 + 1) |
---|
| 291 | { |
---|
| 292 | // There might be an array index |
---|
| 293 | unsigned int index = 0; |
---|
[3196] | 294 | if (convertValue(&index, line.substr(pos2 + 1, pos3 - pos2 - 1))) |
---|
[2710] | 295 | { |
---|
| 296 | // New array |
---|
[6536] | 297 | std::list<ConfigFileEntry*>::iterator it = newsection->getOrCreateEntryIterator(getStripped(line.substr(0, pos2)), index, value, false); |
---|
[2710] | 298 | (*it)->setValue(value); |
---|
| 299 | (*it)->setComment(comment); |
---|
| 300 | continue; |
---|
| 301 | } |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | // New value |
---|
| 305 | newsection->getEntries().insert(newsection->getEntries().end(), new ConfigFileEntryValue(getStripped(line.substr(0, pos1)), value, false, comment)); |
---|
| 306 | continue; |
---|
[1505] | 307 | } |
---|
| 308 | } |
---|
| 309 | } |
---|
| 310 | } |
---|
| 311 | |
---|
[2710] | 312 | file.close(); |
---|
[1505] | 313 | |
---|
[2710] | 314 | COUT(3) << "Loaded config file \"" << this->filename_ << "\"." << std::endl; |
---|
[1505] | 315 | |
---|
[6536] | 316 | // DO NOT save the file --> we can open supposedly read only config files |
---|
[2710] | 317 | } // end file.is_open() |
---|
[1505] | 318 | } |
---|
| 319 | |
---|
| 320 | void ConfigFile::save() const |
---|
| 321 | { |
---|
[6536] | 322 | this->saveAs(this->filename_); |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | void ConfigFile::saveAs(const std::string& filename) const |
---|
| 326 | { |
---|
| 327 | boost::filesystem::path filepath(filename); |
---|
| 328 | if (!filepath.is_complete()) |
---|
| 329 | filepath = PathConfig::getConfigPath() / filename; |
---|
[1505] | 330 | std::ofstream file; |
---|
[6536] | 331 | file.open(filepath.string().c_str(), std::fstream::out); |
---|
[1505] | 332 | file.setf(std::ios::fixed, std::ios::floatfield); |
---|
| 333 | file.precision(6); |
---|
| 334 | |
---|
| 335 | if (!file.is_open()) |
---|
| 336 | { |
---|
[6536] | 337 | COUT(1) << "Error: Couldn't open config-file \"" << filename << "\"." << std::endl; |
---|
[1505] | 338 | return; |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | for (std::list<ConfigFileSection*>::const_iterator it = this->sections_.begin(); it != this->sections_.end(); ++it) |
---|
| 342 | { |
---|
| 343 | file << (*it)->getFileEntry() << std::endl; |
---|
| 344 | |
---|
| 345 | for (std::list<ConfigFileEntry*>::const_iterator it_entries = (*it)->getEntriesBegin(); it_entries != (*it)->getEntriesEnd(); ++it_entries) |
---|
| 346 | file << (*it_entries)->getFileEntry() << std::endl; |
---|
| 347 | |
---|
| 348 | file << std::endl; |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | file.close(); |
---|
| 352 | |
---|
[6536] | 353 | COUT(4) << "Saved config file \"" << filename << "\"." << std::endl; |
---|
[1505] | 354 | } |
---|
| 355 | |
---|
[6536] | 356 | void ConfigFile::clear() |
---|
[1505] | 357 | { |
---|
[6536] | 358 | for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); ) |
---|
| 359 | delete (*(it++)); |
---|
| 360 | this->sections_.clear(); |
---|
[1505] | 361 | } |
---|
| 362 | |
---|
[6536] | 363 | const std::string& ConfigFile::getOrCreateValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString) |
---|
[1505] | 364 | { |
---|
[6536] | 365 | const std::string& output = this->getOrCreateSection(section)->getOrCreateValue(name, fallback, bString); |
---|
| 366 | this->saveIfUpdated(); |
---|
| 367 | return output; |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | const std::string& ConfigFile::getOrCreateValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
| 371 | { |
---|
| 372 | const std::string& output = this->getOrCreateSection(section)->getOrCreateValue(name, index, fallback, bString); |
---|
| 373 | this->saveIfUpdated(); |
---|
| 374 | return output; |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | void ConfigFile::deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex) |
---|
| 378 | { |
---|
| 379 | if (ConfigFileSection* sectionPtr = this->getSection(section)) |
---|
[1505] | 380 | { |
---|
[6536] | 381 | sectionPtr->deleteVectorEntries(name, startindex); |
---|
| 382 | this->save(); |
---|
[1505] | 383 | } |
---|
| 384 | } |
---|
| 385 | |
---|
[6536] | 386 | ConfigFileSection* ConfigFile::getSection(const std::string& section) const |
---|
[2103] | 387 | { |
---|
[6536] | 388 | for (std::list<ConfigFileSection*>::const_iterator it = this->sections_.begin(); it != this->sections_.end(); ++it) |
---|
| 389 | if ((*it)->getName() == section) |
---|
| 390 | return (*it); |
---|
| 391 | return NULL; |
---|
[2103] | 392 | } |
---|
| 393 | |
---|
[6536] | 394 | ConfigFileSection* ConfigFile::getOrCreateSection(const std::string& section) |
---|
[1505] | 395 | { |
---|
| 396 | for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); ++it) |
---|
| 397 | if ((*it)->getName() == section) |
---|
| 398 | return (*it); |
---|
| 399 | |
---|
| 400 | this->bUpdated_ = true; |
---|
| 401 | |
---|
| 402 | return (*this->sections_.insert(this->sections_.end(), new ConfigFileSection(section))); |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | void ConfigFile::saveIfUpdated() |
---|
| 406 | { |
---|
| 407 | bool sectionsUpdated = false; |
---|
| 408 | |
---|
| 409 | for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); ++it) |
---|
| 410 | { |
---|
| 411 | if ((*it)->bUpdated_) |
---|
| 412 | { |
---|
| 413 | sectionsUpdated = true; |
---|
| 414 | (*it)->bUpdated_ = false; |
---|
| 415 | } |
---|
| 416 | } |
---|
| 417 | |
---|
| 418 | if (this->bUpdated_ || sectionsUpdated) |
---|
| 419 | { |
---|
| 420 | this->bUpdated_ = false; |
---|
| 421 | this->save(); |
---|
| 422 | } |
---|
| 423 | } |
---|
| 424 | |
---|
| 425 | |
---|
[6536] | 426 | //////////////////////// |
---|
| 427 | // SettingsConfigFile // |
---|
| 428 | //////////////////////// |
---|
[2103] | 429 | |
---|
[7284] | 430 | static const std::string __CC_load_name = "reloadSettings"; |
---|
| 431 | static const std::string __CC_setFilename_name = "setSettingsFile"; |
---|
| 432 | static const std::string __CC_config_name = "config"; |
---|
| 433 | static const std::string __CC_tconfig_name = "tconfig"; |
---|
| 434 | static const std::string __CC_getConfig_name = "getConfig"; |
---|
| 435 | |
---|
| 436 | SetConsoleCommand(__CC_load_name, &ConfigFile::load); |
---|
| 437 | SetConsoleCommand(__CC_setFilename_name, &SettingsConfigFile::setFilename); |
---|
| 438 | SetConsoleCommand(__CC_config_name, &SettingsConfigFile::config).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries()).argumentCompleter(2, autocompletion::settingsvalue()); |
---|
| 439 | SetConsoleCommand(__CC_tconfig_name, &SettingsConfigFile::tconfig).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries()).argumentCompleter(2, autocompletion::settingsvalue()); |
---|
| 440 | SetConsoleCommand(__CC_getConfig_name, &SettingsConfigFile::getConfig).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries()); |
---|
| 441 | |
---|
[6536] | 442 | SettingsConfigFile* SettingsConfigFile::singletonPtr_s = 0; |
---|
[2103] | 443 | |
---|
[6536] | 444 | SettingsConfigFile::SettingsConfigFile(const std::string& filename) |
---|
| 445 | : ConfigFile(filename) |
---|
| 446 | { |
---|
[7284] | 447 | ModifyConsoleCommand(__CC_load_name).setObject(this); |
---|
| 448 | ModifyConsoleCommand(__CC_setFilename_name).setObject(this); |
---|
| 449 | ModifyConsoleCommand(__CC_config_name).setObject(this); |
---|
| 450 | ModifyConsoleCommand(__CC_tconfig_name).setObject(this); |
---|
| 451 | ModifyConsoleCommand(__CC_getConfig_name).setObject(this); |
---|
[6536] | 452 | } |
---|
[2103] | 453 | |
---|
[6536] | 454 | SettingsConfigFile::~SettingsConfigFile() |
---|
[1505] | 455 | { |
---|
[7284] | 456 | ModifyConsoleCommand(__CC_load_name).setObject(0); |
---|
| 457 | ModifyConsoleCommand(__CC_setFilename_name).setObject(0); |
---|
| 458 | ModifyConsoleCommand(__CC_config_name).setObject(0); |
---|
| 459 | ModifyConsoleCommand(__CC_tconfig_name).setObject(0); |
---|
| 460 | ModifyConsoleCommand(__CC_getConfig_name).setObject(0); |
---|
[1505] | 461 | } |
---|
| 462 | |
---|
[6536] | 463 | void SettingsConfigFile::load() |
---|
[1505] | 464 | { |
---|
[6536] | 465 | ConfigFile::load(); |
---|
| 466 | this->updateConfigValues(); |
---|
[1505] | 467 | } |
---|
| 468 | |
---|
[6536] | 469 | void SettingsConfigFile::setFilename(const std::string& filename) |
---|
[1505] | 470 | { |
---|
[6536] | 471 | ConfigFileManager::getInstance().setFilename(ConfigFileType::Settings, filename); |
---|
[1505] | 472 | } |
---|
| 473 | |
---|
[6536] | 474 | void SettingsConfigFile::addConfigValueContainer(ConfigValueContainer* container) |
---|
[1505] | 475 | { |
---|
[6536] | 476 | if (container == NULL) |
---|
| 477 | return; |
---|
| 478 | std::pair<std::string, ConfigValueContainer*> second(getLowercase(container->getName()), container); |
---|
| 479 | this->containers_.insert(std::make_pair(getLowercase(container->getSectionName()), second)); |
---|
| 480 | this->sectionNames_.insert(container->getSectionName()); |
---|
[1505] | 481 | } |
---|
| 482 | |
---|
[6536] | 483 | void SettingsConfigFile::removeConfigValueContainer(ConfigValueContainer* container) |
---|
[1505] | 484 | { |
---|
[6536] | 485 | if (container == NULL) |
---|
| 486 | return; |
---|
| 487 | const std::string& sectionLC = getLowercase(container->getSectionName()); |
---|
| 488 | ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC); |
---|
| 489 | for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it) |
---|
| 490 | { |
---|
| 491 | if (it->second.second == container) |
---|
| 492 | { |
---|
| 493 | // Remove entry from section name set this was the last container for that section |
---|
| 494 | if (upper == this->containers_.lower_bound(sectionLC)) |
---|
| 495 | this->sectionNames_.erase(container->getSectionName()); |
---|
| 496 | this->containers_.erase(it); |
---|
| 497 | break; |
---|
| 498 | } |
---|
| 499 | } |
---|
[1505] | 500 | } |
---|
| 501 | |
---|
[6536] | 502 | void SettingsConfigFile::updateConfigValues() |
---|
[1505] | 503 | { |
---|
[6536] | 504 | for (ContainerMap::const_iterator it = this->containers_.begin(); it != this->containers_.end(); ++it) |
---|
| 505 | { |
---|
| 506 | it->second.second->update(); |
---|
| 507 | it->second.second->getIdentifier()->updateConfigValues(); |
---|
| 508 | } |
---|
[1505] | 509 | } |
---|
| 510 | |
---|
[6536] | 511 | void SettingsConfigFile::clean(bool bCleanComments) |
---|
[1505] | 512 | { |
---|
[6536] | 513 | for (std::list<ConfigFileSection*>::iterator itSection = this->sections_.begin(); itSection != this->sections_.end(); ) |
---|
| 514 | { |
---|
| 515 | const std::string& sectionLC = getLowercase((*itSection)->getName()); |
---|
| 516 | ContainerMap::const_iterator lower = this->containers_.lower_bound(sectionLC); |
---|
| 517 | ContainerMap::const_iterator upper = this->containers_.upper_bound(sectionLC); |
---|
| 518 | if (lower != upper) |
---|
| 519 | { |
---|
| 520 | // The section exists, delete comment |
---|
| 521 | if (bCleanComments) |
---|
| 522 | (*itSection)->setComment(""); |
---|
| 523 | for (std::list<ConfigFileEntry*>::iterator itEntry = (*itSection)->entries_.begin(); itEntry != (*itSection)->entries_.end(); ) |
---|
| 524 | { |
---|
| 525 | const std::string& entryLC = getLowercase((*itEntry)->getName()); |
---|
| 526 | bool bFound = false; |
---|
| 527 | for (ContainerMap::const_iterator itContainer = lower; itContainer != upper; ++itContainer) |
---|
| 528 | { |
---|
| 529 | if (itContainer->second.first == entryLC) |
---|
| 530 | { |
---|
| 531 | // The config-value exists, delete comment |
---|
| 532 | if (bCleanComments) |
---|
| 533 | (*itEntry)->setComment(""); |
---|
| 534 | ++itEntry; |
---|
| 535 | bFound = true; |
---|
| 536 | break; |
---|
| 537 | } |
---|
| 538 | } |
---|
| 539 | if (!bFound) |
---|
| 540 | { |
---|
| 541 | // The config-value doesn't exist |
---|
| 542 | delete (*itEntry); |
---|
| 543 | (*itSection)->entries_.erase(itEntry++); |
---|
| 544 | } |
---|
| 545 | } |
---|
| 546 | ++itSection; |
---|
| 547 | } |
---|
| 548 | else |
---|
| 549 | { |
---|
| 550 | // The section doesn't exist |
---|
| 551 | delete (*itSection); |
---|
| 552 | this->sections_.erase(itSection++); |
---|
| 553 | } |
---|
| 554 | } |
---|
| 555 | |
---|
| 556 | // Save the file |
---|
| 557 | this->save(); |
---|
[1505] | 558 | } |
---|
| 559 | |
---|
[7284] | 560 | void SettingsConfigFile::config(const std::string& section, const std::string& entry, const std::string& value) |
---|
[1505] | 561 | { |
---|
[7284] | 562 | if (!this->configImpl(section, entry, value, &ConfigValueContainer::set)) |
---|
| 563 | COUT(1) << "Error: Config value \"" << entry << "\" in section \"" << section << "\" doesn't exist." << std::endl; |
---|
[1505] | 564 | } |
---|
| 565 | |
---|
[7284] | 566 | void SettingsConfigFile::tconfig(const std::string& section, const std::string& entry, const std::string& value) |
---|
[1505] | 567 | { |
---|
[7284] | 568 | if (!this->configImpl(section, entry, value, &ConfigValueContainer::tset)) |
---|
| 569 | COUT(1) << "Error: Config value \"" << entry << "\" in section \"" << section << "\" doesn't exist." << std::endl; |
---|
[1505] | 570 | } |
---|
| 571 | |
---|
[6536] | 572 | bool SettingsConfigFile::configImpl(const std::string& section, const std::string& entry, const std::string& value, bool (ConfigValueContainer::*function)(const MultiType&)) |
---|
[1505] | 573 | { |
---|
[6536] | 574 | const std::string& sectionLC = getLowercase(section); |
---|
| 575 | const std::string& entryLC = getLowercase(entry); |
---|
| 576 | ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC); |
---|
| 577 | for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it) |
---|
| 578 | { |
---|
| 579 | // Note: Config value vectors cannot be supported |
---|
| 580 | if (it->second.first == entryLC && !it->second.second->isVector()) |
---|
| 581 | { |
---|
| 582 | return (it->second.second->*function)(value); |
---|
| 583 | } |
---|
| 584 | } |
---|
| 585 | return false; |
---|
[1505] | 586 | } |
---|
| 587 | |
---|
[6536] | 588 | std::string SettingsConfigFile::getConfig(const std::string& section, const std::string& entry) |
---|
[1505] | 589 | { |
---|
[6536] | 590 | const std::string& sectionLC = getLowercase(section); |
---|
| 591 | const std::string& entryLC = getLowercase(entry); |
---|
| 592 | ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC); |
---|
| 593 | for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it) |
---|
| 594 | { |
---|
| 595 | // Note: Config value vectors cannot be supported |
---|
| 596 | if (it->second.first == entryLC && ! it->second.second->isVector()) |
---|
| 597 | { |
---|
| 598 | std::string value; |
---|
| 599 | it->second.second->getValue<std::string, OrxonoxClass>(&value, NULL); |
---|
| 600 | return value; |
---|
| 601 | } |
---|
| 602 | } |
---|
| 603 | return ""; |
---|
[1505] | 604 | } |
---|
| 605 | |
---|
[6536] | 606 | |
---|
| 607 | /////////////////////// |
---|
| 608 | // ConfigFileManager // |
---|
| 609 | /////////////////////// |
---|
| 610 | |
---|
| 611 | ConfigFileManager* ConfigFileManager::singletonPtr_s = 0; |
---|
| 612 | |
---|
| 613 | ConfigFileManager::ConfigFileManager() |
---|
[1505] | 614 | { |
---|
[6536] | 615 | this->configFiles_.assign(NULL); |
---|
[1505] | 616 | } |
---|
| 617 | |
---|
[6536] | 618 | ConfigFileManager::~ConfigFileManager() |
---|
[1505] | 619 | { |
---|
[6536] | 620 | for (boost::array<ConfigFile*, 3>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it) |
---|
| 621 | if (*it) |
---|
| 622 | delete (*it); |
---|
[1505] | 623 | } |
---|
| 624 | |
---|
[6536] | 625 | void ConfigFileManager::setFilename(ConfigFileType::Value type, const std::string& filename) |
---|
[1505] | 626 | { |
---|
[6536] | 627 | if (this->getConfigFile(type)) |
---|
| 628 | delete this->configFiles_[type]; |
---|
| 629 | // Create and load config file |
---|
| 630 | switch (type) |
---|
[2103] | 631 | { |
---|
[6536] | 632 | case ConfigFileType::Settings: |
---|
| 633 | this->configFiles_[type] = new SettingsConfigFile(filename); |
---|
| 634 | break; |
---|
| 635 | case ConfigFileType::JoyStickCalibration: |
---|
| 636 | case ConfigFileType::CommandHistory: |
---|
| 637 | this->configFiles_[type] = new ConfigFile(filename); |
---|
| 638 | break; |
---|
[2103] | 639 | } |
---|
[6536] | 640 | this->configFiles_[type]->load(); |
---|
[1505] | 641 | } |
---|
| 642 | } |
---|