| [513] | 1 | /* |
|---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
|---|
| [1056] | 3 | * > www.orxonox.net < |
|---|
| [513] | 4 | * |
|---|
| 5 | * |
|---|
| 6 | * License notice: |
|---|
| 7 | * |
|---|
| 8 | * This program is free software; you can redistribute it and/or |
|---|
| 9 | * modify it under the terms of the GNU General Public License |
|---|
| 10 | * as published by the Free Software Foundation; either version 2 |
|---|
| 11 | * of the License, or (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This program is distributed in the hope that it will be useful, |
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | * GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * You should have received a copy of the GNU General Public License |
|---|
| 19 | * along with this program; if not, write to the Free Software |
|---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 21 | * |
|---|
| 22 | * Author: |
|---|
| [670] | 23 | * Fabian 'x3n' Landau |
|---|
| [513] | 24 | * Co-authors: |
|---|
| 25 | * ... |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| [871] | 29 | /** |
|---|
| 30 | @file ConfigValueContainer.cc |
|---|
| 31 | @brief Implementation of the ConfigValueContainer class. |
|---|
| 32 | */ |
|---|
| 33 | |
|---|
| [497] | 34 | #include <fstream> |
|---|
| [682] | 35 | |
|---|
| [871] | 36 | #include "ConfigValueContainer.h" |
|---|
| [1052] | 37 | #include "Language.h" |
|---|
| 38 | #include "Identifier.h" |
|---|
| 39 | #include "util/SubString.h" |
|---|
| [742] | 40 | #include "util/Convert.h" |
|---|
| [497] | 41 | |
|---|
| [1052] | 42 | #define MAX_VECTOR_INDEX 255 // to avoid up to 4*10^9 vector entries in the config file after accidentally using a wrong argument |
|---|
| [497] | 43 | |
|---|
| [1052] | 44 | |
|---|
| [497] | 45 | namespace orxonox |
|---|
| 46 | { |
|---|
| 47 | /** |
|---|
| [667] | 48 | @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable. |
|---|
| [1052] | 49 | @param type The type of the corresponding config-file |
|---|
| 50 | @param identifier The identifier of the class the variable belongs to |
|---|
| [497] | 51 | @param varname The name of the variable |
|---|
| 52 | @param defvalue The default-value |
|---|
| 53 | */ |
|---|
| [1052] | 54 | ConfigValueContainer::ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& varname, const MultiTypeMath& defvalue) |
|---|
| [497] | 55 | { |
|---|
| [1052] | 56 | this->type_ = type; |
|---|
| 57 | this->identifier_ = identifier; |
|---|
| 58 | this->sectionname_ = identifier->getName(); |
|---|
| [667] | 59 | this->varname_ = varname; |
|---|
| [497] | 60 | |
|---|
| [1052] | 61 | this->value_ = defvalue; |
|---|
| 62 | this->bAddedDescription_ = false; |
|---|
| 63 | this->bIsVector_ = false; |
|---|
| [497] | 64 | |
|---|
| [1052] | 65 | this->defvalueString_ = defvalue.toString(); |
|---|
| 66 | this->update(); |
|---|
| [497] | 67 | } |
|---|
| 68 | |
|---|
| 69 | /** |
|---|
| [1052] | 70 | @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable. |
|---|
| 71 | @param type The type of the corresponding config-file |
|---|
| 72 | @param identifier The identifier of the class the variable belongs to |
|---|
| 73 | @param varname The name of the variable |
|---|
| 74 | @param defvalue The default-value |
|---|
| [497] | 75 | */ |
|---|
| [1052] | 76 | ConfigValueContainer::ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& varname, const std::vector<MultiTypeMath>& defvalue) |
|---|
| [667] | 77 | { |
|---|
| [1052] | 78 | this->type_ = type; |
|---|
| 79 | this->identifier_ = identifier; |
|---|
| 80 | this->sectionname_ = identifier->getName(); |
|---|
| 81 | this->varname_ = varname; |
|---|
| [667] | 82 | |
|---|
| [1052] | 83 | this->valueVector_ = defvalue; |
|---|
| 84 | this->bAddedDescription_ = false; |
|---|
| 85 | this->bIsVector_ = true; |
|---|
| [667] | 86 | |
|---|
| [1052] | 87 | if (defvalue.size() > 0) |
|---|
| 88 | this->value_ = defvalue[0]; |
|---|
| [667] | 89 | |
|---|
| [1052] | 90 | for (unsigned int i = 0; i < defvalue.size(); i++) |
|---|
| 91 | ConfigFileManager::getSingleton()->getValue(this->type_, this->sectionname_, this->varname_, i, defvalue[i].toString(), this->value_.isA(MT_string)); |
|---|
| [667] | 92 | |
|---|
| [1052] | 93 | for (unsigned int i = 0; i < defvalue.size(); i++) |
|---|
| 94 | this->defvalueStringVector_.push_back(defvalue[i].toString()); |
|---|
| [667] | 95 | |
|---|
| [1052] | 96 | this->update(); |
|---|
| [667] | 97 | } |
|---|
| 98 | |
|---|
| 99 | /** |
|---|
| [1052] | 100 | @brief Adds a new entry to the end of the vector. |
|---|
| 101 | @param input The new entry |
|---|
| 102 | @return True if the new entry was successfully added |
|---|
| [667] | 103 | */ |
|---|
| [1052] | 104 | bool ConfigValueContainer::add(const std::string& input) |
|---|
| [667] | 105 | { |
|---|
| [1052] | 106 | if (this->bIsVector_) |
|---|
| 107 | return this->set(this->valueVector_.size(), input); |
|---|
| [667] | 108 | |
|---|
| [1052] | 109 | COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl; |
|---|
| 110 | return false; |
|---|
| [497] | 111 | } |
|---|
| 112 | |
|---|
| 113 | /** |
|---|
| [1052] | 114 | @brief Removes an existing entry from the vector. |
|---|
| 115 | @param index The index of the entry |
|---|
| 116 | @return True if the entry was removed |
|---|
| [703] | 117 | */ |
|---|
| [1052] | 118 | bool ConfigValueContainer::remove(unsigned int index) |
|---|
| [703] | 119 | { |
|---|
| [1052] | 120 | if (this->bIsVector_) |
|---|
| 121 | { |
|---|
| 122 | if (index < this->valueVector_.size()) |
|---|
| 123 | { |
|---|
| 124 | this->valueVector_.erase(this->valueVector_.begin() + index); |
|---|
| 125 | for (unsigned int i = index; i < this->valueVector_.size(); i++) |
|---|
| 126 | ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, i, this->valueVector_[i], this->value_.isA(MT_string)); |
|---|
| 127 | ConfigFileManager::getSingleton()->deleteVectorEntries(this->type_, this->sectionname_, this->varname_, this->valueVector_.size()); |
|---|
| [703] | 128 | |
|---|
| [1052] | 129 | return true; |
|---|
| 130 | } |
|---|
| 131 | COUT(1) << "Error: Invalid vector-index." << std::endl; |
|---|
| 132 | } |
|---|
| [703] | 133 | |
|---|
| [1052] | 134 | COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl; |
|---|
| 135 | return false; |
|---|
| [703] | 136 | } |
|---|
| 137 | |
|---|
| 138 | /** |
|---|
| [1052] | 139 | @brief Assigns a new value to the config-value of all objects and writes the change into the config-file. |
|---|
| 140 | @param input The new value |
|---|
| 141 | @return True if the new value was successfully assigned |
|---|
| [703] | 142 | */ |
|---|
| [1052] | 143 | bool ConfigValueContainer::set(const std::string& input) |
|---|
| [703] | 144 | { |
|---|
| [1052] | 145 | if (this->bIsVector_) |
|---|
| 146 | { |
|---|
| 147 | SubString token(input, " ", "", true, '"', false, '(', ')', false, '\0'); |
|---|
| 148 | int index = -1; |
|---|
| 149 | bool success = false; |
|---|
| [703] | 150 | |
|---|
| [1052] | 151 | if (token.size() > 0) |
|---|
| 152 | success = ConvertValue(&index, token[0]); |
|---|
| [703] | 153 | |
|---|
| [1052] | 154 | if (!success || index < 0 || index > MAX_VECTOR_INDEX) |
|---|
| 155 | { |
|---|
| 156 | if (!success) |
|---|
| 157 | { |
|---|
| 158 | COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is a vector." << std::endl; |
|---|
| 159 | } |
|---|
| 160 | else |
|---|
| 161 | { |
|---|
| 162 | COUT(1) << "Error: Invalid vector-index." << std::endl; |
|---|
| 163 | } |
|---|
| 164 | return false; |
|---|
| 165 | } |
|---|
| [703] | 166 | |
|---|
| [1052] | 167 | if (token.size() >= 2) |
|---|
| 168 | return this->set(index, token.subSet(1).join()); |
|---|
| 169 | else |
|---|
| 170 | return this->set(index, ""); |
|---|
| 171 | } |
|---|
| [703] | 172 | |
|---|
| [1052] | 173 | bool success = this->tset(input); |
|---|
| 174 | ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, input, this->value_.isA(MT_string)); |
|---|
| [871] | 175 | return success; |
|---|
| [703] | 176 | } |
|---|
| 177 | |
|---|
| 178 | /** |
|---|
| [1052] | 179 | @brief Assigns a new value to the config-value of all objects and writes the change into the config-file. |
|---|
| 180 | @param index The index in the vector |
|---|
| 181 | @param input The new value |
|---|
| 182 | @return True if the new value was successfully assigned |
|---|
| [703] | 183 | */ |
|---|
| [1052] | 184 | bool ConfigValueContainer::set(unsigned int index, const std::string& input) |
|---|
| [703] | 185 | { |
|---|
| [1052] | 186 | if (this->bIsVector_) |
|---|
| [703] | 187 | { |
|---|
| [1052] | 188 | bool success = this->tset(index, input); |
|---|
| 189 | ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, index, input, this->value_.isA(MT_string)); |
|---|
| [871] | 190 | return success; |
|---|
| [703] | 191 | } |
|---|
| 192 | |
|---|
| [1052] | 193 | COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl; |
|---|
| 194 | return false; |
|---|
| [703] | 195 | } |
|---|
| 196 | |
|---|
| 197 | /** |
|---|
| [1052] | 198 | @brief Assigns a new value to the config-value of all objects, but doesn't change the config-file (t stands for temporary). |
|---|
| 199 | @param input The new value |
|---|
| 200 | @return True if the new value was successfully assigned |
|---|
| [703] | 201 | */ |
|---|
| [1052] | 202 | bool ConfigValueContainer::tset(const std::string& input) |
|---|
| [703] | 203 | { |
|---|
| [1052] | 204 | bool success = this->parse(input); |
|---|
| 205 | if (this->identifier_) |
|---|
| 206 | this->identifier_->updateConfigValues(); |
|---|
| 207 | return success; |
|---|
| [703] | 208 | } |
|---|
| 209 | |
|---|
| 210 | /** |
|---|
| [1052] | 211 | @brief Assigns a new value to the config-value of all objects, but doesn't change the config-file (t stands for temporary). |
|---|
| 212 | @param index The index in the vector |
|---|
| 213 | @param input The new value |
|---|
| 214 | @return True if the new value was successfully assigned |
|---|
| [703] | 215 | */ |
|---|
| [1052] | 216 | bool ConfigValueContainer::tset(unsigned int index, const std::string& input) |
|---|
| [703] | 217 | { |
|---|
| [1052] | 218 | if (this->bIsVector_) |
|---|
| [703] | 219 | { |
|---|
| [1052] | 220 | bool success = this->parse(index, input); |
|---|
| 221 | if (this->identifier_) |
|---|
| 222 | this->identifier_->updateConfigValues(); |
|---|
| 223 | return success; |
|---|
| [703] | 224 | } |
|---|
| 225 | |
|---|
| [1052] | 226 | COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl; |
|---|
| [703] | 227 | return false; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | /** |
|---|
| [1052] | 231 | @brief Sets the value of the variable back to the default value and resets the config-file entry. |
|---|
| [703] | 232 | */ |
|---|
| [1052] | 233 | bool ConfigValueContainer::reset() |
|---|
| [703] | 234 | { |
|---|
| [1052] | 235 | if (!this->bIsVector_) |
|---|
| 236 | return this->set(this->defvalueString_); |
|---|
| 237 | else |
|---|
| [703] | 238 | { |
|---|
| [1052] | 239 | bool success = true; |
|---|
| 240 | for (unsigned int i = 0; i < this->defvalueStringVector_.size(); i++) |
|---|
| 241 | if (!this->set(i, this->defvalueStringVector_[i])) |
|---|
| 242 | success = false; |
|---|
| 243 | ConfigFileManager::getSingleton()->deleteVectorEntries(this->type_, this->sectionname_, this->varname_, this->defvalueStringVector_.size()); |
|---|
| 244 | return success; |
|---|
| [703] | 245 | } |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | /** |
|---|
| [1052] | 249 | @brief Retrieves the configured value from the currently loaded config-file. |
|---|
| [703] | 250 | */ |
|---|
| [1052] | 251 | void ConfigValueContainer::update() |
|---|
| [703] | 252 | { |
|---|
| [1052] | 253 | if (!this->bIsVector_) |
|---|
| 254 | this->value_.fromString(ConfigFileManager::getSingleton()->getValue(this->type_, this->sectionname_, this->varname_, this->defvalueString_, this->value_.isA(MT_string))); |
|---|
| 255 | else |
|---|
| [497] | 256 | { |
|---|
| [1052] | 257 | this->valueVector_.clear(); |
|---|
| 258 | for (unsigned int i = 0; i < ConfigFileManager::getSingleton()->getVectorSize(this->type_, this->sectionname_, this->varname_); i++) |
|---|
| [703] | 259 | { |
|---|
| [1052] | 260 | this->value_.fromString(ConfigFileManager::getSingleton()->getValue(this->type_, this->sectionname_, this->varname_, i, this->defvalueStringVector_[i], this->value_.isA(MT_string))); |
|---|
| 261 | this->valueVector_.push_back(this->value_); |
|---|
| [703] | 262 | } |
|---|
| 263 | } |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | /** |
|---|
| [1052] | 267 | @brief Parses a given std::string into a value of the type of the associated variable and assigns it. |
|---|
| [703] | 268 | @param input The string to convert |
|---|
| 269 | @return True if the string was successfully parsed |
|---|
| 270 | */ |
|---|
| [1052] | 271 | bool ConfigValueContainer::parse(const std::string& input) |
|---|
| [703] | 272 | { |
|---|
| [1052] | 273 | if (this->bIsVector_) |
|---|
| 274 | { |
|---|
| 275 | SubString token(input, " ", "", true, '"', false, '(', ')', false, '\0'); |
|---|
| 276 | int index = -1; |
|---|
| 277 | bool success = false; |
|---|
| [703] | 278 | |
|---|
| [1052] | 279 | if (token.size() > 0) |
|---|
| 280 | success = ConvertValue(&index, token[0]); |
|---|
| 281 | |
|---|
| 282 | if (!success || index < 0 || index > MAX_VECTOR_INDEX) |
|---|
| [703] | 283 | { |
|---|
| [1052] | 284 | if (!success) |
|---|
| 285 | { |
|---|
| 286 | COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is a vector." << std::endl; |
|---|
| 287 | } |
|---|
| 288 | else |
|---|
| 289 | { |
|---|
| 290 | COUT(1) << "Error: Invalid vector-index." << std::endl; |
|---|
| 291 | } |
|---|
| [703] | 292 | return false; |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| [1052] | 295 | if (token.size() >= 2) |
|---|
| 296 | return this->parse(index, token.subSet(1).join()); |
|---|
| 297 | else |
|---|
| 298 | return this->parse(index, ""); |
|---|
| [497] | 299 | } |
|---|
| [703] | 300 | |
|---|
| [1052] | 301 | MultiTypeMath temp = this->value_; |
|---|
| 302 | if (temp.fromString(input)) |
|---|
| [871] | 303 | { |
|---|
| [1052] | 304 | this->value_ = temp; |
|---|
| [871] | 305 | return true; |
|---|
| 306 | } |
|---|
| 307 | return false; |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | /** |
|---|
| [1052] | 311 | @brief Parses a given std::string into a value of the type of the associated variable and assigns it. |
|---|
| 312 | @param index The index in the vector |
|---|
| [871] | 313 | @param input The string to convert |
|---|
| 314 | @return True if the string was successfully parsed |
|---|
| 315 | */ |
|---|
| [1052] | 316 | bool ConfigValueContainer::parse(unsigned int index, const std::string& input) |
|---|
| [871] | 317 | { |
|---|
| [1052] | 318 | if (this->bIsVector_) |
|---|
| [497] | 319 | { |
|---|
| [1052] | 320 | if (index >= this->valueVector_.size()) |
|---|
| [497] | 321 | { |
|---|
| [1052] | 322 | for (unsigned int i = this->valueVector_.size(); i <= index; i++) |
|---|
| [497] | 323 | { |
|---|
| [1052] | 324 | this->valueVector_.push_back(MultiTypeMath()); |
|---|
| 325 | ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, i, this->valueVector_[i], this->value_.isA(MT_string)); |
|---|
| [497] | 326 | } |
|---|
| [1052] | 327 | } |
|---|
| [497] | 328 | |
|---|
| [1052] | 329 | MultiTypeMath temp = this->value_; |
|---|
| 330 | if (temp.fromString(input)) |
|---|
| 331 | { |
|---|
| 332 | this->valueVector_[index] = temp; |
|---|
| 333 | return true; |
|---|
| [497] | 334 | } |
|---|
| [1052] | 335 | return false; |
|---|
| [497] | 336 | } |
|---|
| 337 | |
|---|
| [1052] | 338 | COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl; |
|---|
| 339 | return false; |
|---|
| [497] | 340 | } |
|---|
| 341 | |
|---|
| 342 | /** |
|---|
| [1052] | 343 | @brief Parses a given std::string into a value of the type of the associated variable and assigns it. |
|---|
| 344 | @param input The string to convert |
|---|
| 345 | @param defvalue The default value to assign if the parsing fails |
|---|
| 346 | @return True if the string was successfully parsed |
|---|
| [497] | 347 | */ |
|---|
| [1052] | 348 | bool ConfigValueContainer::parse(const std::string& input, const MultiTypeMath& defvalue) |
|---|
| [497] | 349 | { |
|---|
| [1052] | 350 | if (this->parse(input)) |
|---|
| [497] | 351 | return true; |
|---|
| 352 | |
|---|
| [1052] | 353 | this->value_ = defvalue; |
|---|
| [497] | 354 | return false; |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | /** |
|---|
| [1052] | 358 | @brief Parses a given std::string into a value of the type of the associated variable and assigns it. |
|---|
| 359 | @param index The index in the vector |
|---|
| 360 | @param input The string to convert |
|---|
| 361 | @param defvalue The default value to assign if the parsing fails |
|---|
| 362 | @return True if the string was successfully parsed |
|---|
| [497] | 363 | */ |
|---|
| [1052] | 364 | bool ConfigValueContainer::parse(unsigned int index, const std::string& input, const MultiTypeMath& defvalue) |
|---|
| [497] | 365 | { |
|---|
| [1052] | 366 | if (this->bIsVector_) |
|---|
| [704] | 367 | { |
|---|
| [1052] | 368 | if (this->parse(index, input)) |
|---|
| 369 | return true; |
|---|
| [704] | 370 | |
|---|
| [1052] | 371 | this->valueVector_[index] = defvalue; |
|---|
| 372 | return false; |
|---|
| [497] | 373 | } |
|---|
| 374 | |
|---|
| [1052] | 375 | COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl; |
|---|
| 376 | return false; |
|---|
| [497] | 377 | } |
|---|
| 378 | |
|---|
| 379 | /** |
|---|
| [705] | 380 | @brief Adds a description to the config-value. |
|---|
| 381 | @param description The description |
|---|
| 382 | */ |
|---|
| [715] | 383 | void ConfigValueContainer::description(const std::string& description) |
|---|
| [705] | 384 | { |
|---|
| 385 | if (!this->bAddedDescription_) |
|---|
| 386 | { |
|---|
| [1052] | 387 | this->description_ = std::string("ConfigValueDescription::" + this->identifier_->getName() + "::" + this->varname_); |
|---|
| [871] | 388 | AddLanguageEntry(this->description_, description); |
|---|
| [705] | 389 | this->bAddedDescription_ = true; |
|---|
| 390 | } |
|---|
| 391 | } |
|---|
| [871] | 392 | |
|---|
| 393 | /** |
|---|
| 394 | @brief Returns the description of the config-value. |
|---|
| 395 | @return The description |
|---|
| 396 | */ |
|---|
| 397 | const std::string& ConfigValueContainer::getDescription() const |
|---|
| 398 | { |
|---|
| 399 | return GetLocalisation(this->description_); |
|---|
| 400 | } |
|---|
| [497] | 401 | } |
|---|