From 99e71ec1f24917cd14ff81ea2b15d3c0aeb0cadc Mon Sep 17 00:00:00 2001 From: Julian Eisel Date: Mon, 13 Feb 2023 12:42:31 +0100 Subject: [PATCH] Assets: Store pointer to owning asset library in asset representation This is needed to be able to query asset library information from an asset. This again is relevant especially for the "All" asset library, where you can't just directly access the library itself, which is different for different assets. The current design is that an asset representation is owned by exactly one asset library, so having this pointer is perfectly compatible with the design. Reviewed by: Julian Eisel --- .../asset_system/AS_asset_representation.hh | 12 ++++++++-- .../asset_system/intern/asset_library.cc | 5 +++-- .../intern/asset_representation.cc | 22 +++++++++++++++---- .../asset_system/intern/asset_storage.cc | 13 ++++++----- .../asset_system/intern/asset_storage.hh | 7 ++++-- 5 files changed, 44 insertions(+), 15 deletions(-) diff --git a/source/blender/asset_system/AS_asset_representation.hh b/source/blender/asset_system/AS_asset_representation.hh index 65f7e08f395..6793cd8097c 100644 --- a/source/blender/asset_system/AS_asset_representation.hh +++ b/source/blender/asset_system/AS_asset_representation.hh @@ -23,11 +23,15 @@ struct ID; namespace blender::asset_system { +class AssetLibrary; + class AssetRepresentation { AssetIdentifier identifier_; /** Indicate if this is a local or external asset, and as such, which of the union members below * should be used. */ const bool is_local_id_ = false; + /** Asset library that owns this asset representation. */ + const AssetLibrary *owner_asset_library_; struct ExternalAsset { std::string name; @@ -44,10 +48,13 @@ class AssetRepresentation { /** Constructs an asset representation for an external ID. The asset will not be editable. */ AssetRepresentation(AssetIdentifier &&identifier, StringRef name, - std::unique_ptr metadata); + std::unique_ptr metadata, + const AssetLibrary &owner_asset_library); /** Constructs an asset representation for an ID stored in the current file. This makes the asset * local and fully editable. */ - AssetRepresentation(AssetIdentifier &&identifier, ID &id); + AssetRepresentation(AssetIdentifier &&identifier, + ID &id, + const AssetLibrary &owner_asset_library); AssetRepresentation(AssetRepresentation &&other); /* Non-copyable type. */ AssetRepresentation(const AssetRepresentation &other) = delete; @@ -65,6 +72,7 @@ class AssetRepresentation { AssetMetaData &get_metadata() const; /** Returns if this asset is stored inside this current file, and as such fully editable. */ bool is_local_id() const; + const AssetLibrary &owner_asset_library() const; }; } // namespace blender::asset_system diff --git a/source/blender/asset_system/intern/asset_library.cc b/source/blender/asset_system/intern/asset_library.cc index 2379e738e37..fdb54c16af6 100644 --- a/source/blender/asset_system/intern/asset_library.cc +++ b/source/blender/asset_system/intern/asset_library.cc @@ -169,13 +169,14 @@ AssetRepresentation &AssetLibrary::add_external_asset(StringRef relative_asset_p std::unique_ptr metadata) { AssetIdentifier identifier = asset_identifier_from_library(relative_asset_path); - return asset_storage_->add_external_asset(std::move(identifier), name, std::move(metadata)); + return asset_storage_->add_external_asset( + std::move(identifier), name, std::move(metadata), *this); } AssetRepresentation &AssetLibrary::add_local_id_asset(StringRef relative_asset_path, ID &id) { AssetIdentifier identifier = asset_identifier_from_library(relative_asset_path); - return asset_storage_->add_local_id_asset(std::move(identifier), id); + return asset_storage_->add_local_id_asset(std::move(identifier), id, *this); } bool AssetLibrary::remove_asset(AssetRepresentation &asset) diff --git a/source/blender/asset_system/intern/asset_representation.cc b/source/blender/asset_system/intern/asset_representation.cc index 573358b7df7..13c6236a6dd 100644 --- a/source/blender/asset_system/intern/asset_representation.cc +++ b/source/blender/asset_system/intern/asset_representation.cc @@ -17,15 +17,24 @@ namespace blender::asset_system { AssetRepresentation::AssetRepresentation(AssetIdentifier &&identifier, StringRef name, - std::unique_ptr metadata) - : identifier_(identifier), is_local_id_(false), external_asset_() + std::unique_ptr metadata, + const AssetLibrary &owner_asset_library) + : identifier_(identifier), + is_local_id_(false), + owner_asset_library_(&owner_asset_library), + external_asset_() { external_asset_.name = name; external_asset_.metadata_ = std::move(metadata); } -AssetRepresentation::AssetRepresentation(AssetIdentifier &&identifier, ID &id) - : identifier_(identifier), is_local_id_(true), local_asset_id_(&id) +AssetRepresentation::AssetRepresentation(AssetIdentifier &&identifier, + ID &id, + const AssetLibrary &owner_asset_library) + : identifier_(identifier), + is_local_id_(true), + owner_asset_library_(&owner_asset_library), + local_asset_id_(&id) { if (!id.asset_data) { throw std::invalid_argument("Passed ID is not an asset"); @@ -75,6 +84,11 @@ bool AssetRepresentation::is_local_id() const return is_local_id_; } +const AssetLibrary &AssetRepresentation::owner_asset_library() const +{ + return *owner_asset_library_; +} + } // namespace blender::asset_system using namespace blender; diff --git a/source/blender/asset_system/intern/asset_storage.cc b/source/blender/asset_system/intern/asset_storage.cc index 73b9d582616..f14b0cd0f79 100644 --- a/source/blender/asset_system/intern/asset_storage.cc +++ b/source/blender/asset_system/intern/asset_storage.cc @@ -15,18 +15,21 @@ namespace blender::asset_system { -AssetRepresentation &AssetStorage::add_local_id_asset(AssetIdentifier &&identifier, ID &id) +AssetRepresentation &AssetStorage::add_local_id_asset(AssetIdentifier &&identifier, + ID &id, + const AssetLibrary &owner_asset_library) { return *local_id_assets_.lookup_key_or_add( - std::make_unique(std::move(identifier), id)); + std::make_unique(std::move(identifier), id, owner_asset_library)); } AssetRepresentation &AssetStorage::add_external_asset(AssetIdentifier &&identifier, StringRef name, - std::unique_ptr metadata) + std::unique_ptr metadata, + const AssetLibrary &owner_asset_library) { - return *external_assets_.lookup_key_or_add( - std::make_unique(std::move(identifier), name, std::move(metadata))); + return *external_assets_.lookup_key_or_add(std::make_unique( + std::move(identifier), name, std::move(metadata), owner_asset_library)); } bool AssetStorage::remove_asset(AssetRepresentation &asset) diff --git a/source/blender/asset_system/intern/asset_storage.hh b/source/blender/asset_system/intern/asset_storage.hh index 2b4614abca5..c4a9ec14319 100644 --- a/source/blender/asset_system/intern/asset_storage.hh +++ b/source/blender/asset_system/intern/asset_storage.hh @@ -35,9 +35,12 @@ class AssetStorage { /** See #AssetLibrary::add_external_asset(). */ AssetRepresentation &add_external_asset(AssetIdentifier &&identifier, StringRef name, - std::unique_ptr metadata); + std::unique_ptr metadata, + const AssetLibrary &owner_asset_library); /** See #AssetLibrary::add_external_asset(). */ - AssetRepresentation &add_local_id_asset(AssetIdentifier &&identifier, ID &id); + AssetRepresentation &add_local_id_asset(AssetIdentifier &&identifier, + ID &id, + const AssetLibrary &owner_asset_library); /** See #AssetLibrary::remove_asset(). */ bool remove_asset(AssetRepresentation &asset);