Blender V5.3
AS_asset_catalog.hh
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
9#pragma once
10
11#include <memory>
12#include <mutex>
13#include <set>
14#include <string>
15
16#include "BLI_function_ref.hh"
17#include "BLI_map.hh"
18#include "BLI_set.hh"
19#include "BLI_uuid.hh"
20#include "BLI_vector.hh"
21
23
25
26class AssetCatalog;
31
33using CatalogPathComponent = std::string;
34/* Would be nice to be able to use `std::filesystem::path` for this, but it's currently not
35 * available on the minimum macOS target version. */
36using CatalogFilePath = std::string;
38
39/* Manages the asset catalogs of a single asset library (i.e. of catalogs defined in a single
40 * directory hierarchy). */
42 std::unique_ptr<AssetCatalogCollection> catalog_collection_;
43
47 std::shared_ptr<AssetCatalogTree> catalog_tree_;
48 std::recursive_mutex catalog_tree_mutex_;
49
52
53 CatalogFilePath asset_library_root_;
54 bool is_read_only_ = false;
55
56 friend class AssetLibraryService;
57 friend class AssetLibrary;
58
59 public:
61
62 struct read_only_tag {};
63
64 explicit AssetCatalogService(const CatalogFilePath &asset_library_root = {},
65 std::optional<read_only_tag> read_only_tag = std::nullopt);
66 explicit AssetCatalogService(read_only_tag);
68
77 void tag_has_unsaved_changes(AssetCatalog *edited_catalog = nullptr);
78 bool has_unsaved_changes() const;
79
85 bool is_read_only() const;
86
88 void load_from_disk();
90 void load_from_disk(const CatalogFilePath &file_or_directory_path);
91
99 void add_from_existing(const AssetCatalogService &other_service,
100 FunctionRef<void(const AssetCatalog &existing,
101 const AssetCatalog &to_be_ignored)> on_duplicate_items);
102
121 bool write_to_disk(const CatalogFilePath &blend_file_path);
122
132
141 void reload_catalogs();
142
144 AssetCatalog *find_catalog(CatalogID catalog_id) const;
145
154
159 bool is_catalog_known(CatalogID catalog_id) const;
160
167 AssetCatalogFilter create_catalog_filter(CatalogID active_catalog_id) const;
168
178 AssetCatalog *create_catalog(const AssetCatalogPath &catalog_path);
179
184
189 void prune_catalogs_by_id(CatalogID catalog_id);
190
194 void update_catalog_path(CatalogID catalog_id, const AssetCatalogPath &new_catalog_path);
195
199 std::shared_ptr<const AssetCatalogTree> catalog_tree();
200
202 bool is_empty() const;
203
208 void undo_push();
213 void undo();
214 bool is_undo_possbile() const;
219 void redo();
220 bool is_redo_possbile() const;
221
222 protected:
223 void load_directory_recursive(const CatalogFilePath &directory_path);
224 void load_single_file(const CatalogFilePath &catalog_definition_file_path);
225
227 bool write_to_disk_ex(const CatalogFilePath &blend_file_path);
230
238 void purge_catalogs_not_listed(const Set<CatalogID> &catalogs_to_keep);
239
253 void delete_catalog_by_id_soft(CatalogID catalog_id);
254
259 void delete_catalog_by_id_hard(CatalogID catalog_id);
260
261 std::unique_ptr<AssetCatalogDefinitionFile> parse_catalog_file(
262 const CatalogFilePath &catalog_definition_file_path);
263
268 std::unique_ptr<AssetCatalogDefinitionFile> construct_cdf_in_memory(
269 const CatalogFilePath &file_path) const;
270
278 const CatalogFilePath &blend_file_path);
279
280 std::unique_ptr<AssetCatalogTree> read_into_tree() const;
287
292
297
298 /* For access by subclasses, as those will not be marked as friend by #AssetCatalogCollection. */
300 const OwningAssetCatalogMap &get_catalogs() const;
302};
303
313 public:
324 std::string simple_name;
325
326 struct Flags {
327 /* Treat this catalog as deleted. Keeping deleted catalogs around is necessary to support
328 * merging of on-disk changes with in-memory changes. */
329 bool is_deleted = false;
330
331 /* Sort this catalog first when there are multiple catalogs with the same catalog path. This
332 * ensures that in a situation where missing catalogs were auto-created, and then
333 * load-and-merged with a file that also has these catalogs, the first one in that file is
334 * always sorted first, regardless of the sort order of its UUID. */
335 bool is_first_loaded = false;
336
337 /* Merging on-disk changes into memory will not overwrite this catalog.
338 * For example, when a catalog was renamed (i.e. changed path) in this Blender session,
339 * reloading the catalog definition file should not overwrite that change.
340 *
341 * Note that this flag is ignored when is_deleted=true; deleted catalogs that are still in
342 * memory are considered "unsaved" by definition. */
345
346 AssetCatalog() = delete;
348
355 static std::unique_ptr<AssetCatalog> from_path(const AssetCatalogPath &path);
356
358 void simple_name_refresh();
359
360 protected:
362 static std::string sensible_simple_name_for_path(const AssetCatalogPath &path);
363};
364
367 bool operator()(const AssetCatalog *lhs, const AssetCatalog *rhs) const
368 {
369 if (lhs->path != rhs->path) {
370 return lhs->path < rhs->path;
371 }
372
373 if (lhs->flags.is_first_loaded != rhs->flags.is_first_loaded) {
374 return lhs->flags.is_first_loaded;
375 }
376
377 return lhs->catalog_id < rhs->catalog_id;
378 }
379};
380
385using AssetCatalogOrderedSet = std::set<const AssetCatalog *, AssetCatalogLessThan>;
386using MutableAssetCatalogOrderedSet = std::set<AssetCatalog *, AssetCatalogLessThan>;
387
394 const Set<CatalogID> matching_catalog_ids_;
395 const Set<CatalogID> known_catalog_ids_;
396
397 friend AssetCatalogService;
398
399 public:
400 bool contains(CatalogID asset_catalog_id) const;
401
402 /* So that all unknown catalogs can be shown under "Unassigned". */
403 bool is_known(CatalogID asset_catalog_id) const;
404
405 protected:
406 explicit AssetCatalogFilter(Set<CatalogID> &&matching_catalog_ids,
407 Set<CatalogID> &&known_catalog_ids);
408};
409
410} // namespace blender::asset_system
AssetCatalogFilter(Set< CatalogID > &&matching_catalog_ids, Set< CatalogID > &&known_catalog_ids)
void load_single_file(const CatalogFilePath &catalog_definition_file_path)
AssetCatalogFilter create_catalog_filter(CatalogID active_catalog_id) const
AssetCatalog * find_catalog_by_path(const AssetCatalogPath &path) const
AssetCatalog * find_catalog(CatalogID catalog_id) const
std::shared_ptr< const AssetCatalogTree > catalog_tree()
bool write_to_disk(const CatalogFilePath &blend_file_path)
std::unique_ptr< AssetCatalogTree > read_into_tree() const
void purge_catalogs_not_listed(const Set< CatalogID > &catalogs_to_keep)
std::unique_ptr< AssetCatalogDefinitionFile > parse_catalog_file(const CatalogFilePath &catalog_definition_file_path)
std::unique_ptr< AssetCatalogDefinitionFile > construct_cdf_in_memory(const CatalogFilePath &file_path) const
AssetCatalog * create_catalog(const AssetCatalogPath &catalog_path)
void update_catalog_path(CatalogID catalog_id, const AssetCatalogPath &new_catalog_path)
void add_from_existing(const AssetCatalogService &other_service, FunctionRef< void(const AssetCatalog &existing, const AssetCatalog &to_be_ignored)> on_duplicate_items)
static CatalogFilePath find_suitable_cdf_path_for_writing(const CatalogFilePath &blend_file_path)
bool write_to_disk_ex(const CatalogFilePath &blend_file_path)
void load_directory_recursive(const CatalogFilePath &directory_path)
bool is_catalog_known_with_unsaved_changes(CatalogID catalog_id) const
void tag_has_unsaved_changes(AssetCatalog *edited_catalog=nullptr)
static const CatalogFilePath DEFAULT_CATALOG_FILENAME
AssetCatalogService(const CatalogFilePath &asset_library_root={}, std::optional< read_only_tag > read_only_tag=std::nullopt)
const AssetCatalogDefinitionFile * get_catalog_definition_file() const
static std::unique_ptr< AssetCatalog > from_path(const AssetCatalogPath &path)
static std::string sensible_simple_name_for_path(const AssetCatalogPath &path)
struct blender::asset_system::AssetCatalog::Flags flags
std::set< AssetCatalog *, AssetCatalogLessThan > MutableAssetCatalogOrderedSet
Map< CatalogID, std::unique_ptr< AssetCatalog > > OwningAssetCatalogMap
std::set< const AssetCatalog *, AssetCatalogLessThan > AssetCatalogOrderedSet
bool operator()(const AssetCatalog *lhs, const AssetCatalog *rhs) const