Skip to content

Commit

Permalink
fix bool_matches_if_present don't verify if check boolean value or not.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
newfrenchy83 committed Oct 27, 2023
1 parent 81bbcab commit fa207f7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/units/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> filter_type = utils::split(filter["tag_name"]);
Expand Down
9 changes: 7 additions & 2 deletions src/utils/config_filters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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);
}
Expand Down Expand Up @@ -99,7 +104,7 @@ bool utils::config_filters::double_matches_if_present(const config& filter, cons
return in_ranges<double>(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;
Expand Down
4 changes: 2 additions & 2 deletions src/utils/config_filters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit fa207f7

Please sign in to comment.