From 738a226da7898dd4cd17a0ecd757f32dde730ad7 Mon Sep 17 00:00:00 2001 From: newfrenchy83 Date: Fri, 27 Oct 2023 15:00:32 +0200 Subject: [PATCH] fix bool_matches_if_present don't verify if check boolean value or not. if filter[attribute] has value other what boolean, it can be doing checking and should return true like if !filter.has_attribute(attribute). name of function and bool_or_empty() changed. --- src/units/unit.cpp | 8 ++++---- src/utils/config_filters.cpp | 9 +++++++-- src/utils/config_filters.hpp | 4 ++-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/units/unit.cpp b/src/units/unit.cpp index 6d31888f203d0..eb12c9dfc37ee 100644 --- a/src/units/unit.cpp +++ b/src/units/unit.cpp @@ -1432,16 +1432,16 @@ static bool matches_ability_filter(const config & cfg, const std::string& tag_na } } - if(!bool_matches_if_present(filter, cfg, "affect_self", true)) + if(!:bool_matches_if_present_and_valid(filter, cfg, "affect_self", true)) return false; - if(!bool_or_empty(filter, cfg, "affect_allies")) + if(!matches_if_attribute_boolean_or_empty(filter, cfg, "affect_allies")) return false; - if(!bool_matches_if_present(filter, cfg, "affect_enemies", false)) + if(!:bool_matches_if_present_and_valid(filter, cfg, "affect_enemies", false)) return false; - if(!bool_matches_if_present(filter, cfg, "cumulative", false)) + if(!:bool_matches_if_present_and_valid(filter, cfg, "cumulative", false)) return false; const std::vector filter_type = utils::split(filter["tag_name"]); diff --git a/src/utils/config_filters.cpp b/src/utils/config_filters.cpp index 5fc7d9e18cf17..1c669c5cdf1b1 100644 --- a/src/utils/config_filters.cpp +++ b/src/utils/config_filters.cpp @@ -20,11 +20,16 @@ #include "serialization/string_utils.hpp" // for utils::split #include "utils/math.hpp" // for in_ranges -bool utils::config_filters::bool_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, bool def) +bool utils::config_filters:::bool_matches_if_present_and_valid(const config& filter, const config& cfg, const std::string& attribute, bool def) { if(!filter.has_attribute(attribute)) { return true; } + //check if filter is boolean, if not then checking can't be done and return true. + std::set filter_attribute = utils::split_set(filter[attribute].str()); + if(filter_attribute.count("yes") == 0 && filter_attribute.count("true") == 0 && filter_attribute.count("no") == 0 && filter_attribute.count("false") == 0) { + return true; + } return filter[attribute].to_bool() == cfg[attribute].to_bool(def); } @@ -99,7 +104,7 @@ bool utils::config_filters::double_matches_if_present(const config& filter, cons return in_ranges(cfg[attribute].to_double(value_def), utils::parse_ranges_real(filter[attribute].str())); } -bool utils::config_filters::bool_or_empty(const config& filter, const config& cfg, const std::string& attribute) +bool utils::config_filters::matches_if_attribute_boolean_or_empty(const config& filter, const config& cfg, const std::string& attribute) { if(!filter.has_attribute(attribute)) { return true; diff --git a/src/utils/config_filters.hpp b/src/utils/config_filters.hpp index a16c7a5a7cf82..58e8625838571 100644 --- a/src/utils/config_filters.hpp +++ b/src/utils/config_filters.hpp @@ -32,7 +32,7 @@ namespace utils::config_filters * * Always returns true if the filter puts no restriction on the value of @a cfg[@a attribute]. */ -bool bool_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, bool def); +bool :bool_matches_if_present_and_valid(const config& filter, const config& cfg, const std::string& attribute, bool def); /** * Checks whether the filter matches the value of @a cfg[@a attribute]. If @a cfg doesn't have that @@ -67,6 +67,6 @@ bool int_matches_if_present_or_negative( bool string_matches_if_present( const config& filter, const config& cfg, const std::string& attribute, const std::string& def); -bool bool_or_empty(const config& filter, const config& cfg, const std::string& attribute); +bool matches_if_attribute_boolean_or_empty(const config& filter, const config& cfg, const std::string& attribute); } // namespace utils::config_filters