mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-11-29 08:32:00 -05:00
fix: warn on preset save when plugin not found (#16929)
* fix: warn on preset save when plugin not found * refactor: build the unsaved-changes save config in one place * Fixes clang-tidy --------- Co-authored-by: Lam Wei Lun <weilun.lam@gmail.com>
This commit is contained in:
1 parent
3322f6101c
commit
3a0517831f
15 files changed
+411
-66
No files matched your search
+53
-35
@@ -1588,7 +1588,9 @@ void ConfigBase::save_to_json(std::ostream &os, const std::string &name, const s
|
||||
// Serialize the top-level "plugins" manifest: the individual plugin-backed options keep bare
|
||||
// capability names; the full "name;uuid;capability" references are derived here (same helper as
|
||||
// update_plugin_manifest). Only with a resolver (GUI); without one (CLI/headless) leave whatever
|
||||
// the "plugins" option already serialized above, so a round-trip never drops the manifest.
|
||||
// the "plugins" option already serialized above, so a round-trip never drops the manifest. A value
|
||||
// the resolver cannot map is omitted; unresolved_plugin_capabilities() reports it so a GUI save
|
||||
// entry point can prompt before writing.
|
||||
if (resolve_capability_fn) {
|
||||
std::vector<std::string> unique_refs = this->collect_plugin_manifest();
|
||||
if (unique_refs.empty())
|
||||
@@ -1623,40 +1625,32 @@ void ConfigBase::null_nullables()
|
||||
}
|
||||
}
|
||||
|
||||
void ConfigBase::save_plugin_collection(const std::string& opt_key, const ConfigOption* opt, std::vector<std::string>& plugin_refs) const {
|
||||
// Full plugin capability references ("name;uuid;capability") can only be derived through the
|
||||
// resolver registered by the GUI once plugins are loaded. In non-GUI/headless contexts (e.g.
|
||||
// the CLI) it stays null, so skip silently rather than calling an empty std::function.
|
||||
if (!resolve_capability_fn)
|
||||
return;
|
||||
|
||||
void ConfigBase::for_each_plugin_capability(const std::function<void(const std::string& opt_key, const std::string& plugin_type, const std::string& capability_name)>& fn) const
|
||||
{
|
||||
// A plugin-backed option declares its capability type via ConfigOptionDef::plugin_type (the same
|
||||
// metadata PluginResolver::find_option_for_capability scans). Deriving off the def rather than a
|
||||
// per-key branch keeps this generic across every plugin-backed option.
|
||||
const ConfigDef* def = this->def();
|
||||
const ConfigOptionDef* opt_def = def ? def->get(opt_key) : nullptr;
|
||||
if (opt_def == nullptr || !opt_def->is_plugin_backed())
|
||||
const ConfigDef* def = this->def();
|
||||
if (def == nullptr)
|
||||
return;
|
||||
const std::string& type = opt_def->plugin_type;
|
||||
|
||||
// Resolve a single bare capability value into its full reference and append it, skipping unset
|
||||
// values, capabilities that could not be resolved (resolver returns ""), and duplicates already
|
||||
// collected (preserving insertion order).
|
||||
const auto append_ref = [&plugin_refs, &type](const std::string& capability_value) {
|
||||
if (capability_value.empty())
|
||||
return;
|
||||
std::string ref = resolve_capability_fn(capability_value, type);
|
||||
if (!ref.empty() && std::find(plugin_refs.begin(), plugin_refs.end(), ref) == plugin_refs.end())
|
||||
plugin_refs.emplace_back(std::move(ref));
|
||||
};
|
||||
for (const std::string& opt_key : this->keys()) {
|
||||
const ConfigOptionDef* opt_def = def->get(opt_key);
|
||||
const ConfigOption* opt = this->option(opt_key);
|
||||
if (opt_def == nullptr || opt == nullptr || !opt_def->is_plugin_backed())
|
||||
continue;
|
||||
|
||||
// Scalar options carry a single capability name; vector options carry a list. Same scalar/vector
|
||||
// dispatch as PluginResolver::find_option_for_capability.
|
||||
if (const auto* string_option = dynamic_cast<const ConfigOptionString*>(opt))
|
||||
append_ref(string_option->value);
|
||||
else if (const auto* vector_option = dynamic_cast<const ConfigOptionVectorBase*>(opt))
|
||||
for (const std::string& val : vector_option->vserialize())
|
||||
append_ref(val);
|
||||
// Scalar options carry a single capability name; vector options carry a list. Same
|
||||
// scalar/vector dispatch as PluginResolver::find_option_for_capability.
|
||||
if (const auto* scalar_option = dynamic_cast<const ConfigOptionString*>(opt)) {
|
||||
if (!scalar_option->value.empty())
|
||||
fn(opt_key, opt_def->plugin_type, scalar_option->value);
|
||||
} else if (const auto* vector_option = dynamic_cast<const ConfigOptionVectorBase*>(opt)) {
|
||||
for (const std::string& capability_name : vector_option->vserialize())
|
||||
if (!capability_name.empty())
|
||||
fn(opt_key, opt_def->plugin_type, capability_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> ConfigBase::collect_plugin_manifest() const
|
||||
@@ -1665,15 +1659,39 @@ std::vector<std::string> ConfigBase::collect_plugin_manifest() const
|
||||
if (!resolve_capability_fn)
|
||||
return refs;
|
||||
|
||||
// Each plugin-backed option (ConfigOptionDef::is_plugin_backed) contributes its resolved
|
||||
// reference(s) via save_plugin_collection, which appends in order and skips duplicates, so no
|
||||
// second de-duplication pass is needed here.
|
||||
for (const std::string& opt_key : this->keys())
|
||||
if (const ConfigOption* opt = this->option(opt_key))
|
||||
this->save_plugin_collection(opt_key, opt, refs);
|
||||
// Resolve each capability value and append its full reference, dropping duplicates (preserving
|
||||
// insertion order). A value that resolves nowhere has no reference to write, so it is skipped;
|
||||
// unresolved_plugin_capabilities() reports those values so a GUI save can prompt.
|
||||
this->for_each_plugin_capability([&refs](const std::string& /* opt_key */, const std::string& plugin_type, const std::string& capability_name) {
|
||||
std::string ref = resolve_capability_fn(capability_name, plugin_type);
|
||||
if (!ref.empty() && std::find(refs.begin(), refs.end(), ref) == refs.end())
|
||||
refs.emplace_back(std::move(ref));
|
||||
});
|
||||
return refs;
|
||||
}
|
||||
|
||||
std::vector<UnresolvedPluginCapability> ConfigBase::unresolved_plugin_capabilities() const
|
||||
{
|
||||
std::vector<UnresolvedPluginCapability> unresolved;
|
||||
// Without a resolver (CLI/headless) serialization leaves the manifest loaded from disk untouched,
|
||||
// so no value is lost and there is nothing to warn about.
|
||||
if (!resolve_capability_fn)
|
||||
return unresolved;
|
||||
|
||||
// Same walk and same resolver call as collect_plugin_manifest: a value is reported here exactly
|
||||
// when the serializer would omit it.
|
||||
this->for_each_plugin_capability([&unresolved](const std::string& opt_key, const std::string& plugin_type, const std::string& capability_name) {
|
||||
if (!resolve_capability_fn(capability_name, plugin_type).empty())
|
||||
return;
|
||||
const auto matches = [&opt_key, &capability_name](const UnresolvedPluginCapability& entry) {
|
||||
return entry.opt_key == opt_key && entry.capability_name == capability_name;
|
||||
};
|
||||
if (std::none_of(unresolved.begin(), unresolved.end(), matches))
|
||||
unresolved.push_back(UnresolvedPluginCapability{ opt_key, capability_name });
|
||||
});
|
||||
return unresolved;
|
||||
}
|
||||
|
||||
void ConfigBase::update_plugin_manifest()
|
||||
{
|
||||
// Writes the derived manifest back into this config's "plugins" option (save_to_json writes the
|
||||
|
||||
Reference in new issue
Block a user