OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::anonymous_namespace{PresetCacheFormat.cpp} Namespace Reference

Classes

struct  CacheFileHeader

Functions

std::string read_bounded_string (cereal::BinaryInputArchive &ar)
std::string read_cache_stamps (cereal::BinaryInputArchive &ar, const std::string &expected_vendor_name)
bool cache_covers_version (const std::string &cached, const Semver &on_disk)
template<class Archive, class Entry, class ConfigFn>
void visit_entry (Archive &ar, Entry &e, ConfigFn &&config)
void save_entries (cereal::BinaryOutputArchive &ar, const std::vector< CachedPreset > &entries, const CacheDictionary &dict)
void load_entries (cereal::BinaryInputArchive &ar, std::vector< CachedPreset > &entries, const CacheDictionary &dict)
bool read_cache_blob (const std::string &path, std::string &out_blob)
bool write_cache_blob (const std::string &path, const std::string &blob)

Variables

constexpr uint32_t CACHE_MAGIC = 0x4F52435A
constexpr uint32_t CACHE_VERSION = 2
constexpr uint32_t MAX_RESERVED_ENTRIES = 4096

Class Documentation

◆ Slic3r::anonymous_namespace{PresetCacheFormat.cpp}::CacheFileHeader

struct Slic3r::anonymous_namespace{PresetCacheFormat.cpp}::CacheFileHeader
Class Members
uint32_t crc32
uint64_t data_size
uint32_t magic
uint32_t version

Function Documentation

◆ cache_covers_version()

bool Slic3r::anonymous_namespace{PresetCacheFormat.cpp}::cache_covers_version ( const std::string & cached,
const Semver & on_disk )
323{
324 if (on_disk == Semver::inf())
325 return true; // before parsing `cached`: nothing exists that the stamp must cover
326 if (! on_disk.valid())
327 return false;
328 const auto cached_ver = Semver::parse(cached);
329 return cached_ver && *cached_ver >= on_disk;
330}
static const Semver inf()
Definition Semver.hpp:75
static boost::optional< Semver > parse(const std::string &str)
Definition Semver.hpp:63
bool valid() const
Definition Semver.hpp:155

References cache_covers_version(), Slic3r::Semver::inf(), Slic3r::Semver::parse(), and Slic3r::Semver::valid().

Referenced by cache_covers_version().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ load_entries()

void Slic3r::anonymous_namespace{PresetCacheFormat.cpp}::load_entries ( cereal::BinaryInputArchive & ar,
std::vector< CachedPreset > & entries,
const CacheDictionary & dict )
363{
364 uint32_t cnt = 0;
365 ar(cnt);
366 entries.clear();
367 entries.reserve(std::min(cnt, MAX_RESERVED_ENTRIES));
368 for (uint32_t i = 0; i < cnt; ++ i) {
369 CachedPreset e;
370 visit_entry(ar, e, [&] { load_config(ar, e.config_src, dict); });
371 entries.push_back(std::move(e));
372 }
373}
void visit_entry(Archive &ar, Entry &e, ConfigFn &&config)
Definition PresetCacheFormat.cpp:340
constexpr uint32_t MAX_RESERVED_ENTRIES
Definition PresetCacheFormat.cpp:349
void load_config(cereal::BinaryInputArchive &ar, DynamicPrintConfig &config, const CacheDictionary &dict)
Definition PresetCacheFormat.cpp:242
DynamicPrintConfig config_src
Definition PresetCacheFormat.hpp:127
Definition PresetCacheFormat.hpp:124

References Slic3r::CachedPreset::config_src, Slic3r::load_config(), load_entries(), MAX_RESERVED_ENTRIES, and visit_entry().

Referenced by load_entries().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_bounded_string()

