Blender  V3.3
asset_library_service.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
8 
9 #include "BKE_blender.h"
10 
11 #include "BLI_fileops.h" /* For PATH_MAX (at least on Windows). */
12 #include "BLI_path_util.h"
13 #include "BLI_string_ref.hh"
14 
15 #include "CLG_log.h"
16 
17 static CLG_LogRef LOG = {"bke.asset_service"};
18 
19 namespace blender::bke {
20 
21 std::unique_ptr<AssetLibraryService> AssetLibraryService::instance_;
23 
25 {
26  if (!instance_) {
28  }
29  return instance_.get();
30 }
31 
33 {
34  if (!instance_) {
35  return;
36  }
37  instance_->app_handler_unregister();
38  instance_.reset();
39 }
40 
41 namespace {
42 std::string normalize_directory_path(StringRefNull directory)
43 {
44 
45  char dir_normalized[PATH_MAX];
46  STRNCPY(dir_normalized, directory.c_str());
47  BLI_path_normalize_dir(nullptr, dir_normalized);
48  return std::string(dir_normalized);
49 }
50 } // namespace
51 
53 {
54  BLI_assert_msg(!top_level_directory.is_empty(),
55  "top level directory must be given for on-disk asset library");
56 
57  std::string top_dir_trailing_slash = normalize_directory_path(top_level_directory);
58 
59  AssetLibraryPtr *lib_uptr_ptr = on_disk_libraries_.lookup_ptr(top_dir_trailing_slash);
60  if (lib_uptr_ptr != nullptr) {
61  CLOG_INFO(&LOG, 2, "get \"%s\" (cached)", top_dir_trailing_slash.c_str());
62  AssetLibrary *lib = lib_uptr_ptr->get();
63  lib->refresh();
64  return lib;
65  }
66 
67  AssetLibraryPtr lib_uptr = std::make_unique<AssetLibrary>();
68  AssetLibrary *lib = lib_uptr.get();
69 
70  lib->on_blend_save_handler_register();
71  lib->load(top_dir_trailing_slash);
72 
73  on_disk_libraries_.add_new(top_dir_trailing_slash, std::move(lib_uptr));
74  CLOG_INFO(&LOG, 2, "get \"%s\" (loaded)", top_dir_trailing_slash.c_str());
75  return lib;
76 }
77 
79 {
81  CLOG_INFO(&LOG, 2, "get current file lib (cached)");
82  }
83  else {
84  CLOG_INFO(&LOG, 2, "get current file lib (loaded)");
85  current_file_library_ = std::make_unique<AssetLibrary>();
86  current_file_library_->on_blend_save_handler_register();
87  }
88 
90  return lib;
91 }
92 
94 {
95  instance_ = std::make_unique<AssetLibraryService>();
96  instance_->app_handler_register();
97 
99  /* Ensure the instance gets freed before Blender's memory leak detector runs. */
100  BKE_blender_atexit_register([](void * /*user_data*/) { AssetLibraryService::destroy(); },
101  nullptr);
103  }
104 }
105 
106 static void on_blendfile_load(struct Main * /*bMain*/,
107  struct PointerRNA ** /*pointers*/,
108  const int /*num_pointers*/,
109  void * /*arg*/)
110 {
112 }
113 
115 {
116  /* The callback system doesn't own `on_load_callback_store_`. */
118 
121 
123 }
124 
126 {
128  on_load_callback_store_.func = nullptr;
129  on_load_callback_store_.arg = nullptr;
130 }
131 
133 {
134  if (current_file_library_ && current_file_library_->catalog_service->has_unsaved_changes()) {
135  return true;
136  }
137 
138  for (const auto &asset_lib_uptr : on_disk_libraries_.values()) {
139  if (asset_lib_uptr->catalog_service->has_unsaved_changes()) {
140  return true;
141  }
142  }
143 
144  return false;
145 }
146 
147 } // namespace blender::bke
Blender util stuff.
void BKE_blender_atexit_register(void(*func)(void *user_data), void *user_data)
Definition: blender.c:396
void BKE_callback_add(bCallbackFuncStore *funcstore, eCbEvent evt)
Definition: callbacks.c:72
void BKE_callback_remove(bCallbackFuncStore *funcstore, eCbEvent evt)
Definition: callbacks.c:79
@ BKE_CB_EVT_LOAD_PRE
Definition: BKE_callbacks.h:84
#define BLI_assert_msg(a, msg)
Definition: BLI_assert.h:53
File and directory operations.
#define PATH_MAX
Definition: BLI_fileops.h:29
void BLI_path_normalize_dir(const char *relabase, char *dir) ATTR_NONNULL(2)
Definition: path_util.c:221
#define STRNCPY(dst, src)
Definition: BLI_string.h:483
#define CLOG_INFO(clg_ref, level,...)
Definition: CLG_log.h:187
static CLG_LogRef LOG
ValueIterator values() const
Definition: BLI_map.hh:840
void add_new(const Key &key, const Value &value)
Definition: BLI_map.hh:220
const Value * lookup_ptr(const Key &key) const
Definition: BLI_map.hh:463
constexpr bool is_empty() const
constexpr const char * c_str() const
static AssetLibraryService * get()
AssetLibrary * get_asset_library_on_disk(StringRefNull top_level_directory)
static std::unique_ptr< AssetLibraryService > instance_
std::unique_ptr< AssetLibrary > AssetLibraryPtr
Map< std::string, AssetLibraryPtr > on_disk_libraries_
DRWShaderLibrary * lib
static void on_blendfile_load(struct Main *, struct PointerRNA **, const int, void *)
Definition: BKE_main.h:121
void(* func)(struct Main *, struct PointerRNA **, int num_pointers, void *arg)