From 97982360dee29fe8a8852a4c36ffa3319239fc1f Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Mon, 29 Jul 2024 17:23:42 +0200 Subject: [PATCH] Refactor: DNA: Add util to access size of a truct. Avoids reafile code having to access 'internals' specifics of DNA data. --- source/blender/blenloader/intern/readfile.cc | 3 ++- source/blender/blenloader/intern/writefile.cc | 4 +--- source/blender/makesdna/DNA_genfile.h | 7 +++++++ source/blender/makesdna/intern/dna_genfile.cc | 5 +++++ 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/source/blender/blenloader/intern/readfile.cc b/source/blender/blenloader/intern/readfile.cc index e287fc22dde..d06d7744825 100644 --- a/source/blender/blenloader/intern/readfile.cc +++ b/source/blender/blenloader/intern/readfile.cc @@ -1697,7 +1697,8 @@ static void switch_endian_structs(const SDNA *filesdna, BHead *bhead) char *data; data = (char *)(bhead + 1); - blocksize = filesdna->types_size[filesdna->structs[bhead->SDNAnr]->type]; + + blocksize = DNA_struct_size(filesdna, bhead->SDNAnr); nblocks = bhead->nr; while (nblocks--) { diff --git a/source/blender/blenloader/intern/writefile.cc b/source/blender/blenloader/intern/writefile.cc index 55a3d568221..db5c68e4ba6 100644 --- a/source/blender/blenloader/intern/writefile.cc +++ b/source/blender/blenloader/intern/writefile.cc @@ -738,9 +738,7 @@ static void writestruct_at_address_nr( bh.nr = nr; bh.SDNAnr = struct_nr; - const SDNA_Struct *struct_info = wd->sdna->structs[bh.SDNAnr]; - - bh.len = nr * wd->sdna->types_size[struct_info->type]; + bh.len = nr * DNA_struct_size(wd->sdna, bh.SDNAnr); if (bh.len == 0) { return; diff --git a/source/blender/makesdna/DNA_genfile.h b/source/blender/makesdna/DNA_genfile.h index 7901df51b87..7e9e23c77d7 100644 --- a/source/blender/makesdna/DNA_genfile.h +++ b/source/blender/makesdna/DNA_genfile.h @@ -192,6 +192,13 @@ int DNA_struct_member_size(const struct SDNA *sdna, short type, short name); */ int DNA_elem_type_size(eSDNA_Type elem_nr); +/** + * Returns the size of a struct. + * + * \param struct_index: Index into the #sdna.structs array (aka #BHead.SDNAnr). + */ +int DNA_struct_size(const struct SDNA *sdna, int struct_index); + /** * Get the alignment that should be used when allocating memory for this type. */ diff --git a/source/blender/makesdna/intern/dna_genfile.cc b/source/blender/makesdna/intern/dna_genfile.cc index b961dbe4e51..b02ac78341e 100644 --- a/source/blender/makesdna/intern/dna_genfile.cc +++ b/source/blender/makesdna/intern/dna_genfile.cc @@ -156,6 +156,11 @@ void DNA_sdna_free(SDNA *sdna) MEM_freeN(sdna); } +int DNA_struct_size(const SDNA *sdna, int struct_index) +{ + return sdna->types_size[sdna->structs[struct_index]->type]; +} + /** * Return true if the name indicates a pointer of some kind. */