std::string Slic3r::anonymous_namespace{PresetCacheFormat.cpp}::read_bounded_string ( cereal::BinaryInputArchive & ar)
283{
284 constexpr uint64_t MAX_STAMP_LEN = 1024;
285 cereal::size_type len = 0;
286 ar(cereal::make_size_tag(len));
287 if (uint64_t(len) > MAX_STAMP_LEN)
288 throw std::runtime_error("preset cache: string length out of bounds");
289 std::string s(size_t(len), '\0');
290 ar(cereal::binary_data(s.data(), size_t(len)));
291 return s;
292}

References read_bounded_string().

Referenced by read_bounded_string(), and read_cache_stamps().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_cache_blob()

bool Slic3r::anonymous_namespace{PresetCacheFormat.cpp}::read_cache_blob ( const std::string & path,
std::string & out_blob )
377{
378 try {
379 boost::nowide::ifstream ifs(path, std::ios::binary);
380 if (!ifs.is_open())
381 return false;
382 CacheFileHeader fhdr;
383 if (!ifs.read(reinterpret_cast<char*>(&fhdr), sizeof(fhdr)))
384 return false;
385 if (fhdr.magic != CACHE_MAGIC)
386 return false;
387 // data_size is 8 bytes from a file nothing has authenticated yet, and
388 // it is about to size an allocation. The body is the whole of the file
389 // behind the header — anything else is not a cache this build wrote.
390 ifs.seekg(0, std::ios::end);
391 const std::streamoff file_size = ifs.tellg();
392 if (file_size < std::streamoff(sizeof(fhdr)) ||
393 fhdr.data_size == 0 ||
394 fhdr.data_size != uint64_t(file_size) - sizeof(fhdr))
395 return false;
396 ifs.seekg(sizeof(fhdr), std::ios::beg);
397 out_blob.assign(fhdr.data_size, '\0');
398 if (!ifs.read(&out_blob[0], static_cast<std::streamsize>(fhdr.data_size)))
399 return false;
400 boost::crc_32_type crc;
401 crc.process_bytes(out_blob.data(), out_blob.size());
402 if (crc.checksum() != fhdr.crc32) {
403 BOOST_LOG_TRIVIAL(warning) << "VendorCacheFile: CRC mismatch: " << path;
404 return false;
405 }
406 return true;
407 } catch (const std::exception& e) {
408 BOOST_LOG_TRIVIAL(warning) << "VendorCacheFile: read failed (" << path << "): " << e.what();
409 return false;
410 }
411}
uint32_t magic
Definition PresetCacheFormat.cpp:258
constexpr uint32_t CACHE_MAGIC
Definition PresetCacheFormat.cpp:266
uint64_t data_size
Definition PresetCacheFormat.cpp:260
uint32_t crc32
Definition PresetCacheFormat.cpp:261

References CACHE_MAGIC, Slic3r::anonymous_namespace{PresetCacheFormat.cpp}::CacheFileHeader::crc32, Slic3r::anonymous_namespace{PresetCacheFormat.cpp}::CacheFileHeader::data_size, Slic3r::anonymous_namespace{PresetCacheFormat.cpp}::CacheFileHeader::magic, and read_cache_blob().

Referenced by read_cache_blob().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_cache_stamps()

std::string Slic3r::anonymous_namespace{PresetCacheFormat.cpp}::read_cache_stamps ( cereal::BinaryInputArchive & ar,
const std::string & expected_vendor_name )
298{
299 // The version is judged before anything variable-length is read: on a body
300 // that is not a per-vendor cache of this version, the bytes where a string
301 // length would sit may be arbitrary framing.
302 uint32_t cache_version = 0;
303 ar(cache_version);
304 if (cache_version != CACHE_VERSION)
305 return {};
306 const std::string vendor_name = read_bounded_string(ar);
307 const std::string vendor_version = read_bounded_string(ar);
308 if (vendor_name != expected_vendor_name)
309 return {};
310 return vendor_version;
311}
constexpr uint32_t CACHE_VERSION
Definition PresetCacheFormat.cpp:273
std::string read_bounded_string(cereal::BinaryInputArchive &ar)
Definition PresetCacheFormat.cpp:282

References CACHE_VERSION, read_bounded_string(), and read_cache_stamps().

