Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6432


Ignore:
Timestamp:
Dec 29, 2009, 10:30:19 PM (14 years ago)
Author:
rgrieder
Message:

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)

Location:
code/branches/gamestate
Files:
18 edited

Legend:

Unmodified
Added
Removed
  • code/branches/gamestate/data/lua/LuaStateInit.lua

    r6417 r6432  
    5858  orxonox.CommandExecutor:execute(command)
    5959end
     60
     61-- Convenience function for config values
     62orxonox.getConfig = function(section, entry)
     63  return orxonox.SettingsConfigFile:getInstance():getConfig(section, entry)
     64end
     65orxonox.config = function(section, entry, value)
     66  return orxonox.SettingsConfigFile:getInstance():config(section, entry, value)
     67end
     68orxonox.tconfig = function(section, entry, value)
     69  return orxonox.SettingsConfigFile:getInstance():tconfig(section, entry, value)
     70end
  • code/branches/gamestate/src/libraries/core/ArgumentCompletionFunctions.cc

    r6417 r6432  
    3636#include "util/StringUtils.h"
    3737#include "Identifier.h"
     38#include "ConfigFileManager.h"
    3839#include "ConfigValueContainer.h"
    3940#include "TclThreadManager.h"
     
    9697        }
    9798
    98         ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(configvalueclasses)()
     99        ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(settingssections)()
    99100        {
    100             ArgumentCompletionList classlist;
     101            ArgumentCompletionList sectionList;
    101102
    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)));
    105106
    106             return classlist;
     107            return sectionList;
    107108        }
    108109
    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)
    110111        {
    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);
    113115
    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)
    115132            {
    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                }
    118138            }
    119139
    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;
    137141        }
    138142
  • code/branches/gamestate/src/libraries/core/ArgumentCompletionFunctions.h

    r5781 r6432  
    5454        ARGUMENT_COMPLETION_FUNCTION_DECLARATION(fallback)();
    5555        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);
    5959        ARGUMENT_COMPLETION_FUNCTION_DECLARATION(tclthreads)();
    6060    }
  • code/branches/gamestate/src/libraries/core/ConfigFileManager.cc

    r6425 r6432  
    4040namespace orxonox
    4141{
    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 
    9242    //////////////////////////
    9343    // ConfigFileEntryValue //
     
    10151        // Assemble the entry line
    10252        this->fileEntry_ = this->getKeyString() + " = ";
    103         if (this->bString_)
     53        if (this->bString_ && !this->value_.empty())
    10454            this->fileEntry_ += '"' + addSlashes(this->value_) + '"';
    10555        else
     
    14595    }
    14696
    147     unsigned int ConfigFileSection::getVectorSize(const std::string& name)
     97    unsigned int ConfigFileSection::getVectorSize(const std::string& name) const
    14898    {
    14999        unsigned int size = 0;
     
    166116    }
    167117
    168     std::list<ConfigFileEntry*>::iterator ConfigFileSection::getEntryIterator(const std::string& name, const std::string& fallback, bool bString)
     118    std::list<ConfigFileEntry*>::const_iterator ConfigFileSection::getEntryIterator(const std::string& name) const
     119    {
     120        for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it)
     121        {
     122            if ((*it)->getName() == name)
     123                return it;
     124        }
     125        return this->entries_.end();
     126    }
     127
     128    std::list<ConfigFileEntry*>::iterator ConfigFileSection::getOrCreateEntryIterator(const std::string& name, const std::string& fallback, bool bString)
    169129    {
    170130        for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ++it)
     
    179139        this->bUpdated_ = true;
    180140
    181         return this->entries_.insert(this->entries_.end(), static_cast<ConfigFileEntry*>(new ConfigFileEntryValue(name, fallback, bString)));
    182     }
    183 
    184     std::list<ConfigFileEntry*>::iterator ConfigFileSection::getEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
     141        return this->entries_.insert(this->entries_.end(), new ConfigFileEntryValue(name, fallback, bString));
     142    }
     143
     144    std::list<ConfigFileEntry*>::const_iterator ConfigFileSection::getEntryIterator(const std::string& name, unsigned int index) const
     145    {
     146        for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it)
     147        {
     148            if (((*it)->getName() == name) && ((*it)->getIndex() == index))
     149                return it;
     150        }
     151        return this->entries_.end();
     152    }
     153
     154    std::list<ConfigFileEntry*>::iterator ConfigFileSection::getOrCreateEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
    185155    {
    186156        for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ++it)
     
    196166
    197167        if (index == 0)
    198             return this->entries_.insert(this->entries_.end(), static_cast<ConfigFileEntry*>(new ConfigFileEntryVectorValue(name, index, fallback, bString)));
     168            return this->entries_.insert(this->entries_.end(), new ConfigFileEntryVectorValue(name, index, fallback, bString));
    199169        else
    200             return this->entries_.insert(++this->getEntryIterator(name, index - 1, "", bString), static_cast<ConfigFileEntry*>(new ConfigFileEntryVectorValue(name, index, fallback, bString)));
     170            return this->entries_.insert(++this->getOrCreateEntryIterator(name, index - 1, "", bString), new ConfigFileEntryVectorValue(name, index, fallback, bString));
    201171    }
    202172
     
    205175    // ConfigFile //
    206176    ////////////////
     177    ConfigFile::ConfigFile(const std::string& filename)
     178        : filename_(filename)
     179        , bUpdated_(false)
     180    {
     181    }
     182
    207183    ConfigFile::~ConfigFile()
    208184    {
     
    210186    }
    211187
    212     void ConfigFile::load(bool bCreateIfNotExisting)
     188    void ConfigFile::load()
    213189    {
    214190        // Be sure we start from new in the memory
     
    300276                                {
    301277                                    // New array
    302                                     std::list<ConfigFileEntry*>::iterator it = newsection->getEntryIterator(getStripped(line.substr(0, pos2)), index, value, false);
     278                                    std::list<ConfigFileEntry*>::iterator it = newsection->getOrCreateEntryIterator(getStripped(line.substr(0, pos2)), index, value, false);
    303279                                    (*it)->setValue(value);
    304280                                    (*it)->setComment(comment);
     
    319295            COUT(3) << "Loaded config file \"" << this->filename_ << "\"." << std::endl;
    320296
    321             // Save the file in case something changed (like stripped whitespaces)
     297            // Save the file in case something changed (like stripped white space)
    322298            this->save();
    323 
    324             // Update all ConfigValueContainers
    325             this->updateConfigValues();
    326299        } // end file.is_open()
    327300    }
     
    329302    void ConfigFile::save() const
    330303    {
     304        this->saveAs(this->filename_);
     305    }
     306
     307    void ConfigFile::saveAs(const std::string& filename) const
     308    {
    331309        std::ofstream file;
    332         file.open((PathConfig::getConfigPathString() + filename_).c_str(), std::fstream::out);
     310        file.open((PathConfig::getConfigPathString() + filename).c_str(), std::fstream::out);
    333311        file.setf(std::ios::fixed, std::ios::floatfield);
    334312        file.precision(6);
     
    336314        if (!file.is_open())
    337315        {
    338             COUT(1) << "An error occurred in ConfigFileManager.cc:" << std::endl;
    339             COUT(1) << "Error: Couldn't open config-file \"" << this->filename_ << "\"." << std::endl;
     316            COUT(1) << "Error: Couldn't open config-file \"" << filename << "\"." << std::endl;
    340317            return;
    341318        }
     
    346323
    347324            for (std::list<ConfigFileEntry*>::const_iterator it_entries = (*it)->getEntriesBegin(); it_entries != (*it)->getEntriesEnd(); ++it_entries)
    348             {
    349325                file << (*it_entries)->getFileEntry() << std::endl;
    350             }
    351326
    352327            file << std::endl;
     
    355330        file.close();
    356331
    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();
     332        COUT(4) << "Saved config file \"" << filename << "\"." << std::endl;
    407333    }
    408334
     
    414340    }
    415341
    416     ConfigFileSection* ConfigFile::getSection(const std::string& section)
     342    const std::string& ConfigFile::getOrCreateValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString)
     343    {
     344        const std::string& output = this->getOrCreateSection(section)->getOrCreateValue(name, fallback, bString);
     345        this->saveIfUpdated();
     346        return output;
     347    }
     348
     349    const std::string& ConfigFile::getOrCreateValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString)
     350    {
     351        const std::string& output = this->getOrCreateSection(section)->getOrCreateValue(name, index, fallback, bString);
     352        this->saveIfUpdated();
     353        return output;
     354    }
     355
     356    void ConfigFile::deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex)
     357    {
     358        if (ConfigFileSection* sectionPtr = this->getSection(section))
     359        {
     360            sectionPtr->deleteVectorEntries(name, startindex);
     361            this->save();
     362        }
     363    }
     364
     365    ConfigFileSection* ConfigFile::getSection(const std::string& section) const
     366    {
     367        for (std::list<ConfigFileSection*>::const_iterator it = this->sections_.begin(); it != this->sections_.end(); ++it)
     368            if ((*it)->getName() == section)
     369                return (*it);
     370        return NULL;
     371    }
     372
     373    ConfigFileSection* ConfigFile::getOrCreateSection(const std::string& section)
    417374    {
    418375        for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); ++it)
     
    445402    }
    446403
    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())
     404
     405    ////////////////////////
     406    // SettingsConfigFile //
     407    ////////////////////////
     408
     409    SettingsConfigFile* SettingsConfigFile::singletonPtr_s = 0;
     410
     411    SettingsConfigFile::SettingsConfigFile(const std::string& filename)
     412        : ConfigFile(filename)
     413    {
     414        ConsoleCommand* command = createConsoleCommand(createFunctor(&ConfigFile::load, this), "reloadSettings");
     415        CommandExecutor::addConsoleCommandShortcut(command);
     416        command = createConsoleCommand(createFunctor(&SettingsConfigFile::setFilename, this), "setSettingsFile");
     417        CommandExecutor::addConsoleCommandShortcut(command);
     418        command = createConsoleCommand(createFunctor(&SettingsConfigFile::config, this), "config");
     419        CommandExecutor::addConsoleCommandShortcut(command).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries()).argumentCompleter(2, autocompletion::settingsvalue());
     420        command = createConsoleCommand(createFunctor(&SettingsConfigFile::tconfig, this), "tconfig");
     421        CommandExecutor::addConsoleCommandShortcut(command).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries()).argumentCompleter(2, autocompletion::settingsvalue());
     422        command = createConsoleCommand(createFunctor(&SettingsConfigFile::getConfig, this), "getConfig");
     423        CommandExecutor::addConsoleCommandShortcut(command).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries());
     424    }
     425
     426    SettingsConfigFile::~SettingsConfigFile()
     427    {
     428    }
     429
     430    void SettingsConfigFile::load()
     431    {
     432        ConfigFile::load();
     433        this->updateConfigValues();
     434    }
     435
     436    void SettingsConfigFile::setFilename(const std::string& filename)
     437    {
     438        ConfigFileManager::getInstance().setFilename(ConfigFileType::Settings, filename);
     439    }
     440
     441    void SettingsConfigFile::addConfigValueContainer(ConfigValueContainer* container)
     442    {
     443        if (container == NULL)
     444            return;
     445        std::pair<std::string, ConfigValueContainer*> second(getLowercase(container->getName()), container);
     446        this->containers_.insert(std::make_pair(getLowercase(container->getSectionName()), second));
     447        this->sectionNames_.insert(container->getSectionName());
     448    }
     449
     450    void SettingsConfigFile::removeConfigValueContainer(ConfigValueContainer* container)
     451    {
     452        if (container == NULL)
     453            return;
     454        const std::string& sectionLC = getLowercase(container->getSectionName());
     455        ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC);
     456        for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it)
     457        {
     458            if (it->second.second == container)
     459            {
     460                // Remove entry from section name set this was the last container for that section
     461                if (upper == this->containers_.lower_bound(sectionLC))
     462                    this->sectionNames_.erase(container->getSectionName());
     463                this->containers_.erase(it);
     464                break;
     465            }
     466        }
     467    }
     468
     469    void SettingsConfigFile::updateConfigValues()
     470    {
     471        for (ContainerMap::const_iterator it = this->containers_.begin(); it != this->containers_.end(); ++it)
     472        {
     473            it->second.second->update();
     474            it->second.second->getIdentifier()->updateConfigValues();
     475        }
     476    }
     477
     478    void SettingsConfigFile::clean(bool bCleanComments)
     479    {
     480        for (std::list<ConfigFileSection*>::iterator itSection = this->sections_.begin(); itSection != this->sections_.end(); )
     481        {
     482            const std::string& sectionLC = getLowercase((*itSection)->getName());
     483            ContainerMap::const_iterator lower = this->containers_.lower_bound(sectionLC);
     484            ContainerMap::const_iterator upper = this->containers_.upper_bound(sectionLC);
     485            if (lower != upper)
     486            {
     487                // The section exists, delete comment
     488                if (bCleanComments)
     489                    (*itSection)->setComment("");
     490                for (std::list<ConfigFileEntry*>::iterator itEntry = (*itSection)->entries_.begin(); itEntry != (*itSection)->entries_.end(); )
    454491                {
    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();
     492                    const std::string& entryLC = getLowercase((*itEntry)->getName());
     493                    bool bFound = false;
     494                    for (ContainerMap::const_iterator itContainer = lower; itContainer != upper; ++itContainer)
     495                    {
     496                        if (itContainer->second.first == entryLC)
     497                        {
     498                            // The config-value exists, delete comment
     499                            if (bCleanComments)
     500                                (*itEntry)->setComment("");
     501                            ++itEntry;
     502                            bFound = true;
     503                            break;
     504                        }
     505                    }
     506                    if (!bFound)
     507                    {
     508                        // The config-value doesn't exist
     509                        delete (*itEntry);
     510                        (*itSection)->entries_.erase(itEntry++);
     511                    }
    459512                }
    460             }
    461         }
     513                ++itSection;
     514            }
     515            else
     516            {
     517                // The section doesn't exist
     518                delete (*itSection);
     519                this->sections_.erase(itSection++);
     520            }
     521        }
     522
     523        // Save the file
     524        this->save();
     525    }
     526
     527    bool SettingsConfigFile::config(const std::string& section, const std::string& entry, const std::string& value)
     528    {
     529        return this->configImpl(section, entry, value, &ConfigValueContainer::set);
     530    }
     531
     532    bool SettingsConfigFile::tconfig(const std::string& section, const std::string& entry, const std::string& value)
     533    {
     534        return this->configImpl(section, entry, value, &ConfigValueContainer::tset);
     535    }
     536
     537    bool SettingsConfigFile::configImpl(const std::string& section, const std::string& entry, const std::string& value, bool (ConfigValueContainer::*function)(const MultiType&))
     538    {
     539        const std::string& sectionLC = getLowercase(section);
     540        const std::string& entryLC = getLowercase(entry);
     541        ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC);
     542        for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it)
     543        {
     544            // Note: Config value vectors cannot be supported
     545            if (it->second.first == entryLC && !it->second.second->isVector())
     546            {
     547                return (it->second.second->*function)(value);
     548            }
     549        }
     550        return false;
     551    }
     552
     553    std::string SettingsConfigFile::getConfig(const std::string& section, const std::string& entry)
     554    {
     555        const std::string& sectionLC = getLowercase(section);
     556        const std::string& entryLC = getLowercase(entry);
     557        ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC);
     558        for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it)
     559        {
     560            // Note: Config value vectors cannot be supported
     561            if (it->second.first == entryLC && ! it->second.second->isVector())
     562            {
     563                std::string value;
     564                it->second.second->getValue<std::string, OrxonoxClass>(&value, NULL);
     565                return value;
     566            }
     567        }
     568        return "";
    462569    }
    463570
     
    469576    ConfigFileManager* ConfigFileManager::singletonPtr_s = 0;
    470577
    471     std::string ConfigFileManager::DEFAULT_CONFIG_FILE = "default.ini";
    472 
    473578    ConfigFileManager::ConfigFileManager()
    474          : mininmalFreeType_(ConfigFileType::numberOfReservedTypes)
    475     {
     579    {
     580        this->configFiles_.assign(NULL);
    476581    }
    477582
    478583    ConfigFileManager::~ConfigFileManager()
    479584    {
    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         }
     585        for (boost::array<ConfigFile*, 3>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
     586            if (*it)
     587                delete (*it);
     588    }
     589
     590    void ConfigFileManager::setFilename(ConfigFileType::Value type, const std::string& filename)
     591    {
     592        if (this->getConfigFile(type))
     593            delete this->configFiles_[type];
     594        // Create and load config file
     595        switch (type)
     596        {
     597        case ConfigFileType::Settings:
     598            this->configFiles_[type] = new SettingsConfigFile(filename);
     599            break;
     600        case ConfigFileType::JoyStickCalibration:
     601        case ConfigFileType::CommandHistory:
     602            this->configFiles_[type] = new ConfigFile(filename);
     603            break;
     604        }
     605        this->configFiles_[type]->load();
    566606    }
    567607}
  • code/branches/gamestate/src/libraries/core/ConfigFileManager.h

    r6427 r6432  
    3232#include "CorePrereqs.h"
    3333
    34 #include <cassert>
    35 #include <string>
    3634#include <list>
    3735#include <map>
    38 
    39 #include "util/OrxEnum.h"
     36#include <set>
     37#include <string>
     38#include <boost/array.hpp>
     39
    4040#include "util/Singleton.h"
    4141
    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 
     42namespace orxonox // tolua_export
     43{ // tolua_export
    6744
    6845    /////////////////////
     
    196173    {
    197174        friend class ConfigFile;
     175        friend class SettingsConfigFile;
    198176
    199177        public:
     
    212190
    213191            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(); }
     192                { this->getOrCreateEntry(name, value, bString)->setValue(value); }
     193            inline const std::string& getValue(const std::string& name, const std::string& fallback, bool bString) const
     194            {
     195                ConfigFileEntry* entry = this->getEntry(name);
     196                return (entry ? entry->getValue() : BLANKSTRING);
     197            }
     198            inline const std::string& getOrCreateValue(const std::string& name, const std::string& fallback, bool bString)
     199                { return this->getOrCreateEntry(name, fallback, bString)->getValue(); }
    217200
    218201            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(); }
     202                { this->getOrCreateEntry(name, index, value, bString)->setValue(value); }
     203            inline const std::string& getValue(const std::string& name, unsigned int index) const
     204            {
     205                ConfigFileEntry* entry = this->getEntry(name, index);
     206                return (entry ? entry->getValue() : BLANKSTRING);
     207            }
     208            inline const std::string& getOrCreateValue(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
     209                { return this->getOrCreateEntry(name, index, fallback, bString)->getValue(); }
    222210
    223211            void deleteVectorEntries(const std::string& name, unsigned int startindex = 0);
    224             unsigned int getVectorSize(const std::string& name);
     212            unsigned int getVectorSize(const std::string& name) const;
    225213
    226214            std::string getFileEntry() const;
     
    234222                { return this->entries_.end(); }
    235223
    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)); }
     224            std::list<ConfigFileEntry*>::const_iterator getEntryIterator(const std::string& name) const;
     225            std::list<ConfigFileEntry*>::iterator       getOrCreateEntryIterator(const std::string& name, const std::string& fallback, bool bString);
     226            std::list<ConfigFileEntry*>::const_iterator getEntryIterator(const std::string& name, unsigned int index) const;
     227            std::list<ConfigFileEntry*>::iterator       getOrCreateEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString);
     228
     229            inline ConfigFileEntry* getEntry(const std::string& name) const
     230                { return (*this->getEntryIterator(name)); }
     231            inline ConfigFileEntry* getOrCreateEntry(const std::string& name, const std::string& fallback, bool bString)
     232                { return (*this->getOrCreateEntryIterator(name, fallback, bString)); }
     233            inline ConfigFileEntry* getEntry(const std::string& name, unsigned int index) const
     234                { return (*this->getEntryIterator(name, index)); }
     235            inline ConfigFileEntry* getOrCreateEntry(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
     236                { return (*this->getOrCreateEntryIterator(name, index, fallback, bString)); }
    243237
    244238            std::string name_;
     
    255249    {
    256250        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_; }
     251            ConfigFile(const std::string& filename);
     252            virtual ~ConfigFile();
     253
     254            virtual void load();
     255            virtual void save() const;
     256            virtual void saveAs(const std::string& filename) const;
     257            virtual void clear();
     258
     259            inline const std::string& getFilename()
     260                { return this->filename_; }
    271261
    272262            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; }
     263            {
     264                this->getOrCreateSection(section)->setValue(name, value, bString);
     265                this->save();
     266            }
     267            inline const std::string& getValue(const std::string& section, const std::string& name, bool bString) const
     268            {
     269                ConfigFileSection* sectionPtr = this->getSection(section);
     270                return (sectionPtr ? sectionPtr->getValue(name, bString) : BLANKSTRING);
     271            }
     272            const std::string& getOrCreateValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString);
    276273
    277274            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 
     275            {
     276                this->getSection(section)->setValue(name, index, value, bString);
     277                this->save();
     278            }
     279            inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index) const
     280            {
     281                ConfigFileSection* sectionPtr = this->getSection(section);
     282                return (sectionPtr ? sectionPtr->getValue(name, index) : BLANKSTRING);
     283            }
     284            const std::string& getOrCreateValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString);
     285
     286            void deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex = 0);
     287            inline unsigned int getVectorSize(const std::string& section, const std::string& name) const
     288            {
     289                ConfigFileSection* sectionPtr = this->getSection(section);
     290                return (sectionPtr ? sectionPtr->getVectorSize(name) : 0);
     291            }
     292
     293        protected:
     294            ConfigFileSection* getSection(const std::string& section) const;
     295            ConfigFileSection* getOrCreateSection(const std::string& section);
     296
     297            std::list<ConfigFileSection*> sections_;
     298
     299        private:
     300            void saveIfUpdated();
     301            const std::string filename_;
     302            bool bUpdated_;
     303    };
     304
     305
     306    ////////////////////////
     307    // SettingsConfigFile //
     308    ////////////////////////
     309    class _CoreExport SettingsConfigFile // tolua_export
     310        : public ConfigFile, public Singleton<SettingsConfigFile>
     311    { // tolua_export
     312        friend class Singleton<SettingsConfigFile>;
     313
     314        public:
     315            typedef std::multimap<std::string, std::pair<std::string, ConfigValueContainer*> > ContainerMap;
     316
     317            SettingsConfigFile(const std::string& filename);
     318            ~SettingsConfigFile();
     319
     320            void load(); // tolua_export
     321            void setFilename(const std::string& filename); // tolua_export
     322            void clean(bool bCleanComments = false); // tolua_export
     323
     324            bool config(const std::string& section, const std::string& entry, const std::string& value); // tolua_export
     325            bool tconfig(const std::string& section, const std::string& entry, const std::string& value); // tolua_export
     326            std::string getConfig(const std::string& section, const std::string& entry); // tolua_export
     327
     328            void addConfigValueContainer(ConfigValueContainer* container);
     329            void removeConfigValueContainer(ConfigValueContainer* container);
     330
     331            inline const std::set<std::string>& getSectionNames()
     332                { return this->sectionNames_; }
     333            inline ContainerMap::const_iterator getContainerLowerBound(const std::string section)
     334                { return this->containers_.lower_bound(section); }
     335            inline ContainerMap::const_iterator getContainerUpperBound(const std::string section)
     336                { return this->containers_.upper_bound(section); }
     337
     338            static SettingsConfigFile& getInstance() { return Singleton<SettingsConfigFile>::getInstance(); } // tolua_export
     339
     340        private:
    287341            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     };
     342            bool configImpl(const std::string& section, const std::string& entry, const std::string& value, bool (ConfigValueContainer::*function)(const MultiType&));
     343
     344            ContainerMap containers_;
     345            std::set<std::string> sectionNames_;
     346            static SettingsConfigFile* singletonPtr_s;
     347    }; // tolua_export
    298348
    299349
     
    308358            ~ConfigFileManager();
    309359
    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;
     360            void setFilename(ConfigFileType::Value type, const std::string& filename);
     361
     362            inline ConfigFile* getConfigFile(ConfigFileType::Value type)
     363            {
     364                // Check array bounds
     365                return configFiles_.at(type);
     366            }
    343367
    344368        private:
    345369            ConfigFileManager(const ConfigFileManager&);
    346370
    347             ConfigFile* getFile(ConfigFileType type);
    348 
    349             std::map<ConfigFileType, ConfigFile*> configFiles_;
    350             unsigned int mininmalFreeType_;
    351 
     371            boost::array<ConfigFile*, 3> configFiles_;
    352372            static ConfigFileManager* singletonPtr_s;
    353373    };
  • code/branches/gamestate/src/libraries/core/ConfigValueContainer.cc

    r5738 r6432  
    3636#include "util/Convert.h"
    3737#include "util/SubString.h"
     38#include "ConfigFileManager.h"
    3839#include "Language.h"
    3940
     
    4344
    4445    /**
    45         @brief Initializes the ConfigValueContainer with defaultvalues.
    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)
    4849    {
    4950        this->type_ = type;
     
    5556        this->bDoInitialCallback_ = false;
    5657        this->bAddedDescription_ = false;
     58
     59        // Register containers for general settings
     60        if (this->type_ == ConfigFileType::Settings)
     61            SettingsConfigFile::getInstance().addConfigValueContainer(this);
    5762    }
    5863
     
    7883        for (unsigned int i = 0; i < this->valueVector_.size(); i++)
    7984        {
    80             ConfigFileManager::getInstance().getValue(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));
    8186            this->defvalueStringVector_.push_back(this->valueVector_[i]);
    8287        }
     
    9297        if (this->callback_)
    9398            delete this->callback_;
     99
     100        // Unregister general settings containers
     101        if (this->type_ == ConfigFileType::Settings && SettingsConfigFile::exists())
     102            SettingsConfigFile::getInstance().removeConfigValueContainer(this);
    94103    }
    95104
     
    109118            if (this->tset(input))
    110119            {
    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));
    112121                return true;
    113122            }
     
    128137            if (this->tset(index, input))
    129138            {
    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));
    131140                return true;
    132141            }
     
    228237                this->valueVector_.erase(this->valueVector_.begin() + index);
    229238                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());
    232241
    233242                return true;
     
    253262                if (!this->set(i, this->defvalueStringVector_[i]))
    254263                    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());
    256265            return success;
    257266        }
     
    264273    {
    265274        if (!this->bIsVector_)
    266             this->value_ = ConfigFileManager::getInstance().getValue(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));
    267276        else
    268277        {
    269278            this->valueVector_.clear();
    270             unsigned int vectorSize = ConfigFileManager::getInstance().getVectorSize(this->type_, this->sectionname_, this->varname_);
     279            unsigned int vectorSize = ConfigFileManager::getInstance().getConfigFile(this->type_)->getVectorSize(this->sectionname_, this->varname_);
    271280            for (unsigned int i = 0; i < vectorSize; i++)
    272281            {
    273282                if (i < this->defvalueStringVector_.size())
    274283                {
    275                     this->value_ = ConfigFileManager::getInstance().getValue(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));
    276285                }
    277286                else
    278287                {
    279                     this->value_ = ConfigFileManager::getInstance().getValue(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));
    280289                }
    281290
  • code/branches/gamestate/src/libraries/core/ConfigValueContainer.h

    r6417 r6432  
    5050
    5151#include "util/MultiType.h"
    52 #include "ConfigFileManager.h"
    5352#include "Identifier.h"
    5453
     
    108107            */
    109108            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)
    111110            {
    112111                this->init(type, identifier, sectionname, varname);
     
    122121            */
    123122            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)
    125124            {
    126125                this->init(type, identifier, sectionname, varname);
     
    217216            inline const std::string& getName() const
    218217                { return this->varname_; }
    219             /** @brief Retuns the name of the section this config value is in. */
     218            /** @brief Returns the name of the section this config value is in. */
    220219            inline const std::string& getSectionName() const
    221220                { return this->sectionname_; }
     221            /** @brief Returns the associated identifier (can be NULL). */
     222            inline Identifier* getIdentifier() const
     223                { return this->identifier_; }
    222224            /** @brief Returns true if this config-value is a vector */
    223225            inline bool isVector() const
     
    271273
    272274        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);
    274276            void initValue(const MultiType& defvalue);
    275277            void initVector();
     
    278280            bool                       bIsVector_;                  //!< True if the container contains a std::vector
    279281
    280             ConfigFileType             type_;                       //!< The type of the corresponding config-file
     282            ConfigFileType::Value      type_;                       //!< The type of the corresponding config-file
    281283            Identifier*                identifier_;                 //!< The identifier of the class
    282284            std::string                sectionname_;                //!< The name of the class the variable belongs to
  • code/branches/gamestate/src/libraries/core/ConfigValueIncludes.h

    r6423 r6432  
    4040#include "Identifier.h"
    4141#include "ConfigValueContainer.h"
    42 #include "ConfigFileManager.h"
    4342
    4443namespace orxonox
     
    6160    */
    6261    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)
    6463    {
    6564        ConfigValueContainer* container = ClassIdentifier<T>::getIdentifier()->getConfigValueContainer(entryName);
  • code/branches/gamestate/src/libraries/core/CorePrereqs.h

    r6105 r6432  
    8484    }
    8585
     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
    8697    namespace KeybindMode
    8798    {
     
    124135    class ConfigFileManager;
    125136    class ConfigFileSection;
    126     struct ConfigFileType;
    127137    class ConfigValueContainer;
    128138    class ConsoleCommand;
     
    172182    class PathConfig;
    173183    struct ResourceInfo;
     184    class SettingsConfigFile;
    174185    class Shell;
    175186    class ShellListener;
  • code/branches/gamestate/src/libraries/core/Identifier.cc

    r6417 r6432  
    416416        this->bHasConfigValues_ = true;
    417417        this->configValues_[varname] = container;
    418         this->configValues_LC_[getLowercase(varname)] = container;
    419418    }
    420419
     
    428427        std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_.find(varname);
    429428        if (it != configValues_.end())
    430             return it->second;
    431         else
    432             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 lowercase
    438         @return The ConfigValueContainer
    439     */
    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())
    444429            return it->second;
    445430        else
  • code/branches/gamestate/src/libraries/core/Identifier.h

    r6417 r6432  
    200200            inline bool hasConfigValues() const { return this->bHasConfigValues_; }
    201201
    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 
    216202            void addConfigValueContainer(const std::string& varname, ConfigValueContainer* container);
    217203            ConfigValueContainer* getConfigValueContainer(const std::string& varname);
    218             ConfigValueContainer* getLowercaseConfigValueContainer(const std::string& varname);
    219204
    220205
     
    320305            bool bHasConfigValues_;                                        //!< True if this class has at least one assigned config value
    321306            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 ConfigValueContainer
    323307
    324308            bool bHasConsoleCommands_;                                     //!< True if this class has at least one assigned console command
  • code/branches/gamestate/src/libraries/core/Shell.cc

    r6417 r6432  
    3434#include "CommandExecutor.h"
    3535#include "CoreIncludes.h"
     36#include "ConfigFileManager.h"
    3637#include "ConfigValueIncludes.h"
    3738#include "ConsoleCommand.h"
  • code/branches/gamestate/src/libraries/core/Shell.h

    r6417 r6432  
    3939#include "util/OutputHandler.h"
    4040#include "OrxonoxClass.h"
    41 #include "ConfigFileManager.h"
    4241#include "input/InputBuffer.h"
    4342
  • code/branches/gamestate/src/libraries/core/input/Button.cc

    r6428 r6432  
    4242#include "core/CommandEvaluation.h"
    4343#include "core/CommandExecutor.h"
     44#include "core/ConfigFileManager.h"
    4445
    4546namespace orxonox
     
    8182    }
    8283
    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)
     85    {
     86        const std::string& binding = configFile->getOrCreateValue(groupName_, name_, "", true);
    8687        this->parse(binding);
    8788    }
    8889
    89     void Button::setBinding(ConfigFileType type, const std::string& binding, bool bTemporary)
     90    void Button::setBinding(ConfigFile* configFile, const std::string& binding, bool bTemporary)
    9091    {
    9192        if (!bTemporary)
    92             ConfigFileManager::getInstance().setValue(type, groupName_, name_, binding, true);
     93            configFile->setValue(groupName_, name_, binding, true);
    9394        this->parse(binding);
    9495    }
  • code/branches/gamestate/src/libraries/core/input/Button.h

    r6428 r6432  
    4141#include <vector>
    4242#include "InputCommands.h"
    43 #include "core/ConfigFileManager.h"
    4443
    4544namespace orxonox
     
    5352        virtual bool addParamCommand(ParamCommand* command) { return false; }
    5453        void parse(const std::string& binding);
    55         void readBinding(ConfigFileType type);
    56         void setBinding(ConfigFileType type, const std::string& binding, bool bTemporary);
     54        void readBinding(ConfigFile* configFile);
     55        void setBinding(ConfigFile* configFile, const std::string& binding, bool bTemporary);
    5756        bool execute(KeybindMode::Value mode, float abs = 1.0f, float rel = 1.0f);
    5857
  • code/branches/gamestate/src/libraries/core/input/JoyStick.cc

    r6417 r6432  
    106106    {
    107107        list.resize(size);
    108         unsigned int configValueVectorSize = ConfigFileManager::getInstance().getVectorSize(ConfigFileType::JoyStickCalibration, sectionName, valueName);
     108        unsigned int configValueVectorSize = ConfigFileManager::getInstance().getConfigFile(ConfigFileType::JoyStickCalibration)->getVectorSize(sectionName, valueName);
    109109        if (configValueVectorSize > size)
    110110            configValueVectorSize = size;
     
    112112        for (unsigned int i = 0; i < configValueVectorSize; ++i)
    113113        {
    114             list[i] = multi_cast<int>(ConfigFileManager::getInstance().getValue(
    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));
    116116        }
    117117
     
    153153            if (configMinValues_[i] == INT_MAX)
    154154                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);
    157157
    158158            // Maximum values
    159159            if (configMaxValues_[i] == INT_MIN)
    160160                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);
    163163
    164164            // 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);
    167167        }
    168168
  • code/branches/gamestate/src/libraries/core/input/KeyBinder.cc

    r6428 r6432  
    4949        : deriveTime_(0.0f)
    5050        , filename_(filename)
     51        , configFile_(NULL)
    5152    {
    5253        mouseRelative_[0] = 0;
     
    9394            mouseAxes_[i].groupName_ = "MouseAxes";
    9495        }
    95 
    96         // We might not even load any bindings at all (KeyDetector for instance)
    97         this->configFile_ = ConfigFileType::NoType;
    9896
    9997        // initialise joy sticks separatly to allow for reloading
     
    163161
    164162        // load the bindings if required
    165         if (configFile_ != ConfigFileType::NoType)
     163        if (configFile_ != NULL)
    166164        {
    167165            for (unsigned int iDev = oldValue; iDev < joySticks_.size(); ++iDev)
     
    249247        COUT(3) << "KeyBinder: Loading key bindings..." << std::endl;
    250248
    251         // Get a new ConfigFileType from the ConfigFileManager
    252         this->configFile_ = ConfigFileManager::getInstance().getNewConfigFileType();
    253 
    254         ConfigFileManager::getInstance().setFilename(this->configFile_, this->filename_);
     249        this->configFile_ = new ConfigFile(this->filename_);
     250        this->configFile_->load();
    255251
    256252        // Parse bindings and create the ConfigValueContainers if necessary
  • code/branches/gamestate/src/libraries/core/input/KeyBinder.h

    r6428 r6432  
    3838#include <boost/shared_ptr.hpp>
    3939
    40 #include "core/ConfigFileManager.h"
    4140#include "InputHandler.h"
    4241#include "Button.h"
     
    157156        //! Name of the file used in this KeyBinder (constant!)
    158157        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_;
    161160
    162161    private:
Note: See TracChangeset for help on using the changeset viewer.