From c7691ec95e52ce72c5af96a52c21ee575b0209fb Mon Sep 17 00:00:00 2001 From: bubnikv Date: Wed, 30 Jun 2021 16:19:06 +0200 Subject: [PATCH] Forward compatibility - config substitutions: 1) Verify whether a value looks like an enum 2) Always report substitution of an enum with a boolean. --- src/libslic3r/Config.cpp | 15 ++++++++++++++- src/libslic3r/Config.hpp | 4 ++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/libslic3r/Config.cpp b/src/libslic3r/Config.cpp index e0c639fdf0..f93d98e1ba 100644 --- a/src/libslic3r/Config.cpp +++ b/src/libslic3r/Config.cpp @@ -506,6 +506,16 @@ void ConfigBase::set_deserialize(std::initializer_list items this->set_deserialize(item.opt_key, item.opt_value, substitutions_ctxt, item.append); } +static inline bool looks_like_enum_value(const std::string &s) +{ + if (value.empty() || value.size() > 64 || ! isalpha(value.front())) + return false; + for (const char c : s) + if (! (isalnum(c) || c == '_' || c == '-')) + return false; + return true; +} + bool ConfigBase::set_deserialize_raw(const t_config_option_key &opt_key_src, const std::string &value, ConfigSubstitutionContext& substitutions_ctxt, bool append) { t_config_option_key opt_key = opt_key_src; @@ -551,7 +561,10 @@ bool ConfigBase::set_deserialize_raw(const t_config_option_key &opt_key_src, con // Deserialize failed, try to substitute with a default value. assert(substitutions_ctxt.rule == ForwardCompatibilitySubstitutionRule::Enable || substitutions_ctxt.rule == ForwardCompatibilitySubstitutionRule::EnableSilent); - opt->set(optdef->default_value.get()); + if (optdef->type == coBool && looks_like_enum_value(value)) + static_cast(opt)->value = boost::iequals(value, "enabled") || boost::iequals(value, "on"); + else + opt->set(optdef->default_value.get()); if (substitutions_ctxt.rule == ForwardCompatibilitySubstitutionRule::Enable || substitutions_ctxt.rule == ForwardCompatibilitySubstitutionRule::EnableSystemSilent) { diff --git a/src/libslic3r/Config.hpp b/src/libslic3r/Config.hpp index 078e94cfb0..088eec33ab 100644 --- a/src/libslic3r/Config.hpp +++ b/src/libslic3r/Config.hpp @@ -1330,11 +1330,11 @@ public: bool deserialize(const std::string &str, bool append = false) override { UNUSED(append); - if (str == "1" || boost::iequals(str, "enabled") || boost::iequals(str, "on")) { + if (str == "1") { this->value = true; return true; } - if (str == "0" || boost::iequals(str, "disabled") || boost::iequals(str, "off")) { + if (str == "0") { this->value = false; return true; }