Fix Crash when using m_ProfileJson (#79)

* Recover: Packaging Process

* Update: Flutter 1222

* Fix: crash when recreate GUI

* Fix Crash when using m_profile_json
This commit is contained in:
xiaoyeliu
2025-12-22 16:24:48 +08:00
committed by GitHub
parent f6899b277f
commit f729c53b20
4 changed files with 161 additions and 91 deletions

View File

@@ -698,7 +698,7 @@ std::string AppConfig::load()
auto it_app = m_storage.find("app");
if (it_app != m_storage.end()) {
auto it_region = it_app->second.find("region");
if (it_region != it_app->second.end() && it_region->second == "China") {
if (it_region != it_app->second.end() && (it_region->second == "China" || it_region->second == "")) {
it_region->second = "Chinese Mainland";
m_dirty = true;
}

View File

@@ -28,6 +28,7 @@
#include "MoonRaker.hpp"
#include "slic3r/GUI/WebPresetDialog.hpp"
#include <mutex>
#include "slic3r/GUI/SMPhysicalPrinterDialog.hpp"
#include "slic3r/GUI/WebUrlDialog.hpp"
@@ -184,6 +185,7 @@ WCP_Logger::~WCP_Logger()
}
extern json m_ProfileJson;
extern std::mutex m_ProfileJson_mutex;
std::vector<std::string> load_thumbnails(const std::string& file, size_t image_count)
{
@@ -5236,30 +5238,33 @@ void SSWCP_MqttAgent_Instance::sw_mqtt_set_engine()
m_dialog->m_device_id = ip;
// 检查是否该预设已经选入系统
int nModel = m_ProfileJson["model"].size();
bool isFind = false;
for (int m = 0; m < nModel; m++) {
if (m_ProfileJson["model"][m]["model"].get<std::string>() == info.model_name) {
// 绑定的预设已被选入系统
isFind = true;
std::string nozzle_selected = m_ProfileJson["model"][m]["nozzle_selected"]
.get<std::string>();
std::string se_nozz_selected = nozzle_diameters[0];
if (nozzle_selected.find(se_nozz_selected) == std::string::npos) {
nozzle_selected += ";" + se_nozz_selected;
m_ProfileJson["model"][m]["nozzle_selected"] = nozzle_selected;
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
int nModel = m_ProfileJson["model"].size();
bool isFind = false;
for (int m = 0; m < nModel; m++) {
if (m_ProfileJson["model"][m]["model"].get<std::string>() == info.model_name) {
// 绑定的预设已被选入系统
isFind = true;
std::string nozzle_selected = m_ProfileJson["model"][m]["nozzle_selected"]
.get<std::string>();
std::string se_nozz_selected = nozzle_diameters[0];
if (nozzle_selected.find(se_nozz_selected) == std::string::npos) {
nozzle_selected += ";" + se_nozz_selected;
m_ProfileJson["model"][m]["nozzle_selected"] = nozzle_selected;
}
break;
}
break;
}
}
if (!isFind) {
json new_item;
new_item["vendor"] = "Snapmaker";
new_item["model"] = info.model_name;
new_item["nozzle_selected"] = nozzle_diameters[0];
m_ProfileJson["model"].push_back(new_item);
if (!isFind) {
json new_item;
new_item["vendor"] = "Snapmaker";
new_item["model"] = info.model_name;
new_item["nozzle_selected"] = nozzle_diameters[0];
m_ProfileJson["model"].push_back(new_item);
}
}
wxGetApp().mainframe->plater()->sidebar().update_all_preset_comboboxes(false);

View File

@@ -33,12 +33,14 @@
#include <libslic3r/miniz_extension.hpp>
#include <libslic3r/Utils.hpp>
#include "CreatePresetsDialog.hpp"
#include <mutex>
using namespace nlohmann;
namespace Slic3r { namespace GUI {
json m_ProfileJson;
std::mutex m_ProfileJson_mutex;
static wxString update_custom_filaments()
{
@@ -229,7 +231,11 @@ wxString GuideFrame::SetStartPage(GuidePage startpage, bool load)
} else if (startpage == BBL_FILAMENTS) {
SetTitle(_L("Setup Wizard"));
int nSize = m_ProfileJson["model"].size();
int nSize;
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
nSize = m_ProfileJson["model"].size();
}
if (nSize>0)
TargetUrl = from_u8((boost::filesystem::path(resources_dir()) / "web/guide/0/index.html?target=22").make_preferred().string());
@@ -403,7 +409,10 @@ void GuideFrame::OnScriptMessage(wxWebViewEvent &evt)
json m_Res = json::object();
m_Res["command"] = "response_userguide_profile";
m_Res["sequence_id"] = "10001";
m_Res["response"] = m_ProfileJson;
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
m_Res["response"] = m_ProfileJson;
}
//wxString strJS = wxString::Format("HandleStudio(%s)", m_Res.dump(-1, ' ', false, json::error_handler_t::ignore));
wxString strJS = wxString::Format("HandleStudio(%s)", m_Res.dump(-1, ' ', true));
@@ -426,43 +435,53 @@ void GuideFrame::OnScriptMessage(wxWebViewEvent &evt)
{
json MSelected = j["data"];
int nModel = m_ProfileJson["model"].size();
for (int m = 0; m < nModel; m++) {
json TmpModel = m_ProfileJson["model"][m];
m_ProfileJson["model"][m]["nozzle_selected"] = "";
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
int nModel = m_ProfileJson["model"].size();
for (int m = 0; m < nModel; m++) {
json TmpModel = m_ProfileJson["model"][m];
m_ProfileJson["model"][m]["nozzle_selected"] = "";
for (auto it = MSelected.begin(); it != MSelected.end(); ++it) {
json OneSelect = it.value();
for (auto it = MSelected.begin(); it != MSelected.end(); ++it) {
json OneSelect = it.value();
wxString s1 = TmpModel["model"];
wxString s2 = OneSelect["model"];
if (s1.compare(s2) == 0) {
m_ProfileJson["model"][m]["nozzle_selected"] = OneSelect["nozzle_diameter"];
break;
wxString s1 = TmpModel["model"];
wxString s2 = OneSelect["model"];
if (s1.compare(s2) == 0) {
m_ProfileJson["model"][m]["nozzle_selected"] = OneSelect["nozzle_diameter"];
break;
}
}
}
}
}
else if (strCmd == "save_userguide_filaments") {
//reset
for (auto it = m_ProfileJson["filament"].begin(); it != m_ProfileJson["filament"].end(); ++it)
{
m_ProfileJson["filament"][it.key()]["selected"] = 0;
}
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
//reset
for (auto it = m_ProfileJson["filament"].begin(); it != m_ProfileJson["filament"].end(); ++it)
{
m_ProfileJson["filament"][it.key()]["selected"] = 0;
}
json fSelected = j["data"]["filament"];
int nF = fSelected.size();
for (int m = 0; m < nF; m++)
{
std::string fName = fSelected[m];
json fSelected = j["data"]["filament"];
int nF = fSelected.size();
for (int m = 0; m < nF; m++)
{
std::string fName = fSelected[m];
m_ProfileJson["filament"][fName]["selected"] = 1;
m_ProfileJson["filament"][fName]["selected"] = 1;
}
}
}
else if (strCmd == "user_guide_finish") {
SaveProfile();
std::string oldregion = m_ProfileJson["region"];
std::string oldregion;
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
oldregion = m_ProfileJson["region"];
}
bool bLogin = false;
if (m_Region != oldregion) {
AppConfig* config = GUI::wxGetApp().app_config;
@@ -492,7 +511,10 @@ void GuideFrame::OnScriptMessage(wxWebViewEvent &evt)
this->Close();
} else if (strCmd == "save_region") {
m_Region = j["region"];
m_ProfileJson["region"] = m_Region;
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
m_ProfileJson["region"] = m_Region;
}
}
else if (strCmd == "common_openurl") {
@@ -635,7 +657,11 @@ int GuideFrame::SaveProfile()
m_MainPtr->app_config->save();
std::string strAll = m_ProfileJson.dump(-1, ' ', false, json::error_handler_t::ignore);
std::string strAll;
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
strAll = m_ProfileJson.dump(-1, ' ', false, json::error_handler_t::ignore);
}
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << "before save to app_config: "<< std::endl<<strAll;
@@ -643,9 +669,12 @@ int GuideFrame::SaveProfile()
const std::string &section_name = AppConfig::SECTION_FILAMENTS;
std::map<std::string, std::string> section_new;
m_appconfig_new.clear_section(section_name);
for (auto it = m_ProfileJson["filament"].begin(); it != m_ProfileJson["filament"].end(); ++it) {
if (it.value()["selected"] == 1){
section_new[it.key()] = "true";
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
for (auto it = m_ProfileJson["filament"].begin(); it != m_ProfileJson["filament"].end(); ++it) {
if (it.value()["selected"] == 1){
section_new[it.key()] = "true";
}
}
}
m_appconfig_new.set_section(section_name, section_new);
@@ -653,7 +682,9 @@ int GuideFrame::SaveProfile()
//set vendors to app_config
Slic3r::AppConfig::VendorMap empty_vendor_map;
m_appconfig_new.set_vendors(empty_vendor_map);
for (auto it = m_ProfileJson["model"].begin(); it != m_ProfileJson["model"].end(); ++it)
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
for (auto it = m_ProfileJson["model"].begin(); it != m_ProfileJson["model"].end(); ++it)
{
if (it.value().is_object()) {
json temp_model = it.value();
@@ -997,6 +1028,7 @@ int GuideFrame::GetFilamentInfo( std::string VendorDirectory, json & pFilaList,
int GuideFrame::LoadProfileData()
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
try {
m_ProfileJson = json::parse("{}");
m_ProfileJson["model"] = json::array();
@@ -1068,7 +1100,7 @@ int GuideFrame::LoadProfileData()
}
//sync to web
std::string strAll = m_ProfileJson.dump(-1, ' ', false, json::error_handler_t::ignore);
std::string strAll = m_ProfileJson.dump(-1, ' ', false, json::error_handler_t::ignore); // Already locked at function start
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ", finished, json contents: " << std::endl << strAll;
json m_Res = json::object();
@@ -1093,6 +1125,7 @@ int GuideFrame::LoadProfileData()
int GuideFrame::SaveProfileData()
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
try {
const auto enabled_filaments = wxGetApp().app_config->has_section(AppConfig::SECTION_FILAMENTS) ? wxGetApp().app_config->get_section(AppConfig::SECTION_FILAMENTS) : std::map<std::string, std::string>();
m_appconfig_new.set_vendors(*wxGetApp().app_config);

View File

@@ -30,12 +30,14 @@
#include <libslic3r/Utils.hpp>
#include "CreatePresetsDialog.hpp"
#include "slic3r/GUI/Tab.hpp"
#include <mutex>
using namespace nlohmann;
namespace Slic3r { namespace GUI {
extern json m_ProfileJson;
extern std::mutex m_ProfileJson_mutex;
extern void StringReplace(string& strBase, string strSrc, string strDes);
static wxString update_custom_filaments()
@@ -112,7 +114,7 @@ static wxString update_custom_filaments()
}
WebPresetDialog::WebPresetDialog(GUI_App* pGUI, long style)
: DPIDialog((wxWindow*) (pGUI->mainframe), wxID_ANY, "Snapmaker Orca", wxDefaultPosition, wxDefaultSize, style), m_appconfig_new()
: DPIDialog((wxWindow*) (nullptr), wxID_ANY, "Snapmaker Orca", wxDefaultPosition, wxDefaultSize, style), m_appconfig_new()
{
SetBackgroundColour(*wxWHITE);
// INI
@@ -424,7 +426,11 @@ void WebPresetDialog::OnScriptMessage(wxWebViewEvent& evt)
m_Res["command"] = "response_userguide_profile";
m_Res["sequence_id"] = "10001";
json res_json = m_ProfileJson;
json res_json;
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
res_json = m_ProfileJson;
}
// 把所有的选中信息取消,换成当前连接的机器
std::string model_name = "";
@@ -491,27 +497,30 @@ void WebPresetDialog::OnScriptMessage(wxWebViewEvent& evt)
} else if (strCmd == "save_userguide_models") {
json MSelected = j["data"];
int nModel = m_ProfileJson["model"].size();
bool isFind = false;
for (int m = 0; m < nModel; m++) {
if (m_ProfileJson["model"][m]["model"].get<std::string>() == MSelected.begin().value()["model"].get<std::string>()) {
// 绑定的预设已被选入系统
isFind = true;
std::string nozzle_selected = m_ProfileJson["model"][m]["nozzle_selected"].get<std::string>();
std::string se_nozz_selected = MSelected.begin().value()["nozzle_diameter"].get<std::string>();
if (nozzle_selected.find(se_nozz_selected) == std::string::npos) {
nozzle_selected += ";" + se_nozz_selected;
m_ProfileJson["model"][m]["nozzle_selected"] = nozzle_selected;
}
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
int nModel = m_ProfileJson["model"].size();
bool isFind = false;
for (int m = 0; m < nModel; m++) {
if (m_ProfileJson["model"][m]["model"].get<std::string>() == MSelected.begin().value()["model"].get<std::string>()) {
// 绑定的预设已被选入系统
isFind = true;
std::string nozzle_selected = m_ProfileJson["model"][m]["nozzle_selected"].get<std::string>();
std::string se_nozz_selected = MSelected.begin().value()["nozzle_diameter"].get<std::string>();
if (nozzle_selected.find(se_nozz_selected) == std::string::npos) {
nozzle_selected += ";" + se_nozz_selected;
m_ProfileJson["model"][m]["nozzle_selected"] = nozzle_selected;
}
break;
break;
}
}
if (!isFind) {
json new_item;
new_item["model"] = MSelected.begin().value()["model"];
new_item["nozzle_selected"] = MSelected.begin().value()["nozzle_diameter"];
m_ProfileJson["model"].push_back(new_item);
}
}
if (!isFind) {
json new_item;
new_item["model"] = MSelected.begin().value()["model"];
new_item["nozzle_selected"] = MSelected.begin().value()["nozzle_diameter"];
m_ProfileJson["model"].push_back(new_item);
}
DeviceInfo info;
@@ -548,22 +557,29 @@ void WebPresetDialog::OnScriptMessage(wxWebViewEvent& evt)
}
} else if (strCmd == "save_userguide_filaments") {
// reset
for (auto it = m_ProfileJson["filament"].begin(); it != m_ProfileJson["filament"].end(); ++it) {
m_ProfileJson["filament"][it.key()]["selected"] = 0;
}
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
// reset
for (auto it = m_ProfileJson["filament"].begin(); it != m_ProfileJson["filament"].end(); ++it) {
m_ProfileJson["filament"][it.key()]["selected"] = 0;
}
json fSelected = j["data"]["filament"];
int nF = fSelected.size();
for (int m = 0; m < nF; m++) {
std::string fName = fSelected[m];
json fSelected = j["data"]["filament"];
int nF = fSelected.size();
for (int m = 0; m < nF; m++) {
std::string fName = fSelected[m];
m_ProfileJson["filament"][fName]["selected"] = 1;
m_ProfileJson["filament"][fName]["selected"] = 1;
}
}
} else if (strCmd == "user_guide_finish") {
SaveProfile();
std::string oldregion = m_ProfileJson["region"];
std::string oldregion;
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
oldregion = m_ProfileJson["region"];
}
bool bLogin = false;
if (m_Region != oldregion) {
AppConfig* config = GUI::wxGetApp().app_config;
@@ -590,7 +606,10 @@ void WebPresetDialog::OnScriptMessage(wxWebViewEvent& evt)
this->Close();
} else if (strCmd == "save_region") {
m_Region = j["region"];
m_ProfileJson["region"] = m_Region;
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
m_ProfileJson["region"] = m_Region;
}
}
else if (strCmd == "common_openurl") {
@@ -622,7 +641,10 @@ void WebPresetDialog::OnScriptMessage(wxWebViewEvent& evt)
BOOST_LOG_TRIVIAL(trace) << "WebPresetDialog::OnScriptMessage;Error:" << e.what();
}
wxString strAll = m_ProfileJson.dump(-1, ' ', false, json::error_handler_t::ignore);
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
wxString strAll = m_ProfileJson.dump(-1, ' ', false, json::error_handler_t::ignore);
}
}
void WebPresetDialog::RunScript(const wxString& javascript)
@@ -754,7 +776,11 @@ int WebPresetDialog::SaveProfile()
m_MainPtr->app_config->save();
std::string strAll = m_ProfileJson.dump(-1, ' ', false, json::error_handler_t::ignore);
std::string strAll;
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
strAll = m_ProfileJson.dump(-1, ' ', false, json::error_handler_t::ignore);
}
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << "before save to app_config: " << std::endl << strAll;
@@ -762,9 +788,12 @@ int WebPresetDialog::SaveProfile()
const std::string& section_name = AppConfig::SECTION_FILAMENTS;
std::map<std::string, std::string> section_new;
m_appconfig_new.clear_section(section_name);
for (auto it = m_ProfileJson["filament"].begin(); it != m_ProfileJson["filament"].end(); ++it) {
if (it.value()["selected"] == 1) {
section_new[it.key()] = "true";
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
for (auto it = m_ProfileJson["filament"].begin(); it != m_ProfileJson["filament"].end(); ++it) {
if (it.value()["selected"] == 1) {
section_new[it.key()] = "true";
}
}
}
m_appconfig_new.set_section(section_name, section_new);
@@ -772,7 +801,9 @@ int WebPresetDialog::SaveProfile()
// set vendors to app_config
Slic3r::AppConfig::VendorMap empty_vendor_map;
m_appconfig_new.set_vendors(empty_vendor_map);
for (auto it = m_ProfileJson["model"].begin(); it != m_ProfileJson["model"].end(); ++it) {
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
for (auto it = m_ProfileJson["model"].begin(); it != m_ProfileJson["model"].end(); ++it) {
if (it.value().is_object()) {
json temp_model = it.value();
std::string model_name = temp_model["model"];
@@ -1121,6 +1152,7 @@ int WebPresetDialog::GetFilamentInfo(std::string VendorDirectory, json& pFilaLis
int WebPresetDialog::LoadProfile()
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
try {
// wxString ExePath = boost::dll::program_location().parent_path().string();
// wxString TargetFolder = ExePath + "\\resources\\profiles\\";