diff --git a/src/libslic3r/PresetBundle.cpp b/src/libslic3r/PresetBundle.cpp index 13c6110187..d65c49972c 100644 --- a/src/libslic3r/PresetBundle.cpp +++ b/src/libslic3r/PresetBundle.cpp @@ -422,58 +422,81 @@ std::string PresetBundle::find_preset_vendor(const std::string &preset_name, Pre return ""; } - // A vendor is named by its profile or, where the build ships preset caches - // instead of the raw profile JSONs, by its cache alone. - for (const std::string& vendor_name : vendor_names_in(system_dir)) { - const fs::path vendor_json = system_dir / (vendor_name + ".json"); - if (! fs::exists(vendor_json)) { - if (VendorCacheFile::carries_preset((system_dir / (vendor_name + ".opc")).string(), vendor_name, type, preset_name)) { - BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " Found preset " << preset_name - << " in vendor cache " << vendor_name; - return vendor_name; + // A preset's old names are kept in its own file, not in the vendor's list, so they are only read + // once no vendor lists the name itself. + auto renamed_from = [&preset_name](const fs::path &preset_file) { + boost::nowide::ifstream ifs(preset_file.string()); + const std::string text((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); + if (text.find(ORCA_JSON_KEY_RENAMED_FROM) == std::string::npos) + return false; + const json preset = json::parse(text, nullptr, false); + if (!preset.is_object() || !preset.contains(ORCA_JSON_KEY_RENAMED_FROM) || !preset[ORCA_JSON_KEY_RENAMED_FROM].is_string()) + return false; + std::vector names; + return unescape_strings_cstyle(preset[ORCA_JSON_KEY_RENAMED_FROM].get(), names) && + std::find(names.begin(), names.end(), preset_name) != names.end(); + }; + + for (const bool renamed : {false, true}) { + // A vendor is named by its profile or, where the build ships preset caches + // instead of the raw profile JSONs, by its cache alone. + for (const std::string& vendor_name : vendor_names_in(system_dir)) { + const fs::path vendor_json = system_dir / (vendor_name + ".json"); + if (! fs::exists(vendor_json)) { + if (VendorCacheFile::carries_preset((system_dir / (vendor_name + ".opc")).string(), vendor_name, type, preset_name, renamed)) { + BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " Found preset " << preset_name << (renamed ? " as an old name" : "") + << " in vendor cache " << vendor_name; + return vendor_name; + } + continue; } - continue; - } - const std::string vendor_file = vendor_json.string(); - BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " Checking vendor: " << vendor_file; + const std::string vendor_file = vendor_json.string(); + BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " Checking vendor: " << vendor_file; - try { - // Load and parse the vendor JSON file - boost::nowide::ifstream ifs(vendor_file); - json j; - ifs >> j; + try { + // Load and parse the vendor JSON file + boost::nowide::ifstream ifs(vendor_file); + json j; + ifs >> j; - // Check if the preset list key exists - if (!j.contains(preset_list_key)) - continue; - - auto& preset_list = j[preset_list_key]; - if (!preset_list.is_array()) - continue; - - // Search for the preset in the list - for (auto& preset_entry : preset_list) { - if (!preset_entry.is_object()) + // Check if the preset list key exists + if (!j.contains(preset_list_key)) continue; - // Get the preset name - std::string p_name; - if (preset_entry.contains(BBL_JSON_KEY_NAME) && preset_entry[BBL_JSON_KEY_NAME].is_string()) - p_name = preset_entry[BBL_JSON_KEY_NAME].get(); - - if (p_name != preset_name) + auto& preset_list = j[preset_list_key]; + if (!preset_list.is_array()) continue; - // Found the preset! Get the vendor name and install the entire bundle - BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " Found preset " << p_name + // Search for the preset in the list + for (auto& preset_entry : preset_list) { + if (!preset_entry.is_object()) + continue; + + if (renamed) { + if (!preset_entry.contains(BBL_JSON_KEY_SUB_PATH) || !preset_entry[BBL_JSON_KEY_SUB_PATH].is_string() || + !renamed_from(system_dir / vendor_name / preset_entry[BBL_JSON_KEY_SUB_PATH].get())) + continue; + } else { + // Get the preset name + std::string p_name; + if (preset_entry.contains(BBL_JSON_KEY_NAME) && preset_entry[BBL_JSON_KEY_NAME].is_string()) + p_name = preset_entry[BBL_JSON_KEY_NAME].get(); + + if (p_name != preset_name) + continue; + } + + // Found the preset! Get the vendor name and install the entire bundle + BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " Found preset " << preset_name << (renamed ? " as an old name" : "") << " in vendor bundle " << vendor_name; - - return vendor_name; + + return vendor_name; + } + } + catch (const std::exception &e) { + BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << " Failed to find vendor name for " << preset_name << ": " << e.what(); + return ""; } - } - catch (const std::exception &e) { - BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << " Failed to find vendor name for " << preset_name << ": " << e.what(); - return ""; } } @@ -683,6 +706,8 @@ const PresetBundle *PresetBundle::load_source_vendor(const boost::filesystem::pa error = "Vendor bundle contains invalid presets"; return nullptr; } + // Lets find_preset() resolve a name a preset was renamed from. + bundle->update_system_maps(); return m_source_vendor_bundles.emplace(std::move(key), std::move(bundle)).first->second.get(); } diff --git a/src/libslic3r/PresetCacheFormat.cpp b/src/libslic3r/PresetCacheFormat.cpp index 234981c1c5..0a2d82cca7 100644 --- a/src/libslic3r/PresetCacheFormat.cpp +++ b/src/libslic3r/PresetCacheFormat.cpp @@ -567,7 +567,7 @@ Semver VendorCacheFile::usable_version(const std::string& path, const std::strin // static bool VendorCacheFile::carries_preset(const std::string& path, const std::string& vendor_name, - Preset::Type type, const std::string& preset_name) + Preset::Type type, const std::string& preset_name, bool renamed) { std::string blob; if (! read_cache_blob(path, blob)) @@ -581,7 +581,7 @@ bool VendorCacheFile::carries_preset(const std::string& path, const std::string& dict.load(ar); VendorMap vendors; ar(vendors); - // Reused: every entry overwrites it, and only its name is ever looked at. + // Reused: every entry overwrites it, and only its names are ever looked at. CachedPreset entry; // Written in this order by save. The list that could carry the preset // is the last one worth reading. @@ -590,7 +590,8 @@ bool VendorCacheFile::carries_preset(const std::string& path, const std::string& ar(cnt); for (uint32_t i = 0; i < cnt; ++ i) { visit_entry(ar, entry, [&] { skip_config(ar, dict); }); - if (kind == type && entry.name == preset_name) + if (kind == type && (renamed ? std::find(entry.renamed_from.begin(), entry.renamed_from.end(), preset_name) != entry.renamed_from.end() : + entry.name == preset_name)) return true; } if (kind == type) diff --git a/src/libslic3r/PresetCacheFormat.hpp b/src/libslic3r/PresetCacheFormat.hpp index f259f3cb4f..c6e5fcb65a 100644 --- a/src/libslic3r/PresetCacheFormat.hpp +++ b/src/libslic3r/PresetCacheFormat.hpp @@ -193,9 +193,10 @@ public: // read. The three kinds are written in one stream, so reaching the machines // means reading past the processes and filaments — their configs are consumed // and dropped rather than built. This is how a build that ships caches instead - // of preset JSONs answers "which vendor carries this preset?". + // of preset JSONs answers "which vendor carries this preset?". With `renamed` + // it answers whether a preset was renamed from `preset_name` instead. static bool carries_preset(const std::string& path, const std::string& vendor_name, - Preset::Type type, const std::string& preset_name); + Preset::Type type, const std::string& preset_name, bool renamed = false); }; } // namespace Slic3r diff --git a/tests/libslic3r/test_preset_bundle_loading.cpp b/tests/libslic3r/test_preset_bundle_loading.cpp index 0ae438f2fe..dc5325d725 100644 --- a/tests/libslic3r/test_preset_bundle_loading.cpp +++ b/tests/libslic3r/test_preset_bundle_loading.cpp @@ -6907,8 +6907,9 @@ struct ScopedResourcesDir ~ScopedResourcesDir() { set_resources_dir(previous); } }; -// An "Acme" vendor under root whose "Acme Printer" inherits extruder_clearance_dist_to_rod from an -// abstract base, with the printer in a nested sub_path so the name cannot be derived from the file. +// An "Acme" vendor under root whose "Acme Printer", formerly "Acme Classic", inherits +// extruder_clearance_dist_to_rod from an abstract base, with the printer in a nested sub_path so the +// name cannot be derived from the file. void write_acme_printer_vendor(const fs::path &root, double dist_to_rod) { const fs::path machine_dir = root / "Acme" / "machine"; @@ -6924,8 +6925,8 @@ void write_acme_printer_vendor(const fs::path &root, double dist_to_rod) << R"({"type":"machine","name":"fdm_acme_common","from":"system","instantiation":"false",)" << R"("extruder_clearance_dist_to_rod":")" << dist_to_rod << R"("})"; std::ofstream((machine_dir / "nested" / "printer.json").string()) - << R"({"type":"machine","name":"Acme Printer","from":"system","instantiation":"true","inherits":"fdm_acme_common",)" - << R"("printer_model":"Acme One","printer_variant":"0.4"})"; + << R"({"type":"machine","name":"Acme Printer","renamed_from":"Acme Classic","from":"system","instantiation":"true",)" + << R"("inherits":"fdm_acme_common","printer_model":"Acme One","printer_variant":"0.4"})"; } std::string read_file(const fs::path &file) @@ -7908,6 +7909,35 @@ TEST_CASE("A system preset resolves from a vendor shipped as its preset cache al CHECK_THAT(config.opt_float("extruder_clearance_dist_to_rod"), Catch::Matchers::WithinAbs(33., 1e-6)); } +TEST_CASE("A system preset resolves by a name it was renamed from", "[Preset][Bundle]") +{ + ScopedTemporaryDir temp_dir; + ScopedDataDir data(temp_dir.path() / "data"); + ScopedResourcesDir resources(temp_dir.path() / "resources"); + const fs::path profiles = temp_dir.path() / "resources" / "profiles"; + write_acme_printer_vendor(profiles, 33.); + + SECTION("from the profile JSONs") {} + SECTION("from a vendor shipped as its preset cache alone") { + PresetBundle writer; + writer.set_generate_vendor_caches(true); + writer.load_vendor_configs_from_json(profiles.string(), "Acme", PresetBundle::LoadSystem, + ForwardCompatibilitySubstitutionRule::EnableSilent); + REQUIRE(fs::exists(profiles / "Acme.opc")); + fs::remove(profiles / "Acme.json"); + fs::remove_all(profiles / "Acme"); + } + + CHECK(PresetBundle::find_preset_vendor("Acme Classic", Preset::TYPE_PRINTER) == "Acme"); + PresetBundle bundle; + DynamicPrintConfig config; + std::string error; + REQUIRE(bundle.resolve_system_preset(config, Preset::TYPE_PRINTER, "Acme Classic", + ForwardCompatibilitySubstitutionRule::EnableSilent, error)); + CHECK(config.opt_string("printer_model") == "Acme One"); + CHECK_THAT(config.opt_float("extruder_clearance_dist_to_rod"), Catch::Matchers::WithinAbs(33., 1e-6)); +} + TEST_CASE("A system preset no vendor lists is not resolved", "[Preset][Bundle]") { ScopedTemporaryDir temp_dir;