From 2ab9e145255998ea75bccb668b873c5fbc850a0d Mon Sep 17 00:00:00 2001 From: ExPikaPaka Date: Wed, 17 Jun 2026 09:17:07 +0200 Subject: [PATCH] Simplify code a bit more --- src/dev-utils/generate_system_cache.cpp | 4 +- src/dev-utils/inspect_system_cache.cpp | 2 +- src/libslic3r/PresetBundle.cpp | 22 +-- src/libslic3r/PresetBundle.hpp | 232 ++++++++++++------------ src/slic3r/GUI/WebGuideDialog.cpp | 2 +- 5 files changed, 127 insertions(+), 135 deletions(-) diff --git a/src/dev-utils/generate_system_cache.cpp b/src/dev-utils/generate_system_cache.cpp index 6685f40037..0a4ac33c00 100644 --- a/src/dev-utils/generate_system_cache.cpp +++ b/src/dev-utils/generate_system_cache.cpp @@ -94,7 +94,7 @@ int main(int argc, char* argv[]) const std::string ver_str = ver.valid() ? ver.to_string() : ""; const bool is_orca_lib = (vendor_name == PresetBundle::ORCA_FILAMENT_LIBRARY); - Slic3r::VendorCache vc; + Slic3r::PresetBundle::VendorCache vc; vc.capture(*preset_bundle, vendor_name, ver_str, is_orca_lib); const std::string cache_path = @@ -102,7 +102,7 @@ int main(int argc, char* argv[]) vc.save(cache_path); // Verify the file was written and can be reloaded. - Slic3r::VendorCache verify; + Slic3r::PresetBundle::VendorCache verify; if (!verify.load(cache_path) || !verify.is_valid(ver_str)) { std::cerr << "WARNING: verification failed for " << cache_path << "\n"; ++failed; diff --git a/src/dev-utils/inspect_system_cache.cpp b/src/dev-utils/inspect_system_cache.cpp index 97c1ae1856..da1494cc77 100644 --- a/src/dev-utils/inspect_system_cache.cpp +++ b/src/dev-utils/inspect_system_cache.cpp @@ -13,7 +13,7 @@ static void print_bar(char c, int n) { std::cout << std::string(n, c) << "\n"; } static void inspect_one(const std::string& path, const po::variables_map& vm) { - Slic3r::VendorCache vc; + Slic3r::PresetBundle::VendorCache vc; if (!vc.load(path)) { std::cerr << "Failed to load cache: " << path << "\n" << " (wrong format version, truncated file, CRC mismatch, or not a .cache file)\n"; diff --git a/src/libslic3r/PresetBundle.cpp b/src/libslic3r/PresetBundle.cpp index 275b02225c..1eab8dfdfd 100644 --- a/src/libslic3r/PresetBundle.cpp +++ b/src/libslic3r/PresetBundle.cpp @@ -5716,8 +5716,8 @@ static void save_blob(const std::string& path, const T& obj) return; } CacheFileHeader hdr; - hdr.magic = VendorCache::CACHE_MAGIC; - hdr.version = VendorCache::CACHE_VERSION; + hdr.magic = PresetBundle::VendorCache::CACHE_MAGIC; + hdr.version = PresetBundle::VendorCache::CACHE_VERSION; hdr.data_size = static_cast(blob.size()); hdr.crc32 = crc.checksum(); ofs.write(reinterpret_cast(&hdr), sizeof(hdr)); @@ -5737,7 +5737,7 @@ static bool load_blob(const std::string& path, T& obj) CacheFileHeader hdr; if (!ifs.read(reinterpret_cast(&hdr), sizeof(hdr))) return false; - if (hdr.magic != VendorCache::CACHE_MAGIC || hdr.version != VendorCache::CACHE_VERSION) + if (hdr.magic != PresetBundle::VendorCache::CACHE_MAGIC || hdr.version != PresetBundle::VendorCache::CACHE_VERSION) return false; if (hdr.data_size == 0 || hdr.data_size > 512u * 1024u * 1024u) return false; @@ -5762,29 +5762,29 @@ static bool load_blob(const std::string& path, T& obj) } // anonymous namespace -std::string VendorCache::user_path(const std::string& vendor_id) +std::string PresetBundle::VendorCache::user_path(const std::string& vendor_id) { return (boost::filesystem::path(data_dir()) / PRESET_SYSTEM_DIR / (vendor_id + ".cache")) .make_preferred().string(); } -std::string VendorCache::bundled_path(const std::string& vendor_id) +std::string PresetBundle::VendorCache::bundled_path(const std::string& vendor_id) { return (boost::filesystem::path(resources_dir()) / "profiles" / (vendor_id + ".cache")) .make_preferred().string(); } -bool VendorCache::load(const std::string& path) +bool PresetBundle::VendorCache::load(const std::string& path) { return load_blob(path, *this); } -void VendorCache::save(const std::string& path) const +void PresetBundle::VendorCache::save(const std::string& path) const { save_blob(path, *this); } -void VendorCache::capture(const PresetBundle& bundle, +void PresetBundle::VendorCache::capture(const PresetBundle& bundle, const std::string& vendor_id, const std::string& vendor_json_ver, bool capture_filament_maps) @@ -5819,7 +5819,7 @@ void VendorCache::capture(const PresetBundle& bundle, cm.family = model.family; cm.technology = static_cast(model.technology); for (const auto& v : model.variants) - cm.variants.push_back({v.name}); + cm.variants.push_back(v.name); cm.default_materials = model.default_materials; cm.not_support_bed_types = model.not_support_bed_types; cm.bed_model = model.bed_model; @@ -5875,7 +5875,7 @@ void VendorCache::capture(const PresetBundle& bundle, } } -void VendorCache::apply(PresetBundle& bundle) const +void PresetBundle::VendorCache::apply(PresetBundle& bundle) const { // Restore vendor profile (additive — does not reset the bundle) { @@ -5895,7 +5895,7 @@ void VendorCache::apply(PresetBundle& bundle) const model.family = cm.family; model.technology = static_cast(cm.technology); for (const auto& v : cm.variants) - model.variants.emplace_back(v.name); + model.variants.emplace_back(v); model.default_materials = cm.default_materials; model.not_support_bed_types = cm.not_support_bed_types; model.bed_model = cm.bed_model; diff --git a/src/libslic3r/PresetBundle.hpp b/src/libslic3r/PresetBundle.hpp index 0eb29918a6..f77c9ed1d5 100644 --- a/src/libslic3r/PresetBundle.hpp +++ b/src/libslic3r/PresetBundle.hpp @@ -154,130 +154,122 @@ struct PresetBundleMetadata } }; -// ---- Per-vendor binary preset cache ---------------------------------------- -// One file per vendor: -// Bundled (CI-generated): resources/profiles/.cache -// User (runtime): data_dir/system/.cache - -struct CachedPrinterVariant { - std::string name; - template void serialize(Archive& ar) { ar(name); } -}; - -struct CachedPrinterModel { - std::string id, name, model_id, family; - int technology = 0; - std::vector variants; - std::vector default_materials; - std::vector not_support_bed_types; - std::string bed_model, bed_texture, image_bed_type; - std::string bottom_texture_end_name, use_double_extruder_default_texture; - std::string bottom_texture_rect, middle_texture_rect, hotend_model; - - template - void serialize(Archive& ar) - { - ar(id, name, model_id, family, technology, variants, default_materials, - not_support_bed_types, bed_model, bed_texture, image_bed_type, - bottom_texture_end_name, use_double_extruder_default_texture, - bottom_texture_rect, middle_texture_rect, hotend_model); - } -}; - -struct CachedVendorProfile { - std::string id, name, config_version, config_update_url, changelog_url; - std::vector models; - std::vector default_filaments; - std::vector default_sla_materials; - - template - void serialize(Archive& ar) - { - ar(id, name, config_version, config_update_url, changelog_url, - models, default_filaments, default_sla_materials); - } -}; - -struct CachedPreset { - int type = 0; - std::string name, alias, file, version; - std::string vendor_id; - std::string filament_id, setting_id, description; - std::string base_id, user_id, sync_info; - long long updated_time = 0; - std::vector renamed_from; - bool is_system = true; - bool is_visible = true; - bool m_from_orca_filament_lib = false; - DynamicPrintConfig config; - - template - void serialize(Archive& ar) - { - ar(type, name, alias, file, version, vendor_id, filament_id, setting_id, - description, base_id, user_id, sync_info, updated_time, renamed_from, - is_system, is_visible, m_from_orca_filament_lib, config); - } -}; - -struct VendorCache { - static constexpr uint32_t CACHE_MAGIC = 0x4F52435A; // "ORCZ" - static constexpr uint32_t CACHE_VERSION = 3; - - uint32_t cache_version = CACHE_VERSION; - size_t config_options_count = 0; - std::string vendor_json_version; // Semver string, or "" for version-less vendors - - CachedVendorProfile profile; - std::vector print_presets; - std::vector filament_presets; - std::vector printer_presets; - std::vector sla_print_presets; - std::vector sla_material_presets; - - // Only populated for the ORCA_FILAMENT_LIBRARY vendor. - std::map config_maps; - std::map filament_id_maps; - - template - void serialize(Archive& ar) - { - ar(cache_version, config_options_count, vendor_json_version, - profile, - print_presets, filament_presets, printer_presets, - sla_print_presets, sla_material_presets, - config_maps, filament_id_maps); - } - - // True when this cache entry is current and can be used without re-parsing JSON. - bool is_valid(const std::string& current_vendor_json_version) const - { - return cache_version == CACHE_VERSION - && config_options_count == print_config_def.options.size() - && vendor_json_version == current_vendor_json_version; - } - - static std::string user_path(const std::string& vendor_id); // data_dir/system/.cache - static std::string bundled_path(const std::string& vendor_id); // resources/profiles/.cache - - bool load(const std::string& path); - void save(const std::string& path) const; - - // Capture one vendor's data from an already-loaded PresetBundle. - // Set capture_filament_maps=true only for the ORCA_FILAMENT_LIBRARY vendor. - void capture(const PresetBundle& bundle, - const std::string& vendor_id, - const std::string& vendor_json_version, - bool capture_filament_maps); - - // Apply this vendor's data into bundle (additive — does NOT reset the bundle). - void apply(PresetBundle& bundle) const; -}; - // Bundle of Print + Filament + Printer presets. class PresetBundle { public: + // ---- Per-vendor binary preset cache ------------------------------------ + // One file per vendor: + // Bundled (CI-generated): resources/profiles/.cache + // User (runtime): data_dir/system/.cache + + struct CachedPrinterModel { + std::string id, name, model_id, family; + int technology = 0; + std::vector variants; // nozzle diameter strings + std::vector default_materials; + std::vector not_support_bed_types; + std::string bed_model, bed_texture, image_bed_type; + std::string bottom_texture_end_name, use_double_extruder_default_texture; + std::string bottom_texture_rect, middle_texture_rect, hotend_model; + + template + void serialize(Archive& ar) + { + ar(id, name, model_id, family, technology, variants, default_materials, + not_support_bed_types, bed_model, bed_texture, image_bed_type, + bottom_texture_end_name, use_double_extruder_default_texture, + bottom_texture_rect, middle_texture_rect, hotend_model); + } + }; + + struct CachedVendorProfile { + std::string id, name, config_version, config_update_url, changelog_url; + std::vector models; + std::vector default_filaments; + std::vector default_sla_materials; + + template + void serialize(Archive& ar) + { + ar(id, name, config_version, config_update_url, changelog_url, + models, default_filaments, default_sla_materials); + } + }; + + struct CachedPreset { + int type = 0; + std::string name, alias, file, version; + std::string vendor_id; + std::string filament_id, setting_id, description; + std::string base_id, user_id, sync_info; + long long updated_time = 0; + std::vector renamed_from; + bool is_system = true; + bool is_visible = true; + bool m_from_orca_filament_lib = false; + DynamicPrintConfig config; + + template + void serialize(Archive& ar) + { + ar(type, name, alias, file, version, vendor_id, filament_id, setting_id, + description, base_id, user_id, sync_info, updated_time, renamed_from, + is_system, is_visible, m_from_orca_filament_lib, config); + } + }; + + struct VendorCache { + static constexpr uint32_t CACHE_MAGIC = 0x4F52435A; // "ORCZ" + static constexpr uint32_t CACHE_VERSION = 3; + + uint32_t cache_version = CACHE_VERSION; + size_t config_options_count = 0; + std::string vendor_json_version; // Semver string, or "" for version-less vendors + + CachedVendorProfile profile; + std::vector print_presets; + std::vector filament_presets; + std::vector printer_presets; + std::vector sla_print_presets; + std::vector sla_material_presets; + + // Only populated for the ORCA_FILAMENT_LIBRARY vendor. + std::map config_maps; + std::map filament_id_maps; + + template + void serialize(Archive& ar) + { + ar(cache_version, config_options_count, vendor_json_version, + profile, + print_presets, filament_presets, printer_presets, + sla_print_presets, sla_material_presets, + config_maps, filament_id_maps); + } + + bool is_valid(const std::string& current_vendor_json_version) const + { + return cache_version == CACHE_VERSION + && config_options_count == print_config_def.options.size() + && vendor_json_version == current_vendor_json_version; + } + + static std::string user_path(const std::string& vendor_id); // data_dir/system/.cache + static std::string bundled_path(const std::string& vendor_id); // resources/profiles/.cache + + bool load(const std::string& path); + void save(const std::string& path) const; + + void capture(const PresetBundle& bundle, + const std::string& vendor_id, + const std::string& vendor_json_version, + bool capture_filament_maps); + + void apply(PresetBundle& bundle) const; + }; + + static DynamicPrintConfig construct_full_config(Preset &in_printer_preset, Preset &in_print_preset, const DynamicPrintConfig &project_config, diff --git a/src/slic3r/GUI/WebGuideDialog.cpp b/src/slic3r/GUI/WebGuideDialog.cpp index e9cdb6e66b..bfe0b2abbe 100644 --- a/src/slic3r/GUI/WebGuideDialog.cpp +++ b/src/slic3r/GUI/WebGuideDialog.cpp @@ -1403,7 +1403,7 @@ bool GuideFrame::BuildProfileDataFromBundledCache() const Semver ver = get_version_from_json(json_path.string()); const std::string ver_str = ver.valid() ? ver.to_string() : ""; - VendorCache vc; + PresetBundle::VendorCache vc; if (!vc.load(cache_path.string()) || !vc.is_valid(ver_str)) continue;