52namespace asset_system {
54std::unique_ptr<AssetLibraryService> AssetLibraryService::instance_;
55bool AssetLibraryService::atexit_handler_registered_ =
false;
62 return instance_.
get();
96 if (root_path.empty()) {
107 if (!custom_library) {
119 if (root_path.empty()) {
124 library->may_override_import_method_ =
true;
135 if (online_essentials_library_) {
137 online_essentials_library_->load_or_reload_catalogs();
141 online_essentials_library_ = std::make_unique<OnlineEssentialsLibrary>();
159 std::scoped_lock
lock{remote_libraries_mutex_};
161 std::unique_ptr<PreferencesRemoteAssetLibrary> *lib_uptr_ptr = remote_libraries_.lookup_ptr(
163 if (lib_uptr_ptr !=
nullptr) {
170 std::unique_ptr<PreferencesRemoteAssetLibrary> lib_uptr =
171 std::make_unique<PreferencesRemoteAssetLibrary>(custom_library);
175 remote_libraries_.add_new(remote_url, std::move(lib_uptr));
184 const bool load_catalogs,
189 std::scoped_lock
lock{on_disk_libraries_mutex_};
193 CLOG_DEBUG(&
LOG,
"get \"%s\" (cached)", resolved_root_path.c_str());
195 lib->load_or_reload_catalogs();
200 std::unique_ptr<OnDiskAssetLibrary> lib_uptr;
201 switch (library_type) {
203 if (preferences_library) {
204 lib_uptr = std::make_unique<PreferencesOnDiskAssetLibrary>(*preferences_library);
208 lib_uptr = std::make_unique<OnDiskAssetLibrary>(
209 library_type, name, resolved_root_path,
false);
213 lib_uptr = std::make_unique<EssentialsAssetLibrary>();
216 lib_uptr = std::make_unique<OnDiskAssetLibrary>(
217 library_type, name, resolved_root_path,
false);
220 lib_uptr = std::make_unique<OnDiskAssetLibrary>(
221 library_type, name, resolved_root_path,
true);
227 on_disk_libraries_.add_new({library_type, resolved_root_path}, std::move(lib_uptr));
228 CLOG_DEBUG(&
LOG,
"get \"%s\" (loaded)", resolved_root_path.c_str());
247 custom_library->
name,
258 "Use `get_asset_library_on_disk_custom()` for libraries of type `ASSET_LIBRARY_CUSTOM`");
267 if (current_file_library_) {
269 current_file_library_->refresh_catalogs();
273 current_file_library_ = std::make_unique<RuntimeAssetLibrary>();
283 all_library_->tag_catalogs_dirty();
289 if (all_library_ && all_library_->is_catalogs_dirty()) {
293 const bool reload_nested_catalogs =
false;
294 all_library_->rebuild_catalogs_from_nested(reload_nested_catalogs);
304 if (root_path.empty()) {
310 std::scoped_lock
lock{library_service.on_disk_libraries_mutex_};
312 "On-disk \"Current File\" asset library shouldn't exist yet, it should only be "
313 "created now in response to initially saving the file - catalog service "
314 "will be overridden");
329 BLI_assert(on_disk_library != library_service.current_file_library_.get());
332 std::lock_guard lock_runtime{library_service.current_file_library_->catalog_service_mutex_};
334 library_service.current_file_library_->catalog_service_);
338 catalog_service.asset_library_root_ = on_disk_library->
root_path();
342 "new on-disk library shouldn't have catalog definition files - root path "
343 "changed, so they would have to be relocated");
347 if (!catalog_service.catalog_collection_->is_empty()) {
350 sizeof(asset_lib_cdf_path),
353 catalog_service.catalog_collection_->catalog_definition_file_ =
357 library_service.current_file_library_ =
nullptr;
359 return on_disk_library;
377 all_library_ = std::make_unique<AllAssetLibrary>();
384 all_library_->rebuild_catalogs_from_nested(
false);
386 return all_library_.get();
393 "top level directory must be given for on-disk asset library");
397 std::scoped_lock
lock{on_disk_libraries_mutex_};
398 std::unique_ptr<OnDiskAssetLibrary> *lib_uptr_ptr = on_disk_libraries_.lookup_ptr(
399 {library_type, resolved_root_path});
400 return lib_uptr_ptr ? lib_uptr_ptr->get() :
nullptr;
416 std::scoped_lock
lock{on_disk_libraries_mutex_};
417 for (
const std::unique_ptr<OnDiskAssetLibrary> &
library : on_disk_libraries_.values()) {
443 if (!loaded_custom_lib) {
447 library_dirpath = *loaded_custom_lib->root_path_;
462 return normalized_library_dirpath;
467 const std::vector<StringRefNull> blendfile_extensions = {
".blend" SEP_STR,
482 (blendfile_extension_pos < iter_ext_pos))
484 blendfile_extension_pos = iter_ext_pos;
488 return blendfile_extension_pos;
519 group_name_sep_pos + 1);
535 if (library_dirpath.empty()) {
543 return normalized_full_path;
560 exploded.
full_path = std::make_unique<std::string>(path_in_file);
572 if (full_path.empty()) {
579 size_t group_pos = full_path.find(
SEP, blendfile_extension_pos);
582 size_t name_pos = full_path.find(
SEP, group_pos + 1);
589 exploded.
full_path = std::make_unique<std::string>(full_path);
637 instance_ = std::make_unique<AssetLibraryService>();
638 instance_->app_handler_register();
640 if (!atexit_handler_registered_) {
644 atexit_handler_registered_ =
true;
653#ifdef WITH_DESTROY_VIA_LOAD_HANDLER
661 on_load_callback_store_.alloc =
false;
664 on_load_callback_store_.arg =
this;
672 on_load_callback_store_.func =
nullptr;
673 on_load_callback_store_.arg =
nullptr;
678 bool has_unsaved_changes =
false;
682 if (
library.catalog_service().has_unsaved_changes()) {
683 has_unsaved_changes =
true;
687 return has_unsaved_changes;
691 const bool include_all_library)
const
702 if (include_all_library && all_library_) {
703 libraries.
append(all_library_.get());
706 if (current_file_library_) {
707 libraries.
append(current_file_library_.get());
711 std::scoped_lock
lock{on_disk_libraries_mutex_};
715 for (
const auto &asset_lib_uptr : on_disk_libraries_.values()) {
720 if (asset_lib_uptr->is_enabled()) {
721 libraries.
append(asset_lib_uptr.get());
729 if (include_remote_libraries && online_essentials_library_ &&
732 libraries.
append(online_essentials_library_.get());
736 std::scoped_lock
lock{on_disk_libraries_mutex_};
737 for (
const auto &asset_lib_uptr : on_disk_libraries_.values()) {
743 if (asset_lib_uptr->is_enabled()) {
744 libraries.
append(asset_lib_uptr.get());
749 if (include_remote_libraries) {
750 std::scoped_lock
lock{remote_libraries_mutex_};
751 for (
const auto &asset_lib_uptr : remote_libraries_.values()) {
752 if (asset_lib_uptr->is_enabled()) {
753 libraries.
append(asset_lib_uptr.get());
File and directory operations.
#define BLI_path_join(...)
#define CLOG_DEBUG(clg_ref,...)
#define USER_EXPERIMENTAL_TEST(userdef, member)
#define BLI_assert_msg(a, msg)
constexpr const char * c_str() const
void append(const T &value)
std::unique_ptr< AssetCatalogDefinitionFile > construct_cdf_in_memory(const CatalogFilePath &file_path) const
static const CatalogFilePath DEFAULT_CATALOG_FILENAME
const AssetCatalogDefinitionFile * get_catalog_definition_file() const
void tag_all_library_catalogs_dirty()
void app_handler_unregister()
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_on_disk_custom_preferences(bUserAssetLibrary *custom_library)
AssetLibrary * get_asset_library(const Main *bmain, const AssetLibraryReference &library_reference)
AssetLibrary * find_loaded_on_disk_asset_library_from_name(StringRef name) const
AssetLibrary * get_asset_library_current_file()
static bUserAssetLibrary * find_custom_asset_library_from_library_ref(const AssetLibraryReference &library_reference)
void reload_all_library_catalogs_if_dirty()
OnDiskAssetLibrary * lookup_on_disk_library(eAssetLibraryType type, StringRefNull root_path)
void app_handler_register()
static void allocate_service_instance()
std::optional< ExplodedPath > resolve_asset_weak_reference_to_exploded_path(const AssetWeakReference &asset_reference)
AssetLibrary * get_online_essentials_asset_library()
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)
static AssetLibraryService * get()
AssetLibrary * get_asset_library_on_disk_custom(StringRef name, StringRefNull root_path)
static std::string root_path_from_library_ref(const AssetLibraryReference &library_reference)
std::string normalize_asset_weak_reference_relative_asset_identifier(const AssetWeakReference &asset_reference)
static bUserAssetLibrary * find_custom_preferences_asset_library_from_asset_weak_ref(const AssetWeakReference &asset_reference)
std::string resolve_asset_weak_reference_to_library_path(const AssetWeakReference &asset_reference)
AssetLibrary * get_asset_library_on_disk(eAssetLibraryType library_type, StringRef name, StringRefNull root_path, bool load_catalogs=true, bUserAssetLibrary *preferences_library=nullptr)
AssetLibraryService()=default
AssetLibrary * get_preferences_remote_asset_library(const bUserAssetLibrary &custom_library)
AssetLibrary * get_asset_library_all(const Main *bmain)
bool has_any_unsaved_catalogs() const
int64_t rfind_blendfile_extension(StringRef path)
void load_or_reload_catalogs()
std::shared_ptr< AssetCatalogService > catalog_service_
std::recursive_mutex catalog_service_mutex_
AssetCatalogService & catalog_service() const
StringRefNull root_path() const
constexpr int64_t find_first_of(StringRef chars, int64_t pos=0) const
constexpr int64_t rfind(char c, int64_t pos=INT64_MAX) const
static constexpr int64_t not_found
constexpr bool is_empty() const
constexpr StringRef substr(int64_t start, int64_t size) const
std::string resolve_directory_path(StringRef directory)
std::string resolve_path(StringRefNull path, int64_t max_len)
Vector< AssetLibraryReference > all_valid_asset_library_refs()
StringRefNull online_essentials_cache_directory_path()
static void on_blendfile_load(Main *, PointerRNA **, const int, void *)
StringRefNull essentials_directory_path()
bool is_online_essentials_url(StringRef url)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ USER_ASSETS_USE_ONLINE_ESSENTIALS
@ ASSET_LIBRARY_USE_REMOTE_URL
struct bUserAssetLibrary * BKE_preferences_asset_library_find_index(const struct UserDef *userdef, int index) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
@ ASSET_LIBRARY_ESSENTIALS
@ ASSET_LIBRARY_ONLINE_ESSENTIALS
void BKE_callback_add(bCallbackFuncStore *funcstore, eCbEvent evt)
std::string AS_asset_library_find_suitable_root_path_from_main(const Main *bmain)
void BKE_callback_remove(bCallbackFuncStore *funcstore, eCbEvent evt)
struct bUserAssetLibrary * BKE_preferences_asset_library_find_by_name(const struct UserDef *userdef, const char *name) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
void BKE_blender_atexit_register(void(*func)(void *user_data), void *user_data)
const char * asset_library_identifier
const char * relative_asset_identifier
eAssetLibraryType asset_library_type
StringRef group_component
std::unique_ptr< std::string > full_path
char resolved_dirpath[1024]