[Bf-blender-cvs] [887eb22023a] temp-asset-browser-catalogs-ui: Merge branch 'temp-asset-browser-catalogs' into temp-asset-browser-catalogs-ui
Julian Eisel
noreply at git.blender.org
Tue Sep 21 17:04:10 CEST 2021
Commit: 887eb22023aadfce2fad1d717c5c5bb2ed2bf965
Author: Julian Eisel
Date: Tue Sep 21 16:44:35 2021 +0200
Branches: temp-asset-browser-catalogs-ui
https://developer.blender.org/rB887eb22023aadfce2fad1d717c5c5bb2ed2bf965
Merge branch 'temp-asset-browser-catalogs' into temp-asset-browser-catalogs-ui
===================================================================
===================================================================
diff --cc source/blender/blenkernel/BKE_asset_catalog.hh
index d283de9af2f,2e11803cbb5..8375960e3eb
--- a/source/blender/blenkernel/BKE_asset_catalog.hh
+++ b/source/blender/blenkernel/BKE_asset_catalog.hh
@@@ -61,13 -62,19 +63,23 @@@ class AssetCatalogService
/** Load asset catalog definitions from the given file or directory. */
void load_from_disk(const CatalogFilePath &file_or_directory_path);
+ /**
+ * Merge on-disk changes into the in-memory asset catalogs.
+ * This should be called before writing the asset catalogs to disk.
+ *
+ * - New on-disk catalogs are loaded into memory.
+ * - Already-known on-disk catalogs are ignored (so will be overwritten with our in-memory
+ * data). This includes in-memory marked-as-deleted catalogs.
+ */
+ void merge_from_disk_before_writing();
+
/** Return catalog with the given ID. Return nullptr if not found. */
- AssetCatalog *find_catalog(const CatalogID &catalog_id);
+ AssetCatalog *find_catalog(CatalogID catalog_id);
+ /** Return first catalog with the given path. Return nullptr if not found. Not the most efficient
+ * function, better don't use it in performance sensitive areas. */
+ AssetCatalog *find_catalog_from_path(const CatalogPath &path) const;
+
/** Create a catalog with some sensible auto-generated catalog ID.
* The catalog will be saved to the default catalog file.*/
AssetCatalog *create_catalog(const CatalogPath &catalog_path);
diff --cc source/blender/blenkernel/intern/asset_catalog.cc
index 53bfaf05206,b2773579046..6417e78d964
--- a/source/blender/blenkernel/intern/asset_catalog.cc
+++ b/source/blender/blenkernel/intern/asset_catalog.cc
@@@ -52,17 -50,27 +50,38 @@@ AssetCatalog *AssetCatalogService::find
return catalog_uptr_ptr->get();
}
+AssetCatalog *AssetCatalogService::find_catalog_from_path(const CatalogPath &path) const
+{
+ for (auto &catalog : catalogs_.values()) {
+ if (catalog->path == path) {
+ return catalog.get();
+ }
+ }
+
+ return nullptr;
+}
+
+ void AssetCatalogService::delete_catalog(CatalogID catalog_id)
+ {
+ std::unique_ptr<AssetCatalog> *catalog_uptr_ptr = this->catalogs_.lookup_ptr(catalog_id);
+ if (catalog_uptr_ptr == nullptr) {
+ /* Catalog cannot be found, which is fine. */
+ return;
+ }
+
+ /* Mark the catalog as deleted. */
+ AssetCatalog *catalog = catalog_uptr_ptr->get();
+ catalog->flags.is_deleted = true;
+
+ /* Move ownership from this->catalogs_ to this->deleted_catalogs_. */
+ this->deleted_catalogs_.add(catalog_id, std::move(*catalog_uptr_ptr));
+
+ /* The catalog can now be removed from the map without freeing the actual AssetCatalog. */
+ this->catalogs_.remove(catalog_id);
+
+ this->rebuild_tree();
+ }
+
AssetCatalog *AssetCatalogService::create_catalog(const CatalogPath &catalog_path)
{
std::unique_ptr<AssetCatalog> catalog = AssetCatalog::from_path(catalog_path);
@@@ -263,23 -374,14 +366,28 @@@ std::unique_ptr<AssetCatalogTree> Asset
return tree;
}
+/* ---------------------------------------------------------------------- */
+
+AssetCatalogTreeItem::AssetCatalogTreeItem(StringRef name,
+ StringRef catalog_id,
+ const AssetCatalogTreeItem *parent)
+ : name_(name), catalog_id_(catalog_id), parent_(parent)
+{
+}
+
+AssetCatalogTreeItemIterator AssetCatalogTreeItem::children()
+{
+ return AssetCatalogTreeItemIterator(children_.begin(), children_.end());
+}
+
+ void AssetCatalogService::rebuild_tree()
+ {
+ this->catalog_tree_ = read_into_tree();
+ }
+
-AssetCatalogTreeItem::AssetCatalogTreeItem(StringRef name, const AssetCatalogTreeItem *parent)
- : name_(name), parent_(parent)
+StringRef AssetCatalogTreeItem::get_catalog_id() const
{
+ return catalog_id_;
}
StringRef AssetCatalogTreeItem::get_name() const
@@@ -305,97 -407,6 +413,98 @@@ int AssetCatalogTreeItem::count_parents
return i;
}
+bool AssetCatalogTreeItem::has_children() const
+{
+ return !children_.empty();
+}
+
+/* ---------------------------------------------------------------------- */
+
+AssetCatalogTreeItemIterator::AssetCatalogTreeItemIterator(WrappedIterator wrapped_iterator,
+ WrappedIterator wrapped_end_iterator)
+ : wrapped_iterator_(wrapped_iterator), wrapped_end_iterator_(wrapped_end_iterator)
+{
+}
+
+AssetCatalogTreeItemIterator AssetCatalogTreeItemIterator::begin() const
+{
+ return *this;
+}
+
+AssetCatalogTreeItemIterator AssetCatalogTreeItemIterator::end() const
+{
+ return AssetCatalogTreeItemIterator(wrapped_end_iterator_, wrapped_end_iterator_);
+}
+
+AssetCatalogTreeItem &AssetCatalogTreeItemIterator::operator*() const
+{
+ return wrapped_iterator_->second;
+}
+AssetCatalogTreeItem *AssetCatalogTreeItemIterator::operator->() const
+{
+ return &wrapped_iterator_->second;
+}
+
+AssetCatalogTreeItemIterator &AssetCatalogTreeItemIterator::operator++()
+{
+ ++wrapped_iterator_;
+ return *this;
+}
+AssetCatalogTreeItemIterator AssetCatalogTreeItemIterator::operator++(int)
+{
+ AssetCatalogTreeItemIterator copy(*this);
+ ++wrapped_iterator_;
+ return copy;
+}
+
+bool operator==(AssetCatalogTreeItemIterator a, AssetCatalogTreeItemIterator b)
+{
+ return a.wrapped_iterator_ == b.wrapped_iterator_;
+}
+bool operator!=(AssetCatalogTreeItemIterator a, AssetCatalogTreeItemIterator b)
+{
+ return a.wrapped_iterator_ != b.wrapped_iterator_;
+}
+
+/* ---------------------------------------------------------------------- */
+
+void AssetCatalogTree::insert_item(AssetCatalog &catalog)
+{
- /* #fs::path adds useful behavior to the path. Remember that on Windows it uses "\" as
- * separator! For catalogs it should always be "/". Use #fs::path::generic_string if needed. */
- fs::path catalog_path = catalog.path;
-
+ const AssetCatalogTreeItem *parent = nullptr;
+ AssetCatalogTreeItem::ChildMap *insert_to_map = &children_;
+
- BLI_assert_msg(catalog_path.is_relative() && !catalog_path.has_root_path(),
- "Malformed catalog path: Path should be a relative path, with no root-name or "
- "root-directory as defined by std::filesystem::path.");
- for (const fs::path &component : catalog_path) {
- std::string component_name = component.string();
++ BLI_assert_msg(!ELEM(catalog.path[0], '/', '\\'),
++ "Malformed catalog path: Path should be formatted like a relative path");
++
++ const char *next_slash_ptr;
++ /* Looks more complicated than it is, this just iterates over path components. E.g.
++ * "just/some/path" iterates over "just", then "some" then "path". */
++ for (const char *name_begin = catalog.path.data(); name_begin && name_begin[0];
++ /* Jump to one after the next slash if there is any. */
++ name_begin = next_slash_ptr ? next_slash_ptr + 1 : nullptr) {
++ next_slash_ptr = BLI_path_slash_find(name_begin);
++
++ /* Note that this won't be null terminated. */
++ StringRef component_name = next_slash_ptr ?
++ StringRef(name_begin, next_slash_ptr - name_begin) :
++ /* Last component in the path. */
++ name_begin;
+
+ /* Insert new tree element - if no matching one is there yet! */
+ auto [item, was_inserted] = insert_to_map->emplace(
- component_name,
- AssetCatalogTreeItem(
- component_name,
- /* TODO There may be components that don't relate to an actual
- catalog, i.e. there's no catalog ID to set. This should be changed. */
- catalog_path.filename() == component_name ? catalog.catalog_id : "",
- parent));
++ component_name, AssetCatalogTreeItem(component_name, catalog.catalog_id, parent));
+
+ /* Walk further into the path (no matter if a new item was created or not). */
+ parent = &item->second;
+ insert_to_map = &item->second.children_;
+ }
+}
+
+AssetCatalogTreeItemIterator AssetCatalogTree::children()
+{
+ return AssetCatalogTreeItemIterator(children_.begin(), children_.end());
+}
+
void AssetCatalogTree::foreach_item(const AssetCatalogTreeItem::ItemIterFn callback) const
{
AssetCatalogTreeItem::foreach_item_recursive(children_, callback);
More information about the Bf-blender-cvs
mailing list