48std::unique_ptr<AssetLibraryService> AssetLibraryService::instance_;
49bool AssetLibraryService::atexit_handler_registered_ =
false;
56 return instance_.
get();
87 if (root_path.empty()) {
98 if (!custom_library) {
102 std::string root_path = custom_library->
dirpath;
103 if (root_path.empty()) {
123 const bool load_catalogs,
129 lib->load_or_reload_catalogs();
136 std::unique_ptr<OnDiskAssetLibrary> lib_uptr;
137 switch (library_type) {
139 if (preferences_library) {
140 lib_uptr = std::make_unique<PreferencesOnDiskAssetLibrary>(
name, normalized_root_path);
143 lib_uptr = std::make_unique<OnDiskAssetLibrary>(library_type,
name, normalized_root_path);
147 lib_uptr = std::make_unique<EssentialsAssetLibrary>();
150 lib_uptr = std::make_unique<OnDiskAssetLibrary>(library_type,
name, normalized_root_path);
157 lib->load_or_reload_catalogs();
160 on_disk_libraries_.add_new({library_type, normalized_root_path}, std::move(lib_uptr));
161 CLOG_DEBUG(&
LOG,
"get \"%s\" (loaded)", normalized_root_path.c_str());
183 "Use `get_asset_library_on_disk_custom()` for libraries of type `ASSET_LIBRARY_CUSTOM`");
192 if (current_file_library_) {
194 current_file_library_->refresh_catalogs();
198 current_file_library_ = std::make_unique<RuntimeAssetLibrary>();
208 all_library_->tag_catalogs_dirty();
214 if (all_library_ && all_library_->is_catalogs_dirty()) {
218 const bool reload_nested_catalogs =
false;
219 all_library_->rebuild_catalogs_from_nested(reload_nested_catalogs);
229 if (root_path.empty()) {
234 "On-disk \"Current File\" asset library shouldn't exist yet, it should only be "
235 "created now in response to initially saving the file - catalog service "
236 "will be overridden");
249 BLI_assert(on_disk_library != library_service.current_file_library_.get());
252 std::lock_guard lock_runtime{library_service.current_file_library_->catalog_service_mutex_};
254 library_service.current_file_library_->catalog_service_);
258 catalog_service.asset_library_root_ = on_disk_library->
root_path();
262 "new on-disk library shouldn't have catalog definition files - root path "
263 "changed, so they would have to be relocated");
267 if (!catalog_service.catalog_collection_->is_empty()) {
270 sizeof(asset_lib_cdf_path),
273 catalog_service.catalog_collection_->catalog_definition_file_ =
277 library_service.current_file_library_ =
nullptr;
279 return on_disk_library;
297 all_library_ = std::make_unique<AllAssetLibrary>();
304 all_library_->rebuild_catalogs_from_nested(
false);
306 return all_library_.get();
313 "top level directory must be given for on-disk asset library");
317 std::unique_ptr<OnDiskAssetLibrary> *lib_uptr_ptr = on_disk_libraries_.lookup_ptr(
318 {library_type, normalized_root_path});
319 return lib_uptr_ptr ? lib_uptr_ptr->get() :
nullptr;
335 for (
const std::unique_ptr<OnDiskAssetLibrary> &library : on_disk_libraries_.values()) {
336 if (library->name_ ==
name) {
337 return library.get();
353 library_dirpath = custom_lib->
dirpath;
361 if (!loaded_custom_lib) {
365 library_dirpath = *loaded_custom_lib->root_path_;
377 return normalized_library_dirpath;
382 const std::vector<StringRefNull> blendfile_extensions = {
".blend" SEP_STR,
397 (blendfile_extension_pos < iter_ext_pos))
399 blendfile_extension_pos = iter_ext_pos;
403 return blendfile_extension_pos;
434 group_name_sep_pos + 1);
450 if (library_dirpath.empty()) {
458 return normalized_full_path;
475 exploded.
full_path = std::make_unique<std::string>(path_in_file);
486 if (full_path.empty()) {
493 size_t group_pos = full_path.find(
SEP, blendfile_extension_pos);
496 size_t name_pos = full_path.find(
SEP, group_pos + 1);
503 exploded.
full_path = std::make_unique<std::string>(full_path);
539 if (!custom_library || !custom_library->
dirpath[0]) {
543 return custom_library->
dirpath;
548 instance_ = std::make_unique<AssetLibraryService>();
549 instance_->app_handler_register();
551 if (!atexit_handler_registered_) {
555 atexit_handler_registered_ =
true;
564#ifdef WITH_DESTROY_VIA_LOAD_HANDLER
572 on_load_callback_store_.alloc =
false;
575 on_load_callback_store_.arg =
this;
583 on_load_callback_store_.func =
nullptr;
584 on_load_callback_store_.arg =
nullptr;
589 bool has_unsaved_changes =
false;
594 has_unsaved_changes =
true;
598 return has_unsaved_changes;
602 const bool include_all_library)
const
604 if (include_all_library && all_library_) {
608 if (current_file_library_) {
609 fn(*current_file_library_);
612 for (
const auto &asset_lib_uptr : on_disk_libraries_.values()) {
std::string AS_asset_library_find_suitable_root_path_from_main(const Main *bmain)
void BKE_blender_atexit_register(void(*func)(void *user_data), void *user_data)
void BKE_callback_add(bCallbackFuncStore *funcstore, eCbEvent evt)
void BKE_callback_remove(bCallbackFuncStore *funcstore, eCbEvent evt)
struct bUserAssetLibrary * BKE_preferences_asset_library_find_index(const struct UserDef *userdef, int index) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
struct bUserAssetLibrary * BKE_preferences_asset_library_find_by_name(const struct UserDef *userdef, const char *name) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
#define BLI_assert_msg(a, msg)
File and directory operations.
#define BLI_path_join(...)
#define CLOG_DEBUG(clg_ref,...)
@ ASSET_LIBRARY_RELATIVE_PATH
@ ASSET_LIBRARY_ESSENTIALS
static constexpr int64_t not_found
constexpr int64_t rfind(char c, int64_t pos=INT64_MAX) const
constexpr bool is_empty() const
constexpr StringRef substr(int64_t start, int64_t size) const
constexpr int64_t find_first_of(StringRef chars, int64_t pos=0) const
constexpr const char * c_str() const
bool has_unsaved_changes() const
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)
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_asset_library_all(const Main *bmain)
bool has_any_unsaved_catalogs() const
int64_t rfind_blendfile_extension(StringRef path)
std::optional< eAssetImportMethod > import_method_
std::unique_ptr< AssetCatalogService > catalog_service_
bool may_override_import_method_
AssetCatalogService & catalog_service() const
Mutex catalog_service_mutex_
StringRefNull root_path() const
std::string normalize_path(StringRefNull path, int64_t max_len)
std::string normalize_directory_path(StringRef directory)
Vector< AssetLibraryReference > all_valid_asset_library_refs()
static void on_blendfile_load(Main *, PointerRNA **, const int, void *)
StringRefNull essentials_directory_path()
const char * relative_asset_identifier
const char * asset_library_identifier
StringRef group_component
std::unique_ptr< std::string > full_path
static DynamicLibrary lib