Simplify code a bit more

This commit is contained in:
ExPikaPaka
2026-08-05 21:14:55 +08:00
committed by SoftFever
parent 273d325066
commit 1d19e39625
5 changed files with 127 additions and 135 deletions
+2 -2
View File
@@ -94,7 +94,7 @@ int main(int argc, char* argv[])
const std::string ver_str = ver.valid() ? ver.to_string() : ""; const std::string ver_str = ver.valid() ? ver.to_string() : "";
const bool is_orca_lib = (vendor_name == PresetBundle::ORCA_FILAMENT_LIBRARY); 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); vc.capture(*preset_bundle, vendor_name, ver_str, is_orca_lib);
const std::string cache_path = const std::string cache_path =
@@ -102,7 +102,7 @@ int main(int argc, char* argv[])
vc.save(cache_path); vc.save(cache_path);
// Verify the file was written and can be reloaded. // 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)) { if (!verify.load(cache_path) || !verify.is_valid(ver_str)) {
std::cerr << "WARNING: verification failed for " << cache_path << "\n"; std::cerr << "WARNING: verification failed for " << cache_path << "\n";
++failed; ++failed;
+1 -1
View File
@@ -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) static void inspect_one(const std::string& path, const po::variables_map& vm)
{ {
Slic3r::VendorCache vc; Slic3r::PresetBundle::VendorCache vc;
if (!vc.load(path)) { if (!vc.load(path)) {
std::cerr << "Failed to load cache: " << path << "\n" std::cerr << "Failed to load cache: " << path << "\n"
<< " (wrong format version, truncated file, CRC mismatch, or not a .cache file)\n"; << " (wrong format version, truncated file, CRC mismatch, or not a .cache file)\n";
+11 -11
View File
@@ -6111,8 +6111,8 @@ static void save_blob(const std::string& path, const T& obj)
return; return;
} }
CacheFileHeader hdr; CacheFileHeader hdr;
hdr.magic = VendorCache::CACHE_MAGIC; hdr.magic = PresetBundle::VendorCache::CACHE_MAGIC;
hdr.version = VendorCache::CACHE_VERSION; hdr.version = PresetBundle::VendorCache::CACHE_VERSION;
hdr.data_size = static_cast<uint64_t>(blob.size()); hdr.data_size = static_cast<uint64_t>(blob.size());
hdr.crc32 = crc.checksum(); hdr.crc32 = crc.checksum();
ofs.write(reinterpret_cast<const char*>(&hdr), sizeof(hdr)); ofs.write(reinterpret_cast<const char*>(&hdr), sizeof(hdr));
@@ -6132,7 +6132,7 @@ static bool load_blob(const std::string& path, T& obj)
CacheFileHeader hdr; CacheFileHeader hdr;
if (!ifs.read(reinterpret_cast<char*>(&hdr), sizeof(hdr))) if (!ifs.read(reinterpret_cast<char*>(&hdr), sizeof(hdr)))
return false; 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; return false;
if (hdr.data_size == 0 || hdr.data_size > 512u * 1024u * 1024u) if (hdr.data_size == 0 || hdr.data_size > 512u * 1024u * 1024u)
return false; return false;
@@ -6157,29 +6157,29 @@ static bool load_blob(const std::string& path, T& obj)
} // anonymous namespace } // 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")) return (boost::filesystem::path(data_dir()) / PRESET_SYSTEM_DIR / (vendor_id + ".cache"))
.make_preferred().string(); .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")) return (boost::filesystem::path(resources_dir()) / "profiles" / (vendor_id + ".cache"))
.make_preferred().string(); .make_preferred().string();
} }
bool VendorCache::load(const std::string& path) bool PresetBundle::VendorCache::load(const std::string& path)
{ {
return load_blob(path, *this); 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); 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_id,
const std::string& vendor_json_ver, const std::string& vendor_json_ver,
bool capture_filament_maps) bool capture_filament_maps)
@@ -6214,7 +6214,7 @@ void VendorCache::capture(const PresetBundle& bundle,
cm.family = model.family; cm.family = model.family;
cm.technology = static_cast<int>(model.technology); cm.technology = static_cast<int>(model.technology);
for (const auto& v : model.variants) 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.default_materials = model.default_materials;
cm.not_support_bed_types = model.not_support_bed_types; cm.not_support_bed_types = model.not_support_bed_types;
cm.bed_model = model.bed_model; cm.bed_model = model.bed_model;
@@ -6270,7 +6270,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) // Restore vendor profile (additive — does not reset the bundle)
{ {
@@ -6290,7 +6290,7 @@ void VendorCache::apply(PresetBundle& bundle) const
model.family = cm.family; model.family = cm.family;
model.technology = static_cast<PrinterTechnology>(cm.technology); model.technology = static_cast<PrinterTechnology>(cm.technology);
for (const auto& v : cm.variants) for (const auto& v : cm.variants)
model.variants.emplace_back(v.name); model.variants.emplace_back(v);
model.default_materials = cm.default_materials; model.default_materials = cm.default_materials;
model.not_support_bed_types = cm.not_support_bed_types; model.not_support_bed_types = cm.not_support_bed_types;
model.bed_model = cm.bed_model; model.bed_model = cm.bed_model;
+18 -26
View File
@@ -173,20 +173,19 @@ struct PresetBundleMetadata
} }
}; };
// ---- Per-vendor binary preset cache ---------------------------------------- // Bundle of Print + Filament + Printer presets.
// One file per vendor: class PresetBundle
// Bundled (CI-generated): resources/profiles/<vendor_id>.cache {
// User (runtime): data_dir/system/<vendor_id>.cache public:
// ---- Per-vendor binary preset cache ------------------------------------
// One file per vendor:
// Bundled (CI-generated): resources/profiles/<vendor_id>.cache
// User (runtime): data_dir/system/<vendor_id>.cache
struct CachedPrinterVariant { struct CachedPrinterModel {
std::string name;
template<class Archive> void serialize(Archive& ar) { ar(name); }
};
struct CachedPrinterModel {
std::string id, name, model_id, family; std::string id, name, model_id, family;
int technology = 0; int technology = 0;
std::vector<CachedPrinterVariant> variants; std::vector<std::string> variants; // nozzle diameter strings
std::vector<std::string> default_materials; std::vector<std::string> default_materials;
std::vector<std::string> not_support_bed_types; std::vector<std::string> not_support_bed_types;
std::string bed_model, bed_texture, image_bed_type; std::string bed_model, bed_texture, image_bed_type;
@@ -201,9 +200,9 @@ struct CachedPrinterModel {
bottom_texture_end_name, use_double_extruder_default_texture, bottom_texture_end_name, use_double_extruder_default_texture,
bottom_texture_rect, middle_texture_rect, hotend_model); bottom_texture_rect, middle_texture_rect, hotend_model);
} }
}; };
struct CachedVendorProfile { struct CachedVendorProfile {
std::string id, name, config_version, config_update_url, changelog_url; std::string id, name, config_version, config_update_url, changelog_url;
std::vector<CachedPrinterModel> models; std::vector<CachedPrinterModel> models;
std::vector<std::string> default_filaments; std::vector<std::string> default_filaments;
@@ -215,9 +214,9 @@ struct CachedVendorProfile {
ar(id, name, config_version, config_update_url, changelog_url, ar(id, name, config_version, config_update_url, changelog_url,
models, default_filaments, default_sla_materials); models, default_filaments, default_sla_materials);
} }
}; };
struct CachedPreset { struct CachedPreset {
int type = 0; int type = 0;
std::string name, alias, file, version; std::string name, alias, file, version;
std::string vendor_id; std::string vendor_id;
@@ -237,9 +236,9 @@ struct CachedPreset {
description, base_id, user_id, sync_info, updated_time, renamed_from, description, base_id, user_id, sync_info, updated_time, renamed_from,
is_system, is_visible, m_from_orca_filament_lib, config); is_system, is_visible, m_from_orca_filament_lib, config);
} }
}; };
struct VendorCache { struct VendorCache {
static constexpr uint32_t CACHE_MAGIC = 0x4F52435A; // "ORCZ" static constexpr uint32_t CACHE_MAGIC = 0x4F52435A; // "ORCZ"
static constexpr uint32_t CACHE_VERSION = 3; static constexpr uint32_t CACHE_VERSION = 3;
@@ -268,7 +267,6 @@ struct VendorCache {
config_maps, filament_id_maps); 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 bool is_valid(const std::string& current_vendor_json_version) const
{ {
return cache_version == CACHE_VERSION return cache_version == CACHE_VERSION
@@ -282,21 +280,15 @@ struct VendorCache {
bool load(const std::string& path); bool load(const std::string& path);
void save(const std::string& path) const; 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, void capture(const PresetBundle& bundle,
const std::string& vendor_id, const std::string& vendor_id,
const std::string& vendor_json_version, const std::string& vendor_json_version,
bool capture_filament_maps); bool capture_filament_maps);
// Apply this vendor's data into bundle (additive — does NOT reset the bundle).
void apply(PresetBundle& bundle) const; void apply(PresetBundle& bundle) const;
}; };
// Bundle of Print + Filament + Printer presets.
class PresetBundle
{
public:
static DynamicPrintConfig construct_full_config(Preset &in_printer_preset, static DynamicPrintConfig construct_full_config(Preset &in_printer_preset,
Preset &in_print_preset, Preset &in_print_preset,
const DynamicPrintConfig &project_config, const DynamicPrintConfig &project_config,
+1 -1
View File
@@ -1397,7 +1397,7 @@ bool GuideFrame::BuildProfileDataFromBundledCache()
const Semver ver = get_version_from_json(json_path.string()); const Semver ver = get_version_from_json(json_path.string());
const std::string ver_str = ver.valid() ? ver.to_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)) if (!vc.load(cache_path.string()) || !vc.is_valid(ver_str))
continue; continue;