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

Classes

struct  BedAssetSlot
struct  PendingBedAsset

Functions

std::string zip_slashes (std::string name)
bool bed_extension_allowed (const char *option, const std::string &ext)
std::string bed_asset_extension (const std::string &path, const char *option)
bool utf8_is_file (const std::string &path)
std::string bed_asset_dest_utf8 (const fs::path &dest)
bool accepted_bed_zip_name (const std::string &raw, const BedAssetSlot &slot, const fs::path &bundle_dir, std::string &zip_name, std::string &folder)
std::vector< PendingBedAsset > pending_bed_assets (const json &bundle_json, const fs::path &bundle_dir, std::unordered_set< std::string > &skip_names, const std::unordered_set< std::string > &zip_entries)
bool stage_bed_asset (mz_zip_archive &zip, PendingBedAsset &asset, const fs::path &temp_folder)
bool install_bed_asset (const PendingBedAsset &asset)
bool is_pending_bed_asset_entry (const std::string &entry, const std::unordered_set< std::string > &names)
bool printer_kept_bed_paths (const PresetCollection &printers, const std::map< std::string, std::string > &paths)

Variables

constexpr BedAssetSlot k_bed_asset_slots []

Class Documentation

◆ Slic3r::anonymous_namespace{PresetBundle.cpp}::BedAssetSlot

struct Slic3r::anonymous_namespace{PresetBundle.cpp}::BedAssetSlot
Class Members
const char * option
const char * stem

◆ Slic3r::anonymous_namespace{PresetBundle.cpp}::PendingBedAsset

struct Slic3r::anonymous_namespace{PresetBundle.cpp}::PendingBedAsset
Collaboration diagram for Slic3r::anonymous_namespace{PresetBundle.cpp}::PendingBedAsset:
Class Members
string dest
path final_path
string option
string printer_key
path staged
string zip_name

Function Documentation

◆ accepted_bed_zip_name()

bool Slic3r::anonymous_namespace{PresetBundle.cpp}::accepted_bed_zip_name ( const std::string & raw,
const BedAssetSlot & slot,
const fs::path & bundle_dir,
std::string & zip_name,
std::string & folder )
2175{
2176 const std::string name = zip_slashes(raw);
2177 if (name.find("..") != std::string::npos)
2178 return false;
2179 if (name.size() < 8 || name.compare(0, 4, "bed/") != 0)
2180 return false;
2181 const auto slash = name.find('/', 4);
2182 if (slash == std::string::npos || slash == 4 || name.find('/', slash + 1) != std::string::npos)
2183 return false;
2184 const std::string digits = name.substr(4, slash - 4);
2185 for (unsigned char c : digits) {
2186 if (!std::isdigit(c))
2187 return false;
2188 }
2189 const std::string filename = name.substr(slash + 1);
2190 const std::string ext = bed_asset_extension(filename, slot.option);
2191 if (ext.empty() || filename != std::string(slot.stem) + ext)
2192 return false;
2193 if (!is_path_within_root(name, bundle_dir))
2194 return false;
2195 zip_name = name;
2196 folder = digits;
2197 return true;
2198}
const char * stem
Definition PresetBundle.cpp:2107
std::string zip_slashes(std::string name)
Definition PresetBundle.cpp:2115
std::string bed_asset_extension(const std::string &path, const char *option)
Definition PresetBundle.cpp:2134
const char * option
Definition PresetBundle.cpp:2106
bool is_path_within_root(const std::string &rel_path, const boost::filesystem::path &root)
Definition utils.cpp:1219

References accepted_bed_zip_name(), bed_asset_extension(), Slic3r::is_path_within_root(), Slic3r::anonymous_namespace{PresetBundle.cpp}::BedAssetSlot::option, Slic3r::anonymous_namespace{PresetBundle.cpp}::BedAssetSlot::stem, and zip_slashes().

Referenced by accepted_bed_zip_name(), and pending_bed_assets().

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

◆ bed_asset_dest_utf8()

std::string Slic3r::anonymous_namespace{PresetBundle.cpp}::bed_asset_dest_utf8 ( const fs::path & dest)
2164{
2165#ifdef _WIN32
2166 return boost::nowide::narrow(dest.wstring());
2167#else
2168 return dest.string();
2169#endif
2170}

References bed_asset_dest_utf8().

Referenced by bed_asset_dest_utf8(), pending_bed_assets(), and stage_bed_asset().

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

◆ bed_asset_extension()

std::string Slic3r::anonymous_namespace{PresetBundle.cpp}::bed_asset_extension ( const std::string & path,
const char * option )
2135{
2136 const auto slash = path.find_last_of("/\\");
2137 const auto base = slash == std::string::npos ? 0 : slash + 1;
2138 const auto dot = path.find_last_of('.');
2139 if (dot == std::string::npos || dot < base || dot + 1 >= path.size())
2140 return {};
2141 const std::string ext = path.substr(dot);
2142 if (!bed_extension_allowed(option, ext))
2143 return {};
2144 return ext;
2145}
bool bed_extension_allowed(const char *option, const std::string &ext)
Definition PresetBundle.cpp:2124
T dot(const boost::geometry::model::d2::point_xy< T > &v1, const boost::geometry::model::d2::point_xy< T > &v2)
Definition ExtrusionSimulator.cpp:154

