[Bf-blender-cvs] [74fc2f084f1] temp-asset-browser-catalogs-ui: Merge branch 'master' into temp-asset-browser-catalogs-ui
Julian Eisel
noreply at git.blender.org
Thu Sep 23 16:25:14 CEST 2021
Commit: 74fc2f084f1362018c0a0cad68635ba303bc86f6
Author: Julian Eisel
Date: Thu Sep 23 16:18:55 2021 +0200
Branches: temp-asset-browser-catalogs-ui
https://developer.blender.org/rB74fc2f084f1362018c0a0cad68635ba303bc86f6
Merge branch 'master' into temp-asset-browser-catalogs-ui
===================================================================
===================================================================
diff --cc source/blender/blenkernel/intern/asset_catalog.cc
index 6a03bb25e05,0c64a0b085c..14fc603b066
--- a/source/blender/blenkernel/intern/asset_catalog.cc
+++ b/source/blender/blenkernel/intern/asset_catalog.cc
@@@ -97,18 -89,12 +102,18 @@@ AssetCatalog *AssetCatalogService::crea
BLI_assert_msg(!catalogs_.contains(catalog->catalog_id), "duplicate catalog ID not supported");
catalogs_.add_new(catalog->catalog_id, std::move(catalog));
- /* Ensure the new catalog gets written to disk. */
- this->ensure_asset_library_root();
- this->ensure_catalog_definition_file();
- catalog_definition_file_->add_new(catalog_ptr);
- catalog_definition_file_->write_to_disk();
+ if (catalog_definition_file_) {
+ /* Ensure the new catalog gets written to disk at some point. If there is no CDF in memory yet,
+ * it's enough to have the catalog known to the service as it'll be saved to a new file. */
+ catalog_definition_file_->add_new(catalog_ptr);
+ }
+ /* Null when the service only writes, but didn't load anything
+ * (#AssetCatalogService::load_from_disk() not called). */
+ if (catalog_tree_) {
+ catalog_tree_->insert_item(*catalog_ptr);
+ }
+
return catalog_ptr;
}
@@@ -275,83 -220,37 +239,73 @@@ void AssetCatalogService::merge_from_di
catalog_parsed_callback);
}
+void AssetCatalogDefinitionFile::parse_catalog_file(
+ const CatalogFilePath &catalog_definition_file_path,
+ AssetCatalogParsedFn catalog_loaded_callback)
+{
+ std::fstream infile(catalog_definition_file_path);
+ std::string line;
+ while (std::getline(infile, line)) {
+ const StringRef trimmed_line = StringRef(line).trim();
+ if (trimmed_line.is_empty() || trimmed_line[0] == '#') {
+ continue;
+ }
+
+ std::unique_ptr<AssetCatalog> catalog = this->parse_catalog_line(trimmed_line);
+ if (!catalog) {
+ continue;
+ }
+
+ AssetCatalog *non_owning_ptr = catalog.get();
+ const bool keep_catalog = catalog_loaded_callback(std::move(catalog));
+ if (!keep_catalog) {
+ continue;
+ }
+
+ if (this->contains(non_owning_ptr->catalog_id)) {
+ std::cerr << catalog_definition_file_path << ": multiple definitions of catalog "
+ << non_owning_ptr->catalog_id << " in the same file, using first occurrence."
+ << std::endl;
+ /* Don't store 'catalog'; unique_ptr will free its memory. */
+ continue;
+ }
+
+ /* The AssetDefinitionFile should include this catalog when writing it back to disk. */
+ this->add_new(non_owning_ptr);
+ }
+}
+
- std::unique_ptr<AssetCatalog> AssetCatalogDefinitionFile::parse_catalog_line(const StringRef line)
+ bool AssetCatalogService::write_to_disk(const CatalogFilePath &directory_for_new_files)
{
- const char delim = ':';
- const int64_t first_delim = line.find_first_of(delim);
- if (first_delim == StringRef::not_found) {
- std::cerr << "Invalid line in " << this->file_path << ": " << line << std::endl;
- return std::unique_ptr<AssetCatalog>(nullptr);
- }
+ /* TODO(Sybren): expand to support multiple CDFs. */
- /* Parse the catalog ID. */
- const std::string id_as_string = line.substr(0, first_delim).trim();
- UUID catalog_id;
- const bool uuid_parsed_ok = BLI_uuid_parse_string(&catalog_id, id_as_string.c_str());
- if (!uuid_parsed_ok) {
- std::cerr << "Invalid UUID in " << this->file_path << ": " << line << std::endl;
- return std::unique_ptr<AssetCatalog>(nullptr);
+ if (!catalog_definition_file_) {
+ if (catalogs_.is_empty() && deleted_catalogs_.is_empty()) {
+ /* Avoid saving anything, when there is nothing to save. */
+ return true; /* Writing nothing when there is nothing to write is still a success. */
+ }
+
+ /* A CDF has to be created to contain all current in-memory catalogs. */
+ const CatalogFilePath cdf_path = asset_definition_default_file_path_from_dir(
+ directory_for_new_files);
+ catalog_definition_file_ = construct_cdf_in_memory(cdf_path);
}
- /* Parse the path and simple name. */
- const StringRef path_and_simple_name = line.substr(first_delim + 1);
- const int64_t second_delim = path_and_simple_name.find_first_of(delim);
+ merge_from_disk_before_writing();
+ return catalog_definition_file_->write_to_disk();
+ }
- CatalogPath catalog_path;
- std::string simple_name;
- if (second_delim == 0) {
- /* Delimiter as first character means there is no path. These lines are to be ignored. */
- return std::unique_ptr<AssetCatalog>(nullptr);
- }
+ std::unique_ptr<AssetCatalogDefinitionFile> AssetCatalogService::construct_cdf_in_memory(
+ const CatalogFilePath &file_path)
+ {
+ auto cdf = std::make_unique<AssetCatalogDefinitionFile>();
+ cdf->file_path = file_path;
- if (second_delim == StringRef::not_found) {
- /* No delimiter means no simple name, just treat it as all "path". */
- catalog_path = path_and_simple_name;
- simple_name = "";
- }
- else {
- catalog_path = path_and_simple_name.substr(0, second_delim);
- simple_name = path_and_simple_name.substr(second_delim + 1).trim();
+ for (auto &catalog : catalogs_.values()) {
+ cdf->add_new(catalog.get());
}
- catalog_path = AssetCatalog::cleanup_path(catalog_path);
- return std::make_unique<AssetCatalog>(catalog_id, catalog_path, simple_name);
+ return cdf;
}
std::unique_ptr<AssetCatalogTree> AssetCatalogService::read_into_tree()
@@@ -366,28 -292,14 +320,28 @@@
return tree;
}
+ void AssetCatalogService::rebuild_tree()
+ {
+ this->catalog_tree_ = read_into_tree();
+ }
+
-AssetCatalogTreeItem::AssetCatalogTreeItem(StringRef name, const AssetCatalogTreeItem *parent)
- : name_(name), parent_(parent)
+/* ---------------------------------------------------------------------- */
+
+AssetCatalogTreeItem::AssetCatalogTreeItem(StringRef name,
+ const CatalogID &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();
- }
-
+const CatalogID &AssetCatalogTreeItem::get_catalog_id() const
{
+ return catalog_id_;
}
StringRef AssetCatalogTreeItem::get_name() const
@@@ -413,108 -325,6 +367,108 @@@ 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)
+{
+ const AssetCatalogTreeItem *parent = nullptr;
+ AssetCatalogTreeItem::ChildMap *insert_to_map = &children_;
+
+ BLI_assert_msg(!ELEM(catalog.path[0], '/', '\\'),
+ "Malformed catalog path: Path should be formatted like a relative path");
+
- CatalogID unset_id = bke::UUID();
++ CatalogID unset_id = bke::bUUID();
+ 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);
+
+ const bool is_last_component = next_slash_ptr == nullptr;
+ /* Note that this won't be null terminated. */
+ StringRef component_name = is_last_component ?
+ name_begin :
+ StringRef(name_begin, next_slash_ptr - name_begin);
+
+ /* Insert new tree element - if no matching one is there yet! */
+ auto [key_and_item, was_inserted] = insert_to_map->emplace(
+ component_name,
+ AssetCatalogTreeItem(
+ component_name, is_last_component ? catalog.catalog_id : unset_id, parent));
+ AssetCatalogTreeItem &item = key_and_item->second;
+
+ /* If full path of this catalog already exists as parent path of a previously read catalog, we
+ * can ensure this tree item's UUID is set here. */
+ if (is_last_component && (item.catalog_id_ == unset_id)) {
+ item.catalog_id_ = catalog.catalog_id;
+ }
+
+ /* Walk further i
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list