Changeset 6437 for code/branches/gamestate
- Timestamp:
- Jan 1, 2010, 10:05:56 PM (15 years ago)
- Location:
- code/branches/gamestate/src/libraries/core
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/gamestate/src/libraries/core/ConfigFileManager.cc
r6432 r6437 33 33 #include "util/Convert.h" 34 34 #include "util/Math.h" 35 #include "util/StringUtils.h"36 35 #include "ConsoleCommand.h" 37 36 #include "ConfigValueContainer.h" … … 116 115 } 117 116 118 std::list<ConfigFileEntry*>::const_iterator ConfigFileSection::getEntryIterator(const std::string& name) const117 ConfigFileEntry* ConfigFileSection::getEntry(const std::string& name) const 119 118 { 120 119 for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) 121 120 { 122 121 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; 126 135 } 127 136 … … 140 149 141 150 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) const145 {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 151 } 153 152 … … 175 174 // ConfigFile // 176 175 //////////////// 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) 178 180 : filename_(filename) 181 , bCopyFallbackFile_(bCopyFallbackFile) 179 182 , bUpdated_(false) 180 183 { … … 191 194 this->clear(); 192 195 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 } 203 218 } 204 219 } … … 295 310 COUT(3) << "Loaded config file \"" << this->filename_ << "\"." << std::endl; 296 311 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 299 313 } // end file.is_open() 300 314 } … … 307 321 void ConfigFile::saveAs(const std::string& filename) const 308 322 { 323 boost::filesystem::path filepath(filename); 324 if (!filepath.is_complete()) 325 filepath = PathConfig::getConfigPath() / filename; 309 326 std::ofstream file; 310 file.open( (PathConfig::getConfigPathString() + filename).c_str(), std::fstream::out);327 file.open(filepath.string().c_str(), std::fstream::out); 311 328 file.setf(std::ios::fixed, std::ios::floatfield); 312 329 file.precision(6); -
code/branches/gamestate/src/libraries/core/ConfigFileManager.h
r6432 r6437 39 39 40 40 #include "util/Singleton.h" 41 #include "util/StringUtils.h" 41 42 42 43 namespace orxonox // tolua_export … … 191 192 inline void setValue(const std::string& name, const std::string& value, bool bString) 192 193 { this->getOrCreateEntry(name, value, bString)->setValue(value); } 193 inline const std::string& getValue(const std::string& name, const std::string& fallback, bool bString) const194 inline const std::string& getValue(const std::string& name, bool bString) 194 195 { 195 196 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; 197 203 } 198 204 inline const std::string& getOrCreateValue(const std::string& name, const std::string& fallback, bool bString) … … 201 207 inline void setValue(const std::string& name, unsigned int index, const std::string& value, bool bString) 202 208 { this->getOrCreateEntry(name, index, value, bString)->setValue(value); } 203 inline const std::string& getValue(const std::string& name, unsigned int index ) const209 inline const std::string& getValue(const std::string& name, unsigned int index, bool bString) 204 210 { 205 211 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; 207 218 } 208 219 inline const std::string& getOrCreateValue(const std::string& name, unsigned int index, const std::string& fallback, bool bString) … … 222 233 { return this->entries_.end(); } 223 234 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; 231 239 inline ConfigFileEntry* getOrCreateEntry(const std::string& name, const std::string& fallback, bool bString) 232 240 { 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; 235 242 inline ConfigFileEntry* getOrCreateEntry(const std::string& name, unsigned int index, const std::string& fallback, bool bString) 236 243 { return (*this->getOrCreateEntryIterator(name, index, fallback, bString)); } … … 249 256 { 250 257 public: 251 ConfigFile(const std::string& filename );258 ConfigFile(const std::string& filename, bool bCopyFallbackFile = true); 252 259 virtual ~ConfigFile(); 253 260 … … 265 272 this->save(); 266 273 } 267 inline const std::string& getValue(const std::string& section, const std::string& name, bool bString) const274 inline const std::string& getValue(const std::string& section, const std::string& name, bool bString) 268 275 { 269 276 ConfigFileSection* sectionPtr = this->getSection(section); … … 274 281 inline void setValue(const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString) 275 282 { 276 this->get Section(section)->setValue(name, index, value, bString);283 this->getOrCreateSection(section)->setValue(name, index, value, bString); 277 284 this->save(); 278 285 } 279 inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index ) const286 inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index, bool bString) 280 287 { 281 288 ConfigFileSection* sectionPtr = this->getSection(section); 282 return (sectionPtr ? sectionPtr->getValue(name, index ) : BLANKSTRING);289 return (sectionPtr ? sectionPtr->getValue(name, index, bString) : BLANKSTRING); 283 290 } 284 291 const std::string& getOrCreateValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString); … … 290 297 return (sectionPtr ? sectionPtr->getVectorSize(name) : 0); 291 298 } 299 300 static const char* DEFAULT_CONFIG_FOLDER; 292 301 293 302 protected: … … 300 309 void saveIfUpdated(); 301 310 const std::string filename_; 311 const bool bCopyFallbackFile_; 302 312 bool bUpdated_; 303 313 }; -
code/branches/gamestate/src/libraries/core/input/Button.cc
r6432 r6437 82 82 } 83 83 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); 87 89 this->parse(binding); 88 90 } 89 91 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) 91 93 { 92 94 if (!bTemporary) … … 104 106 this->bindingString_ = binding; 105 107 106 if (isEmpty(bindingString_) )108 if (isEmpty(bindingString_) || removeTrailingWhitespaces(getLowercase(binding)) == "nobinding") 107 109 return; 108 110 -
code/branches/gamestate/src/libraries/core/input/Button.h
r6432 r6437 52 52 virtual bool addParamCommand(ParamCommand* command) { return false; } 53 53 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); 56 56 bool execute(KeybindMode::Value mode, float abs = 1.0f, float rel = 1.0f); 57 57 -
code/branches/gamestate/src/libraries/core/input/KeyBinder.cc
r6432 r6437 37 37 #include "core/CoreIncludes.h" 38 38 #include "core/ConfigFileManager.h" 39 #include "core/PathConfig.h" 39 40 #include "InputCommands.h" 40 41 #include "JoyStick.h" … … 50 51 , filename_(filename) 51 52 , configFile_(NULL) 53 , fallbackConfigFile_(NULL) 52 54 { 53 55 mouseRelative_[0] = 0; … … 114 116 // almost no destructors required because most of the arrays are static. 115 117 clearBindings(); // does some destruction work 118 if (this->configFile_) 119 delete this->configFile_; 120 if (this->fallbackConfigFile_) 121 delete this->fallbackConfigFile_; 116 122 } 117 123 … … 166 172 { 167 173 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_); 169 175 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_); 171 177 } 172 178 } … … 247 253 COUT(3) << "KeyBinder: Loading key bindings..." << std::endl; 248 254 249 this->configFile_ = new ConfigFile(this->filename_ );255 this->configFile_ = new ConfigFile(this->filename_, !PathConfig::isDevelopmentRun()); 250 256 this->configFile_->load(); 257 258 if (PathConfig::isDevelopmentRun()) 259 { 260 // Dev users should have combined key bindings files 261 std::string defaultFilepath(PathConfig::getDataPathString() + ConfigFile::DEFAULT_CONFIG_FOLDER + '/' + this->filename_); 262 std::ifstream file(defaultFilepath.c_str()); 263 if (file.is_open()) 264 { 265 file.close(); 266 // Open the default file for later use (use absolute path!) 267 this->fallbackConfigFile_ = new ConfigFile(defaultFilepath, false); 268 this->fallbackConfigFile_->load(); 269 } 270 } 251 271 252 272 // Parse bindings and create the ConfigValueContainers if necessary 253 273 for (std::map<std::string, Button*>::const_iterator it = allButtons_.begin(); it != allButtons_.end(); ++it) 254 274 { 255 it->second->readBinding(this->configFile_ );275 it->second->readBinding(this->configFile_, this->fallbackConfigFile_); 256 276 addButtonToCommand(it->second->bindingString_, it->second); 257 277 } … … 266 286 { 267 287 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); 269 292 return true; 270 293 } -
code/branches/gamestate/src/libraries/core/input/KeyBinder.h
r6432 r6437 158 158 //! Config file used. NULL in case of KeyDetector. Also indicates whether we've already loaded. 159 159 ConfigFile* configFile_; 160 //! Config file from the data directory that only serves as fallback 161 ConfigFile* fallbackConfigFile_; 160 162 161 163 private:
Note: See TracChangeset
for help on using the changeset viewer.