From f7caf0db07266a6ec4f7518143028ec0bc93e5d7 Mon Sep 17 00:00:00 2001 From: Ian Chua Date: Thu, 23 Jul 2026 21:04:08 +0800 Subject: [PATCH 1/2] feat(plugin): storage API --- src/slic3r/plugin/host/PluginHost.cpp | 51 +++++++++++++++++++ src/slic3r/plugin/host/PluginHostBindings.hpp | 2 +- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/slic3r/plugin/host/PluginHost.cpp b/src/slic3r/plugin/host/PluginHost.cpp index 524f07f12c..5fca4d4fbc 100644 --- a/src/slic3r/plugin/host/PluginHost.cpp +++ b/src/slic3r/plugin/host/PluginHost.cpp @@ -1,9 +1,59 @@ #include "PluginHost.hpp" #include "PluginHostBindings.hpp" #include "PluginHostUi.hpp" +#include +#include +#include +#include +#include + +#include namespace Slic3r { +namespace host_bindings { +void register_plugin(pybind11::module_& host) +{ + auto plugin_host = host.def_submodule("plugin", "Plugin host API"); + + plugin_host.def( + "storage", + []() -> std::string { + const std::string plugin_key = PluginAuditManager::instance().current_plugin(); + if (plugin_key.empty()) + throw std::runtime_error("plugin.storage() must be called from a plugin callback"); + + PluginDescriptor descriptor; + if (!PluginManager::instance().try_get_plugin_descriptor(plugin_key, descriptor)) + throw std::runtime_error("The current plugin is not registered"); + + // plugin_root is populated for installed packages. If it is unavailable, the entry + // path still identifies the same package directory. This is important for local + // plugins: their directory is based on the source filename (including its extension), + // while plugin_key is based on the filename stem. + const boost::filesystem::path plugin_root = resolve_plugin_root_from_descriptor(descriptor); + if (!plugin_root.empty()) + return plugin_root.string(); + + if (!descriptor.is_cloud_plugin()) + throw std::runtime_error("The current local plugin folder is unavailable"); + + if (wxTheApp == nullptr || GUI::wxGetApp().getAgent() == nullptr) + throw std::runtime_error("Cloud plugin storage is unavailable before networking is initialized"); + + const std::string user_id = GUI::wxGetApp().getAgent()->get_user_id(); + if (user_id.empty()) + throw std::runtime_error("Cloud plugin storage is unavailable without a logged-in user"); + + if (!is_valid_plugin_id(plugin_key)) + throw std::runtime_error("The current cloud plugin key is not a valid folder name"); + + return (boost::filesystem::path(get_cloud_plugin_dir(user_id)) / plugin_key).string(); + }, + "Return the installed folder of the current plugin."); +} +} // namespace host_bindings + void PluginHost::RegisterBindings(pybind11::module_& module) { auto host = module.def_submodule("host", "Host application API"); @@ -15,6 +65,7 @@ void PluginHost::RegisterBindings(pybind11::module_& module) host_bindings::register_presets(host); host_bindings::register_model(host); host_bindings::register_app(host); + host_bindings::register_plugin(host); // UI: native dialogs and interactive HTML windows for plugins. PluginHostUi::RegisterBindings(host); diff --git a/src/slic3r/plugin/host/PluginHostBindings.hpp b/src/slic3r/plugin/host/PluginHostBindings.hpp index 0f206d5992..94601ad99c 100644 --- a/src/slic3r/plugin/host/PluginHostBindings.hpp +++ b/src/slic3r/plugin/host/PluginHostBindings.hpp @@ -12,5 +12,5 @@ void register_presets(pybind11::module_& host); // PluginHostPresets.cpp void register_model(pybind11::module_& host); // PluginHostModel.cpp void register_app(pybind11::module_& host); // PluginHostApp.cpp void register_slicing(pybind11::module_& host); // PluginHostSlicing.cpp - +void register_plugin(pybind11::module_& host); // PluginHost.cpp } // namespace Slic3r::host_bindings From 2e246341d16bc655f409d2882365508b020c097b Mon Sep 17 00:00:00 2001 From: Ian Chua Date: Fri, 24 Jul 2026 18:57:46 +0800 Subject: [PATCH 2/2] move the storage directory outside the actual plugin code folder --- src/slic3r/plugin/PluginFsUtils.cpp | 2 +- src/slic3r/plugin/PluginFsUtils.hpp | 1 + src/slic3r/plugin/PluginManager.cpp | 32 +++++++++++++++++++++++++++ src/slic3r/plugin/PluginManager.hpp | 4 ++++ src/slic3r/plugin/host/PluginHost.cpp | 30 +------------------------ 5 files changed, 39 insertions(+), 30 deletions(-) diff --git a/src/slic3r/plugin/PluginFsUtils.cpp b/src/slic3r/plugin/PluginFsUtils.cpp index 445dd00b9b..63dc229967 100644 --- a/src/slic3r/plugin/PluginFsUtils.cpp +++ b/src/slic3r/plugin/PluginFsUtils.cpp @@ -632,7 +632,7 @@ void parse_metadata_rfc822(const std::string& content, bool is_ignored_plugin_directory(const boost::filesystem::path& path) { const std::string name = path.filename().string(); - return name.empty() || name[0] == '.' || name.rfind("__", 0) == 0 || name == PLUGIN_SUBSCRIBED_DIR; + return name.empty() || name[0] == '.' || name.rfind("__", 0) == 0 || name == PLUGIN_SUBSCRIBED_DIR || name == PLUGIN_DATA_DIR; } bool is_safe_relative_path(const boost::filesystem::path& path) diff --git a/src/slic3r/plugin/PluginFsUtils.hpp b/src/slic3r/plugin/PluginFsUtils.hpp index 7922946b1c..5f57dbf807 100644 --- a/src/slic3r/plugin/PluginFsUtils.hpp +++ b/src/slic3r/plugin/PluginFsUtils.hpp @@ -12,6 +12,7 @@ #include #define PLUGIN_SUBSCRIBED_DIR "_subscribed" +#define PLUGIN_DATA_DIR "plugin_data" namespace Slic3r { diff --git a/src/slic3r/plugin/PluginManager.cpp b/src/slic3r/plugin/PluginManager.cpp index 761a9aad63..abc2446d55 100644 --- a/src/slic3r/plugin/PluginManager.cpp +++ b/src/slic3r/plugin/PluginManager.cpp @@ -486,6 +486,38 @@ bool PluginManager::try_get_plugin_descriptor_for_capability(const std::string& return false; } +std::string PluginManager::get_storage_dir(const std::string& plugin_key) const +{ + namespace fs = boost::filesystem; + + PluginDescriptor descriptor; + if (!try_get_plugin_descriptor(plugin_key, descriptor)) + throw std::runtime_error("The current plugin is not registered"); + + const fs::path base_storage_dir = fs::path(get_orca_plugins_dir()) / PLUGIN_DATA_DIR; + + if (!descriptor.is_cloud_plugin()) { + const fs::path local_storage_dir = base_storage_dir / plugin_key; + fs::create_directories(local_storage_dir); + return local_storage_dir.string(); + } + + auto agent = m_cloud_service.get_cloud_agent(); + if (!agent) + throw std::runtime_error("Cloud plugin storage is unavailable before networking is initialized"); + + const std::string user_id = agent->get_user_id(); + if (user_id.empty()) + throw std::runtime_error("Cloud plugin storage is unavailable without a logged-in user"); + + if (!is_valid_plugin_id(plugin_key)) + throw std::runtime_error("The current cloud plugin key is not a valid folder name"); + + const fs::path cloud_storage_dir = base_storage_dir / PLUGIN_SUBSCRIBED_DIR / user_id / plugin_key; + fs::create_directories(cloud_storage_dir); + return cloud_storage_dir.string(); +} + // ── Capability instances ──────────────────────────────────────────────────────────────────── std::vector> PluginManager::get_plugin_capabilities(const std::string& plugin_key, diff --git a/src/slic3r/plugin/PluginManager.hpp b/src/slic3r/plugin/PluginManager.hpp index 59a2791355..a0bab2afe3 100644 --- a/src/slic3r/plugin/PluginManager.hpp +++ b/src/slic3r/plugin/PluginManager.hpp @@ -138,6 +138,10 @@ public: bool try_get_plugin_descriptor_for_capability(const std::string& capability_name, PluginCapabilityType type, PluginDescriptor& out) const; + // Per-plugin storage directory under orca_plugins/plugin_data, created if missing. Throws + // std::runtime_error if the plugin is unregistered, the key is invalid, or (cloud plugins) + // no user is logged in yet. + std::string get_storage_dir(const std::string& plugin_key) const; std::vector> get_plugin_capabilities( const std::string& plugin_key = "", // "" => all plugins diff --git a/src/slic3r/plugin/host/PluginHost.cpp b/src/slic3r/plugin/host/PluginHost.cpp index 5fca4d4fbc..2830d6f276 100644 --- a/src/slic3r/plugin/host/PluginHost.cpp +++ b/src/slic3r/plugin/host/PluginHost.cpp @@ -2,10 +2,7 @@ #include "PluginHostBindings.hpp" #include "PluginHostUi.hpp" #include -#include -#include #include -#include #include @@ -23,32 +20,7 @@ void register_plugin(pybind11::module_& host) if (plugin_key.empty()) throw std::runtime_error("plugin.storage() must be called from a plugin callback"); - PluginDescriptor descriptor; - if (!PluginManager::instance().try_get_plugin_descriptor(plugin_key, descriptor)) - throw std::runtime_error("The current plugin is not registered"); - - // plugin_root is populated for installed packages. If it is unavailable, the entry - // path still identifies the same package directory. This is important for local - // plugins: their directory is based on the source filename (including its extension), - // while plugin_key is based on the filename stem. - const boost::filesystem::path plugin_root = resolve_plugin_root_from_descriptor(descriptor); - if (!plugin_root.empty()) - return plugin_root.string(); - - if (!descriptor.is_cloud_plugin()) - throw std::runtime_error("The current local plugin folder is unavailable"); - - if (wxTheApp == nullptr || GUI::wxGetApp().getAgent() == nullptr) - throw std::runtime_error("Cloud plugin storage is unavailable before networking is initialized"); - - const std::string user_id = GUI::wxGetApp().getAgent()->get_user_id(); - if (user_id.empty()) - throw std::runtime_error("Cloud plugin storage is unavailable without a logged-in user"); - - if (!is_valid_plugin_id(plugin_key)) - throw std::runtime_error("The current cloud plugin key is not a valid folder name"); - - return (boost::filesystem::path(get_cloud_plugin_dir(user_id)) / plugin_key).string(); + return PluginManager::instance().get_storage_dir(plugin_key); }, "Return the installed folder of the current plugin."); }