Blender V5.3
dna_genfile.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
14
15#include <algorithm>
16#include <climits>
17#include <cstdio>
18#include <cstdlib>
19#include <cstring>
20#include <iostream>
21
22#include <fmt/format.h>
23
24#include "MEM_guardedalloc.h"
25
26#include "BLI_index_range.hh"
28#include "BLI_set.hh"
29#include "BLI_utildefines.hh"
30
31#include "DNA_genfile.h"
32#include "DNA_print.hh"
33#include "DNA_sdna_pointers.hh"
34#include "DNA_sdna_types.h" /* for SDNA ;-) */
35
36namespace blender {
37
118
119/* NOTE: this is endianness-sensitive. */
120/* Little Endian */
121#define MAKE_ID(a, b, c, d) (int(d) << 24 | int(c) << 16 | (b) << 8 | (a))
122
123/* ************************* DIV ********************** */
124
126{
127 if (this->data_alloc) {
128 MEM_delete(this->data);
129 }
130}
131
132int DNA_struct_size(const SDNA *sdna, int struct_index)
133{
134 return sdna->types_size[sdna->structs[struct_index]->type_index];
135}
136
140static bool ispointer(const StringRef name)
141{
142 /* check if pointer or function pointer */
143 return (name[0] == '*' || (name[0] == '(' && name[1] == '*'));
144}
145
146int DNA_struct_member_size(const SDNA *sdna, short type, short member_index)
147{
148 const StringRef cp = sdna->members[member_index];
149 int len = 0;
150
151 /* is it a pointer or function pointer? */
152 if (ispointer(cp)) {
153 /* has the name an extra length? (array) */
154 len = sdna->pointer_size * sdna->members_array_num[member_index];
155 }
156 else if (sdna->types_size[type]) {
157 /* has the name an extra length? (array) */
158 len = int(sdna->types_size[type]) * sdna->members_array_num[member_index];
159 }
160
161 return len;
162}
163
164#if 0
165static void printstruct(SDNA *sdna, short struct_index)
166{
167 /* is for debug */
168
169 SDNA_Struct *struct_info = sdna->structs[struct_index];
170 printf("struct %s\n", sdna->types[struct_info->type]);
171
172 for (int b = 0; b < struct_info->members_len; b++) {
173 SDNA_StructMember *struct_member = &struct_info->members[b];
174 printf(" %s %s\n", sdna->types[struct_member->type], sdna->names[struct_member->name]);
175 }
176}
177#endif
178
183 /* From SDNA struct. */
184 const Span<StringRef> types,
185 const int /*types_num*/,
186 const Span<SDNA_Struct *> structs,
187#ifdef WITH_DNA_GHASH
188 const Map<StringRef, int> &structs_map,
189#endif
190 /* Regular args. */
191 const StringRef str,
192 uint *struct_index_last)
193{
194 if (*struct_index_last < structs.size()) {
195 const SDNA_Struct *struct_info = structs[*struct_index_last];
196 if (types[struct_info->type_index] == str) {
197 return *struct_index_last;
198 }
199 }
200
201#ifdef WITH_DNA_GHASH
202 if (const std::optional<int> struct_index = structs_map.lookup_try(str)) {
203 *struct_index_last = *struct_index;
204 return *struct_index;
205 }
206#else
207 {
208 for (int struct_index = 0; struct_index < types_num; struct_index++) {
209 const SDNA_Struct *struct_info = structs[struct_index];
210 if (types[struct_info->type] == str) {
211 *struct_index_last = struct_index;
212 return struct_index;
213 }
214 }
215 }
216#endif
217 return -1;
218}
219
221 const StringRef str,
222 uint *struct_index_last)
223{
225 /* Expand SDNA. */
226 sdna->types,
227 sdna->types_num,
228 sdna->structs,
229#ifdef WITH_DNA_GHASH
231#endif
232 /* Regular args. */
233 str,
234 struct_index_last);
235}
236
238 const StringRef str,
239 uint *struct_index_last)
240{
242 /* Expand SDNA. */
243 sdna->alias.types,
244 sdna->types_num,
245 sdna->structs,
246#ifdef WITH_DNA_GHASH
248#endif
249 /* Regular args. */
250 str,
251 struct_index_last);
252}
253
255{
256 uint index_last_dummy = UINT_MAX;
257 return DNA_struct_find_index_without_alias_ex(sdna, str, &index_last_dummy);
258}
259
261{
262 uint index_last_dummy = UINT_MAX;
263 return DNA_struct_find_index_with_alias_ex(sdna, str, &index_last_dummy);
264}
265
267{
268 return DNA_struct_find_with_alias(sdna, str) != -1;
269}
270
271/* ************************* END DIV ********************** */
272
273/* ************************* READ DNA ********************** */
274
275BLI_INLINE const char *pad_up_4(const char *ptr)
276{
277 return reinterpret_cast<const char *>((uintptr_t(ptr) + 3) & ~3);
278}
279
280template<typename T>
282 const int64_t num,
283 const void *const data_pointer_end,
284 const char **r_error_message)
285{
286 BLI_STATIC_ASSERT(std::is_pointer_v<T> && std::is_const_v<std::remove_pointer_t<T>>,
287 "Data type for this util must be a pointer to const data")
288 BLI_assert_msg(num >= 0, "Caller should ensure read size is always >= 0");
289 if ((data + num) > data_pointer_end) [[unlikely]] {
290 *r_error_message = "Invalid data in SDNA file";
291 return nullptr;
292 }
293 T ret = data;
294 data += num;
295 return ret;
296}
297
298template<typename T>
300 T data_pointer_current,
301 const void *const data_pointer_end,
302 const char **r_error_message)
303{
304 BLI_STATIC_ASSERT(std::is_pointer_v<T> && std::is_const_v<std::remove_pointer_t<T>>,
305 "Data type for this util must be a pointer to const data")
306 if (size < 0 || size > (static_cast<T>(data_pointer_end) - data_pointer_current)) [[unlikely]] {
307 *r_error_message = "Invalid array size data in SDNA file";
308 return false;
309 }
310 return true;
311}
312
314 const void *const data_pointer_end,
315 const char **r_error_message)
316{
317 BLI_assert(static_cast<const char *>(data_pointer_end) >= data);
318 const char *str_end = std::char_traits<char>::find(
319 data, size_t(static_cast<const char *>(data_pointer_end) - data), '\0');
320 if (!str_end) [[unlikely]] {
321 *r_error_message = "Invalid string data in SDNA file";
322 return nullptr;
323 }
324 const int64_t string_size = str_end - data;
325 BLI_assert(string_size >= 0);
326 /* Non-empty string is a hard requirement by all use-cases currently. */
327 if (!string_size) [[unlikely]] {
328 *r_error_message = "Invalid string data in SDNA file";
329 return nullptr;
330 }
331 StringRef ret = StringRef(data, string_size);
332 data += (string_size + 1); /* String size + null terminator. */
333 return ret;
334}
335
349static bool sdna_struct_size_check(const SDNA *sdna,
350 const SDNA_Struct *struct_info,
351 const int64_t voided_members_size,
352 const char **r_error_message)
353{
354 int64_t members_size = voided_members_size;
355 for (const SDNA_StructMember &member : Span(struct_info->members, struct_info->members_num)) {
356 const StringRef member_name = sdna->members[member.member_index];
357 const int64_t array_num = sdna->members_array_num[member.member_index];
358 const int64_t member_size = ispointer(member_name) ? sdna->pointer_size :
359 sdna->types_size[member.type_index];
360 if (member_size < 0) [[unlikely]] {
361 *r_error_message = "Invalid struct member size in SDNA file";
362 return false;
363 }
364 members_size += member_size * array_num;
365 }
366 if (members_size != sdna->types_size[struct_info->type_index]) [[unlikely]] {
367 *r_error_message = "Invalid struct size in SDNA file";
368 return false;
369 }
370 return true;
371}
372
376static bool init_structDNA(SDNA *sdna, const char **r_error_message)
377{
378 /* The various pointers iterating over the SDNA::data buffer in this function should never reach
379 * this address. */
380 const void *const data_pointer_end = sdna->data + sdna->data_size;
381
382 int member_index_gravity_fix = -1;
383 const SDNA_Struct *gravity_fix_struct = nullptr;
384
385 const int *int_buf = reinterpret_cast<int *>(const_cast<char *>(sdna->data));
386 const int *int_data;
387 const short *short_buf;
388 const short *short_data;
389 const char *char_buf;
390
391 /* Clear pointers in case of error. */
392 sdna->types_size = nullptr;
393
394 /* Lazy initialize. */
395 sdna->alias = {};
396
397 /* Struct DNA ('SDNA') */
398 if (!(int_data = sdna_data_pointer_read(int_buf, 1, data_pointer_end, r_error_message))) {
399 return false;
400 }
401 if (*int_data != MAKE_ID('S', 'D', 'N', 'A')) {
402 *r_error_message = "SDNA error in SDNA file";
403 return false;
404 }
405
406 /* Names array ('NAME') */
407 if (!(int_data = sdna_data_pointer_read(int_buf, 1, data_pointer_end, r_error_message))) {
408 return false;
409 }
410 if (*int_data == MAKE_ID('N', 'A', 'M', 'E')) {
411 /* NOTE: this is endianness-sensitive. */
412 if (!(int_data = sdna_data_pointer_read(int_buf, 1, data_pointer_end, r_error_message)) ||
413 !sdna_data_validate_array_size(*int_data, int_buf, data_pointer_end, r_error_message))
414 {
415 return false;
416 }
417 sdna->members_num = *int_data;
418 sdna->members.resize(sdna->members_num, nullptr);
419 }
420 if (sdna->members.is_empty()) {
421 *r_error_message = "NAME error in SDNA file";
422 return false;
423 }
424
425 char_buf = reinterpret_cast<const char *>(int_buf);
426 for (int member_index = 0; member_index < sdna->members_num; member_index++) {
427 const StringRef orig_member_name = sdna_data_pointer_read_string(
428 char_buf, data_pointer_end, r_error_message);
429 if (orig_member_name.is_empty()) {
430 return false;
431 }
432 sdna->members[member_index] = orig_member_name;
433
434 /* "float gravity [3]" was parsed wrong giving both "gravity" and
435 * "[3]" members. we rename "[3]", and later set the type of
436 * "gravity" to "void" so the offsets work out correct. See #20330. */
437 if (orig_member_name == "[3]") {
438 if (member_index && sdna->members[member_index - 1] == "Cvi") {
439 sdna->members[member_index] = "gravity[3]";
440 member_index_gravity_fix = member_index;
441 }
442 }
443 }
444 char_buf = pad_up_4(char_buf);
445
446 /* Type names array ('TYPE') */
447 int_buf = reinterpret_cast<const int *>(char_buf);
448 if (!(int_data = sdna_data_pointer_read(int_buf, 1, data_pointer_end, r_error_message))) {
449 return false;
450 }
451 if (*int_data == MAKE_ID('T', 'Y', 'P', 'E')) {
452 /* NOTE: this is endianness-sensitive. */
453 if (!(int_data = sdna_data_pointer_read(int_buf, 1, data_pointer_end, r_error_message)) ||
454 !sdna_data_validate_array_size(*int_data, int_buf, data_pointer_end, r_error_message))
455 {
456 return false;
457 }
458 sdna->types_num = *int_data;
459 sdna->types = Array<StringRef>(sdna->types_num, nullptr);
460 }
461 if (sdna->types.is_empty()) {
462 *r_error_message = "TYPE error in SDNA file";
463 return false;
464 }
465
466 char_buf = reinterpret_cast<const char *>(int_buf);
467 for (int type_index = 0; type_index < sdna->types_num; type_index++) {
468 const StringRef orig_type_name = sdna_data_pointer_read_string(
469 char_buf, data_pointer_end, r_error_message);
470 if (orig_type_name.is_empty()) {
471 return false;
472 }
473 /* WARNING! See: DNA_struct_rename_legacy_hack_static_from_alias docs. */
474 sdna->types[type_index] = DNA_struct_rename_legacy_hack_static_from_alias(orig_type_name);
475 }
476 char_buf = pad_up_4(char_buf);
477
478 /* Type lengths array ('TLEN') */
479 int_buf = reinterpret_cast<const int *>(char_buf);
480 if (!(int_data = sdna_data_pointer_read(int_buf, 1, data_pointer_end, r_error_message))) {
481 return false;
482 }
483 short_buf = reinterpret_cast<const short *>(int_buf);
484 if (*int_data == MAKE_ID('T', 'L', 'E', 'N')) {
485 /* NOTE: this is endianness-sensitive. */
486 if (!(short_data = sdna_data_pointer_read(
487 short_buf, sdna->types_num, data_pointer_end, r_error_message)))
488 {
489 return false;
490 }
491 sdna->types_size = const_cast<short *>(short_data);
492 }
493 if (!sdna->types_size) {
494 *r_error_message = "TLEN error in SDNA file";
495 return false;
496 }
497 /* prevent BUS error */
498 if (sdna->types_num & 1) {
499 if (!sdna_data_pointer_read(short_buf, 1, data_pointer_end, r_error_message)) {
500 return false;
501 }
502 }
503
504 /* Struct array ('STRC') */
505 int_buf = reinterpret_cast<const int *>(short_buf);
506 if (!(int_data = sdna_data_pointer_read(int_buf, 1, data_pointer_end, r_error_message))) {
507 return false;
508 }
509 if (*int_data == MAKE_ID('S', 'T', 'R', 'C')) {
510 /* NOTE: this is endianness-sensitive. */
511 if (!(int_data = sdna_data_pointer_read(int_buf, 1, data_pointer_end, r_error_message)) ||
512 !sdna_data_validate_array_size(*int_data, int_buf, data_pointer_end, r_error_message))
513 {
514 return false;
515 }
516 const int structs_num = *int_data;
517 sdna->structs = Array<SDNA_Struct *, 0>(structs_num, nullptr);
518 }
519 if (sdna->structs.is_empty()) {
520 *r_error_message = "STRC error in SDNA file";
521 return false;
522 }
523
524 /* Safety check, to ensure that there is no multiple usages of a same struct index. */
525 Set<const short> struct_type_indices;
526 struct_type_indices.reserve(sdna->structs.size());
527 short_buf = reinterpret_cast<const short *>(int_buf);
528 for (const int64_t struct_index : sdna->structs.index_range()) {
529 /* First read the always-valid part of SDNA_struct (its first two short members). */
530 /* NOTE: this is endianness-sensitive. */
531 if (!(short_data = sdna_data_pointer_read(short_buf, 2, data_pointer_end, r_error_message))) {
532 return false;
533 }
534 SDNA_Struct *struct_info = reinterpret_cast<SDNA_Struct *>(const_cast<short *>(short_data));
535
536 /* Now validate the size of the SDNA_StructMember array and consume it in the buffer too. */
538 constexpr int64_t struct_member_short_size = (sizeof(SDNA_StructMember) / sizeof(short));
539 const int64_t struct_members_size = struct_member_short_size * struct_info->members_num;
541 struct_members_size, short_buf, data_pointer_end, r_error_message) ||
542 !sdna_data_pointer_read(short_buf, struct_members_size, data_pointer_end, r_error_message))
543 {
544 return false;
545 }
546 sdna->structs[struct_index] = const_cast<SDNA_Struct *>(struct_info);
547
548 if (struct_info->type_index < 0 || struct_info->type_index >= sdna->types_num) {
549 *r_error_message = "Invalid struct type index in SDNA file";
550 return false;
551 }
552 if (!struct_type_indices.add(struct_info->type_index)) {
553 *r_error_message = "Invalid duplicate struct type index in SDNA file";
554 return false;
555 }
556 for (int64_t struct_member_index : IndexRange(struct_info->members_num)) {
557 const SDNA_StructMember &struct_member = struct_info->members[struct_member_index];
558 if (struct_member.type_index < 0 || struct_member.type_index >= sdna->types_num) {
559 *r_error_message = "Invalid struct member's type index in SDNA file";
560 return false;
561 }
562 if (struct_member.member_index < 0 || struct_member.member_index >= sdna->members_num) {
563 *r_error_message = "Invalid struct member's member index in SDNA file";
564 return false;
565 }
566 }
567
568 /* Second part of gravity problem, setting "gravity" type from float (7) to void (9).
569 * See #20330.
570 *
571 * NOTE: The gravity fix only applies to pre-2011 blend-files, which had `void` at
572 * type index 9 in their SDNA (`int64_t` was inserted before `void` in the `types`
573 * array later, shifting `void` to a later index in current Blender's SDNA, but the
574 * file's SDNA loaded here still has `void` at 9). */
575 if (member_index_gravity_fix > -1) {
576 constexpr int float_type_index = 7;
577 constexpr int void_type_index = 9;
578 constexpr int gravity_member_index = 4;
579 if (sdna->types[struct_info->type_index] == "ClothSimSettings") {
580 if (struct_info->members_num < gravity_member_index + 2 ||
581 struct_info->members[gravity_member_index].type_index != float_type_index ||
582 sdna->members[struct_info->members[gravity_member_index].member_index] != "gravity" ||
583 sdna->members[struct_info->members[gravity_member_index + 1].member_index] !=
584 "gravity[3]" ||
585 float_type_index >= sdna->types_num || sdna->types[float_type_index] != "float" ||
586 void_type_index >= sdna->types_num || sdna->types[void_type_index] != "void")
587 {
588 *r_error_message = "Invalid data in SDNA file";
589 return false;
590 }
591 struct_info->members[gravity_member_index].type_index = void_type_index;
592 gravity_fix_struct = struct_info;
593 }
594 }
595 }
596
597 /* The whole SDNA data should have been processed and consumed, so the current pointer should be
598 * at the expected 'end' value now. */
599 char_buf = reinterpret_cast<const char *>(short_buf);
600 if (char_buf != data_pointer_end) {
601 *r_error_message = "Invalid data in SDNA file";
602 return false;
603 }
604
605#ifdef WITH_DNA_GHASH
606 {
607 /* create a hash lookup to speed up */
608 sdna->types_to_structs_map.clear();
609 sdna->types_to_structs_map.reserve(sdna->structs.size());
610 for (const int64_t struct_index : sdna->structs.index_range()) {
611 SDNA_Struct *struct_info = sdna->structs[struct_index];
612 sdna->types_to_structs_map.add_new(sdna->types[struct_info->type_index], int(struct_index));
613 }
614 }
615#endif
616
617 /* Calculate 'sdna->pointer_size'.
618 *
619 * NOTE: Cannot just do `sizeof(void *)` here, since the current DNA may come from a blend-file
620 * saved on a different system, using a different pointer size. So instead, use half the size of
621 * the #ListBase struct (only made of two pointers).
622 */
623 {
624 const int struct_index = DNA_struct_find_index_without_alias(sdna, "ListBase");
625
626 /* Should never happen, only with corrupt file for example. */
627 if (struct_index == -1) [[unlikely]] {
628 *r_error_message = "ListBase struct error! Not found.";
629 return false;
630 }
631
632 const SDNA_Struct *struct_info = sdna->structs[struct_index];
633 sdna->pointer_size = sdna->types_size[struct_info->type_index] / 2;
634
635 /* Should never fail, double-check that #ListBase struct is still what is should be
636 * (a couple of pointers and nothing else). */
637 if (struct_info->members_num != 2 || !ELEM(sdna->pointer_size, 4, 8)) [[unlikely]] {
638 *r_error_message = "ListBase struct error: invalid computed pointer-size.";
639 return false;
640 }
641 }
642
643 /* Cache name size. */
645 for (int member_index = 0; member_index < sdna->members_num; member_index++) {
646 const int array_num = DNA_member_array_num(sdna->members[member_index]);
647 if (array_num < 0 || array_num > SHRT_MAX) [[unlikely]] {
648 *r_error_message = "Invalid struct member array size in SDNA file";
649 return false;
650 }
651 sdna->members_array_num[member_index] = short(array_num);
652 }
653
654 /* Reject internally inconsistent DNA before it reaches struct reconstruction. */
655 for (const SDNA_Struct *struct_info : sdna->structs) {
656 /* The old makesdna counted the wrongly parsed "gravity" float in the struct size. */
657 const int64_t voided_members_size = (struct_info == gravity_fix_struct) ? sizeof(float) : 0;
658 if (!sdna_struct_size_check(sdna, struct_info, voided_members_size, r_error_message)) {
659 return false;
660 }
661 }
662
663 sdna->types_alignment = Array<int>(sdna->types_num, int(__STDCPP_DEFAULT_NEW_ALIGNMENT__));
664 {
665 /* TODO: This should be generalized at some point. We should be able to specify `overaligned`
666 * types directly in the DNA struct definitions. */
667 uint dummy_index = 0;
668 const int mat4x4f_struct_index = DNA_struct_find_index_without_alias_ex(
669 sdna, "mat4x4f", &dummy_index);
670 if (mat4x4f_struct_index > 0) {
671 const SDNA_Struct *struct_info = sdna->structs[mat4x4f_struct_index];
672 const int mat4x4f_type_index = struct_info->type_index;
673 sdna->types_alignment[mat4x4f_type_index] = alignof(float4x4);
674 }
675 }
676
677 return true;
678}
679
680std::unique_ptr<SDNA> DNA_sdna_from_data(const void *data,
681 const int data_len,
682 bool data_alloc,
683 const bool do_alias,
684 const char **r_error_message)
685{
686 auto sdna = std::make_unique<SDNA>();
687 const char *error_message = nullptr;
688
689 sdna->data_size = data_len;
690 if (data_alloc) {
691 char *data_copy = MEM_new_array_uninitialized<char>(size_t(data_len), "sdna_data");
692 memcpy(data_copy, data, data_len);
693 sdna->data = data_copy;
694 }
695 else {
696 sdna->data = static_cast<const char *>(data);
697 }
698 sdna->data_alloc = data_alloc;
699
700 if (init_structDNA(sdna.get(), &error_message)) {
701 if (do_alias) {
703 }
704 return sdna;
705 }
706
707 if (r_error_message == nullptr) {
708 fprintf(stderr, "Error decoding blend file SDNA: %s\n", error_message);
709 }
710 else {
711 *r_error_message = error_message;
712 }
713 return nullptr;
714}
715
717{
718 /* This data is read-only and only changes between Blender versions, so it is safe to create once
719 * and reuse. */
720 static std::unique_ptr<SDNA> g_sdna = DNA_sdna_from_data(DNAstr, DNAlen, false, true, nullptr);
721 return g_sdna.get();
722}
723
724/* ******************** END READ DNA ********************** */
725
726/* ******************* HANDLE DNA ***************** */
727
732static void set_compare_flags_for_struct(const SDNA *oldsdna,
733 const SDNA *newsdna,
734 char *compare_flags,
735 const int old_struct_index)
736{
737 if (compare_flags[old_struct_index] != SDNA_CMP_UNKNOWN) {
738 /* This flag has been initialized already. */
739 return;
740 }
741
742 SDNA_Struct *old_struct = oldsdna->structs[old_struct_index];
743 const StringRef struct_name = oldsdna->types[old_struct->type_index];
744
745 const int new_struct_index = DNA_struct_find_index_without_alias(newsdna, struct_name);
746 if (new_struct_index == -1) {
747 /* Didn't find a matching new struct, so it has been removed. */
748 compare_flags[old_struct_index] = SDNA_CMP_REMOVED;
749 return;
750 }
751
752 SDNA_Struct *new_struct = newsdna->structs[new_struct_index];
753 if (old_struct->members_num != new_struct->members_num) {
754 /* Structs with a different amount of members are not equal. */
755 compare_flags[old_struct_index] = SDNA_CMP_NOT_EQUAL;
756 return;
757 }
758 if (oldsdna->types_size[old_struct->type_index] != newsdna->types_size[new_struct->type_index]) {
759 /* Structs that don't have the same size are not equal. */
760 compare_flags[old_struct_index] = SDNA_CMP_NOT_EQUAL;
761 return;
762 }
763
764 /* Compare each member individually. */
765 for (int member_index = 0; member_index < old_struct->members_num; member_index++) {
766 const SDNA_StructMember *old_member = &old_struct->members[member_index];
767 const SDNA_StructMember *new_member = &new_struct->members[member_index];
768
769 const StringRef old_type_name = oldsdna->types[old_member->type_index];
770 const StringRef new_type_name = newsdna->types[new_member->type_index];
771 if (old_type_name != new_type_name) {
772 /* If two members have a different type in the same place, the structs are not equal. */
773 compare_flags[old_struct_index] = SDNA_CMP_NOT_EQUAL;
774 return;
775 }
776
777 const StringRef old_member_name = oldsdna->members[old_member->member_index];
778 const StringRef new_member_name = newsdna->members[new_member->member_index];
779 if (old_member_name != new_member_name) {
780 /* If two members have a different name in the same place, the structs are not equal. */
781 compare_flags[old_struct_index] = SDNA_CMP_NOT_EQUAL;
782 return;
783 }
784
785 if (ispointer(old_member_name)) {
786 if (oldsdna->pointer_size != newsdna->pointer_size) {
787 /* When the struct contains a pointer, and the pointer sizes differ, the structs are not
788 * equal. */
789 compare_flags[old_struct_index] = SDNA_CMP_NOT_EQUAL;
790 return;
791 }
792 }
793 else {
794 const int old_member_struct_index = DNA_struct_find_index_without_alias(oldsdna,
795 old_type_name);
796 if (old_member_struct_index >= 0) {
797 set_compare_flags_for_struct(oldsdna, newsdna, compare_flags, old_member_struct_index);
798 if (compare_flags[old_member_struct_index] != SDNA_CMP_EQUAL) {
799 /* If an embedded struct is not equal, the parent struct cannot be equal either. */
800 compare_flags[old_struct_index] = SDNA_CMP_NOT_EQUAL;
801 return;
802 }
803 }
804 }
805 }
806
807 compare_flags[old_struct_index] = SDNA_CMP_EQUAL;
808}
809
810const char *DNA_struct_get_compareflags(const SDNA *oldsdna, const SDNA *newsdna)
811{
812 if (oldsdna->structs.is_empty()) {
813 printf("error: file without SDNA\n");
814 return nullptr;
815 }
816
817 char *compare_flags = MEM_new_array_uninitialized<char>(oldsdna->structs.size(),
818 "compare flags");
819 memset(compare_flags, SDNA_CMP_UNKNOWN, oldsdna->structs.size());
820
821 /* Set correct flag for every struct. */
822 for (const int old_struct_index : oldsdna->structs.index_range()) {
823 set_compare_flags_for_struct(oldsdna, newsdna, compare_flags, old_struct_index);
824 BLI_assert(compare_flags[old_struct_index] != SDNA_CMP_UNKNOWN);
825 }
826
827 /* First struct is the fake 'raw data' one (see the #SDNA_TYPE_RAW_DATA 'basic type' definition
828 * and its usages). By definition, it is always 'equal'.
829 *
830 * NOTE: Bugs History (pre-4.3).
831 *
832 * It used to be `struct Link`, it was skipped in compare_flags (at index `0`). This was a bug,
833 * and was dirty-patched by setting `compare_flags[0]` to `SDNA_CMP_EQUAL` unconditionally.
834 *
835 * Then the `0` struct became `struct DrawDataList`, which was never actually written in
836 * blend-files.
837 *
838 * Write and read blend-file code also has had implicit assumptions that a `0` value in the
839 * #BHead.SDNAnr (aka DNA struct index) meant 'raw data', and therefore was not representing any
840 * real DNA struct. This assumption has been false for years. By luck, this bug seems to have
841 * been fully harmless, for at least the following reasons:
842 * - Read code always ignored DNA struct info in BHead blocks with a `0` value.
843 * - `DrawDataList` data was never actually written in blend-files.
844 * - `struct Link` never needed DNA-versioning.
845 *
846 * NOTE: This may have been broken in BE/LE conversion cases, however this endianness handling
847 * code have likely been dead/never used in practice for many years, and has been removed in
848 * Blender 5.0.
849 */
850 BLI_STATIC_ASSERT(SDNA_RAW_DATA_STRUCT_INDEX == 0, "'raw data' SDNA struct index should be 0")
852
853/* This code can be enabled to see which structs have changed. */
854#if 0
855 for (int a = 0; a < oldsdna->structs_len; a++) {
856 if (compare_flags[a] == SDNA_CMP_NOT_EQUAL) {
857 SDNA_Struct *struct_info = oldsdna->structs[a];
858 printf("changed: %s\n", oldsdna->types[struct_info->type]);
859 }
860 }
861#endif
862
863 return compare_flags;
864}
865
878static void cast_primitive_type(const eSDNA_Type old_type,
879 const eSDNA_Type new_type,
880 const int array_len,
881 const char *old_data,
882 char *new_data)
883{
884 /* define lengths */
885 const int oldlen = DNA_elem_type_size(old_type);
886 const int curlen = DNA_elem_type_size(new_type);
887
888 double old_value_f = 0.0;
889 /* Intentionally overflow signed values into an unsigned type.
890 * Casting back to a signed value preserves the sign (when the new value is signed). */
891 uint64_t old_value_i = 0;
892
893 for (int a = 0; a < array_len; a++) {
894 switch (old_type) {
895 case SDNA_TYPE_CHAR: {
896 const char value = *old_data;
897 old_value_i = value;
898 old_value_f = double(value);
899 break;
900 }
901 case SDNA_TYPE_UCHAR: {
902 const uchar value = *reinterpret_cast<const uchar *>(old_data);
903 old_value_i = value;
904 old_value_f = double(value);
905 break;
906 }
907 case SDNA_TYPE_SHORT: {
908 const short value = *reinterpret_cast<const short *>(old_data);
909 old_value_i = value;
910 old_value_f = double(value);
911 break;
912 }
913 case SDNA_TYPE_USHORT: {
914 const ushort value = *reinterpret_cast<const ushort *>(old_data);
915 old_value_i = value;
916 old_value_f = double(value);
917 break;
918 }
919 case SDNA_TYPE_INT: {
920 const int value = *reinterpret_cast<const int *>(old_data);
921 old_value_i = value;
922 old_value_f = double(value);
923 break;
924 }
925 case SDNA_TYPE_FLOAT: {
926 const float value = *reinterpret_cast<const float *>(old_data);
927 /* `int64_t` range stored in a `uint64_t`. */
928 old_value_i = uint64_t(int64_t(value));
929 old_value_f = value;
930 break;
931 }
932 case SDNA_TYPE_DOUBLE: {
933 const double value = *reinterpret_cast<const double *>(old_data);
934 /* `int64_t` range stored in a `uint64_t`. */
935 old_value_i = uint64_t(int64_t(value));
936 old_value_f = value;
937 break;
938 }
939 case SDNA_TYPE_INT64: {
940 const int64_t value = *reinterpret_cast<const int64_t *>(old_data);
941 old_value_i = uint64_t(value);
942 old_value_f = double(value);
943 break;
944 }
945 case SDNA_TYPE_UINT64: {
946 const uint64_t value = *reinterpret_cast<const uint64_t *>(old_data);
947 old_value_i = value;
948 old_value_f = double(value);
949 break;
950 }
951 case SDNA_TYPE_INT8: {
952 const int8_t value = *reinterpret_cast<const int8_t *>(old_data);
953 old_value_i = uint64_t(value);
954 old_value_f = double(value);
955 break;
956 }
957 case SDNA_TYPE_VOID:
959 BLI_assert_msg(false,
960 "Conversion from SDNA_TYPE_VOID/SDNA_TYPE_RAW_DATA is not supported");
961 break;
962 }
963
964 switch (new_type) {
965 case SDNA_TYPE_CHAR:
966 *new_data = char(old_value_i);
967 break;
968 case SDNA_TYPE_UCHAR:
969 *reinterpret_cast<uchar *>(new_data) = uchar(old_value_i);
970 break;
971 case SDNA_TYPE_SHORT:
972 *reinterpret_cast<short *>(new_data) = short(old_value_i);
973 break;
974 case SDNA_TYPE_USHORT:
975 *reinterpret_cast<ushort *>(new_data) = ushort(old_value_i);
976 break;
977 case SDNA_TYPE_INT:
978 *reinterpret_cast<int *>(new_data) = int(old_value_i);
979 break;
980 case SDNA_TYPE_FLOAT:
981 if (old_type < 2) {
982 old_value_f /= 255.0;
983 }
984 *reinterpret_cast<float *>(new_data) = old_value_f;
985 break;
986 case SDNA_TYPE_DOUBLE:
987 if (old_type < 2) {
988 old_value_f /= 255.0;
989 }
990 *reinterpret_cast<double *>(new_data) = old_value_f;
991 break;
992 case SDNA_TYPE_INT64:
993 *reinterpret_cast<int64_t *>(new_data) = int64_t(old_value_i);
994 break;
995 case SDNA_TYPE_UINT64:
996 *reinterpret_cast<uint64_t *>(new_data) = old_value_i;
997 break;
998 case SDNA_TYPE_INT8:
999 *reinterpret_cast<int8_t *>(new_data) = int8_t(old_value_i);
1000 break;
1001 case SDNA_TYPE_VOID:
1002 case SDNA_TYPE_RAW_DATA:
1003 BLI_assert_msg(false, "Conversion to SDNA_TYPE_VOID/SDNA_TYPE_RAW_DATA is not supported");
1004 break;
1005 }
1006
1007 old_data += oldlen;
1008 new_data += curlen;
1009 }
1010}
1011
1012static void cast_pointer_32_to_64(const int array_len,
1013 const uint32_t *old_data,
1014 uint64_t *new_data)
1015{
1016 for (int a = 0; a < array_len; a++) {
1017 new_data[a] = old_data[a];
1018 }
1019}
1020
1021static void cast_pointer_64_to_32(const int array_len,
1022 const uint64_t *old_data,
1023 uint32_t *new_data)
1024{
1025 /* WARNING: 32-bit Blender trying to load file saved by 64-bit Blender,
1026 * pointers may lose uniqueness on truncation! (Hopefully this won't
1027 * happen unless/until we ever get to multi-gigabyte .blend files...) */
1028 for (int a = 0; a < array_len; a++) {
1029 new_data[a] = old_data[a] >> 3;
1030 }
1031}
1032
1036static bool elem_streq(const StringRef name, const StringRef oname)
1037{
1038 for (const int64_t a : IndexRange(std::min(name.size(), oname.size()))) {
1039 if (name[a] != oname[a]) {
1040 return false;
1041 }
1042 if (name[a] == '[' || oname[a] == '[') {
1043 return true;
1044 }
1045 }
1046 return name.size() == oname.size();
1047}
1048
1059 /* Expand SDNA. */
1060 const Span<StringRef> types,
1061 const Span<StringRef> names,
1062 /* Regular args. */
1063 const StringRef type,
1064 const StringRef name,
1065 const SDNA_Struct *old)
1066{
1067 /* in old is the old struct */
1068 for (int a = 0; a < old->members_num; a++) {
1069 const SDNA_StructMember *member = &old->members[a];
1070 const StringRef otype = types[member->type_index];
1071 const StringRef oname = names[member->member_index];
1072
1073 if (elem_streq(name, oname)) { /* name equal */
1074 return type == otype; /* type equal */
1075 }
1076 }
1077 return false;
1078}
1079
1083static bool elem_exists_without_alias(const SDNA *sdna,
1084 const StringRef type,
1085 const StringRef name,
1086 const SDNA_Struct *old)
1087{
1088 return elem_exists_impl(
1089 /* Expand SDNA. */
1090 sdna->types,
1091 sdna->members,
1092 /* Regular args. */
1093 type,
1094 name,
1095 old);
1096}
1097
1098static bool elem_exists_with_alias(const SDNA *sdna,
1099 const StringRef type,
1100 const StringRef name,
1101 const SDNA_Struct *old)
1102{
1103 return elem_exists_impl(
1104 /* Expand SDNA. */
1105 sdna->alias.types,
1106 sdna->alias.members,
1107 /* Regular args. */
1108 type,
1109 name,
1110 old);
1111}
1112
1113static int elem_offset_impl(const SDNA *sdna,
1114 const Span<StringRef> types,
1115 const Span<StringRef> names,
1116 const StringRef type,
1117 const StringRef name,
1118 const SDNA_Struct *old)
1119{
1120 /* Without array-part, so names can differ: return old `namenr` and type. */
1121
1122 /* in old is the old struct */
1123 int offset = 0;
1124 for (int a = 0; a < old->members_num; a++) {
1125 const SDNA_StructMember *member = &old->members[a];
1126 const StringRef otype = types[member->type_index];
1127 const StringRef oname = names[member->member_index];
1128 if (elem_streq(name, oname)) { /* name equal */
1129 if (type == otype) { /* type equal */
1130 return offset;
1131 }
1132 break; /* Fail below. */
1133 }
1134 offset += DNA_struct_member_size(sdna, member->type_index, member->member_index);
1135 }
1136 return -1;
1137}
1138
1154static int elem_offset_without_alias(const SDNA *sdna,
1155 const StringRef type,
1156 const StringRef name,
1157 const SDNA_Struct *old)
1158{
1159 return elem_offset_impl(sdna, sdna->types, sdna->members, type, name, old);
1160}
1161
1165static int elem_offset_with_alias(const SDNA *sdna,
1166 const StringRef type,
1167 const StringRef name,
1168 const SDNA_Struct *old)
1169{
1170 return elem_offset_impl(sdna, sdna->alias.types, sdna->alias.members, type, name, old);
1171}
1172
1173/* Each struct member belongs to one of the categories below. */
1179
1181 const SDNA_StructMember *member)
1182{
1183 const StringRef member_name = sdna->members[member->member_index];
1184 if (ispointer(member_name)) {
1186 }
1187 const StringRef member_type_name = sdna->types[member->type_index];
1188 if (DNA_struct_exists_without_alias(sdna, member_type_name)) {
1190 }
1192}
1193
1194static int get_member_size_in_bytes(const SDNA *sdna, const SDNA_StructMember *member)
1195{
1196 const StringRef name = sdna->members[member->member_index];
1197 const int array_length = sdna->members_array_num[member->member_index];
1198 if (ispointer(name)) {
1199 return sdna->pointer_size * array_length;
1200 }
1201 const int type_size = sdna->types_size[member->type_index];
1202 return type_size * array_length;
1203}
1204
1213
1216 union {
1217 struct {
1218 int old_offset;
1219 int new_offset;
1220 int size;
1221 } memcpy;
1222 struct {
1223 int old_offset;
1224 int new_offset;
1225 int array_len;
1229 struct {
1230 int old_offset;
1231 int new_offset;
1232 int array_len;
1233 } cast_pointer;
1234 struct {
1235 int old_offset;
1236 int new_offset;
1237 int array_len;
1238 short old_struct_index;
1239 short new_struct_index;
1240 } substruct;
1242};
1243
1252
1253static void reconstruct_structs(const DNA_ReconstructInfo *reconstruct_info,
1254 const int blocks,
1255 const int old_struct_index,
1256 const int new_struct_index,
1257 const char *old_blocks,
1258 char *new_blocks);
1259
1269static void reconstruct_struct(const DNA_ReconstructInfo *reconstruct_info,
1270 const int new_struct_index,
1271 const char *old_block,
1272 char *new_block)
1273{
1274 const ReconstructStep *steps = reconstruct_info->steps[new_struct_index];
1275 const int step_count = reconstruct_info->step_counts[new_struct_index];
1276
1277 /* Execute all preprocessed steps. */
1278 for (int a = 0; a < step_count; a++) {
1279 const ReconstructStep *step = &steps[a];
1280 switch (step->type) {
1282 memcpy(new_block + step->data.memcpy.new_offset,
1283 old_block + step->data.memcpy.old_offset,
1284 step->data.memcpy.size);
1285 break;
1287 cast_primitive_type(step->data.cast_primitive.old_type,
1288 step->data.cast_primitive.new_type,
1289 step->data.cast_primitive.array_len,
1290 old_block + step->data.cast_primitive.old_offset,
1291 new_block + step->data.cast_primitive.new_offset);
1292 break;
1295 step->data.cast_pointer.array_len,
1296 reinterpret_cast<const uint64_t *>(old_block + step->data.cast_pointer.old_offset),
1297 reinterpret_cast<uint32_t *>(new_block + step->data.cast_pointer.new_offset));
1298 break;
1301 step->data.cast_pointer.array_len,
1302 reinterpret_cast<const uint32_t *>(old_block + step->data.cast_pointer.old_offset),
1303 reinterpret_cast<uint64_t *>(new_block + step->data.cast_pointer.new_offset));
1304 break;
1306 reconstruct_structs(reconstruct_info,
1307 step->data.substruct.array_len,
1308 step->data.substruct.old_struct_index,
1309 step->data.substruct.new_struct_index,
1310 old_block + step->data.substruct.old_offset,
1311 new_block + step->data.substruct.new_offset);
1312 break;
1314 /* Do nothing, because the memory block are zeroed (from #MEM_new_zeroed).
1315 *
1316 * Note that the struct could be initialized with the default struct,
1317 * however this complicates versioning, especially with flags, see: D4500. */
1318 break;
1319 }
1320 }
1321}
1322
1324static void reconstruct_structs(const DNA_ReconstructInfo *reconstruct_info,
1325 const int blocks,
1326 const int old_struct_index,
1327 const int new_struct_index,
1328 const char *old_blocks,
1329 char *new_blocks)
1330{
1331 const SDNA_Struct *old_struct = reconstruct_info->oldsdna->structs[old_struct_index];
1332 const SDNA_Struct *new_struct = reconstruct_info->newsdna->structs[new_struct_index];
1333
1334 const int old_block_size = reconstruct_info->oldsdna->types_size[old_struct->type_index];
1335 const int new_block_size = reconstruct_info->newsdna->types_size[new_struct->type_index];
1336
1337 for (int a = 0; a < blocks; a++) {
1338 const char *old_block = old_blocks + a * old_block_size;
1339 char *new_block = new_blocks + a * new_block_size;
1340 reconstruct_struct(reconstruct_info, new_struct_index, old_block, new_block);
1341 }
1342}
1343
1344void *DNA_struct_reconstruct(const DNA_ReconstructInfo *reconstruct_info,
1345 int old_struct_index,
1346 int blocks,
1347 const void *old_blocks,
1348 const char *alloc_name,
1349 int64_t *r_alloc_size)
1350{
1351 const SDNA *oldsdna = reconstruct_info->oldsdna;
1352 const SDNA *newsdna = reconstruct_info->newsdna;
1353
1354 const SDNA_Struct *old_struct = oldsdna->structs[old_struct_index];
1355 const StringRef type_name = oldsdna->types[old_struct->type_index];
1356 const int new_struct_index = DNA_struct_find_index_without_alias(newsdna, type_name);
1357
1358 if (new_struct_index == -1) {
1359 return nullptr;
1360 }
1361
1362 const SDNA_Struct *new_struct = newsdna->structs[new_struct_index];
1363 const int new_block_size = newsdna->types_size[new_struct->type_index];
1364
1365 const int alignment = DNA_struct_alignment(newsdna, new_struct_index);
1366 char *new_blocks = static_cast<char *>(
1367 MEM_new_array_zeroed_aligned(new_block_size, blocks, alignment, alloc_name));
1368 reconstruct_structs(reconstruct_info,
1369 blocks,
1370 old_struct_index,
1371 new_struct_index,
1372 static_cast<const char *>(old_blocks),
1373 new_blocks);
1374 if (r_alloc_size) {
1375 *r_alloc_size = int64_t(new_block_size) * blocks;
1376 }
1377 return new_blocks;
1378}
1379
1382 const SDNA_Struct *struct_info,
1383 const StringRef name,
1384 int *r_offset)
1385{
1386 int offset = 0;
1387 for (int a = 0; a < struct_info->members_num; a++) {
1388 const SDNA_StructMember *member = &struct_info->members[a];
1389 const StringRef member_name = sdna->members[member->member_index];
1390 if (elem_streq(name, member_name)) {
1391 *r_offset = offset;
1392 return member;
1393 }
1394 offset += get_member_size_in_bytes(sdna, member);
1395 }
1396 return nullptr;
1397}
1398
1400static void init_reconstruct_step_for_member(const SDNA *oldsdna,
1401 const SDNA *newsdna,
1402 const char *compare_flags,
1403 const SDNA_Struct *old_struct,
1404 const SDNA_StructMember *new_member,
1405 const int new_member_offset,
1406 ReconstructStep *r_step)
1407{
1408
1409 /* Find the matching old member. */
1410 int old_member_offset;
1411 const StringRef new_name = newsdna->members[new_member->member_index];
1413 oldsdna, old_struct, new_name, &old_member_offset);
1414
1415 if (old_member == nullptr) {
1416 /* No matching member has been found in the old struct. */
1418 return;
1419 }
1420
1421 /* Determine the member category of the old an new members. */
1422 const eStructMemberCategory new_category = get_struct_member_category(newsdna, new_member);
1423 const eStructMemberCategory old_category = get_struct_member_category(oldsdna, old_member);
1424
1425 if (new_category != old_category) {
1426 /* Can only reconstruct the new member based on the old member, when the belong to the same
1427 * category. */
1429 return;
1430 }
1431
1432 const int new_array_length = newsdna->members_array_num[new_member->member_index];
1433 const int old_array_length = oldsdna->members_array_num[old_member->member_index];
1434 const int shared_array_length = std::min(new_array_length, old_array_length);
1435
1436 const StringRef new_type_name = newsdna->types[new_member->type_index];
1437 const StringRef old_type_name = oldsdna->types[old_member->type_index];
1438
1439 switch (new_category) {
1441 if (new_type_name == old_type_name) {
1442 const int old_struct_index = DNA_struct_find_index_without_alias(oldsdna, old_type_name);
1443 BLI_assert(old_struct_index != -1);
1444 enum eSDNA_StructCompare compare_flag = eSDNA_StructCompare(
1445 compare_flags[old_struct_index]);
1446 BLI_assert(compare_flag != SDNA_CMP_REMOVED);
1447 if (compare_flag == SDNA_CMP_EQUAL) {
1448 /* The old and new members are identical, just do a #memcpy. */
1449 r_step->type = RECONSTRUCT_STEP_MEMCPY;
1450 r_step->data.memcpy.new_offset = new_member_offset;
1451 r_step->data.memcpy.old_offset = old_member_offset;
1452 r_step->data.memcpy.size = newsdna->types_size[new_member->type_index] *
1453 shared_array_length;
1454 }
1455 else {
1456 const int new_struct_index = DNA_struct_find_index_without_alias(newsdna, new_type_name);
1457 BLI_assert(new_struct_index != -1);
1458
1459 /* The old and new members are different, use recursion to reconstruct the
1460 * nested struct. */
1461 BLI_assert(compare_flag == SDNA_CMP_NOT_EQUAL);
1463 r_step->data.substruct.new_offset = new_member_offset;
1464 r_step->data.substruct.old_offset = old_member_offset;
1465 r_step->data.substruct.array_len = shared_array_length;
1466 r_step->data.substruct.new_struct_index = new_struct_index;
1467 r_step->data.substruct.old_struct_index = old_struct_index;
1468 }
1469 }
1470 else {
1471 /* Cannot match structs that have different names. */
1473 }
1474 break;
1475 }
1477 if (new_type_name == old_type_name) {
1478 /* Primitives with the same name cannot be different, so just do a #memcpy. */
1479 r_step->type = RECONSTRUCT_STEP_MEMCPY;
1480 r_step->data.memcpy.new_offset = new_member_offset;
1481 r_step->data.memcpy.old_offset = old_member_offset;
1482 r_step->data.memcpy.size = newsdna->types_size[new_member->type_index] *
1483 shared_array_length;
1484 }
1485 else {
1486 /* The old and new primitive types are different, cast from the old to new type. */
1488 r_step->data.cast_primitive.array_len = shared_array_length;
1489 r_step->data.cast_primitive.new_offset = new_member_offset;
1490 r_step->data.cast_primitive.old_offset = old_member_offset;
1491 r_step->data.cast_primitive.new_type = eSDNA_Type(new_member->type_index);
1492 r_step->data.cast_primitive.old_type = eSDNA_Type(old_member->type_index);
1493 }
1494 break;
1495 }
1497 if (newsdna->pointer_size == oldsdna->pointer_size) {
1498 /* The pointer size is the same, so just do a #memcpy. */
1499 r_step->type = RECONSTRUCT_STEP_MEMCPY;
1500 r_step->data.memcpy.new_offset = new_member_offset;
1501 r_step->data.memcpy.old_offset = old_member_offset;
1502 r_step->data.memcpy.size = newsdna->pointer_size * shared_array_length;
1503 }
1504 else if (newsdna->pointer_size == 8 && oldsdna->pointer_size == 4) {
1505 /* Need to convert from 32 bit to 64 bit pointers. */
1507 r_step->data.cast_pointer.new_offset = new_member_offset;
1508 r_step->data.cast_pointer.old_offset = old_member_offset;
1509 r_step->data.cast_pointer.array_len = shared_array_length;
1510 }
1511 else if (newsdna->pointer_size == 4 && oldsdna->pointer_size == 8) {
1512 /* Need to convert from 64 bit to 32 bit pointers. */
1514 r_step->data.cast_pointer.new_offset = new_member_offset;
1515 r_step->data.cast_pointer.old_offset = old_member_offset;
1516 r_step->data.cast_pointer.array_len = shared_array_length;
1517 }
1518 else {
1519 BLI_assert_msg(0, "invalid pointer size");
1521 }
1522 break;
1523 }
1524 }
1525}
1526
1528[[maybe_unused]] static void print_reconstruct_step(const ReconstructStep *step,
1529 const SDNA *oldsdna,
1530 const SDNA *newsdna)
1531{
1532 switch (step->type) {
1534 printf("initialize zero");
1535 break;
1536 }
1538 printf("memcpy, size: %d, old offset: %d, new offset: %d",
1539 step->data.memcpy.size,
1540 step->data.memcpy.old_offset,
1541 step->data.memcpy.new_offset);
1542 break;
1543 }
1545 printf(
1546 "cast element, old type: %d ('%s'), new type: %d ('%s'), old offset: %d, new offset: "
1547 "%d, length: %d",
1548 int(step->data.cast_primitive.old_type),
1549 std::string(oldsdna->types[step->data.cast_primitive.old_type]).c_str(),
1550 int(step->data.cast_primitive.new_type),
1551 std::string(newsdna->types[step->data.cast_primitive.new_type]).c_str(),
1552 step->data.cast_primitive.old_offset,
1553 step->data.cast_primitive.new_offset,
1554 step->data.cast_primitive.array_len);
1555 break;
1556 }
1558 printf("pointer to 32, old offset: %d, new offset: %d, length: %d",
1559 step->data.cast_pointer.old_offset,
1560 step->data.cast_pointer.new_offset,
1561 step->data.cast_pointer.array_len);
1562 break;
1563 }
1565 printf("pointer to 64, old offset: %d, new offset: %d, length: %d",
1566 step->data.cast_pointer.old_offset,
1567 step->data.cast_pointer.new_offset,
1568 step->data.cast_pointer.array_len);
1569 break;
1570 }
1572 printf(
1573 "substruct, old offset: %d, new offset: %d, new struct: %d ('%s', size per struct: %d), "
1574 "length: %d",
1575 step->data.substruct.old_offset,
1576 step->data.substruct.new_offset,
1577 step->data.substruct.new_struct_index,
1578 std::string(
1579 newsdna->types[newsdna->structs[step->data.substruct.new_struct_index]->type_index])
1580 .c_str(),
1581 newsdna->types_size[newsdna->structs[step->data.substruct.new_struct_index]->type_index],
1582 step->data.substruct.array_len);
1583 break;
1584 }
1585 }
1586}
1587
1593 const SDNA *newsdna,
1594 const char *compare_flags,
1595 const SDNA_Struct *old_struct,
1596 const SDNA_Struct *new_struct)
1597{
1599 __func__);
1600
1601 int new_member_offset = 0;
1602 for (int new_member_index = 0; new_member_index < new_struct->members_num; new_member_index++) {
1603 const SDNA_StructMember *new_member = &new_struct->members[new_member_index];
1605 newsdna,
1606 compare_flags,
1607 old_struct,
1608 new_member,
1609 new_member_offset,
1610 &steps[new_member_index]);
1611 new_member_offset += get_member_size_in_bytes(newsdna, new_member);
1612 }
1613
1614 return steps;
1615}
1616
1618static int compress_reconstruct_steps(ReconstructStep *steps, const int old_step_count)
1619{
1620 int new_step_count = 0;
1621 for (int a = 0; a < old_step_count; a++) {
1622 ReconstructStep *step = &steps[a];
1623 switch (step->type) {
1625 /* These steps are simply removed. */
1626 break;
1628 if (new_step_count > 0) {
1629 /* Try to merge this memcpy step with the previous one. */
1630 ReconstructStep *prev_step = &steps[new_step_count - 1];
1631 if (prev_step->type == RECONSTRUCT_STEP_MEMCPY) {
1632 /* Check if there are no bytes between the blocks to copy. */
1633 if (prev_step->data.memcpy.old_offset + prev_step->data.memcpy.size ==
1634 step->data.memcpy.old_offset &&
1635 prev_step->data.memcpy.new_offset + prev_step->data.memcpy.size ==
1636 step->data.memcpy.new_offset)
1637 {
1638 prev_step->data.memcpy.size += step->data.memcpy.size;
1639 break;
1640 }
1641 }
1642 }
1643 steps[new_step_count] = *step;
1644 new_step_count++;
1645 break;
1650 /* These steps are not changed at all for now. It should be possible to merge consecutive
1651 * steps of the same type, but it is not really worth it. */
1652 steps[new_step_count] = *step;
1653 new_step_count++;
1654 break;
1655 }
1656 }
1657 return new_step_count;
1658}
1659
1661 const SDNA *newsdna,
1662 const char *compare_flags)
1663{
1664 DNA_ReconstructInfo *reconstruct_info = MEM_new_zeroed<DNA_ReconstructInfo>(__func__);
1665 reconstruct_info->oldsdna = oldsdna;
1666 reconstruct_info->newsdna = newsdna;
1667 reconstruct_info->compare_flags = compare_flags;
1668 reconstruct_info->step_counts = MEM_new_array_uninitialized<int>(size_t(newsdna->structs.size()),
1669 __func__);
1671 size_t(newsdna->structs.size()), __func__);
1672
1673 /* Generate reconstruct steps for all structs. */
1674 for (const int new_struct_index : newsdna->structs.index_range()) {
1675 const SDNA_Struct *new_struct = newsdna->structs[new_struct_index];
1676 const StringRef new_struct_name = newsdna->types[new_struct->type_index];
1677 const int old_struct_index = DNA_struct_find_index_without_alias(oldsdna, new_struct_name);
1678 if (old_struct_index < 0) {
1679 reconstruct_info->steps[new_struct_index] = nullptr;
1680 reconstruct_info->step_counts[new_struct_index] = 0;
1681 continue;
1682 }
1683 const SDNA_Struct *old_struct = oldsdna->structs[old_struct_index];
1685 oldsdna, newsdna, compare_flags, old_struct, new_struct);
1686
1687 /* Comment the line below to skip the compression for debugging purposes. */
1688 const int steps_len = compress_reconstruct_steps(steps, new_struct->members_num);
1689
1690 reconstruct_info->steps[new_struct_index] = steps;
1691 reconstruct_info->step_counts[new_struct_index] = steps_len;
1692
1693/* This is useful when debugging the reconstruct steps. */
1694#if 0
1695 printf("%s: \n", new_struct_name);
1696 for (int a = 0; a < steps_len; a++) {
1697 printf(" ");
1698 print_reconstruct_step(&steps[a], oldsdna, newsdna);
1699 printf("\n");
1700 }
1701#endif
1702 }
1703
1704 return reconstruct_info;
1705}
1706
1708{
1709 for (const int new_struct_index : reconstruct_info->newsdna->structs.index_range()) {
1710 if (reconstruct_info->steps[new_struct_index] != nullptr) {
1711 MEM_delete(reconstruct_info->steps[new_struct_index]);
1712 }
1713 }
1714 MEM_delete(reconstruct_info->steps);
1715 MEM_delete(reconstruct_info->step_counts);
1716 MEM_delete(reconstruct_info);
1717}
1718
1720 const StringRef stype,
1721 const StringRef vartype,
1722 const StringRef name)
1723{
1724 const int struct_index = DNA_struct_find_index_without_alias(sdna, stype);
1725 BLI_assert(struct_index != -1);
1726 const SDNA_Struct *const struct_info = sdna->structs[struct_index];
1727 return elem_offset_without_alias(sdna, vartype, name, struct_info);
1728}
1729
1731 const StringRef stype,
1732 const StringRef vartype,
1733 const StringRef name)
1734{
1735 const int struct_index = DNA_struct_find_with_alias(sdna, stype);
1736 BLI_assert(struct_index != -1);
1737 const SDNA_Struct *const struct_info = sdna->structs[struct_index];
1738 return elem_offset_with_alias(sdna, vartype, name, struct_info);
1739}
1740
1741bool DNA_struct_exists_without_alias(const SDNA *sdna, const StringRef stype)
1742{
1743 return DNA_struct_find_index_without_alias(sdna, stype) != -1;
1744}
1745
1747 const StringRef stype,
1748 const StringRef vartype,
1749 const StringRef name)
1750{
1751 const int struct_index = DNA_struct_find_index_without_alias(sdna, stype);
1752
1753 if (struct_index != -1) {
1754 const SDNA_Struct *const struct_info = sdna->structs[struct_index];
1755 const bool found = elem_exists_without_alias(sdna, vartype, name, struct_info);
1756
1757 if (found) {
1758 return true;
1759 }
1760 }
1761 return false;
1762}
1763
1765 const StringRef stype,
1766 const StringRef vartype,
1767 const StringRef name)
1768{
1769 const int SDNAnr = DNA_struct_find_with_alias(sdna, stype);
1770
1771 if (SDNAnr != -1) {
1772 const SDNA_Struct *const spo = sdna->structs[SDNAnr];
1773 const bool found = elem_exists_with_alias(sdna, vartype, name, spo);
1774
1775 if (found) {
1776 return true;
1777 }
1778 }
1779 return false;
1780}
1781
1783{
1784 /* should contain all enum types */
1785 switch (elem_nr) {
1786 case SDNA_TYPE_CHAR:
1787 case SDNA_TYPE_UCHAR:
1788 case SDNA_TYPE_INT8:
1789 return 1;
1790 case SDNA_TYPE_SHORT:
1791 case SDNA_TYPE_USHORT:
1792 return 2;
1793 case SDNA_TYPE_INT:
1794 case SDNA_TYPE_FLOAT:
1795 return 4;
1796 case SDNA_TYPE_DOUBLE:
1797 case SDNA_TYPE_INT64:
1798 case SDNA_TYPE_UINT64:
1799 return 8;
1800 case SDNA_TYPE_VOID:
1801 return 0;
1802 case SDNA_TYPE_RAW_DATA:
1803 BLI_assert_msg(false, "Operations on the size of SDNA_TYPE_RAW_DATA is not supported");
1804 return 0;
1805 }
1806
1807 /* weak */
1808 return 8;
1809}
1810
1811int DNA_struct_alignment(const SDNA *sdna, const int struct_index)
1812{
1813 return sdna->types_alignment[struct_index];
1814}
1815
1816StringRef DNA_struct_identifier(SDNA *sdna, const int struct_index)
1817{
1819 const SDNA_Struct *struct_info = sdna->structs[struct_index];
1820 return sdna->alias.types[struct_info->type_index];
1821}
1822
1823/* -------------------------------------------------------------------- */
1826
1827static bool DNA_sdna_patch_struct(SDNA *sdna,
1828 const int struct_index,
1829 const StringRef new_type_name)
1830{
1832 const SDNA_Struct *struct_info = sdna->structs[struct_index];
1833#ifdef WITH_DNA_GHASH
1834 sdna->types_to_structs_map.remove(sdna->types[struct_info->type_index]);
1835 sdna->types_to_structs_map.add(new_type_name, struct_index);
1836#endif
1837 sdna->types[struct_info->type_index] = new_type_name;
1838 return true;
1839}
1841 const StringRef old_type_name,
1842 const StringRef new_type_name)
1843{
1844 const int struct_index = DNA_struct_find_index_without_alias(sdna, old_type_name);
1845 if (struct_index != -1) {
1846 return DNA_sdna_patch_struct(sdna, struct_index, new_type_name);
1847 }
1848 return false;
1849}
1850
1851/* Make public if called often with same struct (avoid duplicate lookups). */
1853 const int struct_index,
1854 const StringRef old_member_name,
1855 const StringRef new_member_name)
1856{
1857 /* These names aren't handled here (it's not used).
1858 * Ensure they are never used or we get out of sync arrays. */
1859 BLI_assert(sdna->alias.members.is_empty());
1860 BLI_assert(!new_member_name.is_empty());
1861 SDNA_Struct *struct_info = sdna->structs[struct_index];
1862 for (int struct_member_index = struct_info->members_num; struct_member_index > 0;
1863 struct_member_index--)
1864 {
1865 SDNA_StructMember *member_info = &struct_info->members[struct_member_index];
1866 const StringRef old_member_name_full = sdna->members[member_info->member_index];
1867 /* Start & end offsets in #old_member_full. */
1868 uint old_member_name_full_offset_start;
1870 old_member_name, old_member_name_full, &old_member_name_full_offset_start))
1871 {
1872 const StringRef new_member_name_full = DNA_member_id_rename(
1873 sdna->mem_arena,
1874 old_member_name,
1875 new_member_name,
1876 old_member_name_full,
1877 old_member_name_full_offset_start);
1878
1879 const short old_member_index = member_info->member_index;
1880 const int old_member_array_num = sdna->members_array_num[old_member_index];
1881 member_info->member_index = sdna->members_num++;
1882 sdna->members.append(new_member_name_full);
1883 sdna->members_array_num.append(old_member_array_num);
1884
1885 return true;
1886 }
1887 }
1888 return false;
1889}
1891 const StringRef type_name,
1892 const StringRef old_member_name,
1893 const StringRef new_member_name)
1894{
1895 const int struct_index = DNA_struct_find_index_without_alias(sdna, type_name);
1896 if (struct_index != -1) {
1897 return DNA_sdna_patch_struct_member(sdna, struct_index, old_member_name, new_member_name);
1898 }
1899 return false;
1900}
1901
1903
1904/* -------------------------------------------------------------------- */
1909
1916static void sdna_expand_names(SDNA *sdna)
1917{
1918 int names_expand_len = 0;
1919 for (const int struct_index : sdna->structs.index_range()) {
1920 const SDNA_Struct *struct_old = sdna->structs[struct_index];
1921 names_expand_len += struct_old->members_num;
1922 }
1923 Vector<StringRef> names_expand(names_expand_len);
1924 Vector<short> names_array_len_expand(names_expand_len);
1925
1926 int names_expand_index = 0;
1927 for (const int struct_index : sdna->structs.index_range()) {
1928 /* We can't edit this memory 'sdna->structs' points to (read-only `datatoc` file). */
1929 const SDNA_Struct *struct_old = sdna->structs[struct_index];
1930
1931 const int array_size = sizeof(short) * 2 + sizeof(SDNA_StructMember) * struct_old->members_num;
1932 SDNA_Struct *struct_new = static_cast<SDNA_Struct *>(
1933 sdna->mem_arena.allocate(array_size, alignof(SDNA_Struct)));
1934 memcpy(struct_new, struct_old, array_size);
1935 sdna->structs[struct_index] = struct_new;
1936
1937 for (int i = 0; i < struct_old->members_num; i++) {
1938 const SDNA_StructMember *member_old = &struct_old->members[i];
1939 SDNA_StructMember *member_new = &struct_new->members[i];
1940
1941 names_expand[names_expand_index] = sdna->members[member_old->member_index];
1942 names_array_len_expand[names_expand_index] =
1943 sdna->members_array_num[member_old->member_index];
1944
1945 BLI_assert(names_expand_index < SHRT_MAX);
1946 member_new->member_index = names_expand_index;
1947 names_expand_index++;
1948 }
1949 }
1950 sdna->members = std::move(names_expand);
1951 sdna->members_array_num = std::move(names_array_len_expand);
1952
1953 sdna->members_num = names_expand_len;
1954}
1955
1957 const DnaRenameMaps &rename_maps,
1958 const StringRef struct_name_static,
1959 const StringRef elem_static_full)
1960{
1961 const StringRef elem_static = DNA_member_id_string_ref(elem_static_full);
1962 const StringRef *elem_alias = rename_maps.members.lookup_ptr({struct_name_static, elem_static});
1963 if (elem_alias) {
1964 return DNA_member_id_rename(sdna->mem_arena,
1965 elem_static,
1966 *elem_alias,
1967 elem_static_full,
1968 DNA_member_id_offset_start(elem_static_full));
1969 }
1970 return nullptr;
1971}
1972
1974{
1975 if (!sdna->alias.members.is_empty() && !sdna->alias.types.is_empty()) {
1976 return;
1977 }
1978
1979 /* We may want this to be optional later. */
1980 const bool use_legacy_hack = true;
1981
1982 const DnaRenameMaps rename_maps = DNA_rename_maps_static_to_alias();
1983
1984 if (sdna->alias.types.is_empty()) {
1985 sdna->alias.types = Array<StringRef>(sdna->types_num);
1986 for (int type_index = 0; type_index < sdna->types_num; type_index++) {
1987 StringRef type_name_static = sdna->types[type_index];
1988
1989 if (use_legacy_hack) {
1990 type_name_static = DNA_struct_rename_legacy_hack_alias_from_static(type_name_static);
1991 }
1992
1993 sdna->alias.types[type_index] = rename_maps.types.lookup_default_as(type_name_static,
1994 type_name_static);
1995 }
1996 }
1997
1998 if (sdna->alias.members.is_empty()) {
1999 sdna_expand_names(sdna);
2000 sdna->alias.members.resize(sdna->members_num);
2001 for (const int struct_index : sdna->structs.index_range()) {
2002 const SDNA_Struct *struct_info = sdna->structs[struct_index];
2003 StringRef struct_name_static = sdna->types[struct_info->type_index];
2004
2005 if (use_legacy_hack) {
2006 struct_name_static = DNA_struct_rename_legacy_hack_alias_from_static(struct_name_static);
2007 }
2008
2009 for (int a = 0; a < struct_info->members_num; a++) {
2010 const SDNA_StructMember *member = &struct_info->members[a];
2011 const StringRef member_alias_full = dna_sdna_alias_from_static_elem_full(
2012 sdna, rename_maps, struct_name_static, sdna->members[member->member_index]);
2013 if (member_alias_full != nullptr) {
2014 sdna->alias.members[member->member_index] = member_alias_full;
2015 }
2016 else {
2017 sdna->alias.members[member->member_index] = sdna->members[member->member_index];
2018 }
2019 }
2020 }
2021 }
2022}
2023
2025{
2026 if (!sdna->alias.types_to_structs_map.is_empty()) {
2027 return;
2028 }
2029
2031#ifdef WITH_DNA_GHASH
2032 /* create a ghash lookup to speed up */
2033 sdna->alias.types_to_structs_map.clear();
2034 sdna->alias.types_to_structs_map.reserve(sdna->structs.size());
2035 for (const int struct_index : sdna->structs.index_range()) {
2036 const SDNA_Struct *struct_info = sdna->structs[struct_index];
2037 sdna->alias.types_to_structs_map.add(sdna->alias.types[struct_info->type_index], struct_index);
2038 }
2039#else
2040 UNUSED_VARS(sdna);
2041#endif
2042}
2043
2044namespace dna::pointers {
2045
2046PointersInDNA::PointersInDNA(const SDNA &sdna) : sdna_(sdna)
2047{
2048 structs_.resize(sdna.structs.size());
2049 for (const int struct_i : sdna.structs.index_range()) {
2050 const SDNA_Struct &sdna_struct = *sdna.structs[struct_i];
2051 StructInfo &struct_info = structs_[struct_i];
2052
2053 struct_info.size_in_bytes = 0;
2054 for (const int member_i : IndexRange(sdna_struct.members_num)) {
2055 struct_info.size_in_bytes += get_member_size_in_bytes(&sdna_,
2056 &sdna_struct.members[member_i]);
2057 }
2058
2059 this->gather_pointer_members_recursive(sdna_struct, 0, structs_[struct_i]);
2060 }
2061}
2062
2063void PointersInDNA::gather_pointer_members_recursive(const SDNA_Struct &sdna_struct,
2064 int initial_offset,
2065 StructInfo &r_struct_info) const
2066{
2067 int offset = initial_offset;
2068 for (const int member_i : IndexRange(sdna_struct.members_num)) {
2069 const SDNA_StructMember &member = sdna_struct.members[member_i];
2070 const StringRef member_type_name = sdna_.types[member.type_index];
2071 const eStructMemberCategory member_category = get_struct_member_category(&sdna_, &member);
2072 const int array_elem_num = sdna_.members_array_num[member.member_index];
2073
2074 if (member_category == STRUCT_MEMBER_CATEGORY_POINTER) {
2075 for (int elem_i = 0; elem_i < array_elem_num; elem_i++) {
2076 const StringRef member_name = sdna_.members[member.member_index];
2077 r_struct_info.pointers.append(
2078 {offset + elem_i * sdna_.pointer_size, member_type_name, member_name});
2079 }
2080 }
2081 else if (member_category == STRUCT_MEMBER_CATEGORY_STRUCT) {
2082 const int substruct_i = DNA_struct_find_index_without_alias(&sdna_, member_type_name);
2083 const SDNA_Struct &sub_sdna_struct = *sdna_.structs[substruct_i];
2084 int substruct_size = sdna_.types_size[member.type_index];
2085 for (int elem_i = 0; elem_i < array_elem_num; elem_i++) {
2086 this->gather_pointer_members_recursive(
2087 sub_sdna_struct, offset + elem_i * substruct_size, r_struct_info);
2088 }
2089 }
2090 offset += get_member_size_in_bytes(&sdna_, &member);
2091 }
2092}
2093
2095{
2096 for (const PointerInfo &info : this->pointers) {
2097 if (info.offset == offset) {
2098 return true;
2099 }
2100 }
2101 return false;
2102}
2103
2104} // namespace dna::pointers
2105
2107
2108/* -------------------------------------------------------------------- */
2112
2113namespace dna {
2114
2115static void print_struct_array_recursive(const SDNA &sdna,
2116 const SDNA_Struct &sdna_struct,
2117 const void *initial_data,
2118 const int64_t element_num,
2119 const int indent,
2120 fmt::appender &dst);
2121static void print_single_struct_recursive(const SDNA &sdna,
2122 const SDNA_Struct &sdna_struct,
2123 const void *initial_data,
2124 const int indent,
2125 fmt::appender &dst);
2126
2130static bool char_array_startswith_simple_name(const char *data, const int array_len)
2131{
2132 const int string_length = strnlen(data, array_len);
2133 if (string_length == array_len) {
2134 return false;
2135 }
2136 for (const int i : IndexRange(string_length)) {
2137 const uchar c = data[i];
2138 /* This is only a very simple check and does not cover more complex cases with multi-byte UTF8
2139 * characters. It's only a heuristic anyway, making a wrong decision here just means that the
2140 * data will be printed differently. */
2141 if (!std::isprint(c)) {
2142 return false;
2143 }
2144 }
2145 return true;
2146}
2147
2148static void print_struct_array_recursive(const SDNA &sdna,
2149 const SDNA_Struct &sdna_struct,
2150 const void *data,
2151 const int64_t element_num,
2152 const int indent,
2153 fmt::appender &dst)
2154{
2155 if (element_num == 1) {
2156 print_single_struct_recursive(sdna, sdna_struct, data, indent, dst);
2157 return;
2158 }
2159
2160 const StringRef struct_name = sdna.types[sdna_struct.type_index];
2161 const int64_t struct_size = sdna.types_size[sdna_struct.type_index];
2162 for (const int64_t i : IndexRange(element_num)) {
2163 const void *element_data = POINTER_OFFSET(data, i * struct_size);
2164 fmt::format_to(dst, "{:{}}{}: <{}>\n", "", indent, i, struct_name);
2165 print_single_struct_recursive(sdna, sdna_struct, element_data, indent + 2, dst);
2166 }
2167}
2168
2169static void print_single_struct_recursive(const SDNA &sdna,
2170 const SDNA_Struct &sdna_struct,
2171 const void *initial_data,
2172 const int indent,
2173 fmt::appender &dst)
2174{
2175 const void *data = initial_data;
2176
2177 for (const int member_i : IndexRange(sdna_struct.members_num)) {
2178 const SDNA_StructMember &member = sdna_struct.members[member_i];
2179 const StringRef member_type_name = sdna.types[member.type_index];
2180 const StringRef member_name = sdna.members[member.member_index];
2181 const eStructMemberCategory member_category = get_struct_member_category(&sdna, &member);
2182 const int member_array_len = sdna.members_array_num[member.member_index];
2183
2184 fmt::format_to(dst, "{:{}}{} {}:", "", indent, member_type_name, member_name);
2185
2186 if (member_category == STRUCT_MEMBER_CATEGORY_PRIMITIVE &&
2187 member.type_index == SDNA_TYPE_CHAR && member_array_len > 1)
2188 {
2189 const char *str_data = static_cast<const char *>(data);
2190 fmt::format_to(dst, " ");
2191 if (char_array_startswith_simple_name(str_data, member_array_len)) {
2192 fmt::format_to(dst, "'{}'", str_data);
2193 }
2194 else {
2195 for (const int i : IndexRange(member_array_len)) {
2196 fmt::format_to(dst, "{} ", int(str_data[i]));
2197 }
2198 }
2199 fmt::format_to(dst, "\n");
2200 }
2201 else {
2202 switch (member_category) {
2204 fmt::format_to(dst, "\n");
2205 const int substruct_i = DNA_struct_find_index_without_alias(&sdna, member_type_name);
2206 const SDNA_Struct &sub_sdna_struct = *sdna.structs[substruct_i];
2208 sdna, sub_sdna_struct, data, member_array_len, indent + 2, dst);
2209 break;
2210 }
2212 fmt::format_to(dst, " ");
2213 const int type_size = sdna.types_size[member.type_index];
2214 const eSDNA_Type type = eSDNA_Type(member.type_index);
2215 for ([[maybe_unused]] const int elem_i : IndexRange(member_array_len)) {
2216 const void *current_data = POINTER_OFFSET(data, elem_i * type_size);
2217 switch (type) {
2218 case SDNA_TYPE_CHAR: {
2219 const char value = *reinterpret_cast<const char *>(current_data);
2220 fmt::format_to(dst, "{}", int(value));
2221 break;
2222 }
2223 case SDNA_TYPE_UCHAR: {
2224 const uchar value = *reinterpret_cast<const uchar *>(current_data);
2225 fmt::format_to(dst, "{}", int(value));
2226 break;
2227 }
2228 case SDNA_TYPE_INT8: {
2229 fmt::format_to(dst, "{}", *reinterpret_cast<const int8_t *>(current_data));
2230 break;
2231 }
2232 case SDNA_TYPE_SHORT: {
2233 fmt::format_to(dst, "{}", *reinterpret_cast<const short *>(current_data));
2234 break;
2235 }
2236 case SDNA_TYPE_USHORT: {
2237 fmt::format_to(dst, "{}", *reinterpret_cast<const ushort *>(current_data));
2238 break;
2239 }
2240 case SDNA_TYPE_INT: {
2241 fmt::format_to(dst, "{}", *reinterpret_cast<const int *>(current_data));
2242 break;
2243 }
2244 case SDNA_TYPE_FLOAT: {
2245 fmt::format_to(dst, "{}", *reinterpret_cast<const float *>(current_data));
2246 break;
2247 }
2248 case SDNA_TYPE_DOUBLE: {
2249 fmt::format_to(dst, "{}", *reinterpret_cast<const double *>(current_data));
2250 break;
2251 }
2252 case SDNA_TYPE_INT64: {
2253 fmt::format_to(dst, "{}", *reinterpret_cast<const int64_t *>(current_data));
2254 break;
2255 }
2256 case SDNA_TYPE_UINT64: {
2257 fmt::format_to(dst, "{}", *reinterpret_cast<const uint64_t *>(current_data));
2258 break;
2259 }
2260 case SDNA_TYPE_VOID:
2261 case SDNA_TYPE_RAW_DATA: {
2263 break;
2264 }
2265 }
2266 fmt::format_to(dst, " ");
2267 }
2268 fmt::format_to(dst, "\n");
2269 break;
2270 }
2272 for ([[maybe_unused]] const int elem_i : IndexRange(member_array_len)) {
2273 const void *current_data = POINTER_OFFSET(data, sdna.pointer_size * elem_i);
2274 fmt::format_to(dst, " {}", *reinterpret_cast<const void *const *>(current_data));
2275 }
2276 fmt::format_to(dst, "\n");
2277 break;
2278 }
2279 }
2280 }
2281 const int member_size = get_member_size_in_bytes(&sdna, &member);
2282 data = POINTER_OFFSET(data, member_size);
2283 }
2284}
2285
2287 const int struct_id,
2288 const void *initial_data,
2289 const void *address,
2290 const int64_t element_num,
2291 std::ostream &stream)
2292{
2293 const SDNA_Struct &sdna_struct = *sdna.structs[struct_id];
2294
2295 fmt::memory_buffer buf;
2296 fmt::appender dst{buf};
2297
2298 const StringRef struct_name = sdna.types[sdna_struct.type_index];
2299 fmt::format_to(dst, "<{}> {}x at {}\n", struct_name, element_num, address);
2300
2301 print_struct_array_recursive(sdna, sdna_struct, initial_data, element_num, 2, dst);
2302 stream << fmt::to_string(buf);
2303}
2304
2305void print_struct_by_id(const int struct_id, const void *data)
2306{
2307 const SDNA &sdna = *DNA_sdna_current_get();
2308 print_structs_at_address(sdna, struct_id, data, data, 1, std::cout);
2309}
2310
2311} // namespace dna
2312
2314
2315} // namespace blender
blenloader genfile private function prototypes
const unsigned char DNAstr[]
const int DNAlen
#define SDNA_RAW_DATA_STRUCT_INDEX
Read Blender memory allocation and freeing.
#define MAKE_ID(a, b, c, d)
Definition blendthumb.hh:50
#define BLI_assert_unreachable()
#define UNUSED_VARS(...)
#define BLI_INLINE
#define ELEM(...)
#define POINTER_OFFSET(v, ofs)
#define BLI_assert_msg(a, msg)
#define BLI_STATIC_ASSERT_ALIGN(st, align)
Array(Allocator allocator={}) noexcept
Definition BLI_array.hh:78
constexpr IndexRange()=default
void * allocate(const int64_t size, const int64_t alignment)
void reserve(const int64_t n)
Definition BLI_set.hh:656
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr Span()=default
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr StringRef()
constexpr bool is_empty() const
constexpr int64_t size() const
void append(const T &value)
void resize(const int64_t new_size)
#define printf
#define str(s)
#define SHRT_MAX
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
#define UINT_MAX
Definition hash_md5.cc:46
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
#define T
static void print_struct_array_recursive(const SDNA &sdna, const SDNA_Struct &sdna_struct, const void *initial_data, const int64_t element_num, const int indent, fmt::appender &dst)
static bool char_array_startswith_simple_name(const char *data, const int array_len)
static void print_single_struct_recursive(const SDNA &sdna, const SDNA_Struct &sdna_struct, const void *initial_data, const int indent, fmt::appender &dst)
void print_struct_by_id(int struct_id, const void *data)
void print_structs_at_address(const SDNA &sdna, int struct_id, const void *data, const void *address, int64_t element_num, std::ostream &stream)
uint32_t struct_size(Span< shader::ShaderCreateInfo::PushConst > push_constants)
BMesh Iterators.
static bool elem_exists_impl(const Span< StringRef > types, const Span< StringRef > names, const StringRef type, const StringRef name, const SDNA_Struct *old)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
struct DNA_ReconstructInfo * DNA_reconstruct_info_create(const struct SDNA *oldsdna, const struct SDNA *newsdna, const char *compare_flags)
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
BLI_STATIC_ASSERT(ARRAY_SIZE(data_mode_strings)==CTX_MODE_NUM+1, "Must have a string for each context mode") const char *CTX_data_mode_string(const bContext *C)
int DNA_struct_member_offset_by_name_without_alias(const struct SDNA *sdna, StringRef stype, StringRef vartype, StringRef name)
static const StringRef sdna_data_pointer_read_string(const char *&data, const void *const data_pointer_end, const char **r_error_message)
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static int dna_struct_find_index_ex_impl(const Span< StringRef > types, const int, const Span< SDNA_Struct * > structs, const StringRef str, uint *struct_index_last)
bool DNA_sdna_patch_struct_member_by_name(struct SDNA *sdna, StringRef type_name, StringRef old_member_name, StringRef new_member_name)
static int elem_offset_without_alias(const SDNA *sdna, const StringRef type, const StringRef name, const SDNA_Struct *old)
static bool elem_exists_with_alias(const SDNA *sdna, const StringRef type, const StringRef name, const SDNA_Struct *old)
bool DNA_member_id_match(const StringRef member_id, const StringRef member_full, uint *r_member_full_offset)
Definition dna_utils.cc:102
unsigned short ushort
Definition BKE_text.h:56
static void reconstruct_struct(const DNA_ReconstructInfo *reconstruct_info, const int new_struct_index, const char *old_block, char *new_block)
BLI_INLINE const char * pad_up_4(const char *ptr)
bool DNA_sdna_patch_struct_by_name(struct SDNA *sdna, StringRef old_type_name, StringRef new_type_name)
void DNA_reconstruct_info_free(struct DNA_ReconstructInfo *reconstruct_info)
eSDNA_StructCompare
Definition DNA_genfile.h:61
@ SDNA_CMP_EQUAL
Definition DNA_genfile.h:66
@ SDNA_CMP_NOT_EQUAL
Definition DNA_genfile.h:69
@ SDNA_CMP_UNKNOWN
Definition DNA_genfile.h:71
@ SDNA_CMP_REMOVED
Definition DNA_genfile.h:64
MatBase< float, 4, 4 > float4x4
void * DNA_struct_reconstruct(const struct DNA_ReconstructInfo *reconstruct_info, int old_struct_index, int blocks, const void *old_blocks, const char *alloc_name, int64_t *r_alloc_size=nullptr)
static int elem_offset_impl(const SDNA *sdna, const Span< StringRef > types, const Span< StringRef > names, const StringRef type, const StringRef name, const SDNA_Struct *old)
int DNA_struct_member_size(const struct SDNA *sdna, short type, short member_index)
uint DNA_member_id_offset_start(const StringRef member_full)
Definition dna_utils.cc:71
static bool init_structDNA(SDNA *sdna, const char **r_error_message)
int DNA_elem_type_size(eSDNA_Type elem_nr)
static void print_reconstruct_step(const ReconstructStep *step, const SDNA *oldsdna, const SDNA *newsdna)
bool DNA_struct_exists_without_alias(const struct SDNA *sdna, StringRef stype)
StringRef DNA_member_id_rename(LinearAllocator<> &mem_arena, const StringRef member_id_src, const StringRef member_id_dst, const StringRef member_full_src, const uint member_full_src_offset_len)
Definition dna_utils.cc:118
static void cast_primitive_type(const eSDNA_Type old_type, const eSDNA_Type new_type, const int array_len, const char *old_data, char *new_data)
StringRef DNA_struct_identifier(struct SDNA *sdna, int struct_index)
static int get_member_size_in_bytes(const SDNA *sdna, const SDNA_StructMember *member)
unsigned int uint32_t
Definition BKE_text.h:81
@ STRUCT_MEMBER_CATEGORY_POINTER
@ STRUCT_MEMBER_CATEGORY_PRIMITIVE
@ STRUCT_MEMBER_CATEGORY_STRUCT
int DNA_struct_find_index_without_alias_ex(const struct SDNA *sdna, StringRef str, unsigned int *struct_index_last)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
static void reconstruct_structs(const DNA_ReconstructInfo *reconstruct_info, const int blocks, const int old_struct_index, const int new_struct_index, const char *old_blocks, char *new_blocks)
unsigned char uchar
Definition BKE_text.h:58
DnaRenameMaps DNA_rename_maps_static_to_alias()
Definition dna_utils.cc:197
static eStructMemberCategory get_struct_member_category(const SDNA *sdna, const SDNA_StructMember *member)
const char * DNA_struct_get_compareflags(const struct SDNA *oldsdna, const struct SDNA *newsdna)
BMesh const char void * data
StringRef DNA_member_id_string_ref(const StringRef member_full)
Definition dna_utils.cc:95
static bool sdna_data_validate_array_size(const int64_t size, T data_pointer_current, const void *const data_pointer_end, const char **r_error_message)
static bool DNA_sdna_patch_struct_member(SDNA *sdna, const int struct_index, const StringRef old_member_name, const StringRef new_member_name)
StringRef DNA_struct_rename_legacy_hack_static_from_alias(const StringRef name)
Definition dna_utils.cc:228
static bool ispointer(const StringRef name)
static void sdna_expand_names(SDNA *sdna)
bool DNA_struct_member_exists_without_alias(const struct SDNA *sdna, StringRef stype, StringRef vartype, StringRef name)
static bool DNA_sdna_patch_struct(SDNA *sdna, const int struct_index, const StringRef new_type_name)
ATTR_WARN_UNUSED_RESULT const size_t num
int DNA_struct_size(const struct SDNA *sdna, int struct_index)
_W64 unsigned int uintptr_t
Definition BKE_text.h:120
void DNA_sdna_alias_data_ensure_structs_map(struct SDNA *sdna)
static void set_compare_flags_for_struct(const SDNA *oldsdna, const SDNA *newsdna, char *compare_flags, const int old_struct_index)
int DNA_struct_find_with_alias(const struct SDNA *sdna, StringRef str)
static void cast_pointer_64_to_32(const int array_len, const uint64_t *old_data, uint32_t *new_data)
static bool elem_exists_without_alias(const SDNA *sdna, const StringRef type, const StringRef name, const SDNA_Struct *old)
int DNA_struct_find_index_without_alias(const struct SDNA *sdna, StringRef str)
int DNA_member_array_num(const StringRef str)
Definition dna_utils.cc:28
StringRef DNA_struct_rename_legacy_hack_alias_from_static(const StringRef name)
Definition dna_utils.cc:244
bool DNA_struct_member_exists_with_alias(const struct SDNA *sdna, StringRef stype, StringRef vartype, StringRef name)
int DNA_struct_alignment(const struct SDNA *sdna, int struct_index)
static void cast_pointer_32_to_64(const int array_len, const uint32_t *old_data, uint64_t *new_data)
@ SDNA_TYPE_INT64
Definition DNA_genfile.h:45
@ SDNA_TYPE_SHORT
Definition DNA_genfile.h:38
@ SDNA_TYPE_UINT64
Definition DNA_genfile.h:46
@ SDNA_TYPE_FLOAT
Definition DNA_genfile.h:43
@ SDNA_TYPE_RAW_DATA
Definition DNA_genfile.h:55
@ SDNA_TYPE_USHORT
Definition DNA_genfile.h:39
@ SDNA_TYPE_UCHAR
Definition DNA_genfile.h:37
@ SDNA_TYPE_INT
Definition DNA_genfile.h:40
@ SDNA_TYPE_CHAR
Definition DNA_genfile.h:36
@ SDNA_TYPE_DOUBLE
Definition DNA_genfile.h:44
@ SDNA_TYPE_VOID
Definition DNA_genfile.h:47
@ SDNA_TYPE_INT8
Definition DNA_genfile.h:48
void DNA_sdna_alias_data_ensure(struct SDNA *sdna)
const SDNA * DNA_sdna_current_get()
void *(*) MEM_new_array_zeroed_aligned(size_t len, size_t size, size_t alignment, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
int DNA_struct_member_offset_by_name_with_alias(const struct SDNA *sdna, StringRef stype, StringRef vartype, StringRef name)
int DNA_struct_find_index_with_alias_ex(const struct SDNA *sdna, StringRef str, unsigned int *struct_index_last)
static ReconstructStep * create_reconstruct_steps_for_struct(const SDNA *oldsdna, const SDNA *newsdna, const char *compare_flags, const SDNA_Struct *old_struct, const SDNA_Struct *new_struct)
static bool sdna_struct_size_check(const SDNA *sdna, const SDNA_Struct *struct_info, const int64_t voided_members_size, const char **r_error_message)
@ RECONSTRUCT_STEP_CAST_POINTER_TO_32
@ RECONSTRUCT_STEP_SUBSTRUCT
@ RECONSTRUCT_STEP_MEMCPY
@ RECONSTRUCT_STEP_INIT_ZERO
@ RECONSTRUCT_STEP_CAST_POINTER_TO_64
@ RECONSTRUCT_STEP_CAST_PRIMITIVE
static T sdna_data_pointer_read(T &data, const int64_t num, const void *const data_pointer_end, const char **r_error_message)
static void init_reconstruct_step_for_member(const SDNA *oldsdna, const SDNA *newsdna, const char *compare_flags, const SDNA_Struct *old_struct, const SDNA_StructMember *new_member, const int new_member_offset, ReconstructStep *r_step)
bool DNA_struct_exists_with_alias(const struct SDNA *sdna, StringRef str)
static const SDNA_StructMember * find_member_with_matching_name(const SDNA *sdna, const SDNA_Struct *struct_info, const StringRef name, int *r_offset)
static bool elem_streq(const StringRef name, const StringRef oname)
signed char int8_t
Definition BKE_text.h:76
static int compress_reconstruct_steps(ReconstructStep *steps, const int old_step_count)
std::unique_ptr< SDNA > DNA_sdna_from_data(const void *data, int data_len, bool data_alloc, bool do_alias, const char **r_error_message)
static int elem_offset_with_alias(const SDNA *sdna, const StringRef type, const StringRef name, const SDNA_Struct *old)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static StringRef dna_sdna_alias_from_static_elem_full(SDNA *sdna, const DnaRenameMaps &rename_maps, const StringRef struct_name_static, const StringRef elem_static_full)
Map< std::pair< StringRef, StringRef >, StringRef > members
Definition dna_utils.h:76
Map< StringRef, StringRef > types
Definition dna_utils.h:74
union blender::ReconstructStep::@370104264300151155006146011007205111233044333126 data
eReconstructStepType type
SDNA_StructMember members[]
Array< SDNA_Struct *, 0 > structs
Array< StringRef > types
Vector< short > members_array_num
LinearAllocator mem_arena
struct blender::SDNA::@276375047207276123200247134171113130103131113323 alias
Map< StringRef, int > types_to_structs_map
Array< int > types_alignment
const char * data
Vector< StringRef > members
bool has_pointer_at_offset(int64_t offset) const
void resize(int new_size)
Definition vector.hh:94
struct blender::ReconstructStep::@370104264300151155006146011007205111233044333126::@056216020370063175064236300326054315042333105364 substruct
struct blender::ReconstructStep::@370104264300151155006146011007205111233044333126::@375215207215274014330373137311365227171066213356 memcpy
struct blender::ReconstructStep::@370104264300151155006146011007205111233044333126::@254275337223066047364002100163372131176351135173 cast_primitive
struct blender::ReconstructStep::@370104264300151155006146011007205111233044333126::@015052325033005042010174340106001303127333001106 cast_pointer