feat: Python Plugins

This commit is contained in:
Ian Chua
2026-07-02 17:49:36 +08:00
parent 395e070a0e
commit ecddf3d18f
183 changed files with 49955 additions and 2120 deletions

View File

@@ -1,6 +1,7 @@
#include "ExportPresetBundleDialog.hpp"
#include "OrcaCloudServiceAgent.hpp"
#include "libslic3r/Technologies.hpp"
#include "libslic3r/Platform.hpp"
#include "GUI_App.hpp"
#include "GUI_Init.hpp"
#include "GUI_ObjectList.hpp"
@@ -13,6 +14,7 @@
#include <boost/log/detail/native_typeof.hpp>
#include <libslic3r/Config.hpp>
#include <mutex>
#include <slic3r/plugin/PythonPluginInterface.hpp>
#include <wx/event.h>
// Localization headers: include libslic3r version first so everything in this file
@@ -76,6 +78,9 @@
#include "libslic3r/miniz_extension.hpp"
#include "libslic3r/Utils.hpp"
#include "libslic3r/Color.hpp"
#include "slic3r/plugin/PluginManager.hpp"
#include "slic3r/plugin/PluginHostUi.hpp"
#include "slic3r/plugin/PythonInterpreter.hpp"
#include "GUI.hpp"
#include "GUI_Utils.hpp"
@@ -135,6 +140,9 @@
#include "slic3r/Utils/BBLNetworkPlugin.hpp"
#include "slic3r/Utils/bambu_networking.hpp"
#include "PluginsDialog.hpp"
#include "TerminalDialog.hpp"
//#ifdef WIN32
//#include "BaseException.h"
//#endif
@@ -1113,6 +1121,12 @@ void GUI_App::shutdown()
if (m_is_recreating_gui) return;
stop_http_server();
set_closing(true);
Slic3r::PluginManager::instance().get_loader().set_shutting_down();
if (m_agent)
m_agent->set_printer_agent(nullptr);
NetworkAgentFactory::clear_printer_agent_cache();
BOOST_LOG_TRIVIAL(info) << "GUI_App::shutdown exit";
}
@@ -2217,6 +2231,14 @@ void GUI_App::init_networking_callbacks()
GUI_App::~GUI_App()
{
BOOST_LOG_TRIVIAL(info) << __FUNCTION__<< boost::format(": enter");
if (m_agent)
m_agent->set_printer_agent(nullptr);
NetworkAgentFactory::clear_printer_agent_cache();
Slic3r::PluginManager::instance().shutdown();
Slic3r::PythonInterpreter::instance().shutdown();
if (app_config != nullptr) {
BOOST_LOG_TRIVIAL(info) << __FUNCTION__<< boost::format(": destroy app_config");
delete app_config;
@@ -3045,13 +3067,119 @@ bool GUI_App::on_init_inner()
wxMessageBox("Force using legacy bambu networking plugin because debugger is attached! If the app terminates itself immediately, please delete installed plugin and try again!");
}
} */
copy_network_if_available();
if (scrn) {
scrn->SetText(_L("Loading Plugins") + dots, 20);
wxYield();
}
on_init_network();
// Initialize plugins after network then register on_load callbacks so once the plugin loads finish, it gets registered automatically.
PluginManager& plugin_mgr = PluginManager::instance();
plugin_mgr.initialize();
ConfigBase::set_resolve_capability_fn([&plugin_mgr](const std::string& cap_name, const std::string& cap_type) {
auto plugin_cap = plugin_mgr.get_loader().try_get_plugin_capability_by_name_and_type(cap_name, plugin_capability_type_from_string(cap_type));
if (!plugin_cap)
return std::string();
PluginDescriptor descriptor;
if (!plugin_mgr.get_catalog().try_get_plugin_descriptor(plugin_cap->plugin_key, descriptor))
return std::string();
// Cloud plugins are resolved at runtime via the UUID in the middle field, so the first
// field keeps the friendly display name. Local plugins are looked up by plugin_key (the
// first field, with an empty UUID), so emit the plugin_key to keep them resolvable.
const std::string identity = descriptor.is_cloud_plugin() ? descriptor.name : descriptor.plugin_key;
return identity + ';' + descriptor.cloud_uuid() + ';' + cap_name;
});
// Set cloud plugin directory from previous session so cloud-installed
// plugins are discovered even before the network agent is ready.
const std::string preset_folder = app_config->get("preset_folder");
if (!preset_folder.empty()) {
plugin_mgr.get_catalog().set_cloud_plugin_dir(preset_folder);
plugin_mgr.get_loader().set_cloud_user_id(preset_folder);
}
plugin_mgr.discover_plugins(false, true);
auto refresh_plugins_dialog = [] {
if (!wxTheApp)
return;
GUI_App* app = &GUI::wxGetApp();
if (app->is_closing())
return;
app->CallAfter([app] {
if (!app->is_closing() && app->m_plugins_dlg)
app->m_plugins_dlg->update_plugin_dialog_ui();
});
};
plugin_mgr.get_loader().subscribe_on_load_callback([refresh_plugins_dialog](const std::string&) { refresh_plugins_dialog(); });
plugin_mgr.get_loader().subscribe_on_unload_callback([refresh_plugins_dialog](const std::string&) { refresh_plugins_dialog(); });
plugin_mgr.get_loader().subscribe_on_load_callback(NetworkAgentFactory::register_python_plugin);
plugin_mgr.get_loader().subscribe_on_unload_callback(NetworkAgentFactory::deregister_python_plugin);
plugin_mgr.get_loader().subscribe_on_capability_load_callback(
[refresh_plugins_dialog](const PluginCapabilityIdentifier& capability) {
if (capability.type == PluginCapabilityType::PrinterConnection)
NetworkAgentFactory::register_python_printer_agent(capability.plugin_key, capability.name);
refresh_plugins_dialog();
// A newly loaded capability may satisfy a missing-plugin notification; re-validate the
// current plate (on the UI thread) so the notification clears once its plugin is available.
if (wxTheApp && !wxGetApp().is_closing())
wxGetApp().CallAfter([]() {
if (Plater* plater = wxGetApp().plater())
plater->revalidate_current_plate_if_plugins_missing();
});
});
plugin_mgr.get_loader().subscribe_on_capability_unload_callback(
[refresh_plugins_dialog](const PluginCapabilityIdentifier& capability) {
if (capability.type == PluginCapabilityType::PrinterConnection)
NetworkAgentFactory::deregister_python_printer_agent(capability.plugin_key, capability.name);
refresh_plugins_dialog();
});
for (const std::string& plugin_key : plugin_mgr.get_catalog().get_enabled_plugin_keys()) {
if (!plugin_mgr.get_loader().is_plugin_loaded(plugin_key)) {
plugin_mgr.get_loader().load_plugin(plugin_mgr.get_catalog(), plugin_key, false);
BOOST_LOG_TRIVIAL(info) << "Auto-loading plugin on startup: " << plugin_key;
}
}
if (m_agent)
plugin_mgr.set_cloud_agent(std::dynamic_pointer_cast<OrcaCloudServiceAgent>(m_agent->get_cloud_agent()));
if (m_agent && m_agent->is_user_login()) {
enable_user_preset_folder(true);
plugin_mgr.get_catalog().set_cloud_plugin_dir(m_agent->get_user_id());
plugin_mgr.get_loader().set_cloud_user_id(m_agent->get_user_id());
// If there is a user logged in we do an immediate sync.
std::vector<std::string> not_found, unauthorized;
plugin_mgr.fetch_plugins_from_cloud(&not_found, &unauthorized);
if (plater()) {
for (const auto& uuid : not_found) {
plater()->get_notification_manager()->push_notification(
NotificationType::CustomNotification,
NotificationManager::NotificationLevel::RegularNotificationLevel,
format(_L("Plugin %s is no longer available."), uuid));
}
for (const auto& uuid : unauthorized) {
plater()->get_notification_manager()->push_notification(
NotificationType::CustomNotification,
NotificationManager::NotificationLevel::RegularNotificationLevel,
format(_L("Plugin %s access is unauthorized."), uuid));
}
}
} else {
enable_user_preset_folder(false);
plugin_mgr.get_catalog().set_cloud_plugin_dir("");
plugin_mgr.get_loader().set_cloud_user_id("");
}
// BBS if load user preset failed
@@ -3543,52 +3671,54 @@ void GUI_App::switch_printer_agent()
// Read printer_agent from config, falling back to default
std::string effective_agent_id = ORCA_PRINTER_AGENT_ID;
std::string cloud_agent_id = ORCA_CLOUD_PROVIDER;
if (preset_bundle->is_bbl_vendor()) {
if (preset_bundle->is_bbl_vendor())
effective_agent_id = BBL_PRINTER_AGENT_ID;
cloud_agent_id = BBL_CLOUD_PROVIDER;
} else {
const DynamicPrintConfig& config = preset_bundle->printers.get_edited_preset().config;
if (config.has("printer_agent")) {
const std::string& value = config.option<ConfigOptionString>("printer_agent")->value;
if (!value.empty())
effective_agent_id = value;
}
const DynamicPrintConfig& config = preset_bundle->printers.get_edited_preset().config;
if (config.has("printer_agent")) {
const std::string& value = config.option<ConfigOptionString>("printer_agent")->value;
if (!value.empty())
effective_agent_id = value;
}
// Check if agent is registered
if (!NetworkAgentFactory::is_printer_agent_registered(effective_agent_id)) {
const PrinterAgentInfo* agent_info_ptr = NetworkAgentFactory::get_printer_agent_info(effective_agent_id);
if (!agent_info_ptr) {
BOOST_LOG_TRIVIAL(warning) << __FUNCTION__ << ": unregistered agent ID '" << effective_agent_id
<< "', keeping current agent";
// Keep current agent, don't switch
return;
}
const PrinterAgentInfo agent_info = *agent_info_ptr;
std::string current_agent_id;
if (m_agent->get_printer_agent())
current_agent_id = m_agent->get_printer_agent()->get_agent_info().id;
std::string log_dir = data_dir();
std::string cloud_agent_id = agent_info.id == BBL_PRINTER_AGENT_ID ? BBL_CLOUD_PROVIDER : ORCA_CLOUD_PROVIDER;
std::shared_ptr<ICloudServiceAgent> cloud_agent = m_agent->get_cloud_agent(cloud_agent_id);
if (current_agent_id != effective_agent_id) {
std::string log_dir = data_dir();
std::shared_ptr<ICloudServiceAgent> cloud_agent = m_agent->get_cloud_agent(cloud_agent_id);
// Create new printer agent via registry
std::shared_ptr<IPrinterAgent> new_printer_agent =
NetworkAgentFactory::create_printer_agent_by_id(effective_agent_id, cloud_agent, log_dir);
// Create new printer agent via registry
std::shared_ptr<IPrinterAgent> new_printer_agent =
NetworkAgentFactory::create_printer_agent_by_id(effective_agent_id, cloud_agent, log_dir);
if (!new_printer_agent) {
BOOST_LOG_TRIVIAL(warning) << __FUNCTION__ << ": failed to create agent '" << effective_agent_id << "', keeping current agent";
return;
}
if (!new_printer_agent) {
BOOST_LOG_TRIVIAL(warning) << __FUNCTION__ << ": failed to create agent '" << effective_agent_id << "', keeping current agent";
return;
}
if (m_agent->get_printer_agent() == new_printer_agent)
return;
// Swap the agent
m_agent->set_printer_agent(new_printer_agent);
sidebar().update_all_preset_comboboxes();
// Swap the agent
m_agent->set_printer_agent(new_printer_agent);
sidebar().update_all_preset_comboboxes();
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ": printer agent switched to " << effective_agent_id;
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ": printer agent switched to " << effective_agent_id;
// Start discovery so Python agents can populate the device list via SSDP callback
m_agent->start_discovery(true, false);
{
// Auto-switch MachineObject
select_machine(effective_agent_id);
select_machine(agent_info.id);
}
}
@@ -4587,9 +4717,17 @@ void GUI_App::request_user_logout(const std::string& provider/* = ORCA_CLOUD_PRO
remove_user_presets();
enable_user_preset_folder(false);
Slic3r::PluginManager::instance().get_loader().unload_cloud_plugins();
Slic3r::PluginManager::instance().get_catalog().clear_cloud_plugin_catalog();
Slic3r::PluginManager::instance().get_catalog().set_cloud_plugin_dir("");
Slic3r::PluginManager::instance().get_loader().set_cloud_user_id("");
preset_bundle->load_user_presets(DEFAULT_USER_FOLDER_NAME, ForwardCompatibilitySubstitutionRule::Enable);
mainframe->update_side_preset_ui();
// keep this here. refresh_from_catalog is meant to update the dialog UI.
if (m_plugins_dlg)
m_plugins_dlg->update_plugin_dialog_ui();
GUI::wxGetApp().stop_sync_user_preset();
}
@@ -5119,10 +5257,14 @@ void GUI_App::enable_user_preset_folder(bool enable)
std::string user_id = m_agent->get_user_id();
app_config->set("preset_folder", user_id);
GUI::wxGetApp().preset_bundle->update_user_presets_directory(user_id);
PluginManager::instance().get_catalog().set_cloud_plugin_dir(user_id);
PluginManager::instance().get_loader().set_cloud_user_id(user_id);
} else {
BOOST_LOG_TRIVIAL(info) << "preset_folder: set to empty";
app_config->set("preset_folder", "");
GUI::wxGetApp().preset_bundle->update_user_presets_directory(DEFAULT_USER_FOLDER_NAME);
PluginManager::instance().get_catalog().set_cloud_plugin_dir("");
PluginManager::instance().get_loader().set_cloud_user_id("");
}
}
@@ -5160,12 +5302,27 @@ void GUI_App::on_user_login_handle(wxCommandEvent &evt)
});
if (online_login && provider == ORCA_CLOUD_PROVIDER) {
// The steps below run synchronously on the UI thread (cloud plugin fetch and
// user-preset load both block on network/disk). Show an indeterminate progress
// dialog so the window isn't frozen without feedback. Percentages are cosmetic
// milestones, not measured progress.
ProgressDialog dlg(_L("Loading"), _L("Syncing your account…"), 100, mainframe, wxPD_AUTO_HIDE | wxPD_APP_MODAL);
dlg.Update(10, _L("Migrating presets…"));
maybe_migrate_user_presets_on_login();
remove_user_presets();
enable_user_preset_folder(true);
dlg.Update(40, _L("Fetching plugins…"));
PluginManager::instance().fetch_plugins_from_cloud();
if (m_plugins_dlg)
m_plugins_dlg->update_plugin_dialog_ui();
dlg.Update(70, _L("Loading user presets…"));
preset_bundle->load_user_presets(m_agent->get_user_id(provider), ForwardCompatibilitySubstitutionRule::Enable);
mainframe->update_side_preset_ui();
dlg.Update(100);
GUI::wxGetApp().mainframe->show_sync_dialog();
}
@@ -5179,7 +5336,7 @@ void GUI_App::on_user_login_handle(wxCommandEvent &evt)
void GUI_App::check_track_enable()
{
// Orca: telemetry only exists on the BBL cloud agent; always disable it.
// Orca: alaways disable track event
if (m_agent) {
m_agent->track_enable(false);
m_agent->track_remove_files();
@@ -5327,40 +5484,6 @@ struct UpdaterQuery
std::string os_info;
};
std::string detect_updater_os()
{
#if defined(_WIN32)
return "win";
#elif defined(__APPLE__)
return "macos";
#elif defined(__linux__) || defined(__LINUX__)
return "linux";
#else
return "unknown";
#endif
}
std::string detect_updater_arch()
{
#if defined(__aarch64__) || defined(_M_ARM64)
return "arm64";
#elif defined(__x86_64__) || defined(_M_X64)
return "x86_64";
#elif defined(__i386__) || defined(_M_IX86)
return "i386";
#else
std::string arch = wxPlatformInfo::Get().GetArchName().ToStdString();
boost::algorithm::to_lower(arch);
if (arch.find("aarch64") != std::string::npos || arch.find("arm64") != std::string::npos)
return "arm64";
if (arch.find("x86_64") != std::string::npos || arch.find("amd64") != std::string::npos)
return "x86_64";
if (arch.find("i686") != std::string::npos || arch.find("i386") != std::string::npos || arch.find("x86") != std::string::npos)
return "i386";
return "unknown";
#endif
}
std::string detect_updater_os_info()
{
wxString description = wxPlatformInfo::Get().GetOperatingSystemDescription();
@@ -5589,8 +5712,8 @@ void GUI_App::check_new_version_sf(bool show_tips, int by_user)
UpdaterQuery query{
detect_updater_iid(app_config),
detect_updater_version(),
detect_updater_os(),
detect_updater_arch(),
platform_os_type(),
platform_architecture(),
detect_updater_os_info()
};
@@ -8020,6 +8143,64 @@ void GUI_App::open_presetbundledialog(size_t open_on_tab, const std::string& hig
}
}
void GUI_App::open_plugins_dialog(size_t open_on_tab, const std::string& highlight_option)
{
if (m_plugins_dlg) {
m_plugins_dlg->Show();
m_plugins_dlg->Raise();
return;
}
try {
m_plugins_dlg = new PluginsDialog(mainframe, open_on_tab, highlight_option);
m_plugins_dlg->set_open_terminal_dlg_fn();
m_plugins_dlg->Bind(wxEVT_DESTROY, [this](wxWindowDestroyEvent& event) {
if (event.GetEventObject() == m_plugins_dlg)
m_plugins_dlg = nullptr;
event.Skip();
});
m_plugins_dlg->Show();
m_plugins_dlg->Raise();
} catch (const std::exception& e) {
BOOST_LOG_TRIVIAL(error) << "open_plugins_dialog failed: " << e.what();
if (m_plugins_dlg) {
m_plugins_dlg->Destroy();
m_plugins_dlg = nullptr;
}
wxMessageBox(wxString::Format(_L("Failed to open the Plugins dialog:\n%s"), from_u8(e.what())), _L("Plugins"),
wxOK | wxICON_ERROR, mainframe);
} catch (...) {
BOOST_LOG_TRIVIAL(error) << "open_plugins_dialog failed with a non-standard exception";
if (m_plugins_dlg) {
m_plugins_dlg->Destroy();
m_plugins_dlg = nullptr;
}
wxMessageBox(_L("Failed to open the Plugins dialog (unknown error)."), _L("Plugins"), wxOK | wxICON_ERROR, mainframe);
}
}
void GUI_App::open_terminal_dialog()
{
if (m_terminal_dlg) {
m_terminal_dlg->Show();
m_terminal_dlg->Raise();
return;
}
m_terminal_dlg = new TerminalDialog(mainframe, wxID_ANY, _L("Plugin Terminal"),
wxDefaultPosition, wxSize(820, 600));
m_terminal_dlg->Bind(wxEVT_DESTROY, [this](wxWindowDestroyEvent& event) {
if (event.GetEventObject() == m_terminal_dlg)
m_terminal_dlg = nullptr;
event.Skip();
});
m_terminal_dlg->Show();
m_terminal_dlg->Raise();
}
void GUI_App::open_exportpresetbundledialog(size_t open_on_tab, const std::string& highlight_option)
{
bool app_layout_changed = false;