FIX: reset bambu lib after restart network plugin

Change-Id: I4a3a4b7420745835ca3fa00c6edebe9d8d98cbf6
Jira: STUDIO-7571
This commit is contained in:
chunmao.guo 2024-07-09 16:36:19 +08:00 committed by Lane.Wei
parent fd4f28c6c0
commit 28d9c6743f
3 changed files with 38 additions and 18 deletions

View File

@ -144,6 +144,11 @@
using namespace std::literals; using namespace std::literals;
namespace pt = boost::property_tree; namespace pt = boost::property_tree;
struct StaticBambuLib
{
static void reset();
};
namespace Slic3r { namespace Slic3r {
namespace GUI { namespace GUI {
@ -1834,6 +1839,7 @@ void GUI_App::restart_networking()
{ {
BOOST_LOG_TRIVIAL(info) << __FUNCTION__<< boost::format(" enter, mainframe %1%")%mainframe; BOOST_LOG_TRIVIAL(info) << __FUNCTION__<< boost::format(" enter, mainframe %1%")%mainframe;
on_init_network(true); on_init_network(true);
StaticBambuLib::reset();
if(m_agent) { if(m_agent) {
init_networking_callbacks(); init_networking_callbacks();
m_agent->set_on_ssdp_msg_fn( m_agent->set_on_ssdp_msg_fn(
@ -4019,9 +4025,9 @@ std::string GUI_App::handle_web_request(std::string cmd)
boost::optional<std::string> ModelID = root.get_optional<std::string>("makerworld_model_id"); boost::optional<std::string> ModelID = root.get_optional<std::string>("makerworld_model_id");
if (ModelID.has_value()) { if (ModelID.has_value()) {
if (mainframe) { if (mainframe) {
if (mainframe->m_webview) if (mainframe->m_webview)
{ {
mainframe->m_webview->SetMakerworldModelID(ModelID.value()); mainframe->m_webview->SetMakerworldModelID(ModelID.value());
} }
} }
} }

View File

@ -26,9 +26,9 @@
std::string last_system_error() { std::string last_system_error() {
return Slic3r::decode_path(std::error_code( return Slic3r::decode_path(std::error_code(
#ifdef _WIN32 #ifdef _WIN32
GetLastError(), GetLastError(),
#else #else
errno, errno,
#endif #endif
std::system_category()).message().c_str()); std::system_category()).message().c_str());
} }
@ -54,12 +54,15 @@ static std::map<int, std::string> error_messages = {
}; };
struct StaticBambuLib : BambuLib { struct StaticBambuLib : BambuLib {
static StaticBambuLib & get(); static StaticBambuLib &get(BambuLib * copy = nullptr);
static int Fake_Bambu_Create(Bambu_Tunnel*, char const*) { return -2; } static int Fake_Bambu_Create(Bambu_Tunnel*, char const*) { return -2; }
static void reset();
private:
std::vector<BambuLib *> copies_;
}; };
PrinterFileSystem::PrinterFileSystem() PrinterFileSystem::PrinterFileSystem()
: BambuLib(StaticBambuLib::get()) : BambuLib(StaticBambuLib::get(this))
{ {
if (!default_thumbnail.IsOk()) { if (!default_thumbnail.IsOk()) {
default_thumbnail = *Slic3r::GUI::BitmapCache().load_svg("printer_file", 0, 0); default_thumbnail = *Slic3r::GUI::BitmapCache().load_svg("printer_file", 0, 0);
@ -360,7 +363,7 @@ void PrinterFileSystem::FetchModel(size_t index, std::function<void(int, std::st
m_task_flags &= ~FF_FETCH_MODEL; m_task_flags &= ~FF_FETCH_MODEL;
if (result != 0) { if (result != 0) {
auto iter = error_messages.find(result); auto iter = error_messages.find(result);
if (iter != error_messages.end()) if (iter != error_messages.end())
*file_data = _u8L(iter->second.c_str()); *file_data = _u8L(iter->second.c_str());
else else
file_data->clear(); file_data->clear();
@ -754,7 +757,7 @@ void PrinterFileSystem::UpdateFocusThumbnail()
if (names.empty() && paths.empty()) if (names.empty() && paths.empty())
return; return;
m_task_flags |= FF_THUMNAIL; m_task_flags |= FF_THUMNAIL;
UpdateFocusThumbnail2(std::make_shared<std::vector<File>>(paths.empty() ? names : paths), UpdateFocusThumbnail2(std::make_shared<std::vector<File>>(paths.empty() ? names : paths),
paths.empty() ? OldThumbnail : m_file_type == F_MODEL ? ModelMetadata : VideoThumbnail); paths.empty() ? OldThumbnail : m_file_type == F_MODEL ? ModelMetadata : VideoThumbnail);
} }
@ -838,7 +841,7 @@ void PrinterFileSystem::UpdateFocusThumbnail2(std::shared_ptr<std::vector<File>>
if (!thumbnail.empty()) { if (!thumbnail.empty()) {
arr.push_back(file.path + "#" + thumbnail); arr.push_back(file.path + "#" + thumbnail);
file.flags &= ~FF_THUMNAIL; file.flags &= ~FF_THUMNAIL;
file.local_path.clear(); file.local_path.clear();
} }
} }
} }
@ -944,7 +947,7 @@ std::pair<PrinterFileSystem::FileList &, size_t> PrinterFileSystem::FindFile(std
m_file_list : m_file_list :
m_file_list_cache[type]; m_file_list_cache[type];
if (index >= file_list.size() || (by_path ? file_list[index].path : file_list[index].name) != name) { if (index >= file_list.size() || (by_path ? file_list[index].path : file_list[index].name) != name) {
auto iter = std::find_if(m_file_list.begin(), file_list.end(), auto iter = std::find_if(m_file_list.begin(), file_list.end(),
[name, by_path](File &f) { return (by_path ? f.path : f.name) == name; }); [name, by_path](File &f) { return (by_path ? f.path : f.name) == name; });
if (iter == m_file_list.end()) return {file_list, -1}; if (iter == m_file_list.end()) return {file_list, -1};
index = std::distance(m_file_list.begin(), iter); index = std::distance(m_file_list.begin(), iter);
@ -1013,7 +1016,7 @@ void PrinterFileSystem::SendChangedEvent(wxEventType type, size_t index, std::st
event.SetInt(index); event.SetInt(index);
if (!str.empty()) if (!str.empty())
event.SetString(wxString::FromUTF8(str.c_str())); event.SetString(wxString::FromUTF8(str.c_str()));
else if (auto iter = error_messages.find(extra); iter != error_messages.end()) else if (auto iter = error_messages.find(extra); iter != error_messages.end())
event.SetString(_L(iter->second.c_str())); event.SetString(_L(iter->second.c_str()));
else if (extra > CONTINUE && extra != ERROR_CANCEL) else if (extra > CONTINUE && extra != ERROR_CANCEL)
event.SetString(wxString::Format(_L("Error code: %d"), int(extra))); event.SetString(wxString::Format(_L("Error code: %d"), int(extra)));
@ -1276,7 +1279,7 @@ void PrinterFileSystem::Reconnect(boost::unique_lock<boost::mutex> &l, int resul
} }
if (ret == 0) if (ret == 0)
do { do {
ret = Bambu_StartStreamEx ret = Bambu_StartStreamEx
? Bambu_StartStreamEx(tunnel, CTRL_TYPE) ? Bambu_StartStreamEx(tunnel, CTRL_TYPE)
: Bambu_StartStream(tunnel, false); : Bambu_StartStream(tunnel, false);
} while (ret == Bambu_would_block); } while (ret == Bambu_would_block);
@ -1344,12 +1347,12 @@ static void* get_function(const char* name)
#define GET_FUNC(x) lib.x = reinterpret_cast<decltype(lib.x)>(get_function(#x)) #define GET_FUNC(x) lib.x = reinterpret_cast<decltype(lib.x)>(get_function(#x))
StaticBambuLib &StaticBambuLib::get() StaticBambuLib &StaticBambuLib::get(BambuLib *copy)
{ {
static StaticBambuLib lib; static StaticBambuLib lib;
// first load the library // first load the library
if (lib.Bambu_Open) if (lib.Bambu_Create)
return lib; return lib;
if (!module) { if (!module) {
@ -1373,11 +1376,22 @@ StaticBambuLib &StaticBambuLib::get()
GET_FUNC(Bambu_SetLogger); GET_FUNC(Bambu_SetLogger);
GET_FUNC(Bambu_FreeLogMsg); GET_FUNC(Bambu_FreeLogMsg);
if (!lib.Bambu_Open) if (!lib.Bambu_Create) {
lib.Bambu_Create = Fake_Bambu_Create; lib.Bambu_Create = Fake_Bambu_Create;
if (copy)
lib.copies_.push_back(copy);
}
return lib; return lib;
} }
void StaticBambuLib::reset()
{
get().Bambu_Create = nullptr;
auto &lib = get();
for (auto c : lib.copies_)
*c = lib;
}
extern "C" BambuLib *bambulib_get() { extern "C" BambuLib *bambulib_get() {
return &StaticBambuLib::get(); return &StaticBambuLib::get();
} }

View File

@ -19,12 +19,12 @@ END_EVENT_TABLE()
struct StaticBambuLib : BambuLib struct StaticBambuLib : BambuLib
{ {
static StaticBambuLib &get(); static StaticBambuLib &get(BambuLib *);
}; };
wxMediaCtrl3::wxMediaCtrl3(wxWindow *parent) wxMediaCtrl3::wxMediaCtrl3(wxWindow *parent)
: wxWindow(parent, wxID_ANY) : wxWindow(parent, wxID_ANY)
, BambuLib(StaticBambuLib::get()) , BambuLib(StaticBambuLib::get(this))
, m_thread([this] { PlayThread(); }) , m_thread([this] { PlayThread(); })
{ {
SetBackgroundColour(*wxBLACK); SetBackgroundColour(*wxBLACK);