2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2023-03-30 12:25:42 +02:00
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
|
* \ingroup bke
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
2023-09-01 21:37:11 +02:00
|
|
|
#include "BLI_string.h"
|
|
|
|
|
|
2023-03-30 12:25:42 +02:00
|
|
|
#include "AS_asset_identifier.hh"
|
|
|
|
|
#include "AS_asset_library.hh"
|
|
|
|
|
|
2023-11-16 11:41:55 +01:00
|
|
|
#include "BKE_asset.hh"
|
2023-03-30 12:25:42 +02:00
|
|
|
|
2023-08-28 15:01:05 +02:00
|
|
|
#include "BLO_read_write.hh"
|
2023-03-30 12:25:42 +02:00
|
|
|
|
|
|
|
|
#include "DNA_asset_types.h"
|
|
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
|
|
using namespace blender;
|
|
|
|
|
|
|
|
|
|
/* #AssetWeakReference -------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
AssetWeakReference::AssetWeakReference()
|
|
|
|
|
: asset_library_type(0), asset_library_identifier(nullptr), relative_asset_identifier(nullptr)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-21 10:45:24 -05:00
|
|
|
AssetWeakReference::AssetWeakReference(const AssetWeakReference &other)
|
|
|
|
|
: asset_library_type(other.asset_library_type),
|
|
|
|
|
asset_library_identifier(BLI_strdup_null(other.asset_library_identifier)),
|
|
|
|
|
relative_asset_identifier(BLI_strdup_null(other.relative_asset_identifier))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-30 12:25:42 +02:00
|
|
|
AssetWeakReference::AssetWeakReference(AssetWeakReference &&other)
|
|
|
|
|
: asset_library_type(other.asset_library_type),
|
|
|
|
|
asset_library_identifier(other.asset_library_identifier),
|
|
|
|
|
relative_asset_identifier(other.relative_asset_identifier)
|
|
|
|
|
{
|
|
|
|
|
other.asset_library_type = 0; /* Not a valid type. */
|
|
|
|
|
other.asset_library_identifier = nullptr;
|
|
|
|
|
other.relative_asset_identifier = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AssetWeakReference::~AssetWeakReference()
|
|
|
|
|
{
|
|
|
|
|
MEM_delete(asset_library_identifier);
|
|
|
|
|
MEM_delete(relative_asset_identifier);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-21 10:45:24 -05:00
|
|
|
AssetWeakReference &AssetWeakReference::operator=(const AssetWeakReference &other)
|
2023-03-30 12:25:42 +02:00
|
|
|
{
|
2024-02-21 10:45:24 -05:00
|
|
|
if (this == &other) {
|
|
|
|
|
return *this;
|
2023-03-30 12:25:42 +02:00
|
|
|
}
|
2024-02-21 10:45:24 -05:00
|
|
|
std::destroy_at(this);
|
|
|
|
|
new (this) AssetWeakReference(other);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2023-03-30 12:25:42 +02:00
|
|
|
|
2024-02-21 10:45:24 -05:00
|
|
|
AssetWeakReference &AssetWeakReference::operator=(AssetWeakReference &&other)
|
|
|
|
|
{
|
|
|
|
|
if (this == &other) {
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
std::destroy_at(this);
|
|
|
|
|
new (this) AssetWeakReference(std::move(other));
|
|
|
|
|
return *this;
|
2023-03-30 12:25:42 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-01 13:24:39 -04:00
|
|
|
bool operator==(const AssetWeakReference &a, const AssetWeakReference &b)
|
|
|
|
|
{
|
|
|
|
|
if (a.asset_library_type != b.asset_library_type) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (StringRef(a.asset_library_identifier) != StringRef(b.asset_library_identifier)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (StringRef(a.relative_asset_identifier) != StringRef(b.relative_asset_identifier)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-16 11:57:16 -05:00
|
|
|
AssetWeakReference AssetWeakReference::make_reference(
|
2023-03-30 12:25:42 +02:00
|
|
|
const asset_system::AssetLibrary &library,
|
|
|
|
|
const asset_system::AssetIdentifier &asset_identifier)
|
|
|
|
|
{
|
2024-02-16 11:57:16 -05:00
|
|
|
AssetWeakReference weak_ref{};
|
2023-03-30 12:25:42 +02:00
|
|
|
|
2024-02-16 11:57:16 -05:00
|
|
|
weak_ref.asset_library_type = library.library_type();
|
2023-03-30 12:25:42 +02:00
|
|
|
StringRefNull name = library.name();
|
|
|
|
|
if (!name.is_empty()) {
|
2024-02-16 11:57:16 -05:00
|
|
|
weak_ref.asset_library_identifier = BLI_strdupn(name.c_str(), name.size());
|
2023-03-30 12:25:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringRefNull relative_identifier = asset_identifier.library_relative_identifier();
|
2024-02-16 11:57:16 -05:00
|
|
|
weak_ref.relative_asset_identifier = BLI_strdupn(relative_identifier.c_str(),
|
|
|
|
|
relative_identifier.size());
|
2023-03-30 12:25:42 +02:00
|
|
|
|
|
|
|
|
return weak_ref;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_asset_weak_reference_write(BlendWriter *writer, const AssetWeakReference *weak_ref)
|
|
|
|
|
{
|
|
|
|
|
BLO_write_struct(writer, AssetWeakReference, weak_ref);
|
|
|
|
|
BLO_write_string(writer, weak_ref->asset_library_identifier);
|
|
|
|
|
BLO_write_string(writer, weak_ref->relative_asset_identifier);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_asset_weak_reference_read(BlendDataReader *reader, AssetWeakReference *weak_ref)
|
|
|
|
|
{
|
2024-04-24 17:01:22 +02:00
|
|
|
BLO_read_string(reader, &weak_ref->asset_library_identifier);
|
|
|
|
|
BLO_read_string(reader, &weak_ref->relative_asset_identifier);
|
2023-03-30 12:25:42 +02:00
|
|
|
}
|
2024-05-03 10:20:01 -04:00
|
|
|
|
|
|
|
|
void BKE_asset_catalog_path_list_free(ListBase &catalog_path_list)
|
|
|
|
|
{
|
|
|
|
|
LISTBASE_FOREACH_MUTABLE (AssetCatalogPathLink *, catalog_path, &catalog_path_list) {
|
|
|
|
|
MEM_delete(catalog_path->path);
|
|
|
|
|
BLI_freelinkN(&catalog_path_list, catalog_path);
|
|
|
|
|
}
|
|
|
|
|
BLI_assert(BLI_listbase_is_empty(&catalog_path_list));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ListBase BKE_asset_catalog_path_list_duplicate(const ListBase &catalog_path_list)
|
|
|
|
|
{
|
|
|
|
|
ListBase duplicated_list = {nullptr};
|
|
|
|
|
|
|
|
|
|
LISTBASE_FOREACH (AssetCatalogPathLink *, catalog_path, &catalog_path_list) {
|
|
|
|
|
AssetCatalogPathLink *copied_path = MEM_cnew<AssetCatalogPathLink>(__func__);
|
|
|
|
|
copied_path->path = BLI_strdup(catalog_path->path);
|
|
|
|
|
|
|
|
|
|
BLI_addtail(&duplicated_list, copied_path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return duplicated_list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_asset_catalog_path_list_blend_write(BlendWriter *writer,
|
|
|
|
|
const ListBase &catalog_path_list)
|
|
|
|
|
{
|
|
|
|
|
LISTBASE_FOREACH (const AssetCatalogPathLink *, catalog_path, &catalog_path_list) {
|
|
|
|
|
BLO_write_struct(writer, AssetCatalogPathLink, catalog_path);
|
|
|
|
|
BLO_write_string(writer, catalog_path->path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_asset_catalog_path_list_blend_read_data(BlendDataReader *reader,
|
|
|
|
|
ListBase &catalog_path_list)
|
|
|
|
|
{
|
|
|
|
|
BLO_read_struct_list(reader, AssetCatalogPathLink, &catalog_path_list);
|
|
|
|
|
LISTBASE_FOREACH (AssetCatalogPathLink *, catalog_path, &catalog_path_list) {
|
|
|
|
|
BLO_read_data_address(reader, &catalog_path->path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BKE_asset_catalog_path_list_has_path(const ListBase &catalog_path_list,
|
|
|
|
|
const char *catalog_path)
|
|
|
|
|
{
|
|
|
|
|
return BLI_findstring_ptr(
|
|
|
|
|
&catalog_path_list, catalog_path, offsetof(AssetCatalogPathLink, path)) != nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_asset_catalog_path_list_add_path(ListBase &catalog_path_list, const char *catalog_path)
|
|
|
|
|
{
|
|
|
|
|
AssetCatalogPathLink *new_path = MEM_cnew<AssetCatalogPathLink>(__func__);
|
|
|
|
|
new_path->path = BLI_strdup(catalog_path);
|
|
|
|
|
BLI_addtail(&catalog_path_list, new_path);
|
|
|
|
|
}
|