Blender V5.3
writefile.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
61
62#include <cerrno>
63#include <climits>
64#include <condition_variable>
65#include <cstdio>
66#include <cstdlib>
67#include <cstring>
68#include <fcntl.h>
69#include <iomanip>
70#include <mutex>
71#include <sstream>
72#include <xxhash.h>
73
74#ifdef WIN32
75# include "BLI_winstuff.hh"
76# include "winsock2.h"
77# include <io.h>
78#else
79# include <unistd.h> /* FreeBSD, for write() and close(). */
80#endif
81
82#include <fmt/format.h>
83
84#include "BLI_utildefines.hh"
85
86#include "CLG_log.h"
87
88/* Allow writefile to use deprecated functionality (for forward compatibility code). */
89#define DNA_DEPRECATED_ALLOW
90
92#include "DNA_genfile.h"
93#include "DNA_key_types.h"
94#include "DNA_print.hh"
95#include "DNA_sdna_pointers.hh"
96#include "DNA_sdna_types.h"
97#include "DNA_userdef_types.h"
99
100#include "BLI_endian_defines.hh"
101#include "BLI_fileops.hh"
103#include "BLI_listbase.hh"
104#include "BLI_math_base_c.hh"
105#include "BLI_math_matrix_c.hh"
106#include "BLI_multi_value_map.hh"
107#include "BLI_path_utils.hh"
108#include "BLI_set.hh"
109#include "BLI_string.hh"
110#include "BLI_task_c.hh"
111#include "BLI_threads.hh"
112#include "BLI_time.hh"
113
114#include "MEM_guardedalloc.h"
115
116#include "BKE_asset.hh"
117#include "BKE_blender_version.h"
118#include "BKE_bpath.hh"
119#include "BKE_global.hh" /* For #Global `G`. */
120#include "BKE_idprop.hh"
121#include "BKE_idtype.hh"
122#include "BKE_layer.hh"
123#include "BKE_lib_id.hh"
124#include "BKE_lib_override.hh"
125#include "BKE_lib_query.hh"
126#include "BKE_library.hh"
127#include "BKE_main.hh"
128#include "BKE_main_namemap.hh"
129#include "BKE_node.hh"
130#include "BKE_packedFile.hh"
131#include "BKE_preferences.h"
132#include "BKE_report.hh"
133#include "BKE_workspace.hh"
134
135#include "DRW_engine.hh"
136
137#include "BLO_blend_validate.hh"
138#include "BLO_read_write.hh"
139#include "BLO_readfile.hh"
140#include "BLO_undofile.hh"
141#include "BLO_writefile.hh"
142
143#include "readfile.hh"
144#include "writefile.hh"
145
146#include <zstd.h>
147
148namespace blender {
149
150/* Make preferences read-only. */
151#define U (*((const UserDef *)&U))
152
157#define GENERATE_DEBUG_BLEND_FILE 0
158#define DEBUG_BLEND_FILE_SUFFIX ".debug.txt"
159
160/* ********* my write, buffered writing with minimum size chunks ************ */
161
162/* Use optimal allocation since blocks of this size are kept in memory for undo. */
163#define MEM_BUFFER_SIZE MEM_SIZE_OPTIMAL(1 << 17) /* 128kb */
164#define MEM_CHUNK_SIZE MEM_SIZE_OPTIMAL(1 << 15) /* ~32kb */
165
166#define ZSTD_BUFFER_SIZE (1 << 21) /* 2mb */
167#define ZSTD_CHUNK_SIZE (1 << 20) /* 1mb */
168
169#define ZSTD_COMPRESSION_LEVEL 3
170
171static CLG_LogRef LOG = {"blend.writefile"};
172static CLG_LogRef LOG_UNDO = {"undo"};
173
175// #define USE_WRITE_DATA_LEN
176
177/* -------------------------------------------------------------------- */
180
182 public:
183 virtual bool open(const char *filepath) = 0;
184 virtual bool close() = 0;
185 virtual bool write(const void *buf, size_t buf_len) = 0;
186
188 bool use_buf = true;
189};
190
191class RawWriteWrap : public WriteWrap {
192 public:
193 bool open(const char *filepath) override;
194 bool close() override;
195 bool write(const void *buf, size_t buf_len) override;
196
197 private:
198 int file_handle = 0;
199};
200
201bool RawWriteWrap::open(const char *filepath)
202{
203 int file;
204
205 file = BLI_open(filepath, O_BINARY + O_WRONLY + O_CREAT + O_TRUNC, 0666);
206
207 if (file != -1) {
208 file_handle = file;
209 return true;
210 }
211
212 return false;
213}
215{
216 return (::close(file_handle) != -1);
217}
218bool RawWriteWrap::write(const void *buf, size_t buf_len)
219{
220 return ::write(file_handle, buf, buf_len) == buf_len;
221}
222
223class ZstdWriteWrap : public WriteWrap {
224 struct ZstdFrame {
225 const void *uncompressed_data = nullptr;
226 uint32_t uncompressed_size = 0;
227
228 uint32_t compressed_size = 0;
229 const void *compressed_data = nullptr;
230
238 std::atomic<bool> compressed_done = false;
239 };
240
241 WriteWrap &base_wrap;
242
244 TaskPool *pool = nullptr;
245
263 int next_frame = 0;
264
270 std::atomic<bool> write_error = false;
271
272 public:
273 ZstdWriteWrap(WriteWrap &base_wrap) : base_wrap(base_wrap) {}
274
275 bool open(const char *filepath) override;
276 bool close() override;
277 bool write(const void *buf, size_t buf_len) override;
278
279 private:
281 static void compress_task_run(TaskPool *pool, void *taskdata);
289 void write_compressed_frames();
291 void write_u32_le(uint32_t val);
303 void write_seekable_frames();
304};
305
306void ZstdWriteWrap::compress_task_run(TaskPool *pool, void *taskdata)
307{
308 auto *frame = static_cast<ZstdFrame *>(taskdata);
309 auto *ww = static_cast<ZstdWriteWrap *>(BLI_task_pool_user_data(pool));
310
311 size_t out_buf_len = ZSTD_compressBound(frame->uncompressed_size);
312 void *out_buf = MEM_new_uninitialized(out_buf_len, "Zstd out buffer");
313 const size_t out_size = ZSTD_compress(out_buf,
314 out_buf_len,
315 frame->uncompressed_data,
316 frame->uncompressed_size,
318 MEM_delete_void(frame->uncompressed_data);
319 frame->uncompressed_data = nullptr;
320
321 frame->compressed_data = out_buf;
322 /* Do not store 'error code' size, as its value will be out of uint32_t range. The size value
323 * is not used in case an error has occurred anyway (compressed frames are not written, and
324 * neither is the final seek table). */
325 if (ZSTD_isError(out_size)) [[unlikely]] {
326 ww->write_error = true;
327 }
328 else {
329 frame->compressed_size = uint32_t(out_size);
330 }
331 frame->compressed_done = true;
332}
333
334void ZstdWriteWrap::write_compressed_frames()
335{
336 /* Loop over all pending frames in the correct ascendant order, and write them on disk until we
337 * reach one which has not yet available compressed data. */
338 for (const std::unique_ptr<ZstdFrame> &frame : frames.as_span().drop_front(next_frame)) {
339 if (!frame->compressed_done) {
340 /* This frame has not yet been compressed, cannot write further data. */
341 break;
342 }
343 if (!write_error) [[likely]] {
344 BLI_assert(frame->compressed_size > 0);
345 const bool has_error = !base_wrap.write(frame->compressed_data, frame->compressed_size);
346 if (has_error) [[unlikely]] {
347 write_error = true;
348 }
349 }
350 next_frame++;
351 BLI_assert(frame->uncompressed_data == nullptr);
352 MEM_SAFE_DELETE_VOID(frame->compressed_data);
353 }
354}
355
356bool ZstdWriteWrap::open(const char *filepath)
357{
358 if (!base_wrap.open(filepath)) {
359 return false;
360 }
361
363
364 return true;
365}
366
367void ZstdWriteWrap::write_u32_le(uint32_t val)
368{
369 /* NOTE: this is endianness-sensitive.
370 * This value must always be written as little-endian. */
372 base_wrap.write(&val, sizeof(uint32_t));
373}
374
375void ZstdWriteWrap::write_seekable_frames()
376{
377 if (write_error) [[unlikely]] {
378 /* Do not write a seek table if the data itself could not be fully written. */
379 return;
380 }
381
382 /* Write seek table header (magic number and frame size). */
383 write_u32_le(0x184D2A5E);
384
385 const uint32_t num_frames = uint32_t(frames.size());
386 /* Each frame consists of two u32, so 8 bytes each.
387 * After the frames, a footer containing two u32 and one byte (9 bytes total) is written. */
388 const uint32_t frame_size = num_frames * 8 + 9;
389 write_u32_le(frame_size);
390
391 /* Write seek table entries. */
392 for (const std::unique_ptr<ZstdFrame> &frame : frames) {
393 write_u32_le(frame->compressed_size);
394 write_u32_le(frame->uncompressed_size);
395 }
396
397 /* Write seek table footer (number of frames, option flags and second magic number). */
398 write_u32_le(num_frames);
399 const char flags = 0; /* We don't store checksums for each frame. */
400 base_wrap.write(&flags, 1);
401 write_u32_le(0x8F92EAB1);
402}
403
405{
407 BLI_task_pool_free(pool);
408 pool = nullptr;
409
410 write_compressed_frames();
411 BLI_assert(next_frame == frames.size());
412
413 write_seekable_frames();
414 frames.clear();
415
416 return base_wrap.close() && !write_error;
417}
418
419bool ZstdWriteWrap::write(const void *buf, const size_t buf_len)
420{
421 if (write_error) [[unlikely]] {
422 return false;
423 }
424
425 void *uncompressed_data = MEM_new_uninitialized(buf_len, __func__);
426 memcpy(uncompressed_data, buf, buf_len);
427
428 auto task = std::make_unique<ZstdFrame>();
429 task->uncompressed_data = uncompressed_data;
430 task->uncompressed_size = uint32_t(buf_len);
431 ZstdFrame *frame_p = task.get();
432
433 frames.append(std::move(task));
434 BLI_task_pool_push(pool, compress_task_run, frame_p, false, nullptr);
435
436 write_compressed_frames();
437 return true;
438}
439
441
442/* -------------------------------------------------------------------- */
445
447{
448 WriteData *wd = MEM_new<WriteData>(__func__);
449
451
453 wd->stable_address_ids.sdna_pointers = std::make_unique<dna::pointers::PointersInDNA>(*wd->sdna);
454
455 wd->ww = ww;
456
457 if ((ww == nullptr) || (ww->use_buf)) {
458 if (ww == nullptr) {
459 wd->buffer.max_size = MEM_BUFFER_SIZE;
460 wd->buffer.chunk_size = MEM_CHUNK_SIZE;
461 }
462 else {
463 wd->buffer.max_size = ZSTD_BUFFER_SIZE;
464 wd->buffer.chunk_size = ZSTD_CHUNK_SIZE;
465 }
466 wd->buffer.buf = MEM_new_array_uninitialized<uchar>(wd->buffer.max_size, "wd->buffer.buf");
467 }
468
469 return wd;
470}
471
472static void writedata_do_write(WriteData *wd, const void *mem, const size_t memlen)
473{
474 if ((wd == nullptr) || wd->validation_data.critical_error || (mem == nullptr) || memlen < 1) {
475 return;
476 }
477
478 if (memlen > INT_MAX) {
479 BLI_assert_msg(0, "Cannot write chunks bigger than INT_MAX.");
480 return;
481 }
482
483 if (wd->validation_data.critical_error) [[unlikely]] {
484 return;
485 }
486
487 /* Memory based save. */
488 if (wd->use_memfile) {
489 BLO_memfile_chunk_add(&wd->mem, static_cast<const char *>(mem), memlen);
490 }
491 else {
492 if (!wd->ww->write(mem, memlen)) {
493 wd->validation_data.critical_error = true;
494 }
495 }
496}
497
498static void writedata_free(WriteData *wd)
499{
500 if (wd->buffer.buf) {
501 MEM_delete(wd->buffer.buf);
502 }
503 MEM_delete(wd);
504}
505
507
508/* -------------------------------------------------------------------- */
511
516static void mywrite_flush(WriteData *wd)
517{
518 if (wd->buffer.used_len != 0) {
519 writedata_do_write(wd, wd->buffer.buf, wd->buffer.used_len);
520 wd->buffer.used_len = 0;
521 }
522}
523
529static void mywrite(WriteData *wd, const void *adr, size_t len)
530{
531 if (wd->validation_data.critical_error) [[unlikely]] {
532 return;
533 }
534
535 if (adr == nullptr) [[unlikely]] {
536 BLI_assert(0);
537 return;
538 }
539
540#ifdef USE_WRITE_DATA_LEN
541 wd->write_len += len;
542#endif
543
544 if (wd->buffer.buf == nullptr) {
545 writedata_do_write(wd, adr, len);
546 }
547 else {
548 /* If we have a single big chunk, write existing data in
549 * buffer and write out big chunk in smaller pieces. */
550 if (len > wd->buffer.chunk_size) {
551 if (wd->buffer.used_len != 0) {
552 writedata_do_write(wd, wd->buffer.buf, wd->buffer.used_len);
553 wd->buffer.used_len = 0;
554 }
555
556 do {
557 const size_t writelen = std::min(len, wd->buffer.chunk_size);
558 writedata_do_write(wd, adr, writelen);
559 adr = static_cast<const char *>(adr) + writelen;
560 len -= writelen;
561 } while (len > 0);
562
563 return;
564 }
565
566 /* If data would overflow buffer, write out the buffer. */
567 if (len + wd->buffer.used_len > wd->buffer.max_size - 1) {
568 writedata_do_write(wd, wd->buffer.buf, wd->buffer.used_len);
569 wd->buffer.used_len = 0;
570 }
571
572 /* Append data at end of buffer. */
573 memcpy(&wd->buffer.buf[wd->buffer.used_len], adr, len);
574 wd->buffer.used_len += len;
575 }
576}
577
585static WriteData *mywrite_begin(WriteWrap *ww, MemFile *compare, MemFile *current)
586{
587 WriteData *wd = writedata_new(ww);
588
589 if (current != nullptr) {
590 BLO_memfile_write_init(wd, &wd->mem, current, compare);
591 }
592
593 return wd;
594}
595
602static bool mywrite_end(WriteData *wd)
603{
604 if (wd->buffer.used_len != 0) {
605 writedata_do_write(wd, wd->buffer.buf, wd->buffer.used_len);
606 wd->buffer.used_len = 0;
607 }
608
609 if (wd->use_memfile) {
612 "Memfile undo step written in %.3f seconds",
614 }
615 else {
616 CLOG_INFO(
617 &LOG, "Blendfile written in %.3f seconds", BLI_time_now_seconds() - wd->timestamp_init);
618 }
619
620 const bool err = wd->validation_data.critical_error;
621 writedata_free(wd);
622
623 return err;
624}
625
626static uint64_t get_stable_pointer_hint_for_id(const ID &id, const bool is_undo)
627{
628 /* Make the stable pointer depend on a specific data of the ID.
629 * Note that this is different when writing blendfile on disk, and when writing an undo step in
630 * memory (memfile).
631 *
632 * For the blendfile on disk, the ID name is used, together with its library if linked, as this
633 * is effectively the 'unique identifier' of IDs in blend-files and across linking,
634 * so if these change, it's also fine to get a different 'stable pointer'.
635 *
636 * For the undo memfile however, things are different: It is possible that a same ID name is
637 * reused for two different IDs in two different consecutive undo steps (see #149899). Getting
638 * the same stable pointer in this case can lead to falsely detecting other IDs using these as
639 * unchanged, leading to undo data corruption and crashes.
640 * In this case, using the session UID is a better source of info, as these are assumed unique
641 * during an editing session, and are extremely stable for a same ID (even if it is e.g.
642 * renamed).
643 */
644 if (is_undo) {
645 /* Note: Using the uint32_t session_uid also means that library data can be ignored (and ID
646 * made local always get a new session UID), and that there is no need to call the hashing code
647 * at all.
648 *
649 * However, to leave enough 'address space' for all the sub-data pointers, its value is shifted
650 * into higher significant bits of the returned value (only shift by 20 bits here, since
651 * #stable_id_from_hint also shifts further the generated values by 4, and some of the most
652 * significant bits are also reserved for flags, like the #generated_address_id_on_undo_flag
653 * one). */
654 return uint64_t(id.session_uid) << 20;
655 }
656
657 const uint64_t id_hash = XXH3_64bits(id.name, strlen(id.name));
658 if (!id.lib) {
659 return id_hash;
660 }
661
662 const uint64_t lib_hash = XXH3_64bits(id.lib->id.name, strlen(id.lib->id.name));
663 return id_hash ^ lib_hash;
664}
665
671static void mywrite_id_begin(WriteData *wd, ID *id)
672{
673 BLI_assert(wd->is_writing_id == false);
674 wd->is_writing_id = true;
675
676 BLI_assert(wd->validation_data.per_id_addresses_set.is_empty());
677
678 BLI_assert_msg(ID_IS_PACKED(id) || id->deep_hash.is_null(),
679 "The only IDs with non-null deep-hash data should be packed linked ones");
680 BLI_assert_msg((id->flag & ID_FLAG_EMBEDDED_DATA) == 0 || id->deep_hash.is_null(),
681 "Embedded IDs should always have a null deep-hash data");
682
683 const bool is_undo = wd->use_memfile;
684
686
687 if (is_undo) {
688 wd->mem.current_id_session_uid = id->session_uid;
689
690 /* If current next memchunk does not match the ID we are about to write, or is not the _first_
691 * one for said ID, try to find the correct memchunk in the mapping using ID's session_uid. */
692 const MemFileChunk *curr_memchunk = wd->mem.reference_current_chunk;
693 const MemFileChunk *prev_memchunk = curr_memchunk != nullptr ?
694 static_cast<MemFileChunk *>(curr_memchunk->prev) :
695 nullptr;
696 if (curr_memchunk == nullptr || curr_memchunk->id_session_uid != id->session_uid ||
697 (prev_memchunk != nullptr &&
698 (prev_memchunk->id_session_uid == curr_memchunk->id_session_uid)))
699 {
700 if (MemFileChunk *ref = wd->mem.id_session_uid_mapping.lookup_default(id->session_uid,
701 nullptr))
702 {
704 }
705 /* Else, no existing memchunk found, i.e. this is supposed to be a new ID. */
706 }
707 /* Otherwise, we try with the current memchunk in any case, whether it is matching current
708 * ID's session_uid or not. */
709 }
710}
711
717static void mywrite_id_end(WriteData *wd, ID * /*id*/)
718{
719 if (wd->use_memfile) {
720 /* Very important to do it after every ID write now, otherwise we cannot know whether a
721 * specific ID changed or not. */
722 mywrite_flush(wd);
724
725 /* In undo case:
726 * - IDs never get a stable address id;
727 * - Shared data (implicit sharing) never gets a stable address id.
728 * - Other addresses (private ID data) should never be shared across IDs.
729 *
730 * So we can clear the stable address ids after each ID writing. This makes the mapping even
731 * smaller, and ensures that data dynamically generated on write (which may re-use the same
732 * addresses between different IDs) do not trigger the assert in `generated_pointer_tag`.
733 *
734 * Note that `WriteDataStableAddressIDs::used_ids` and
735 * `WriteDataStableAddressIDs::next_id_hint` are kept, to ensure generated address ids are
736 * never re-used in another ID (not strictly necessary, but can helps with debugging etc.).
737 */
739 }
740
741 wd->validation_data.per_id_addresses_set.clear();
743
744 BLI_assert(wd->is_writing_id == true);
745 wd->is_writing_id = false;
746}
747
749
750/* -------------------------------------------------------------------- */
753
761static bool write_at_address_validate(WriteData *wd, const int filecode, const void *address)
762{
763 /* Skip in undo case. */
764 if (wd->use_memfile) {
765 return true;
766 }
767
768 if (wd->is_writing_id && filecode == BLO_CODE_DATA) {
769 if (!wd->validation_data.per_id_addresses_set.add(address)) {
771 "Same identifier (old address) used several times for a same ID, skipping this "
772 "block to avoid critical corruption of the Blender file.");
773 return false;
774 }
775 }
776 return true;
777}
778
779static void write_bhead(WriteData *wd, const BHead &bhead)
780{
781 if constexpr (sizeof(void *) == 4) {
782 /* Always write #BHead4 in 32 bit builds. */
783 BHead4 bh;
784 bh.code = bhead.code;
785 bh.old = uint32_t(uintptr_t(bhead.old));
786 bh.nr = bhead.nr;
787 bh.SDNAnr = bhead.SDNAnr;
788 bh.len = bhead.len;
789 mywrite(wd, &bh, sizeof(bh));
790 return;
791 }
792 /* Write new #LargeBHead8 headers if enabled. Older Blender versions can't read those. */
793 if (!USER_DEVELOPER_TOOL_TEST(&U, write_legacy_blend_file_format)) {
795 static_assert(sizeof(BHead) == sizeof(LargeBHead8));
796 mywrite(wd, &bhead, sizeof(bhead));
797 return;
798 }
799 }
800 /* Write older #SmallBHead8 headers so that Blender versions that don't support #LargeBHead8 can
801 * read the file. */
802 SmallBHead8 bh;
803 bh.code = bhead.code;
804 bh.old = uint64_t(bhead.old);
805 bh.nr = bhead.nr;
806 bh.SDNAnr = bhead.SDNAnr;
807 bh.len = bhead.len;
808 /* Check that the written buffer size is compatible with the limits of #SmallBHead8. */
809 if (bhead.len > std::numeric_limits<decltype(bh.len)>::max()) {
810 CLOG_ERROR(&LOG, "Written .blend file is corrupt, because a memory block is too large.");
811 return;
812 }
813 mywrite(wd, &bh, sizeof(bh));
814}
815
823
825{
826 /* Add a stride. This is not strictly necessary but may help with debugging later on because it's
827 * easier to identify bad ids. */
828 uint64_t stable_id = hint << 4;
829 if (stable_id == 0) {
830 /* Null values are reserved for nullptr. */
831 stable_id = (1 << 4);
832 }
833 /* Remove the first bits, which are reserved for certain types of generated values (like
834 * actual stable ids used in some cases when writing undo steps). */
835 stable_id &= ~generated_address_id_mask;
836 return stable_id;
837}
838
840{
841 uint64_t stable_id = stable_id_from_hint(hint);
842 while (!wd.stable_address_ids.used_ids.add(stable_id)) {
843 /* Generate a new hint because there is a collision. Collisions are generally expected to be
844 * very rare. It can happen when #get_stable_pointer_hint_for_id produces values that are very
845 * close for different IDs. */
846 hint = XXH3_64bits(&hint, sizeof(uint64_t));
847 stable_id = stable_id_from_hint(hint);
848 }
849 hint++;
850 return stable_id;
851}
852
853static uint64_t get_address_id_int(WriteData &wd, const void *address)
854{
855 if (address == nullptr) {
856 return 0;
857 }
858 /* In undo case, addresses are kept as-is, unless they have been tagged by specific functions
859 * like `generated_pointer_tag`, in which case their value will already be in the
860 * `pointer_map`. */
861 if (wd.use_memfile) {
863 address, reinterpret_cast<uint64_t>(address));
864 }
865 /* Either reuse an existing identifier or create a new one. */
866 return wd.stable_address_ids.pointer_map.lookup_or_add_cb(address, [&]() {
868 });
869}
870
871static const void *get_address_id(WriteData &wd, const void *address)
872{
873 return reinterpret_cast<const void *>(get_address_id_int(wd, address));
874}
875
890
892 const int filecode,
893 const int struct_nr,
894 const int64_t nr,
895 const void *adr,
896 const void *data,
898{
899 BLI_assert(struct_nr > 0 && struct_nr <= dna::sdna_struct_id_get_max());
900
901 if (adr == nullptr || data == nullptr || nr == 0) {
902 return;
903 }
904
905 if (!write_at_address_validate(wd, filecode, adr)) {
906 return;
907 }
908
909 const int64_t len_in_bytes = nr * DNA_struct_size(wd->sdna, struct_nr);
911 USER_DEVELOPER_TOOL_TEST(&U, write_legacy_blend_file_format))
912 {
913 if (len_in_bytes > INT32_MAX) {
914 CLOG_ERROR(&LOG, "Cannot write chunks bigger than INT_MAX.");
915 return;
916 }
917 }
918
919 /* Get the address identifier that will be written to the file. */
920 const void *address_id = get_address_id(*wd, adr);
921
922 const void *data_to_write;
923 DynamicStackBuffer<16 * 1024> buffer_owner(len_in_bytes, 64);
924 const dna::pointers::StructInfo &struct_info =
925 wd->stable_address_ids.sdna_pointers->get_for_struct(struct_nr);
926
927 const bool needs_general_pointer_remap = !wd->use_memfile && !struct_info.pointers.is_empty();
928 const bool has_custom_fn = bool(fn);
929 const bool can_write_raw_runtime_data = !needs_general_pointer_remap && !has_custom_fn;
930
931 if (can_write_raw_runtime_data) {
932 /* The passed in data contains no pointers, so it can be written without an additional copy. */
933 data_to_write = data;
934 }
935 else {
936 void *buffer = buffer_owner.buffer();
937 data_to_write = buffer;
938 memcpy(buffer, data, len_in_bytes);
939
940 /* Optionally allow custom modifications to the struct data before it is written. */
941 if (has_custom_fn) {
942 for (const int i : IndexRange(nr)) {
943 const int offset = i * struct_info.size_in_bytes;
944 BlendStructWriterVoid struct_writer(
945 *wd,
946 struct_nr,
947 {static_cast<char *>(POINTER_OFFSET(buffer, offset)), struct_info.size_in_bytes});
948 fn(struct_writer);
949 }
950 }
951
952 /* When writing to file, use stable pointers for everything. */
953 if (needs_general_pointer_remap) {
954 /* Overwrite pointers with their corresponding address identifiers. */
955 for (const int i : IndexRange(nr)) {
956 for (const dna::pointers::PointerInfo &pointer_info : struct_info.pointers) {
957 const int offset = i * struct_info.size_in_bytes + pointer_info.offset;
958 const void **p_ptr = reinterpret_cast<const void **>(POINTER_OFFSET(buffer, offset));
959 const void *p_ptr_address_id = get_address_id(*wd, *p_ptr);
960 *p_ptr = p_ptr_address_id;
961 }
962 }
963 }
964 }
965
966 BHead bh;
967 bh.code = filecode;
968 bh.old = address_id;
969 bh.nr = nr;
970 bh.SDNAnr = struct_nr;
971 bh.len = len_in_bytes;
972
973 if (bh.len == 0) {
974 return;
975 }
976
977 if (wd->debug_dst) {
979 *wd->sdna, struct_nr, data_to_write, address_id, nr, *wd->debug_dst);
980 }
981
982 write_bhead(wd, bh);
983 mywrite(wd, data_to_write, size_t(bh.len));
984}
985
987{
988 if (!wd_->use_memfile) {
989 /* When writing to file, all pointers are remapped to stable pointers. */
990 return;
991 }
992#ifndef NDEBUG
993 const dna::pointers::StructInfo &struct_info =
995 BLI_assert(struct_info.has_pointer_at_offset(offset));
996#else
998#endif
999
1000 /* In undo case, replace generated pointers by corresponding stable pointers. */
1001 const void **p_ptr = reinterpret_cast<const void **>(POINTER_OFFSET(data_.data(), offset));
1002 if (!*p_ptr) {
1003 return;
1004 }
1005 /* Should exist if #generated_pointer_tag has been called before. */
1007 const void *p_ptr_address_id = get_address_id(*wd_, *p_ptr);
1008 *p_ptr = p_ptr_address_id;
1009}
1010
1012 const int filecode,
1013 const int struct_nr,
1014 const int64_t nr,
1015 const void *adr,
1016 const BlendStructWriterFn fn)
1017{
1018 writestruct_at_address_nr(wd, filecode, struct_nr, nr, adr, adr, fn);
1019}
1020
1022 const size_t len,
1023 const void *address_id,
1024 const void *data)
1025{
1026 fmt::memory_buffer buf;
1027 fmt::appender dst{buf};
1028
1029 fmt::format_to(dst, "<Raw Data> at {} ({} bytes)\n", address_id, len);
1030
1031 constexpr int bytes_per_row = 8;
1032 const int len_digits = std::to_string(std::max<size_t>(0, len - 1)).size();
1033
1034 for (size_t i = 0; i < len; i++) {
1035 if (i % bytes_per_row == 0) {
1036 fmt::format_to(dst, " {:{}}: ", i, len_digits);
1037 }
1038 fmt::format_to(dst, "{:02x} ", reinterpret_cast<const uint8_t *>(data)[i]);
1039 if (i % bytes_per_row == bytes_per_row - 1) {
1040 fmt::format_to(dst, "\n");
1041 }
1042 }
1043 if (len % bytes_per_row != 0) {
1044 fmt::format_to(dst, "\n");
1045 }
1046
1047 *wd->debug_dst << fmt::to_string(buf);
1048}
1049
1053static void writedata(
1054 WriteData *wd, const int filecode, const void *data, const size_t len, const void *adr)
1055{
1056 if (data == nullptr || len == 0) {
1057 return;
1058 }
1059
1060 if (!write_at_address_validate(wd, filecode, adr)) {
1061 return;
1062 }
1063
1065 USER_DEVELOPER_TOOL_TEST(&U, write_legacy_blend_file_format)) &&
1066 len > INT_MAX)
1067 {
1068 BLI_assert_msg(0, "Cannot write chunks bigger than INT_MAX.");
1069 return;
1070 }
1071
1072 const void *address_id = get_address_id(*wd, adr);
1073
1074 BHead bh;
1075 bh.code = filecode;
1076 bh.old = address_id;
1077 bh.nr = 1;
1078 BLI_STATIC_ASSERT(SDNA_RAW_DATA_STRUCT_INDEX == 0, "'raw data' SDNA struct index should be 0")
1080 bh.len = int64_t(len);
1081
1082 if (wd->debug_dst) {
1083 write_raw_data_in_debug_file(wd, len, address_id, data);
1084 }
1085
1086 write_bhead(wd, bh);
1087 mywrite(wd, data, len);
1088}
1089
1090static void writedata(WriteData *wd, const int filecode, const size_t len, const void *adr)
1091{
1092 writedata(wd, filecode, adr, len, adr);
1093}
1094
1098static void writelist_nr(WriteData *wd,
1099 const int filecode,
1100 const int struct_nr,
1101 const ListBase *lb,
1102 const BlendStructWriterFn fn)
1103{
1104 const Link *link = static_cast<Link *>(lb->first_);
1105
1106 while (link) {
1107 writestruct_nr(wd, filecode, struct_nr, 1, link, fn);
1108 link = link->next;
1109 }
1110}
1111
1112#if 0
1113static void writelist_id(WriteData *wd, const int filecode, const char *structname, const ListBase *lb)
1114{
1115 const Link *link = lb->first;
1116 if (link) {
1117
1118 const int struct_nr = DNA_struct_find_with_alias(wd->sdna, structname);
1119 if (struct_nr == -1) {
1120 printf("error: cannot find SDNA code <%s>\n", structname);
1121 return;
1122 }
1123
1124 while (link) {
1125 writestruct_nr(wd, filecode, struct_nr, 1, link);
1126 link = link->next;
1127 }
1128 }
1129}
1130#endif
1131
1132#define writestruct_at_address(wd, filecode, struct_id, nr, adr, data, fn) \
1133 writestruct_at_address_nr(wd, filecode, dna::sdna_struct_id_get<struct_id>(), nr, adr, data, fn)
1134
1135#define writestruct(wd, filecode, struct_id, nr, adr, fn) \
1136 writestruct_nr(wd, filecode, dna::sdna_struct_id_get<struct_id>(), nr, adr, fn)
1137
1139
1140/* -------------------------------------------------------------------- */
1145
1150static void current_screen_compat(Main *mainvar,
1151 const bool use_active_win,
1152 bScreen **r_screen,
1153 Scene **r_scene,
1154 ViewLayer **r_view_layer)
1155{
1157 wmWindow *window = nullptr;
1158
1159 /* Find a global current screen in the first open window, to have
1160 * a reasonable default for reading in older versions. */
1161 wm = mainvar->wm.first();
1162
1163 if (wm) {
1164 if (use_active_win) {
1165 /* Write the active window into the file, needed for multi-window undo #43424. */
1166 for (window = wm->windows.first(); window; window = window->next) {
1167 if (window->active) {
1168 break;
1169 }
1170 }
1171
1172 /* Fallback. */
1173 if (window == nullptr) {
1174 window = wm->windows.first();
1175 }
1176 }
1177 else {
1178 window = wm->windows.first();
1179 }
1180 }
1181
1182 *r_screen = (window) ? BKE_workspace_active_screen_get(window->workspace_hook) : nullptr;
1183 *r_scene = (window) ? window->scene : nullptr;
1184 *r_view_layer = (window && *r_scene) ? BKE_view_layer_find(*r_scene, window->view_layer_name) :
1185 nullptr;
1186}
1187
1189 int sfra;
1190 int efra;
1192};
1193
1200static void write_renderinfo(WriteData *wd, Main *mainvar)
1201{
1202 bScreen *curscreen;
1203 Scene *curscene = nullptr;
1204 ViewLayer *view_layer;
1205
1206 /* XXX: in future, handle multiple windows with multiple screens? */
1207 current_screen_compat(mainvar, false, &curscreen, &curscene, &view_layer);
1208
1209 for (Scene &sce : mainvar->scenes) {
1210 if (!ID_IS_LINKED(&sce) && (&sce == curscene || (sce.r.scemode & R_BG_RENDER))) {
1212 data.sfra = sce.r.sfra;
1213 data.efra = sce.r.efra;
1214 memset(data.scene_name, 0, sizeof(data.scene_name));
1215
1216 STRNCPY(data.scene_name, sce.id.name + 2);
1217
1218 writedata(wd, BLO_CODE_REND, sizeof(data), &data);
1219 }
1220 }
1221}
1222
1223static void write_keymapitem(BlendWriter *writer, const wmKeyMapItem *kmi)
1224{
1225 writer->write_struct(kmi);
1226 if (kmi->properties) {
1227 IDP_BlendWrite(writer, kmi->properties);
1228 }
1229}
1230
1231static void write_userdef(BlendWriter *writer, const UserDef *userdef)
1232{
1233 /* Filter out temporary/non user defined asset libraries. */
1234 ListBaseT<bUserAssetLibrary> asset_libraries_filtered = {nullptr, nullptr};
1235 ListBaseT<bUserAssetLibrary> asset_libraries_backup = userdef->asset_libraries;
1236
1237 for (const bUserAssetLibrary &asset_library_ref : userdef->asset_libraries) {
1238 if (asset_library_ref.flag & ASSET_LIBRARY_PROJECT_DEFINED) {
1239 /* Do not save project defined asset libraries. */
1240 continue;
1241 }
1242
1243 bUserAssetLibrary *copied_item = MEM_new<bUserAssetLibrary>(__func__);
1244 *copied_item = asset_library_ref;
1245 BLI_addtail(&asset_libraries_filtered, copied_item);
1246 }
1247
1248 ListBaseT<bUserAssetLibrary> *asset_libraries = const_cast<ListBaseT<bUserAssetLibrary> *>(
1249 &userdef->asset_libraries);
1250 /* Remap the first and last pointers of `userdef->asset_libraries`.
1251 * #writestruct will write out the pointers in here so make sure they are the same as our
1252 * filtered list.
1253 */
1254 asset_libraries->first_ = asset_libraries_filtered.first();
1255 asset_libraries->last_ = asset_libraries_filtered.last();
1256
1257 writestruct(writer->wd, BLO_CODE_USER, UserDef, 1, userdef, nullptr);
1258
1259 for (const bTheme &btheme : userdef->themes) {
1260 writer->write_struct(&btheme);
1261 }
1262
1263 for (const wmKeyMap &keymap : userdef->user_keymaps) {
1264 writer->write_struct(&keymap);
1265
1266 for (const wmKeyMapDiffItem &kmdi : keymap.diff_items) {
1267 writer->write_struct(&kmdi);
1268 if (kmdi.remove_item) {
1269 write_keymapitem(writer, kmdi.remove_item);
1270 }
1271 if (kmdi.add_item) {
1272 write_keymapitem(writer, kmdi.add_item);
1273 }
1274 }
1275
1276 for (const wmKeyMapItem &kmi : keymap.items) {
1277 write_keymapitem(writer, &kmi);
1278 }
1279 }
1280
1281 for (const wmKeyConfigPref &kpt : userdef->user_keyconfig_prefs) {
1282 writer->write_struct(&kpt);
1283 if (kpt.prop) {
1284 IDP_BlendWrite(writer, kpt.prop);
1285 }
1286 }
1287
1288 for (const bUserMenu &um : userdef->user_menus) {
1289 writer->write_struct(&um);
1290 for (const bUserMenuItem &umi : um.items) {
1291 if (umi.type == USER_MENU_TYPE_OPERATOR) {
1292 const bUserMenuItem_Op *umi_op = reinterpret_cast<const bUserMenuItem_Op *>(&umi);
1293 writer->write_struct(umi_op);
1294 if (umi_op->prop) {
1295 IDP_BlendWrite(writer, umi_op->prop);
1296 }
1297 }
1298 else if (umi.type == USER_MENU_TYPE_MENU) {
1299 const bUserMenuItem_Menu *umi_mt = reinterpret_cast<const bUserMenuItem_Menu *>(&umi);
1300 writer->write_struct(umi_mt);
1301 }
1302 else if (umi.type == USER_MENU_TYPE_PROP) {
1303 const bUserMenuItem_Prop *umi_pr = reinterpret_cast<const bUserMenuItem_Prop *>(&umi);
1304 writer->write_struct(umi_pr);
1305 }
1306 else {
1307 writer->write_struct(&umi);
1308 }
1309 }
1310 }
1311
1312 for (const bAddon &bext : userdef->addons) {
1313 writer->write_struct(&bext);
1314 if (bext.prop) {
1315 IDP_BlendWrite(writer, bext.prop);
1316 }
1317 }
1318
1319 for (const bPathCompare &path_cmp : userdef->autoexec_paths) {
1320 writer->write_struct(&path_cmp);
1321 }
1322
1323 for (const bUserScriptDirectory &script_dir : userdef->script_directories) {
1324 writer->write_struct(&script_dir);
1325 }
1326
1327 for (const bUserAssetLibrary &asset_library_ref : asset_libraries_filtered) {
1328 writer->write_struct(&asset_library_ref);
1329 BKE_preferences_asset_library_write_data(writer, &asset_library_ref);
1330 }
1331 BLI_freelistN(&asset_libraries_filtered);
1332 asset_libraries->first_ = asset_libraries_backup.first();
1333 asset_libraries->last_ = asset_libraries_backup.last();
1334
1335 for (const bUserExtensionRepo &repo_ref : userdef->extension_repos) {
1336 writer->write_struct(&repo_ref);
1338 }
1339 for (const bUserAssetShelfSettings &shelf_settings : userdef->asset_shelves_settings) {
1340 writer->write_struct(&shelf_settings);
1342 }
1343
1344 for (const uiStyle &style : userdef->uistyles) {
1345 writer->write_struct(&style);
1346 }
1347}
1348
1352static void write_id(WriteData *wd, ID *id)
1353{
1354 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id);
1355 mywrite_id_begin(wd, id);
1356 if (id_type->blend_write != nullptr) {
1357 BlendWriter writer = {wd};
1358 BLO_Write_IDBuffer id_buffer{*id, wd->use_memfile, false};
1359 id_type->blend_write(&writer, id_buffer.get(), id);
1360 }
1361 mywrite_id_end(wd, id);
1362}
1363
1364static void write_id_placeholder(WriteData *wd, ID *id)
1365{
1366 mywrite_id_begin(wd, id);
1367
1368 /* Only copy required data for the placeholder ID. */
1369 BLO_Write_IDBuffer id_buffer{*id, wd->use_memfile, true};
1370
1371 writestruct_at_address(wd, ID_LINK_PLACEHOLDER, ID, 1, id, &id_buffer, nullptr);
1372
1373 mywrite_id_end(wd, id);
1374}
1375
1382 MultiValueMap<Library *, ID *> &linked_ids_by_library,
1383 Set<Library *> &written_libraries,
1385{
1386 const bool is_undo = wd->use_memfile;
1387
1388 const Span<ID *> ids = linked_ids_by_library.lookup(&library);
1389
1390 /* Gather IDs that are somehow directly referenced by data in the current blend file. */
1391 Vector<ID *> ids_used_from_library;
1392 if (is_undo) {
1393 /* Always write placeholders for all linked IDs in undo case. This allows to properly remove
1394 * linked data that should not exist on undo/redo. See also #read_undo_move_libmain_data,
1395 * #read_libblock_undo_restore_linked and #read_undo_libraries_cleanup_unused_ids. */
1396 ids_used_from_library = ids;
1397 }
1398 else {
1399 for (ID *id : ids) {
1400 if (id->us == 0) {
1401 continue;
1402 }
1403 if (id->tag & ID_TAG_EXTERN) {
1404 ids_used_from_library.append(id);
1405 continue;
1406 }
1407 /* NOTE: If we ever want to support writing (and then linking) directly linked data for
1408 * copy-paste, we need to also consider linked IDs flagged with `ID_FLAG_CLIPBOARD_MARK` as
1409 * used here. */
1410 if ((id->tag & ID_TAG_INDIRECT) && (id->flag & ID_FLAG_INDIRECT_WEAK_LINK)) {
1411 ids_used_from_library.append(id);
1412 continue;
1413 }
1414 }
1415 }
1416
1417 bool should_write_library = false;
1418 if (library.packedfile) {
1419 should_write_library = true;
1420 }
1421 else if (!library.runtime->archived_libraries.is_empty()) {
1422 /* Reference 'real' blendfile library of archived 'copies' of it containing packed linked
1423 * IDs should always be written. */
1424 /* FIXME: A bit weak, as it could be that all archive libs are now empty (if all related
1425 * packed linked IDs have been deleted e.g.)...
1426 * Could be fixed by either adding more checks here, or ensuring empty archive libs are
1427 * deleted when no ID uses them anymore? */
1428 should_write_library = true;
1429 }
1430 else if (is_undo) {
1431 /* When writing undo step we always write all existing libraries. That makes reading undo
1432 * step much easier when dealing with purely indirectly used libraries. */
1433 should_write_library = true;
1434 }
1435 else {
1436 should_write_library = !ids_used_from_library.is_empty();
1437 }
1438
1439 if (!should_write_library) {
1440 /* Nothing from the library is used, so it does not have to be written. */
1441 return false;
1442 }
1443
1444 /* Since code below checking that archive libraries' parents have already been written, may
1445 * forcefully write that parent library in some cases, also double-check here that current
1446 * library has not yet been written.
1447 *
1448 * Note: In theory this should never be the case, as a parent library is always expected to be
1449 * written before its archives. */
1450 if (written_libraries.contains(&library)) {
1451 CLOG_ERROR(
1452 &LOG, "Attempt to re-write already written library '%s', skipping", library.id.name);
1453 return false;
1454 }
1455
1456 if (library.flag & LIBRARY_FLAG_IS_ARCHIVE) {
1457 if (!library.archive_parent_library) {
1458 CLOG_ERROR(
1459 &LOG, "Written archive library '%s' has no parent library, skipping", library.id.name);
1460 return false;
1461 }
1462 if (!written_libraries.contains(library.archive_parent_library)) {
1463 CLOG_ERROR(
1464 &LOG,
1465 "Written archive library '%s', while its parent library '%s' has not been written",
1466 library.id.name,
1467 library.archive_parent_library->id.name);
1468
1469 /* Only write the parent library itself, if it was not written so far, none of its IDs was
1470 * to be written either. */
1471 write_id(wd, &library.archive_parent_library->id);
1472 written_libraries.add(library.archive_parent_library);
1473 }
1474 }
1475
1476 write_id(wd, &library.id);
1477 written_libraries.add(&library);
1478
1479 /* Write placeholders for linked data-blocks that are used, and real IDs for the packed linked
1480 * ones. */
1481 for (ID *id : ids_used_from_library) {
1482 if (ID_IS_PACKED(id)) {
1483 write_id(wd, id);
1484 }
1485 else {
1486 /* In undo case, all existing linked IDs get a placeholder, even the ones not directly
1487 * linkable. */
1488 if (!is_undo && !BKE_idtype_idcode_is_linkable(id->id_type())) {
1489 CLOG_ERROR(&LOG,
1490 "Data-block '%s' from lib '%s' is not linkable, but is flagged as "
1491 "directly linked",
1492 id->name,
1493 library.runtime->filepath_abs);
1494 }
1495 write_id_placeholder(wd, id);
1496 }
1497 }
1498 return true;
1499}
1500
1502static void write_libraries(WriteData *wd, Main *bmain)
1503{
1504 /* Gather IDs coming from each library. */
1505 MultiValueMap<Library *, ID *> linked_ids_by_library;
1506 for (ID &id : MainAllIDsIterator(*bmain)) {
1507 if (!ID_IS_LINKED(&id)) {
1508 continue;
1509 }
1510 BLI_assert(id.lib);
1511 linked_ids_by_library.add(id.lib, &id);
1512 }
1513
1514 Set<Library *> written_libraries;
1515 for (Library &library : bmain->libraries) {
1516 /* This ensures parent regular library is always written immediately before all of its archive
1517 * 'children'. Due to ID naming, actual order of these libraries in Main is unknown. */
1518 if (library.flag & LIBRARY_FLAG_IS_ARCHIVE) {
1519 continue;
1520 }
1521 const bool is_mainlib_written = write_library_if_needed(
1522 wd, linked_ids_by_library, written_libraries, library);
1523 UNUSED_VARS_NDEBUG(is_mainlib_written);
1524 for (Library *archive_library : library.runtime->archived_libraries) {
1525 BLI_assert(archive_library->flag & LIBRARY_FLAG_IS_ARCHIVE);
1526 const bool is_archivelib_written = write_library_if_needed(
1527 wd, linked_ids_by_library, written_libraries, *archive_library);
1529 is_mainlib_written || !is_archivelib_written,
1530 "Archive libraries should never be written if their owner 'parent' library is not");
1531 UNUSED_VARS_NDEBUG(is_archivelib_written);
1532 }
1533 }
1534
1535 mywrite_flush(wd);
1536}
1537
1538#ifdef WITH_BUILDINFO
1539extern "C" ulong build_commit_timestamp;
1540extern "C" char build_hash[];
1541#endif
1542
1548static void write_global(WriteData *wd, const int fileflags, Main *mainvar)
1549{
1550 const bool is_undo = wd->use_memfile;
1551 FileGlobal fg;
1552 bScreen *screen;
1553 Scene *scene;
1554 ViewLayer *view_layer;
1555 char subvstr[8];
1556
1557 /* Prevent memory checkers from complaining. */
1558 memset(fg._pad, 0, sizeof(fg._pad));
1559 memset(fg.filepath, 0, sizeof(fg.filepath));
1560 memset(fg.build_hash, 0, sizeof(fg.build_hash));
1561 fg._pad1 = nullptr;
1562
1563 current_screen_compat(mainvar, is_undo, &screen, &scene, &view_layer);
1564
1565 /* XXX: still remap `G`. */
1566 fg.curscreen = screen;
1567 fg.curscene = scene;
1568 fg.cur_view_layer = view_layer;
1569
1572
1573 /* Prevent to save this, is not good convention, and feature with concerns. */
1574 fg.fileflags = (fileflags & ~G_FILE_FLAG_ALL_RUNTIME);
1575
1576 fg.globalf = G.f;
1577 /* Write information needed for recovery. */
1578 if (fileflags & G_FILE_RECOVER_WRITE) {
1579 STRNCPY(fg.filepath, mainvar->filepath);
1580 /* Compression is often turned of when writing recovery files. However, when opening the file,
1581 * it should be enabled again. */
1582 fg.fileflags = G.fileflags & G_FILE_COMPRESS;
1583 }
1584 SNPRINTF(subvstr, "%4d", BLENDER_FILE_SUBVERSION);
1585 memcpy(fg.subvstr, subvstr, 4);
1586
1590#ifdef WITH_BUILDINFO
1591 /* TODO(sergey): Add branch name to file as well? */
1594#else
1596 STRNCPY(fg.build_hash, "unknown");
1597#endif
1598 writestruct(wd, BLO_CODE_GLOB, FileGlobal, 1, &fg, nullptr);
1599}
1600
1606static void write_thumb(WriteData *wd, const BlendThumbnail *thumb)
1607{
1608 if (thumb) {
1609 writedata(wd, BLO_CODE_TEST, BLEN_THUMB_MEMSIZE_FILE(thumb->width, thumb->height), thumb);
1610 }
1611}
1612
1614
1615/* -------------------------------------------------------------------- */
1618
1619BLO_Write_IDBuffer::BLO_Write_IDBuffer(ID &id, const bool is_undo, const bool is_placeholder)
1620 : buffer_(is_placeholder ? sizeof(ID) : BKE_idtype_get_info_from_id(&id)->struct_size,
1621 alignof(ID))
1622{
1623 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(&id);
1624 ID *temp_id = static_cast<ID *>(buffer_.buffer());
1625
1626 if (is_undo) {
1627 /* Record the changes that happened up to this undo push in
1628 * recalc_up_to_undo_push, and clear `recalc_after_undo_push` again
1629 * to start accumulating for the next undo push. */
1630 id.recalc_up_to_undo_push = id.recalc_after_undo_push;
1631 id.recalc_after_undo_push = 0;
1632 }
1633
1634 /* Copy ID data itself into buffer, to be able to freely modify it. */
1635
1636 if (is_placeholder) {
1637 /* For placeholders (references to linked data), zero-initialize, and only explicitly copy the
1638 * very small subset of required data. */
1639 *temp_id = ID{};
1640 temp_id->lib = id.lib;
1641 STRNCPY(temp_id->name, id.name);
1642 temp_id->flag = id.flag;
1643 temp_id->session_uid = id.session_uid;
1644 if (is_undo) {
1645 temp_id->recalc_up_to_undo_push = id.recalc_up_to_undo_push;
1646 temp_id->tag = id.tag & ID_TAG_KEEP_ON_UNDO;
1647 }
1648 return;
1649 }
1650
1651 /* Common ID-header runtime fields are cleared when the ID is written via
1652 * #BlendWriter::write_id_struct. The shallow copy here is still necessary because some functions
1653 * like #mesh_blend_write still override the data. */
1654 memcpy(temp_id, &id, id_type->struct_size);
1655}
1656
1658 : BLO_Write_IDBuffer(id, writer->is_undo(), false)
1659{
1660}
1661
1662static std::string get_blend_file_header()
1663{
1665 !USER_DEVELOPER_TOOL_TEST(&U, write_legacy_blend_file_format))
1666 {
1667 const int header_size_in_bytes = SIZEOFBLENDERHEADER_VERSION_1;
1668
1669 /* New blend file header format. */
1670 std::stringstream ss;
1671 ss << "BLENDER";
1672 ss << header_size_in_bytes;
1673 ss << '-';
1674 ss << std::setfill('0') << std::setw(2) << BLEND_FILE_FORMAT_VERSION_1;
1675 ss << 'v';
1676 ss << std::setfill('0') << std::setw(4) << BLENDER_FILE_VERSION;
1677
1678 const std::string header = ss.str();
1679 BLI_assert(header.size() == header_size_in_bytes);
1680 return header;
1681 }
1682
1683 const char pointer_size_char = sizeof(void *) == 8 ? '-' : '_';
1684 const char endian_char = 'v';
1685
1686 /* Legacy blend file header format. */
1687 std::stringstream ss;
1688 ss << "BLENDER";
1689 ss << pointer_size_char;
1690 ss << endian_char;
1692 const std::string header = ss.str();
1693 BLI_assert(header.size() == SIZEOFBLENDERHEADER_VERSION_0);
1694 return header;
1695}
1696
1698{
1699 const std::string header = get_blend_file_header();
1700 mywrite(wd, header.data(), header.size());
1701}
1702
1706static Vector<ID *> gather_local_ids_to_write(Main *bmain, const bool is_undo)
1707{
1708 Vector<ID *> local_ids_to_write;
1709 for (ID &id : MainAllIDsIterator(*bmain)) {
1710 if (id.id_type() == ID_LI) {
1711 /* Libraries are handled separately below. */
1712 continue;
1713 }
1714 if (ID_IS_LINKED(&id)) {
1715 /* Linked data-blocks are handled separately below. */
1716 continue;
1717 }
1718 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(&id);
1719 UNUSED_VARS_NDEBUG(id_type);
1720 /* We should never attempt to write non-regular IDs
1721 * (i.e. all kind of temp/runtime ones). */
1723 /* We only write unused IDs in undo case. */
1724 if (!is_undo) {
1725 /* NOTE: All 'never unused' local IDs (Scenes, WindowManagers, ...) should always be
1726 * written to disk, so their user-count should never be zero currently. Note that
1727 * libraries have already been skipped above, as they need a specific handling. */
1728 if (id.us == 0) {
1729 /* FIXME: #124857: Some old files seem to cause incorrect handling of their temp
1730 * screens.
1731 *
1732 * See e.g. file attached to #124777 (from 2.79.1).
1733 *
1734 * For now ignore, issue is not obvious to track down (`temp` bScreen ID from read data
1735 * _does_ have the proper `temp` tag), and seems anecdotal at worst. */
1736 BLI_assert((id_type->flags & IDTYPE_FLAGS_NEVER_UNUSED) == 0);
1737 continue;
1738 }
1739
1740 /* XXX Special handling for ShapeKeys, as having unused shape-keys is not a good thing
1741 * (and reported as error by e.g. `BLO_main_validate_shapekeys`), skip writing shape-keys
1742 * when their 'owner' is not written.
1743 *
1744 * NOTE: Since ShapeKeys are conceptually embedded IDs (like root node trees e.g.), this
1745 * behavior actually makes sense anyway. This remains more of a temp hack until topic of
1746 * how to handle unused data on save is properly tackled. */
1747 if (id.id_type() == ID_KE) {
1748 Key &shape_key = id_cast<Key &>(id);
1749 /* NOTE: Here we are accessing the real owner ID data, not it's 'proxy' shallow copy
1750 * generated for its file-writing. This is not expected to be an issue, but is worth
1751 * noting. */
1752 if (shape_key.from == nullptr || shape_key.from->us == 0) {
1753 continue;
1754 }
1755 }
1756 }
1757
1758 if ((id.tag & ID_TAG_RUNTIME) != 0 && !is_undo) {
1759 /* Runtime IDs are never written to .blend files, and they should not influence
1760 * (in)direct status of linked IDs they may use. */
1761 continue;
1762 }
1763
1764 local_ids_to_write.append(&id);
1765 }
1766 return local_ids_to_write;
1767}
1768
1774{
1775 for (ID &id : MainAllIDsIterator(bmain)) {
1776 /* Ensure no other stable pointer has been created before. */
1778
1779 /* IDs themselves never need to get stable addresses in undo case. */
1780 if (wd.use_memfile) {
1781 continue;
1782 }
1783
1784 /* Derive the stable pointer from the id/library name which is independent of the write-order
1785 * of data-blocks. */
1787 const uint64_t address_id = get_next_stable_address_id(wd, hint);
1788
1789 /* Store the computed stable pointer so that it is used whenever the data-block is written or
1790 * referenced. */
1791 wd.stable_address_ids.pointer_map.add(&id, address_id);
1792 }
1793}
1794
1801static bool write_file_handle(Main *mainvar,
1802 WriteWrap *ww,
1803 StringRefNull filepath,
1804 MemFile *compare,
1805 MemFile *current,
1806 const int write_flags,
1807 const bool use_userdef,
1808 const BlendThumbnail *thumb,
1809 std::ostream *debug_dst)
1810{
1811 WriteData *wd;
1812
1813 wd = mywrite_begin(ww, compare, current);
1814 wd->debug_dst = debug_dst;
1815 wd->filepath = filepath;
1816 BlendWriter writer = {wd};
1817
1819
1820 prepare_stable_data_block_ids(*wd, *mainvar);
1821
1822 /* Recompute all ID user-counts if requested. Allows to avoid skipping writing of IDs wrongly
1823 * detected as unused due to invalid user-count. */
1824 if (!wd->use_memfile) {
1825 if (USER_DEVELOPER_TOOL_TEST(&U, use_recompute_usercount_on_save_debug)) {
1826 BKE_main_id_refcount_recompute(mainvar, false);
1827 }
1828 }
1829
1831 write_renderinfo(wd, mainvar);
1832 write_thumb(wd, thumb);
1833 write_global(wd, write_flags, mainvar);
1834
1835 /* The window-manager and screen often change,
1836 * avoid thumbnail detecting changes because of this. */
1837 mywrite_flush(wd);
1838
1839 const bool is_undo = wd->use_memfile;
1840 Vector<ID *> local_ids_to_write = gather_local_ids_to_write(mainvar, is_undo);
1841
1842 if (!is_undo) {
1843 BKE_main_id_indirect_linked_update(*mainvar, local_ids_to_write);
1844
1845 /* Forcefully ensure we know about all needed override operations. */
1846 for (ID *id : local_ids_to_write) {
1848 BKE_lib_override_library_operations_create(mainvar, id, nullptr);
1849 }
1850 }
1851 }
1852
1853 /* Actually write local data-blocks to the file. */
1854 for (ID *id : local_ids_to_write) {
1855 write_id(wd, id);
1856 }
1857
1858 /* Write libraries about libraries and linked data-blocks. */
1859 write_libraries(wd, mainvar);
1860
1861 /* So changes above don't cause a 'DNA1' to be detected as changed on undo. */
1862 mywrite_flush(wd);
1863
1864 if (use_userdef) {
1865 write_userdef(&writer, &U);
1866 }
1867
1868 /* Write DNA last, because (to be implemented) test for which structs are written.
1869 *
1870 * Note that we *borrow* the pointer to 'DNAstr',
1871 * so writing each time uses the same address and doesn't cause unnecessary undo overhead. */
1872 writedata(wd, BLO_CODE_DNA1, size_t(wd->sdna->data_size), wd->sdna->data);
1873
1874 /* End of file. */
1875 BHead bhead{};
1876 bhead.code = BLO_CODE_ENDB;
1877 write_bhead(wd, bhead);
1878
1879 return mywrite_end(wd);
1880}
1881
1886static bool do_history(const char *filepath, ReportList *reports)
1887{
1888 /* Add 2 because version number maximum is double-digits. */
1889 char filepath_tmp1[FILE_MAX + 2], filepath_tmp2[FILE_MAX + 2];
1890 int version_number = min_ii(99, U.versions);
1891
1892 if (version_number == 0) {
1893 return true;
1894 }
1895
1896 if (strlen(filepath) < 2) {
1897 BKE_report(reports, RPT_ERROR, "Unable to make version backup: filename too short");
1898 return false;
1899 }
1900
1901 while (version_number > 1) {
1902 SNPRINTF(filepath_tmp1, "%s%d", filepath, version_number - 1);
1903 if (BLI_exists(filepath_tmp1)) {
1904 SNPRINTF(filepath_tmp2, "%s%d", filepath, version_number);
1905
1906 if (BLI_rename_overwrite(filepath_tmp1, filepath_tmp2)) {
1907 BKE_report(reports, RPT_ERROR, "Unable to make version backup");
1908 return false;
1909 }
1910 }
1911 version_number--;
1912 }
1913
1914 /* Needed when `version_number == 1`. */
1915 if (BLI_exists(filepath)) {
1916 SNPRINTF(filepath_tmp1, "%s%d", filepath, version_number);
1917
1918 if (BLI_rename_overwrite(filepath, filepath_tmp1)) {
1919 BKE_report(reports, RPT_ERROR, "Unable to make version backup");
1920 return false;
1921 }
1922 }
1923
1924 return true;
1925}
1926
1928 const BlendFileWriteParams &params,
1929 ReportList *reports)
1930{
1931 if (!bmain.lock) {
1932 return;
1933 }
1934
1935 if (G.debug & G_DEBUG_IO) {
1936 BKE_report(
1937 reports, RPT_DEBUG, "Checking validity of current .blend file *BEFORE* save to disk");
1938 }
1939
1940 BLO_main_validate_shapekeys(&bmain, reports);
1942 BKE_report(reports,
1943 RPT_ERROR,
1944 "Critical data corruption: Conflicts and/or otherwise invalid data-blocks names "
1945 "(see console for details)");
1946 }
1947
1948 if (G.debug & G_DEBUG_IO) {
1949 BLO_main_validate_libraries(&bmain, reports);
1950 }
1951
1952 if (!params.is_copypaste_buffer) {
1953 bool has_clipboard_flag = false;
1954 for (ID &id_iter : MainAllIDsIterator(bmain)) {
1955 if (id_iter.flag & ID_FLAG_CLIPBOARD_MARK) {
1956 CLOG_WARN(&LOG,
1957 "Found an ID '%s' flagged as copy/paste clipboard data while writing a regular "
1958 "blendfile, clearing the flag",
1959 id_iter.name);
1960 id_iter.flag &= ~ID_FLAG_CLIPBOARD_MARK;
1961 has_clipboard_flag = true;
1962 }
1963 }
1964 if (has_clipboard_flag) {
1965 BKE_report(reports,
1966 RPT_INFO,
1967 "Found IDs flagged as copy/paste clipboard data while writing a regular "
1968 "blendfile, the flag has been cleared in these IDs");
1969 }
1970 }
1971}
1972
1973static void write_file_main_validate_post(Main *bmain, ReportList *reports)
1974{
1975 if (!bmain->lock) {
1976 return;
1977 }
1978
1979 if (G.debug & G_DEBUG_IO) {
1980 BKE_report(
1981 reports, RPT_DEBUG, "Checking validity of current .blend file *AFTER* save to disk");
1982 BLO_main_validate_libraries(bmain, reports);
1983 }
1984}
1985
1986static bool BLO_write_file_impl(Main *mainvar,
1987 const char *filepath,
1988 const int write_flags,
1989 const BlendFileWriteParams *params,
1990 ReportList *reports,
1991 WriteWrap &ww)
1992{
1993 BLI_assert(!BLI_path_is_rel(filepath));
1995
1996 char tempname[FILE_MAX + 1];
1997
1998 eBLO_WritePathRemap remap_mode = params->remap_mode;
1999 const bool use_save_versions = params->use_save_versions;
2000 const bool use_save_as_copy = params->use_save_as_copy;
2001 const bool use_userdef = params->use_userdef;
2002 const BlendThumbnail *thumb = params->thumb;
2003 const bool relbase_valid = (mainvar->filepath[0] != '\0');
2004
2005 /* Extra protection: Never save a non asset file as asset file. Otherwise a normal file is turned
2006 * into an asset file, which can result in data loss because the asset system will allow editing
2007 * this file from the UI, regenerating its content with just the asset and it dependencies. */
2008 if ((write_flags & G_FILE_ASSET_EDIT_FILE) && !mainvar->is_asset_edit_file) {
2009 BKE_reportf(reports, RPT_ERROR, "Cannot save normal file (%s) as asset system file", tempname);
2010 return false;
2011 }
2012
2013 /* Path backup/restore. */
2014 void *path_list_backup = nullptr;
2017
2018 write_file_main_validate_pre(*mainvar, *params, reports);
2019
2020 /* Open temporary file, so we preserve the original in case we crash. */
2021 SNPRINTF(tempname, "%s@", filepath);
2022
2023 if (ww.open(tempname) == false) {
2025 reports, RPT_ERROR, "Cannot open file %s for writing: %s", tempname, strerror(errno));
2026 return false;
2027 }
2028
2029 if (remap_mode == BLO_WRITE_PATH_REMAP_ABSOLUTE) {
2030 /* Paths will already be absolute, no remapping to do. */
2031 if (relbase_valid == false) {
2032 remap_mode = BLO_WRITE_PATH_REMAP_NONE;
2033 }
2034 }
2035
2036 /* Remapping of relative paths to new file location. */
2037 if (remap_mode != BLO_WRITE_PATH_REMAP_NONE) {
2038 if (remap_mode == BLO_WRITE_PATH_REMAP_RELATIVE) {
2039 /* Make all relative as none of the existing paths can be relative in an unsaved document. */
2040 if (relbase_valid == false) {
2042 }
2043 }
2044
2045 /* The source path only makes sense to set if the file was saved (`relbase_valid`). */
2046 char dir_src[FILE_MAX];
2047 char dir_dst[FILE_MAX];
2048
2049 /* Normalize the paths in case there is some subtle difference (so they can be compared). */
2050 if (relbase_valid) {
2051 BLI_path_split_dir_part(mainvar->filepath, dir_src, sizeof(dir_src));
2052 BLI_path_normalize(dir_src);
2053 }
2054 else {
2055 dir_src[0] = '\0';
2056 }
2057 BLI_path_split_dir_part(filepath, dir_dst, sizeof(dir_dst));
2058 BLI_path_normalize(dir_dst);
2059
2060 /* Only for relative, not relative-all, as this means making existing paths relative. */
2061 if (remap_mode == BLO_WRITE_PATH_REMAP_RELATIVE) {
2062 if (relbase_valid && (BLI_path_cmp(dir_dst, dir_src) == 0)) {
2063 /* Saved to same path. Nothing to do. */
2064 remap_mode = BLO_WRITE_PATH_REMAP_NONE;
2065 }
2066 }
2067 else if (remap_mode == BLO_WRITE_PATH_REMAP_ABSOLUTE) {
2068 if (relbase_valid == false) {
2069 /* Unsaved, all paths are absolute.Even if the user manages to set a relative path,
2070 * there is no base-path that can be used to make it absolute. */
2071 remap_mode = BLO_WRITE_PATH_REMAP_NONE;
2072 }
2073 }
2074
2075 if (remap_mode != BLO_WRITE_PATH_REMAP_NONE) {
2076 /* Some path processing (e.g. with libraries) may use the current `main->filepath`, if this
2077 * is not matching the path currently used for saving, unexpected paths corruptions can
2078 * happen. See #98201. */
2079 char mainvar_filepath_orig[FILE_MAX];
2080 STRNCPY(mainvar_filepath_orig, mainvar->filepath);
2081 STRNCPY(mainvar->filepath, filepath);
2082
2083 /* Check if we need to backup and restore paths. */
2084 if (use_save_as_copy) [[unlikely]] {
2085 path_list_backup = BKE_bpath_list_backup(mainvar, path_list_flag);
2086 }
2087
2088 switch (remap_mode) {
2090 /* Saved, make relative paths relative to new location (if possible). */
2091 BLI_assert(relbase_valid);
2092 BKE_bpath_relative_rebase(mainvar, dir_src, dir_dst, nullptr);
2093 break;
2095 /* If saved, make relative paths relative to new location (if possible). */
2096 if (relbase_valid && (BLI_path_cmp(dir_dst, dir_src) != 0)) {
2097 BKE_bpath_relative_rebase(mainvar, dir_src, dir_dst, nullptr);
2098 }
2099 /* Make all absolute paths relative (when requested or unsaved). */
2100 BKE_bpath_relative_convert(mainvar, dir_dst, nullptr);
2101 break;
2103 /* Make all absolute (when requested or unsaved). */
2104 BLI_assert(relbase_valid);
2105 BKE_bpath_absolute_convert(mainvar, dir_src, nullptr);
2106 break;
2108 BLI_assert_unreachable(); /* Unreachable. */
2109 break;
2110 }
2111
2112 STRNCPY(mainvar->filepath, mainvar_filepath_orig);
2113 }
2114 }
2115
2116#if GENERATE_DEBUG_BLEND_FILE
2117 std::string debug_dst_path = StringRef(filepath) + DEBUG_BLEND_FILE_SUFFIX;
2118 fstream debug_dst_file(debug_dst_path, std::ios::out);
2119 std::ostream *debug_dst = &debug_dst_file;
2120#else
2121 std::ostream *debug_dst = nullptr;
2122#endif
2123
2124 /* Actual file writing. */
2125 const bool err = write_file_handle(
2126 mainvar, &ww, filepath, nullptr, nullptr, write_flags, use_userdef, thumb, debug_dst);
2127
2128 const bool close_error = !ww.close();
2129
2130 if (path_list_backup) [[unlikely]] {
2131 BKE_bpath_list_restore(mainvar, path_list_flag, path_list_backup);
2132 BKE_bpath_list_free(path_list_backup);
2133 }
2134
2135 if (err || close_error) {
2136 if (err) {
2137 /* Note: `errno` will often be meaningless in case of a zstd compression error. */
2138 BKE_reportf(reports, RPT_ERROR, "Failed to write blendfile: %s", strerror(errno));
2139 }
2140 else {
2141 BKE_report(reports, RPT_ERROR, "Failed to write blendfile");
2142 }
2143 remove(tempname);
2144
2145 return false;
2146 }
2147
2148 /* File save to temporary file was successful, now do reverse file history
2149 * (move `.blend1` -> `.blend2`, `.blend` -> `.blend1` .. etc). */
2150 if (use_save_versions) {
2151 if (!do_history(filepath, reports)) {
2152 BKE_report(reports, RPT_ERROR, "Version backup failed (file saved with @)");
2153 return false;
2154 }
2155 }
2156
2157 if (BLI_rename_overwrite(tempname, filepath) != 0) {
2158 BKE_report(reports, RPT_ERROR, "Cannot change old file (file saved with @)");
2159 return false;
2160 }
2161
2162 write_file_main_validate_post(mainvar, reports);
2163 if (mainvar->is_global_main && !params->use_save_as_copy) {
2164 /* It is used to reload Blender after a crash on Windows OS. */
2165 STRNCPY(G.filepath_last_blend, filepath);
2166 }
2167 return true;
2168}
2169
2171
2172/* -------------------------------------------------------------------- */
2175
2176bool BLO_write_file(Main *mainvar,
2177 const char *filepath,
2178 const int write_flags,
2179 const BlendFileWriteParams *params,
2180 ReportList *reports)
2181{
2182 RawWriteWrap raw_wrap;
2183
2184 if (write_flags & G_FILE_COMPRESS) {
2185 ZstdWriteWrap zstd_wrap(raw_wrap);
2186 return BLO_write_file_impl(mainvar, filepath, write_flags, params, reports, zstd_wrap);
2187 }
2188
2189 return BLO_write_file_impl(mainvar, filepath, write_flags, params, reports, raw_wrap);
2190}
2191
2192bool BLO_write_file_mem(Main *mainvar, MemFile *compare, MemFile *current, const int write_flags)
2193{
2194 bool use_userdef = false;
2195
2196 const bool err = write_file_handle(
2197 mainvar, nullptr, "", compare, current, write_flags, use_userdef, nullptr, nullptr);
2198
2199 return (err == 0);
2200}
2201
2203{
2204 return this->wd->use_memfile;
2205}
2206
2207/*
2208 * API to write chunks of data.
2209 */
2210
2211void BlendWriter::write_struct_by_name(const char *struct_name,
2212 const void *data,
2213 const BlendStructWriterFn fn)
2214{
2215 this->write_struct_array_by_name(struct_name, 1, data, fn);
2216}
2217
2218void BlendWriter::write_struct_array_by_name(const char *struct_name,
2219 const int64_t array_size,
2220 const void *data,
2221 const BlendStructWriterFn fn)
2222{
2223 int struct_id = this->struct_id_by_name(struct_name);
2224 if (struct_id == -1) [[unlikely]] {
2225 CLOG_ERROR(&LOG, "Can't find SDNA code <%s>", struct_name);
2226 return;
2227 }
2228 this->write_struct_array_by_id(struct_id, array_size, data, fn);
2229}
2230
2231void BlendWriter::write_struct_by_id(const int struct_id,
2232 const void *data,
2233 const BlendStructWriterFn fn)
2234{
2235 writestruct_nr(this->wd, BLO_CODE_DATA, struct_id, 1, data, fn);
2236}
2237
2239 const void *address,
2240 const void *data,
2241 const BlendStructWriterFn fn)
2242{
2244}
2245
2247 const int struct_id,
2248 const void *address,
2249 const void *data,
2250 const BlendStructWriterFn fn)
2251{
2252 writestruct_at_address_nr(this->wd, filecode, struct_id, 1, address, data, fn);
2253}
2254
2256 const int64_t array_size,
2257 const void *data,
2258 const BlendStructWriterFn fn)
2259{
2260 writestruct_nr(this->wd, BLO_CODE_DATA, struct_id, array_size, data, fn);
2261}
2262
2264 const int64_t array_size,
2265 const void *address,
2266 const void *data,
2267 const BlendStructWriterFn fn)
2268{
2269 writestruct_at_address_nr(this->wd, BLO_CODE_DATA, struct_id, array_size, address, data, fn);
2270}
2271
2273 const ListBase *list,
2274 const BlendStructWriterFn fn)
2275{
2276 writelist_nr(this->wd, BLO_CODE_DATA, struct_id, list, fn);
2277}
2278
2279void BlendWriter::write_struct_list_by_name(const char *struct_name,
2280 ListBase *list,
2281 const BlendStructWriterFn fn)
2282{
2283 int struct_id = this->struct_id_by_name(struct_name);
2284 if (struct_id == -1) [[unlikely]] {
2285 CLOG_ERROR(&LOG, "Can't find SDNA code <%s>", struct_name);
2286 return;
2287 }
2288 this->write_struct_list_by_id(struct_id, list, fn);
2289}
2290
2291int BlendWriter::struct_id_by_name(const char *struct_name) const
2292{
2293 int struct_id = DNA_struct_find_with_alias(this->wd->sdna, struct_name);
2294 return struct_id;
2295}
2296
2297void BlendWriter::write_raw(size_t size_in_bytes, const void *data)
2298{
2299 writedata(this->wd, BLO_CODE_DATA, size_in_bytes, data);
2300}
2301
2303{
2304 this->write_raw(sizeof(char) * size_t(num), data);
2305}
2306
2308{
2309 this->write_raw(sizeof(int8_t) * size_t(num), data);
2310}
2311
2313{
2314 this->write_raw(sizeof(int16_t) * size_t(num), data);
2315}
2316
2318{
2319 this->write_raw(sizeof(uint8_t) * size_t(num), data);
2320}
2321
2323{
2324 this->write_raw(sizeof(int32_t) * size_t(num), data);
2325}
2326
2328{
2329 this->write_raw(sizeof(uint32_t) * size_t(num), data);
2330}
2331
2333{
2334 this->write_raw(sizeof(float) * size_t(num), data);
2335}
2336
2338{
2339 this->write_raw(sizeof(double) * size_t(num), data);
2340}
2341
2343{
2344 this->write_raw(sizeof(float[3]) * size_t(num), data);
2345}
2346
2348{
2349 /* Create a temporary copy of the pointer array, because all pointers need to be remapped to
2350 * their stable address ids. */
2352 reinterpret_cast<const void *const *>(data_ptr), num);
2353 for (const int64_t i : data.index_range()) {
2354 data[i] = get_address_id(*this->wd, data[i]);
2355 }
2356
2357 writedata(this->wd, BLO_CODE_DATA, data.data(), data.as_span().size_in_bytes(), data_ptr);
2358}
2359
2361{
2362 if (data != nullptr) {
2363 this->write_raw(strlen(data) + 1, data);
2364 }
2365}
2366
2368{
2369 if (!data) {
2370 return;
2371 }
2372 if (!this->is_undo()) {
2373 return;
2374 }
2375 const uint64_t address_id = get_address_id_for_undo(*this->wd);
2376 /* Check that the pointer has not been written before it was tagged as being generated. */
2377 BLI_assert(this->wd->stable_address_ids.pointer_map.lookup_default(data, address_id) ==
2378 address_id);
2379 this->wd->stable_address_ids.pointer_map.add(data, address_id);
2380}
2381
2383{
2384 return this->wd->filepath;
2385}
2386
2388 const size_t approximate_size_in_bytes,
2389 const ImplicitSharingInfo *sharing_info,
2390 const FunctionRef<void()> write_fn)
2391{
2392 if (data == nullptr) {
2393 return;
2394 }
2395 const uint64_t address_id = get_address_id_int(*this->wd, data);
2396 if (this->is_undo()) {
2397 MemFile &memfile = *this->wd->mem.written_memfile;
2398 if (sharing_info != nullptr) {
2399 if (memfile.shared_storage == nullptr) {
2400 memfile.shared_storage = MEM_new<MemFileSharedStorage>(__func__);
2401 }
2402 if (memfile.shared_storage->sharing_info_by_address_id.add(address_id, {sharing_info, data}))
2403 {
2404 /* The undo-step takes (shared) ownership of the data, which also makes it immutable. */
2405 sharing_info->add_user();
2406 /* This size is an estimate, but good enough to count data with many users less. */
2407 memfile.size += approximate_size_in_bytes / sharing_info->strong_users();
2408 return;
2409 }
2410 }
2411 }
2412 if (sharing_info != nullptr) {
2413 if (!this->wd->per_id_written_shared_addresses.add(data)) {
2414 /* Was written already. */
2415 return;
2416 }
2417 }
2418 write_fn();
2419}
2420
2422
2423} // namespace blender
#define BLENDER_FILE_SUBVERSION
#define BLENDER_FILE_MIN_VERSION
#define BLENDER_FILE_VERSION
#define BLENDER_FILE_MIN_SUBVERSION
#define G_FILE_FLAG_ALL_RUNTIME
#define MAIN_ID_SESSION_UID_UNSET
#define INT32_MAX
#define L_ENDIAN
#define ENDIAN_ORDER
File and directory operations.
#define O_BINARY
#define FILE_MAX
#define BLI_path_cmp
#define SNPRINTF(dst, format,...)
Platform independent time functions.
Compatibility-like things for windows.
Utilities ensuring .blend file (i.e. Main) is in valid state during write and/or read process.
#define SIZEOFBLENDERHEADER_VERSION_0
#define SIZEOFBLENDERHEADER_VERSION_1
#define BLEND_FILE_FORMAT_VERSION_1
#define SYSTEM_SUPPORTS_WRITING_FILE_VERSION_1
external readfile function prototypes.
#define BLEN_THUMB_MEMSIZE_FILE(_x, _y)
external writefile.cc function prototypes.
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define CLOG_INFO(clg_ref,...)
Definition CLG_log.h:190
#define ID_IS_OVERRIDE_LIBRARY_VIRTUAL(_id)
Definition DNA_ID.h:733
#define ID_IS_PACKED(_id)
Definition DNA_ID.h:703
#define ID_IS_OVERRIDE_LIBRARY_REAL(_id)
Definition DNA_ID.h:729
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define MAX_ID_NAME
Definition DNA_ID.h:367
#define ID_TAG_KEEP_ON_UNDO
Definition DNA_ID.h:1044
#define ID_LINK_PLACEHOLDER
blenloader genfile private function prototypes
#define SDNA_RAW_DATA_STRUCT_INDEX
#define USER_DEVELOPER_TOOL_TEST(userdef, member)
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert(a)
#define POINTER_OFFSET(v, ofs)
#define BLI_assert_msg(a, msg)
btScalar max
char build_hash[]
Definition buildinfo.c:34
ulong build_commit_timestamp
Definition buildinfo.c:35
void generated_ptr(int64_t offset)
Definition writefile.cc:986
constexpr IndexRange()=default
void clear()
Definition BLI_map.hh:1038
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
Value & lookup_or_add_cb(const Key &key, const CreateValueF &create_value)
Definition BLI_map.hh:620
bool contains(const Key &key) const
Definition BLI_map.hh:353
Value lookup_default_as(const ForwardKey &key, ForwardValue &&...default_value) const
Definition BLI_map.hh:575
Span< Value > lookup(const Key &key) const
void add(const Key &key, const Value &value)
bool close() override
Definition writefile.cc:214
bool open(const char *filepath) override
Definition writefile.cc:201
bool write(const void *buf, size_t buf_len) override
Definition writefile.cc:218
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
void clear()
Definition BLI_set.hh:570
constexpr Span()=default
constexpr StringRef()
void append(const T &value)
bool is_empty() const
virtual bool open(const char *filepath)=0
virtual bool write(const void *buf, size_t buf_len)=0
virtual bool close()=0
bool close() override
Definition writefile.cc:404
ZstdWriteWrap(WriteWrap &base_wrap)
Definition writefile.cc:273
bool write(const void *buf, size_t buf_len) override
Definition writefile.cc:419
bool open(const char *filepath) override
Definition writefile.cc:356
#define printf
#define sizeof
#define INT_MAX
void MEM_delete_void(void *vmemh)
Definition mallocn.cc:109
void * MEM_new_uninitialized(size_t len, const char *str)
Definition mallocn.cc:124
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
void print_structs_at_address(const SDNA &sdna, int struct_id, const void *data, const void *address, int64_t element_num, std::ostream &stream)
int sdna_struct_id_get_max()
uint32_t struct_size(Span< shader::ShaderCreateInfo::PushConst > push_constants)
BMesh Iterators.
static void writestruct_nr(WriteData *wd, const int filecode, const int struct_nr, const int64_t nr, const void *adr, const BlendStructWriterFn fn)
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
static void write_id(WriteData *wd, ID *id)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
static uint64_t get_address_id_int(WriteData &wd, const void *address)
Definition writefile.cc:853
BLI_STATIC_ASSERT(ARRAY_SIZE(data_mode_strings)==CTX_MODE_NUM+1, "Must have a string for each context mode") const char *CTX_data_mode_string(const bContext *C)
char * STRNCPY(char(&dst)[N], const char *src)
static void mywrite_id_end(WriteData *wd, ID *)
Definition writefile.cc:717
static bool write_file_handle(Main *mainvar, WriteWrap *ww, StringRefNull filepath, MemFile *compare, MemFile *current, const int write_flags, const bool use_userdef, const BlendThumbnail *thumb, std::ostream *debug_dst)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool BLO_write_file(Main *mainvar, const char *filepath, int write_flags, const BlendFileWriteParams *params, ReportList *reports)
static void write_global(WriteData *wd, const int fileflags, Main *mainvar)
void copy_m3_m3(float m1[3][3], const float m2[3][3])
@ ASSET_LIBRARY_PROJECT_DEFINED
static bool write_at_address_validate(WriteData *wd, const int filecode, const void *address)
Definition writefile.cc:761
@ TASK_PRIORITY_HIGH
Definition BLI_task_c.hh:61
bool BKE_idtype_idcode_is_linkable(short idcode)
Definition idtype.cc:307
static bool write_library_if_needed(WriteData *wd, MultiValueMap< Library *, ID * > &linked_ids_by_library, Set< Library * > &written_libraries, Library &library)
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
void IDP_BlendWrite(BlendWriter *writer, const IDProperty *prop)
Definition idprop.cc:1696
static void write_userdef(BlendWriter *writer, const UserDef *userdef)
unsigned long ulong
Definition BKE_text.h:57
static bool mywrite_end(WriteData *wd)
Definition writefile.cc:602
void BKE_bpath_relative_rebase(Main *bmain, const char *basedir_src, const char *basedir_dst, ReportList *reports, BPathSummary *r_summary=nullptr)
Definition bpath.cc:568
static void write_libraries(WriteData *wd, Main *bmain)
static void write_bhead(WriteData *wd, const BHead &bhead)
Definition writefile.cc:779
static void write_id_placeholder(WriteData *wd, ID *id)
static const void * get_address_id(WriteData &wd, const void *address)
Definition writefile.cc:871
static void write_blend_file_header(WriteData *wd)
bScreen * BKE_workspace_active_screen_get(const WorkSpaceInstanceHook *hook) GETTER_ATTRS
Definition workspace.cc:684
static void writedata_free(WriteData *wd)
Definition writefile.cc:498
int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BKE_bpath_relative_convert(Main *bmain, const char *basedir, ReportList *reports, BPathSummary *r_summary=nullptr)
Definition bpath.cc:711
static void current_screen_compat(Main *mainvar, const bool use_active_win, bScreen **r_screen, Scene **r_scene, ViewLayer **r_view_layer)
void BKE_preferences_extension_repo_write_data(struct BlendWriter *writer, const bUserExtensionRepo *repo)
static void mywrite_flush(WriteData *wd)
Definition writefile.cc:516
void BKE_main_id_refcount_recompute(Main *bmain, bool do_linked_only)
Definition lib_id.cc:2131
static CLG_LogRef LOG_UNDO
Definition readfile.cc:192
void BLI_task_pool_work_and_wait(TaskPool *pool)
Definition task_pool.cc:455
static void writelist_nr(WriteData *wd, const int filecode, const int struct_nr, const ListBase *lb, const BlendStructWriterFn fn)
void * BLI_task_pool_user_data(TaskPool *pool)
Definition task_pool.cc:460
bool BLO_main_validate_libraries(Main *bmain, ReportList *reports)
Global G
Definition blender.cc:57
static WriteData * mywrite_begin(WriteWrap *ww, MemFile *compare, MemFile *current)
Definition writefile.cc:585
static bool do_history(const char *filepath, ReportList *reports)
unsigned int uint32_t
Definition BKE_text.h:81
@ G_FILE_RECOVER_WRITE
@ G_FILE_COMPRESS
@ G_FILE_ASSET_EDIT_FILE
static uint64_t get_stable_pointer_hint_for_id(const ID &id, const bool is_undo)
Definition writefile.cc:626
const IDTypeInfo * BKE_idtype_get_info_from_id(const ID *id)
Definition idtype.cc:232
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
void void BLI_freelistN(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:504
static void writedata_do_write(WriteData *wd, const void *mem, const size_t memlen)
Definition writefile.cc:472
void BKE_preferences_asset_library_write_data(struct BlendWriter *writer, const struct bUserAssetLibrary *library)
bool BLO_write_file_mem(Main *mainvar, MemFile *compare, MemFile *current, int write_flags)
@ BLO_WRITE_PATH_REMAP_RELATIVE_ALL
@ BLO_WRITE_PATH_REMAP_RELATIVE
@ BLO_WRITE_PATH_REMAP_NONE
@ BLO_WRITE_PATH_REMAP_ABSOLUTE
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
void BLO_memfile_write_finalize(WriteData *wd, MemFileWriteData *mem_data)
Definition undofile.cc:130
bool BLI_path_is_abs_from_cwd(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool BKE_main_namemap_validate_and_fix(Main &bmain)
unsigned char uint8_t
Definition BKE_text.h:79
bool BKE_main_view_layers_synced_ensure(const Main *bmain)
char build_hash[]
Definition bpy_app.cc:73
int BLI_rename_overwrite(const char *from, const char *to) ATTR_NONNULL()
Definition fileops_c.cc:541
constexpr uint64_t generated_address_id_on_undo_flag
Definition writefile.cc:820
int BLI_path_normalize(char *path) ATTR_NONNULL(1)
@ LIBRARY_FLAG_IS_ARCHIVE
ATTR_WARN_UNUSED_RESULT const size_t num
int DNA_struct_size(const struct SDNA *sdna, int struct_index)
constexpr uint64_t generated_address_id_mask
Definition writefile.cc:822
void BLI_task_pool_free(TaskPool *pool)
Definition task_pool.cc:441
_W64 unsigned int uintptr_t
Definition BKE_text.h:120
static Vector< ID * > gather_local_ids_to_write(Main *bmain, const bool is_undo)
static uint64_t get_next_stable_address_id(WriteData &wd, uint64_t &hint)
Definition writefile.cc:839
static bool BLO_write_file_impl(Main *mainvar, const char *filepath, const int write_flags, const BlendFileWriteParams *params, ReportList *reports, WriteWrap &ww)
int DNA_struct_find_with_alias(const struct SDNA *sdna, StringRef str)
static uint64_t stable_id_from_hint(const uint64_t hint)
Definition writefile.cc:824
ViewLayer * BKE_view_layer_find(const Scene *scene, const char *layer_name)
eBPathForeachFlag
Definition BKE_bpath.hh:39
@ BKE_BPATH_FOREACH_PATH_SKIP_MULTIFILE
Definition BKE_bpath.hh:76
@ BKE_BPATH_FOREACH_PATH_SKIP_LINKED
Definition BKE_bpath.hh:47
void BKE_bpath_list_restore(Main *bmain, eBPathForeachFlag flag, void *path_list_handle)
Definition bpath.cc:799
void BKE_bpath_list_free(void *path_list_handle)
Definition bpath.cc:811
static void mywrite(WriteData *wd, const void *adr, size_t len)
Definition writefile.cc:529
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
static std::string get_blend_file_header()
void BLO_memfile_chunk_add(MemFileWriteData *mem_data, const char *buf, size_t size)
Definition undofile.cc:135
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
static void prepare_stable_data_block_ids(WriteData &wd, Main &bmain)
void * BKE_bpath_list_backup(Main *bmain, eBPathForeachFlag flag)
Definition bpath.cc:785
static uint64_t get_address_id_for_undo(WriteData &wd)
Definition writefile.cc:885
static void writedata(WriteData *wd, const int filecode, const void *data, const size_t len, const void *adr)
@ ID_TAG_NO_MAIN
Definition DNA_ID.h:981
@ ID_TAG_INDIRECT
Definition DNA_ID.h:851
@ ID_TAG_NO_USER_REFCOUNT
Definition DNA_ID.h:1011
@ ID_TAG_RUNTIME
Definition DNA_ID.h:862
@ ID_TAG_EXTERN
Definition DNA_ID.h:845
@ ID_TAG_NOT_ALLOCATED
Definition DNA_ID.h:1018
signed int int32_t
Definition BKE_text.h:78
static WriteData * writedata_new(WriteWrap *ww)
Definition writefile.cc:446
void BKE_asset_catalog_path_list_blend_write(BlendWriter *writer, const ListBaseT< AssetCatalogPathLink > &catalog_path_list)
void BLO_memfile_write_init(WriteData *wd, MemFileWriteData *mem_data, MemFile *written_memfile, MemFile *reference_memfile)
Definition undofile.cc:101
static CLG_LogRef LOG
Definition logging.cpp:12
double BLI_time_now_seconds()
Definition time.cc:30
static void write_keymapitem(BlendWriter *writer, const wmKeyMapItem *kmi)
static void writestruct_at_address_nr(WriteData *wd, const int filecode, const int struct_nr, const int64_t nr, const void *adr, const void *data, const BlendStructWriterFn fn)
Definition writefile.cc:891
@ ID_FLAG_INDIRECT_WEAK_LINK
Definition DNA_ID.h:781
@ ID_FLAG_EMBEDDED_DATA
Definition DNA_ID.h:775
@ ID_FLAG_CLIPBOARD_MARK
Definition DNA_ID.h:801
FunctionRef< void(BlendStructWriterVoid &struct_writer)> BlendStructWriterFn
const SDNA * DNA_sdna_current_get()
void BKE_bpath_absolute_convert(Main *bmain, const char *basedir, ReportList *reports, BPathSummary *r_summary=nullptr)
Definition bpath.cc:720
void BKE_main_id_indirect_linked_update(Main &bmain, std::optional< Span< ID * > > local_ids=std::nullopt)
Definition lib_id.cc:2211
void BKE_lib_override_library_operations_create(Main *bmain, ID *local, int *r_report_flags)
static void write_file_main_validate_post(Main *bmain, ReportList *reports)
static void write_file_main_validate_pre(Main &bmain, const BlendFileWriteParams &params, ReportList *reports)
MINLINE int min_ii(int a, int b)
void BLI_task_pool_push(TaskPool *pool, TaskRunFunction run, void *taskdata, bool free_taskdata, TaskFreeFunction freedata)
Definition task_pool.cc:446
static void mywrite_id_begin(WriteData *wd, ID *id)
Definition writefile.cc:671
static void write_renderinfo(WriteData *wd, Main *mainvar)
bool BLO_main_validate_shapekeys(Main *bmain, ReportList *reports)
signed char int8_t
Definition BKE_text.h:76
static void write_thumb(WriteData *wd, const BlendThumbnail *thumb)
signed short int16_t
Definition BKE_text.h:77
TaskPool * BLI_task_pool_create_background(void *userdata, eTaskPriority priority)
Definition task_pool.cc:409
ulong build_commit_timestamp
Definition bpy_app.cc:70
static void write_raw_data_in_debug_file(WriteData *wd, const size_t len, const void *address_id, const void *data)
@ IDTYPE_FLAGS_NEVER_UNUSED
Definition BKE_idtype.hh:73
const void * old
BLO_Write_IDBuffer(ID &id, bool is_undo, bool is_placeholder)
eBLO_WritePathRemap remap_mode
const BlendThumbnail * thumb
void write_pointer_array(int64_t num, const void *data)
void write_struct_by_id(int struct_id, const void *data, BlendStructWriterFn fn=nullptr)
void write_struct_array_by_id(int struct_id, int64_t array_size, const void *data, BlendStructWriterFn fn=nullptr)
void write_struct_array_by_name(const char *struct_name, int64_t array_size, const void *data, BlendStructWriterFn fn=nullptr)
int struct_id_by_name(const char *struct_name) const
void write_shared(const void *data, size_t approximate_size_in_bytes, const ImplicitSharingInfo *sharing_info, FunctionRef< void()> write_fn)
void generated_pointer_tag(const void *data)
void write_string(const char *data)
void write_struct_by_name(const char *struct_name, const void *data, BlendStructWriterFn fn=nullptr)
void write_uint8_array(int64_t num, const uint8_t *data)
void write_struct_list_by_id(int struct_id, const ListBase *list, BlendStructWriterFn fn=nullptr)
void write_int8_array(int64_t num, const int8_t *data)
void write_struct_at_address_by_id(int struct_id, const void *address, const void *data, BlendStructWriterFn fn=nullptr)
void write_float_array(int64_t num, const float *data)
void write_struct_list_by_name(const char *struct_name, ListBase *list, BlendStructWriterFn fn=nullptr)
void write_struct_array_at_address_by_id(int struct_id, int64_t array_size, const void *address, const void *data, BlendStructWriterFn fn=nullptr)
void write_float3_array(int64_t num, const float *data)
void write_char_array(int64_t num, const char *data)
void write_int16_array(int64_t num, const int16_t *data)
void write_uint32_array(int64_t num, const uint32_t *data)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
void write_double_array(int64_t num, const double *data)
bool is_undo() const
void write_struct_at_address_by_id_with_filecode(int filecode, int struct_id, const void *address, const void *data, BlendStructWriterFn fn=nullptr)
void write_raw(size_t size_in_bytes, const void *data)
void write_int32_array(int64_t num, const int32_t *data)
StringRefNull filepath() const
struct bScreen * curscreen
float colorspace_scene_linear_to_xyz[3][3]
struct ViewLayer * cur_view_layer
IDTypeBlendWriteFunction blend_write
char name[258]
Definition DNA_ID.h:433
unsigned int session_uid
Definition DNA_ID.h:463
unsigned int recalc_up_to_undo_push
Definition DNA_ID.h:456
struct Library * lib
Definition DNA_ID.h:421
short flag
Definition DNA_ID.h:439
uint16_t flag
Definition DNA_ID.h:565
ListBaseT< wmWindowManager > wm
Definition BKE_main.hh:415
char filepath[1024]
Definition BKE_main.hh:267
MainColorspace colorspace
Definition BKE_main.hh:371
MainLock * lock
Definition BKE_main.hh:451
bool is_global_main
Definition BKE_main.hh:328
ListBaseT< Library > libraries
Definition BKE_main.hh:387
bool is_asset_edit_file
Definition BKE_main.hh:288
ListBaseT< Scene > scenes
Definition BKE_main.hh:386
const c_style_mat & ptr() const
Map< uint64_t, ImplicitSharingInfoAndData > sharing_info_by_address_id
Map< uint, MemFileChunk * > id_session_uid_mapping
MemFileChunk * reference_current_chunk
MemFileSharedStorage * shared_storage
char scene_name[MAX_ID_NAME - 2]
const char * data
struct RenderData r
ListBaseT< bUserExtensionRepo > extension_repos
ListBaseT< bTheme > themes
ListBaseT< struct wmKeyMap > user_keymaps
ListBaseT< struct wmKeyConfigPref > user_keyconfig_prefs
ListBaseT< bUserAssetLibrary > asset_libraries
ListBaseT< bAddon > addons
ListBaseT< bPathCompare > autoexec_paths
ListBaseT< bUserScriptDirectory > script_directories
ListBaseT< uiStyle > uistyles
ListBaseT< bUserAssetShelfSettings > asset_shelves_settings
ListBaseT< bUserMenu > user_menus
std::shared_ptr< dna::pointers::PointersInDNA > sdna_pointers
Definition writefile.hh:31
Map< const void *, uint64_t > pointer_map
Definition writefile.hh:49
const SDNA * sdna
Definition writefile.hh:80
struct blender::WriteData::@001170002120114163277232003255130215200052024114 buffer
Set< const void * > per_id_written_shared_addresses
Definition writefile.hh:130
struct blender::WriteData::@044343357155064103211111077354243036325062231364 validation_data
std::string filepath
Definition writefile.hh:145
MemFileWriteData mem
Definition writefile.hh:133
WriteDataStableAddressIDs stable_address_ids
Definition writefile.hh:124
std::ostream * debug_dst
Definition writefile.hh:81
struct IDProperty * prop
ListBaseT< AssetCatalogPathLink > enabled_catalog_paths
ListBaseT< bUserMenuItem > items
bool has_pointer_at_offset(int64_t offset) const
ListBaseT< wmKeyMapDiffItem > diff_items
ListBaseT< wmKeyMapItem > items
struct WorkSpaceInstanceHook * workspace_hook
#define ZSTD_COMPRESSION_LEVEL
Definition writefile.cc:169
#define DEBUG_BLEND_FILE_SUFFIX
Definition writefile.cc:158
#define ZSTD_CHUNK_SIZE
Definition writefile.cc:167
#define MEM_CHUNK_SIZE
Definition writefile.cc:164
#define writestruct(wd, filecode, struct_id, nr, adr, fn)
#define ZSTD_BUFFER_SIZE
Definition writefile.cc:166
#define writestruct_at_address(wd, filecode, struct_id, nr, adr, data, fn)
#define MEM_BUFFER_SIZE
Definition writefile.cc:163