Blender V5.0
main_namemap.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 "BKE_idtype.hh"
10#include "BKE_lib_id.hh"
11#include "BKE_library.hh"
12#include "BKE_main.hh"
13#include "BKE_main_namemap.hh"
14
15#include "BLI_assert.h"
16#include "BLI_bit_span_ops.hh"
17#include "BLI_bit_vector.hh"
18#include "BLI_listbase.h"
19#include "BLI_map.hh"
20#include "BLI_math_base.hh"
21#include "BLI_string.h"
22#include "BLI_string_utf8.h"
23#include "BLI_string_utils.hh"
24
25#include "DNA_ID.h"
26
27#include "MEM_guardedalloc.h"
28
29#include "CLG_log.h"
30
31#include <fmt/format.h>
32
33static CLG_LogRef LOG = {"lib.main_namemap"};
34
35// #define DEBUG_PRINT_MEMORY_USAGE
36
37using namespace blender;
38
39/* Assumes and ensure that the suffix number can never go beyond 1 billion. */
40constexpr int MAX_NUMBER = 999999999;
41/* Value representing that there is no available number. Must be negative value. */
42constexpr int NO_AVAILABLE_NUMBER = -1;
43
44/* Tracking of used numeric suffixes. For each base name:
45 *
46 * - Exactly track which of the lowest 1023 suffixes are in use,
47 * whenever there is a name collision we pick the lowest "unused"
48 * one. This is done with a bit vector.
49 * - Above 1023, do not track them exactly, just track the maximum
50 * suffix value seen so far. Upon collision, assign number that is
51 * one larger.
52 */
54 static constexpr int max_exact_tracking = 1023;
55 std::optional<int> max_value_in_use = {};
57 /* Only created when required. Used to manage cases where the same numeric value is used by
58 * several unique full names ('Foo.1' and 'Foo.001' e.g.).
59 *
60 * The key is the suffix numeric value.
61 * The value is the number of how many different full names using that same base name and numeric
62 * suffix value are currently registered.
63 *
64 * This code will never generate such cases for local maps, but it needs to support users doing
65 * so explicitly.
66 *
67 * For global maps, this is a much more common case, as duplicates of ID names across libraries
68 * are fairly common. */
69 std::optional<Map<int, int>> numbers_multi_usages = std::nullopt;
70
71 void mark_used(const int number)
72 {
73 BLI_assert(number >= 0);
74 if (number >= 0 && number <= max_exact_tracking) {
75 if (this->mask.size() <= number) {
76 this->mask.resize(number + 1);
77 }
78 if (this->mask[number]) {
79 if (!this->numbers_multi_usages.has_value()) {
80 this->numbers_multi_usages.emplace();
81 }
82 int &multi_usages_num = this->numbers_multi_usages->lookup_or_add(number, 1);
83 BLI_assert(multi_usages_num >= 1);
84 multi_usages_num++;
85 }
86 else {
87 this->mask[number].set(true);
88 }
89 }
90 if (number <= MAX_NUMBER) {
91 if (this->max_value_in_use) {
92 math::max_inplace(this->max_value_in_use.value(), number);
93 }
94 else {
95 this->max_value_in_use = number;
96 }
97 }
98 }
99
100 void mark_unused(const int number)
101 {
102 BLI_assert(number >= 0);
103 if (number >= 0 && number <= max_exact_tracking) {
104 BLI_assert_msg(number < this->mask.size(),
105 "Trying to unregister a number suffix higher than current size of the bit "
106 "vector, should never happen.");
107
108 if (this->numbers_multi_usages.has_value() && this->numbers_multi_usages->contains(number)) {
109 int &multi_usages_num = this->numbers_multi_usages->lookup(number);
110 BLI_assert(multi_usages_num > 1);
111 multi_usages_num--;
112 if (multi_usages_num == 1) {
113 this->numbers_multi_usages->remove_contained(number);
114 }
115 /* Do not unset the matching bit, nor handle #max_value_in_use, since there are more IDs
116 * with the same base name and numeric suffix value. */
117 return;
118 }
119
120 this->mask[number].set(false);
121 }
122 if (number == this->max_value_in_use.value_or(NO_AVAILABLE_NUMBER)) {
123 if (number > 0) {
124 this->max_value_in_use.value()--;
125 }
126 else {
127 this->max_value_in_use.reset();
128 }
129 }
130 }
131
139 {
140 if (this->mask.size() < 2) {
141 /* No numbered entry was ever registered for this name yet, so first non-null value is
142 * unused. */
143 return 1;
144 }
145 /* Never pick the `0` value (e.g. if 'Foo.001' is used and another 'Foo.001' is requested,
146 * return 'Foo.002' and not 'Foo'). So only search on mask[1:] range. */
147 BitSpan search_mask(this->mask.data(), IndexRange(1, this->mask.size() - 1));
148 std::optional<int64_t> result = find_first_0_index(search_mask);
149 if (result) {
150 return int(*result + 1);
151 }
152 if (this->mask.size() <= max_exact_tracking) {
153 /* No need to increase size of the mask here, this will be done by calls to #mark_used once
154 * the final name with its final number has been defined. */
155 return int(this->mask.size());
156 }
157 if (this->max_value_in_use) {
158 if (this->max_value_in_use.value() + 1 <= MAX_NUMBER) {
159 return this->max_value_in_use.value() + 1;
160 }
161 return NO_AVAILABLE_NUMBER;
162 }
163 return 1;
164 }
165};
166
167/* Tracking of names for a single ID type. */
169 /* Map of full names that are in use, and the amount of times they are used.
170 * For local maps, the number values must all always be `1`.
171 * For global maps, a same name can be used by several IDs from different libraries. */
173 /* For each base name (i.e. without numeric suffix), track the
174 * numeric suffixes that are in use. */
176};
177
179 std::array<UniqueName_TypeMap, INDEX_ID_MAX> type_maps;
180 /* Whether this map covers all IDs in its Main, or only the local (or a specific library) ones.
181 */
183
185
186 UniqueName_TypeMap &find_by_type(const short id_type)
187 {
188 int index = BKE_idtype_idcode_to_index(id_type);
189 return type_maps[size_t(index)];
190 }
191
192 void populate(Main &bmain, Library *library, ID *ignore_id)
193 {
194 for (UniqueName_TypeMap &type_map : this->type_maps) {
195 type_map.full_names.clear();
196 type_map.base_name_to_num_suffix.clear();
197 }
198 ID *id;
199 FOREACH_MAIN_ID_BEGIN (&bmain, id) {
200 if ((id == ignore_id) || (!this->is_global && (id->lib != library))) {
201 continue;
202 }
203 UniqueName_TypeMap &type_map = this->find_by_type(GS(id->name));
204
205 /* Insert the full name into the map. */
206 if (this->is_global) {
207 /* Global name-map is expected to have several IDs using the same name (from different
208 * libraries). */
210 count++;
211 if (count > 1) {
212 /* Name is already used at least once, just increase user-count. */
213 continue;
214 }
215 }
216 else {
217 /* For non-global name-maps, there should only be one usage for each name, adding the
218 * full-name as key should always return `true`. */
219 if (!type_map.full_names.add(BKE_id_name(*id), 1)) {
220 /* Do not assert, this code is also used by #BKE_main_namemap_validate_and_fix, where
221 * duplicates are expected. */
222#if 0
223 BLI_assert_msg(false,
224 "The key (name) already exists in the namemap, should only happen when "
225 "`do_global` is true.");
226#endif
227 continue;
228 }
229 }
230
231 /* Get the name and number parts ("name.number"). */
232 int number = 0;
233 const std::string name_base = BLI_string_split_name_number(BKE_id_name(*id), '.', number);
234
235 /* Get and update the entry for this base name. */
236 std::unique_ptr<UniqueName_Value> &val = type_map.base_name_to_num_suffix.lookup_or_add_as(
237 name_base, std::make_unique<UniqueName_Value>(UniqueName_Value{}));
238 val->mark_used(number);
239 }
241 }
242
243 /* Add the given name to the given #type_map, returns `true` if added, `false` if it was already
244 * in the map. */
246 StringRef name_full,
247 StringRef name_base,
248 const int number)
249 {
250 BLI_assert(name_full.size() < MAX_ID_NAME - 2);
251
252 if (this->is_global) {
253 /* By definition adding to global map is always successful. */
254 int &count = type_map.full_names.lookup_or_add_as(name_full, 0);
255 if (!count) {
256 std::unique_ptr<UniqueName_Value> &val = type_map.base_name_to_num_suffix.lookup_or_add_as(
257 name_base, std::make_unique<UniqueName_Value>(UniqueName_Value{}));
258 val->mark_used(number);
259 }
260 count++;
261 return;
262 }
263
264 if (!type_map.full_names.add(name_full, 1)) {
266 false, "Adding a name to Main namemaps that was already in it, should never happen.");
267 return;
268 }
269
270 std::unique_ptr<UniqueName_Value> &val = type_map.base_name_to_num_suffix.lookup_or_add_as(
271 name_base, std::make_unique<UniqueName_Value>(UniqueName_Value{}));
272 val->mark_used(number);
273 }
274 void add_name(const short id_type, StringRef name_full, StringRef name_base, const int number)
275 {
276 this->add_name(this->find_by_type(id_type), name_full, name_base, number);
277 }
278
279 /* Remove a full name_full from the specified #type_map. Trying to remove an unknown
280 * (unregistered) name_full is an error. */
282 {
283 BLI_assert(name_full.size() < MAX_ID_NAME - 2);
284
285 if (this->is_global) {
286 /* By definition adding to global map is always successful. */
287 int *count = type_map.full_names.lookup_ptr(name_full);
288 if (!count) {
290 false, "Removing a name from Main namemaps that was not in it, should never happen.");
291 return;
292 }
293 if (*count > 1) {
294 (*count)--;
295 }
296 else {
297 BLI_assert(*count == 1);
298 type_map.full_names.remove_contained(name_full);
299 }
300 }
301 else if (!type_map.full_names.remove(name_full)) {
303 false, "Removing a name from Main namemaps that was not in it, should never happen.");
304 return;
305 }
306
307 int number = 0;
308 const std::string name_base = BLI_string_split_name_number(name_full, '.', number);
309 std::unique_ptr<UniqueName_Value> *val = type_map.base_name_to_num_suffix.lookup_ptr(
310 name_base);
311 if (val == nullptr) {
313 return;
314 }
315 val->get()->mark_unused(number);
316 if (!val->get()->max_value_in_use) {
317 /* This was the only base name usage, remove the whole key. */
318 type_map.base_name_to_num_suffix.remove(name_base);
319 }
320 }
321 void remove_full_name(const short id_type, blender::StringRef name_full)
322 {
323 this->remove_full_name(this->find_by_type(id_type), name_full);
324 }
325};
326
328{
329 if (*r_name_map == nullptr) {
330 return;
331 }
332#ifdef DEBUG_PRINT_MEMORY_USAGE
333 int64_t size_full_names = 0;
334 int64_t size_base_names = 0;
335 for (const UniqueName_TypeMap &type_map : (*r_name_map)->type_maps) {
336 size_full_names += type_map.full_names.size_in_bytes();
337 size_base_names += type_map.base_name_to_num_suffix.size_in_bytes();
338 }
339 printf("NameMap memory usage: full_names %.1fKB, base_names %.1fKB\n",
340 size_full_names / 1024.0,
341 size_base_names / 1024.0);
342#endif
343 MEM_SAFE_DELETE(*r_name_map);
344}
345
347{
348 auto bmain_namemap_clear = [](Main *bmain_iter) -> void {
349 BKE_main_namemap_destroy(&bmain_iter->name_map);
350 BKE_main_namemap_destroy(&bmain_iter->name_map_global);
351 for (Library *lib_iter = static_cast<Library *>(bmain_iter->libraries.first);
352 lib_iter != nullptr;
353 lib_iter = static_cast<Library *>(lib_iter->id.next))
354 {
355 BKE_main_namemap_destroy(&lib_iter->runtime->name_map);
356 }
357 };
358
359 if (bmain.split_mains) {
360 BLI_assert_msg(bmain.split_mains->contains(&bmain),
361 "Main should always be part of its own `split_mains`");
362 for (Main *bmain_iter : *bmain.split_mains) {
363 bmain_namemap_clear(bmain_iter);
364 }
365 }
366 else {
367 bmain_namemap_clear(&bmain);
368 }
369}
370
371/* Get the global (local and linked IDs) name map object used for the given Main/ID.
372 * Lazily creates and populates the contents of the name map, if ensure_created is true.
373 * NOTE: if the contents are populated, the name of the given #ignore_id ID (if any) is not added.
374 */
376 ID *ignore_id,
377 const bool ensure_created)
378{
379 if (ensure_created && bmain.name_map_global == nullptr) {
380 bmain.name_map_global = MEM_new<UniqueName_Map>(__func__, true);
381 bmain.name_map_global->populate(bmain, nullptr, ignore_id);
382 }
383 return bmain.name_map_global;
384}
385
386/* Get the local or library-specific (when #lib is not null) name map object used for the given
387 * Main/ID. Lazily creates and populates the contents of the name map, if ensure_created is true.
388 * NOTE: if the contents are populated, the name of the given #ignore_id ID (if any) is not added.
389 */
391 Library *lib,
392 ID *ignore_id,
393 const bool ensure_created)
394{
395 if (lib != nullptr) {
396 if (ensure_created && lib->runtime->name_map == nullptr) {
397 lib->runtime->name_map = MEM_new<UniqueName_Map>(__func__, false);
398 lib->runtime->name_map->populate(bmain, lib, ignore_id);
399 }
400 return lib->runtime->name_map;
401 }
402 if (ensure_created && bmain.name_map == nullptr) {
403 bmain.name_map = MEM_new<UniqueName_Map>(__func__, false);
404 bmain.name_map->populate(bmain, lib, ignore_id);
405 }
406 return bmain.name_map;
407}
408
409bool BKE_main_global_namemap_contain_name(Main &bmain, const short id_type, StringRef name)
410{
411 UniqueName_Map *name_map = get_global_namemap_for(bmain, nullptr, true);
412 BLI_assert(name_map != nullptr);
413 BLI_assert(name.size() < MAX_ID_NAME - 2);
414 UniqueName_TypeMap &type_map = name_map->find_by_type(id_type);
415
416 return type_map.full_names.contains(name);
417}
418
419bool BKE_main_namemap_contain_name(Main &bmain, Library *lib, const short id_type, StringRef name)
420{
421 UniqueName_Map *name_map = get_namemap_for(bmain, lib, nullptr, true);
422 BLI_assert(name_map != nullptr);
423 BLI_assert(name.size() < MAX_ID_NAME - 2);
424 UniqueName_TypeMap &type_map = name_map->find_by_type(id_type);
425
426 return type_map.full_names.contains(name);
427}
428
442 const StringRefNull base_name,
443 const int number,
444 std::string &r_name_final)
445{
446 /* In case no number value is available, current base name cannot be used to generate a final
447 * full name. */
448 if (number != NO_AVAILABLE_NUMBER) {
449 BLI_assert(number >= 0 && number <= MAX_NUMBER);
450 r_name_final = fmt::format("{}.{:03}", base_name, number);
451 /* Most common case, there is a valid number suffix value and it fits in the #MAX_ID_NAME - 2
452 * length limit.
453 */
454 if (r_name_final.size() < MAX_ID_NAME - 2) {
455 return true;
456 }
457 }
458
459 /* The base name cannot be used as-is, it needs to be modified, and a new number suffix must be
460 * generated for it. */
461 r_name_final = base_name;
462
463 /* If the base name is long enough, shorten it by one (UTF8) char, until a base name with
464 * available number suffixes is found. */
465 while (r_name_final.size() > 8) {
466 char base_name_modified[MAX_ID_NAME - 2];
467
468 BLI_strncpy(base_name_modified, r_name_final.c_str(), r_name_final.size() + 1);
469 base_name_modified[r_name_final.size() - 1] = '\0';
470 /* Raw truncation of an UTF8 string may generate invalid UTF8 char-code at the end.
471 * Ensure we get a valid one now. */
472 BLI_str_utf8_invalid_strip(base_name_modified, r_name_final.size() - 1);
473
474 r_name_final = base_name_modified;
475 std::unique_ptr<UniqueName_Value> *val = type_map.base_name_to_num_suffix.lookup_ptr(
476 r_name_final);
477 if (!val || val->get()->max_value_in_use.value_or(0) < MAX_NUMBER) {
478 return false;
479 }
480 }
481 /* Else, extend the name with an increasing three-or-more-digits number, as it's better to add
482 * gibberish at the end of a short name, rather than shorten it further. */
483 uint64_t suffix = 1;
484 const StringRef new_base_name = r_name_final;
485 r_name_final = fmt::format("{}_{:03}", r_name_final, suffix);
486 while (r_name_final.size() < MAX_ID_NAME - 2 - 12) {
487 std::unique_ptr<UniqueName_Value> *val = type_map.base_name_to_num_suffix.lookup_ptr(
488 r_name_final);
489 if (!val || val->get()->max_value_in_use.value_or(0) < MAX_NUMBER) {
490 return false;
491 }
492 suffix++;
493 r_name_final = fmt::format("{}_{:03}", new_base_name, suffix);
494 }
495
496 /* WARNING: This code is absolute last defense, essentially theoretical case at this point.
497 * It is not expected to ever be reached in practice. It is also virtually impossible to actually
498 * test that case, given the enormous amount of IDs that would need to be created before it is
499 * reached. */
501 "Impossible to find an available name for '%s' base name, even by editing that base "
502 "name. This should never happen in real-life scenarii. Now trying to brute-force "
503 "generate random names until a free one is found.",
504 base_name.c_str());
505 BLI_assert(new_base_name.size() <= 8);
506 while (true) {
507 r_name_final = fmt::format("{}_{}", new_base_name, uint32_t(get_default_hash(r_name_final)));
508 std::unique_ptr<UniqueName_Value> *val = type_map.base_name_to_num_suffix.lookup_ptr(
509 r_name_final);
510 if (!val || val->get()->max_value_in_use.value_or(0) < MAX_NUMBER) {
511 return false;
512 }
513 }
514}
515
516static bool namemap_get_name(Main &bmain,
517 ID &id,
518 std::string &r_name_full,
519 const bool do_unique_in_bmain)
520{
521 UniqueName_Map *name_map = do_unique_in_bmain ? get_global_namemap_for(bmain, &id, true) :
522 get_namemap_for(bmain, id.lib, &id, true);
523 UniqueName_Map *name_map_other = do_unique_in_bmain ?
524 get_namemap_for(bmain, id.lib, &id, false) :
525 get_global_namemap_for(bmain, &id, false);
526 BLI_assert(name_map != nullptr);
527 BLI_assert(r_name_full.size() < MAX_ID_NAME - 2);
528 UniqueName_TypeMap &type_map = name_map->find_by_type(GS(id.name));
529
530 bool is_name_changed = false;
531
532 while (true) {
533 /* Get the name and number parts ("name.number"). */
534 int number = 0;
535 const std::string name_base = BLI_string_split_name_number(r_name_full, '.', number);
536 std::unique_ptr<UniqueName_Value> &val = type_map.base_name_to_num_suffix.lookup_or_add_as(
537 name_base, std::make_unique<UniqueName_Value>(UniqueName_Value{}));
538
539 /* If the full original name is unused, and its number suffix is unused, or is above the max
540 * managed value, the name can be used directly.
541 *
542 * NOTE: The second part of the check is designed to prevent issues with different names with
543 * the same name base, and the same numeric value as suffix, but written differently.
544 * E.g. `Mesh.001` and `Mesh.1` would both "use" the numeric suffix for base name `Mesh`.
545 * Removing `Mesh.1` would then mark `001` suffix as available, which would be incorrect. */
546 if (!type_map.full_names.contains(r_name_full)) {
547 name_map->add_name(type_map, r_name_full, name_base, number);
548 if (name_map_other != nullptr) {
549 name_map_other->add_name(GS(id.name), r_name_full, name_base, number);
550 }
551 return is_name_changed;
552 }
553
554 /* At this point, if this is the first iteration, the initially given name is colliding with an
555 * existing ID name, and has to be modified. If this is a later iteration, the given name has
556 * already been modified one way or another. */
557 is_name_changed = true;
558
559 /* The base name and current number suffix are already used.
560 * Request the lowest available valid number suffix (will return #NO_AVAILABLE_NUMBER if none
561 * are available for the current base name). */
562 const int number_to_use = val->get_smallest_unused();
563
564 /* Try to build final name from the current base name and the number.
565 * Note that this will fail if the suffix number is #NO_AVAILABLE_NUMBER, or if the base name
566 * and suffix number would give a too long name. In such cases, this call will modify
567 * the base name and put it into r_name_full, and a new iteration to find a suitable suffix
568 * number and valid full name is needed. */
569 if (!id_name_final_build(type_map, name_base, number_to_use, r_name_full)) {
570 continue;
571 }
572
573 /* All good, add final name to the set. */
574 name_map->add_name(type_map, r_name_full, name_base, number_to_use);
575 if (name_map_other != nullptr) {
576 name_map_other->add_name(GS(id.name), r_name_full, name_base, number_to_use);
577 }
578 return is_name_changed;
579 }
580}
581
582bool BKE_main_namemap_get_unique_name(Main &bmain, ID &id, char *r_name)
583{
584 std::string r_name_full = r_name;
585 BLI_assert(r_name_full.size() < MAX_ID_NAME - 2);
586 const bool is_name_modified = namemap_get_name(bmain, id, r_name_full, false);
587 BLI_assert(r_name_full.size() < MAX_ID_NAME - 2);
588 BLI_strncpy(r_name, r_name_full.c_str(), MAX_ID_NAME - 2);
589 return is_name_modified;
590}
591bool BKE_main_global_namemap_get_unique_name(Main &bmain, ID &id, char *r_name)
592{
593 std::string r_name_full = r_name;
594 BLI_assert(r_name_full.size() < MAX_ID_NAME - 2);
595 const bool is_name_modified = namemap_get_name(bmain, id, r_name_full, true);
596 BLI_assert(r_name_full.size() < MAX_ID_NAME - 2);
597 BLI_strncpy(r_name, r_name_full.c_str(), MAX_ID_NAME - 2);
598 return is_name_modified;
599}
600
602{
604 if (name.is_empty()) {
605 /* Name is empty or not initialized yet, nothing to remove. */
606 return;
607 }
608 const short id_code = GS(id.name);
609
610 UniqueName_Map *name_map_local = get_namemap_for(bmain, id.lib, nullptr, false);
611 if (name_map_local) {
612 name_map_local->remove_full_name(id_code, name);
613 }
614
615 UniqueName_Map *name_map_global = get_global_namemap_for(bmain, nullptr, false);
616 if (name_map_global) {
617 name_map_global->remove_full_name(id_code, name);
618 }
619}
620
622 std::string name;
625 {
627 }
628 friend bool operator==(const Uniqueness_Key &a, const Uniqueness_Key &b)
629 {
630 return a.lib == b.lib && a.name == b.name;
631 }
632};
633
634static bool main_namemap_validate_and_fix(Main &bmain, const bool do_fix)
635{
636 Set<Uniqueness_Key> id_names_libs;
637 Set<ID *> id_validated;
638 bool is_valid = true;
639 ListBase *lb_iter;
640 FOREACH_MAIN_LISTBASE_BEGIN (&bmain, lb_iter) {
641 LISTBASE_FOREACH_MUTABLE (ID *, id_iter, lb_iter) {
642 if (id_validated.contains(id_iter)) {
643 /* Do not re-check an already validated ID. */
644 continue;
645 }
646
647 Uniqueness_Key key = {id_iter->name, id_iter->lib};
648 if (!id_names_libs.add(key)) {
649 is_valid = false;
650 if (do_fix) {
651 CLOG_WARN(&LOG,
652 "ID name '%s' (from library '%s') is found more than once",
653 id_iter->name,
654 id_iter->lib != nullptr ? id_iter->lib->filepath : "<None>");
655 /* NOTE: this may imply moving this ID in its listbase. The logic below will add the ID
656 * to the validated set if it can now be added to `id_names_libs`, and will prevent
657 * further checking (which would fail again, since the new ID name/lib key has already
658 * been added to `id_names_libs`). */
660 *which_libbase(&bmain, GS(id_iter->name)),
661 *id_iter,
662 nullptr,
664 true);
665 key.name = id_iter->name;
666 if (!id_names_libs.add(key)) {
667 /* This is a serious error, very likely a bug, keep it as CLOG_ERROR even when doing
668 * fixes. */
670 "\tID has been renamed to '%s', but it still seems to be already in use",
671 id_iter->name);
672 }
673 else {
674 CLOG_WARN(&LOG, "\tID has been renamed to '%s'", id_iter->name);
675 id_validated.add(id_iter);
676 }
677 }
678 else {
680 "ID name '%s' (from library '%s') is found more than once",
681 id_iter->name,
682 id_iter->lib != nullptr ? id_iter->lib->filepath : "<None>");
683 }
684 }
685
686 UniqueName_Map *name_map = get_namemap_for(bmain, id_iter->lib, id_iter, false);
687 if (name_map == nullptr) {
688 continue;
689 }
690 UniqueName_TypeMap &type_map = name_map->find_by_type(GS(id_iter->name));
691
692 /* Remove full name from the set. */
693 const std::string id_name = BKE_id_name(*id_iter);
694 if (!type_map.full_names.contains(id_name)) {
695 is_valid = false;
696 if (do_fix) {
697 CLOG_WARN(
698 &LOG,
699 "ID name '%s' (from library '%s') exists in current Main, but is not listed in "
700 "the namemap",
701 id_iter->name,
702 id_iter->lib != nullptr ? id_iter->lib->filepath : "<None>");
703 }
704 else {
706 &LOG,
707 "ID name '%s' (from library '%s') exists in current Main, but is not listed in "
708 "the namemap",
709 id_iter->name,
710 id_iter->lib != nullptr ? id_iter->lib->filepath : "<None>");
711 }
712 }
713 }
714 }
716
717 Library *lib = nullptr;
718 UniqueName_Map *name_map = bmain.name_map;
719 do {
720 if (name_map) {
721 int i = 0;
722 for (short idcode = BKE_idtype_idcode_iter_step(&i); idcode != 0;
724 {
725 UniqueName_TypeMap &type_map = name_map->find_by_type(idcode);
726 for (const std::string &id_name : type_map.full_names.keys()) {
727 Uniqueness_Key key = {
728 fmt::format("{}{}", StringRef{reinterpret_cast<char *>(&idcode), 2}, id_name), lib};
729 if (!id_names_libs.contains(key)) {
730 is_valid = false;
731 if (do_fix) {
732 CLOG_WARN(&LOG,
733 "ID name '%s' (from library '%s') is listed in the namemap, but does not "
734 "exists in current Main",
735 key.name.c_str(),
736 lib != nullptr ? lib->filepath : "<None>");
737 }
738 else {
740 "ID name '%s' (from library '%s') is listed in the namemap, but does not "
741 "exists in current Main",
742 key.name.c_str(),
743 lib != nullptr ? lib->filepath : "<None>");
744 }
745 }
746 }
747 }
748 }
749
750 lib = static_cast<Library *>((lib == nullptr) ? bmain.libraries.first : lib->id.next);
751 name_map = (lib != nullptr) ? lib->runtime->name_map : nullptr;
752 } while (lib != nullptr);
753
754 if (is_valid || !do_fix) {
755 return is_valid;
756 }
757
758 /* Clear all existing name-maps. */
760
761 return is_valid;
762}
763
765{
766 const bool is_valid = main_namemap_validate_and_fix(bmain, true);
768 return is_valid;
769}
770
772{
773 return main_namemap_validate_and_fix(bmain, false);
774}
int BKE_idtype_idcode_to_index(short idcode)
Definition idtype.cc:228
short BKE_idtype_idcode_iter_step(int *idtype_index)
Definition idtype.cc:373
IDNewNameResult BKE_id_new_name_validate(Main &bmain, ListBase &lb, ID &id, const char *newname, IDNewNameMode mode, bool do_linked_data)
Definition lib_id.cc:1903
const char * BKE_id_name(const ID &id)
#define FOREACH_MAIN_ID_END
Definition BKE_main.hh:583
ListBase * which_libbase(Main *bmain, short type)
Definition main.cc:902
#define FOREACH_MAIN_LISTBASE_END
Definition BKE_main.hh:564
#define FOREACH_MAIN_LISTBASE_BEGIN(_bmain, _lb)
Definition BKE_main.hh:557
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition BKE_main.hh:577
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
#define LISTBASE_FOREACH_MUTABLE(type, var, list)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
int BLI_str_utf8_invalid_strip(char *str, size_t str_len) ATTR_NONNULL(1)
size_t BLI_string_split_name_number(const char *name, char delim, char *r_name_left, int *r_number) ATTR_NONNULL(1
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
ID and Library types, which are fundamental for SDNA.
#define MAX_ID_NAME
Definition DNA_ID.h:373
Read Guarded memory(de)allocation.
long long int int64_t
unsigned long long int uint64_t
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
int64_t size_in_bytes() const
Definition BLI_map.hh:1019
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
Value & lookup_or_add_as(ForwardKey &&key, ForwardValue &&...value)
Definition BLI_map.hh:605
void remove_contained(const Key &key)
Definition BLI_map.hh:387
bool remove(const Key &key)
Definition BLI_map.hh:368
KeyIterator keys() const &
Definition BLI_map.hh:875
bool contains(const Key &key) const
Definition BLI_map.hh:353
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr int64_t size() const
constexpr const char * c_str() const
void resize(const int64_t new_size_in_bits, const bool value=false)
#define GS(x)
#define printf(...)
int count
#define LOG(level)
Definition log.h:97
void BKE_main_namemap_destroy(UniqueName_Map **r_name_map)
static bool namemap_get_name(Main &bmain, ID &id, std::string &r_name_full, const bool do_unique_in_bmain)
static bool main_namemap_validate_and_fix(Main &bmain, const bool do_fix)
bool BKE_main_namemap_validate_and_fix(Main &bmain)
void BKE_main_namemap_remove_id(Main &bmain, ID &id)
bool BKE_main_namemap_contain_name(Main &bmain, Library *lib, const short id_type, StringRef name)
constexpr int NO_AVAILABLE_NUMBER
static bool id_name_final_build(UniqueName_TypeMap &type_map, const StringRefNull base_name, const int number, std::string &r_name_final)
void BKE_main_namemap_clear(Main &bmain)
bool BKE_main_namemap_get_unique_name(Main &bmain, ID &id, char *r_name)
bool BKE_main_global_namemap_get_unique_name(Main &bmain, ID &id, char *r_name)
static UniqueName_Map * get_namemap_for(Main &bmain, Library *lib, ID *ignore_id, const bool ensure_created)
constexpr int MAX_NUMBER
static UniqueName_Map * get_global_namemap_for(Main &bmain, ID *ignore_id, const bool ensure_created)
bool BKE_main_namemap_validate(Main &bmain)
bool BKE_main_global_namemap_contain_name(Main &bmain, const short id_type, StringRef name)
void max_inplace(T &a, const T &b)
uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:233
const char * name
Definition DNA_ID.h:414
struct Library * lib
Definition DNA_ID.h:420
char name[258]
Definition DNA_ID.h:432
void * first
UniqueName_Map * name_map_global
Definition BKE_main.hh:341
UniqueName_Map * name_map
Definition BKE_main.hh:335
ListBase libraries
Definition BKE_main.hh:279
std::shared_ptr< blender::VectorSet< Main * > > split_mains
Definition BKE_main.hh:166
void add_name(UniqueName_TypeMap &type_map, StringRef name_full, StringRef name_base, const int number)
std::array< UniqueName_TypeMap, INDEX_ID_MAX > type_maps
void populate(Main &bmain, Library *library, ID *ignore_id)
UniqueName_Map(const bool is_global)
void remove_full_name(const short id_type, blender::StringRef name_full)
void remove_full_name(UniqueName_TypeMap &type_map, blender::StringRef name_full)
UniqueName_TypeMap & find_by_type(const short id_type)
void add_name(const short id_type, StringRef name_full, StringRef name_base, const int number)
Map< std::string, int > full_names
Map< std::string, std::unique_ptr< UniqueName_Value > > base_name_to_num_suffix
void mark_unused(const int number)
void mark_used(const int number)
std::optional< Map< int, int > > numbers_multi_usages
std::optional< int > max_value_in_use
static constexpr int max_exact_tracking
uint64_t hash() const
std::string name
friend bool operator==(const Uniqueness_Key &a, const Uniqueness_Key &b)
i
Definition text_draw.cc:230
static DynamicLibrary lib