Files
OrcaSlicer/src/slic3r/plugin/PluginConfig.hpp
2026-07-16 18:18:07 +08:00

119 lines
4.3 KiB
C++

#pragma once
#include <libslic3r/Utils.hpp>
#include <boost/filesystem.hpp>
#include <nlohmann/json.hpp>
#include <slic3r/plugin/PluginFsUtils.hpp>
#include <slic3r/plugin/PythonPluginInterface.hpp>
#include <map>
#include <mutex>
#include <optional>
#include <string>
#include <vector>
#define PLUGIN_CONFIG_DIR "config.json"
namespace Slic3r {
class Preset;
struct CapabilityConfigEntry
{
PluginCapabilityId id;
std::string plugin_version;
nlohmann::json config = nlohmann::json::object();
};
class CapabilityConfigDocument
{
public:
static constexpr const char* KeyEntries = "config";
static CapabilityConfigDocument from_root_json(const nlohmann::json& root);
static CapabilityConfigDocument from_entries(const nlohmann::json& entries);
std::optional<CapabilityConfigEntry> find(const PluginCapabilityId& id) const;
bool contains(const PluginCapabilityId& id) const;
bool upsert(CapabilityConfigEntry entry);
bool erase(const PluginCapabilityId& id);
bool empty() const;
nlohmann::json serialize_entries() const;
nlohmann::json root_json() const;
private:
std::map<PluginCapabilityId, nlohmann::json> m_entries;
std::vector<nlohmann::json> m_opaque_entries;
};
inline constexpr const char* PLUGIN_OVERRIDES_OPTION_KEY = "plugin_preference_overrides";
std::string plugin_overrides_of(const Preset& preset);
bool parse_plugin_overrides(const std::string& raw, CapabilityConfigDocument& document, std::string& error);
std::string serialize_plugin_overrides(const CapabilityConfigDocument& document);
struct EffectiveCapabilityConfig
{
PluginCapabilityId id;
nlohmann::json config = nlohmann::json::object();
bool has_preset_override = false;
bool has_base_config = false;
std::string stored_plugin_version;
std::string running_plugin_version;
};
struct MutationResult
{
bool ok = false;
bool changed = false;
std::string error;
EffectiveCapabilityConfig effective;
};
class PresetPluginConfigService
{
public:
EffectiveCapabilityConfig get_effective_config(const CapabilityConfigDocument& overrides,
const PluginCapabilityId& id) const;
MutationResult set_preset_override(CapabilityConfigDocument& overrides,
const PluginCapabilityId& id,
const nlohmann::json& value) const;
MutationResult remove_preset_override(CapabilityConfigDocument& overrides,
const PluginCapabilityId& id) const;
};
EffectiveCapabilityConfig active_capability_config(const PluginCapabilityId& id);
class PluginConfig
{
public:
static const std::string plugin_config_file() { return (boost::filesystem::path(get_orca_plugins_dir()) / PLUGIN_CONFIG_DIR).string(); }
void load();
bool save();
void save_config(const CapabilityConfigEntry& config);
bool store_capability_config(const PluginCapabilityId& id, const nlohmann::json& config);
bool erase_capability_config(const PluginCapabilityId& id);
std::optional<CapabilityConfigEntry> get_config(const PluginCapabilityId& id) const;
bool has_config(const PluginCapabilityId& id) const;
bool dirty() const;
static nlohmann::json capabilities_payload(const std::vector<PluginCapabilityId>& caps);
static nlohmann::json get_config_response(const PluginCapabilityId& id);tom UI.
static nlohmann::json save_config_response(const PluginCapabilityId& id, const nlohmann::json& config);
static nlohmann::json restore_config_response(const PluginCapabilityId& id);
private:
mutable std::mutex m_mutex;
CapabilityConfigDocument m_document;
bool m_dirty = false;
};
nlohmann::json capability_get_config(const PluginCapabilityInterface& capability);
std::string capability_get_config_version(const PluginCapabilityInterface& capability);
bool capability_save_config(const PluginCapabilityInterface& capability, const nlohmann::json& config);
} // namespace Slic3r