References bed_asset_extension(), bed_extension_allowed(), and Slic3r::dot().

Referenced by accepted_bed_zip_name(), and bed_asset_extension().

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

◆ bed_extension_allowed()

bool Slic3r::anonymous_namespace{PresetBundle.cpp}::bed_extension_allowed ( const char * option,
const std::string & ext )
2125{
2126 if (std::string(option) == "bed_custom_texture")
2127 return boost::iequals(ext, ".png") || boost::iequals(ext, ".svg");
2128 if (std::string(option) == "bed_custom_model")
2129 return boost::iequals(ext, ".stl");
2130 return false;
2131}

References bed_extension_allowed().

Referenced by bed_asset_extension(), and bed_extension_allowed().

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

◆ install_bed_asset()

bool Slic3r::anonymous_namespace{PresetBundle.cpp}::install_bed_asset ( const PendingBedAsset & asset)
2279{
2280 boost::system::error_code ec;
2281 fs::create_directories(asset.final_path.parent_path(), ec);
2282 if (!ec)
2283 fs::copy_file(asset.staged, asset.final_path, fs::copy_options::overwrite_existing, ec);
2284 if (ec) {
2285 BOOST_LOG_TRIVIAL(warning) << "Printer bundle asset could not be installed: " << asset.dest;
2286 return false;
2287 }
2288 return true;
2289}
fs::path final_path
Definition PresetBundle.cpp:2205
fs::path staged
Definition PresetBundle.cpp:2206
std::string dest
Definition PresetBundle.cpp:2203

References Slic3r::anonymous_namespace{PresetBundle.cpp}::PendingBedAsset::dest, Slic3r::anonymous_namespace{PresetBundle.cpp}::PendingBedAsset::final_path, install_bed_asset(), and Slic3r::anonymous_namespace{PresetBundle.cpp}::PendingBedAsset::staged.

Referenced by install_bed_asset().

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

◆ is_pending_bed_asset_entry()

bool Slic3r::anonymous_namespace{PresetBundle.cpp}::is_pending_bed_asset_entry ( const std::string & entry,
const std::unordered_set< std::string > & names )
2292{
2293 return names.count(entry) != 0 || names.count(zip_slashes(entry)) != 0;
2294}

References is_pending_bed_asset_entry(), and zip_slashes().

Referenced by is_pending_bed_asset_entry().

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

◆ pending_bed_assets()

std::vector< PendingBedAsset > Slic3r::anonymous_namespace{PresetBundle.cpp}::pending_bed_assets ( const json & bundle_json,
const fs::path & bundle_dir,
std::unordered_set< std::string > & skip_names,
const std::unordered_set< std::string > & zip_entries )
2215{
2216 std::vector<PendingBedAsset> pending;
2217 if (!bundle_json.contains("printer_asset") || !bundle_json["printer_asset"].is_object())
2218 return pending;
2219 const json &assets = bundle_json["printer_asset"];
2220 for (auto it = assets.begin(); it != assets.end(); ++it) {
2221 const std::string printer_key = it.key();
2222 if (zip_entries.count(printer_key) == 0) {
2223 BOOST_LOG_TRIVIAL(warning) << "Printer bundle asset entry is ignored, no matching printer in the zip: " << printer_key;
2224 continue;
2225 }
2226 if (!it.value().is_object())
2227 continue;
2228 const json &per_printer = it.value();
2229 for (const BedAssetSlot &slot : k_bed_asset_slots) {
2230 if (!per_printer.contains(slot.option) || !per_printer[slot.option].is_string())
2231 continue;
2232 const std::string raw = per_printer[slot.option].get<std::string>();
2233 std::string zip_name;
2234 std::string folder;
2235 if (!accepted_bed_zip_name(raw, slot, bundle_dir, zip_name, folder)) {
2236 BOOST_LOG_TRIVIAL(warning) << "Printer bundle asset path is rejected: " << raw;
2237 continue;
2238 }
2239 const std::string filename = zip_name.substr(zip_name.rfind('/') + 1);
2240 const fs::path final_path = fs::absolute(bundle_dir / "bed" / folder / filename);
2241 pending.push_back({slot.option, zip_name, bed_asset_dest_utf8(final_path), printer_key, final_path, {}});
2242 skip_names.insert(zip_name);
2243 }
2244 }
2245 return pending;
2246}
constexpr BedAssetSlot k_bed_asset_slots[]
Definition PresetBundle.cpp:2110
std::string bed_asset_dest_utf8(const fs::path &dest)
Definition PresetBundle.cpp:2163
bool accepted_bed_zip_name(const std::string &raw, const BedAssetSlot &slot, const fs::path &bundle_dir, std::string &zip_name, std::string &folder)
Definition PresetBundle.cpp:2173
nlohmann::json json
Definition AssembleList.cpp:42

