[Bf-blender-cvs] [783f3376eeb] temp-dna-rename: Reviewers: brecht mont29 sergey dflinto
Campbell Barton
noreply at git.blender.org
Tue Feb 12 13:53:33 CET 2019
Commit: 783f3376eebecd25a8aef6a3697643166ff72162
Author: Campbell Barton
Date: Tue Feb 12 11:43:33 2019 +1100
Branches: temp-dna-rename
https://developer.blender.org/rB783f3376eebecd25a8aef6a3697643166ff72162
Reviewers: brecht mont29 sergey dflinto
Differential Revision: https://developer.blender.org/D4342
===================================================================
M source/blender/blenloader/intern/versioning_dna.c
M source/blender/makesdna/DNA_genfile.h
M source/blender/makesdna/intern/CMakeLists.txt
M source/blender/makesdna/intern/dna_genfile.c
M source/blender/makesdna/intern/dna_utils.c
M source/blender/makesdna/intern/dna_utils.h
M source/blender/makesdna/intern/makesdna.c
M source/blender/makesrna/intern/rna_define.c
===================================================================
diff --git a/source/blender/blenloader/intern/versioning_dna.c b/source/blender/blenloader/intern/versioning_dna.c
index d379edd83f6..c855971d131 100644
--- a/source/blender/blenloader/intern/versioning_dna.c
+++ b/source/blender/blenloader/intern/versioning_dna.c
@@ -16,19 +16,27 @@
/** \file \ingroup blenloader
*
- * Apply edits to DNA at load time
- * to behave as if old files were written new names.
+ * Apply edits to DNA at load time.
+ *
+ * - #DNA_MAKESDNA undefined:
+ * Behave as if old files were written new names.
+ * - #DNA_MAKESDNA defined:
+ * Defines store files with old names but runtime uses new names.
*/
-#include "BLI_compiler_attrs.h"
-#include "BLI_utildefines.h"
+#ifndef DNA_MAKESDNA
+# include "BLI_compiler_attrs.h"
+# include "BLI_utildefines.h"
+
+# include "DNA_genfile.h"
+# include "DNA_listBase.h"
-#include "DNA_genfile.h"
-#include "DNA_listBase.h"
+# include "BLO_readfile.h"
+# include "readfile.h"
+#endif
-#include "BLO_readfile.h"
-#include "readfile.h"
+#ifndef DNA_MAKESDNA
/**
* Manipulates SDNA before calling #DNA_struct_get_compareflags,
* allowing us to rename structs and struct members.
@@ -57,3 +65,15 @@ void blo_do_versions_dna(SDNA *sdna, const int versionfile, const int subversion
#undef DNA_VERSION_ATLEAST
}
+
+#else /* !DNA_MAKESDNA */
+
+/* Included in DNA versioning code. */
+
+DNA_STRUCT_REPLACE(Lamp, Light)
+DNA_STRUCT_MEMBER_REPLACE(Lamp, clipsta, clip_start)
+DNA_STRUCT_MEMBER_REPLACE(Lamp, clipend, clip_end)
+
+DNA_STRUCT_MEMBER_REPLACE(Camera, YF_dofdist, dof_dist)
+
+#endif /* !DNA_MAKESDNA */
diff --git a/source/blender/makesdna/DNA_genfile.h b/source/blender/makesdna/DNA_genfile.h
index 0d330c8999a..b46ec446791 100644
--- a/source/blender/makesdna/DNA_genfile.h
+++ b/source/blender/makesdna/DNA_genfile.h
@@ -90,6 +90,7 @@ void DNA_sdna_current_free(void);
int DNA_struct_find_nr_ex(const struct SDNA *sdna, const char *str, unsigned int *index_last);
int DNA_struct_find_nr(const struct SDNA *sdna, const char *str);
+
void DNA_struct_switch_endian(const struct SDNA *oldsdna, int oldSDNAnr, char *data);
const char *DNA_struct_get_compareflags(const struct SDNA *sdna, const struct SDNA *newsdna);
void *DNA_struct_reconstruct(
diff --git a/source/blender/makesdna/intern/CMakeLists.txt b/source/blender/makesdna/intern/CMakeLists.txt
index 4d6ad4b4389..d6ad251d373 100644
--- a/source/blender/makesdna/intern/CMakeLists.txt
+++ b/source/blender/makesdna/intern/CMakeLists.txt
@@ -35,7 +35,11 @@ blender_include_dirs(
set(SRC
dna_utils.c
makesdna.c
+ ../../blenlib/intern/BLI_ghash.c
+ ../../blenlib/intern/BLI_ghash_utils.c
../../blenlib/intern/BLI_memarena.c
+ ../../blenlib/intern/BLI_mempool.c
+ ../../blenlib/intern/hash_mm2a.c
../../../../intern/guardedalloc/intern/mallocn.c
../../../../intern/guardedalloc/intern/mallocn_guarded_impl.c
../../../../intern/guardedalloc/intern/mallocn_lockfree_impl.c
diff --git a/source/blender/makesdna/intern/dna_genfile.c b/source/blender/makesdna/intern/dna_genfile.c
index 6d4eab11c0f..ed5b00f1458 100644
--- a/source/blender/makesdna/intern/dna_genfile.c
+++ b/source/blender/makesdna/intern/dna_genfile.c
@@ -156,6 +156,9 @@ void DNA_sdna_free(SDNA *sdna)
BLI_memarena_free(sdna->mem_arena);
}
+ MEM_SAFE_FREE(sdna->runtime.names);
+ MEM_SAFE_FREE(sdna->runtime.types);
+
MEM_freeN(sdna);
}
@@ -311,6 +314,9 @@ static bool init_structDNA(
#endif
sdna->mem_arena = NULL;
+ /* Lazy initialize. */
+ memset(&sdna->runtime, 0, sizeof(sdna->runtime));
+
/* Struct DNA ('SDNA') */
if (*data != MAKE_ID('S', 'D', 'N', 'A')) {
*r_error_message = "SDNA error in SDNA file";
@@ -1438,3 +1444,113 @@ bool DNA_sdna_patch_struct_member(
}
/** \} */
+
+
+/* -------------------------------------------------------------------- */
+/** \name Versioning (Forward Compatible)
+ *
+ * Versioning that allows new names.
+ * \{ */
+
+/**
+ * Unique names are shared, which causes problems renaming.
+ * Make sure every struct member gets it's own name so any renaming
+ */
+static void sdna_softpatch_runtime_expand_names(SDNA *sdna)
+{
+ int names_len_all = 0;
+ for (int struct_nr = 0; struct_nr < sdna->nr_structs; struct_nr++) {
+ const short *sp = sdna->structs[struct_nr];
+ names_len_all += sp[1];
+ }
+ const char **names_expand = MEM_mallocN(sizeof(*names_expand) * names_len_all, __func__);
+
+ int names_expand_index = 0;
+ for (int struct_nr = 0; struct_nr < sdna->nr_structs; struct_nr++) {
+ /* We can't edit this memory 'sdna->structs' points to (readonly datatoc file). */
+ const short *sp = sdna->structs[struct_nr];
+ short *sp_expand = BLI_memarena_alloc(sdna->mem_arena, sizeof(short[2]) * (1 + sp[1]));
+ memcpy(sp_expand, sp, sizeof(short[2]) * (1 + sp[1]));
+ sdna->structs[struct_nr] = sp_expand;
+ const int names_len = sp[1];
+ sp += 2;
+ sp_expand += 2;
+ for (int i = 0; i < names_len; i++, sp += 2, sp_expand += 2) {
+ names_expand[names_expand_index] = sdna->names[sp[1]];
+ BLI_assert(names_expand_index < SHRT_MAX);
+ sp_expand[1] = names_expand_index;
+ names_expand_index++;
+ }
+ }
+ MEM_freeN((void *)sdna->names);
+ sdna->names = names_expand;
+ sdna->nr_names = names_len_all;
+}
+
+void DNA_sdna_softpatch_runtime_ensure(SDNA *sdna)
+{
+ if (sdna->mem_arena == NULL) {
+ sdna->mem_arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
+ }
+
+ GHash *struct_map_runtime_from_static;
+ GHash *elem_map_runtime_from_static;
+
+ DNA_softupdate_maps(
+ DNA_VERSION_RUNTIME_FROM_STATIC,
+ &struct_map_runtime_from_static,
+ &elem_map_runtime_from_static);
+
+
+ if (sdna->runtime.types == NULL) {
+ sdna->runtime.types = MEM_mallocN(sizeof(*sdna->runtime.types) * sdna->nr_types, __func__);
+ for (int type_nr = 0; type_nr < sdna->nr_types; type_nr++) {
+ const char *str = sdna->types[type_nr];
+ sdna->runtime.types[type_nr] = BLI_ghash_lookup_default(
+ struct_map_runtime_from_static, str, (void *)str);
+ }
+ }
+
+ if (sdna->runtime.names == NULL) {
+ sdna_softpatch_runtime_expand_names(sdna);
+ sdna->runtime.names = MEM_mallocN(sizeof(*sdna->runtime.names) * sdna->nr_names, __func__);
+ for (int struct_nr = 0; struct_nr < sdna->nr_structs; struct_nr++) {
+ const short *sp = sdna->structs[struct_nr];
+ const char *struct_name_static = sdna->types[sp[0]];
+ const int dna_struct_names_len = sp[1];
+ sp += 2;
+ for (int a = 0; a < dna_struct_names_len; a++, sp += 2) {
+ const char *elem_static = sdna->names[sp[1]];
+ const uint elem_static_offset_start = DNA_elem_id_offset_start(elem_static);
+ const char *elem_static_trim = elem_static + elem_static_offset_start;
+ const uint member_dna_offset_end = (
+ elem_static_offset_start + DNA_elem_id_offset_end(elem_static_trim));
+
+ const uint elem_static_buf_len = member_dna_offset_end - elem_static_offset_start;
+ char elem_static_buf[1024];
+ strncpy(elem_static_buf, elem_static_trim, elem_static_buf_len);
+ elem_static_buf[elem_static_buf_len] = '\0';
+ // printf("Searching '%s.%s'\n", struct_name_static, elem_static_buf);
+ const char *str_pair[2] = {struct_name_static, elem_static_buf};
+ const char *elem_runtime = BLI_ghash_lookup(elem_map_runtime_from_static, str_pair);
+ if (elem_runtime) {
+ // printf("Found %s from %s\n", elem_runtime, elem_static_buf);
+ sdna->runtime.names[sp[1]] = DNA_elem_id_rename(
+ sdna->mem_arena,
+ elem_static_trim, strlen(elem_static_trim),
+ elem_runtime, strlen(elem_runtime),
+ elem_static, strlen(elem_static),
+ elem_static_offset_start);
+ // printf("result is: %s\n", sdna->runtime.names[sp[1]]);
+ }
+ else {
+ sdna->runtime.names[sp[1]] = sdna->names[sp[1]];
+ }
+ }
+ }
+ }
+ BLI_ghash_free(struct_map_runtime_from_static, NULL, NULL);
+ BLI_ghash_free(elem_map_runtime_from_static, MEM_freeN, NULL);
+}
+
+/** \} */
diff --git a/source/blender/makesdna/intern/dna_utils.c b/source/blender/makesdna/intern/dna_utils.c
index f1a04eae7aa..1ca290e73db 100644
--- a/source/blender/makesdna/intern/dna_utils.c
+++ b/source/blender/makesdna/intern/dna_utils.c
@@ -23,9 +23,12 @@
#include <string.h>
+#include "MEM_guardedalloc.h"
+
#include "BLI_sys_types.h"
#include "BLI_utildefines.h"
#include "BLI_assert.h"
+#include "BLI_ghash.h"
#include "BLI_memarena.h"
@@ -104,6 +107,7 @@ uint DNA_elem_id_offset_end(const char *elem_dna)
return elem_dna_offset;
}
+
/**
* Check if 'var' matches '*var[3]' for eg,
* return true if it does, with start/end offsets.
@@ -162,3 +166,96 @@ char *DNA_elem_id_rename(
}
/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Versioning
+ * \{ */
+
+/* Use for #DNA_VERSIONING_INCLUDE */
+#define DNA_MAKESDNA
+
+static void dna_softupdate_ghash_add_pair(GHash *gh, const char *a, const char *b, void *value)
+{
+ const char **str_pair = MEM_mallocN(sizeof(char *) * 2, __func__);
+ str_pair[0] = a;
+ str_pair[1] = b;
+ BLI_ghash_insert(gh, str_pair, value);
+}
+
+static uint strhash_pair_p(const void *ptr)
+{
+ const char * const *pair = ptr;
+ return (BLI_ghashutil_strhash_p(pair[0]) ^
+ BLI_ghashutil_strhash_p(pair[1]));
+}
+
+static bool strhash_pair_cmp(const void *a, const void *b)
+{
+ const char * const *pair_a = a;
+ const char * const *pair_b = b;
+ return (STREQ(pair_a[0], pair_b[0]) &&
+ STREQ(pair_a[1], pair_b[1])) ? false : true;
+}
+
+void DNA_softupdate_maps(
+ enum eDNAVersionDir version_dir,
+ GHash **r_struct_map, GHash **r_elem_map)
+{
+
+ if (r_struct_map != NULL) {
+ const char *data[][2] = {
+#define DNA_STRUCT_REPLACE(old, new) {#old, #new},
+#define DNA_STRUCT_MEMBER_REPLACE(struct_name, old, new)
+#include DNA_VERSIONING_DEFINES
+#undef DNA_STRUCT_REPLACE
+#undef DNA_STRUCT_MEMBER_REPLACE
+ };
+
+ int elem_key, elem_val;
+ if (version_dir == DNA_VERSION_RUNTIME_FROM_STATIC) {
+ elem_key = 0;
+ elem_val = 1;
+
+ }
+ else {
+ elem_key = 1;
+ elem_val = 0;
+ }
+
+ GHash *struct_map = BLI_ghash_str_new_ex(__func__, ARRAY_SIZE(data));
+ for (int i = 0; i < ARRAY_SIZE(d
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list