From eb141f1608e6fd4b498e304f6bbf93f2afabc172 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 25 Sep 2023 12:25:32 +1000 Subject: [PATCH] DNA: differentiate with/without alias versions of SDNA lookup functions Use with_alias / without_alias suffix for functions so it's clear which is intended (no functional changes). Add macros for versioning checks to avoid noisy changes, currently they use the non-aliased versions of these functions but should eventually be moved to use the aliased versions because it's confusing to use the original names when these should only need to be referenced from `dna_rename_defs.h`. --- .../blenloader/intern/readblenentry.cc | 6 +-- source/blender/blenloader/intern/readfile.cc | 4 +- .../blenloader/intern/versioning_260.cc | 5 ++ .../blenloader/intern/versioning_270.cc | 5 ++ .../blenloader/intern/versioning_280.cc | 5 ++ .../blenloader/intern/versioning_290.cc | 5 ++ .../blenloader/intern/versioning_300.cc | 5 ++ .../blenloader/intern/versioning_400.cc | 5 ++ source/blender/blenloader/intern/writefile.cc | 3 +- source/blender/makesdna/DNA_genfile.h | 35 +++++++----- source/blender/makesdna/intern/dna_genfile.cc | 54 +++++++++---------- source/blender/makesrna/intern/rna_define.cc | 2 +- 12 files changed, 88 insertions(+), 46 deletions(-) diff --git a/source/blender/blenloader/intern/readblenentry.cc b/source/blender/blenloader/intern/readblenentry.cc index fcc30bdce79..0a28f2f5316 100644 --- a/source/blender/blenloader/intern/readblenentry.cc +++ b/source/blender/blenloader/intern/readblenentry.cc @@ -158,7 +158,7 @@ LinkNode *BLO_blendhandle_get_datablock_info(BlendHandle *bh, BHead *bhead; int tot = 0; - const int sdna_nr_preview_image = DNA_struct_find(fd->filesdna, "PreviewImage"); + const int sdna_nr_preview_image = DNA_struct_find_without_alias(fd->filesdna, "PreviewImage"); for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) { if (bhead->code == BLO_CODE_ENDB) { @@ -261,7 +261,7 @@ PreviewImage *BLO_blendhandle_get_preview_for_id(BlendHandle *bh, { FileData *fd = (FileData *)bh; bool looking = false; - const int sdna_preview_image = DNA_struct_find(fd->filesdna, "PreviewImage"); + const int sdna_preview_image = DNA_struct_find_without_alias(fd->filesdna, "PreviewImage"); for (BHead *bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) { if (bhead->code == BLO_CODE_DATA) { @@ -331,7 +331,7 @@ LinkNode *BLO_blendhandle_get_previews(BlendHandle *bh, int ofblocktype, int *r_ } else if (bhead->code == BLO_CODE_DATA) { if (looking) { - if (bhead->SDNAnr == DNA_struct_find(fd->filesdna, "PreviewImage")) { + if (bhead->SDNAnr == DNA_struct_find_without_alias(fd->filesdna, "PreviewImage")) { prv = static_cast(BLO_library_read_struct(fd, bhead, "PreviewImage")); if (prv) { diff --git a/source/blender/blenloader/intern/readfile.cc b/source/blender/blenloader/intern/readfile.cc index 78fbf8bf0e9..52569a0e92f 100644 --- a/source/blender/blenloader/intern/readfile.cc +++ b/source/blender/blenloader/intern/readfile.cc @@ -995,10 +995,10 @@ static bool read_file_dna(FileData *fd, const char **r_error_message) fd->reconstruct_info = DNA_reconstruct_info_create( fd->filesdna, fd->memsdna, fd->compflags); /* used to retrieve ID names from (bhead+1) */ - fd->id_name_offset = DNA_struct_member_offset_by_name( + fd->id_name_offset = DNA_struct_member_offset_by_name_without_alias( fd->filesdna, "ID", "char", "name[]"); BLI_assert(fd->id_name_offset != -1); - fd->id_asset_data_offset = DNA_struct_member_offset_by_name( + fd->id_asset_data_offset = DNA_struct_member_offset_by_name_without_alias( fd->filesdna, "ID", "AssetMetaData", "*asset_data"); return true; diff --git a/source/blender/blenloader/intern/versioning_260.cc b/source/blender/blenloader/intern/versioning_260.cc index a5ca615c98d..cbdce6ae685 100644 --- a/source/blender/blenloader/intern/versioning_260.cc +++ b/source/blender/blenloader/intern/versioning_260.cc @@ -12,6 +12,9 @@ /* allow readfile to use deprecated functionality */ #define DNA_DEPRECATED_ALLOW +/* Define macros in `DNA_genfile.h`. */ +#define DNA_GENFILE_VERSIONING_MACROS + #include "DNA_anim_types.h" #include "DNA_brush_types.h" #include "DNA_camera_types.h" @@ -36,6 +39,8 @@ #include "DNA_view3d_types.h" #include "DNA_world_types.h" +#undef DNA_GENFILE_VERSIONING_MACROS + #include "MEM_guardedalloc.h" #include "BLI_blenlib.h" diff --git a/source/blender/blenloader/intern/versioning_270.cc b/source/blender/blenloader/intern/versioning_270.cc index 3bf4bd57bcb..8c1c6d17bc3 100644 --- a/source/blender/blenloader/intern/versioning_270.cc +++ b/source/blender/blenloader/intern/versioning_270.cc @@ -15,6 +15,9 @@ /* allow readfile to use deprecated functionality */ #define DNA_DEPRECATED_ALLOW +/* Define macros in `DNA_genfile.h`. */ +#define DNA_GENFILE_VERSIONING_MACROS + #include "DNA_anim_types.h" #include "DNA_armature_types.h" #include "DNA_brush_types.h" @@ -41,6 +44,8 @@ #include "DNA_genfile.h" +#undef DNA_GENFILE_VERSIONING_MACROS + #include "BKE_anim_data.h" #include "BKE_animsys.h" #include "BKE_colortools.h" diff --git a/source/blender/blenloader/intern/versioning_280.cc b/source/blender/blenloader/intern/versioning_280.cc index ad51f9ecfc3..185d5b3ed20 100644 --- a/source/blender/blenloader/intern/versioning_280.cc +++ b/source/blender/blenloader/intern/versioning_280.cc @@ -18,6 +18,9 @@ #include "BLI_string.h" #include "BLI_utildefines.h" +/* Define macros in `DNA_genfile.h`. */ +#define DNA_GENFILE_VERSIONING_MACROS + #include "DNA_defaults.h" #include "DNA_anim_types.h" @@ -54,6 +57,8 @@ #include "DNA_workspace_types.h" #include "DNA_world_types.h" +#undef DNA_GENFILE_VERSIONING_MACROS + #include "BKE_animsys.h" #include "BKE_blender.h" #include "BKE_brush.hh" diff --git a/source/blender/blenloader/intern/versioning_290.cc b/source/blender/blenloader/intern/versioning_290.cc index 061ab8e7b5f..537888a7bdd 100644 --- a/source/blender/blenloader/intern/versioning_290.cc +++ b/source/blender/blenloader/intern/versioning_290.cc @@ -14,6 +14,9 @@ #include "BLI_string.h" #include "BLI_utildefines.h" +/* Define macros in `DNA_genfile.h`. */ +#define DNA_GENFILE_VERSIONING_MACROS + #include "DNA_anim_types.h" #include "DNA_armature_types.h" #include "DNA_brush_types.h" @@ -40,6 +43,8 @@ #include "DNA_tracking_types.h" #include "DNA_workspace_types.h" +#undef DNA_GENFILE_VERSIONING_MACROS + #include "BKE_animsys.h" #include "BKE_armature.h" #include "BKE_attribute.h" diff --git a/source/blender/blenloader/intern/versioning_300.cc b/source/blender/blenloader/intern/versioning_300.cc index 2a926c184ed..5824cb74fb1 100644 --- a/source/blender/blenloader/intern/versioning_300.cc +++ b/source/blender/blenloader/intern/versioning_300.cc @@ -24,6 +24,9 @@ #include "BLI_string_utils.h" #include "BLI_utildefines.h" +/* Define macros in `DNA_genfile.h`. */ +#define DNA_GENFILE_VERSIONING_MACROS + #include "DNA_anim_types.h" #include "DNA_armature_types.h" #include "DNA_brush_types.h" @@ -47,6 +50,8 @@ #include "DNA_tracking_types.h" #include "DNA_workspace_types.h" +#undef DNA_GENFILE_VERSIONING_MACROS + #include "BKE_action.h" #include "BKE_anim_data.h" #include "BKE_animsys.h" diff --git a/source/blender/blenloader/intern/versioning_400.cc b/source/blender/blenloader/intern/versioning_400.cc index c39367bcb64..86d678fb9fa 100644 --- a/source/blender/blenloader/intern/versioning_400.cc +++ b/source/blender/blenloader/intern/versioning_400.cc @@ -12,6 +12,9 @@ #include "CLG_log.h" +/* Define macros in `DNA_genfile.h`. */ +#define DNA_GENFILE_VERSIONING_MACROS + #include "DNA_brush_types.h" #include "DNA_camera_types.h" #include "DNA_light_types.h" @@ -25,6 +28,8 @@ #include "DNA_genfile.h" #include "DNA_particle_types.h" +#undef DNA_GENFILE_VERSIONING_MACROS + #include "BLI_assert.h" #include "BLI_listbase.h" #include "BLI_map.hh" diff --git a/source/blender/blenloader/intern/writefile.cc b/source/blender/blenloader/intern/writefile.cc index 39efc4128e2..d332d3eab8d 100644 --- a/source/blender/blenloader/intern/writefile.cc +++ b/source/blender/blenloader/intern/writefile.cc @@ -1745,7 +1745,8 @@ void blo_write_id_struct(BlendWriter *writer, int struct_id, const void *id_addr int BLO_get_struct_id_by_name(BlendWriter *writer, const char *struct_name) { - int struct_id = DNA_struct_find(writer->wd->sdna, struct_name); + /* TODO(@ideasman42): use the "alias" version. */ + int struct_id = DNA_struct_find_without_alias(writer->wd->sdna, struct_name); return struct_id; } diff --git a/source/blender/makesdna/DNA_genfile.h b/source/blender/makesdna/DNA_genfile.h index 802131555e4..3276be5ab0f 100644 --- a/source/blender/makesdna/DNA_genfile.h +++ b/source/blender/makesdna/DNA_genfile.h @@ -97,8 +97,10 @@ void DNA_reconstruct_info_free(struct DNA_ReconstructInfo *reconstruct_info); /** * Returns the index of the struct info for the struct with the specified name. */ -int DNA_struct_find_ex(const struct SDNA *sdna, const char *str, unsigned int *index_last); -int DNA_struct_find(const struct SDNA *sdna, const char *str); +int DNA_struct_find_without_alias_ex(const struct SDNA *sdna, + const char *str, + unsigned int *index_last); +int DNA_struct_find_without_alias(const struct SDNA *sdna, const char *str); /** * Does endian swapping on the fields of a struct value. * @@ -128,10 +130,10 @@ void *DNA_struct_reconstruct(const struct DNA_ReconstructInfo *reconstruct_info, * Returns the offset of the field with the specified name and type within the specified * struct type in #SDNA, -1 on failure. */ -int DNA_struct_member_offset_by_name(const struct SDNA *sdna, - const char *stype, - const char *vartype, - const char *name); +int DNA_struct_member_offset_by_name_without_alias(const struct SDNA *sdna, + const char *stype, + const char *vartype, + const char *name); int DNA_struct_member_offset_by_name_with_alias(const struct SDNA *sdna, const char *stype, const char *vartype, @@ -146,11 +148,11 @@ int DNA_struct_member_offset_by_name_with_alias(const struct SDNA *sdna, */ int DNA_struct_member_size(const struct SDNA *sdna, short type, short name); -bool DNA_struct_exists(const struct SDNA *sdna, const char *stype); -bool DNA_struct_member_exists(const struct SDNA *sdna, - const char *stype, - const char *vartype, - const char *name); +bool DNA_struct_exists_without_alias(const struct SDNA *sdna, const char *stype); +bool DNA_struct_member_exists_without_alias(const struct SDNA *sdna, + const char *stype, + const char *vartype, + const char *name); /** * Returns the size in bytes of a primitive type. @@ -200,10 +202,19 @@ bool DNA_struct_member_exists_with_alias(const struct SDNA *sdna, const char *name); /** * Separated from #DNA_sdna_alias_data_ensure because it's not needed - * unless we want to lookup aliased struct names (#DNA_struct_alias_find and friends). + * unless we want to lookup aliased struct names (#DNA_struct_find_with_alias and friends). */ void DNA_sdna_alias_data_ensure_structs_map(struct SDNA *sdna); +/* For versioning, avoid verbosity selecting between with/without alias versions of functions. */ +#ifdef DNA_GENFILE_VERSIONING_MACROS +# define DNA_struct_exists(sdna, str) DNA_struct_exists_without_alias(sdna, str) +# define DNA_struct_member_exists(sdna, stype, vartype, name) \ + DNA_struct_member_exists_without_alias(sdna, stype, vartype, name) +# define DNA_struct_find(sdna, str) DNA_struct_find_without_alias(sdna, str) +# define DNA_struct_find_ex(sdna, str, n) DNA_struct_find_without_alias_ex(sdna, str) +#endif + #ifdef __cplusplus } #endif diff --git a/source/blender/makesdna/intern/dna_genfile.cc b/source/blender/makesdna/intern/dna_genfile.cc index af40caa4173..0ea22323d0d 100644 --- a/source/blender/makesdna/intern/dna_genfile.cc +++ b/source/blender/makesdna/intern/dna_genfile.cc @@ -241,7 +241,7 @@ static int dna_struct_find_nr_ex_impl( return -1; } -int DNA_struct_find_ex(const SDNA *sdna, const char *str, uint *index_last) +int DNA_struct_find_without_alias_ex(const SDNA *sdna, const char *str, uint *index_last) { return dna_struct_find_nr_ex_impl( /* Expand SDNA. */ @@ -276,10 +276,10 @@ int DNA_struct_find_with_alias_ex(const SDNA *sdna, const char *str, uint *index index_last); } -int DNA_struct_find(const SDNA *sdna, const char *str) +int DNA_struct_find_without_alias(const SDNA *sdna, const char *str) { uint index_last_dummy = UINT_MAX; - return DNA_struct_find_ex(sdna, str, &index_last_dummy); + return DNA_struct_find_without_alias_ex(sdna, str, &index_last_dummy); } int DNA_struct_find_with_alias(const SDNA *sdna, const char *str) @@ -490,7 +490,7 @@ static bool init_structDNA(SDNA *sdna, bool do_endian_swap, const char **r_error /* Calculate 'sdna->pointer_size' */ { - const int nr = DNA_struct_find(sdna, "ListBase"); + const int nr = DNA_struct_find_without_alias(sdna, "ListBase"); /* should never happen, only with corrupt file for example */ if (UNLIKELY(nr == -1)) { @@ -604,7 +604,7 @@ static void set_compare_flags_for_struct(const SDNA *oldsdna, SDNA_Struct *old_struct = oldsdna->structs[old_struct_index]; const char *struct_name = oldsdna->types[old_struct->type]; - const int new_struct_index = DNA_struct_find(newsdna, struct_name); + const int new_struct_index = DNA_struct_find_without_alias(newsdna, struct_name); if (new_struct_index == -1) { /* Didn't find a matching new struct, so it has been removed. */ compare_flags[old_struct_index] = SDNA_CMP_REMOVED; @@ -653,7 +653,7 @@ static void set_compare_flags_for_struct(const SDNA *oldsdna, } } else { - const int old_member_struct_index = DNA_struct_find(oldsdna, old_type_name); + const int old_member_struct_index = DNA_struct_find_without_alias(oldsdna, old_type_name); if (old_member_struct_index >= 0) { set_compare_flags_for_struct(oldsdna, newsdna, compare_flags, old_member_struct_index); if (compare_flags[old_member_struct_index] != SDNA_CMP_EQUAL) { @@ -1019,7 +1019,7 @@ static eStructMemberCategory get_struct_member_category(const SDNA *sdna, return STRUCT_MEMBER_CATEGORY_POINTER; } const char *member_type_name = sdna->types[member->type]; - if (DNA_struct_exists(sdna, member_type_name)) { + if (DNA_struct_exists_without_alias(sdna, member_type_name)) { return STRUCT_MEMBER_CATEGORY_STRUCT; } return STRUCT_MEMBER_CATEGORY_PRIMITIVE; @@ -1055,7 +1055,7 @@ void DNA_struct_switch_endian(const SDNA *sdna, int struct_nr, char *data) switch (member_category) { case STRUCT_MEMBER_CATEGORY_STRUCT: { const int substruct_size = sdna->types_size[member->type]; - const int substruct_nr = DNA_struct_find(sdna, member_type_name); + const int substruct_nr = DNA_struct_find_without_alias(sdna, member_type_name); BLI_assert(substruct_nr != -1); for (int a = 0; a < member_array_length; a++) { DNA_struct_switch_endian(sdna, substruct_nr, member_data + a * substruct_size); @@ -1251,7 +1251,7 @@ void *DNA_struct_reconstruct(const DNA_ReconstructInfo *reconstruct_info, const SDNA_Struct *old_struct = oldsdna->structs[old_struct_nr]; const char *type_name = oldsdna->types[old_struct->type]; - const int new_struct_nr = DNA_struct_find(newsdna, type_name); + const int new_struct_nr = DNA_struct_find_without_alias(newsdna, type_name); if (new_struct_nr == -1) { return nullptr; @@ -1332,7 +1332,7 @@ static void init_reconstruct_step_for_member(const SDNA *oldsdna, switch (new_category) { case STRUCT_MEMBER_CATEGORY_STRUCT: { if (STREQ(new_type_name, old_type_name)) { - const int old_struct_nr = DNA_struct_find(oldsdna, old_type_name); + const int old_struct_nr = DNA_struct_find_without_alias(oldsdna, old_type_name); BLI_assert(old_struct_nr != -1); enum eSDNA_StructCompare compare_flag = eSDNA_StructCompare(compare_flags[old_struct_nr]); BLI_assert(compare_flag != SDNA_CMP_REMOVED); @@ -1344,7 +1344,7 @@ static void init_reconstruct_step_for_member(const SDNA *oldsdna, r_step->data.memcpy.size = newsdna->types_size[new_member->type] * shared_array_length; } else { - const int new_struct_nr = DNA_struct_find(newsdna, new_type_name); + const int new_struct_nr = DNA_struct_find_without_alias(newsdna, new_type_name); BLI_assert(new_struct_nr != -1); /* The old and new members are different, use recursion to reconstruct the @@ -1563,7 +1563,7 @@ DNA_ReconstructInfo *DNA_reconstruct_info_create(const SDNA *oldsdna, for (int new_struct_nr = 0; new_struct_nr < newsdna->structs_len; new_struct_nr++) { const SDNA_Struct *new_struct = newsdna->structs[new_struct_nr]; const char *new_struct_name = newsdna->types[new_struct->type]; - const int old_struct_nr = DNA_struct_find(oldsdna, new_struct_name); + const int old_struct_nr = DNA_struct_find_without_alias(oldsdna, new_struct_name); if (old_struct_nr < 0) { reconstruct_info->steps[new_struct_nr] = nullptr; reconstruct_info->step_counts[new_struct_nr] = 0; @@ -1605,12 +1605,12 @@ void DNA_reconstruct_info_free(DNA_ReconstructInfo *reconstruct_info) MEM_freeN(reconstruct_info); } -int DNA_struct_member_offset_by_name(const SDNA *sdna, - const char *stype, - const char *vartype, - const char *name) +int DNA_struct_member_offset_by_name_without_alias(const SDNA *sdna, + const char *stype, + const char *vartype, + const char *name) { - const int SDNAnr = DNA_struct_find(sdna, stype); + const int SDNAnr = DNA_struct_find_without_alias(sdna, stype); BLI_assert(SDNAnr != -1); const SDNA_Struct *const spo = sdna->structs[SDNAnr]; return elem_offset_without_alias(sdna, vartype, name, spo); @@ -1627,17 +1627,17 @@ int DNA_struct_member_offset_by_name_with_alias(const SDNA *sdna, return elem_offset_with_alias(sdna, vartype, name, spo); } -bool DNA_struct_exists(const SDNA *sdna, const char *stype) +bool DNA_struct_exists_without_alias(const SDNA *sdna, const char *stype) { - return DNA_struct_find(sdna, stype) != -1; + return DNA_struct_find_without_alias(sdna, stype) != -1; } -bool DNA_struct_member_exists(const SDNA *sdna, - const char *stype, - const char *vartype, - const char *name) +bool DNA_struct_member_exists_without_alias(const SDNA *sdna, + const char *stype, + const char *vartype, + const char *name) { - const int SDNAnr = DNA_struct_find(sdna, stype); + const int SDNAnr = DNA_struct_find_without_alias(sdna, stype); if (SDNAnr != -1) { const SDNA_Struct *const spo = sdna->structs[SDNAnr]; @@ -1700,7 +1700,7 @@ static bool DNA_sdna_patch_struct(SDNA *sdna, const int struct_name_old_nr, const char *struct_name_new) { - BLI_assert(DNA_struct_find(DNA_sdna_current_get(), struct_name_new) != -1); + BLI_assert(DNA_struct_find_without_alias(DNA_sdna_current_get(), struct_name_new) != -1); const SDNA_Struct *struct_info = sdna->structs[struct_name_old_nr]; #ifdef WITH_DNA_GHASH BLI_ghash_remove(sdna->structs_map, (void *)sdna->types[struct_info->type], nullptr, nullptr); @@ -1714,7 +1714,7 @@ bool DNA_sdna_patch_struct_by_name(SDNA *sdna, const char *struct_name_old, const char *struct_name_new) { - const int struct_name_old_nr = DNA_struct_find(sdna, struct_name_old); + const int struct_name_old_nr = DNA_struct_find_without_alias(sdna, struct_name_old); if (struct_name_old_nr != -1) { return DNA_sdna_patch_struct(sdna, struct_name_old_nr, struct_name_new); } @@ -1775,7 +1775,7 @@ bool DNA_sdna_patch_struct_member_by_name(SDNA *sdna, const char *elem_old, const char *elem_new) { - const int struct_name_nr = DNA_struct_find(sdna, struct_name); + const int struct_name_nr = DNA_struct_find_without_alias(sdna, struct_name); if (struct_name_nr != -1) { return DNA_sdna_patch_struct_member(sdna, struct_name_nr, elem_old, elem_new); } diff --git a/source/blender/makesrna/intern/rna_define.cc b/source/blender/makesrna/intern/rna_define.cc index b0ee863ed2d..03e5058c070 100644 --- a/source/blender/makesrna/intern/rna_define.cc +++ b/source/blender/makesrna/intern/rna_define.cc @@ -212,7 +212,7 @@ static int DNA_struct_find_nr_wrapper(const SDNA *sdna, const char *struct_name) struct_name = static_cast(BLI_ghash_lookup_default( g_version_data.struct_map_static_from_alias, struct_name, (void *)struct_name)); #endif - return DNA_struct_find(sdna, struct_name); + return DNA_struct_find_without_alias(sdna, struct_name); } StructDefRNA *rna_find_struct_def(StructRNA *srna)