35namespace asset_system {
42 asset_library_root_(asset_library_root),
49 const_cast<bool &
>(is_read_only_) =
true;
62 catalog_collection_->has_unsaved_changes_ =
true;
68 catalog_collection_->has_unsaved_changes_ =
false;
73 if (catalog_collection_->catalog_definition_file_) {
74 for (
CatalogID catalog_id : catalog_collection_->deleted_catalogs_.keys()) {
75 catalog_collection_->catalog_definition_file_->forget(catalog_id);
78 catalog_collection_->deleted_catalogs_.clear();
81 for (
auto &catalog_uptr : catalog_collection_->catalogs_.values()) {
82 catalog_uptr->flags.has_unsaved_changes =
false;
89 return catalog_collection_->has_unsaved_changes_;
99 for (
auto &catalog : catalog_collection_->catalogs_.values()) {
100 catalog->flags.has_unsaved_changes =
true;
102 catalog_collection_->has_unsaved_changes_ =
true;
108 return catalog_collection_->catalogs_.is_empty();
113 return catalog_collection_->catalogs_;
117 return catalog_collection_->deleted_catalogs_;
122 return catalog_collection_->catalog_definition_file_.get();
127 const std::unique_ptr<AssetCatalog> *catalog_uptr_ptr =
128 catalog_collection_->catalogs_.lookup_ptr(catalog_id);
129 if (catalog_uptr_ptr ==
nullptr) {
132 return catalog_uptr_ptr->get();
142 for (
const auto &catalog : catalog_collection_->catalogs_.values()) {
143 if (catalog->path == path) {
144 ordered_catalogs.insert(catalog.get());
148 if (ordered_catalogs.empty()) {
152 MutableAssetCatalogOrderedSet::iterator best_choice_it = ordered_catalogs.begin();
153 return *best_choice_it;
159 return catalog_collection_->catalogs_.contains(catalog_id);
167 matching_catalog_ids.
add(active_catalog_id);
176 for (
const auto &catalog_uptr : catalog_collection_->catalogs_.values()) {
178 matching_catalog_ids.
add(catalog_uptr->catalog_id);
180 known_catalog_ids.
add(catalog_uptr->catalog_id);
183 return AssetCatalogFilter(std::move(matching_catalog_ids), std::move(known_catalog_ids));
188 std::unique_ptr<AssetCatalog> *catalog_uptr_ptr = catalog_collection_->catalogs_.lookup_ptr(
190 if (catalog_uptr_ptr ==
nullptr) {
201 catalog_collection_->deleted_catalogs_.add(catalog_id, std::move(*catalog_uptr_ptr));
204 catalog_collection_->catalogs_.remove(catalog_id);
209 catalog_collection_->catalogs_.remove(catalog_id);
210 catalog_collection_->deleted_catalogs_.remove(catalog_id);
213 catalog_collection_->catalog_definition_file_->forget(catalog_id);
220 for (
const auto &catalog_uptr : catalog_collection_->catalogs_.values()) {
228 for (
const CatalogID cat_id : catalogs_to_delete) {
239 BLI_assert_msg(catalog,
"trying to prune asset catalogs by the path of a non-existent catalog");
252 for (
auto &catalog_uptr : catalog_collection_->catalogs_.values()) {
259 cat->
path = new_path;
275 catalog->flags.has_unsaved_changes =
true;
283 BLI_assert_msg(!catalog_collection_->catalogs_.contains(catalog->catalog_id),
284 "duplicate catalog ID not supported");
285 catalog_collection_->catalogs_.add_new(catalog->catalog_id, std::move(catalog));
287 if (catalog_collection_->catalog_definition_file_) {
290 catalog_collection_->catalog_definition_file_->add_new(catalog_ptr);
304 asset_library_root.
data(),
317 if (
BLI_stat(file_or_directory_path.data(), &status) == -1) {
319 CLOG_DEBUG(&
LOG,
"path not found: %s", file_or_directory_path.data());
326 else if (
S_ISDIR(status.st_mode)) {
343 catalog_collection_->add_catalogs_from_existing(*other_service.catalog_collection_,
367 catalog_definition_file_path);
370 "Only loading of a single catalog definition file is supported.");
371 catalog_collection_->catalog_definition_file_ = std::move(cdf);
377 auto cdf = std::make_unique<AssetCatalogDefinitionFile>(catalog_definition_file_path);
382 auto catalog_parsed_callback = [
this, catalog_definition_file_path, &seen_paths](
383 std::unique_ptr<AssetCatalog> catalog) {
388 if (catalog_collection_->catalogs_.contains(catalog->catalog_id)) {
390 std::cerr << catalog_definition_file_path <<
": multiple definitions of catalog "
391 << catalog->catalog_id <<
" in multiple files, ignoring this one." << std::endl;
396 catalog->flags.is_first_loaded = seen_paths.
add(catalog->path);
399 catalog_collection_->catalogs_.add_new(catalog->catalog_id, std::move(catalog));
403 cdf->parse_catalog_file(cdf->file_path, catalog_parsed_callback);
420 auto catalog_parsed_callback = [
this, &cats_in_file](std::unique_ptr<AssetCatalog> catalog) {
425 const CatalogID catalog_id = catalog->catalog_id;
426 cats_in_file.
add(catalog_id);
436 catalog_collection_->catalogs_.add_overwrite(catalog_id, std::move(catalog));
449 for (
CatalogID cat_id : this->catalog_collection_->catalogs_.keys()) {
450 if (catalogs_to_keep.
contains(cat_id)) {
457 cats_to_remove.
add(cat_id);
460 for (
CatalogID cat_id : cats_to_remove) {
467 if (catalog_collection_->deleted_catalogs_.contains(catalog_id)) {
472 const std::unique_ptr<AssetCatalog> *catalog_uptr_ptr =
473 catalog_collection_->catalogs_.lookup_ptr(catalog_id);
474 if (!catalog_uptr_ptr) {
509 if (catalog_collection_->catalog_definition_file_) {
514 catalog_collection_->catalog_definition_file_->exists_on_disk())
518 return catalog_collection_->catalog_definition_file_->write_to_disk();
521 if (catalog_collection_->is_empty()) {
529 return catalog_collection_->catalog_definition_file_->write_to_disk();
536 if (!catalog_collection_->catalog_definition_file_) {
543 catalog_collection_->catalog_definition_file_.reset();
554 "A non-empty .blend file path is required to be able to determine where the "
555 "catalog definition file should be put");
560 if (!suitable_root_path.empty()) {
563 sizeof(asset_lib_cdf_path),
564 suitable_root_path.c_str(),
566 return asset_lib_cdf_path;
574 return cdf_path_next_to_blend;
580 auto cdf = std::make_unique<AssetCatalogDefinitionFile>(file_path);
582 for (
auto &catalog : catalog_collection_->catalogs_.values()) {
583 cdf->add_new(catalog.get());
591 auto tree = std::make_unique<AssetCatalogTree>();
594 for (
auto &catalog : catalog_collection_->catalogs_.values()) {
595 tree->insert_item(*catalog);
603 std::lock_guard
lock{catalog_tree_mutex_};
604 this->catalog_tree_ =
nullptr;
609 std::lock_guard
lock{catalog_tree_mutex_};
610 if (!catalog_tree_) {
617 return catalog_tree_;
623 std::set<AssetCatalogPath> paths_to_check;
624 for (
auto &catalog : catalog_collection_->catalogs_.values()) {
625 paths_to_check.insert(catalog->path);
628 std::set<AssetCatalogPath> seen_paths;
633 while (!paths_to_check.empty()) {
636 paths_to_check.erase(paths_to_check.begin());
638 if (seen_paths.contains(path)) {
642 seen_paths.insert(path);
645 if (seen_paths.contains(parent_path)) {
654 paths_to_check.insert(parent_path);
662 return !undo_snapshots_.is_empty();
667 return !redo_snapshots_.is_empty();
674 redo_snapshots_.append(std::move(catalog_collection_));
675 catalog_collection_ = undo_snapshots_.pop_last();
686 undo_snapshots_.append(std::move(catalog_collection_));
687 catalog_collection_ = redo_snapshots_.pop_last();
696 std::unique_ptr<AssetCatalogCollection> snapshot = catalog_collection_->deep_copy();
697 undo_snapshots_.append(std::move(snapshot));
698 redo_snapshots_.clear();
715 auto catalog = std::make_unique<AssetCatalog>(cat_id, clean_path,
simple_name);
734 return "..." + name.substr(name.length() - 60);
741 : matching_catalog_ids_(std::move(matching_catalog_ids)),
742 known_catalog_ids_(std::move(known_catalog_ids))
748 return matching_catalog_ids_.contains(asset_catalog_id);
756 return known_catalog_ids_.contains(asset_catalog_id);
File and directory operations.
#define BLI_path_join(...)
Compatibility-like things for windows.
#define CLOG_DEBUG(clg_ref,...)
#define BLI_assert_msg(a, msg)
bool contains(const Key &key) const
FunctionRef< void(const AssetCatalog &existing, const AssetCatalog &to_be_ignored)> OnDuplicateCatalogIdFn
void parse_catalog_file(const CatalogFilePath &catalog_definition_file_path, AssetCatalogParsedFn catalog_loaded_callback)
const CatalogFilePath file_path
bool contains(CatalogID asset_catalog_id) const
bool is_known(CatalogID asset_catalog_id) const
AssetCatalogFilter(Set< CatalogID > &&matching_catalog_ids, Set< CatalogID > &&known_catalog_ids)
AssetCatalogPath parent() const
const std::string & str() const
AssetCatalogPath cleanup() const
AssetCatalogPath rebase(const AssetCatalogPath &from_path, const AssetCatalogPath &to_path) const
AssetCatalogPath()=default
bool is_contained_in(const AssetCatalogPath &other_path) const
static const char SEPARATOR
bool is_undo_possbile() const
void prepare_to_merge_on_write()
void delete_catalog_by_id_soft(CatalogID catalog_id)
void load_single_file(const CatalogFilePath &catalog_definition_file_path)
AssetCatalogFilter create_catalog_filter(CatalogID active_catalog_id) const
AssetCatalog * find_catalog_by_path(const AssetCatalogPath &path) const
AssetCatalog * find_catalog(CatalogID catalog_id) const
std::shared_ptr< const AssetCatalogTree > catalog_tree()
void delete_catalog_by_id_hard(CatalogID catalog_id)
void prune_catalogs_by_path(const AssetCatalogPath &path)
bool write_to_disk(const CatalogFilePath &blend_file_path)
bool has_unsaved_changes() const
std::unique_ptr< AssetCatalogTree > read_into_tree() const
void purge_catalogs_not_listed(const Set< CatalogID > &catalogs_to_keep)
std::unique_ptr< AssetCatalogDefinitionFile > parse_catalog_file(const CatalogFilePath &catalog_definition_file_path)
std::unique_ptr< AssetCatalogDefinitionFile > construct_cdf_in_memory(const CatalogFilePath &file_path) const
void create_missing_catalogs()
void tag_all_catalogs_as_unsaved_changes()
AssetCatalog * create_catalog(const AssetCatalogPath &catalog_path)
void update_catalog_path(CatalogID catalog_id, const AssetCatalogPath &new_catalog_path)
bool is_read_only() const
void prune_catalogs_by_id(CatalogID catalog_id)
void invalidate_catalog_tree()
void add_from_existing(const AssetCatalogService &other_service, FunctionRef< void(const AssetCatalog &existing, const AssetCatalog &to_be_ignored)> on_duplicate_items)
void untag_has_unsaved_changes()
bool is_redo_possbile() const
static CatalogFilePath find_suitable_cdf_path_for_writing(const CatalogFilePath &blend_file_path)
bool write_to_disk_ex(const CatalogFilePath &blend_file_path)
void load_directory_recursive(const CatalogFilePath &directory_path)
bool is_catalog_known_with_unsaved_changes(CatalogID catalog_id) const
const OwningAssetCatalogMap & get_deleted_catalogs() const
void tag_has_unsaved_changes(AssetCatalog *edited_catalog=nullptr)
static const CatalogFilePath DEFAULT_CATALOG_FILENAME
bool is_catalog_known(CatalogID catalog_id) const
AssetCatalogService(const CatalogFilePath &asset_library_root={}, std::optional< read_only_tag > read_only_tag=std::nullopt)
const AssetCatalogDefinitionFile * get_catalog_definition_file() const
const OwningAssetCatalogMap & get_catalogs() const
const CatalogID catalog_id
static std::unique_ptr< AssetCatalog > from_path(const AssetCatalogPath &path)
void simple_name_refresh()
static std::string sensible_simple_name_for_path(const AssetCatalogPath &path)
struct blender::asset_system::AssetCatalog::Flags flags
void tag_all_library_catalogs_dirty()
static AssetLibraryService * get()
constexpr const char * data() const
bool skip_experimental_asset_catalog(const UUID &catalog_id)
std::set< AssetCatalog *, AssetCatalogLessThan > MutableAssetCatalogOrderedSet
Map< CatalogID, std::unique_ptr< AssetCatalog > > OwningAssetCatalogMap
static std::string asset_definition_default_file_path_from_dir(StringRef asset_library_root)
std::string CatalogFilePath
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
std::string AS_asset_library_find_suitable_root_path_from_path(StringRefNull input_path)
bool BLI_uuid_is_nil(bUUID uuid)
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
int BLI_stat(const char *path, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
bUUID BLI_uuid_generate_random()
bool BLI_is_file(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()