Fixed downloading resources of installed bundle

This commit is contained in:
Jan Bařtipán 2024-05-17 18:08:53 +02:00 committed by David Kocik
parent 52d01478ea
commit 05361f3da7
5 changed files with 33 additions and 90 deletions

View File

@ -3354,8 +3354,9 @@ bool ConfigWizard::priv::apply_config(AppConfig *app_config, PresetBundle *prese
if (install_bundles.size() > 0) { if (install_bundles.size() > 0) {
// Install bundles from resources or cache / vendor. // Install bundles from resources or cache / vendor.
// Don't create snapshot - we've already done that above if applicable. // Don't create snapshot - we've already done that above if applicable.
GUI_App& app = wxGetApp();
bool install_result = updater->install_bundles_rsrc_or_cache_vendor(std::move(install_bundles), false); const auto* archive_db = app.plater()->get_preset_archive_database();
bool install_result = updater->install_bundles_rsrc_or_cache_vendor(std::move(install_bundles), archive_db->get_archive_repositories(), false);
if (!install_result) if (!install_result)
return false; return false;
} else { } else {
@ -3574,10 +3575,11 @@ void ConfigWizard::priv::set_config_updated_from_archive(bool is_updated)
// This set with preset_updater used to be done in GUI_App::run_wizard before ConfigWizard::run() // This set with preset_updater used to be done in GUI_App::run_wizard before ConfigWizard::run()
GUI_App& app = wxGetApp(); GUI_App& app = wxGetApp();
// Do blocking sync on every change of archive repos, so user is always offered recent profiles. // Do blocking sync on every change of archive repos, so user is always offered recent profiles.
app.preset_updater->sync_blocking(app.preset_bundle, &app, app.plater()->get_preset_archive_database()->get_archive_repositories(), app.plater()->get_preset_archive_database()->get_selected_repositories_uuid()); const ArchiveRepositoryVector &repos = app.plater()->get_preset_archive_database()->get_archive_repositories();
app.preset_updater->sync_blocking(app.preset_bundle, &app, repos, app.plater()->get_preset_archive_database()->get_selected_repositories_uuid());
// Offer update installation. It used to be offered only when wizard run reason was RR_USER. // Offer update installation. It used to be offered only when wizard run reason was RR_USER.
app.preset_updater->update_index_db(); app.preset_updater->update_index_db();
app.preset_updater->config_update(app.app_config->orig_version(), PresetUpdater::UpdateParams::SHOW_TEXT_BOX); app.preset_updater->config_update(app.app_config->orig_version(), PresetUpdater::UpdateParams::SHOW_TEXT_BOX, repos);
// We have now probably changed data. We need to rebuild or database from which wizards constructs. // We have now probably changed data. We need to rebuild or database from which wizards constructs.
// DK: Im not sure if we should do full load_vendors. or only load BundleMap::load(). // DK: Im not sure if we should do full load_vendors. or only load BundleMap::load().

View File

@ -3475,7 +3475,7 @@ bool GUI_App::check_updates(const bool verbose)
PresetUpdater::UpdateResult updater_result; PresetUpdater::UpdateResult updater_result;
try { try {
preset_updater->update_index_db(); preset_updater->update_index_db();
updater_result = preset_updater->config_update(app_config->orig_version(), verbose ? PresetUpdater::UpdateParams::SHOW_TEXT_BOX : PresetUpdater::UpdateParams::SHOW_NOTIFICATION); updater_result = preset_updater->config_update(app_config->orig_version(), verbose ? PresetUpdater::UpdateParams::SHOW_TEXT_BOX : PresetUpdater::UpdateParams::SHOW_NOTIFICATION, plater()->get_preset_archive_database()->get_archive_repositories());
if (updater_result == PresetUpdater::R_INCOMPAT_EXIT) { if (updater_result == PresetUpdater::R_INCOMPAT_EXIT) {
mainframe->Close(); mainframe->Close();
// Applicaiton is closing. // Applicaiton is closing.

View File

@ -781,7 +781,10 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
if (wxGetApp().is_editor()) { if (wxGetApp().is_editor()) {
this->q->Bind(EVT_EJECT_DRIVE_NOTIFICAION_CLICKED, [this](EjectDriveNotificationClickedEvent&) { this->q->eject_drive(); }); this->q->Bind(EVT_EJECT_DRIVE_NOTIFICAION_CLICKED, [this](EjectDriveNotificationClickedEvent&) { this->q->eject_drive(); });
this->q->Bind(EVT_EXPORT_GCODE_NOTIFICAION_CLICKED, [this](ExportGcodeNotificationClickedEvent&) { this->q->export_gcode(true); }); this->q->Bind(EVT_EXPORT_GCODE_NOTIFICAION_CLICKED, [this](ExportGcodeNotificationClickedEvent&) { this->q->export_gcode(true); });
this->q->Bind(EVT_PRESET_UPDATE_AVAILABLE_CLICKED, [](PresetUpdateAvailableClickedEvent&) { wxGetApp().get_preset_updater()->on_update_notification_confirm(); }); this->q->Bind(EVT_PRESET_UPDATE_AVAILABLE_CLICKED, [](PresetUpdateAvailableClickedEvent&) {
GUI_App &app = wxGetApp();
app.get_preset_updater()->on_update_notification_confirm(app.plater()->get_preset_archive_database()->get_archive_repositories());
});
this->q->Bind(EVT_REMOVABLE_DRIVE_EJECTED, [this, q](RemovableDriveEjectEvent &evt) { this->q->Bind(EVT_REMOVABLE_DRIVE_EJECTED, [this, q](RemovableDriveEjectEvent &evt) {
if (evt.data.second) { if (evt.data.second) {
q->show_action_buttons(); q->show_action_buttons();

View File

@ -184,21 +184,18 @@ struct PresetUpdater::priv
priv(); priv();
void set_download_prefs(const AppConfig *app_config); void set_download_prefs(const AppConfig *app_config);
//bool get_file(const std::string &url, const fs::path &target_path) const;
void prune_tmps() const; void prune_tmps() const;
void clear_cache_vendor() const; void clear_cache_vendor() const;
void sync_config(const VendorMap& vendors, const GUI::ArchiveRepository& archive); void sync_config(const VendorMap& vendors, const GUI::ArchiveRepository& archive);
void check_install_indices() const; void check_install_indices() const;
Updates get_config_updates(const Semver& old_slic3r_version) const; Updates get_config_updates(const Semver& old_slic3r_version) const;
bool perform_updates(Updates &&updates, bool snapshot = true) const; bool perform_updates(Updates &&updates, const ArchiveRepositoryVector& repositories, bool snapshot = true) const;
void set_waiting_updates(Updates u); void set_waiting_updates(Updates u);
// checks existence and downloads resource to cache // checks existence and downloads resource to cache
void get_missing_resource(const GUI::ArchiveRepository& archive, const std::string& vendor, const std::string& filename, const std::string& repository_id_from_ini) const; void get_missing_resource(const GUI::ArchiveRepository& archive, const std::string& vendor, const std::string& filename, const std::string& repository_id_from_ini) const;
// checks existence and downloads resource to vendor or copy from cache to vendor // checks existence and downloads resource to vendor or copy from cache to vendor
void get_or_copy_missing_resource(const GUI::ArchiveRepository& archive, const std::string& vendor, const std::string& filename, const std::string& repository_id_from_ini) const; void get_or_copy_missing_resource(const GUI::ArchiveRepository& archive, const std::string& vendor, const std::string& filename, const std::string& repository_id_from_ini) const;
// checks existence and copies resource to vendor from cache to vendor
void copy_missing_resource(const std::string& vendor, const std::string& filename, const std::string& url) const;
void update_index_db(); void update_index_db();
}; };
@ -229,41 +226,6 @@ void PresetUpdater::priv::set_download_prefs(const AppConfig *app_config)
enabled_config_update = app_config->get_bool("preset_update") && !app_config->legacy_datadir(); enabled_config_update = app_config->get_bool("preset_update") && !app_config->legacy_datadir();
} }
// Downloads a file (http get operation). Cancels if the Updater is being destroyed.
//bool PresetUpdater::priv::get_file(const std::string &url, const fs::path &target_path) const
//{
// bool res = false;
// fs::path tmp_path = target_path;
// tmp_path += format(".%1%%2%", get_current_pid(), TMP_EXTENSION);
//
// BOOST_LOG_TRIVIAL(info) << format("Get: `%1%`\n\t-> `%2%`\n\tvia tmp path `%3%`",
// url,
// target_path.string(),
// tmp_path.string());
//
// Http::get(url)
// .on_progress([](Http::Progress, bool &cancel) {
// if (cancel) { cancel = true; }
// })
// .on_error([&](std::string body, std::string error, unsigned http_status) {
// (void)body;
// BOOST_LOG_TRIVIAL(error) << format("Error getting: `%1%`: HTTP %2%, %3%",
// url,
// http_status,
// error);
// })
// .on_complete([&](std::string body, unsigned /* http_status */) {
// fs::fstream file(tmp_path, std::ios::out | std::ios::binary | std::ios::trunc);
// file.write(body.c_str(), body.size());
// file.close();
// fs::rename(tmp_path, target_path);
// res = true;
// })
// .perform_sync();
//
// return res;
//}
// Remove leftover paritally downloaded files, if any. // Remove leftover paritally downloaded files, if any.
void PresetUpdater::priv::prune_tmps() const void PresetUpdater::priv::prune_tmps() const
{ {
@ -359,35 +321,6 @@ void PresetUpdater::priv::get_or_copy_missing_resource(const GUI::ArchiveReposit
BOOST_LOG_TRIVIAL(debug) << "Copiing: " << file_in_cache << " to " << file_in_vendor; BOOST_LOG_TRIVIAL(debug) << "Copiing: " << file_in_cache << " to " << file_in_vendor;
copy_file_fix(file_in_cache, file_in_vendor); copy_file_fix(file_in_cache, file_in_vendor);
} }
// gets resource to vendor/<vendor_name>/
void PresetUpdater::priv::copy_missing_resource(const std::string& vendor, const std::string& filename, const std::string& url) const
{
if (filename.empty() || vendor.empty())
return;
const fs::path file_in_vendor(vendor_path / (vendor + "/" + filename));
const fs::path file_in_rsrc(rsrc_path / (vendor + "/" + filename));
const fs::path file_in_cache(cache_path / (vendor + "/" + filename));
if (fs::exists(file_in_vendor)) { // Already in vendor. No need to do anything.
BOOST_LOG_TRIVIAL(info) << "Resource " << vendor << " / " << filename << " found in vendor folder. No need to download.";
return;
}
if (fs::exists(file_in_rsrc)) { // In resources dir since installation. No need to do anything.
BOOST_LOG_TRIVIAL(info) << "Resource " << vendor << " / " << filename << " found in resources folder. No need to download.";
return;
}
if (!fs::exists(file_in_cache)) { // No file to copy. Bad!
BOOST_LOG_TRIVIAL(error) << "Resource " << vendor << " / " << filename << " not found!";
return;
}
if (!fs::exists(file_in_vendor.parent_path())) // create vendor_name dir in vendor
fs::create_directory(file_in_vendor.parent_path());
BOOST_LOG_TRIVIAL(debug) << "Copiing: " << file_in_cache << " to " << file_in_vendor;
copy_file_fix(file_in_cache, file_in_vendor);
}
// Download vendor indices. Also download new bundles if an index indicates there's a new one available. // Download vendor indices. Also download new bundles if an index indicates there's a new one available.
// Both are saved in cache. // Both are saved in cache.
@ -788,14 +721,14 @@ void PresetUpdater::priv::sync_config(const VendorMap& vendors, const GUI::Archi
} }
for (const auto& model : vp.models) { for (const auto& model : vp.models) {
for (const std::string& res : { model.bed_texture, model.bed_model, model.thumbnail }) { for (const std::string& res : { model.bed_texture, model.bed_model, model.thumbnail }) {
if (!model.thumbnail.empty()) { if (!res.empty()) {
try try
{ {
get_or_copy_missing_resource(archive_repository, vp.id, res, vp.repo_id); get_or_copy_missing_resource(archive_repository, vp.id, res, vp.repo_id);
} }
catch (const std::exception& e) catch (const std::exception& e)
{ {
BOOST_LOG_TRIVIAL(error) << "Failed to get " << model.thumbnail << " for " << vp.id << " " << model.id << ": " << e.what(); BOOST_LOG_TRIVIAL(error) << "Failed to get " << res << " for " << vp.id << " " << model.id << ": " << e.what();
} }
} }
if (cancel) if (cancel)
@ -1029,7 +962,7 @@ Updates PresetUpdater::priv::get_config_updates(const Semver &old_slic3r_version
return updates; return updates;
} }
bool PresetUpdater::priv::perform_updates(Updates &&updates, bool snapshot) const bool PresetUpdater::priv::perform_updates(Updates &&updates, const ArchiveRepositoryVector& repositories, bool snapshot) const
{ {
if (updates.incompats.size() > 0) { if (updates.incompats.size() > 0) {
if (snapshot) { if (snapshot) {
@ -1116,7 +1049,12 @@ bool PresetUpdater::priv::perform_updates(Updates &&updates, bool snapshot) cons
continue; continue;
try try
{ {
copy_missing_resource(vp.id, resource, vp.config_update_url); auto it = std::find_if(repositories.begin(), repositories.end(), [&vp](const auto& i){ return vp.repo_id == i->get_manifest().id; });
if (it != repositories.end())
get_or_copy_missing_resource(*(*it), vp.id, resource, vp.repo_id);
else {
BOOST_LOG_TRIVIAL(error) << "Failed to prepare " << resource << " for " << vp.id << " " << model.id << ": Missing record for repository with repo_id " << vp.repo_id;
}
} }
catch (const std::exception& e) catch (const std::exception& e)
{ {
@ -1159,7 +1097,7 @@ void PresetUpdater::sync(const PresetBundle *preset_bundle, wxEvtHandler* evt_ha
{ {
p->set_download_prefs(GUI::wxGetApp().app_config); p->set_download_prefs(GUI::wxGetApp().app_config);
if (!p->enabled_config_update) { return; } if (!p->enabled_config_update) { return; }
p->thread = std::thread([this, &vendors = preset_bundle->vendors, &repositories, &selected_repo_uuids, evt_handler]() { p->thread = std::thread([this, &vendors = preset_bundle->vendors, &repositories, &selected_repo_uuids, evt_handler]() {
this->p->clear_cache_vendor(); this->p->clear_cache_vendor();
this->p->prune_tmps(); this->p->prune_tmps();
@ -1247,7 +1185,7 @@ static bool reload_configs_update_gui()
return true; return true;
} }
PresetUpdater::UpdateResult PresetUpdater::config_update(const Semver& old_slic3r_version, UpdateParams params) const PresetUpdater::UpdateResult PresetUpdater::config_update(const Semver& old_slic3r_version, UpdateParams params, const ArchiveRepositoryVector& repositories) const
{ {
if (! p->enabled_config_update) { return R_NOOP; } if (! p->enabled_config_update) { return R_NOOP; }
@ -1281,7 +1219,7 @@ PresetUpdater::UpdateResult PresetUpdater::config_update(const Semver& old_slic3
// This effectively removes the incompatible bundles: // This effectively removes the incompatible bundles:
// (snapshot is taken beforehand) // (snapshot is taken beforehand)
if (! p->perform_updates(std::move(updates)) || if (! p->perform_updates(std::move(updates), repositories) ||
! GUI::wxGetApp().run_wizard(GUI::ConfigWizard::RR_DATA_INCOMPAT)) ! GUI::wxGetApp().run_wizard(GUI::ConfigWizard::RR_DATA_INCOMPAT))
return R_INCOMPAT_EXIT; return R_INCOMPAT_EXIT;
@ -1323,7 +1261,7 @@ PresetUpdater::UpdateResult PresetUpdater::config_update(const Semver& old_slic3
const auto res = dlg.ShowModal(); const auto res = dlg.ShowModal();
if (res == wxID_OK) { if (res == wxID_OK) {
BOOST_LOG_TRIVIAL(info) << "User wants to update..."; BOOST_LOG_TRIVIAL(info) << "User wants to update...";
if (! p->perform_updates(std::move(updates)) || if (! p->perform_updates(std::move(updates), repositories) ||
! reload_configs_update_gui()) ! reload_configs_update_gui())
return R_INCOMPAT_EXIT; return R_INCOMPAT_EXIT;
return R_UPDATE_INSTALLED; return R_UPDATE_INSTALLED;
@ -1366,7 +1304,7 @@ PresetUpdater::UpdateResult PresetUpdater::config_update(const Semver& old_slic3
const auto res = dlg.ShowModal(); const auto res = dlg.ShowModal();
if (res == wxID_OK) { if (res == wxID_OK) {
BOOST_LOG_TRIVIAL(debug) << "User agreed to perform the update"; BOOST_LOG_TRIVIAL(debug) << "User agreed to perform the update";
if (! p->perform_updates(std::move(updates)) || if (! p->perform_updates(std::move(updates), repositories) ||
! reload_configs_update_gui()) ! reload_configs_update_gui())
return R_ALL_CANCELED; return R_ALL_CANCELED;
return R_UPDATE_INSTALLED; return R_UPDATE_INSTALLED;
@ -1387,7 +1325,7 @@ PresetUpdater::UpdateResult PresetUpdater::config_update(const Semver& old_slic3
return R_NOOP; return R_NOOP;
} }
bool PresetUpdater::install_bundles_rsrc_or_cache_vendor(std::vector<std::string> bundles, bool snapshot) const bool PresetUpdater::install_bundles_rsrc_or_cache_vendor(std::vector<std::string> bundles, const ArchiveRepositoryVector& repositories, bool snapshot) const
{ {
Updates updates; Updates updates;
@ -1490,10 +1428,10 @@ bool PresetUpdater::install_bundles_rsrc_or_cache_vendor(std::vector<std::string
} }
} }
return p->perform_updates(std::move(updates), snapshot); return p->perform_updates(std::move(updates), repositories, snapshot);
} }
void PresetUpdater::on_update_notification_confirm() void PresetUpdater::on_update_notification_confirm(const ArchiveRepositoryVector& repositories)
{ {
if (!p->has_waiting_updates) if (!p->has_waiting_updates)
return; return;
@ -1516,7 +1454,7 @@ void PresetUpdater::on_update_notification_confirm()
const auto res = dlg.ShowModal(); const auto res = dlg.ShowModal();
if (res == wxID_OK) { if (res == wxID_OK) {
BOOST_LOG_TRIVIAL(debug) << "User agreed to perform the update"; BOOST_LOG_TRIVIAL(debug) << "User agreed to perform the update";
if (p->perform_updates(std::move(p->waiting_updates)) && if (p->perform_updates(std::move(p->waiting_updates), repositories) &&
reload_configs_update_gui()) { reload_configs_update_gui()) {
p->has_waiting_updates = false; p->has_waiting_updates = false;
} }

View File

@ -62,14 +62,14 @@ public:
// A false return value implies Slic3r should exit due to incompatibility of configuration. // A false return value implies Slic3r should exit due to incompatibility of configuration.
// Providing old slic3r version upgrade profiles on upgrade of an application even in case // Providing old slic3r version upgrade profiles on upgrade of an application even in case
// that the config index installed from the Internet is equal to the index contained in the installation package. // that the config index installed from the Internet is equal to the index contained in the installation package.
UpdateResult config_update(const Semver &old_slic3r_version, UpdateParams params) const; UpdateResult config_update(const Semver &old_slic3r_version, UpdateParams params, const ArchiveRepositoryVector& repositories) const;
void update_index_db(); void update_index_db();
// "Update" a list of bundles from resources or cache/vendor (behaves like an online update). // "Update" a list of bundles from resources or cache/vendor (behaves like an online update).
bool install_bundles_rsrc_or_cache_vendor(std::vector<std::string> bundles, bool snapshot = true) const; bool install_bundles_rsrc_or_cache_vendor(std::vector<std::string> bundles, const ArchiveRepositoryVector& repositories, bool snapshot = true) const;
void on_update_notification_confirm(); void on_update_notification_confirm(const ArchiveRepositoryVector& repositories);
bool version_check_enabled() const; bool version_check_enabled() const;