Blender V5.3
asset_weak_reference.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <memory>
10
11#include "BLI_listbase.hh"
12#include "BLI_path_utils.hh"
13#include "BLI_string.hh"
14
15#include "AS_asset_library.hh"
16
17#include "BKE_asset.hh"
18#include "BKE_idtype.hh"
19#include "BKE_main.hh"
20
21#include "BLO_read_write.hh"
22
23#include "DNA_asset_types.h"
24
25#include "MEM_guardedalloc.h"
26
27namespace blender {
28
29/* #AssetWeakReference -------------------------------------------- */
30
31AssetWeakReference::AssetWeakReference() = default;
32
33AssetWeakReference::AssetWeakReference(const AssetWeakReference &other)
34 : asset_library_type(other.asset_library_type),
35 asset_library_identifier(BLI_strdup_null(other.asset_library_identifier)),
36 relative_asset_identifier(BLI_strdup_null(other.relative_asset_identifier))
37{
38}
39
40AssetWeakReference::AssetWeakReference(AssetWeakReference &&other)
41 : asset_library_type(other.asset_library_type),
42 asset_library_identifier(other.asset_library_identifier),
43 relative_asset_identifier(other.relative_asset_identifier)
44{
45 other.asset_library_type = eAssetLibraryType{}; /* Not a valid type. */
46 other.asset_library_identifier = nullptr;
47 other.relative_asset_identifier = nullptr;
48}
49
50AssetWeakReference::~AssetWeakReference()
51{
52 MEM_delete(asset_library_identifier);
53 MEM_delete(relative_asset_identifier);
54}
55
56AssetWeakReference &AssetWeakReference::operator=(const AssetWeakReference &other)
57{
58 if (this == &other) {
59 return *this;
60 }
61 std::destroy_at(this);
62 new (this) AssetWeakReference(other);
63 return *this;
64}
65
66AssetWeakReference &AssetWeakReference::operator=(AssetWeakReference &&other)
67{
68 if (this == &other) {
69 return *this;
70 }
71 std::destroy_at(this);
72 new (this) AssetWeakReference(std::move(other));
73 return *this;
74}
75
77{
78 if (a.asset_library_type != b.asset_library_type) {
79 return false;
80 }
81
83 const char *a_lib_idenfifier = a.asset_library_identifier ? a.asset_library_identifier : "";
84 const char *b_lib_idenfifier = b.asset_library_identifier ? b.asset_library_identifier : "";
85 if (BLI_path_cmp_normalized(a_lib_idenfifier, b_lib_idenfifier) != 0) {
86 return false;
87 }
88 }
89 const char *a_asset_idenfifier = a.relative_asset_identifier ? a.relative_asset_identifier : "";
90 const char *b_asset_idenfifier = b.relative_asset_identifier ? b.relative_asset_identifier : "";
91 if (BLI_path_cmp_normalized(a_asset_idenfifier, b_asset_idenfifier) != 0) {
92 return false;
93 }
94 return true;
95}
96
97AssetWeakReference AssetWeakReference::make_reference(const asset_system::AssetLibrary &library,
98 const StringRef library_relative_identifier)
99{
100 AssetWeakReference weak_ref{};
101
103 !(library.library_type() == ASSET_LIBRARY_CUSTOM && library.name().is_empty()),
104 "Custom asset libraries should have a name set, otherwise weak references will not work");
105
106 weak_ref.asset_library_type = library.library_type();
107 StringRefNull name = library.name();
108 if (!name.is_empty()) {
109 weak_ref.asset_library_identifier = BLI_strdupn(name.c_str(), name.size());
110 }
111
112 weak_ref.relative_asset_identifier = BLI_strdupn(library_relative_identifier.data(),
113 library_relative_identifier.size());
114
115 return weak_ref;
116}
117
119{
120 writer->write_struct(weak_ref);
121 writer->write_string(weak_ref->asset_library_identifier);
122 writer->write_string(weak_ref->relative_asset_identifier);
123}
124
130
132 FunctionRef<void(AssetWeakReference &weak_ref)> fn)
133{
134 ID *id;
135 FOREACH_MAIN_ID_BEGIN (&bmain, id) {
136 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id);
137 if (id_type->foreach_asset_weak_reference) {
138 id_type->foreach_asset_weak_reference(id, fn);
139 }
140 }
142}
143
144void BKE_asset_catalog_path_list_free(ListBaseT<AssetCatalogPathLink> &catalog_path_list)
145{
146 for (AssetCatalogPathLink &catalog_path : catalog_path_list.items_mutable()) {
147 MEM_delete(catalog_path.path);
148 BLI_freelinkN(&catalog_path_list, &catalog_path);
149 }
150 BLI_assert(catalog_path_list.is_empty());
151}
152
153ListBaseT<AssetCatalogPathLink> BKE_asset_catalog_path_list_duplicate(
154 const ListBaseT<AssetCatalogPathLink> &catalog_path_list)
155{
156 ListBaseT<AssetCatalogPathLink> duplicated_list = {nullptr};
157
158 for (AssetCatalogPathLink &catalog_path : catalog_path_list) {
159 AssetCatalogPathLink *copied_path = MEM_new<AssetCatalogPathLink>(__func__);
160 copied_path->path = BLI_strdup(catalog_path.path);
161
162 BLI_addtail(&duplicated_list, copied_path);
163 }
164
165 return duplicated_list;
166}
167
169 BlendWriter *writer, const ListBaseT<AssetCatalogPathLink> &catalog_path_list)
170{
171 for (const AssetCatalogPathLink &catalog_path : catalog_path_list) {
172 writer->write_struct(&catalog_path);
173 writer->write_string(catalog_path.path);
174 }
175}
176
178 BlendDataReader *reader, ListBaseT<AssetCatalogPathLink> &catalog_path_list)
179{
180 BLO_read_struct_list(reader, AssetCatalogPathLink, &catalog_path_list);
181 for (AssetCatalogPathLink &catalog_path : catalog_path_list) {
182 BLO_read_string(reader, &catalog_path.path);
183 }
184}
185
186bool BKE_asset_catalog_path_list_has_path(const ListBaseT<AssetCatalogPathLink> &catalog_path_list,
187 const char *catalog_path)
188{
189 return BLI_findstring_ptr(
190 &catalog_path_list, catalog_path, offsetof(AssetCatalogPathLink, path)) != nullptr;
191}
192
193void BKE_asset_catalog_path_list_add_path(ListBaseT<AssetCatalogPathLink> &catalog_path_list,
194 const char *catalog_path)
195{
196 AssetCatalogPathLink *new_path = MEM_new<AssetCatalogPathLink>(__func__);
197 new_path->path = BLI_strdup(catalog_path);
198 BLI_addtail(&catalog_path_list, new_path);
199}
200
201bool BKE_asset_catalog_path_list_remove_path(ListBaseT<AssetCatalogPathLink> &catalog_path_list,
202 const char *catalog_path)
203{
204 bool changed = false;
205
206 for (AssetCatalogPathLink &path_link : catalog_path_list.items_mutable()) {
207 if (!STREQ(path_link.path, catalog_path)) {
208 continue;
209 }
210 MEM_delete(path_link.path);
211 BLI_freelinkN(&catalog_path_list, &path_link);
212 changed = true;
213 }
214
215 return changed;
216}
217
218} // namespace blender
#define FOREACH_MAIN_ID_END
Definition BKE_main.hh:735
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition BKE_main.hh:729
#define BLO_read_struct_list(reader, struct_name, list)
Read Blender memory allocation and freeing.
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
#define offsetof(t, d)
bool operator==(const Error &left, const Error &right)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_asset_catalog_path_list_free(ListBaseT< AssetCatalogPathLink > &catalog_path_list)
bool BKE_asset_catalog_path_list_has_path(const ListBaseT< AssetCatalogPathLink > &catalog_path_list, const char *catalog_path)
void BKE_asset_weak_reference_foreach_main(Main &bmain, FunctionRef< void(AssetWeakReference &weak_ref)> fn)
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
@ ASSET_LIBRARY_CUSTOM
const IDTypeInfo * BKE_idtype_get_info_from_id(const ID *id)
Definition idtype.cc:232
void * BLI_findstring_ptr(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:658
void BKE_asset_catalog_path_list_blend_read_data(BlendDataReader *reader, ListBaseT< AssetCatalogPathLink > &catalog_path_list)
int BLI_path_cmp_normalized(const char *p1, const char *p2) ATTR_NONNULL(1
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
void BKE_asset_weak_reference_write(BlendWriter *writer, const AssetWeakReference *weak_ref)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
bool BKE_asset_catalog_path_list_remove_path(ListBaseT< AssetCatalogPathLink > &catalog_path_list, const char *catalog_path)
void BKE_asset_weak_reference_read(BlendDataReader *reader, AssetWeakReference *weak_ref)
void BKE_asset_catalog_path_list_blend_write(BlendWriter *writer, const ListBaseT< AssetCatalogPathLink > &catalog_path_list)
char * BLI_strdup_null(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_MALLOC
void BKE_asset_catalog_path_list_add_path(ListBaseT< AssetCatalogPathLink > &catalog_path_list, const char *catalog_path)
ListBaseT< AssetCatalogPathLink > BKE_asset_catalog_path_list_duplicate(const ListBaseT< AssetCatalogPathLink > &catalog_path_list)
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
eAssetLibraryType asset_library_type
void write_string(const char *data)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
IDTypeForeachAssetWeakReferenceFunction foreach_asset_weak_reference