45std::unique_ptr<AssetLibraryService> AssetLibraryService::instance_;
46bool AssetLibraryService::atexit_handler_registered_ =
false;
53 return instance_.get();
61 instance_->app_handler_unregister();
84 if (root_path.empty()) {
95 if (!custom_library) {
99 std::string root_path = custom_library->
dirpath;
100 if (root_path.empty()) {
122 "top level directory must be given for on-disk asset library");
126 std::unique_ptr<OnDiskAssetLibrary> *lib_uptr_ptr = on_disk_libraries_.lookup_ptr(
127 {library_type, normalized_root_path});
128 if (lib_uptr_ptr !=
nullptr) {
129 CLOG_INFO(&
LOG, 2,
"get \"%s\" (cached)", normalized_root_path.c_str());
131 lib->refresh_catalogs();
135 std::unique_ptr<OnDiskAssetLibrary> lib_uptr;
136 switch (library_type) {
138 lib_uptr = std::make_unique<PreferencesOnDiskAssetLibrary>(name, normalized_root_path);
141 lib_uptr = std::make_unique<EssentialsAssetLibrary>();
144 lib_uptr = std::make_unique<OnDiskAssetLibrary>(library_type, name, normalized_root_path);
150 lib->load_catalogs();
152 on_disk_libraries_.add_new({library_type, normalized_root_path}, std::move(lib_uptr));
153 CLOG_INFO(&
LOG, 2,
"get \"%s\" (loaded)", normalized_root_path.c_str());
168 "Use `get_asset_library_on_disk_custom()` for libraries of type `ASSET_LIBRARY_CUSTOM`");
177 if (current_file_library_) {
179 current_file_library_->refresh_catalogs();
183 current_file_library_ = std::make_unique<RuntimeAssetLibrary>();
193 all_library_->tag_catalogs_dirty();
199 if (all_library_ && all_library_->is_catalogs_dirty()) {
203 const bool reload_nested_catalogs =
false;
204 all_library_->rebuild_catalogs_from_nested(reload_nested_catalogs);
223 all_library_ = std::make_unique<AllAssetLibrary>();
230 all_library_->rebuild_catalogs_from_nested(
false);
232 return all_library_.get();
248 for (
const std::unique_ptr<OnDiskAssetLibrary> &library : on_disk_libraries_.values()) {
249 if (library->name_ == name) {
250 return library.get();
266 library_dirpath = custom_lib->
dirpath;
274 if (!loaded_custom_lib) {
278 library_dirpath = *loaded_custom_lib->root_path_;
290 return normalized_library_dirpath;
295 const std::vector<StringRefNull> blendfile_extensions = {
".blend" SEP_STR,
310 (blendfile_extension_pos < iter_ext_pos))
312 blendfile_extension_pos = iter_ext_pos;
316 return blendfile_extension_pos;
347 group_name_sep_pos + 1);
363 if (library_dirpath.empty()) {
371 return normalized_full_path;
388 exploded.
full_path = std::make_unique<std::string>(path_in_file);
399 if (full_path.empty()) {
406 size_t group_pos = full_path.find(
SEP, blendfile_extension_pos);
409 size_t name_pos = full_path.find(
SEP, group_pos + 1);
416 exploded.
full_path = std::make_unique<std::string>(full_path);
452 if (!custom_library || !custom_library->
dirpath[0]) {
456 return custom_library->
dirpath;
461 instance_ = std::make_unique<AssetLibraryService>();
462 instance_->app_handler_register();
464 if (!atexit_handler_registered_) {
468 atexit_handler_registered_ =
true;
477#ifdef WITH_DESTROY_VIA_LOAD_HANDLER
485 on_load_callback_store_.
alloc =
false;
488 on_load_callback_store_.
arg =
this;
496 on_load_callback_store_.
func =
nullptr;
497 on_load_callback_store_.
arg =
nullptr;
502 bool has_unsaved_changes =
false;
507 has_unsaved_changes = true;
511 return has_unsaved_changes;
515 const bool include_all_library)
const
517 if (include_all_library && all_library_) {
521 if (current_file_library_) {
522 fn(*current_file_library_);
525 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)
#define CLOG_INFO(clg_ref, level,...)
@ 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
bool has_unsaved_changes() 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(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()
AssetLibrary * get_asset_library_on_disk(eAssetLibraryType library_type, StringRef name, StringRefNull root_path)
static bUserAssetLibrary * find_custom_asset_library_from_library_ref(const AssetLibraryReference &library_reference)
void reload_all_library_catalogs_if_dirty()
void app_handler_register()
static void allocate_service_instance()
std::optional< ExplodedPath > resolve_asset_weak_reference_to_exploded_path(const AssetWeakReference &asset_reference)
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_all(const Main *bmain)
bool has_any_unsaved_catalogs() const
int64_t rfind_blendfile_extension(StringRef path)
std::optional< eAssetImportMethod > import_method_
bool may_override_import_method_
AssetCatalogService & catalog_service() 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
void(* func)(Main *, PointerRNA **, int num_pointers, void *arg)
StringRef group_component
std::unique_ptr< std::string > full_path
static DynamicLibrary lib