diff --git a/CHANGELOG.md b/CHANGELOG.md index 1256da340..f4fa6ecfe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ Bug #4745: Editor: Interior cell lighting field values are not displayed as colors Bug #4746: Non-solid player can't run or sneak Bug #4750: Sneaking doesn't work in first person view if the player is in attack ready state + Bug #4768: Fallback numerical value recovery chokes on invalid arguments Feature #2229: Improve pathfinding AI Feature #3442: Default values for fallbacks from ini file Feature #3610: Option to invert X axis diff --git a/components/fallback/fallback.cpp b/components/fallback/fallback.cpp index edc3f2678..70ee2f681 100644 --- a/components/fallback/fallback.cpp +++ b/components/fallback/fallback.cpp @@ -1,70 +1,86 @@ #include "fallback.hpp" -#include - +#include namespace Fallback { - bool stob(std::string const& s) { - return s != "0"; - } - Map::Map(const std::map& fallback):mFallbackMap(fallback) {} std::string Map::getFallbackString(const std::string& fall) const { std::map::const_iterator it; - if((it = mFallbackMap.find(fall)) == mFallbackMap.end()) + if ((it = mFallbackMap.find(fall)) == mFallbackMap.end()) { - return ""; + return std::string(); } return it->second; } float Map::getFallbackFloat(const std::string& fall) const { - std::string fallback=getFallbackString(fall); - if (fallback.empty()) - return 0; - else - return boost::lexical_cast(fallback); + std::string fallback = getFallbackString(fall); + if (!fallback.empty()) + { + try + { + return std::stof(fallback); + } + catch (const std::invalid_argument&) + { + Log(Debug::Error) << "Error: '" << fall << "' setting value (" << fallback << ") is not a valid number, using 0 as a fallback"; + } + } + + return 0; } int Map::getFallbackInt(const std::string& fall) const { - std::string fallback=getFallbackString(fall); - if (fallback.empty()) - return 0; - else - return std::stoi(fallback); + std::string fallback = getFallbackString(fall); + if (!fallback.empty()) + { + try + { + return std::stoi(fallback); + } + catch (const std::invalid_argument&) + { + Log(Debug::Error) << "Error: '" << fall << "' setting value (" << fallback << ") is not a valid number, using 0 as a fallback"; + } + } + + return 0; } bool Map::getFallbackBool(const std::string& fall) const { - std::string fallback=getFallbackString(fall); - if (fallback.empty()) - return false; - else - return stob(fallback); + return getFallbackString(fall) != "0"; } osg::Vec4f Map::getFallbackColour(const std::string& fall) const { - std::string sum=getFallbackString(fall); - if (sum.empty()) - return osg::Vec4f(0.5f,0.5f,0.5f,1.f); - else + std::string sum = getFallbackString(fall); + if (!sum.empty()) { - std::string ret[3]; - unsigned int j=0; - for(unsigned int i=0;i