Blender V5.3
asset_library.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 "AS_asset_catalog.hh"
12#include "AS_asset_library.hh"
15#include "AS_remote_library.hh"
16
17#include "BKE_lib_remap.hh"
18#include "BKE_main.hh"
19#include "BKE_preferences.h"
20
21#include "BLI_listbase.hh" // IWYU pragma: keep
22#include "BLI_path_utils.hh"
23#include "BLI_string.hh"
24
25#include "DNA_asset_types.h"
26#include "DNA_space_types.h"
27#include "DNA_userdef_types.h"
29
32#include "runtime_library.hh"
33#include "utils.hh"
34
35namespace blender {
36
37using namespace blender::asset_system;
38
40
42{
43 /* NOTE: Can probably removed once #WITH_DESTROY_VIA_LOAD_HANDLER gets enabled by default. */
44
46}
47
49 const AssetLibraryReference &library_reference)
50{
52 return service->get_asset_library(bmain, library_reference);
53}
54
55AssetLibrary *AS_asset_library_load_from_directory(const char *name, const char *library_dirpath)
56{
57 /* NOTE: Loading an asset library at this point only means loading the catalogs.
58 * Later on this should invoke reading of asset representations too. */
59
61 AssetLibrary *lib;
62 if (library_dirpath == nullptr || library_dirpath[0] == '\0') {
63 lib = service->get_asset_library_current_file();
64 }
65 else {
66 lib = service->get_asset_library_on_disk_custom(name, library_dirpath);
67 }
68 return lib;
69}
70
75
81
83 const AssetLibraryReference &library_reference)
84{
86}
87
89{
91 &U, input_path.c_str()))
92 {
93 return preferences_lib->resolved_dirpath;
94 }
95
96 char buffer[FILE_MAXDIR];
97 BLI_path_split_dir_part(input_path.c_str(), buffer, FILE_MAXDIR);
98 return buffer;
99}
100
105
107{
110 [mappings](AssetLibrary &library) { library.remap_ids_and_remove_invalid(mappings); }, true);
111}
112
114 char r_path_buffer[/*FILE_MAX_LIBEXTRA*/ 1282],
115 char **r_dir,
116 char **r_group,
117 char **r_name)
118{
120 std::optional<AssetLibraryService::ExplodedPath> exploded =
121 service->resolve_asset_weak_reference_to_exploded_path(*asset_reference);
122
123 if (!exploded) {
124 if (r_dir) {
125 *r_dir = nullptr;
126 }
127 if (r_group) {
128 *r_group = nullptr;
129 }
130 if (r_name) {
131 *r_name = nullptr;
132 }
133 r_path_buffer[0] = '\0';
134 return;
135 }
136
137 BLI_assert(!exploded->group_component.is_empty());
138 BLI_assert(!exploded->name_component.is_empty());
139
140 BLI_strncpy(r_path_buffer, exploded->full_path->c_str(), /*FILE_MAX_LIBEXTRA*/ 1282);
141
142 if (!exploded->dir_component.is_empty()) {
143 r_path_buffer[exploded->dir_component.size()] = '\0';
144 r_path_buffer[exploded->dir_component.size() + 1 + exploded->group_component.size()] = '\0';
145
146 if (r_dir) {
147 *r_dir = r_path_buffer;
148 }
149 if (r_group) {
150 *r_group = r_path_buffer + exploded->dir_component.size() + 1;
151 }
152 if (r_name) {
153 *r_name = r_path_buffer + exploded->dir_component.size() + 1 +
154 exploded->group_component.size() + 1;
155 }
156 }
157 else {
158 r_path_buffer[exploded->group_component.size()] = '\0';
159
160 if (r_dir) {
161 *r_dir = nullptr;
162 }
163 if (r_group) {
164 *r_group = r_path_buffer;
165 }
166 if (r_name) {
167 *r_name = r_path_buffer + exploded->group_component.size() + 1;
168 }
169 }
170}
171
173{
174 for (bUserAssetLibrary &library : U.asset_libraries) {
175 if (U.experimental.no_data_block_packing) {
176 if (library.import_method == ASSET_IMPORT_PACK) {
177 library.import_method = ASSET_IMPORT_APPEND_REUSE;
178 }
179 }
180 else {
181 if (library.import_method == ASSET_IMPORT_APPEND_REUSE) {
182 library.import_method = ASSET_IMPORT_PACK;
183 }
184 }
185 }
186}
187
189{
190 for (bScreen &screen : bmain.screens) {
191 for (ScrArea &area : screen.areabase) {
192 for (SpaceLink &sl : area.spacedata) {
193 if (sl.spacetype != SPACE_FILE) {
194 continue;
195 }
196 SpaceFile *sfile = reinterpret_cast<SpaceFile *>(&sl);
197 if (!sfile->asset_params) {
198 continue;
199 }
200 if (U.experimental.no_data_block_packing) {
203 }
204 }
205 else {
208 }
209 }
210 }
211 }
212 }
213}
214
220
221namespace asset_system {
222
224 const bool is_read_only,
227 : library_type_(library_type),
228 is_read_only_(is_read_only),
229 name_(name),
230 root_path_(std::make_shared<std::string>(utils::resolve_directory_path(root_path))),
231 catalog_service_(std::make_unique<AssetCatalogService>(
232 *root_path_,
233 is_read_only ? std::optional{AssetCatalogService::read_only_tag{}} : std::nullopt))
234{
235}
236
243
245 const bool include_all_library)
246{
248 service->foreach_loaded_asset_library(fn, include_all_library);
249}
250
252{
253 /* Default implementation is a no-op. */
254}
255
257{
258 return nullptr;
259}
260
262{
263 return true;
264}
265
266std::optional<StringRefNull> AssetLibrary::remote_url() const
267{
268 /* Remote asset library support is implemented in #RemoteAssetLibrary::remote_url(). */
269 return {};
270};
271
272std::optional<StringRefNull> AssetLibrary::auth_token() const
273{
274 return std::nullopt;
275}
276
282
283std::shared_ptr<AssetCatalogService> AssetLibrary::catalog_service_ptr() const
284{
285 std::lock_guard lock{catalog_service_mutex_};
286 return catalog_service_;
287}
288
290{
291 /* To be implemented by a subclass, like #OnDiskAssetLibrary::refresh_catalogs. */
292}
293
295{
296 std::lock_guard lock{catalog_service_mutex_};
297
298 /* Should never actually be the case, catalog service gets allocated with the asset library. */
299 if (catalog_service_ == nullptr) {
300 auto catalog_service = std::make_unique<AssetCatalogService>(*root_path_);
303 return;
304 }
305
306 /* The catalog service was created before without being associated with a definition file. */
307 if (catalog_service_->get_catalog_definition_file() == nullptr) {
308 catalog_service_->load_from_disk();
310 this->refresh_catalogs();
311 }
312 }
313 else {
314 this->refresh_catalogs();
315 }
316}
317
318std::weak_ptr<AssetRepresentation> AssetLibrary::add_external_on_disk_asset(
319 StringRef relative_asset_path,
321 const int id_type,
322 std::unique_ptr<AssetMetaData> metadata)
323{
324 std::scoped_lock lock{asset_storage_.external_assets_mutex};
325 return asset_storage_.external_assets.lookup_key_or_add(std::make_shared<AssetRepresentation>(
326 relative_asset_path, name, id_type, std::move(metadata), *this));
327}
328
329std::weak_ptr<AssetRepresentation> AssetLibrary::add_external_online_asset(
330 StringRef relative_asset_path,
332 const int id_type,
333 std::unique_ptr<AssetMetaData> metadata,
334 OnlineAssetInfo online_info)
335{
336 std::scoped_lock lock{asset_storage_.external_assets_mutex};
337 return asset_storage_.external_assets.lookup_key_or_add(std::make_shared<AssetRepresentation>(
338 relative_asset_path, name, id_type, std::move(metadata), *this, online_info));
339}
340
341std::weak_ptr<AssetRepresentation> AssetLibrary::add_local_id_asset(ID &id)
342{
343 std::scoped_lock lock{asset_storage_.local_id_assets_mutex};
344 return asset_storage_.local_id_assets.lookup_key_or_add(
345 std::make_shared<AssetRepresentation>(id, *this));
346}
347
349{
350 /* Make sure this is forwarded to the library actually owning the asset if needed. For example
351 * the "All Libraries" library doesn't own the assets itself. */
352 if (&asset.owner_asset_library_ != this) {
353 return asset.owner_asset_library_.remove_asset(asset);
354 }
355
356 std::scoped_lock lock{asset_storage_.external_assets_mutex,
357 asset_storage_.local_id_assets_mutex};
358
359 BLI_assert(asset_storage_.local_id_assets.contains_as(&asset) ||
360 asset_storage_.external_assets.contains_as(&asset));
361
362 if (asset_storage_.local_id_assets.remove_as(&asset)) {
363 return true;
364 }
365 return asset_storage_.external_assets.remove_as(&asset);
366}
367
369{
370 Set<AssetRepresentation *> removed_assets;
371
372 {
373 std::scoped_lock lock{asset_storage_.local_id_assets_mutex};
374
375 for (const auto &asset_ptr : asset_storage_.local_id_assets) {
376 AssetRepresentation &asset = *asset_ptr;
377 BLI_assert(asset.is_local_id());
378
379 const IDRemapperApplyResult result = mappings.apply(&std::get<ID *>(asset.asset_),
381
382 /* Entirely remove assets whose ID is unset. We don't want assets with a null ID pointer. */
384 removed_assets.add(&asset);
385 }
386 }
387 }
388
389 for (AssetRepresentation *asset : removed_assets) {
390 this->remove_asset(*asset);
391 }
392}
393
394namespace {
395void asset_library_on_save_post(Main *bmain,
396 PointerRNA **pointers,
397 const int num_pointers,
398 void *arg)
399{
400 AssetLibrary *asset_lib = static_cast<AssetLibrary *>(arg);
401
402 /* Transform 'runtime' current file library into 'on-disk' current file library. */
403 if (asset_lib->library_type() == ASSET_LIBRARY_LOCAL && asset_lib->root_path().is_empty()) {
404 BLI_assert(dynamic_cast<RuntimeAssetLibrary *>(asset_lib) != nullptr);
405
406 if (AssetLibrary *on_disk_lib =
408 {
409 /* Allow undoing to the state before merging in catalogs from disk. */
410 on_disk_lib->catalog_service().undo_push();
411
412 /* Force refresh to merge on-disk catalogs with the ones stolen from the runtime library. */
414 ASSET_LIBRARY_LOCAL, on_disk_lib->root_path());
415 BLI_assert(asset_lib == on_disk_lib);
416 }
417 }
418
419 asset_lib->on_blend_save_post(bmain, pointers, num_pointers);
420}
421
422} // namespace
423
425{
426 /* The callback system doesn't own `on_save_callback_store_`. */
428
429 on_save_callback_store_.func = asset_library_on_save_post;
431
433}
434
441
443 PointerRNA ** /*pointers*/,
444 const int /*num_pointers*/)
445{
447 this->catalog_service().write_to_disk(bmain->filepath);
448 }
449}
450
452 const AssetWeakReference &asset_reference)
453{
455 return service->resolve_asset_weak_reference_to_full_path(asset_reference);
456}
457
459{
460 if (BLI_uuid_is_nil(asset_data->catalog_id)) {
461 asset_data->catalog_simple_name[0] = '\0';
462 return;
463 }
464 const AssetCatalog *catalog = this->catalog_service().find_catalog(asset_data->catalog_id);
465 if (catalog == nullptr) {
466 /* No-op if the catalog cannot be found. This could be the kind of "the catalog definition file
467 * is corrupt/lost" scenario that the simple name is meant to help recover from. */
468 return;
469 }
470 STRNCPY(asset_data->catalog_simple_name, catalog->simple_name.c_str());
471}
472
474{
475 return library_type_;
476}
477
479{
480 return name_;
481}
482
484{
485 return *root_path_;
486}
487
489{
490 return is_read_only_;
491}
492
494{
496 {
497 AssetLibraryReference library_ref{};
498 library_ref.custom_library_index = -1;
499 library_ref.type = ASSET_LIBRARY_ESSENTIALS;
500 result.append(library_ref);
501 }
502
503 const bool include_remote_libraries = USER_EXPERIMENTAL_TEST(&U, use_remote_asset_libraries);
504 const bool include_online_essentials = (U.asset_flag & USER_ASSETS_USE_ONLINE_ESSENTIALS) != 0;
505 if (include_remote_libraries && include_online_essentials) {
506 AssetLibraryReference library_ref{};
507 library_ref.custom_library_index = -1;
509 result.append(library_ref);
510 }
511
512 for (const auto [i, asset_library] : U.asset_libraries.enumerate()) {
513 if (!BKE_preferences_asset_library_is_valid(&U, &asset_library, true)) {
514 continue;
515 }
516 AssetLibraryReference library_ref{};
517 library_ref.custom_library_index = i;
518 library_ref.type = ASSET_LIBRARY_CUSTOM;
519 result.append(library_ref);
520 }
521
522 AssetLibraryReference library_ref{};
523 library_ref.custom_library_index = -1;
524 library_ref.type = ASSET_LIBRARY_LOCAL;
525 result.append(library_ref);
526 return result;
527}
528
530{
531 AssetLibraryReference all_library_ref{};
532 all_library_ref.custom_library_index = -1;
533 all_library_ref.type = ASSET_LIBRARY_ALL;
534 return all_library_ref;
535}
536
538{
539 AssetLibraryReference all_library_ref{};
540 all_library_ref.custom_library_index = -1;
541 all_library_ref.type = ASSET_LIBRARY_ESSENTIALS;
542 return all_library_ref;
543}
544
546{
547 AssetLibraryReference library_ref{};
548 library_ref.custom_library_index = -1;
549 library_ref.type = ASSET_LIBRARY_LOCAL;
550 return library_ref;
551}
552
554{
555 AssetLibraryReference library_ref{};
556 library_ref.custom_library_index = -1;
558 return library_ref;
559}
560
566
572
574{
575 switch (reference.type) {
576 /* Also returns true since it contains the online essentials. */
578 return true;
581 return true;
583 if (bUserAssetLibrary *asset_library =
585 {
586 if (asset_library->flag & ASSET_LIBRARY_USE_REMOTE_URL) {
587 return true;
588 }
589 }
590 break;
591 }
593 return false;
594 }
595
596 return false;
597}
598
600{
601 switch (library.library_type()) {
602 case ASSET_LIBRARY_ALL: {
603 if (is_online_essentials_url(remote_url)) {
604 return true;
605 }
606 bool has_match = false;
608 [&](const AssetLibrary &nested) {
609 if (nested.remote_url() == remote_url) {
610 has_match = true;
611 }
612 },
613 /*include_all_library=*/false);
614 return has_match;
615 }
618 return is_online_essentials_url(remote_url);
620 return library.remote_url() == remote_url;
622 return false;
623 }
624
625 return false;
626}
627
628} // namespace asset_system
629
630} // namespace blender
Main runtime representation of an asset.
#define FILE_MAXDIR
#define USER_EXPERIMENTAL_TEST(userdef, member)
volatile int lock
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr const char * c_str() const
AssetCatalog * find_catalog(CatalogID catalog_id) const
bool write_to_disk(const CatalogFilePath &blend_file_path)
void foreach_loaded_asset_library(FunctionRef< void(AssetLibrary &)> fn, bool include_all_library) const
AssetLibrary * get_asset_library_on_disk_builtin(eAssetLibraryType type, StringRefNull root_path)
AssetLibrary * get_asset_library(const Main *bmain, const AssetLibraryReference &library_reference)
static bUserAssetLibrary * find_custom_asset_library_from_library_ref(const AssetLibraryReference &library_reference)
std::optional< ExplodedPath > resolve_asset_weak_reference_to_exploded_path(const AssetWeakReference &asset_reference)
static AssetLibrary * move_runtime_current_file_into_on_disk_library(const Main &bmain)
std::string resolve_asset_weak_reference_to_full_path(const AssetWeakReference &asset_reference)
AssetLibrary * get_asset_library_on_disk_custom(StringRef name, StringRefNull root_path)
static std::string root_path_from_library_ref(const AssetLibraryReference &library_reference)
void on_blend_save_post(Main *bmain, PointerRNA **pointers, int num_pointers)
eAssetLibraryType library_type() const
std::shared_ptr< AssetCatalogService > catalog_service_
virtual bool use_relative_paths() const
virtual const bUserAssetLibrary * user_asset_library() const
std::shared_ptr< AssetCatalogService > catalog_service_ptr() const
std::weak_ptr< AssetRepresentation > add_local_id_asset(ID &id)
virtual std::optional< StringRefNull > remote_url() const
std::weak_ptr< AssetRepresentation > add_external_on_disk_asset(StringRef relative_asset_path, StringRef name, int id_type, std::unique_ptr< AssetMetaData > metadata)
std::weak_ptr< AssetRepresentation > add_external_online_asset(StringRef relative_asset_path, StringRef name, int id_type, std::unique_ptr< AssetMetaData > metadata, OnlineAssetInfo online_info)
AssetLibrary(eAssetLibraryType library_type, bool is_read_only, StringRef name="", StringRef root_path="")
AssetCatalogService & catalog_service() const
void remap_ids_and_remove_invalid(const bke::id::IDRemapper &mappings)
static void foreach_loaded(FunctionRef< void(AssetLibrary &)> fn, bool include_all_library)
virtual std::optional< StringRefNull > auth_token() const
bool remove_asset(AssetRepresentation &asset)
void refresh_catalog_simplename(AssetMetaData *asset_data)
std::string resolve_asset_weak_reference_to_full_path(const AssetWeakReference &asset_reference)
virtual void force_remote_listing_download() const
IDRemapperApplyResult apply(ID **r_id_ptr, IDRemapperApplyOptions options, ID *id_self=nullptr) const
constexpr bool is_empty() const
std::string resolve_directory_path(StringRef directory)
AssetLibraryReference all_library_reference()
AssetLibraryReference essentials_library_reference()
AssetLibraryReference current_file_library_reference()
void all_library_reload_catalogs_if_dirty()
Vector< AssetLibraryReference > all_valid_asset_library_refs()
bool is_or_contains_remote_libraries(const AssetLibraryReference &reference)
bool contains_assets_from_remote_url(const AssetLibrary &library, StringRef remote_url)
AssetLibraryReference online_essentials_library_reference()
bool is_online_essentials_url(StringRef url)
BMesh Iterators.
@ ID_REMAP_RESULT_SOURCE_UNASSIGNED
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ USER_ASSETS_USE_ONLINE_ESSENTIALS
@ FILE_ASSET_IMPORT_APPEND_REUSE
@ FILE_ASSET_IMPORT_PACK
char * STRNCPY(char(&dst)[N], const char *src)
std::string AS_asset_library_find_suitable_root_path_from_path(StringRefNull input_path)
static void update_import_method_for_asset_browsers(Main &bmain)
bool BLI_uuid_is_nil(bUUID uuid)
Definition uuid.cc:72
@ BKE_CB_EVT_SAVE_POST
@ ASSET_LIBRARY_USE_REMOTE_URL
static void update_import_method_for_user_libraries()
bool AS_asset_library_has_any_unsaved_catalogs()
void AS_asset_full_path_explode_from_weak_ref(const AssetWeakReference *asset_reference, char r_path_buffer[1282], char **r_dir, char **r_group, char **r_name)
bool BKE_preferences_asset_library_is_valid(const UserDef *userdef, const struct bUserAssetLibrary *library, const bool check_directory_exists) ATTR_NONNULL()
@ ASSET_LIBRARY_CUSTOM
@ ASSET_LIBRARY_ESSENTIALS
@ ASSET_LIBRARY_ONLINE_ESSENTIALS
@ ASSET_LIBRARY_LOCAL
void BKE_callback_add(bCallbackFuncStore *funcstore, eCbEvent evt)
Definition callbacks.cc:87
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
std::string AS_asset_library_resolve_path(StringRef path)
@ ASSET_IMPORT_APPEND_REUSE
std::string AS_asset_library_find_suitable_root_path_from_main(const Main *bmain)
void BKE_callback_remove(bCallbackFuncStore *funcstore, eCbEvent evt)
Definition callbacks.cc:94
void AS_asset_library_import_method_ensure_valid(Main &bmain)
void AS_asset_libraries_exit()
asset_system::AssetLibrary * AS_asset_library_load(const Main *bmain, const AssetLibraryReference &library_reference)
void AS_asset_library_remap_ids(const bke::id::IDRemapper &mappings)
std::string AS_asset_library_root_path_from_library_ref(const AssetLibraryReference &library_reference)
@ ID_REMAP_APPLY_DEFAULT
asset_system::AssetLibrary * AS_asset_library_load_from_directory(const char *name, const char *library_dirpath)
struct bUserAssetLibrary * BKE_preferences_asset_library_containing_path(const struct UserDef *userdef, const char *path) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
The meta-data of an asset. By creating and giving this for a data-block (ID.asset_data),...
eFileAssetImportMethod import_method
char filepath[1024]
Definition BKE_main.hh:267
ListBaseT< bScreen > screens
Definition BKE_main.hh:400
ListBaseT< SpaceLink > spacedata
FileAssetSelectParams * asset_params
void(*) func(Main *, PointerRNA **, int pointers_num, void *arg)
ListBaseT< struct ScrArea > areabase