Blender V5.3
asset_catalog_definition_file.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
9#include <iostream>
10
11#include "BLI_fileops.hh"
12#include "BLI_path_utils.hh"
13
14#include "CLG_log.h"
15
17
18namespace blender {
19
20static CLG_LogRef LOG = {"asset.catalog"};
21
22namespace asset_system {
23
25const std::string AssetCatalogDefinitionFile::VERSION_MARKER = "VERSION ";
26
27const std::string AssetCatalogDefinitionFile::HEADER =
28 "# This is an Asset Catalog Definition file for Blender.\n"
29 "#\n"
30 "# Empty lines and lines starting with `#` will be ignored.\n"
31 "# The first non-ignored line should be the version indicator.\n"
32 "# Other lines are of the format \"UUID:catalog/path/for/assets:simple catalog name\"\n";
33
35{
36 return catalogs_.contains(catalog_id);
37}
38
40{
41 catalogs_.add_new(catalog->catalog_id, catalog);
42}
43
45{
46 catalogs_.add_overwrite(catalog->catalog_id, catalog);
47}
48
50{
51 catalogs_.remove(catalog_id);
52}
53
55 const CatalogFilePath &catalog_definition_file_path,
56 AssetCatalogParsedFn catalog_loaded_callback)
57{
58 fstream infile(catalog_definition_file_path, std::ios::in);
59
60 if (!infile.is_open()) {
61 CLOG_ERROR(&LOG, "%s: unable to open file", catalog_definition_file_path.c_str());
62 return;
63 }
64 bool seen_version_number = false;
65 std::string line;
66 while (std::getline(infile, line)) {
67 const StringRef trimmed_line = StringRef(line).trim();
68 if (trimmed_line.is_empty() || trimmed_line[0] == '#') {
69 continue;
70 }
71
72 if (!seen_version_number) {
73 /* The very first non-ignored line should be the version declaration. */
74 const bool is_valid_version = this->parse_version_line(trimmed_line);
75 if (!is_valid_version) {
76 std::cerr << catalog_definition_file_path
77 << ": first line should be version declaration; ignoring file." << std::endl;
78 break;
79 }
80 seen_version_number = true;
81 continue;
82 }
83
84 std::unique_ptr<AssetCatalog> catalog = this->parse_catalog_line(trimmed_line);
85 if (!catalog) {
86 continue;
87 }
88
89 AssetCatalog *non_owning_ptr = catalog.get();
90 const bool keep_catalog = catalog_loaded_callback(std::move(catalog));
91 if (!keep_catalog) {
92 continue;
93 }
94
95 /* The AssetDefinitionFile should include this catalog when writing it back to disk. */
96 this->add_overwrite(non_owning_ptr);
97 }
98}
99
101{
102 if (!line.startswith(VERSION_MARKER)) {
103 return false;
104 }
105
106 const std::string version_string = line.substr(VERSION_MARKER.length());
107 const int file_version = std::atoi(version_string.c_str());
108
109 /* No versioning, just a blunt check whether it's the right one. */
110 return file_version == SUPPORTED_VERSION;
111}
112
113std::unique_ptr<AssetCatalog> AssetCatalogDefinitionFile::parse_catalog_line(const StringRef line)
114{
115 const char delim = ':';
116 const int64_t first_delim = line.find_first_of(delim);
117 if (first_delim == StringRef::not_found) {
118 std::cerr << "Invalid catalog line in " << this->file_path << ": " << line << std::endl;
119 return std::unique_ptr<AssetCatalog>(nullptr);
120 }
121
122 /* Parse the catalog ID. */
123 const std::string id_as_string = line.substr(0, first_delim).trim();
124 bUUID catalog_id;
125 const bool uuid_parsed_ok = BLI_uuid_parse_string(&catalog_id, id_as_string.c_str());
126 if (!uuid_parsed_ok) {
127 std::cerr << "Invalid UUID in " << this->file_path << ": " << line << std::endl;
128 return std::unique_ptr<AssetCatalog>(nullptr);
129 }
130
131 /* Parse the path and simple name. */
132 const StringRef path_and_simple_name = line.substr(first_delim + 1);
133 const int64_t second_delim = path_and_simple_name.find_first_of(delim);
134
135 std::string path_in_file;
136 std::string simple_name;
137 if (second_delim == 0) {
138 /* Delimiter as first character means there is no path. These lines are to be ignored. */
139 return std::unique_ptr<AssetCatalog>(nullptr);
140 }
141
142 if (second_delim == StringRef::not_found) {
143 /* No delimiter means no simple name, just treat it as all "path". */
144 path_in_file = path_and_simple_name;
145 simple_name = "";
146 }
147 else {
148 path_in_file = path_and_simple_name.substr(0, second_delim);
149 simple_name = path_and_simple_name.substr(second_delim + 1).trim();
150 }
151
152 AssetCatalogPath catalog_path = path_in_file;
153 return std::make_unique<AssetCatalog>(catalog_id, catalog_path.cleanup(), simple_name);
154}
155
160
162{
163 BLI_assert_msg(!this->file_path.empty(), "Writing to CDF requires its file path to be known");
164 return this->write_to_disk(this->file_path);
165}
166
168{
169 const CatalogFilePath writable_path = dest_file_path + ".writing";
170 const CatalogFilePath backup_path = dest_file_path + "~";
171
172 if (!this->write_to_disk_unsafe(writable_path)) {
173 /* TODO: communicate what went wrong. */
174 return false;
175 }
176 if (BLI_exists(dest_file_path.c_str())) {
177 if (BLI_rename_overwrite(dest_file_path.c_str(), backup_path.c_str())) {
178 /* TODO: communicate what went wrong. */
179 return false;
180 }
181 }
182 if (BLI_rename_overwrite(writable_path.c_str(), dest_file_path.c_str())) {
183 /* TODO: communicate what went wrong. */
184 return false;
185 }
186
187 return true;
188}
189
191{
192 return BLI_exists(this->file_path.c_str());
193}
194
196{
197 char directory[PATH_MAX];
198 BLI_path_split_dir_part(dest_file_path.c_str(), directory, sizeof(directory));
199 if (!ensure_directory_exists(directory)) {
200 /* TODO(Sybren): pass errors to the UI somehow. */
201 return false;
202 }
203
204 fstream output(dest_file_path, std::ios::out);
205
206 /* TODO(@sybren): remember the line ending style that was originally read, then use that to write
207 * the file again. */
208
209 /* Write the header. */
210 output << HEADER;
211 output << "" << std::endl;
212 output << VERSION_MARKER << SUPPORTED_VERSION << std::endl;
213 output << "" << std::endl;
214
215 /* Write the catalogs, ordered by path (primary) and UUID (secondary). */
216 AssetCatalogOrderedSet catalogs_by_path;
217 for (const AssetCatalog *catalog : catalogs_.values()) {
218 if (catalog->flags.is_deleted) {
219 continue;
220 }
221 catalogs_by_path.insert(catalog);
222 }
223
224 for (const AssetCatalog *catalog : catalogs_by_path) {
225 output << catalog->catalog_id << ":" << catalog->path << ":" << catalog->simple_name
226 << std::endl;
227 }
228 output.close();
229 return !output.bad();
230}
231
233 const CatalogFilePath &directory_path) const
234{
235 /* TODO(@sybren): design a way to get such errors presented to users (or ensure that they never
236 * occur). */
237 if (directory_path.empty()) {
238 std::cerr
239 << "AssetCatalogService: no asset library root configured, unable to ensure it exists."
240 << std::endl;
241 return false;
242 }
243
244 if (BLI_exists(directory_path.data())) {
245 if (!BLI_is_dir(directory_path.data())) {
246 std::cerr << "AssetCatalogService: " << directory_path
247 << " exists but is not a directory, this is not a supported situation."
248 << std::endl;
249 return false;
250 }
251
252 /* Root directory exists, work is done. */
253 return true;
254 }
255
256 /* Ensure the root directory exists. */
257 std::error_code err_code;
258 if (!BLI_dir_create_recursive(directory_path.data())) {
259 std::cerr << "AssetCatalogService: error creating directory " << directory_path << ": "
260 << err_code << std::endl;
261 return false;
262 }
263
264 /* Root directory has been created, work is done. */
265 return true;
266}
267
268std::unique_ptr<AssetCatalogDefinitionFile> AssetCatalogDefinitionFile::copy_and_remap(
269 const OwningAssetCatalogMap &catalogs, const OwningAssetCatalogMap &deleted_catalogs) const
270{
271 auto copy = std::make_unique<AssetCatalogDefinitionFile>(*this);
272 copy->catalogs_.clear();
273
274 /* Remap pointers of the copy from the original AssetCatalogCollection to the given one. */
275 for (CatalogID catalog_id : catalogs_.keys()) {
276 /* The catalog can be in the regular or the deleted map. */
277 const std::unique_ptr<AssetCatalog> *remapped_catalog_uptr_ptr = catalogs.lookup_ptr(
278 catalog_id);
279 if (remapped_catalog_uptr_ptr) {
280 copy->catalogs_.add_new(catalog_id, remapped_catalog_uptr_ptr->get());
281 continue;
282 }
283
284 remapped_catalog_uptr_ptr = deleted_catalogs.lookup_ptr(catalog_id);
285 if (remapped_catalog_uptr_ptr) {
286 copy->catalogs_.add_new(catalog_id, remapped_catalog_uptr_ptr->get());
287 continue;
288 }
289
290 BLI_assert_msg(false, "A CDF should only reference known catalogs.");
291 }
292
293 return copy;
294}
295
296} // namespace asset_system
297
298} // namespace blender
File and directory operations.
#define PATH_MAX
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define BLI_assert_msg(a, msg)
#define output
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
constexpr StringRef()
bool ensure_directory_exists(const CatalogFilePath &directory_path) const
void parse_catalog_file(const CatalogFilePath &catalog_definition_file_path, AssetCatalogParsedFn catalog_loaded_callback)
std::unique_ptr< AssetCatalogDefinitionFile > copy_and_remap(const OwningAssetCatalogMap &catalogs, const OwningAssetCatalogMap &deleted_catalogs) const
bool write_to_disk_unsafe(const CatalogFilePath &dest_file_path) const
std::unique_ptr< AssetCatalog > parse_catalog_line(StringRef line)
FunctionRef< bool(std::unique_ptr< AssetCatalog >)> AssetCatalogParsedFn
constexpr int64_t find_first_of(StringRef chars, int64_t pos=0) const
constexpr StringRef trim() const
constexpr bool startswith(StringRef prefix) const
static constexpr int64_t not_found
constexpr bool is_empty() const
constexpr StringRef substr(int64_t start, int64_t size) const
Map< CatalogID, std::unique_ptr< AssetCatalog > > OwningAssetCatalogMap
std::set< const AssetCatalog *, AssetCatalogLessThan > AssetCatalogOrderedSet
BMesh Iterators.
__int64 int64_t
Definition BKE_text.h:90
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
Definition fileops_c.cc:416
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
int BLI_rename_overwrite(const char *from, const char *to) ATTR_NONNULL()
Definition fileops_c.cc:541
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:457
bool BLI_uuid_parse_string(bUUID *uuid, const char *buffer) ATTR_NONNULL()
Definition uuid.cc:105
static CLG_LogRef LOG
Definition logging.cpp:12
Universally Unique Identifier according to RFC4122.