References accepted_bed_zip_name(), bed_asset_dest_utf8(), k_bed_asset_slots, and pending_bed_assets().

Referenced by pending_bed_assets().

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

◆ printer_kept_bed_paths()

bool Slic3r::anonymous_namespace{PresetBundle.cpp}::printer_kept_bed_paths ( const PresetCollection & printers,
const std::map< std::string, std::string > & paths )
2297{
2298 if (paths.empty())
2299 return false;
2300 for (const Preset &preset : printers.get_presets()) {
2301 for (const auto &entry : paths)
2302 if (preset.config.opt_string(entry.first) == entry.second)
2303 return true;
2304 }
2305 return false;
2306}
const std::deque< Preset > & get_presets() const
Definition Preset.hpp:781
Definition Preset.hpp:241

References Slic3r::Preset::config, Slic3r::PresetCollection::get_presets(), Slic3r::DynamicConfig::opt_string(), and printer_kept_bed_paths().

Referenced by printer_kept_bed_paths().

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

◆ stage_bed_asset()

bool Slic3r::anonymous_namespace{PresetBundle.cpp}::stage_bed_asset ( mz_zip_archive & zip,
PendingBedAsset & asset,
const fs::path & temp_folder )
2250{
2251 size_t size = 0;
2252 void *buf = mz_zip_reader_extract_file_to_heap(&zip, asset.zip_name.c_str(), &size, MZ_ZIP_FLAG_CASE_SENSITIVE);
2253 if (buf == nullptr) {
2254 BOOST_LOG_TRIVIAL(warning) << "Printer bundle asset is missing, skipping " << asset.option << ": " << asset.zip_name;
2255 return false;
2256 }
2257 const std::string filename = asset.zip_name.substr(asset.zip_name.rfind('/') + 1);
2258 asset.staged = temp_folder / "bed" / asset.final_path.parent_path().filename() / filename;
2259 boost::system::error_code ec;
2260 fs::create_directories(asset.staged.parent_path(), ec);
2261 const std::string staged_utf8 = bed_asset_dest_utf8(asset.staged);
2262 bool wrote = false;
2263 if (!ec) {
2264 boost::nowide::ofstream out(staged_utf8, std::ios::binary | std::ios::trunc);
2265 wrote = static_cast<bool>(out);
2266 if (wrote && size > 0)
2267 wrote = static_cast<bool>(out.write(static_cast<const char *>(buf), static_cast<std::streamsize>(size)));
2268 }
2269 mz_free(buf);
2270 if (!wrote) {
2271 BOOST_LOG_TRIVIAL(warning) << "Printer bundle asset could not be written, skipping " << asset.option << ": " << staged_utf8;
2272 fs::remove(asset.staged, ec);
2273 return false;
2274 }
2275 return true;
2276}
std::string zip_name
Definition PresetBundle.cpp:2202
std::string option
Definition PresetBundle.cpp:2201

References bed_asset_dest_utf8(), Slic3r::anonymous_namespace{PresetBundle.cpp}::PendingBedAsset::final_path, Slic3r::anonymous_namespace{PresetBundle.cpp}::PendingBedAsset::option, stage_bed_asset(), Slic3r::anonymous_namespace{PresetBundle.cpp}::PendingBedAsset::staged, and Slic3r::anonymous_namespace{PresetBundle.cpp}::PendingBedAsset::zip_name.

Referenced by stage_bed_asset().

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

◆ utf8_is_file()

bool Slic3r::anonymous_namespace{PresetBundle.cpp}::utf8_is_file ( const std::string & path)
2148{
2149 if (path.empty())
2150 return false;
2151 boost::system::error_code ec;
2152#ifdef _WIN32
2153 // A narrow path is the ANSI code page unless nowide_filesystem() has run.
2154 const std::wstring wide = boost::nowide::widen(path);
2155 if (wide.empty())
2156 return false;
2157 return boost::filesystem::is_regular_file(boost::filesystem::path(wide), ec) && !ec;
2158#else
2159 return boost::filesystem::is_regular_file(path, ec) && !ec;
2160#endif
2161}

References utf8_is_file().

Referenced by utf8_is_file().

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

◆ zip_slashes()

std::string Slic3r::anonymous_namespace{PresetBundle.cpp}::zip_slashes ( std::string name)
2116{
2117 for (char &c : name)
2118 if (c == '\\')
2119 c = '/';
2120 return name;
2121}

References zip_slashes().

Referenced by accepted_bed_zip_name(), is_pending_bed_asset_entry(), and zip_slashes().

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

Variable Documentation

◆ k_bed_asset_slots

BedAssetSlot Slic3r::anonymous_namespace{PresetBundle.cpp}::k_bed_asset_slots[]
constexpr
Initial value:
= {
{"bed_custom_texture", "texture"},
{"bed_custom_model", "model"},
}
2110 {
2111 {"bed_custom_texture", "texture"},
2112 {"bed_custom_model", "model"},
2113};

Referenced by pending_bed_assets().