Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6437


Ignore:
Timestamp:
Jan 1, 2010, 10:05:56 PM (14 years ago)
Author:
rgrieder
Message:

Added support for keybindings.ini merging:
When running development builds, the keybinder will merge the local file and the one from the data folder.
Catch: if you want to remove a binding, you'll have to write "NoBinding" (not case sensitive) to override the default command

The keybind command already does that for you though.

Location:
code/branches/gamestate/src/libraries/core
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • code/branches/gamestate/src/libraries/core/ConfigFileManager.cc

    r6432 r6437  
    3333#include "util/Convert.h"
    3434#include "util/Math.h"
    35 #include "util/StringUtils.h"
    3635#include "ConsoleCommand.h"
    3736#include "ConfigValueContainer.h"
     
    116115    }
    117116
    118     std::list<ConfigFileEntry*>::const_iterator ConfigFileSection::getEntryIterator(const std::string& name) const
     117    ConfigFileEntry* ConfigFileSection::getEntry(const std::string& name) const
    119118    {
    120119        for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it)
    121120        {
    122121            if ((*it)->getName() == name)
    123                 return it;
    124         }
    125         return this->entries_.end();
     122                return *it;
     123        }
     124        return NULL;
     125    }
     126
     127    ConfigFileEntry* ConfigFileSection::getEntry(const std::string& name, unsigned int index) const
     128    {
     129        for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it)
     130        {
     131            if (((*it)->getName() == name) && ((*it)->getIndex() == index))
     132                return *it;
     133        }
     134        return NULL;
    126135    }
    127136
     
    140149
    141150        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();
    152151    }
    153152
     
    175174    // ConfigFile //
    176175    ////////////////
    177     ConfigFile::ConfigFile(const std::string& filename)
     176
     177    const char* ConfigFile::DEFAULT_CONFIG_FOLDER = "defaultConfig";
     178
     179    ConfigFile::ConfigFile(const std::string& filename, bool bCopyFallbackFile)
    178180        : filename_(filename)
     181        , bCopyFallbackFile_(bCopyFallbackFile)
    179182        , bUpdated_(false)
    180183    {
     
    191194        this->clear();
    192195
    193         // Get default file if necessary and available
    194         boost::filesystem::path filepath(PathConfig::getConfigPath() / this->filename_);
    195         if (!boost::filesystem::exists(filepath))
    196         {
    197             // Try to get default one from the data folder
    198             boost::filesystem::path defaultFilepath(PathConfig::getDataPath() / "defaultConfig" / this->filename_);
    199             if (boost::filesystem::exists(defaultFilepath))
    200             {
    201                 COUT(3) << "Copied " << this->filename_ << " from the defaultConfig folder." << std::endl;
    202                 boost::filesystem::copy_file(defaultFilepath, filepath);
     196        boost::filesystem::path filepath(this->filename_);
     197        if (!filepath.is_complete())
     198        {
     199            filepath = PathConfig::getConfigPath() / filepath;
     200            if (this->bCopyFallbackFile_)
     201            {
     202                // Look for default file in the data folder
     203                if (!boost::filesystem::exists(filepath))
     204                {
     205                    boost::filesystem::path defaultFilepath(PathConfig::getDataPath() / DEFAULT_CONFIG_FOLDER / this->filename_);
     206                    if (boost::filesystem::exists(defaultFilepath))
     207                    {
     208                        // Try to copy default file from the data folder
     209                        try
     210                        {
     211                            boost::filesystem::copy_file(defaultFilepath, filepath);
     212                            COUT(3) << "Copied " << this->filename_ << " from the default config folder." << std::endl;
     213                        }
     214                        catch (const boost::filesystem::filesystem_error& ex)
     215                        { COUT(1) << "Error in ConfigFile: " << ex.what() << std::endl; }
     216                    }
     217                }
    203218            }
    204219        }
     
    295310            COUT(3) << "Loaded config file \"" << this->filename_ << "\"." << std::endl;
    296311
    297             // Save the file in case something changed (like stripped white space)
    298             this->save();
     312            // DO NOT save the file --> we can open supposedly read only config files
    299313        } // end file.is_open()
    300314    }
     
    307321    void ConfigFile::saveAs(const std::string& filename) const
    308322    {
     323        boost::filesystem::path filepath(filename);
     324        if (!filepath.is_complete())
     325            filepath = PathConfig::getConfigPath() / filename;
    309326        std::ofstream file;
    310         file.open((PathConfig::getConfigPathString() + filename).c_str(), std::fstream::out);
     327        file.open(filepath.string().c_str(), std::fstream::out);
    311328        file.setf(std::ios::fixed, std::ios::floatfield);
    312329        file.precision(6);
  • code/branches/gamestate/src/libraries/core/ConfigFileManager.h

    r6432 r6437  
    3939
    4040#include "util/Singleton.h"
     41#include "util/StringUtils.h"
    4142
    4243namespace orxonox // tolua_export
     
    191192            inline void setValue(const std::string& name, const std::string& value, bool bString)
    192193                { 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            inline const std::string& getValue(const std::string& name, bool bString)
    194195            {
    195196                ConfigFileEntry* entry = this->getEntry(name);
    196                 return (entry ? entry->getValue() : BLANKSTRING);
     197                if (entry)
     198                {
     199                    entry->setString(bString);
     200                    return entry->getValue();
     201                }
     202                return BLANKSTRING;
    197203            }
    198204            inline const std::string& getOrCreateValue(const std::string& name, const std::string& fallback, bool bString)
     
    201207            inline void setValue(const std::string& name, unsigned int index, const std::string& value, bool bString)
    202208                { this->getOrCreateEntry(name, index, value, bString)->setValue(value); }
    203             inline const std::string& getValue(const std::string& name, unsigned int index) const
     209            inline const std::string& getValue(const std::string& name, unsigned int index, bool bString)
    204210            {
    205211                ConfigFileEntry* entry = this->getEntry(name, index);
    206                 return (entry ? entry->getValue() : BLANKSTRING);
     212                if (entry)
     213                {
     214                    entry->setString(bString);
     215                    return entry->getValue();
     216                }
     217                return BLANKSTRING;
    207218            }
    208219            inline const std::string& getOrCreateValue(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
     
    222233                { return this->entries_.end(); }
    223234
    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)); }
     235            std::list<ConfigFileEntry*>::iterator getOrCreateEntryIterator(const std::string& name, const std::string& fallback, bool bString);
     236            std::list<ConfigFileEntry*>::iterator getOrCreateEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString);
     237
     238            ConfigFileEntry* getEntry(const std::string& name) const;
    231239            inline ConfigFileEntry* getOrCreateEntry(const std::string& name, const std::string& fallback, bool bString)
    232240                { return (*this->getOrCreateEntryIterator(name, fallback, bString)); }
    233             inline ConfigFileEntry* getEntry(const std::string& name, unsigned int index) const
    234                 { return (*this->getEntryIterator(name, index)); }
     241            ConfigFileEntry* getEntry(const std::string& name, unsigned int index) const;
    235242            inline ConfigFileEntry* getOrCreateEntry(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
    236243                { return (*this->getOrCreateEntryIterator(name, index, fallback, bString)); }
     
    249256    {
    250257        public:
    251             ConfigFile(const std::string& filename);
     258            ConfigFile(const std::string& filename, bool bCopyFallbackFile = true);
    252259            virtual ~ConfigFile();
    253260
     
    265272                this->save();
    266273            }
    267             inline const std::string& getValue(const std::string& section, const std::string& name, bool bString) const
     274            inline const std::string& getValue(const std::string& section, const std::string& name, bool bString)
    268275            {
    269276                ConfigFileSection* sectionPtr = this->getSection(section);
     
    274281            inline void setValue(const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString)
    275282            {
    276                 this->getSection(section)->setValue(name, index, value, bString);
     283                this->getOrCreateSection(section)->setValue(name, index, value, bString);
    277284                this->save();
    278285            }
    279             inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index) const
     286            inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index, bool bString)
    280287            {
    281288                ConfigFileSection* sectionPtr = this->getSection(section);
    282                 return (sectionPtr ? sectionPtr->getValue(name, index) : BLANKSTRING);
     289                return (sectionPtr ? sectionPtr->getValue(name, index, bString) : BLANKSTRING);
    283290            }
    284291            const std::string& getOrCreateValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString);
     
    290297                return (sectionPtr ? sectionPtr->getVectorSize(name) : 0);
    291298            }
     299
     300            static const char* DEFAULT_CONFIG_FOLDER;
    292301
    293302        protected:
     
    300309            void saveIfUpdated();
    301310            const std::string filename_;
     311            const bool bCopyFallbackFile_;
    302312            bool bUpdated_;
    303313    };
  • code/branches/gamestate/src/libraries/core/input/Button.cc

    r6432 r6437  
    8282    }
    8383
    84     void Button::readBinding(ConfigFile* configFile)
    85     {
    86         const std::string& binding = configFile->getOrCreateValue(groupName_, name_, "", true);
     84    void Button::readBinding(ConfigFile* configFile, ConfigFile* fallbackFile)
     85    {
     86        std::string binding = configFile->getOrCreateValue(groupName_, name_, "", true);
     87        if (binding.empty() && fallbackFile)
     88            binding = fallbackFile->getValue(groupName_, name_, true);
    8789        this->parse(binding);
    8890    }
    8991
    90     void Button::setBinding(ConfigFile* configFile, const std::string& binding, bool bTemporary)
     92    void Button::setBinding(ConfigFile* configFile, ConfigFile* fallbackFile, const std::string& binding, bool bTemporary)
    9193    {
    9294        if (!bTemporary)
     
    104106        this->bindingString_ = binding;
    105107
    106         if (isEmpty(bindingString_))
     108        if (isEmpty(bindingString_) || removeTrailingWhitespaces(getLowercase(binding)) == "nobinding")
    107109            return;
    108110
  • code/branches/gamestate/src/libraries/core/input/Button.h

    r6432 r6437  
    5252        virtual bool addParamCommand(ParamCommand* command) { return false; }
    5353        void parse(const std::string& binding);
    54         void readBinding(ConfigFile* configFile);
    55         void setBinding(ConfigFile* configFile, const std::string& binding, bool bTemporary);
     54        void readBinding(ConfigFile* configFile, ConfigFile* fallbackFile);
     55        void setBinding(ConfigFile* configFile, ConfigFile* fallbackFile, const std::string& binding, bool bTemporary);
    5656        bool execute(KeybindMode::Value mode, float abs = 1.0f, float rel = 1.0f);
    5757
  • code/branches/gamestate/src/libraries/core/input/KeyBinder.cc

    r6432 r6437  
    3737#include "core/CoreIncludes.h"
    3838#include "core/ConfigFileManager.h"
     39#include "core/PathConfig.h"
    3940#include "InputCommands.h"
    4041#include "JoyStick.h"
     
    5051        , filename_(filename)
    5152        , configFile_(NULL)
     53        , fallbackConfigFile_(NULL)
    5254    {
    5355        mouseRelative_[0] = 0;
     
    114116        // almost no destructors required because most of the arrays are static.
    115117        clearBindings(); // does some destruction work
     118        if (this->configFile_)
     119            delete this->configFile_;
     120        if (this->fallbackConfigFile_)
     121            delete this->fallbackConfigFile_;
    116122    }
    117123
     
    166172            {
    167173                for (unsigned int i = 0; i < JoyStickButtonCode::numberOfButtons; ++i)
    168                     (*joyStickButtons_[iDev])[i].readBinding(this->configFile_);
     174                    (*joyStickButtons_[iDev])[i].readBinding(this->configFile_, this->fallbackConfigFile_);
    169175                for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; ++i)
    170                     (*joyStickAxes_[iDev])[i].readBinding(this->configFile_);
     176                    (*joyStickAxes_[iDev])[i].readBinding(this->configFile_, this->fallbackConfigFile_);
    171177            }
    172178        }
     
    247253        COUT(3) << "KeyBinder: Loading key bindings..." << std::endl;
    248254
    249         this->configFile_ = new ConfigFile(this->filename_);
     255        this->configFile_ = new ConfigFile(this->filename_, !PathConfig::isDevelopmentRun());
    250256        this->configFile_->load();
     257
     258        if (PathConfig::isDevelopmentRun())
     259        {
     260            // Dev users should have combined key bindings files
     261            std::string defaultFilepath(PathConfig::getDataPathString() + ConfigFile::DEFAULT_CONFIG_FOLDER + '/' + this->filename_);
     262            std::ifstream file(defaultFilepath.c_str());
     263            if (file.is_open())
     264            {
     265                file.close();
     266                // Open the default file for later use (use absolute path!)
     267                this->fallbackConfigFile_ = new ConfigFile(defaultFilepath, false);
     268                this->fallbackConfigFile_->load();
     269            }
     270        }
    251271
    252272        // Parse bindings and create the ConfigValueContainers if necessary
    253273        for (std::map<std::string, Button*>::const_iterator it = allButtons_.begin(); it != allButtons_.end(); ++it)
    254274        {
    255             it->second->readBinding(this->configFile_);
     275            it->second->readBinding(this->configFile_, this->fallbackConfigFile_);
    256276            addButtonToCommand(it->second->bindingString_, it->second);
    257277        }
     
    266286        {
    267287            addButtonToCommand(binding, it->second);
    268             it->second->setBinding(this->configFile_, binding, bTemporary);
     288            std::string str = binding;
     289            if (PathConfig::isDevelopmentRun() && binding.empty())
     290                str = "NoBinding";
     291            it->second->setBinding(this->configFile_, this->fallbackConfigFile_, binding, bTemporary);
    269292            return true;
    270293        }
  • code/branches/gamestate/src/libraries/core/input/KeyBinder.h

    r6432 r6437  
    158158        //! Config file used. NULL in case of KeyDetector. Also indicates whether we've already loaded.
    159159        ConfigFile* configFile_;
     160        //! Config file from the data directory that only serves as fallback
     161        ConfigFile* fallbackConfigFile_;
    160162
    161163    private:
Note: See TracChangeset for help on using the changeset viewer.