From bbc3c890ea54f08037535c7fa5040030bec1ec89 Mon Sep 17 00:00:00 2001 From: Vojtech Kral Date: Wed, 25 Apr 2018 13:44:06 +0200 Subject: [PATCH] Snapshots: Disable activation of incompatible snapshots --- xs/src/slic3r/Config/Snapshot.cpp | 28 +++++++++++----------- xs/src/slic3r/Config/Snapshot.hpp | 9 ++----- xs/src/slic3r/Config/Version.cpp | 4 ++-- xs/src/slic3r/GUI/ConfigSnapshotDialog.cpp | 16 +++++++------ xs/src/slic3r/Utils/Semver.hpp | 11 +++++++++ 5 files changed, 38 insertions(+), 30 deletions(-) diff --git a/xs/src/slic3r/Config/Snapshot.cpp b/xs/src/slic3r/Config/Snapshot.cpp index 18329aa5c..704fbcfa1 100644 --- a/xs/src/slic3r/Config/Snapshot.cpp +++ b/xs/src/slic3r/Config/Snapshot.cpp @@ -117,11 +117,11 @@ void Snapshot::load_ini(const std::string &path) if (! semver) throw_on_parse_error("invalid " + kvp.first + " format for " + section.first); if (kvp.first == "version") - vc.version = *semver; + vc.version.config_version = *semver; else if (kvp.first == "min_slic3r_version") - vc.min_slic3r_version = *semver; + vc.version.min_slic3r_version = *semver; else - vc.max_slic3r_version = *semver; + vc.version.max_slic3r_version = *semver; } else if (boost::starts_with(kvp.first, key_prefix_model) && kvp.first.size() > key_prefix_model.size()) { // Parse the printer variants installed for the current model. auto &set_variants = vc.models_variants_installed[kvp.first.substr(key_prefix_model.size())]; @@ -181,9 +181,9 @@ void Snapshot::save_ini(const std::string &path) // Export the vendor configs. for (const VendorConfig &vc : this->vendor_configs) { c << std::endl << "[Vendor:" << vc.name << "]" << std::endl; - c << "version = " << vc.version.to_string() << std::endl; - c << "min_slic3r_version = " << vc.min_slic3r_version.to_string() << std::endl; - c << "max_slic3r_version = " << vc.max_slic3r_version.to_string() << std::endl; + c << "version = " << vc.version.config_version.to_string() << std::endl; + c << "min_slic3r_version = " << vc.version.min_slic3r_version.to_string() << std::endl; + c << "max_slic3r_version = " << vc.version.max_slic3r_version.to_string() << std::endl; // Export installed printer models and their variants. for (const auto &model : vc.models_variants_installed) { if (model.second.size() == 0) @@ -326,10 +326,10 @@ void SnapshotDB::update_slic3r_versions(std::vector &index_db) for (Snapshot::VendorConfig &vendor_config : snapshot.vendor_configs) { auto it = std::find_if(index_db.begin(), index_db.end(), [&vendor_config](const Index &idx) { return idx.vendor() == vendor_config.name; }); if (it != index_db.end()) { - Index::const_iterator it_version = it->find(vendor_config.version); + Index::const_iterator it_version = it->find(vendor_config.version.config_version); if (it_version != it->end()) { - vendor_config.min_slic3r_version = it_version->min_slic3r_version; - vendor_config.max_slic3r_version = it_version->max_slic3r_version; + vendor_config.version.min_slic3r_version = it_version->min_slic3r_version; + vendor_config.version.max_slic3r_version = it_version->max_slic3r_version; } } } @@ -395,16 +395,16 @@ const Snapshot& SnapshotDB::take_snapshot(const AppConfig &app_config, Snapshot: bundle.load_configbundle((data_dir / "vendor" / (cfg.name + ".ini")).string(), PresetBundle::LOAD_CFGBUNDLE_VENDOR_ONLY); for (const VendorProfile &vp : bundle.vendors) if (vp.id == cfg.name) - cfg.version = vp.config_version; + cfg.version.config_version = vp.config_version; // Fill-in the min/max slic3r version from the config index, if possible. try { // Load the config index for the vendor. Index index; index.load(data_dir / "vendor" / (cfg.name + ".idx")); - auto it = index.find(cfg.version); + auto it = index.find(cfg.version.config_version); if (it != index.end()) { - cfg.min_slic3r_version = it->min_slic3r_version; - cfg.max_slic3r_version = it->max_slic3r_version; + cfg.version.min_slic3r_version = it->min_slic3r_version; + cfg.version.max_slic3r_version = it->max_slic3r_version; } } catch (const std::runtime_error &err) { } @@ -476,7 +476,7 @@ SnapshotDB::const_iterator SnapshotDB::snapshot_with_vendor_preset(const std::st auto it_vendor_config = std::lower_bound(snapshot.vendor_configs.begin(), snapshot.vendor_configs.end(), key, [](const Snapshot::VendorConfig &cfg1, const Snapshot::VendorConfig &cfg2) { return cfg1.name < cfg2.name; }); if (it_vendor_config != snapshot.vendor_configs.end() && it_vendor_config->name == vendor_name && - config_version == it_vendor_config->version) { + config_version == it_vendor_config->version.config_version) { // Vendor config found with the correct version. // Save it, but continue searching, as we want the newest snapshot. it_found = it; diff --git a/xs/src/slic3r/Config/Snapshot.hpp b/xs/src/slic3r/Config/Snapshot.hpp index 77aee3e21..a916dfe92 100644 --- a/xs/src/slic3r/Config/Snapshot.hpp +++ b/xs/src/slic3r/Config/Snapshot.hpp @@ -18,7 +18,6 @@ class AppConfig; namespace GUI { namespace Config { -class Version; class Index; // A snapshot contains: @@ -76,12 +75,8 @@ public: struct VendorConfig { // Name of the vendor contained in this snapshot. std::string name; - // Version of the vendor config contained in this snapshot. - Semver version = Semver::invalid(); - // Minimum Slic3r version compatible with this vendor configuration. - Semver min_slic3r_version = Semver::zero(); - // Maximum Slic3r version compatible with this vendor configuration, or empty. - Semver max_slic3r_version = Semver::inf(); + // Version of the vendor config contained in this snapshot, along with compatibility data. + Version version; // Which printer models of this vendor were installed, and which variants of the models? std::map> models_variants_installed; }; diff --git a/xs/src/slic3r/Config/Version.cpp b/xs/src/slic3r/Config/Version.cpp index dc09ac870..a85322eca 100644 --- a/xs/src/slic3r/Config/Version.cpp +++ b/xs/src/slic3r/Config/Version.cpp @@ -13,7 +13,7 @@ namespace Slic3r { namespace GUI { namespace Config { -static boost::optional s_current_slic3r_semver = Semver::parse(SLIC3R_VERSION); +static const Semver s_current_slic3r_semver(SLIC3R_VERSION); // Optimized lexicographic compare of two pre-release versions, ignoring the numeric suffix. static int compare_prerelease(const char *p1, const char *p2) @@ -62,7 +62,7 @@ bool Version::is_slic3r_supported(const Semver &slic3r_version) const bool Version::is_current_slic3r_supported() const { - return this->is_slic3r_supported(*s_current_slic3r_semver); + return this->is_slic3r_supported(s_current_slic3r_semver); } #if 0 diff --git a/xs/src/slic3r/GUI/ConfigSnapshotDialog.cpp b/xs/src/slic3r/GUI/ConfigSnapshotDialog.cpp index 99af707e1..c0affcab7 100644 --- a/xs/src/slic3r/GUI/ConfigSnapshotDialog.cpp +++ b/xs/src/slic3r/GUI/ConfigSnapshotDialog.cpp @@ -39,18 +39,16 @@ static std::string generate_html_row(const Config::Snapshot &snapshot, bool row_ text += " (" + snapshot.comment + ")"; text += "
"; // End of row header. -// text += _(L("ID:")) + " " + snapshot.id + "
"; - // text += _(L("time captured:")) + " " + Utils::format_local_date_time(snapshot.time_captured) + "
"; text += _(L("slic3r version")) + ": " + snapshot.slic3r_version_captured.to_string() + "
"; -// text += "reason: " + snapshot.reason + "
"; text += _(L("print")) + ": " + snapshot.print + "
"; text += _(L("filaments")) + ": " + snapshot.filaments.front() + "
"; text += _(L("printer")) + ": " + snapshot.printer + "
"; + bool compatible = true; for (const Config::Snapshot::VendorConfig &vc : snapshot.vendor_configs) { - text += _(L("vendor")) + ": " + vc.name + ", ver: " + vc.version.to_string() + ", min slic3r ver: " + vc.min_slic3r_version.to_string(); - if (vc.max_slic3r_version != Semver::inf()) - text += ", max slic3r ver: " + vc.max_slic3r_version.to_string(); + text += _(L("vendor")) + ": " + vc.name + ", ver: " + vc.version.config_version.to_string() + ", min slic3r ver: " + vc.version.min_slic3r_version.to_string(); + if (vc.version.max_slic3r_version != Semver::inf()) + text += ", max slic3r ver: " + vc.version.max_slic3r_version.to_string(); text += "
"; for (const std::pair> &model : vc.models_variants_installed) { text += _(L("model")) + ": " + model.first + ", " + _(L("variants")) + ": "; @@ -61,9 +59,13 @@ static std::string generate_html_row(const Config::Snapshot &snapshot, bool row_ } text += "
"; } + if (! vc.version.is_current_slic3r_supported()) { compatible = false; } } - if (! snapshot_active) + if (! compatible) { + text += "

" + _(L("Incompatible with this Slic3r")) + "

"; + } + else if (! snapshot_active) text += "

" + _(L("Activate")) + "

"; text += ""; text += ""; diff --git a/xs/src/slic3r/Utils/Semver.hpp b/xs/src/slic3r/Utils/Semver.hpp index 87396d812..ae9d21c6b 100644 --- a/xs/src/slic3r/Utils/Semver.hpp +++ b/xs/src/slic3r/Utils/Semver.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -33,6 +34,16 @@ public: set_prerelease(prerelease); } + Semver(const std::string &str) : ver(semver_zero()) + { + auto parsed = parse(str); + if (! parsed) { + throw std::runtime_error(std::string("Could not parse version string: ") + str); + } + ver = parsed->ver; + parsed->ver = semver_zero(); + } + static boost::optional parse(const std::string &str) { semver_t ver = semver_zero();