diff --git a/src/slic3r/Utils/PresetUpdater.cpp b/src/slic3r/Utils/PresetUpdater.cpp index 8324832108..655078318e 100644 --- a/src/slic3r/Utils/PresetUpdater.cpp +++ b/src/slic3r/Utils/PresetUpdater.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include @@ -204,7 +203,6 @@ struct PresetUpdater::priv // Per-vendor update checking std::set checked_vendors; - std::mutex vendor_check_mutex; std::vector vendor_check_threads; std::atomic vendor_check_cancel{false}; @@ -221,10 +219,10 @@ struct PresetUpdater::priv priv(); void set_download_prefs(AppConfig *app_config); - bool get_file(const std::string &url, const fs::path &target_path) const; - //BBS: refine preset update logic + bool get_file(const std::string &url, const fs::path &target_path) const; + //BBS: refine preset update logic bool extract_file(const fs::path &source_path, const fs::path &dest_path = {}); - void prune_tmps() const; + void prune_tmp(const std::string& vendor_id) const; void sync_version() const; void parse_version_string(const std::string& body) const; void sync_resources(std::string http_url, std::map &resources, bool check_patch = false, std::string current_version="", std::string changelog_file=""); @@ -371,22 +369,14 @@ bool PresetUpdater::priv::extract_file(const fs::path &source_path, const fs::pa return true; } -// Remove leftover paritally downloaded files, if any. -void PresetUpdater::priv::prune_tmps() const +// Remove a leftover partial archive for the vendor about to be synchronized. +void PresetUpdater::priv::prune_tmp(const std::string& vendor_id) const { - boost::system::error_code ec; - auto dir_it = boost::filesystem::directory_iterator(cache_path, ec); - if (ec) { - BOOST_LOG_TRIVIAL(warning) << "[Orca Updater]failed to list cache dir " << cache_path.string() << ": " << ec.message(); - return; - } - for (auto &dir_entry : dir_it) - if (is_plain_file(dir_entry) && dir_entry.path().extension() == TMP_EXTENSION) { - BOOST_LOG_TRIVIAL(debug) << "[Orca Updater]remove old cached files: " << dir_entry.path().string(); - fs::remove(dir_entry.path(), ec); - if (ec) - BOOST_LOG_TRIVIAL(warning) << "[Orca Updater]failed to remove " << dir_entry.path().string() << ": " << ec.message(); - } + boost::system::error_code ec; + const fs::path tmp_path = cache_path / (vendor_id + TMP_EXTENSION); + fs::remove(tmp_path, ec); + if (ec) + BOOST_LOG_TRIVIAL(warning) << "[Orca Updater]failed to remove " << tmp_path.string() << ": " << ec.message(); } //BBS: refine the Preset Updater logic @@ -1346,49 +1336,29 @@ PresetUpdater::~PresetUpdater() //BBS: change directories by design //BBS: refine the preset updater logic -void PresetUpdater::sync(std::string http_url, std::string language, std::string plugin_version, PresetBundle *preset_bundle) +void PresetUpdater::sync(std::string http_url, std::string language, std::string plugin_version, PresetBundle * /*preset_bundle*/) { //p->set_download_prefs(GUI::wxGetApp().app_config); if (!p->enabled_version_check && !p->enabled_config_update) { return; } - // Copy the whole vendors data for use in the background thread - // Unfortunatelly as of C++11, it needs to be copied again - // into the closure (but perhaps the compiler can elide this). - VendorMap vendors = preset_bundle ? preset_bundle->vendors : VendorMap{}; - - // Determine active vendor before entering the thread - std::string active_vendor; - if (preset_bundle) { - const Preset& printer = preset_bundle->printers.get_edited_preset(); - if (printer.vendor) - active_vendor = printer.vendor->id; - } - - p->thread = std::thread([this, vendors, active_vendor, http_url, language, plugin_version]() { - this->p->prune_tmps(); - if (p->cancel) - return; - this->p->sync_version(); - if (p->cancel) - return; - // Per-vendor config check for the active vendor at startup - if (!active_vendor.empty() && !vendors.empty()) { - this->p->sync_vendor_config(active_vendor); - if (p->cancel) - return; - { - std::lock_guard lock(this->p->vendor_check_mutex); - this->p->checked_vendors.insert(active_vendor); - } - } - if (p->cancel) - return; - this->p->sync_plugins(http_url, plugin_version); - this->p->sync_printer_config(http_url); - //if (p->cancel) - // return; - //remove the tooltip currently - //this->p->sync_tooltip(http_url, language); + p->thread = std::thread([this, http_url, language, plugin_version]() { + try { + this->p->sync_version(); + if (p->cancel) + return; + // Vendor profile updates are triggered by check_vendor_update() + // after the startup printer preset has been restored. + this->p->sync_plugins(http_url, plugin_version); + this->p->sync_printer_config(http_url); + //if (p->cancel) + // return; + //remove the tooltip currently + //this->p->sync_tooltip(http_url, language); + } catch (const std::exception &e) { + BOOST_LOG_TRIVIAL(error) << "[Orca Updater] background sync failed: " << e.what(); + } catch (...) { + BOOST_LOG_TRIVIAL(error) << "[Orca Updater] background sync failed with an unknown exception"; + } }); } @@ -1397,16 +1367,17 @@ void PresetUpdater::check_vendor_update(const std::string& vendor_id) if (!p->enabled_config_update) return; if (vendor_id.empty()) return; - std::lock_guard lock(p->vendor_check_mutex); - if (!p->checked_vendors.insert(vendor_id).second) return; p->vendor_check_threads.emplace_back([this, vendor_id]() { try { + this->p->prune_tmp(vendor_id); this->p->sync_vendor_config(vendor_id); } catch (const std::exception& e) { BOOST_LOG_TRIVIAL(error) << "[Orca Updater] vendor update failed for " << vendor_id << ": " << e.what(); + } catch (...) { + BOOST_LOG_TRIVIAL(error) << "[Orca Updater] vendor update failed for " << vendor_id << " with an unknown exception"; } }); }