Blender V5.3
essentials_library.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
8
10
11#include "BKE_appdir.hh"
12
13#include "BLI_path_utils.hh"
14#include "BLI_string_ref.hh"
15
16#include "CLG_log.h"
17
18#include "DNA_asset_types.h"
19#include "DNA_userdef_types.h"
20
21#include "on_disk_library.hh"
22#include "remote_library.hh"
23#include "utils.hh"
24
26#include "essentials_library.hh"
27
28namespace blender::asset_system {
29
30static CLG_LogRef LOG = {"asset.library.essentials"};
31
39
45
46std::optional<AssetLibraryReference> EssentialsAssetLibrary::library_reference() const
47{
48 AssetLibraryReference library_ref{};
49 library_ref.custom_library_index = -1;
50 library_ref.type = ASSET_LIBRARY_ESSENTIALS;
51 return library_ref;
52}
53
54std::optional<eAssetImportMethod> EssentialsAssetLibrary::import_method() const
55{
56 if (U.experimental.no_data_block_packing) {
58 }
59 return ASSET_IMPORT_PACK;
60}
61
63{
64 /* Start with empty catalog storage. Don't do this directly in #this.catalog_service to avoid
65 * race conditions. Rather build into a new service and replace the current one when done. */
66 std::unique_ptr<AssetCatalogService> new_catalog_service = std::make_unique<AssetCatalogService>(
68
69 const bool skip_remote_libraries = !USER_EXPERIMENTAL_TEST(&U, use_remote_asset_libraries);
70
71 const auto load_catalogs_fn = [&](const AssetLibrary *library) {
72 const bool is_online_lib = library->remote_url().has_value();
73 if (is_online_lib && skip_remote_libraries) {
74 return;
75 }
76
77 library->catalog_service().reload_catalogs();
78
79 new_catalog_service->add_from_existing(
80 library->catalog_service(),
81 /*on_duplicate_items=*/[](const AssetCatalog &existing,
82 const AssetCatalog &to_be_ignored) {
83 if (existing.path == to_be_ignored.path) {
84 CLOG_DEBUG(&LOG,
85 "multiple definitions of catalog %s (path: %s), ignoring duplicate",
86 existing.catalog_id.str().c_str(),
87 existing.path.c_str());
88 }
89 else {
90 /* This is to be expected at some point in the future. The Online Essentials library
91 * may change its catalog paths, while whatever version of Blender is running right now
92 * still has the same old bundled assets. This means the Bundled Essentials and Online
93 * Essentials diverge. There is no need to bother users with this, as it's bound to
94 * happen eventually.
95 *
96 * Note that this same check happens in the 'All' library as well, and that already
97 * logs this at INFO level, so there really is no need to be louder than DEBUG here. */
98 CLOG_DEBUG(&LOG,
99 "multiple definitions of catalog %s with differing paths (%s vs. %s), "
100 "ignoring second one",
101 existing.catalog_id.str().c_str(),
102 existing.path.c_str(),
103 to_be_ignored.path.c_str());
104 }
105 });
106 };
107
108 load_catalogs_fn(this);
109 if (U.asset_flag & USER_ASSETS_USE_ONLINE_ESSENTIALS) {
111 }
112
113 std::lock_guard lock{catalog_service_mutex_};
114 catalog_service_ = std::move(new_catalog_service);
115}
116
118{
119 static std::string path = []() {
120 const std::optional<std::string> datafiles_path = BKE_appdir_folder_id(
121 BLENDER_SYSTEM_DATAFILES, "assets");
122 return datafiles_path.value_or("");
123 }();
124 return path;
125}
126
127bool skip_experimental_asset_catalog(const UUID & /*catalog_id*/)
128{
129 /* Return true when the catalog_id should be rejected based on experimental features:
130 *
131 * const UUID UUID_my_feature_catalog_id("11111111-2222-3333-4444-555555555555");
132 * if (!U.experimental.use_my_feature && catalog_id == UUID_my_feature_catalog_id) {
133 * return true;
134 * }
135 */
136
137 return false;
138}
139
140/* -------------------------------------------------------------------- */
145
147{
148 static std::string path = []() {
149 return remote_library_cache_directory_path("online-essentials");
150 }();
151 return path;
152}
153
158
160{
161 if (url.is_empty()) {
162 return false;
163 }
164
169 }
170
171 return url == OnlineEssentialsLibrary::URL;
172}
173
175{
176 if (dirpath.is_empty()) {
177 return false;
178 }
179 if (dirpath.endswith(SEP_STR)) {
180 dirpath = dirpath.drop_known_suffix(SEP_STR);
181 }
183
184 return dirpath == online_essentials_cache_directory_path();
185}
186
189 /*is_read_only=*/true,
190 /*remote_url=*/URL,
191 /*name=*/"Online Essentials",
193{
194}
195
196std::optional<AssetLibraryReference> OnlineEssentialsLibrary::library_reference() const
197{
198 AssetLibraryReference library_ref{};
200 library_ref.custom_library_index = -1;
201 return library_ref;
202}
203
205
206} // namespace blender::asset_system
#define USER_EXPERIMENTAL_TEST(userdef, member)
volatile int lock
constexpr StringRef drop_suffix(int64_t n) const
constexpr StringRef drop_known_suffix(StringRef suffix) const
std::optional< AssetLibraryReference > library_reference() const override
std::optional< eAssetImportMethod > import_method() const override
OnDiskAssetLibrary(eAssetLibraryType library_type, StringRef name, StringRef root_path, bool is_read_only)
std::optional< AssetLibraryReference > library_reference() const override
RemoteAssetLibrary(eAssetLibraryType library_type, bool is_read_only, StringRef remote_url, StringRef name, StringRef root_path)
constexpr const char & back() const
constexpr bool is_empty() const
constexpr int64_t size() const
constexpr bool endswith(StringRef suffix) const
std::string resolve_directory_path(StringRef directory)
bool is_online_essentials_dirpath(StringRef dirpath)
bool skip_experimental_asset_catalog(const UUID &catalog_id)
StringRefNull online_essentials_cache_directory_path()
constexpr StringRefNull REMOTE_LIBRARY_TOP_META_FILE_NAME
StringRefNull online_essentials_url()
std::string remote_library_cache_directory_path(StringRefNull library_dirname)
StringRefNull essentials_directory_path()
void remote_library_request_download(const RemoteLibraryDefinitionRef &library_definition)
AssetLibraryReference online_essentials_library_reference()
bool is_online_essentials_url(StringRef url)
bool remote_library_url_ends_with_top_meta_file_name(const StringRef url)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ USER_ASSETS_USE_ONLINE_ESSENTIALS
@ ASSET_LIBRARY_ESSENTIALS
@ ASSET_LIBRARY_ONLINE_ESSENTIALS
std::optional< std::string > BKE_appdir_folder_id(int folder_id, const char *subfolder) ATTR_WARN_UNUSED_RESULT
Definition appdir.cc:762
@ ASSET_IMPORT_APPEND_REUSE
asset_system::AssetLibrary * AS_asset_library_load(const Main *bmain, const AssetLibraryReference &library_reference)
@ BLENDER_SYSTEM_DATAFILES
#define SEP_STR
Definition unit.cc:41