Files
OrcaSlicer/tests/slic3rutils/test_plugin_printer_asset.cpp
9b886b5346 Resource printer asset plugins for portable bed models and textures, with a printer preset plugin manifest (#16814)
* fix(plugins): show missing-plugin notices on preset switch; printer plugin manifest

Missing-plugin notices were rebuilt only when a plate was validated, so
switching to a preset that needs an absent plugin showed nothing on an
empty plate, and new_project() removed the notices for good (their
shown-signature stayed set). They are now refreshed on every preset
change, when a plugin loads, unloads, is disabled, deleted or
unsubscribed, and after a new project, and pushed again whenever they
are no longer on screen.

Printer presets get their own manifest key, printer_plugins: the full
print config applies the printer preset after the process preset, so a
shared "plugins" key would replace the process manifest, and printer
presets dropped "plugins" on load. Plugins referenced by the printer
preset (e.g. a Python printer agent) are reported but never block
slicing, since they do not change the sliced result. Saving a preset on
a machine that lacks one of its plugins keeps that plugin's manifest
entry, so other machines are still offered it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(plugins): resource printer asset plugins for portable bed models and textures

A printer preset's bed_custom_model / bed_custom_texture hold the
author's absolute local paths, so the custom bed is lost on every other
machine and for every bundle subscriber (#15217). A resource printer
asset plugin is a pack of bed models (.stl) and textures (.png/.svg)
installed like a resource texture pack; the preset then stores a
portable reference, orcacloud-asset/<plugin id>/<file>, that each
machine resolves to its own installed copy.

- New capability type ResourcePrinterAsset ("resource-printer-asset",
  orca.resource_printer_asset) with get_asset_dir(), validated at load
  like ResourceTexture: inside the plugin folder, regular files only,
  .stl/.png/.svg, at most 16, sorted so every file system keeps the same.
- PluginPrinterAssets tracks loaded packs and resolves references;
  Plater::set_bed_shape draws the vendor bed while a pack is missing.
- Bed Shape dialog: "From asset pack..." next to each Load button.
- The hidden plugin-backed option printer_asset_plugins names the packs
  the bed files come from, so the printer manifest lists them and the
  missing-plugin notices offer to install or activate a missing pack
  (non-blocking). A before-save hook re-derives it on every printer
  preset save, and transfers between presets carry the manifest along.
- References are OS-neutral: no colon or backslash, and Windows device
  names are rejected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Remove the missing-plugin notice changes from this PR

They moved to a separate PR (#16897) so this branch only carries the
printer preset plugin manifest and the slicing block rule.

Removed here: the notice refresh hooks in GUI_App and PluginsDialog,
Plater::refresh_missing_plugin_notifications, the preset-switch and
new-project refresh calls, and NotificationManager::has_notification_of_type.
The has_blocking_plugins() changes in Plater.cpp stay.

* Close the unresolved_plugin_capabilities test case

The merge with main dropped the closing brace of the new test.

* Use one manifest-key rule and cover printer plugins in tests

ConfigBase::plugin_manifest_key() is now the only definition of which key
holds a config's plugin manifest; PluginResolver used its own copy keyed by
preset type. The resolver calls the config's method instead.

Tests: a printer config keeps its printer_plugins entry for a capability not
loaded here, and a missing printer-preset plugin is reported without blocking
slicing. Comments that named the "plugins" option for every preset now say
plugin manifest.

* fix(plugins): build the missing-plugin search link without a running app

A missing local plugin gets a "Find on OrcaCloud" link, whose base URL
came from GUI::wxGetApp().getAgent(). With no app (unit tests, headless
callers) that dereferenced null and crashed refresh_missing_plugins.
Keep the default cloud URL when there is no app.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: check the printer manifest key through ConfigBase::plugin_manifest_key

PluginResolver no longer has its own preset-type helper; the config's
method is the single rule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Include wx/app.h for wxTheApp in PluginResolver

clang-tidy's misc-include-cleaner wants the header that declares
wxTheApp included directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Replace the Resource Printer Asset Plugin Type With a Generic Resource Pack

A resource pack ships one folder of files, and each file declares the kinds
it serves (bed_model, bed_texture, displacement_texture) in Python through
get_resource_kinds(). Consumers list only the files of their kind, so one
PNG can be both a bed texture and a displacement texture.

PluginResourcePacks replaces PluginPrinterAssets and also serves the
existing resource-texture type as displacement_texture files, which retires
PluginResourceTextures. The resource-texture capability, its Python API,
type id, 64-file cap and extension list are unchanged.

Printer asset references stay kind-less; resolve() takes the kind of the
field being resolved, and only resource-pack entries resolve references.
The texture picker groups pack sections by pack_key so a ResourceTexture
and a ResourcePack with the same name stay separate.

Not built or run locally.

* Include the headers clang-tidy asks for in the resource pack code

misc-include-cleaner wants PythonPluginInterface.hpp (PluginCapabilityType)
in TextureLibrary.cpp, <string_view> in PluginResourcePacks.cpp,
ResourcePackKinds.hpp (ResourceKindMap) in the resource pack trampoline,
and <cstdint> in the resource pack test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Keep the Resource Printer Asset Capability and Infer File Kinds From the Extension

Orca Cloud builds resource pack wheels from uploaded files, stores no
per-file metadata, and names its types resource-textures and
resource-printer-assets. Authors write no Python, so a pack cannot declare
the kinds of its files.

Restore the resource-printer-asset capability (get_asset_dir) and keep
resource-texture (get_texture_dir) as it is. PluginResourcePacks still
serves both from one registry, indexed by kind: bed_model (.stl) and
bed_texture (.png, .svg) for a printer asset pack, displacement_texture
for a texture pack. Each kind is capped at 16 (64 for textures) after
sorting. Only printer asset packs resolve printer asset references.

Drop get_resource_kinds, the resource-pack capability and its type id.
Rewrite docs/HLSD/resource-packs.md for the two types.

Not built or run locally.

* Keep the Texture and Printer Asset Plugin Types Separate

Orca Cloud treats resource-textures and resource-printer-assets as
independent types with no per-file kinds, so the slicer keeps them
independent too. Each has its own capability, registry, loader branch and
tests.

Restore PluginPrinterAssets as a registry of its own with a plain file
list and the extension helpers the Bed Shape dialog uses, and put the
resource texture code (PluginResourceTextures, the texture picker, its
callbacks and tests) back as it was on main. Remove the shared
PluginResourcePacks registry, the kind index and pack_key.

Replace docs/HLSD/resource-packs.md with docs/HLSD/resource-printer-assets.md.

Not built or run locally.

* Keep the Printer Plugin Keys Out of the G-code Config Dump

printer_asset_plugins and printer_plugins are newly-registered printer keys
with empty defaults, so append_full_config() would write
"; printer_asset_plugins = " and "; printer_plugins = " into every printer's
config block, machines that never install a pack included. Both are read
from the preset and never parsed back out of the dump, so ban them like
farthest_point_timelapse and the IMEX keys.

Not built or run locally.

* Move a Printer Preset's Legacy Plugins Manifest Into printer_plugins

A printer preset written before printer_plugins existed stores its plugin
references under the process preset's key, plugins. That key is not a
printer option, so remove_invalid_keys() reported it as an incorrect key and
dropped it on every start, taking with it the reference the missing-plugin
notice needs for a plugin this machine lacks.

Rename it before the validation, and merge rather than replace: a child
preset's file holds only the references it added, while the parent's
manifest already sits in the config it was built from. A config holding both
keys is a printer preset's, so the migration needs no type argument.

Cover the merge and the untouched process preset in test_config, and the
file path in test_preset_bundle_loading. Document the migration in the HLSD.

Not built or run locally.

* Say Which Presets Block Slicing in the Plugin Gate Comments

has_blocking_plugins() replaced has_missing_plugins() as the slicing gate:
only a plugin referenced by the process or filament preset blocks, while a
printer preset's plugins (printer agents, printer assets) only notify. Three
comments still described the old, type-agnostic rule: the cached
missing-plugin signature block in Plater::priv, refresh_missing_plugin_block's
return value, and plugins_block_slicing.

Not built or run locally.

* Test That a Missing Process Preset Plugin Blocks Slicing

Every existing assertion on has_blocking_plugins() is a negative one, so a
body that returned false would keep the suite green while slicing stopped
being blocked for a missing slicing-pipeline plugin -- the behavior this
change kept. Load an unresolved reference from a process preset: the gate
reports it missing and blocks, and clearing the missing-plugin sets unblocks.

Not built or run locally.

* Keep Inherited Plugin References in a Child Preset's Manifest

Preset::save wrote a diff against the parent, and save_to_json derived the
plugin manifest from that diff. A child that changed only printer_agent lost
the parent's bed pack reference, and the shortened manifest replaced the
parent's on reload. save_to_json now takes a manifest_source, and Preset::save
passes the whole resolved config.

A legacy "plugins" key in the file of a printer preset that inherits a parent
was dropped by the parent merge. The user, project-embedded and cloud load
paths and Preset::reload now move it into printer_plugins, merged with the
parent's manifest.

* Write the Printer Plugin Keys to the G-code Config Dump When Set

The config block is read back by config_from_gcode, so a printer imported from
a G-code that used a printer asset pack kept the bed reference but lost the
pack names and the manifest. The two keys are now written when they are not
empty, so a printer without packs keeps an unchanged block.

* Copy the Printer Plugin Keys When Creating a Nozzle Preset

The nozzle flow copied bed_custom_model and bed_custom_texture from the source
printer without printer_asset_plugins and printer_plugins, so a reference to a
pack that is not installed here was saved with an empty manifest.

* Describe the Manifest Derivation and the Pack Constraints in the HLD

* Redraw the Bed After a Printer Asset Pack Loads or Unloads

A texture-only change goes through update_logo_texture_filename, which does
not mark the canvas dirty, so the old texture stayed until the next paint.
The pack callback now requests an extra frame, as Plater::on_config_change does.

* Match a Printer Asset Pack by File and Keep Unusable Names Out of the Cap

capability_name() returned the first capability of the plugin whatever file
the reference named, so with two capabilities in one plugin the manifest could
name the wrong one. It now matches the file against the pack's listing, as
resolve() does, through one shared helper.

update_printer_asset_plugins rebuilt the manifest through a resolver that finds
a capability by name alone, so a loaded plugin with a capability of the same
name replaced the entry of a pack that is not loaded here. The pack's own entry
is put back.

The 16-file cap counted files a reference cannot name (spaces, device names),
which the Bed Shape dialog skips, so a pack could list nothing usable. The
listing now drops those names before the cap.

* Document the Printer Asset Capability's Python Bindings

The capability's pybind11 class and its two methods had no docstrings, so
script_list_api and script_describe_api listed them bare and "every public
member of the orca package has documentation and named parameters" failed
in slic3rutils_tests on every platform.

Say what a pack holds and how a preset refers to its files, and what
get_asset_dir must return: the checks its folder goes through once at load,
and that a rejected folder or a raised exception costs the pack its files.
Worded as resource_texture's bindings are.

* Harden the Printer Asset Pack Code Against Locale, Preset Type and a Failing Sweep

The reference charset used <cctype>, whose predicates follow LC_CTYPE. The
app pins LC_NUMERIC only and GUI_App constructs a default wxLocale, so
LC_CTYPE is the user's: on a single-byte Windows codepage a pack file named
bed_é.stl passes validation, is stored as raw UTF-8, and then parses as a
plain path on a machine with a UTF-8 or C locale, drawing the vendor bed
with no diagnostic. The HLD promises A-Z a-z 0-9 . _ -. Check those ranges
explicitly instead, in is_segment, is_windows_device_name and iends_with.

The legacy migration ran for every preset type and asked only that both
keys be present. A print or filament file carrying both had its "plugins"
moved into "printer_plugins" and erased, and the validation loop then
stripped "printer_plugins" as a non-print option: both manifests gone,
where main kept "plugins". Only a printer config has "printer_plugins" in
its defaults, so gate on that, and say so where the comment claimed a
config holding both keys must be a printer preset's.

initial_sweep() was called straight from GUI_App::on_init, outside the
loader's and the manager's guards, and it lists files. A pack file whose
name cannot be converted to the Windows ANSI code page throws
filesystem_error out of OnInit and terminates the app. Catch per
capability and log, so a fault costs the pack its files, as it does on the
loader's path.

on_cap_register() also reported a change unconditionally, so a disable and
re-enable redrew the bed for an entry nothing had altered. Compare the
rebuilt entry against the stored one: pack id, name, folder and file list.

Cover the three with tests: non-ASCII and out-of-set characters refused,
the 128-character boundary, an escaping and a dangling symlink skipped,
and the change flag staying false across a rebuild and turning true when a
file is added or removed.

* clang tidy missing header

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Lam Wei Lun <weilun.lam@gmail.com>
2026-12-03 16:22:54 +08:00

572 lines
26 KiB
C++

// ResourcePrinterAsset plugin type: asset folder validation, the host registry's lifecycle,
// resolving the "orcacloud-asset/<plugin id>/<file>" references printer presets store in
// bed_custom_model / bed_custom_texture, and the printer preset manifest that lets PluginResolver
// offer a missing pack without blocking slicing.
//
// The GUI wiring lives in GUI_App::init_plugin_gui_wiring; the test binary has no app, so the same
// registry callbacks are installed here. Each Catch2 test case runs in its own process
// (catch_discover_tests), so the PluginManager and interpreter come up at most once per test.
#include "test_utils.hpp"
#include <slic3r/plugin/PluginDescriptor.hpp>
#ifdef WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <Windows.h>
#endif
#include <boost/filesystem.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <catch2/catch_all.hpp>
#include <catch2/catch_message.hpp>
#include <catch2/catch_test_macros.hpp>
#include <algorithm>
#include <chrono>
#include <fstream>
#include <ios>
#include <memory>
#include <string>
#include <vector>
#include "libslic3r/Config.hpp"
#include "libslic3r/Preset.hpp"
#include "libslic3r/PrintConfig.hpp"
#include "libslic3r/PrinterAssetRef.hpp"
#include "slic3r/plugin/PluginFsUtils.hpp"
#include "slic3r/plugin/PluginManager.hpp"
#include "slic3r/plugin/PluginResolver.hpp"
#include "slic3r/plugin/PythonInterpreter.hpp"
#include "slic3r/plugin/PythonPluginInterface.hpp"
#include "slic3r/plugin/host/PluginPrinterAssets.hpp"
#include "plugin_test_utils.hpp"
using namespace Slic3r;
namespace fs = boost::filesystem;
namespace {
// Declare FIRST in a test so it is destroyed last (see test_plugin_resource_texture.cpp).
struct ScopedPluginManager
{
ScopedDataDir python_data_dir{"asset-python"};
bool initialized = false;
ScopedPluginManager() { initialized = PluginManager::instance().initialize(); }
~ScopedPluginManager() { PluginManager::instance().shutdown(); }
};
void install_printer_asset_wiring()
{
static bool installed = false;
if (installed)
return;
installed = true;
PluginManager& manager = PluginManager::instance();
manager.subscribe_on_load_callback([](const std::string& plugin_key) { PluginPrinterAssets::instance().on_plugin_register(plugin_key); });
manager.subscribe_on_unload_callback([](const std::string& plugin_key) { PluginPrinterAssets::instance().on_plugin_deregister(plugin_key); });
manager.subscribe_on_capability_load_callback([](const PluginCapabilityId& capability) {
if (capability.type == PluginCapabilityType::ResourcePrinterAsset)
PluginPrinterAssets::instance().on_cap_register(capability);
});
manager.subscribe_on_capability_unload_callback([](const PluginCapabilityId& capability) {
if (capability.type == PluginCapabilityType::ResourcePrinterAsset)
PluginPrinterAssets::instance().on_cap_deregister(capability);
});
}
std::string printer_asset_script_header(const std::string& display_name)
{
return std::string{}
+ "# /// script\n"
+ "# requires-python = \">=3.12\"\n"
+ "# dependencies = []\n"
+ "#\n"
+ "# [tool.orcaslicer.plugin]\n"
+ "# name = \"" + display_name + "\"\n"
+ "# description = \"Printer asset pack fixture.\"\n"
+ "# author = \"tests\"\n"
+ "# version = \"1.0.0\"\n"
+ "# type = \"resource-printer-asset\"\n"
+ "# ///\n";
}
// A minimal printer asset pack shaped like the wheel Orca Cloud builds (pa/__init__.py + pa/a/<file>).
std::string make_printer_asset_source(const std::string& display_name, const std::string& get_asset_dir_body)
{
return printer_asset_script_header(display_name)
+ "import os\n"
+ "import orca\n"
+ "\n\n"
+ "class PrinterAssets(orca.resource_printer_asset.ResourcePrinterAssetPluginCapabilityBase):\n"
+ " def get_name(self):\n"
+ " return \"" + display_name + "\"\n"
+ "\n"
+ " def get_asset_dir(self):\n"
+ " " + get_asset_dir_body + "\n"
+ "\n\n"
+ "@orca.plugin\n"
+ "class PrinterAssetPack(orca.base):\n"
+ " def register_capabilities(self):\n"
+ " orca.register_capability(PrinterAssets)\n";
}
// One plugin with two capabilities, "Voron Bed" over folder a and "Voron Plate" over folder b.
std::string make_two_capability_source()
{
const std::string base = "orca.resource_printer_asset.ResourcePrinterAssetPluginCapabilityBase";
return printer_asset_script_header("Voron Packs")
+ "import os\n"
+ "import orca\n"
+ "\n\n"
+ "class BedPack(" + base + "):\n"
+ " def get_name(self):\n"
+ " return \"Voron Bed\"\n"
+ "\n"
+ " def get_asset_dir(self):\n"
+ " return os.path.join(os.path.dirname(__file__), \"a\")\n"
+ "\n\n"
+ "class PlatePack(" + base + "):\n"
+ " def get_name(self):\n"
+ " return \"Voron Plate\"\n"
+ "\n"
+ " def get_asset_dir(self):\n"
+ " return os.path.join(os.path.dirname(__file__), \"b\")\n"
+ "\n\n"
+ "@orca.plugin\n"
+ "class PrinterAssetPack(orca.base):\n"
+ " def register_capabilities(self):\n"
+ " orca.register_capability(BedPack)\n"
+ " orca.register_capability(PlatePack)\n";
}
std::string default_asset_dir_body() { return "return os.path.join(os.path.dirname(__file__), \"a\")"; }
fs::path write_plugin(const ScopedDataDir& data_dir_guard, const std::string& stem, const std::string& source)
{
const fs::path plugin_dir = data_dir_guard.plugins_dir() / stem;
fs::create_directories(plugin_dir);
std::ofstream out((plugin_dir / (stem + ".py")).string(), std::ios::binary);
out << source;
return plugin_dir;
}
void write_text_file(const fs::path& path, const std::string& content = "x")
{
fs::create_directories(path.parent_path());
std::ofstream out(path.string(), std::ios::binary);
out << content;
}
bool load_and_wait(PluginManager& manager, const std::string& plugin_key, std::string& error)
{
manager.load_plugin(plugin_key, /*skip_deps=*/true);
return manager.wait_for_plugin_load(plugin_key, std::chrono::seconds(120), error);
}
std::vector<std::string> filenames(const std::vector<std::string>& paths)
{
std::vector<std::string> names;
for (const std::string& path : paths)
names.push_back(fs::path(path).filename().string());
std::sort(names.begin(), names.end());
return names;
}
// Loads one local pack "Voron Bed" (plugin key Voron_Bed) with a model, a texture and a stray file.
std::string load_voron_pack(const ScopedDataDir& data_dir_guard)
{
const std::string plugin_key = "Voron_Bed";
const fs::path plugin_dir = write_plugin(data_dir_guard, plugin_key, make_printer_asset_source("Voron Bed", default_asset_dir_body()));
write_text_file(plugin_dir / "a" / "bed.stl");
write_text_file(plugin_dir / "a" / "plate.svg");
write_text_file(plugin_dir / "a" / "readme.txt");
PluginManager& manager = PluginManager::instance();
install_printer_asset_wiring();
manager.discover_plugins(/*async=*/false, /*clear=*/true);
std::string error;
const bool loaded = load_and_wait(manager, plugin_key, error);
INFO("load error: " << error);
REQUIRE(loaded);
return plugin_key;
}
} // namespace
// ── Validation (collect_printer_asset_files, no Python) ─────────────────────────────────────────
TEST_CASE("An asset folder lists only its .stl, .png and .svg files", "[PluginPrinterAsset]")
{
ScopedTemporaryDir root("asset-root");
write_text_file(root.path() / "a" / "bed.stl");
write_text_file(root.path() / "a" / "plate.PNG");
write_text_file(root.path() / "a" / "plate.svg");
write_text_file(root.path() / "a" / "notes.txt");
write_text_file(root.path() / "a" / "photo.jpg");
write_text_file(root.path() / "a" / "nested" / "deep.stl");
std::string error;
const std::vector<std::string> files = collect_printer_asset_files((root.path() / "a").string(), root.string(), error);
CHECK(error.empty());
CHECK(filenames(files) == std::vector<std::string>{"bed.stl", "plate.PNG", "plate.svg"});
}
TEST_CASE("An asset folder outside the plugin folder is refused", "[PluginPrinterAsset]")
{
ScopedTemporaryDir parent("asset-escape");
const fs::path root = parent.path() / "pack";
const fs::path outside = parent.path() / "outside";
fs::create_directories(root);
write_text_file(outside / "bed.stl");
std::string error;
CHECK(collect_printer_asset_files((root / ".." / "outside").string(), root.string(), error).empty());
CHECK_FALSE(error.empty());
CHECK(collect_printer_asset_files("", root.string(), error).empty());
CHECK_FALSE(error.empty());
}
TEST_CASE("The asset listing stops at 16 files", "[PluginPrinterAsset]")
{
ScopedTemporaryDir root("asset-cap");
for (int i = 0; i < 20; ++i)
write_text_file(root.path() / ("plate_" + std::to_string(i) + ".stl"));
// Sorted before the cap, so every file system keeps the same 16.
std::string error;
const std::vector<std::string> files = collect_printer_asset_files(root.string(), root.string(), error);
REQUIRE(files.size() == 16);
CHECK(std::is_sorted(files.begin(), files.end()));
CHECK(fs::path(files.back()).filename() == "plate_5.stl");
}
TEST_CASE("Names a reference cannot hold do not take a slot in the 16-file cap", "[PluginPrinterAsset]")
{
ScopedTemporaryDir root("asset-names");
// Spaces and device names sort before the valid files, so they would fill the cap first.
for (int i = 0; i < 16; ++i)
write_text_file(root.path() / ("a plate " + std::to_string(i) + ".stl"));
write_text_file(root.path() / "con.stl");
write_text_file(root.path() / "plate_a.stl");
write_text_file(root.path() / "plate_b.png");
std::string error;
const std::vector<std::string> files = collect_printer_asset_files(root.string(), root.string(), error);
CHECK(error.empty());
CHECK(filenames(files) == std::vector<std::string>{"plate_a.stl", "plate_b.png"});
}
TEST_CASE("A symlink pointing outside the plugin folder is skipped, not reported as an error", "[PluginPrinterAsset]")
{
ScopedTemporaryDir root("asset-symlink-root");
ScopedTemporaryDir target("asset-symlink-target");
write_text_file(target.path() / "secret.stl");
write_text_file(root.path() / "a" / "bed.stl");
const fs::path link = root.path() / "a" / "link.stl";
boost::system::error_code ec;
fs::create_symlink(target.path() / "secret.stl", link, ec);
if (ec)
SKIP("symlinks unavailable on this filesystem: " + ec.message());
std::string error;
const std::vector<std::string> files = collect_printer_asset_files((root.path() / "a").string(), root.string(), error);
// The folder itself is legitimate; the escaping link is dropped, not an error.
CHECK(error.empty());
CHECK(filenames(files) == std::vector<std::string>{"bed.stl"});
}
TEST_CASE("A dangling symlink in the asset folder is skipped", "[PluginPrinterAsset]")
{
ScopedTemporaryDir root("asset-dangling");
write_text_file(root.path() / "a" / "bed.stl");
const fs::path link = root.path() / "a" / "gone.stl";
boost::system::error_code ec;
fs::create_symlink(root.path() / "a" / "missing.stl", link, ec);
if (ec)
SKIP("symlinks unavailable on this filesystem: " + ec.message());
std::string error;
const std::vector<std::string> files = collect_printer_asset_files((root.path() / "a").string(), root.string(), error);
CHECK(error.empty());
CHECK(filenames(files) == std::vector<std::string>{"bed.stl"});
}
TEST_CASE("Plain bed paths resolve to themselves and packs that are not loaded to nothing", "[PluginPrinterAsset]")
{
const std::string missing_ref = "orcacloud-asset/3f2a6c1e-8d4b-4c2a-9e7f-1a2b3c4d5e6f/bed.stl";
CHECK(resolve_printer_asset_value("") == "");
CHECK(resolve_printer_asset_value("/Users/me/bed.stl") == "/Users/me/bed.stl");
CHECK(resolve_printer_asset_value(missing_ref).empty());
}
TEST_CASE("A printer's asset pack names follow its bed files, falling back to its manifest for packs not loaded here",
"[PluginPrinterAsset]")
{
std::unique_ptr<DynamicPrintConfig> config(DynamicPrintConfig::new_from_defaults_keys(Preset::printer_options()));
const auto names = [&config]() { return config->option<ConfigOptionStrings>("printer_asset_plugins")->values; };
// Plain paths name no pack, so a stale name is dropped.
config->option<ConfigOptionStrings>("printer_asset_plugins")->values = {"Old Pack"};
config->set_key_value("bed_custom_model", new ConfigOptionString("/Users/me/bed.stl"));
update_printer_asset_plugins(*config);
CHECK(names().empty());
// A pack that is not loaded here keeps the name its manifest entry (matched by uuid) holds.
config->set_key_value("bed_custom_model", new ConfigOptionString("orcacloud-asset/3f2a6c1e-8d4b-4c2a-9e7f-1a2b3c4d5e6f/bed.stl"));
config->set_key_value("bed_custom_texture", new ConfigOptionString("orcacloud-asset/9b1d0000-0000-4000-8000-000000000000/plate.png"));
config->option<ConfigOptionStrings>("printer_plugins")->values = {"Voron Bed;3F2A6C1E-8D4B-4C2A-9E7F-1A2B3C4D5E6F;Voron Bed by sam"};
update_printer_asset_plugins(*config);
// The texture's pack is neither loaded nor in the manifest: its name is unknown.
CHECK(names() == std::vector<std::string>{"Voron Bed by sam"});
// The saved preset's manifest names a pack this config's manifest has lost (e.g. bed files
// transferred from another preset). Entries carry no type: the plugin's other capability
// ("Voron Agent") loses to the name the config already lists as an asset pack.
config->option<ConfigOptionStrings>("printer_plugins")->values = {};
config->option<ConfigOptionStrings>("printer_asset_plugins")->values = {"Voron Bed by sam"};
const ConfigOptionStrings saved_manifest({"Voron Bed;3f2a6c1e-8d4b-4c2a-9e7f-1a2b3c4d5e6f;Voron Agent",
"Voron Bed;3f2a6c1e-8d4b-4c2a-9e7f-1a2b3c4d5e6f;Voron Bed by sam",
"Other Agent;77777777-0000-4000-8000-000000000000;Agent"});
update_printer_asset_plugins(*config, &saved_manifest);
CHECK(names() == std::vector<std::string>{"Voron Bed by sam"});
// Only the bed files' packs are taken over from the fallback, even without a resolver to rebuild
// the manifest.
const std::vector<std::string> manifest = config->option<ConfigOptionStrings>("printer_plugins")->values;
CHECK(std::find(manifest.begin(), manifest.end(), "Other Agent;77777777-0000-4000-8000-000000000000;Agent") == manifest.end());
CHECK(std::find(manifest.begin(), manifest.end(), "Voron Bed;3f2a6c1e-8d4b-4c2a-9e7f-1a2b3c4d5e6f;Voron Bed by sam") != manifest.end());
// Not a printer config: nothing to do.
std::unique_ptr<DynamicPrintConfig> print(DynamicPrintConfig::new_from_defaults_keys({"bed_custom_model"}));
update_printer_asset_plugins(*print);
CHECK(print->option("printer_asset_plugins") == nullptr);
}
TEST_CASE("A pack that is not loaded keeps its own manifest entry when another plugin has a capability of the same name",
"[PluginPrinterAsset]")
{
struct ScopedResolver
{
ScopedResolver()
{
ConfigBase::set_resolve_capability_fn([](const std::string& name, const std::string&) {
return name == "PEI Plate" ? std::string("Other Pack;aaaaaaaa-0000-4000-8000-000000000000;PEI Plate") : std::string();
});
}
~ScopedResolver() { ConfigBase::set_resolve_capability_fn(nullptr); }
} resolver;
// The bed file belongs to a pack that is not installed here. A loaded plugin has a capability with the
// same name, and the resolver finds capabilities by name alone.
std::unique_ptr<DynamicPrintConfig> config(DynamicPrintConfig::new_from_defaults_keys(Preset::printer_options()));
config->set_key_value("bed_custom_model", new ConfigOptionString("orcacloud-asset/3f2a6c1e-8d4b-4c2a-9e7f-1a2b3c4d5e6f/bed.stl"));
config->option<ConfigOptionStrings>("printer_asset_plugins")->values = {"PEI Plate"};
config->option<ConfigOptionStrings>("printer_plugins")->values = {"Voron Bed;3f2a6c1e-8d4b-4c2a-9e7f-1a2b3c4d5e6f;PEI Plate"};
update_printer_asset_plugins(*config);
CHECK(config->option<ConfigOptionStrings>("printer_asset_plugins")->values == std::vector<std::string>{"PEI Plate"});
CHECK(config->option<ConfigOptionStrings>("printer_plugins")->values ==
std::vector<std::string>{"Voron Bed;3f2a6c1e-8d4b-4c2a-9e7f-1a2b3c4d5e6f;PEI Plate"});
}
// ── Lifecycle (Python) ──────────────────────────────────────────────────────────────────────────
TEST_CASE("Registering a pack again reports no change unless its files or name did", "[PluginPrinterAsset][PluginLifecycle][Python]")
{
ScopedPluginManager plugin_system;
if (!plugin_system.initialized)
SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error());
ScopedDataDir data_dir_guard("asset-change-flag");
const std::string plugin_key = load_voron_pack(data_dir_guard);
PluginManager& manager = PluginManager::instance();
const PluginCapabilityId id{PluginCapabilityType::ResourcePrinterAsset, "Voron Bed", plugin_key};
// Rebuilding the same entry must not redraw the bed: a load registers a pack twice, once through
// the plugin callbacks and once through the capability's.
CHECK_FALSE(PluginPrinterAssets::instance().on_cap_register(id));
// A file added to the folder changes what a reference can resolve to, so it is a change.
const fs::path added = data_dir_guard.plugins_dir() / plugin_key / "a" / "bed_small.stl";
write_text_file(added);
CHECK(PluginPrinterAssets::instance().on_cap_register(id));
CHECK_FALSE(PluginPrinterAssets::instance().on_cap_register(id));
fs::remove(added);
CHECK(PluginPrinterAssets::instance().on_cap_register(id));
REQUIRE(manager.unload_plugin(plugin_key));
}
TEST_CASE("A pack's files resolve from references and stop resolving on disable and unload",
"[PluginPrinterAsset][PluginLifecycle][Python]")
{
ScopedPluginManager plugin_system;
if (!plugin_system.initialized)
SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error());
ScopedDataDir data_dir_guard("asset-lifecycle");
const std::string plugin_key = load_voron_pack(data_dir_guard);
PluginManager& manager = PluginManager::instance();
const std::vector<PrinterAssetPackEntry> packs = PluginPrinterAssets::instance().entries();
REQUIRE(packs.size() == 1);
CHECK(packs.front().display_name == "Voron Bed");
// A local pack has no cloud uuid, so references use its plugin key.
CHECK(packs.front().pack_id == plugin_key);
CHECK(filenames(packs.front().files) == std::vector<std::string>{"bed.stl", "plate.svg"});
const std::string model_ref = make_printer_asset_ref(plugin_key, "bed.stl");
const std::string model = resolve_printer_asset_value(model_ref);
CHECK(fs::path(model).filename() == "bed.stl");
CHECK(fs::exists(model));
// Only files the pack lists resolve; the reference can never leave the asset folder.
CHECK(resolve_printer_asset_value(make_printer_asset_ref(plugin_key, "readme.txt")).empty());
manager.set_capability_enabled({PluginCapabilityType::ResourcePrinterAsset, "Voron Bed", plugin_key}, false);
CHECK(PluginPrinterAssets::instance().entries().empty());
CHECK(resolve_printer_asset_value(model_ref).empty());
manager.set_capability_enabled({PluginCapabilityType::ResourcePrinterAsset, "Voron Bed", plugin_key}, true);
CHECK(resolve_printer_asset_value(model_ref) == model);
REQUIRE(manager.unload_plugin(plugin_key));
CHECK(PluginPrinterAssets::instance().entries().empty());
}
TEST_CASE("A printer preset names its pack in its own manifest, and an inactive pack prompts without blocking slicing",
"[PluginPrinterAsset][PluginResolver][Python]")
{
ScopedPluginManager plugin_system;
if (!plugin_system.initialized)
SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error());
ScopedDataDir data_dir_guard("asset-manifest");
const std::string plugin_key = load_voron_pack(data_dir_guard);
PluginManager& manager = PluginManager::instance();
// What the Bed Shape dialog stores: references to the pack's files. The pack's capability name and
// manifest entry follow from them. A file from a pack this machine knows nothing about adds no name
// but does not drop the loaded pack's.
std::unique_ptr<DynamicPrintConfig> config(DynamicPrintConfig::new_from_defaults_keys(Preset::printer_options()));
config->set_key_value("bed_custom_texture", new ConfigOptionString("orcacloud-asset/9b1d0000-0000-4000-8000-000000000000/plate.png"));
config->set_key_value("bed_custom_model", new ConfigOptionString(make_printer_asset_ref(plugin_key, "bed.stl")));
update_printer_asset_plugins(*config);
CHECK(config->option<ConfigOptionStrings>("printer_asset_plugins")->values == std::vector<std::string>{"Voron Bed"});
// The printer keeps its manifest apart from the process preset's "plugins", which the full print
// config would otherwise overwrite with it.
const std::vector<std::string> expected_manifest{plugin_key + ";;Voron Bed"};
CHECK(config->option("plugins") == nullptr);
CHECK(config->option<ConfigOptionStrings>("printer_plugins")->values == expected_manifest);
CHECK(config->plugin_manifest_key() == "printer_plugins");
Preset printer(Preset::TYPE_PRINTER, "Voron with bed pack");
printer.config = *config;
const auto refresh = [&printer]() {
refresh_missing_plugins(Preset::TYPE_PRINTER, printer.config.option<ConfigOptionStrings>("printer_plugins"), &printer);
};
refresh();
CHECK_FALSE(has_inactive_plugins());
// Turned off: offered for activation, never blocking slicing. The installed pack still names itself
// when the manifest has lost its entry.
manager.set_capability_enabled({PluginCapabilityType::ResourcePrinterAsset, "Voron Bed", plugin_key}, false);
refresh();
CHECK(has_inactive_plugins());
CHECK_FALSE(has_blocking_plugins());
{
DynamicPrintConfig stripped = *config;
stripped.option<ConfigOptionStrings>("printer_plugins")->values = {};
update_printer_asset_plugins(stripped);
CHECK(stripped.option<ConfigOptionStrings>("printer_asset_plugins")->values == std::vector<std::string>{"Voron Bed"});
CHECK(stripped.option<ConfigOptionStrings>("printer_plugins")->values == expected_manifest);
}
// Unloaded, as on a machine where the pack is not installed yet: saving the preset there keeps the
// pack's name and the manifest entry other machines need.
REQUIRE(manager.unload_plugin(plugin_key));
update_printer_asset_plugins(*config);
CHECK(config->option<ConfigOptionStrings>("printer_asset_plugins")->values == std::vector<std::string>{"Voron Bed"});
CHECK(config->option<ConfigOptionStrings>("printer_plugins")->values == expected_manifest);
printer.config = *config;
refresh();
CHECK(has_inactive_plugins());
CHECK_FALSE(has_blocking_plugins());
refresh_missing_plugins(Preset::TYPE_PRINTER, nullptr);
CHECK_FALSE(has_inactive_plugins());
}
TEST_CASE("A pack whose get_asset_dir raises or leaves the plugin folder provides no files",
"[PluginPrinterAsset][Python]")
{
ScopedPluginManager plugin_system;
if (!plugin_system.initialized)
SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error());
ScopedDataDir data_dir_guard("asset-hostile");
ScopedTemporaryDir elsewhere("asset-elsewhere");
write_text_file(elsewhere.path() / "bed.stl");
write_plugin(data_dir_guard, "Raising_Bed", make_printer_asset_source("Raising Bed", "raise RuntimeError(\"no assets today\")"));
write_plugin(data_dir_guard, "Outside_Bed",
make_printer_asset_source("Outside Bed", "return r\"" + elsewhere.path().string() + "\""));
PluginManager& manager = PluginManager::instance();
install_printer_asset_wiring();
manager.discover_plugins(/*async=*/false, /*clear=*/true);
std::string error;
CHECK(load_and_wait(manager, "Raising_Bed", error));
CHECK(load_and_wait(manager, "Outside_Bed", error));
CHECK(PluginPrinterAssets::instance().entries().empty());
CHECK(resolve_printer_asset_value(make_printer_asset_ref("Outside_Bed", "bed.stl")).empty());
}
TEST_CASE("The capability name of a pack follows the file a reference names", "[PluginPrinterAsset][Python]")
{
ScopedPluginManager plugin_system;
if (!plugin_system.initialized)
SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error());
ScopedDataDir data_dir_guard("asset-two-capabilities");
const std::string plugin_key = "Voron_Packs";
const fs::path plugin_dir = write_plugin(data_dir_guard, plugin_key, make_two_capability_source());
write_text_file(plugin_dir / "a" / "bed.stl");
write_text_file(plugin_dir / "b" / "plate.svg");
PluginManager& manager = PluginManager::instance();
install_printer_asset_wiring();
manager.discover_plugins(/*async=*/false, /*clear=*/true);
std::string error;
const bool loaded = load_and_wait(manager, plugin_key, error);
INFO("load error: " << error);
REQUIRE(loaded);
REQUIRE(PluginPrinterAssets::instance().entries().size() == 2);
// Both capabilities belong to one plugin, so the plugin id alone cannot tell them apart.
const PluginPrinterAssets& packs = PluginPrinterAssets::instance();
CHECK(packs.capability_name(PrinterAssetRef{plugin_key, "bed.stl"}) == "Voron Bed");
CHECK(packs.capability_name(PrinterAssetRef{plugin_key, "plate.svg"}) == "Voron Plate");
CHECK(packs.capability_name(PrinterAssetRef{plugin_key, "missing.stl"}).empty());
}