1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-26 09:35:28 +00:00

Merge pull request #2104 from Capostrophic/fallback

Improve fallback numerical value handling (bug #4768)
This commit is contained in:
Bret Curtis 2018-12-29 08:01:18 +01:00 committed by GitHub
commit 02c8e328bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 34 deletions

View File

@ -12,6 +12,7 @@
Bug #4745: Editor: Interior cell lighting field values are not displayed as colors Bug #4745: Editor: Interior cell lighting field values are not displayed as colors
Bug #4746: Non-solid player can't run or sneak 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 #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 #2229: Improve pathfinding AI
Feature #3442: Default values for fallbacks from ini file Feature #3442: Default values for fallbacks from ini file
Feature #3610: Option to invert X axis Feature #3610: Option to invert X axis

View File

@ -1,70 +1,86 @@
#include "fallback.hpp" #include "fallback.hpp"
#include <boost/lexical_cast.hpp> #include <components/debug/debuglog.hpp>
namespace Fallback namespace Fallback
{ {
bool stob(std::string const& s) {
return s != "0";
}
Map::Map(const std::map<std::string,std::string>& fallback):mFallbackMap(fallback) Map::Map(const std::map<std::string,std::string>& fallback):mFallbackMap(fallback)
{} {}
std::string Map::getFallbackString(const std::string& fall) const std::string Map::getFallbackString(const std::string& fall) const
{ {
std::map<std::string,std::string>::const_iterator it; std::map<std::string,std::string>::const_iterator it;
if((it = mFallbackMap.find(fall)) == mFallbackMap.end()) if ((it = mFallbackMap.find(fall)) == mFallbackMap.end())
{ {
return ""; return std::string();
} }
return it->second; return it->second;
} }
float Map::getFallbackFloat(const std::string& fall) const float Map::getFallbackFloat(const std::string& fall) const
{ {
std::string fallback=getFallbackString(fall); std::string fallback = getFallbackString(fall);
if (fallback.empty()) if (!fallback.empty())
return 0; {
else try
return boost::lexical_cast<float>(fallback); {
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 int Map::getFallbackInt(const std::string& fall) const
{ {
std::string fallback=getFallbackString(fall); std::string fallback = getFallbackString(fall);
if (fallback.empty()) if (!fallback.empty())
return 0; {
else try
return std::stoi(fallback); {
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 bool Map::getFallbackBool(const std::string& fall) const
{ {
std::string fallback=getFallbackString(fall); return getFallbackString(fall) != "0";
if (fallback.empty())
return false;
else
return stob(fallback);
} }
osg::Vec4f Map::getFallbackColour(const std::string& fall) const osg::Vec4f Map::getFallbackColour(const std::string& fall) const
{ {
std::string sum=getFallbackString(fall); std::string sum = getFallbackString(fall);
if (sum.empty()) if (!sum.empty())
return osg::Vec4f(0.5f,0.5f,0.5f,1.f);
else
{ {
std::string ret[3]; try
unsigned int j=0; {
for(unsigned int i=0;i<sum.length();++i){ std::string ret[3];
if(sum[i]==',') j++; unsigned int j = 0;
else if (sum[i] != ' ') ret[j]+=sum[i]; for (unsigned int i = 0; i < sum.length(); ++i)
{
if(sum[i]==',') j++;
else if (sum[i] != ' ') ret[j]+=sum[i];
}
return osg::Vec4f(std::stoi(ret[0])/255.f,std::stoi(ret[1])/255.f,std::stoi(ret[2])/255.f, 1.f);
}
catch (const std::invalid_argument&)
{
Log(Debug::Error) << "Error: '" << fall << "' setting value (" << sum << ") is not a valid color, using middle gray as a fallback";
} }
return osg::Vec4f(std::stoi(ret[0])/255.f,std::stoi(ret[1])/255.f,std::stoi(ret[2])/255.f, 1.f);
} }
return osg::Vec4f(0.5f,0.5f,0.5f,1.f);
} }
} }