[Orxonox-commit 1714] r6432 - in code/branches/gamestate: data/lua src/libraries/core src/libraries/core/input
rgrieder at orxonox.net
rgrieder at orxonox.net
Tue Dec 29 22:30:19 CET 2009
Author: rgrieder
Date: 2009-12-29 22:30:19 +0100 (Tue, 29 Dec 2009)
New Revision: 6432
Modified:
code/branches/gamestate/data/lua/LuaStateInit.lua
code/branches/gamestate/src/libraries/core/ArgumentCompletionFunctions.cc
code/branches/gamestate/src/libraries/core/ArgumentCompletionFunctions.h
code/branches/gamestate/src/libraries/core/ConfigFileManager.cc
code/branches/gamestate/src/libraries/core/ConfigFileManager.h
code/branches/gamestate/src/libraries/core/ConfigValueContainer.cc
code/branches/gamestate/src/libraries/core/ConfigValueContainer.h
code/branches/gamestate/src/libraries/core/ConfigValueIncludes.h
code/branches/gamestate/src/libraries/core/CorePrereqs.h
code/branches/gamestate/src/libraries/core/Identifier.cc
code/branches/gamestate/src/libraries/core/Identifier.h
code/branches/gamestate/src/libraries/core/Shell.cc
code/branches/gamestate/src/libraries/core/Shell.h
code/branches/gamestate/src/libraries/core/input/Button.cc
code/branches/gamestate/src/libraries/core/input/Button.h
code/branches/gamestate/src/libraries/core/input/JoyStick.cc
code/branches/gamestate/src/libraries/core/input/KeyBinder.cc
code/branches/gamestate/src/libraries/core/input/KeyBinder.h
Log:
Changed the way config values associated with general settings (ConfigFileType::Settings) are handled:
- ConfigFileManager only handles config files listed in the ConfigFileType enum (normal enum again)
- ConfigFileManager only takes care of ConfigFiles and returns a pointer to the right one, just two functions left.
--> use like: ConfigFileManager::getInstance().getConfigFile(myType)->doSomething();
- Moved all code (except for the argument completion functions) relating to ConfigFileType::Settings to a new class:
SettingsConfigFile, which is a Singleton (it doesn't make sense to have multiple instances unless you start coding a lot more)
- SettingsConfigFile handles config value containers according to their section and entry in the ini file, not according to class and variables names.
(In most cases it will be class and variable names though)
- SettingsConfigFile supports:
- clear() (removes any file entries not associated to a config value container)
- updateConfigValues() (does exactly that through the identifier)
- config, tconfig and getConfig
- commands listed above are exported to tolua, and tconfig, config and getConfig were given shortcuts in Lua (e.g. orxonox.config)
- If you need to organise ConfigFiles yourself, just do it without the ConfigFileManager, like the KeyBinder does.
- All getValue() functions have been split into getOrCreateValue() and getValue(), which is const
- Removed obsolete config value management code in the Identifier (it still stores and destroys them and provides access to them)
All of that leads to one HUGE advantage:
"config OutputHandler softDebugLevelInGameConsole"
works now :D (any further implications are up to the reader...)
(it didn't work before because the actual config value container is in the InGameConsole singleton)
Modified: code/branches/gamestate/data/lua/LuaStateInit.lua
===================================================================
--- code/branches/gamestate/data/lua/LuaStateInit.lua 2009-12-29 18:16:36 UTC (rev 6431)
+++ code/branches/gamestate/data/lua/LuaStateInit.lua 2009-12-29 21:30:19 UTC (rev 6432)
@@ -57,3 +57,14 @@
orxonox.execute = function(command)
orxonox.CommandExecutor:execute(command)
end
+
+-- Convenience function for config values
+orxonox.getConfig = function(section, entry)
+ return orxonox.SettingsConfigFile:getInstance():getConfig(section, entry)
+end
+orxonox.config = function(section, entry, value)
+ return orxonox.SettingsConfigFile:getInstance():config(section, entry, value)
+end
+orxonox.tconfig = function(section, entry, value)
+ return orxonox.SettingsConfigFile:getInstance():tconfig(section, entry, value)
+end
Modified: code/branches/gamestate/src/libraries/core/ArgumentCompletionFunctions.cc
===================================================================
--- code/branches/gamestate/src/libraries/core/ArgumentCompletionFunctions.cc 2009-12-29 18:16:36 UTC (rev 6431)
+++ code/branches/gamestate/src/libraries/core/ArgumentCompletionFunctions.cc 2009-12-29 21:30:19 UTC (rev 6432)
@@ -35,6 +35,7 @@
#include "util/Convert.h"
#include "util/StringUtils.h"
#include "Identifier.h"
+#include "ConfigFileManager.h"
#include "ConfigValueContainer.h"
#include "TclThreadManager.h"
@@ -95,45 +96,48 @@
return filelist;
}
- ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(configvalueclasses)()
+ ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(settingssections)()
{
- ArgumentCompletionList classlist;
+ ArgumentCompletionList sectionList;
- for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getStringIdentifierMapBegin(); it != Identifier::getStringIdentifierMapEnd(); ++it)
- if (it->second->hasConfigValues())
- classlist.push_back(ArgumentCompletionListElement(it->first, getLowercase(it->first)));
+ const std::set<std::string>& names = SettingsConfigFile::getInstance().getSectionNames();
+ for (std::set<std::string>::const_iterator it = names.begin(); it != names.end(); ++it)
+ sectionList.push_back(ArgumentCompletionListElement(*it, getLowercase(*it)));
- return classlist;
+ return sectionList;
}
- ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(configvalues)(const std::string& fragment, const std::string& classname)
+ ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(settingsentries)(const std::string& fragment, const std::string& section)
{
- ArgumentCompletionList configvalues;
- std::map<std::string, Identifier*>::const_iterator identifier = Identifier::getLowercaseStringIdentifierMap().find(getLowercase(classname));
+ ArgumentCompletionList entryList;
+ SettingsConfigFile& settings = SettingsConfigFile::getInstance();
+ const std::string& sectionLC = getLowercase(section);
- if (identifier != Identifier::getLowercaseStringIdentifierMapEnd() && identifier->second->hasConfigValues())
- {
- for (std::map<std::string, ConfigValueContainer*>::const_iterator it = identifier->second->getConfigValueMapBegin(); it != identifier->second->getConfigValueMapEnd(); ++it)
- configvalues.push_back(ArgumentCompletionListElement(it->first, getLowercase(it->first)));
- }
+ SettingsConfigFile::ContainerMap::const_iterator upper = settings.getContainerUpperBound(sectionLC);
+ for (SettingsConfigFile::ContainerMap::const_iterator it = settings.getContainerLowerBound(sectionLC); it != upper; ++it)
+ entryList.push_back(ArgumentCompletionListElement(it->second.second->getName(), it->second.first));
- return configvalues;
+ return entryList;
}
- ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(configvalue)(const std::string& fragment, const std::string& varname, const std::string& classname)
+ ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(settingsvalue)(const std::string& fragment, const std::string& entry, const std::string& section)
{
- ArgumentCompletionList oldvalue;
- std::map<std::string, Identifier*>::const_iterator identifier = Identifier::getLowercaseStringIdentifierMap().find(getLowercase(classname));
- if (identifier != Identifier::getLowercaseStringIdentifierMapEnd())
+ ArgumentCompletionList oldValue;
+ SettingsConfigFile& settings = SettingsConfigFile::getInstance();
+ const std::string& sectionLC = getLowercase(section);
+ const std::string& entryLC = getLowercase(entry);
+
+ SettingsConfigFile::ContainerMap::const_iterator upper = settings.getContainerUpperBound(sectionLC);
+ for (SettingsConfigFile::ContainerMap::const_iterator it = settings.getContainerLowerBound(sectionLC); it != upper; ++it)
{
- std::map<std::string, ConfigValueContainer*>::const_iterator variable = identifier->second->getLowercaseConfigValueMap().find(getLowercase(varname));
- if (variable != identifier->second->getLowercaseConfigValueMapEnd())
+ if (it->second.first == entryLC)
{
- const std::string& valuestring = variable->second->toString();
- oldvalue.push_back(ArgumentCompletionListElement(valuestring, getLowercase(valuestring), "Old value: " + valuestring));
+ const std::string& valuestring = it->second.second->toString();
+ oldValue.push_back(ArgumentCompletionListElement(valuestring, getLowercase(valuestring), "Old value: " + valuestring));
}
}
- return oldvalue;
+
+ return oldValue;
}
ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(tclthreads)()
Modified: code/branches/gamestate/src/libraries/core/ArgumentCompletionFunctions.h
===================================================================
--- code/branches/gamestate/src/libraries/core/ArgumentCompletionFunctions.h 2009-12-29 18:16:36 UTC (rev 6431)
+++ code/branches/gamestate/src/libraries/core/ArgumentCompletionFunctions.h 2009-12-29 21:30:19 UTC (rev 6432)
@@ -53,9 +53,9 @@
{
ARGUMENT_COMPLETION_FUNCTION_DECLARATION(fallback)();
ARGUMENT_COMPLETION_FUNCTION_DECLARATION(files)(const std::string& fragment);
- ARGUMENT_COMPLETION_FUNCTION_DECLARATION(configvalueclasses)();
- ARGUMENT_COMPLETION_FUNCTION_DECLARATION(configvalues)(const std::string& fragment, const std::string& classname);
- ARGUMENT_COMPLETION_FUNCTION_DECLARATION(configvalue)(const std::string& fragment, const std::string& varname, const std::string& classname);
+ ARGUMENT_COMPLETION_FUNCTION_DECLARATION(settingssections)();
+ ARGUMENT_COMPLETION_FUNCTION_DECLARATION(settingsentries)(const std::string& fragment, const std::string& section);
+ ARGUMENT_COMPLETION_FUNCTION_DECLARATION(settingsvalue)(const std::string& fragment, const std::string& entry, const std::string& section);
ARGUMENT_COMPLETION_FUNCTION_DECLARATION(tclthreads)();
}
}
Modified: code/branches/gamestate/src/libraries/core/ConfigFileManager.cc
===================================================================
--- code/branches/gamestate/src/libraries/core/ConfigFileManager.cc 2009-12-29 18:16:36 UTC (rev 6431)
+++ code/branches/gamestate/src/libraries/core/ConfigFileManager.cc 2009-12-29 21:30:19 UTC (rev 6432)
@@ -39,56 +39,6 @@
namespace orxonox
{
- SetConsoleCommandShortcutExtern(config).argumentCompleter(0, autocompletion::configvalueclasses()).argumentCompleter(1, autocompletion::configvalues()).argumentCompleter(2, autocompletion::configvalue());
- SetConsoleCommandShortcutExtern(tconfig).argumentCompleter(0, autocompletion::configvalueclasses()).argumentCompleter(1, autocompletion::configvalues()).argumentCompleter(2, autocompletion::configvalue());
- SetConsoleCommandShortcutExtern(reloadConfig);
- SetConsoleCommandShortcutExtern(cleanConfig);
- SetConsoleCommandShortcutExtern(loadSettings).argumentCompleter(0, autocompletion::files());
-
- bool config(const std::string& classname, const std::string& varname, const std::string& value)
- {
- std::map<std::string, Identifier*>::const_iterator identifier = Identifier::getLowercaseStringIdentifierMap().find(getLowercase(classname));
- if (identifier != Identifier::getLowercaseStringIdentifierMapEnd())
- {
- std::map<std::string, ConfigValueContainer*>::const_iterator variable = identifier->second->getLowercaseConfigValueMap().find(getLowercase(varname));
- if (variable != identifier->second->getLowercaseConfigValueMapEnd())
- return variable->second->set(value);
- }
- return false;
- }
-
- const std::string& getConfig(const std::string& classname, const std::string& varname)
- {
- return ConfigFileManager::getInstance().getValue(ConfigFileType::Settings, classname, varname, "", true);
- }
-
- bool tconfig(const std::string& classname, const std::string& varname, const std::string& value)
- {
- std::map<std::string, Identifier*>::const_iterator identifier = Identifier::getLowercaseStringIdentifierMap().find(getLowercase(classname));
- if (identifier != Identifier::getLowercaseStringIdentifierMapEnd())
- {
- std::map<std::string, ConfigValueContainer*>::const_iterator variable = identifier->second->getLowercaseConfigValueMap().find(getLowercase(varname));
- if (variable != identifier->second->getLowercaseConfigValueMapEnd())
- return variable->second->tset(value);
- }
- return false;
- }
-
- void reloadConfig()
- {
- ConfigFileManager::getInstance().load();
- }
-
- void cleanConfig()
- {
- ConfigFileManager::getInstance().clean(false);
- }
-
- void loadSettings(const std::string& filename)
- {
- ConfigFileManager::getInstance().setFilename(ConfigFileType::Settings, filename);
- }
-
//////////////////////////
// ConfigFileEntryValue //
//////////////////////////
@@ -100,7 +50,7 @@
this->value_ = stripEnclosingQuotes(this->value_);
// Assemble the entry line
this->fileEntry_ = this->getKeyString() + " = ";
- if (this->bString_)
+ if (this->bString_ && !this->value_.empty())
this->fileEntry_ += '"' + addSlashes(this->value_) + '"';
else
this->fileEntry_ += this->value_;
@@ -144,7 +94,7 @@
}
}
- unsigned int ConfigFileSection::getVectorSize(const std::string& name)
+ unsigned int ConfigFileSection::getVectorSize(const std::string& name) const
{
unsigned int size = 0;
for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it)
@@ -165,8 +115,18 @@
return ('[' + this->name_ + "] " + this->additionalComment_);
}
- std::list<ConfigFileEntry*>::iterator ConfigFileSection::getEntryIterator(const std::string& name, const std::string& fallback, bool bString)
+ std::list<ConfigFileEntry*>::const_iterator ConfigFileSection::getEntryIterator(const std::string& name) const
{
+ for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it)
+ {
+ if ((*it)->getName() == name)
+ return it;
+ }
+ return this->entries_.end();
+ }
+
+ std::list<ConfigFileEntry*>::iterator ConfigFileSection::getOrCreateEntryIterator(const std::string& name, const std::string& fallback, bool bString)
+ {
for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ++it)
{
if ((*it)->getName() == name)
@@ -178,11 +138,21 @@
this->bUpdated_ = true;
- return this->entries_.insert(this->entries_.end(), static_cast<ConfigFileEntry*>(new ConfigFileEntryValue(name, fallback, bString)));
+ return this->entries_.insert(this->entries_.end(), new ConfigFileEntryValue(name, fallback, bString));
}
- std::list<ConfigFileEntry*>::iterator ConfigFileSection::getEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
+ std::list<ConfigFileEntry*>::const_iterator ConfigFileSection::getEntryIterator(const std::string& name, unsigned int index) const
{
+ for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it)
+ {
+ if (((*it)->getName() == name) && ((*it)->getIndex() == index))
+ return it;
+ }
+ return this->entries_.end();
+ }
+
+ std::list<ConfigFileEntry*>::iterator ConfigFileSection::getOrCreateEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
+ {
for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ++it)
{
if (((*it)->getName() == name) && ((*it)->getIndex() == index))
@@ -195,21 +165,27 @@
this->bUpdated_ = true;
if (index == 0)
- return this->entries_.insert(this->entries_.end(), static_cast<ConfigFileEntry*>(new ConfigFileEntryVectorValue(name, index, fallback, bString)));
+ return this->entries_.insert(this->entries_.end(), new ConfigFileEntryVectorValue(name, index, fallback, bString));
else
- return this->entries_.insert(++this->getEntryIterator(name, index - 1, "", bString), static_cast<ConfigFileEntry*>(new ConfigFileEntryVectorValue(name, index, fallback, bString)));
+ return this->entries_.insert(++this->getOrCreateEntryIterator(name, index - 1, "", bString), new ConfigFileEntryVectorValue(name, index, fallback, bString));
}
////////////////
// ConfigFile //
////////////////
+ ConfigFile::ConfigFile(const std::string& filename)
+ : filename_(filename)
+ , bUpdated_(false)
+ {
+ }
+
ConfigFile::~ConfigFile()
{
this->clear();
}
- void ConfigFile::load(bool bCreateIfNotExisting)
+ void ConfigFile::load()
{
// Be sure we start from new in the memory
this->clear();
@@ -299,7 +275,7 @@
if (convertValue(&index, line.substr(pos2 + 1, pos3 - pos2 - 1)))
{
// New array
- std::list<ConfigFileEntry*>::iterator it = newsection->getEntryIterator(getStripped(line.substr(0, pos2)), index, value, false);
+ std::list<ConfigFileEntry*>::iterator it = newsection->getOrCreateEntryIterator(getStripped(line.substr(0, pos2)), index, value, false);
(*it)->setValue(value);
(*it)->setComment(comment);
continue;
@@ -318,25 +294,26 @@
COUT(3) << "Loaded config file \"" << this->filename_ << "\"." << std::endl;
- // Save the file in case something changed (like stripped whitespaces)
+ // Save the file in case something changed (like stripped white space)
this->save();
-
- // Update all ConfigValueContainers
- this->updateConfigValues();
} // end file.is_open()
}
void ConfigFile::save() const
{
+ this->saveAs(this->filename_);
+ }
+
+ void ConfigFile::saveAs(const std::string& filename) const
+ {
std::ofstream file;
- file.open((PathConfig::getConfigPathString() + filename_).c_str(), std::fstream::out);
+ file.open((PathConfig::getConfigPathString() + filename).c_str(), std::fstream::out);
file.setf(std::ios::fixed, std::ios::floatfield);
file.precision(6);
if (!file.is_open())
{
- COUT(1) << "An error occurred in ConfigFileManager.cc:" << std::endl;
- COUT(1) << "Error: Couldn't open config-file \"" << this->filename_ << "\"." << std::endl;
+ COUT(1) << "Error: Couldn't open config-file \"" << filename << "\"." << std::endl;
return;
}
@@ -345,75 +322,55 @@
file << (*it)->getFileEntry() << std::endl;
for (std::list<ConfigFileEntry*>::const_iterator it_entries = (*it)->getEntriesBegin(); it_entries != (*it)->getEntriesEnd(); ++it_entries)
- {
file << (*it_entries)->getFileEntry() << std::endl;
- }
file << std::endl;
}
file.close();
- COUT(4) << "Saved config file \"" << this->filename_ << "\"." << std::endl;
+ COUT(4) << "Saved config file \"" << filename << "\"." << std::endl;
}
- void ConfigFile::saveAs(const std::string& filename)
+ void ConfigFile::clear()
{
- std::string temp = this->filename_;
- this->filename_ = filename;
- this->save();
- this->filename_ = temp;
+ for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); )
+ delete (*(it++));
+ this->sections_.clear();
}
- void ConfigFile::clean(bool bCleanComments)
+ const std::string& ConfigFile::getOrCreateValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString)
{
- for (std::list<ConfigFileSection*>::iterator it1 = this->sections_.begin(); it1 != this->sections_.end(); )
+ const std::string& output = this->getOrCreateSection(section)->getOrCreateValue(name, fallback, bString);
+ this->saveIfUpdated();
+ return output;
+ }
+
+ const std::string& ConfigFile::getOrCreateValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString)
+ {
+ const std::string& output = this->getOrCreateSection(section)->getOrCreateValue(name, index, fallback, bString);
+ this->saveIfUpdated();
+ return output;
+ }
+
+ void ConfigFile::deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex)
+ {
+ if (ConfigFileSection* sectionPtr = this->getSection(section))
{
- std::map<std::string, Identifier*>::const_iterator it2 = Identifier::getStringIdentifierMap().find((*it1)->getName());
- if (it2 != Identifier::getStringIdentifierMapEnd() && it2->second->hasConfigValues())
- {
- // The section exists, delete comment
- if (bCleanComments)
- (*it1)->setComment("");
- for (std::list<ConfigFileEntry*>::iterator it3 = (*it1)->entries_.begin(); it3 != (*it1)->entries_.end(); )
- {
- std::map<std::string, ConfigValueContainer*>::const_iterator it4 = it2->second->getConfigValueMap().find((*it3)->getName());
- if (it4 != it2->second->getConfigValueMapEnd())
- {
- // The config-value exists, delete comment
- if (bCleanComments)
- (*it3)->setComment("");
- ++it3;
- }
- else
- {
- // The config-value doesn't exist
- delete (*it3);
- (*it1)->entries_.erase(it3++);
- }
- }
- ++it1;
- }
- else
- {
- // The section doesn't exist
- delete (*it1);
- this->sections_.erase(it1++);
- }
+ sectionPtr->deleteVectorEntries(name, startindex);
+ this->save();
}
-
- // Save the file
- this->save();
}
- void ConfigFile::clear()
+ ConfigFileSection* ConfigFile::getSection(const std::string& section) const
{
- for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); )
- delete (*(it++));
- this->sections_.clear();
+ for (std::list<ConfigFileSection*>::const_iterator it = this->sections_.begin(); it != this->sections_.end(); ++it)
+ if ((*it)->getName() == section)
+ return (*it);
+ return NULL;
}
- ConfigFileSection* ConfigFile::getSection(const std::string& section)
+ ConfigFileSection* ConfigFile::getOrCreateSection(const std::string& section)
{
for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); ++it)
if ((*it)->getName() == section)
@@ -444,124 +401,207 @@
}
}
- void ConfigFile::updateConfigValues()
- {
- if (this->type_ == ConfigFileType::Settings)
- {
- for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getStringIdentifierMapBegin(); it != Identifier::getStringIdentifierMapEnd(); ++it)
- {
- if (it->second->hasConfigValues())
- {
- for (std::map<std::string, ConfigValueContainer*>::const_iterator it2 = it->second->getConfigValueMapBegin(); it2 != it->second->getConfigValueMapEnd(); ++it2)
- it2->second->update();
- it->second->updateConfigValues();
- }
- }
- }
- }
+ ////////////////////////
+ // SettingsConfigFile //
+ ////////////////////////
+ SettingsConfigFile* SettingsConfigFile::singletonPtr_s = 0;
- ///////////////////////
- // ConfigFileManager //
- ///////////////////////
+ SettingsConfigFile::SettingsConfigFile(const std::string& filename)
+ : ConfigFile(filename)
+ {
+ ConsoleCommand* command = createConsoleCommand(createFunctor(&ConfigFile::load, this), "reloadSettings");
+ CommandExecutor::addConsoleCommandShortcut(command);
+ command = createConsoleCommand(createFunctor(&SettingsConfigFile::setFilename, this), "setSettingsFile");
+ CommandExecutor::addConsoleCommandShortcut(command);
+ command = createConsoleCommand(createFunctor(&SettingsConfigFile::config, this), "config");
+ CommandExecutor::addConsoleCommandShortcut(command).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries()).argumentCompleter(2, autocompletion::settingsvalue());
+ command = createConsoleCommand(createFunctor(&SettingsConfigFile::tconfig, this), "tconfig");
+ CommandExecutor::addConsoleCommandShortcut(command).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries()).argumentCompleter(2, autocompletion::settingsvalue());
+ command = createConsoleCommand(createFunctor(&SettingsConfigFile::getConfig, this), "getConfig");
+ CommandExecutor::addConsoleCommandShortcut(command).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries());
+ }
- ConfigFileManager* ConfigFileManager::singletonPtr_s = 0;
-
- std::string ConfigFileManager::DEFAULT_CONFIG_FILE = "default.ini";
-
- ConfigFileManager::ConfigFileManager()
- : mininmalFreeType_(ConfigFileType::numberOfReservedTypes)
+ SettingsConfigFile::~SettingsConfigFile()
{
}
- ConfigFileManager::~ConfigFileManager()
+ void SettingsConfigFile::load()
{
- for (std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); )
- delete (it++)->second;
+ ConfigFile::load();
+ this->updateConfigValues();
}
- void ConfigFileManager::setFilename(ConfigFileType type, const std::string& filename)
+ void SettingsConfigFile::setFilename(const std::string& filename)
{
- std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type);
- if (it != this->configFiles_.end())
- {
- assert(it->second);
- delete it->second;
- }
- this->configFiles_[type] = new ConfigFile(filename, type);
- this->load(type);
+ ConfigFileManager::getInstance().setFilename(ConfigFileType::Settings, filename);
}
- void ConfigFileManager::load()
+ void SettingsConfigFile::addConfigValueContainer(ConfigValueContainer* container)
{
- for (std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
- it->second->load();
+ if (container == NULL)
+ return;
+ std::pair<std::string, ConfigValueContainer*> second(getLowercase(container->getName()), container);
+ this->containers_.insert(std::make_pair(getLowercase(container->getSectionName()), second));
+ this->sectionNames_.insert(container->getSectionName());
}
- void ConfigFileManager::save()
+ void SettingsConfigFile::removeConfigValueContainer(ConfigValueContainer* container)
{
- for (std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
- it->second->save();
+ if (container == NULL)
+ return;
+ const std::string& sectionLC = getLowercase(container->getSectionName());
+ ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC);
+ for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it)
+ {
+ if (it->second.second == container)
+ {
+ // Remove entry from section name set this was the last container for that section
+ if (upper == this->containers_.lower_bound(sectionLC))
+ this->sectionNames_.erase(container->getSectionName());
+ this->containers_.erase(it);
+ break;
+ }
+ }
}
- void ConfigFileManager::clean(bool bCleanComments)
+ void SettingsConfigFile::updateConfigValues()
{
- for (std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
- this->clean(it->first, bCleanComments);
+ for (ContainerMap::const_iterator it = this->containers_.begin(); it != this->containers_.end(); ++it)
+ {
+ it->second.second->update();
+ it->second.second->getIdentifier()->updateConfigValues();
+ }
}
- void ConfigFileManager::load(ConfigFileType type)
+ void SettingsConfigFile::clean(bool bCleanComments)
{
- this->getFile(type)->load();
+ for (std::list<ConfigFileSection*>::iterator itSection = this->sections_.begin(); itSection != this->sections_.end(); )
+ {
+ const std::string& sectionLC = getLowercase((*itSection)->getName());
+ ContainerMap::const_iterator lower = this->containers_.lower_bound(sectionLC);
+ ContainerMap::const_iterator upper = this->containers_.upper_bound(sectionLC);
+ if (lower != upper)
+ {
+ // The section exists, delete comment
+ if (bCleanComments)
+ (*itSection)->setComment("");
+ for (std::list<ConfigFileEntry*>::iterator itEntry = (*itSection)->entries_.begin(); itEntry != (*itSection)->entries_.end(); )
+ {
+ const std::string& entryLC = getLowercase((*itEntry)->getName());
+ bool bFound = false;
+ for (ContainerMap::const_iterator itContainer = lower; itContainer != upper; ++itContainer)
+ {
+ if (itContainer->second.first == entryLC)
+ {
+ // The config-value exists, delete comment
+ if (bCleanComments)
+ (*itEntry)->setComment("");
+ ++itEntry;
+ bFound = true;
+ break;
+ }
+ }
+ if (!bFound)
+ {
+ // The config-value doesn't exist
+ delete (*itEntry);
+ (*itSection)->entries_.erase(itEntry++);
+ }
+ }
+ ++itSection;
+ }
+ else
+ {
+ // The section doesn't exist
+ delete (*itSection);
+ this->sections_.erase(itSection++);
+ }
+ }
+
+ // Save the file
+ this->save();
}
- void ConfigFileManager::save(ConfigFileType type)
+ bool SettingsConfigFile::config(const std::string& section, const std::string& entry, const std::string& value)
{
- this->getFile(type)->save();
+ return this->configImpl(section, entry, value, &ConfigValueContainer::set);
}
- void ConfigFileManager::saveAs(ConfigFileType type, const std::string& saveFilename)
+ bool SettingsConfigFile::tconfig(const std::string& section, const std::string& entry, const std::string& value)
{
- this->getFile(type)->saveAs(saveFilename);
+ return this->configImpl(section, entry, value, &ConfigValueContainer::tset);
}
- void ConfigFileManager::clean(ConfigFileType type, bool bCleanComments)
+ bool SettingsConfigFile::configImpl(const std::string& section, const std::string& entry, const std::string& value, bool (ConfigValueContainer::*function)(const MultiType&))
{
- this->getFile(type)->clean(bCleanComments);
+ const std::string& sectionLC = getLowercase(section);
+ const std::string& entryLC = getLowercase(entry);
+ ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC);
+ for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it)
+ {
+ // Note: Config value vectors cannot be supported
+ if (it->second.first == entryLC && !it->second.second->isVector())
+ {
+ return (it->second.second->*function)(value);
+ }
+ }
+ return false;
}
- void ConfigFileManager::updateConfigValues()
+ std::string SettingsConfigFile::getConfig(const std::string& section, const std::string& entry)
{
- for (std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
- it->second->updateConfigValues();
+ const std::string& sectionLC = getLowercase(section);
+ const std::string& entryLC = getLowercase(entry);
+ ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC);
+ for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it)
+ {
+ // Note: Config value vectors cannot be supported
+ if (it->second.first == entryLC && ! it->second.second->isVector())
+ {
+ std::string value;
+ it->second.second->getValue<std::string, OrxonoxClass>(&value, NULL);
+ return value;
+ }
+ }
+ return "";
}
- void ConfigFileManager::updateConfigValues(ConfigFileType type)
+
+ ///////////////////////
+ // ConfigFileManager //
+ ///////////////////////
+
+ ConfigFileManager* ConfigFileManager::singletonPtr_s = 0;
+
+ ConfigFileManager::ConfigFileManager()
{
- this->getFile(type)->updateConfigValues();
+ this->configFiles_.assign(NULL);
}
- const std::string& ConfigFileManager::getFilename(ConfigFileType type)
+ ConfigFileManager::~ConfigFileManager()
{
- std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type);
- if (it != this->configFiles_.end())
- return it->second->getFilename();
- else
- return BLANKSTRING;
+ for (boost::array<ConfigFile*, 3>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
+ if (*it)
+ delete (*it);
}
- ConfigFile* ConfigFileManager::getFile(ConfigFileType type)
+ void ConfigFileManager::setFilename(ConfigFileType::Value type, const std::string& filename)
{
- std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type);
- if (it != this->configFiles_.end())
- return it->second;
- else
+ if (this->getConfigFile(type))
+ delete this->configFiles_[type];
+ // Create and load config file
+ switch (type)
{
- COUT(1) << "ConfigFileManager: Can't find a config file for type with ID " << static_cast<int>(type) << std::endl;
- COUT(1) << "Using " << DEFAULT_CONFIG_FILE << " file." << std::endl;
- this->setFilename(type, DEFAULT_CONFIG_FILE);
- return getFile(type);
+ case ConfigFileType::Settings:
+ this->configFiles_[type] = new SettingsConfigFile(filename);
+ break;
+ case ConfigFileType::JoyStickCalibration:
+ case ConfigFileType::CommandHistory:
+ this->configFiles_[type] = new ConfigFile(filename);
+ break;
}
+ this->configFiles_[type]->load();
}
}
Modified: code/branches/gamestate/src/libraries/core/ConfigFileManager.h
===================================================================
--- code/branches/gamestate/src/libraries/core/ConfigFileManager.h 2009-12-29 18:16:36 UTC (rev 6431)
+++ code/branches/gamestate/src/libraries/core/ConfigFileManager.h 2009-12-29 21:30:19 UTC (rev 6432)
@@ -31,40 +31,17 @@
#include "CorePrereqs.h"
-#include <cassert>
-#include <string>
#include <list>
#include <map>
+#include <set>
+#include <string>
+#include <boost/array.hpp>
-#include "util/OrxEnum.h"
#include "util/Singleton.h"
-// tolua_begin
-namespace orxonox
-{
- // tolua_end
- // Use int as config file type to have an arbitrary number of files
- struct ConfigFileType : OrxEnum<ConfigFileType>
- {
- OrxEnumConstructors(ConfigFileType);
+namespace orxonox // tolua_export
+{ // tolua_export
- static const int NoType = 0;
- static const int Settings = 1;
- static const int JoyStickCalibration = 2;
- static const int CommandHistory = 3;
-
- static const int numberOfReservedTypes = 1024;
- };
-
- _CoreExport bool config(const std::string& classname, const std::string& varname, const std::string& value); // tolua_export
- _CoreExport const std::string& getConfig(const std::string& classname, const std::string& varname); // tolua_export
- _CoreExport bool tconfig(const std::string& classname, const std::string& varname, const std::string& value);
- _CoreExport void reloadConfig();
- _CoreExport void saveConfig();
- _CoreExport void cleanConfig();
- _CoreExport void loadSettings(const std::string& filename);
-
-
/////////////////////
// ConfigFileEntry //
/////////////////////
@@ -195,6 +172,7 @@
class _CoreExport ConfigFileSection
{
friend class ConfigFile;
+ friend class SettingsConfigFile;
public:
inline ConfigFileSection(const std::string& name, const std::string& additionalComment = "")
@@ -211,17 +189,27 @@
{ this->additionalComment_ = comment; }
inline void setValue(const std::string& name, const std::string& value, bool bString)
- { this->getEntry(name, value, bString)->setValue(value); }
- inline const std::string& getValue(const std::string& name, const std::string& fallback, bool bString)
- { return this->getEntry(name, fallback, bString)->getValue(); }
+ { this->getOrCreateEntry(name, value, bString)->setValue(value); }
+ inline const std::string& getValue(const std::string& name, const std::string& fallback, bool bString) const
+ {
+ ConfigFileEntry* entry = this->getEntry(name);
+ return (entry ? entry->getValue() : BLANKSTRING);
+ }
+ inline const std::string& getOrCreateValue(const std::string& name, const std::string& fallback, bool bString)
+ { return this->getOrCreateEntry(name, fallback, bString)->getValue(); }
inline void setValue(const std::string& name, unsigned int index, const std::string& value, bool bString)
- { this->getEntry(name, index, value, bString)->setValue(value); }
- inline const std::string& getValue(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
- { return this->getEntry(name, index, fallback, bString)->getValue(); }
+ { this->getOrCreateEntry(name, index, value, bString)->setValue(value); }
+ inline const std::string& getValue(const std::string& name, unsigned int index) const
+ {
+ ConfigFileEntry* entry = this->getEntry(name, index);
+ return (entry ? entry->getValue() : BLANKSTRING);
+ }
+ inline const std::string& getOrCreateValue(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
+ { return this->getOrCreateEntry(name, index, fallback, bString)->getValue(); }
void deleteVectorEntries(const std::string& name, unsigned int startindex = 0);
- unsigned int getVectorSize(const std::string& name);
+ unsigned int getVectorSize(const std::string& name) const;
std::string getFileEntry() const;
@@ -233,13 +221,19 @@
std::list<ConfigFileEntry*>::const_iterator getEntriesEnd() const
{ return this->entries_.end(); }
- std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, const std::string& fallback, bool bString);
- std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString);
+ std::list<ConfigFileEntry*>::const_iterator getEntryIterator(const std::string& name) const;
+ std::list<ConfigFileEntry*>::iterator getOrCreateEntryIterator(const std::string& name, const std::string& fallback, bool bString);
+ std::list<ConfigFileEntry*>::const_iterator getEntryIterator(const std::string& name, unsigned int index) const;
+ std::list<ConfigFileEntry*>::iterator getOrCreateEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString);
- inline ConfigFileEntry* getEntry(const std::string& name, const std::string& fallback, bool bString)
- { return (*this->getEntryIterator(name, fallback, bString)); }
- inline ConfigFileEntry* getEntry(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
- { return (*this->getEntryIterator(name, index, fallback, bString)); }
+ inline ConfigFileEntry* getEntry(const std::string& name) const
+ { return (*this->getEntryIterator(name)); }
+ inline ConfigFileEntry* getOrCreateEntry(const std::string& name, const std::string& fallback, bool bString)
+ { return (*this->getOrCreateEntryIterator(name, fallback, bString)); }
+ inline ConfigFileEntry* getEntry(const std::string& name, unsigned int index) const
+ { return (*this->getEntryIterator(name, index)); }
+ inline ConfigFileEntry* getOrCreateEntry(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
+ { return (*this->getOrCreateEntryIterator(name, index, fallback, bString)); }
std::string name_;
std::string additionalComment_;
@@ -254,49 +248,105 @@
class _CoreExport ConfigFile
{
public:
- inline ConfigFile(const std::string& filename, ConfigFileType type)
- : filename_(filename)
- , type_(type)
- , bUpdated_(false)
- { }
- ~ConfigFile();
+ ConfigFile(const std::string& filename);
+ virtual ~ConfigFile();
- void load(bool bCreateIfNotExisting = true);
- void save() const;
- void saveAs(const std::string& filename);
- void clean(bool bCleanComments = false);
- void clear();
+ virtual void load();
+ virtual void save() const;
+ virtual void saveAs(const std::string& filename) const;
+ virtual void clear();
- const std::string& getFilename() { return this->filename_; }
+ inline const std::string& getFilename()
+ { return this->filename_; }
inline void setValue(const std::string& section, const std::string& name, const std::string& value, bool bString)
- { this->getSection(section)->setValue(name, value, bString); this->save(); }
- inline const std::string& getValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString)
- { const std::string& output = this->getSection(section)->getValue(name, fallback, bString); this->saveIfUpdated(); return output; }
+ {
+ this->getOrCreateSection(section)->setValue(name, value, bString);
+ this->save();
+ }
+ inline const std::string& getValue(const std::string& section, const std::string& name, bool bString) const
+ {
+ ConfigFileSection* sectionPtr = this->getSection(section);
+ return (sectionPtr ? sectionPtr->getValue(name, bString) : BLANKSTRING);
+ }
+ const std::string& getOrCreateValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString);
inline void setValue(const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString)
- { this->getSection(section)->setValue(name, index, value, bString); this->save(); }
- inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString)
- { const std::string& output = this->getSection(section)->getValue(name, index, fallback, bString); this->saveIfUpdated(); return output; }
+ {
+ this->getSection(section)->setValue(name, index, value, bString);
+ this->save();
+ }
+ inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index) const
+ {
+ ConfigFileSection* sectionPtr = this->getSection(section);
+ return (sectionPtr ? sectionPtr->getValue(name, index) : BLANKSTRING);
+ }
+ const std::string& getOrCreateValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString);
- inline void deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex = 0)
- { this->getSection(section)->deleteVectorEntries(name, startindex); }
- inline unsigned int getVectorSize(const std::string& section, const std::string& name)
- { return this->getSection(section)->getVectorSize(name); }
+ void deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex = 0);
+ inline unsigned int getVectorSize(const std::string& section, const std::string& name) const
+ {
+ ConfigFileSection* sectionPtr = this->getSection(section);
+ return (sectionPtr ? sectionPtr->getVectorSize(name) : 0);
+ }
- void updateConfigValues();
+ protected:
+ ConfigFileSection* getSection(const std::string& section) const;
+ ConfigFileSection* getOrCreateSection(const std::string& section);
+ std::list<ConfigFileSection*> sections_;
+
private:
- ConfigFileSection* getSection(const std::string& section);
void saveIfUpdated();
-
- std::string filename_;
- ConfigFileType type_;
- std::list<ConfigFileSection*> sections_;
+ const std::string filename_;
bool bUpdated_;
};
+ ////////////////////////
+ // SettingsConfigFile //
+ ////////////////////////
+ class _CoreExport SettingsConfigFile // tolua_export
+ : public ConfigFile, public Singleton<SettingsConfigFile>
+ { // tolua_export
+ friend class Singleton<SettingsConfigFile>;
+
+ public:
+ typedef std::multimap<std::string, std::pair<std::string, ConfigValueContainer*> > ContainerMap;
+
+ SettingsConfigFile(const std::string& filename);
+ ~SettingsConfigFile();
+
+ void load(); // tolua_export
+ void setFilename(const std::string& filename); // tolua_export
+ void clean(bool bCleanComments = false); // tolua_export
+
+ bool config(const std::string& section, const std::string& entry, const std::string& value); // tolua_export
+ bool tconfig(const std::string& section, const std::string& entry, const std::string& value); // tolua_export
+ std::string getConfig(const std::string& section, const std::string& entry); // tolua_export
+
+ void addConfigValueContainer(ConfigValueContainer* container);
+ void removeConfigValueContainer(ConfigValueContainer* container);
+
+ inline const std::set<std::string>& getSectionNames()
+ { return this->sectionNames_; }
+ inline ContainerMap::const_iterator getContainerLowerBound(const std::string section)
+ { return this->containers_.lower_bound(section); }
+ inline ContainerMap::const_iterator getContainerUpperBound(const std::string section)
+ { return this->containers_.upper_bound(section); }
+
+ static SettingsConfigFile& getInstance() { return Singleton<SettingsConfigFile>::getInstance(); } // tolua_export
+
+ private:
+ void updateConfigValues();
+ bool configImpl(const std::string& section, const std::string& entry, const std::string& value, bool (ConfigValueContainer::*function)(const MultiType&));
+
+ ContainerMap containers_;
+ std::set<std::string> sectionNames_;
+ static SettingsConfigFile* singletonPtr_s;
+ }; // tolua_export
+
+
///////////////////////
// ConfigFileManager //
///////////////////////
@@ -307,48 +357,18 @@
ConfigFileManager();
~ConfigFileManager();
- void load();
- void save();
- void clean(bool bCleanComments = false);
+ void setFilename(ConfigFileType::Value type, const std::string& filename);
- void setFilename(ConfigFileType type, const std::string& filename);
- const std::string& getFilename(ConfigFileType type);
+ inline ConfigFile* getConfigFile(ConfigFileType::Value type)
+ {
+ // Check array bounds
+ return configFiles_.at(type);
+ }
- ConfigFileType getNewConfigFileType() { return mininmalFreeType_++; }
-
- void load(ConfigFileType type);
- void save(ConfigFileType type);
- void saveAs(ConfigFileType type, const std::string& saveFilename);
- void clean(ConfigFileType type, bool bCleanComments = false);
-
- inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& value, bool bString)
- { this->getFile(type)->setValue(section, name, value, bString); }
- inline const std::string& getValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& fallback, bool bString)
- { return this->getFile(type)->getValue(section, name, fallback, bString); }
-
- inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString)
- { this->getFile(type)->setValue(section, name, index, value, bString); }
- inline const std::string& getValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString)
- { return this->getFile(type)->getValue(section, name, index, fallback, bString); }
-
- inline void deleteVectorEntries(ConfigFileType type, const std::string& section, const std::string& name, unsigned int startindex = 0)
- { this->getFile(type)->deleteVectorEntries(section, name, startindex); }
- inline unsigned int getVectorSize(ConfigFileType type, const std::string& section, const std::string& name)
- { return this->getFile(type)->getVectorSize(section, name); }
-
- void updateConfigValues();
- void updateConfigValues(ConfigFileType type);
-
- static std::string DEFAULT_CONFIG_FILE;
-
private:
ConfigFileManager(const ConfigFileManager&);
- ConfigFile* getFile(ConfigFileType type);
-
- std::map<ConfigFileType, ConfigFile*> configFiles_;
- unsigned int mininmalFreeType_;
-
+ boost::array<ConfigFile*, 3> configFiles_;
static ConfigFileManager* singletonPtr_s;
};
} // tolua_export
Modified: code/branches/gamestate/src/libraries/core/ConfigValueContainer.cc
===================================================================
--- code/branches/gamestate/src/libraries/core/ConfigValueContainer.cc 2009-12-29 18:16:36 UTC (rev 6431)
+++ code/branches/gamestate/src/libraries/core/ConfigValueContainer.cc 2009-12-29 21:30:19 UTC (rev 6432)
@@ -35,6 +35,7 @@
#include "util/Convert.h"
#include "util/SubString.h"
+#include "ConfigFileManager.h"
#include "Language.h"
namespace orxonox
@@ -42,9 +43,9 @@
const unsigned int MAX_VECTOR_INDEX = 255; // to avoid up to 4*10^9 vector entries in the config file after accidentally using a wrong argument
/**
- @brief Initializes the ConfigValueContainer with defaultvalues.
+ @brief Initializes the ConfigValueContainer with default values.
*/
- void ConfigValueContainer::init(ConfigFileType type, Identifier* identifier, const std::string& sectionname, const std::string& varname)
+ void ConfigValueContainer::init(ConfigFileType::Value type, Identifier* identifier, const std::string& sectionname, const std::string& varname)
{
this->type_ = type;
this->identifier_ = identifier;
@@ -54,6 +55,10 @@
this->bContainerIsNew_ = true;
this->bDoInitialCallback_ = false;
this->bAddedDescription_ = false;
+
+ // Register containers for general settings
+ if (this->type_ == ConfigFileType::Settings)
+ SettingsConfigFile::getInstance().addConfigValueContainer(this);
}
/**
@@ -77,7 +82,7 @@
for (unsigned int i = 0; i < this->valueVector_.size(); i++)
{
- ConfigFileManager::getInstance().getValue(this->type_, this->sectionname_, this->varname_, i, this->valueVector_[i], this->value_.isType(MT_Type::String));
+ ConfigFileManager::getInstance().getConfigFile(this->type_)->getOrCreateValue(this->sectionname_, this->varname_, i, this->valueVector_[i], this->value_.isType(MT_Type::String));
this->defvalueStringVector_.push_back(this->valueVector_[i]);
}
@@ -91,6 +96,10 @@
{
if (this->callback_)
delete this->callback_;
+
+ // Unregister general settings containers
+ if (this->type_ == ConfigFileType::Settings && SettingsConfigFile::exists())
+ SettingsConfigFile::getInstance().removeConfigValueContainer(this);
}
/**
@@ -108,7 +117,7 @@
{
if (this->tset(input))
{
- ConfigFileManager::getInstance().setValue(this->type_, this->sectionname_, this->varname_, input, this->value_.isType(MT_Type::String));
+ ConfigFileManager::getInstance().getConfigFile(this->type_)->setValue(this->sectionname_, this->varname_, input, this->value_.isType(MT_Type::String));
return true;
}
}
@@ -127,7 +136,7 @@
{
if (this->tset(index, input))
{
- ConfigFileManager::getInstance().setValue(this->type_, this->sectionname_, this->varname_, index, input, this->value_.isType(MT_Type::String));
+ ConfigFileManager::getInstance().getConfigFile(this->type_)->setValue(this->sectionname_, this->varname_, index, input, this->value_.isType(MT_Type::String));
return true;
}
}
@@ -227,8 +236,8 @@
// Erase the entry from the vector, change (shift) all entries beginning with index in the config file, remove the last entry from the file
this->valueVector_.erase(this->valueVector_.begin() + index);
for (unsigned int i = index; i < this->valueVector_.size(); i++)
- ConfigFileManager::getInstance().setValue(this->type_, this->sectionname_, this->varname_, i, this->valueVector_[i], this->value_.isType(MT_Type::String));
- ConfigFileManager::getInstance().deleteVectorEntries(this->type_, this->sectionname_, this->varname_, this->valueVector_.size());
+ ConfigFileManager::getInstance().getConfigFile(this->type_)->setValue(this->sectionname_, this->varname_, i, this->valueVector_[i], this->value_.isType(MT_Type::String));
+ ConfigFileManager::getInstance().getConfigFile(this->type_)->deleteVectorEntries(this->sectionname_, this->varname_, this->valueVector_.size());
return true;
}
@@ -252,7 +261,7 @@
for (unsigned int i = 0; i < this->defvalueStringVector_.size(); i++)
if (!this->set(i, this->defvalueStringVector_[i]))
success = false;
- ConfigFileManager::getInstance().deleteVectorEntries(this->type_, this->sectionname_, this->varname_, this->defvalueStringVector_.size());
+ ConfigFileManager::getInstance().getConfigFile(this->type_)->deleteVectorEntries(this->sectionname_, this->varname_, this->defvalueStringVector_.size());
return success;
}
}
@@ -263,20 +272,20 @@
void ConfigValueContainer::update()
{
if (!this->bIsVector_)
- this->value_ = ConfigFileManager::getInstance().getValue(this->type_, this->sectionname_, this->varname_, this->defvalueString_, this->value_.isType(MT_Type::String));
+ this->value_ = ConfigFileManager::getInstance().getConfigFile(this->type_)->getOrCreateValue(this->sectionname_, this->varname_, this->defvalueString_, this->value_.isType(MT_Type::String));
else
{
this->valueVector_.clear();
- unsigned int vectorSize = ConfigFileManager::getInstance().getVectorSize(this->type_, this->sectionname_, this->varname_);
+ unsigned int vectorSize = ConfigFileManager::getInstance().getConfigFile(this->type_)->getVectorSize(this->sectionname_, this->varname_);
for (unsigned int i = 0; i < vectorSize; i++)
{
if (i < this->defvalueStringVector_.size())
{
- this->value_ = ConfigFileManager::getInstance().getValue(this->type_, this->sectionname_, this->varname_, i, this->defvalueStringVector_[i], this->value_.isType(MT_Type::String));
+ this->value_ = ConfigFileManager::getInstance().getConfigFile(this->type_)->getOrCreateValue(this->sectionname_, this->varname_, i, this->defvalueStringVector_[i], this->value_.isType(MT_Type::String));
}
else
{
- this->value_ = ConfigFileManager::getInstance().getValue(this->type_, this->sectionname_, this->varname_, i, MultiType(), this->value_.isType(MT_Type::String));
+ this->value_ = ConfigFileManager::getInstance().getConfigFile(this->type_)->getOrCreateValue(this->sectionname_, this->varname_, i, MultiType(), this->value_.isType(MT_Type::String));
}
this->valueVector_.push_back(this->value_);
Modified: code/branches/gamestate/src/libraries/core/ConfigValueContainer.h
===================================================================
--- code/branches/gamestate/src/libraries/core/ConfigValueContainer.h 2009-12-29 18:16:36 UTC (rev 6431)
+++ code/branches/gamestate/src/libraries/core/ConfigValueContainer.h 2009-12-29 21:30:19 UTC (rev 6432)
@@ -49,7 +49,6 @@
#include <vector>
#include "util/MultiType.h"
-#include "ConfigFileManager.h"
#include "Identifier.h"
namespace orxonox
@@ -107,7 +106,7 @@
@param value Only needed do determine the right type.
*/
template <class D, class V>
- ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& sectionname, const std::string& varname, const D& defvalue, const V& value)
+ ConfigValueContainer(ConfigFileType::Value type, Identifier* identifier, const std::string& sectionname, const std::string& varname, const D& defvalue, const V& value)
{
this->init(type, identifier, sectionname, varname);
this->initValue(static_cast<V>(defvalue));
@@ -121,7 +120,7 @@
@param defvalue The default-value
*/
template <class D, class V>
- ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& sectionname, const std::string& varname, const std::vector<D>& defvalue, const std::vector<V>& value)
+ ConfigValueContainer(ConfigFileType::Value type, Identifier* identifier, const std::string& sectionname, const std::string& varname, const std::vector<D>& defvalue, const std::vector<V>& value)
{
this->init(type, identifier, sectionname, varname);
@@ -216,9 +215,12 @@
/** @brief Returns the name of this container. */
inline const std::string& getName() const
{ return this->varname_; }
- /** @brief Retuns the name of the section this config value is in. */
+ /** @brief Returns the name of the section this config value is in. */
inline const std::string& getSectionName() const
{ return this->sectionname_; }
+ /** @brief Returns the associated identifier (can be NULL). */
+ inline Identifier* getIdentifier() const
+ { return this->identifier_; }
/** @brief Returns true if this config-value is a vector */
inline bool isVector() const
{ return this->bIsVector_; }
@@ -270,14 +272,14 @@
{ return this->value_.getTypename(); }
private:
- void init(ConfigFileType type, Identifier* identifier, const std::string& sectionname, const std::string& varname);
+ void init(ConfigFileType::Value type, Identifier* identifier, const std::string& sectionname, const std::string& varname);
void initValue(const MultiType& defvalue);
void initVector();
bool callFunctionWithIndex(bool (ConfigValueContainer::* function) (unsigned int, const MultiType&), const std::string& input);
bool bIsVector_; //!< True if the container contains a std::vector
- ConfigFileType type_; //!< The type of the corresponding config-file
+ ConfigFileType::Value type_; //!< The type of the corresponding config-file
Identifier* identifier_; //!< The identifier of the class
std::string sectionname_; //!< The name of the class the variable belongs to
std::string varname_; //!< The name of the variable
Modified: code/branches/gamestate/src/libraries/core/ConfigValueIncludes.h
===================================================================
--- code/branches/gamestate/src/libraries/core/ConfigValueIncludes.h 2009-12-29 18:16:36 UTC (rev 6431)
+++ code/branches/gamestate/src/libraries/core/ConfigValueIncludes.h 2009-12-29 21:30:19 UTC (rev 6432)
@@ -39,7 +39,6 @@
#include "Identifier.h"
#include "ConfigValueContainer.h"
-#include "ConfigFileManager.h"
namespace orxonox
{
@@ -60,7 +59,7 @@
Value to be used if it cannot be read from the ini file
*/
template <class T, class D, class V>
- inline ConfigValueContainer& setConfigValueGeneric(T* object, V* variable, ConfigFileType type, const std::string& sectionName, const std::string& entryName, const D& defaultValue)
+ inline ConfigValueContainer& setConfigValueGeneric(T* object, V* variable, ConfigFileType::Value type, const std::string& sectionName, const std::string& entryName, const D& defaultValue)
{
ConfigValueContainer* container = ClassIdentifier<T>::getIdentifier()->getConfigValueContainer(entryName);
if (!container)
Modified: code/branches/gamestate/src/libraries/core/CorePrereqs.h
===================================================================
--- code/branches/gamestate/src/libraries/core/CorePrereqs.h 2009-12-29 18:16:36 UTC (rev 6431)
+++ code/branches/gamestate/src/libraries/core/CorePrereqs.h 2009-12-29 21:30:19 UTC (rev 6432)
@@ -83,6 +83,17 @@
};
}
+ namespace ConfigFileType
+ {
+ enum Value
+ {
+ Settings,
+ JoyStickCalibration,
+ CommandHistory
+ // Don't forget to adjust the array size in the ConfigFileManager when adding a new entry here!
+ };
+ }
+
namespace KeybindMode
{
enum Value
@@ -123,7 +134,6 @@
class ConfigFileEntryValue;
class ConfigFileManager;
class ConfigFileSection;
- struct ConfigFileType;
class ConfigValueContainer;
class ConsoleCommand;
class Core;
@@ -171,6 +181,7 @@
class OrxonoxClass;
class PathConfig;
struct ResourceInfo;
+ class SettingsConfigFile;
class Shell;
class ShellListener;
template <class T>
Modified: code/branches/gamestate/src/libraries/core/Identifier.cc
===================================================================
--- code/branches/gamestate/src/libraries/core/Identifier.cc 2009-12-29 18:16:36 UTC (rev 6431)
+++ code/branches/gamestate/src/libraries/core/Identifier.cc 2009-12-29 21:30:19 UTC (rev 6432)
@@ -415,7 +415,6 @@
this->bHasConfigValues_ = true;
this->configValues_[varname] = container;
- this->configValues_LC_[getLowercase(varname)] = container;
}
/**
@@ -433,20 +432,6 @@
}
/**
- @brief Returns the ConfigValueContainer of a variable, given by the string of its name in lowercase.
- @param varname The name of the variable in lowercase
- @return The ConfigValueContainer
- */
- ConfigValueContainer* Identifier::getLowercaseConfigValueContainer(const std::string& varname)
- {
- std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_LC_.find(varname);
- if (it != configValues_LC_.end())
- return it->second;
- else
- return 0;
- }
-
- /**
@brief Adds a new console command of this class.
@param executor The executor of the command
@param bCreateShortcut If this is true a shortcut gets created so you don't have to add the classname to access this command
Modified: code/branches/gamestate/src/libraries/core/Identifier.h
===================================================================
--- code/branches/gamestate/src/libraries/core/Identifier.h 2009-12-29 18:16:36 UTC (rev 6431)
+++ code/branches/gamestate/src/libraries/core/Identifier.h 2009-12-29 21:30:19 UTC (rev 6432)
@@ -199,23 +199,8 @@
/** @brief Returns true if this class has at least one config value. @return True if this class has at least one config value */
inline bool hasConfigValues() const { return this->bHasConfigValues_; }
- /** @brief Returns the map that stores all config values. @return The const_iterator */
- inline const std::map<std::string, ConfigValueContainer*>& getConfigValueMap() const { return this->configValues_; }
- /** @brief Returns a const_iterator to the beginning of the map that stores all config values. @return The const_iterator */
- inline std::map<std::string, ConfigValueContainer*>::const_iterator getConfigValueMapBegin() const { return this->configValues_.begin(); }
- /** @brief Returns a const_iterator to the end of the map that stores all config values. @return The const_iterator */
- inline std::map<std::string, ConfigValueContainer*>::const_iterator getConfigValueMapEnd() const { return this->configValues_.end(); }
-
- /** @brief Returns the map that stores all config values with their names in lowercase. @return The const_iterator */
- inline const std::map<std::string, ConfigValueContainer*>& getLowercaseConfigValueMap() const { return this->configValues_LC_; }
- /** @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 */
- inline std::map<std::string, ConfigValueContainer*>::const_iterator getLowercaseConfigValueMapBegin() const { return this->configValues_LC_.begin(); }
- /** @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 */
- inline std::map<std::string, ConfigValueContainer*>::const_iterator getLowercaseConfigValueMapEnd() const { return this->configValues_LC_.end(); }
-
void addConfigValueContainer(const std::string& varname, ConfigValueContainer* container);
ConfigValueContainer* getConfigValueContainer(const std::string& varname);
- ConfigValueContainer* getLowercaseConfigValueContainer(const std::string& varname);
////////////////////////////
@@ -319,7 +304,6 @@
bool bHasConfigValues_; //!< True if this class has at least one assigned config value
std::map<std::string, ConfigValueContainer*> configValues_; //!< A map to link the string of configurable variables with their ConfigValueContainer
- std::map<std::string, ConfigValueContainer*> configValues_LC_; //!< A map to link the string of configurable variables with their ConfigValueContainer
bool bHasConsoleCommands_; //!< True if this class has at least one assigned console command
std::map<std::string, ConsoleCommand*> consoleCommands_; //!< All console commands of this class
Modified: code/branches/gamestate/src/libraries/core/Shell.cc
===================================================================
--- code/branches/gamestate/src/libraries/core/Shell.cc 2009-12-29 18:16:36 UTC (rev 6431)
+++ code/branches/gamestate/src/libraries/core/Shell.cc 2009-12-29 21:30:19 UTC (rev 6432)
@@ -33,6 +33,7 @@
#include "util/SubString.h"
#include "CommandExecutor.h"
#include "CoreIncludes.h"
+#include "ConfigFileManager.h"
#include "ConfigValueIncludes.h"
#include "ConsoleCommand.h"
Modified: code/branches/gamestate/src/libraries/core/Shell.h
===================================================================
--- code/branches/gamestate/src/libraries/core/Shell.h 2009-12-29 18:16:36 UTC (rev 6431)
+++ code/branches/gamestate/src/libraries/core/Shell.h 2009-12-29 21:30:19 UTC (rev 6432)
@@ -38,7 +38,6 @@
#include "util/OutputHandler.h"
#include "OrxonoxClass.h"
-#include "ConfigFileManager.h"
#include "input/InputBuffer.h"
namespace orxonox
Modified: code/branches/gamestate/src/libraries/core/input/Button.cc
===================================================================
--- code/branches/gamestate/src/libraries/core/input/Button.cc 2009-12-29 18:16:36 UTC (rev 6431)
+++ code/branches/gamestate/src/libraries/core/input/Button.cc 2009-12-29 21:30:19 UTC (rev 6432)
@@ -41,6 +41,7 @@
#include "core/ConsoleCommand.h"
#include "core/CommandEvaluation.h"
#include "core/CommandExecutor.h"
+#include "core/ConfigFileManager.h"
namespace orxonox
{
@@ -80,16 +81,16 @@
this->bindingString_.clear();
}
- void Button::readBinding(ConfigFileType type)
+ void Button::readBinding(ConfigFile* configFile)
{
- const std::string& binding = ConfigFileManager::getInstance().getValue(type, groupName_, name_, "", true);
+ const std::string& binding = configFile->getOrCreateValue(groupName_, name_, "", true);
this->parse(binding);
}
- void Button::setBinding(ConfigFileType type, const std::string& binding, bool bTemporary)
+ void Button::setBinding(ConfigFile* configFile, const std::string& binding, bool bTemporary)
{
if (!bTemporary)
- ConfigFileManager::getInstance().setValue(type, groupName_, name_, binding, true);
+ configFile->setValue(groupName_, name_, binding, true);
this->parse(binding);
}
Modified: code/branches/gamestate/src/libraries/core/input/Button.h
===================================================================
--- code/branches/gamestate/src/libraries/core/input/Button.h 2009-12-29 18:16:36 UTC (rev 6431)
+++ code/branches/gamestate/src/libraries/core/input/Button.h 2009-12-29 21:30:19 UTC (rev 6432)
@@ -40,7 +40,6 @@
#include <string>
#include <vector>
#include "InputCommands.h"
-#include "core/ConfigFileManager.h"
namespace orxonox
{
@@ -52,8 +51,8 @@
virtual void clear();
virtual bool addParamCommand(ParamCommand* command) { return false; }
void parse(const std::string& binding);
- void readBinding(ConfigFileType type);
- void setBinding(ConfigFileType type, const std::string& binding, bool bTemporary);
+ void readBinding(ConfigFile* configFile);
+ void setBinding(ConfigFile* configFile, const std::string& binding, bool bTemporary);
bool execute(KeybindMode::Value mode, float abs = 1.0f, float rel = 1.0f);
//! The configured string value
Modified: code/branches/gamestate/src/libraries/core/input/JoyStick.cc
===================================================================
--- code/branches/gamestate/src/libraries/core/input/JoyStick.cc 2009-12-29 18:16:36 UTC (rev 6431)
+++ code/branches/gamestate/src/libraries/core/input/JoyStick.cc 2009-12-29 21:30:19 UTC (rev 6432)
@@ -105,14 +105,14 @@
void loadCalibration(std::vector<int>& list, const std::string& sectionName, const std::string& valueName, size_t size, int defaultValue)
{
list.resize(size);
- unsigned int configValueVectorSize = ConfigFileManager::getInstance().getVectorSize(ConfigFileType::JoyStickCalibration, sectionName, valueName);
+ unsigned int configValueVectorSize = ConfigFileManager::getInstance().getConfigFile(ConfigFileType::JoyStickCalibration)->getVectorSize(sectionName, valueName);
if (configValueVectorSize > size)
configValueVectorSize = size;
for (unsigned int i = 0; i < configValueVectorSize; ++i)
{
- list[i] = multi_cast<int>(ConfigFileManager::getInstance().getValue(
- ConfigFileType::JoyStickCalibration, sectionName, valueName, i, multi_cast<std::string>(defaultValue), false));
+ list[i] = multi_cast<int>(ConfigFileManager::getInstance().getConfigFile(ConfigFileType::JoyStickCalibration)
+ ->getOrCreateValue(sectionName, valueName, i, multi_cast<std::string>(defaultValue), false));
}
// fill the rest with default values
@@ -152,18 +152,18 @@
// Minimum values
if (configMinValues_[i] == INT_MAX)
configMinValues_[i] = -32768;
- ConfigFileManager::getInstance().setValue(ConfigFileType::JoyStickCalibration,
- deviceName_, "MinValue", i, multi_cast<std::string>(configMinValues_[i]), false);
+ ConfigFileManager::getInstance().getConfigFile(ConfigFileType::JoyStickCalibration)
+ ->getOrCreateValue(deviceName_, "MinValue", i, multi_cast<std::string>(configMinValues_[i]), false);
// Maximum values
if (configMaxValues_[i] == INT_MIN)
configMaxValues_[i] = 32767;
- ConfigFileManager::getInstance().setValue(ConfigFileType::JoyStickCalibration,
- deviceName_, "MaxValue", i, multi_cast<std::string>(configMaxValues_[i]), false);
+ ConfigFileManager::getInstance().getConfigFile(ConfigFileType::JoyStickCalibration)
+ ->getOrCreateValue(deviceName_, "MaxValue", i, multi_cast<std::string>(configMaxValues_[i]), false);
// Middle values
- ConfigFileManager::getInstance().setValue(ConfigFileType::JoyStickCalibration,
- deviceName_, "ZeroValue", i, multi_cast<std::string>(configZeroValues_[i]), false);
+ ConfigFileManager::getInstance().getConfigFile(ConfigFileType::JoyStickCalibration)
+ ->getOrCreateValue(deviceName_, "ZeroValue", i, multi_cast<std::string>(configZeroValues_[i]), false);
}
this->evaluateCalibration();
Modified: code/branches/gamestate/src/libraries/core/input/KeyBinder.cc
===================================================================
--- code/branches/gamestate/src/libraries/core/input/KeyBinder.cc 2009-12-29 18:16:36 UTC (rev 6431)
+++ code/branches/gamestate/src/libraries/core/input/KeyBinder.cc 2009-12-29 21:30:19 UTC (rev 6432)
@@ -48,6 +48,7 @@
KeyBinder::KeyBinder(const std::string& filename)
: deriveTime_(0.0f)
, filename_(filename)
+ , configFile_(NULL)
{
mouseRelative_[0] = 0;
mouseRelative_[1] = 0;
@@ -93,9 +94,6 @@
mouseAxes_[i].groupName_ = "MouseAxes";
}
- // We might not even load any bindings at all (KeyDetector for instance)
- this->configFile_ = ConfigFileType::NoType;
-
// initialise joy sticks separatly to allow for reloading
this->JoyStickQuantityChanged(this->getJoyStickList());
@@ -162,7 +160,7 @@
compilePointerLists();
// load the bindings if required
- if (configFile_ != ConfigFileType::NoType)
+ if (configFile_ != NULL)
{
for (unsigned int iDev = oldValue; iDev < joySticks_.size(); ++iDev)
{
@@ -248,11 +246,9 @@
{
COUT(3) << "KeyBinder: Loading key bindings..." << std::endl;
- // Get a new ConfigFileType from the ConfigFileManager
- this->configFile_ = ConfigFileManager::getInstance().getNewConfigFileType();
+ this->configFile_ = new ConfigFile(this->filename_);
+ this->configFile_->load();
- ConfigFileManager::getInstance().setFilename(this->configFile_, this->filename_);
-
// Parse bindings and create the ConfigValueContainers if necessary
for (std::map<std::string, Button*>::const_iterator it = allButtons_.begin(); it != allButtons_.end(); ++it)
{
Modified: code/branches/gamestate/src/libraries/core/input/KeyBinder.h
===================================================================
--- code/branches/gamestate/src/libraries/core/input/KeyBinder.h 2009-12-29 18:16:36 UTC (rev 6431)
+++ code/branches/gamestate/src/libraries/core/input/KeyBinder.h 2009-12-29 21:30:19 UTC (rev 6432)
@@ -37,7 +37,6 @@
#include <map>
#include <boost/shared_ptr.hpp>
-#include "core/ConfigFileManager.h"
#include "InputHandler.h"
#include "Button.h"
#include "HalfAxis.h"
@@ -156,8 +155,8 @@
//! Name of the file used in this KeyBinder (constant!)
const std::string filename_;
- //! Config file used. ConfigFileType::NoType in case of KeyDetector. Also indicates whether we've already loaded.
- ConfigFileType configFile_;
+ //! Config file used. NULL in case of KeyDetector. Also indicates whether we've already loaded.
+ ConfigFile* configFile_;
private:
void addButtonToCommand(const std::string& command, Button* button);
More information about the Orxonox-commit
mailing list