Changeset 6536
- Timestamp:
- Mar 16, 2010, 11:22:36 AM (15 years ago)
- Location:
- code/trunk
- Files:
-
- 23 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/gamestate (added) merged: 6430-6432,6437,6439-6440
- Property svn:mergeinfo changed
-
code/trunk/data/lua/LuaStateInit.lua
r6417 r6536 58 58 orxonox.CommandExecutor:execute(command) 59 59 end 60 61 -- Convenience function for config values 62 orxonox.getConfig = function(section, entry) 63 return orxonox.SettingsConfigFile:getInstance():getConfig(section, entry) 64 end 65 orxonox.config = function(section, entry, value) 66 return orxonox.SettingsConfigFile:getInstance():config(section, entry, value) 67 end 68 orxonox.tconfig = function(section, entry, value) 69 return orxonox.SettingsConfigFile:getInstance():tconfig(section, entry, value) 70 end -
code/trunk/src/libraries/core/ArgumentCompletionFunctions.cc
r6417 r6536 36 36 #include "util/StringUtils.h" 37 37 #include "Identifier.h" 38 #include "ConfigFileManager.h" 38 39 #include "ConfigValueContainer.h" 39 40 #include "TclThreadManager.h" … … 96 97 } 97 98 98 ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION( configvalueclasses)()99 ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(settingssections)() 99 100 { 100 ArgumentCompletionList classlist;101 ArgumentCompletionList sectionList; 101 102 102 for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getStringIdentifierMapBegin(); it != Identifier::getStringIdentifierMapEnd(); ++it)103 if (it->second->hasConfigValues())104 classlist.push_back(ArgumentCompletionListElement(it->first, getLowercase(it->first)));103 const std::set<std::string>& names = SettingsConfigFile::getInstance().getSectionNames(); 104 for (std::set<std::string>::const_iterator it = names.begin(); it != names.end(); ++it) 105 sectionList.push_back(ArgumentCompletionListElement(*it, getLowercase(*it))); 105 106 106 return classlist;107 return sectionList; 107 108 } 108 109 109 ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION( configvalues)(const std::string& fragment, const std::string& classname)110 ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(settingsentries)(const std::string& fragment, const std::string& section) 110 111 { 111 ArgumentCompletionList configvalues; 112 std::map<std::string, Identifier*>::const_iterator identifier = Identifier::getLowercaseStringIdentifierMap().find(getLowercase(classname)); 112 ArgumentCompletionList entryList; 113 SettingsConfigFile& settings = SettingsConfigFile::getInstance(); 114 const std::string& sectionLC = getLowercase(section); 113 115 114 if (identifier != Identifier::getLowercaseStringIdentifierMapEnd() && identifier->second->hasConfigValues()) 116 SettingsConfigFile::ContainerMap::const_iterator upper = settings.getContainerUpperBound(sectionLC); 117 for (SettingsConfigFile::ContainerMap::const_iterator it = settings.getContainerLowerBound(sectionLC); it != upper; ++it) 118 entryList.push_back(ArgumentCompletionListElement(it->second.second->getName(), it->second.first)); 119 120 return entryList; 121 } 122 123 ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(settingsvalue)(const std::string& fragment, const std::string& entry, const std::string& section) 124 { 125 ArgumentCompletionList oldValue; 126 SettingsConfigFile& settings = SettingsConfigFile::getInstance(); 127 const std::string& sectionLC = getLowercase(section); 128 const std::string& entryLC = getLowercase(entry); 129 130 SettingsConfigFile::ContainerMap::const_iterator upper = settings.getContainerUpperBound(sectionLC); 131 for (SettingsConfigFile::ContainerMap::const_iterator it = settings.getContainerLowerBound(sectionLC); it != upper; ++it) 115 132 { 116 for (std::map<std::string, ConfigValueContainer*>::const_iterator it = identifier->second->getConfigValueMapBegin(); it != identifier->second->getConfigValueMapEnd(); ++it) 117 configvalues.push_back(ArgumentCompletionListElement(it->first, getLowercase(it->first))); 133 if (it->second.first == entryLC) 134 { 135 const std::string& valuestring = it->second.second->toString(); 136 oldValue.push_back(ArgumentCompletionListElement(valuestring, getLowercase(valuestring), "Old value: " + valuestring)); 137 } 118 138 } 119 139 120 return configvalues; 121 } 122 123 ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(configvalue)(const std::string& fragment, const std::string& varname, const std::string& classname) 124 { 125 ArgumentCompletionList oldvalue; 126 std::map<std::string, Identifier*>::const_iterator identifier = Identifier::getLowercaseStringIdentifierMap().find(getLowercase(classname)); 127 if (identifier != Identifier::getLowercaseStringIdentifierMapEnd()) 128 { 129 std::map<std::string, ConfigValueContainer*>::const_iterator variable = identifier->second->getLowercaseConfigValueMap().find(getLowercase(varname)); 130 if (variable != identifier->second->getLowercaseConfigValueMapEnd()) 131 { 132 const std::string& valuestring = variable->second->toString(); 133 oldvalue.push_back(ArgumentCompletionListElement(valuestring, getLowercase(valuestring), "Old value: " + valuestring)); 134 } 135 } 136 return oldvalue; 140 return oldValue; 137 141 } 138 142 -
code/trunk/src/libraries/core/ArgumentCompletionFunctions.h
r5781 r6536 54 54 ARGUMENT_COMPLETION_FUNCTION_DECLARATION(fallback)(); 55 55 ARGUMENT_COMPLETION_FUNCTION_DECLARATION(files)(const std::string& fragment); 56 ARGUMENT_COMPLETION_FUNCTION_DECLARATION( configvalueclasses)();57 ARGUMENT_COMPLETION_FUNCTION_DECLARATION( configvalues)(const std::string& fragment, const std::string& classname);58 ARGUMENT_COMPLETION_FUNCTION_DECLARATION( configvalue)(const std::string& fragment, const std::string& varname, const std::string& classname);56 ARGUMENT_COMPLETION_FUNCTION_DECLARATION(settingssections)(); 57 ARGUMENT_COMPLETION_FUNCTION_DECLARATION(settingsentries)(const std::string& fragment, const std::string& section); 58 ARGUMENT_COMPLETION_FUNCTION_DECLARATION(settingsvalue)(const std::string& fragment, const std::string& entry, const std::string& section); 59 59 ARGUMENT_COMPLETION_FUNCTION_DECLARATION(tclthreads)(); 60 60 } -
code/trunk/src/libraries/core/ConfigFileManager.cc
r6425 r6536 33 33 #include "util/Convert.h" 34 34 #include "util/Math.h" 35 #include "util/StringUtils.h"36 35 #include "ConsoleCommand.h" 37 36 #include "ConfigValueContainer.h" … … 40 39 namespace orxonox 41 40 { 42 SetConsoleCommandShortcutExtern(config).argumentCompleter(0, autocompletion::configvalueclasses()).argumentCompleter(1, autocompletion::configvalues()).argumentCompleter(2, autocompletion::configvalue());43 SetConsoleCommandShortcutExtern(tconfig).argumentCompleter(0, autocompletion::configvalueclasses()).argumentCompleter(1, autocompletion::configvalues()).argumentCompleter(2, autocompletion::configvalue());44 SetConsoleCommandShortcutExtern(reloadConfig);45 SetConsoleCommandShortcutExtern(cleanConfig);46 SetConsoleCommandShortcutExtern(loadSettings).argumentCompleter(0, autocompletion::files());47 48 bool config(const std::string& classname, const std::string& varname, const std::string& value)49 {50 std::map<std::string, Identifier*>::const_iterator identifier = Identifier::getLowercaseStringIdentifierMap().find(getLowercase(classname));51 if (identifier != Identifier::getLowercaseStringIdentifierMapEnd())52 {53 std::map<std::string, ConfigValueContainer*>::const_iterator variable = identifier->second->getLowercaseConfigValueMap().find(getLowercase(varname));54 if (variable != identifier->second->getLowercaseConfigValueMapEnd())55 return variable->second->set(value);56 }57 return false;58 }59 60 const std::string& getConfig(const std::string& classname, const std::string& varname)61 {62 return ConfigFileManager::getInstance().getValue(ConfigFileType::Settings, classname, varname, "", true);63 }64 65 bool tconfig(const std::string& classname, const std::string& varname, const std::string& value)66 {67 std::map<std::string, Identifier*>::const_iterator identifier = Identifier::getLowercaseStringIdentifierMap().find(getLowercase(classname));68 if (identifier != Identifier::getLowercaseStringIdentifierMapEnd())69 {70 std::map<std::string, ConfigValueContainer*>::const_iterator variable = identifier->second->getLowercaseConfigValueMap().find(getLowercase(varname));71 if (variable != identifier->second->getLowercaseConfigValueMapEnd())72 return variable->second->tset(value);73 }74 return false;75 }76 77 void reloadConfig()78 {79 ConfigFileManager::getInstance().load();80 }81 82 void cleanConfig()83 {84 ConfigFileManager::getInstance().clean(false);85 }86 87 void loadSettings(const std::string& filename)88 {89 ConfigFileManager::getInstance().setFilename(ConfigFileType::Settings, filename);90 }91 92 41 ////////////////////////// 93 42 // ConfigFileEntryValue // … … 101 50 // Assemble the entry line 102 51 this->fileEntry_ = this->getKeyString() + " = "; 103 if (this->bString_ )52 if (this->bString_ && !this->value_.empty()) 104 53 this->fileEntry_ += '"' + addSlashes(this->value_) + '"'; 105 54 else … … 145 94 } 146 95 147 unsigned int ConfigFileSection::getVectorSize(const std::string& name) 96 unsigned int ConfigFileSection::getVectorSize(const std::string& name) const 148 97 { 149 98 unsigned int size = 0; … … 166 115 } 167 116 168 std::list<ConfigFileEntry*>::iterator ConfigFileSection::getEntryIterator(const std::string& name, const std::string& fallback, bool bString) 117 ConfigFileEntry* ConfigFileSection::getEntry(const std::string& name) const 118 { 119 for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) 120 { 121 if ((*it)->getName() == name) 122 return *it; 123 } 124 return NULL; 125 } 126 127 ConfigFileEntry* ConfigFileSection::getEntry(const std::string& name, unsigned int index) const 128 { 129 for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) 130 { 131 if (((*it)->getName() == name) && ((*it)->getIndex() == index)) 132 return *it; 133 } 134 return NULL; 135 } 136 137 std::list<ConfigFileEntry*>::iterator ConfigFileSection::getOrCreateEntryIterator(const std::string& name, const std::string& fallback, bool bString) 169 138 { 170 139 for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) … … 179 148 this->bUpdated_ = true; 180 149 181 return this->entries_.insert(this->entries_.end(), static_cast<ConfigFileEntry*>(new ConfigFileEntryValue(name, fallback, bString)));182 } 183 184 std::list<ConfigFileEntry*>::iterator ConfigFileSection::get EntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString)150 return this->entries_.insert(this->entries_.end(), new ConfigFileEntryValue(name, fallback, bString)); 151 } 152 153 std::list<ConfigFileEntry*>::iterator ConfigFileSection::getOrCreateEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString) 185 154 { 186 155 for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) … … 196 165 197 166 if (index == 0) 198 return this->entries_.insert(this->entries_.end(), static_cast<ConfigFileEntry*>(new ConfigFileEntryVectorValue(name, index, fallback, bString)));167 return this->entries_.insert(this->entries_.end(), new ConfigFileEntryVectorValue(name, index, fallback, bString)); 199 168 else 200 return this->entries_.insert(++this->get EntryIterator(name, index - 1, "", bString), static_cast<ConfigFileEntry*>(new ConfigFileEntryVectorValue(name, index, fallback, bString)));169 return this->entries_.insert(++this->getOrCreateEntryIterator(name, index - 1, "", bString), new ConfigFileEntryVectorValue(name, index, fallback, bString)); 201 170 } 202 171 … … 205 174 // ConfigFile // 206 175 //////////////// 176 177 const char* ConfigFile::DEFAULT_CONFIG_FOLDER = "defaultConfig"; 178 179 ConfigFile::ConfigFile(const std::string& filename, bool bCopyFallbackFile) 180 : filename_(filename) 181 , bCopyFallbackFile_(bCopyFallbackFile) 182 , bUpdated_(false) 183 { 184 } 185 207 186 ConfigFile::~ConfigFile() 208 187 { … … 210 189 } 211 190 212 void ConfigFile::load( bool bCreateIfNotExisting)191 void ConfigFile::load() 213 192 { 214 193 // Be sure we start from new in the memory 215 194 this->clear(); 216 195 217 // Get default file if necessary and available 218 boost::filesystem::path filepath(PathConfig::getConfigPath() / this->filename_); 219 if (!boost::filesystem::exists(filepath)) 220 { 221 // Try to get default one from the data folder 222 boost::filesystem::path defaultFilepath(PathConfig::getDataPath() / "defaultConfig" / this->filename_); 223 if (boost::filesystem::exists(defaultFilepath)) 224 { 225 COUT(3) << "Copied " << this->filename_ << " from the defaultConfig folder." << std::endl; 226 boost::filesystem::copy_file(defaultFilepath, filepath); 196 boost::filesystem::path filepath(this->filename_); 197 if (!filepath.is_complete()) 198 { 199 filepath = PathConfig::getConfigPath() / filepath; 200 if (this->bCopyFallbackFile_) 201 { 202 // Look for default file in the data folder 203 if (!boost::filesystem::exists(filepath)) 204 { 205 boost::filesystem::path defaultFilepath(PathConfig::getDataPath() / DEFAULT_CONFIG_FOLDER / this->filename_); 206 if (boost::filesystem::exists(defaultFilepath)) 207 { 208 // Try to copy default file from the data folder 209 try 210 { 211 boost::filesystem::copy_file(defaultFilepath, filepath); 212 COUT(3) << "Copied " << this->filename_ << " from the default config folder." << std::endl; 213 } 214 catch (const boost::filesystem::filesystem_error& ex) 215 { COUT(1) << "Error in ConfigFile: " << ex.what() << std::endl; } 216 } 217 } 227 218 } 228 219 } … … 300 291 { 301 292 // New array 302 std::list<ConfigFileEntry*>::iterator it = newsection->get EntryIterator(getStripped(line.substr(0, pos2)), index, value, false);293 std::list<ConfigFileEntry*>::iterator it = newsection->getOrCreateEntryIterator(getStripped(line.substr(0, pos2)), index, value, false); 303 294 (*it)->setValue(value); 304 295 (*it)->setComment(comment); … … 319 310 COUT(3) << "Loaded config file \"" << this->filename_ << "\"." << std::endl; 320 311 321 // Save the file in case something changed (like stripped whitespaces) 322 this->save(); 323 324 // Update all ConfigValueContainers 325 this->updateConfigValues(); 312 // DO NOT save the file --> we can open supposedly read only config files 326 313 } // end file.is_open() 327 314 } … … 329 316 void ConfigFile::save() const 330 317 { 318 this->saveAs(this->filename_); 319 } 320 321 void ConfigFile::saveAs(const std::string& filename) const 322 { 323 boost::filesystem::path filepath(filename); 324 if (!filepath.is_complete()) 325 filepath = PathConfig::getConfigPath() / filename; 331 326 std::ofstream file; 332 file.open( (PathConfig::getConfigPathString() + filename_).c_str(), std::fstream::out);327 file.open(filepath.string().c_str(), std::fstream::out); 333 328 file.setf(std::ios::fixed, std::ios::floatfield); 334 329 file.precision(6); … … 336 331 if (!file.is_open()) 337 332 { 338 COUT(1) << "An error occurred in ConfigFileManager.cc:" << std::endl; 339 COUT(1) << "Error: Couldn't open config-file \"" << this->filename_ << "\"." << std::endl; 333 COUT(1) << "Error: Couldn't open config-file \"" << filename << "\"." << std::endl; 340 334 return; 341 335 } … … 346 340 347 341 for (std::list<ConfigFileEntry*>::const_iterator it_entries = (*it)->getEntriesBegin(); it_entries != (*it)->getEntriesEnd(); ++it_entries) 348 {349 342 file << (*it_entries)->getFileEntry() << std::endl; 350 }351 343 352 344 file << std::endl; … … 355 347 file.close(); 356 348 357 COUT(4) << "Saved config file \"" << this->filename_ << "\"." << std::endl; 358 } 359 360 void ConfigFile::saveAs(const std::string& filename) 361 { 362 std::string temp = this->filename_; 363 this->filename_ = filename; 364 this->save(); 365 this->filename_ = temp; 366 } 367 368 void ConfigFile::clean(bool bCleanComments) 369 { 370 for (std::list<ConfigFileSection*>::iterator it1 = this->sections_.begin(); it1 != this->sections_.end(); ) 371 { 372 std::map<std::string, Identifier*>::const_iterator it2 = Identifier::getStringIdentifierMap().find((*it1)->getName()); 373 if (it2 != Identifier::getStringIdentifierMapEnd() && it2->second->hasConfigValues()) 374 { 375 // The section exists, delete comment 376 if (bCleanComments) 377 (*it1)->setComment(""); 378 for (std::list<ConfigFileEntry*>::iterator it3 = (*it1)->entries_.begin(); it3 != (*it1)->entries_.end(); ) 379 { 380 std::map<std::string, ConfigValueContainer*>::const_iterator it4 = it2->second->getConfigValueMap().find((*it3)->getName()); 381 if (it4 != it2->second->getConfigValueMapEnd()) 382 { 383 // The config-value exists, delete comment 384 if (bCleanComments) 385 (*it3)->setComment(""); 386 ++it3; 387 } 388 else 389 { 390 // The config-value doesn't exist 391 delete (*it3); 392 (*it1)->entries_.erase(it3++); 393 } 394 } 395 ++it1; 396 } 397 else 398 { 399 // The section doesn't exist 400 delete (*it1); 401 this->sections_.erase(it1++); 402 } 403 } 404 405 // Save the file 406 this->save(); 349 COUT(4) << "Saved config file \"" << filename << "\"." << std::endl; 407 350 } 408 351 … … 414 357 } 415 358 416 ConfigFileSection* ConfigFile::getSection(const std::string& section) 359 const std::string& ConfigFile::getOrCreateValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString) 360 { 361 const std::string& output = this->getOrCreateSection(section)->getOrCreateValue(name, fallback, bString); 362 this->saveIfUpdated(); 363 return output; 364 } 365 366 const std::string& ConfigFile::getOrCreateValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString) 367 { 368 const std::string& output = this->getOrCreateSection(section)->getOrCreateValue(name, index, fallback, bString); 369 this->saveIfUpdated(); 370 return output; 371 } 372 373 void ConfigFile::deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex) 374 { 375 if (ConfigFileSection* sectionPtr = this->getSection(section)) 376 { 377 sectionPtr->deleteVectorEntries(name, startindex); 378 this->save(); 379 } 380 } 381 382 ConfigFileSection* ConfigFile::getSection(const std::string& section) const 383 { 384 for (std::list<ConfigFileSection*>::const_iterator it = this->sections_.begin(); it != this->sections_.end(); ++it) 385 if ((*it)->getName() == section) 386 return (*it); 387 return NULL; 388 } 389 390 ConfigFileSection* ConfigFile::getOrCreateSection(const std::string& section) 417 391 { 418 392 for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); ++it) … … 445 419 } 446 420 447 void ConfigFile::updateConfigValues() 448 { 449 if (this->type_ == ConfigFileType::Settings) 450 { 451 for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getStringIdentifierMapBegin(); it != Identifier::getStringIdentifierMapEnd(); ++it) 452 { 453 if (it->second->hasConfigValues()) 421 422 //////////////////////// 423 // SettingsConfigFile // 424 //////////////////////// 425 426 SettingsConfigFile* SettingsConfigFile::singletonPtr_s = 0; 427 428 SettingsConfigFile::SettingsConfigFile(const std::string& filename) 429 : ConfigFile(filename) 430 { 431 ConsoleCommand* command = createConsoleCommand(createFunctor(&ConfigFile::load, this), "reloadSettings"); 432 CommandExecutor::addConsoleCommandShortcut(command); 433 command = createConsoleCommand(createFunctor(&SettingsConfigFile::setFilename, this), "setSettingsFile"); 434 CommandExecutor::addConsoleCommandShortcut(command); 435 command = createConsoleCommand(createFunctor(&SettingsConfigFile::config, this), "config"); 436 CommandExecutor::addConsoleCommandShortcut(command).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries()).argumentCompleter(2, autocompletion::settingsvalue()); 437 command = createConsoleCommand(createFunctor(&SettingsConfigFile::tconfig, this), "tconfig"); 438 CommandExecutor::addConsoleCommandShortcut(command).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries()).argumentCompleter(2, autocompletion::settingsvalue()); 439 command = createConsoleCommand(createFunctor(&SettingsConfigFile::getConfig, this), "getConfig"); 440 CommandExecutor::addConsoleCommandShortcut(command).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries()); 441 } 442 443 SettingsConfigFile::~SettingsConfigFile() 444 { 445 } 446 447 void SettingsConfigFile::load() 448 { 449 ConfigFile::load(); 450 this->updateConfigValues(); 451 } 452 453 void SettingsConfigFile::setFilename(const std::string& filename) 454 { 455 ConfigFileManager::getInstance().setFilename(ConfigFileType::Settings, filename); 456 } 457 458 void SettingsConfigFile::addConfigValueContainer(ConfigValueContainer* container) 459 { 460 if (container == NULL) 461 return; 462 std::pair<std::string, ConfigValueContainer*> second(getLowercase(container->getName()), container); 463 this->containers_.insert(std::make_pair(getLowercase(container->getSectionName()), second)); 464 this->sectionNames_.insert(container->getSectionName()); 465 } 466 467 void SettingsConfigFile::removeConfigValueContainer(ConfigValueContainer* container) 468 { 469 if (container == NULL) 470 return; 471 const std::string& sectionLC = getLowercase(container->getSectionName()); 472 ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC); 473 for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it) 474 { 475 if (it->second.second == container) 476 { 477 // Remove entry from section name set this was the last container for that section 478 if (upper == this->containers_.lower_bound(sectionLC)) 479 this->sectionNames_.erase(container->getSectionName()); 480 this->containers_.erase(it); 481 break; 482 } 483 } 484 } 485 486 void SettingsConfigFile::updateConfigValues() 487 { 488 for (ContainerMap::const_iterator it = this->containers_.begin(); it != this->containers_.end(); ++it) 489 { 490 it->second.second->update(); 491 it->second.second->getIdentifier()->updateConfigValues(); 492 } 493 } 494 495 void SettingsConfigFile::clean(bool bCleanComments) 496 { 497 for (std::list<ConfigFileSection*>::iterator itSection = this->sections_.begin(); itSection != this->sections_.end(); ) 498 { 499 const std::string& sectionLC = getLowercase((*itSection)->getName()); 500 ContainerMap::const_iterator lower = this->containers_.lower_bound(sectionLC); 501 ContainerMap::const_iterator upper = this->containers_.upper_bound(sectionLC); 502 if (lower != upper) 503 { 504 // The section exists, delete comment 505 if (bCleanComments) 506 (*itSection)->setComment(""); 507 for (std::list<ConfigFileEntry*>::iterator itEntry = (*itSection)->entries_.begin(); itEntry != (*itSection)->entries_.end(); ) 454 508 { 455 for (std::map<std::string, ConfigValueContainer*>::const_iterator it2 = it->second->getConfigValueMapBegin(); it2 != it->second->getConfigValueMapEnd(); ++it2) 456 it2->second->update(); 457 458 it->second->updateConfigValues(); 509 const std::string& entryLC = getLowercase((*itEntry)->getName()); 510 bool bFound = false; 511 for (ContainerMap::const_iterator itContainer = lower; itContainer != upper; ++itContainer) 512 { 513 if (itContainer->second.first == entryLC) 514 { 515 // The config-value exists, delete comment 516 if (bCleanComments) 517 (*itEntry)->setComment(""); 518 ++itEntry; 519 bFound = true; 520 break; 521 } 522 } 523 if (!bFound) 524 { 525 // The config-value doesn't exist 526 delete (*itEntry); 527 (*itSection)->entries_.erase(itEntry++); 528 } 459 529 } 460 } 461 } 530 ++itSection; 531 } 532 else 533 { 534 // The section doesn't exist 535 delete (*itSection); 536 this->sections_.erase(itSection++); 537 } 538 } 539 540 // Save the file 541 this->save(); 542 } 543 544 bool SettingsConfigFile::config(const std::string& section, const std::string& entry, const std::string& value) 545 { 546 return this->configImpl(section, entry, value, &ConfigValueContainer::set); 547 } 548 549 bool SettingsConfigFile::tconfig(const std::string& section, const std::string& entry, const std::string& value) 550 { 551 return this->configImpl(section, entry, value, &ConfigValueContainer::tset); 552 } 553 554 bool SettingsConfigFile::configImpl(const std::string& section, const std::string& entry, const std::string& value, bool (ConfigValueContainer::*function)(const MultiType&)) 555 { 556 const std::string& sectionLC = getLowercase(section); 557 const std::string& entryLC = getLowercase(entry); 558 ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC); 559 for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it) 560 { 561 // Note: Config value vectors cannot be supported 562 if (it->second.first == entryLC && !it->second.second->isVector()) 563 { 564 return (it->second.second->*function)(value); 565 } 566 } 567 return false; 568 } 569 570 std::string SettingsConfigFile::getConfig(const std::string& section, const std::string& entry) 571 { 572 const std::string& sectionLC = getLowercase(section); 573 const std::string& entryLC = getLowercase(entry); 574 ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC); 575 for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it) 576 { 577 // Note: Config value vectors cannot be supported 578 if (it->second.first == entryLC && ! it->second.second->isVector()) 579 { 580 std::string value; 581 it->second.second->getValue<std::string, OrxonoxClass>(&value, NULL); 582 return value; 583 } 584 } 585 return ""; 462 586 } 463 587 … … 469 593 ConfigFileManager* ConfigFileManager::singletonPtr_s = 0; 470 594 471 std::string ConfigFileManager::DEFAULT_CONFIG_FILE = "default.ini";472 473 595 ConfigFileManager::ConfigFileManager() 474 : mininmalFreeType_(ConfigFileType::numberOfReservedTypes)475 {596 { 597 this->configFiles_.assign(NULL); 476 598 } 477 599 478 600 ConfigFileManager::~ConfigFileManager() 479 601 { 480 for (std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ) 481 delete (it++)->second; 482 } 483 484 void ConfigFileManager::setFilename(ConfigFileType type, const std::string& filename) 485 { 486 std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type); 487 if (it != this->configFiles_.end()) 488 { 489 assert(it->second); 490 delete it->second; 491 } 492 this->configFiles_[type] = new ConfigFile(filename, type); 493 this->load(type); 494 } 495 496 void ConfigFileManager::load() 497 { 498 for (std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it) 499 it->second->load(); 500 } 501 502 void ConfigFileManager::save() 503 { 504 for (std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it) 505 it->second->save(); 506 } 507 508 void ConfigFileManager::clean(bool bCleanComments) 509 { 510 for (std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it) 511 this->clean(it->first, bCleanComments); 512 } 513 514 void ConfigFileManager::load(ConfigFileType type) 515 { 516 this->getFile(type)->load(); 517 } 518 519 void ConfigFileManager::save(ConfigFileType type) 520 { 521 this->getFile(type)->save(); 522 } 523 524 void ConfigFileManager::saveAs(ConfigFileType type, const std::string& saveFilename) 525 { 526 this->getFile(type)->saveAs(saveFilename); 527 } 528 529 void ConfigFileManager::clean(ConfigFileType type, bool bCleanComments) 530 { 531 this->getFile(type)->clean(bCleanComments); 532 } 533 534 void ConfigFileManager::updateConfigValues() 535 { 536 for (std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it) 537 it->second->updateConfigValues(); 538 } 539 540 void ConfigFileManager::updateConfigValues(ConfigFileType type) 541 { 542 this->getFile(type)->updateConfigValues(); 543 } 544 545 const std::string& ConfigFileManager::getFilename(ConfigFileType type) 546 { 547 std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type); 548 if (it != this->configFiles_.end()) 549 return it->second->getFilename(); 550 else 551 return BLANKSTRING; 552 } 553 554 ConfigFile* ConfigFileManager::getFile(ConfigFileType type) 555 { 556 std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type); 557 if (it != this->configFiles_.end()) 558 return it->second; 559 else 560 { 561 COUT(1) << "ConfigFileManager: Can't find a config file for type with ID " << static_cast<int>(type) << std::endl; 562 COUT(1) << "Using " << DEFAULT_CONFIG_FILE << " file." << std::endl; 563 this->setFilename(type, DEFAULT_CONFIG_FILE); 564 return getFile(type); 565 } 602 for (boost::array<ConfigFile*, 3>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it) 603 if (*it) 604 delete (*it); 605 } 606 607 void ConfigFileManager::setFilename(ConfigFileType::Value type, const std::string& filename) 608 { 609 if (this->getConfigFile(type)) 610 delete this->configFiles_[type]; 611 // Create and load config file 612 switch (type) 613 { 614 case ConfigFileType::Settings: 615 this->configFiles_[type] = new SettingsConfigFile(filename); 616 break; 617 case ConfigFileType::JoyStickCalibration: 618 case ConfigFileType::CommandHistory: 619 this->configFiles_[type] = new ConfigFile(filename); 620 break; 621 } 622 this->configFiles_[type]->load(); 566 623 } 567 624 } -
code/trunk/src/libraries/core/ConfigFileManager.h
r6427 r6536 32 32 #include "CorePrereqs.h" 33 33 34 #include <cassert>35 #include <string>36 34 #include <list> 37 35 #include <map> 38 39 #include "util/OrxEnum.h" 36 #include <set> 37 #include <string> 38 #include <boost/array.hpp> 39 40 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 41 #include "util/StringUtils.h" 42 43 namespace orxonox // tolua_export 44 { // tolua_export 67 45 68 46 ///////////////////// … … 196 174 { 197 175 friend class ConfigFile; 176 friend class SettingsConfigFile; 198 177 199 178 public: … … 212 191 213 192 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(); } 193 { this->getOrCreateEntry(name, value, bString)->setValue(value); } 194 inline const std::string& getValue(const std::string& name, bool bString) 195 { 196 ConfigFileEntry* entry = this->getEntry(name); 197 if (entry) 198 { 199 entry->setString(bString); 200 return entry->getValue(); 201 } 202 return BLANKSTRING; 203 } 204 inline const std::string& getOrCreateValue(const std::string& name, const std::string& fallback, bool bString) 205 { return this->getOrCreateEntry(name, fallback, bString)->getValue(); } 217 206 218 207 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(); } 208 { this->getOrCreateEntry(name, index, value, bString)->setValue(value); } 209 inline const std::string& getValue(const std::string& name, unsigned int index, bool bString) 210 { 211 ConfigFileEntry* entry = this->getEntry(name, index); 212 if (entry) 213 { 214 entry->setString(bString); 215 return entry->getValue(); 216 } 217 return BLANKSTRING; 218 } 219 inline const std::string& getOrCreateValue(const std::string& name, unsigned int index, const std::string& fallback, bool bString) 220 { return this->getOrCreateEntry(name, index, fallback, bString)->getValue(); } 222 221 223 222 void deleteVectorEntries(const std::string& name, unsigned int startindex = 0); 224 unsigned int getVectorSize(const std::string& name) ;223 unsigned int getVectorSize(const std::string& name) const; 225 224 226 225 std::string getFileEntry() const; … … 234 233 { return this->entries_.end(); } 235 234 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)); } 235 std::list<ConfigFileEntry*>::iterator getOrCreateEntryIterator(const std::string& name, const std::string& fallback, bool bString); 236 std::list<ConfigFileEntry*>::iterator getOrCreateEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString); 237 238 ConfigFileEntry* getEntry(const std::string& name) const; 239 inline ConfigFileEntry* getOrCreateEntry(const std::string& name, const std::string& fallback, bool bString) 240 { return (*this->getOrCreateEntryIterator(name, fallback, bString)); } 241 ConfigFileEntry* getEntry(const std::string& name, unsigned int index) const; 242 inline ConfigFileEntry* getOrCreateEntry(const std::string& name, unsigned int index, const std::string& fallback, bool bString) 243 { return (*this->getOrCreateEntryIterator(name, index, fallback, bString)); } 243 244 244 245 std::string name_; … … 255 256 { 256 257 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_; } 258 ConfigFile(const std::string& filename, bool bCopyFallbackFile = true); 259 virtual ~ConfigFile(); 260 261 virtual void load(); 262 virtual void save() const; 263 virtual void saveAs(const std::string& filename) const; 264 virtual void clear(); 265 266 inline const std::string& getFilename() 267 { return this->filename_; } 271 268 272 269 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; } 270 { 271 this->getOrCreateSection(section)->setValue(name, value, bString); 272 this->save(); 273 } 274 inline const std::string& getValue(const std::string& section, const std::string& name, bool bString) 275 { 276 ConfigFileSection* sectionPtr = this->getSection(section); 277 return (sectionPtr ? sectionPtr->getValue(name, bString) : BLANKSTRING); 278 } 279 const std::string& getOrCreateValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString); 276 280 277 281 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 282 { 283 this->getOrCreateSection(section)->setValue(name, index, value, bString); 284 this->save(); 285 } 286 inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index, bool bString) 287 { 288 ConfigFileSection* sectionPtr = this->getSection(section); 289 return (sectionPtr ? sectionPtr->getValue(name, index, bString) : BLANKSTRING); 290 } 291 const std::string& getOrCreateValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString); 292 293 void deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex = 0); 294 inline unsigned int getVectorSize(const std::string& section, const std::string& name) const 295 { 296 ConfigFileSection* sectionPtr = this->getSection(section); 297 return (sectionPtr ? sectionPtr->getVectorSize(name) : 0); 298 } 299 300 static const char* DEFAULT_CONFIG_FOLDER; 301 302 protected: 303 ConfigFileSection* getSection(const std::string& section) const; 304 ConfigFileSection* getOrCreateSection(const std::string& section); 305 306 std::list<ConfigFileSection*> sections_; 307 308 private: 309 void saveIfUpdated(); 310 const std::string filename_; 311 const bool bCopyFallbackFile_; 312 bool bUpdated_; 313 }; 314 315 316 //////////////////////// 317 // SettingsConfigFile // 318 //////////////////////// 319 class _CoreExport SettingsConfigFile // tolua_export 320 : public ConfigFile, public Singleton<SettingsConfigFile> 321 { // tolua_export 322 friend class Singleton<SettingsConfigFile>; 323 324 public: 325 typedef std::multimap<std::string, std::pair<std::string, ConfigValueContainer*> > ContainerMap; 326 327 SettingsConfigFile(const std::string& filename); 328 ~SettingsConfigFile(); 329 330 void load(); // tolua_export 331 void setFilename(const std::string& filename); // tolua_export 332 void clean(bool bCleanComments = false); // tolua_export 333 334 bool config(const std::string& section, const std::string& entry, const std::string& value); // tolua_export 335 bool tconfig(const std::string& section, const std::string& entry, const std::string& value); // tolua_export 336 std::string getConfig(const std::string& section, const std::string& entry); // tolua_export 337 338 void addConfigValueContainer(ConfigValueContainer* container); 339 void removeConfigValueContainer(ConfigValueContainer* container); 340 341 inline const std::set<std::string>& getSectionNames() 342 { return this->sectionNames_; } 343 inline ContainerMap::const_iterator getContainerLowerBound(const std::string section) 344 { return this->containers_.lower_bound(section); } 345 inline ContainerMap::const_iterator getContainerUpperBound(const std::string section) 346 { return this->containers_.upper_bound(section); } 347 348 static SettingsConfigFile& getInstance() { return Singleton<SettingsConfigFile>::getInstance(); } // tolua_export 349 350 private: 287 351 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 }; 352 bool configImpl(const std::string& section, const std::string& entry, const std::string& value, bool (ConfigValueContainer::*function)(const MultiType&)); 353 354 ContainerMap containers_; 355 std::set<std::string> sectionNames_; 356 static SettingsConfigFile* singletonPtr_s; 357 }; // tolua_export 298 358 299 359 … … 308 368 ~ConfigFileManager(); 309 369 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; 370 void setFilename(ConfigFileType::Value type, const std::string& filename); 371 372 inline ConfigFile* getConfigFile(ConfigFileType::Value type) 373 { 374 // Check array bounds 375 return configFiles_.at(type); 376 } 343 377 344 378 private: 345 379 ConfigFileManager(const ConfigFileManager&); 346 380 347 ConfigFile* getFile(ConfigFileType type); 348 349 std::map<ConfigFileType, ConfigFile*> configFiles_; 350 unsigned int mininmalFreeType_; 351 381 boost::array<ConfigFile*, 3> configFiles_; 352 382 static ConfigFileManager* singletonPtr_s; 353 383 }; -
code/trunk/src/libraries/core/ConfigValueContainer.cc
r5738 r6536 36 36 #include "util/Convert.h" 37 37 #include "util/SubString.h" 38 #include "ConfigFileManager.h" 38 39 #include "Language.h" 39 40 … … 43 44 44 45 /** 45 @brief Initializes the ConfigValueContainer with default values.46 */ 47 void ConfigValueContainer::init(ConfigFileType type, Identifier* identifier, const std::string& sectionname, const std::string& varname)46 @brief Initializes the ConfigValueContainer with default values. 47 */ 48 void ConfigValueContainer::init(ConfigFileType::Value type, Identifier* identifier, const std::string& sectionname, const std::string& varname) 48 49 { 49 50 this->type_ = type; … … 55 56 this->bDoInitialCallback_ = false; 56 57 this->bAddedDescription_ = false; 58 59 // Register containers for general settings 60 if (this->type_ == ConfigFileType::Settings) 61 SettingsConfigFile::getInstance().addConfigValueContainer(this); 57 62 } 58 63 … … 78 83 for (unsigned int i = 0; i < this->valueVector_.size(); i++) 79 84 { 80 ConfigFileManager::getInstance().get Value(this->type_,this->sectionname_, this->varname_, i, this->valueVector_[i], this->value_.isType(MT_Type::String));85 ConfigFileManager::getInstance().getConfigFile(this->type_)->getOrCreateValue(this->sectionname_, this->varname_, i, this->valueVector_[i], this->value_.isType(MT_Type::String)); 81 86 this->defvalueStringVector_.push_back(this->valueVector_[i]); 82 87 } … … 92 97 if (this->callback_) 93 98 delete this->callback_; 99 100 // Unregister general settings containers 101 if (this->type_ == ConfigFileType::Settings && SettingsConfigFile::exists()) 102 SettingsConfigFile::getInstance().removeConfigValueContainer(this); 94 103 } 95 104 … … 109 118 if (this->tset(input)) 110 119 { 111 ConfigFileManager::getInstance(). setValue(this->type_,this->sectionname_, this->varname_, input, this->value_.isType(MT_Type::String));120 ConfigFileManager::getInstance().getConfigFile(this->type_)->setValue(this->sectionname_, this->varname_, input, this->value_.isType(MT_Type::String)); 112 121 return true; 113 122 } … … 128 137 if (this->tset(index, input)) 129 138 { 130 ConfigFileManager::getInstance(). setValue(this->type_,this->sectionname_, this->varname_, index, input, this->value_.isType(MT_Type::String));139 ConfigFileManager::getInstance().getConfigFile(this->type_)->setValue(this->sectionname_, this->varname_, index, input, this->value_.isType(MT_Type::String)); 131 140 return true; 132 141 } … … 228 237 this->valueVector_.erase(this->valueVector_.begin() + index); 229 238 for (unsigned int i = index; i < this->valueVector_.size(); i++) 230 ConfigFileManager::getInstance(). setValue(this->type_,this->sectionname_, this->varname_, i, this->valueVector_[i], this->value_.isType(MT_Type::String));231 ConfigFileManager::getInstance(). deleteVectorEntries(this->type_,this->sectionname_, this->varname_, this->valueVector_.size());239 ConfigFileManager::getInstance().getConfigFile(this->type_)->setValue(this->sectionname_, this->varname_, i, this->valueVector_[i], this->value_.isType(MT_Type::String)); 240 ConfigFileManager::getInstance().getConfigFile(this->type_)->deleteVectorEntries(this->sectionname_, this->varname_, this->valueVector_.size()); 232 241 233 242 return true; … … 253 262 if (!this->set(i, this->defvalueStringVector_[i])) 254 263 success = false; 255 ConfigFileManager::getInstance(). deleteVectorEntries(this->type_,this->sectionname_, this->varname_, this->defvalueStringVector_.size());264 ConfigFileManager::getInstance().getConfigFile(this->type_)->deleteVectorEntries(this->sectionname_, this->varname_, this->defvalueStringVector_.size()); 256 265 return success; 257 266 } … … 264 273 { 265 274 if (!this->bIsVector_) 266 this->value_ = ConfigFileManager::getInstance().get Value(this->type_,this->sectionname_, this->varname_, this->defvalueString_, this->value_.isType(MT_Type::String));275 this->value_ = ConfigFileManager::getInstance().getConfigFile(this->type_)->getOrCreateValue(this->sectionname_, this->varname_, this->defvalueString_, this->value_.isType(MT_Type::String)); 267 276 else 268 277 { 269 278 this->valueVector_.clear(); 270 unsigned int vectorSize = ConfigFileManager::getInstance().get VectorSize(this->type_,this->sectionname_, this->varname_);279 unsigned int vectorSize = ConfigFileManager::getInstance().getConfigFile(this->type_)->getVectorSize(this->sectionname_, this->varname_); 271 280 for (unsigned int i = 0; i < vectorSize; i++) 272 281 { 273 282 if (i < this->defvalueStringVector_.size()) 274 283 { 275 this->value_ = ConfigFileManager::getInstance().get Value(this->type_,this->sectionname_, this->varname_, i, this->defvalueStringVector_[i], this->value_.isType(MT_Type::String));284 this->value_ = ConfigFileManager::getInstance().getConfigFile(this->type_)->getOrCreateValue(this->sectionname_, this->varname_, i, this->defvalueStringVector_[i], this->value_.isType(MT_Type::String)); 276 285 } 277 286 else 278 287 { 279 this->value_ = ConfigFileManager::getInstance().get Value(this->type_,this->sectionname_, this->varname_, i, MultiType(), this->value_.isType(MT_Type::String));288 this->value_ = ConfigFileManager::getInstance().getConfigFile(this->type_)->getOrCreateValue(this->sectionname_, this->varname_, i, MultiType(), this->value_.isType(MT_Type::String)); 280 289 } 281 290 -
code/trunk/src/libraries/core/ConfigValueContainer.h
r6417 r6536 50 50 51 51 #include "util/MultiType.h" 52 #include "ConfigFileManager.h"53 52 #include "Identifier.h" 54 53 … … 108 107 */ 109 108 template <class D, class V> 110 ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& sectionname, const std::string& varname, const D& defvalue, const V& value)109 ConfigValueContainer(ConfigFileType::Value type, Identifier* identifier, const std::string& sectionname, const std::string& varname, const D& defvalue, const V& value) 111 110 { 112 111 this->init(type, identifier, sectionname, varname); … … 122 121 */ 123 122 template <class D, class V> 124 ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& sectionname, const std::string& varname, const std::vector<D>& defvalue, const std::vector<V>& value)123 ConfigValueContainer(ConfigFileType::Value type, Identifier* identifier, const std::string& sectionname, const std::string& varname, const std::vector<D>& defvalue, const std::vector<V>& value) 125 124 { 126 125 this->init(type, identifier, sectionname, varname); … … 217 216 inline const std::string& getName() const 218 217 { return this->varname_; } 219 /** @brief Retu ns the name of the section this config value is in. */218 /** @brief Returns the name of the section this config value is in. */ 220 219 inline const std::string& getSectionName() const 221 220 { return this->sectionname_; } 221 /** @brief Returns the associated identifier (can be NULL). */ 222 inline Identifier* getIdentifier() const 223 { return this->identifier_; } 222 224 /** @brief Returns true if this config-value is a vector */ 223 225 inline bool isVector() const … … 271 273 272 274 private: 273 void init(ConfigFileType type, Identifier* identifier, const std::string& sectionname, const std::string& varname);275 void init(ConfigFileType::Value type, Identifier* identifier, const std::string& sectionname, const std::string& varname); 274 276 void initValue(const MultiType& defvalue); 275 277 void initVector(); … … 278 280 bool bIsVector_; //!< True if the container contains a std::vector 279 281 280 ConfigFileType 282 ConfigFileType::Value type_; //!< The type of the corresponding config-file 281 283 Identifier* identifier_; //!< The identifier of the class 282 284 std::string sectionname_; //!< The name of the class the variable belongs to -
code/trunk/src/libraries/core/ConfigValueIncludes.h
r6423 r6536 40 40 #include "Identifier.h" 41 41 #include "ConfigValueContainer.h" 42 #include "ConfigFileManager.h"43 42 44 43 namespace orxonox … … 61 60 */ 62 61 template <class T, class D, class V> 63 inline ConfigValueContainer& setConfigValueGeneric(T* object, V* variable, ConfigFileType type, const std::string& sectionName, const std::string& entryName, const D& defaultValue)62 inline ConfigValueContainer& setConfigValueGeneric(T* object, V* variable, ConfigFileType::Value type, const std::string& sectionName, const std::string& entryName, const D& defaultValue) 64 63 { 65 64 ConfigValueContainer* container = ClassIdentifier<T>::getIdentifier()->getConfigValueContainer(entryName); -
code/trunk/src/libraries/core/CorePrereqs.h
r6105 r6536 84 84 } 85 85 86 namespace ConfigFileType 87 { 88 enum Value 89 { 90 Settings, 91 JoyStickCalibration, 92 CommandHistory 93 // Don't forget to adjust the array size in the ConfigFileManager when adding a new entry here! 94 }; 95 } 96 86 97 namespace KeybindMode 87 98 { … … 124 135 class ConfigFileManager; 125 136 class ConfigFileSection; 126 struct ConfigFileType;127 137 class ConfigValueContainer; 128 138 class ConsoleCommand; … … 172 182 class PathConfig; 173 183 struct ResourceInfo; 184 class SettingsConfigFile; 174 185 class Shell; 175 186 class ShellListener; -
code/trunk/src/libraries/core/Identifier.cc
r6417 r6536 416 416 this->bHasConfigValues_ = true; 417 417 this->configValues_[varname] = container; 418 this->configValues_LC_[getLowercase(varname)] = container;419 418 } 420 419 … … 428 427 std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_.find(varname); 429 428 if (it != configValues_.end()) 430 return it->second;431 else432 return 0;433 }434 435 /**436 @brief Returns the ConfigValueContainer of a variable, given by the string of its name in lowercase.437 @param varname The name of the variable in lowercase438 @return The ConfigValueContainer439 */440 ConfigValueContainer* Identifier::getLowercaseConfigValueContainer(const std::string& varname)441 {442 std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_LC_.find(varname);443 if (it != configValues_LC_.end())444 429 return it->second; 445 430 else -
code/trunk/src/libraries/core/Identifier.h
r6417 r6536 200 200 inline bool hasConfigValues() const { return this->bHasConfigValues_; } 201 201 202 /** @brief Returns the map that stores all config values. @return The const_iterator */203 inline const std::map<std::string, ConfigValueContainer*>& getConfigValueMap() const { return this->configValues_; }204 /** @brief Returns a const_iterator to the beginning of the map that stores all config values. @return The const_iterator */205 inline std::map<std::string, ConfigValueContainer*>::const_iterator getConfigValueMapBegin() const { return this->configValues_.begin(); }206 /** @brief Returns a const_iterator to the end of the map that stores all config values. @return The const_iterator */207 inline std::map<std::string, ConfigValueContainer*>::const_iterator getConfigValueMapEnd() const { return this->configValues_.end(); }208 209 /** @brief Returns the map that stores all config values with their names in lowercase. @return The const_iterator */210 inline const std::map<std::string, ConfigValueContainer*>& getLowercaseConfigValueMap() const { return this->configValues_LC_; }211 /** @brief Returns a const_iterator to the beginning of the map that stores all config values with their names in lowercase. @return The const_iterator */212 inline std::map<std::string, ConfigValueContainer*>::const_iterator getLowercaseConfigValueMapBegin() const { return this->configValues_LC_.begin(); }213 /** @brief Returns a const_iterator to the end of the map that stores all config values with their names in lowercase. @return The const_iterator */214 inline std::map<std::string, ConfigValueContainer*>::const_iterator getLowercaseConfigValueMapEnd() const { return this->configValues_LC_.end(); }215 216 202 void addConfigValueContainer(const std::string& varname, ConfigValueContainer* container); 217 203 ConfigValueContainer* getConfigValueContainer(const std::string& varname); 218 ConfigValueContainer* getLowercaseConfigValueContainer(const std::string& varname);219 204 220 205 … … 320 305 bool bHasConfigValues_; //!< True if this class has at least one assigned config value 321 306 std::map<std::string, ConfigValueContainer*> configValues_; //!< A map to link the string of configurable variables with their ConfigValueContainer 322 std::map<std::string, ConfigValueContainer*> configValues_LC_; //!< A map to link the string of configurable variables with their ConfigValueContainer323 307 324 308 bool bHasConsoleCommands_; //!< True if this class has at least one assigned console command -
code/trunk/src/libraries/core/Language.h
r6417 r6536 52 52 #include <cassert> 53 53 #include "util/Singleton.h" 54 55 #define AddLanguageEntry(label, fallbackstring) \56 orxonox::Language::getInstance().addEntry(label, fallbackstring)57 58 #define GetLocalisation(label) \59 orxonox::Language::getInstance().getLocalisation(label)60 61 54 62 55 namespace orxonox … … 140 133 static Language* singletonPtr_s; 141 134 }; 135 136 //! Shortcut function for Language::addEntry 137 inline void AddLanguageEntry(const LanguageEntryLabel& label, const std::string& fallbackString) 138 { 139 Language::getInstance().addEntry(label, fallbackString); 140 } 141 142 //! Shortcut function for Language::getLocalisation 143 inline const std::string& GetLocalisation(const LanguageEntryLabel& label) 144 { 145 return Language::getInstance().getLocalisation(label); 146 } 142 147 } 143 148 -
code/trunk/src/libraries/core/Shell.cc
r6417 r6536 34 34 #include "CommandExecutor.h" 35 35 #include "CoreIncludes.h" 36 #include "ConfigFileManager.h" 36 37 #include "ConfigValueIncludes.h" 37 38 #include "ConsoleCommand.h" -
code/trunk/src/libraries/core/Shell.h
r6417 r6536 39 39 #include "util/OutputHandler.h" 40 40 #include "OrxonoxClass.h" 41 #include "ConfigFileManager.h"42 41 #include "input/InputBuffer.h" 43 42 -
code/trunk/src/libraries/core/input/Button.cc
r6428 r6536 42 42 #include "core/CommandEvaluation.h" 43 43 #include "core/CommandExecutor.h" 44 #include "core/ConfigFileManager.h" 44 45 45 46 namespace orxonox … … 81 82 } 82 83 83 void Button::readBinding(ConfigFileType type) 84 { 85 const std::string& binding = ConfigFileManager::getInstance().getValue(type, groupName_, name_, "", true); 84 void Button::readBinding(ConfigFile* configFile, ConfigFile* fallbackFile) 85 { 86 std::string binding = configFile->getOrCreateValue(groupName_, name_, "", true); 87 if (binding.empty() && fallbackFile) 88 binding = fallbackFile->getValue(groupName_, name_, true); 86 89 this->parse(binding); 87 90 } 88 91 89 void Button::setBinding(ConfigFile Type type, const std::string& binding, bool bTemporary)92 void Button::setBinding(ConfigFile* configFile, ConfigFile* fallbackFile, const std::string& binding, bool bTemporary) 90 93 { 91 94 if (!bTemporary) 92 ConfigFileManager::getInstance().setValue(type,groupName_, name_, binding, true);95 configFile->setValue(groupName_, name_, binding, true); 93 96 this->parse(binding); 94 97 } … … 103 106 this->bindingString_ = binding; 104 107 105 if (isEmpty(bindingString_) )108 if (isEmpty(bindingString_) || removeTrailingWhitespaces(getLowercase(binding)) == "nobinding") 106 109 return; 107 110 -
code/trunk/src/libraries/core/input/Button.h
r6428 r6536 41 41 #include <vector> 42 42 #include "InputCommands.h" 43 #include "core/ConfigFileManager.h"44 43 45 44 namespace orxonox … … 53 52 virtual bool addParamCommand(ParamCommand* command) { return false; } 54 53 void parse(const std::string& binding); 55 void readBinding(ConfigFile Type type);56 void setBinding(ConfigFile Type type, const std::string& binding, bool bTemporary);54 void readBinding(ConfigFile* configFile, ConfigFile* fallbackFile); 55 void setBinding(ConfigFile* configFile, ConfigFile* fallbackFile, const std::string& binding, bool bTemporary); 57 56 bool execute(KeybindMode::Value mode, float abs = 1.0f, float rel = 1.0f); 58 57 -
code/trunk/src/libraries/core/input/JoyStick.cc
r6417 r6536 106 106 { 107 107 list.resize(size); 108 unsigned int configValueVectorSize = ConfigFileManager::getInstance().get VectorSize(ConfigFileType::JoyStickCalibration,sectionName, valueName);108 unsigned int configValueVectorSize = ConfigFileManager::getInstance().getConfigFile(ConfigFileType::JoyStickCalibration)->getVectorSize(sectionName, valueName); 109 109 if (configValueVectorSize > size) 110 110 configValueVectorSize = size; … … 112 112 for (unsigned int i = 0; i < configValueVectorSize; ++i) 113 113 { 114 list[i] = multi_cast<int>(ConfigFileManager::getInstance().get Value(115 ConfigFileType::JoyStickCalibration,sectionName, valueName, i, multi_cast<std::string>(defaultValue), false));114 list[i] = multi_cast<int>(ConfigFileManager::getInstance().getConfigFile(ConfigFileType::JoyStickCalibration) 115 ->getOrCreateValue(sectionName, valueName, i, multi_cast<std::string>(defaultValue), false)); 116 116 } 117 117 … … 153 153 if (configMinValues_[i] == INT_MAX) 154 154 configMinValues_[i] = -32768; 155 ConfigFileManager::getInstance(). setValue(ConfigFileType::JoyStickCalibration,156 deviceName_, "MinValue", i, multi_cast<std::string>(configMinValues_[i]), false);155 ConfigFileManager::getInstance().getConfigFile(ConfigFileType::JoyStickCalibration) 156 ->getOrCreateValue(deviceName_, "MinValue", i, multi_cast<std::string>(configMinValues_[i]), false); 157 157 158 158 // Maximum values 159 159 if (configMaxValues_[i] == INT_MIN) 160 160 configMaxValues_[i] = 32767; 161 ConfigFileManager::getInstance(). setValue(ConfigFileType::JoyStickCalibration,162 deviceName_, "MaxValue", i, multi_cast<std::string>(configMaxValues_[i]), false);161 ConfigFileManager::getInstance().getConfigFile(ConfigFileType::JoyStickCalibration) 162 ->getOrCreateValue(deviceName_, "MaxValue", i, multi_cast<std::string>(configMaxValues_[i]), false); 163 163 164 164 // Middle values 165 ConfigFileManager::getInstance(). setValue(ConfigFileType::JoyStickCalibration,166 deviceName_, "ZeroValue", i, multi_cast<std::string>(configZeroValues_[i]), false);165 ConfigFileManager::getInstance().getConfigFile(ConfigFileType::JoyStickCalibration) 166 ->getOrCreateValue(deviceName_, "ZeroValue", i, multi_cast<std::string>(configZeroValues_[i]), false); 167 167 } 168 168 -
code/trunk/src/libraries/core/input/KeyBinder.cc
r6428 r6536 37 37 #include "core/CoreIncludes.h" 38 38 #include "core/ConfigFileManager.h" 39 #include "core/PathConfig.h" 39 40 #include "InputCommands.h" 40 41 #include "JoyStick.h" … … 49 50 : deriveTime_(0.0f) 50 51 , filename_(filename) 52 , configFile_(NULL) 53 , fallbackConfigFile_(NULL) 51 54 { 52 55 mouseRelative_[0] = 0; … … 94 97 } 95 98 96 // We might not even load any bindings at all (KeyDetector for instance)97 this->configFile_ = ConfigFileType::NoType;98 99 99 // initialise joy sticks separatly to allow for reloading 100 100 this->JoyStickQuantityChanged(this->getJoyStickList()); … … 116 116 // almost no destructors required because most of the arrays are static. 117 117 clearBindings(); // does some destruction work 118 if (this->configFile_) 119 delete this->configFile_; 120 if (this->fallbackConfigFile_) 121 delete this->fallbackConfigFile_; 118 122 } 119 123 … … 163 167 164 168 // load the bindings if required 165 if (configFile_ != ConfigFileType::NoType)169 if (configFile_ != NULL) 166 170 { 167 171 for (unsigned int iDev = oldValue; iDev < joySticks_.size(); ++iDev) 168 172 { 169 173 for (unsigned int i = 0; i < JoyStickButtonCode::numberOfButtons; ++i) 170 (*joyStickButtons_[iDev])[i].readBinding(this->configFile_ );174 (*joyStickButtons_[iDev])[i].readBinding(this->configFile_, this->fallbackConfigFile_); 171 175 for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; ++i) 172 (*joyStickAxes_[iDev])[i].readBinding(this->configFile_ );176 (*joyStickAxes_[iDev])[i].readBinding(this->configFile_, this->fallbackConfigFile_); 173 177 } 174 178 } … … 249 253 COUT(3) << "KeyBinder: Loading key bindings..." << std::endl; 250 254 251 // Get a new ConfigFileType from the ConfigFileManager 252 this->configFile_ = ConfigFileManager::getInstance().getNewConfigFileType(); 253 254 ConfigFileManager::getInstance().setFilename(this->configFile_, this->filename_); 255 this->configFile_ = new ConfigFile(this->filename_, !PathConfig::isDevelopmentRun()); 256 this->configFile_->load(); 257 258 if (PathConfig::isDevelopmentRun()) 259 { 260 // Dev users should have combined key bindings files 261 std::string defaultFilepath(PathConfig::getDataPathString() + ConfigFile::DEFAULT_CONFIG_FOLDER + '/' + this->filename_); 262 std::ifstream file(defaultFilepath.c_str()); 263 if (file.is_open()) 264 { 265 file.close(); 266 // Open the default file for later use (use absolute path!) 267 this->fallbackConfigFile_ = new ConfigFile(defaultFilepath, false); 268 this->fallbackConfigFile_->load(); 269 } 270 } 255 271 256 272 // Parse bindings and create the ConfigValueContainers if necessary 257 273 for (std::map<std::string, Button*>::const_iterator it = allButtons_.begin(); it != allButtons_.end(); ++it) 258 274 { 259 it->second->readBinding(this->configFile_ );275 it->second->readBinding(this->configFile_, this->fallbackConfigFile_); 260 276 addButtonToCommand(it->second->bindingString_, it->second); 261 277 } … … 270 286 { 271 287 addButtonToCommand(binding, it->second); 272 it->second->setBinding(this->configFile_, binding, bTemporary); 288 std::string str = binding; 289 if (PathConfig::isDevelopmentRun() && binding.empty()) 290 str = "NoBinding"; 291 it->second->setBinding(this->configFile_, this->fallbackConfigFile_, binding, bTemporary); 273 292 return true; 274 293 } -
code/trunk/src/libraries/core/input/KeyBinder.h
r6428 r6536 38 38 #include <boost/shared_ptr.hpp> 39 39 40 #include "core/ConfigFileManager.h"41 40 #include "InputHandler.h" 42 41 #include "Button.h" … … 157 156 //! Name of the file used in this KeyBinder (constant!) 158 157 const std::string filename_; 159 //! Config file used. ConfigFileType::NoType in case of KeyDetector. Also indicates whether we've already loaded. 160 ConfigFileType configFile_; 158 //! Config file used. NULL in case of KeyDetector. Also indicates whether we've already loaded. 159 ConfigFile* configFile_; 160 //! Config file from the data directory that only serves as fallback 161 ConfigFile* fallbackConfigFile_; 161 162 162 163 private: -
code/trunk/src/libraries/util/Singleton.h
r6417 r6536 51 51 static T& getInstance() 52 52 { 53 assert(T::singletonPtr_s != 0);53 assert(T::singletonPtr_s != NULL); 54 54 return *T::singletonPtr_s; 55 } 56 57 //! Tells whether the singleton has been created 58 static bool exists() 59 { 60 return (T::singletonPtr_s != NULL); 55 61 } 56 62 … … 68 74 Singleton() 69 75 { 70 assert(T::singletonPtr_s == 0);76 assert(T::singletonPtr_s == NULL); 71 77 T::singletonPtr_s = static_cast<T*>(this); 72 78 } … … 75 81 ~Singleton() 76 82 { 77 assert(T::singletonPtr_s != 0);78 T::singletonPtr_s = 0;83 assert(T::singletonPtr_s != NULL); 84 T::singletonPtr_s = NULL; 79 85 } 80 86 -
code/trunk/src/modules/questsystem/QuestManager.cc
r6417 r6536 235 235 CEGUI::Window* QuestManager::getQuestGUI(const std::string & guiName) 236 236 { 237 PlayerInfo* player = this->retr eivePlayer(guiName);237 PlayerInfo* player = this->retrievePlayer(guiName); 238 238 239 239 if(this->questGUIs_.find(player) == this->questGUIs_.end()) //!< Create a new GUI, if there is none, yet. … … 253 253 This very well might be outdated. So: Check if still needed, and update if necessary. 254 254 */ 255 PlayerInfo* QuestManager::retr eivePlayer(const std::string & guiName)255 PlayerInfo* QuestManager::retrievePlayer(const std::string & guiName) 256 256 { 257 257 PlayerInfo* player = GUIManager::getInstance().getPlayer(guiName); -
code/trunk/src/modules/questsystem/QuestManager.h
r6417 r6536 52 52 /** 53 53 @brief 54 Is a Singleton and manages Quests, by registering every Quest/QuestHint (through registerX()) and making them globally access able (through findX()).54 Is a Singleton and manages Quests, by registering every Quest/QuestHint (through registerX()) and making them globally accessible (through findX()). 55 55 Quests (and QuestHints) are registered in the QuestManager with their id, and can be accessed in the same way. 56 56 @author … … 72 72 static QuestManager& getInstance() { return Singleton<QuestManager>::getInstance(); } // tolua_export 73 73 74 //! Retr eive the main window for the GUI.74 //! Retrieve the main window for the GUI. 75 75 CEGUI::Window* getQuestGUI(const std::string & guiName); // tolua_export 76 76 … … 82 82 83 83 protected: 84 std::map<std::string, Quest*> & getQuests(void); //!< Retr eive all Quests.84 std::map<std::string, Quest*> & getQuests(void); //!< Retrieve all Quests. 85 85 86 86 private: 87 87 static QuestManager* singletonPtr_s; 88 PlayerInfo* retr eivePlayer(const std::string & guiName); //!< Retrieve the player for a certain GUI.88 PlayerInfo* retrievePlayer(const std::string & guiName); //!< Retrieve the player for a certain GUI. 89 89 90 90 std::map<std::string, Quest*> questMap_; //!< All Quests registered by their id's.
Note: See TracChangeset
for help on using the changeset viewer.