diff --git a/source/blender/asset_system/AS_asset_library.hh b/source/blender/asset_system/AS_asset_library.hh index df175d46755..efcc7813ff6 100644 --- a/source/blender/asset_system/AS_asset_library.hh +++ b/source/blender/asset_system/AS_asset_library.hh @@ -23,6 +23,7 @@ struct AssetLibrary; struct IDRemapper; struct Main; +struct UserDef; namespace blender::asset_system { @@ -30,6 +31,19 @@ class AssetIdentifier; class AssetRepresentation; class AssetStorage; +/** + * Make an asset library available to the asset system, by letting it take ownership. + */ +void register_library(eAssetLibraryType type, std::unique_ptr library); +/** + * Unregister all asset libraries of \a type, returning the unregistered libraries as owning + * pointers. The return value can be discarded to completely destruct the asset libraries. + */ +Vector> unregister_libraries_of_type(eAssetLibraryType type); + +void register_builtin_libraries(); +void register_userdef_libraries(const UserDef *userdef); + /** * AssetLibrary provides access to an asset library's data. * @@ -76,6 +90,10 @@ class AssetLibrary { bCallbackFuncStore on_save_callback_store_{}; + /* TODO temp, just to set up some settings. */ + friend void register_builtin_libraries(); + friend void register_userdef_libraries(const UserDef *userdef); + public: /* Controlled by #ED_asset_catalogs_set_save_catalogs_when_file_is_saved, * for managing the "Save Catalog Changes" in the quit-confirmation dialog box. */ @@ -95,7 +113,7 @@ class AssetLibrary { * \param root_path: If this is an asset library on disk, the top-level directory path. */ AssetLibrary(eAssetLibraryType library_type, StringRef name = "", StringRef root_path = ""); - ~AssetLibrary(); + virtual ~AssetLibrary(); /** * Execute \a fn for every asset library that is loaded. The asset library is passed to the @@ -107,6 +125,8 @@ class AssetLibrary { */ static void foreach_loaded(FunctionRef fn, bool include_all_library); + virtual void load(const Main &bmain) = 0; + void load_catalogs(); /** Load catalogs that have changed on disk. */ diff --git a/source/blender/asset_system/AS_essentials_library.hh b/source/blender/asset_system/AS_essentials_library.hh index 918440e20b1..ed99e840514 100644 --- a/source/blender/asset_system/AS_essentials_library.hh +++ b/source/blender/asset_system/AS_essentials_library.hh @@ -12,6 +12,11 @@ namespace blender::asset_system { +class EssentialsAssetLibrary : public AssetLibrary { + public: + void load() override; +}; + StringRefNull essentials_directory_path(); -} +} // namespace blender::asset_system diff --git a/source/blender/asset_system/CMakeLists.txt b/source/blender/asset_system/CMakeLists.txt index 9200d1da9c0..46b7e4628f1 100644 --- a/source/blender/asset_system/CMakeLists.txt +++ b/source/blender/asset_system/CMakeLists.txt @@ -15,13 +15,16 @@ set(SRC intern/asset_catalog.cc intern/asset_catalog_path.cc intern/asset_catalog_tree.cc - intern/asset_essentials_library.cc intern/asset_identifier.cc intern/asset_library.cc + intern/asset_library_registry.cc intern/asset_library_service.cc intern/asset_representation.cc intern/asset_storage.cc intern/utils.cc + intern/asset_libraries/all_library.cc + intern/asset_libraries/essentials_library.cc + intern/asset_libraries/current_file_library.cc AS_asset_catalog.hh AS_asset_catalog_path.hh @@ -33,6 +36,8 @@ set(SRC intern/asset_library_service.hh intern/asset_storage.hh intern/utils.hh + intern/asset_libraries/all_library.hh + intern/asset_libraries/current_file_library.hh AS_asset_library.h ) diff --git a/source/blender/asset_system/intern/asset_libraries/all_library.cc b/source/blender/asset_system/intern/asset_libraries/all_library.cc new file mode 100644 index 00000000000..36cb50ef6c5 --- /dev/null +++ b/source/blender/asset_system/intern/asset_libraries/all_library.cc @@ -0,0 +1,22 @@ +/* SPDX-FileCopyrightText: 2023 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +/** \file + * \ingroup asset_system + */ + +#include "asset_library_service.hh" + +#include "all_library.hh" + +namespace blender::asset_system { + +AllAssetLibrary::AllAssetLibrary() : AssetLibrary(ASSET_LIBRARY_ALL) {} + +void AllAssetLibrary::load(const Main &bmain) +{ + AssetLibraryService::get_asset_library_all(&bmain); +} + +} // namespace blender::asset_system diff --git a/source/blender/asset_system/intern/asset_libraries/all_library.hh b/source/blender/asset_system/intern/asset_libraries/all_library.hh new file mode 100644 index 00000000000..87c20d6a9e1 --- /dev/null +++ b/source/blender/asset_system/intern/asset_libraries/all_library.hh @@ -0,0 +1,21 @@ +/* SPDX-FileCopyrightText: 2023 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +/** \file + * \ingroup asset_system + */ + +#pragma once + +#include "AS_asset_library.hh" + +namespace blender::asset_system { + +class AllAssetLibrary : public AssetLibrary { + public: + AllAssetLibrary(); + void load(const Main &bmain) override; +}; + +} // namespace blender::asset_system diff --git a/source/blender/asset_system/intern/asset_libraries/current_file_library.cc b/source/blender/asset_system/intern/asset_libraries/current_file_library.cc new file mode 100644 index 00000000000..0b1256c7b13 --- /dev/null +++ b/source/blender/asset_system/intern/asset_libraries/current_file_library.cc @@ -0,0 +1,30 @@ + +/* SPDX-FileCopyrightText: 2023 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +/** \file + * \ingroup asset_system + */ + +#include "asset_library_service.hh" + +#include "current_file_library.hh" + +namespace blender::asset_system { + +CurrentFileAssetLibrary::CurrentFileAssetLibrary() : {} + +void CurrentFileAssetLibrary::load(const Main &bmain) +{ + /* For the "Current File" library we get the asset library root path based on main. */ + std::string root_path = bmain ? AS_asset_library_find_suitable_root_path_from_main(bmain) : ""; + + if (root_path.empty()) { + /* File wasn't saved yet. */ + return AssetLibraryService::get_asset_library_current_file(); + } + return AssetLibraryService::get_asset_library_on_disk_builtin(ASSET_LIBRARY_LOCAL, root_path); +} + +} // namespace blender::asset_system diff --git a/source/blender/asset_system/intern/asset_libraries/current_file_library.hh b/source/blender/asset_system/intern/asset_libraries/current_file_library.hh new file mode 100644 index 00000000000..f27d9664f9f --- /dev/null +++ b/source/blender/asset_system/intern/asset_libraries/current_file_library.hh @@ -0,0 +1,22 @@ +/* SPDX-FileCopyrightText: 2023 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +/** \file + * \ingroup asset_system + */ + +#pragma once + +#include "AS_asset_library.hh" + +namespace blender::asset_system { + +class CurrentFileAssetLibrary : public AssetLibrary { + public: + CurrentFileAssetLibrary(); + + void load(const Main &bmain) override; +}; + +} // namespace blender::asset_system diff --git a/source/blender/asset_system/intern/asset_essentials_library.cc b/source/blender/asset_system/intern/asset_libraries/essentials_library.cc similarity index 59% rename from source/blender/asset_system/intern/asset_essentials_library.cc rename to source/blender/asset_system/intern/asset_libraries/essentials_library.cc index 336480203c1..0260715fbfc 100644 --- a/source/blender/asset_system/intern/asset_essentials_library.cc +++ b/source/blender/asset_system/intern/asset_libraries/essentials_library.cc @@ -10,10 +10,21 @@ #include "BKE_appdir.h" +#include "AS_asset_library.hh" +#include "asset_library_service.hh" + #include "AS_essentials_library.hh" namespace blender::asset_system { +void EssentialsAssetLibrary::load(const Main & /*bmain*/) override +{ + /* Builtin asset libraries don't need a name, the #eAssetLibraryType is enough to identify them + * (and doesn't change, unlike the name). */ + AssetLibraryService::get_asset_library_on_disk( + ASSET_LIBRARY_ESSENTIALS, "", essentials_directory_path()); +} + StringRefNull essentials_directory_path() { static std::string path = []() { diff --git a/source/blender/asset_system/intern/asset_library_registry.cc b/source/blender/asset_system/intern/asset_library_registry.cc new file mode 100644 index 00000000000..930dc4924e8 --- /dev/null +++ b/source/blender/asset_system/intern/asset_library_registry.cc @@ -0,0 +1,118 @@ +/* SPDX-FileCopyrightText: 2023 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +/** \file + * \ingroup asset_system + */ + +#include +#include + +#include "DNA_userdef_types.h" + +#include "BLI_listbase.h" + +#include "AS_asset_library.hh" +#include "AS_essentials_library.hh" +#include "asset_libraries/all_library.hh" +#include "asset_libraries/current_file_library.hh" + +#include "BLI_map.hh" +#include "BLI_vector.hh" + +#include "CLG_log.h" + +static CLG_LogRef LOG = {"asset_system"}; + +namespace blender::asset_system { + +class AssetLibraryRegistry { + public: + using LibrariesVec = Vector>; + Map libraries_by_type; + + static AssetLibraryRegistry &get_registry(); +}; + +AssetLibraryRegistry &AssetLibraryRegistry::get_registry() +{ + static AssetLibraryRegistry instance; + return instance; +} + +void register_library(const eAssetLibraryType type, std::unique_ptr library) +{ + AssetLibraryRegistry ®istry = AssetLibraryRegistry::get_registry(); + registry.libraries_by_type.add_or_modify( + type, + [&](AssetLibraryRegistry::LibrariesVec *libraries) { + *libraries = Vector>{}; + libraries->append(std::move(library)); + }, + [&](AssetLibraryRegistry::LibrariesVec *libraries) { + libraries->append(std::move(library)); + }); +} + +Vector> unregister_libraries_of_type(const eAssetLibraryType type) +{ + AssetLibraryRegistry ®istry = AssetLibraryRegistry::get_registry(); + std::optional libraries = registry.libraries_by_type.pop_try( + type); + + if (!libraries) { + CLOG_INFO(&LOG, 2, "No asset libraries registered for this type [%i].", type); + } + + return libraries; +} + +void register_builtin_libraries() +{ + register_library(ASSET_LIBRARY_ALL, std::make_unique(ASSET_LIBRARY_ALL)); + + register_library(ASSET_LIBRARY_LOCAL, std::make_unique(ASSET_LIBRARY_LOCAL)); + + std::unique_ptr essentials_library = std::make_unique( + ASSET_LIBRARY_ESSENTIALS, "", essentials_directory_path()); + essentials_library->import_method_ = ASSET_IMPORT_APPEND_REUSE; + register_library(ASSET_LIBRARY_ESSENTIALS, std::move(essentials_library)); +} + +void unregister_all_libraries() +{ + AssetLibraryRegistry ®istry = AssetLibraryRegistry::get_registry(); + registry.libraries_by_type.clear_and_shrink(); +} + +void register_userdef_libraries(const UserDef *userdef) +{ + LISTBASE_FOREACH (bUserAssetLibrary *, user_library, &userdef->asset_libraries) { + std::unique_ptr library = std::make_unique( + ASSET_LIBRARY_CUSTOM, user_library->name, user_library->dirpath); + + library->import_method_ = eAssetImportMethod(user_library->import_method); + library->may_override_import_method_ = true; + library->use_relative_path_ = (user_library->flag & ASSET_LIBRARY_RELATIVE_PATH) != 0; + + register_library(ASSET_LIBRARY_CUSTOM, std::move(library)); + } +} + +void update_registered_userdef_libraries(const UserDef *userdef) +{ + AssetLibraryRegistry ®istry = AssetLibraryRegistry::get_registry(); + + Map registered_libraries_to_index; + AssetLibraryRegistry::LibrariesVec &user_libraries = registry.libraries_by_type.lookup_default( + ASSET_LIBRARY_CUSTOM, {}); + for (std::unique_ptr &user_library : user_libraries) { + // registered_libraries_to_index.add(user_library.) + } + + LISTBASE_FOREACH (bUserAssetLibrary *, user_library, &userdef->asset_libraries) { + } +} + +} // namespace blender::asset_system