Referenced by read_cache_stamps().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ save_entries()

void Slic3r::anonymous_namespace{PresetCacheFormat.cpp}::save_entries ( cereal::BinaryOutputArchive & ar,
const std::vector< CachedPreset > & entries,
const CacheDictionary & dict )
354{
355 ar(uint32_t(entries.size()));
356 for (const CachedPreset& e : entries)
357 visit_entry(ar, e, [&] { save_config(ar, e.config_src, dict); });
358}
void save_config(cereal::BinaryOutputArchive &ar, const DynamicPrintConfig &config, const CacheDictionary &dict)
Definition PresetCacheFormat.cpp:193

References Slic3r::save_config(), save_entries(), and visit_entry().

Referenced by save_entries().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ visit_entry()

template<class Archive, class Entry, class ConfigFn>
void Slic3r::anonymous_namespace{PresetCacheFormat.cpp}::visit_entry ( Archive & ar,
Entry & e,
ConfigFn && config )
341{
342 ar(e.name, e.sub_path);
343 config();
344 ar(e.inherits, e.includes, e.description, e.instantiation, e.setting_id, e.filament_id, e.renamed_from);
345}

References visit_entry().

Referenced by load_entries(), save_entries(), and visit_entry().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_cache_blob()

bool Slic3r::anonymous_namespace{PresetCacheFormat.cpp}::write_cache_blob ( const std::string & path,
const std::string & blob )
416{
417 boost::crc_32_type crc;
418 crc.process_bytes(blob.data(), blob.size());
419 // Written beside the target and moved into place: a cache is truncated and
420 // rewritten in full, so a write that dies partway would otherwise leave a
421 // header claiming more body than the file holds.
422 try {
423 boost::filesystem::create_directories(boost::filesystem::path(path).parent_path());
424 CacheFileHeader fhdr;
425 fhdr.magic = CACHE_MAGIC;
426 fhdr.version = CACHE_VERSION;
427 fhdr.data_size = static_cast<uint64_t>(blob.size());
428 fhdr.crc32 = crc.checksum();
429 const std::string_view header(reinterpret_cast<const char*>(&fhdr), sizeof(fhdr));
430 if (const std::error_code ec = write_file_atomically(path, { header, std::string_view(blob) }, /*binary=*/true)) {
431 BOOST_LOG_TRIVIAL(warning) << "VendorCacheFile: write failed (" << path << "): " << ec.message();
432 return false;
433 }
434 return true;
435 } catch (const std::exception& e) {
436 BOOST_LOG_TRIVIAL(warning) << "VendorCacheFile: write failed (" << path << "): " << e.what();
437 return false;
438 }
439}
uint32_t version
Definition PresetCacheFormat.cpp:259
std::error_code write_file_atomically(const std::string &path, std::initializer_list< std::string_view > chunks, bool binary)
Definition utils.cpp:796

References CACHE_MAGIC, CACHE_VERSION, Slic3r::anonymous_namespace{PresetCacheFormat.cpp}::CacheFileHeader::crc32, Slic3r::anonymous_namespace{PresetCacheFormat.cpp}::CacheFileHeader::data_size, Slic3r::anonymous_namespace{PresetCacheFormat.cpp}::CacheFileHeader::magic, Slic3r::anonymous_namespace{PresetCacheFormat.cpp}::CacheFileHeader::version, write_cache_blob(), and Slic3r::write_file_atomically().

Referenced by write_cache_blob().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ CACHE_MAGIC

uint32_t Slic3r::anonymous_namespace{PresetCacheFormat.cpp}::CACHE_MAGIC = 0x4F52435A
constexpr

◆ CACHE_VERSION

uint32_t Slic3r::anonymous_namespace{PresetCacheFormat.cpp}::CACHE_VERSION = 2
constexpr

◆ MAX_RESERVED_ENTRIES

uint32_t Slic3r::anonymous_namespace{PresetCacheFormat.cpp}::MAX_RESERVED_ENTRIES = 4096
constexpr

Referenced by load_entries().