Blender V5.3
readfile.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "fmt/core.h"
10
11#include <algorithm>
12#include <cerrno>
13#include <climits>
14#include <cstdarg> /* for va_start/end. */
15#include <cstddef> /* for offsetof. */
16#include <cstdlib> /* for atoi. */
17#include <cstring>
18#include <ctime> /* for gmtime. */
19#include <deque>
20#include <fcntl.h> /* for open flags (O_BINARY, O_RDONLY). */
21#include <queue>
22
23#ifndef WIN32
24# include <unistd.h> /* for read close */
25#else
26# include "BLI_winstuff.hh"
27# include "winsock2.h"
28# include <io.h> /* for open close read */
29#endif
30
31#include <fmt/format.h>
32
33#include "CLG_log.h"
34
35/* allow readfile to use deprecated functionality */
36#define DNA_DEPRECATED_ALLOW
37
38#include "DNA_asset_types.h"
42#include "DNA_genfile.h"
43#include "DNA_key_types.h"
44#include "DNA_layer_types.h"
45#include "DNA_node_types.h"
47#include "DNA_screen_types.h"
48#include "DNA_sdna_types.h"
49#include "DNA_userdef_types.h"
51
53#include "MEM_guardedalloc.h"
54#include "MEM_safe_multiply.h"
55
56#include "BLI_endian_defines.hh"
57#include "BLI_fileops.hh"
58#include "BLI_ghash.hh"
59#include "BLI_listbase.hh"
60#include "BLI_map.hh"
61#include "BLI_memarena.hh"
62#include "BLI_memory_utils.hh"
63#include "BLI_set.hh"
64#include "BLI_string.hh"
65#include "BLI_string_ref.hh"
66#include "BLI_string_utf8.hh"
67#include "BLI_string_utils.hh"
68#include "BLI_threads.hh"
69#include "BLI_time.hh"
70#include "BLI_utildefines.hh"
71
72#include "BLT_translation.hh"
73
74#include "BKE_anim_data.hh"
75#include "BKE_animsys.hh"
76#include "BKE_asset.hh"
77#include "BKE_blender_version.h"
78#include "BKE_collection.hh"
79#include "BKE_global.hh" /* for G */
80#include "BKE_idprop.hh"
81#include "BKE_idtype.hh"
82#include "BKE_layer.hh"
83#include "BKE_lib_id.hh"
84#include "BKE_lib_override.hh"
85#include "BKE_lib_query.hh"
86#include "BKE_lib_remap.hh"
87#include "BKE_library.hh"
88#include "BKE_main.hh" /* for Main */
89#include "BKE_main_idmap.hh"
91#include "BKE_main_namemap.hh"
92#include "BKE_material.hh"
93#include "BKE_mesh.hh"
94#include "BKE_modifier.hh"
95#include "BKE_nla.hh"
96#include "BKE_node.hh" /* for tree type defines */
98#include "BKE_object.hh"
99#include "BKE_packedFile.hh"
100#include "BKE_preferences.h"
101#include "BKE_report.hh"
102#include "BKE_scene.hh"
103#include "BKE_screen.hh"
104#include "BKE_undo_system.hh"
105#include "BKE_workspace.hh"
106
107#include "DRW_engine.hh"
108
109#include "DEG_depsgraph.hh"
110
111#include "BLO_blend_validate.hh"
113#include "BLO_read_write.hh"
114#include "BLO_readfile.hh"
115#include "BLO_undofile.hh"
116
117#include "SEQ_iterator.hh"
118#include "SEQ_modifier.hh"
119#include "SEQ_sequencer.hh"
120#include "SEQ_utils.hh"
121
122#include "IMB_colormanagement.hh"
123
124#include "readfile.hh"
125#include "versioning_common.hh"
126
127namespace blender {
128
129/* Make preferences read-only. */
130#define U (*((const UserDef *)&U))
131
181
189#define USE_BHEAD_READ_ON_DEMAND
190
191static CLG_LogRef LOG = {"blend.readfile"};
192static CLG_LogRef LOG_UNDO = {"undo"};
193
194#if ENDIAN_ORDER == B_ENDIAN
195# warning "Support for Big Endian endianness is deprecated and will be removed in Blender 5.0"
196#endif
197
198/* local prototypes */
199static void read_libraries(FileData *basefd);
200static void read_libraries_free_placeholders(FileData *basefd);
201static void *read_struct(FileData *fd,
202 BHead *bh,
203 const char *blockname,
204 const int id_type_index,
205 int64_t *r_alloc_len = nullptr);
206static void *read_struct_and_validate_size(FileData *fd,
207 BHead *bh,
208 const char *blockname,
209 const int id_type_index,
210 const int64_t min_size);
211static BHead *find_bhead_from_code_name(FileData *fd, const short idcode, const char *name);
212
213struct BHeadN {
215#ifdef USE_BHEAD_READ_ON_DEMAND
217 off64_t file_offset;
220#endif
223};
224
225#define BHEADN_FROM_BHEAD(bh) ((BHeadN *)POINTER_OFFSET(bh, -int(offsetof(BHeadN, bhead))))
226
231#define BHEAD_USE_READ_ON_DEMAND(bhead) ((bhead)->code == BLO_CODE_DATA)
232
233/* -------------------------------------------------------------------- */
236
238 const eReportType type,
239 const char *format,
240 ...)
241{
242 char fixed_buf[1024]; /* should be long enough */
243
244 va_list args;
245
246 va_start(args, format);
247 vsnprintf(fixed_buf, sizeof(fixed_buf), format, args);
248 va_end(args);
249
250 fixed_buf[sizeof(fixed_buf) - 1] = '\0';
251
252 BKE_report(reports->reports, type, fixed_buf);
253
254 if (G.background == 0) {
255 BKE_report_log(type, fixed_buf, &LOG);
256 }
257}
258
259/* for reporting linking messages */
260static const char *library_parent_filepath(Library *lib)
261{
262 return lib->runtime->parent ? lib->runtime->parent->runtime->filepath_abs : "<direct>";
263}
264
266
267/* -------------------------------------------------------------------- */
270
272 void *newp;
273
281
283 int nr;
284};
285
289
291{
292 return MEM_new<OldNewMap>(__func__);
293}
294
300 OldNewMap *onm, const void *oldaddr, void *newaddr, const int nr, const int64_t alloc_len = 0)
301{
302 if (oldaddr == nullptr || newaddr == nullptr) {
303 return false;
304 }
305
306 return onm->map.add_overwrite(oldaddr, NewAddress{newaddr, alloc_len, nr});
307}
308
309static void oldnewmap_lib_insert(FileData *fd, const void *oldaddr, ID *newaddr, const int id_code)
310{
311 oldnewmap_insert(fd->libmap, oldaddr, newaddr, id_code);
312}
313
315 const void *oldaddr,
316 void *newaddr,
317 const int nr)
318{
319 oldnewmap_insert(onm, oldaddr, newaddr, nr);
320}
321
323 const void *addr,
324 const bool increase_users,
325 int64_t *r_alloc_len = nullptr)
326{
327 NewAddress *entry = onm->map.lookup_ptr(addr);
328 if (entry == nullptr) {
329 return nullptr;
330 }
331 if (increase_users) {
332 entry->nr++;
333 }
334 if (r_alloc_len) {
335 *r_alloc_len = entry->alloc_len;
336 }
337 return entry->newp;
338}
339
340/* for libdata, NewAddress.nr has ID code, no increment */
341static void *oldnewmap_liblookup(OldNewMap *onm, const void *addr, const bool is_linked_only)
342{
343 if (addr == nullptr) {
344 return nullptr;
345 }
346
347 ID *id = static_cast<ID *>(oldnewmap_lookup_and_inc(onm, addr, false));
348 if (id == nullptr) {
349 return nullptr;
350 }
351 if (!is_linked_only || ID_IS_LINKED(id)) {
352 return id;
353 }
354 return nullptr;
355}
356
357static void oldnewmap_clear(OldNewMap *onm)
358{
359 /* Free unused data. */
360 for (NewAddress &new_addr : onm->map.values()) {
361 if (new_addr.nr == 0) {
362 MEM_delete_void(new_addr.newp);
363 }
364 }
365 onm->map.clear();
366}
367
368static void oldnewmap_free(OldNewMap *onm)
369{
370 MEM_delete(onm);
371}
372
374
375/* -------------------------------------------------------------------- */
378
379static void add_main_to_main(Main *mainvar, Main *from)
380{
381 if (from->is_read_invalid) {
382 mainvar->is_read_invalid = true;
383 }
384
385 MainListsArray lbarray = BKE_main_lists_get(*mainvar);
386 MainListsArray fromarray = BKE_main_lists_get(*from);
387 int a = fromarray.size();
388 while (a--) {
389 BLI_movelisttolist(lbarray[a], fromarray[a]);
390 }
391}
392
393void blo_join_main(Main *bmain)
394{
395 BLI_assert(bmain->split_mains);
396 /* For now, we could relax this requirement in the future if needed. */
397 BLI_assert((*bmain->split_mains)[0] == bmain);
398
399 if (bmain->split_mains->size() == 1) {
400 bmain->split_mains.reset();
401 return;
402 }
403
404 if (bmain->id_map != nullptr) {
405 /* Cannot keep this since we add some IDs from joined mains. */
407 bmain->id_map = nullptr;
408 }
409 /* Will no longer be valid after joining. */
411
412 for (Main *tojoin : *bmain->split_mains) {
413 if (tojoin == bmain) {
414 continue;
415 }
417 tojoin->is_asset_edit_file);
418 add_main_to_main(bmain, tojoin);
419 tojoin->split_mains.reset();
420 BKE_main_free(tojoin);
421 }
422
423 BLI_assert(bmain->split_mains.use_count() == 1);
424 bmain->split_mains.reset();
425}
426
427static void split_libdata(ListBaseT<ID> *lb_src,
428 Vector<Main *> &lib_main_array,
429 const bool do_split_packed_ids)
430{
431 for (ID *id = lb_src->first(), *idnext; id; id = idnext) {
432 idnext = static_cast<ID *>(id->next);
433
434 if (id->lib && (do_split_packed_ids || (id->lib->flag & LIBRARY_FLAG_IS_ARCHIVE) == 0)) {
435 if (uint(id->lib->runtime->temp_index) < lib_main_array.size()) {
436 Main *mainvar = lib_main_array[id->lib->runtime->temp_index];
437 BLI_assert(mainvar->curlib == id->lib);
438 ListBaseT<ID> *lb_dst = which_libbase(mainvar, id->id_type());
439 BLI_remlink(lb_src, id);
440 BLI_addtail(lb_dst, id);
441 }
442 else {
443 CLOG_ERROR(&LOG, "Invalid library for '%s'", id->name);
444 }
445 }
446 }
447}
448
449void blo_split_main(Main *bmain, const bool do_split_packed_ids)
450{
451 BLI_assert(!bmain->split_mains);
452 bmain->split_mains = std::make_shared<VectorSet<Main *>>();
453 bmain->split_mains->add_new(bmain);
454
455 if (bmain->libraries.is_empty()) {
456 return;
457 }
458
459 if (bmain->id_map != nullptr) {
460 /* Cannot keep this since we remove some IDs from given main. */
462 bmain->id_map = nullptr;
463 }
464
465 /* Will no longer be valid after splitting. */
467
468 /* (Library.temp_index -> Main), lookup table */
469 Vector<Main *> lib_main_array;
470
471 int i = 0;
472 int lib_index = 0;
473 for (Library *lib = bmain->libraries.first(); lib;
474 lib = static_cast<Library *>(lib->id.next), i++)
475 {
476 if (!do_split_packed_ids && (lib->flag & LIBRARY_FLAG_IS_ARCHIVE) != 0) {
477 continue;
478 }
479 Main *libmain = BKE_main_new();
480 libmain->curlib = lib;
481 libmain->versionfile = lib->runtime->versionfile;
482 libmain->subversionfile = lib->runtime->subversionfile;
485 libmain->is_asset_edit_file = (lib->runtime->tag & LIBRARY_IS_ASSET_EDIT_FILE) != 0;
486 libmain->colorspace = lib->runtime->colorspace;
487 bmain->split_mains->add_new(libmain);
488 libmain->split_mains = bmain->split_mains;
489 lib->runtime->temp_index = lib_index;
490 lib_main_array.append(libmain);
491 lib_index++;
492 }
493
494 MainListsArray lbarray = BKE_main_lists_get(*bmain);
495 i = lbarray.size();
496 while (i--) {
497 ID *id = lbarray[i]->first();
498 if (id == nullptr || id->id_type() == ID_LI) {
499 /* No ID_LI data-block should ever be linked anyway, but just in case, better be explicit. */
500 continue;
501 }
502 split_libdata(lbarray[i], lib_main_array, do_split_packed_ids);
503 }
504}
505
507{
508 BHead *bhead;
509
510 for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
511 if (bhead->code == BLO_CODE_GLOB) {
513 fd, bhead, "Data from Global block", INDEX_ID_NULL, sizeof(FileGlobal)));
514 if (fg) {
515 if (main->versionfile != fd->fileversion) {
516 /* `versionfile` remains unset when linking from a new library (`main` has then just be
517 * created by `blo_find_main`). */
518 BLI_assert(main->versionfile == 0);
519 main->versionfile = short(fd->fileversion);
520 }
521 main->subversionfile = fg->subversion;
522 main->minversionfile = fg->minversion;
523 main->minsubversionfile = fg->minsubversion;
524 main->has_forward_compatibility_issues = !MAIN_VERSION_FILE_OLDER_OR_EQUAL(
526 main->is_asset_edit_file = (fg->fileflags & G_FILE_ASSET_EDIT_FILE) != 0;
527 STRNCPY(main->colorspace.scene_linear_name, fg->colorspace_scene_linear_name);
528 main->colorspace.scene_linear_to_xyz = float3x3(fg->colorspace_scene_linear_to_xyz);
529 MEM_delete(fg);
530 }
531 else if (bhead->code == BLO_CODE_ENDB) {
532 break;
533 }
534 }
535 }
536 if (main->curlib) {
537 main->curlib->runtime->versionfile = main->versionfile;
538 main->curlib->runtime->subversionfile = main->subversionfile;
540 main->curlib->runtime->tag, main->is_asset_edit_file, LIBRARY_IS_ASSET_EDIT_FILE);
541 main->curlib->runtime->colorspace = main->colorspace;
542 }
543}
544
545static bool blo_bhead_is_id(const BHead *bhead)
546{
547 /* BHead codes are four bytes (like 'ENDB', 'TEST', etc.), but if the two most-significant bytes
548 * are zero, the values actually indicate an ID type. */
549 return bhead->code <= 0xFFFF;
550}
551
552static bool blo_bhead_is_id_valid_type(const BHead *bhead)
553{
554 if (!blo_bhead_is_id(bhead)) {
555 return false;
556 }
557
558 const short id_type_code = bhead->code & 0xFFFF;
559 return BKE_idtype_idcode_is_valid(id_type_code);
560}
561
563{
564 /* dummy values */
565 bool is_link = false;
566 int code_prev = BLO_CODE_ENDB;
567
568 fd->bhead_idname_map.emplace();
569 for (BHead *bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
570 if (code_prev != bhead->code) {
571 code_prev = bhead->code;
572 is_link = blo_bhead_is_id_valid_type(bhead) ?
573 BKE_idtype_idcode_is_linkable(short(code_prev)) :
574 false;
575 }
576
577 if (is_link) {
578 /* #idname may be null in case the ID name of the given BHead is detected as invalid (e.g.
579 * because it comes from a future version of Blender allowing for longer ID names). These
580 * 'invalid-named IDs' are skipped here, which will e.g. prevent them from being linked. */
581 const char *idname = blo_bhead_id_name(fd, bhead);
582 if (idname) {
583 fd->bhead_idname_map->add(idname, bhead);
584 }
585 }
586 }
587}
588
589void blo_readfile_invalidate(FileData *fd, Main *bmain, const char *message)
590{
591 /* Tag given `bmain`, and 'root 'local' main one (in case given one is a library one) as invalid.
592 */
593 bmain->is_read_invalid = true;
594 if (bmain->split_mains) {
595 (*bmain->split_mains)[0]->is_read_invalid = true;
596 }
597
599 RPT_ERROR,
600 "A critical error happened (the blend file is likely corrupted): %s",
601 message);
602}
603
605
606/* -------------------------------------------------------------------- */
609
611{
612 BHeadN *new_bhead = nullptr;
613
614 if (fd) {
615 if (!fd->is_eof) {
616 std::optional<BHead> bhead_opt = BLO_readfile_read_bhead(fd->file,
618 BHead *bhead = nullptr;
619 if (!bhead_opt.has_value()) {
620 fd->is_eof = true;
621 }
622 else if (bhead_opt->len < 0) {
623 /* Make sure people are not trying to parse bad blend files. */
624 fd->is_eof = true;
625 }
626 else {
627 bhead = &bhead_opt.value();
628 }
629
630 /* bhead now contains the (converted) bhead structure. Now read
631 * the associated data and put everything in a BHeadN (creative naming !)
632 */
633 if (fd->is_eof) {
634 /* pass */
635 }
636#ifdef USE_BHEAD_READ_ON_DEMAND
637 else if (fd->file->seek != nullptr && BHEAD_USE_READ_ON_DEMAND(bhead)) {
638 /* Delay reading bhead content. */
639 new_bhead = MEM_new_uninitialized<BHeadN>("new_bhead");
640 if (new_bhead) {
641 new_bhead->next = new_bhead->prev = nullptr;
642 new_bhead->file_offset = fd->file->offset;
643 new_bhead->has_data = false;
644 new_bhead->is_memchunk_identical = false;
645 new_bhead->bhead = *bhead;
646 const off64_t seek_new = fd->file->seek(fd->file, bhead->len, SEEK_CUR);
647 if (seek_new == -1) [[unlikely]] {
648 fd->is_eof = true;
649 MEM_delete(new_bhead);
650 new_bhead = nullptr;
651 }
652 else {
653 BLI_assert(fd->file->offset == seek_new);
654 }
655 }
656 else {
657 fd->is_eof = true;
658 }
659 }
660#endif
661 else {
662 new_bhead = static_cast<BHeadN *>(
663 MEM_new_uninitialized(sizeof(BHeadN) + size_t(bhead->len), "new_bhead"));
664 if (new_bhead) {
665 new_bhead->next = new_bhead->prev = nullptr;
666#ifdef USE_BHEAD_READ_ON_DEMAND
667 new_bhead->file_offset = 0; /* don't seek. */
668 new_bhead->has_data = true;
669#endif
670 new_bhead->is_memchunk_identical = false;
671 new_bhead->bhead = *bhead;
672
673 const int64_t readsize = fd->file->read(fd->file, new_bhead + 1, size_t(bhead->len));
674
675 if (readsize != bhead->len) [[unlikely]] {
676 fd->is_eof = true;
677 MEM_delete(new_bhead);
678 new_bhead = nullptr;
679 }
680 else {
681 if (fd->flags & FD_FLAGS_IS_MEMFILE) {
682 new_bhead->is_memchunk_identical =
683 (reinterpret_cast<UndoReader *>(fd->file))->memchunk_identical;
684 }
685 }
686 }
687 else {
688 fd->is_eof = true;
689 }
690 }
691 }
692 }
693
694 /* We've read a new block. Now add it to the list
695 * of blocks.
696 */
697 if (new_bhead) {
698 BLI_addtail(&fd->bhead_list, new_bhead);
699 }
700
701 return new_bhead;
702}
703
705{
706 BHeadN *new_bhead;
707 BHead *bhead = nullptr;
708
709 /* Rewind the file
710 * Read in a new block if necessary
711 */
712 new_bhead = fd->bhead_list.first();
713 if (new_bhead == nullptr) {
714 new_bhead = get_bhead(fd);
715 }
716
717 if (new_bhead) {
718 bhead = &new_bhead->bhead;
719 }
720
721 return bhead;
722}
723
724BHead *blo_bhead_prev(FileData * /*fd*/, BHead *thisblock)
725{
726 BHeadN *bheadn = BHEADN_FROM_BHEAD(thisblock);
727 BHeadN *prev = bheadn->prev;
728
729 return (prev) ? &prev->bhead : nullptr;
730}
731
733{
734 BHeadN *new_bhead = nullptr;
735 BHead *bhead = nullptr;
736
737 if (thisblock) {
738 /* bhead is actually a sub part of BHeadN
739 * We calculate the BHeadN pointer from the BHead pointer below */
740 new_bhead = BHEADN_FROM_BHEAD(thisblock);
741
742 /* get the next BHeadN. If it doesn't exist we read in the next one */
743 new_bhead = new_bhead->next;
744 if (new_bhead == nullptr) {
745 new_bhead = get_bhead(fd);
746 }
747 }
748
749 if (new_bhead) {
750 /* here we do the reverse:
751 * go from the BHeadN pointer to the BHead pointer */
752 bhead = &new_bhead->bhead;
753 }
754
755 return bhead;
756}
757
758#ifdef USE_BHEAD_READ_ON_DEMAND
759static bool blo_bhead_read_data(FileData *fd, BHead *thisblock, void *buf)
760{
761 bool success = true;
762 BHeadN *new_bhead = BHEADN_FROM_BHEAD(thisblock);
763 BLI_assert(new_bhead->has_data == false && new_bhead->file_offset != 0);
764 off64_t offset_backup = fd->file->offset;
765 if (fd->file->seek(fd->file, new_bhead->file_offset, SEEK_SET) == -1) [[unlikely]] {
766 success = false;
767 }
768 else {
769 if (fd->file->read(fd->file, buf, size_t(new_bhead->bhead.len)) != new_bhead->bhead.len)
770 [[unlikely]]
771 {
772 success = false;
773 }
774 else {
775 if (fd->flags & FD_FLAGS_IS_MEMFILE) {
776 new_bhead->is_memchunk_identical =
777 (reinterpret_cast<UndoReader *>(fd->file))->memchunk_identical;
778 }
779 }
780 }
781 if (fd->file->seek(fd->file, offset_backup, SEEK_SET) == -1) {
782 success = false;
783 }
784 return success;
785}
786
787static BHead *blo_bhead_read_full(FileData *fd, BHead *thisblock)
788{
789 BHeadN *new_bhead = BHEADN_FROM_BHEAD(thisblock);
790 BHeadN *new_bhead_data = static_cast<BHeadN *>(
791 MEM_new_uninitialized(sizeof(BHeadN) + new_bhead->bhead.len, "new_bhead"));
792 new_bhead_data->bhead = new_bhead->bhead;
793 new_bhead_data->file_offset = new_bhead->file_offset;
794 new_bhead_data->has_data = true;
795 new_bhead_data->is_memchunk_identical = false;
796 if (!blo_bhead_read_data(fd, thisblock, new_bhead_data + 1)) {
797 MEM_delete(new_bhead_data);
798 return nullptr;
799 }
800 return &new_bhead_data->bhead;
801}
802#endif /* USE_BHEAD_READ_ON_DEMAND */
803
808template<typename T> static const T *blo_bhead_data_at(const BHead *bhead, const int offset)
809{
810 if (offset < 0 || int64_t(offset) + int64_t(sizeof(T)) > bhead->len) [[unlikely]] {
811 return nullptr;
812 }
813 return reinterpret_cast<const T *>(POINTER_OFFSET(bhead, sizeof(*bhead) + offset));
814}
815
817static const char *blo_bhead_string_at(const BHead *bhead, const int offset, const size_t maxsize)
818{
819 const char *str = blo_bhead_data_at<char>(bhead, offset);
820 if (str == nullptr) [[unlikely]] {
821 return nullptr;
822 }
823 /* Ensure the string is null terminated. */
824 const size_t size = std::min(maxsize, size_t(bhead->len - offset));
825 return std::memchr(str, '\0', size) ? str : nullptr;
826}
827
828const char *blo_bhead_id_name(FileData *fd, const BHead *bhead)
829{
831 const char *id_name = blo_bhead_string_at(bhead, fd->id_name_offset, MAX_ID_NAME);
832 /* Names must at least contain the two character ID code, so code using `id_name + 2`
833 * always gets a null terminated string. */
834 if (id_name && id_name[0] != '\0' && id_name[1] != '\0') {
835 return id_name;
836 }
837
838 /* ID name longer than MAX_ID_NAME - 1, or otherwise corrupted. */
840 return nullptr;
841}
842
843short blo_bhead_id_flag(const FileData *fd, const BHead *bhead)
844{
846 const short *flag = blo_bhead_data_at<short>(bhead, fd->id_flag_offset);
847 return flag ? *flag : 0;
848}
849
851{
853 AssetMetaData *const *asset_data = blo_bhead_data_at<AssetMetaData *>(bhead,
855 return asset_data ? *asset_data : nullptr;
856}
857
858static const IDHash *blo_bhead_id_deep_hash(const FileData *fd, const BHead *bhead)
859{
861 const short flag = blo_bhead_id_flag(fd, bhead);
862 if (!(flag & ID_FLAG_LINKED_AND_PACKED)) {
863 return nullptr;
864 }
866}
867
868const char *blo_bhead_library_filepath(const FileData *fd, const BHead *bhead)
869{
870 BLI_assert(blo_bhead_is_id(bhead) && (bhead->code & 0xFFFF) == ID_LI);
872}
873
875{
876 BLI_assert(blo_bhead_is_id(bhead) && (bhead->code & 0xFFFF) == ID_LI);
878 return flag ? LibraryFlag(*flag) : LibraryFlag(0);
879}
880
882{
884 if (std::holds_alternative<BlenderHeaderInvalid>(header_variant)) {
885 return;
886 }
887 if (std::holds_alternative<BlenderHeaderUnknown>(header_variant)) {
889 return;
890 }
891 const BlenderHeader &header = std::get<BlenderHeader>(header_variant);
892 fd->flags |= FD_FLAGS_FILE_OK;
893 if (header.pointer_size == 4) {
895 }
896 if (header.pointer_size != sizeof(void *)) {
898 }
899 if (header.endian != ENDIAN_ORDER) {
901 }
902 fd->fileversion = header.file_version;
903 fd->blender_header = header;
904}
905
909static bool read_file_dna(FileData *fd, const char **r_error_message)
910{
911 BHead *bhead;
912 int subversion = 0;
913
914 for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
915 if (bhead->code == BLO_CODE_GLOB) {
916 /* Before this, the subversion didn't exist in 'FileGlobal' so the subversion
917 * value isn't accessible for the purpose of DNA versioning in this case. */
918 if (fd->fileversion <= 242) {
919 continue;
920 }
921 /* We can't use read_global because this needs 'DNA1' to be decoded,
922 * however the first 4 chars are _always_ the subversion. */
923 BLI_STATIC_ASSERT(offsetof(FileGlobal, subvstr) == 0, "Must be first: subvstr")
924 if (bhead->len < int64_t(sizeof(FileGlobal::subvstr))) [[unlikely]] {
925 continue;
926 }
927 const FileGlobal *fg = reinterpret_cast<const FileGlobal *>(&bhead[1]);
928 char num[5];
929 memcpy(num, fg->subvstr, 4);
930 num[4] = 0;
931 subversion = atoi(num);
932 }
933 else if (bhead->code == BLO_CODE_DNA1) {
935 const bool do_alias = false; /* Postpone until after #blo_do_versions_dna runs. */
936 fd->filesdna = DNA_sdna_from_data(&bhead[1], bhead->len, true, do_alias, r_error_message);
937 if (fd->filesdna) {
938 blo_do_versions_dna(fd->filesdna.get(), fd->fileversion, subversion);
939 /* Allow aliased lookups (must be after version patching DNA). */
941
944 fd->filesdna.get(), fd->memsdna, fd->compflags);
945 /* used to retrieve ID names from (bhead+1) */
947 fd->filesdna.get(), "ID", "char", "name[]");
948 BLI_assert(fd->id_name_offset != -1);
950 fd->filesdna.get(), "ID", "AssetMetaData", "*asset_data");
952 fd->filesdna.get(), "ID", "short", "flag");
954 fd->filesdna.get(), "ID", "IDHash", "deep_hash");
955
957 fd->filesdna.get(), "Library", "char", "filepath[]");
959 fd->filesdna.get(), "Library", "ushort", "flag");
960
961 fd->filesubversion = subversion;
962
963 return true;
964 }
965
966 return false;
967 }
968 else if (bhead->code == BLO_CODE_ENDB) {
969 break;
970 }
971 }
972
973 *r_error_message = "Missing DNA block";
974 return false;
975}
976
978{
979 BHead *bhead;
980 int *blend_thumb = nullptr;
981
982 for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
983 if (bhead->code == BLO_CODE_TEST) {
985 int *data = reinterpret_cast<int *>(bhead + 1);
986
987 if (bhead->len < sizeof(int[2])) {
988 break;
989 }
990
991 const int width = data[0];
992 const int height = data[1];
993 if (!BLEN_THUMB_MEMSIZE_IS_VALID(width, height)) {
994 break;
995 }
996 if (bhead->len < BLEN_THUMB_MEMSIZE_FILE(width, height)) {
997 break;
998 }
999
1000 blend_thumb = data;
1001 break;
1002 }
1003 if (bhead->code != BLO_CODE_REND) {
1004 /* Thumbnail is stored in TEST immediately after first REND... */
1005 break;
1006 }
1007 }
1008
1009 return blend_thumb;
1010}
1011
1024{
1025 ListBaseT<ID> *lb_iter;
1026 /* Using a set is needed, to avoid renaming names when there is no collision, and deal with IDs
1027 * being moved around in their list when renamed. A simple set is enough, since here only local
1028 * IDs are processed. */
1029 Set<StringRef> used_names;
1030 Set<ID *> processed_ids;
1031
1032 FOREACH_MAIN_LISTBASE_BEGIN (bmain, lb_iter) {
1033 for (ID &id_iter : lb_iter->items_mutable()) {
1034 if (processed_ids.contains(&id_iter)) {
1035 continue;
1036 }
1037 processed_ids.add_new(&id_iter);
1038 /* Linked IDs can be fully ignored here, 'long names' IDs cannot be linked in any way. */
1039 if (ID_IS_LINKED(&id_iter)) {
1040 continue;
1041 }
1042 if (!used_names.contains(id_iter.name)) {
1043 used_names.add_new(id_iter.name);
1044 continue;
1045 }
1046
1048 *bmain, *lb_iter, id_iter, nullptr, IDNewNameMode::RenameExistingNever, false);
1049 BLI_assert(!used_names.contains(id_iter.name));
1050 used_names.add_new(id_iter.name);
1051 CLOG_DEBUG(&LOG, "ID name has been de-duplicated to '%s'", id_iter.name);
1052 }
1053 }
1055}
1056
1066{
1067 /* NOTE: A large part of this code follows a similar logic to
1068 * #foreach_action_slot_use_with_references.
1069 *
1070 * However, no slot identifier should ever be skipped here, even if it is not in use in any way,
1071 * since it is critical to remove all non-null terminated strings.
1072 */
1073
1074 ID *id_iter;
1075 FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
1076 switch (id_iter->id_type()) {
1077 case ID_AC: {
1078 bool has_truncated_slot_identifier = false;
1079 bAction *act = reinterpret_cast<bAction *>(id_iter);
1080 for (int i = 0; i < act->slot_array_num; i++) {
1082 CLOG_DEBUG(&LOG,
1083 "Truncated too long action slot name to '%s'",
1084 act->slot_array[i]->identifier);
1085 has_truncated_slot_identifier = true;
1086 }
1087 }
1088 if (!has_truncated_slot_identifier) {
1089 continue;
1090 }
1091
1092 /* If there are truncated slots identifiers, ensuring their uniqueness must happen in a
1093 * second loop, to avoid e.g. an attempt to read a slot identifier that has not yet been
1094 * truncated. */
1095 for (int i = 0; i < act->slot_array_num; i++) {
1097 [&](const StringRef name) -> bool {
1098 for (int j = 0; j < act->slot_array_num; j++) {
1099 if (i == j) {
1100 continue;
1101 }
1102 if (act->slot_array[j]->identifier == name) {
1103 return true;
1104 }
1105 }
1106 return false;
1107 },
1108 "",
1109 '.',
1110 act->slot_array[i]->identifier,
1111 sizeof(act->slot_array[i]->identifier));
1112 }
1113 break;
1114 }
1115 case ID_OB: {
1116 auto visit_constraint = [](const bConstraint &constraint) -> bool {
1117 if (constraint.type != CONSTRAINT_TYPE_ACTION) {
1118 return true;
1119 }
1120 bActionConstraint *constraint_data = static_cast<bActionConstraint *>(constraint.data);
1122 CLOG_DEBUG(&LOG,
1123 "Truncated too long bActionConstraint.last_slot_identifier to '%s'",
1124 constraint_data->last_slot_identifier);
1125 }
1126 return true;
1127 };
1128
1129 Object *object = reinterpret_cast<Object *>(id_iter);
1130 for (bConstraint &con : object->constraints) {
1131 visit_constraint(con);
1132 }
1133 if (object->pose) {
1134 for (bPoseChannel &pchan : object->pose->chanbase) {
1135 for (bConstraint &con : pchan.constraints) {
1136 visit_constraint(con);
1137 }
1138 }
1139 }
1140 }
1142 default: {
1143 AnimData *anim_data = BKE_animdata_from_id(id_iter);
1144 if (anim_data) {
1146 CLOG_DEBUG(&LOG,
1147 "Truncated too long AnimData.last_slot_identifier to '%s'",
1148 anim_data->last_slot_identifier);
1149 }
1151 CLOG_DEBUG(&LOG,
1152 "Truncated too long AnimData.tmp_last_slot_identifier to '%s'",
1153 anim_data->tmp_last_slot_identifier);
1154 }
1155
1156 bke::nla::foreach_strip_adt(*anim_data, [&](NlaStrip *strip) -> bool {
1158 CLOG_DEBUG(&LOG,
1159 "Truncated too long NlaStrip.last_slot_identifier to '%s'",
1160 strip->last_slot_identifier);
1161 }
1162
1163 return true;
1164 });
1165 }
1166 }
1167 }
1169 }
1170}
1171
1173
1174/* -------------------------------------------------------------------- */
1177
1179{
1180 BLI_assert(reports != nullptr);
1181
1182 FileData *fd = MEM_new<FileData>(__func__);
1183
1185
1186 fd->datamap = oldnewmap_new();
1187 fd->globmap = oldnewmap_new();
1188 fd->libmap = oldnewmap_new();
1189 fd->id_by_deep_hash = std::make_shared<Map<IDHash, ID *>>();
1190
1191 fd->reports = reports;
1192
1193 return fd;
1194}
1195
1205{
1206 BLI_assert(fd->filesdna != nullptr);
1207 for (BHead *bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
1208 if (bhead->code != BLO_CODE_GLOB) {
1209 continue;
1210 }
1211
1213 fd, bhead, "Data from Global block", INDEX_ID_NULL, sizeof(FileGlobal)));
1214 if (fg == nullptr) [[unlikely]] {
1215 BKE_reportf(reports, RPT_ERROR, "Blend file '%s' is corrupt, unable to read", fd->relabase);
1216 return true;
1217 }
1218 if ((fg->minversion > BLENDER_FILE_VERSION) ||
1220 {
1221 char writer_ver_str[16];
1222 char min_reader_ver_str[16];
1223 if (fd->fileversion == fg->minversion) {
1225 writer_ver_str, sizeof(writer_ver_str), short(fd->fileversion), fg->subversion);
1227 min_reader_ver_str, sizeof(min_reader_ver_str), fg->minversion, fg->minsubversion);
1228 }
1229 else {
1231 writer_ver_str, sizeof(writer_ver_str), short(fd->fileversion), -1);
1233 min_reader_ver_str, sizeof(min_reader_ver_str), fg->minversion, -1);
1234 }
1235 BKE_reportf(reports,
1236 RPT_ERROR,
1237 "The file was saved by a newer version, open it with Blender %s or later",
1238 min_reader_ver_str);
1239 CLOG_WARN(&LOG,
1240 "%s: File saved by a newer version of Blender (%s), Blender %s or later is "
1241 "needed to open it.",
1242 fd->relabase,
1243 writer_ver_str,
1244 min_reader_ver_str);
1245 MEM_delete(fg);
1246 return true;
1247 }
1248 MEM_delete(fg);
1249 return false;
1250 }
1251 return false;
1252}
1253
1255{
1257
1258 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1259 BLI_STATIC_ASSERT(ENDIAN_ORDER == L_ENDIAN, "Blender only builds on little endian systems")
1260 BKE_reportf(reports,
1261 RPT_ERROR,
1262 "Blend file '%s' created by a Big Endian version of Blender, support for "
1263 "these files has been removed in Blender 5.0, use an older version of Blender "
1264 "to open and convert it.",
1265 fd->relabase);
1267 fd = nullptr;
1268 }
1269 else if (fd->flags & FD_FLAGS_FILE_OK) {
1270 const char *error_message = nullptr;
1271 if (read_file_dna(fd, &error_message) == false) {
1273 reports, RPT_ERROR, "Failed to read blend file '%s': %s", fd->relabase, error_message);
1275 fd = nullptr;
1276 }
1277 else if (is_minversion_older_than_blender(fd, reports)) {
1279 fd = nullptr;
1280 }
1281 }
1282 else if (fd->flags & FD_FLAGS_FILE_FUTURE) {
1284 reports,
1285 RPT_ERROR,
1286 "Cannot read blend file '%s', incomplete header, may be from a newer version of Blender",
1287 fd->relabase);
1289 fd = nullptr;
1290 }
1291 else {
1292 BKE_reportf(reports, RPT_ERROR, "Failed to read file '%s', not a blend file", fd->relabase);
1294 fd = nullptr;
1295 }
1296
1297 return fd;
1298}
1299
1300static FileData *blo_filedata_from_file_descriptor(const char *filepath,
1301 BlendFileReadReport *reports,
1302 const int filedes)
1303{
1305 if (file == nullptr) {
1306 BKE_reportf(reports->reports, RPT_WARNING, "Unrecognized file format '%s'", filepath);
1307 return nullptr;
1308 }
1309
1310 FileData *fd = filedata_new(reports);
1311 fd->file = file;
1312
1313 BLI_stat_t stat;
1314 if (BLI_stat(filepath, &stat) != -1) {
1315 fd->file_stat = stat;
1316 }
1317
1318 return fd;
1319}
1320
1321static FileData *blo_filedata_from_file_open(const char *filepath, BlendFileReadReport *reports)
1322{
1323 errno = 0;
1324 const int file = BLI_open(filepath, O_BINARY | O_RDONLY, 0);
1325 if (file == -1) {
1326 BKE_reportf(reports->reports,
1328 "Unable to open '%s': %s",
1329 filepath,
1330 errno ? strerror(errno) : RPT_("unknown error reading file"));
1331 return nullptr;
1332 }
1333 return blo_filedata_from_file_descriptor(filepath, reports, file);
1334}
1335
1337{
1338 FileData *fd = blo_filedata_from_file_open(filepath, reports);
1339 if (fd != nullptr) {
1340 /* needed for library_append and read_libraries */
1341 STRNCPY(fd->relabase, filepath);
1342
1343 return blo_decode_and_check(fd, reports->reports);
1344 }
1345 return nullptr;
1346}
1347
1352static FileData *blo_filedata_from_file_minimal(const char *filepath)
1353{
1354 BlendFileReadReport read_report{};
1355 FileData *fd = blo_filedata_from_file_open(filepath, &read_report);
1356 if (fd != nullptr) {
1358 if (fd->flags & FD_FLAGS_FILE_OK) {
1359 return fd;
1360 }
1362 }
1363 return nullptr;
1364}
1365
1367 const int memsize,
1368 BlendFileReadReport *reports)
1369{
1370 if (!mem || memsize < MIN_SIZEOFBLENDERHEADER) {
1371 BKE_report(
1372 reports->reports, RPT_WARNING, (mem) ? RPT_("Unable to read") : RPT_("Unable to open"));
1373 return nullptr;
1374 }
1375
1377 if (!file) {
1378 return nullptr;
1379 }
1380
1381 FileData *fd = filedata_new(reports);
1382 fd->file = file;
1383
1384 return blo_decode_and_check(fd, reports->reports);
1385}
1386
1388 const BlendFileReadParams *params,
1389 BlendFileReadReport *reports)
1390{
1391 if (!memfile) {
1392 BKE_report(reports->reports, RPT_WARNING, "Unable to open blend <memory>");
1393 return nullptr;
1394 }
1395
1396 FileData *fd = filedata_new(reports);
1397 fd->file = BLO_memfile_new_filereader(memfile, params->undo_direction);
1398 fd->undo_direction = params->undo_direction;
1400
1401 return blo_decode_and_check(fd, reports->reports);
1402}
1403
1405{
1406 /* Free all BHeadN data blocks */
1407#ifdef NDEBUG
1408 fd->bhead_list.free_no_destruct();
1409#else
1410 /* Sanity check we're not keeping memory we don't need. */
1411 for (BHeadN &new_bhead : fd->bhead_list.items_mutable()) {
1412# ifdef USE_BHEAD_READ_ON_DEMAND
1413 if (fd->file->seek != nullptr && BHEAD_USE_READ_ON_DEMAND(&new_bhead.bhead)) {
1414 BLI_assert(new_bhead.has_data == 0);
1415 }
1416# endif
1417 MEM_delete(&new_bhead);
1418 }
1419#endif
1420 fd->file->close(fd->file);
1421
1422 if (fd->compflags) {
1423 MEM_delete(fd->compflags);
1424 }
1425 if (fd->reconstruct_info) {
1427 }
1428
1429 if (fd->datamap) {
1431 }
1432 if (fd->globmap) {
1434 }
1435 if (fd->libmap) {
1437 }
1438 if (fd->old_idmap_uid != nullptr) {
1440 }
1441 if (fd->new_idmap_uid != nullptr) {
1443 }
1445 if (fd->bheadmap) {
1446 MEM_delete(fd->bheadmap);
1447 }
1448
1449 MEM_delete(fd);
1450}
1451
1453
1454/* -------------------------------------------------------------------- */
1457
1459{
1460 BlendThumbnail *data = nullptr;
1461
1463 if (fd) {
1464 if (const int *fd_data = read_file_thumbnail(fd)) {
1465 const int width = fd_data[0];
1466 const int height = fd_data[1];
1467 if (BLEN_THUMB_MEMSIZE_IS_VALID(width, height)) {
1468 const size_t data_size = BLEN_THUMB_MEMSIZE(width, height);
1469 data = static_cast<BlendThumbnail *>(MEM_new_uninitialized(data_size, __func__));
1470 if (data) {
1471 BLI_assert((data_size - sizeof(*data)) ==
1472 (BLEN_THUMB_MEMSIZE_FILE(width, height) - (sizeof(*fd_data) * 2)));
1473 data->width = width;
1474 data->height = height;
1475 memcpy(data->rect, &fd_data[2], data_size - sizeof(*data));
1476 }
1477 }
1478 }
1480 }
1481
1482 return data;
1483}
1484
1485short BLO_version_from_file(const char *filepath)
1486{
1487 short version = 0;
1489 if (fd) {
1490 version = fd->fileversion;
1492 }
1493 return version;
1494}
1495
1497
1498/* -------------------------------------------------------------------- */
1501
1502/* Only direct data-blocks. */
1503static void *newdataadr(FileData *fd, const void *adr, int64_t *r_alloc_len = nullptr)
1504{
1505 return oldnewmap_lookup_and_inc(fd->datamap, adr, true, r_alloc_len);
1506}
1507
1508/* Only direct data-blocks. */
1509static void *newdataadr_no_us(FileData *fd, const void *adr, int64_t *r_alloc_len = nullptr)
1510{
1511 return oldnewmap_lookup_and_inc(fd->datamap, adr, false, r_alloc_len);
1512}
1513
1518static NewAddress *newdataadr_entry(FileData *fd, const void *adr)
1519{
1520 return fd->datamap->map.lookup_ptr(adr);
1521}
1522
1524{
1525 return oldnewmap_lookup_and_inc(fd->globmap, adr, true);
1526}
1527
1528/* only lib data */
1529static void *newlibadr(FileData *fd, ID * /*self_id*/, const bool is_linked_only, const void *adr)
1530{
1531 return oldnewmap_liblookup(fd->libmap, adr, is_linked_only);
1532}
1533
1535 ID *self_id,
1536 const bool is_linked_only,
1537 const void *adr)
1538{
1539 return newlibadr(fd, self_id, is_linked_only, adr);
1540}
1541
1542/* increases user number */
1544 const void *old,
1545 void *newp)
1546{
1547 for (NewAddress &entry : fd->libmap->map.values()) {
1548 if (old == entry.newp && entry.nr == ID_LINK_PLACEHOLDER) {
1549 entry.newp = newp;
1550 if (newp) {
1551 entry.nr = ((ID *)newp)->id_type();
1552 }
1553 }
1554 }
1555}
1556
1557/* Very rarely needed, allows some form of ID remapping as part of readfile process.
1558 *
1559 * Currently only used to remap duplicate library pointers.
1560 */
1561static void change_ID_pointer_to_real_ID_pointer_fd(FileData *fd, const void *old, void *newp)
1562{
1563 for (NewAddress &entry : fd->libmap->map.values()) {
1564 if (old == entry.newp) {
1565 BLI_assert(BKE_idtype_idcode_is_valid(short(entry.nr)));
1566 entry.newp = newp;
1567 if (newp) {
1568 entry.nr = ((ID *)newp)->id_type();
1569 }
1570 }
1571 }
1572}
1573
1575{
1576 if (bmain->curlib) {
1577 return bmain->curlib->runtime->filedata;
1578 }
1579 return basefd;
1580}
1581
1583 const void *old,
1584 void *newp)
1585{
1586 for (Main *mainptr : *basefd->bmain->split_mains) {
1587 FileData *fd = change_ID_link_filedata_get(mainptr, basefd);
1588 if (fd) {
1590 }
1591 }
1592}
1593
1594static void change_ID_pointer_to_real_ID_pointer(FileData *basefd, const void *old, void *newp)
1595{
1596 for (Main *mainptr : *basefd->bmain->split_mains) {
1597 FileData *fd = change_ID_link_filedata_get(mainptr, basefd);
1598 if (fd) {
1600 }
1601 }
1602}
1603
1605{
1606 if (fd->old_idmap_uid != nullptr) {
1608 }
1609 fd->old_idmap_uid = BKE_main_idmap_create(bmain, false, nullptr, MAIN_IDMAP_TYPE_UID);
1610}
1611
1616
1621
1624 ID *id, const IDCacheKey *key, void **cache_p, uint /*flags*/, void *cache_storage_v)
1625{
1626 if (*cache_p == nullptr) {
1627 return;
1628 }
1629
1630 BLI_assert(key->id_session_uid == id->session_uid);
1632
1633 BLOCacheStorage *cache_storage = static_cast<BLOCacheStorage *>(cache_storage_v);
1634 BLI_assert(!BLI_ghash_haskey(cache_storage->cache_map, key));
1635
1636 IDCacheKey *storage_key = static_cast<IDCacheKey *>(
1637 BLI_memarena_alloc(cache_storage->memarena, sizeof(*storage_key)));
1638 *storage_key = *key;
1639 BLOCacheStorageValue *storage_value = static_cast<BLOCacheStorageValue *>(
1640 BLI_memarena_alloc(cache_storage->memarena, sizeof(*storage_value)));
1641 storage_value->cache_v = *cache_p;
1642 storage_value->new_usage_count = 0;
1643 BLI_ghash_insert(cache_storage->cache_map, storage_key, storage_value);
1644}
1645
1648 ID *id, const IDCacheKey *key, void **cache_p, const uint flags, void *cache_storage_v)
1649{
1650 BLOCacheStorage *cache_storage = static_cast<BLOCacheStorage *>(cache_storage_v);
1651
1652 if (cache_storage == nullptr) {
1653 /* In non-undo case, only clear the pointer if it is a purely runtime one.
1654 * If it may be stored in a persistent way in the .blend file, direct_link code is responsible
1655 * to properly deal with it. */
1656 if ((flags & IDTYPE_CACHE_CB_FLAGS_PERSISTENT) == 0) {
1657 *cache_p = nullptr;
1658 }
1659 return;
1660 }
1661
1662 /* Assume that when ID source is tagged as changed, its caches need to be cleared.
1663 * NOTE: This is mainly a work-around for some IDs, like Image, which use a non-depsgraph-handled
1664 * process for part of their updates.
1665 */
1666 if (id->recalc & ID_RECALC_SOURCE) {
1667 *cache_p = nullptr;
1668 return;
1669 }
1670
1671 BLOCacheStorageValue *storage_value = static_cast<BLOCacheStorageValue *>(
1672 BLI_ghash_lookup(cache_storage->cache_map, key));
1673 if (storage_value == nullptr) {
1674 *cache_p = nullptr;
1675 return;
1676 }
1677 storage_value->new_usage_count++;
1678 *cache_p = storage_value->cache_v;
1679}
1680
1683 const IDCacheKey *key,
1684 void **cache_p,
1685 const uint /*flags*/,
1686 void *cache_storage_v)
1687{
1688 BLOCacheStorage *cache_storage = static_cast<BLOCacheStorage *>(cache_storage_v);
1689
1690 BLOCacheStorageValue *storage_value = static_cast<BLOCacheStorageValue *>(
1691 BLI_ghash_lookup(cache_storage->cache_map, key));
1692 if (storage_value == nullptr) {
1693 *cache_p = nullptr;
1694 return;
1695 }
1696 /* If that cache has been restored into some new ID, we want to remove it from old one, otherwise
1697 * keep it there so that it gets properly freed together with its ID. */
1698 if (storage_value->new_usage_count != 0) {
1699 *cache_p = nullptr;
1700 }
1701 else {
1702 BLI_assert(*cache_p == storage_value->cache_v);
1703 }
1704}
1705
1707{
1708 if (fd->flags & FD_FLAGS_IS_MEMFILE) {
1709 BLI_assert(fd->cache_storage == nullptr);
1714
1715 ListBaseT<ID> *lb;
1716 FOREACH_MAIN_LISTBASE_BEGIN (bmain, lb) {
1717 ID *id = lb->first();
1718 if (id == nullptr) {
1719 continue;
1720 }
1721
1722 const IDTypeInfo *type_info = BKE_idtype_get_info_from_id(id);
1723 if (type_info->foreach_cache == nullptr) {
1724 continue;
1725 }
1726
1728 if (ID_IS_LINKED(id)) {
1729 continue;
1730 }
1732 }
1734 }
1736 }
1737 else {
1738 fd->cache_storage = nullptr;
1739 }
1740}
1741
1743{
1744 if (fd->cache_storage != nullptr) {
1745 ListBaseT<ID> *lb;
1746 FOREACH_MAIN_LISTBASE_BEGIN (bmain_old, lb) {
1747 ID *id = lb->first();
1748 if (id == nullptr) {
1749 continue;
1750 }
1751
1752 const IDTypeInfo *type_info = BKE_idtype_get_info_from_id(id);
1753 if (type_info->foreach_cache == nullptr) {
1754 continue;
1755 }
1756
1758 if (ID_IS_LINKED(id)) {
1759 continue;
1760 }
1762 }
1764 }
1766 }
1767}
1768
1770{
1771 if (fd->cache_storage != nullptr) {
1772 BLI_ghash_free(fd->cache_storage->cache_map, nullptr, nullptr);
1774 MEM_delete(fd->cache_storage);
1775 fd->cache_storage = nullptr;
1776 }
1777}
1778
1780
1781/* -------------------------------------------------------------------- */
1784
1791static const char *get_alloc_name(FileData *fd,
1792 BHead *bh,
1793 const char *blockname,
1794 const int id_type_index = INDEX_ID_NULL)
1795{
1796#ifndef NDEBUG
1797 /* Storage key is a pair of (string , int), where the first is the concatenation of the 'owner
1798 * block' string and DNA struct type name, and the second the length of the array, as defined by
1799 * the #BHead.nr value. */
1800 using keyT = const std::pair<const std::string, const int>;
1801#else
1802 /* Storage key is simple int, which is the ID type index. */
1803 using keyT = int;
1804#endif
1805 constexpr std::string_view STORAGE_ID = "readfile";
1806
1807 /* NOTE: This is thread_local storage, so as long as the handling of a same FileData is not
1808 * spread across threads (which is not supported at all currently), this is thread-safe. */
1809 if (!fd->storage_handle) {
1811 std::string(STORAGE_ID));
1812 }
1815
1816 const bool is_id_data = !blockname && (id_type_index >= 0 && id_type_index < INDEX_ID_MAX);
1817
1818#ifndef NDEBUG
1819 /* Local storage of id type names, for fast access to this info. */
1820 static const std::array<std::string, INDEX_ID_MAX> id_alloc_names = [] {
1821 auto n = decltype(id_alloc_names)();
1822 for (int idtype_index = 0; idtype_index < INDEX_ID_MAX; idtype_index++) {
1823 const IDTypeInfo *idtype_info = BKE_idtype_get_info_from_idtype_index(idtype_index);
1824 BLI_assert(idtype_info);
1825 if (idtype_index == INDEX_ID_NULL) {
1826 /* #INDEX_ID_NULL returns the #IDType_ID_LINK_PLACEHOLDER type info, here we will rather
1827 * use it for unknown/invalid ID types. */
1828 n[size_t(idtype_index)] = "UNKNWOWN";
1829 }
1830 else {
1831 n[size_t(idtype_index)] = idtype_info->name;
1832 }
1833 }
1834 return n;
1835 }();
1836
1837 const std::string block_alloc_name = is_id_data ? id_alloc_names[id_type_index] : blockname;
1838 const std::string struct_name = DNA_struct_identifier(fd->filesdna.get(), bh->SDNAnr);
1839 keyT key{block_alloc_name + struct_name, bh->nr};
1840 if (!storage.contains(key)) {
1841 const std::string alloc_string = fmt::format(
1842 fmt::runtime(is_id_data ? "{}{} (for ID type '{}')" : "{}{} (for block '{}')"),
1843 struct_name,
1844 bh->nr > 1 ? fmt::format("[{}]", bh->nr) : "",
1845 block_alloc_name);
1846 return storage.insert(key, alloc_string);
1847 }
1848 return storage.find(key);
1849#else
1850 /* Simple storage for pure release builds, using integer as key, one entry for each ID type. */
1852 if (is_id_data) {
1853 if (!storage.contains(id_type_index)) [[unlikely]] {
1854 if (id_type_index == INDEX_ID_NULL) {
1855 return storage.insert(id_type_index, "Data from UNKNOWN");
1856 }
1857 const IDTypeInfo *id_type = BKE_idtype_get_info_from_idtype_index(id_type_index);
1858 const std::string alloc_string = fmt::format("Data from '{}' ID type", id_type->name);
1859 return storage.insert(id_type_index, alloc_string);
1860 }
1861 return storage.find(id_type_index);
1862 }
1863 return blockname;
1864#endif
1865}
1866
1867static void *read_struct(
1868 FileData *fd, BHead *bh, const char *blockname, const int id_type_index, int64_t *r_alloc_len)
1869{
1870 void *temp = nullptr;
1871
1872 if (bh->len) {
1873#ifdef USE_BHEAD_READ_ON_DEMAND
1874 BHead *bh_orig = bh;
1875#endif
1876
1877 /* A corrupt file could reference a struct index outside the file's SDNA, used below to index
1878 * the compare-flags, reconstruction & alignment tables. */
1879 if (bh->SDNAnr < 0 || bh->SDNAnr >= fd->filesdna->structs.size()) [[unlikely]] {
1880 fd->flags &= ~FD_FLAGS_FILE_OK;
1881 if (fd->bmain) {
1882 blo_readfile_invalidate(fd, fd->bmain, "Corrupt .blend file, invalid block struct index");
1883 }
1884 return nullptr;
1885 }
1886
1887 /* Endianness switch is based on file DNA.
1888 *
1889 * NOTE: raw data (aka #SDNA_RAW_DATA_STRUCT_INDEX #SDNAnr) is not handled here, it's up to
1890 * the calling code to manage this. */
1892 BLI_STATIC_ASSERT(SDNA_RAW_DATA_STRUCT_INDEX == 0, "'raw data' SDNA struct index should be 0")
1894#ifdef USE_BHEAD_READ_ON_DEMAND
1895 if (BHEADN_FROM_BHEAD(bh)->has_data == false) {
1896 bh = blo_bhead_read_full(fd, bh);
1897 if (bh == nullptr) [[unlikely]] {
1898 fd->flags &= ~FD_FLAGS_FILE_OK;
1899 return nullptr;
1900 }
1901 }
1902#endif
1903 }
1904
1905 /* The block must be large enough to hold the number of structs it claims, otherwise
1906 * reconstruction (which strides the block by the file struct size) reads past its end.
1907 * Check any head that is not raw data and not a removed struct.
1908 * Written as a division to avoid overflow in `nr * struct_size`. */
1910 {
1911 const int64_t old_struct_size = DNA_struct_size(fd->filesdna.get(), bh->SDNAnr);
1912 if (bh->nr < 1 || bh->nr > INT_MAX ||
1913 (old_struct_size != 0 && bh->nr > bh->len / old_struct_size)) [[unlikely]]
1914 {
1915 fd->flags &= ~FD_FLAGS_FILE_OK;
1916 if (fd->bmain) {
1917 blo_readfile_invalidate(fd, fd->bmain, "Corrupt .blend file, invalid block count");
1918 }
1919#ifdef USE_BHEAD_READ_ON_DEMAND
1920 if (bh_orig != bh) {
1921 MEM_delete(BHEADN_FROM_BHEAD(bh));
1922 }
1923#endif
1924 return nullptr;
1925 }
1926 }
1927
1928 if (fd->compflags[bh->SDNAnr] != SDNA_CMP_REMOVED) {
1929 const char *alloc_name = get_alloc_name(fd, bh, blockname, id_type_index);
1930 if (fd->compflags[bh->SDNAnr] == SDNA_CMP_NOT_EQUAL) {
1931#ifdef USE_BHEAD_READ_ON_DEMAND
1932 if (BHEADN_FROM_BHEAD(bh)->has_data == false) {
1933 bh = blo_bhead_read_full(fd, bh);
1934 if (bh == nullptr) [[unlikely]] {
1935 fd->flags &= ~FD_FLAGS_FILE_OK;
1936 return nullptr;
1937 }
1938 }
1939#endif
1941 fd->reconstruct_info, bh->SDNAnr, bh->nr, (bh + 1), alloc_name, r_alloc_len);
1942 }
1943 else {
1944 /* SDNA_CMP_EQUAL */
1945 const int alignment = DNA_struct_alignment(fd->filesdna.get(), bh->SDNAnr);
1946 temp = MEM_new_uninitialized_aligned(bh->len, alignment, alloc_name);
1947#ifdef USE_BHEAD_READ_ON_DEMAND
1948 if (BHEADN_FROM_BHEAD(bh)->has_data) {
1949 memcpy(temp, (bh + 1), bh->len);
1950 }
1951 else {
1952 /* Instead of allocating the bhead, then copying it,
1953 * read the data from the file directly into the memory. */
1954 if (!blo_bhead_read_data(fd, bh, temp)) [[unlikely]] {
1955 fd->flags &= ~FD_FLAGS_FILE_OK;
1956 MEM_delete_void(temp);
1957 temp = nullptr;
1958 }
1959 }
1960#else
1961 memcpy(temp, (bh + 1), bh->len);
1962#endif
1963 if (r_alloc_len && temp) {
1964 *r_alloc_len = bh->len;
1965 }
1966 }
1967 }
1968
1969#ifdef USE_BHEAD_READ_ON_DEMAND
1970 if (bh_orig != bh) {
1971 MEM_delete(BHEADN_FROM_BHEAD(bh));
1972 }
1973#endif
1974 }
1975
1976 return temp;
1977}
1978
1981 BHead *bh,
1982 const char *blockname,
1983 const int id_type_index,
1984 const int64_t min_size)
1985{
1986 int64_t alloc_len = 0;
1987 void *data = read_struct(fd, bh, blockname, id_type_index, &alloc_len);
1988 if (data == nullptr || alloc_len >= min_size) {
1989 return data;
1990 }
1991
1993 fd->flags &= ~FD_FLAGS_FILE_OK;
1994 if (fd->bmain) {
1995 blo_readfile_invalidate(fd, fd->bmain, "Corrupt .blend file, block too small for its type");
1996 }
1997 return nullptr;
1998}
1999
2000static ID *read_id_struct(FileData *fd, BHead *bh, const char *blockname, const int id_type_index)
2001{
2002 int64_t alloc_len = 0;
2003 ID *id = static_cast<ID *>(read_struct(fd, bh, blockname, id_type_index, &alloc_len));
2004 if (!id) {
2005 return id;
2006 }
2007
2008 /* Verify the allocation size is large enough for the ID type. */
2009 int64_t min_size = sizeof(ID);
2010 if (alloc_len >= min_size && bh->code != ID_LINK_PLACEHOLDER) {
2011 if (const IDTypeInfo *id_type = BKE_idtype_get_info_from_idcode(id->id_type())) {
2012 min_size = id_type->struct_size;
2013 }
2014 }
2015 if (alloc_len < min_size) [[unlikely]] {
2016 MEM_delete(id);
2017 fd->flags &= ~FD_FLAGS_FILE_OK;
2018 if (fd->bmain) {
2019 blo_readfile_invalidate(fd, fd->bmain, "Corrupt .blend file, block too small for its type");
2020 }
2021 return nullptr;
2022 }
2023
2024 /* Invalid ID name (probably from 'too long' ID name from a future Blender version).
2025 *
2026 * They can only be truncated here, ensuring that all ID names remain unique happens later, after
2027 * reading all local IDs, but before linking them, see the call to
2028 * #long_id_names_ensure_unique_id_names in #blo_read_file_internal. */
2029 if (BLI_str_utf8_truncate_at_size(id->name + 2, MAX_ID_NAME - 2)) {
2031 CLOG_DEBUG(&LOG, "Truncated too long ID name to '%s'", id->name);
2032 }
2033
2034 return id;
2035}
2036
2037/* Like read_struct, but gets a pointer without allocating. Only works for
2038 * undo since DNA must match. */
2039static const void *peek_struct_undo(FileData *fd, BHead *bhead)
2040{
2043 return (bhead->len) ? static_cast<const void *>(bhead + 1) : nullptr;
2044}
2045
2046static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
2047{
2048 Link *ln, *prev;
2049 void *poin;
2050
2051 if (BLI_listbase_is_empty(lb)) {
2052 return;
2053 }
2054 poin = newdataadr(fd, lb->first_);
2055 if (lb->first_) {
2056 oldnewmap_insert(fd->globmap, lb->first_, poin, 0);
2057 }
2058 lb->first_ = poin;
2059
2060 ln = static_cast<Link *>(lb->first_);
2061 prev = nullptr;
2062 while (ln) {
2063 poin = newdataadr(fd, ln->next);
2064 if (ln->next) {
2065 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
2066 }
2067 ln->next = static_cast<Link *>(poin);
2068 ln->prev = prev;
2069 prev = ln;
2070 ln = ln->next;
2071 }
2072 lb->last_ = prev;
2073}
2074
2076
2077/* -------------------------------------------------------------------- */
2080
2081static void after_liblink_id_process(BlendLibReader *reader, ID *id);
2082
2084{
2085
2086 /* Handle 'private IDs'. */
2087 bNodeTree *nodetree = bke::node_tree_from_id(id);
2088 if (nodetree != nullptr) {
2089 after_liblink_id_process(reader, &nodetree->id);
2090
2091 if (nodetree->owner_id == nullptr) {
2092 CLOG_WARN(&LOG,
2093 "NULL owner_id pointer for embedded NodeTree of %s, should never happen",
2094 id->name);
2095 nodetree->owner_id = id;
2096 }
2097 else if (nodetree->owner_id != id) {
2098 CLOG_WARN(&LOG,
2099 "Inconsistent owner_id pointer for embedded NodeTree of %s, should never happen",
2100 id->name);
2101 nodetree->owner_id = id;
2102 }
2103 }
2104
2105 if (id->id_type() == ID_SCE) {
2106 Scene *scene = id_cast<Scene *>(id);
2107 if (scene->master_collection != nullptr) {
2109
2110 if (scene->master_collection->owner_id == nullptr) {
2111 CLOG_WARN(&LOG,
2112 "NULL owner_id pointer for embedded Scene Collection of %s, should never happen",
2113 id->name);
2114 scene->master_collection->owner_id = id;
2115 }
2116 else if (scene->master_collection->owner_id != id) {
2117 CLOG_WARN(&LOG,
2118 "Inconsistent owner_id pointer for embedded Scene Collection of %s, should "
2119 "never happen",
2120 id->name);
2121 scene->master_collection->owner_id = id;
2122 }
2123 }
2124 }
2125}
2126
2128{
2129 /* NOTE: WM IDProperties are never written to file, hence they should always be nullptr here. */
2130 BLI_assert((id->id_type() != ID_WM) || id->properties == nullptr);
2131
2133
2134 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id);
2135 if (id_type->blend_read_after_liblink != nullptr) {
2136 id_type->blend_read_after_liblink(reader, id);
2137 }
2138}
2139
2142{
2143 BLO_read_string(reader, &op->rna_path);
2144
2145 op->tag = {}; /* Runtime only. */
2146
2148
2151 BLO_read_string(reader, &opop.subitem_local_name);
2152 BLO_read_string(reader, &opop.label);
2153 BLO_read_string(reader, &opop.tooltip);
2154
2155 opop.tag = {}; /* Runtime only. */
2156 }
2157}
2158
2159static void direct_link_id_common(BlendDataReader *reader,
2160 Library *current_library,
2161 ID *id,
2162 ID *id_old,
2163 eID_Tag id_tag,
2164 ID_Readfile_Data::Tags id_read_tags);
2165
2167 Library *current_library,
2168 ID *id,
2169 ID *id_old)
2170{
2171 /* Handle 'private IDs'. */
2172 if (id->id_type() == ID_SCE) {
2173 Scene *scene = id_cast<Scene *>(id);
2174 if (scene->compositing_node_group) {
2175 /* If `scene->compositing_node_group != nullptr`, then this means the blend file was created
2176 * by a version that wrote the compositing_node_group as its own ID datablock. Since
2177 * `scene->nodetree` was written for forward compatibility reasons only, we can ignore it. */
2178 scene->nodetree = nullptr;
2179 }
2180 }
2181 bNodeTree **nodetree = bke::node_tree_ptr_from_id(id);
2182 if (nodetree != nullptr && *nodetree != nullptr) {
2183 BLO_read_struct(reader, bNodeTree, nodetree);
2184 if (!*nodetree || !BKE_idtype_idcode_is_valid((*nodetree)->id.id_type())) {
2186 reader->fd->reports,
2187 RPT_ERROR,
2188 RPT_("Data-block '%s' had an invalid embedded node group, which has not been read"),
2189 id->name);
2190 MEM_SAFE_DELETE(*nodetree);
2191 }
2192 else {
2193 direct_link_id_common(reader,
2194 current_library,
2195 id_cast<ID *>(*nodetree),
2196 id_old != nullptr ? id_cast<ID *>(bke::node_tree_from_id(id_old)) :
2197 nullptr,
2198 {},
2200 bke::node_tree_blend_read_data(reader, id, *nodetree);
2201 }
2202 }
2203
2204 if (id->id_type() == ID_SCE) {
2205 Scene *scene = id_cast<Scene *>(id);
2206 if (scene->master_collection != nullptr) {
2208 if (!scene->master_collection ||
2210 {
2212 reader->fd->reports,
2213 RPT_ERROR,
2214 RPT_("Scene '%s' had an invalid root collection, which has not been read"),
2215 BKE_id_name(*id));
2216 MEM_SAFE_DELETE(scene->master_collection);
2217 }
2218 else {
2220 reader,
2221 current_library,
2222 &scene->master_collection->id,
2223 id_old != nullptr ? &(id_cast<Scene *>(id_old))->master_collection->id : nullptr,
2224 {},
2226 BKE_collection_blend_read_data(reader, scene->master_collection, &scene->id);
2227 }
2228 }
2229 }
2230}
2231
2233{
2234 /* Exception for armature objects, where the pose has direct points to the
2235 * armature data-block. */
2236 if (id_current->id_type() == ID_OB && (id_cast<Object *>(const_cast<ID *>(id_current)))->pose) {
2237 return ID_RECALC_GEOMETRY;
2238 }
2239
2240 return 0;
2241}
2242
2244 const ID *id_target,
2245 const ID *id_current,
2246 const bool is_identical)
2247{
2248 /* These are the evaluations that had not been performed yet at the time the
2249 * target undo state was written. These need to be done again, since they may
2250 * flush back changes to the original datablock. */
2251 int recalc = id_target->recalc;
2252
2253 if (id_current == nullptr) {
2254 /* ID does not currently exist in the database, so also will not exist in
2255 * the dependency graphs. That means it will be newly created and as a
2256 * result also fully re-evaluated regardless of the recalc flag set here. */
2257 recalc |= ID_RECALC_ALL;
2258 }
2259 else {
2260 /* If the contents datablock changed, the depsgraph needs to copy the
2261 * datablock again to ensure it matches the original datablock. */
2262 if (!is_identical) {
2263 recalc |= ID_RECALC_SYNC_TO_EVAL;
2264 }
2265
2266 /* Special exceptions. */
2267 recalc |= direct_link_id_restore_recalc_exceptions(id_current);
2268
2269 /* Evaluations for the current state that have not been performed yet
2270 * by the time we are performing this undo step. */
2271 recalc |= id_current->recalc;
2272
2273 /* Tags that were set between the target state and the current state,
2274 * that we need to perform again. */
2275 if (fd->undo_direction == STEP_UNDO) {
2276 /* Undo: tags from target to the current state. */
2277 recalc |= id_current->recalc_up_to_undo_push;
2278 }
2279 else {
2281 /* Redo: tags from current to the target state. */
2282 recalc |= id_target->recalc_up_to_undo_push;
2283 }
2284 }
2285
2286 return recalc;
2287}
2288
2290{
2291 if (id.runtime->readfile_data) {
2292 return;
2293 }
2294 id.runtime->readfile_data = MEM_new_zeroed<ID_Readfile_Data>(__func__);
2295}
2296
2298{
2299 /* NOTE: While usually not expected, there are some valid cases where ID::runtime will be nullptr
2300 * (e.g. for temporary buffers used as 'proxies' of actual IDs in writefile process). */
2301 if (!id.runtime || !id.runtime->readfile_data) {
2302 return ID_Readfile_Data::Tags{};
2303 }
2304 return id.runtime->readfile_data->tags;
2305}
2306
2308{
2310 return id.runtime->readfile_data->tags;
2311}
2312
2314{
2315 MEM_SAFE_DELETE(id.runtime->readfile_data);
2316}
2317
2319{
2320 ID *id;
2321 FOREACH_MAIN_ID_BEGIN (&bmain, id) {
2322 /* Handle the ID itself. */
2324
2325 /* Handle its embedded IDs, because they do not get referenced by bmain. */
2326 if (id->id_type() == ID_SCE) {
2327 Collection *collection = reinterpret_cast<Scene *>(id)->master_collection;
2328 if (collection) {
2330 }
2331 }
2332
2333 bNodeTree *node_tree = bke::node_tree_from_id(id);
2334 if (node_tree) {
2336 }
2337 }
2339}
2340
2342 Library *current_library,
2343 ID *id,
2344 ID *id_old,
2345 const eID_Tag id_tag,
2346 const ID_Readfile_Data::Tags id_read_tags)
2347{
2348 /* This should have been caught already, either by a call to `#blo_bhead_is_id_valid_type` for
2349 * regular IDs, or in `#direct_link_id_embedded_id` for embedded ones. */
2351 "Unknown or invalid ID type, this should never happen");
2352
2353 BLI_assert(id->runtime == nullptr);
2355
2356 if (!BLO_read_data_is_undo(reader)) {
2357 /* When actually reading a file, we do want to reset/re-generate session UIDS.
2358 * In undo case, we want to re-use existing ones. */
2359 id->session_uid = MAIN_ID_SESSION_UID_UNSET;
2360 }
2361
2362 id->lib = current_library;
2363 if (id->lib) {
2364 /* Always fully clear fake user flag for linked data. */
2365 id->flag &= ~ID_FLAG_FAKEUSER;
2366 }
2367 id->us = ID_FAKE_USERS(id);
2368 id->icon_id = 0;
2369 id->newid = nullptr; /* Needed because .blend may have been saved with crap value here... */
2370 id->orig_id = nullptr;
2371 id->py_instance = nullptr;
2372
2373 /* Initialize with provided tag. */
2374 if (BLO_read_data_is_undo(reader)) {
2375 id->tag = (id_tag & ~ID_TAG_KEEP_ON_UNDO) | (id->tag & ID_TAG_KEEP_ON_UNDO);
2376 }
2377 else {
2378 id->tag = id_tag;
2379 }
2380
2381 if (id->flag & ID_FLAG_LINKED_AND_PACKED) {
2382 if (!id->lib) {
2383 CLOG_ERROR(&LOG,
2384 "Data-block '%s' flagged as packed, but without a valid library, fixing by "
2385 "making fully local...",
2386 id->name);
2387 id->tag &= ~(ID_TAG_INDIRECT | ID_TAG_EXTERN);
2389 }
2390 else if ((id->lib->flag & LIBRARY_FLAG_IS_ARCHIVE) == 0) {
2391 CLOG_ERROR(&LOG,
2392 "Data-block '%s' flagged as packed, but using a regular library, fixing by "
2393 "making fully indirectly linked...",
2394 id->name);
2395 id->flag &= ~ID_FLAG_LINKED_AND_PACKED;
2396 if (id->tag & ID_TAG_EXTERN) {
2397 id->tag &= ~ID_TAG_EXTERN;
2398 id->tag |= ID_TAG_INDIRECT;
2399 }
2400 }
2401 }
2402
2404 id->runtime->readfile_data->tags = id_read_tags;
2405
2406 if ((id_tag & ID_TAG_TEMP_MAIN) == 0) {
2408 }
2409
2410 if (ID_IS_LINKED(id)) {
2411 id->library_weak_reference = nullptr;
2412 }
2413 else {
2414 BLO_read_struct(reader, LibraryWeakReference, &id->library_weak_reference);
2415 }
2416
2418 /* For placeholder we only need to set the tag and properly initialize generic ID fields above,
2419 * no further data to read. */
2420 return;
2421 }
2422
2423 BKE_animdata_blend_read_data(reader, id);
2424
2425 BLO_read_struct(reader, AssetMetaData, &id->asset_data);
2426 if (id->asset_data) {
2427 BKE_asset_metadata_read(reader, id->asset_data);
2428 /* Restore runtime asset type info. */
2429 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id);
2430 id->asset_data->local_type_info = id_type->asset_type_info;
2431 }
2432
2433 /* Link direct data of ID properties. */
2434 if (id->properties) {
2435 BLO_read_struct(reader, IDProperty, &id->properties);
2436 /* this case means the data was written incorrectly, it should not happen */
2437 IDP_BlendDataRead(reader, &id->properties);
2438 }
2439
2440 if (id->system_properties) {
2441 BLO_read_struct(reader, IDProperty, &id->system_properties);
2442 IDP_BlendDataRead(reader, &id->system_properties);
2443 }
2444
2445 id->flag &= ~ID_FLAG_INDIRECT_WEAK_LINK;
2446
2447 /* NOTE: It is important to not clear the recalc flags for undo/redo.
2448 * Preserving recalc flags on redo/undo is the only way to make dependency graph detect
2449 * that animation is to be evaluated on undo/redo. If this is not enforced by the recalc
2450 * flags dependency graph does not do animation update to avoid loss of unkeyed changes.,
2451 * which conflicts with undo/redo of changes to animation data itself.
2452 *
2453 * But for regular file load we clear the flag, since the flags might have been changed since
2454 * the version the file has been saved with. */
2455 if (!BLO_read_data_is_undo(reader)) {
2456 id->recalc = 0;
2457 id->recalc_after_undo_push = 0;
2458 }
2459 else if ((reader->fd->skip_flags & BLO_READ_SKIP_UNDO_OLD_MAIN) == 0) {
2460 id->recalc = direct_link_id_restore_recalc(reader->fd, id, id_old, false);
2461 id->recalc_after_undo_push = 0;
2462 }
2463
2464 /* Link direct data of overrides. */
2465 if (id->override_library) {
2466 BLO_read_struct(reader, IDOverrideLibrary, &id->override_library);
2467 /* Work around file corruption on writing, see #86853. */
2468 if (id->override_library != nullptr) {
2469 BLO_read_struct_list(reader, IDOverrideLibraryProperty, &id->override_library->properties);
2470 for (IDOverrideLibraryProperty &op : id->override_library->properties) {
2472 }
2473 id->override_library->runtime = nullptr;
2474 }
2475 }
2476
2477 /* Handle 'private IDs'. */
2478 direct_link_id_embedded_id(reader, current_library, id, id_old);
2479}
2480
2482
2483/* -------------------------------------------------------------------- */
2486
2488
2489/* -------------------------------------------------------------------- */
2492
2494{
2495 key->uidgen = 1;
2496 for (KeyBlock &block : key->block) {
2497 block.uid = key->uidgen++;
2498 }
2499}
2500
2502
2503/* -------------------------------------------------------------------- */
2506
2507#ifdef USE_SETSCENE_CHECK
2511static bool scene_validate_setscene__liblink(Scene *sce, const int totscene)
2512{
2513 Scene *sce_iter;
2514 int a;
2515
2516 if (sce->set == nullptr) {
2517 return true;
2518 }
2519
2520 for (a = 0, sce_iter = sce; sce_iter->set; sce_iter = sce_iter->set, a++) {
2521 /* This runs per library (before each libraries #Main has been joined),
2522 * so we can't step into other libraries since `totscene` is only for this library.
2523 *
2524 * Also, other libraries may not have been linked yet, while we could check for
2525 * #ID_Readfile_Data::Tags.needs_linking the library pointer check is sufficient. */
2526 if (sce->id.lib != sce_iter->id.lib) {
2527 return true;
2528 }
2529 if (sce_iter->flag & SCE_READFILE_LIBLINK_NEED_SETSCENE_CHECK) {
2530 return true;
2531 }
2532
2533 if (a > totscene) {
2534 sce->set = nullptr;
2535 return false;
2536 }
2537 }
2538
2539 return true;
2540}
2541#endif
2542
2544{
2545#ifdef USE_SETSCENE_CHECK
2546 const int totscene = bmain->scenes.count();
2547 for (Scene &sce : bmain->scenes) {
2550 if (!scene_validate_setscene__liblink(&sce, totscene)) {
2551 CLOG_WARN(&LOG, "Found cyclic background scene when linking %s", sce.id.name + 2);
2552 }
2553 }
2554 }
2555#else
2556 UNUSED_VARS(bmain, totscene);
2557#endif
2558}
2559
2560#undef USE_SETSCENE_CHECK
2561
2563
2564/* -------------------------------------------------------------------- */
2567
2569{
2570 if (lib->runtime->filedata) {
2571 /* External libraries have no version info. */
2574 "Packed Archive libraries should never own their filedata");
2575 if (lib->runtime->is_filedata_owner) {
2577 }
2578
2579 lib->runtime->filedata = nullptr;
2580 }
2581 lib->runtime->is_filedata_owner = false;
2582}
2583
2584/* Add a Main (and optionally create a matching Library ID), for the given filepath.
2585 *
2586 * - If `lib` is `nullptr`, create a new Library ID, otherwise only create a new Main for the given
2587 * library.
2588 * - If a new library is created, and `bheadlib` is not `nullptr`, add a remapping from this
2589 * `BHead::old` value to the newly created Library.
2590 * - `reference_lib` is the 'archive parent' of an archive (packed) library, can be null and will
2591 * be ignored otherwise. */
2593 const BHead *bheadlib,
2594 Library *lib,
2595 Library *reference_lib,
2596 const char *lib_filepath,
2597 char (&filepath_abs)[FILE_MAX],
2598 const bool is_packed_library,
2599 const bool is_external_lib)
2600{
2601 Main *bmain = BKE_main_new();
2602 fd->bmain->split_mains->add_new(bmain);
2603 bmain->split_mains = fd->bmain->split_mains;
2604
2605 if (!lib) {
2606 /* Add library data-block itself to 'main' Main, since libraries are **never** linked data.
2607 * Fixes bug where you could end with all ID_LI data-blocks having the same name... */
2608 lib = BKE_id_new<Library>(fd->bmain,
2609 reference_lib ? BKE_id_name(reference_lib->id) :
2610 BLI_path_basename(lib_filepath));
2611 /* When a lib ID is created based on data from a BHead, ensure that this library BHead's old
2612 * pointer value is remapped to the actual new library. */
2613 if (bheadlib) {
2614 change_ID_pointer_to_real_ID_pointer(fd, bheadlib->old, lib);
2615 }
2616
2617 /* Important, consistency with main ID reading code from read_libblock(). */
2618 lib->id.us = ID_FAKE_USERS(lib);
2619
2620 /* Matches direct_link_library(). */
2621 id_us_ensure_real(&lib->id);
2622
2623 STRNCPY(lib->filepath, lib_filepath);
2624 STRNCPY(lib->runtime->filepath_abs, filepath_abs);
2625
2626 if (is_external_lib) {
2628 }
2629
2630 if (is_packed_library) {
2631 /* FIXME: This logic is very similar to the code in BKE_library dealing with archived
2632 * libraries (e.g. #add_archive_library). Might be good to try to factorize it. */
2633 lib->archive_parent_library = reference_lib;
2634 constexpr uint16_t copy_flag = ~LIBRARY_FLAG_IS_ARCHIVE;
2635 lib->flag = (reference_lib->flag & copy_flag) | LIBRARY_FLAG_IS_ARCHIVE;
2636
2637 lib->runtime->parent = reference_lib->runtime->parent;
2638 /* Only copy a subset of the reference library tags. E.g. an archive library should never be
2639 * considered as writable, so never copy #LIBRARY_ASSET_FILE_WRITABLE. This may need further
2640 * tweaking still. */
2643 lib->runtime->tag = reference_lib->runtime->tag & copy_tag;
2644
2645 /* The filedata of a packed archive library should always be the one of the blendfile which
2646 * defines the library ID and packs its linked IDs. */
2647 lib->runtime->filedata = fd;
2648 lib->runtime->is_filedata_owner = false;
2649
2650 reference_lib->runtime->archived_libraries.append(lib);
2651 }
2652 }
2653 else {
2654 if (is_packed_library) {
2656 BLI_assert(lib->archive_parent_library == reference_lib);
2657
2658 /* If there is already an archive library in the new set of Mains, but not a 'libmain' for it
2659 * yet, it is the first time that this archive library is effectively used to own a packed
2660 * ID. Since regular libraries have their list of owned archive libs cleared when reused on
2661 * undo, it means that this archive library should yet be listed in its regular owner one,
2662 * and needs to be added there. See also #read_undo_move_libmain_data. */
2663 BLI_assert(!reference_lib->runtime->archived_libraries.contains(lib));
2664 reference_lib->runtime->archived_libraries.append(lib);
2665
2666 BLI_assert(lib->runtime->filedata == nullptr);
2667 lib->runtime->filedata = fd;
2668 lib->runtime->is_filedata_owner = false;
2669 }
2670 else {
2671 /* Should never happen currently. */
2673 }
2674 }
2675
2676 bmain->curlib = lib;
2677 return bmain;
2678}
2679
2680static bool library_filepath_is_valid(const Library *lib)
2681{
2682 /* Corrupt file might not be null terminated. */
2683 return std::memchr(lib->filepath, '\0', sizeof(lib->filepath)) != nullptr;
2684}
2685
2687{
2688 if (!library_filepath_is_valid(lib)) [[unlikely]] {
2689 blo_readfile_invalidate(fd, main, "Corrupt .blend file, invalid library file path");
2690 lib->filepath[0] = '\0';
2691 /* Continue processing to avoid invalid pointers in this datablock that
2692 * would be read by later code. */
2693 }
2694
2695 /* Make sure we have full path in lib->runtime->filepath_abs */
2696 /* NOTE: Since existing libraries are searched by their absolute path, this has to be generated
2697 * before the lookup below. Otherwise, in case the stored absolute filepath is not 'correct' (may
2698 * be empty, or have been stored in a different 'relative path context'), the comparison below
2699 * will always fail, leading to creating duplicates IDs of a same library. */
2700 /* TODO: May be worth checking whether comparison below could use `lib->filepath` instead? */
2701 STRNCPY(lib->runtime->filepath_abs, lib->filepath);
2704
2705 /* check if the library was already read */
2706 for (Main *newmain : *fd->bmain->split_mains) {
2707 if (!newmain->curlib) {
2708 continue;
2709 }
2711 /* Archive library should never be used to link new data, and there can be many such
2712 * archive libraries for a same 'real' blendfile one. */
2713 continue;
2714 }
2715 if (BLI_path_cmp(newmain->curlib->runtime->filepath_abs, lib->runtime->filepath_abs) == 0) {
2718 RPT_("Library '%s', '%s' had multiple instances, save and reload!"),
2719 lib->filepath,
2720 lib->runtime->filepath_abs);
2721
2723 // change_link_placeholder_to_real_ID_pointer_fd(fd, lib, newmain->curlib);
2724
2725 BLI_remlink(&main->libraries, lib);
2726 MEM_delete(lib);
2727
2728 /* Now, since Blender always expect **last** Main pointer from fd->bmain->split_mains
2729 * to be the active library Main pointer, where to add all non-library data-blocks found in
2730 * file next, we have to switch that 'dupli' found Main to latest position in the list!
2731 * Otherwise, you get weird disappearing linked data on a rather inconsistent basis.
2732 * See also #53977 for reproducible case. */
2733 /* Note: the change in order in `fd->bmain->split_mains` should not be an issue here, and we
2734 * return immediately. */
2735 fd->bmain->split_mains->remove_contained(newmain);
2736 fd->bmain->split_mains->add_new(newmain);
2737 BLI_assert((*fd->bmain->split_mains)[0] == fd->bmain);
2738
2739 return;
2740 }
2741 }
2742
2743 /* There are currently some cases where archive libraries have no 'real library' parent on file
2744 * opening (see e.g. #150275, #150147, #150375).
2745 *
2746 * This code detects such issues, reports them, and fixes them as best as possible by
2747 * re-generating an empty real parent library. */
2748 if (lib->flag & LIBRARY_FLAG_IS_ARCHIVE) {
2749 if (lib->archive_parent_library == nullptr) [[unlikely]] {
2751 fd, main, "Corrupt .blend file, archive library without parent library");
2752 /* Continue processing to avoid invalid pointers in this datablock that
2753 * would be read by later code. */
2754 }
2755
2756 Library *parent_lib = static_cast<Library *>(
2757 newlibadr(fd, &lib->id, false, lib->archive_parent_library));
2758
2759 if (!parent_lib) {
2761 RPT_ERROR,
2762 RPT_("Library '%s' ('%s') is an archive storage for packed data, but has "
2763 "no real library parent."),
2764 lib->filepath,
2765 lib->runtime->filepath_abs);
2766
2767 Main *parent_lib_bmain = blo_add_main_for_library(fd,
2768 nullptr,
2769 nullptr,
2770 nullptr,
2771 lib->filepath,
2772 lib->runtime->filepath_abs,
2773 false,
2774 (lib->flag & LIBRARY_FLAG_IS_EXTERNAL) !=
2775 0);
2776 parent_lib = parent_lib_bmain->curlib;
2777 BLI_assert(parent_lib);
2778 oldnewmap_lib_insert(fd, lib->archive_parent_library, &parent_lib->id, ID_LI);
2779 }
2780 }
2781
2782 // printf("direct_link_library: filepath %s\n", lib->filepath);
2783 // printf("direct_link_library: filepath_abs %s\n", lib->runtime->filepath_abs);
2784
2785 BlendDataReader reader = {fd};
2786 BKE_packedfile_blend_read(&reader, &lib->packedfile, lib->filepath);
2787
2788 /* TODO: Replace most of this code by a call to #blo_add_main_for_library(). */
2789 /* new main */
2790 Main *newmain = BKE_main_new();
2791 fd->bmain->split_mains->add_new(newmain);
2792 newmain->split_mains = fd->bmain->split_mains;
2793 newmain->curlib = lib;
2794
2795 if (lib->flag & LIBRARY_FLAG_IS_ARCHIVE) {
2796 /* Archive libraries contains only embedded linked IDs, which by definition have the same
2797 * fileversion as the blendfile that contains them. */
2798 lib->runtime->versionfile = newmain->versionfile = fd->bmain->versionfile;
2800
2801 /* The filedata of a packed archive library should always be the one of the blendfile which
2802 * defines the library ID and packs its linked IDs. */
2803 lib->runtime->filedata = fd;
2804 lib->runtime->is_filedata_owner = false;
2805 }
2806
2807 lib->runtime->parent = nullptr;
2808
2809 id_us_ensure_real(&lib->id);
2810
2811 /* Should always be null, Library IDs in Blender are always local. */
2812 lib->id.lib = nullptr;
2813}
2814
2815/* Always call this once you have loaded new library data to set the relative paths correctly
2816 * in relation to the blend file. */
2817static void fix_relpaths_library(const char *basepath, Main *main)
2818{
2819 /* #BLO_read_from_memory uses a blank file-path. */
2820 if (basepath == nullptr || basepath[0] == '\0') {
2821 for (Library &lib : main->libraries) {
2822 /* when loading a linked lib into a file which has not been saved,
2823 * there is nothing we can be relative to, so instead we need to make
2824 * it absolute. This can happen when appending an object with a relative
2825 * link into an unsaved blend file. See #27405.
2826 * The remap relative option will make it relative again on save - campbell */
2827 if (BLI_path_is_rel(lib.filepath)) {
2829 }
2830 }
2831 }
2832 else {
2833 for (Library &lib : main->libraries) {
2834 /* Libraries store both relative and abs paths, recreate relative paths,
2835 * relative to the blend file since indirectly linked libraries will be
2836 * relative to their direct linked library. */
2837 if (BLI_path_is_rel(lib.filepath)) { /* if this is relative to begin with? */
2839 BLI_path_rel(lib.filepath, basepath);
2840 }
2841 }
2842 }
2843}
2844
2846
2847/* -------------------------------------------------------------------- */
2850
2851static ID *create_placeholder(Main *mainvar,
2852 const short idcode,
2853 const char *idname,
2854 const int tag,
2855 const bool was_liboverride)
2856{
2857 ListBaseT<ID> *lb = which_libbase(mainvar, idcode);
2858 ID *ph_id = BKE_libblock_alloc_notest(idcode);
2859 /* Important to immediately set the library, as API like `BKE_libblock_init_empty` might rely on
2860 * it e.g. to allocate an embedded ID in the correct library too. */
2861 ph_id->lib = mainvar->curlib;
2863
2864 *(reinterpret_cast<short *>(ph_id->name)) = idcode;
2865 BLI_strncpy(ph_id->name + 2, idname, sizeof(ph_id->name) - 2);
2867 ph_id->tag = tag | ID_TAG_MISSING;
2868 ph_id->us = ID_FAKE_USERS(ph_id);
2869 ph_id->icon_id = 0;
2870
2871 if (was_liboverride) {
2872 /* 'Abuse' `ID_TAG_LIBOVERRIDE_NEED_RESYNC` to mark that placeholder missing linked ID as
2873 * being a liboverride.
2874 *
2875 * This will be used by the liboverride resync process, see #lib_override_library_resync. */
2877 }
2878
2879 BLI_addtail(lb, ph_id);
2880 id_sort_by_name(lb, ph_id, nullptr);
2881
2882 if (mainvar->id_map != nullptr) {
2883 BKE_main_idmap_insert_id(mainvar->id_map, ph_id);
2884 }
2885
2886 if ((tag & ID_TAG_TEMP_MAIN) == 0) {
2888 }
2889
2890 return ph_id;
2891}
2892
2894{
2895 /* Placeholder ObData IDs won't have any material, we have to update their objects for that,
2896 * otherwise the inconsistency between both will lead to crashes (especially in Eevee?). */
2897 for (Object &ob : bmain->objects) {
2898 ID *obdata = ob.data;
2899 if (obdata != nullptr && obdata->tag & ID_TAG_MISSING) {
2900 BKE_object_materials_sync_length(bmain, &ob, obdata);
2901 }
2902 }
2903}
2904
2905static bool direct_link_id(FileData *fd,
2906 Main *main,
2907 const eID_Tag id_tag,
2908 const ID_Readfile_Data::Tags id_read_tags,
2909 ID *id,
2910 ID *id_old)
2911{
2912 BlendDataReader reader = {fd};
2913 /* Sharing is only allowed within individual data-blocks currently. The clearing is done
2914 * explicitly here, in case the `reader` is used by multiple IDs in the future. */
2915 reader.shared_data_by_stored_address.clear();
2916
2917 /* Read part of datablock that is common between real and embedded datablocks. */
2918 direct_link_id_common(&reader, main->curlib, id, id_old, id_tag, id_read_tags);
2919
2921 /* For placeholder we only need to set the tag, no further data to read. */
2922 id->tag = id_tag;
2923 return true;
2924 }
2925
2926 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id);
2927 if (id_type->blend_read_data != nullptr) {
2928 id_type->blend_read_data(&reader, id);
2929 }
2930
2931 /* XXX Very weakly handled currently, see comment in read_libblock() before trying to
2932 * use it for anything new. */
2933 bool success = true;
2934
2935 switch (id->id_type()) {
2936 case ID_SCR:
2937 success = BKE_screen_blend_read_data(&reader, id_cast<bScreen *>(id));
2938 break;
2939 case ID_LI:
2940 direct_link_library(fd, id_cast<Library *>(id), main);
2941 break;
2942 default:
2943 /* Do nothing. Handled by IDTypeInfo callback. */
2944 break;
2945 }
2946
2947 /* try to restore (when undoing) or clear ID's cache pointers. */
2948 if (id_type->foreach_cache != nullptr) {
2951 }
2952
2953 return success;
2954}
2955
2956/* Read all data associated with a datablock into datamap. */
2958 BHead *bhead,
2959 const char *allocname,
2960 const int id_type_index)
2961{
2962 bhead = blo_bhead_next(fd, bhead);
2963
2964 while (bhead && bhead->code == BLO_CODE_DATA) {
2965 int64_t alloc_len = 0;
2966 void *data = read_struct(fd, bhead, allocname, id_type_index, &alloc_len);
2967 if (data) {
2968 const bool is_new = oldnewmap_insert(fd->datamap, bhead->old, data, 0, alloc_len);
2969 if (!is_new) {
2970 CLOG_ERROR(&LOG,
2971 "Blendfile corruption: Invalid, or multiple `bhead` with same old address "
2972 "value (%p) for a given ID.",
2973 bhead->old);
2974 }
2975 }
2976
2977 bhead = blo_bhead_next(fd, bhead);
2978 }
2979
2980 return bhead;
2981}
2982
2983/* Verify if the datablock and all associated data is identical. */
2985{
2986 /* Test ID itself. */
2987 if (bhead->len && !BHEADN_FROM_BHEAD(bhead)->is_memchunk_identical) {
2988 return false;
2989 }
2990
2991 /* Test any other data that is part of ID (logic must match read_data_into_datamap). */
2992 bhead = blo_bhead_next(fd, bhead);
2993
2994 while (bhead && bhead->code == BLO_CODE_DATA) {
2995 if (bhead->len && !BHEADN_FROM_BHEAD(bhead)->is_memchunk_identical) {
2996 return false;
2997 }
2998
2999 bhead = blo_bhead_next(fd, bhead);
3000 }
3001
3002 return true;
3003}
3004
3005/* Mark all 'no-undo' IDs in the old Main, these (and their dependencies for linked ones) need to
3006 * be unconditionally moved into the new Main. */
3008{
3009 Main *old_bmain = fd->old_bmain;
3010 BLI_assert(old_bmain != nullptr);
3011 BLI_assert(old_bmain->curlib == nullptr);
3012 BLI_assert(old_bmain->split_mains);
3013
3014 /* First find all 'no undo' IDs themselves. */
3015 std::deque<ID *> no_undo_ids;
3016 for (Main *old_bmain_iter : *old_bmain->split_mains) {
3017 MainListsArray lbarray = BKE_main_lists_get(*old_bmain_iter);
3018 int i = lbarray.size();
3019 while (i--) {
3020 if (BLI_listbase_is_empty(lbarray[i])) {
3021 continue;
3022 }
3023
3024 ID *id = lbarray[i]->first();
3025 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id);
3026 if ((id_type->flags & IDTYPE_FLAGS_NO_MEMFILE_UNDO) == 0) {
3027 continue;
3028 }
3029
3030 if (old_bmain_iter->curlib) {
3032 true;
3033 }
3034 ID *id_iter;
3035 FOREACH_MAIN_LISTBASE_ID_BEGIN (lbarray[i], id_iter) {
3037 no_undo_ids.push_back(id_iter);
3038 }
3040 }
3041 }
3042
3043 /* Now find all dependencies of the 'no undo' IDs. */
3044 while (!no_undo_ids.empty()) {
3045 ID *id_iter = no_undo_ids.front();
3046 no_undo_ids.pop_front();
3048 nullptr,
3049 id_iter,
3050 [&no_undo_ids](LibraryIDLinkCallbackData *cb_data) -> int {
3051 ID *id_owner = cb_data->owner_id;
3052 ID *id = *cb_data->id_pointer;
3053
3055 UNUSED_VARS_NDEBUG(id_owner);
3057 return IDWALK_RET_NOP;
3058 }
3059
3060 if (cb_data->cb_flag &
3062 {
3063 return IDWALK_RET_NOP;
3064 }
3065
3067 no_undo_ids.push_back(id);
3068 if (ID_IS_LINKED(id)) {
3070 }
3071 return IDWALK_RET_NOP;
3072 },
3073 nullptr,
3075 }
3076}
3077
3078/* Re-use the whole 'noundo' local IDs by moving them from old to new main. Linked ones are handled
3079 * separately together with their libraries.
3080 *
3081 * NOTE: While in theory Library IDs (and their related linked IDs) are also 'noundo' data, in
3082 * practice they need to be handled separately, to ensure that their order in the new bmain list
3083 * matches the one from the read blend-file. Reading linked 'placeholder' entries in a memfile
3084 * relies on current library being the last item in the new main list. */
3086{
3087 Main *new_bmain = fd->bmain;
3088 Main *old_bmain = fd->old_bmain;
3089
3090 BLI_assert(old_bmain->curlib == nullptr);
3091 BLI_assert(old_bmain->split_mains);
3092
3093 MainListsArray lbarray = BKE_main_lists_get(*old_bmain);
3094 int i = lbarray.size();
3095 while (i--) {
3096 if (BLI_listbase_is_empty(lbarray[i])) {
3097 continue;
3098 }
3099
3100 /* Only move 'noundo' local IDs. */
3101 ID *id = lbarray[i]->first();
3102 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id);
3103 if ((id_type->flags & IDTYPE_FLAGS_NO_MEMFILE_UNDO) == 0) {
3104 continue;
3105 }
3106
3107 ListBaseT<ID> *new_lb = which_libbase(new_bmain, id_type->id_code);
3108 BLI_assert(new_lb->is_empty());
3109 BLI_movelisttolist(new_lb, lbarray[i]);
3110
3111 /* Update mappings accordingly. */
3112 for (ID &id_iter : *new_lb) {
3114 id_iter.tag |= ID_TAG_UNDO_OLD_ID_REUSED_NOUNDO;
3115 }
3116 }
3117}
3118
3119static void read_undo_move_libmain_data(FileData *fd, Main *libmain, BHead *bhead)
3120{
3121 Main *old_main = fd->old_bmain;
3122 Main *new_main = fd->bmain;
3123 Library *curlib = libmain->curlib;
3124
3125 /* Archived libraries should never be processed here. */
3126 BLI_assert((curlib->flag & LIBRARY_FLAG_IS_ARCHIVE) == 0);
3127
3128 /* NOTE: This may change the order of items in `old_main->split_mains`. So calling code cannot
3129 * directly iterate over it. */
3130 old_main->split_mains->remove_contained(libmain);
3131 BLI_remlink_safe(&old_main->libraries, curlib);
3132 new_main->split_mains->add_new(libmain);
3133 BLI_addtail(&new_main->libraries, curlib);
3134
3135 /* Remove all references to the archive libraries owned by this 'regular' library. The
3136 * archive ones are only moved over into the new Main if some of their IDs are actually
3137 * re-used. Otherwise they are deleted, so the 'regular' library cannot keep references to
3138 * them at this point. See also #blo_add_main_for_library. */
3139 curlib->runtime->archived_libraries = {};
3140
3143 if (bhead != nullptr) {
3144 oldnewmap_lib_insert(fd, bhead->old, &curlib->id, curlib->id.id_type());
3145 }
3146
3148 ID *id_iter;
3149 FOREACH_MAIN_ID_BEGIN (libmain, id_iter) {
3150 /* There should never be any packed ID in a regular library. */
3151 BLI_assert(!ID_IS_PACKED(id_iter));
3153 /* Presume that all linked IDs moved from old to new Main are unused. The ones actually still
3154 * used in the loaded new Main will be removed from this set when their BHead is processed (see
3155 * #read_libblock_undo_restore_linked).
3156 *
3157 * 'No undo' ID types and their dependencies are never considered unused here, by definition.
3158 */
3159 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id_iter);
3160 if (id_type->flags & IDTYPE_FLAGS_NO_MEMFILE_UNDO) {
3162 }
3164 curlib->runtime->unused_ids_on_undo.add_new(id_iter);
3165 }
3166 }
3168}
3169
3170/* For undo, restore matching library datablock from the old main. */
3172 const ID *id,
3173 ID *id_old,
3174 BHead *bhead)
3175{
3176 /* In undo case, most libraries and linked data should be kept as is from previous state
3177 * (see BLO_read_from_memfile).
3178 * However, some needed by the snapshot being read may have been removed in previous one,
3179 * and would go missing.
3180 * This leads e.g. to disappearing objects in some undo/redo case, see #34446.
3181 * That means we have to carefully check whether current lib or
3182 * libdata already exits in old main, if it does we merely copy it over into new main area,
3183 * otherwise we have to do a full read of that bhead... */
3184 CLOG_DEBUG(&LOG_UNDO, "UNDO: restore library %s", id->name);
3185
3186 if (id_old == nullptr) {
3187 CLOG_DEBUG(&LOG_UNDO, " -> NO match");
3188 return false;
3189 }
3190
3191 /* Skip `oldmain` itself. */
3192 /* NOTE: Only one item is removed from `old_main->split_mains`, so it is safe to iterate directly
3193 * on it here. The fact that the order of the other mains contained in this split_mains may be
3194 * modified should not be an issue currently. */
3195 for (Main *libmain : fd->old_bmain->split_mains->as_span().drop_front(1)) {
3196 if (&libmain->curlib->id == id_old) {
3197 BLI_assert(libmain->curlib);
3198 BLI_assert((libmain->curlib->flag & LIBRARY_FLAG_IS_ARCHIVE) == 0);
3200 " compare with %s -> match (existing libpath: %s)",
3201 libmain->curlib->id.name,
3202 libmain->curlib->runtime->filepath_abs);
3203
3204 /* In case of a library, we need to re-add its main to fd->bmain->split_mains,
3205 * because if we have later a missing ID_LINK_PLACEHOLDER,
3206 * we need to get the correct lib it is linked to!
3207 * Order is crucial, we cannot bulk-add it in BLO_read_from_memfile()
3208 * like it used to be. */
3209 read_undo_move_libmain_data(fd, libmain, bhead);
3211 BLI_assert(fd->old_bmain->split_mains->size() >= 1);
3212 BLI_assert((*fd->old_bmain->split_mains)[0] == fd->old_bmain);
3213 return true;
3214 }
3215 }
3216
3217 return false;
3218}
3219
3220/* Libraries containing 'never undo' data (e.g. Brushes) must always be kept across undo steps,
3221 * even if they did not exist in the loaded undo step. */
3223{
3224 Main *old_bmain = fd->old_bmain;
3225 BLI_assert(old_bmain != nullptr);
3226 BLI_assert(old_bmain->curlib == nullptr);
3227 BLI_assert(old_bmain->split_mains);
3228 /* Cannot iterate directly over `old_main->split_mains`, as this is likely going to remove some
3229 * of its items. */
3230 Vector<Main *> old_bmain_split_mains = {old_bmain->split_mains->as_span().drop_front(1)};
3231 for (Main *lib_bmain : old_bmain_split_mains) {
3232 BLI_assert(lib_bmain->curlib);
3233 if (BLO_readfile_id_runtime_tags(lib_bmain->curlib->id).used_by_no_undo_id) {
3234 read_undo_move_libmain_data(fd, lib_bmain, nullptr);
3235 }
3236 }
3237}
3238
3239/* Once all ID BHeads have been read, remove the regular linked IDs that have been moved from the
3240 * old to the new Main, but are actually not used in the new one. */
3242{
3243 /* When writing undo memfile data, all regular linked IDs are written as placeholder (reference),
3244 * even if unused.
3245 *
3246 * This ensures that here, all regular linked IDs in the loaded undo step, that were still
3247 * present in the old Main libraries, have been accounted for, see #read_undo_move_libmain_data()
3248 * and #read_libblock_undo_restore_linked().
3249 *
3250 * So the libraries that remain in the old Main are not needed by newly loaded undo step, and can
3251 * be discarded with the rest of the old Main data.
3252 *
3253 * The IDs still listed in the `unused_ids_on_undo` set of libraries that were re-used (moved
3254 * from old to new Main) are also not needed in the new Main, and can be moved back into the old
3255 * Main (as mere local data, to simplify things).
3256 */
3257
3258 Main *old_bmain = fd->old_bmain;
3259 BLI_assert(old_bmain != nullptr);
3260 BLI_assert(old_bmain->curlib == nullptr);
3261 BLI_assert(old_bmain->split_mains);
3262
3263 Main *new_bmain = fd->bmain;
3264 BLI_assert(new_bmain != nullptr);
3265 BLI_assert(new_bmain->curlib == nullptr);
3266 BLI_assert(new_bmain->split_mains);
3267
3268 for (Main *lib_bmain : new_bmain->split_mains->as_span().drop_front(1)) {
3269 BLI_assert(lib_bmain->curlib);
3270 if (lib_bmain->curlib->flag & LIBRARY_FLAG_IS_ARCHIVE) {
3271 /* Archived libraries are handled differently than regular libraries, and can be ignored
3272 * here. */
3273 continue;
3274 }
3275 for (ID *unused_id : lib_bmain->curlib->runtime->unused_ids_on_undo) {
3276 BLI_assert(ID_IS_LINKED(unused_id) && !ID_IS_PACKED(unused_id));
3277
3278#ifndef NDEBUG
3279 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(unused_id);
3281#endif
3282 CLOG_DEBUG(&LOG_UNDO, "Unused linked ID '%s' will be discarded", unused_id->name);
3283
3284 const short idcode = unused_id->id_type();
3285 ListBaseT<ID> *new_lb = which_libbase(lib_bmain, idcode);
3286 ListBaseT<ID> *old_lb = which_libbase(old_bmain, idcode);
3287 BLI_remlink(new_lb, unused_id);
3288 unused_id->lib = nullptr;
3290 BLI_addtail(old_lb, unused_id);
3291 /* NOTE: There should be no need to update ID pointers mapping (`fd->libmap`) here, as
3292 * these IDs should not be in there in the first place. */
3293 }
3294 lib_bmain->curlib->runtime->unused_ids_on_undo.clear();
3295 }
3296}
3297
3298static ID *library_id_is_yet_read(FileData *fd, Main *mainvar, BHead *bhead);
3299
3300/* For undo, restore existing linked datablock from the old main.
3301 *
3302 * Note that IDs from existing libs have already been moved into the new main when their (local)
3303 * ID_LI library ID was handled by #read_libblock_undo_restore_library, so this function has very
3304 * little to do. */
3306 FileData *fd, Main *libmain, const ID *id, ID **r_id_old, BHead *bhead)
3307{
3308 CLOG_DEBUG(&LOG_UNDO, "UNDO: restore linked datablock %s", id->name);
3309
3310 if (*r_id_old == nullptr) {
3311 /* If the linked ID had to be re-read at some point, its session_uid may not be the same as
3312 * its reference stored in the memfile anymore. Do a search by name then. */
3313 *r_id_old = library_id_is_yet_read(fd, libmain, bhead);
3314
3315 if (*r_id_old == nullptr) {
3317 " from %s (%s): NOT found",
3318 libmain->curlib ? libmain->curlib->id.name : "<nullptr>",
3319 libmain->curlib ? libmain->curlib->filepath : "<nullptr>");
3320 return false;
3321 }
3322
3324 " from %s (%s): found by name",
3325 libmain->curlib ? libmain->curlib->id.name : "<nullptr>",
3326 libmain->curlib ? libmain->curlib->filepath : "<nullptr>");
3327 /* The Library ID 'owning' this linked ID should already have been moved to new main by a call
3328 * to #read_libblock_undo_restore_library. */
3329 BLI_assert(*r_id_old == static_cast<ID *>(BKE_main_idmap_lookup_uid(
3330 fd->new_idmap_uid, (*r_id_old)->session_uid)));
3331 }
3332 else {
3334 " from %s (%s): found by session_uid",
3335 libmain->curlib ? libmain->curlib->id.name : "<nullptr>",
3336 libmain->curlib ? libmain->curlib->filepath : "<nullptr>");
3337 /* The Library ID 'owning' this linked ID should already have been moved to new main by a call
3338 * to #read_libblock_undo_restore_library. */
3339 BLI_assert(*r_id_old ==
3340 static_cast<ID *>(BKE_main_idmap_lookup_uid(fd->new_idmap_uid, id->session_uid)));
3341 }
3342
3343 oldnewmap_lib_insert(fd, bhead->old, *r_id_old, (*r_id_old)->id_type());
3344 /* This old linked ID is still being used. */
3345 libmain->curlib->runtime->unused_ids_on_undo.remove(*r_id_old);
3346
3347 /* No need to do anything else for ID_LINK_PLACEHOLDER, it's assumed
3348 * already present in its lib's main. */
3349 return true;
3350}
3351
3352/* For undo, restore unchanged local datablock from old main. */
3354 FileData *fd, Main *main, const ID * /*id*/, ID *id_old, BHead *bhead, const eID_Tag id_tag)
3355{
3357 BLI_assert(id_old != nullptr);
3358
3359 /* Do not add ID_TAG_NEW here, this should not be needed/used in undo case anyway (as
3360 * this is only for do_version-like code), but for sake of consistency, and also because
3361 * it will tell us which ID is re-used from old Main, and which one is actually newly read. */
3362 /* Also do not set #ID_Readfile_Data::Tags.needs_linking, this ID will never be re-liblinked,
3363 * hence that tag will never be cleared, leading to critical issue in link/append code. */
3364 /* Some tags need to be preserved here. */
3366 (id_old->tag & ID_TAG_KEEP_ON_UNDO);
3367 id_old->lib = main->curlib;
3368 id_old->us = ID_FAKE_USERS(id_old);
3369 /* Do not reset id->icon_id here, memory allocated for it remains valid. */
3370 /* Needed because .blend may have been saved with crap value here... */
3371 id_old->newid = nullptr;
3372 id_old->orig_id = nullptr;
3373
3374 const short idcode = id_old->id_type();
3375 Main *old_bmain = fd->old_bmain;
3376 ListBaseT<ID> *old_lb = which_libbase(old_bmain, idcode);
3377 ListBaseT<ID> *new_lb = which_libbase(main, idcode);
3378 BLI_remlink(old_lb, id_old);
3379 BLI_addtail(new_lb, id_old);
3380
3381 /* Recalc flags, mostly these just remain as they are. */
3383 id_old->recalc_after_undo_push = 0;
3384
3385 /* Insert into library map for lookup by newly read datablocks (with pointer value bhead->old).
3386 * Note that existing datablocks in memory (which pointer value would be id_old) are not
3387 * remapped, so no need to store this info here. */
3388 oldnewmap_lib_insert(fd, bhead->old, id_old, bhead->code);
3389
3391
3392 if (id_old->id_type() == ID_OB) {
3393 Object *ob = id_cast<Object *>(id_old);
3394 /* For undo we stay in object mode during undo presses, so keep editmode disabled for re-used
3395 * data-blocks too. */
3396 ob->mode &= ~OB_MODE_EDIT;
3397 }
3398 if (id_old->id_type() == ID_LI) {
3399 Library *lib = reinterpret_cast<Library *>(id_old);
3400 if (lib->flag & LIBRARY_FLAG_IS_ARCHIVE) {
3401 BLI_assert(lib->runtime->filedata == nullptr);
3403 /* The 'normal' parent of this archive library should already have been moved into the new
3404 * Main. */
3407 &lib->archive_parent_library->id);
3408 /* The archive library ID has been moved in the new Main, but not its own old split main, as
3409 * these packed IDs should be handled like local ones in undo case. So a new split libmain
3410 * needs to be created to contain its packed IDs. */
3411 const bool is_external_lib = lib->flag & LIBRARY_FLAG_IS_EXTERNAL;
3413 nullptr,
3414 lib,
3416 lib->filepath,
3417 lib->runtime->filepath_abs,
3418 true,
3419 is_external_lib);
3420 }
3421 else {
3423 }
3424 }
3425}
3426
3427/* For undo, store changed datablock at old address. */
3429{
3430 /* During memfile undo, if an ID changed and we cannot directly re-use existing one from old
3431 * bmain, we do a full read of the new id from the memfile, and then fully swap its content
3432 * with the old id. This allows us to keep the same pointer even for modified data, which
3433 * helps reducing further detected changes by the depsgraph (since unchanged IDs remain fully
3434 * unchanged, even if they are using/pointing to a changed one). */
3436 BLI_assert(id_old != nullptr);
3437
3438 const short idcode = id->id_type();
3439
3440 Main *old_bmain = fd->old_bmain;
3441 ListBaseT<ID> *old_lb = which_libbase(old_bmain, idcode);
3442 ListBaseT<ID> *new_lb = which_libbase(main, idcode);
3443 BLI_remlink(old_lb, id_old);
3444 BLI_remlink(new_lb, id);
3445
3446 /* We do need remapping of internal pointers to the ID itself here.
3447 *
3448 * Passing a null #Main means that not all potential runtime data (like collections' parent
3449 * pointers etc.) will be up-to-date. However, this should not be a problem here, since these
3450 * data are re-generated later in file-read process anyway. */
3451 BKE_lib_id_swap_full(nullptr,
3452 id,
3453 id_old,
3454 true,
3458
3459 /* Special temporary usage of this pointer, necessary for the `undo_preserve` call after
3460 * lib-linking to restore some data that should never be affected by undo, e.g. the 3D cursor of
3461 * #Scene. */
3462 id_old->orig_id = id;
3465
3466 BLI_addtail(new_lb, id_old);
3467 BLI_addtail(old_lb, id);
3468
3469 /* In case a library has been re-read, it has added already its own split main to the new Main
3470 * (see #direct_link_library code).
3471 *
3472 * Since we are replacing it with the 'id_old' address, we need to update that Main::curlib
3473 * pointer accordingly.
3474 *
3475 * Note that:
3476 * - This code is only for undo, and on undo we do not re-read regular libraries, only archive
3477 * ones for packed data.
3478 * - The new split main should still be empty at this stage (this code and adding the split
3479 * Main in #direct_link_library are part of the same #read_libblock call).
3480 */
3481 if (id_old->id_type() == ID_LI) {
3482 Library *lib_old = id_cast<Library *>(id_old);
3483 Library *lib = id_cast<Library *>(id);
3485
3486 for (Main *bmain_iter : *fd->bmain->split_mains) {
3487 if (bmain_iter->curlib == lib) {
3488 BLI_assert(BKE_main_is_empty(bmain_iter));
3489 bmain_iter->curlib = lib_old;
3490 }
3491 }
3492 }
3493}
3494
3496 FileData *fd, Main *main, BHead *bhead, const eID_Tag id_tag, ID **r_id_old)
3497{
3498 BLI_assert(fd->old_idmap_uid != nullptr);
3499
3500 /* Get pointer to memory of new ID that we will be reading. */
3501 const ID *id = static_cast<const ID *>(peek_struct_undo(fd, bhead));
3502 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id);
3503
3504 const bool do_partial_undo = (fd->skip_flags & BLO_READ_SKIP_UNDO_OLD_MAIN) == 0;
3505#ifndef NDEBUG
3506 if (do_partial_undo && (bhead->code != ID_LINK_PLACEHOLDER) &&
3507 (blo_bhead_id_flag(fd, bhead) & ID_FLAG_LINKED_AND_PACKED) == 0)
3508 {
3509 /* This code should only ever be reached for local or packed data-blocks. */
3510 BLI_assert(main->curlib == nullptr);
3511 }
3512#endif
3513
3514 /* Find the 'current' existing ID we want to reuse instead of the one we
3515 * would read from the undo memfile. */
3516 ID *id_old = (fd->old_idmap_uid != nullptr) ?
3517 BKE_main_idmap_lookup_uid(fd->old_idmap_uid, id->session_uid) :
3518 nullptr;
3519
3520 if (bhead->code == ID_LI) {
3521 /* Restore library datablock, if possible.
3522 *
3523 * Never handle archive libraries and their packed IDs as normal ones. These are local data,
3524 * and need to be fully handled like local IDs. */
3525 if (id_old && (reinterpret_cast<Library *>(id_old)->flag & LIBRARY_FLAG_IS_ARCHIVE) == 0 &&
3526 read_libblock_undo_restore_library(fd, id, id_old, bhead))
3527 {
3528 return true;
3529 }
3530 }
3531 else if (bhead->code == ID_LINK_PLACEHOLDER) {
3532 /* Restore linked datablock. */
3533 if (read_libblock_undo_restore_linked(fd, main, id, &id_old, bhead)) {
3534 return true;
3535 }
3536 }
3537 else if (id_type->flags & IDTYPE_FLAGS_NO_MEMFILE_UNDO) {
3538 CLOG_DEBUG(
3539 &LOG_UNDO, "UNDO: skip restore datablock %s, 'NO_MEMFILE_UNDO' type of ID", id->name);
3540
3541 /* If that local noundo ID still exists currently, the call to
3542 * #read_undo_reuse_noundo_local_ids at the beginning of #blo_read_file_internal will already
3543 * have moved it into the new main, and populated accordingly the new_idmap_uid.
3544 *
3545 * If this is the case, it can also be remapped for newly read data. Otherwise, this is 'lost'
3546 * data that cannot be restored on undo, so no remapping should exist for it in the ID
3547 * oldnewmap. */
3548 if (id_old) {
3549 BLI_assert(id_old ==
3550 static_cast<ID *>(BKE_main_idmap_lookup_uid(fd->new_idmap_uid, id->session_uid)));
3551 oldnewmap_lib_insert(fd, bhead->old, id_old, bhead->code);
3552 }
3553 return true;
3554 }
3555
3556 if (!do_partial_undo) {
3558 "UNDO: read %s (uid %u) -> no partial undo, always read at new address",
3559 id->name,
3560 id->session_uid);
3561 return false;
3562 }
3563
3564 /* Restore local datablocks. */
3565 if (id_old != nullptr && read_libblock_is_identical(fd, bhead)) {
3566 /* Local datablock was unchanged, restore from the old main. */
3568 "UNDO: read %s (uid %u) -> keep identical data-block",
3569 id->name,
3570 id->session_uid);
3571
3572 read_libblock_undo_restore_identical(fd, main, id, id_old, bhead, id_tag);
3573
3574 *r_id_old = id_old;
3575 return true;
3576 }
3577 if (id_old != nullptr) {
3578 /* Local datablock was changed. Restore at the address of the old datablock. */
3580 "UNDO: read %s (uid %u) -> read to old existing address",
3581 id->name,
3582 id->session_uid);
3583 *r_id_old = id_old;
3584 return false;
3585 }
3586
3587 /* Local datablock does not exist in the undo step, so read from scratch. */
3588 CLOG_DEBUG(
3589 &LOG_UNDO, "UNDO: read %s (uid %u) -> read at new address", id->name, id->session_uid);
3590 return false;
3591}
3592
3593/* This routine reads a datablock and its direct data, and advances bhead to
3594 * the next datablock. For library linked datablocks, only a placeholder will
3595 * be generated, to be replaced in read_library_linked_ids.
3596 *
3597 * When reading for undo, libraries, linked datablocks and unchanged datablocks
3598 * will be restored from the old database. Only new or changed datablocks will
3599 * actually be read. */
3601 Main *main,
3602 BHead *bhead,
3603 eID_Tag id_tag,
3604 ID_Readfile_Data::Tags id_read_tags,
3605 const bool placeholder_set_indirect_extern,
3606 ID **r_id)
3607{
3608 const bool do_partial_undo = (fd->skip_flags & BLO_READ_SKIP_UNDO_OLD_MAIN) == 0;
3609
3610 auto process_packed_id = [&fd, &main](ID &id) -> void {
3611 if (!ID_IS_PACKED(&id)) {
3612 return;
3613 }
3615 BLI_assert(main->curlib);
3616 if ((id.lib->flag & LIBRARY_FLAG_IS_EXTERNAL) != 0) {
3617 /* External libraries should have a null deep hash. */
3618 BLI_assert(id.deep_hash == IDHash::get_null());
3619 }
3620 else {
3621 BLI_assert(id.deep_hash != IDHash::get_null());
3622 fd->id_by_deep_hash->add_new(id.deep_hash, &id);
3623 }
3624 };
3625
3626 /* First attempt to restore existing datablocks for undo.
3627 * When datablocks are changed but still exist, we restore them at the old
3628 * address and inherit recalc flags for the dependency graph. */
3629 ID *id_old = nullptr;
3630 if (fd->flags & FD_FLAGS_IS_MEMFILE) {
3631 if (read_libblock_undo_restore(fd, main, bhead, id_tag, &id_old)) {
3632 if (r_id) {
3633 *r_id = id_old;
3634 }
3635 if (id_old != nullptr) {
3636 if (main->id_map != nullptr) {
3637 BKE_main_idmap_insert_id(main->id_map, id_old);
3638 }
3639 process_packed_id(*id_old);
3640 }
3641
3642 return blo_bhead_next(fd, bhead);
3643 }
3644 }
3645
3646 /* Read libblock struct. */
3647 const int id_type_index = BKE_idtype_idcode_to_index(bhead->code);
3648#ifndef NDEBUG
3649 const char *blockname = nullptr;
3650#else
3651 /* Avoid looking up in the mapping for all read BHead, since this only contains the ID type name
3652 * in release builds. */
3653 const char *blockname = get_alloc_name(fd, bhead, nullptr, id_type_index);
3654#endif
3655 ID *id = read_id_struct(fd, bhead, blockname, id_type_index);
3656 if (id == nullptr) {
3657 if (r_id) {
3658 *r_id = nullptr;
3659 }
3660 return blo_bhead_next(fd, bhead);
3661 }
3662
3663 /* Determine ID type and add to main database list. */
3664 const short idcode = id->id_type();
3665 ListBaseT<ID> *lb = which_libbase(main, idcode);
3666 if (lb == nullptr) {
3667 /* Unknown ID type. */
3668 CLOG_WARN(&LOG, "Unknown id code '%c%c'", (idcode & 0xff), (idcode >> 8));
3669 MEM_delete(id);
3670 if (r_id) {
3671 *r_id = nullptr;
3672 }
3673 return blo_bhead_next(fd, bhead);
3674 }
3675
3676 /* NOTE: id must be added to the list before direct_link_id(), since
3677 * direct_link_library() may remove it from there in case of duplicates. */
3678 BLI_addtail(lb, id);
3679
3680 /* Insert into library map for lookup by newly read datablocks (with pointer value bhead->old).
3681 * Note that existing datablocks in memory (which pointer value would be id_old) are not remapped
3682 * remapped anymore, so no need to store this info here. */
3683 ID *id_target = (do_partial_undo && id_old != nullptr) ? id_old : id;
3684 oldnewmap_lib_insert(fd, bhead->old, id_target, bhead->code);
3685
3686 if (r_id) {
3687 *r_id = id_target;
3688 }
3689
3690 /* Set tag for new datablock to indicate lib linking and versioning needs
3691 * to be done still. */
3692 id_tag |= ID_TAG_NEW;
3693 id_read_tags.needs_linking = true;
3694
3695 if (bhead->code == ID_LINK_PLACEHOLDER) {
3696 /* Read placeholder for linked datablock. */
3697 id_read_tags.is_link_placeholder = true;
3698
3699 if (placeholder_set_indirect_extern) {
3700 if (id->flag & ID_FLAG_INDIRECT_WEAK_LINK) {
3701 id_tag |= ID_TAG_INDIRECT;
3702 }
3703 else {
3704 id_tag |= ID_TAG_EXTERN;
3705 }
3706 }
3707
3708 direct_link_id(fd, main, id_tag, id_read_tags, id, id_old);
3709
3710 if (main->id_map != nullptr) {
3711 BKE_main_idmap_insert_id(main->id_map, id);
3712 }
3713
3714 return blo_bhead_next(fd, bhead);
3715 }
3716
3717 /* Read datablock contents.
3718 * Use convenient malloc name for debugging and better memory link prints. */
3719 const void *id_address_old = bhead->old;
3720 bhead = read_data_into_datamap(fd, bhead, blockname, id_type_index);
3721 const bool success = direct_link_id(fd, main, id_tag, id_read_tags, id, id_old);
3723
3724 if (!success) {
3725 /* Only happens with corrupt files. Remove the ID from the library mapping,
3726 * so pointers to it resolve to null instead of the freed ID. */
3727 const NewAddress *entry = fd->libmap->map.lookup_ptr(id_address_old);
3728 if (entry && entry->newp == id) {
3729 fd->libmap->map.remove(id_address_old);
3730 }
3731
3732 /* Free without user refcounting, as ID pointers still have their old address. */
3733 BLI_remlink(lb, id);
3734 BKE_id_free_ex(nullptr,
3735 id,
3738 false);
3739 fd->flags &= ~FD_FLAGS_FILE_OK;
3740 if (fd->bmain) {
3741 blo_readfile_invalidate(fd, fd->bmain, "Corrupt .blend file, failed to read a data-block");
3742 }
3743 if (r_id != nullptr) {
3744 *r_id = nullptr;
3745 }
3746 }
3747 else {
3748 if (do_partial_undo && id_old != nullptr) {
3749 /* For undo, store contents read into id at id_old. */
3751 }
3752 if (fd->new_idmap_uid != nullptr) {
3754 }
3755 if (main->id_map != nullptr) {
3756 BKE_main_idmap_insert_id(main->id_map, id_target);
3757 }
3758 process_packed_id(*id_target);
3759 if (fd->file_stat) {
3760 id->runtime->src_blend_modifification_time = fd->file_stat->st_mtime;
3761 }
3762 }
3763
3764 return bhead;
3765}
3766
3768
3769/* -------------------------------------------------------------------- */
3772
3774{
3776
3777 bhead = read_data_into_datamap(fd, bhead, "Data for Asset meta-data", INDEX_ID_NULL);
3778
3779 BlendDataReader reader = {fd};
3780 BLO_read_struct(&reader, AssetMetaData, r_asset_data);
3781 if (*r_asset_data) {
3782 BKE_asset_metadata_read(&reader, *r_asset_data);
3783 }
3784
3786
3787 return bhead;
3788}
3789
3791
3792/* -------------------------------------------------------------------- */
3795
3804{
3806 fd, bhead, "Data from Global block", INDEX_ID_NULL, sizeof(FileGlobal)));
3807
3808 if (fg == nullptr) [[unlikely]] {
3809 blo_readfile_invalidate(fd, bfd->main, "Corrupt .blend file, missing global block");
3810 return nullptr;
3811 }
3812
3813 /* NOTE: `bfd->main->versionfile` is supposed to have already been set from `fd->fileversion`
3814 * beforehand by calling code. */
3815 bfd->main->subversionfile = fg->subversion;
3818
3819 bfd->main->minversionfile = fg->minversion;
3821
3823 STRNCPY(bfd->main->build_hash, fg->build_hash);
3825
3828
3829 bfd->fileflags = fg->fileflags;
3830 bfd->globalf = fg->globalf;
3831
3832 /* NOTE: since 88b24bc6bb, `fg->filepath` is only written for crash recovery and autosave files,
3833 * so only overwrite `fd->relabase` if it is not empty, in case a regular blendfile is opened
3834 * through one of the 'recover' operators.
3835 *
3836 * In all other cases, the path is just set to the current path of the blendfile being read, so
3837 * there is no need to handle anymore older files (pre-2.65) that did not store (correctly) their
3838 * path. */
3839 if (G.fileflags & G_FILE_RECOVER_READ) {
3840 if (fg->filepath[0] != '\0') {
3841 STRNCPY(fd->relabase, fg->filepath);
3842 /* Used to set expected original filepath in read Main, instead of the path of the recovery
3843 * file itself. */
3844 STRNCPY(bfd->filepath, fg->filepath);
3845 }
3846 }
3847
3848 bfd->curscreen = fg->curscreen;
3849 bfd->curscene = fg->curscene;
3850 bfd->cur_view_layer = fg->cur_view_layer;
3851
3852 MEM_delete(fg);
3853
3854 fd->globalf = bfd->globalf;
3855 fd->fileflags = bfd->fileflags;
3856
3857 return blo_bhead_next(fd, bhead);
3858}
3859
3860/* NOTE: this has to be kept for reading older files... */
3861static void link_global(FileData *fd, BlendFileData *bfd)
3862{
3863 bfd->cur_view_layer = static_cast<ViewLayer *>(
3865 bfd->curscreen = static_cast<bScreen *>(newlibadr(fd, nullptr, false, bfd->curscreen));
3866 bfd->curscene = static_cast<Scene *>(newlibadr(fd, nullptr, false, bfd->curscene));
3867 /* this happens in files older than 2.35 */
3868 if (bfd->curscene == nullptr) {
3869 if (bfd->curscreen) {
3870 bfd->curscene = bfd->curscreen->scene;
3871 }
3872 }
3873 if (bfd->curscene == nullptr) {
3874 bfd->curscene = bfd->main->scenes.first();
3875 }
3876}
3877
3879
3880/* -------------------------------------------------------------------- */
3883
3884static void do_versions_userdef(FileData * /*fd*/, BlendFileData *bfd)
3885{
3886 UserDef *user = bfd->user;
3887
3888 if (user == nullptr) {
3889 return;
3890 }
3891
3893}
3894
3895static void do_versions(FileData *fd, Library *lib, Main *main)
3896{
3897 /* WATCH IT!!!: pointers from libdata have not been converted */
3898
3899 /* Don't allow versioning to create new data-blocks. */
3900 main->is_locked_for_linking = true;
3901
3902 /* Code ensuring conversion to/from new 'system IDProperties'. This needs to run before any other
3903 * data versioning. Otherwise, things like Cycles versioning code cannot work as expected. */
3904 if (!MAIN_VERSION_FILE_ATLEAST(main, 500, 27)) {
3905 /* Generate System IDProperties by copying the whole 'user-defined' historic IDProps into new
3906 * system-defined-only storage. While not optimal (as it also duplicates actual user-defined
3907 * IDProperties), this seems to be the only safe and sound way to handle the migration. */
3909 }
3910 if (!MAIN_VERSION_FILE_ATLEAST(main, 500, 70)) {
3911 /* Same as above, but decision to keep user-defined (aka custom properties) in nodes was taken
3912 * later during 5.0 development process. */
3914 }
3915 if (!MAIN_VERSION_FILE_ATLEAST(main, 500, 110)) {
3916 /* Same as above, but children bones were missed by initial versioning code, attempt to
3917 * transfer idprops data still in case they have no system properties defined yet. */
3919 }
3920
3921 if (G.debug & G_DEBUG) {
3922 char build_commit_datetime[32];
3923 time_t temp_time = main->build_commit_timestamp;
3924 tm *tm = (temp_time) ? gmtime(&temp_time) : nullptr;
3925 if (tm) [[likely]] {
3926 strftime(build_commit_datetime, sizeof(build_commit_datetime), "%Y-%m-%d %H:%M", tm);
3927 }
3928 else {
3929 STRNCPY(build_commit_datetime, "unknown");
3930 }
3931
3932 CLOG_INFO(&LOG, "Read file %s", fd->relabase);
3933 CLOG_INFO(&LOG,
3934 " Version %d sub %d date %s hash %s",
3935 main->versionfile,
3936 main->subversionfile,
3937 build_commit_datetime,
3938 main->build_hash);
3939 }
3940
3941 if (!main->is_read_invalid) {
3942 blo_do_versions_pre250(fd, lib, main);
3943 }
3944 if (!main->is_read_invalid) {
3945 blo_do_versions_250(fd, lib, main);
3946 }
3947 if (!main->is_read_invalid) {
3948 blo_do_versions_260(fd, lib, main);
3949 }
3950 if (!main->is_read_invalid) {
3951 blo_do_versions_270(fd, lib, main);
3952 }
3953 if (!main->is_read_invalid) {
3954 blo_do_versions_280(fd, lib, main);
3955 }
3956 if (!main->is_read_invalid) {
3957 blo_do_versions_290(fd, lib, main);
3958 }
3959 if (!main->is_read_invalid) {
3960 blo_do_versions_300(fd, lib, main);
3961 }
3962 if (!main->is_read_invalid) {
3963 blo_do_versions_400(fd, lib, main);
3964 }
3965 if (!main->is_read_invalid) {
3966 blo_do_versions_401(fd, lib, main);
3967 }
3968 if (!main->is_read_invalid) {
3969 blo_do_versions_402(fd, lib, main);
3970 }
3971 if (!main->is_read_invalid) {
3972 blo_do_versions_403(fd, lib, main);
3973 }
3974 if (!main->is_read_invalid) {
3975 blo_do_versions_404(fd, lib, main);
3976 }
3977 if (!main->is_read_invalid) {
3978 blo_do_versions_405(fd, lib, main);
3979 }
3980 if (!main->is_read_invalid) {
3981 blo_do_versions_500(fd, lib, main);
3982 }
3983 if (!main->is_read_invalid) {
3984 blo_do_versions_501(fd, lib, main);
3985 }
3986 if (!main->is_read_invalid) {
3987 blo_do_versions_502(fd, lib, main);
3988 }
3989 if (!main->is_read_invalid) {
3990 blo_do_versions_503(fd, lib, main);
3991 }
3992
3993 /* WATCH IT!!!: pointers from libdata have not been converted yet here! */
3994 /* WATCH IT 2!: #UserDef struct init see #do_versions_userdef() above! */
3995
3996 /* don't forget to set version number in BKE_blender_version.h! */
3997
3998 main->is_locked_for_linking = false;
3999}
4000
4002{
4003 BLI_assert(fd != nullptr);
4004
4005 CLOG_DEBUG(&LOG,
4006 "Processing %s (%s), %d.%d",
4007 main->curlib ? main->curlib->filepath : main->filepath,
4008 main->curlib ? "LIB" : "MAIN",
4009 main->versionfile,
4010 main->subversionfile);
4011
4012 /* Don't allow versioning to create new data-blocks. */
4013 main->is_locked_for_linking = true;
4014
4015 if (!main->is_read_invalid) {
4017 }
4018 if (!main->is_read_invalid) {
4020 }
4021 if (!main->is_read_invalid) {
4023 }
4024 if (!main->is_read_invalid) {
4026 }
4027 if (!main->is_read_invalid) {
4029 }
4030 if (!main->is_read_invalid) {
4032 }
4033 if (!main->is_read_invalid) {
4035 }
4036 if (!main->is_read_invalid) {
4038 }
4039 if (!main->is_read_invalid) {
4041 }
4042 if (!main->is_read_invalid) {
4044 }
4045 if (!main->is_read_invalid) {
4047 }
4048 if (!main->is_read_invalid) {
4050 }
4051 if (!main->is_read_invalid) {
4053 }
4054 if (!main->is_read_invalid) {
4056 }
4057 if (!main->is_read_invalid) {
4059 }
4060 if (!main->is_read_invalid) {
4062 }
4063
4064 main->is_locked_for_linking = false;
4065}
4066
4068
4069/* -------------------------------------------------------------------- */
4072
4074{
4075 /* Embedded IDs are not known by lib_link code, so they would be remapped to `nullptr`. But there
4076 * is no need to process them anyway, as they are already handled during the 'read_data' phase.
4077 *
4078 * NOTE: Some external non-owning pointers to embedded IDs (like the node-tree pointers of the
4079 * Node editor) will not be detected as embedded ones though at 'lib_link' stage (because their
4080 * source data cannot be accessed). This is handled on a case-by-case basis in 'after_lib_link'
4081 * validation code. */
4083 return IDWALK_RET_NOP;
4084 }
4085
4086 /* Explicitly requested to be ignored during readfile processing. Means the read_data code
4087 * already handled this pointer. Typically, the 'owner_id' pointer of an embedded ID. */
4088 if (cb_data->cb_flag & IDWALK_CB_READFILE_IGNORE) {
4089 return IDWALK_RET_NOP;
4090 }
4091
4092 BlendLibReader *reader = static_cast<BlendLibReader *>(cb_data->user_data);
4093 ID **id_ptr = cb_data->id_pointer;
4094 ID *owner_id = cb_data->owner_id;
4095
4096 *id_ptr = BLO_read_get_new_id_address(reader, owner_id, ID_IS_LINKED(owner_id), *id_ptr);
4097
4098 return IDWALK_RET_NOP;
4099}
4100
4101static void lib_link_all(FileData *fd, Main *bmain)
4102{
4103 BlendLibReader reader = {fd, bmain};
4104
4105 ID *id;
4106 FOREACH_MAIN_ID_BEGIN (bmain, id) {
4107 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id);
4108
4110 {
4112 /* This ID has been re-used from 'old' bmain. Since it was therefore unchanged across
4113 * current undo step, and old IDs re-use their old memory address, we do not need to liblink
4114 * it at all. */
4116
4117 /* Some data that should be persistent, like the 3DCursor or the tool settings, are
4118 * stored in IDs affected by undo, like Scene. So this requires some specific handling. */
4119 /* NOTE: even though the ID may have been detected as unchanged, the 'undo_preserve' may have
4120 * to actually change some of its ID pointers, it's e.g. the case with Scene's tool-settings
4121 * Brush/Palette pointers. This is the case where both new and old ID may be the same. */
4122 if (id_type->blend_read_undo_preserve != nullptr) {
4124 id_type->blend_read_undo_preserve(&reader, id, id->orig_id ? id->orig_id : id);
4125 }
4126 continue;
4127 }
4128
4130 /* Not all original pointer values can be considered as valid.
4131 * Handling of DNA deprecated data should never be needed in undo case. */
4133 ((fd->flags & FD_FLAGS_IS_MEMFILE) ?
4134 IDWALK_NOP :
4136 BKE_library_foreach_ID_link(bmain, id, lib_link_cb, &reader, flag);
4137
4138 after_liblink_id_process(&reader, id);
4139
4141 }
4142
4143 /* Some data that should be persistent, like the 3DCursor or the tool settings, are
4144 * stored in IDs affected by undo, like Scene. So this requires some specific handling. */
4145 if (id_type->blend_read_undo_preserve != nullptr && id->orig_id != nullptr) {
4147 id_type->blend_read_undo_preserve(&reader, id, id->orig_id);
4148 }
4149 }
4151
4152 /* Cleanup `ID.orig_id`, this is now reserved for depsgraph/copy-on-eval usage only. */
4153 FOREACH_MAIN_ID_BEGIN (bmain, id) {
4154 id->orig_id = nullptr;
4155 }
4157
4158#ifndef NDEBUG
4159 /* Double check we do not have any 'need link' tag remaining, this should never be the case once
4160 * this function has run. */
4161 FOREACH_MAIN_ID_BEGIN (bmain, id) {
4163 }
4165#endif
4166}
4167
4174{
4175 /* We only expect a merged Main here, not a split one. */
4176 BLI_assert(!bmain->split_mains);
4177
4178 if (!BKE_main_namemap_validate_and_fix(*bmain)) {
4179 BKE_report(
4180 reports ? reports->reports : nullptr,
4181 RPT_ERROR,
4182 "Critical blend-file corruption: Conflicts and/or otherwise invalid data-blocks names "
4183 "(see console for details)");
4184 }
4185
4186 /* Check for possible cycles in scenes' 'set' background property. */
4188
4189 /* We could integrate that to mesh/curve/lattice lib_link, but this is really cheap process,
4190 * so simpler to just use it directly in this single call. */
4191 BLO_main_validate_shapekeys(bmain, reports ? reports->reports : nullptr);
4192
4193 BLO_main_validate_embedded_flag(bmain, reports ? reports->reports : nullptr);
4194 BLO_main_validate_embedded_liboverrides(bmain, reports ? reports->reports : nullptr);
4195
4196 /* We have to rebuild that runtime information *after* all data-blocks have been properly linked.
4197 */
4199}
4200
4202
4203/* -------------------------------------------------------------------- */
4206
4208{
4209 BLO_read_struct(reader, IDProperty, &kmi->properties);
4210 IDP_BlendDataRead(reader, &kmi->properties);
4211 kmi->ptr = nullptr;
4212 kmi->flag &= ~KMI_UPDATE;
4213}
4214
4216{
4217 switch (type) {
4218 case USER_MENU_TYPE_SEP:
4219 return sizeof(bUserMenuItem);
4221 return sizeof(bUserMenuItem_Op);
4223 return sizeof(bUserMenuItem_Menu);
4225 return sizeof(bUserMenuItem_Prop);
4226 }
4227 return sizeof(bUserMenuItem);
4228}
4229
4236{
4237 UserDef *user;
4238 bfd->user = user = static_cast<UserDef *>(read_struct_and_validate_size(
4239 fd, bhead, "Data for User Def", INDEX_ID_NULL, sizeof(UserDef)));
4240
4241 if (user == nullptr) [[unlikely]] {
4242 blo_readfile_invalidate(fd, bfd->main, "Corrupt .blend file, missing user-preferences block");
4243 return nullptr;
4244 }
4245
4246 /* User struct has separate do-version handling */
4247 user->versionfile = bfd->main->versionfile;
4248 user->subversionfile = bfd->main->subversionfile;
4249
4250 /* read all data into fd->datamap */
4251 bhead = read_data_into_datamap(fd, bhead, "Data for User Def", INDEX_ID_NULL);
4252
4253 BlendDataReader reader_ = {fd};
4254 BlendDataReader *reader = &reader_;
4255
4256 BLO_read_struct_list(reader, bTheme, &user->themes);
4259 BLO_read_struct_list(reader, bUserMenu, &user->user_menus);
4260 BLO_read_struct_list(reader, bAddon, &user->addons);
4266
4267 for (wmKeyMap &keymap : user->user_keymaps) {
4268 keymap.modal_items = nullptr;
4269 keymap.poll = nullptr;
4270 keymap.flag &= ~KEYMAP_UPDATE;
4271
4273 BLO_read_struct_list(reader, wmKeyMapItem, &keymap.items);
4274
4275 for (wmKeyMapDiffItem &kmdi : keymap.diff_items) {
4277 BLO_read_struct(reader, wmKeyMapItem, &kmdi.add_item);
4278
4279 if (kmdi.remove_item) {
4280 direct_link_keymapitem(reader, kmdi.remove_item);
4281 }
4282 if (kmdi.add_item) {
4283 direct_link_keymapitem(reader, kmdi.add_item);
4284 }
4285 }
4286
4287 for (wmKeyMapItem &kmi : keymap.items) {
4288 direct_link_keymapitem(reader, &kmi);
4289 }
4290 }
4291
4292 for (wmKeyConfigPref &kpt : user->user_keyconfig_prefs) {
4293 BLO_read_struct(reader, IDProperty, &kpt.prop);
4294 IDP_BlendDataRead(reader, &kpt.prop);
4295 }
4296
4297 for (bUserMenu &um : user->user_menus) {
4299 reader, &um.items, [](const bUserMenuItem &umi) {
4300 return user_menu_item_struct_size(umi.type);
4301 });
4302 for (bUserMenuItem &umi : um.items) {
4303 if (umi.type == USER_MENU_TYPE_OPERATOR) {
4304 bUserMenuItem_Op *umi_op = reinterpret_cast<bUserMenuItem_Op *>(&umi);
4305 BLO_read_struct(reader, IDProperty, &umi_op->prop);
4306 IDP_BlendDataRead(reader, &umi_op->prop);
4307 }
4308 }
4309 }
4310
4311 for (bAddon &addon : user->addons) {
4312 BLO_read_struct(reader, IDProperty, &addon.prop);
4313 IDP_BlendDataRead(reader, &addon.prop);
4314 }
4315
4316 for (bUserAssetLibrary &asset_library_ref : user->asset_libraries) {
4317 BKE_preferences_asset_library_read_data(reader, &asset_library_ref);
4318 }
4319
4320 for (bUserExtensionRepo &repo_ref : user->extension_repos) {
4322 }
4323
4324 for (bUserAssetShelfSettings &shelf_settings : user->asset_shelves_settings) {
4326 }
4327
4328 /* XXX */
4329 user->uifonts.first_ = user->uifonts.last_ = nullptr;
4330
4331 BLO_read_struct_list(reader, uiStyle, &user->uistyles);
4332
4333 /* Don't read the active app template, use the default one. */
4334 user->app_template[0] = '\0';
4335
4336 /* Clear runtime data. */
4337 user->runtime.is_dirty = false;
4338 user->edit_studio_light = 0;
4339
4340 /* free fd->datamap again */
4342
4343 return bhead;
4344}
4345
4347
4348/* -------------------------------------------------------------------- */
4351
4353{
4355 return IDWALK_RET_NOP;
4356 }
4357
4358 IDNameLib_Map *new_idmap_uid = static_cast<IDNameLib_Map *>(cb_data->user_data);
4359 ID **id_pointer = cb_data->id_pointer;
4360 if (*id_pointer != nullptr) {
4361 *id_pointer = BKE_main_idmap_lookup_uid(new_idmap_uid, (*id_pointer)->session_uid);
4362 }
4363
4364 return IDWALK_RET_NOP;
4365}
4366
4367/* Remap 'no undo' ID usages to matching IDs in new main.
4368 *
4369 * 'no undo' IDs have simply be moved from old to new main so far. However, unlike the other
4370 * re-used IDs (the 'unchanged' ones), there is no guarantee that all the ID pointers they use are
4371 * still valid.
4372 *
4373 * This code performs a remapping based on the session_uid. */
4375{
4376 Main *new_bmain = fd->bmain;
4377 ID *id_iter;
4378 FOREACH_MAIN_ID_BEGIN (new_bmain, id_iter) {
4379 if (ID_IS_LINKED(id_iter)) {
4380 continue;
4381 }
4382 if ((id_iter->tag & ID_TAG_UNDO_OLD_ID_REUSED_NOUNDO) == 0) {
4383 continue;
4384 }
4385
4388 }
4390}
4391
4396static void blo_read_file_checks(Main *bmain)
4397{
4398#ifndef NDEBUG
4399 BLI_assert(!bmain->split_mains);
4400 BLI_assert(!bmain->is_read_invalid);
4401
4402 for (wmWindowManager &wm : bmain->wm) {
4403 for (wmWindow &win : wm.windows) {
4404 /* This pointer is deprecated and should always be nullptr. */
4405 BLI_assert(win.screen == nullptr);
4406 }
4407 }
4408#endif
4409 UNUSED_VARS_NDEBUG(bmain);
4410}
4411
4413{
4414 BHead *bhead = blo_bhead_first(fd);
4415 BlendFileData *bfd;
4416
4417 const bool is_undo = (fd->flags & FD_FLAGS_IS_MEMFILE) != 0;
4418 if (is_undo) {
4419 CLOG_DEBUG(&LOG_UNDO, "UNDO: read step");
4420
4421 /* Find all 'no undo' IDs from the old Main. These will be moved (re-used) into the new Main.
4422 * Also find all of their dependencies (local ones are ignored currently, but linked ones are
4423 * also forced-moved into the new Main, even if they did not exist in the loaded memfile). */
4425 }
4426
4427 bfd = MEM_new<BlendFileData>(__func__);
4428
4429 bfd->main = BKE_main_new();
4430 bfd->main->versionfile = fd->fileversion;
4431 STRNCPY(bfd->filepath, filepath);
4432
4433 /* Prevent any run of layer collections rebuild during readfile process, and the do_versions
4434 * calls.
4435 *
4436 * NOTE: Typically readfile code should not trigger such updates anyway. But some calls to
4437 * non-BLO functions (e.g. ID deletion) can indirectly trigger it. */
4439
4440 fd->bmain = bfd->main;
4441 fd->fd_bmain = bfd->main;
4442
4443 bfd->type = BLENFILETYPE_BLEND;
4444
4445 if ((fd->skip_flags & BLO_READ_SKIP_DATA) == 0) {
4446 blo_split_main(bfd->main);
4447 STRNCPY(bfd->main->filepath, filepath);
4448 }
4449
4450 BLI_SCOPED_DEFER([&]() {
4451 if (bfd->main->is_read_invalid && bfd->main->split_mains) {
4453 }
4454 });
4455
4456 if (G.background) {
4457 /* We only read & store .blend thumbnail in background mode
4458 * (because we cannot re-generate it, no OpenGL available).
4459 */
4460 const int *data = read_file_thumbnail(fd);
4461
4462 if (data) {
4463 const int width = data[0];
4464 const int height = data[1];
4465 if (BLEN_THUMB_MEMSIZE_IS_VALID(width, height)) {
4466 const size_t data_size = BLEN_THUMB_MEMSIZE(width, height);
4467 bfd->main->blen_thumb = static_cast<BlendThumbnail *>(
4468 MEM_new_uninitialized(data_size, __func__));
4469
4470 BLI_assert((data_size - sizeof(*bfd->main->blen_thumb)) ==
4471 (BLEN_THUMB_MEMSIZE_FILE(width, height) - (sizeof(*data) * 2)));
4472 bfd->main->blen_thumb->width = width;
4473 bfd->main->blen_thumb->height = height;
4474 memcpy(bfd->main->blen_thumb->rect, &data[2], data_size - sizeof(*bfd->main->blen_thumb));
4475 }
4476 }
4477 }
4478
4479 if (is_undo) {
4480 /* This idmap will store UIDs of all IDs ending up in the new main, whether they are newly
4481 * read, or re-used from the old main. */
4483
4484 /* Copy all 'no undo' local data from old to new bmain. */
4486 }
4487
4488 while (bhead) {
4489 /* If not-null after the `switch`, the BHead is an ID one and needs to be read. */
4490 Main *bmain_to_read_into = nullptr;
4491 bool placeholder_set_indirect_extern = false;
4492 bool is_linked_packed_id = false;
4493
4494 switch (bhead->code) {
4495 case BLO_CODE_DATA:
4496 case BLO_CODE_DNA1:
4497 case BLO_CODE_TEST: /* used as preview since 2.5x */
4498 case BLO_CODE_REND:
4499 bhead = blo_bhead_next(fd, bhead);
4500 break;
4501 case BLO_CODE_GLOB:
4502 bhead = read_global(bfd, fd, bhead);
4503 break;
4504 case BLO_CODE_USER:
4506 bhead = blo_bhead_next(fd, bhead);
4507 }
4508 else {
4509 bhead = read_userdef(bfd, fd, bhead);
4510 }
4511 break;
4512 case BLO_CODE_ENDB:
4513 bhead = nullptr;
4514 break;
4515
4517 if ((fd->skip_flags & BLO_READ_SKIP_DATA) != 0) {
4518 bhead = blo_bhead_next(fd, bhead);
4519 break;
4520 }
4521 /* Add link placeholder to the main of the library it belongs to.
4522 *
4523 * The library is the most recently loaded #ID_LI block, according to the file format
4524 * definition. So we can use the entry at the end of `fd->bmain->split_mains`, typically
4525 * the one last added in #direct_link_library. */
4526 bmain_to_read_into = (*fd->bmain->split_mains)[fd->bmain->split_mains->size() - 1];
4527 placeholder_set_indirect_extern = true;
4528
4529 /* Corrupt file without library before the placeholder, which would otherwise be
4530 * read as a local ID. */
4531 if (bmain_to_read_into->curlib == nullptr) [[unlikely]] {
4532 blo_readfile_invalidate(fd, bfd->main, "Corrupt .blend file, missing library");
4533 bmain_to_read_into = nullptr;
4534 bhead = blo_bhead_next(fd, bhead);
4535 }
4536 /* Archive libraries contain packed IDs, never placeholders. The placeholder would
4537 * otherwise be used as a full ID. */
4538 else if (bmain_to_read_into->curlib->flag & LIBRARY_FLAG_IS_ARCHIVE) [[unlikely]] {
4540 fd, bfd->main, "Corrupt .blend file, link placeholder in archive library");
4541 bmain_to_read_into = nullptr;
4542 bhead = blo_bhead_next(fd, bhead);
4543 }
4544 break;
4545 case ID_LI:
4546 if ((fd->skip_flags & BLO_READ_SKIP_DATA) != 0) {
4547 bhead = blo_bhead_next(fd, bhead);
4548 break;
4549 }
4550 /* Library IDs are always read into the first (aka 'local') Main, even if they are written
4551 * in 'library' blendfile-space (for archive libraries e.g.). */
4552 bmain_to_read_into = fd->bmain;
4553 break;
4554 case ID_SCRN:
4555 /* in 2.50+ files, the file identifier for screens is patched, forward compatibility */
4556 bhead->code = ID_SCR;
4557 /* pass on to default */
4559 default: {
4560 if ((fd->skip_flags & BLO_READ_SKIP_DATA) != 0 || !blo_bhead_is_id_valid_type(bhead)) {
4561 bhead = blo_bhead_next(fd, bhead);
4562 break;
4563 }
4564 /* Put read real ID into the main of the library it belongs to.
4565 *
4566 * Local IDs should all be written before any Library in the blendfile, so this code will
4567 * always select `fd->bmain` for these.
4568 *
4569 * Packed linked IDs are real ID data in the currently read blendfile (unlike placeholders
4570 * for regular linked data). But they are in their archive library 'name space' and
4571 * 'blendfile space', so this follows the same logic as for placeholders to select the
4572 * Main.
4573 *
4574 * The library is the most recently loaded #ID_LI block, according to the file format
4575 * definition. So we can use the entry at the end of `fd->bmain->split_mains`, typically
4576 * the one last added in #direct_link_library. */
4577 bmain_to_read_into = (*fd->bmain->split_mains)[fd->bmain->split_mains->size() - 1];
4578 is_linked_packed_id = (blo_bhead_id_flag(fd, bhead) & ID_FLAG_LINKED_AND_PACKED) != 0;
4579 BLI_assert_msg((bmain_to_read_into == fd->bmain || is_linked_packed_id),
4580 "Local IDs should always be put in the first Main split data-base, not in "
4581 "a 'linked data' one");
4582 }
4583 }
4584 if (bmain_to_read_into) {
4585 const eID_Tag id_tag = is_linked_packed_id ? ID_TAG_EXTERN : ID_TAG_LOCAL;
4586 bhead = read_libblock(
4587 fd, bmain_to_read_into, bhead, id_tag, {}, placeholder_set_indirect_extern, nullptr);
4588 }
4589
4590 /* It's not enough to check `bfd->main->is_read_invalid` because the error may have
4591 * occurred before the #Main struct is available, so it's important to check the flag here. */
4592 if (!(fd->flags & FD_FLAGS_FILE_OK)) [[unlikely]] {
4593 /* Skip when already invalidated - that error is more specific. */
4594 if (!bfd->main->is_read_invalid) {
4595 blo_readfile_invalidate(fd, bfd->main, "Corrupt .blend file, failed to read a block");
4596 }
4597 }
4598
4599 if (bfd->main->is_read_invalid) {
4600 return bfd;
4601 }
4602 }
4603
4604 if (is_undo) {
4605 /* Move remaining libraries containing 'no undo' IDs from old to new Main. */
4607 /* Remove from the new Main regular linked IDs that are unused. */
4609 }
4610
4611 /* Ensure fully valid and unique ID names before calling first stage of versioning. */
4612 if (!is_undo && (fd->flags & FD_FLAGS_HAS_INVALID_ID_NAMES) != 0) {
4614
4618 "Blendfile '%s' was created by a future version of Blender and contains ID "
4619 "names longer than currently supported. These have been truncated.",
4620 bfd->filepath);
4621 }
4622 else {
4624 RPT_ERROR,
4625 "Blendfile '%s' appears corrupted, it contains invalid ID names. These have "
4626 "been truncated.",
4627 bfd->filepath);
4628 }
4629
4630 /* This part is only to ensure forward compatibility with 5.0+ blend-files in 4.5.
4631 * It will be removed in 5.0. */
4633 }
4634 else {
4635 /* Getting invalid ID names from memfile undo data would be a critical error. */
4637 if ((fd->flags & FD_FLAGS_HAS_INVALID_ID_NAMES) != 0) {
4638 bfd->main->is_read_invalid = true;
4639 }
4640 }
4641
4642 if (bfd->main->is_read_invalid) {
4643 return bfd;
4644 }
4645
4646 /* Do versioning before read_libraries, but skip in undo case. */
4647 if (!is_undo) {
4648 if ((fd->skip_flags & BLO_READ_SKIP_DATA) == 0) {
4649 for (Main *bmain : *fd->bmain->split_mains) {
4650 /* Packed IDs are stored in the current .blend file, but belong to dedicated 'archive
4651 * library' Mains, not the first, 'local' Main. So they do need versioning here, as for
4652 * local IDs, which is why all the split Mains in the list need to be checked.
4653 *
4654 * Placeholders (of 'real' linked data) can't be versioned yet. Since they also belong to
4655 * dedicated 'library' Mains, and are not mixed with the 'packed' ones, these Mains can be
4656 * entirely skipped. */
4657 const bool contains_link_placeholder = (bmain->curlib != nullptr &&
4658 (bmain->curlib->flag & LIBRARY_FLAG_IS_ARCHIVE) ==
4659 0);
4660#ifndef NDEBUG
4661 MainListsArray lbarray = BKE_main_lists_get(*bmain);
4662 for (ListBaseT<ID> *lb_array : lbarray) {
4663 for (ID &id : lb_array->items_mutable()) {
4665 (id.runtime->readfile_data->tags.is_link_placeholder == contains_link_placeholder),
4666 contains_link_placeholder ?
4667 "Real Library split Main contains non-placeholder IDs" :
4668 (bmain->curlib == nullptr ?
4669 "Local data split Main contains placeholder IDs" :
4670 "Archive Library split Main contains placeholder IDs"));
4671 }
4672 }
4673#endif
4674 if (contains_link_placeholder) {
4675 continue;
4676 }
4677 do_versions(fd, bmain->curlib, bmain);
4678 }
4679 }
4680
4681 if ((fd->skip_flags & BLO_READ_SKIP_USERDEF) == 0) {
4682 do_versions_userdef(fd, bfd);
4683 }
4684 }
4685
4686 if (bfd->main->is_read_invalid) {
4687 return bfd;
4688 }
4689
4690 if ((fd->skip_flags & BLO_READ_SKIP_DATA) == 0) {
4691 fd->reports->duration.libraries = BLI_time_now_seconds();
4692 read_libraries(fd);
4693 if (bfd->main->is_read_invalid) {
4694 return bfd;
4695 }
4696
4697 BLI_assert((*bfd->main->split_mains)[0] == bfd->main);
4698 blo_join_main(bfd->main);
4699
4700 lib_link_all(fd, bfd->main);
4701 if (bfd->main->is_read_invalid) {
4702 return bfd;
4703 }
4704
4706
4707 if (is_undo) {
4708 /* Ensure ID usages of reused 'no undo' IDs remain valid. */
4709 /* Although noundo data was reused as-is from the old main, it may have ID pointers to data
4710 * that has been removed, or that have a new address. */
4712 }
4713
4714 fd->reports->duration.libraries = BLI_time_now_seconds() - fd->reports->duration.libraries;
4715
4716 /* Skip in undo case. */
4717 if (!is_undo) {
4718 /* Note that we can't recompute user-counts at this point in undo case, we play too much with
4719 * IDs from different memory realms, and Main database is not in a fully valid state yet.
4720 */
4721 /* Some versioning code does expect some proper user-reference-counting, e.g. in conversion
4722 * from groups to collections... We could optimize out that first call when we are reading a
4723 * current version file, but again this is really not a bottle neck currently.
4724 * So not worth it. */
4726
4727 /* Necessary to allow 2.80 layer collections conversion code to work. */
4729
4730 /* Yep, second splitting... but this is a very cheap operation, so no big deal. */
4731 blo_split_main(bfd->main);
4732 for (Main *mainvar : *bfd->main->split_mains) {
4733 /* Do versioning for newly added linked data-blocks. If no data-blocks were read from a
4734 * library versionfile will still be zero and we can skip it. */
4735 if (mainvar->versionfile == 0) {
4736 continue;
4737 }
4738 do_versions_after_linking((mainvar->curlib && mainvar->curlib->runtime->filedata) ?
4739 mainvar->curlib->runtime->filedata :
4740 fd,
4741 mainvar);
4743 }
4744 blo_join_main(bfd->main);
4745
4747
4748 /* And we have to compute those user-reference-counts again, as `do_versions_after_linking()`
4749 * does not always properly handle user counts, and/or that function does not take into
4750 * account old, deprecated data. */
4752 }
4753
4754 for (Library &lib : bfd->main->libraries.items_mutable()) {
4755 /* Now we can clear this runtime library filedata, it is not needed anymore.
4756 *
4757 * NOTE: This is also important to do for archive libraries. */
4759 /* If no data-blocks were read from a library (should only happen when all references to a
4760 * library's data are `ID_FLAG_INDIRECT_WEAK_LINK`), its versionfile will still be zero and
4761 * it can be deleted.
4762 *
4763 * NOTES:
4764 * - In case the library blendfile exists but is missing all the referenced linked IDs, the
4765 * placeholders IDs created will reference the library ID, and the library ID will have a
4766 * valid version number as the file was read to search for the linked IDs.
4767 * - In case the library blendfile does not exist, its local Library ID will get the version
4768 * of the current local Main (i.e. the loaded blendfile).
4769 * - In case it is a reference library for archived ones, its runtime #archived_libraries
4770 * vector will not be empty, and it must be kept, even if no data is directly linked from
4771 * it anymore.
4772 * - External libraries never have versionfile info, so always skip them here.
4773 */
4774 if (lib.runtime->versionfile == 0 && lib.runtime->archived_libraries.is_empty() &&
4775 (lib.flag & LIBRARY_FLAG_IS_EXTERNAL) == 0)
4776 {
4777#ifndef NDEBUG
4778 ID *id_iter;
4779 FOREACH_MAIN_ID_BEGIN (bfd->main, id_iter) {
4780 BLI_assert(id_iter->lib != &lib);
4781 }
4783#endif
4785 bfd->main,
4786 &lib,
4787 {.extra_remapping_flags = (ID_REMAP_FORCE_UI_POINTERS | ID_REMAP_SKIP_USER_REFCOUNT |
4788 ID_REMAP_SKIP_UPDATE_TAGGING | ID_REMAP_SKIP_USER_CLEAR)});
4789 }
4790 }
4791
4792 if (bfd->main->is_read_invalid) {
4793 return bfd;
4794 }
4795
4796 /* After all data has been read and versioned, uses ID_TAG_NEW. Theoretically this should
4797 * not be calculated in the undo case, but it is currently needed even on undo to recalculate
4798 * a cache. */
4800
4802
4803 BKE_main_id_tag_all(bfd->main, ID_TAG_NEW, false);
4804
4805 /* Must happen before applying liboverrides, as this process may fully invalidate e.g. view
4806 * layer pointers in case a Scene is a liboverride. */
4807 link_global(fd, bfd);
4808
4809 /* Now that all our data-blocks are loaded,
4810 * we can re-generate overrides from their references. */
4811 if (!is_undo) {
4812 /* Do not apply in undo case! */
4813 fd->reports->duration.lib_overrides = BLI_time_now_seconds();
4814
4815 std::string cur_view_layer_name = bfd->cur_view_layer != nullptr ?
4816 bfd->cur_view_layer->name :
4817 "";
4818
4821
4822 /* In case the current scene is a liboverride, while the ID pointer itself remains valid,
4823 * above update of liboverrides will have completely invalidated its old content, so the
4824 * current view-layer needs to be searched for again. */
4825 if (bfd->cur_view_layer != nullptr) {
4826 bfd->cur_view_layer = BKE_view_layer_find(bfd->curscene, cur_view_layer_name.c_str());
4827 }
4828
4829 /* FIXME Temporary 'fix' to a problem in how temp ID are copied in
4830 * `BKE_lib_override_library_main_update`, see #103062.
4831 * Proper fix involves first addressing #90610. */
4833
4834 /* Update invariants after re-generating overrides. */
4836
4837 /* Some versioning code can leave some ID link status tags in invalid state, due to low-level
4838 * manipulations of these instead of using API like `id_lib_extern`, which will be also fixed
4839 * by this call. */
4841
4842 fd->reports->duration.lib_overrides = BLI_time_now_seconds() -
4843 fd->reports->duration.lib_overrides;
4844 }
4845
4847
4849
4850 /* Make all relative paths, relative to the open blend file. */
4852
4854 }
4855 else {
4857 }
4858
4859 BLI_assert(!bfd->main->split_mains);
4860 BLI_assert(bfd->main->id_map == nullptr);
4861
4862 /* Sanity checks. */
4864
4865 return bfd;
4866}
4867
4869
4870/* -------------------------------------------------------------------- */
4875
4878 const void *old;
4879};
4880
4881static int verg_bheadsort(const void *v1, const void *v2)
4882{
4883 const BHeadSort *x1 = static_cast<const BHeadSort *>(v1),
4884 *x2 = static_cast<const BHeadSort *>(v2);
4885
4886 if (x1->old > x2->old) {
4887 return 1;
4888 }
4889 if (x1->old < x2->old) {
4890 return -1;
4891 }
4892 return 0;
4893}
4894
4896{
4897 BHead *bhead;
4898 BHeadSort *bhs;
4899 int tot = 0;
4900
4901 for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
4902 tot++;
4903 }
4904
4905 fd->tot_bheadmap = tot;
4906 if (tot == 0) {
4907 return;
4908 }
4909
4910 bhs = fd->bheadmap = MEM_new_array_uninitialized<BHeadSort>(tot, "BHeadSort");
4911
4912 for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead), bhs++) {
4913 bhs->bhead = bhead;
4914 bhs->old = bhead->old;
4915 }
4916
4917 qsort(fd->bheadmap, tot, sizeof(BHeadSort), verg_bheadsort);
4918}
4919
4921{
4922 /* Skip library data-blocks in undo, see comment in read_libblock. */
4923 if (fd->flags & FD_FLAGS_IS_MEMFILE) {
4924 return nullptr;
4925 }
4926
4927 for (; bhead; bhead = blo_bhead_prev(fd, bhead)) {
4928 if (bhead->code == ID_LI) {
4929 break;
4930 }
4931 }
4932
4933 return bhead;
4934}
4935
4937{
4938 ID *id = read_id_struct(fd, bheadlib, "Data for Library ID type", INDEX_ID_NULL);
4939 if (id && id->id_type() != ID_LI) [[unlikely]] {
4940 MEM_delete(id);
4941 fd->flags &= ~FD_FLAGS_FILE_OK;
4942 if (fd->bmain) {
4944 fd, fd->bmain, "Corrupt .blend file, unexpected non-library block type");
4945 }
4946 return nullptr;
4947 }
4948
4949 /* The file path must have a null terminated string. */
4950 Library *lib = reinterpret_cast<Library *>(id);
4951 if (lib && !library_filepath_is_valid(lib)) [[unlikely]] {
4952 MEM_delete(lib);
4953 fd->flags &= ~FD_FLAGS_FILE_OK;
4954 if (fd->bmain) {
4955 blo_readfile_invalidate(fd, fd->bmain, "Corrupt .blend file, invalid library file path");
4956 }
4957 return nullptr;
4958 }
4959 return lib;
4960}
4961
4962static BHead *find_bhead(FileData *fd, void *old)
4963{
4964#if 0
4965 BHead *bhead;
4966#endif
4967 BHeadSort *bhs, bhs_s;
4968
4969 if (!old) {
4970 return nullptr;
4971 }
4972
4973 if (fd->bheadmap == nullptr) {
4975 }
4976
4977 bhs_s.old = old;
4978 bhs = static_cast<BHeadSort *>(
4979 bsearch(&bhs_s, fd->bheadmap, fd->tot_bheadmap, sizeof(BHeadSort), verg_bheadsort));
4980
4981 if (bhs) {
4982 return bhs->bhead;
4983 }
4984
4985#if 0
4986 for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
4987 if (bhead->old == old) {
4988 return bhead;
4989 }
4990 }
4991#endif
4992
4993 return nullptr;
4994}
4995
4996static BHead *find_bhead_from_code_name(FileData *fd, const short idcode, const char *name)
4997{
4998 char idname_full[MAX_ID_NAME];
4999 *(reinterpret_cast<short *>(idname_full)) = idcode;
5000 BLI_strncpy(idname_full + 2, name, sizeof(idname_full) - 2);
5001
5002 return fd->bhead_idname_map->lookup_default(idname_full, nullptr);
5003}
5004
5005static BHead *find_bhead_from_idname(FileData *fd, const char *idname)
5006{
5007 BHead *bhead = fd->bhead_idname_map->lookup_default(idname, nullptr);
5008 if (bhead) [[likely]] {
5009 return bhead;
5010 }
5011
5012 /* Expected ID was not found, attempt to load the same name, but for an older, deprecated and
5013 * converted ID type. */
5014 const short id_code_old = do_versions_new_to_old_idcode_get(GS(idname));
5015 if (id_code_old == ID_LINK_PLACEHOLDER) {
5016 return bhead;
5017 }
5018 return find_bhead_from_code_name(fd, id_code_old, idname + 2);
5019}
5020
5022{
5023 if (const IDHash *deep_hash = blo_bhead_id_deep_hash(fd, bhead)) {
5024 if (ID *existing_id = fd->id_by_deep_hash->lookup_default(*deep_hash, nullptr)) {
5025 return existing_id;
5026 }
5027 }
5028 return nullptr;
5029}
5030
5031static ID *library_id_is_yet_read_main(Main *mainvar, const char *idname)
5032{
5033 if (mainvar->id_map == nullptr) {
5034 mainvar->id_map = BKE_main_idmap_create(mainvar, false, nullptr, MAIN_IDMAP_TYPE_NAME);
5035 }
5036 BLI_assert(BKE_main_idmap_main_get(mainvar->id_map) == mainvar);
5037
5038 ID *existing_id = BKE_main_idmap_lookup_name(
5039 mainvar->id_map, GS(idname), idname + 2, mainvar->curlib);
5040 BLI_assert(existing_id ==
5041 BLI_findstring(which_libbase(mainvar, GS(idname)), idname, offsetof(ID, name)));
5042 return existing_id;
5043}
5044
5045static ID *library_id_is_yet_read(FileData *fd, Main *mainvar, BHead *bhead)
5046{
5047 if (ID *existing_id = library_id_is_yet_read_deep_hash(fd, bhead)) {
5048 return existing_id;
5049 }
5050
5051 const char *idname = blo_bhead_id_name(fd, bhead);
5052 if (!idname) {
5053 return nullptr;
5054 }
5055 return library_id_is_yet_read_main(mainvar, idname);
5056}
5057
5059 ReportList *reports,
5060 const bool has_forward_compatibility_issues,
5061 const char *filepath)
5062{
5063 if (!fd || (fd->flags & FD_FLAGS_HAS_INVALID_ID_NAMES) == 0) {
5064 return;
5065 }
5066 if (has_forward_compatibility_issues) {
5067 BKE_reportf(reports,
5069 "Library '%s' was created by a future version of Blender and contains ID names "
5070 "longer than currently supported. This may cause missing linked data, consider "
5071 "opening and re-saving that library with the current Blender version.",
5072 filepath);
5073 }
5074 else {
5075 BKE_reportf(reports,
5076 RPT_ERROR,
5077 "Library '%s' appears corrupted, it contains invalid ID names. This may cause "
5078 "missing linked data.",
5079 filepath);
5080 }
5081}
5082
5084
5085/* -------------------------------------------------------------------- */
5088
5089using BLOExpandDoitCallback = void (*)(void *fdhandle,
5090 std::queue<ID *> &ids_to_expand,
5091 Main *mainvar,
5092 void *idv);
5093
5100
5101/* Find the existing Main matching the given blendfile library filepath, or create a new one (with
5102 * the matching Library ID) if needed.
5103 *
5104 * If there is a known BHead referencing/source for this library, it should be passed as
5105 * `bheadlib`. This allows the code to generate the correct remapping for this BHead::old pointer.
5106 * This is not critical currently, but once libraries are actually 'used' like other IDs, this
5107 * become necessary to ensure these usages are properly 'remapped' to the new data on read (e.g.
5108 * for upcoming changes to `CollectionImport`).
5109 *
5110 * NOTE: The process is a bit more complex for packed linked IDs and their archive libraries, as
5111 * in this case, this function also needs to find or create a new suitable archive library, i.e.
5112 * one which does not contain yet the given ID (from its name & type). */
5114 const BHead *bheadlib,
5115 const char *lib_filepath,
5116 const char *relabase,
5117 const BHead *id_bhead,
5118 const char *id_name,
5119 const bool is_packed_id,
5120 const bool is_external_lib)
5121{
5122 Library *parent_lib = nullptr;
5123 char filepath_abs[FILE_MAX];
5124
5125 STRNCPY(filepath_abs, lib_filepath);
5126 BLI_path_abs(filepath_abs, relabase);
5127 BLI_path_normalize(filepath_abs);
5128
5129 for (Main *main_it : *fd->bmain->split_mains) {
5130 const char *libname = (main_it->curlib) ? main_it->curlib->runtime->filepath_abs :
5131 main_it->filepath;
5132
5133 if (BLI_path_cmp(filepath_abs, libname) == 0) {
5134 /* Due to how parent and archive libraries are created and written in the blend-file,
5135 * the first library matching a given filepath should never be an archive one. Skip it
5136 * anyway to handle corrupt files, placeholders in archive libraries are never read. */
5137 if (main_it->curlib && (main_it->curlib->flag & LIBRARY_FLAG_IS_ARCHIVE)) {
5138 continue;
5139 }
5140 CLOG_DEBUG(&LOG,
5141 "Found library '%s' for file path '%s'",
5142 main_it->curlib ? main_it->curlib->id.name : "<None>",
5143 lib_filepath);
5144 if (!is_packed_id) {
5145 return main_it;
5146 }
5147 /* For packed IDs, the Main of the main owner library is not a valid one. Another loop is
5148 * needed into all the Mains matching the archive libraries of this main library. */
5149 BLI_assert(main_it->curlib);
5150 parent_lib = main_it->curlib;
5151 break;
5152 }
5153 }
5154
5155 if (is_packed_id) {
5156 if (parent_lib) {
5157 /* Try to find an 'available' existing archive Main library, i.e. one that does not yet
5158 * contain an ID of the same type and name. */
5159 for (Main *main_it : *fd->bmain->split_mains) {
5160 if (!main_it->curlib || (main_it->curlib->flag & LIBRARY_FLAG_IS_ARCHIVE) == 0 ||
5161 main_it->curlib->archive_parent_library != parent_lib)
5162 {
5163 continue;
5164 }
5165 if (ID *packed_id = library_id_is_yet_read_main(main_it, id_name)) {
5166 /* Archive Main library already contains a 'same' ID - but it should have a different
5167 * deep_hash. Otherwise, a previous call to `library_id_is_yet_read()` should have
5168 * returned this ID, and this code should not be reached. */
5169 BLI_assert((packed_id->lib->flag & LIBRARY_FLAG_IS_EXTERNAL) ||
5170 !blo_bhead_id_deep_hash(fd, id_bhead) ||
5171 packed_id->deep_hash != *blo_bhead_id_deep_hash(fd, id_bhead));
5172 UNUSED_VARS_NDEBUG(packed_id, id_bhead);
5173 continue;
5174 }
5175 if (main_it->curlib->runtime->filedata) {
5176 /* If this Main was already used to read and store some packed data, it will have an
5177 * assigned (non-owned) FileData pointer.
5178 *
5179 * Even though that Main does not contain any version of the linked packed ID being
5180 * read, it cannot be used as its container if the FileData does not match.
5181 *
5182 * Otherwise, in complex cases, code can end up putting some packed IDs from libC, used
5183 * in (and read from) libA, into the same Main as other packed IDs from libC, read from
5184 * libB.
5185 *
5186 * This would result in 'stealing' the FileData of this Main, which will likely result in
5187 * liblink failure later on in reading process, as the old addresses of some packed data
5188 * in that Main will not be found anymore in the libmap of the currently assigned
5189 * FileData.
5190 */
5191 BLI_assert(main_it->curlib->runtime->is_filedata_owner == false);
5192 if (main_it->curlib->runtime->filedata != fd) {
5193 continue;
5194 }
5195 }
5196 /* Since an archive library is an abstract, local storage for packed data, in complex
5197 * production files with many layers of libraries, a single archive library may end up
5198 * 'containing' packed IDs from _different_ sources (reminder, packed IDs are stored in
5199 * their _user_ blend-files).
5200 *
5201 * This 'local merge' of all 'instances' of the same archive libraries and their packed IDs
5202 * across all of the dependencies means that, across several iterations of ID expanding
5203 * from several different real library dependencies, a same local archive library (and its
5204 * Main) may be re-used for different file-data.
5205 *
5206 * So asserting that `BLI_assert(ELEM(main_it->curlib->runtime->filedata, fd, nullptr));`
5207 * is not possible here (though _usually_ true). Instead, simply ensure that the chosen
5208 * archive library does not own its file-data (it never should!), before overriding it. */
5210 main_it->curlib->runtime->filedata = fd;
5211 main_it->curlib->runtime->is_filedata_owner = false;
5212 BLI_assert(main_it->versionfile != 0);
5213 CLOG_DEBUG(&LOG,
5214 "Found archive library '%s' for the packed ID '%s'",
5215 main_it->curlib->id.name,
5216 id_name);
5217 return main_it;
5218 }
5219 }
5220 else {
5221 /* An archive library requires an existing parent library, create an empty, 'virtual' one if
5222 * needed. */
5223 Main *reference_bmain = blo_add_main_for_library(
5224 fd, bheadlib, nullptr, nullptr, lib_filepath, filepath_abs, false, is_external_lib);
5225 parent_lib = reference_bmain->curlib;
5226 CLOG_DEBUG(&LOG,
5227 "Added new parent library '%s' for file path '%s'",
5228 parent_lib->id.name,
5229 lib_filepath);
5230 }
5231 }
5232 BLI_assert(parent_lib || !is_packed_id);
5233
5234 Main *bmain = blo_add_main_for_library(fd,
5235 bheadlib,
5236 nullptr,
5237 parent_lib,
5238 lib_filepath,
5239 filepath_abs,
5240 is_packed_id,
5241 is_external_lib);
5242
5244
5245 if (is_packed_id) {
5246 CLOG_DEBUG(&LOG,
5247 "Added new archive library '%s' for the packed ID '%s'",
5248 bmain->curlib->id.name,
5249 id_name);
5250 }
5251 else {
5252 CLOG_DEBUG(
5253 &LOG, "Added new library '%s' for file path '%s'", bmain->curlib->id.name, lib_filepath);
5254 }
5255 return bmain;
5256}
5257
5258/* Actually load an ID from a library. There are three possible cases here:
5259 * - `existing_id` is non-null: calling code already found a suitable existing ID, this function
5260 * essentially then only updates the mappings for `bhead->old` address to point to the given
5261 * ID. This is the only case where `libmain` may be `nullptr`.
5262 * - The given bhead has an already loaded matching ID (found by a call to
5263 * `library_id_is_yet_read`), then once that ID is found behavior is as in the previous case.
5264 * - No matching existing ID is found, then a new one is actually read from the given FileData.
5265 */
5266static void read_id_in_lib(FileData *fd,
5267 std::queue<ID *> &ids_to_expand,
5268 Main *libmain,
5269 Library *parent_lib,
5270 BHead *bhead,
5271 ID *existing_id,
5272 ID_Readfile_Data::Tags id_read_tags)
5273{
5274 ID *id = existing_id;
5275
5276 if (id == nullptr) {
5277 BLI_assert(libmain);
5278 id = library_id_is_yet_read(fd, libmain, bhead);
5279 }
5280 if (id == nullptr) {
5281 /* ID has not been read yet, add placeholder to the main of the
5282 * library it belongs to, so that it will be read later. */
5285 fd, libmain, bhead, fd->id_tag_extra | ID_TAG_INDIRECT, id_read_tags, false, &id);
5286 /* A corrupt block may fail to read, leaving `id` null.
5287 * Skip it rather than dereferencing null - #read_libblock has already advanced
5288 * past the block, and #link_named_part handles the same case this way. */
5289 if (id == nullptr) {
5290 return;
5291 }
5292 id_sort_by_name(which_libbase(libmain, id->id_type()), id, static_cast<ID *>(id->prev));
5293
5294 /* commented because this can print way too much */
5295 // if (G.debug & G_DEBUG) printf("expand_doit: other lib %s\n", lib->filepath);
5296
5297 /* For outliner dependency only. */
5298 if (parent_lib) {
5299 libmain->curlib->runtime->parent = parent_lib;
5300 }
5301
5302 /* Only newly read ID needs to be added to the expand TODO queue, existing ones should already
5303 * be in it - or already have been expanded. */
5304 if (id_read_tags.needs_expanding) {
5305 ids_to_expand.push(id);
5306 }
5307 }
5308 else {
5309 /* Convert any previously read weak link to regular link to signal that we want to read this
5310 * data-block.
5311 *
5312 * Note that this function also visits already-loaded data-blocks, and thus their
5313 * `readfile_data` field might already have been freed. */
5315 id->flag &= ~ID_FLAG_INDIRECT_WEAK_LINK;
5316 }
5317
5318 /* "id" is either a placeholder or real ID that is already in the
5319 * main of the library (A) it belongs to. However it might have been
5320 * put there by another library (C) which only updated its own
5321 * fd->libmap. In that case we also need to update the fd->libmap
5322 * of the current library (B) so we can find it for lookups.
5323 *
5324 * An example of such a setup is:
5325 * (A) tree.blend: contains Tree object.
5326 * (B) forest.blend: contains Forest collection linking in Tree from tree.blend.
5327 * (C) shot.blend: links in both Tree from tree.blend and Forest from forest.blend.
5328 */
5329 oldnewmap_lib_insert(fd, bhead->old, id, bhead->code);
5330
5331 /* Commented because this can print way too much. */
5332#if 0
5333 if (G.debug & G_DEBUG) {
5334 printf("expand_doit: already linked: %s lib: %s\n", id->name, lib->filepath);
5335 }
5336#endif
5337 }
5338}
5339
5340static void expand_doit_library(void *fdhandle,
5341 std::queue<ID *> &ids_to_expand,
5342 Main *mainvar,
5343 void *old)
5344{
5345 FileData *fd = static_cast<FileData *>(fdhandle);
5346
5347 if (mainvar->is_read_invalid) {
5348 return;
5349 }
5350
5351 BHead *bhead = find_bhead(fd, old);
5352 if (bhead == nullptr) {
5353 return;
5354 }
5355 /* In 2.50+ file identifier for screens is patched, forward compatibility. */
5356 if (bhead->code == ID_SCRN) {
5357 bhead->code = ID_SCR;
5358 }
5359 if (!blo_bhead_is_id_valid_type(bhead)) {
5360 return;
5361 }
5362 const char *id_name = blo_bhead_id_name(fd, bhead);
5363 if (!id_name) {
5364 /* Do not allow linking ID which names are invalid (likely coming from a future version of
5365 * Blender allowing longer names). */
5366 return;
5367 }
5368 const bool is_packed_id = (blo_bhead_id_flag(fd, bhead) & ID_FLAG_LINKED_AND_PACKED) != 0;
5369
5370 BLI_assert_msg(!is_packed_id || bhead->code != ID_LINK_PLACEHOLDER,
5371 "A link placeholder ID (aka reference to some ID linked from another library) "
5372 "should never be packed.");
5373
5374 if (bhead->code == ID_LI) {
5375 /* Currently, we do no support expanding library usages/pointers in linked IDs.
5376 * This can happen e.g. with some data-block custom property referencing a library, or when
5377 * linking a collection import referencing its external archive library.
5378 *
5379 * This is a dangerous thing to try in general, as unlike almost any other ID types, libraries
5380 * are more of a runtime ID than anything (they are always local, only written on disk as file
5381 * reference & namespace, and are deduplicated on load - such that if a same library is used by
5382 * several other dependencies, there is still only one ID for it in the final Main).
5383 *
5384 * Supporting expanding of non-archive library should be fairly straightforward (based on their
5385 * absolute filepath), whether they are regular blendfile ones, new or external ones.
5386 *
5387 * Supporting expanding of archive libraries however is significantly more complex:
5388 * - Archive libraries for packed data are essentially purely runtime data. They are only
5389 * used as name-space, and there is not even any guarantee that packed IDs end up in the
5390 * same archive library name-space, depending on linking order etc.
5391 * - External archive libraries are more of an actual 'real container', their IDs are not
5392 * expected to move between them, even after a save & reload cycle. So we _could_ assign
5393 * some form of unique identifier to them, maybe abusing the 'deep hash' of packed IDs for
5394 * that (since libraries themselves are never packed)... not convinced that this would be
5395 * worth the complexity though.
5396 *
5397 * Note: Commented below is some initial attempt at supporting library expanding, not working
5398 * with archive ones though.
5399 */
5400 return;
5401#if 0
5402 if (library_id_is_yet_read(fd, mainvar, bhead)) {
5403 return;
5404 }
5405
5406 Library *lib = reinterpret_cast<Library *>(
5407 read_id_struct(fd, bhead, "Data for Library ID type", INDEX_ID_NULL));
5408 if (lib->flag & LIBRARY_FLAG_IS_ARCHIVE) {
5410 MEM_delete(lib);
5411 return;
5412 }
5413 const bool is_external_lib = bool(lib->flag & LIBRARY_FLAG_IS_EXTERNAL);
5414
5416 fd, bhead, lib->filepath, fd->relabase, nullptr, nullptr, false, is_external_lib);
5417 MEM_delete(lib);
5418
5419 if (libmain->curlib == nullptr) {
5422 RPT_("LIB: Data refers to main .blend file: '%s' from %s"),
5423 id_name ? id_name : "<InvalidIDName>",
5424 mainvar->curlib->runtime->filepath_abs);
5425 return;
5426 }
5427#endif
5428 }
5429 else if (bhead->code == ID_LINK_PLACEHOLDER) {
5430 /* Placeholder link to data-block in another library. */
5431 BHead *bheadlib = find_previous_lib(fd, bhead);
5432 if (bheadlib == nullptr) {
5434 RPT_ERROR,
5435 RPT_("LIB: .blend file %s seems corrupted, no owner 'Library' data found "
5436 "for the linked data-block '%s'. Try saving the file again."),
5437 mainvar->curlib->runtime->filepath_abs,
5438 id_name ? id_name : "<InvalidIDName>");
5439 return;
5440 }
5441
5442 Library *lib = read_library_struct(fd, bheadlib);
5443 if (lib == nullptr) {
5444 return;
5445 }
5446 const bool is_external_lib = bool(lib->flag & LIBRARY_FLAG_IS_EXTERNAL);
5447 if (is_external_lib) {
5448 CLOG_WARN(&LOG,
5449 "External libraries are not supported for link/append. ID '%s' belongs to "
5450 "external archive '%s'",
5451 id_name ? id_name : "<InvalidIDName>",
5452 lib->filepath);
5453 MEM_delete(lib);
5454 return;
5455 }
5456
5458 fd, bheadlib, lib->filepath, fd->relabase, nullptr, nullptr, false, is_external_lib);
5459 MEM_delete(lib);
5460
5461 if (libmain->curlib == nullptr) {
5464 RPT_("LIB: Data refers to main .blend file: '%s' from %s"),
5465 id_name ? id_name : "<InvalidIDName>",
5466 mainvar->curlib->runtime->filepath_abs);
5467 return;
5468 }
5469
5470 /* Placeholders never need expanding, as they are a mere reference to ID from another
5471 * library/blendfile. */
5472 read_id_in_lib(fd, ids_to_expand, libmain, mainvar->curlib, bhead, nullptr, {});
5473 }
5474 else if (is_packed_id) {
5475 /* Packed data-block from another library. */
5476
5477 /* That exact same packed ID may have already been read before. */
5478 if (ID *existing_id = library_id_is_yet_read_deep_hash(fd, bhead)) {
5479 /* Ensure that the current BHead's `old` pointer will also be remapped to the found existing
5480 * ID. */
5481 read_id_in_lib(fd, ids_to_expand, nullptr, nullptr, bhead, existing_id, {});
5482 return;
5483 }
5484
5485 BHead *bheadlib = find_previous_lib(fd, bhead);
5486 if (bheadlib == nullptr) {
5488 RPT_ERROR,
5489 RPT_("LIB: .blend file %s seems corrupted, no owner 'Library' data found "
5490 "for the packed linked data-block %s. Try saving the file again."),
5491 mainvar->curlib->runtime->filepath_abs,
5492 id_name ? id_name : "<InvalidIDName>");
5493 return;
5494 }
5495
5496 Library *lib = read_library_struct(fd, bheadlib);
5497 if (lib == nullptr) {
5498 return;
5499 }
5500 const bool is_external_lib = bool(lib->flag & LIBRARY_FLAG_IS_EXTERNAL);
5501 if (is_external_lib) {
5502 CLOG_WARN(&LOG,
5503 "External libraries are not supported for link/append. ID '%s' belongs to "
5504 "external archive '%s'",
5505 id_name ? id_name : "<InvalidIDName>",
5506 lib->filepath);
5507 MEM_delete(lib);
5508 return;
5509 }
5510
5512 fd, bheadlib, lib->filepath, fd->relabase, bhead, id_name, is_packed_id, is_external_lib);
5513 MEM_delete(lib);
5514
5515 if (libmain->curlib == nullptr) {
5518 RPT_("LIB: Data refers to main .blend file: '%s' from %s"),
5519 id_name ? id_name : "<InvalidIDName>",
5520 mainvar->curlib->runtime->filepath_abs);
5521 return;
5522 }
5523
5524 ID_Readfile_Data::Tags id_read_tags{};
5525 id_read_tags.needs_expanding = true;
5526 read_id_in_lib(fd, ids_to_expand, libmain, nullptr, bhead, nullptr, id_read_tags);
5527 }
5528 else {
5529 /* Data-block in same library. */
5530 ID_Readfile_Data::Tags id_read_tags{};
5531 id_read_tags.needs_expanding = true;
5532 read_id_in_lib(fd, ids_to_expand, mainvar, nullptr, bhead, nullptr, id_read_tags);
5533 }
5534}
5535
5537{
5538 /* Embedded IDs are not known by lib_link code, so they would be remapped to `nullptr`. But there
5539 * is no need to process them anyway, as they are already handled during the 'read_data' phase.
5540 */
5542 return IDWALK_RET_NOP;
5543 }
5544
5545 /* Do not expand weak links. These are used when the user interface links to scene data,
5546 * but we don't want to bring along such datablocks with a workspace. */
5547 if (cb_data->cb_flag & IDWALK_CB_DIRECT_WEAK_LINK) {
5548 return IDWALK_RET_NOP;
5549 }
5550
5551 /* Explicitly requested to be ignored during readfile processing. Means the read_data code
5552 * already handled this pointer. Typically, the 'owner_id' pointer of an embedded ID. */
5553 if (cb_data->cb_flag & IDWALK_CB_READFILE_IGNORE) {
5554 return IDWALK_RET_NOP;
5555 }
5556
5557 /* Expand process can be re-entrant or have other complex interactions that will not work well
5558 * with loop-back pointers. Further more, processing such data should not be needed here anyway.
5559 *
5560 * The exception are root liboverride pointers - when linking a data from a liboverride
5561 * hierarchy, it is mandatory to get the whole hierarchy in.
5562 */
5563 if ((cb_data->cb_flag & IDWALK_CB_LOOPBACK) &&
5565 {
5566 return IDWALK_RET_NOP;
5567 }
5568
5569 BlendExpander *expander = static_cast<BlendExpander *>(cb_data->user_data);
5570 ID *id = *(cb_data->id_pointer);
5571
5572 if (id) {
5573 expander->callback(expander->fd, expander->ids_to_expand, expander->main, id);
5574 }
5575
5576 return IDWALK_RET_NOP;
5577}
5578
5579static void expand_main(void *fdhandle, Main *mainvar, BLOExpandDoitCallback callback)
5580{
5581 FileData *fd = static_cast<FileData *>(fdhandle);
5582 BlendExpander expander = {fd, {}, mainvar, callback};
5583
5584 /* Note: Packed IDs are the only current case where IDs read/loaded from a library blendfile will
5585 * end up in another Main (outside of placeholders, which never need to be expanded). This is not
5586 * a problem for initialization of the 'to be expanded' queue though, as no packed ID can be
5587 * directly linked currently, they are only brought in indirectly, i.e. during the expansion
5588 * process itself.
5589 *
5590 * So just looping on the 'main'/root Main of the read library is fine here currently. */
5591 ID *id_iter;
5592 FOREACH_MAIN_ID_BEGIN (mainvar, id_iter) {
5594 expander.ids_to_expand.push(id_iter);
5595 }
5596 }
5598
5599 while (!expander.ids_to_expand.empty()) {
5600 id_iter = expander.ids_to_expand.front();
5601 expander.ids_to_expand.pop();
5603
5604 /* Original (current) ID pointer can be considered as valid, but _not_ its own pointers to
5605 * other IDs - the already loaded ones will be valid, but the yet-to-be-read ones will not.
5606 * Expanding should _not_ require processing of UI ID pointers.
5607 * Expanding should never modify ID pointers themselves.
5608 * Handling of DNA deprecated data should never be needed in undo case. */
5610 ((!fd || (fd->flags & FD_FLAGS_IS_MEMFILE)) ?
5611 IDWALK_NOP :
5613 BKE_library_foreach_ID_link(nullptr, id_iter, expand_cb, &expander, flag);
5614
5616 }
5617}
5618
5620
5621/* -------------------------------------------------------------------- */
5624
5625/* returns true if the item was found
5626 * but it may already have already been appended/linked */
5628 Main *mainl, FileData *fd, const short idcode, const char *name, const int flag)
5629{
5630 BHead *bhead = find_bhead_from_code_name(fd, idcode, name);
5631 ID *id;
5632
5633 const bool use_placeholders = (flag & BLO_LIBLINK_USE_PLACEHOLDERS) != 0;
5634 const bool force_indirect = (flag & BLO_LIBLINK_FORCE_INDIRECT) != 0;
5635
5637
5638 if (bhead && blo_bhead_is_id_valid_type(bhead)) {
5639 id = library_id_is_yet_read(fd, mainl, bhead);
5640 if (id == nullptr) {
5641 /* not read yet */
5642 const eID_Tag id_tag = eID_Tag((force_indirect ? ID_TAG_INDIRECT : ID_TAG_EXTERN) |
5643 fd->id_tag_extra);
5644 ID_Readfile_Data::Tags id_read_tags{};
5645 id_read_tags.needs_expanding = true;
5646 read_libblock(fd, mainl, bhead, id_tag, id_read_tags, false, &id);
5647
5648 if (id) {
5649 /* sort by name in list */
5650 ListBaseT<ID> *lb = which_libbase(mainl, idcode);
5651 id_sort_by_name(lb, id, nullptr);
5652 }
5653 }
5654 else {
5655 /* already linked */
5656 CLOG_WARN(&LOG, "Append: ID '%s' is already linked", id->name);
5657 oldnewmap_lib_insert(fd, bhead->old, id, bhead->code);
5658 if (!force_indirect && (id->tag & ID_TAG_INDIRECT)) {
5659 id->tag &= ~ID_TAG_INDIRECT;
5660 id->flag &= ~ID_FLAG_INDIRECT_WEAK_LINK;
5661 id->tag |= ID_TAG_EXTERN;
5662 }
5663 }
5664 }
5665 else if (use_placeholders) {
5666 /* XXX flag part is weak! */
5667 id = create_placeholder(
5668 mainl, idcode, name, force_indirect ? ID_TAG_INDIRECT : ID_TAG_EXTERN, false);
5669 }
5670 else {
5671 id = nullptr;
5672 }
5673
5674 /* NOTE: `id` may be `nullptr` even if a BHead was found, in case e.g. it is an invalid BHead. */
5675
5676 return id;
5677}
5678
5680 BlendHandle **bh,
5681 const short idcode,
5682 const char *name,
5683 const LibraryLink_Params *params)
5684{
5685 FileData *fd = reinterpret_cast<FileData *>(*bh);
5686
5687 ID *ret_id = nullptr;
5688 if (!mainl->is_read_invalid) {
5689 ret_id = link_named_part(mainl, fd, idcode, name, params->flag);
5690 }
5691
5692 if (mainl->is_read_invalid) {
5693 return nullptr;
5694 }
5695 return ret_id;
5696}
5697
5698/* common routine to append/link something from a library */
5699
5701 FileData *fd,
5702 const char *filepath,
5703 const eID_Tag id_tag_extra)
5704{
5705 Main *mainl;
5706
5707 /* Only allow specific tags to be set as extra,
5708 * otherwise this could conflict with library loading logic.
5709 * Other flags can be added here, as long as they are safe. */
5710 BLI_assert((id_tag_extra & ~ID_TAG_TEMP_MAIN) == 0);
5711
5712 fd->id_tag_extra = id_tag_extra;
5713
5714 fd->bmain = mainvar;
5715
5716 /* Add already existing packed data-blocks to map so that they are not loaded again. */
5717 ID *id;
5718 FOREACH_MAIN_ID_BEGIN (mainvar, id) {
5719 if (ID_IS_PACKED(id)) {
5720 fd->id_by_deep_hash->add(id->deep_hash, id);
5721 }
5722 }
5724
5725 /* make mains */
5726 blo_split_main(mainvar);
5727
5728 /* Find or create a Main matching the current library filepath. */
5729 /* Note: Directly linking packed IDs is not supported currently. */
5731 fd, nullptr, filepath, BKE_main_blendfile_path(mainvar), nullptr, nullptr, false, false);
5732 fd->fd_bmain = mainl;
5733 if (mainl->curlib) {
5734 mainl->curlib->runtime->filedata = fd;
5735 /* This filedata is owned and managed by the calling code. */
5736 mainl->curlib->runtime->is_filedata_owner = false;
5737 }
5738
5739 /* needed for do_version */
5740 mainl->versionfile = short(fd->fileversion);
5743
5744 return mainl;
5745}
5746
5748 Main *bmain,
5749 const int flag,
5750 const int id_tag_extra)
5751{
5752 memset(params, 0, sizeof(*params));
5753 params->bmain = bmain;
5754 params->flag = flag;
5755 params->id_tag_extra = id_tag_extra;
5756}
5757
5759 Main *bmain,
5760 const int flag,
5761 const int id_tag_extra,
5762 /* Context arguments. */
5763 Scene *scene,
5764 ViewLayer *view_layer,
5765 const View3D *v3d)
5766{
5767 BLO_library_link_params_init(params, bmain, flag, id_tag_extra);
5768 if (scene != nullptr) {
5769 params->context.scene = scene;
5770 params->context.view_layer = view_layer;
5771 params->context.v3d = v3d;
5772 }
5773}
5774
5776 const char *filepath,
5777 const LibraryLink_Params *params)
5778{
5779 FileData *fd = reinterpret_cast<FileData *>(*bh);
5780 return library_link_begin(params->bmain, fd, filepath, eID_Tag(params->id_tag_extra));
5781}
5782
5783static void split_main_newid(Main *mainptr, Main *main_newid)
5784{
5785 /* We only copy the necessary subset of data in this temp main. */
5786 main_newid->versionfile = mainptr->versionfile;
5787 main_newid->subversionfile = mainptr->subversionfile;
5788 STRNCPY(main_newid->filepath, mainptr->filepath);
5789 main_newid->curlib = mainptr->curlib;
5790 main_newid->colorspace = mainptr->colorspace;
5791
5792 MainListsArray lbarray = BKE_main_lists_get(*mainptr);
5793 MainListsArray lbarray_newid = BKE_main_lists_get(*main_newid);
5794 int i = lbarray.size();
5795 while (i--) {
5796 BLI_listbase_clear(lbarray_newid[i]);
5797
5798 for (ID &id : lbarray[i]->items_mutable()) {
5799 if (id.tag & ID_TAG_NEW) {
5800 BLI_remlink(lbarray[i], &id);
5801 BLI_addtail(lbarray_newid[i], &id);
5802 }
5803 }
5804 }
5805}
5806
5807static void library_link_end(Main *mainl, FileData **fd, const int flag, ReportList *reports)
5808{
5809 Main *mainvar = (*fd)->bmain;
5810 Library *curlib;
5811
5812 if (mainl->id_map == nullptr) {
5813 mainl->id_map = BKE_main_idmap_create(mainl, false, nullptr, MAIN_IDMAP_TYPE_NAME);
5814 }
5815
5816 /* make main consistent */
5817 expand_main(*fd, mainl, expand_doit_library);
5818
5820 *fd, reports, mainl->has_forward_compatibility_issues, mainl->curlib->runtime->filepath_abs);
5821
5822 /* Do this when expand found other libraries. */
5823 read_libraries(*fd);
5824
5825 curlib = mainl->curlib;
5826
5827 /* make the lib path relative if required */
5828 if (flag & FILE_RELPATH) {
5829 /* use the full path, this could have been read by other library even */
5830 STRNCPY(curlib->filepath, curlib->runtime->filepath_abs);
5831
5832 /* uses current .blend file as reference */
5834 }
5835
5836 blo_join_main(mainvar);
5837 mainl = nullptr; /* blo_join_main free's mainl, can't use anymore */
5838
5839 if (mainvar->is_read_invalid) {
5840 return;
5841 }
5842
5843 lib_link_all(*fd, mainvar);
5845 after_liblink_merged_bmain_process(mainvar, (*fd)->reports);
5846 }
5847
5848 /* Some versioning code does expect some proper userrefcounting, e.g. in conversion from
5849 * groups to collections... We could optimize out that first call when we are reading a
5850 * current version file, but again this is really not a bottle neck currently. So not worth
5851 * it. */
5852 BKE_main_id_refcount_recompute(mainvar, false);
5853
5854 /* FIXME: This is suspiciously early call compared to similar process in
5855 * #blo_read_file_internal, where it is called towards the very end, after all do_version,
5856 * liboverride updates etc. have been done. */
5857 /* FIXME: Probably also need to forbid layer collections updates until this call, as done in
5858 * #blo_read_file_internal? */
5860
5861 /* Yep, second splitting... but this is a very cheap operation, so no big deal. */
5862 blo_split_main(mainvar);
5863 Main *main_newid = BKE_main_new();
5864 for (Main *mainlib : mainvar->split_mains->as_span().drop_front(1)) {
5865
5866 BLI_assert(mainlib->versionfile != 0 || BKE_main_is_empty(mainlib));
5867 /* We need to split out IDs already existing,
5868 * or they will go again through do_versions - bad, very bad! */
5869 split_main_newid(mainlib, main_newid);
5870
5871 do_versions_after_linking((main_newid->curlib && main_newid->curlib->runtime->filedata) ?
5872 main_newid->curlib->runtime->filedata :
5873 *fd,
5874 main_newid);
5875 IMB_colormanagement_working_space_convert(main_newid, mainvar);
5876
5877 add_main_to_main(mainlib, main_newid);
5878
5879 if (mainlib->is_read_invalid) {
5880 break;
5881 }
5882 }
5883
5884 blo_join_main(mainvar);
5885
5886 if (mainvar->is_read_invalid) {
5887 BKE_main_free(main_newid);
5888 return;
5889 }
5890
5891 /* This does not take into account old, deprecated data, so we also have to do it after
5892 * `do_versions_after_linking()`. */
5893 BKE_main_id_refcount_recompute(mainvar, false);
5894
5895 /* After all data has been read and versioned, uses ID_TAG_NEW. */
5897
5899
5900 /* Apply overrides of newly linked data if needed. Already existing IDs need to split out, to
5901 * avoid re-applying their own overrides. */
5902 BLI_assert(BKE_main_is_empty(main_newid));
5903 split_main_newid(mainvar, main_newid);
5904 BKE_lib_override_library_main_validate(main_newid, (*fd)->reports->reports);
5906 add_main_to_main(mainvar, main_newid);
5907 BKE_main_free(main_newid);
5908
5909 BKE_main_id_tag_all(mainvar, ID_TAG_NEW, false);
5910
5911 /* FIXME Temporary 'fix' to a problem in how temp ID are copied in
5912 * `BKE_lib_override_library_main_update`, see #103062.
5913 * Proper fix involves first addressing #90610. */
5916 }
5917
5918 /* Make all relative paths, relative to the open blend file. */
5920
5921 /* patch to prevent switch_endian happens twice */
5922 /* FIXME This is extremely bad design, #library_link_end should probably _always_ free the file
5923 * data? */
5924 if ((*fd)->flags & FD_FLAGS_SWITCH_ENDIAN) {
5925 /* Big Endian blend-files are not supported for linking. */
5927 blo_filedata_free(*fd);
5928 *fd = nullptr;
5929 }
5930
5931 /* Sanity checks. */
5932 blo_read_file_checks(mainvar);
5933}
5934
5936 BlendHandle **bh,
5937 const LibraryLink_Params *params,
5938 ReportList *reports)
5939{
5940 FileData *fd = reinterpret_cast<FileData *>(*bh);
5941
5942 if (!mainl->is_read_invalid) {
5943 library_link_end(mainl, &fd, params->flag, reports);
5944 }
5945
5946 for (Library &lib : params->bmain->libraries) {
5947 /* Now we can clear this runtime library filedata, it is not needed anymore. */
5948 /* TODO: In the future, could be worth keeping them in case data are linked from several
5949 * libraries at once? To avoid closing and re-opening the same file several times. Would need
5950 * a global cleanup callback then once all linking is done, though. */
5952 }
5953
5954 *bh = reinterpret_cast<BlendHandle *>(fd);
5955}
5956
5957void *BLO_library_read_struct(FileData *fd, BHead *bh, const char *blockname)
5958{
5959 return read_struct(fd, bh, blockname, INDEX_ID_NULL);
5960}
5961
5963
5964/* -------------------------------------------------------------------- */
5967
5968static int has_linked_ids_to_read(Main *mainvar)
5969{
5970 MainListsArray lbarray = BKE_main_lists_get(*mainvar);
5971 int a = lbarray.size();
5972 while (a--) {
5973 for (ID &id : *lbarray[a]) {
5974 if (BLO_readfile_id_runtime_tags(id).is_link_placeholder &&
5975 !(id.flag & ID_FLAG_INDIRECT_WEAK_LINK))
5976 {
5977 return true;
5978 }
5979 }
5980 }
5981
5982 return false;
5983}
5984
5986 FileData *basefd, FileData *fd, Main *mainvar, ID *id, ID **r_id)
5987{
5988 BHead *bhead = nullptr;
5989 BLI_assert_msg(!ID_IS_PACKED(id), "Packed IDs should never take this code-path.");
5990 const bool is_valid = BKE_idtype_idcode_is_linkable(id->id_type()) ||
5991 ((id->tag & ID_TAG_EXTERN) == 0);
5992
5993 if (fd) {
5994 /* About future longer ID names: This is one of the main places that prevent linking IDs with
5995 * names longer than MAX_ID_NAME - 1.
5996 *
5997 * See also #read_file_bhead_idname_map_create. */
5998 bhead = find_bhead_from_idname(fd, id->name);
5999 }
6000
6001 if (!is_valid) {
6002 BLO_reportf_wrap(basefd->reports,
6003 RPT_ERROR,
6004 RPT_("LIB: %s: '%s' is directly linked from '%s' (parent '%s'), but is a "
6005 "non-linkable data type"),
6006 BKE_idtype_idcode_to_name(id->id_type()),
6007 id->name + 2,
6008 mainvar->curlib->runtime->filepath_abs,
6010 }
6011
6013 id->flag &= ~ID_FLAG_INDIRECT_WEAK_LINK;
6014
6015 if (bhead) {
6017 // printf("read lib block %s\n", id->name);
6019 fd, mainvar, bhead, eID_Tag(id->tag), BLO_readfile_id_runtime_tags(*id), false, r_id);
6020 }
6021 else {
6022 CLOG_DEBUG(&LOG,
6023 "LIB: %s: '%s' missing from '%s', parent '%s'",
6024 BKE_idtype_idcode_to_name(id->id_type()),
6025 id->name + 2,
6026 mainvar->curlib->runtime->filepath_abs,
6028 basefd->reports->count.missing_linked_id++;
6029
6030 /* Generate a placeholder for this ID (simplified version of read_libblock actually...). */
6031 if (r_id) {
6032 *r_id = is_valid ? create_placeholder(mainvar,
6033 id->id_type(),
6034 id->name + 2,
6035 id->tag,
6036 id->override_library != nullptr) :
6037 nullptr;
6038 }
6039 }
6040}
6041
6042static void read_library_linked_ids(FileData *basefd, FileData *fd, Main *mainvar)
6043{
6044 Map<std::string, ID *> loaded_ids;
6045
6046 MainListsArray lbarray = BKE_main_lists_get(*mainvar);
6047 int a = lbarray.size();
6048 while (a--) {
6049 ID *id = lbarray[a]->first();
6050
6051 while (id) {
6052 ID *id_next = static_cast<ID *>(id->next);
6054 !(id->flag & ID_FLAG_INDIRECT_WEAK_LINK))
6055 {
6056 BLI_remlink(lbarray[a], id);
6057 if (mainvar->id_map != nullptr) {
6058 BKE_main_idmap_remove_id(mainvar->id_map, id);
6059 }
6060
6061 /* When playing with lib renaming and such, you may end with cases where
6062 * you have more than one linked ID of the same data-block from same
6063 * library. This is absolutely horrible, hence we use a ghash to ensure
6064 * we go back to a single linked data when loading the file. */
6065 ID *realid = loaded_ids.lookup_default(id->name, nullptr);
6066 if (!realid) {
6067 read_library_linked_id(basefd, fd, mainvar, id, &realid);
6068 loaded_ids.add_overwrite(id->name, realid);
6069 }
6070
6071 /* `realid` shall never be nullptr - unless some source file/lib is broken
6072 * (known case: some directly linked shape-key from a missing lib...). */
6073 // BLI_assert(*realid != nullptr);
6074
6075 /* Now that we have a real ID, replace all pointers to placeholders in
6076 * fd->libmap with pointers to the real data-blocks. We do this for all
6077 * libraries since multiple might be referencing this ID. */
6079
6080 /* Transfer the readfile data from the placeholder to the real ID, but
6081 * only if the real ID has no readfile data yet. The same realid may be
6082 * referred to by multiple placeholders. */
6083 if (realid && !realid->runtime->readfile_data) {
6084 realid->runtime->readfile_data = id->runtime->readfile_data;
6085 id->runtime->readfile_data = nullptr;
6086 }
6087
6088 /* Ensure that the runtime pointer, and its 'readfile' sub-data, are properly freed, as
6089 * this ID placeholder does not go through versioning (the usual place where this data is
6090 * freed). Since `id` is not a real ID, this shouldn't follow any pointers to embedded IDs.
6091 *
6092 * WARNING! This placeholder ID is only an ID struct, with a very small subset of regular
6093 * ID common data actually valid and needing to be freed. Therefore, calling
6094 * #BKE_libblock_free_data on it would not work. */
6096 MEM_delete(id);
6097
6098 /* A failed on-demand block read only clears `FD_FLAGS_FILE_OK` on `fd` (as for the
6099 * local-data read loop in #blo_read_file_internal), it doesn't invalidate `mainvar` by
6100 * itself. Without this check such a failure looks just like a "missing linked ID" below,
6101 * silently continuing the read instead of reporting the corrupt library and aborting.
6102 *
6103 * Note: Needs to be done at the end, to ensure placeholder `id` is correctly freed. */
6104 if (fd && !(fd->flags & FD_FLAGS_FILE_OK)) [[unlikely]] {
6105 if (!mainvar->is_read_invalid) {
6106 blo_readfile_invalidate(fd, mainvar, "Failed to read a block");
6107 }
6108 return;
6109 }
6110 }
6111 id = id_next;
6112 }
6113
6114 loaded_ids.clear();
6115 }
6116
6118 basefd->reports->reports,
6120 mainvar->curlib->runtime->filepath_abs);
6121}
6122
6123static void read_library_clear_weak_links(FileData *basefd, Main *mainvar)
6124{
6125 /* Any remaining weak links at this point have been lost, silently drop
6126 * those by setting them to nullptr pointers. */
6127 MainListsArray lbarray = BKE_main_lists_get(*mainvar);
6128 int a = lbarray.size();
6129 while (a--) {
6130 ID *id = lbarray[a]->first();
6131
6132 while (id) {
6133 ID *id_next = static_cast<ID *>(id->next);
6134
6135 /* This function also visits already-loaded data-blocks, and thus their
6136 * `readfile_data` field might already have been freed. */
6139 {
6140 CLOG_DEBUG(&LOG, "Dropping weak link to '%s'", id->name);
6141 change_link_placeholder_to_real_ID_pointer(basefd, id, nullptr);
6142 /* Some ID-specific data may already have been allocated (e.g. the ID::runtime), so some
6143 * low-level proper ID 'deletion' is needed here. */
6144 BKE_libblock_free_data(id, false);
6145 BLI_freelinkN(lbarray[a], id);
6146 }
6147 id = id_next;
6148 }
6149 }
6150}
6151
6152static FileData *read_library_file_data(FileData *basefd, Main *bmain, Main *lib_bmain)
6153{
6154 FileData *fd = lib_bmain->curlib->runtime->filedata;
6155
6156 if (fd != nullptr) {
6157 /* File already open. */
6158 return fd;
6159 }
6160
6161 if (lib_bmain->curlib->packedfile) {
6162 /* Read packed file. */
6163 const PackedFile *pf = lib_bmain->curlib->packedfile;
6164
6165 BLO_reportf_wrap(basefd->reports,
6166 RPT_INFO,
6167 RPT_("Read packed library: '%s', parent '%s'"),
6168 lib_bmain->curlib->filepath,
6169 library_parent_filepath(lib_bmain->curlib));
6170 fd = blo_filedata_from_memory(pf->data, pf->size, basefd->reports);
6171
6172 /* Needed for library_append and read_libraries. */
6173 STRNCPY(fd->relabase, lib_bmain->curlib->runtime->filepath_abs);
6174 }
6175 else {
6176 /* Read file on disk. */
6177 BLO_reportf_wrap(basefd->reports,
6178 RPT_INFO,
6179 RPT_("Read library: '%s', '%s', parent '%s'"),
6180 lib_bmain->curlib->runtime->filepath_abs,
6181 lib_bmain->curlib->filepath,
6182 library_parent_filepath(lib_bmain->curlib));
6183 fd = blo_filedata_from_file(lib_bmain->curlib->runtime->filepath_abs, basefd->reports);
6184 }
6185
6186 if (fd) {
6187 /* `mainptr` is sharing the same `split_mains`, so all libraries are added immediately in a
6188 * single vectorset. It used to be that all FileData's had their own list, but with indirectly
6189 * linking this meant that not all duplicate libraries were caught properly. */
6190 fd->bmain = bmain;
6191 fd->fd_bmain = lib_bmain;
6192
6193 fd->reports = basefd->reports;
6194
6195 if (fd->libmap) {
6197 }
6198
6199 fd->libmap = oldnewmap_new();
6200 fd->id_by_deep_hash = basefd->id_by_deep_hash;
6201
6202 lib_bmain->curlib->runtime->filedata = fd;
6203 lib_bmain->curlib->runtime->is_filedata_owner = true;
6204 lib_bmain->versionfile = fd->fileversion;
6205
6206 /* subversion */
6207 read_file_version_and_colorspace(fd, lib_bmain);
6209 }
6210 else {
6211 lib_bmain->curlib->runtime->filedata = nullptr;
6212 lib_bmain->curlib->runtime->is_filedata_owner = false;
6213 lib_bmain->curlib->id.tag |= ID_TAG_MISSING;
6214 /* Set lib version to current main one... Makes assert later happy. */
6215 lib_bmain->versionfile = lib_bmain->curlib->runtime->versionfile = bmain->versionfile;
6216 lib_bmain->subversionfile = lib_bmain->curlib->runtime->subversionfile = bmain->subversionfile;
6217 lib_bmain->colorspace = lib_bmain->curlib->runtime->colorspace = bmain->colorspace;
6218 }
6219
6220 if (fd == nullptr) {
6221 BLO_reportf_wrap(basefd->reports,
6222 RPT_INFO,
6223 RPT_("Cannot find lib '%s'"),
6224 lib_bmain->curlib->runtime->filepath_abs);
6225 basefd->reports->count.missing_libraries++;
6226 }
6227
6228 return fd;
6229}
6230
6237{
6238 for (Main *libmain : basefd->bmain->split_mains->as_span().drop_front(1)) {
6239 for (ListBaseT<ID> *lb : BKE_main_lists_get(*libmain)) {
6240 for (ID &id : lb->items_mutable()) {
6241 if (!BLO_readfile_id_runtime_tags(id).is_link_placeholder) {
6242 continue;
6243 }
6244 change_link_placeholder_to_real_ID_pointer(basefd, &id, nullptr);
6245 BLI_remlink(lb, &id);
6246 if (libmain->id_map != nullptr) {
6247 BKE_main_idmap_remove_id(libmain->id_map, &id);
6248 }
6250 MEM_delete(&id);
6251 }
6252 }
6253 }
6254}
6255
6256static void read_libraries(FileData *basefd)
6257{
6258 Main *bmain = basefd->bmain;
6259 BLI_assert(bmain->split_mains);
6260 bool do_it = true;
6261
6262 BLI_SCOPED_DEFER([&]() {
6263 if (bmain->is_read_invalid) {
6265 }
6266 });
6267
6268 /* At this point the base blend file has been read, and each library blend
6269 * encountered so far has a main with placeholders for linked data-blocks.
6270 *
6271 * Now we will read the library blend files and replace the placeholders
6272 * with actual data-blocks. We loop over library mains multiple times in
6273 * case a library needs to link additional data-blocks from another library
6274 * that had been read previously. */
6275 while (do_it) {
6276 do_it = false;
6277
6278 /* Loop over mains of all library blend files encountered so far. Note
6279 * this list gets longer as more indirectly library blends are found. */
6280 for (int i = 1; i < bmain->split_mains->size(); i++) {
6281 Main *libmain = (*bmain->split_mains)[i];
6282 BLI_assert(libmain->curlib);
6283 if (libmain->is_read_invalid) [[unlikely]] {
6284 return;
6285 }
6286 /* Always skip archived libraries here, these should _never_ need to be processed here, as
6287 * their data is local data from a blendfile perspective. */
6288 if (libmain->curlib->flag & LIBRARY_FLAG_IS_ARCHIVE) {
6290 continue;
6291 }
6292 /* Does this library have any more linked data-blocks we need to read? */
6293 if (has_linked_ids_to_read(libmain)) {
6294 CLOG_DEBUG(&LOG,
6295 "Reading linked data-blocks from %s (%s)",
6296 libmain->curlib->id.name,
6297 libmain->curlib->filepath);
6298
6299 /* Open file if it has not been done yet. */
6300 FileData *fd = read_library_file_data(basefd, bmain, libmain);
6301
6302 if (fd) {
6303 if (!(fd->flags & FD_FLAGS_FILE_OK)) [[unlikely]] {
6304 if (!libmain->is_read_invalid) {
6305 blo_readfile_invalidate(fd, libmain, "Failed to open the library blendfile");
6306 }
6307 return;
6308 }
6309 do_it = true;
6310
6311 if (libmain->id_map == nullptr) {
6312 libmain->id_map = BKE_main_idmap_create(libmain, false, nullptr, MAIN_IDMAP_TYPE_NAME);
6313 }
6314 }
6315
6316 /* Read linked data-blocks for each link placeholder, and replace
6317 * the placeholder with the real data-block. */
6318 read_library_linked_ids(basefd, fd, libmain);
6319 if (libmain->is_read_invalid) [[unlikely]] {
6320 return;
6321 }
6322
6323 /* Test if linked data-blocks need to read further linked data-blocks
6324 * and create link placeholders for them. */
6325 expand_main(fd, libmain, expand_doit_library);
6326 if (libmain->is_read_invalid) [[unlikely]] {
6327 return;
6328 }
6329 }
6330 }
6331 }
6332
6333 for (Main *libmain : bmain->split_mains->as_span().drop_front(1)) {
6334 /* Drop weak links for which no data-block was found.
6335 * Since this can remap pointers in `libmap` of all libraries, it needs to be performed in its
6336 * own loop, before any call to `lib_link_all` (and the freeing of the libraries' filedata). */
6337 read_library_clear_weak_links(basefd, libmain);
6338 if (libmain->is_read_invalid) [[unlikely]] {
6339 return;
6340 }
6341 }
6342
6343 Main *main_newid = BKE_main_new();
6344 for (Main *libmain : bmain->split_mains->as_span().drop_front(1)) {
6345 /* Do versioning for newly added linked data-blocks. If no data-blocks
6346 * were read from a library versionfile will still be zero and we can
6347 * skip it. */
6348 if (libmain->versionfile) {
6349 /* Split out already existing IDs to avoid them going through
6350 * do_versions multiple times, which would have bad consequences. */
6351 split_main_newid(libmain, main_newid);
6352
6353 /* `filedata` can be NULL when loading linked data from nonexistent or invalid library
6354 * reference. Or during linking/appending, when processing data from a library not involved
6355 * in the current linking/appending operation.
6356 *
6357 * Skip versioning in these cases, since the only IDs here will be placeholders (missing
6358 * lib), or already existing IDs (linking/appending). */
6359 if (libmain->curlib->runtime->filedata) {
6360 do_versions(libmain->curlib->runtime->filedata, libmain->curlib, main_newid);
6361 }
6362
6363 add_main_to_main(libmain, main_newid);
6364 }
6365 if (libmain->is_read_invalid) [[unlikely]] {
6366 BKE_main_free(main_newid);
6367 return;
6368 }
6369
6370 /* Lib linking. */
6371 if (libmain->curlib->runtime->filedata) {
6372 lib_link_all(libmain->curlib->runtime->filedata, libmain);
6373 }
6374 if (libmain->is_read_invalid) [[unlikely]] {
6375 BKE_main_free(main_newid);
6376 return;
6377 }
6378
6379 /* NOTE: No need to call #do_versions_after_linking() or #BKE_main_id_refcount_recompute()
6380 * here, as this function is only called for library 'subset' data handling, as part of
6381 * either full blend-file reading (#blo_read_file_internal()), or library-data linking
6382 * (#library_link_end()).
6383 *
6384 * For this to work reliably, `mainptr->curlib->runtime->filedata` also needs to be freed after
6385 * said versioning code has run. */
6386 }
6387 BKE_main_free(main_newid);
6388}
6389
6391 void *new_address,
6392 const void * /*old_address*/,
6393 const int64_t alloc_len,
6394 const size_t expected_size)
6395{
6396 if (new_address != nullptr) {
6397 /* Not testing equality, since size might have been aligned up,
6398 * or might be passed the size of a base struct with inheritance.
6399 *
6400 * Note we cast to size_t so integer overflow will fail the check. */
6401 if (size_t(alloc_len) < expected_size) {
6402 if (fd->bmain) {
6404 (*fd->bmain->split_mains)[fd->bmain->split_mains->size() - 1],
6405 "Corrupt .blend file, unexpected data size.");
6406 }
6407 /* Return null to trigger a hard-crash rather than allowing readfile code to further access
6408 * this invalid block of memory.
6409 *
6410 * It could also potentially allow the calling code to do its own error checking and abort
6411 * reading process, but that is not implemented currently. */
6412 return nullptr;
6413 }
6414 }
6415
6416 return new_address;
6417}
6418
6419void *blo_read_raw_address_impl(BlendDataReader *reader, const void *old_address)
6420{
6421 return newdataadr(reader->fd, old_address);
6422}
6423
6425 const void *old_address,
6426 const size_t expected_size)
6427{
6428 NewAddress *entry = newdataadr_entry(reader->fd, old_address);
6429 if (entry == nullptr) {
6430 return nullptr;
6431 }
6432 void *new_address = blo_verify_data_address(
6433 reader->fd, entry->newp, old_address, entry->alloc_len, expected_size);
6434 if (new_address) {
6435 entry->nr++;
6436 }
6437 return new_address;
6438}
6439
6441 const void *old_address,
6442 const size_t expected_size)
6443{
6444 int64_t alloc_len = 0;
6445 void *new_address = newdataadr_no_us(reader->fd, old_address, &alloc_len);
6446 return blo_verify_data_address(reader->fd, new_address, old_address, alloc_len, expected_size);
6447}
6448
6450 const void *old_address,
6451 void *new_address)
6452{
6453 if (old_address != nullptr && new_address == nullptr) {
6455 (*fd->bmain->split_mains)[fd->bmain->split_mains->size() - 1],
6456 "Corrupt .blend file, missing required data block.");
6457 }
6458 return new_address;
6459}
6460
6462 const void *old_address,
6463 const size_t expected_size)
6464{
6465 void *new_address = blo_read_struct_impl(reader, old_address, expected_size);
6466 return blo_check_data_address_nonnull(reader->fd, old_address, new_address);
6467}
6468
6470 const void *old_address,
6471 const size_t expected_size)
6472{
6473 void *new_address = blo_read_struct_no_us_impl(reader, old_address, expected_size);
6474 return blo_check_data_address_nonnull(reader->fd, old_address, new_address);
6475}
6476
6478 const int64_t array_size,
6479 const int elems,
6480 const size_t elem_size,
6481 void **ptr_p)
6482{
6483 int64_t total_elems;
6484 if (elems == 1) {
6485 total_elems = array_size;
6486 }
6487 else if (!MEM_size_safe_multiply(array_size, int64_t(elems), &total_elems)) {
6488 total_elems = -1;
6489 }
6490 if (total_elems < 0) {
6492 reader->fd,
6493 (*reader->fd->bmain->split_mains)[reader->fd->bmain->split_mains->size() - 1],
6494 "Corrupt .blend file, array size integer overflow or invalid.");
6495 *ptr_p = nullptr;
6496 return false;
6497 }
6498
6499 NewAddress *entry = newdataadr_entry(reader->fd, *ptr_p);
6500 if (entry == nullptr) {
6501 *ptr_p = nullptr;
6502 return total_elems == 0;
6503 }
6504 if (elem_size > 0) {
6505 const int64_t max_array_size = entry->alloc_len / int64_t(elem_size);
6506 if (total_elems > max_array_size) {
6508 reader->fd,
6509 (*reader->fd->bmain->split_mains)[reader->fd->bmain->split_mains->size() - 1],
6510 "Corrupt .blend file, array size exceeds allocated size.");
6511 *ptr_p = nullptr;
6512 return false;
6513 }
6514 }
6515 BLI_assert((reader->fd->flags & FD_FLAGS_SWITCH_ENDIAN) == 0);
6516 entry->nr++;
6517 *ptr_p = entry->newp;
6518 return true;
6519}
6520
6522 const StringRef struct_name,
6523 const int64_t items_num,
6524 const void *old_address)
6525{
6526 const int struct_index = DNA_struct_find_with_alias(reader->fd->memsdna, struct_name);
6527 BLI_assert(DNA_struct_identifier(const_cast<SDNA *>(reader->fd->memsdna), struct_index) ==
6528 struct_name);
6529 const size_t struct_size = size_t(DNA_struct_size(reader->fd->memsdna, struct_index));
6530 return blo_read_struct_impl(reader, old_address, struct_size * items_num);
6531}
6532
6534 ID *self_id,
6535 const bool is_linked_only,
6536 ID *id)
6537{
6538 return static_cast<ID *>(newlibadr(reader->fd, self_id, is_linked_only, id));
6539}
6540
6542{
6543 return BKE_main_idmap_lookup_uid(reader->fd->new_idmap_uid, session_uid);
6544}
6545
6547{
6548 return reader->fd->fileversion;
6549}
6550
6552 const size_t expected_elem_size,
6553 ListBase *list)
6554{
6556 return;
6557 }
6558
6559 list->first_ = blo_read_struct_impl(reader, list->first_, expected_elem_size);
6560 Link *ln = static_cast<Link *>(list->first_);
6561 Link *prev = nullptr;
6562 while (ln) {
6563 ln->next = static_cast<Link *>(blo_read_struct_impl(reader, ln->next, expected_elem_size));
6564 ln->prev = prev;
6565 prev = ln;
6566 ln = ln->next;
6567 }
6568 list->last_ = prev;
6569}
6570
6576 NewAddress &entry,
6577 const void *old_address,
6578 FunctionRef<size_t(const void *data)> type_size_fn)
6579{
6580 const size_t type_size = type_size_fn(entry.newp);
6581 if (blo_verify_data_address(fd, entry.newp, old_address, entry.alloc_len, type_size)) {
6582 return true;
6583 }
6584 if (entry.nr == 0) {
6585 MEM_delete_void(entry.newp);
6586 }
6587 fd->datamap->map.remove(old_address);
6588 return false;
6589}
6590
6592 const size_t base_size,
6593 ListBase *list,
6594 FunctionRef<size_t(const void *data)> type_size_fn)
6595{
6596 FileData *fd = reader->fd;
6597 const void *old_address = list->first_;
6598 Link *prev = nullptr;
6599 list->first_ = nullptr;
6600
6601 while (old_address) {
6602 NewAddress *entry = newdataadr_entry(fd, old_address);
6603 if (entry == nullptr) {
6604 break;
6605 }
6606 Link *ln = static_cast<Link *>(
6607 blo_verify_data_address(fd, entry->newp, old_address, entry->alloc_len, base_size));
6608 if (ln == nullptr) {
6609 break;
6610 }
6611
6612 /* Skip items too small for their type, continuing with the next one. */
6613 const void *old_address_item = old_address;
6614 old_address = ln->next;
6615 if (!blo_read_verify_type_size(fd, *entry, old_address_item, type_size_fn)) {
6616 continue;
6617 }
6618
6619 entry->nr++;
6620 ln->next = nullptr;
6621 ln->prev = prev;
6622 if (prev) {
6623 prev->next = ln;
6624 }
6625 else {
6626 list->first_ = ln;
6627 }
6628 prev = ln;
6629 }
6630 list->last_ = prev;
6631}
6632
6634 const void *old_address,
6635 const size_t base_size,
6636 FunctionRef<size_t(const void *data)> type_size_fn)
6637{
6638 FileData *fd = reader->fd;
6639 NewAddress *entry = newdataadr_entry(fd, old_address);
6640 if (entry == nullptr) {
6641 return nullptr;
6642 }
6643 void *new_address = blo_verify_data_address(
6644 fd, entry->newp, old_address, entry->alloc_len, base_size);
6645 if (new_address == nullptr || !blo_read_verify_type_size(fd, *entry, old_address, type_size_fn))
6646 {
6647 return nullptr;
6648 }
6649 entry->nr++;
6650 return new_address;
6651}
6652
6653void BLO_read_string(BlendDataReader *reader, char **ptr_p)
6654{
6655#ifndef NDEBUG
6656 int64_t alloc_len = 0;
6657 *ptr_p = static_cast<char *>(newdataadr(reader->fd, *ptr_p, &alloc_len));
6658
6659 const char *str = *ptr_p;
6660 if (str) {
6661 /* Verify that we have a null terminator. */
6662 for (int64_t len = alloc_len; len > 0; len--) {
6663 if (str[len - 1] == '\0') {
6664 return;
6665 }
6666 }
6667
6668 BLI_assert_msg(0, "Corrupt .blend file, expected string to be null terminated.");
6669 }
6670#else
6671 BLO_read_raw_address(reader, ptr_p);
6672#endif
6673}
6674
6675void BLO_read_string(BlendDataReader *reader, char *const *ptr_p)
6676{
6677 BLO_read_string(reader, const_cast<char **>(ptr_p));
6678}
6679
6680void BLO_read_string(BlendDataReader *reader, const char **ptr_p)
6681{
6682 BLO_read_string(reader, const_cast<char **>(ptr_p));
6683}
6684
6686 const int64_t array_size,
6687 const uint64_t *src,
6688 uint32_t *dst)
6689{
6690 BLI_assert((reader->fd->flags & FD_FLAGS_SWITCH_ENDIAN) == 0);
6691 UNUSED_VARS_NDEBUG(reader);
6692 for (int i = 0; i < array_size; i++) {
6693 dst[i] = uint32_from_uint64_ptr(src[i]);
6694 }
6695}
6696
6698 const int64_t array_size,
6699 const uint32_t *src,
6700 uint64_t *dst)
6701{
6702 /* Match pointer conversion rules from bh8_from_bh4 and cast_pointer_32_to_64. */
6703 for (int i = 0; i < array_size; i++) {
6704 dst[i] = src[i];
6705 }
6706}
6707
6708bool blo_read_pointer_array_impl(BlendDataReader *reader, const int64_t array_size, void **ptr_p)
6709{
6710 FileData *fd = reader->fd;
6711
6712 if (array_size < 0) {
6714 fd,
6715 (*fd->bmain->split_mains)[fd->bmain->split_mains->size() - 1],
6716 "Corrupt .blend file, pointer array size integer overflow or invalid.");
6717 *ptr_p = nullptr;
6718 return false;
6719 }
6720
6721 NewAddress *entry = newdataadr_entry(fd, *ptr_p);
6722 if (entry == nullptr) {
6723 /* See comment in #blo_read_array_impl. */
6724 *ptr_p = nullptr;
6725 return array_size == 0;
6726 }
6727
6728 const int file_pointer_size = fd->filesdna->pointer_size;
6729 const int current_pointer_size = fd->memsdna->pointer_size;
6730
6731 const int64_t max_array_size = entry->alloc_len / file_pointer_size;
6732 if (array_size > max_array_size) {
6734 (*fd->bmain->split_mains)[fd->bmain->split_mains->size() - 1],
6735 "Corrupt .blend file, pointer array size exceeds allocated size.");
6736 *ptr_p = nullptr;
6737 return false;
6738 }
6739
6740 entry->nr++;
6741 void *orig_array = entry->newp;
6742 void *final_array = nullptr;
6743
6744 BLI_assert((reader->fd->flags & FD_FLAGS_SWITCH_ENDIAN) == 0);
6745
6746 if (file_pointer_size == current_pointer_size) {
6747 /* No pointer conversion necessary. */
6748 final_array = orig_array;
6749 }
6750 else if (file_pointer_size == 8 && current_pointer_size == 4) {
6751 /* Convert pointers from 64 to 32 bit. */
6752 final_array = MEM_new_array_uninitialized(array_size, 4, "new pointer array");
6754 array_size,
6755 static_cast<uint64_t *>(orig_array),
6756 static_cast<uint32_t *>(final_array));
6757 MEM_delete_void(orig_array);
6758 }
6759 else if (file_pointer_size == 4 && current_pointer_size == 8) {
6760 /* Convert pointers from 32 to 64 bit. */
6761 final_array = MEM_new_array_uninitialized(array_size, 8, "new pointer array");
6763 array_size,
6764 static_cast<uint32_t *>(orig_array),
6765 static_cast<uint64_t *>(final_array));
6766 MEM_delete_void(orig_array);
6767 }
6768 else {
6770 }
6771
6772 *ptr_p = final_array;
6773 return true;
6774}
6775
6777 BlendDataReader *reader,
6778 const void **ptr_p,
6779 const FunctionRef<const ImplicitSharingInfo *()> read_fn)
6780{
6781 const uint64_t old_address_id = uint64_t(*ptr_p);
6782 if (BLO_read_data_is_undo(reader)) {
6783 if (reader->fd->flags & FD_FLAGS_IS_MEMFILE) {
6784 UndoReader *undo_reader = reinterpret_cast<UndoReader *>(reader->fd->file);
6785 const MemFile &memfile = *undo_reader->memfile;
6786 if (memfile.shared_storage) {
6787 /* Check if the data was saved with sharing-info. */
6788 if (const ImplicitSharingInfoAndData *sharing_info_data =
6789 memfile.shared_storage->sharing_info_by_address_id.lookup_ptr(old_address_id))
6790 {
6791 /* Add a new owner of the data that is passed to the caller. */
6792 sharing_info_data->sharing_info->add_user();
6793 return *sharing_info_data;
6794 }
6795 }
6796 }
6797 }
6798
6799 if (const ImplicitSharingInfoAndData *shared_data =
6800 reader->shared_data_by_stored_address.lookup_ptr(old_address_id))
6801 {
6802 /* The data was loaded before. No need to load it again. Just increase the user count to
6803 * indicate that it is shared. */
6804 if (shared_data->sharing_info) {
6805 shared_data->sharing_info->add_user();
6806 }
6807 return *shared_data;
6808 }
6809
6810 /* This is the first time this data is loaded. The callback also creates the corresponding
6811 * sharing info which may be reused later. */
6812 const ImplicitSharingInfo *sharing_info = read_fn();
6813 const void *new_address = *ptr_p;
6814 const ImplicitSharingInfoAndData shared_data{sharing_info, new_address};
6815 reader->shared_data_by_stored_address.add(old_address_id, shared_data);
6816 return shared_data;
6817}
6818
6820{
6821 return (reader->fd->flags & FD_FLAGS_IS_MEMFILE);
6822}
6823
6824void BLO_read_data_globmap_add(BlendDataReader *reader, void *oldaddr, void *newaddr)
6825{
6826 oldnewmap_insert(reader->fd->globmap, oldaddr, newaddr, 0);
6827}
6828
6830{
6831 link_glob_list(reader->fd, list);
6832}
6833
6835{
6836 return reader->fd->reports;
6837}
6838
6839void BLO_read_data_invalidate(BlendDataReader *reader, const char *message)
6840{
6841 FileData *fd = reader->fd;
6842 if (fd->bmain) {
6844 fd, (*fd->bmain->split_mains)[fd->bmain->split_mains->size() - 1], message);
6845 }
6846}
6847
6849{
6850 return (reader->fd->flags & FD_FLAGS_IS_MEMFILE);
6851}
6852
6854{
6855 return reader->main;
6856}
6857
6859{
6860 return reader->fd->reports;
6861}
6862
6867
6869
6870} // namespace blender
#define BLENDER_FILE_SUBVERSION
#define BLENDER_FILE_VERSION
#define IDP_BlendDataRead(reader, prop)
#define MAIN_ID_SESSION_UID_UNSET
#define FOREACH_MAIN_ID_END
Definition BKE_main.hh:735
#define MAIN_VERSION_FILE_ATLEAST(main, ver, subver)
Definition BKE_main.hh:949
#define FOREACH_MAIN_LISTBASE_ID_END
Definition BKE_main.hh:703
#define FOREACH_MAIN_LISTBASE_ID_BEGIN(_lb, _id)
Definition BKE_main.hh:697
#define FOREACH_MAIN_LISTBASE_END
Definition BKE_main.hh:715
#define FOREACH_MAIN_LISTBASE_BEGIN(_bmain, _lb)
Definition BKE_main.hh:708
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition BKE_main.hh:729
#define MAIN_VERSION_FILE_OLDER_OR_EQUAL(main, ver, subver)
Definition BKE_main.hh:957
#define BLEN_THUMB_MEMSIZE(_x, _y)
Definition BKE_main.hh:979
#define BLEN_THUMB_MEMSIZE_IS_VALID(_x, _y)
Definition BKE_main.hh:982
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
#define ATTR_FALLTHROUGH
#define L_ENDIAN
#define ENDIAN_ORDER
File and directory operations.
#define O_BINARY
#define BLI_MEMARENA_STD_BUFSIZE
#define BLI_SCOPED_DEFER(function_to_defer)
#define FILE_MAX
#define BLI_path_cmp
Platform independent time functions.
Compatibility-like things for windows.
Utilities ensuring .blend file (i.e. Main) is in valid state during write and/or read process.
#define MIN_SIZEOFBLENDERHEADER
#define BLO_read_raw_address(reader, ptr_p)
#define BLO_read_struct_list(reader, struct_name, list)
#define BLO_read_struct(reader, struct_name, ptr_p)
external readfile function prototypes.
#define BLEN_THUMB_MEMSIZE_FILE(_x, _y)
#define RPT_(msgid)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define CLOG_INFO(clg_ref,...)
Definition CLG_log.h:190
#define ID_IS_PACKED(_id)
Definition DNA_ID.h:703
#define ID_FAKE_USERS(id)
Definition DNA_ID.h:658
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define MAX_ID_NAME
Definition DNA_ID.h:367
#define INDEX_ID_MAX
Definition DNA_ID.h:1362
#define ID_TAG_KEEP_ON_UNDO
Definition DNA_ID.h:1044
#define ID_SCRN
#define ID_LINK_PLACEHOLDER
Object groups, one object can be in many groups at once.
blenloader genfile private function prototypes
#define SDNA_RAW_DATA_STRUCT_INDEX
Read Blender memory allocation and freeing.
bool MEM_size_safe_multiply(size_t a, size_t b, size_t *result)
#define BLI_assert_unreachable()
#define UNUSED_VARS(...)
#define UNUSED_VARS_NDEBUG(...)
#define SET_FLAG_FROM_TEST(value, test, flag)
#define POINTER_OFFSET(v, ofs)
#define BLI_assert_msg(a, msg)
long long int int64_t
Definition scene.h:133
void clear()
Definition BLI_map.hh:1038
bool add_overwrite(const Key &key, const Value &value)
Definition BLI_map.hh:325
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool is_empty() const
Definition BLI_set.hh:614
void add_new(const Key &key)
Definition BLI_set.hh:233
bool remove(const Key &key)
Definition BLI_set.hh:404
int64_t size() const
void append(const T &value)
#define offsetof(t, d)
#define str(s)
#define pf(_x, _i)
Prefetch 64.
Definition gim_memory.h:48
#define INT_MAX
#define main()
#define storage(slot, qualifiers)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
#define T
bool foreach_strip_adt(const AnimData &adt, FunctionRef< bool(NlaStrip *)> callback)
void node_tree_blend_read_data(BlendDataReader *reader, ID *owner_id, bNodeTree *ntree)
Definition node.cc:2087
void node_tree_update_all_new(Main &main)
Definition node.cc:5677
bNodeTree ** node_tree_ptr_from_id(ID *id)
Definition node.cc:5312
bNodeTree * node_tree_from_id(ID *id)
Definition node.cc:5337
uint32_t struct_size(Span< shader::ShaderCreateInfo::PushConst > push_constants)
BMesh Iterators.
ID_Readfile_Data::Tags & BLO_readfile_id_runtime_tags_for_write(ID &id)
Definition readfile.cc:2307
const char * BKE_idtype_idcode_to_name(short idcode)
Definition idtype.cc:261
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void blo_readfile_invalidate(FileData *fd, Main *bmain, const char *message)
Definition readfile.cc:589
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
struct DNA_ReconstructInfo * DNA_reconstruct_info_create(const struct SDNA *oldsdna, const struct SDNA *newsdna, const char *compare_flags)
void BKE_idtype_id_foreach_cache(ID *id, IDTypeForeachCacheFunctionCallback function_callback, void *user_data)
Definition idtype.cc:489
std::array< ListBaseT< ID > *, INDEX_ID_MAX - 1 > MainListsArray
Definition BKE_main.hh:797
@ SCE_READFILE_LIBLINK_NEED_SETSCENE_CHECK
static FileData * blo_filedata_from_file_minimal(const char *filepath)
Definition readfile.cc:1352
static int direct_link_id_restore_recalc_exceptions(const ID *id_current)
Definition readfile.cc:2232
static void direct_link_library(FileData *fd, Library *lib, Main *main)
Definition readfile.cc:2686
bool blo_read_array_impl(BlendDataReader *reader, int64_t array_size, int elems, size_t elem_size, void **ptr_p)
Definition readfile.cc:6477
unsigned __int64 uint64_t
Definition BKE_text.h:91
static int read_undo_remap_noundo_data_cb(LibraryIDLinkCallbackData *cb_data)
Definition readfile.cc:4352
void BLO_read_glob_list(BlendDataReader *reader, ListBase *list)
Definition readfile.cc:6829
__int64 int64_t
Definition BKE_text.h:90
BLI_STATIC_ASSERT(ARRAY_SIZE(data_mode_strings)==CTX_MODE_NUM+1, "Must have a string for each context mode") const char *CTX_data_mode_string(const bContext *C)
static void expand_doit_library(void *fdhandle, std::queue< ID * > &ids_to_expand, Main *mainvar, void *old)
Definition readfile.cc:5340
static const char * get_alloc_name(FileData *fd, BHead *bh, const char *blockname, const int id_type_index=INDEX_ID_NULL)
Definition readfile.cc:1791
const IDTypeInfo * BKE_idtype_get_info_from_idcode(short id_code)
Definition idtype.cc:227
char * STRNCPY(char(&dst)[N], const char *src)
static BHeadN * get_bhead(FileData *fd)
Definition readfile.cc:610
void BLO_library_link_params_init_with_context(LibraryLink_Params *params, Main *bmain, int flag, int id_tag_extra, Scene *scene, ViewLayer *view_layer, const View3D *v3d)
Definition readfile.cc:5758
void BLO_read_struct_list_with_size(BlendDataReader *reader, size_t expected_elem_size, ListBase *list)
Definition readfile.cc:6551
@ LIBRARY_TAG_RESYNC_REQUIRED
@ LIBRARY_IS_ASSET_EDIT_FILE
@ LIBRARY_ASSET_EDITABLE
static void * newlibadr(FileData *fd, ID *, const bool is_linked_only, const void *adr)
Definition readfile.cc:1529
static void library_filedata_release(Library *lib)
Definition readfile.cc:2568
static void oldnewmap_clear(OldNewMap *onm)
Definition readfile.cc:357
@ RPT_WARNING
Definition BKE_report.hh:40
static void placeholders_ensure_valid(Main *bmain)
Definition readfile.cc:2893
void BKE_id_delete(Main *bmain, void *idv, const BKEIDDeleteOptions &options={}) ATTR_NONNULL()
MemArena * BLI_memarena_new(size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(2) ATTR_MALLOC
static void split_main_newid(Main *mainptr, Main *main_newid)
Definition readfile.cc:5783
bool BKE_screen_blend_read_data(BlendDataReader *reader, bScreen *screen)
Definition screen.cc:238
static void * blo_check_data_address_nonnull(FileData *fd, const void *old_address, void *new_address)
Definition readfile.cc:6449
void * blo_read_struct_no_us_impl(BlendDataReader *reader, const void *old_address, size_t expected_size)
Definition readfile.cc:6440
void do_versions_after_linking_401(FileData *fd, Main *bmain)
FileData * blo_filedata_from_memfile(MemFile *memfile, const BlendFileReadParams *params, BlendFileReadReport *reports)
Definition readfile.cc:1387
static BHead * find_bhead_from_idname(FileData *fd, const char *idname)
Definition readfile.cc:5005
static int * read_file_thumbnail(FileData *fd)
Definition readfile.cc:977
static void library_link_end(Main *mainl, FileData **fd, const int flag, ReportList *reports)
Definition readfile.cc:5807
unsigned int uint
Definition BKE_text.h:55
static void read_file_version_and_colorspace(FileData *fd, Main *main)
Definition readfile.cc:506
BlendFileReadReport * BLO_read_data_reports(BlendDataReader *reader)
Definition readfile.cc:6834
static void change_link_placeholder_to_real_ID_pointer_fd(FileData *fd, const void *old, void *newp)
Definition readfile.cc:1543
@ IDTYPE_CACHE_CB_FLAGS_PERSISTENT
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
void * BLO_read_struct_by_name_array(BlendDataReader *reader, StringRef struct_name, int64_t items_num, const void *old_address)
Definition readfile.cc:6521
bool BKE_idtype_idcode_is_valid(short idcode)
Definition idtype.cc:302
void BLO_read_data_globmap_add(BlendDataReader *reader, void *oldaddr, void *newaddr)
Definition readfile.cc:6824
static ID * library_id_is_yet_read_deep_hash(FileData *fd, BHead *bhead)
Definition readfile.cc:5021
static void expand_main(void *fdhandle, Main *mainvar, BLOExpandDoitCallback callback)
Definition readfile.cc:5579
static void read_undo_libraries_preserve_never_undo_libraries(FileData *fd)
Definition readfile.cc:3222
short BLO_version_from_file(const char *filepath)
Definition readfile.cc:1485
BLI_INLINE void BLI_listbase_clear(ListBase *lb)
void * BLI_ghash_lookup(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:735
void * BLO_library_read_struct(FileData *fd, BHead *bh, const char *blockname)
Definition readfile.cc:5957
void do_versions_after_linking_270(Main *bmain)
void * blo_do_versions_newlibadr(FileData *fd, ID *self_id, const bool is_linked_only, const void *adr)
Definition readfile.cc:1534
static int verg_bheadsort(const void *v1, const void *v2)
Definition readfile.cc:4881
void blo_do_versions_key_uidgen(Key *key)
Definition readfile.cc:2493
static void do_versions_userdef(FileData *, BlendFileData *bfd)
Definition readfile.cc:3884
@ IDWALK_NO_ORIG_POINTERS_ACCESS
@ IDWALK_DO_DEPRECATED_POINTERS
static const IDHash * blo_bhead_id_deep_hash(const FileData *fd, const BHead *bhead)
Definition readfile.cc:858
void do_versions_after_linking_500(FileData *fd, Main *bmain)
bool BKE_idtype_idcode_is_linkable(short idcode)
Definition idtype.cc:307
void * blo_read_get_new_globaldata_address(FileData *fd, const void *adr)
Definition readfile.cc:1523
static void do_versions(FileData *fd, Library *lib, Main *main)
Definition readfile.cc:3895
void BKE_lib_libblock_session_uid_ensure(ID *id)
Definition lib_id.cc:1575
ID * BKE_main_idmap_lookup_name(IDNameLib_Map *id_map, short id_type, const char *name, const Library *lib) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
void blo_do_versions_dna(SDNA *sdna, int versionfile, int subversionfile)
void blo_do_versions_500(FileData *fd, Library *lib, Main *bmain)
static int lib_link_cb(LibraryIDLinkCallbackData *cb_data)
Definition readfile.cc:4073
void blo_cache_storage_init(FileData *fd, Main *bmain)
Definition readfile.cc:1706
void BKE_main_collections_parent_relations_rebuild(Main *bmain)
BlendThumbnail * BLO_thumbnail_from_file(const char *filepath)
Definition readfile.cc:1458
Main * BKE_main_new()
Definition main.cc:93
std::optional< BHead > BLO_readfile_read_bhead(FileReader *file, BHeadType type)
static bool read_libblock_undo_restore_linked(FileData *fd, Main *libmain, const ID *id, ID **r_id_old, BHead *bhead)
Definition readfile.cc:3305
void BKE_id_free_ex(Main *bmain, void *idv, int flag_orig, bool use_flag_from_idtag)
void blo_do_versions_userdef(UserDef *userdef)
uint BKE_idtype_cache_key_hash(const void *key_v)
Definition idtype.cc:30
static void read_libraries_free_placeholders(FileData *basefd)
Definition readfile.cc:6236
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:615
static void read_undo_move_libmain_data(FileData *fd, Main *libmain, BHead *bhead)
Definition readfile.cc:3119
FileReader * BLO_file_reader_uncompressed_from_descriptor(int filedes)
ID * BKE_libblock_alloc_notest(short type) ATTR_WARN_UNUSED_RESULT
Definition lib_id.cc:1427
void(*)(void *fdhandle, std::queue< ID * > &ids_to_expand, Main *mainvar, void *idv) BLOExpandDoitCallback
Definition readfile.cc:5089
static FileData * blo_filedata_from_file_open(const char *filepath, BlendFileReadReport *reports)
Definition readfile.cc:1321
@ BLENFILETYPE_BLEND
void void void BLI_movelisttolist(ListBase *dst, ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:32
static void blo_cache_storage_entry_clear_in_old(ID *, const IDCacheKey *key, void **cache_p, const uint, void *cache_storage_v)
Definition readfile.cc:1682
AssetMetaData * blo_bhead_id_asset_data_address(const FileData *fd, const BHead *bhead)
Definition readfile.cc:850
void BKE_preferences_extension_repo_read_data(struct BlendDataReader *reader, bUserExtensionRepo *repo)
ImplicitSharingInfoAndData blo_read_shared_impl(BlendDataReader *reader, const void **ptr_p, FunctionRef< const ImplicitSharingInfo *()> read_fn)
void DNA_reconstruct_info_free(struct DNA_ReconstructInfo *reconstruct_info)
BLI_INLINE bool BLI_listbase_is_empty(const ListBase *lb)
@ ID_REMAP_SKIP_NEVER_NULL_USAGE
@ ID_REMAP_NO_ORIG_POINTERS_ACCESS
@ ID_REMAP_SKIP_USER_REFCOUNT
@ ID_REMAP_SKIP_USER_CLEAR
@ ID_REMAP_SKIP_UPDATE_TAGGING
const char * BKE_main_blendfile_path_from_global()
Definition main.cc:1063
MainListsArray BKE_main_lists_get(Main &bmain)
Definition main.cc:1158
static FileData * blo_decode_and_check(FileData *fd, ReportList *reports)
Definition readfile.cc:1254
void blo_do_versions_501(FileData *fd, Library *lib, Main *bmain)
FileData * blo_filedata_from_file(const char *filepath, BlendFileReadReport *reports)
Definition readfile.cc:1336
static void read_library_linked_ids(FileData *basefd, FileData *fd, Main *mainvar)
Definition readfile.cc:6042
void do_versions_after_linking_260(Main *bmain)
static void oldnewmap_free(OldNewMap *onm)
Definition readfile.cc:368
@ LIB_ID_FREE_NO_USER_REFCOUNT
@ LIB_ID_FREE_NO_DEG_TAG
@ LIB_ID_FREE_NO_MAIN
@ LIB_ID_FREE_NO_UI_USER
@ SDNA_CMP_NOT_EQUAL
Definition DNA_genfile.h:69
@ SDNA_CMP_REMOVED
Definition DNA_genfile.h:64
static BHead * find_bhead_from_code_name(FileData *fd, const short idcode, const char *name)
Definition readfile.cc:4996
static void * newdataadr(FileData *fd, const void *adr, int64_t *r_alloc_len=nullptr)
Definition readfile.cc:1503
void BLO_read_data_invalidate(BlendDataReader *reader, const char *message)
Definition readfile.cc:6839
void * DNA_struct_reconstruct(const struct DNA_ReconstructInfo *reconstruct_info, int old_struct_index, int blocks, const void *old_blocks, const char *alloc_name, int64_t *r_alloc_size=nullptr)
const char * BKE_id_name(const ID &id)
ID * BLO_library_link_named_part(Main *mainl, BlendHandle **bh, short idcode, const char *name, const LibraryLink_Params *params)
Definition readfile.cc:5679
int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
size_t size_t size_t bool BLI_str_utf8_truncate_at_size(char *str, const size_t str_size)
static void blo_read_file_checks(Main *bmain)
Definition readfile.cc:4396
@ BLO_LIBLINK_FORCE_INDIRECT
@ BLO_LIBLINK_COLLECTION_NO_HIERARCHY_REBUILD
@ BLO_LIBLINK_USE_PLACEHOLDERS
void MEM_delete_void(void *vmemh)
static Library * read_library_struct(FileData *fd, BHead *bheadlib)
Definition readfile.cc:4936
void do_versions_after_linking_502(FileData *fd, Main *bmain)
void * blo_read_raw_address_impl(BlendDataReader *reader, const void *old_address)
Definition readfile.cc:6419
static void * oldnewmap_liblookup(OldNewMap *onm, const void *addr, const bool is_linked_only)
Definition readfile.cc:341
void blo_do_versions_300(FileData *fd, Library *lib, Main *bmain)
void BLO_library_link_params_init(LibraryLink_Params *params, Main *bmain, int flag, int id_tag_extra)
Definition readfile.cc:5747
static void read_undo_tag_all_noundo_ids(FileData *fd)
Definition readfile.cc:3007
void blo_do_versions_502(FileData *fd, Library *lib, Main *bmain)
static Main * blo_add_main_for_library(FileData *fd, const BHead *bheadlib, Library *lib, Library *reference_lib, const char *lib_filepath, char(&filepath_abs)[FILE_MAX], const bool is_packed_library, const bool is_external_lib)
Definition readfile.cc:2592
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition BLI_ghash.cc:711
void do_versions_after_linking_503(FileData *fd, Main *bmain)
static void after_liblink_merged_bmain_process(Main *bmain, BlendFileReadReport *reports)
Definition readfile.cc:4173
static void read_id_in_lib(FileData *fd, std::queue< ID * > &ids_to_expand, Main *libmain, Library *parent_lib, BHead *bhead, ID *existing_id, ID_Readfile_Data::Tags id_read_tags)
Definition readfile.cc:5266
void BKE_main_id_refcount_recompute(Main *bmain, bool do_linked_only)
Definition lib_id.cc:2131
static CLG_LogRef LOG_UNDO
Definition readfile.cc:192
void do_versions_after_linking_402(FileData *fd, Main *bmain)
void BKE_layer_collection_resync_forbid(Main &bmain)
static void read_undo_remap_noundo_data(FileData *fd)
Definition readfile.cc:4374
void BKE_libblock_free_data(ID *id, bool do_id_user) ATTR_NONNULL()
static void read_library_clear_weak_links(FileData *basefd, Main *mainvar)
Definition readfile.cc:6123
Global G
Definition blender.cc:57
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
void do_versions_after_linking_280(FileData *fd, Main *bmain)
void BKE_animdata_blend_read_data(BlendDataReader *reader, ID *id)
void blo_do_versions_401(FileData *fd, Library *lib, Main *bmain)
FileReader * BLO_file_reader_uncompressed_from_memory(const void *mem, const int memsize)
unsigned short uint16_t
Definition BKE_text.h:80
void blo_cache_storage_old_bmain_clear(FileData *fd, Main *bmain_old)
Definition readfile.cc:1742
void id_sort_by_name(ListBaseT< ID > *lb, ID *id, ID *id_sorting_hint)
Definition lib_id.cc:1888
static ID * read_id_struct(FileData *fd, BHead *bh, const char *blockname, const int id_type_index)
Definition readfile.cc:2000
AnimData * BKE_animdata_from_id(const ID *id)
Definition anim_data.cc:93
size_t void BLI_uniquename_cb(FunctionRef< bool(StringRefNull)> unique_check, const char *defname, char delim, char *name, size_t name_maxncpy) ATTR_NONNULL(2
void * MEM_new_uninitialized_aligned(size_t len, size_t alignment, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(3)
static void lib_link_all(FileData *fd, Main *bmain)
Definition readfile.cc:4101
static Main * blo_find_main_for_library_and_idname(FileData *fd, const BHead *bheadlib, const char *lib_filepath, const char *relabase, const BHead *id_bhead, const char *id_name, const bool is_packed_id, const bool is_external_lib)
Definition readfile.cc:5113
StringRef DNA_struct_identifier(struct SDNA *sdna, int struct_index)
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
void do_versions_after_linking_404(FileData *fd, Main *bmain)
bool BLO_read_data_is_undo(BlendDataReader *reader)
Definition readfile.cc:6819
unsigned int uint32_t
Definition BKE_text.h:81
static void split_libdata(ListBaseT< ID > *lb_src, Vector< Main * > &lib_main_array, const bool do_split_packed_ids)
Definition readfile.cc:427
void blo_do_versions_250(FileData *fd, Library *lib, Main *bmain)
const char * blo_bhead_library_filepath(const FileData *fd, const BHead *bhead)
Definition readfile.cc:868
@ G_FILE_RECOVER_READ
@ G_FILE_ASSET_EDIT_FILE
static void read_undo_reuse_noundo_local_ids(FileData *fd)
Definition readfile.cc:3085
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
const IDTypeInfo * BKE_idtype_get_info_from_id(const ID *id)
Definition idtype.cc:232
void blo_split_main(Main *bmain, const bool do_split_packed_ids)
Definition readfile.cc:449
static FileData * change_ID_link_filedata_get(Main *bmain, FileData *basefd)
Definition readfile.cc:1574
static void direct_link_id_override_property(BlendDataReader *reader, IDOverrideLibraryProperty *op)
Definition readfile.cc:2140
void * blo_read_struct_nonnull_impl(BlendDataReader *reader, const void *old_address, size_t expected_size)
Definition readfile.cc:6461
static bool is_minversion_older_than_blender(FileData *fd, ReportList *reports)
Definition readfile.cc:1204
static void direct_link_keymapitem(BlendDataReader *reader, wmKeyMapItem *kmi)
Definition readfile.cc:4207
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void BKE_libblock_runtime_ensure(ID &id)
Definition lib_id.cc:1403
void BKE_layer_collection_resync_allow(Main &bmain)
void BKE_asset_catalog_path_list_blend_read_data(BlendDataReader *reader, ListBaseT< AssetCatalogPathLink > &catalog_path_list)
static void read_libraries(FileData *basefd)
Definition readfile.cc:6256
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition BLI_ghash.cc:866
const char * blo_bhead_id_name(FileData *fd, const BHead *bhead)
Definition readfile.cc:828
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
const char * DNA_struct_get_compareflags(const struct SDNA *oldsdna, const struct SDNA *newsdna)
static void after_liblink_id_embedded_id_process(BlendLibReader *reader, ID *id)
Definition readfile.cc:2083
void do_versions_after_linking_400(FileData *fd, Main *bmain)
void blo_do_versions_oldnewmap_insert(OldNewMap *onm, const void *oldaddr, void *newaddr, const int nr)
Definition readfile.cc:314
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
LibraryFlag blo_bhead_library_flag(const FileData *fd, const BHead *bhead)
Definition readfile.cc:874
void BKE_object_materials_sync_length(Main *bmain, Object *ob, ID *id)
int BLI_stat(const char *path, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static FileData * filedata_new(BlendFileReadReport *reports)
Definition readfile.cc:1178
static void * newdataadr_no_us(FileData *fd, const void *adr, int64_t *r_alloc_len=nullptr)
Definition readfile.cc:1509
static void after_liblink_id_process(BlendLibReader *reader, ID *id)
Definition readfile.cc:2127
static int has_linked_ids_to_read(Main *mainvar)
Definition readfile.cc:5968
static void * read_struct(FileData *fd, BHead *bh, const char *blockname, const int id_type_index, int64_t *r_alloc_len=nullptr)
Definition readfile.cc:1867
void blo_do_versions_403(FileData *fd, Library *lib, Main *bmain)
bool BKE_main_namemap_validate_and_fix(Main &bmain)
void * blo_read_struct_with_type_size_impl(BlendDataReader *reader, const void *old_address, size_t base_size, FunctionRef< size_t(const void *data)> type_size_fn)
Definition readfile.cc:6633
std::variant< BlenderHeaderInvalid, BlenderHeaderUnknown, BlenderHeader > BlenderHeaderVariant
static void read_libraries_report_invalid_id_names(FileData *fd, ReportList *reports, const bool has_forward_compatibility_issues, const char *filepath)
Definition readfile.cc:5058
void BKE_main_ensure_invariants(Main &bmain, std::optional< Span< ID * > > modified_ids=std::nullopt)
void version_system_idprops_nodes_generate(Main *bmain)
const IDTypeInfo * BKE_idtype_get_info_from_idtype_index(const int idtype_index)
Definition idtype.cc:213
static void read_file_bhead_idname_map_create(FileData *fd)
Definition readfile.cc:562
BlendFileReadReport * BLO_read_lib_reports(BlendLibReader *reader)
Definition readfile.cc:6858
static BHead * blo_bhead_read_full(FileData *fd, BHead *thisblock)
Definition readfile.cc:787
void BLO_library_link_end(Main *mainl, BlendHandle **bh, const LibraryLink_Params *params, ReportList *reports)
Definition readfile.cc:5935
int BKE_idtype_idcode_to_index(short idcode)
Definition idtype.cc:338
void blo_do_versions_270(FileData *fd, Library *lib, Main *bmain)
void BLO_read_struct_list_with_type_size(BlendDataReader *reader, size_t base_size, ListBase *list, FunctionRef< size_t(const void *data)> type_size_fn)
Definition readfile.cc:6591
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
static void read_libblock_undo_restore_identical(FileData *fd, Main *main, const ID *, ID *id_old, BHead *bhead, const eID_Tag id_tag)
Definition readfile.cc:3353
uint32_t uint32_from_uint64_ptr(uint64_t ptr)
void BLO_main_validate_embedded_liboverrides(Main *bmain, ReportList *reports)
ID * BLO_read_get_new_id_address(BlendLibReader *reader, ID *self_id, const bool is_linked_only, ID *id) ATTR_NONNULL(2)
Definition readfile.cc:6533
bool void BLI_path_rel(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1)
static ID * create_placeholder(Main *mainvar, const short idcode, const char *idname, const int tag, const bool was_liboverride)
Definition readfile.cc:2851
static const T * blo_bhead_data_at(const BHead *bhead, const int offset)
Definition readfile.cc:808
void BKE_collections_after_lib_link(Main *bmain)
void blo_do_versions_290(FileData *fd, Library *lib, Main *bmain)
bool blo_read_pointer_array_impl(BlendDataReader *reader, int64_t array_size, void **ptr_p)
Definition readfile.cc:6708
void BKE_libblock_init_empty(ID *id) ATTR_NONNULL(1)
Definition lib_id.cc:1550
void BLO_read_data_set_need_preview_render_restart(BlendDataReader *reader)
Definition readfile.cc:6863
static OldNewMap * oldnewmap_new()
Definition readfile.cc:290
int BLI_path_normalize(char *path) ATTR_NONNULL(1)
static void add_main_to_main(Main *mainvar, Main *from)
Definition readfile.cc:379
static bool read_file_dna(FileData *fd, const char **r_error_message)
Definition readfile.cc:909
static void change_link_placeholder_to_real_ID_pointer(FileData *basefd, const void *old, void *newp)
Definition readfile.cc:1582
void BKE_main_namemap_clear(Main &bmain)
@ LIBRARY_FLAG_IS_ARCHIVE
@ LIBRARY_FLAG_IS_EXTERNAL
ATTR_WARN_UNUSED_RESULT const size_t num
int DNA_struct_size(const struct SDNA *sdna, int struct_index)
void BLO_reportf_wrap(BlendFileReadReport *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition readfile.cc:237
BHead * blo_read_asset_data_block(FileData *fd, BHead *bhead, AssetMetaData **r_asset_data)
Definition readfile.cc:3773
void BLO_main_validate_embedded_flag(Main *bmain, ReportList *reports)
void IMB_colormanagement_working_space_convert(Main *bmain, const float3x3 &current_scene_linear_to_xyz, const float3x3 &new_xyz_to_scene_linear, bool depsgraph_tag=false, bool linked_only=false, bool editable_assets_only=false)
MatBase< float, 3, 3 > float3x3
static void change_ID_pointer_to_real_ID_pointer_fd(FileData *fd, const void *old, void *newp)
Definition readfile.cc:1561
static size_t user_menu_item_struct_size(const eUserMenu_Type type)
Definition readfile.cc:4215
void BKE_main_id_tag_all(Main *mainvar, int tag, bool value)
Definition lib_id.cc:1314
static BHead * find_previous_lib(FileData *fd, BHead *bhead)
Definition readfile.cc:4920
static void * blo_verify_data_address(FileData *fd, void *new_address, const void *, const int64_t alloc_len, const size_t expected_size)
Definition readfile.cc:6390
BHead * blo_bhead_prev(FileData *, BHead *thisblock)
Definition readfile.cc:724
void do_versions_after_linking_403(FileData *fd, Main *bmain)
BlendFileData * blo_read_file_internal(FileData *fd, const char *filepath)
Definition readfile.cc:4412
static void * read_struct_and_validate_size(FileData *fd, BHead *bh, const char *blockname, const int id_type_index, const int64_t min_size)
Definition readfile.cc:1980
@ BLO_READ_SKIP_USERDEF
@ BLO_READ_SKIP_UNDO_OLD_MAIN
@ BLO_READ_SKIP_DATA
static void read_libblock_undo_restore_at_old_address(FileData *fd, Main *main, ID *id, ID *id_old)
Definition readfile.cc:3428
void do_versions_after_linking_501(FileData *fd, Main *bmain)
static void change_ID_pointer_to_real_ID_pointer(FileData *basefd, const void *old, void *newp)
Definition readfile.cc:1594
static bool blo_bhead_is_id(const BHead *bhead)
Definition readfile.cc:545
void DNA_sdna_alias_data_ensure_structs_map(struct SDNA *sdna)
static FileData * read_library_file_data(FileData *basefd, Main *bmain, Main *lib_bmain)
Definition readfile.cc:6152
Main * BLO_library_link_begin(BlendHandle **bh, const char *filepath, const LibraryLink_Params *params)
Definition readfile.cc:5775
static Main * library_link_begin(Main *mainvar, FileData *fd, const char *filepath, const eID_Tag id_tag_extra)
Definition readfile.cc:5700
void BKE_report_log(eReportType type, const char *message, CLG_LogRef *log)
Definition report.cc:42
void BKE_main_free(Main *bmain)
Definition main.cc:202
Main * BLO_read_lib_get_main(BlendLibReader *reader)
Definition readfile.cc:6853
int DNA_struct_find_with_alias(const struct SDNA *sdna, StringRef str)
static void blo_cache_storage_entry_restore_in_new(ID *id, const IDCacheKey *key, void **cache_p, const uint flags, void *cache_storage_v)
Definition readfile.cc:1647
void * BLI_memarena_alloc(MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
FileReader * BLO_memfile_new_filereader(MemFile *memfile, int undo_direction)
Definition undofile.cc:272
ViewLayer * BKE_view_layer_find(const Scene *scene, const char *layer_name)
void id_us_ensure_real(ID *id)
Definition lib_id.cc:388
static void read_undo_libraries_cleanup_unused_ids(FileData *fd)
Definition readfile.cc:3241
void BKE_lib_override_library_main_update(Main *bmain)
void BKE_libblock_free_runtime_data(ID *id)
void BKE_lib_override_library_main_validate(Main *bmain, ReportList *reports)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
static void blo_cache_storage_entry_register(ID *id, const IDCacheKey *key, void **cache_p, uint, void *cache_storage_v)
Definition readfile.cc:1623
static ID * library_id_is_yet_read_main(Main *mainvar, const char *idname)
Definition readfile.cc:5031
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
static const char * library_parent_filepath(Library *lib)
Definition readfile.cc:260
void BKE_asset_metadata_read(BlendDataReader *reader, AssetMetaData *asset_data)
Definition asset.cc:219
static ID * library_id_is_yet_read(FileData *fd, Main *mainvar, BHead *bhead)
Definition readfile.cc:5045
ATTR_WARN_UNUSED_RESULT const BMVert * v2
@ INDEX_ID_NULL
Definition DNA_ID.h:1359
void BKE_lib_id_swap_full(Main *bmain, ID *id_a, ID *id_b, const bool do_self_remap, const int self_remap_flags)
Definition lib_id.cc:1136
void BLO_readfile_id_runtime_data_free(ID &id)
Definition readfile.cc:2313
static BHead * read_global(BlendFileData *bfd, FileData *fd, BHead *bhead)
Definition readfile.cc:3803
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
void version_system_idprops_children_bones_generate(Main *bmain)
int BLO_read_fileversion_get(BlendDataReader *reader)
Definition readfile.cc:6546
static const void * peek_struct_undo(FileData *fd, BHead *bhead)
Definition readfile.cc:2039
static bool library_filepath_is_valid(const Library *lib)
Definition readfile.cc:2680
void blo_do_versions_402(FileData *fd, Library *lib, Main *bmain)
static void lib_link_scenes_check_set(Main *bmain)
Definition readfile.cc:2543
short blo_bhead_id_flag(const FileData *fd, const BHead *bhead)
Definition readfile.cc:843
@ ID_RECALC_SOURCE
Definition DNA_ID.h:1143
@ ID_RECALC_ALL
Definition DNA_ID.h:1190
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
ID_Type GS(const char *name)
Definition DNA_ID.h:410
void BKE_main_idmap_insert_id(IDNameLib_Map *id_map, ID *id) ATTR_NONNULL()
void blo_do_versions_260(FileData *fd, Library *lib, Main *bmain)
IDNameLib_Map * BKE_main_idmap_create(Main *bmain, bool create_valid_ids_set, Main *old_bmain, int idmap_types) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition main_idmap.cc:80
static void fix_relpaths_library(const char *basepath, Main *main)
Definition readfile.cc:2817
static bool read_libblock_undo_restore(FileData *fd, Main *main, BHead *bhead, const eID_Tag id_tag, ID **r_id_old)
Definition readfile.cc:3495
static bool direct_link_id(FileData *fd, Main *main, const eID_Tag id_tag, const ID_Readfile_Data::Tags id_read_tags, ID *id, ID *id_old)
Definition readfile.cc:2905
@ ID_TAG_LIBOVERRIDE_NEED_RESYNC
Definition DNA_ID.h:909
@ ID_TAG_MISSING
Definition DNA_ID.h:870
@ ID_TAG_INDIRECT
Definition DNA_ID.h:851
@ ID_TAG_TEMP_MAIN
Definition DNA_ID.h:974
@ ID_TAG_UNDO_OLD_ID_REREAD_IN_PLACE
Definition DNA_ID.h:957
@ ID_TAG_UNDO_OLD_ID_REUSED_NOUNDO
Definition DNA_ID.h:950
@ ID_TAG_LOCAL
Definition DNA_ID.h:839
@ ID_TAG_EXTERN
Definition DNA_ID.h:845
@ ID_TAG_UNDO_OLD_ID_REUSED_UNCHANGED
Definition DNA_ID.h:939
@ ID_TAG_NEW
Definition DNA_ID.h:922
static void direct_link_id_common(BlendDataReader *reader, Library *current_library, ID *id, ID *id_old, eID_Tag id_tag, ID_Readfile_Data::Tags id_read_tags)
Definition readfile.cc:2341
static bool oldnewmap_insert(OldNewMap *onm, const void *oldaddr, void *newaddr, const int nr, const int64_t alloc_len=0)
Definition readfile.cc:299
static BHead * read_libblock(FileData *fd, Main *main, BHead *bhead, eID_Tag id_tag, ID_Readfile_Data::Tags id_read_tags, const bool placeholder_set_indirect_extern, ID **r_id)
Definition readfile.cc:3600
FileData * blo_filedata_from_memory(const void *mem, const int memsize, BlendFileReadReport *reports)
Definition readfile.cc:1366
static void readfile_id_runtime_data_ensure(ID &id)
Definition readfile.cc:2289
static void long_id_names_process_action_slots_identifiers(Main *bmain)
Definition readfile.cc:1065
bool BKE_idtype_cache_key_cmp(const void *key_a_v, const void *key_b_v)
Definition idtype.cc:38
static CLG_LogRef LOG
Definition logging.cpp:12
static void convert_pointer_array_32_to_64(BlendDataReader *, const int64_t array_size, const uint32_t *src, uint64_t *dst)
Definition readfile.cc:6697
double BLI_time_now_seconds()
Definition time.cc:30
@ MAIN_IDMAP_TYPE_UID
@ MAIN_IDMAP_TYPE_NAME
bool BLO_read_lib_is_undo(BlendLibReader *reader)
Definition readfile.cc:6848
static BHead * find_bhead(FileData *fd, void *old)
Definition readfile.cc:4962
int DNA_struct_alignment(const struct SDNA *sdna, int struct_index)
bool BLI_remlink_safe(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:161
void * blo_read_struct_no_us_nonnull_impl(BlendDataReader *reader, const void *old_address, size_t expected_size)
Definition readfile.cc:6469
static ID * link_named_part(Main *mainl, FileData *fd, const short idcode, const char *name, const int flag)
Definition readfile.cc:5627
void blo_do_versions_404(FileData *fd, Library *lib, Main *bmain)
@ ID_FLAG_INDIRECT_WEAK_LINK
Definition DNA_ID.h:781
@ ID_FLAG_FAKEUSER
Definition DNA_ID.h:770
@ ID_FLAG_LINKED_AND_PACKED
Definition DNA_ID.h:806
static void direct_link_id_embedded_id(BlendDataReader *reader, Library *current_library, ID *id, ID *id_old)
Definition readfile.cc:2166
void BKE_packedfile_blend_read(BlendDataReader *reader, PackedFile **pf_p, StringRefNull filepath)
static bool blo_read_verify_type_size(FileData *fd, NewAddress &entry, const void *old_address, FunctionRef< size_t(const void *data)> type_size_fn)
Definition readfile.cc:6575
static void link_glob_list(FileData *fd, ListBase *lb)
Definition readfile.cc:2046
static void convert_pointer_array_64_to_32(BlendDataReader *reader, const int64_t array_size, const uint64_t *src, uint32_t *dst)
Definition readfile.cc:6685
static void do_versions_after_linking(FileData *fd, Main *main)
Definition readfile.cc:4001
const SDNA * DNA_sdna_current_get()
static BHead * read_data_into_datamap(FileData *fd, BHead *bhead, const char *allocname, const int id_type_index)
Definition readfile.cc:2957
void do_versions_after_linking_250(Main *bmain)
GHash * BLI_ghash_new(GHashHashFP hashfp, GHashCmpFP cmpfp, const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:690
void do_versions_after_linking_405(FileData *fd, Main *bmain)
int DNA_struct_member_offset_by_name_with_alias(const struct SDNA *sdna, StringRef stype, StringRef vartype, StringRef name)
static void link_global(FileData *fd, BlendFileData *bfd)
Definition readfile.cc:3861
struct stat BLI_stat_t
@ IDWALK_CB_LOOPBACK
@ IDWALK_CB_DIRECT_WEAK_LINK
@ IDWALK_CB_EMBEDDED_NOT_OWNING
@ IDWALK_CB_OVERRIDE_LIBRARY_HIERARCHY_ROOT
@ IDWALK_CB_READFILE_IGNORE
@ IDWALK_CB_EMBEDDED
void BKE_main_id_indirect_linked_update(Main &bmain, std::optional< Span< ID * > > local_ids=std::nullopt)
Definition lib_id.cc:2211
static bool blo_bhead_read_data(FileData *fd, BHead *thisblock, void *buf)
Definition readfile.cc:759
void blo_make_old_idmap_from_main(FileData *fd, Main *bmain)
Definition readfile.cc:1604
static FileData * blo_filedata_from_file_descriptor(const char *filepath, BlendFileReadReport *reports, const int filedes)
Definition readfile.cc:1300
static void read_library_linked_id(FileData *basefd, FileData *fd, Main *mainvar, ID *id, ID **r_id)
Definition readfile.cc:5985
void BKE_main_idmap_remove_id(IDNameLib_Map *id_map, const ID *id) ATTR_NONNULL()
static bool blo_bhead_is_id_valid_type(const BHead *bhead)
Definition readfile.cc:552
void BKE_blender_version_blendfile_string_from_values(char *str_buff, const size_t str_buff_maxncpy, const short file_version, const short file_subversion)
Definition blender.cc:157
static void * oldnewmap_lookup_and_inc(OldNewMap *onm, const void *addr, const bool increase_users, int64_t *r_alloc_len=nullptr)
Definition readfile.cc:322
void * blo_read_struct_impl(BlendDataReader *reader, const void *old_address, size_t expected_size)
Definition readfile.cc:6424
void BLO_readfile_id_runtime_data_free_all(Main &bmain)
Definition readfile.cc:2318
BHead * blo_bhead_next(FileData *fd, BHead *thisblock)
Definition readfile.cc:732
static bool read_libblock_undo_restore_library(FileData *fd, const ID *id, ID *id_old, BHead *bhead)
Definition readfile.cc:3171
void do_versions_after_linking_290(FileData *fd, Main *bmain)
ID ID ID * BKE_main_idmap_lookup_uid(IDNameLib_Map *id_map, uint session_uid) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
static int direct_link_id_restore_recalc(const FileData *fd, const ID *id_target, const ID *id_current, const bool is_identical)
Definition readfile.cc:2243
void blo_do_versions_280(FileData *fd, Library *lib, Main *bmain)
void BKE_preferences_asset_library_read_data(struct BlendDataReader *reader, struct bUserAssetLibrary *library)
bool BKE_main_is_empty(Main *bmain)
Definition main.cc:637
static void oldnewmap_lib_insert(FileData *fd, const void *oldaddr, ID *newaddr, const int id_code)
Definition readfile.cc:309
void blo_cache_storage_end(FileData *fd)
Definition readfile.cc:1769
bool BLI_ghash_haskey(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:825
static bool read_libblock_is_identical(FileData *fd, BHead *bhead)
Definition readfile.cc:2984
void BKE_library_foreach_ID_link(Main *bmain, ID *id, FunctionRef< LibraryIDLinkCallback > callback, void *user_data, LibraryForeachIDFlag flag)
Definition lib_query.cc:449
ID * BLO_read_get_new_id_address_from_session_uid(BlendLibReader *reader, uint session_uid) ATTR_NONNULL(1)
Definition readfile.cc:6541
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
short do_versions_new_to_old_idcode_get(const short id_code_new)
Main * BKE_main_idmap_main_get(IDNameLib_Map *id_map) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void blo_do_versions_pre250(FileData *fd, Library *lib, Main *bmain)
BHead * blo_bhead_first(FileData *fd)
Definition readfile.cc:704
static const char * blo_bhead_string_at(const BHead *bhead, const int offset, const size_t maxsize)
Definition readfile.cc:817
static NewAddress * newdataadr_entry(FileData *fd, const void *adr)
Definition readfile.cc:1518
bool BLO_main_validate_shapekeys(Main *bmain, ReportList *reports)
void blo_do_versions_400(FileData *fd, Library *lib, Main *bmain)
@ FD_FLAGS_FILE_FUTURE
Definition readfile.hh:62
@ FD_FLAGS_IS_MEMFILE
Definition readfile.hh:57
@ FD_FLAGS_FILE_POINTSIZE_IS_4
Definition readfile.hh:54
@ FD_FLAGS_POINTSIZE_DIFFERS
Definition readfile.hh:55
@ FD_FLAGS_FILE_OK
Definition readfile.hh:56
@ FD_FLAGS_SWITCH_ENDIAN
Definition readfile.hh:53
@ FD_FLAGS_HAS_INVALID_ID_NAMES
Definition readfile.hh:67
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
void blo_join_main(Main *bmain)
Definition readfile.cc:393
void blo_filedata_free(FileData *fd)
Definition readfile.cc:1404
void BKE_collection_blend_read_data(BlendDataReader *reader, Collection *collection, ID *owner_id)
void blo_do_versions_503(FileData *fd, Library *lib, Main *bmain)
void BLI_memarena_free(MemArena *ma) ATTR_NONNULL(1)
IDNewNameResult BKE_id_new_name_validate(Main &bmain, ListBaseT< ID > &lb, ID &id, const char *newname, IDNewNameMode mode, bool do_linked_data)
Definition lib_id.cc:1999
static void read_blender_header(FileData *fd)
Definition readfile.cc:881
std::unique_ptr< SDNA > DNA_sdna_from_data(const void *data, int data_len, bool data_alloc, bool do_alias, const char **r_error_message)
BlenderHeaderVariant BLO_readfile_blender_header_decode(FileReader *file)
ID_Readfile_Data::Tags BLO_readfile_id_runtime_tags(const ID &id)
Definition readfile.cc:2297
void blo_do_versions_405(FileData *fd, Library *lib, Main *bmain)
ListBaseT< ID > * which_libbase(Main *bmain, short type)
Definition main.cc:1073
static void sort_bhead_old_map(FileData *fd)
Definition readfile.cc:4895
void do_versions_after_linking_300(FileData *fd, Main *bmain)
static int expand_cb(LibraryIDLinkCallbackData *cb_data)
Definition readfile.cc:5536
void BKE_main_idmap_destroy(IDNameLib_Map *id_map) ATTR_NONNULL()
static BHead * read_userdef(BlendFileData *bfd, FileData *fd, BHead *bhead)
Definition readfile.cc:4235
@ IDTYPE_FLAGS_NO_MEMFILE_UNDO
Definition BKE_idtype.hh:59
static void long_id_names_ensure_unique_id_names(Main *bmain)
Definition readfile.cc:1023
void version_system_idprops_generate(Main *bmain)
AllocStringStorage< keyT, hashT > & alloc_string_storage_get(const std::string &storage_identifier)
#define BHEADN_FROM_BHEAD(bh)
Definition readfile.cc:225
#define BHEAD_USE_READ_ON_DEMAND(bhead)
Definition readfile.cc:231
char tmp_last_slot_identifier[258]
char last_slot_identifier[258]
The meta-data of an asset. By creating and giving this for a data-block (ID.asset_data),...
BHeadN * next
Definition readfile.cc:214
BHeadN * prev
Definition readfile.cc:214
bool is_memchunk_identical
Definition readfile.cc:221
off64_t file_offset
Definition readfile.cc:217
const void * old
Definition readfile.cc:4878
const void * old
Map< uint64_t, ImplicitSharingInfoAndData > shared_data_by_stored_address
std::queue< ID * > ids_to_expand
Definition readfile.cc:5096
BLOExpandDoitCallback callback
Definition readfile.cc:5098
struct blender::BlendFileReadReport::@140003175311343306107257242277261221002217304000 count
struct blender::BlendFileReadReport::@070156237370130334241363347365056360307125257043 duration
BlenderHeader blender_header
Definition readfile.hh:87
std::shared_ptr< Map< IDHash, ID * > > id_by_deep_hash
Definition readfile.hh:157
const char * compflags
Definition readfile.hh:114
BLOCacheStorage * cache_storage
Definition readfile.hh:166
char relabase[FILE_MAX]
Definition readfile.hh:108
std::optional< Map< StringRefNull, BHead * > > bhead_idname_map
Definition readfile.hh:171
ListBaseT< BHeadN > bhead_list
Definition readfile.hh:84
std::optional< BLI_stat_t > file_stat
Definition readfile.hh:90
eBLOReadSkip skip_flags
Definition readfile.hh:144
bool need_preview_render_restart
Definition readfile.hh:216
IDNameLib_Map * new_idmap_uid
Definition readfile.hh:206
const SDNA * memsdna
Definition readfile.hh:112
DNA_ReconstructInfo * reconstruct_info
Definition readfile.hh:115
OldNewMap * datamap
Definition readfile.hh:154
OldNewMap * libmap
Definition readfile.hh:164
int library_filepath_offset
Definition readfile.hh:137
eID_Tag id_tag_extra
Definition readfile.hh:152
BlendFileReadReport * reports
Definition readfile.hh:208
std::unique_ptr< SDNA > filesdna
Definition readfile.hh:111
IDNameLib_Map * old_idmap_uid
Definition readfile.hh:195
BHeadSort * bheadmap
Definition readfile.hh:168
enum eFileDataFlag flags
Definition readfile.hh:85
OldNewMap * globmap
Definition readfile.hh:155
FileReader * file
Definition readfile.hh:89
void * storage_handle
Definition readfile.hh:211
struct bScreen * curscreen
float colorspace_scene_linear_to_xyz[3][3]
struct ViewLayer * cur_view_layer
FileReaderCloseFn close
FileReaderSeekFn seek
FileReaderReadFn read
unsigned int id_session_uid
Definition BKE_idtype.hh:78
ListBaseT< IDOverrideLibraryPropertyOperation > operations
Definition DNA_ID.h:326
eID_OverrideLib_PropTag tag
Definition DNA_ID.h:331
IDTypeBlendReadAfterLiblinkFunction blend_read_after_liblink
IDTypeForeachCacheFunction foreach_cache
AssetTypeInfo * asset_type_info
IDTypeBlendReadUndoPreserve blend_read_undo_preserve
IDTypeBlendReadDataFunction blend_read_data
const char * name
char name[258]
Definition DNA_ID.h:433
unsigned int session_uid
Definition DNA_ID.h:463
bke::id::ID_Runtime * runtime
Definition DNA_ID.h:541
struct ID * newid
Definition DNA_ID.h:419
unsigned int recalc_up_to_undo_push
Definition DNA_ID.h:456
struct Library * lib
Definition DNA_ID.h:421
int icon_id
Definition DNA_ID.h:445
unsigned int recalc_after_undo_push
Definition DNA_ID.h:457
unsigned int recalc
Definition DNA_ID.h:446
struct ID * orig_id
Definition DNA_ID.h:502
ListBaseT< KeyBlock > block
LibraryForeachIDCallbackFlag cb_flag
struct Library * archive_parent_library
Definition DNA_ID.h:575
struct PackedFile * packedfile
Definition DNA_ID.h:584
char filepath[1024]
Definition DNA_ID.h:562
bke::library::LibraryRuntime * runtime
Definition DNA_ID.h:591
uint16_t flag
Definition DNA_ID.h:565
Library * curlib
Definition BKE_main.hh:366
ListBaseT< wmWindowManager > wm
Definition BKE_main.hh:415
char filepath[1024]
Definition BKE_main.hh:267
IDNameLib_Map * id_map
Definition BKE_main.hh:440
MainColorspace colorspace
Definition BKE_main.hh:371
std::shared_ptr< VectorSet< Main * > > split_mains
Definition BKE_main.hh:254
short minsubversionfile
Definition BKE_main.hh:273
bool need_preview_render_restart
Definition BKE_main.hh:347
ListBaseT< Library > libraries
Definition BKE_main.hh:387
short versionfile
Definition BKE_main.hh:269
char build_hash[16]
Definition BKE_main.hh:293
bool is_read_invalid
Definition BKE_main.hh:320
ListBaseT< Object > objects
Definition BKE_main.hh:388
short subversionfile
Definition BKE_main.hh:270
bool is_asset_edit_file
Definition BKE_main.hh:288
bool has_forward_compatibility_issues
Definition BKE_main.hh:281
uint64_t build_commit_timestamp
Definition BKE_main.hh:291
ListBaseT< Scene > scenes
Definition BKE_main.hh:386
BlendThumbnail * blen_thumb
Definition BKE_main.hh:356
short minversionfile
Definition BKE_main.hh:272
Map< uint64_t, ImplicitSharingInfoAndData > sharing_info_by_address_id
MemFileSharedStorage * shared_storage
char last_slot_identifier[258]
Map< const void *, NewAddress > map
Definition readfile.cc:287
struct Collection * master_collection
DNA_DEPRECATED struct bNodeTree * compositing_node_group
DNA_DEPRECATED struct bNodeTree * nodetree
ListBaseT< bUserExtensionRepo > extension_repos
ListBaseT< bTheme > themes
ListBaseT< struct wmKeyMap > user_keymaps
ListBaseT< struct wmKeyConfigPref > user_keyconfig_prefs
UserDef_Runtime runtime
ListBaseT< bUserAssetLibrary > asset_libraries
ListBaseT< bAddon > addons
ListBaseT< bPathCompare > autoexec_paths
ListBaseT< bUserScriptDirectory > script_directories
ListBaseT< uiStyle > uistyles
ListBaseT< bUserAssetShelfSettings > asset_shelves_settings
ListBaseT< uiFont > uifonts
ListBaseT< bUserMenu > user_menus
struct ActionSlot ** slot_array
struct IDProperty * prop
ListBaseT< struct bConstraint > constraints
DNA_DEPRECATED struct Scene * scene
ListBaseT< AssetCatalogPathLink > enabled_catalog_paths
ListBaseT< bUserMenuItem > items
ID_Readfile_Data * readfile_data
Definition BKE_lib_id.hh:93
ListBaseT< wmKeyMapDiffItem > diff_items
bool(*) poll(struct bContext *)
ListBaseT< wmKeyMapItem > items
DNA_DEPRECATED struct bScreen * screen