FIX: presetupdater: fix the crash issue when exit app at the early stage

Change-Id: Ia6e6e56e09c5d3ebbdd7b9a8d0bb1b898d05935c
This commit is contained in:
lane.wei 2022-07-29 17:28:13 +08:00 committed by Lane.Wei
parent ab16d4c73e
commit fc659fcf2c
3 changed files with 57 additions and 39 deletions

View file

@ -1046,7 +1046,8 @@ void GUI_App::post_init()
bool cw_showed = this->config_wizard_startup(); bool cw_showed = this->config_wizard_startup();
std::string http_url = get_http_url(app_config->get_country_code()); std::string http_url = get_http_url(app_config->get_country_code());
this->preset_updater->sync(http_url, preset_bundle); std::string language = GUI::into_u8(current_language_code());
this->preset_updater->sync(http_url, language, preset_bundle);
//BBS: check new version //BBS: check new version
this->check_new_version(); this->check_new_version();
@ -1541,7 +1542,7 @@ void GUI_App::init_networking_callbacks()
} }
} }
} }
}); });
} }
); );
@ -2567,7 +2568,7 @@ void GUI_App::recreate_GUI(const wxString& msg_name)
// }); // });
m_is_recreating_gui = false; m_is_recreating_gui = false;
CallAfter([this]() { CallAfter([this]() {
mainframe->refresh_plugin_tips(); mainframe->refresh_plugin_tips();
}); });
@ -2952,7 +2953,7 @@ std::string GUI_App::handle_web_request(std::string cmd)
} }
else if (command_str.compare("begin_network_plugin_download") == 0) { else if (command_str.compare("begin_network_plugin_download") == 0) {
CallAfter([this] { wxGetApp().ShowDownNetPluginDlg(); }); CallAfter([this] { wxGetApp().ShowDownNetPluginDlg(); });
} }
else if (command_str.compare("get_web_shortcut") == 0) { else if (command_str.compare("get_web_shortcut") == 0) {
if (root.get_child_optional("key_event") != boost::none) { if (root.get_child_optional("key_event") != boost::none) {
pt::ptree key_event_node = root.get_child("key_event"); pt::ptree key_event_node = root.get_child("key_event");

View file

@ -227,7 +227,7 @@ struct PresetUpdater::priv
void parse_version_string(const std::string& body) const; void parse_version_string(const std::string& body) const;
void sync_resources(std::string http_url, std::map<std::string, Resource> &resources); void sync_resources(std::string http_url, std::map<std::string, Resource> &resources);
void sync_config(std::string http_url, const VendorMap vendors); void sync_config(std::string http_url, const VendorMap vendors);
void sync_tooltip(std::string http_url); void sync_tooltip(std::string http_url, std::string language);
//BBS: refine preset update logic //BBS: refine preset update logic
bool install_bundles_rsrc(std::vector<std::string> bundles, bool snapshot) const; bool install_bundles_rsrc(std::vector<std::string> bundles, bool snapshot) const;
@ -268,36 +268,38 @@ void PresetUpdater::priv::set_download_prefs(AppConfig *app_config)
// Downloads a file (http get operation). Cancels if the Updater is being destroyed. // 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 PresetUpdater::priv::get_file(const std::string &url, const fs::path &target_path) const
{ {
bool res = false; bool res = false;
fs::path tmp_path = target_path; fs::path tmp_path = target_path;
tmp_path += format(".%1%%2%", get_current_pid(), TMP_EXTENSION); tmp_path += format(".%1%%2%", get_current_pid(), TMP_EXTENSION);
BOOST_LOG_TRIVIAL(info) << format("[BBS Updater]download file `%1%`, stored to `%2%`, tmp path `%3%`", BOOST_LOG_TRIVIAL(info) << format("[BBS Updater]download file `%1%`, stored to `%2%`, tmp path `%3%`",
url, url,
target_path.string(), target_path.string(),
tmp_path.string()); tmp_path.string());
Slic3r::Http::get(url) Slic3r::Http::get(url)
.on_progress([](Slic3r::Http::Progress, bool &cancel) { .on_progress([this](Slic3r::Http::Progress, bool &cancel_http) {
if (cancel) { cancel = true; } if (cancel) {
}) cancel_http = true;
.on_error([&](std::string body, std::string error, unsigned http_status) { }
(void)body; })
BOOST_LOG_TRIVIAL(error) << format("[BBS Updater]getting: `%1%`: http status %2%, %3%", .on_error([&](std::string body, std::string error, unsigned http_status) {
url, (void)body;
http_status, BOOST_LOG_TRIVIAL(error) << format("[BBS Updater]getting: `%1%`: http status %2%, %3%",
error); url,
}) http_status,
.on_complete([&](std::string body, unsigned /* http_status */) { error);
fs::fstream file(tmp_path, std::ios::out | std::ios::binary | std::ios::trunc); })
file.write(body.c_str(), body.size()); .on_complete([&](std::string body, unsigned /* http_status */) {
file.close(); fs::fstream file(tmp_path, std::ios::out | std::ios::binary | std::ios::trunc);
fs::rename(tmp_path, target_path); file.write(body.c_str(), body.size());
res = true; file.close();
}) fs::rename(tmp_path, target_path);
.perform_sync(); res = true;
})
.perform_sync();
return res; return res;
} }
//BBS: refine preset update logic //BBS: refine preset update logic
@ -494,7 +496,12 @@ void PresetUpdater::priv::sync_resources(std::string http_url, std::map<std::str
std::string url = http_url; std::string url = http_url;
url += query_params; url += query_params;
Slic3r::Http http = Slic3r::Http::get(url); Slic3r::Http http = Slic3r::Http::get(url);
http.on_complete([this, &resource_list, resources](std::string body, unsigned) { http.on_progress([this](Slic3r::Http::Progress, bool &cancel_http) {
if (cancel) {
cancel_http = true;
}
})
.on_complete([this, &resource_list, resources](std::string body, unsigned) {
try { try {
BOOST_LOG_TRIVIAL(trace) << "[BBL Updater]: request_resources, body=" << body; BOOST_LOG_TRIVIAL(trace) << "[BBL Updater]: request_resources, body=" << body;
@ -634,7 +641,12 @@ void PresetUpdater::priv::sync_config(std::string http_url, const VendorMap vend
std::string url = http_url; std::string url = http_url;
url += query_params; url += query_params;
Slic3r::Http http = Slic3r::Http::get(url); Slic3r::Http http = Slic3r::Http::get(url);
http.on_complete( http.on_progress([this](Slic3r::Http::Progress, bool &cancel_http) {
if (cancel) {
cancel_http = true;
}
})
.on_complete(
[this, &vendor_list, &vendor_descriptions, vendors](std::string body, unsigned) { [this, &vendor_list, &vendor_descriptions, vendors](std::string body, unsigned) {
try { try {
BOOST_LOG_TRIVIAL(trace) << "[BBL Updater]::body=" << body; BOOST_LOG_TRIVIAL(trace) << "[BBL Updater]::body=" << body;
@ -778,10 +790,9 @@ void PresetUpdater::priv::sync_config(std::string http_url, const VendorMap vend
} }
} }
void PresetUpdater::priv::sync_tooltip(std::string http_url) void PresetUpdater::priv::sync_tooltip(std::string http_url, std::string language)
{ {
try { try {
std::string language = GUI::into_u8(GUI::wxGetApp().current_language_code());
std::string common_version = "00.00.00.00"; std::string common_version = "00.00.00.00";
std::string language_version = "00.00.00.00"; std::string language_version = "00.00.00.00";
fs::path cache_root = fs::path(data_dir()) / "resources/tooltip"; fs::path cache_root = fs::path(data_dir()) / "resources/tooltip";
@ -1054,7 +1065,7 @@ PresetUpdater::~PresetUpdater()
//BBS: change directories by design //BBS: change directories by design
//BBS: refine the preset updater logic //BBS: refine the preset updater logic
void PresetUpdater::sync(std::string http_url, PresetBundle *preset_bundle) void PresetUpdater::sync(std::string http_url, std::string language, PresetBundle *preset_bundle)
{ {
//p->set_download_prefs(GUI::wxGetApp().app_config); //p->set_download_prefs(GUI::wxGetApp().app_config);
if (!p->enabled_version_check && !p->enabled_config_update) { return; } if (!p->enabled_version_check && !p->enabled_config_update) { return; }
@ -1064,11 +1075,17 @@ void PresetUpdater::sync(std::string http_url, PresetBundle *preset_bundle)
// into the closure (but perhaps the compiler can elide this). // into the closure (but perhaps the compiler can elide this).
VendorMap vendors = preset_bundle->vendors; VendorMap vendors = preset_bundle->vendors;
p->thread = std::thread([this, vendors, http_url]() { p->thread = std::thread([this, vendors, http_url, language]() {
this->p->prune_tmps(); this->p->prune_tmps();
if (p->cancel)
return;
this->p->sync_version(); this->p->sync_version();
if (p->cancel)
return;
this->p->sync_config(http_url, std::move(vendors)); this->p->sync_config(http_url, std::move(vendors));
this->p->sync_tooltip(http_url); if (p->cancel)
return;
this->p->sync_tooltip(http_url, language);
}); });
} }

View file

@ -26,7 +26,7 @@ public:
~PresetUpdater(); ~PresetUpdater();
// If either version check or config updating is enabled, get the appropriate data in the background and cache it. // If either version check or config updating is enabled, get the appropriate data in the background and cache it.
void sync(std::string http_url, PresetBundle *preset_bundle); void sync(std::string http_url, std::string language, PresetBundle *preset_bundle);
// If version check is enabled, check if chaced online slic3r version is newer, notify if so. // If version check is enabled, check if chaced online slic3r version is newer, notify if so.
void slic3r_update_notify(); void slic3r_update_notify();