Refactor: Assets: Remove unnecessary asset identifier class

This was just rather useless level of abstraction. I heard from other
devs that these helper classes caused confusion, so better to avoid
this.

Now the asset representation has all the needed bits to create its full
path, blend-file path and asset library relative path. In fact only the
asset library relative path needs to be stored to make all this
available, since the asset representation already stores a reference to
the asset library owning it, so the paths can be recreated easily.
This commit is contained in:
Julian Eisel
2024-07-26 13:10:33 +02:00
parent 0dad3bdfa7
commit 693e590d9f
13 changed files with 74 additions and 61 deletions

View File

@@ -29,7 +29,6 @@ class IDRemapper;
namespace blender::asset_system {
class AssetIdentifier;
class AssetRepresentation;
/**
@@ -177,12 +176,6 @@ class AssetLibrary {
void on_blend_save_post(Main *bmain, PointerRNA **pointers, int num_pointers);
/**
* Create an asset identifier from the root path of this asset library and the given relative
* asset path (relative to the asset library root directory).
*/
AssetIdentifier asset_identifier_from_library(StringRef relative_asset_path);
std::string resolve_asset_weak_reference_to_full_path(const AssetWeakReference &asset_reference);
eAssetLibraryType library_type() const;

View File

@@ -23,8 +23,6 @@
#include "DNA_ID_enums.h"
#include "DNA_asset_types.h"
#include "AS_asset_identifier.hh"
struct AssetMetaData;
struct ID;
@@ -33,14 +31,18 @@ namespace blender::asset_system {
class AssetLibrary;
class AssetRepresentation : NonCopyable, NonMovable {
AssetIdentifier identifier_;
/** Pointer back to the asset library that owns this asset representation. */
const AssetLibrary &owner_asset_library_;
/**
* Uniquely identifies the asset within the asset library. Currently this is always a path (path
* within the asset library).
*/
std::string relative_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;
@@ -56,7 +58,7 @@ class AssetRepresentation : NonCopyable, NonMovable {
public:
/** Constructs an asset representation for an external ID. The asset will not be editable. */
AssetRepresentation(AssetIdentifier &&identifier,
AssetRepresentation(StringRef relative_asset_path,
StringRef name,
int id_type,
std::unique_ptr<AssetMetaData> metadata,
@@ -65,13 +67,11 @@ class AssetRepresentation : NonCopyable, NonMovable {
* Constructs an asset representation for an ID stored in the current file. This makes the asset
* local and fully editable.
*/
AssetRepresentation(AssetIdentifier &&identifier,
AssetRepresentation(StringRef relative_asset_path,
ID &id,
const AssetLibrary &owner_asset_library);
~AssetRepresentation();
const AssetIdentifier &get_identifier() const;
/**
* Create a weak reference for this asset that can be written to files, but can break under a
* number of conditions.
@@ -82,6 +82,11 @@ class AssetRepresentation : NonCopyable, NonMovable {
StringRefNull get_name() const;
ID_Type get_id_type() const;
AssetMetaData &get_metadata() const;
StringRefNull library_relative_identifier() const;
std::string full_path() const;
std::string full_library_path() const;
/**
* Get the import method to use for this asset. A different one may be used if
* #may_override_import_method() returns true, otherwise, the returned value must be used. If

View File

@@ -206,17 +206,15 @@ std::weak_ptr<AssetRepresentation> AssetLibrary::add_external_asset(
const int id_type,
std::unique_ptr<AssetMetaData> metadata)
{
AssetIdentifier identifier = this->asset_identifier_from_library(relative_asset_path);
return storage_.external_assets.lookup_key_or_add(std::make_shared<AssetRepresentation>(
std::move(identifier), name, id_type, std::move(metadata), *this));
relative_asset_path, name, id_type, std::move(metadata), *this));
}
std::weak_ptr<AssetRepresentation> AssetLibrary::add_local_id_asset(StringRef relative_asset_path,
ID &id)
{
AssetIdentifier identifier = this->asset_identifier_from_library(relative_asset_path);
return storage_.local_id_assets.lookup_key_or_add(
std::make_shared<AssetRepresentation>(std::move(identifier), id, *this));
std::make_shared<AssetRepresentation>(relative_asset_path, id, *this));
}
bool AssetLibrary::remove_asset(AssetRepresentation &asset)
@@ -288,11 +286,6 @@ void AssetLibrary::on_blend_save_post(Main *main,
}
}
AssetIdentifier AssetLibrary::asset_identifier_from_library(StringRef relative_asset_path)
{
return AssetIdentifier(root_path_, relative_asset_path);
}
std::string AssetLibrary::resolve_asset_weak_reference_to_full_path(
const AssetWeakReference &asset_reference)
{

View File

@@ -8,6 +8,10 @@
#include <stdexcept>
#include "BLI_path_util.h"
#include "BKE_blendfile.hh"
#include "DNA_ID.h"
#include "DNA_asset_types.h"
@@ -17,14 +21,14 @@
namespace blender::asset_system {
AssetRepresentation::AssetRepresentation(AssetIdentifier &&identifier,
AssetRepresentation::AssetRepresentation(StringRef relative_path,
StringRef name,
const int id_type,
std::unique_ptr<AssetMetaData> metadata,
const AssetLibrary &owner_asset_library)
: identifier_(std::move(identifier)),
: owner_asset_library_(owner_asset_library),
relative_identifier_(relative_path),
is_local_id_(false),
owner_asset_library_(owner_asset_library),
external_asset_()
{
external_asset_.name = name;
@@ -32,12 +36,12 @@ AssetRepresentation::AssetRepresentation(AssetIdentifier &&identifier,
external_asset_.metadata_ = std::move(metadata);
}
AssetRepresentation::AssetRepresentation(AssetIdentifier &&identifier,
AssetRepresentation::AssetRepresentation(StringRef relative_path,
ID &id,
const AssetLibrary &owner_asset_library)
: identifier_(std::move(identifier)),
: owner_asset_library_(owner_asset_library),
relative_identifier_(relative_path),
is_local_id_(true),
owner_asset_library_(owner_asset_library),
local_asset_id_(&id)
{
if (!id.asset_data) {
@@ -52,14 +56,9 @@ AssetRepresentation::~AssetRepresentation()
}
}
const AssetIdentifier &AssetRepresentation::get_identifier() const
{
return identifier_;
}
AssetWeakReference AssetRepresentation::make_weak_reference() const
{
return AssetWeakReference::make_reference(owner_asset_library_, identifier_);
return AssetWeakReference::make_reference(owner_asset_library_, relative_identifier_);
}
StringRefNull AssetRepresentation::get_name() const
@@ -85,6 +84,33 @@ AssetMetaData &AssetRepresentation::get_metadata() const
return is_local_id_ ? *local_asset_id_->asset_data : *external_asset_.metadata_;
}
StringRefNull AssetRepresentation::library_relative_identifier() const
{
return relative_identifier_;
}
std::string AssetRepresentation::full_path() const
{
char filepath[FILE_MAX];
BLI_path_join(filepath,
sizeof(filepath),
owner_asset_library_.root_path().c_str(),
relative_identifier_.c_str());
return filepath;
}
std::string AssetRepresentation::full_library_path() const
{
std::string asset_path = full_path();
char blend_path[1090 /*FILE_MAX_LIBEXTRA*/];
if (!BKE_blendfile_library_path_explode(asset_path.c_str(), blend_path, nullptr, nullptr)) {
return {};
}
return blend_path;
}
std::optional<eAssetImportMethod> AssetRepresentation::get_import_method() const
{
return owner_asset_library_.import_method_;

View File

@@ -93,9 +93,8 @@ bool operator==(const AssetWeakReference &a, const AssetWeakReference &b)
return true;
}
AssetWeakReference AssetWeakReference::make_reference(
const asset_system::AssetLibrary &library,
const asset_system::AssetIdentifier &asset_identifier)
AssetWeakReference AssetWeakReference::make_reference(const asset_system::AssetLibrary &library,
const StringRef library_relative_identifier)
{
AssetWeakReference weak_ref{};
@@ -105,9 +104,8 @@ AssetWeakReference AssetWeakReference::make_reference(
weak_ref.asset_library_identifier = BLI_strdupn(name.c_str(), name.size());
}
StringRefNull relative_identifier = asset_identifier.library_relative_identifier();
weak_ref.relative_asset_identifier = BLI_strdupn(relative_identifier.c_str(),
relative_identifier.size());
weak_ref.relative_asset_identifier = BLI_strdupn(library_relative_identifier.data(),
library_relative_identifier.size());
return weak_ref;
}

View File

@@ -24,7 +24,7 @@ ID *asset_local_id_ensure_imported(Main &bmain, const asset_system::AssetReprese
return local_id;
}
std::string blend_path = asset.get_identifier().full_library_path();
std::string blend_path = asset.full_library_path();
if (blend_path.empty()) {
return nullptr;
}

View File

@@ -70,7 +70,7 @@ static const asset_system::AssetRepresentation *get_local_asset_from_relative_id
const asset_system::AssetRepresentation *matching_asset = nullptr;
list::iterate(library_ref, [&](asset_system::AssetRepresentation &asset) {
if (asset.get_identifier().library_relative_identifier() == relative_identifier) {
if (asset.library_relative_identifier() == relative_identifier) {
matching_asset = &asset;
return false;
}
@@ -110,7 +110,7 @@ const asset_system::AssetRepresentation *find_asset_from_weak_ref(
const asset_system::AssetRepresentation *matching_asset = nullptr;
list::iterate(library_ref, [&](asset_system::AssetRepresentation &asset) {
if (asset.get_identifier().full_path() == full_path) {
if (asset.full_path() == full_path) {
matching_asset = &asset;
return false;
}

View File

@@ -121,7 +121,7 @@ void AssetView::build_items()
const bool show_names = (shelf_.settings.display_flag & ASSETSHELF_SHOW_NAMES);
const StringRef identifier = asset->get_identifier().library_relative_identifier();
const StringRef identifier = asset->library_relative_identifier();
const int preview_id = [&]() -> int {
if (list::asset_image_is_loading(&library_ref_, &asset_handle)) {
return ICON_TEMP;

View File

@@ -47,7 +47,7 @@ class AssetTemporaryIDConsumer : NonCopyable, NonMovable {
ID *import_id(ID_Type id_type, Main &bmain, ReportList &reports)
{
const char *asset_name = asset_->get_name().c_str();
std::string blend_file_path = asset_->get_identifier().full_library_path();
std::string blend_file_path = asset_->full_library_path();
temp_lib_context_ = BLO_library_temp_load_id(
&bmain, blend_file_path.c_str(), id_type, asset_name, &reports);

View File

@@ -1174,7 +1174,7 @@ void filelist_file_get_full_path(const FileList *filelist,
char r_filepath[/*FILE_MAX_LIBEXTRA*/])
{
if (file->asset) {
const std::string asset_path = file->asset->get_identifier().full_path();
const std::string asset_path = file->asset->full_path();
BLI_strncpy(r_filepath, asset_path.c_str(), FILE_MAX_LIBEXTRA);
return;
}

View File

@@ -15,9 +15,11 @@
#ifdef __cplusplus
# include <memory>
namespace blender {
class StringRef;
}
namespace blender::asset_system {
class AssetLibrary;
class AssetIdentifier;
} // namespace blender::asset_system
#endif
@@ -147,9 +149,6 @@ typedef struct AssetLibraryReference {
* renamed, or when a file storing this is opened on a different system (with different
* Preferences).
*
* #AssetWeakReference is similar to #AssetIdentifier, but is designed for file storage, not for
* runtime references.
*
* It has two main components:
* - A reference to the asset library: The #eAssetLibraryType and if that is not enough to identify
* the library, a library name (typically given by the user, but may change).
@@ -186,9 +185,8 @@ typedef struct AssetWeakReference {
/**
* See AssetRepresentation::make_weak_reference().
*/
static AssetWeakReference make_reference(
const blender::asset_system::AssetLibrary &library,
const blender::asset_system::AssetIdentifier &asset_identifier);
static AssetWeakReference make_reference(const blender::asset_system::AssetLibrary &library,
blender::StringRef library_relative_identifier);
#endif
} AssetWeakReference;

View File

@@ -431,28 +431,28 @@ static PointerRNA rna_AssetRepresentation_local_id_get(PointerRNA *ptr)
static void rna_AssetRepresentation_full_library_path_get(PointerRNA *ptr, char *value)
{
const AssetRepresentation *asset = static_cast<const AssetRepresentation *>(ptr->data);
const std::string full_library_path = asset->get_identifier().full_library_path();
const std::string full_library_path = asset->full_library_path();
BLI_strncpy(value, full_library_path.c_str(), full_library_path.size() + 1);
}
static int rna_AssetRepresentation_full_library_path_length(PointerRNA *ptr)
{
const AssetRepresentation *asset = static_cast<const AssetRepresentation *>(ptr->data);
const std::string full_library_path = asset->get_identifier().full_library_path();
const std::string full_library_path = asset->full_library_path();
return full_library_path.size();
}
static void rna_AssetRepresentation_full_path_get(PointerRNA *ptr, char *value)
{
const AssetRepresentation *asset = static_cast<const AssetRepresentation *>(ptr->data);
const std::string full_path = asset->get_identifier().full_path();
const std::string full_path = asset->full_path();
BLI_strncpy(value, full_path.c_str(), full_path.size() + 1);
}
static int rna_AssetRepresentation_full_path_length(PointerRNA *ptr)
{
const AssetRepresentation *asset = static_cast<const AssetRepresentation *>(ptr->data);
const std::string full_path = asset->get_identifier().full_path();
const std::string full_path = asset->full_path();
return full_path.size();
}

View File

@@ -696,7 +696,7 @@ ID *WM_drag_asset_id_import(const bContext *C, wmDragAsset *asset_drag, const in
FILE_ACTIVE_COLLECTION;
const char *name = asset_drag->asset->get_name().c_str();
const std::string blend_path = asset_drag->asset->get_identifier().full_library_path();
const std::string blend_path = asset_drag->asset->full_library_path();
const ID_Type idtype = asset_drag->asset->get_id_type();
const bool use_relative_path = asset_drag->asset->get_use_relative_path();