Blender V5.3
asset_list.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
14
15#include <optional>
16#include <string>
17
18#include "AS_asset_library.hh"
20
21#include "BKE_context.hh"
22#include "BKE_main.hh"
23#include "BKE_preferences.h"
24#include "BKE_screen.hh"
25
26#include "BLI_listbase.hh"
27#include "BLI_map.hh"
28#include "BLI_string.hh"
29#include "BLI_utility_mixins.hh"
30
31#include "DNA_asset_types.h"
32#include "DNA_space_enums.h"
33#include "DNA_space_types.h"
34
35#include "WM_api.hh"
36
37/* XXX uses private header of file-space. */
40
41#include "ED_asset_indexer.hh"
42#include "ED_asset_list.hh"
43#include "ED_fileselect.hh"
44#include "ED_screen.hh"
45
47
48/* TODO somehow update online asset status after downloaded by subscribing to
49 * #WM_MSG_TYPE_REMOTE_DOWNLOADER messages. */
50
52
53/* -------------------------------------------------------------------- */
58
63 static void filelist_free_fn(FileList *list)
64 {
66 }
67
68 std::unique_ptr<FileList, decltype(&filelist_free_fn)> file_list_;
69
70 public:
71 explicit FileListWrapper(eFileSelectType filesel_type)
72 : file_list_(filelist_new(filesel_type, /*is_from_global_asset_list=*/true),
73 filelist_free_fn)
74 {
75 }
76 FileListWrapper(FileListWrapper &&other) = default;
79 {
80 /* Destructs the owned pointer. */
81 file_list_ = nullptr;
82 }
83
84 operator FileList *() const
85 {
86 return file_list_.get();
87 }
88};
89
91 public:
94
95 AssetList() = delete;
96 AssetList(eFileSelectType filesel_type, const AssetLibraryReference &asset_library_ref);
97 AssetList(AssetList &&other) = default;
98 ~AssetList() = default;
99
100 static bool listen(const wmNotifier &notifier);
101
102 void ensure_updated();
103 void setup();
104 void fetch(const bContext &C);
105 void ensure_blocking(const bContext &C);
106 void clear(wmWindowManager *wm);
108
109 bool needs_refetch() const;
110 bool is_loaded() const;
112 void iterate(AssetListIterFn fn) const;
113 int size() const;
114 void tag_main_data_dirty() const;
115 void remap_id(ID *id_old, ID *id_new) const;
116};
117
118AssetList::AssetList(eFileSelectType filesel_type, const AssetLibraryReference &asset_library_ref)
119 : filelist_(filesel_type), library_ref_(asset_library_ref)
120{
121}
122
124{
125 FileList *files = filelist_;
126 std::string asset_lib_path = AS_asset_library_root_path_from_library_ref(library_ref_);
127
128 /* Relevant bits from file_refresh(). */
129 /* TODO pass options properly. */
132
133 const bool use_asset_indexer = !USER_DEVELOPER_TOOL_TEST(&U, no_asset_indexing);
135
136 char dirpath[FILE_MAX_LIBEXTRA] = "";
137 if (!asset_lib_path.empty()) {
138 STRNCPY(dirpath, asset_lib_path.c_str());
139 }
140 filelist_setdir(files, dirpath);
141}
142
144{
145 FileList *files = filelist_;
146
148
149 const bool show_online = ELEM(
151 const bool show_offline = ELEM(
153
155 files,
156 true,
157 true,
158 true, /* Just always hide parent, prefer to not add an extra user option for this. */
161 true,
162 /*filter_assets_hide_online=*/!show_online,
163 /*filter_assets_hide_offline=*/!show_offline,
164 "");
165 filelist_set_asset_include_online(files, show_online);
166}
167
169{
170 FileList *files = filelist_;
171
172 if (filelist_needs_force_reset(files)) {
175 }
176
177 if (filelist_needs_reading(files)) {
178 if (!filelist_pending(files)) {
180 }
181 }
182 filelist_sort(files);
183 filelist_filter(files);
184}
185
187{
188 FileList *files = filelist_;
189
190 if (filelist_needs_force_reset(files)) {
192 }
193
194 if (filelist_needs_reading(files)) {
196 }
197
198 filelist_sort(files);
199 filelist_filter(files);
200}
201
206
208{
210}
211
216
218{
219 FileList *files = filelist_;
220 const int numfiles = filelist_files_ensure(files);
221
222 for (int i = 0; i < numfiles; i++) {
224 if (!asset) {
225 continue;
226 }
227
228 if (!fn(*asset)) {
229 break;
230 }
231 }
232}
233
235{
236 /* Based on #ED_fileselect_clear() */
237
238 FileList *files = filelist_;
240 filelist_freelib(files);
241 filelist_clear(files);
243
245}
246
248{
249 /* Based on #ED_fileselect_clear_main_assets() */
250
251 FileList *files = filelist_;
253 filelist_freelib(files);
257
259}
260
264bool AssetList::listen(const wmNotifier &notifier)
265{
266 switch (notifier.category) {
267 case NC_ID: {
268 if (ELEM(notifier.action, NA_RENAME)) {
269 return true;
270 }
271 break;
272 }
273 case NC_ASSET:
275 return true;
276 }
278 return true;
279 }
280 break;
281 }
282
283 return false;
284}
285
290{
292}
293
305
306void AssetList::remap_id(ID * /*id_old*/, ID * /*id_new*/) const
307{
308 /* Trigger full re-fetch of the file list if main data was changed, don't even attempt remap
309 * pointers. We could give file list types a id-remap callback, but it's probably not worth it.
310 * Refreshing local file lists is relatively cheap. */
311 this->tag_main_data_dirty();
312}
313
315
316/* -------------------------------------------------------------------- */
319
320static void clear(const AssetLibraryReference *library_reference, wmWindowManager *wm);
321static void on_save_post(Main *main, PointerRNA **pointers, int num_pointers, void *arg);
322
327
340
345{
346 static GlobalStorage global_storage;
347 return global_storage.list_map;
348}
349
350static AssetList *lookup_list(const AssetLibraryReference &library_ref)
351{
352 return libraries_map().lookup_ptr(library_ref);
353}
354
356{
357 for (AssetList &list : libraries_map().values()) {
358 list.tag_main_data_dirty();
359 }
360}
361
362void storage_id_remap(ID *id_old, ID *id_new)
363{
364 for (AssetList &list : libraries_map().values()) {
365 list.remap_id(id_old, id_new);
366 }
367}
368
369static std::optional<eFileSelectType> asset_library_reference_to_fileselect_type(
370 const AssetLibraryReference &library_reference)
371{
372 switch (eAssetLibraryType(library_reference.type)) {
380 &U, library_reference.custom_library_index);
381 if (!user_library) {
382 /* The caller should make sure the passed library reference is valid. */
384 return std::nullopt;
385 }
386
387 if (user_library->flag & ASSET_LIBRARY_USE_REMOTE_URL) {
389 }
390 return FILE_ASSET_LIBRARY;
391 }
393 return FILE_MAIN_ASSET;
394 }
395
396 return std::nullopt;
397}
398
399using is_new_t = bool;
400static std::tuple<AssetList &, is_new_t> ensure_list_storage(
401 const AssetLibraryReference &library_reference, eFileSelectType filesel_type)
402{
404
405 if (AssetList *list = storage.lookup_ptr(library_reference)) {
406 return {*list, false};
407 }
408 storage.add(library_reference, AssetList(filesel_type, library_reference));
409 return {storage.lookup(library_reference), true};
410}
411
413
415{
416 const wmNotifier *wmn = params->notifier;
417 ARegion *region = params->region;
418
419 switch (wmn->category) {
420 case NC_ASSET:
423 }
424 if (ELEM(wmn->action, NA_DOWNLOAD_FINISHED)) {
426 }
427 break;
428 }
429}
430
431static void on_save_post(Main *main,
432 PointerRNA ** /*pointers*/,
433 int /*num_pointers*/,
434 void * /*arg*/)
435{
436 wmWindowManager *wm = main->wm.first();
437 const AssetLibraryReference current_file_library =
439 clear(&current_file_library, wm);
440}
441
442/* -------------------------------------------------------------------- */
445
446void storage_fetch(const AssetLibraryReference *library_reference, const bContext *C)
447{
448 std::optional filesel_type = asset_library_reference_to_fileselect_type(*library_reference);
449 if (!filesel_type) {
450 return;
451 }
452
453 auto [list, is_new] = ensure_list_storage(*library_reference, *filesel_type);
454 list.ensure_updated();
455
456 if (is_new || list.needs_refetch()) {
457 list.setup();
458 list.fetch(*C);
459 }
460}
461
462void storage_fetch_blocking(const AssetLibraryReference &library_reference, const bContext &C)
463{
464 std::optional filesel_type = asset_library_reference_to_fileselect_type(library_reference);
465 if (!filesel_type) {
466 /* TODO: Warn? */
467 return;
468 }
469
470 auto [list, is_new] = ensure_list_storage(library_reference, *filesel_type);
471 list.ensure_updated();
472
473 if (is_new || list.needs_refetch()) {
474 list.setup();
475 list.ensure_blocking(C);
476 }
477}
478
479bool is_loaded(const AssetLibraryReference *library_reference)
480{
481 AssetList *list = lookup_list(*library_reference);
482 if (!list) {
483 return false;
484 }
485 if (list->needs_refetch()) {
486 return false;
487 }
488 return list->is_loaded();
489}
490
492 const AssetLibraryReference &library_reference,
493 const wmWindowManager *wm,
494 const FunctionRef<void(SpaceFile &sfile)> fn)
495{
496 for (const wmWindow &win : wm->windows) {
497 const bScreen *screen = WM_window_get_active_screen(&win);
498 for (const ScrArea &area : screen->areabase) {
499 /* Only needs to cover visible file/asset browsers, since others are already cleared through
500 * area exiting. */
501 if (area.spacetype == SPACE_FILE) {
502 SpaceFile *sfile = area.spacedata.first_as<SpaceFile>();
503 if (sfile->browse_mode == FILE_BROWSE_MODE_ASSETS) {
504 if (sfile->asset_params && sfile->asset_params->asset_library_ref == library_reference) {
505 fn(*sfile);
506 }
507 }
508 }
509 }
510 }
511}
512
513void clear(const AssetLibraryReference *library_reference, wmWindowManager *wm)
514{
515 AssetList *list = lookup_list(*library_reference);
516 if (list) {
517 list->clear(wm);
518 }
519
520 /* Only needs to cover visible file/asset browsers, since others are already cleared through area
521 * exiting. */
523 *library_reference, wm, [&](SpaceFile &sfile) { ED_fileselect_clear(wm, &sfile); });
524
525 /* Always clear the all library when clearing a nested one. */
526 if (library_reference->type != ASSET_LIBRARY_ALL) {
528 AssetList *all_lib_list = lookup_list(all_lib_ref);
529
530 /* If the cleared nested library is the current file one, only clear current file assets. */
531 if (library_reference->type == ASSET_LIBRARY_LOCAL) {
532 if (all_lib_list) {
533 all_lib_list->clear_current_file_assets(wm);
534 }
535
537 all_lib_ref, wm, [&](SpaceFile &sfile) { ED_fileselect_clear_main_assets(wm, &sfile); });
538 }
539 else {
540 if (all_lib_list) {
541 all_lib_list->clear(wm);
542 }
543
545 all_lib_ref, wm, [&](SpaceFile &sfile) { ED_fileselect_clear(wm, &sfile); });
546 }
547 }
548}
549
550void clear(const AssetLibraryReference *library_reference, const bContext *C)
551{
552 clear(library_reference, CTX_wm_manager(C));
553}
554
556{
558 clear(&all_lib_ref, CTX_wm_manager(C));
559}
560
562 const StringRef library_url,
563 const StringRef downloaded_file_abspath)
564{
565 for (const wmWindow &win : wm.windows) {
566 const bScreen *screen = WM_window_get_active_screen(&win);
567 for (const ScrArea &area : screen->areabase) {
568 /* Only needs to cover visible file/asset browsers, since others are already cleared through
569 * area exiting. */
570 if (area.spacetype == SPACE_FILE) {
571 SpaceFile *sfile = area.spacedata.first_as<SpaceFile>();
572 if (sfile->browse_mode == FILE_BROWSE_MODE_ASSETS) {
574 sfile->files, library_url, downloaded_file_abspath);
575 }
576 }
577 }
578 }
579
580 for (AssetList &list : libraries_map().values()) {
582 list.filelist_, library_url, downloaded_file_abspath);
583 }
584
586}
587
589{
590 return lookup_list(*library_reference) != nullptr;
591}
592
594 const bContext *C)
595{
596 bool has_asset_browser = false;
598 *library_reference, CTX_wm_manager(C), [&](SpaceFile & /*sfile*/) {
599 has_asset_browser = true;
600 });
601
602 return has_asset_browser;
603}
604
605void iterate(const AssetLibraryReference &library_reference, AssetListIterFn fn)
606{
607 AssetList *list = lookup_list(library_reference);
608 if (list) {
609 list->iterate(fn);
610 }
611}
612
614 const AssetLibraryReference &library_reference)
615{
616 const AssetList *list = lookup_list(library_reference);
617 if (!list) {
618 return nullptr;
619 }
620 return list->asset_library();
621}
622
623bool listen(const wmNotifier *notifier)
624{
625 return AssetList::listen(*notifier);
626}
627
628int size(const AssetLibraryReference *library_reference)
629{
630 AssetList *list = lookup_list(*library_reference);
631 if (list) {
632 return list->size();
633 }
634 return -1;
635}
636
638{
640}
641
643
644} // namespace blender::ed::asset::list
Main runtime representation of an asset.
#define FILTER_ID_ALL
Definition DNA_ID.h:1241
#define FILE_MAX_LIBEXTRA
#define FILE_SELECT_MAX_RECURSIONS
#define USER_DEVELOPER_TOOL_TEST(userdef, member)
#define C
Definition RandGen.cpp:29
#define ND_ASSET_LIST_READING
Definition WM_types.hh:593
#define NC_ID
Definition WM_types.hh:408
#define NA_ADDED
Definition WM_types.hh:636
#define NA_EDITED
Definition WM_types.hh:634
#define NA_DOWNLOAD_FINISHED
Definition WM_types.hh:643
#define ND_ASSET_LIST_PREVIEW
Definition WM_types.hh:592
#define NC_ASSET
Definition WM_types.hh:417
#define NA_REMOVED
Definition WM_types.hh:637
#define ND_ASSET_LIST
Definition WM_types.hh:591
#define NA_RENAME
Definition WM_types.hh:638
#define BLI_assert_unreachable()
#define ELEM(...)
void clear()
Definition BLI_map.hh:1038
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
NonCopyable(const NonCopyable &other)=delete
static bool listen(const wmNotifier &notifier)
AssetList(AssetList &&other)=default
void remap_id(ID *id_old, ID *id_new) const
asset_system::AssetLibrary * asset_library() const
void fetch(const bContext &C)
AssetLibraryReference library_ref_
Definition asset_list.cc:93
void clear(wmWindowManager *wm)
void iterate(AssetListIterFn fn) const
void clear_current_file_assets(wmWindowManager *wm)
void ensure_blocking(const bContext &C)
FileListWrapper & operator=(FileListWrapper &&other)=default
FileListWrapper(eFileSelectType filesel_type)
Definition asset_list.cc:71
FileListWrapper(FileListWrapper &&other)=default
#define main()
#define storage(slot, qualifiers)
AssetLibraryReference all_library_reference()
AssetLibraryReference current_file_library_reference()
const FileIndexerType file_indexer_asset
void storage_fetch(const AssetLibraryReference *library_reference, const bContext *C)
void iterate(const AssetLibraryReference &library_reference, AssetListIterFn fn)
void on_remote_assets_downloaded(wmWindowManager &wm, StringRef library_url, StringRef downloaded_file_abspath)
int size(const AssetLibraryReference *library_reference)
void storage_id_remap(ID *id_old, ID *id_new)
bool has_asset_browser_storage_for_library(const AssetLibraryReference *library_reference, const bContext *C)
static void on_save_post(Main *main, PointerRNA **pointers, int num_pointers, void *arg)
static std::tuple< AssetList &, is_new_t > ensure_list_storage(const AssetLibraryReference &library_reference, eFileSelectType filesel_type)
asset_system::AssetLibrary * library_get_once_available(const AssetLibraryReference &library_reference)
void storage_fetch_blocking(const AssetLibraryReference &library_reference, const bContext &C)
void clear(const AssetLibraryReference *library_reference, const bContext *C)
bool listen(const wmNotifier *notifier)
static std::optional< eFileSelectType > asset_library_reference_to_fileselect_type(const AssetLibraryReference &library_reference)
void clear_all_library(const bContext *C)
bool has_list_storage_for_library(const AssetLibraryReference *library_reference)
bool is_loaded(const AssetLibraryReference *library_reference)
static AssetList * lookup_list(const AssetLibraryReference &library_ref)
Map< AssetLibraryReference, AssetList > AssetListMap
static void foreach_visible_asset_browser_showing_library(const AssetLibraryReference &library_reference, const wmWindowManager *wm, const FunctionRef< void(SpaceFile &sfile)> fn)
FunctionRef< bool(asset_system::AssetRepresentation &)> AssetListIterFn
static AssetListMap & libraries_map()
void asset_reading_region_listen_fn(const wmRegionListenerParams *params)
void WM_main_add_notifier(uint type, void *reference)
UserDef U
Definition blender.cc:58
const FileIndexerType file_indexer_noop
char * STRNCPY(char(&dst)[N], const char *src)
void WM_event_add_notifier_ex(wmWindowManager *wm, const wmWindow *win, uint type, void *reference)
void filelist_freelib(FileList *filelist)
Definition filelist.cc:1055
@ BKE_CB_EVT_SAVE_POST
@ ASSET_LIBRARY_USE_REMOTE_URL
@ FILE_SORT_ASSET_CATALOG
void filelist_setrecursion(FileList *filelist, int recursion_level)
Definition filelist.cc:1150
struct bUserAssetLibrary * BKE_preferences_asset_library_find_index(const struct UserDef *userdef, int index) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
void filelist_set_asset_include_online(FileList *filelist, bool show_online_assets)
Definition filelist.cc:139
@ FILE_BROWSE_MODE_ASSETS
void filelist_setsorting(FileList *filelist, short sort, bool invert_sort)
void filelist_setdir(FileList *filelist, char dirpath[1282])
void filelist_clear_from_reset_tag(FileList *filelist)
Definition filelist.cc:1011
asset_system::AssetLibrary * filelist_asset_library(FileList *filelist)
Definition filelist.cc:1050
@ ASSET_LIBRARY_CUSTOM
@ ASSET_LIBRARY_ESSENTIALS
@ ASSET_LIBRARY_ONLINE_ESSENTIALS
@ ASSET_LIBRARY_LOCAL
asset_system::AssetRepresentation * filelist_entry_get_asset_representation(const FileList *filelist, const int index)
Definition filelist.cc:1402
void filelist_setfilter_options(FileList *filelist, bool do_filter, bool hide_dot, bool hide_parent, uint64_t filter, uint64_t filter_id, bool filter_assets_only, bool filter_assets_hide_online, bool filter_assets_hide_offline, const char *filter_search)
FileList * filelist_new(short type, bool is_from_global_asset_list=false)
Definition filelist.cc:876
void BKE_callback_add(bCallbackFuncStore *funcstore, eCbEvent evt)
Definition callbacks.cc:87
@ FILE_ASSET_LIBRARY_ALL
@ FILE_ASSET_LIBRARY_ESSENTIALS
@ FILE_ASSET_LIBRARY_REMOTE
void ED_region_tag_refresh_ui(ARegion *region)
Definition area.cc:738
void filelist_readjob_blocking_run(FileList *filelist, int space_notifier, const bContext *C)
void filelist_setindexer(FileList *filelist, const FileIndexerType *indexer)
Definition filelist.cc:131
bool filelist_needs_force_reset(const FileList *filelist)
Definition filelist.cc:1197
wmWindowManager * CTX_wm_manager(const bContext *C)
int filelist_needs_reading(const FileList *filelist)
Definition filelist.cc:1981
bool filelist_is_ready(const FileList *filelist)
Definition filelist.cc:1220
void filelist_tag_force_reset(FileList *filelist)
Definition filelist.cc:1202
void ED_fileselect_clear(wmWindowManager *wm, SpaceFile *sfile)
Definition filesel.cc:1335
void filelist_readjob_stop(FileList *filelist, wmWindowManager *wm)
void filelist_setlibrary(FileList *filelist, const AssetLibraryReference *asset_library_ref, FunctionRef< void()> on_change=nullptr)
Definition filelist.cc:203
void filelist_remote_asset_library_refresh_online_assets_status(const FileList *filelist, StringRef remote_url, StringRef absolute_downloaded_file)
int filelist_files_ensure(FileList *filelist)
Definition filelist.cc:1235
bool filelist_needs_reset_on_main_changes(const FileList *filelist)
Definition filelist.cc:1230
void filelist_tag_reload_asset_library(FileList *filelist)
Definition filelist.cc:1215
void ED_fileselect_clear_main_assets(wmWindowManager *wm, SpaceFile *sfile)
Definition filesel.cc:1350
void filelist_readjob_start(FileList *filelist, int space_notifier, const bContext *C)
void filelist_filter(FileList *filelist)
void filelist_sort(FileList *filelist)
std::string AS_asset_library_root_path_from_library_ref(const AssetLibraryReference &library_reference)
void filelist_tag_force_reset_mainfiles(FileList *filelist)
Definition filelist.cc:1207
void filelist_free(FileList *filelist)
Definition filelist.cc:1025
bScreen * WM_window_get_active_screen(const wmWindow *win)
bool filelist_pending(const FileList *filelist)
Definition filelist.cc:1225
void filelist_clear(FileList *filelist)
Definition filelist.cc:1006
AssetLibraryReference asset_library_ref
ListBaseT< SpaceLink > spacedata
eFileBrowse_Mode browse_mode
FileAssetSelectParams * asset_params
struct FileList * files
void(*) func(Main *, PointerRNA **, int pointers_num, void *arg)
ListBaseT< struct ScrArea > areabase
unsigned int action
Definition WM_types.hh:371
unsigned int category
Definition WM_types.hh:371
unsigned int data
Definition WM_types.hh:371