Blender V4.5
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
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 <fcntl.h> /* for open flags (O_BINARY, O_RDONLY). */
20
21#ifndef WIN32
22# include <unistd.h> /* for read close */
23#else
24# include "BLI_winstuff.h"
25# include "winsock2.h"
26# include <io.h> /* for open close read */
27#endif
28
29#include <fmt/format.h>
30
31#include "CLG_log.h"
32
33/* allow readfile to use deprecated functionality */
34#define DNA_DEPRECATED_ALLOW
35
36#include "DNA_asset_types.h"
40#include "DNA_genfile.h"
41#include "DNA_key_types.h"
42#include "DNA_layer_types.h"
43#include "DNA_node_types.h"
45#include "DNA_sdna_types.h"
46#include "DNA_userdef_types.h"
47
49#include "MEM_guardedalloc.h"
50
51#include "BLI_endian_defines.h"
52#include "BLI_endian_switch.h"
53#include "BLI_fileops.h"
54#include "BLI_ghash.h"
55#include "BLI_map.hh"
56#include "BLI_memarena.h"
57#include "BLI_memory_utils.hh"
58#include "BLI_set.hh"
59#include "BLI_string.h"
60#include "BLI_string_ref.hh"
61#include "BLI_string_utf8.h"
62#include "BLI_string_utils.hh"
63#include "BLI_threads.h"
64#include "BLI_time.h"
65#include "BLI_utildefines.h"
66
67#include "BLT_translation.hh"
68
69#include "BKE_anim_data.hh"
70#include "BKE_animsys.h"
71#include "BKE_asset.hh"
72#include "BKE_blender_version.h"
73#include "BKE_collection.hh"
74#include "BKE_global.hh" /* for G */
75#include "BKE_idprop.hh"
76#include "BKE_idtype.hh"
77#include "BKE_layer.hh"
78#include "BKE_lib_id.hh"
79#include "BKE_lib_override.hh"
80#include "BKE_lib_query.hh"
81#include "BKE_lib_remap.hh"
82#include "BKE_library.hh"
83#include "BKE_main.hh" /* for Main */
84#include "BKE_main_idmap.hh"
86#include "BKE_main_namemap.hh"
87#include "BKE_material.hh"
88#include "BKE_mesh.hh"
89#include "BKE_modifier.hh"
90#include "BKE_nla.hh"
91#include "BKE_node.hh" /* for tree type defines */
93#include "BKE_object.hh"
94#include "BKE_packedFile.hh"
95#include "BKE_preferences.h"
96#include "BKE_report.hh"
97#include "BKE_scene.hh"
98#include "BKE_screen.hh"
99#include "BKE_undo_system.hh"
100#include "BKE_workspace.hh"
101
102#include "DRW_engine.hh"
103
104#include "DEG_depsgraph.hh"
105
106#include "BLO_blend_validate.hh"
107#include "BLO_read_write.hh"
108#include "BLO_readfile.hh"
109#include "BLO_undofile.hh"
110
111#include "SEQ_iterator.hh"
112#include "SEQ_modifier.hh"
113#include "SEQ_sequencer.hh"
114#include "SEQ_utils.hh"
115
116#include "readfile.hh"
117#include "versioning_common.hh"
118
119/* Make preferences read-only. */
120#define U (*((const UserDef *)&U))
121
179#define USE_BHEAD_READ_ON_DEMAND
180
181static CLG_LogRef LOG = {"blo.readfile"};
182static CLG_LogRef LOG_UNDO = {"blo.readfile.undo"};
183
184#if ENDIAN_ORDER == B_ENDIAN
185# warning "Support for Big Endian endianness is deprecated and will be removed in Blender 5.0"
186#endif
187
188/* local prototypes */
189static void read_libraries(FileData *basefd, ListBase *mainlist);
190static void read_libraries_free_placeholders(FileData *basefd, ListBase *mainlist);
191static void *read_struct(FileData *fd, BHead *bh, const char *blockname, const int id_type_index);
193 BHead *bh,
194 const char *blockname,
195 const int id_type_index,
196 const int64_t min_size);
197static BHead *find_bhead_from_code_name(FileData *fd, const short idcode, const char *name);
198
199struct BHeadN {
201#ifdef USE_BHEAD_READ_ON_DEMAND
203 off64_t file_offset;
206#endif
209};
210
211#define BHEADN_FROM_BHEAD(bh) ((BHeadN *)POINTER_OFFSET(bh, -int(offsetof(BHeadN, bhead))))
212
217#define BHEAD_USE_READ_ON_DEMAND(bhead) ((bhead)->code == BLO_CODE_DATA)
218
219/* -------------------------------------------------------------------- */
224 const eReportType type,
225 const char *format,
226 ...)
227{
228 char fixed_buf[1024]; /* should be long enough */
229
230 va_list args;
231
232 va_start(args, format);
233 vsnprintf(fixed_buf, sizeof(fixed_buf), format, args);
234 va_end(args);
235
236 fixed_buf[sizeof(fixed_buf) - 1] = '\0';
237
238 BKE_report(reports->reports, type, fixed_buf);
239
240 if (G.background == 0) {
241 printf("%s: %s\n", BKE_report_type_str(type), fixed_buf);
242 }
243}
244
245/* for reporting linking messages */
247{
248 return lib->runtime->parent ? lib->runtime->parent->runtime->filepath_abs : "<direct>";
249}
250
253/* -------------------------------------------------------------------- */
258 void *newp;
259
261 int nr;
262};
263
267
269{
270 return MEM_new<OldNewMap>(__func__);
271}
272
277static bool oldnewmap_insert(OldNewMap *onm, const void *oldaddr, void *newaddr, const int nr)
278{
279 if (oldaddr == nullptr || newaddr == nullptr) {
280 return false;
281 }
282
283 return onm->map.add_overwrite(oldaddr, NewAddress{newaddr, nr});
284}
285
286static void oldnewmap_lib_insert(FileData *fd, const void *oldaddr, ID *newaddr, const int id_code)
287{
288 oldnewmap_insert(fd->libmap, oldaddr, newaddr, id_code);
289}
290
292 const void *oldaddr,
293 void *newaddr,
294 const int nr)
295{
296 oldnewmap_insert(onm, oldaddr, newaddr, nr);
297}
298
299static void *oldnewmap_lookup_and_inc(OldNewMap *onm, const void *addr, const bool increase_users)
300{
301 NewAddress *entry = onm->map.lookup_ptr(addr);
302 if (entry == nullptr) {
303 return nullptr;
304 }
305 if (increase_users) {
306 entry->nr++;
307 }
308 return entry->newp;
309}
310
311/* for libdata, NewAddress.nr has ID code, no increment */
312static void *oldnewmap_liblookup(OldNewMap *onm, const void *addr, const bool is_linked_only)
313{
314 if (addr == nullptr) {
315 return nullptr;
316 }
317
318 ID *id = static_cast<ID *>(oldnewmap_lookup_and_inc(onm, addr, false));
319 if (id == nullptr) {
320 return nullptr;
321 }
322 if (!is_linked_only || ID_IS_LINKED(id)) {
323 return id;
324 }
325 return nullptr;
326}
327
328static void oldnewmap_clear(OldNewMap *onm)
329{
330 /* Free unused data. */
331 for (NewAddress &new_addr : onm->map.values()) {
332 if (new_addr.nr == 0) {
333 MEM_freeN(new_addr.newp);
334 }
335 }
336 onm->map.clear();
337}
338
339static void oldnewmap_free(OldNewMap *onm)
340{
341 MEM_delete(onm);
342}
343
346/* -------------------------------------------------------------------- */
350static void add_main_to_main(Main *mainvar, Main *from)
351{
352 if (from->is_read_invalid) {
353 mainvar->is_read_invalid = true;
354 }
355
356 MainListsArray lbarray = BKE_main_lists_get(*mainvar);
358 int a = fromarray.size();
359 while (a--) {
360 BLI_movelisttolist(lbarray[a], fromarray[a]);
361 }
362}
363
364void blo_join_main(ListBase *mainlist)
365{
366 Main *tojoin, *mainl;
367
368 mainl = static_cast<Main *>(mainlist->first);
369
370 if (mainl->id_map != nullptr) {
371 /* Cannot keep this since we add some IDs from joined mains. */
373 mainl->id_map = nullptr;
374 }
375
376 /* Will no longer be valid after joining. */
378
379 while ((tojoin = mainl->next)) {
380 BLI_assert(((tojoin->curlib->runtime->tag & LIBRARY_IS_ASSET_EDIT_FILE) != 0) ==
381 tojoin->is_asset_edit_file);
382 add_main_to_main(mainl, tojoin);
383 BLI_remlink(mainlist, tojoin);
384 tojoin->next = tojoin->prev = nullptr;
385 BKE_main_free(tojoin);
386 }
387}
388
389static void split_libdata(ListBase *lb_src, Main **lib_main_array, const uint lib_main_array_len)
390{
391 for (ID *id = static_cast<ID *>(lb_src->first), *idnext; id; id = idnext) {
392 idnext = static_cast<ID *>(id->next);
393
394 if (id->lib) {
395 if ((uint(id->lib->runtime->temp_index) < lib_main_array_len) &&
396 /* this check should never fail, just in case 'id->lib' is a dangling pointer. */
397 (lib_main_array[id->lib->runtime->temp_index]->curlib == id->lib))
398 {
399 Main *mainvar = lib_main_array[id->lib->runtime->temp_index];
400 ListBase *lb_dst = which_libbase(mainvar, GS(id->name));
401 BLI_remlink(lb_src, id);
402 BLI_addtail(lb_dst, id);
403 }
404 else {
405 CLOG_ERROR(&LOG, "Invalid library for '%s'", id->name);
406 }
407 }
408 }
409}
410
412{
413 mainlist->first = mainlist->last = main;
414 main->next = nullptr;
415
416 if (BLI_listbase_is_empty(&main->libraries)) {
417 return;
418 }
419
420 if (main->id_map != nullptr) {
421 /* Cannot keep this since we remove some IDs from given main. */
423 main->id_map = nullptr;
424 }
425
426 /* Will no longer be valid after splitting. */
428
429 /* (Library.temp_index -> Main), lookup table */
430 const uint lib_main_array_len = BLI_listbase_count(&main->libraries);
431 Main **lib_main_array = MEM_malloc_arrayN<Main *>(lib_main_array_len, __func__);
432
433 int i = 0;
434 for (Library *lib = static_cast<Library *>(main->libraries.first); lib;
435 lib = static_cast<Library *>(lib->id.next), i++)
436 {
437 Main *libmain = BKE_main_new();
438 libmain->curlib = lib;
439 libmain->versionfile = lib->runtime->versionfile;
440 libmain->subversionfile = lib->runtime->subversionfile;
443 libmain->is_asset_edit_file = (lib->runtime->tag & LIBRARY_IS_ASSET_EDIT_FILE) != 0;
444 BLI_addtail(mainlist, libmain);
445 lib->runtime->temp_index = i;
446 lib_main_array[i] = libmain;
447 }
448
450 i = lbarray.size();
451 while (i--) {
452 ID *id = static_cast<ID *>(lbarray[i]->first);
453 if (id == nullptr || GS(id->name) == ID_LI) {
454 /* No ID_LI data-block should ever be linked anyway, but just in case, better be explicit. */
455 continue;
456 }
457 split_libdata(lbarray[i], lib_main_array, lib_main_array_len);
458 }
459
460 MEM_freeN(lib_main_array);
461}
462
464{
465 BHead *bhead;
466
467 for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
468 if (bhead->code == BLO_CODE_GLOB) {
470 fd, bhead, "Data from Global block", INDEX_ID_NULL, sizeof(FileGlobal)));
471 if (fg) {
472 if (main->versionfile != fd->fileversion) {
473 /* `versionfile` remains unset when linking from a new library (`main` has then just be
474 * created by `blo_find_main`). */
475 BLI_assert(main->versionfile == 0);
476 main->versionfile = short(fd->fileversion);
477 }
478 main->subversionfile = fg->subversion;
479 main->minversionfile = fg->minversion;
480 main->minsubversionfile = fg->minsubversion;
481 main->has_forward_compatibility_issues = !MAIN_VERSION_FILE_OLDER_OR_EQUAL(
483 main->is_asset_edit_file = (fg->fileflags & G_FILE_ASSET_EDIT_FILE) != 0;
484 MEM_freeN(fg);
485 }
486 else if (bhead->code == BLO_CODE_ENDB) {
487 break;
488 }
489 }
490 }
491 if (main->curlib) {
492 main->curlib->runtime->versionfile = main->versionfile;
493 main->curlib->runtime->subversionfile = main->subversionfile;
495 main->curlib->runtime->tag, main->is_asset_edit_file, LIBRARY_IS_ASSET_EDIT_FILE);
496 }
497}
498
499static bool blo_bhead_is_id(const BHead *bhead)
500{
501 /* BHead codes are four bytes (like 'ENDB', 'TEST', etc.), but if the two most-significant bytes
502 * are zero, the values actually indicate an ID type. */
503 return bhead->code <= 0xFFFF;
504}
505
506static bool blo_bhead_is_id_valid_type(const BHead *bhead)
507{
508 if (!blo_bhead_is_id(bhead)) {
509 return false;
510 }
511
512 const short id_type_code = bhead->code & 0xFFFF;
513 return BKE_idtype_idcode_is_valid(id_type_code);
514}
515
517{
518 /* dummy values */
519 bool is_link = false;
520 int code_prev = BLO_CODE_ENDB;
521
522 fd->bhead_idname_map.emplace();
523 for (BHead *bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
524 if (code_prev != bhead->code) {
525 code_prev = bhead->code;
526 is_link = blo_bhead_is_id_valid_type(bhead) ?
527 BKE_idtype_idcode_is_linkable(short(code_prev)) :
528 false;
529 }
530
531 if (is_link) {
532 /* #idname may be null in case the ID name of the given BHead is detected as invalid (e.g.
533 * because it comes from a future version of Blender allowing for longer ID names). These
534 * 'invalid-named IDs' are skipped here, which will e.g. prevent them from being linked. */
535 const char *idname = blo_bhead_id_name(fd, bhead);
536 if (idname) {
537 fd->bhead_idname_map->add(idname, bhead);
538 }
539 }
540 }
541}
542
543static Main *blo_find_main(FileData *fd, const char *filepath, const char *relabase)
544{
545 ListBase *mainlist = fd->mainlist;
546 Main *m;
547 Library *lib;
548 char filepath_abs[FILE_MAX];
549
550 STRNCPY(filepath_abs, filepath);
551 BLI_path_abs(filepath_abs, relabase);
552 BLI_path_normalize(filepath_abs);
553
554 // printf("blo_find_main: relabase %s\n", relabase);
555 // printf("blo_find_main: original in %s\n", filepath);
556 // printf("blo_find_main: converted to %s\n", filepath_abs);
557
558 LISTBASE_FOREACH (Main *, m, mainlist) {
559 const char *libname = (m->curlib) ? m->curlib->runtime->filepath_abs : m->filepath;
560
561 if (BLI_path_cmp(filepath_abs, libname) == 0) {
562 if (G.debug & G_DEBUG) {
563 CLOG_INFO(&LOG, 3, "Found library %s", libname);
564 }
565 return m;
566 }
567 }
568
569 m = BKE_main_new();
570 BLI_addtail(mainlist, m);
571
572 /* Add library data-block itself to 'main' Main, since libraries are **never** linked data.
573 * Fixes bug where you could end with all ID_LI data-blocks having the same name... */
574 lib = BKE_id_new<Library>(static_cast<Main *>(mainlist->first), BLI_path_basename(filepath));
575
576 /* Important, consistency with main ID reading code from read_libblock(). */
577 lib->id.us = ID_FAKE_USERS(lib);
578
579 /* Matches direct_link_library(). */
581
582 STRNCPY(lib->filepath, filepath);
583 STRNCPY(lib->runtime->filepath_abs, filepath_abs);
584
585 m->curlib = lib;
586
587 read_file_version(fd, m);
588
589 if (G.debug & G_DEBUG) {
590 CLOG_INFO(&LOG, 3, "Added new lib %s", filepath);
591 }
592 return m;
593}
594
595void blo_readfile_invalidate(FileData *fd, Main *bmain, const char *message)
596{
597 /* Tag given `bmain`, and 'root 'local' main one (in case given one is a library one) as invalid.
598 */
599 bmain->is_read_invalid = true;
600 for (; bmain->prev != nullptr; bmain = bmain->prev) {
601 /* Pass. */
602 }
603 bmain->is_read_invalid = true;
604
606 RPT_ERROR,
607 "A critical error happened (the blend file is likely corrupted): %s",
608 message);
609}
610
613{
614 return fd->mainlist ? static_cast<Main *>(fd->mainlist->first) : nullptr;
615}
616
619/* -------------------------------------------------------------------- */
632
637
639{
640 BHeadN *new_bhead = nullptr;
641 const bool do_endian_swap = fd->flags & FD_FLAGS_SWITCH_ENDIAN;
642
643 if (fd) {
644 if (!fd->is_eof) {
645 std::optional<BHead> bhead_opt = BLO_readfile_read_bhead(
646 fd->file, fd->blender_header.bhead_type(), do_endian_swap);
647 BHead *bhead = nullptr;
648 if (!bhead_opt.has_value()) {
649 fd->is_eof = true;
650 }
651 else if (bhead_opt->len < 0) {
652 /* Make sure people are not trying to parse bad blend files. */
653 fd->is_eof = true;
654 }
655 else {
656 bhead = &bhead_opt.value();
657 }
658
659 /* bhead now contains the (converted) bhead structure. Now read
660 * the associated data and put everything in a BHeadN (creative naming !)
661 */
662 if (fd->is_eof) {
663 /* pass */
664 }
665#ifdef USE_BHEAD_READ_ON_DEMAND
666 else if (fd->file->seek != nullptr && BHEAD_USE_READ_ON_DEMAND(bhead)) {
667 /* Delay reading bhead content. */
668 new_bhead = MEM_mallocN<BHeadN>("new_bhead");
669 if (new_bhead) {
670 new_bhead->next = new_bhead->prev = nullptr;
671 new_bhead->file_offset = fd->file->offset;
672 new_bhead->has_data = false;
673 new_bhead->is_memchunk_identical = false;
674 new_bhead->bhead = *bhead;
675 const off64_t seek_new = fd->file->seek(fd->file, bhead->len, SEEK_CUR);
676 if (UNLIKELY(seek_new == -1)) {
677 fd->is_eof = true;
678 MEM_freeN(new_bhead);
679 new_bhead = nullptr;
680 }
681 else {
682 BLI_assert(fd->file->offset == seek_new);
683 }
684 }
685 else {
686 fd->is_eof = true;
687 }
688 }
689#endif
690 else {
691 new_bhead = static_cast<BHeadN *>(
692 MEM_mallocN(sizeof(BHeadN) + size_t(bhead->len), "new_bhead"));
693 if (new_bhead) {
694 new_bhead->next = new_bhead->prev = nullptr;
695#ifdef USE_BHEAD_READ_ON_DEMAND
696 new_bhead->file_offset = 0; /* don't seek. */
697 new_bhead->has_data = true;
698#endif
699 new_bhead->is_memchunk_identical = false;
700 new_bhead->bhead = *bhead;
701
702 const int64_t readsize = fd->file->read(fd->file, new_bhead + 1, size_t(bhead->len));
703
704 if (UNLIKELY(readsize != bhead->len)) {
705 fd->is_eof = true;
706 MEM_freeN(new_bhead);
707 new_bhead = nullptr;
708 }
709 else {
710 if (fd->flags & FD_FLAGS_IS_MEMFILE) {
711 new_bhead->is_memchunk_identical = ((UndoReader *)fd->file)->memchunk_identical;
712 }
713 }
714 }
715 else {
716 fd->is_eof = true;
717 }
718 }
719 }
720 }
721
722 /* We've read a new block. Now add it to the list
723 * of blocks.
724 */
725 if (new_bhead) {
726 BLI_addtail(&fd->bhead_list, new_bhead);
727 }
728
729 return new_bhead;
730}
731
733{
734 BHeadN *new_bhead;
735 BHead *bhead = nullptr;
736
737 /* Rewind the file
738 * Read in a new block if necessary
739 */
740 new_bhead = static_cast<BHeadN *>(fd->bhead_list.first);
741 if (new_bhead == nullptr) {
742 new_bhead = get_bhead(fd);
743 }
744
745 if (new_bhead) {
746 bhead = &new_bhead->bhead;
747 }
748
749 return bhead;
750}
751
752BHead *blo_bhead_prev(FileData * /*fd*/, BHead *thisblock)
753{
754 BHeadN *bheadn = BHEADN_FROM_BHEAD(thisblock);
755 BHeadN *prev = bheadn->prev;
756
757 return (prev) ? &prev->bhead : nullptr;
758}
759
761{
762 BHeadN *new_bhead = nullptr;
763 BHead *bhead = nullptr;
764
765 if (thisblock) {
766 /* bhead is actually a sub part of BHeadN
767 * We calculate the BHeadN pointer from the BHead pointer below */
768 new_bhead = BHEADN_FROM_BHEAD(thisblock);
769
770 /* get the next BHeadN. If it doesn't exist we read in the next one */
771 new_bhead = new_bhead->next;
772 if (new_bhead == nullptr) {
773 new_bhead = get_bhead(fd);
774 }
775 }
776
777 if (new_bhead) {
778 /* here we do the reverse:
779 * go from the BHeadN pointer to the BHead pointer */
780 bhead = &new_bhead->bhead;
781 }
782
783 return bhead;
784}
785
786#ifdef USE_BHEAD_READ_ON_DEMAND
787static bool blo_bhead_read_data(FileData *fd, BHead *thisblock, void *buf)
788{
789 bool success = true;
790 BHeadN *new_bhead = BHEADN_FROM_BHEAD(thisblock);
791 BLI_assert(new_bhead->has_data == false && new_bhead->file_offset != 0);
792 off64_t offset_backup = fd->file->offset;
793 if (UNLIKELY(fd->file->seek(fd->file, new_bhead->file_offset, SEEK_SET) == -1)) {
794 success = false;
795 }
796 else {
797 if (UNLIKELY(fd->file->read(fd->file, buf, size_t(new_bhead->bhead.len)) !=
798 new_bhead->bhead.len))
799 {
800 success = false;
801 }
802 else {
803 if (fd->flags & FD_FLAGS_IS_MEMFILE) {
804 new_bhead->is_memchunk_identical = ((UndoReader *)fd->file)->memchunk_identical;
805 }
806 }
807 }
808 if (fd->file->seek(fd->file, offset_backup, SEEK_SET) == -1) {
809 success = false;
810 }
811 return success;
812}
813
814static BHead *blo_bhead_read_full(FileData *fd, BHead *thisblock)
815{
816 BHeadN *new_bhead = BHEADN_FROM_BHEAD(thisblock);
817 BHeadN *new_bhead_data = static_cast<BHeadN *>(
818 MEM_mallocN(sizeof(BHeadN) + new_bhead->bhead.len, "new_bhead"));
819 new_bhead_data->bhead = new_bhead->bhead;
820 new_bhead_data->file_offset = new_bhead->file_offset;
821 new_bhead_data->has_data = true;
822 new_bhead_data->is_memchunk_identical = false;
823 if (!blo_bhead_read_data(fd, thisblock, new_bhead_data + 1)) {
824 MEM_freeN(new_bhead_data);
825 return nullptr;
826 }
827 return &new_bhead_data->bhead;
828}
829#endif /* USE_BHEAD_READ_ON_DEMAND */
830
835template<typename T> static const T *blo_bhead_data_at(const BHead *bhead, const int offset)
836{
837 if (offset < 0 || int64_t(offset) + int64_t(sizeof(T)) > bhead->len) [[unlikely]] {
838 return nullptr;
839 }
840 return reinterpret_cast<const T *>(POINTER_OFFSET(bhead, sizeof(*bhead) + offset));
841}
842
844static const char *blo_bhead_string_at(const BHead *bhead, const int offset, const size_t maxsize)
845{
846 const char *str = blo_bhead_data_at<char>(bhead, offset);
847 if (str == nullptr) [[unlikely]] {
848 return nullptr;
849 }
850 /* Ensure the string is null terminated. */
851 const size_t size = std::min(maxsize, size_t(bhead->len - offset));
852 return std::memchr(str, '\0', size) ? str : nullptr;
853}
854
855const char *blo_bhead_id_name(FileData *fd, const BHead *bhead)
856{
857 const char *id_name = blo_bhead_string_at(bhead, fd->id_name_offset, MAX_ID_NAME);
858 /* Names must at least contain the two character ID code, so code using `id_name + 2`
859 * always gets a null terminated string. */
860 if (id_name && id_name[0] != '\0' && id_name[1] != '\0') {
861 return id_name;
862 }
863
864 /* ID name longer than MAX_ID_NAME - 1, or otherwise corrupted. */
866 return nullptr;
867}
868
870{
872 AssetMetaData *const *asset_data = blo_bhead_data_at<AssetMetaData *>(bhead,
874 return asset_data ? *asset_data : nullptr;
875}
876
878{
880 if (std::holds_alternative<BlenderHeaderInvalid>(header_variant)) {
881 return;
882 }
883 if (std::holds_alternative<BlenderHeaderUnknown>(header_variant)) {
885 return;
886 }
887 const BlenderHeader &header = std::get<BlenderHeader>(header_variant);
888 fd->flags |= FD_FLAGS_FILE_OK;
889 if (header.pointer_size == 4) {
891 }
892 if (header.pointer_size != sizeof(void *)) {
894 }
895 if (header.endian != ENDIAN_ORDER) {
897 }
898 fd->fileversion = header.file_version;
899 fd->blender_header = header;
900}
901
905static bool read_file_dna(FileData *fd, const char **r_error_message)
906{
907 BHead *bhead;
908 int subversion = 0;
909
910 for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
911 if (bhead->code == BLO_CODE_GLOB) {
912 /* Before this, the subversion didn't exist in 'FileGlobal' so the subversion
913 * value isn't accessible for the purpose of DNA versioning in this case. */
914 if (fd->fileversion <= 242) {
915 continue;
916 }
917 /* We can't use read_global because this needs 'DNA1' to be decoded,
918 * however the first 4 chars are _always_ the subversion. */
919 BLI_STATIC_ASSERT(offsetof(FileGlobal, subvstr) == 0, "Must be first: subvstr")
920 if (bhead->len < int64_t(sizeof(FileGlobal::subvstr))) [[unlikely]] {
921 continue;
922 }
923 const FileGlobal *fg = reinterpret_cast<const FileGlobal *>(&bhead[1]);
924 char num[5];
925 memcpy(num, fg->subvstr, 4);
926 num[4] = 0;
927 subversion = atoi(num);
928 }
929 else if (bhead->code == BLO_CODE_DNA1) {
930 const bool do_endian_swap = (fd->flags & FD_FLAGS_SWITCH_ENDIAN) != 0;
931 const bool do_alias = false; /* Postpone until after #blo_do_versions_dna runs. */
933 &bhead[1], bhead->len, do_endian_swap, true, do_alias, r_error_message);
934 if (fd->filesdna) {
935 blo_do_versions_dna(fd->filesdna, fd->fileversion, subversion);
936 /* Allow aliased lookups (must be after version patching DNA). */
938
941 fd->filesdna, fd->memsdna, fd->compflags);
942 /* used to retrieve ID names from (bhead+1) */
944 fd->filesdna, "ID", "char", "name[]");
945 BLI_assert(fd->id_name_offset != -1);
947 fd->filesdna, "ID", "AssetMetaData", "*asset_data");
948
949 return true;
950 }
951
952 return false;
953 }
954 else if (bhead->code == BLO_CODE_ENDB) {
955 break;
956 }
957 }
958
959 *r_error_message = "Missing DNA block";
960 return false;
961}
962
964{
965 BHead *bhead;
966 int *blend_thumb = nullptr;
967
968 for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
969 if (bhead->code == BLO_CODE_TEST) {
970 const bool do_endian_swap = (fd->flags & FD_FLAGS_SWITCH_ENDIAN) != 0;
971 int *data = (int *)(bhead + 1);
972
973 if (bhead->len < sizeof(int[2])) {
974 break;
975 }
976
977 if (do_endian_swap) {
980 }
981
982 const int width = data[0];
983 const int height = data[1];
984 if (!BLEN_THUMB_MEMSIZE_IS_VALID(width, height)) {
985 break;
986 }
987 if (bhead->len < BLEN_THUMB_MEMSIZE_FILE(width, height)) {
988 break;
989 }
990
991 blend_thumb = data;
992 break;
993 }
994 if (bhead->code != BLO_CODE_REND) {
995 /* Thumbnail is stored in TEST immediately after first REND... */
996 break;
997 }
998 }
999
1000 return blend_thumb;
1001}
1002
1015{
1016 ListBase *lb_iter;
1017 /* Using a set is needed, to avoid renaming names when there is no collision, and deal with IDs
1018 * being moved around in their list when renamed. A simple set is enough, since here only local
1019 * IDs are processed. */
1021 blender::Set<ID *> processed_ids;
1022
1023 FOREACH_MAIN_LISTBASE_BEGIN (bmain, lb_iter) {
1024 LISTBASE_FOREACH_MUTABLE (ID *, id_iter, lb_iter) {
1025 if (processed_ids.contains(id_iter)) {
1026 continue;
1027 }
1028 processed_ids.add_new(id_iter);
1029 /* Linked IDs can be fully ignored here, 'long names' IDs cannot be linked in any way. */
1030 if (ID_IS_LINKED(id_iter)) {
1031 continue;
1032 }
1033 if (!used_names.contains(id_iter->name)) {
1034 used_names.add_new(id_iter->name);
1035 continue;
1036 }
1037
1039 *bmain, *lb_iter, *id_iter, nullptr, IDNewNameMode::RenameExistingNever, false);
1040 BLI_assert(!used_names.contains(id_iter->name));
1041 used_names.add_new(id_iter->name);
1042 CLOG_INFO(&LOG, 3, "ID name has been de-duplicated to '%s'", id_iter->name);
1043 }
1044 }
1046}
1047
1057{
1058 /* NOTE: A large part of this code follows a similar logic to
1059 * #foreach_action_slot_use_with_references.
1060 *
1061 * However, no slot identifier should ever be skipped here, even if it is not in use in any way,
1062 * since it is critical to remove all non-null terminated strings.
1063 */
1064
1065 ID *id_iter;
1066 FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
1067 switch (GS(id_iter->name)) {
1068 case ID_AC: {
1069 bool has_truncated_slot_identifer = false;
1070 bAction *act = reinterpret_cast<bAction *>(id_iter);
1071 for (int i = 0; i < act->slot_array_num; i++) {
1073 CLOG_INFO(&LOG,
1074 4,
1075 "Truncated too long action slot name to '%s'",
1076 act->slot_array[i]->identifier);
1077 has_truncated_slot_identifer = true;
1078 }
1079 }
1080 if (!has_truncated_slot_identifer) {
1081 continue;
1082 }
1083
1084 /* If there are truncated slots identifiers, ensuring their uniqueness must happen in a
1085 * second loop, to avoid e.g. an attempt to read a slot identifier that has not yet been
1086 * truncated. */
1087 for (int i = 0; i < act->slot_array_num; i++) {
1089 [&](const blender::StringRef name) -> bool {
1090 for (int j = 0; j < act->slot_array_num; j++) {
1091 if (i == j) {
1092 continue;
1093 }
1094 if (act->slot_array[j]->identifier == name) {
1095 return true;
1096 }
1097 }
1098 return false;
1099 },
1100 "",
1101 '.',
1102 act->slot_array[i]->identifier,
1103 sizeof(act->slot_array[i]->identifier));
1104 }
1105 break;
1106 }
1107 case ID_OB: {
1108 auto visit_constraint = [](const bConstraint &constraint) -> bool {
1109 if (constraint.type != CONSTRAINT_TYPE_ACTION) {
1110 return true;
1111 }
1112 bActionConstraint *constraint_data = static_cast<bActionConstraint *>(constraint.data);
1114 CLOG_INFO(&LOG,
1115 4,
1116 "Truncated too long bActionConstraint.last_slot_identifier to '%s'",
1117 constraint_data->last_slot_identifier);
1118 }
1119 return true;
1120 };
1121
1122 Object *object = reinterpret_cast<Object *>(id_iter);
1123 LISTBASE_FOREACH (bConstraint *, con, &object->constraints) {
1124 visit_constraint(*con);
1125 }
1126 if (object->pose) {
1127 LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
1128 LISTBASE_FOREACH (bConstraint *, con, &pchan->constraints) {
1129 visit_constraint(*con);
1130 }
1131 }
1132 }
1133 }
1135 default: {
1136 AnimData *anim_data = BKE_animdata_from_id(id_iter);
1137 if (anim_data) {
1139 CLOG_INFO(&LOG,
1140 4,
1141 "Truncated too long AnimData.last_slot_identifier to '%s'",
1142 anim_data->last_slot_identifier);
1143 }
1145 CLOG_INFO(&LOG,
1146 4,
1147 "Truncated too long AnimData.tmp_last_slot_identifier to '%s'",
1148 anim_data->tmp_last_slot_identifier);
1149 }
1150
1151 blender::bke::nla::foreach_strip_adt(*anim_data, [&](NlaStrip *strip) -> bool {
1152 if (BLI_str_utf8_truncate_at_size(strip->last_slot_identifier, MAX_ID_NAME)) {
1153 CLOG_INFO(&LOG,
1154 4,
1155 "Truncated too long NlaStrip.last_slot_identifier to '%s'",
1156 strip->last_slot_identifier);
1157 }
1158
1159 return true;
1160 });
1161 }
1162 }
1163 }
1165 }
1166}
1167
1170/* -------------------------------------------------------------------- */
1175{
1176 BLI_assert(reports != nullptr);
1177
1178 FileData *fd = MEM_new<FileData>(__func__);
1179
1181
1182 fd->datamap = oldnewmap_new();
1183 fd->globmap = oldnewmap_new();
1184 fd->libmap = oldnewmap_new();
1185
1186 fd->reports = reports;
1187
1188 return fd;
1189}
1190
1200{
1201 BLI_assert(fd->filesdna != nullptr);
1202 for (BHead *bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
1203 if (bhead->code != BLO_CODE_GLOB) {
1204 continue;
1205 }
1206
1208 fd, bhead, "Data from Global block", INDEX_ID_NULL, sizeof(FileGlobal)));
1209 if (fg == nullptr) [[unlikely]] {
1210 BKE_reportf(reports, RPT_ERROR, "Blend file '%s' is corrupt, unable to read", fd->relabase);
1211 return true;
1212 }
1213 if ((fg->minversion > BLENDER_FILE_VERSION) ||
1215 {
1216 char writer_ver_str[16];
1217 char min_reader_ver_str[16];
1218 if (fd->fileversion == fg->minversion) {
1220 writer_ver_str, sizeof(writer_ver_str), short(fd->fileversion), fg->subversion);
1222 min_reader_ver_str, sizeof(min_reader_ver_str), fg->minversion, fg->minsubversion);
1223 }
1224 else {
1226 writer_ver_str, sizeof(writer_ver_str), short(fd->fileversion), -1);
1228 min_reader_ver_str, sizeof(min_reader_ver_str), fg->minversion, -1);
1229 }
1230 BKE_reportf(reports,
1231 RPT_ERROR,
1232 "The file was saved by a newer version, open it with Blender %s or later",
1233 min_reader_ver_str);
1234 CLOG_WARN(&LOG,
1235 "%s: File saved by a newer version of Blender (%s), Blender %s or later is "
1236 "needed to open it.",
1237 fd->relabase,
1238 writer_ver_str,
1239 min_reader_ver_str);
1240 MEM_freeN(fg);
1241 return true;
1242 }
1243 MEM_freeN(fg);
1244 return false;
1245 }
1246 return false;
1247}
1248
1250{
1252
1253 if (fd->flags & FD_FLAGS_FILE_OK) {
1254 const char *error_message = nullptr;
1255 if (read_file_dna(fd, &error_message) == false) {
1257 reports, RPT_ERROR, "Failed to read blend file '%s': %s", fd->relabase, error_message);
1259 fd = nullptr;
1260 }
1261 else if (is_minversion_older_than_blender(fd, reports)) {
1263 fd = nullptr;
1264 }
1265 else if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1266 if (ENDIAN_ORDER == L_ENDIAN) {
1267 if (!BKE_reports_print_test(reports, RPT_WARNING)) {
1268 CLOG_WARN(
1269 &LOG,
1270 "Blend file '%s' created by a Big Endian version of Blender, support for these "
1271 "files will be removed in Blender 5.0",
1272 fd->relabase);
1273 }
1274 BKE_reportf(reports,
1276 "Blend file '%s' created by a Big Endian version of Blender, support for "
1277 "these files will be removed in Blender 5.0",
1278 fd->relabase);
1279 }
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{
1304 char header[7];
1305 FileReader *rawfile = BLI_filereader_new_file(filedes);
1306 FileReader *file = nullptr;
1307
1308 errno = 0;
1309 /* If opening the file failed or we can't read the header, give up. */
1310 if (rawfile == nullptr || rawfile->read(rawfile, header, sizeof(header)) != sizeof(header)) {
1311 BKE_reportf(reports->reports,
1313 "Unable to read '%s': %s",
1314 filepath,
1315 errno ? strerror(errno) : RPT_("insufficient content"));
1316 if (rawfile) {
1317 rawfile->close(rawfile);
1318 }
1319 else {
1320 close(filedes);
1321 }
1322 return nullptr;
1323 }
1324
1325 /* Rewind the file after reading the header. */
1326 rawfile->seek(rawfile, 0, SEEK_SET);
1327
1328 /* Check if we have a regular file. */
1329 if (memcmp(header, "BLENDER", sizeof(header)) == 0) {
1330 /* Try opening the file with memory-mapped IO. */
1331 file = BLI_filereader_new_mmap(filedes);
1332 if (file == nullptr) {
1333 /* `mmap` failed, so just keep using `rawfile`. */
1334 file = rawfile;
1335 rawfile = nullptr;
1336 }
1337 }
1338 else if (BLI_file_magic_is_gzip(header)) {
1339 file = BLI_filereader_new_gzip(rawfile);
1340 if (file != nullptr) {
1341 rawfile = nullptr; /* The `Gzip` #FileReader takes ownership of `rawfile`. */
1342 }
1343 }
1344 else if (BLI_file_magic_is_zstd(header)) {
1345 file = BLI_filereader_new_zstd(rawfile);
1346 if (file != nullptr) {
1347 rawfile = nullptr; /* The `Zstd` #FileReader takes ownership of `rawfile`. */
1348 }
1349 }
1350
1351 /* Clean up `rawfile` if it wasn't taken over. */
1352 if (rawfile != nullptr) {
1353 rawfile->close(rawfile);
1354 }
1355 if (file == nullptr) {
1356 BKE_reportf(reports->reports, RPT_WARNING, "Unrecognized file format '%s'", filepath);
1357 return nullptr;
1358 }
1359
1360 FileData *fd = filedata_new(reports);
1361 fd->file = file;
1362
1363 return fd;
1364}
1365
1366static FileData *blo_filedata_from_file_open(const char *filepath, BlendFileReadReport *reports)
1367{
1368 errno = 0;
1369 const int file = BLI_open(filepath, O_BINARY | O_RDONLY, 0);
1370 if (file == -1) {
1371 BKE_reportf(reports->reports,
1373 "Unable to open '%s': %s",
1374 filepath,
1375 errno ? strerror(errno) : RPT_("unknown error reading file"));
1376 return nullptr;
1377 }
1378 return blo_filedata_from_file_descriptor(filepath, reports, file);
1379}
1380
1382{
1383 FileData *fd = blo_filedata_from_file_open(filepath, reports);
1384 if (fd != nullptr) {
1385 /* needed for library_append and read_libraries */
1386 STRNCPY(fd->relabase, filepath);
1387
1388 return blo_decode_and_check(fd, reports->reports);
1389 }
1390 return nullptr;
1391}
1392
1397static FileData *blo_filedata_from_file_minimal(const char *filepath)
1398{
1399 BlendFileReadReport read_report{};
1400 FileData *fd = blo_filedata_from_file_open(filepath, &read_report);
1401 if (fd != nullptr) {
1403 if (fd->flags & FD_FLAGS_FILE_OK) {
1404 return fd;
1405 }
1407 }
1408 return nullptr;
1409}
1410
1412 const int memsize,
1413 BlendFileReadReport *reports)
1414{
1415 if (!mem || memsize < MIN_SIZEOFBLENDERHEADER) {
1416 BKE_report(
1417 reports->reports, RPT_WARNING, (mem) ? RPT_("Unable to read") : RPT_("Unable to open"));
1418 return nullptr;
1419 }
1420
1421 FileReader *mem_file = BLI_filereader_new_memory(mem, memsize);
1422 FileReader *file = mem_file;
1423
1424 if (BLI_file_magic_is_gzip(static_cast<const char *>(mem))) {
1425 file = BLI_filereader_new_gzip(mem_file);
1426 }
1427 else if (BLI_file_magic_is_zstd(static_cast<const char *>(mem))) {
1428 file = BLI_filereader_new_zstd(mem_file);
1429 }
1430
1431 if (file == nullptr) {
1432 /* Compression initialization failed. */
1433 mem_file->close(mem_file);
1434 return nullptr;
1435 }
1436
1437 FileData *fd = filedata_new(reports);
1438 fd->file = file;
1439
1440 return blo_decode_and_check(fd, reports->reports);
1441}
1442
1445 BlendFileReadReport *reports)
1446{
1447 if (!memfile) {
1448 BKE_report(reports->reports, RPT_WARNING, "Unable to open blend <memory>");
1449 return nullptr;
1450 }
1451
1452 FileData *fd = filedata_new(reports);
1453 fd->file = BLO_memfile_new_filereader(memfile, params->undo_direction);
1454 fd->undo_direction = params->undo_direction;
1456
1457 return blo_decode_and_check(fd, reports->reports);
1458}
1459
1461{
1462 /* Free all BHeadN data blocks */
1463#ifdef NDEBUG
1465#else
1466 /* Sanity check we're not keeping memory we don't need. */
1467 LISTBASE_FOREACH_MUTABLE (BHeadN *, new_bhead, &fd->bhead_list) {
1468# ifdef USE_BHEAD_READ_ON_DEMAND
1469 if (fd->file->seek != nullptr && BHEAD_USE_READ_ON_DEMAND(&new_bhead->bhead)) {
1470 BLI_assert(new_bhead->has_data == 0);
1471 }
1472# endif
1473 MEM_freeN(new_bhead);
1474 }
1475#endif
1476 fd->file->close(fd->file);
1477
1478 if (fd->filesdna) {
1480 }
1481 if (fd->compflags) {
1482 MEM_freeN(fd->compflags);
1483 }
1484 if (fd->reconstruct_info) {
1486 }
1487
1488 if (fd->datamap) {
1490 }
1491 if (fd->globmap) {
1493 }
1494 if (fd->libmap) {
1496 }
1497 if (fd->old_idmap_uid != nullptr) {
1499 }
1500 if (fd->new_idmap_uid != nullptr) {
1502 }
1504 if (fd->bheadmap) {
1505 MEM_freeN(fd->bheadmap);
1506 }
1507
1508 MEM_delete(fd);
1509}
1510
1513/* -------------------------------------------------------------------- */
1518{
1519 BlendThumbnail *data = nullptr;
1520
1522 if (fd) {
1523 if (const int *fd_data = read_file_thumbnail(fd)) {
1524 const int width = fd_data[0];
1525 const int height = fd_data[1];
1526 if (BLEN_THUMB_MEMSIZE_IS_VALID(width, height)) {
1527 const size_t data_size = BLEN_THUMB_MEMSIZE(width, height);
1528 data = static_cast<BlendThumbnail *>(MEM_mallocN(data_size, __func__));
1529 if (data) {
1530 BLI_assert((data_size - sizeof(*data)) ==
1531 (BLEN_THUMB_MEMSIZE_FILE(width, height) - (sizeof(*fd_data) * 2)));
1532 data->width = width;
1533 data->height = height;
1534 memcpy(data->rect, &fd_data[2], data_size - sizeof(*data));
1535 }
1536 }
1537 }
1539 }
1540
1541 return data;
1542}
1543
1544short BLO_version_from_file(const char *filepath)
1545{
1546 short version = 0;
1548 if (fd) {
1549 version = fd->fileversion;
1551 }
1552 return version;
1553}
1554
1557/* -------------------------------------------------------------------- */
1561/* Only direct data-blocks. */
1562static void *newdataadr(FileData *fd, const void *adr)
1563{
1564 return oldnewmap_lookup_and_inc(fd->datamap, adr, true);
1565}
1566
1567/* Only direct data-blocks. */
1568static void *newdataadr_no_us(FileData *fd, const void *adr)
1569{
1570 return oldnewmap_lookup_and_inc(fd->datamap, adr, false);
1571}
1572
1577static NewAddress *newdataadr_entry(FileData *fd, const void *adr)
1578{
1579 return fd->datamap->map.lookup_ptr(adr);
1580}
1581
1583{
1584 return oldnewmap_lookup_and_inc(fd->globmap, adr, true);
1585}
1586
1587/* only lib data */
1588static void *newlibadr(FileData *fd, ID * /*self_id*/, const bool is_linked_only, const void *adr)
1589{
1590 return oldnewmap_liblookup(fd->libmap, adr, is_linked_only);
1591}
1592
1594 ID *self_id,
1595 const bool is_linked_only,
1596 const void *adr)
1597{
1598 return newlibadr(fd, self_id, is_linked_only, adr);
1599}
1600
1601/* increases user number */
1603 const void *old,
1604 void *newp)
1605{
1606 for (NewAddress &entry : fd->libmap->map.values()) {
1607 if (old == entry.newp && entry.nr == ID_LINK_PLACEHOLDER) {
1608 entry.newp = newp;
1609 if (newp) {
1610 entry.nr = GS(((ID *)newp)->name);
1611 }
1612 }
1613 }
1614}
1615
1616/* Very rarely needed, allows some form of ID remapping as part of readfile process.
1617 *
1618 * Currently only used to remap duplicate library pointers.
1619 */
1620static void change_ID_pointer_to_real_ID_pointer_fd(FileData *fd, const void *old, void *newp)
1621{
1622 for (NewAddress &entry : fd->libmap->map.values()) {
1623 if (old == entry.newp) {
1624 BLI_assert(BKE_idtype_idcode_is_valid(short(entry.nr)));
1625 entry.newp = newp;
1626 if (newp) {
1627 entry.nr = GS(((ID *)newp)->name);
1628 }
1629 }
1630 }
1631}
1632
1634{
1635 if (bmain->curlib) {
1636 return bmain->curlib->runtime->filedata;
1637 }
1638 else {
1639 return basefd;
1640 }
1641}
1642
1644 FileData *basefd,
1645 void *old,
1646 void *newp)
1647{
1648 LISTBASE_FOREACH (Main *, mainptr, mainlist) {
1649 FileData *fd = change_ID_link_filedata_get(mainptr, basefd);
1650 if (fd) {
1652 }
1653 }
1654}
1655
1657 FileData *basefd,
1658 void *old,
1659 void *newp)
1660{
1661 LISTBASE_FOREACH (Main *, mainptr, mainlist) {
1662 FileData *fd = change_ID_link_filedata_get(mainptr, basefd);
1663 if (fd) {
1665 }
1666 }
1667}
1668
1670{
1671 if (fd->old_idmap_uid != nullptr) {
1673 }
1674 fd->old_idmap_uid = BKE_main_idmap_create(bmain, false, nullptr, MAIN_IDMAP_TYPE_UID);
1675}
1676
1681
1686
1689 ID *id, const IDCacheKey *key, void **cache_p, uint /*flags*/, void *cache_storage_v)
1690{
1691 BLI_assert(key->id_session_uid == id->session_uid);
1693
1694 BLOCacheStorage *cache_storage = static_cast<BLOCacheStorage *>(cache_storage_v);
1695 BLI_assert(!BLI_ghash_haskey(cache_storage->cache_map, key));
1696
1697 IDCacheKey *storage_key = static_cast<IDCacheKey *>(
1698 BLI_memarena_alloc(cache_storage->memarena, sizeof(*storage_key)));
1699 *storage_key = *key;
1700 BLOCacheStorageValue *storage_value = static_cast<BLOCacheStorageValue *>(
1701 BLI_memarena_alloc(cache_storage->memarena, sizeof(*storage_value)));
1702 storage_value->cache_v = *cache_p;
1703 storage_value->new_usage_count = 0;
1704 BLI_ghash_insert(cache_storage->cache_map, storage_key, storage_value);
1705}
1706
1709 ID *id, const IDCacheKey *key, void **cache_p, const uint flags, void *cache_storage_v)
1710{
1711 BLOCacheStorage *cache_storage = static_cast<BLOCacheStorage *>(cache_storage_v);
1712
1713 if (cache_storage == nullptr) {
1714 /* In non-undo case, only clear the pointer if it is a purely runtime one.
1715 * If it may be stored in a persistent way in the .blend file, direct_link code is responsible
1716 * to properly deal with it. */
1717 if ((flags & IDTYPE_CACHE_CB_FLAGS_PERSISTENT) == 0) {
1718 *cache_p = nullptr;
1719 }
1720 return;
1721 }
1722
1723 /* Assume that when ID source is tagged as changed, its caches need to be cleared.
1724 * NOTE: This is mainly a work-around for some IDs, like Image, which use a non-depsgraph-handled
1725 * process for part of their updates.
1726 */
1727 if (id->recalc & ID_RECALC_SOURCE) {
1728 *cache_p = nullptr;
1729 return;
1730 }
1731
1732 BLOCacheStorageValue *storage_value = static_cast<BLOCacheStorageValue *>(
1733 BLI_ghash_lookup(cache_storage->cache_map, key));
1734 if (storage_value == nullptr) {
1735 *cache_p = nullptr;
1736 return;
1737 }
1738 storage_value->new_usage_count++;
1739 *cache_p = storage_value->cache_v;
1740}
1741
1744 const IDCacheKey *key,
1745 void **cache_p,
1746 const uint /*flags*/,
1747 void *cache_storage_v)
1748{
1749 BLOCacheStorage *cache_storage = static_cast<BLOCacheStorage *>(cache_storage_v);
1750
1751 BLOCacheStorageValue *storage_value = static_cast<BLOCacheStorageValue *>(
1752 BLI_ghash_lookup(cache_storage->cache_map, key));
1753 if (storage_value == nullptr) {
1754 *cache_p = nullptr;
1755 return;
1756 }
1757 /* If that cache has been restored into some new ID, we want to remove it from old one, otherwise
1758 * keep it there so that it gets properly freed together with its ID. */
1759 if (storage_value->new_usage_count != 0) {
1760 *cache_p = nullptr;
1761 }
1762 else {
1763 BLI_assert(*cache_p == storage_value->cache_v);
1764 }
1765}
1766
1768{
1769 if (fd->flags & FD_FLAGS_IS_MEMFILE) {
1770 BLI_assert(fd->cache_storage == nullptr);
1775
1776 ListBase *lb;
1777 FOREACH_MAIN_LISTBASE_BEGIN (bmain, lb) {
1778 ID *id = static_cast<ID *>(lb->first);
1779 if (id == nullptr) {
1780 continue;
1781 }
1782
1783 const IDTypeInfo *type_info = BKE_idtype_get_info_from_id(id);
1784 if (type_info->foreach_cache == nullptr) {
1785 continue;
1786 }
1787
1789 if (ID_IS_LINKED(id)) {
1790 continue;
1791 }
1793 }
1795 }
1797 }
1798 else {
1799 fd->cache_storage = nullptr;
1800 }
1801}
1802
1804{
1805 if (fd->cache_storage != nullptr) {
1806 ListBase *lb;
1807 FOREACH_MAIN_LISTBASE_BEGIN (bmain_old, lb) {
1808 ID *id = static_cast<ID *>(lb->first);
1809 if (id == nullptr) {
1810 continue;
1811 }
1812
1813 const IDTypeInfo *type_info = BKE_idtype_get_info_from_id(id);
1814 if (type_info->foreach_cache == nullptr) {
1815 continue;
1816 }
1817
1819 if (ID_IS_LINKED(id)) {
1820 continue;
1821 }
1823 }
1825 }
1827 }
1828}
1829
1831{
1832 if (fd->cache_storage != nullptr) {
1833 BLI_ghash_free(fd->cache_storage->cache_map, nullptr, nullptr);
1836 fd->cache_storage = nullptr;
1837 }
1838}
1839
1842/* -------------------------------------------------------------------- */
1846static void switch_endian_structs(const SDNA *filesdna, BHead *bhead)
1847{
1848 int blocksize, nblocks;
1849 char *data;
1850
1851 data = (char *)(bhead + 1);
1852
1853 blocksize = DNA_struct_size(filesdna, bhead->SDNAnr);
1854
1855 nblocks = bhead->nr;
1856 while (nblocks--) {
1857 DNA_struct_switch_endian(filesdna, bhead->SDNAnr, data);
1858
1859 data += blocksize;
1860 }
1861}
1862
1869static const char *get_alloc_name(FileData *fd,
1870 BHead *bh,
1871 const char *blockname,
1872 const int id_type_index = INDEX_ID_NULL)
1873{
1874#ifndef NDEBUG
1875 /* Storage key is a pair of (string , int), where the first is the concatenation of the 'owner
1876 * block' string and DNA struct type name, and the second the length of the array, as defined by
1877 * the #BHead.nr value. */
1878 using keyT = const std::pair<const std::string, const int>;
1879#else
1880 /* Storage key is simple int, which is the ID type index. */
1881 using keyT = int;
1882#endif
1883 constexpr std::string_view STORAGE_ID = "readfile";
1884
1885 /* NOTE: This is thread_local storage, so as long as the handling of a same FileData is not
1886 * spread across threads (which is not supported at all currently), this is thread-safe. */
1887 if (!fd->storage_handle) {
1889 std::string(STORAGE_ID));
1890 }
1893 fd->storage_handle);
1894
1895 const bool is_id_data = !blockname && (id_type_index >= 0 && id_type_index < INDEX_ID_MAX);
1896
1897#ifndef NDEBUG
1898 /* Local storage of id type names, for fast access to this info. */
1899 static const std::array<std::string, INDEX_ID_MAX> id_alloc_names = [] {
1900 auto n = decltype(id_alloc_names)();
1901 for (int idtype_index = 0; idtype_index < INDEX_ID_MAX; idtype_index++) {
1902 const IDTypeInfo *idtype_info = BKE_idtype_get_info_from_idtype_index(idtype_index);
1903 BLI_assert(idtype_info);
1904 if (idtype_index == INDEX_ID_NULL) {
1905 /* #INDEX_ID_NULL returns the #IDType_ID_LINK_PLACEHOLDER type info, here we will rather
1906 * use it for unknown/invalid ID types. */
1907 n[size_t(idtype_index)] = "UNKNWOWN";
1908 }
1909 else {
1910 n[size_t(idtype_index)] = idtype_info->name;
1911 }
1912 }
1913 return n;
1914 }();
1915
1916 const std::string block_alloc_name = is_id_data ? id_alloc_names[id_type_index] : blockname;
1917 const std::string struct_name = DNA_struct_identifier(fd->filesdna, bh->SDNAnr);
1918 keyT key{block_alloc_name + struct_name, bh->nr};
1919 if (!storage.contains(key)) {
1920 const std::string alloc_string = fmt::format(
1921 fmt::runtime((is_id_data ? "{}{} (for ID type '{}')" : "{}{} (for block '{}')")),
1922 struct_name,
1923 bh->nr > 1 ? fmt::format("[{}]", bh->nr) : "",
1924 block_alloc_name);
1925 return storage.insert(key, alloc_string);
1926 }
1927 return storage.find(key);
1928#else
1929 /* Simple storage for pure release builds, using integer as key, one entry for each ID type. */
1931 if (is_id_data) {
1932 if (UNLIKELY(!storage.contains(id_type_index))) {
1933 if (id_type_index == INDEX_ID_NULL) {
1934 return storage.insert(id_type_index, "Data from UNKNOWN");
1935 }
1936 const IDTypeInfo *id_type = BKE_idtype_get_info_from_idtype_index(id_type_index);
1937 const std::string alloc_string = fmt::format("Data from '{}' ID type", id_type->name);
1938 return storage.insert(id_type_index, alloc_string);
1939 }
1940 return storage.find(id_type_index);
1941 }
1942 return blockname;
1943#endif
1944}
1945
1946static void *read_struct(FileData *fd, BHead *bh, const char *blockname, const int id_type_index)
1947{
1948 void *temp = nullptr;
1949
1950 if (bh->len) {
1951#ifdef USE_BHEAD_READ_ON_DEMAND
1952 BHead *bh_orig = bh;
1953#endif
1954
1955 /* A corrupt file could reference a struct index outside the file's SDNA, used below to index
1956 * the compare-flags, reconstruction & alignment tables. */
1957 if (bh->SDNAnr < 0 || bh->SDNAnr >= fd->filesdna->structs_num) [[unlikely]] {
1958 fd->flags &= ~FD_FLAGS_FILE_OK;
1959 if (Main *bmain = filedata_main(fd)) {
1960 blo_readfile_invalidate(fd, bmain, "Corrupt .blend file, invalid block struct index");
1961 }
1962 return nullptr;
1963 }
1964
1965 /* The block must be large enough to hold the number of structs it claims, otherwise
1966 * reconstruction (which strides the block by the file struct size) reads past its end.
1967 * Check any head that is not raw data and not a removed struct.
1968 * Written as a division to avoid overflow in `nr * struct_size`. */
1970 {
1971 const int64_t old_struct_size = DNA_struct_size(fd->filesdna, bh->SDNAnr);
1972 if (bh->nr < 1 || bh->nr > INT_MAX ||
1973 (old_struct_size != 0 && bh->nr > bh->len / old_struct_size)) [[unlikely]]
1974 {
1975 fd->flags &= ~FD_FLAGS_FILE_OK;
1976 if (Main *bmain = filedata_main(fd)) {
1977 blo_readfile_invalidate(fd, bmain, "Corrupt .blend file, invalid block count");
1978 }
1979 return nullptr;
1980 }
1981 }
1982
1983 /* Endianness switch is based on file DNA.
1984 *
1985 * NOTE: raw data (aka #SDNA_RAW_DATA_STRUCT_INDEX #SDNAnr) is not handled here, it's up to
1986 * the calling code to manage this. */
1987 BLI_STATIC_ASSERT(SDNA_RAW_DATA_STRUCT_INDEX == 0, "'raw data' SDNA struct index should be 0")
1989#ifdef USE_BHEAD_READ_ON_DEMAND
1990 if (BHEADN_FROM_BHEAD(bh)->has_data == false) {
1991 bh = blo_bhead_read_full(fd, bh);
1992 if (UNLIKELY(bh == nullptr)) {
1993 fd->flags &= ~FD_FLAGS_FILE_OK;
1994 return nullptr;
1995 }
1996 }
1997#endif
1999 }
2000
2001 if (fd->compflags[bh->SDNAnr] != SDNA_CMP_REMOVED) {
2002 const char *alloc_name = get_alloc_name(fd, bh, blockname, id_type_index);
2003 if (fd->compflags[bh->SDNAnr] == SDNA_CMP_NOT_EQUAL) {
2004#ifdef USE_BHEAD_READ_ON_DEMAND
2005 if (BHEADN_FROM_BHEAD(bh)->has_data == false) {
2006 bh = blo_bhead_read_full(fd, bh);
2007 if (UNLIKELY(bh == nullptr)) {
2008 fd->flags &= ~FD_FLAGS_FILE_OK;
2009 return nullptr;
2010 }
2011 }
2012#endif
2014 fd->reconstruct_info, bh->SDNAnr, bh->nr, (bh + 1), alloc_name);
2015 }
2016 else {
2017 /* SDNA_CMP_EQUAL */
2018 const int alignment = DNA_struct_alignment(fd->filesdna, bh->SDNAnr);
2019 temp = MEM_mallocN_aligned(bh->len, alignment, alloc_name);
2020#ifdef USE_BHEAD_READ_ON_DEMAND
2021 if (BHEADN_FROM_BHEAD(bh)->has_data) {
2022 memcpy(temp, (bh + 1), bh->len);
2023 }
2024 else {
2025 /* Instead of allocating the bhead, then copying it,
2026 * read the data from the file directly into the memory. */
2027 if (UNLIKELY(!blo_bhead_read_data(fd, bh, temp))) {
2028 fd->flags &= ~FD_FLAGS_FILE_OK;
2029 MEM_freeN(temp);
2030 temp = nullptr;
2031 }
2032 }
2033#else
2034 memcpy(temp, (bh + 1), bh->len);
2035#endif
2036 }
2037 }
2038
2039#ifdef USE_BHEAD_READ_ON_DEMAND
2040 if (bh_orig != bh) {
2042 }
2043#endif
2044 }
2045
2046 return temp;
2047}
2048
2051 BHead *bh,
2052 const char *blockname,
2053 const int id_type_index,
2054 const int64_t min_size)
2055{
2056 void *data = read_struct(fd, bh, blockname, id_type_index);
2057 if (data == nullptr || int64_t(MEM_allocN_len(data)) >= min_size) {
2058 return data;
2059 }
2060
2061 MEM_freeN(data);
2062 fd->flags &= ~FD_FLAGS_FILE_OK;
2063 if (Main *bmain = filedata_main(fd)) {
2064 blo_readfile_invalidate(fd, bmain, "Corrupt .blend file, block too small for its type");
2065 }
2066 return nullptr;
2067}
2068
2069static ID *read_id_struct(FileData *fd, BHead *bh, const char *blockname, const int id_type_index)
2070{
2071 ID *id = static_cast<ID *>(read_struct(fd, bh, blockname, id_type_index));
2072 if (!id) {
2073 return id;
2074 }
2075
2076 /* Verify the allocation size is large enough for the ID type. */
2077 const int64_t alloc_len = int64_t(MEM_allocN_len(id));
2078 int64_t min_size = sizeof(ID);
2079 if (alloc_len >= min_size && bh->code != ID_LINK_PLACEHOLDER) {
2080 if (const IDTypeInfo *id_type = BKE_idtype_get_info_from_idcode(GS(id->name))) {
2081 min_size = id_type->struct_size;
2082 }
2083 }
2084 if (alloc_len < min_size) [[unlikely]] {
2085 MEM_freeN(id);
2086 fd->flags &= ~FD_FLAGS_FILE_OK;
2087 if (Main *bmain = filedata_main(fd)) {
2088 blo_readfile_invalidate(fd, bmain, "Corrupt .blend file, block too small for its type");
2089 }
2090 return nullptr;
2091 }
2092
2093 /* Invalid ID name (probably from 'too long' ID name from a future Blender version).
2094 *
2095 * They can only be truncated here, ensuring that all ID names remain unique happens later, after
2096 * reading all local IDs, but before linking them, see the call to
2097 * #long_id_names_ensure_unique_id_names in #blo_read_file_internal. */
2100 CLOG_INFO(&LOG, 3, "Truncated too long ID name to '%s'", id->name);
2101 }
2102
2103 return id;
2104}
2105
2106/* Like read_struct, but gets a pointer without allocating. Only works for
2107 * undo since DNA must match. */
2108static const void *peek_struct_undo(FileData *fd, BHead *bhead)
2109{
2112 return (bhead->len) ? (const void *)(bhead + 1) : nullptr;
2113}
2114
2115static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
2116{
2117 Link *ln, *prev;
2118 void *poin;
2119
2120 if (BLI_listbase_is_empty(lb)) {
2121 return;
2122 }
2123 poin = newdataadr(fd, lb->first);
2124 if (lb->first) {
2125 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
2126 }
2127 lb->first = poin;
2128
2129 ln = static_cast<Link *>(lb->first);
2130 prev = nullptr;
2131 while (ln) {
2132 poin = newdataadr(fd, ln->next);
2133 if (ln->next) {
2134 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
2135 }
2136 ln->next = static_cast<Link *>(poin);
2137 ln->prev = prev;
2138 prev = ln;
2139 ln = ln->next;
2140 }
2141 lb->last = prev;
2142}
2143
2146/* -------------------------------------------------------------------- */
2150static void after_liblink_id_process(BlendLibReader *reader, ID *id);
2151
2153{
2154
2155 /* Handle 'private IDs'. */
2157 if (nodetree != nullptr) {
2158 after_liblink_id_process(reader, &nodetree->id);
2159
2160 if (nodetree->owner_id == nullptr) {
2161 CLOG_WARN(&LOG,
2162 "NULL owner_id pointer for embedded NodeTree of %s, should never happen",
2163 id->name);
2164 nodetree->owner_id = id;
2165 }
2166 else if (nodetree->owner_id != id) {
2167 CLOG_WARN(&LOG,
2168 "Inconsistent owner_id pointer for embedded NodeTree of %s, should never happen",
2169 id->name);
2170 nodetree->owner_id = id;
2171 }
2172 }
2173
2174 if (GS(id->name) == ID_SCE) {
2175 Scene *scene = (Scene *)id;
2176 if (scene->master_collection != nullptr) {
2178
2179 if (scene->master_collection->owner_id == nullptr) {
2180 CLOG_WARN(&LOG,
2181 "NULL owner_id pointer for embedded Scene Collection of %s, should never happen",
2182 id->name);
2184 }
2185 else if (scene->master_collection->owner_id != id) {
2186 CLOG_WARN(&LOG,
2187 "Inconsistent owner_id pointer for embedded Scene Collection of %s, should "
2188 "never happen",
2189 id->name);
2191 }
2192 }
2193 }
2194}
2195
2197{
2198 /* NOTE: WM IDProperties are never written to file, hence they should always be nullptr here. */
2199 BLI_assert((GS(id->name) != ID_WM) || id->properties == nullptr);
2200
2202
2203 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id);
2204 if (id_type->blend_read_after_liblink != nullptr) {
2205 id_type->blend_read_after_liblink(reader, id);
2206 }
2207}
2208
2211{
2212 BLO_read_string(reader, &op->rna_path);
2213
2214 op->tag = 0; /* Runtime only. */
2215
2217
2219 BLO_read_string(reader, &opop->subitem_reference_name);
2220 BLO_read_string(reader, &opop->subitem_local_name);
2221
2222 opop->tag = 0; /* Runtime only. */
2223 }
2224}
2225
2226static void direct_link_id_common(BlendDataReader *reader,
2227 Library *current_library,
2228 ID *id,
2229 ID *id_old,
2230 int id_tag,
2231 ID_Readfile_Data::Tags id_read_tags);
2232
2234 Library *current_library,
2235 ID *id,
2236 ID *id_old)
2237{
2238 /* Handle 'private IDs'. */
2240 if (nodetree != nullptr && *nodetree != nullptr) {
2241 BLO_read_struct(reader, bNodeTree, nodetree);
2242 if (!*nodetree || !BKE_idtype_idcode_is_valid(GS((*nodetree)->id.name))) {
2244 reader->fd->reports,
2245 RPT_ERROR,
2246 RPT_("Data-block '%s' had an invalid embedded node group, which has not been read"),
2247 id->name);
2248 MEM_SAFE_FREE(*nodetree);
2249 }
2250 else {
2251 direct_link_id_common(reader,
2252 current_library,
2253 (ID *)*nodetree,
2254 id_old != nullptr ? (ID *)blender::bke::node_tree_from_id(id_old) :
2255 nullptr,
2256 0,
2258 blender::bke::node_tree_blend_read_data(reader, id, *nodetree);
2259 }
2260 }
2261
2262 if (GS(id->name) == ID_SCE) {
2263 Scene *scene = (Scene *)id;
2264 if (scene->master_collection != nullptr) {
2266 if (!scene->master_collection ||
2268 {
2270 reader->fd->reports,
2271 RPT_ERROR,
2272 RPT_("Scene '%s' had an invalid root collection, which has not been read"),
2273 BKE_id_name(*id));
2275 }
2276 else {
2277 direct_link_id_common(reader,
2278 current_library,
2280 id_old != nullptr ? &((Scene *)id_old)->master_collection->id :
2281 nullptr,
2282 0,
2285 }
2286 }
2287 }
2288}
2289
2291{
2292 /* Exception for armature objects, where the pose has direct points to the
2293 * armature data-block. */
2294 if (GS(id_current->name) == ID_OB && ((Object *)id_current)->pose) {
2295 return ID_RECALC_GEOMETRY;
2296 }
2297
2298 return 0;
2299}
2300
2302 const ID *id_target,
2303 const ID *id_current,
2304 const bool is_identical)
2305{
2306 /* These are the evaluations that had not been performed yet at the time the
2307 * target undo state was written. These need to be done again, since they may
2308 * flush back changes to the original datablock. */
2309 int recalc = id_target->recalc;
2310
2311 if (id_current == nullptr) {
2312 /* ID does not currently exist in the database, so also will not exist in
2313 * the dependency graphs. That means it will be newly created and as a
2314 * result also fully re-evaluated regardless of the recalc flag set here. */
2315 recalc |= ID_RECALC_ALL;
2316 }
2317 else {
2318 /* If the contents datablock changed, the depsgraph needs to copy the
2319 * datablock again to ensure it matches the original datablock. */
2320 if (!is_identical) {
2321 recalc |= ID_RECALC_SYNC_TO_EVAL;
2322 }
2323
2324 /* Special exceptions. */
2325 recalc |= direct_link_id_restore_recalc_exceptions(id_current);
2326
2327 /* Evaluations for the current state that have not been performed yet
2328 * by the time we are performing this undo step. */
2329 recalc |= id_current->recalc;
2330
2331 /* Tags that were set between the target state and the current state,
2332 * that we need to perform again. */
2333 if (fd->undo_direction == STEP_UNDO) {
2334 /* Undo: tags from target to the current state. */
2335 recalc |= id_current->recalc_up_to_undo_push;
2336 }
2337 else {
2339 /* Redo: tags from current to the target state. */
2340 recalc |= id_target->recalc_up_to_undo_push;
2341 }
2342 }
2343
2344 return recalc;
2345}
2346
2348{
2349 if (id.runtime.readfile_data) {
2350 return;
2351 }
2352 id.runtime.readfile_data = MEM_callocN<ID_Readfile_Data>(__func__);
2353}
2354
2356{
2357 if (!id.runtime.readfile_data) {
2358 return ID_Readfile_Data::Tags{};
2359 }
2360 return id.runtime.readfile_data->tags;
2361}
2362
2364{
2366 return id.runtime.readfile_data->tags;
2367}
2368
2370{
2371 MEM_SAFE_FREE(id.runtime.readfile_data);
2372}
2373
2375{
2376 ID *id;
2377 FOREACH_MAIN_ID_BEGIN (&bmain, id) {
2378 /* Handle the ID itself. */
2380
2381 /* Handle its embedded IDs, because they do not get referenced by bmain. */
2382 if (GS(id->name) == ID_SCE) {
2383 Collection *collection = reinterpret_cast<Scene *>(id)->master_collection;
2384 if (collection) {
2386 }
2387 }
2388
2390 if (node_tree) {
2392 }
2393 }
2395}
2396
2398 Library *current_library,
2399 ID *id,
2400 ID *id_old,
2401 const int id_tag,
2402 const ID_Readfile_Data::Tags id_read_tags)
2403{
2404 /* This should have been caught already, either by a call to `#blo_bhead_is_id_valid_type` for
2405 * regular IDs, or in `#direct_link_id_embedded_id` for embedded ones. */
2407 "Unknown or invalid ID type, this should never happen");
2408
2409 if (!BLO_read_data_is_undo(reader)) {
2410 /* When actually reading a file, we do want to reset/re-generate session UIDS.
2411 * In undo case, we want to re-use existing ones. */
2412 id->session_uid = MAIN_ID_SESSION_UID_UNSET;
2413 }
2414
2415 id->lib = current_library;
2416 if (id->lib) {
2417 /* Always fully clear fake user flag for linked data. */
2418 id->flag &= ~ID_FLAG_FAKEUSER;
2419 }
2420 id->us = ID_FAKE_USERS(id);
2421 id->icon_id = 0;
2422 id->newid = nullptr; /* Needed because .blend may have been saved with crap value here... */
2423 id->orig_id = nullptr;
2424 id->py_instance = nullptr;
2425
2426 /* Initialize with provided tag. */
2427 if (BLO_read_data_is_undo(reader)) {
2428 id->tag = (id_tag & ~ID_TAG_KEEP_ON_UNDO) | (id->tag & ID_TAG_KEEP_ON_UNDO);
2429 }
2430 else {
2431 id->tag = id_tag;
2432 }
2433
2434 if (!BLO_read_data_is_undo(reader)) {
2435 /* Reset the runtime data, as there were versions of Blender that did not do
2436 * this before writing to disk. */
2437 id->runtime = ID_Runtime{};
2438 }
2440 id->runtime.readfile_data->tags = id_read_tags;
2441
2442 if ((id_tag & ID_TAG_TEMP_MAIN) == 0) {
2444 }
2445
2446 if (ID_IS_LINKED(id)) {
2447 id->library_weak_reference = nullptr;
2448 }
2449 else {
2451 }
2452
2454 /* For placeholder we only need to set the tag and properly initialize generic ID fields above,
2455 * no further data to read. */
2456 return;
2457 }
2458
2459 BKE_animdata_blend_read_data(reader, id);
2460
2462 if (id->asset_data) {
2464 /* Restore runtime asset type info. */
2465 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id);
2466 id->asset_data->local_type_info = id_type->asset_type_info;
2467 }
2468
2469 /* Link direct data of ID properties. */
2470 if (id->properties) {
2471 BLO_read_struct(reader, IDProperty, &id->properties);
2472 /* this case means the data was written incorrectly, it should not happen */
2473 IDP_BlendDataRead(reader, &id->properties);
2474 }
2475
2476 if (id->system_properties) {
2479 }
2480
2481 id->flag &= ~ID_FLAG_INDIRECT_WEAK_LINK;
2482
2483 /* NOTE: It is important to not clear the recalc flags for undo/redo.
2484 * Preserving recalc flags on redo/undo is the only way to make dependency graph detect
2485 * that animation is to be evaluated on undo/redo. If this is not enforced by the recalc
2486 * flags dependency graph does not do animation update to avoid loss of unkeyed changes.,
2487 * which conflicts with undo/redo of changes to animation data itself.
2488 *
2489 * But for regular file load we clear the flag, since the flags might have been changed since
2490 * the version the file has been saved with. */
2491 if (!BLO_read_data_is_undo(reader)) {
2492 id->recalc = 0;
2493 id->recalc_after_undo_push = 0;
2494 }
2495 else if ((reader->fd->skip_flags & BLO_READ_SKIP_UNDO_OLD_MAIN) == 0) {
2496 id->recalc = direct_link_id_restore_recalc(reader->fd, id, id_old, false);
2497 id->recalc_after_undo_push = 0;
2498 }
2499
2500 /* Link direct data of overrides. */
2501 if (id->override_library) {
2503 /* Work around file corruption on writing, see #86853. */
2504 if (id->override_library != nullptr) {
2508 }
2509 id->override_library->runtime = nullptr;
2510 }
2511 }
2512
2513 /* Handle 'private IDs'. */
2514 direct_link_id_embedded_id(reader, current_library, id, id_old);
2515}
2516
2519/* -------------------------------------------------------------------- */
2525/* -------------------------------------------------------------------- */
2530{
2531 key->uidgen = 1;
2532 LISTBASE_FOREACH (KeyBlock *, block, &key->block) {
2533 block->uid = key->uidgen++;
2534 }
2535}
2536
2539/* -------------------------------------------------------------------- */
2543#ifdef USE_SETSCENE_CHECK
2547static bool scene_validate_setscene__liblink(Scene *sce, const int totscene)
2548{
2549 Scene *sce_iter;
2550 int a;
2551
2552 if (sce->set == nullptr) {
2553 return true;
2554 }
2555
2556 for (a = 0, sce_iter = sce; sce_iter->set; sce_iter = sce_iter->set, a++) {
2557 /* This runs per library (before each libraries #Main has been joined),
2558 * so we can't step into other libraries since `totscene` is only for this library.
2559 *
2560 * Also, other libraries may not have been linked yet, while we could check for
2561 * #ID_Readfile_Data::Tags.needs_linking the library pointer check is sufficient. */
2562 if (sce->id.lib != sce_iter->id.lib) {
2563 return true;
2564 }
2566 return true;
2567 }
2568
2569 if (a > totscene) {
2570 sce->set = nullptr;
2571 return false;
2572 }
2573 }
2574
2575 return true;
2576}
2577#endif
2578
2580{
2581#ifdef USE_SETSCENE_CHECK
2582 const int totscene = BLI_listbase_count(&bmain->scenes);
2583 LISTBASE_FOREACH (Scene *, sce, &bmain->scenes) {
2585 sce->flag &= ~SCE_READFILE_LIBLINK_NEED_SETSCENE_CHECK;
2586 if (!scene_validate_setscene__liblink(sce, totscene)) {
2587 CLOG_WARN(&LOG, "Found cyclic background scene when linking %s", sce->id.name + 2);
2588 }
2589 }
2590 }
2591#else
2592 UNUSED_VARS(bmain, totscene);
2593#endif
2594}
2595
2596#undef USE_SETSCENE_CHECK
2597
2600/* -------------------------------------------------------------------- */
2601
2606{
2607 /* Corrupt file might not be null terminated. */
2608 return std::memchr(lib->filepath, '\0', sizeof(lib->filepath)) != nullptr;
2609}
2610
2612{
2613 if (!library_filepath_is_valid(lib)) [[unlikely]] {
2614 blo_readfile_invalidate(fd, main, "Corrupt .blend file, invalid library file path");
2615 lib->filepath[0] = '\0';
2616 /* Continue processing to avoid invalid pointers in this datablock that
2617 * would be read by later code. */
2618 }
2619
2620 /* Make sure we have full path in lib->runtime->filepath_abs */
2621 /* NOTE: Since existing libraries are searched by their absolute path, this has to be generated
2622 * before the lookup below. Otherwise, in case the stored absolute filepath is not 'correct' (may
2623 * be empty, or have been stored in a different 'relative path context'), the comparison below
2624 * will always fail, leading to creating duplicates IDs of a same library. */
2625 /* TODO: May be worth checking whether comparison below could use `lib->filepath` instead? */
2626 STRNCPY(lib->runtime->filepath_abs, lib->filepath);
2627 BLI_path_abs(lib->runtime->filepath_abs, fd->relabase);
2628 BLI_path_normalize(lib->runtime->filepath_abs);
2629
2630 /* check if the library was already read */
2631 LISTBASE_FOREACH (Main *, newmain, fd->mainlist) {
2632 if (newmain->curlib) {
2633 if (BLI_path_cmp(newmain->curlib->runtime->filepath_abs, lib->runtime->filepath_abs) == 0) {
2636 RPT_("Library '%s', '%s' had multiple instances, save and reload!"),
2637 lib->filepath,
2638 lib->runtime->filepath_abs);
2639
2640 change_ID_pointer_to_real_ID_pointer(fd->mainlist, fd, lib, newmain->curlib);
2641 // change_link_placeholder_to_real_ID_pointer_fd(fd, lib, newmain->curlib);
2642
2643 BLI_remlink(&main->libraries, lib);
2644 MEM_freeN(lib);
2645
2646 /* Now, since Blender always expect **latest** Main pointer from fd->mainlist
2647 * to be the active library Main pointer,
2648 * where to add all non-library data-blocks found in file next, we have to switch that
2649 * 'dupli' found Main to latest position in the list!
2650 * Otherwise, you get weird disappearing linked data on a rather inconsistent basis.
2651 * See also #53977 for reproducible case. */
2652 BLI_remlink(fd->mainlist, newmain);
2653 BLI_addtail(fd->mainlist, newmain);
2654
2655 return;
2656 }
2657 }
2658 }
2659
2660 // printf("direct_link_library: filepath %s\n", lib->filepath);
2661 // printf("direct_link_library: filepath_abs %s\n", lib->runtime->filepath_abs);
2662
2663 BlendDataReader reader = {fd};
2664 BKE_packedfile_blend_read(&reader, &lib->packedfile, lib->filepath);
2665
2666 /* new main */
2667 Main *newmain = BKE_main_new();
2668 BLI_addtail(fd->mainlist, newmain);
2669 newmain->curlib = lib;
2670
2671 lib->runtime->parent = nullptr;
2672
2673 id_us_ensure_real(&lib->id);
2674}
2675
2676/* Always call this once you have loaded new library data to set the relative paths correctly
2677 * in relation to the blend file. */
2678static void fix_relpaths_library(const char *basepath, Main *main)
2679{
2680 /* #BLO_read_from_memory uses a blank file-path. */
2681 if (basepath == nullptr || basepath[0] == '\0') {
2682 LISTBASE_FOREACH (Library *, lib, &main->libraries) {
2683 /* when loading a linked lib into a file which has not been saved,
2684 * there is nothing we can be relative to, so instead we need to make
2685 * it absolute. This can happen when appending an object with a relative
2686 * link into an unsaved blend file. See #27405.
2687 * The remap relative option will make it relative again on save - campbell */
2688 if (BLI_path_is_rel(lib->filepath)) {
2689 STRNCPY(lib->filepath, lib->runtime->filepath_abs);
2690 }
2691 }
2692 }
2693 else {
2694 LISTBASE_FOREACH (Library *, lib, &main->libraries) {
2695 /* Libraries store both relative and abs paths, recreate relative paths,
2696 * relative to the blend file since indirectly linked libraries will be
2697 * relative to their direct linked library. */
2698 if (BLI_path_is_rel(lib->filepath)) { /* if this is relative to begin with? */
2699 STRNCPY(lib->filepath, lib->runtime->filepath_abs);
2700 BLI_path_rel(lib->filepath, basepath);
2701 }
2702 }
2703 }
2704}
2705
2708/* -------------------------------------------------------------------- */
2712static ID *create_placeholder(Main *mainvar,
2713 const short idcode,
2714 const char *idname,
2715 const int tag,
2716 const bool was_liboverride)
2717{
2718 ListBase *lb = which_libbase(mainvar, idcode);
2719 ID *ph_id = BKE_libblock_alloc_notest(idcode);
2720
2721 *((short *)ph_id->name) = idcode;
2722 BLI_strncpy(ph_id->name + 2, idname, sizeof(ph_id->name) - 2);
2724 ph_id->lib = mainvar->curlib;
2725 ph_id->tag = tag | ID_TAG_MISSING;
2726 ph_id->us = ID_FAKE_USERS(ph_id);
2727 ph_id->icon_id = 0;
2728
2729 if (was_liboverride) {
2730 /* 'Abuse' `ID_TAG_LIBOVERRIDE_NEED_RESYNC` to mark that placeholder missing linked ID as
2731 * being a liboverride.
2732 *
2733 * This will be used by the liboverride resync process, see #lib_override_library_resync. */
2735 }
2736
2737 BLI_addtail(lb, ph_id);
2738 id_sort_by_name(lb, ph_id, nullptr);
2739
2740 if (mainvar->id_map != nullptr) {
2741 BKE_main_idmap_insert_id(mainvar->id_map, ph_id);
2742 }
2743
2744 if ((tag & ID_TAG_TEMP_MAIN) == 0) {
2746 }
2747
2748 return ph_id;
2749}
2750
2752{
2753 /* Placeholder ObData IDs won't have any material, we have to update their objects for that,
2754 * otherwise the inconsistency between both will lead to crashes (especially in Eevee?). */
2755 LISTBASE_FOREACH (Object *, ob, &bmain->objects) {
2756 ID *obdata = static_cast<ID *>(ob->data);
2757 if (obdata != nullptr && obdata->tag & ID_TAG_MISSING) {
2758 BKE_object_materials_sync_length(bmain, ob, obdata);
2759 }
2760 }
2761}
2762
2763static bool direct_link_id(FileData *fd,
2764 Main *main,
2765 const int tag,
2766 const ID_Readfile_Data::Tags id_read_tags,
2767 ID *id,
2768 ID *id_old)
2769{
2770 BlendDataReader reader = {fd};
2771 /* Sharing is only allowed within individual data-blocks currently. The clearing is done
2772 * explicitly here, in case the `reader` is used by multiple IDs in the future. */
2774
2775 /* Read part of datablock that is common between real and embedded datablocks. */
2776 direct_link_id_common(&reader, main->curlib, id, id_old, tag, id_read_tags);
2777
2779 /* For placeholder we only need to set the tag, no further data to read. */
2780 id->tag = tag;
2781 return true;
2782 }
2783
2784 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id);
2785 if (id_type->blend_read_data != nullptr) {
2786 id_type->blend_read_data(&reader, id);
2787 }
2788
2789 /* XXX Very weakly handled currently, see comment in read_libblock() before trying to
2790 * use it for anything new. */
2791 bool success = true;
2792
2793 switch (GS(id->name)) {
2794 case ID_SCR:
2795 success = BKE_screen_blend_read_data(&reader, (bScreen *)id);
2796 break;
2797 case ID_LI:
2798 direct_link_library(fd, (Library *)id, main);
2799 break;
2800 default:
2801 /* Do nothing. Handled by IDTypeInfo callback. */
2802 break;
2803 }
2804
2805 /* try to restore (when undoing) or clear ID's cache pointers. */
2806 if (id_type->foreach_cache != nullptr) {
2809 }
2810
2811 return success;
2812}
2813
2814/* Read all data associated with a datablock into datamap. */
2816 BHead *bhead,
2817 const char *allocname,
2818 const int id_type_index)
2819{
2820 bhead = blo_bhead_next(fd, bhead);
2821
2822 while (bhead && bhead->code == BLO_CODE_DATA) {
2823 void *data = read_struct(fd, bhead, allocname, id_type_index);
2824 if (data) {
2825 const bool is_new = oldnewmap_insert(fd->datamap, bhead->old, data, 0);
2826 if (!is_new) {
2827 CLOG_ERROR(&LOG,
2828 "Blendfile corruption: Invalid, or multiple `bhead` with same old address "
2829 "value (%p) for a given ID.",
2830 bhead->old);
2831 }
2832 }
2833
2834 bhead = blo_bhead_next(fd, bhead);
2835 }
2836
2837 return bhead;
2838}
2839
2840/* Verify if the datablock and all associated data is identical. */
2842{
2843 /* Test ID itself. */
2844 if (bhead->len && !BHEADN_FROM_BHEAD(bhead)->is_memchunk_identical) {
2845 return false;
2846 }
2847
2848 /* Test any other data that is part of ID (logic must match read_data_into_datamap). */
2849 bhead = blo_bhead_next(fd, bhead);
2850
2851 while (bhead && bhead->code == BLO_CODE_DATA) {
2852 if (bhead->len && !BHEADN_FROM_BHEAD(bhead)->is_memchunk_identical) {
2853 return false;
2854 }
2855
2856 bhead = blo_bhead_next(fd, bhead);
2857 }
2858
2859 return true;
2860}
2861
2862/* Re-use the whole 'noundo' local IDs by moving them from old to new main. Linked ones are handled
2863 * separately together with their libraries.
2864 *
2865 * NOTE: While in theory Library IDs (and their related linked IDs) are also 'noundo' data, in
2866 * practice they need to be handled separately, to ensure that their order in the new bmain list
2867 * matches the one from the read blend-file. Reading linked 'placeholder' entries in a memfile
2868 * relies on current library being the last item in the new main list. */
2870{
2871 Main *old_bmain = static_cast<Main *>(fd->old_mainlist->first);
2872
2873 BLI_assert(old_bmain->curlib == nullptr);
2875
2876 MainListsArray lbarray = BKE_main_lists_get(*old_bmain);
2877 int i = lbarray.size();
2878 while (i--) {
2879 if (BLI_listbase_is_empty(lbarray[i])) {
2880 continue;
2881 }
2882
2883 /* Only move 'noundo' local IDs. */
2884 ID *id = static_cast<ID *>(lbarray[i]->first);
2885 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id);
2886 if ((id_type->flags & IDTYPE_FLAGS_NO_MEMFILE_UNDO) == 0) {
2887 continue;
2888 }
2889
2890 Main *new_bmain = static_cast<Main *>(fd->mainlist->first);
2891 ListBase *new_lb = which_libbase(new_bmain, id_type->id_code);
2893 BLI_movelisttolist(new_lb, lbarray[i]);
2894
2895 /* Update mappings accordingly. */
2896 LISTBASE_FOREACH (ID *, id_iter, new_lb) {
2898 id_iter->tag |= ID_TAG_UNDO_OLD_ID_REUSED_NOUNDO;
2899 }
2900 }
2901}
2902
2904 FileData *fd, Main *new_main, Main *old_main, Main *libmain, BHead *bhead)
2905{
2906 Library *curlib = libmain->curlib;
2907
2908 BLI_remlink(fd->old_mainlist, libmain);
2909 BLI_remlink_safe(&old_main->libraries, libmain->curlib);
2910 BLI_addtail(fd->mainlist, libmain);
2911 BLI_addtail(&new_main->libraries, libmain->curlib);
2912
2915 if (bhead != nullptr) {
2916 oldnewmap_lib_insert(fd, bhead->old, &curlib->id, GS(curlib->id.name));
2917 }
2918
2919 ID *id_iter;
2920 FOREACH_MAIN_ID_BEGIN (libmain, id_iter) {
2922 }
2924}
2925
2926/* For undo, restore matching library datablock from the old main. */
2928 FileData *fd, Main *new_main, const ID *id, ID *id_old, BHead *bhead)
2929{
2930 /* In undo case, most libraries and linked data should be kept as is from previous state
2931 * (see BLO_read_from_memfile).
2932 * However, some needed by the snapshot being read may have been removed in previous one,
2933 * and would go missing.
2934 * This leads e.g. to disappearing objects in some undo/redo case, see #34446.
2935 * That means we have to carefully check whether current lib or
2936 * libdata already exits in old main, if it does we merely copy it over into new main area,
2937 * otherwise we have to do a full read of that bhead... */
2938 CLOG_INFO(&LOG_UNDO, 2, "UNDO: restore library %s", id->name);
2939
2940 if (id_old == nullptr) {
2941 CLOG_INFO(&LOG_UNDO, 2, " -> NO match");
2942 return false;
2943 }
2944
2945 Main *libmain = static_cast<Main *>(fd->old_mainlist->first);
2946 /* Skip `oldmain` itself. */
2947 for (libmain = libmain->next; libmain; libmain = libmain->next) {
2948 if (&libmain->curlib->id == id_old) {
2949 Main *old_main = static_cast<Main *>(fd->old_mainlist->first);
2951 2,
2952 " compare with %s -> match (existing libpath: %s)",
2953 libmain->curlib ? libmain->curlib->id.name : "<none>",
2954 libmain->curlib ? libmain->curlib->runtime->filepath_abs : "<none>");
2955 /* In case of a library, we need to re-add its main to fd->mainlist,
2956 * because if we have later a missing ID_LINK_PLACEHOLDER,
2957 * we need to get the correct lib it is linked to!
2958 * Order is crucial, we cannot bulk-add it in BLO_read_from_memfile()
2959 * like it used to be. */
2960 read_undo_move_libmain_data(fd, new_main, old_main, libmain, bhead);
2961 return true;
2962 }
2963 }
2964
2965 return false;
2966}
2967
2968static ID *library_id_is_yet_read(FileData *fd, Main *mainvar, BHead *bhead);
2969
2970/* For undo, restore existing linked datablock from the old main.
2971 *
2972 * Note that IDs from existing libs have already been moved into the new main when their (local)
2973 * ID_LI library ID was handled by #read_libblock_undo_restore_library, so this function has very
2974 * little to do. */
2976 FileData *fd, Main *libmain, const ID *id, ID **r_id_old, BHead *bhead)
2977{
2978 CLOG_INFO(&LOG_UNDO, 2, "UNDO: restore linked datablock %s", id->name);
2979
2980 if (*r_id_old == nullptr) {
2981 /* If the linked ID had to be re-read at some point, its session_uid may not be the same as
2982 * its reference stored in the memfile anymore. Do a search by name then. */
2983 *r_id_old = library_id_is_yet_read(fd, libmain, bhead);
2984
2985 if (*r_id_old == nullptr) {
2987 2,
2988 " from %s (%s): NOT found",
2989 libmain->curlib ? libmain->curlib->id.name : "<nullptr>",
2990 libmain->curlib ? libmain->curlib->filepath : "<nullptr>");
2991 return false;
2992 }
2993
2995 2,
2996 " from %s (%s): found by name",
2997 libmain->curlib ? libmain->curlib->id.name : "<nullptr>",
2998 libmain->curlib ? libmain->curlib->filepath : "<nullptr>");
2999 /* The Library ID 'owning' this linked ID should already have been moved to new main by a call
3000 * to #read_libblock_undo_restore_library. */
3001 BLI_assert(*r_id_old == static_cast<ID *>(BKE_main_idmap_lookup_uid(
3002 fd->new_idmap_uid, (*r_id_old)->session_uid)));
3003 }
3004 else {
3006 2,
3007 " from %s (%s): found by session_uid",
3008 libmain->curlib ? libmain->curlib->id.name : "<nullptr>",
3009 libmain->curlib ? libmain->curlib->filepath : "<nullptr>");
3010 /* The Library ID 'owning' this linked ID should already have been moved to new main by a call
3011 * to #read_libblock_undo_restore_library. */
3012 BLI_assert(*r_id_old ==
3013 static_cast<ID *>(BKE_main_idmap_lookup_uid(fd->new_idmap_uid, id->session_uid)));
3014 }
3015
3016 oldnewmap_lib_insert(fd, bhead->old, *r_id_old, GS((*r_id_old)->name));
3017
3018 /* No need to do anything else for ID_LINK_PLACEHOLDER, it's assumed
3019 * already present in its lib's main. */
3020 return true;
3021}
3022
3023/* For undo, restore unchanged local datablock from old main. */
3025 FileData *fd, Main *main, const ID * /*id*/, ID *id_old, BHead *bhead, const int id_tag)
3026{
3028 BLI_assert(id_old != nullptr);
3029
3030 /* Do not add ID_TAG_NEW here, this should not be needed/used in undo case anyway (as
3031 * this is only for do_version-like code), but for sake of consistency, and also because
3032 * it will tell us which ID is re-used from old Main, and which one is actually newly read. */
3033 /* Also do not set #ID_Readfile_Data::Tags.needs_linking, this ID will never be re-liblinked,
3034 * hence that tag will never be cleared, leading to critical issue in link/append code. */
3035 /* Some tags need to be preserved here. */
3037 (id_old->tag & ID_TAG_KEEP_ON_UNDO);
3038 id_old->lib = main->curlib;
3039 id_old->us = ID_FAKE_USERS(id_old);
3040 /* Do not reset id->icon_id here, memory allocated for it remains valid. */
3041 /* Needed because .blend may have been saved with crap value here... */
3042 id_old->newid = nullptr;
3043 id_old->orig_id = nullptr;
3044
3045 const short idcode = GS(id_old->name);
3046 Main *old_bmain = static_cast<Main *>(fd->old_mainlist->first);
3047 ListBase *old_lb = which_libbase(old_bmain, idcode);
3048 ListBase *new_lb = which_libbase(main, idcode);
3049 BLI_remlink(old_lb, id_old);
3050 BLI_addtail(new_lb, id_old);
3051
3052 /* Recalc flags, mostly these just remain as they are. */
3054 id_old->recalc_after_undo_push = 0;
3055
3056 /* Insert into library map for lookup by newly read datablocks (with pointer value bhead->old).
3057 * Note that existing datablocks in memory (which pointer value would be id_old) are not
3058 * remapped, so no need to store this info here. */
3059 oldnewmap_lib_insert(fd, bhead->old, id_old, bhead->code);
3060
3062
3063 if (GS(id_old->name) == ID_OB) {
3064 Object *ob = (Object *)id_old;
3065 /* For undo we stay in object mode during undo presses, so keep editmode disabled for re-used
3066 * data-blocks too. */
3067 ob->mode &= ~OB_MODE_EDIT;
3068 }
3069}
3070
3071/* For undo, store changed datablock at old address. */
3073{
3074 /* During memfile undo, if an ID changed and we cannot directly re-use existing one from old
3075 * bmain, we do a full read of the new id from the memfile, and then fully swap its content
3076 * with the old id. This allows us to keep the same pointer even for modified data, which
3077 * helps reducing further detected changes by the depsgraph (since unchanged IDs remain fully
3078 * unchanged, even if they are using/pointing to a changed one). */
3080 BLI_assert(id_old != nullptr);
3081
3082 const short idcode = GS(id->name);
3083
3084 Main *old_bmain = static_cast<Main *>(fd->old_mainlist->first);
3085 ListBase *old_lb = which_libbase(old_bmain, idcode);
3086 ListBase *new_lb = which_libbase(main, idcode);
3087 BLI_remlink(old_lb, id_old);
3088 BLI_remlink(new_lb, id);
3089
3090 /* We do need remapping of internal pointers to the ID itself here.
3091 *
3092 * Passing a null #Main means that not all potential runtime data (like collections' parent
3093 * pointers etc.) will be up-to-date. However, this should not be a problem here, since these
3094 * data are re-generated later in file-read process anyway. */
3095 BKE_lib_id_swap_full(nullptr,
3096 id,
3097 id_old,
3098 true,
3102
3103 /* Special temporary usage of this pointer, necessary for the `undo_preserve` call after
3104 * lib-linking to restore some data that should never be affected by undo, e.g. the 3D cursor of
3105 * #Scene. */
3106 id_old->orig_id = id;
3109
3110 BLI_addtail(new_lb, id_old);
3111 BLI_addtail(old_lb, id);
3112}
3113
3115 FileData *fd, Main *main, BHead *bhead, const int id_tag, ID **r_id_old)
3116{
3117 BLI_assert(fd->old_idmap_uid != nullptr);
3118
3119 /* Get pointer to memory of new ID that we will be reading. */
3120 const ID *id = static_cast<const ID *>(peek_struct_undo(fd, bhead));
3121 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id);
3122
3123 const bool do_partial_undo = (fd->skip_flags & BLO_READ_SKIP_UNDO_OLD_MAIN) == 0;
3124#ifndef NDEBUG
3125 if (do_partial_undo && (bhead->code != ID_LINK_PLACEHOLDER)) {
3126 /* This code should only ever be reached for local data-blocks. */
3127 BLI_assert(main->curlib == nullptr);
3128 }
3129#endif
3130
3131 /* Find the 'current' existing ID we want to reuse instead of the one we
3132 * would read from the undo memfile. */
3133 ID *id_old = (fd->old_idmap_uid != nullptr) ?
3135 nullptr;
3136
3137 if (bhead->code == ID_LI) {
3138 /* Restore library datablock, if possible. */
3139 if (read_libblock_undo_restore_library(fd, main, id, id_old, bhead)) {
3140 return true;
3141 }
3142 }
3143 else if (bhead->code == ID_LINK_PLACEHOLDER) {
3144 /* Restore linked datablock. */
3145 if (read_libblock_undo_restore_linked(fd, main, id, &id_old, bhead)) {
3146 return true;
3147 }
3148 }
3149 else if (id_type->flags & IDTYPE_FLAGS_NO_MEMFILE_UNDO) {
3150 CLOG_INFO(
3151 &LOG_UNDO, 2, "UNDO: skip restore datablock %s, 'NO_MEMFILE_UNDO' type of ID", id->name);
3152
3153 /* If that local noundo ID still exists currently, the call to
3154 * #read_undo_reuse_noundo_local_ids at the beginning of #blo_read_file_internal will already
3155 * have moved it into the new main, and populated accordingly the new_idmap_uid.
3156 *
3157 * If this is the case, it can also be remapped for newly read data. Otherwise, this is 'lost'
3158 * data that cannot be restored on undo, so no remapping should exist for it in the ID
3159 * oldnewmap. */
3160 if (id_old) {
3161 BLI_assert(id_old ==
3162 static_cast<ID *>(BKE_main_idmap_lookup_uid(fd->new_idmap_uid, id->session_uid)));
3163 oldnewmap_lib_insert(fd, bhead->old, id_old, bhead->code);
3164 }
3165 return true;
3166 }
3167
3168 if (!do_partial_undo) {
3170 2,
3171 "UNDO: read %s (uid %u) -> no partial undo, always read at new address",
3172 id->name,
3173 id->session_uid);
3174 return false;
3175 }
3176
3177 /* Restore local datablocks. */
3178 if (id_old != nullptr && read_libblock_is_identical(fd, bhead)) {
3179 /* Local datablock was unchanged, restore from the old main. */
3181 2,
3182 "UNDO: read %s (uid %u) -> keep identical datablock",
3183 id->name,
3184 id->session_uid);
3185
3186 read_libblock_undo_restore_identical(fd, main, id, id_old, bhead, id_tag);
3187
3188 *r_id_old = id_old;
3189 return true;
3190 }
3191 if (id_old != nullptr) {
3192 /* Local datablock was changed. Restore at the address of the old datablock. */
3194 2,
3195 "UNDO: read %s (uid %u) -> read to old existing address",
3196 id->name,
3197 id->session_uid);
3198 *r_id_old = id_old;
3199 return false;
3200 }
3201
3202 /* Local datablock does not exist in the undo step, so read from scratch. */
3203 CLOG_INFO(
3204 &LOG_UNDO, 2, "UNDO: read %s (uid %u) -> read at new address", id->name, id->session_uid);
3205 return false;
3206}
3207
3208/* This routine reads a datablock and its direct data, and advances bhead to
3209 * the next datablock. For library linked datablocks, only a placeholder will
3210 * be generated, to be replaced in read_library_linked_ids.
3211 *
3212 * When reading for undo, libraries, linked datablocks and unchanged datablocks
3213 * will be restored from the old database. Only new or changed datablocks will
3214 * actually be read. */
3216 Main *main,
3217 BHead *bhead,
3218 int id_tag,
3219 ID_Readfile_Data::Tags id_read_tags,
3220 const bool placeholder_set_indirect_extern,
3221 ID **r_id)
3222{
3223 const bool do_partial_undo = (fd->skip_flags & BLO_READ_SKIP_UNDO_OLD_MAIN) == 0;
3224
3225 /* First attempt to restore existing datablocks for undo.
3226 * When datablocks are changed but still exist, we restore them at the old
3227 * address and inherit recalc flags for the dependency graph. */
3228 ID *id_old = nullptr;
3229 if (fd->flags & FD_FLAGS_IS_MEMFILE) {
3230 if (read_libblock_undo_restore(fd, main, bhead, id_tag, &id_old)) {
3231 if (r_id) {
3232 *r_id = id_old;
3233 }
3234 if (main->id_map != nullptr && id_old != nullptr) {
3235 BKE_main_idmap_insert_id(main->id_map, id_old);
3236 }
3237
3238 return blo_bhead_next(fd, bhead);
3239 }
3240 }
3241
3242 /* Read libblock struct. */
3243 const int id_type_index = BKE_idtype_idcode_to_index(bhead->code);
3244#ifndef NDEBUG
3245 const char *blockname = nullptr;
3246#else
3247 /* Avoid looking up in the mapping for all read BHead, since this only contains the ID type name
3248 * in release builds. */
3249 const char *blockname = get_alloc_name(fd, bhead, nullptr, id_type_index);
3250#endif
3251 ID *id = read_id_struct(fd, bhead, blockname, id_type_index);
3252 if (id == nullptr) {
3253 if (r_id) {
3254 *r_id = nullptr;
3255 }
3256 return blo_bhead_next(fd, bhead);
3257 }
3258
3259 /* Determine ID type and add to main database list. */
3260 const short idcode = GS(id->name);
3261 ListBase *lb = which_libbase(main, idcode);
3262 if (lb == nullptr) {
3263 /* Unknown ID type. */
3264 CLOG_WARN(&LOG, "Unknown id code '%c%c'", (idcode & 0xff), (idcode >> 8));
3265 MEM_freeN(id);
3266 if (r_id) {
3267 *r_id = nullptr;
3268 }
3269 return blo_bhead_next(fd, bhead);
3270 }
3271
3272 /* NOTE: id must be added to the list before direct_link_id(), since
3273 * direct_link_library() may remove it from there in case of duplicates. */
3274 BLI_addtail(lb, id);
3275
3276 /* Insert into library map for lookup by newly read datablocks (with pointer value bhead->old).
3277 * Note that existing datablocks in memory (which pointer value would be id_old) are not remapped
3278 * remapped anymore, so no need to store this info here. */
3279 ID *id_target = (do_partial_undo && id_old != nullptr) ? id_old : id;
3280 oldnewmap_lib_insert(fd, bhead->old, id_target, bhead->code);
3281
3282 if (r_id) {
3283 *r_id = id_target;
3284 }
3285
3286 /* Set tag for new datablock to indicate lib linking and versioning needs
3287 * to be done still. */
3288 id_tag |= ID_TAG_NEW;
3289 id_read_tags.needs_linking = true;
3290
3291 if (bhead->code == ID_LINK_PLACEHOLDER) {
3292 /* Read placeholder for linked datablock. */
3293 id_read_tags.is_link_placeholder = true;
3294
3295 if (placeholder_set_indirect_extern) {
3296 if (id->flag & ID_FLAG_INDIRECT_WEAK_LINK) {
3297 id_tag |= ID_TAG_INDIRECT;
3298 }
3299 else {
3300 id_tag |= ID_TAG_EXTERN;
3301 }
3302 }
3303
3304 direct_link_id(fd, main, id_tag, id_read_tags, id, id_old);
3305
3306 if (main->id_map != nullptr) {
3307 BKE_main_idmap_insert_id(main->id_map, id);
3308 }
3309
3310 return blo_bhead_next(fd, bhead);
3311 }
3312
3313 /* Read datablock contents.
3314 * Use convenient malloc name for debugging and better memory link prints. */
3315 const void *id_address_old = bhead->old;
3316 bhead = read_data_into_datamap(fd, bhead, blockname, id_type_index);
3317 const bool success = direct_link_id(fd, main, id_tag, id_read_tags, id, id_old);
3319
3320 if (!success) {
3321 /* Only happens with corrupt files. Remove the ID from the library mapping,
3322 * so pointers to it resolve to null instead of the freed ID. */
3323 const NewAddress *entry = fd->libmap->map.lookup_ptr(id_address_old);
3324 if (entry && entry->newp == id) {
3325 fd->libmap->map.remove(id_address_old);
3326 }
3327
3328 /* Free without user refcounting, as ID pointers still have their old address. */
3329 BLI_remlink(lb, id);
3330 BKE_id_free_ex(nullptr,
3331 id,
3334 false);
3335 fd->flags &= ~FD_FLAGS_FILE_OK;
3336 if (Main *bmain = filedata_main(fd)) {
3337 blo_readfile_invalidate(fd, bmain, "Corrupt .blend file, failed to read a data-block");
3338 }
3339 if (r_id != nullptr) {
3340 *r_id = nullptr;
3341 }
3342 }
3343 else {
3344 if (do_partial_undo && id_old != nullptr) {
3345 /* For undo, store contents read into id at id_old. */
3347 }
3348 if (fd->new_idmap_uid != nullptr) {
3350 }
3351 if (main->id_map != nullptr) {
3352 BKE_main_idmap_insert_id(main->id_map, id_target);
3353 }
3354 }
3355
3356 return bhead;
3357}
3358
3361/* -------------------------------------------------------------------- */
3366{
3368
3369 bhead = read_data_into_datamap(fd, bhead, "Data for Asset meta-data", INDEX_ID_NULL);
3370
3371 BlendDataReader reader = {fd};
3372 BLO_read_struct(&reader, AssetMetaData, r_asset_data);
3373 if (*r_asset_data) {
3374 BKE_asset_metadata_read(&reader, *r_asset_data);
3375 }
3376
3378
3379 return bhead;
3380}
3381
3384/* -------------------------------------------------------------------- */
3396{
3398 fd, bhead, "Data from Global block", INDEX_ID_NULL, sizeof(FileGlobal)));
3399
3400 if (fg == nullptr) [[unlikely]] {
3401 blo_readfile_invalidate(fd, bfd->main, "Corrupt .blend file, missing global block");
3402 return nullptr;
3403 }
3404
3405 /* NOTE: `bfd->main->versionfile` is supposed to have already been set from `fd->fileversion`
3406 * beforehand by calling code. */
3407 bfd->main->subversionfile = fg->subversion;
3410
3411 bfd->main->minversionfile = fg->minversion;
3413
3415 STRNCPY(bfd->main->build_hash, fg->build_hash);
3417
3418 bfd->fileflags = fg->fileflags;
3419 bfd->globalf = fg->globalf;
3420
3421 /* NOTE: since 88b24bc6bb, `fg->filepath` is only written for crash recovery and autosave files,
3422 * so only overwrite `fd->relabase` if it is not empty, in case a regular blendfile is opened
3423 * through one of the 'recover' operators.
3424 *
3425 * In all other cases, the path is just set to the current path of the blendfile being read, so
3426 * there is no need to handle anymore older files (pre-2.65) that did not store (correctly) their
3427 * path. */
3428 if (G.fileflags & G_FILE_RECOVER_READ) {
3429 if (fg->filepath[0] != '\0') {
3430 STRNCPY(fd->relabase, fg->filepath);
3431 /* Used to set expected original filepath in read Main, instead of the path of the recovery
3432 * file itself. */
3433 STRNCPY(bfd->filepath, fg->filepath);
3434 }
3435 }
3436
3437 bfd->curscreen = fg->curscreen;
3438 bfd->curscene = fg->curscene;
3439 bfd->cur_view_layer = fg->cur_view_layer;
3440
3441 MEM_freeN(fg);
3442
3443 fd->globalf = bfd->globalf;
3444 fd->fileflags = bfd->fileflags;
3445
3446 return blo_bhead_next(fd, bhead);
3447}
3448
3449/* NOTE: this has to be kept for reading older files... */
3450static void link_global(FileData *fd, BlendFileData *bfd)
3451{
3452 bfd->cur_view_layer = static_cast<ViewLayer *>(
3454 bfd->curscreen = static_cast<bScreen *>(newlibadr(fd, nullptr, false, bfd->curscreen));
3455 bfd->curscene = static_cast<Scene *>(newlibadr(fd, nullptr, false, bfd->curscene));
3456 /* this happens in files older than 2.35 */
3457 if (bfd->curscene == nullptr) {
3458 if (bfd->curscreen) {
3459 bfd->curscene = bfd->curscreen->scene;
3460 }
3461 }
3462 if (bfd->curscene == nullptr) {
3463 bfd->curscene = static_cast<Scene *>(bfd->main->scenes.first);
3464 }
3465}
3466
3469/* -------------------------------------------------------------------- */
3473static void do_versions_userdef(FileData * /*fd*/, BlendFileData *bfd)
3474{
3475 UserDef *user = bfd->user;
3476
3477 if (user == nullptr) {
3478 return;
3479 }
3480
3482}
3483
3485{
3486 /* WATCH IT!!!: pointers from libdata have not been converted */
3487
3488 /* Don't allow versioning to create new data-blocks. */
3489 main->is_locked_for_linking = true;
3490
3491 /* Code ensuring conversion from new 'system IDProperties' in 5.0. This needs to run before any
3492 * other data versioning. Otherwise, things like Cycles versioning code cannot work as expected.
3493 *
3494 * Merge (with overwrite) future system properties storage into current IDProperties. */
3496
3497 if (G.debug & G_DEBUG) {
3498 char build_commit_datetime[32];
3499 time_t temp_time = main->build_commit_timestamp;
3500 tm *tm = (temp_time) ? gmtime(&temp_time) : nullptr;
3501 if (LIKELY(tm)) {
3502 strftime(build_commit_datetime, sizeof(build_commit_datetime), "%Y-%m-%d %H:%M", tm);
3503 }
3504 else {
3505 STRNCPY(build_commit_datetime, "unknown");
3506 }
3507
3508 CLOG_INFO(&LOG, 0, "Read file %s", fd->relabase);
3509 CLOG_INFO(&LOG,
3510 0,
3511 " Version %d sub %d date %s hash %s",
3512 main->versionfile,
3513 main->subversionfile,
3514 build_commit_datetime,
3515 main->build_hash);
3516 }
3517
3518 if (!main->is_read_invalid) {
3520 }
3521 if (!main->is_read_invalid) {
3523 }
3524 if (!main->is_read_invalid) {
3526 }
3527 if (!main->is_read_invalid) {
3529 }
3530 if (!main->is_read_invalid) {
3532 }
3533 if (!main->is_read_invalid) {
3535 }
3536 if (!main->is_read_invalid) {
3538 }
3539 if (!main->is_read_invalid) {
3541 }
3542 if (!main->is_read_invalid) {
3544 }
3545 if (!main->is_read_invalid) {
3547 }
3548 if (!main->is_read_invalid) {
3550 }
3551 if (!main->is_read_invalid) {
3553 }
3554 if (!main->is_read_invalid) {
3556 }
3557
3558 /* WATCH IT!!!: pointers from libdata have not been converted yet here! */
3559 /* WATCH IT 2!: #UserDef struct init see #do_versions_userdef() above! */
3560
3561 /* don't forget to set version number in BKE_blender_version.h! */
3562
3563 main->is_locked_for_linking = false;
3564}
3565
3567{
3568 BLI_assert(fd != nullptr);
3569
3570 CLOG_INFO(&LOG,
3571 2,
3572 "Processing %s (%s), %d.%d",
3573 main->curlib ? main->curlib->filepath : main->filepath,
3574 main->curlib ? "LIB" : "MAIN",
3575 main->versionfile,
3576 main->subversionfile);
3577
3578 /* Don't allow versioning to create new data-blocks. */
3579 main->is_locked_for_linking = true;
3580
3581 if (!main->is_read_invalid) {
3583 }
3584 if (!main->is_read_invalid) {
3586 }
3587 if (!main->is_read_invalid) {
3589 }
3590 if (!main->is_read_invalid) {
3592 }
3593 if (!main->is_read_invalid) {
3595 }
3596 if (!main->is_read_invalid) {
3598 }
3599 if (!main->is_read_invalid) {
3601 }
3602 if (!main->is_read_invalid) {
3604 }
3605 if (!main->is_read_invalid) {
3607 }
3608 if (!main->is_read_invalid) {
3610 }
3611 if (!main->is_read_invalid) {
3613 }
3614 if (!main->is_read_invalid) {
3616 }
3617
3618 main->is_locked_for_linking = false;
3619}
3620
3623/* -------------------------------------------------------------------- */
3628{
3629 /* Embedded IDs are not known by lib_link code, so they would be remapped to `nullptr`. But there
3630 * is no need to process them anyway, as they are already handled during the 'read_data' phase.
3631 *
3632 * NOTE: Some external non-owning pointers to embedded IDs (like the node-tree pointers of the
3633 * Node editor) will not be detected as embedded ones though at 'lib_link' stage (because their
3634 * source data cannot be accessed). This is handled on a case-by-case basis in 'after_lib_link'
3635 * validation code. */
3637 return IDWALK_RET_NOP;
3638 }
3639
3640 /* Explicitly requested to be ignored during readfile processing. Means the read_data code
3641 * already handled this pointer. Typically, the 'owner_id' pointer of an embedded ID. */
3642 if (cb_data->cb_flag & IDWALK_CB_READFILE_IGNORE) {
3643 return IDWALK_RET_NOP;
3644 }
3645
3646 BlendLibReader *reader = static_cast<BlendLibReader *>(cb_data->user_data);
3647 ID **id_ptr = cb_data->id_pointer;
3648 ID *owner_id = cb_data->owner_id;
3649
3650 *id_ptr = BLO_read_get_new_id_address(reader, owner_id, ID_IS_LINKED(owner_id), *id_ptr);
3651
3652 return IDWALK_RET_NOP;
3653}
3654
3655static void lib_link_all(FileData *fd, Main *bmain)
3656{
3657 BlendLibReader reader = {fd, bmain};
3658
3659 ID *id;
3660 FOREACH_MAIN_ID_BEGIN (bmain, id) {
3661 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id);
3662
3664 {
3666 /* This ID has been re-used from 'old' bmain. Since it was therefore unchanged across
3667 * current undo step, and old IDs re-use their old memory address, we do not need to liblink
3668 * it at all. */
3670
3671 /* Some data that should be persistent, like the 3DCursor or the tool settings, are
3672 * stored in IDs affected by undo, like Scene. So this requires some specific handling. */
3673 /* NOTE: even though the ID may have been detected as unchanged, the 'undo_preserve' may have
3674 * to actually change some of its ID pointers, it's e.g. the case with Scene's tool-settings
3675 * Brush/Palette pointers. This is the case where both new and old ID may be the same. */
3676 if (id_type->blend_read_undo_preserve != nullptr) {
3678 id_type->blend_read_undo_preserve(&reader, id, id->orig_id ? id->orig_id : id);
3679 }
3680 continue;
3681 }
3682
3684 /* Not all original pointer values can be considered as valid.
3685 * Handling of DNA deprecated data should never be needed in undo case. */
3687 ((fd->flags & FD_FLAGS_IS_MEMFILE) ?
3688 IDWALK_NOP :
3690 BKE_library_foreach_ID_link(bmain, id, lib_link_cb, &reader, flag);
3691
3692 after_liblink_id_process(&reader, id);
3693
3695 }
3696
3697 /* Some data that should be persistent, like the 3DCursor or the tool settings, are
3698 * stored in IDs affected by undo, like Scene. So this requires some specific handling. */
3699 if (id_type->blend_read_undo_preserve != nullptr && id->orig_id != nullptr) {
3701 id_type->blend_read_undo_preserve(&reader, id, id->orig_id);
3702 }
3703 }
3705
3706 /* Cleanup `ID.orig_id`, this is now reserved for depsgraph/copy-on-eval usage only. */
3707 FOREACH_MAIN_ID_BEGIN (bmain, id) {
3708 id->orig_id = nullptr;
3709 }
3711
3712#ifndef NDEBUG
3713 /* Double check we do not have any 'need link' tag remaining, this should never be the case once
3714 * this function has run. */
3715 FOREACH_MAIN_ID_BEGIN (bmain, id) {
3717 }
3719#endif
3720}
3721
3728{
3729 /* We only expect a merged Main here, not a split one. */
3730 BLI_assert((bmain->prev == nullptr) && (bmain->next == nullptr));
3731
3732 if (!BKE_main_namemap_validate_and_fix(*bmain)) {
3733 BKE_report(
3734 reports ? reports->reports : nullptr,
3735 RPT_ERROR,
3736 "Critical blend-file corruption: Conflicts and/or otherwise invalid data-blocks names "
3737 "(see console for details)");
3738 }
3739
3740 /* Check for possible cycles in scenes' 'set' background property. */
3742
3743 /* We could integrate that to mesh/curve/lattice lib_link, but this is really cheap process,
3744 * so simpler to just use it directly in this single call. */
3745 BLO_main_validate_shapekeys(bmain, reports ? reports->reports : nullptr);
3746
3747 BLO_main_validate_embedded_flag(bmain, reports ? reports->reports : nullptr);
3748 BLO_main_validate_embedded_liboverrides(bmain, reports ? reports->reports : nullptr);
3749
3750 /* We have to rebuild that runtime information *after* all data-blocks have been properly linked.
3751 */
3753}
3754
3757/* -------------------------------------------------------------------- */
3762{
3763 BLO_read_struct(reader, IDProperty, &kmi->properties);
3764 IDP_BlendDataRead(reader, &kmi->properties);
3765 kmi->ptr = nullptr;
3766 kmi->flag &= ~KMI_UPDATE;
3767}
3768
3769static size_t user_menu_item_struct_size(const int type)
3770{
3771 switch (type) {
3772 case USER_MENU_TYPE_SEP:
3773 return sizeof(bUserMenuItem);
3775 return sizeof(bUserMenuItem_Op);
3777 return sizeof(bUserMenuItem_Menu);
3779 return sizeof(bUserMenuItem_Prop);
3780 }
3781 return sizeof(bUserMenuItem);
3782}
3783
3790{
3791 UserDef *user;
3792 bfd->user = user = static_cast<UserDef *>(read_struct_and_validate_size(
3793 fd, bhead, "Data for User Def", INDEX_ID_NULL, sizeof(UserDef)));
3794
3795 if (user == nullptr) [[unlikely]] {
3796 blo_readfile_invalidate(fd, bfd->main, "Corrupt .blend file, missing user-preferences block");
3797 return nullptr;
3798 }
3799
3800 /* User struct has separate do-version handling */
3801 user->versionfile = bfd->main->versionfile;
3802 user->subversionfile = bfd->main->subversionfile;
3803
3804 /* read all data into fd->datamap */
3805 bhead = read_data_into_datamap(fd, bhead, "Data for User Def", INDEX_ID_NULL);
3806
3807 BlendDataReader reader_ = {fd};
3808 BlendDataReader *reader = &reader_;
3809
3810 BLO_read_struct_list(reader, bTheme, &user->themes);
3813 BLO_read_struct_list(reader, bUserMenu, &user->user_menus);
3814 BLO_read_struct_list(reader, bAddon, &user->addons);
3820
3821 LISTBASE_FOREACH (wmKeyMap *, keymap, &user->user_keymaps) {
3822 keymap->modal_items = nullptr;
3823 keymap->poll = nullptr;
3824 keymap->flag &= ~KEYMAP_UPDATE;
3825
3826 BLO_read_struct_list(reader, wmKeyMapDiffItem, &keymap->diff_items);
3827 BLO_read_struct_list(reader, wmKeyMapItem, &keymap->items);
3828
3829 LISTBASE_FOREACH (wmKeyMapDiffItem *, kmdi, &keymap->diff_items) {
3830 BLO_read_struct(reader, wmKeyMapItem, &kmdi->remove_item);
3831 BLO_read_struct(reader, wmKeyMapItem, &kmdi->add_item);
3832
3833 if (kmdi->remove_item) {
3834 direct_link_keymapitem(reader, kmdi->remove_item);
3835 }
3836 if (kmdi->add_item) {
3837 direct_link_keymapitem(reader, kmdi->add_item);
3838 }
3839 }
3840
3841 LISTBASE_FOREACH (wmKeyMapItem *, kmi, &keymap->items) {
3842 direct_link_keymapitem(reader, kmi);
3843 }
3844 }
3845
3847 BLO_read_struct(reader, IDProperty, &kpt->prop);
3848 IDP_BlendDataRead(reader, &kpt->prop);
3849 }
3850
3851 LISTBASE_FOREACH (bUserMenu *, um, &user->user_menus) {
3853 reader, &um->items, [](const bUserMenuItem &umi) {
3854 return user_menu_item_struct_size(umi.type);
3855 });
3856 LISTBASE_FOREACH (bUserMenuItem *, umi, &um->items) {
3857 if (umi->type == USER_MENU_TYPE_OPERATOR) {
3858 bUserMenuItem_Op *umi_op = (bUserMenuItem_Op *)umi;
3859 BLO_read_struct(reader, IDProperty, &umi_op->prop);
3860 IDP_BlendDataRead(reader, &umi_op->prop);
3861 }
3862 }
3863 }
3864
3865 LISTBASE_FOREACH (bAddon *, addon, &user->addons) {
3866 BLO_read_struct(reader, IDProperty, &addon->prop);
3867 IDP_BlendDataRead(reader, &addon->prop);
3868 }
3869
3872 }
3873
3875 BKE_asset_catalog_path_list_blend_read_data(reader, shelf_settings->enabled_catalog_paths);
3876 }
3877
3878 /* XXX */
3879 user->uifonts.first = user->uifonts.last = nullptr;
3880
3881 BLO_read_struct_list(reader, uiStyle, &user->uistyles);
3882
3883 /* Don't read the active app template, use the default one. */
3884 user->app_template[0] = '\0';
3885
3886 /* Clear runtime data. */
3887 user->runtime.is_dirty = false;
3888 user->edit_studio_light = 0;
3889
3890 /* free fd->datamap again */
3892
3893 return bhead;
3894}
3895
3898/* -------------------------------------------------------------------- */
3903{
3905 return IDWALK_RET_NOP;
3906 }
3907
3908 IDNameLib_Map *new_idmap_uid = static_cast<IDNameLib_Map *>(cb_data->user_data);
3909 ID **id_pointer = cb_data->id_pointer;
3910 if (*id_pointer != nullptr) {
3911 *id_pointer = BKE_main_idmap_lookup_uid(new_idmap_uid, (*id_pointer)->session_uid);
3912 }
3913
3914 return IDWALK_RET_NOP;
3915}
3916
3917/* Remap 'no undo' ID usages to matching IDs in new main.
3918 *
3919 * 'no undo' IDs have simply be moved from old to new main so far. However, unlike the other
3920 * re-used IDs (the 'unchanged' ones), there is no guarantee that all the ID pointers they use are
3921 * still valid.
3922 *
3923 * This code performs a remapping based on the session_uid. */
3925{
3926 Main *new_bmain = static_cast<Main *>(fd->mainlist->first);
3927 ID *id_iter;
3928 FOREACH_MAIN_ID_BEGIN (new_bmain, id_iter) {
3929 if (ID_IS_LINKED(id_iter)) {
3930 continue;
3931 }
3932 if ((id_iter->tag & ID_TAG_UNDO_OLD_ID_REUSED_NOUNDO) == 0) {
3933 continue;
3934 }
3935
3938 }
3940}
3941
3946static void blo_read_file_checks(Main *bmain)
3947{
3948#ifndef NDEBUG
3949 BLI_assert(bmain->next == nullptr);
3950 BLI_assert(!bmain->is_read_invalid);
3951
3952 LISTBASE_FOREACH (wmWindowManager *, wm, &bmain->wm) {
3953 LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
3954 /* This pointer is deprecated and should always be nullptr. */
3955 BLI_assert(win->screen == nullptr);
3956 }
3957 }
3958#endif
3959 UNUSED_VARS_NDEBUG(bmain);
3960}
3961
3963{
3964 BHead *bhead = blo_bhead_first(fd);
3965 BlendFileData *bfd;
3966 ListBase mainlist = {nullptr, nullptr};
3967
3968 const bool is_undo = (fd->flags & FD_FLAGS_IS_MEMFILE) != 0;
3969 if (is_undo) {
3970 CLOG_INFO(&LOG_UNDO, 2, "UNDO: read step");
3971 }
3972
3973 /* Prevent any run of layer collections rebuild during readfile process, and the do_versions
3974 * calls.
3975 *
3976 * NOTE: Typically readfile code should not trigger such updates anyway. But some calls to
3977 * non-BLO functions (e.g. ID deletion) can indirectly trigger it. */
3979
3980 bfd = MEM_new<BlendFileData>(__func__);
3981
3982 bfd->main = BKE_main_new();
3983 bfd->main->versionfile = fd->fileversion;
3984 STRNCPY(bfd->filepath, filepath);
3985
3986 /* Placeholders of libraries read before a failure can't be freed as regular IDs. */
3987 BLI_SCOPED_DEFER([&]() {
3988 if (bfd->main->is_read_invalid && mainlist.first != mainlist.last) {
3989 read_libraries_free_placeholders(fd, &mainlist);
3990 }
3991 });
3992
3993 bfd->type = BLENFILETYPE_BLEND;
3994
3995 if ((fd->skip_flags & BLO_READ_SKIP_DATA) == 0) {
3996 BLI_addtail(&mainlist, bfd->main);
3997 fd->mainlist = &mainlist;
3998 STRNCPY(bfd->main->filepath, filepath);
3999 }
4000
4001 if (G.background) {
4002 /* We only read & store .blend thumbnail in background mode
4003 * (because we cannot re-generate it, no OpenGL available).
4004 */
4005 const int *data = read_file_thumbnail(fd);
4006
4007 if (data) {
4008 const int width = data[0];
4009 const int height = data[1];
4010 if (BLEN_THUMB_MEMSIZE_IS_VALID(width, height)) {
4011 const size_t data_size = BLEN_THUMB_MEMSIZE(width, height);
4012 bfd->main->blen_thumb = static_cast<BlendThumbnail *>(MEM_mallocN(data_size, __func__));
4013
4014 BLI_assert((data_size - sizeof(*bfd->main->blen_thumb)) ==
4015 (BLEN_THUMB_MEMSIZE_FILE(width, height) - (sizeof(*data) * 2)));
4016 bfd->main->blen_thumb->width = width;
4017 bfd->main->blen_thumb->height = height;
4018 memcpy(bfd->main->blen_thumb->rect, &data[2], data_size - sizeof(*bfd->main->blen_thumb));
4019 }
4020 }
4021 }
4022
4023 if (is_undo) {
4024 /* This idmap will store UIDs of all IDs ending up in the new main, whether they are newly
4025 * read, or re-used from the old main. */
4027 static_cast<Main *>(fd->mainlist->first), false, nullptr, MAIN_IDMAP_TYPE_UID);
4028
4029 /* Copy all 'no undo' local data from old to new bmain. */
4031 }
4032
4033 while (bhead) {
4034 switch (bhead->code) {
4035 case BLO_CODE_DATA:
4036 case BLO_CODE_DNA1:
4037 case BLO_CODE_TEST: /* used as preview since 2.5x */
4038 case BLO_CODE_REND:
4039 bhead = blo_bhead_next(fd, bhead);
4040 break;
4041 case BLO_CODE_GLOB:
4042 bhead = read_global(bfd, fd, bhead);
4043 break;
4044 case BLO_CODE_USER:
4046 bhead = blo_bhead_next(fd, bhead);
4047 }
4048 else {
4049 bhead = read_userdef(bfd, fd, bhead);
4050 }
4051 break;
4052 case BLO_CODE_ENDB:
4053 bhead = nullptr;
4054 break;
4055
4057 if (fd->skip_flags & BLO_READ_SKIP_DATA) {
4058 bhead = blo_bhead_next(fd, bhead);
4059 }
4060 else {
4061 /* Add link placeholder to the main of the library it belongs to.
4062 * The library is the most recently loaded ID_LI block, according
4063 * to the file format definition. So we can use the entry at the
4064 * end of mainlist, added in direct_link_library. */
4065 Main *libmain = static_cast<Main *>(mainlist.last);
4066
4067 /* Corrupt file without library before the placeholder, which would otherwise be
4068 * read as a local ID. */
4069 if (libmain->curlib == nullptr) [[unlikely]] {
4070 blo_readfile_invalidate(fd, bfd->main, "Corrupt .blend file, missing library");
4071 bhead = blo_bhead_next(fd, bhead);
4072 break;
4073 }
4074 bhead = read_libblock(fd, libmain, bhead, 0, {}, true, nullptr);
4075 }
4076 break;
4077 /* in 2.50+ files, the file identifier for screens is patched, forward compatibility */
4078 case ID_SCRN:
4079 bhead->code = ID_SCR;
4080 /* pass on to default */
4082 default: {
4083 if (blo_bhead_is_id_valid_type(bhead)) {
4084 /* BHead is a valid known ID type one, read the whole ID and its sub-data, unless reading
4085 * actual data is skipped. */
4086 if (fd->skip_flags & BLO_READ_SKIP_DATA) {
4087 bhead = blo_bhead_next(fd, bhead);
4088 }
4089 else {
4090 bhead = read_libblock(fd, bfd->main, bhead, ID_TAG_LOCAL, {}, false, nullptr);
4091 }
4092 }
4093 else {
4094 /* Unknown BHead type (or ID type), ignore it and skip to next BHead. */
4095 bhead = blo_bhead_next(fd, bhead);
4096 }
4097 }
4098 }
4099
4100 /* It's not enough to check `bfd->main->is_read_invalid` because the error may have
4101 * occurred before the #Main struct is available, so it's important to check the flag here. */
4102 if (!(fd->flags & FD_FLAGS_FILE_OK)) [[unlikely]] {
4103 /* Skip when already invalidated - that error is more specific. */
4104 if (!bfd->main->is_read_invalid) {
4105 blo_readfile_invalidate(fd, bfd->main, "Corrupt .blend file, failed to read a block");
4106 }
4107 }
4108
4109 if (bfd->main->is_read_invalid) {
4111 return bfd;
4112 }
4113 }
4114
4115 if (is_undo) {
4116 /* Move the remaining Library IDs and their linked data to the new main.
4117 *
4118 * NOTE: These linked IDs have not been detected as used in newly read main. However, they
4119 * could be dependencies from some 'no undo' IDs that were unconditionally moved from the old
4120 * to the new main.
4121 *
4122 * While there could be some more refined check here to detect such cases and only move these
4123 * into the new bmain, in practice it is simpler to systematically move all linked data. The
4124 * handling of libraries already moves all their linked IDs too, regardless of whether they are
4125 * effectively used or not. */
4126
4127 Main *new_main = bfd->main;
4128 Main *old_main = static_cast<Main *>(fd->old_mainlist->first);
4129 BLI_assert(old_main != nullptr);
4130 BLI_assert(old_main->curlib == nullptr);
4131 Main *libmain, *libmain_next;
4132 for (libmain = old_main->next; libmain != nullptr; libmain = libmain_next) {
4133 libmain_next = libmain->next;
4134 read_undo_move_libmain_data(fd, new_main, old_main, libmain, nullptr);
4135 }
4136 }
4137
4138 /* Ensure fully valid and unique ID names before calling first stage of versioning. */
4139 if (!is_undo && (fd->flags & FD_FLAGS_HAS_INVALID_ID_NAMES) != 0) {
4141
4145 "Blendfile '%s' was created by a future version of Blender and contains ID "
4146 "names longer than currently supported. These have been truncated.",
4147 bfd->filepath);
4148 }
4149 else {
4151 RPT_ERROR,
4152 "Blendfile '%s' appears corrupted, it contains invalid ID names. These have "
4153 "been truncated.",
4154 bfd->filepath);
4155 }
4156
4157 /* This part is only to ensure forward compatibility with 5.0+ blend-files in 4.5.
4158 * It will be removed in 5.0. */
4160 }
4161 else {
4162 /* Getting invalid ID names from memfile undo data would be a critical error. */
4164 if ((fd->flags & FD_FLAGS_HAS_INVALID_ID_NAMES) != 0) {
4165 bfd->main->is_read_invalid = true;
4166 }
4167 }
4168
4169 if (bfd->main->is_read_invalid) {
4171 return bfd;
4172 }
4173
4174 /* Do versioning before read_libraries, but skip in undo case. */
4175 if (!is_undo) {
4176 if ((fd->skip_flags & BLO_READ_SKIP_DATA) == 0) {
4177 do_versions(fd, nullptr, bfd->main);
4178 }
4179
4180 if ((fd->skip_flags & BLO_READ_SKIP_USERDEF) == 0) {
4181 do_versions_userdef(fd, bfd);
4182 }
4183 }
4184
4185 if (bfd->main->is_read_invalid) {
4187 return bfd;
4188 }
4189
4190 if ((fd->skip_flags & BLO_READ_SKIP_DATA) == 0) {
4192 read_libraries(fd, &mainlist);
4193 if (bfd->main->is_read_invalid) {
4195 return bfd;
4196 }
4197
4198 blo_join_main(&mainlist);
4199
4200 lib_link_all(fd, bfd->main);
4201 if (bfd->main->is_read_invalid) {
4203 return bfd;
4204 }
4205
4207
4208 if (is_undo) {
4209 /* Ensure ID usages of reused 'no undo' IDs remain valid. */
4210 /* Although noundo data was reused as-is from the old main, it may have ID pointers to data
4211 * that has been removed, or that have a new address. */
4213 }
4214
4216
4217 /* Skip in undo case. */
4218 if (!is_undo) {
4219 /* Note that we can't recompute user-counts at this point in undo case, we play too much with
4220 * IDs from different memory realms, and Main database is not in a fully valid state yet.
4221 */
4222 /* Some versioning code does expect some proper user-reference-counting, e.g. in conversion
4223 * from groups to collections... We could optimize out that first call when we are reading a
4224 * current version file, but again this is really not a bottle neck currently.
4225 * So not worth it. */
4227
4228 /* Necessary to allow 2.80 layer collections conversion code to work. */
4230
4231 /* Yep, second splitting... but this is a very cheap operation, so no big deal. */
4232 blo_split_main(&mainlist, bfd->main);
4233 LISTBASE_FOREACH (Main *, mainvar, &mainlist) {
4234 /* Do versioning for newly added linked data-blocks. If no data-blocks were read from a
4235 * library versionfile will still be zero and we can skip it. */
4236 if (mainvar->versionfile == 0) {
4237 continue;
4238 }
4239 do_versions_after_linking((mainvar->curlib && mainvar->curlib->runtime->filedata) ?
4240 mainvar->curlib->runtime->filedata :
4241 fd,
4242 mainvar);
4243 }
4244 blo_join_main(&mainlist);
4245
4247
4248 /* And we have to compute those user-reference-counts again, as `do_versions_after_linking()`
4249 * does not always properly handle user counts, and/or that function does not take into
4250 * account old, deprecated data. */
4252 }
4253
4255 /* Now we can clear this runtime library filedata, it is not needed anymore. */
4256 if (lib->runtime->filedata) {
4257 BLI_assert(lib->runtime->versionfile != 0);
4258 blo_filedata_free(lib->runtime->filedata);
4259 lib->runtime->filedata = nullptr;
4260 }
4261 /* If no data-blocks were read from a library (should only happen when all references to a
4262 * library's data are `ID_FLAG_INDIRECT_WEAK_LINK`), its versionfile will still be zero and
4263 * it can be deleted.
4264 *
4265 * NOTES:
4266 * - In case the library blendfile exists but is missing all the referenced linked IDs, the
4267 * placeholders IDs created will reference the library ID, and the library ID will have a
4268 * valid version number as the file was read to search for the linked IDs.
4269 * - In case the library blendfile does not exist, its local Library ID will get the version
4270 * of the current local Main (i.e. the loaded blendfile). */
4271 else if (lib->runtime->versionfile == 0) {
4272#ifndef NDEBUG
4273 ID *id_iter;
4274 FOREACH_MAIN_ID_BEGIN (bfd->main, id_iter) {
4275 BLI_assert(id_iter->lib != lib);
4276 }
4278#endif
4279 BKE_id_delete(bfd->main, lib);
4280 }
4281 }
4282
4283 if (bfd->main->is_read_invalid) {
4285 return bfd;
4286 }
4287
4288 /* After all data has been read and versioned, uses ID_TAG_NEW. Theoretically this should
4289 * not be calculated in the undo case, but it is currently needed even on undo to recalculate
4290 * a cache. */
4292
4294
4295 BKE_main_id_tag_all(bfd->main, ID_TAG_NEW, false);
4296
4297 /* Must happen before applying liboverrides, as this process may fully invalidate e.g. view
4298 * layer pointers in case a Scene is a liboverride. */
4299 link_global(fd, bfd);
4300
4301 /* Now that all our data-blocks are loaded,
4302 * we can re-generate overrides from their references. */
4303 if (!is_undo) {
4304 /* Do not apply in undo case! */
4306
4307 std::string cur_view_layer_name = bfd->cur_view_layer != nullptr ?
4308 bfd->cur_view_layer->name :
4309 "";
4310
4313
4314 /* In case the current scene is a liboverride, while the ID pointer itself remains valid,
4315 * above update of liboverrides will have completely invalidated its old content, so the
4316 * current view-layer needs to be searched for again. */
4317 if (bfd->cur_view_layer != nullptr) {
4318 bfd->cur_view_layer = BKE_view_layer_find(bfd->curscene, cur_view_layer_name.c_str());
4319 }
4320
4321 /* FIXME Temporary 'fix' to a problem in how temp ID are copied in
4322 * `BKE_lib_override_library_main_update`, see #103062.
4323 * Proper fix involves first addressing #90610. */
4325
4326 /* Update invariants after re-generating overrides. */
4328
4331 }
4332
4334
4336
4337 /* Make all relative paths, relative to the open blend file. */
4339 }
4340 else {
4342 }
4343
4344 fd->mainlist = nullptr; /* Safety, this is local variable, shall not be used afterward. */
4345
4346 BLI_assert(bfd->main->id_map == nullptr);
4347
4348 /* Sanity checks. */
4350
4351 return bfd;
4352}
4353
4356/* -------------------------------------------------------------------- */
4364 const void *old;
4365};
4366
4367static int verg_bheadsort(const void *v1, const void *v2)
4368{
4369 const BHeadSort *x1 = static_cast<const BHeadSort *>(v1),
4370 *x2 = static_cast<const BHeadSort *>(v2);
4371
4372 if (x1->old > x2->old) {
4373 return 1;
4374 }
4375 if (x1->old < x2->old) {
4376 return -1;
4377 }
4378 return 0;
4379}
4380
4382{
4383 BHead *bhead;
4384 BHeadSort *bhs;
4385 int tot = 0;
4386
4387 for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
4388 tot++;
4389 }
4390
4391 fd->tot_bheadmap = tot;
4392 if (tot == 0) {
4393 return;
4394 }
4395
4396 bhs = fd->bheadmap = MEM_malloc_arrayN<BHeadSort>(tot, "BHeadSort");
4397
4398 for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead), bhs++) {
4399 bhs->bhead = bhead;
4400 bhs->old = bhead->old;
4401 }
4402
4403 qsort(fd->bheadmap, tot, sizeof(BHeadSort), verg_bheadsort);
4404}
4405
4407{
4408 /* Skip library data-blocks in undo, see comment in read_libblock. */
4409 if (fd->flags & FD_FLAGS_IS_MEMFILE) {
4410 return nullptr;
4411 }
4412
4413 for (; bhead; bhead = blo_bhead_prev(fd, bhead)) {
4414 if (bhead->code == ID_LI) {
4415 break;
4416 }
4417 }
4418
4419 return bhead;
4420}
4421
4423{
4424 ID *id = read_id_struct(fd, bheadlib, "Data for Library ID type", INDEX_ID_NULL);
4425 if (id && GS(id->name) != ID_LI) [[unlikely]] {
4426 MEM_freeN(id);
4427 fd->flags &= ~FD_FLAGS_FILE_OK;
4428 if (Main *bmain = filedata_main(fd)) {
4429 blo_readfile_invalidate(fd, bmain, "Corrupt .blend file, unexpected non-library block type");
4430 }
4431 return nullptr;
4432 }
4433
4434 /* The file path must have a null terminated string. */
4435 Library *lib = reinterpret_cast<Library *>(id);
4436 if (lib && !library_filepath_is_valid(lib)) [[unlikely]] {
4437 MEM_freeN(lib);
4438 fd->flags &= ~FD_FLAGS_FILE_OK;
4439 if (Main *bmain = filedata_main(fd)) {
4440 blo_readfile_invalidate(fd, bmain, "Corrupt .blend file, invalid library file path");
4441 }
4442 return nullptr;
4443 }
4444 return lib;
4445}
4446
4447static BHead *find_bhead(FileData *fd, void *old)
4448{
4449#if 0
4450 BHead *bhead;
4451#endif
4452 BHeadSort *bhs, bhs_s;
4453
4454 if (!old) {
4455 return nullptr;
4456 }
4457
4458 if (fd->bheadmap == nullptr) {
4460 }
4461
4462 bhs_s.old = old;
4463 bhs = static_cast<BHeadSort *>(
4464 bsearch(&bhs_s, fd->bheadmap, fd->tot_bheadmap, sizeof(BHeadSort), verg_bheadsort));
4465
4466 if (bhs) {
4467 return bhs->bhead;
4468 }
4469
4470#if 0
4471 for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
4472 if (bhead->old == old) {
4473 return bhead;
4474 }
4475 }
4476#endif
4477
4478 return nullptr;
4479}
4480
4481static BHead *find_bhead_from_code_name(FileData *fd, const short idcode, const char *name)
4482{
4483 char idname_full[MAX_ID_NAME];
4484 *((short *)idname_full) = idcode;
4485 BLI_strncpy(idname_full + 2, name, sizeof(idname_full) - 2);
4486
4487 return fd->bhead_idname_map->lookup_default(idname_full, nullptr);
4488}
4489
4490static BHead *find_bhead_from_idname(FileData *fd, const char *idname)
4491{
4492 BHead *bhead = fd->bhead_idname_map->lookup_default(idname, nullptr);
4493 if (LIKELY(bhead)) {
4494 return bhead;
4495 }
4496
4497 /* Expected ID was not found, attempt to load the same name, but for an older, deprecated and
4498 * converted ID type. */
4499 const short id_code_old = do_versions_new_to_old_idcode_get(GS(idname));
4500 if (id_code_old == ID_LINK_PLACEHOLDER) {
4501 return bhead;
4502 }
4503 return find_bhead_from_code_name(fd, id_code_old, idname + 2);
4504}
4505
4506static ID *library_id_is_yet_read(FileData *fd, Main *mainvar, BHead *bhead)
4507{
4508 if (mainvar->id_map == nullptr) {
4509 mainvar->id_map = BKE_main_idmap_create(mainvar, false, nullptr, MAIN_IDMAP_TYPE_NAME);
4510 }
4511 BLI_assert(BKE_main_idmap_main_get(mainvar->id_map) == mainvar);
4512
4513 const char *idname = blo_bhead_id_name(fd, bhead);
4514 if (!idname) {
4515 return nullptr;
4516 }
4517
4518 ID *id = BKE_main_idmap_lookup_name(mainvar->id_map, GS(idname), idname + 2, mainvar->curlib);
4519 BLI_assert(id == BLI_findstring(which_libbase(mainvar, GS(idname)), idname, offsetof(ID, name)));
4520 return id;
4521}
4522
4524 ReportList *reports,
4525 const bool has_forward_compatibility_issues,
4526 const char *filepath)
4527{
4528 if (!fd || (fd->flags & FD_FLAGS_HAS_INVALID_ID_NAMES) == 0) {
4529 return;
4530 }
4531 if (has_forward_compatibility_issues) {
4532 BKE_reportf(reports,
4534 "Library '%s' was created by a future version of Blender and contains ID names "
4535 "longer than currently supported. This may cause missing linked data, consider "
4536 "opening and re-saving that library with the current Blender version.",
4537 filepath);
4538 }
4539 else {
4540 BKE_reportf(reports,
4541 RPT_ERROR,
4542 "Library '%s' appears corrupted, it contains invalid ID names. This may cause "
4543 "missing linked data.",
4544 filepath);
4545 }
4546}
4547
4550/* -------------------------------------------------------------------- */
4559
4560static void expand_doit_library(void *fdhandle, Main *mainvar, void *old)
4561{
4562 FileData *fd = static_cast<FileData *>(fdhandle);
4563
4564 if (mainvar->is_read_invalid) {
4565 return;
4566 }
4567
4568 BHead *bhead = find_bhead(fd, old);
4569 if (bhead == nullptr) {
4570 return;
4571 }
4572 /* In 2.50+ file identifier for screens is patched, forward compatibility. */
4573 if (bhead->code == ID_SCRN) {
4574 bhead->code = ID_SCR;
4575 }
4576 if (!blo_bhead_is_id_valid_type(bhead)) {
4577 return;
4578 }
4579 if (!blo_bhead_id_name(fd, bhead)) {
4580 /* Do not allow linking ID which names are invalid (likely coming from a future version of
4581 * Blender allowing longer names). */
4582 return;
4583 }
4584
4585 if (bhead->code == ID_LINK_PLACEHOLDER) {
4586 /* Placeholder link to data-block in another library. */
4587 BHead *bheadlib = find_previous_lib(fd, bhead);
4588 if (bheadlib == nullptr) {
4589 return;
4590 }
4591
4592 Library *lib = read_library_struct(fd, bheadlib);
4593 if (lib == nullptr) {
4594 return;
4595 }
4596 Main *libmain = blo_find_main(fd, lib->filepath, fd->relabase);
4597
4598 if (libmain->curlib == nullptr) {
4599 const char *idname = blo_bhead_id_name(fd, bhead);
4600
4603 RPT_("LIB: Data refers to main .blend file: '%s' from %s"),
4604 idname ? idname : "<InvalidIDName>",
4605 mainvar->curlib->runtime->filepath_abs);
4606 return;
4607 }
4608
4609 ID *id = library_id_is_yet_read(fd, libmain, bhead);
4610
4611 if (id == nullptr) {
4612 /* ID has not been read yet, add placeholder to the main of the
4613 * library it belongs to, so that it will be read later. */
4614 read_libblock(fd, libmain, bhead, fd->id_tag_extra | ID_TAG_INDIRECT, {}, false, &id);
4615 /* A corrupt block may fail to read, leaving `id` null.
4616 * Skip it rather than dereferencing null - #read_libblock has already advanced
4617 * past the block, and #link_named_part handles the same case this way. */
4618 if (id == nullptr) {
4619 MEM_freeN(lib);
4620 return;
4621 }
4622 id_sort_by_name(which_libbase(libmain, GS(id->name)), id, static_cast<ID *>(id->prev));
4623
4624 /* commented because this can print way too much */
4625 // if (G.debug & G_DEBUG) printf("expand_doit: other lib %s\n", lib->filepath);
4626
4627 /* for outliner dependency only */
4628 libmain->curlib->runtime->parent = mainvar->curlib;
4629 }
4630 else {
4631 /* Convert any previously read weak link to regular link
4632 * to signal that we want to read this data-block. */
4634 id->flag &= ~ID_FLAG_INDIRECT_WEAK_LINK;
4635 }
4636
4637 /* "id" is either a placeholder or real ID that is already in the
4638 * main of the library (A) it belongs to. However it might have been
4639 * put there by another library (C) which only updated its own
4640 * fd->libmap. In that case we also need to update the fd->libmap
4641 * of the current library (B) so we can find it for lookups.
4642 *
4643 * An example of such a setup is:
4644 * (A) tree.blend: contains Tree object.
4645 * (B) forest.blend: contains Forest collection linking in Tree from tree.blend.
4646 * (C) shot.blend: links in both Tree from tree.blend and Forest from forest.blend.
4647 */
4648 oldnewmap_lib_insert(fd, bhead->old, id, bhead->code);
4649
4650 /* Commented because this can print way too much. */
4651#if 0
4652 if (G.debug & G_DEBUG) {
4653 printf("expand_doit: already linked: %s lib: %s\n", id->name, lib->filepath);
4654 }
4655#endif
4656 }
4657
4658 MEM_freeN(lib);
4659 }
4660 else {
4661 /* Data-block in same library. */
4662 ID *id = library_id_is_yet_read(fd, mainvar, bhead);
4663 if (id == nullptr) {
4664 ID_Readfile_Data::Tags id_read_tags{};
4665 id_read_tags.needs_expanding = true;
4667 fd, mainvar, bhead, fd->id_tag_extra | ID_TAG_INDIRECT, id_read_tags, false, &id);
4668 /* A corrupt block may fail to read, leaving `id` null. */
4669 if (id == nullptr) {
4670 return;
4671 }
4672 id_sort_by_name(which_libbase(mainvar, GS(id->name)), id, static_cast<ID *>(id->prev));
4673 }
4674 else {
4675 /* Convert any previously read weak link to regular link to signal that we want to read this
4676 * data-block. Note that this function also visits already-loaded data-blocks, and thus their
4677 * `readfile_data` field might already have been freed. */
4679 id->flag &= ~ID_FLAG_INDIRECT_WEAK_LINK;
4680 }
4681
4682 /* this is actually only needed on UI call? when ID was already read before,
4683 * and another append happens which invokes same ID...
4684 * in that case the lookup table needs this entry */
4685 oldnewmap_lib_insert(fd, bhead->old, id, bhead->code);
4686 /* commented because this can print way too much */
4687 // if (G.debug & G_DEBUG) printf("expand: already read %s\n", id->name);
4688 }
4689 }
4690}
4691
4693{
4694 /* Embedded IDs are not known by lib_link code, so they would be remapped to `nullptr`. But there
4695 * is no need to process them anyway, as they are already handled during the 'read_data' phase.
4696 */
4698 return IDWALK_RET_NOP;
4699 }
4700
4701 /* Explicitly requested to be ignored during readfile processing. Means the read_data code
4702 * already handled this pointer. Typically, the 'owner_id' pointer of an embedded ID. */
4703 if (cb_data->cb_flag & IDWALK_CB_READFILE_IGNORE) {
4704 return IDWALK_RET_NOP;
4705 }
4706
4707 /* Expand process can be re-entrant or have other complex interactions that will not work well
4708 * with loop-back pointers. Further more, processing such data should not be needed here anyway.
4709 */
4710 if (cb_data->cb_flag & (IDWALK_CB_LOOPBACK)) {
4711 return IDWALK_RET_NOP;
4712 }
4713
4714 BlendExpander *expander = static_cast<BlendExpander *>(cb_data->user_data);
4715 ID *id = *(cb_data->id_pointer);
4716
4717 expander->callback(expander->fd, expander->main, id);
4718
4719 return IDWALK_RET_NOP;
4720}
4721
4723{
4724 FileData *fd = static_cast<FileData *>(fdhandle);
4725 BlendExpander expander = {fd, mainvar, callback};
4726
4727 for (bool do_it = true; do_it;) {
4728 do_it = false;
4729 ID *id_iter;
4730
4731 FOREACH_MAIN_ID_BEGIN (mainvar, id_iter) {
4733 continue;
4734 }
4735
4736 /* Original (current) ID pointer can be considered as valid, but _not_ its own pointers to
4737 * other IDs - the already loaded ones will be valid, but the yet-to-be-read ones will not.
4738 * Expanding should _not_ require processing of UI ID pointers.
4739 * Expanding should never modify ID pointers themselves.
4740 * Handling of DNA deprecated data should never be needed in undo case. */
4742 ((!fd || (fd->flags & FD_FLAGS_IS_MEMFILE)) ?
4743 IDWALK_NOP :
4745 BKE_library_foreach_ID_link(nullptr, id_iter, expand_cb, &expander, flag);
4746
4747 do_it = true;
4749 }
4751 }
4752}
4753
4756/* -------------------------------------------------------------------- */
4760/* returns true if the item was found
4761 * but it may already have already been appended/linked */
4763 Main *mainl, FileData *fd, const short idcode, const char *name, const int flag)
4764{
4765 BHead *bhead = find_bhead_from_code_name(fd, idcode, name);
4766 ID *id;
4767
4768 const bool use_placeholders = (flag & BLO_LIBLINK_USE_PLACEHOLDERS) != 0;
4769 const bool force_indirect = (flag & BLO_LIBLINK_FORCE_INDIRECT) != 0;
4770
4772
4773 if (bhead && blo_bhead_is_id_valid_type(bhead)) {
4774 id = library_id_is_yet_read(fd, mainl, bhead);
4775 if (id == nullptr) {
4776 /* not read yet */
4777 const int tag = ((force_indirect ? ID_TAG_INDIRECT : ID_TAG_EXTERN) | fd->id_tag_extra);
4778 ID_Readfile_Data::Tags id_read_tags{};
4779 id_read_tags.needs_expanding = true;
4780 read_libblock(fd, mainl, bhead, tag, id_read_tags, false, &id);
4781
4782 if (id) {
4783 /* sort by name in list */
4784 ListBase *lb = which_libbase(mainl, idcode);
4785 id_sort_by_name(lb, id, nullptr);
4786 }
4787 }
4788 else {
4789 /* already linked */
4790 CLOG_WARN(&LOG, "Append: ID '%s' is already linked", id->name);
4791 oldnewmap_lib_insert(fd, bhead->old, id, bhead->code);
4792 if (!force_indirect && (id->tag & ID_TAG_INDIRECT)) {
4793 id->tag &= ~ID_TAG_INDIRECT;
4794 id->flag &= ~ID_FLAG_INDIRECT_WEAK_LINK;
4795 id->tag |= ID_TAG_EXTERN;
4796 }
4797 }
4798 }
4799 else if (use_placeholders) {
4800 /* XXX flag part is weak! */
4801 id = create_placeholder(
4802 mainl, idcode, name, force_indirect ? ID_TAG_INDIRECT : ID_TAG_EXTERN, false);
4803 }
4804 else {
4805 id = nullptr;
4806 }
4807
4808 /* NOTE: `id` may be `nullptr` even if a BHead was found, in case e.g. it is an invalid BHead. */
4809
4810 return id;
4811}
4812
4814 BlendHandle **bh,
4815 const short idcode,
4816 const char *name,
4818{
4819 FileData *fd = (FileData *)(*bh);
4820
4821 ID *ret_id = nullptr;
4822 if (!mainl->is_read_invalid) {
4823 ret_id = link_named_part(mainl, fd, idcode, name, params->flag);
4824 }
4825
4826 if (mainl->is_read_invalid) {
4827 return nullptr;
4828 }
4829 return ret_id;
4830}
4831
4832/* common routine to append/link something from a library */
4833
4835 FileData *fd,
4836 const char *filepath,
4837 const int id_tag_extra)
4838{
4839 Main *mainl;
4840
4841 /* Only allow specific tags to be set as extra,
4842 * otherwise this could conflict with library loading logic.
4843 * Other flags can be added here, as long as they are safe. */
4844 BLI_assert((id_tag_extra & ~ID_TAG_TEMP_MAIN) == 0);
4845
4846 fd->id_tag_extra = id_tag_extra;
4847
4848 fd->mainlist = MEM_callocN<ListBase>("FileData.mainlist");
4849
4850 /* make mains */
4851 blo_split_main(fd->mainlist, mainvar);
4852
4853 /* which one do we need? */
4854 mainl = blo_find_main(fd, filepath, BKE_main_blendfile_path(mainvar));
4855 if (mainl->curlib) {
4856 mainl->curlib->runtime->filedata = fd;
4857 }
4858
4859 /* needed for do_version */
4860 mainl->versionfile = short(fd->fileversion);
4861 read_file_version(fd, mainl);
4863
4864 return mainl;
4865}
4866
4868 Main *bmain,
4869 const int flag,
4870 const int id_tag_extra)
4871{
4872 memset(params, 0, sizeof(*params));
4873 params->bmain = bmain;
4874 params->flag = flag;
4875 params->id_tag_extra = id_tag_extra;
4876}
4877
4879 Main *bmain,
4880 const int flag,
4881 const int id_tag_extra,
4882 /* Context arguments. */
4883 Scene *scene,
4884 ViewLayer *view_layer,
4885 const View3D *v3d)
4886{
4887 BLO_library_link_params_init(params, bmain, flag, id_tag_extra);
4888 if (scene != nullptr) {
4889 params->context.scene = scene;
4890 params->context.view_layer = view_layer;
4891 params->context.v3d = v3d;
4892 }
4893}
4894
4896 const char *filepath,
4898{
4899 FileData *fd = reinterpret_cast<FileData *>(*bh);
4900 return library_link_begin(params->bmain, fd, filepath, params->id_tag_extra);
4901}
4902
4903static void split_main_newid(Main *mainptr, Main *main_newid)
4904{
4905 /* We only copy the necessary subset of data in this temp main. */
4906 main_newid->versionfile = mainptr->versionfile;
4907 main_newid->subversionfile = mainptr->subversionfile;
4908 STRNCPY(main_newid->filepath, mainptr->filepath);
4909 main_newid->curlib = mainptr->curlib;
4910
4911 MainListsArray lbarray = BKE_main_lists_get(*mainptr);
4912 MainListsArray lbarray_newid = BKE_main_lists_get(*main_newid);
4913 int i = lbarray.size();
4914 while (i--) {
4915 BLI_listbase_clear(lbarray_newid[i]);
4916
4917 LISTBASE_FOREACH_MUTABLE (ID *, id, lbarray[i]) {
4918 if (id->tag & ID_TAG_NEW) {
4919 BLI_remlink(lbarray[i], id);
4920 BLI_addtail(lbarray_newid[i], id);
4921 }
4922 }
4923 }
4924}
4925
4926static void library_link_end(Main *mainl, FileData **fd, const int flag, ReportList *reports)
4927{
4928 Main *mainvar;
4929 Library *curlib;
4930
4931 if (mainl->id_map == nullptr) {
4932 mainl->id_map = BKE_main_idmap_create(mainl, false, nullptr, MAIN_IDMAP_TYPE_NAME);
4933 }
4934
4935 /* make main consistent */
4937
4939 *fd, reports, mainl->has_forward_compatibility_issues, mainl->curlib->runtime->filepath_abs);
4940
4941 /* Do this when expand found other libraries. */
4942 read_libraries(*fd, (*fd)->mainlist);
4943
4944 curlib = mainl->curlib;
4945
4946 /* make the lib path relative if required */
4947 if (flag & FILE_RELPATH) {
4948 /* use the full path, this could have been read by other library even */
4949 STRNCPY(curlib->filepath, curlib->runtime->filepath_abs);
4950
4951 /* uses current .blend file as reference */
4953 }
4954
4955 blo_join_main((*fd)->mainlist);
4956 mainvar = static_cast<Main *>((*fd)->mainlist->first);
4957 mainl = nullptr; /* blo_join_main free's mainl, can't use anymore */
4958
4959 if (mainvar->is_read_invalid) {
4960 MEM_freeN((*fd)->mainlist);
4961 return;
4962 }
4963
4964 lib_link_all(*fd, mainvar);
4965 after_liblink_merged_bmain_process(mainvar, (*fd)->reports);
4966
4967 /* Some versioning code does expect some proper userrefcounting, e.g. in conversion from
4968 * groups to collections... We could optimize out that first call when we are reading a
4969 * current version file, but again this is really not a bottle neck currently. So not worth
4970 * it. */
4971 BKE_main_id_refcount_recompute(mainvar, false);
4972
4973 /* FIXME: This is suspiciously early call compared to similar process in
4974 * #blo_read_file_internal, where it is called towards the very end, after all do_version,
4975 * liboverride updates etc. have been done. */
4976 /* FIXME: Probably also need to forbid layer collections updates until this call, as done in
4977 * #blo_read_file_internal? */
4979
4980 /* Yep, second splitting... but this is a very cheap operation, so no big deal. */
4981 blo_split_main((*fd)->mainlist, mainvar);
4982 Main *main_newid = BKE_main_new();
4983 for (mainvar = ((Main *)(*fd)->mainlist->first)->next; mainvar; mainvar = mainvar->next) {
4984 BLI_assert(mainvar->versionfile != 0);
4985 /* We need to split out IDs already existing,
4986 * or they will go again through do_versions - bad, very bad! */
4987 split_main_newid(mainvar, main_newid);
4988
4989 do_versions_after_linking((main_newid->curlib && main_newid->curlib->runtime->filedata) ?
4990 main_newid->curlib->runtime->filedata :
4991 *fd,
4992 main_newid);
4993
4994 add_main_to_main(mainvar, main_newid);
4995
4996 if (mainvar->is_read_invalid) {
4997 break;
4998 }
4999 }
5000
5001 blo_join_main((*fd)->mainlist);
5002 mainvar = static_cast<Main *>((*fd)->mainlist->first);
5003 MEM_freeN((*fd)->mainlist);
5004
5005 if (mainvar->is_read_invalid) {
5006 BKE_main_free(main_newid);
5007 return;
5008 }
5009
5010 /* This does not take into account old, deprecated data, so we also have to do it after
5011 * `do_versions_after_linking()`. */
5012 BKE_main_id_refcount_recompute(mainvar, false);
5013
5014 /* After all data has been read and versioned, uses ID_TAG_NEW. */
5016
5018
5019 /* Apply overrides of newly linked data if needed. Already existing IDs need to split out, to
5020 * avoid re-applying their own overrides. */
5021 BLI_assert(BKE_main_is_empty(main_newid));
5022 split_main_newid(mainvar, main_newid);
5023 BKE_lib_override_library_main_validate(main_newid, (*fd)->reports->reports);
5025 add_main_to_main(mainvar, main_newid);
5026 BKE_main_free(main_newid);
5027
5028 BKE_main_id_tag_all(mainvar, ID_TAG_NEW, false);
5029
5030 /* FIXME Temporary 'fix' to a problem in how temp ID are copied in
5031 * `BKE_lib_override_library_main_update`, see #103062.
5032 * Proper fix involves first addressing #90610. */
5035 }
5036
5037 /* Make all relative paths, relative to the open blend file. */
5039
5040 /* patch to prevent switch_endian happens twice */
5041 /* FIXME This is extremely bad design, #library_link_end should probably _always_ free the file
5042 * data? */
5043 if ((*fd)->flags & FD_FLAGS_SWITCH_ENDIAN) {
5044 blo_filedata_free(*fd);
5045 *fd = nullptr;
5046 }
5047
5048 /* Sanity checks. */
5049 blo_read_file_checks(mainvar);
5050}
5051
5053 BlendHandle **bh,
5055 ReportList *reports)
5056{
5057 FileData *fd = reinterpret_cast<FileData *>(*bh);
5058
5059 if (!mainl->is_read_invalid) {
5060 library_link_end(mainl, &fd, params->flag, reports);
5061 }
5062 else {
5063 /* Restore the main split by #library_link_begin. */
5065 MEM_freeN(fd->mainlist);
5066 }
5067
5068 LISTBASE_FOREACH (Library *, lib, &params->bmain->libraries) {
5069 /* Now we can clear this runtime library filedata, it is not needed anymore. */
5070 if (lib->runtime->filedata == reinterpret_cast<FileData *>(*bh)) {
5071 /* The filedata is owned and managed by caller code, only clear matching library pointer. */
5072 lib->runtime->filedata = nullptr;
5073 }
5074 else if (lib->runtime->filedata) {
5075 /* In case other libraries had to be read as dependencies of the main linked one, they need
5076 * to be cleared here.
5077 *
5078 * TODO: In the future, could be worth keeping them in case data are linked from several
5079 * libraries at once? To avoid closing and re-opening the same file several times. Would need
5080 * a global cleanup callback then once all linking is done, though. */
5081 blo_filedata_free(lib->runtime->filedata);
5082 lib->runtime->filedata = nullptr;
5083 }
5084 }
5085
5086 *bh = reinterpret_cast<BlendHandle *>(fd);
5087}
5088
5089void *BLO_library_read_struct(FileData *fd, BHead *bh, const char *blockname)
5090{
5091 return read_struct(fd, bh, blockname, INDEX_ID_NULL);
5092}
5093
5096/* -------------------------------------------------------------------- */
5100static int has_linked_ids_to_read(Main *mainvar)
5101{
5102 MainListsArray lbarray = BKE_main_lists_get(*mainvar);
5103 int a = lbarray.size();
5104 while (a--) {
5105 LISTBASE_FOREACH (ID *, id, lbarray[a]) {
5107 !(id->flag & ID_FLAG_INDIRECT_WEAK_LINK))
5108 {
5109 return true;
5110 }
5111 }
5112 }
5113
5114 return false;
5115}
5116
5118 FileData *basefd, FileData *fd, Main *mainvar, ID *id, ID **r_id)
5119{
5120 BHead *bhead = nullptr;
5121 const bool is_valid = BKE_idtype_idcode_is_linkable(GS(id->name)) ||
5122 ((id->tag & ID_TAG_EXTERN) == 0);
5123
5124 if (fd) {
5125 /* About future longer ID names: This is one of the main places that prevent linking IDs with
5126 * names longer than MAX_ID_NAME - 1.
5127 *
5128 * See also #read_file_bhead_idname_map_create. */
5129 bhead = find_bhead_from_idname(fd, id->name);
5130 }
5131
5132 if (!is_valid) {
5133 BLO_reportf_wrap(basefd->reports,
5134 RPT_ERROR,
5135 RPT_("LIB: %s: '%s' is directly linked from '%s' (parent '%s'), but is a "
5136 "non-linkable data type"),
5138 id->name + 2,
5139 mainvar->curlib->runtime->filepath_abs,
5141 }
5142
5144 id->flag &= ~ID_FLAG_INDIRECT_WEAK_LINK;
5145
5146 if (bhead) {
5148 // printf("read lib block %s\n", id->name);
5149 read_libblock(fd, mainvar, bhead, id->tag, BLO_readfile_id_runtime_tags(*id), false, r_id);
5150 }
5151 else {
5152 CLOG_INFO(&LOG,
5153 3,
5154 "LIB: %s: '%s' missing from '%s', parent '%s'",
5156 id->name + 2,
5157 mainvar->curlib->runtime->filepath_abs,
5159 basefd->reports->count.missing_linked_id++;
5160
5161 /* Generate a placeholder for this ID (simplified version of read_libblock actually...). */
5162 if (r_id) {
5163 *r_id = is_valid ? create_placeholder(mainvar,
5164 GS(id->name),
5165 id->name + 2,
5166 id->tag,
5167 id->override_library != nullptr) :
5168 nullptr;
5169 }
5170 }
5171}
5172
5174 FileData *fd,
5175 ListBase *mainlist,
5176 Main *mainvar)
5177{
5179
5180 MainListsArray lbarray = BKE_main_lists_get(*mainvar);
5181 int a = lbarray.size();
5182 while (a--) {
5183 ID *id = static_cast<ID *>(lbarray[a]->first);
5184
5185 while (id) {
5186 ID *id_next = static_cast<ID *>(id->next);
5189 {
5190 BLI_remlink(lbarray[a], id);
5191 if (mainvar->id_map != nullptr) {
5192 BKE_main_idmap_remove_id(mainvar->id_map, id);
5193 }
5194
5195 /* When playing with lib renaming and such, you may end with cases where
5196 * you have more than one linked ID of the same data-block from same
5197 * library. This is absolutely horrible, hence we use a ghash to ensure
5198 * we go back to a single linked data when loading the file. */
5199 ID *realid = loaded_ids.lookup_default(id->name, nullptr);
5200 if (!realid) {
5201 read_library_linked_id(basefd, fd, mainvar, id, &realid);
5202 loaded_ids.add_overwrite(id->name, realid);
5203 }
5204
5205 /* `realid` shall never be nullptr - unless some source file/lib is broken
5206 * (known case: some directly linked shape-key from a missing lib...). */
5207 // BLI_assert(*realid != nullptr);
5208
5209 /* Now that we have a real ID, replace all pointers to placeholders in
5210 * fd->libmap with pointers to the real data-blocks. We do this for all
5211 * libraries since multiple might be referencing this ID. */
5212 change_link_placeholder_to_real_ID_pointer(mainlist, basefd, id, realid);
5213
5214 /* Transfer the readfile data from the placeholder to the real ID, but
5215 * only if the real ID has no readfile data yet. The same realid may be
5216 * referred to by multiple placeholders. */
5217 if (realid && !realid->runtime.readfile_data) {
5218 realid->runtime.readfile_data = id->runtime.readfile_data;
5219 id->runtime.readfile_data = nullptr;
5220 }
5221
5222 /* The 'readfile' runtime data needs to be freed here, as this ID placeholder does not go
5223 * through versioning (the usual place where this data is freed). Since `id` is not a real
5224 * ID, this shouldn't follow any pointers to embedded IDs. */
5226 MEM_freeN(id);
5227
5228 /* A failed on-demand block read only clears `FD_FLAGS_FILE_OK` on `fd` (as for the
5229 * local-data read loop in #blo_read_file_internal), it doesn't invalidate `mainvar` by
5230 * itself. Without this check such a failure looks just like a "missing linked ID" below,
5231 * silently continuing the read instead of reporting the corrupt library and aborting.
5232 *
5233 * Note: Needs to be done at the end, to ensure placeholder `id` is correctly freed. */
5234 if (fd && !(fd->flags & FD_FLAGS_FILE_OK)) [[unlikely]] {
5235 if (!mainvar->is_read_invalid) {
5236 blo_readfile_invalidate(fd, mainvar, "Failed to read a block");
5237 }
5238 return;
5239 }
5240 }
5241 id = id_next;
5242 }
5243
5244 loaded_ids.clear();
5245 }
5246
5248 basefd->reports->reports,
5250 mainvar->curlib->runtime->filepath_abs);
5251}
5252
5253static void read_library_clear_weak_links(FileData *basefd, ListBase *mainlist, Main *mainvar)
5254{
5255 /* Any remaining weak links at this point have been lost, silently drop
5256 * those by setting them to nullptr pointers. */
5257 MainListsArray lbarray = BKE_main_lists_get(*mainvar);
5258 int a = lbarray.size();
5259 while (a--) {
5260 ID *id = static_cast<ID *>(lbarray[a]->first);
5261
5262 while (id) {
5263 ID *id_next = static_cast<ID *>(id->next);
5264
5265 /* This function also visits already-loaded data-blocks, and thus their
5266 * `readfile_data` field might already have been freed. */
5269 {
5270 CLOG_INFO(&LOG, 3, "Dropping weak link to '%s'", id->name);
5271 change_link_placeholder_to_real_ID_pointer(mainlist, basefd, id, nullptr);
5272 BLI_freelinkN(lbarray[a], id);
5273 }
5274 id = id_next;
5275 }
5276 }
5277}
5278
5280 ListBase *mainlist,
5281 Main *mainl,
5282 Main *mainptr)
5283{
5284 FileData *fd = mainptr->curlib->runtime->filedata;
5285
5286 if (fd != nullptr) {
5287 /* File already open. */
5288 return fd;
5289 }
5290
5291 if (mainptr->curlib->packedfile) {
5292 /* Read packed file. */
5293 const PackedFile *pf = mainptr->curlib->packedfile;
5294
5295 BLO_reportf_wrap(basefd->reports,
5296 RPT_INFO,
5297 RPT_("Read packed library: '%s', parent '%s'"),
5298 mainptr->curlib->filepath,
5300 fd = blo_filedata_from_memory(pf->data, pf->size, basefd->reports);
5301
5302 /* Needed for library_append and read_libraries. */
5303 STRNCPY(fd->relabase, mainptr->curlib->runtime->filepath_abs);
5304 }
5305 else {
5306 /* Read file on disk. */
5307 BLO_reportf_wrap(basefd->reports,
5308 RPT_INFO,
5309 RPT_("Read library: '%s', '%s', parent '%s'"),
5310 mainptr->curlib->runtime->filepath_abs,
5311 mainptr->curlib->filepath,
5313 fd = blo_filedata_from_file(mainptr->curlib->runtime->filepath_abs, basefd->reports);
5314 }
5315
5316 if (fd) {
5317 /* Share the mainlist, so all libraries are added immediately in a
5318 * single list. It used to be that all FileData's had their own list,
5319 * but with indirectly linking this meant we didn't catch duplicate
5320 * libraries properly. */
5321 fd->mainlist = mainlist;
5322
5323 fd->reports = basefd->reports;
5324
5325 if (fd->libmap) {
5327 }
5328
5329 fd->libmap = oldnewmap_new();
5330
5331 mainptr->curlib->runtime->filedata = fd;
5332 mainptr->versionfile = fd->fileversion;
5333
5334 /* subversion */
5335 read_file_version(fd, mainptr);
5337 }
5338 else {
5339 mainptr->curlib->runtime->filedata = nullptr;
5340 mainptr->curlib->id.tag |= ID_TAG_MISSING;
5341 /* Set lib version to current main one... Makes assert later happy. */
5342 mainptr->versionfile = mainptr->curlib->runtime->versionfile = mainl->versionfile;
5343 mainptr->subversionfile = mainptr->curlib->runtime->subversionfile = mainl->subversionfile;
5344 }
5345
5346 if (fd == nullptr) {
5347 BLO_reportf_wrap(basefd->reports,
5348 RPT_INFO,
5349 RPT_("Cannot find lib '%s'"),
5350 mainptr->curlib->runtime->filepath_abs);
5351 basefd->reports->count.missing_libraries++;
5352 }
5353
5354 return fd;
5355}
5356
5363{
5364 Main *mainl = static_cast<Main *>(mainlist->first);
5365 for (Main *libmain = mainl->next; libmain; libmain = libmain->next) {
5366 for (ListBase *lb : BKE_main_lists_get(*libmain)) {
5367 LISTBASE_FOREACH_MUTABLE (ID *, id, lb) {
5369 continue;
5370 }
5371 change_link_placeholder_to_real_ID_pointer(mainlist, basefd, id, nullptr);
5372 BLI_remlink(lb, id);
5373 if (libmain->id_map != nullptr) {
5374 BKE_main_idmap_remove_id(libmain->id_map, id);
5375 }
5377 MEM_freeN(id);
5378 }
5379 }
5380 }
5381}
5382
5383static void read_libraries(FileData *basefd, ListBase *mainlist)
5384{
5385 Main *mainl = static_cast<Main *>(mainlist->first);
5386 bool do_it = true;
5387
5388 BLI_SCOPED_DEFER([&]() {
5389 if (mainl->is_read_invalid) {
5390 read_libraries_free_placeholders(basefd, mainlist);
5391 }
5392 });
5393
5394 /* At this point the base blend file has been read, and each library blend
5395 * encountered so far has a main with placeholders for linked data-blocks.
5396 *
5397 * Now we will read the library blend files and replace the placeholders
5398 * with actual data-blocks. We loop over library mains multiple times in
5399 * case a library needs to link additional data-blocks from another library
5400 * that had been read previously. */
5401 while (do_it) {
5402 do_it = false;
5403
5404 /* Loop over mains of all library blend files encountered so far. Note
5405 * this list gets longer as more indirectly library blends are found. */
5406 for (Main *mainptr = mainl->next; mainptr; mainptr = mainptr->next) {
5407 if (mainptr->is_read_invalid) [[unlikely]] {
5408 return;
5409 }
5410 /* Does this library have any more linked data-blocks we need to read? */
5411 if (has_linked_ids_to_read(mainptr)) {
5412 CLOG_INFO(&LOG,
5413 3,
5414 "Reading linked data-blocks from %s (%s)",
5415 mainptr->curlib->id.name,
5416 mainptr->curlib->filepath);
5417
5418 /* Open file if it has not been done yet. */
5419 FileData *fd = read_library_file_data(basefd, mainlist, mainl, mainptr);
5420
5421 if (fd) {
5422 if (!(fd->flags & FD_FLAGS_FILE_OK)) [[unlikely]] {
5423 if (!mainptr->is_read_invalid) {
5424 blo_readfile_invalidate(fd, mainptr, "Failed to open the library blendfile");
5425 }
5426 return;
5427 }
5428 do_it = true;
5429
5430 if (mainptr->id_map == nullptr) {
5431 mainptr->id_map = BKE_main_idmap_create(mainptr, false, nullptr, MAIN_IDMAP_TYPE_NAME);
5432 }
5433 }
5434
5435 /* Read linked data-blocks for each link placeholder, and replace
5436 * the placeholder with the real data-block. */
5437 read_library_linked_ids(basefd, fd, mainlist, mainptr);
5438 if (mainptr->is_read_invalid) [[unlikely]] {
5439 return;
5440 }
5441
5442 /* Test if linked data-blocks need to read further linked data-blocks
5443 * and create link placeholders for them. */
5445 if (mainptr->is_read_invalid) [[unlikely]] {
5446 return;
5447 }
5448 }
5449 }
5450 }
5451
5452 for (Main *mainptr = mainl->next; mainptr; mainptr = mainptr->next) {
5453 /* Drop weak links for which no data-block was found.
5454 * Since this can remap pointers in `libmap` of all libraries, it needs to be performed in its
5455 * own loop, before any call to `lib_link_all` (and the freeing of the libraries' filedata). */
5456 read_library_clear_weak_links(basefd, mainlist, mainptr);
5457 if (mainptr->is_read_invalid) [[unlikely]] {
5458 return;
5459 }
5460 }
5461
5462 Main *main_newid = BKE_main_new();
5463 for (Main *mainptr = mainl->next; mainptr; mainptr = mainptr->next) {
5464 /* Do versioning for newly added linked data-blocks. If no data-blocks
5465 * were read from a library versionfile will still be zero and we can
5466 * skip it. */
5467 if (mainptr->versionfile) {
5468 /* Split out already existing IDs to avoid them going through
5469 * do_versions multiple times, which would have bad consequences. */
5470 split_main_newid(mainptr, main_newid);
5471
5472 /* `filedata` can be NULL when loading linked data from nonexistent or invalid library
5473 * reference. Or during linking/appending, when processing data from a library not involved
5474 * in the current linking/appending operation.
5475 *
5476 * Skip versioning in these cases, since the only IDs here will be placeholders (missing
5477 * lib), or already existing IDs (linking/appending). */
5478 if (mainptr->curlib->runtime->filedata) {
5479 do_versions(mainptr->curlib->runtime->filedata, mainptr->curlib, main_newid);
5480 }
5481
5482 add_main_to_main(mainptr, main_newid);
5483 }
5484 if (mainptr->is_read_invalid) [[unlikely]] {
5485 BKE_main_free(main_newid);
5486 return;
5487 }
5488
5489 /* Lib linking. */
5490 if (mainptr->curlib->runtime->filedata) {
5491 lib_link_all(mainptr->curlib->runtime->filedata, mainptr);
5492 }
5493 if (mainptr->is_read_invalid) [[unlikely]] {
5494 BKE_main_free(main_newid);
5495 return;
5496 }
5497
5498 /* NOTE: No need to call #do_versions_after_linking() or #BKE_main_id_refcount_recompute()
5499 * here, as this function is only called for library 'subset' data handling, as part of
5500 * either full blend-file reading (#blo_read_file_internal()), or library-data linking
5501 * (#library_link_end()).
5502 *
5503 * For this to work reliably, `mainptr->curlib->runtime->filedata` also needs to be freed after
5504 * said versioning code has run. */
5505 }
5506 BKE_main_free(main_newid);
5507}
5508
5510 void *new_address,
5511 const void * /*old_address*/,
5512 const size_t expected_size)
5513{
5514 if (new_address != nullptr) {
5515 /* Not testing equality, since size might have been aligned up,
5516 * or might be passed the size of a base struct with inheritance. */
5517 if (MEM_allocN_len(new_address) < expected_size) {
5518 if (fd->mainlist) {
5520 static_cast<Main *>(fd->mainlist->last),
5521 "Corrupt .blend file, unexpected data size.");
5522 }
5523 /* Return null to trigger a hard-crash rather than allowing readfile code to further access
5524 * this invalid block of memory.
5525 *
5526 * It could also potentially allow the calling code to do its own error checking and abort
5527 * reading process, but that is not implemented currently. */
5528 return nullptr;
5529 }
5530 }
5531
5532 return new_address;
5533}
5534
5535void *BLO_read_get_new_data_address(BlendDataReader *reader, const void *old_address)
5536{
5537 return newdataadr(reader->fd, old_address);
5538}
5539
5541 const void *old_address,
5542 const size_t expected_size)
5543{
5544 void *new_address = newdataadr_no_us(reader->fd, old_address);
5545 return blo_verify_data_address(reader->fd, new_address, old_address, expected_size);
5546}
5547
5549 const void *old_address,
5550 const size_t expected_size)
5551{
5552 NewAddress *entry = newdataadr_entry(reader->fd, old_address);
5553 if (entry == nullptr) {
5554 return nullptr;
5555 }
5556 void *new_address = blo_verify_data_address(reader->fd, entry->newp, old_address, expected_size);
5557 if (new_address) {
5558 entry->nr++;
5559 }
5560 return new_address;
5561}
5562
5564 const char *struct_name,
5565 const int64_t items_num,
5566 const void *old_address)
5567{
5568 const int struct_index = DNA_struct_find_with_alias(reader->fd->memsdna, struct_name);
5569 BLI_assert(STREQ(DNA_struct_identifier(const_cast<SDNA *>(reader->fd->memsdna), struct_index),
5570 struct_name));
5571 const size_t struct_size = size_t(DNA_struct_size(reader->fd->memsdna, struct_index));
5572 return BLO_read_struct_array_with_size(reader, old_address, struct_size * items_num);
5573}
5574
5576 ID *self_id,
5577 const bool is_linked_only,
5578 ID *id)
5579{
5580 return static_cast<ID *>(newlibadr(reader->fd, self_id, is_linked_only, id));
5581}
5582
5584{
5585 return BKE_main_idmap_lookup_uid(reader->fd->new_idmap_uid, session_uid);
5586}
5587
5589{
5590 return reader->fd->fileversion;
5591}
5592
5594{
5595 return (reader->fd->flags & FD_FLAGS_SWITCH_ENDIAN) != 0;
5596}
5597
5599 const char *stype,
5600 const char *vartype,
5601 const char *name)
5602{
5603 return DNA_struct_member_offset_by_name_with_alias(reader->fd->filesdna, stype, vartype, name);
5604}
5605
5607 const size_t expected_elem_size,
5608 ListBase *list)
5609{
5610 if (BLI_listbase_is_empty(list)) {
5611 return;
5612 }
5613
5614 list->first = BLO_read_struct_array_with_size(reader, list->first, expected_elem_size);
5615 Link *ln = static_cast<Link *>(list->first);
5616 Link *prev = nullptr;
5617 while (ln) {
5618 ln->next = static_cast<Link *>(
5619 BLO_read_struct_array_with_size(reader, ln->next, expected_elem_size));
5620 ln->prev = prev;
5621 prev = ln;
5622 ln = ln->next;
5623 }
5624 list->last = prev;
5625}
5626
5632 NewAddress &entry,
5633 const void *old_address,
5634 blender::FunctionRef<size_t(const void *data)> type_size_fn)
5635{
5636 const size_t type_size = type_size_fn(entry.newp);
5637 if (blo_verify_data_address(fd, entry.newp, old_address, type_size)) {
5638 return true;
5639 }
5640 if (entry.nr == 0) {
5641 MEM_freeN(entry.newp);
5642 }
5643 fd->datamap->map.remove(old_address);
5644 return false;
5645}
5646
5648 BlendDataReader *reader,
5649 const size_t base_size,
5650 ListBase *list,
5651 blender::FunctionRef<size_t(const void *data)> type_size_fn)
5652{
5653 FileData *fd = reader->fd;
5654 const void *old_address = list->first;
5655 Link *prev = nullptr;
5656 list->first = nullptr;
5657
5658 while (old_address) {
5659 NewAddress *entry = newdataadr_entry(fd, old_address);
5660 if (entry == nullptr) {
5661 break;
5662 }
5663 Link *ln = static_cast<Link *>(
5664 blo_verify_data_address(fd, entry->newp, old_address, base_size));
5665 if (ln == nullptr) {
5666 break;
5667 }
5668
5669 /* Skip items too small for their type, continuing with the next one. */
5670 const void *old_address_item = old_address;
5671 old_address = ln->next;
5672 if (!blo_read_verify_type_size(fd, *entry, old_address_item, type_size_fn)) {
5673 continue;
5674 }
5675
5676 entry->nr++;
5677 ln->next = nullptr;
5678 ln->prev = prev;
5679 if (prev) {
5680 prev->next = ln;
5681 }
5682 else {
5683 list->first = ln;
5684 }
5685 prev = ln;
5686 }
5687 list->last = prev;
5688}
5689
5691 BlendDataReader *reader,
5692 const void *old_address,
5693 const size_t base_size,
5694 blender::FunctionRef<size_t(const void *data)> type_size_fn)
5695{
5696 FileData *fd = reader->fd;
5697 NewAddress *entry = newdataadr_entry(fd, old_address);
5698 if (entry == nullptr) {
5699 return nullptr;
5700 }
5701 void *new_address = blo_verify_data_address(fd, entry->newp, old_address, base_size);
5702 if (new_address == nullptr || !blo_read_verify_type_size(fd, *entry, old_address, type_size_fn))
5703 {
5704 return nullptr;
5705 }
5706 entry->nr++;
5707 return new_address;
5708}
5709
5710void BLO_read_char_array(BlendDataReader *reader, const int64_t array_size, char **ptr_p)
5711{
5712 *ptr_p = reinterpret_cast<char *>(
5713 BLO_read_struct_array_with_size(reader, *((void **)ptr_p), sizeof(char) * array_size));
5714}
5715
5716void BLO_read_uint8_array(BlendDataReader *reader, const int64_t array_size, uint8_t **ptr_p)
5717{
5718 *ptr_p = reinterpret_cast<uint8_t *>(
5719 BLO_read_struct_array_with_size(reader, *((void **)ptr_p), sizeof(uint8_t) * array_size));
5720}
5721
5722void BLO_read_int8_array(BlendDataReader *reader, const int64_t array_size, int8_t **ptr_p)
5723{
5724 *ptr_p = reinterpret_cast<int8_t *>(
5725 BLO_read_struct_array_with_size(reader, *((void **)ptr_p), sizeof(int8_t) * array_size));
5726}
5727
5728void BLO_read_int16_array(BlendDataReader *reader, const int64_t array_size, int16_t **ptr_p)
5729{
5730 *ptr_p = reinterpret_cast<int16_t *>(
5731 BLO_read_struct_array_with_size(reader, *((void **)ptr_p), sizeof(int16_t) * array_size));
5732
5733 if (*ptr_p && BLO_read_requires_endian_switch(reader)) {
5734 BLI_endian_switch_int16_array(*ptr_p, array_size);
5735 }
5736}
5737
5738void BLO_read_int32_array(BlendDataReader *reader, const int64_t array_size, int32_t **ptr_p)
5739{
5740 *ptr_p = reinterpret_cast<int32_t *>(
5741 BLO_read_struct_array_with_size(reader, *((void **)ptr_p), sizeof(int32_t) * array_size));
5742
5743 if (*ptr_p && BLO_read_requires_endian_switch(reader)) {
5744 BLI_endian_switch_int32_array(*ptr_p, array_size);
5745 }
5746}
5747
5748void BLO_read_uint32_array(BlendDataReader *reader, const int64_t array_size, uint32_t **ptr_p)
5749{
5750 *ptr_p = reinterpret_cast<uint32_t *>(
5751 BLO_read_struct_array_with_size(reader, *((void **)ptr_p), sizeof(uint32_t) * array_size));
5752
5753 if (*ptr_p && BLO_read_requires_endian_switch(reader)) {
5754 BLI_endian_switch_uint32_array(*ptr_p, array_size);
5755 }
5756}
5757
5758void BLO_read_float_array(BlendDataReader *reader, const int64_t array_size, float **ptr_p)
5759{
5760 *ptr_p = reinterpret_cast<float *>(
5761 BLO_read_struct_array_with_size(reader, *((void **)ptr_p), sizeof(float) * array_size));
5762
5763 if (*ptr_p && BLO_read_requires_endian_switch(reader)) {
5764 BLI_endian_switch_float_array(*ptr_p, array_size);
5765 }
5766}
5767
5768void BLO_read_float3_array(BlendDataReader *reader, const int64_t array_size, float **ptr_p)
5769{
5770 BLO_read_float_array(reader, array_size * 3, ptr_p);
5771}
5772
5773void BLO_read_double_array(BlendDataReader *reader, const int64_t array_size, double **ptr_p)
5774{
5775 *ptr_p = reinterpret_cast<double *>(
5776 BLO_read_struct_array_with_size(reader, *((void **)ptr_p), sizeof(double) * array_size));
5777
5778 if (*ptr_p && BLO_read_requires_endian_switch(reader)) {
5779 BLI_endian_switch_double_array(*ptr_p, array_size);
5780 }
5781}
5782
5783void BLO_read_string(BlendDataReader *reader, char **ptr_p)
5784{
5785 BLO_read_data_address(reader, ptr_p);
5786
5787#ifndef NDEBUG
5788 const char *str = *ptr_p;
5789 if (str) {
5790 /* Verify that we have a null terminator. */
5791 for (size_t len = MEM_allocN_len(str); len > 0; len--) {
5792 if (str[len - 1] == '\0') {
5793 return;
5794 }
5795 }
5796
5797 BLI_assert_msg(0, "Corrupt .blend file, expected string to be null terminated.");
5798 }
5799#endif
5800}
5801
5802void BLO_read_string(BlendDataReader *reader, char *const *ptr_p)
5803{
5804 BLO_read_string(reader, const_cast<char **>(ptr_p));
5805}
5806
5807void BLO_read_string(BlendDataReader *reader, const char **ptr_p)
5808{
5809 BLO_read_string(reader, const_cast<char **>(ptr_p));
5810}
5811
5813 const int64_t array_size,
5814 const uint64_t *src,
5815 uint32_t *dst)
5816{
5817 const bool use_endian_swap = BLO_read_requires_endian_switch(reader);
5818 for (int i = 0; i < array_size; i++) {
5819 dst[i] = uint32_from_uint64_ptr(src[i], use_endian_swap);
5820 }
5821}
5822
5824 const int64_t array_size,
5825 const uint32_t *src,
5826 uint64_t *dst)
5827{
5828 /* Match pointer conversion rules from bh8_from_bh4 and cast_pointer_32_to_64. */
5829 for (int i = 0; i < array_size; i++) {
5830 dst[i] = src[i];
5831 }
5832}
5833
5834void BLO_read_pointer_array(BlendDataReader *reader, const int64_t array_size, void **ptr_p)
5835{
5836 FileData *fd = reader->fd;
5837
5838 void *orig_array = newdataadr(fd, *ptr_p);
5839 if (orig_array == nullptr) {
5840 *ptr_p = nullptr;
5841 return;
5842 }
5843
5844 int file_pointer_size = fd->filesdna->pointer_size;
5845 int current_pointer_size = fd->memsdna->pointer_size;
5846
5847 void *final_array = nullptr;
5848
5849 if (file_pointer_size == current_pointer_size) {
5850 /* No pointer conversion necessary. */
5851 final_array = orig_array;
5852 }
5853 else if (file_pointer_size == 8 && current_pointer_size == 4) {
5854 /* Convert pointers from 64 to 32 bit. */
5855 final_array = MEM_malloc_arrayN(array_size, 4, "new pointer array");
5857 reader, array_size, (uint64_t *)orig_array, (uint32_t *)final_array);
5858 MEM_freeN(orig_array);
5859 }
5860 else if (file_pointer_size == 4 && current_pointer_size == 8) {
5861 /* Convert pointers from 32 to 64 bit. */
5862 final_array = MEM_malloc_arrayN(array_size, 8, "new pointer array");
5864 reader, array_size, (uint32_t *)orig_array, (uint64_t *)final_array);
5865 MEM_freeN(orig_array);
5866 }
5867 else {
5869 }
5870
5871 *ptr_p = final_array;
5872}
5873
5875 BlendDataReader *reader,
5876 const void **ptr_p,
5877 const blender::FunctionRef<const blender::ImplicitSharingInfo *()> read_fn)
5878{
5879 const void *old_address = *ptr_p;
5880 if (BLO_read_data_is_undo(reader)) {
5881 if (reader->fd->flags & FD_FLAGS_IS_MEMFILE) {
5882 UndoReader *undo_reader = reinterpret_cast<UndoReader *>(reader->fd->file);
5883 const MemFile &memfile = *undo_reader->memfile;
5884 if (memfile.shared_storage) {
5885 /* Check if the data was saved with sharing-info. */
5886 if (const blender::ImplicitSharingInfo *sharing_info =
5887 memfile.shared_storage->map.lookup_default(old_address, nullptr))
5888 {
5889 /* Add a new owner of the data that is passed to the caller. */
5890 sharing_info->add_user();
5891 return {sharing_info, old_address};
5892 }
5893 }
5894 }
5895 }
5896
5897 if (const blender::ImplicitSharingInfoAndData *shared_data =
5898 reader->shared_data_by_stored_address.lookup_ptr(old_address))
5899 {
5900 /* The data was loaded before. No need to load it again. Just increase the user count to
5901 * indicate that it is shared. */
5902 if (shared_data->sharing_info) {
5903 shared_data->sharing_info->add_user();
5904 }
5905 return *shared_data;
5906 }
5907
5908 /* This is the first time this data is loaded. The callback also creates the corresponding
5909 * sharing info which may be reused later. */
5910 const blender::ImplicitSharingInfo *sharing_info = read_fn();
5911 const void *new_address = *ptr_p;
5912 const blender::ImplicitSharingInfoAndData shared_data{sharing_info, new_address};
5913 reader->shared_data_by_stored_address.add(old_address, shared_data);
5914 return shared_data;
5915}
5916
5918{
5919 return (reader->fd->flags & FD_FLAGS_IS_MEMFILE);
5920}
5921
5922void BLO_read_data_globmap_add(BlendDataReader *reader, void *oldaddr, void *newaddr)
5923{
5924 oldnewmap_insert(reader->fd->globmap, oldaddr, newaddr, 0);
5925}
5926
5928{
5929 link_glob_list(reader->fd, list);
5930}
5931
5933{
5934 return reader->fd->reports;
5935}
5936
5937void BLO_read_data_invalidate(BlendDataReader *reader, const char *message)
5938{
5939 FileData *fd = reader->fd;
5940 if (fd->mainlist) {
5941 blo_readfile_invalidate(fd, static_cast<Main *>(fd->mainlist->last), message);
5942 }
5943}
5944
5946{
5947 return (reader->fd->flags & FD_FLAGS_IS_MEMFILE);
5948}
5949
5951{
5952 return reader->main;
5953}
5954
5956{
5957 return reader->fd->reports;
5958}
5959
AnimData * BKE_animdata_from_id(const ID *id)
Definition anim_data.cc:82
void BKE_animdata_blend_read_data(BlendDataReader *reader, ID *id)
void BKE_asset_metadata_read(BlendDataReader *reader, AssetMetaData *asset_data)
Definition asset.cc:215
void BKE_asset_catalog_path_list_blend_read_data(BlendDataReader *reader, ListBase &catalog_path_list)
#define BLENDER_FILE_SUBVERSION
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:153
#define BLENDER_FILE_VERSION
void BKE_collections_after_lib_link(Main *bmain)
void BKE_collection_blend_read_data(BlendDataReader *reader, Collection *collection, ID *owner_id)
void BKE_main_collections_parent_relations_rebuild(Main *bmain)
@ G_FILE_ASSET_EDIT_FILE
@ G_FILE_RECOVER_READ
@ G_DEBUG
#define IDP_BlendDataRead(reader, prop)
const IDTypeInfo * BKE_idtype_get_info_from_idtype_index(const int idtype_index)
Definition idtype.cc:128
const IDTypeInfo * BKE_idtype_get_info_from_id(const ID *id)
Definition idtype.cc:147
bool BKE_idtype_cache_key_cmp(const void *key_a_v, const void *key_b_v)
Definition idtype.cc:36
const IDTypeInfo * BKE_idtype_get_info_from_idcode(short id_code)
Definition idtype.cc:142
const char * BKE_idtype_idcode_to_name(short idcode)
Definition idtype.cc:165
bool BKE_idtype_idcode_is_linkable(short idcode)
Definition idtype.cc:198
void BKE_idtype_id_foreach_cache(ID *id, IDTypeForeachCacheFunctionCallback function_callback, void *user_data)
Definition idtype.cc:382
int BKE_idtype_idcode_to_index(short idcode)
Definition idtype.cc:229
@ IDTYPE_FLAGS_NO_MEMFILE_UNDO
Definition BKE_idtype.hh:55
uint BKE_idtype_cache_key_hash(const void *key_v)
Definition idtype.cc:28
bool BKE_idtype_idcode_is_valid(short idcode)
Definition idtype.cc:193
@ IDTYPE_CACHE_CB_FLAGS_PERSISTENT
void BKE_layer_collection_resync_forbid()
void BKE_layer_collection_resync_allow()
ViewLayer * BKE_view_layer_find(const Scene *scene, const char *layer_name)
void BKE_id_delete(Main *bmain, void *idv) ATTR_NONNULL()
IDNewNameResult BKE_id_new_name_validate(Main &bmain, ListBase &lb, ID &id, const char *newname, IDNewNameMode mode, bool do_linked_data)
Definition lib_id.cc:1883
void id_sort_by_name(ListBase *lb, ID *id, ID *id_sorting_hint)
Definition lib_id.cc:1770
#define MAIN_ID_SESSION_UID_UNSET
@ LIB_ID_FREE_NO_UI_USER
@ LIB_ID_FREE_NO_DEG_TAG
@ LIB_ID_FREE_NO_USER_REFCOUNT
@ LIB_ID_FREE_NO_MAIN
void BKE_libblock_init_empty(ID *id) ATTR_NONNULL(1)
Definition lib_id.cc:1433
void BKE_lib_libblock_session_uid_ensure(ID *id)
Definition lib_id.cc:1458
void id_us_ensure_real(ID *id)
Definition lib_id.cc:308
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:1052
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1495
const char * BKE_id_name(const ID &id)
void BKE_id_free_ex(Main *bmain, void *idv, int flag_orig, bool use_flag_from_idtag)
void BKE_main_id_refcount_recompute(Main *bmain, bool do_linked_only)
Definition lib_id.cc:2015
ID * BKE_libblock_alloc_notest(short type) ATTR_WARN_UNUSED_RESULT
Definition lib_id.cc:1323
void BKE_main_id_tag_all(Main *mainvar, int tag, bool value)
Definition lib_id.cc:1214
void BKE_lib_override_library_main_validate(Main *bmain, ReportList *reports)
void BKE_lib_override_library_main_update(Main *bmain)
@ IDWALK_RET_NOP
@ IDWALK_CB_LOOPBACK
@ IDWALK_CB_EMBEDDED_NOT_OWNING
@ IDWALK_CB_EMBEDDED
@ IDWALK_CB_READFILE_IGNORE
void BKE_library_foreach_ID_link(Main *bmain, ID *id, blender::FunctionRef< LibraryIDLinkCallback > callback, void *user_data, LibraryForeachIDFlag flag)
Definition lib_query.cc:431
LibraryForeachIDFlag
@ IDWALK_DO_DEPRECATED_POINTERS
@ IDWALK_INCLUDE_UI
@ IDWALK_NOP
@ IDWALK_READONLY
@ IDWALK_NO_ORIG_POINTERS_ACCESS
@ ID_REMAP_SKIP_USER_CLEAR
@ ID_REMAP_SKIP_USER_REFCOUNT
@ ID_REMAP_SKIP_NEVER_NULL_USAGE
@ ID_REMAP_SKIP_UPDATE_TAGGING
@ ID_REMAP_NO_ORIG_POINTERS_ACCESS
@ LIBRARY_IS_ASSET_EDIT_FILE
#define FOREACH_MAIN_ID_END
Definition BKE_main.hh:563
MainListsArray BKE_main_lists_get(Main &bmain)
Definition main.cc:974
ListBase * which_libbase(Main *bmain, short type)
Definition main.cc:887
#define FOREACH_MAIN_LISTBASE_ID_END
Definition BKE_main.hh:532
std::array< ListBase *, INDEX_ID_MAX - 1 > MainListsArray
Definition BKE_main.hh:621
#define FOREACH_MAIN_LISTBASE_ID_BEGIN(_lb, _id)
Definition BKE_main.hh:526
#define FOREACH_MAIN_LISTBASE_END
Definition BKE_main.hh:544
Main * BKE_main_new()
Definition main.cc:48
#define FOREACH_MAIN_LISTBASE_BEGIN(_bmain, _lb)
Definition BKE_main.hh:537
void BKE_main_free(Main *bmain)
Definition main.cc:175
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition BKE_main.hh:557
#define MAIN_VERSION_FILE_OLDER_OR_EQUAL(main, ver, subver)
Definition BKE_main.hh:642
#define BLEN_THUMB_MEMSIZE(_x, _y)
Definition BKE_main.hh:659
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:877
#define BLEN_THUMB_MEMSIZE_IS_VALID(_x, _y)
Definition BKE_main.hh:662
bool BKE_main_is_empty(Main *bmain)
Definition main.cc:462
const char * BKE_main_blendfile_path_from_global()
Definition main.cc:882
ID ID ID * BKE_main_idmap_lookup_uid(IDNameLib_Map *id_map, uint session_uid) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Main * BKE_main_idmap_main_get(IDNameLib_Map *id_map) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
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:77
void BKE_main_idmap_remove_id(IDNameLib_Map *id_map, const ID *id) ATTR_NONNULL()
void BKE_main_idmap_destroy(IDNameLib_Map *id_map) ATTR_NONNULL()
void BKE_main_idmap_insert_id(IDNameLib_Map *id_map, ID *id) ATTR_NONNULL()
@ MAIN_IDMAP_TYPE_UID
@ MAIN_IDMAP_TYPE_NAME
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 BKE_main_ensure_invariants(Main &bmain, std::optional< blender::Span< ID * > > modified_ids=std::nullopt)
bool BKE_main_namemap_validate_and_fix(Main &bmain)
void BKE_main_namemap_clear(Main &bmain)
General operations, lookup, etc. for materials.
void BKE_object_materials_sync_length(Main *bmain, Object *ob, ID *id)
General operations, lookup, etc. for blender objects.
void BKE_packedfile_blend_read(BlendDataReader *reader, PackedFile **pf_p, blender::StringRefNull filepath)
void BKE_preferences_extension_repo_read_data(struct BlendDataReader *reader, bUserExtensionRepo *repo)
bool BKE_reports_print_test(const ReportList *reports, eReportType type)
Definition report.cc:298
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
const char * BKE_report_type_str(eReportType type)
Definition report.cc:29
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:126
bool BKE_screen_blend_read_data(BlendDataReader *reader, bScreen *screen)
Definition screen.cc:136
@ STEP_UNDO
@ STEP_REDO
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_STATIC_ASSERT(a, msg)
Definition BLI_assert.h:83
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
#define ATTR_FALLTHROUGH
#define L_ENDIAN
#define ENDIAN_ORDER
void BLI_endian_switch_float_array(float *val, int size) ATTR_NONNULL(1)
BLI_INLINE void BLI_endian_switch_int32(int *val) ATTR_NONNULL(1)
void BLI_endian_switch_int32_array(int *val, int size) ATTR_NONNULL(1)
void BLI_endian_switch_uint32_array(unsigned int *val, int size) ATTR_NONNULL(1)
void BLI_endian_switch_int16_array(short *val, int size) ATTR_NONNULL(1)
void BLI_endian_switch_double_array(double *val, int size) ATTR_NONNULL(1)
File and directory operations.
#define O_BINARY
bool BLI_file_magic_is_gzip(const char header[4])
Definition fileops_c.cc:257
bool BLI_file_magic_is_zstd(const char header[4])
Definition fileops_c.cc:264
int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
FileReader * BLI_filereader_new_mmap(int filedes) ATTR_WARN_UNUSED_RESULT
FileReader * BLI_filereader_new_zstd(FileReader *base) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
FileReader * BLI_filereader_new_file(int filedes) ATTR_WARN_UNUSED_RESULT
FileReader * BLI_filereader_new_gzip(FileReader *base) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
FileReader * BLI_filereader_new_memory(const void *data, size_t len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
bool BLI_ghash_haskey(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:819
GHash * BLI_ghash_new(GHashHashFP hashfp, GHashCmpFP cmpfp, const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:686
void * BLI_ghash_lookup(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:731
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition BLI_ghash.cc:707
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition BLI_ghash.cc:860
void void void BLI_movelisttolist(ListBase *dst, ListBase *src) ATTR_NONNULL(1
#define LISTBASE_FOREACH(type, var, list)
BLI_INLINE void BLI_listbase_clear(ListBase *lb)
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:608
BLI_INLINE bool BLI_listbase_is_empty(const ListBase *lb)
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:270
#define LISTBASE_FOREACH_MUTABLE(type, var, list)
void void BLI_freelistN(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:497
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:111
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:131
bool BLI_remlink_safe(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:154
int BLI_listbase_count(const ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:524
void void BLI_INLINE bool BLI_listbase_is_single(const ListBase *lb)
#define BLI_MEMARENA_STD_BUFSIZE
MemArena * BLI_memarena_new(size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(2) ATTR_MALLOC
void BLI_memarena_free(MemArena *ma) ATTR_NONNULL(1)
void * BLI_memarena_alloc(MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
ATTR_WARN_UNUSED_RESULT const size_t num
#define BLI_SCOPED_DEFER(function_to_defer)
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
#define FILE_MAX
int BLI_path_normalize(char *path) ATTR_NONNULL(1)
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool void BLI_path_rel(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1)
#define BLI_path_cmp
int key
std::string data
char * STRNCPY(char(&dst)[N], const char *src)
Definition BLI_string.h:688
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
size_t size_t size_t bool BLI_str_utf8_truncate_at_size(char *str, const size_t str_size)
size_t void BLI_uniquename_cb(blender::FunctionRef< bool(blender::StringRefNull)> unique_check, const char *defname, char delim, char *name, size_t name_maxncpy) ATTR_NONNULL(2
unsigned int uint
Platform independent time functions.
double BLI_time_now_seconds(void)
Definition time.cc:65
#define UNUSED_VARS(...)
#define UNUSED_VARS_NDEBUG(...)
#define SET_FLAG_FROM_TEST(value, test, flag)
#define UNLIKELY(x)
#define POINTER_OFFSET(v, ofs)
#define STREQ(a, b)
#define LIKELY(x)
Compatibility-like things for windows.
Utilities ensuring .blend file (i.e. Main) is in valid state during write and/or read process.
void BLO_main_validate_embedded_flag(Main *bmain, ReportList *reports)
bool BLO_main_validate_shapekeys(Main *bmain, ReportList *reports)
void BLO_main_validate_embedded_liboverrides(Main *bmain, ReportList *reports)
uint32_t uint32_from_uint64_ptr(uint64_t ptr, const bool use_endian_swap)
@ BLO_CODE_ENDB
@ BLO_CODE_REND
@ BLO_CODE_TEST
@ BLO_CODE_GLOB
@ BLO_CODE_DATA
@ BLO_CODE_DNA1
@ BLO_CODE_USER
std::optional< BHead > BLO_readfile_read_bhead(FileReader *file, BHeadType type, bool do_endian_swap)
BlenderHeaderVariant BLO_readfile_blender_header_decode(FileReader *file)
#define MIN_SIZEOFBLENDERHEADER
std::variant< BlenderHeaderInvalid, BlenderHeaderUnknown, BlenderHeader > BlenderHeaderVariant
#define BLO_read_data_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.
void(*)(void *fdhandle, Main *mainvar, void *idv) BLOExpandDoitCallback
@ BLO_LIBLINK_USE_PLACEHOLDERS
@ BLO_LIBLINK_COLLECTION_NO_HIERARCHY_REBUILD
@ BLO_LIBLINK_FORCE_INDIRECT
@ BLO_READ_SKIP_DATA
@ BLO_READ_SKIP_USERDEF
@ BLO_READ_SKIP_UNDO_OLD_MAIN
#define BLEN_THUMB_MEMSIZE_FILE(_x, _y)
@ BLENFILETYPE_BLEND
FileReader * BLO_memfile_new_filereader(MemFile *memfile, int undo_direction)
Definition undofile.cc:265
#define RPT_(msgid)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:182
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:181
#define CLOG_INFO(clg_ref, level,...)
Definition CLG_log.h:179
@ ID_FLAG_INDIRECT_WEAK_LINK
Definition DNA_ID.h:693
#define ID_FAKE_USERS(id)
Definition DNA_ID.h:578
@ ID_RECALC_SOURCE
Definition DNA_ID.h:1051
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1026
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:982
@ ID_RECALC_ALL
Definition DNA_ID.h:1096
@ ID_TAG_UNDO_OLD_ID_REUSED_UNCHANGED
Definition DNA_ID.h:844
@ ID_TAG_NEW
Definition DNA_ID.h:827
@ ID_TAG_UNDO_OLD_ID_REUSED_NOUNDO
Definition DNA_ID.h:855
@ ID_TAG_TEMP_MAIN
Definition DNA_ID.h:879
@ ID_TAG_INDIRECT
Definition DNA_ID.h:756
@ ID_TAG_EXTERN
Definition DNA_ID.h:750
@ ID_TAG_MISSING
Definition DNA_ID.h:775
@ ID_TAG_LOCAL
Definition DNA_ID.h:744
@ ID_TAG_UNDO_OLD_ID_REREAD_IN_PLACE
Definition DNA_ID.h:862
@ ID_TAG_LIBOVERRIDE_NEED_RESYNC
Definition DNA_ID.h:814
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:615
#define MAX_ID_NAME
Definition DNA_ID.h:363
struct ID ID
#define INDEX_ID_MAX
Definition DNA_ID.h:1269
@ INDEX_ID_NULL
Definition DNA_ID.h:1266
#define ID_TAG_KEEP_ON_UNDO
Definition DNA_ID.h:954
@ ID_WM
@ ID_LI
@ ID_SCE
@ ID_AC
@ ID_SCR
@ ID_OB
#define ID_SCRN
#define ID_LINK_PLACEHOLDER
Object groups, one object can be in many groups at once.
@ CONSTRAINT_TYPE_ACTION
blenloader genfile private function prototypes
const char * DNA_struct_get_compareflags(const struct SDNA *oldsdna, const struct SDNA *newsdna)
void DNA_sdna_free(struct SDNA *sdna)
const struct SDNA * DNA_sdna_current_get(void)
int DNA_struct_find_with_alias(const struct SDNA *sdna, const char *str)
int DNA_struct_member_offset_by_name_with_alias(const struct SDNA *sdna, const char *stype, const char *vartype, const char *name)
int DNA_struct_size(const struct SDNA *sdna, int struct_index)
const char * DNA_struct_identifier(struct SDNA *sdna, int struct_index)
void * DNA_struct_reconstruct(const struct DNA_ReconstructInfo *reconstruct_info, int old_struct_index, int blocks, const void *old_blocks, const char *alloc_name)
struct DNA_ReconstructInfo * DNA_reconstruct_info_create(const struct SDNA *oldsdna, const struct SDNA *newsdna, const char *compare_flags)
struct SDNA * DNA_sdna_from_data(const void *data, int data_len, bool do_endian_swap, bool data_alloc, bool do_alias, const char **r_error_message)
void DNA_struct_switch_endian(const struct SDNA *sdna, int struct_index, char *data)
void DNA_reconstruct_info_free(struct DNA_ReconstructInfo *reconstruct_info)
int DNA_struct_alignment(const struct SDNA *sdna, int struct_index)
@ SDNA_CMP_REMOVED
Definition DNA_genfile.h:63
@ SDNA_CMP_NOT_EQUAL
Definition DNA_genfile.h:69
void DNA_sdna_alias_data_ensure_structs_map(struct SDNA *sdna)
@ SCE_READFILE_LIBLINK_NEED_SETSCENE_CHECK
#define SDNA_RAW_DATA_STRUCT_INDEX
@ FILE_RELPATH
@ USER_MENU_TYPE_OPERATOR
@ USER_MENU_TYPE_SEP
@ USER_MENU_TYPE_PROP
@ USER_MENU_TYPE_MENU
struct bUserMenuItem_Op bUserMenuItem_Op
struct bUserMenuItem_Prop bUserMenuItem_Prop
struct bUserMenuItem_Menu bUserMenuItem_Menu
struct bUserMenuItem bUserMenuItem
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
void clear()
Definition BLI_map.hh:1038
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
bool add_overwrite(const Key &key, const Value &value)
Definition BLI_map.hh:325
ValueIterator values() const &
Definition BLI_map.hh:884
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
bool remove(const Key &key)
Definition BLI_map.hh:368
bool contains(const Key &key) const
Definition BLI_set.hh:310
void add_new(const Key &key)
Definition BLI_set.hh:233
const char * insert(const keyT &key, std::string alloc_string)
std::string id_name(void *id)
StackEntry * from
FILE * file
Scene scene
DEGForeachIDComponentCallback callback
#define offsetof(t, d)
#define str(s)
#define GS(x)
StringRefNull name
#define pf(_x, _i)
Prefetch 64.
Definition gim_memory.h:48
unsigned int uint32_t
#define main()
#define printf(...)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
format
void * MEM_mallocN(size_t len, const char *str)
Definition mallocn.cc:128
void * MEM_mallocN_aligned(size_t len, size_t alignment, const char *str)
Definition mallocn.cc:138
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:133
size_t(* MEM_allocN_len)(const void *vmemh)
Definition mallocn.cc:36
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
#define G(x, y, z)
bool foreach_strip_adt(const AnimData &adt, blender::FunctionRef< bool(NlaStrip *)> callback)
void node_tree_blend_read_data(BlendDataReader *reader, ID *owner_id, bNodeTree *ntree)
Definition node.cc:2146
void node_tree_update_all_new(Main &main)
Definition node.cc:5190
bNodeTree ** node_tree_ptr_from_id(ID *id)
Definition node.cc:4816
bNodeTree * node_tree_from_id(ID *id)
Definition node.cc:4840
AllocStringStorage< keyT, hashT > & alloc_string_storage_get(const std::string &storage_identifier)
void BLO_read_struct_list_with_type_size(BlendDataReader *reader, const size_t base_size, ListBase *list, blender::FunctionRef< size_t(const void *data)> type_size_fn)
Definition readfile.cc:5647
static int lib_link_cb(LibraryIDLinkCallbackData *cb_data)
Definition readfile.cc:3627
static BHead * find_bhead(FileData *fd, void *old)
Definition readfile.cc:4447
static void * newdataadr_no_us(FileData *fd, const void *adr)
Definition readfile.cc:1568
static void read_libraries(FileData *basefd, ListBase *mainlist)
Definition readfile.cc:5383
static void long_id_names_ensure_unique_id_names(Main *bmain)
Definition readfile.cc:1014
void * blo_do_versions_newlibadr(FileData *fd, ID *self_id, const bool is_linked_only, const void *adr)
Definition readfile.cc:1593
static void lib_link_scenes_check_set(Main *bmain)
Definition readfile.cc:2579
void BLO_read_struct_list_with_size(BlendDataReader *reader, const size_t expected_elem_size, ListBase *list)
Definition readfile.cc:5606
static bool read_libblock_undo_restore_library(FileData *fd, Main *new_main, const ID *id, ID *id_old, BHead *bhead)
Definition readfile.cc:2927
static void link_glob_list(FileData *fd, ListBase *lb)
Definition readfile.cc:2115
static ID * read_id_struct(FileData *fd, BHead *bh, const char *blockname, const int id_type_index)
Definition readfile.cc:2069
static ID * create_placeholder(Main *mainvar, const short idcode, const char *idname, const int tag, const bool was_liboverride)
Definition readfile.cc:2712
static void lib_link_all(FileData *fd, Main *bmain)
Definition readfile.cc:3655
void blo_cache_storage_end(FileData *fd)
Definition readfile.cc:1830
static void long_id_names_process_action_slots_identifiers(Main *bmain)
Definition readfile.cc:1056
static FileData * filedata_new(BlendFileReadReport *reports)
Definition readfile.cc:1174
void BLO_read_int32_array(BlendDataReader *reader, const int64_t array_size, int32_t **ptr_p)
Definition readfile.cc:5738
BlendThumbnail * BLO_thumbnail_from_file(const char *filepath)
Definition readfile.cc:1517
static void direct_link_library(FileData *fd, Library *lib, Main *main)
Definition readfile.cc:2611
int BLO_read_struct_member_offset(const BlendDataReader *reader, const char *stype, const char *vartype, const char *name)
Definition readfile.cc:5598
static void read_file_bhead_idname_map_create(FileData *fd)
Definition readfile.cc:516
static int direct_link_id_restore_recalc_exceptions(const ID *id_current)
Definition readfile.cc:2290
void BLO_read_uint8_array(BlendDataReader *reader, const int64_t array_size, uint8_t **ptr_p)
Definition readfile.cc:5716
static void convert_pointer_array_32_to_64(BlendDataReader *, const int64_t array_size, const uint32_t *src, uint64_t *dst)
Definition readfile.cc:5823
const char * blo_bhead_id_name(FileData *fd, const BHead *bhead)
Definition readfile.cc:855
void BLO_library_link_params_init(LibraryLink_Params *params, Main *bmain, const int flag, const int id_tag_extra)
Definition readfile.cc:4867
static void split_main_newid(Main *mainptr, Main *main_newid)
Definition readfile.cc:4903
void BLO_read_float_array(BlendDataReader *reader, const int64_t array_size, float **ptr_p)
Definition readfile.cc:5758
static void link_global(FileData *fd, BlendFileData *bfd)
Definition readfile.cc:3450
static BHead * read_global(BlendFileData *bfd, FileData *fd, BHead *bhead)
Definition readfile.cc:3395
int BLO_read_fileversion_get(BlendDataReader *reader)
Definition readfile.cc:5588
BHead * blo_bhead_next(FileData *fd, BHead *thisblock)
Definition readfile.cc:760
void BLO_read_uint32_array(BlendDataReader *reader, const int64_t array_size, uint32_t **ptr_p)
Definition readfile.cc:5748
blender::ImplicitSharingInfoAndData blo_read_shared_impl(BlendDataReader *reader, const void **ptr_p, const blender::FunctionRef< const blender::ImplicitSharingInfo *()> read_fn)
Definition readfile.cc:5874
static void read_undo_reuse_noundo_local_ids(FileData *fd)
Definition readfile.cc:2869
BlendFileReadReport * BLO_read_data_reports(BlendDataReader *reader)
Definition readfile.cc:5932
BHead * blo_bhead_first(FileData *fd)
Definition readfile.cc:732
static bool blo_read_verify_type_size(FileData *fd, NewAddress &entry, const void *old_address, blender::FunctionRef< size_t(const void *data)> type_size_fn)
Definition readfile.cc:5631
static bool read_libblock_is_identical(FileData *fd, BHead *bhead)
Definition readfile.cc:2841
static void change_ID_pointer_to_real_ID_pointer_fd(FileData *fd, const void *old, void *newp)
Definition readfile.cc:1620
Main * BLO_read_lib_get_main(BlendLibReader *reader)
Definition readfile.cc:5950
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:2301
static bool blo_bhead_is_id(const BHead *bhead)
Definition readfile.cc:499
static void direct_link_id_override_property(BlendDataReader *reader, IDOverrideLibraryProperty *op)
Definition readfile.cc:2209
static const char * library_parent_filepath(Library *lib)
Definition readfile.cc:246
static void change_ID_pointer_to_real_ID_pointer(ListBase *mainlist, FileData *basefd, void *old, void *newp)
Definition readfile.cc:1656
static void direct_link_id_common(BlendDataReader *reader, Library *current_library, ID *id, ID *id_old, int id_tag, ID_Readfile_Data::Tags id_read_tags)
Definition readfile.cc:2397
static void direct_link_id_embedded_id(BlendDataReader *reader, Library *current_library, ID *id, ID *id_old)
Definition readfile.cc:2233
static bool oldnewmap_insert(OldNewMap *onm, const void *oldaddr, void *newaddr, const int nr)
Definition readfile.cc:277
void BLO_read_glob_list(BlendDataReader *reader, ListBase *list)
Definition readfile.cc:5927
static int has_linked_ids_to_read(Main *mainvar)
Definition readfile.cc:5100
static BHead * blo_bhead_read_full(FileData *fd, BHead *thisblock)
Definition readfile.cc:814
static void do_versions_userdef(FileData *, BlendFileData *bfd)
Definition readfile.cc:3473
static void split_libdata(ListBase *lb_src, Main **lib_main_array, const uint lib_main_array_len)
Definition readfile.cc:389
static void library_link_end(Main *mainl, FileData **fd, const int flag, ReportList *reports)
Definition readfile.cc:4926
void blo_do_versions_oldnewmap_insert(OldNewMap *onm, const void *oldaddr, void *newaddr, const int nr)
Definition readfile.cc:291
static void add_main_to_main(Main *mainvar, Main *from)
Definition readfile.cc:350
static OldNewMap * oldnewmap_new()
Definition readfile.cc:268
static void after_liblink_id_process(BlendLibReader *reader, ID *id)
Definition readfile.cc:2196
bool BLO_read_data_is_undo(BlendDataReader *reader)
Definition readfile.cc:5917
static bool read_libblock_undo_restore_linked(FileData *fd, Main *libmain, const ID *id, ID **r_id_old, BHead *bhead)
Definition readfile.cc:2975
static void * newdataadr(FileData *fd, const void *adr)
Definition readfile.cc:1562
void * BLO_read_get_new_data_address_no_us(BlendDataReader *reader, const void *old_address, const size_t expected_size)
Definition readfile.cc:5540
static void do_versions_after_linking(FileData *fd, Main *main)
Definition readfile.cc:3566
static const char * blo_bhead_string_at(const BHead *bhead, const int offset, const size_t maxsize)
Definition readfile.cc:844
void BLO_library_link_end(Main *mainl, BlendHandle **bh, const LibraryLink_Params *params, ReportList *reports)
Definition readfile.cc:5052
static BHead * read_data_into_datamap(FileData *fd, BHead *bhead, const char *allocname, const int id_type_index)
Definition readfile.cc:2815
void BLO_read_data_invalidate(BlendDataReader *reader, const char *message)
Definition readfile.cc:5937
ID * BLO_read_get_new_id_address(BlendLibReader *reader, ID *self_id, const bool is_linked_only, ID *id)
Definition readfile.cc:5575
static void readfile_id_runtime_data_ensure(ID &id)
Definition readfile.cc:2347
static void read_libraries_report_invalid_id_names(FileData *fd, ReportList *reports, const bool has_forward_compatibility_issues, const char *filepath)
Definition readfile.cc:4523
static BHead * find_bhead_from_idname(FileData *fd, const char *idname)
Definition readfile.cc:4490
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:1708
static Main * filedata_main(FileData *fd)
Definition readfile.cc:612
BlendFileReadReport * BLO_read_lib_reports(BlendLibReader *reader)
Definition readfile.cc:5955
AssetMetaData * blo_bhead_id_asset_data_address(const FileData *fd, const BHead *bhead)
Definition readfile.cc:869
BlendFileData * blo_read_file_internal(FileData *fd, const char *filepath)
Definition readfile.cc:3962
static void switch_endian_structs(const SDNA *filesdna, BHead *bhead)
Definition readfile.cc:1846
void BLO_readfile_id_runtime_data_free(ID &id)
Definition readfile.cc:2369
static BHead * read_libblock(FileData *fd, Main *main, BHead *bhead, int id_tag, ID_Readfile_Data::Tags id_read_tags, const bool placeholder_set_indirect_extern, ID **r_id)
Definition readfile.cc:3215
static bool library_filepath_is_valid(const Library *lib)
Definition readfile.cc:2605
static void sort_bhead_old_map(FileData *fd)
Definition readfile.cc:4381
FileData * blo_filedata_from_memfile(MemFile *memfile, const BlendFileReadParams *params, BlendFileReadReport *reports)
Definition readfile.cc:1443
void BLO_read_double_array(BlendDataReader *reader, const int64_t array_size, double **ptr_p)
Definition readfile.cc:5773
static void fix_relpaths_library(const char *basepath, Main *main)
Definition readfile.cc:2678
static bool read_libblock_undo_restore(FileData *fd, Main *main, BHead *bhead, const int id_tag, ID **r_id_old)
Definition readfile.cc:3114
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:5812
static Library * read_library_struct(FileData *fd, BHead *bheadlib)
Definition readfile.cc:4422
void BLO_read_int16_array(BlendDataReader *reader, const int64_t array_size, int16_t **ptr_p)
Definition readfile.cc:5728
static void expand_doit_library(void *fdhandle, Main *mainvar, void *old)
Definition readfile.cc:4560
bool BLO_read_lib_is_undo(BlendLibReader *reader)
Definition readfile.cc:5945
void * BLO_read_struct_array_with_size(BlendDataReader *reader, const void *old_address, const size_t expected_size)
Definition readfile.cc:5548
static const void * peek_struct_undo(FileData *fd, BHead *bhead)
Definition readfile.cc:2108
static void * oldnewmap_liblookup(OldNewMap *onm, const void *addr, const bool is_linked_only)
Definition readfile.cc:312
static void oldnewmap_free(OldNewMap *onm)
Definition readfile.cc:339
static void oldnewmap_lib_insert(FileData *fd, const void *oldaddr, ID *newaddr, const int id_code)
Definition readfile.cc:286
void * BLO_read_struct_by_name_array(BlendDataReader *reader, const char *struct_name, const int64_t items_num, const void *old_address)
Definition readfile.cc:5563
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:1743
static BHeadN * get_bhead(FileData *fd)
Definition readfile.cc:638
static ID * link_named_part(Main *mainl, FileData *fd, const short idcode, const char *name, const int flag)
Definition readfile.cc:4762
static void after_liblink_merged_bmain_process(Main *bmain, BlendFileReadReport *reports)
Definition readfile.cc:3727
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:5783
void BLO_read_data_globmap_add(BlendDataReader *reader, void *oldaddr, void *newaddr)
Definition readfile.cc:5922
static size_t user_menu_item_struct_size(const int type)
Definition readfile.cc:3769
ID_Readfile_Data::Tags BLO_readfile_id_runtime_tags(ID &id)
Definition readfile.cc:2355
static int expand_cb(LibraryIDLinkCallbackData *cb_data)
Definition readfile.cc:4692
static void read_undo_remap_noundo_data(FileData *fd)
Definition readfile.cc:3924
FileData * blo_filedata_from_file(const char *filepath, BlendFileReadReport *reports)
Definition readfile.cc:1381
void BLO_read_int8_array(BlendDataReader *reader, const int64_t array_size, int8_t **ptr_p)
Definition readfile.cc:5722
static void read_library_linked_id(FileData *basefd, FileData *fd, Main *mainvar, ID *id, ID **r_id)
Definition readfile.cc:5117
static void read_library_linked_ids(FileData *basefd, FileData *fd, ListBase *mainlist, Main *mainvar)
Definition readfile.cc:5173
static void do_versions(FileData *fd, Library *lib, Main *main)
Definition readfile.cc:3484
static const char * get_alloc_name(FileData *fd, BHead *bh, const char *blockname, const int id_type_index=INDEX_ID_NULL)
Definition readfile.cc:1869
FileData * blo_filedata_from_memory(const void *mem, const int memsize, BlendFileReadReport *reports)
Definition readfile.cc:1411
#define BHEADN_FROM_BHEAD(bh)
Definition readfile.cc:211
static void change_link_placeholder_to_real_ID_pointer_fd(FileData *fd, const void *old, void *newp)
Definition readfile.cc:1602
Main * BLO_library_link_begin(BlendHandle **bh, const char *filepath, const LibraryLink_Params *params)
Definition readfile.cc:4895
bool BLO_read_requires_endian_switch(BlendDataReader *reader)
Definition readfile.cc:5593
static ID * library_id_is_yet_read(FileData *fd, Main *mainvar, BHead *bhead)
Definition readfile.cc:4506
static FileData * blo_filedata_from_file_descriptor(const char *filepath, BlendFileReadReport *reports, const int filedes)
Definition readfile.cc:1300
static void direct_link_keymapitem(BlendDataReader *reader, wmKeyMapItem *kmi)
Definition readfile.cc:3761
static void blo_cache_storage_entry_register(ID *id, const IDCacheKey *key, void **cache_p, uint, void *cache_storage_v)
Definition readfile.cc:1688
static void * newlibadr(FileData *fd, ID *, const bool is_linked_only, const void *adr)
Definition readfile.cc:1588
void blo_split_main(ListBase *mainlist, Main *main)
Definition readfile.cc:411
static int verg_bheadsort(const void *v1, const void *v2)
Definition readfile.cc:4367
void BLO_library_link_params_init_with_context(LibraryLink_Params *params, Main *bmain, const int flag, const int id_tag_extra, Scene *scene, ViewLayer *view_layer, const View3D *v3d)
Definition readfile.cc:4878
static bool read_file_dna(FileData *fd, const char **r_error_message)
Definition readfile.cc:905
void blo_do_versions_key_uidgen(Key *key)
Definition readfile.cc:2529
static void placeholders_ensure_valid(Main *bmain)
Definition readfile.cc:2751
static BHead * find_bhead_from_code_name(FileData *fd, const short idcode, const char *name)
Definition readfile.cc:4481
void blo_cache_storage_init(FileData *fd, Main *bmain)
Definition readfile.cc:1767
void blo_make_old_idmap_from_main(FileData *fd, Main *bmain)
Definition readfile.cc:1669
short BLO_version_from_file(const char *filepath)
Definition readfile.cc:1544
BHead * blo_read_asset_data_block(FileData *fd, BHead *bhead, AssetMetaData **r_asset_data)
Definition readfile.cc:3365
static void read_libblock_undo_restore_identical(FileData *fd, Main *main, const ID *, ID *id_old, BHead *bhead, const int id_tag)
Definition readfile.cc:3024
static void change_link_placeholder_to_real_ID_pointer(ListBase *mainlist, FileData *basefd, void *old, void *newp)
Definition readfile.cc:1643
static FileData * blo_filedata_from_file_minimal(const char *filepath)
Definition readfile.cc:1397
static void oldnewmap_clear(OldNewMap *onm)
Definition readfile.cc:328
void BLO_readfile_id_runtime_data_free_all(Main &bmain)
Definition readfile.cc:2374
static void read_libblock_undo_restore_at_old_address(FileData *fd, Main *main, ID *id, ID *id_old)
Definition readfile.cc:3072
static NewAddress * newdataadr_entry(FileData *fd, const void *adr)
Definition readfile.cc:1577
static bool direct_link_id(FileData *fd, Main *main, const int tag, const ID_Readfile_Data::Tags id_read_tags, ID *id, ID *id_old)
Definition readfile.cc:2763
static BHead * find_previous_lib(FileData *fd, BHead *bhead)
Definition readfile.cc:4406
#define BHEAD_USE_READ_ON_DEMAND(bhead)
Definition readfile.cc:217
static void read_library_clear_weak_links(FileData *basefd, ListBase *mainlist, Main *mainvar)
Definition readfile.cc:5253
void BLO_read_char_array(BlendDataReader *reader, const int64_t array_size, char **ptr_p)
Definition readfile.cc:5710
void * BLO_library_read_struct(FileData *fd, BHead *bh, const char *blockname)
Definition readfile.cc:5089
void BLO_read_float3_array(BlendDataReader *reader, const int64_t array_size, float **ptr_p)
Definition readfile.cc:5768
static CLG_LogRef LOG
Definition readfile.cc:181
void * blo_read_struct_with_type_size_impl(BlendDataReader *reader, const void *old_address, const size_t base_size, blender::FunctionRef< size_t(const void *data)> type_size_fn)
Definition readfile.cc:5690
static bool is_minversion_older_than_blender(FileData *fd, ReportList *reports)
Definition readfile.cc:1199
void BLO_expand_main(void *fdhandle, Main *mainvar, BLOExpandDoitCallback callback)
Definition readfile.cc:4722
ID * BLO_read_get_new_id_address_from_session_uid(BlendLibReader *reader, const uint session_uid)
Definition readfile.cc:5583
void * BLO_read_get_new_data_address(BlendDataReader *reader, const void *old_address)
Definition readfile.cc:5535
ID_Readfile_Data::Tags & BLO_readfile_id_runtime_tags_for_write(ID &id)
Definition readfile.cc:2363
static Main * library_link_begin(Main *mainvar, FileData *fd, const char *filepath, const int id_tag_extra)
Definition readfile.cc:4834
static void blo_read_file_checks(Main *bmain)
Definition readfile.cc:3946
ID * BLO_library_link_named_part(Main *mainl, BlendHandle **bh, const short idcode, const char *name, const LibraryLink_Params *params)
Definition readfile.cc:4813
static bool blo_bhead_read_data(FileData *fd, BHead *thisblock, void *buf)
Definition readfile.cc:787
static Main * blo_find_main(FileData *fd, const char *filepath, const char *relabase)
Definition readfile.cc:543
static BHead * read_userdef(BlendFileData *bfd, FileData *fd, BHead *bhead)
Definition readfile.cc:3789
void blo_filedata_free(FileData *fd)
Definition readfile.cc:1460
static int * read_file_thumbnail(FileData *fd)
Definition readfile.cc:963
static bool blo_bhead_is_id_valid_type(const BHead *bhead)
Definition readfile.cc:506
static void * blo_verify_data_address(FileData *fd, void *new_address, const void *, const size_t expected_size)
Definition readfile.cc:5509
static void after_liblink_id_embedded_id_process(BlendLibReader *reader, ID *id)
Definition readfile.cc:2152
static const T * blo_bhead_data_at(const BHead *bhead, const int offset)
Definition readfile.cc:835
static FileData * change_ID_link_filedata_get(Main *bmain, FileData *basefd)
Definition readfile.cc:1633
BHead * blo_bhead_prev(FileData *, BHead *thisblock)
Definition readfile.cc:752
static void read_undo_move_libmain_data(FileData *fd, Main *new_main, Main *old_main, Main *libmain, BHead *bhead)
Definition readfile.cc:2903
void BLO_read_pointer_array(BlendDataReader *reader, const int64_t array_size, void **ptr_p)
Definition readfile.cc:5834
void blo_readfile_invalidate(FileData *fd, Main *bmain, const char *message)
Definition readfile.cc:595
static void read_blender_header(FileData *fd)
Definition readfile.cc:877
static void read_file_version(FileData *fd, Main *main)
Definition readfile.cc:463
static void * oldnewmap_lookup_and_inc(OldNewMap *onm, const void *addr, const bool increase_users)
Definition readfile.cc:299
void blo_cache_storage_old_bmain_clear(FileData *fd, Main *bmain_old)
Definition readfile.cc:1803
static CLG_LogRef LOG_UNDO
Definition readfile.cc:182
static FileData * blo_filedata_from_file_open(const char *filepath, BlendFileReadReport *reports)
Definition readfile.cc:1366
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:2050
static int read_undo_remap_noundo_data_cb(LibraryIDLinkCallbackData *cb_data)
Definition readfile.cc:3902
static void read_libraries_free_placeholders(FileData *basefd, ListBase *mainlist)
Definition readfile.cc:5362
static void * read_struct(FileData *fd, BHead *bh, const char *blockname, const int id_type_index)
Definition readfile.cc:1946
void blo_join_main(ListBase *mainlist)
Definition readfile.cc:364
static FileData * blo_decode_and_check(FileData *fd, ReportList *reports)
Definition readfile.cc:1249
void BLO_reportf_wrap(BlendFileReadReport *reports, const eReportType type, const char *format,...)
Definition readfile.cc:223
void * blo_read_get_new_globaldata_address(FileData *fd, const void *adr)
Definition readfile.cc:1582
static FileData * read_library_file_data(FileData *basefd, ListBase *mainlist, Main *mainl, Main *mainptr)
Definition readfile.cc:5279
void blo_do_versions_400(FileData *fd, Library *lib, Main *bmain)
void blo_do_versions_300(FileData *fd, Library *lib, Main *bmain)
void do_versions_after_linking_450(FileData *fd, Main *bmain)
void do_versions_after_linking_300(FileData *fd, Main *bmain)
void blo_do_versions_dna(SDNA *sdna, int versionfile, int subversionfile)
void blo_do_versions_450(FileData *fd, Library *lib, Main *bmain)
void do_versions_after_linking_410(FileData *fd, Main *bmain)
void blo_do_versions_250(FileData *fd, Library *lib, Main *bmain)
void blo_do_versions_410(FileData *fd, Library *lib, Main *bmain)
void do_versions_after_linking_400(FileData *fd, Main *bmain)
void do_versions_after_linking_430(FileData *fd, Main *bmain)
void do_versions_after_linking_250(Main *bmain)
void do_versions_after_linking_290(FileData *fd, Main *bmain)
@ FD_FLAGS_FILE_POINTSIZE_IS_4
Definition readfile.hh:49
@ FD_FLAGS_POINTSIZE_DIFFERS
Definition readfile.hh:50
@ FD_FLAGS_IS_MEMFILE
Definition readfile.hh:52
@ FD_FLAGS_FILE_OK
Definition readfile.hh:51
@ FD_FLAGS_SWITCH_ENDIAN
Definition readfile.hh:48
@ FD_FLAGS_FILE_FUTURE
Definition readfile.hh:57
@ FD_FLAGS_HAS_INVALID_ID_NAMES
Definition readfile.hh:62
void do_versions_after_linking_440(FileData *fd, Main *bmain)
void do_versions_after_linking_420(FileData *fd, Main *bmain)
void blo_do_versions_userdef(UserDef *userdef)
void blo_do_versions_420(FileData *fd, Library *lib, Main *bmain)
void do_versions_after_linking_280(FileData *fd, Main *bmain)
void blo_do_versions_290(FileData *fd, Library *lib, Main *bmain)
void blo_do_versions_270(FileData *fd, Library *lib, Main *bmain)
void do_versions_after_linking_270(Main *bmain)
void blo_do_versions_440(FileData *fd, Library *lib, Main *bmain)
void blo_do_versions_430(FileData *fd, Library *lib, Main *bmain)
void do_versions_after_linking_260(Main *bmain)
void blo_do_versions_260(FileData *fd, Library *lib, Main *bmain)
void blo_do_versions_pre250(FileData *fd, Library *lib, Main *bmain)
void blo_do_versions_280(FileData *fd, Library *lib, Main *bmain)
signed short int16_t
Definition stdint.h:76
__int64 int64_t
Definition stdint.h:89
signed int int32_t
Definition stdint.h:77
unsigned char uint8_t
Definition stdint.h:78
unsigned __int64 uint64_t
Definition stdint.h:90
signed char int8_t
Definition stdint.h:75
char identifier[66]
char last_slot_identifier[66]
char tmp_last_slot_identifier[66]
The meta-data of an asset. By creating and giving this for a data-block (ID.asset_data),...
off64_t file_offset
Definition readfile.cc:203
bool has_data
Definition readfile.cc:205
BHeadN * prev
Definition readfile.cc:200
bool is_memchunk_identical
Definition readfile.cc:207
BHead bhead
Definition readfile.cc:208
BHeadN * next
Definition readfile.cc:200
const void * old
Definition readfile.cc:4364
BHead * bhead
Definition readfile.cc:4363
int64_t len
int64_t nr
const void * old
MemArena * memarena
Definition readfile.cc:1679
GHash * cache_map
Definition readfile.cc:1678
blender::Map< const void *, blender::ImplicitSharingInfoAndData > shared_data_by_stored_address
Definition readfile.cc:630
FileData * fd
Definition readfile.cc:624
FileData * fd
Definition readfile.cc:4555
BLOExpandDoitCallback callback
Definition readfile.cc:4557
UserDef * user
ViewLayer * cur_view_layer
char filepath[1024]
eBlenFileType type
bScreen * curscreen
struct BlendFileReadReport::@124 duration
struct BlendFileReadReport::@125 count
FileData * fd
Definition readfile.cc:634
char rect[0]
Definition BKE_main.hh:50
BHeadType bhead_type() const
eBLOReadSkip skip_flags
Definition readfile.hh:120
int undo_direction
Definition readfile.hh:91
int id_asset_data_offset
Definition readfile.hh:114
ListBase * old_mainlist
Definition readfile.hh:149
enum eFileDataFlag flags
Definition readfile.hh:80
ListBase bhead_list
Definition readfile.hh:79
int fileversion
Definition readfile.hh:109
BlenderHeader blender_header
Definition readfile.hh:82
void * storage_handle
Definition readfile.hh:168
int fileflags
Definition readfile.hh:117
DNA_ReconstructInfo * reconstruct_info
Definition readfile.hh:107
BHeadSort * bheadmap
Definition readfile.hh:142
ListBase * mainlist
Definition readfile.hh:147
std::optional< blender::Map< blender::StringRefNull, BHead * > > bhead_idname_map
Definition readfile.hh:145
IDNameLib_Map * new_idmap_uid
Definition readfile.hh:163
OldNewMap * globmap
Definition readfile.hh:131
OldNewMap * datamap
Definition readfile.hh:130
int id_tag_extra
Definition readfile.hh:128
FileReader * file
Definition readfile.hh:84
char relabase[FILE_MAX]
Definition readfile.hh:100
const char * compflags
Definition readfile.hh:106
BLOCacheStorage * cache_storage
Definition readfile.hh:140
int globalf
Definition readfile.hh:116
int id_name_offset
Definition readfile.hh:111
OldNewMap * libmap
Definition readfile.hh:138
BlendFileReadReport * reports
Definition readfile.hh:165
bool is_eof
Definition readfile.hh:81
int tot_bheadmap
Definition readfile.hh:143
const SDNA * memsdna
Definition readfile.hh:104
IDNameLib_Map * old_idmap_uid
Definition readfile.hh:153
SDNA * filesdna
Definition readfile.hh:103
struct ViewLayer * cur_view_layer
struct Scene * curscene
uint64_t build_commit_timestamp
struct bScreen * curscreen
char filepath[1024]
FileReaderSeekFn seek
off64_t offset
FileReaderCloseFn close
FileReaderReadFn read
ListBase properties
Definition DNA_ID.h:326
IDTypeBlendReadUndoPreserve blend_read_undo_preserve
short id_code
const char * name
IDTypeForeachCacheFunction foreach_cache
IDTypeBlendReadDataFunction blend_read_data
uint32_t flags
AssetTypeInfo * asset_type_info
IDTypeBlendReadAfterLiblinkFunction blend_read_after_liblink
struct ID_Readfile_Data * readfile_data
Definition DNA_ID.h:401
Definition DNA_ID.h:404
unsigned int recalc_after_undo_push
Definition DNA_ID.h:438
unsigned int recalc
Definition DNA_ID.h:427
int tag
Definition DNA_ID.h:424
struct AssetMetaData * asset_data
Definition DNA_ID.h:413
struct Library * lib
Definition DNA_ID.h:410
IDProperty * system_properties
Definition DNA_ID.h:454
int us
Definition DNA_ID.h:425
int icon_id
Definition DNA_ID.h:426
struct ID * newid
Definition DNA_ID.h:408
void * prev
Definition DNA_ID.h:407
IDProperty * properties
Definition DNA_ID.h:446
IDOverrideLibrary * override_library
Definition DNA_ID.h:459
struct ID * orig_id
Definition DNA_ID.h:466
short flag
Definition DNA_ID.h:420
char name[66]
Definition DNA_ID.h:415
unsigned int recalc_up_to_undo_push
Definition DNA_ID.h:437
unsigned int session_uid
Definition DNA_ID.h:444
struct LibraryWeakReference * library_weak_reference
Definition DNA_ID.h:491
struct ID_Runtime runtime
Definition DNA_ID.h:493
LibraryForeachIDCallbackFlag cb_flag
struct PackedFile * packedfile
Definition DNA_ID.h:509
char filepath[1024]
Definition DNA_ID.h:507
ID id
Definition DNA_ID.h:505
LibraryRuntimeHandle * runtime
Definition DNA_ID.h:516
void * last
void * first
ListBase scenes
Definition BKE_main.hh:245
ListBase wm
Definition BKE_main.hh:276
short subversionfile
Definition BKE_main.hh:156
bool is_asset_edit_file
Definition BKE_main.hh:171
bool has_forward_compatibility_issues
Definition BKE_main.hh:164
char filepath[1024]
Definition BKE_main.hh:155
BlendThumbnail * blen_thumb
Definition BKE_main.hh:231
Main * prev
Definition BKE_main.hh:142
short minversionfile
Definition BKE_main.hh:157
uint64_t build_commit_timestamp
Definition BKE_main.hh:174
ListBase libraries
Definition BKE_main.hh:246
IDNameLib_Map * id_map
Definition BKE_main.hh:301
char build_hash[16]
Definition BKE_main.hh:176
Main * next
Definition BKE_main.hh:142
short minsubversionfile
Definition BKE_main.hh:157
short versionfile
Definition BKE_main.hh:156
bool is_read_invalid
Definition BKE_main.hh:203
Library * curlib
Definition BKE_main.hh:241
ListBase objects
Definition BKE_main.hh:247
blender::Map< const void *, const blender::ImplicitSharingInfo * > map
MemFileSharedStorage * shared_storage
void * newp
Definition readfile.cc:258
ListBase constraints
struct bPose * pose
blender::Map< const void *, NewAddress > map
Definition readfile.cc:265
int structs_num
int pointer_size
struct Collection * master_collection
struct Scene * set
MemFile * memfile
struct ListBase addons
ListBase script_directories
struct ListBase uistyles
struct ListBase asset_shelves_settings
struct ListBase user_keymaps
short edit_studio_light
struct ListBase themes
struct ListBase autoexec_paths
struct ListBase uifonts
struct ListBase user_keyconfig_prefs
struct ListBase extension_repos
struct ListBase user_menus
struct ListBase asset_libraries
char app_template[64]
UserDef_Runtime runtime
char name[64]
struct ActionSlot ** slot_array
ListBase chanbase
struct IDProperty * prop
struct PointerRNA * ptr
uint len
void version_forward_compat_system_idprops(Main *bmain)
short do_versions_new_to_old_idcode_get(const short id_code_new)
static DynamicLibrary lib