From 631e5d5d4f3730438ec970eb59f3621f5747a42d Mon Sep 17 00:00:00 2001 From: Julian Eisel Date: Thu, 8 Jun 2023 20:49:28 +0200 Subject: [PATCH] Asset system: Store ID type in asset representation No user visible changes expected. This brings us another step closer to replacing the temporary asset handle design with the proper asset representation design. I held off with this a bit because we eventually want to support non-ID assets, but for now it is fine to consider all assets to be IDs. In future the asset system can make the necessary distinctions still. Now only the preview is handled via asset handle still. --- .../blender/asset_system/AS_asset_library.hh | 1 + .../asset_system/AS_asset_representation.h | 1 + .../asset_system/AS_asset_representation.hh | 3 +++ .../asset_system/intern/asset_library.cc | 3 ++- .../intern/asset_representation.cc | 18 ++++++++++++++++++ .../asset_system/intern/asset_storage.cc | 3 ++- .../asset_system/intern/asset_storage.hh | 1 + .../tests/asset_representation_test.cc | 3 ++- .../editors/asset/intern/asset_handle.cc | 4 ++-- source/blender/editors/space_file/filelist.cc | 2 +- 10 files changed, 33 insertions(+), 6 deletions(-) diff --git a/source/blender/asset_system/AS_asset_library.hh b/source/blender/asset_system/AS_asset_library.hh index f5f9473d7df..96e723ce19b 100644 --- a/source/blender/asset_system/AS_asset_library.hh +++ b/source/blender/asset_system/AS_asset_library.hh @@ -124,6 +124,7 @@ class AssetLibrary { */ AssetRepresentation &add_external_asset(StringRef relative_asset_path, StringRef name, + int id_type, std::unique_ptr metadata); /** See #AssetLibrary::add_external_asset(). */ AssetRepresentation &add_local_id_asset(StringRef relative_asset_path, ID &id); diff --git a/source/blender/asset_system/AS_asset_representation.h b/source/blender/asset_system/AS_asset_representation.h index 8401209fba4..dd80811c228 100644 --- a/source/blender/asset_system/AS_asset_representation.h +++ b/source/blender/asset_system/AS_asset_representation.h @@ -22,6 +22,7 @@ typedef struct AssetRepresentation AssetRepresentation; const char *AS_asset_representation_name_get(const AssetRepresentation *asset) ATTR_WARN_UNUSED_RESULT; +int AS_asset_representation_id_type_get(const AssetRepresentation *asset) ATTR_WARN_UNUSED_RESULT; AssetMetaData *AS_asset_representation_metadata_get(const AssetRepresentation *asset) ATTR_WARN_UNUSED_RESULT; struct ID *AS_asset_representation_local_id_get(const AssetRepresentation *asset) diff --git a/source/blender/asset_system/AS_asset_representation.hh b/source/blender/asset_system/AS_asset_representation.hh index 89ee5d66a9b..2f8b39df465 100644 --- a/source/blender/asset_system/AS_asset_representation.hh +++ b/source/blender/asset_system/AS_asset_representation.hh @@ -42,6 +42,7 @@ class AssetRepresentation { struct ExternalAsset { std::string name; + int id_type = 0; std::unique_ptr metadata_ = nullptr; }; union { @@ -55,6 +56,7 @@ class AssetRepresentation { /** Constructs an asset representation for an external ID. The asset will not be editable. */ AssetRepresentation(AssetIdentifier &&identifier, StringRef name, + int id_type, std::unique_ptr metadata, const AssetLibrary &owner_asset_library); /** @@ -85,6 +87,7 @@ class AssetRepresentation { std::unique_ptr make_weak_reference() const; StringRefNull get_name() const; + int get_id_type() const; AssetMetaData &get_metadata() const; /** * Get the import method to use for this asset. A different one may be used if diff --git a/source/blender/asset_system/intern/asset_library.cc b/source/blender/asset_system/intern/asset_library.cc index 401c8166e90..4500e0faaf9 100644 --- a/source/blender/asset_system/intern/asset_library.cc +++ b/source/blender/asset_system/intern/asset_library.cc @@ -230,11 +230,12 @@ void AssetLibrary::refresh() AssetRepresentation &AssetLibrary::add_external_asset(StringRef relative_asset_path, StringRef name, + const int id_type, 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), *this); + std::move(identifier), name, id_type, std::move(metadata), *this); } AssetRepresentation &AssetLibrary::add_local_id_asset(StringRef relative_asset_path, ID &id) diff --git a/source/blender/asset_system/intern/asset_representation.cc b/source/blender/asset_system/intern/asset_representation.cc index d05db4fafd6..fb45d9ab632 100644 --- a/source/blender/asset_system/intern/asset_representation.cc +++ b/source/blender/asset_system/intern/asset_representation.cc @@ -21,6 +21,7 @@ namespace blender::asset_system { AssetRepresentation::AssetRepresentation(AssetIdentifier &&identifier, StringRef name, + const int id_type, std::unique_ptr metadata, const AssetLibrary &owner_asset_library) : identifier_(identifier), @@ -29,6 +30,7 @@ AssetRepresentation::AssetRepresentation(AssetIdentifier &&identifier, external_asset_() { external_asset_.name = name; + external_asset_.id_type = id_type; external_asset_.metadata_ = std::move(metadata); } @@ -87,6 +89,15 @@ StringRefNull AssetRepresentation::get_name() const return external_asset_.name; } +int AssetRepresentation::get_id_type() const +{ + if (is_local_id_) { + return GS(local_asset_id_->name); + } + + return external_asset_.id_type; +} + AssetMetaData &AssetRepresentation::get_metadata() const { return is_local_id_ ? *local_asset_id_->asset_data : *external_asset_.metadata_; @@ -192,6 +203,13 @@ const char *AS_asset_representation_name_get(const AssetRepresentation *asset_ha return asset->get_name().c_str(); } +int AS_asset_representation_id_type_get(const AssetRepresentation *asset_handle) +{ + const asset_system::AssetRepresentation *asset = + reinterpret_cast(asset_handle); + return asset->get_id_type(); +} + AssetMetaData *AS_asset_representation_metadata_get(const AssetRepresentation *asset_handle) { const asset_system::AssetRepresentation *asset = diff --git a/source/blender/asset_system/intern/asset_storage.cc b/source/blender/asset_system/intern/asset_storage.cc index 646623af51d..365621266cc 100644 --- a/source/blender/asset_system/intern/asset_storage.cc +++ b/source/blender/asset_system/intern/asset_storage.cc @@ -27,11 +27,12 @@ AssetRepresentation &AssetStorage::add_local_id_asset(AssetIdentifier &&identifi AssetRepresentation &AssetStorage::add_external_asset(AssetIdentifier &&identifier, StringRef name, + const int id_type, 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), owner_asset_library)); + std::move(identifier), name, id_type, 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 9020be95d48..282c2507771 100644 --- a/source/blender/asset_system/intern/asset_storage.hh +++ b/source/blender/asset_system/intern/asset_storage.hh @@ -37,6 +37,7 @@ class AssetStorage { /** See #AssetLibrary::add_external_asset(). */ AssetRepresentation &add_external_asset(AssetIdentifier &&identifier, StringRef name, + int id_type, std::unique_ptr metadata, const AssetLibrary &owner_asset_library); /** See #AssetLibrary::add_external_asset(). */ diff --git a/source/blender/asset_system/tests/asset_representation_test.cc b/source/blender/asset_system/tests/asset_representation_test.cc index d8739566bae..f5e30d315fa 100644 --- a/source/blender/asset_system/tests/asset_representation_test.cc +++ b/source/blender/asset_system/tests/asset_representation_test.cc @@ -32,7 +32,8 @@ class AssetRepresentationTest : public AssetLibraryTestBase { AssetRepresentation &add_dummy_asset(AssetLibrary &library, StringRef relative_path) { std::unique_ptr dummy_metadata = std::make_unique(); - return library.add_external_asset(relative_path, "Some asset name", std::move(dummy_metadata)); + return library.add_external_asset( + relative_path, "Some asset name", 0, std::move(dummy_metadata)); } }; diff --git a/source/blender/editors/asset/intern/asset_handle.cc b/source/blender/editors/asset/intern/asset_handle.cc index a6d5c4002f6..70de0a23415 100644 --- a/source/blender/editors/asset/intern/asset_handle.cc +++ b/source/blender/editors/asset/intern/asset_handle.cc @@ -41,9 +41,9 @@ ID *ED_asset_handle_get_local_id(const AssetHandle *asset_handle) return AS_asset_representation_local_id_get(asset_handle->file_data->asset); } -ID_Type ED_asset_handle_get_id_type(const AssetHandle *asset) +ID_Type ED_asset_handle_get_id_type(const AssetHandle *asset_handle) { - return static_cast(asset->file_data->blentype); + return static_cast(AS_asset_representation_id_type_get(asset_handle->file_data->asset)); } int ED_asset_handle_get_preview_icon_id(const AssetHandle *asset) diff --git a/source/blender/editors/space_file/filelist.cc b/source/blender/editors/space_file/filelist.cc index 8b5cc2a6f6f..385fab2831b 100644 --- a/source/blender/editors/space_file/filelist.cc +++ b/source/blender/editors/space_file/filelist.cc @@ -3187,7 +3187,7 @@ static void filelist_readjob_list_lib_add_datablock(FileListReadJob *job_params, datablock_info->free_asset_data = false; entry->asset = &job_params->load_asset_library->add_external_asset( - entry->relpath, datablock_info->name, std::move(metadata)); + entry->relpath, datablock_info->name, idcode, std::move(metadata)); } } }