[Bf-blender-cvs] [458bfeac219] uuid-undo-experiments: Add uuid support to main_idmap.

Bastien Montagne noreply at git.blender.org
Mon Mar 2 16:25:22 CET 2020


Commit: 458bfeac2194f63e78083b5925a80bddc1e0532d
Author: Bastien Montagne
Date:   Mon Mar 2 16:14:25 2020 +0100
Branches: uuid-undo-experiments
https://developer.blender.org/rB458bfeac2194f63e78083b5925a80bddc1e0532d

Add uuid support to main_idmap.

===================================================================

M	source/blender/blenkernel/BKE_main_idmap.h
M	source/blender/blenkernel/intern/lib_id.c
M	source/blender/blenkernel/intern/main_idmap.c
M	source/blender/blenloader/intern/readfile.c
M	source/blender/makesdna/DNA_ID.h

===================================================================

diff --git a/source/blender/blenkernel/BKE_main_idmap.h b/source/blender/blenkernel/BKE_main_idmap.h
index b411d34be47..7523ab48a1e 100644
--- a/source/blender/blenkernel/BKE_main_idmap.h
+++ b/source/blender/blenkernel/BKE_main_idmap.h
@@ -36,20 +36,30 @@ struct ID;
 struct IDNameLib_Map;
 struct Main;
 
+enum {
+  MAIN_IDMAP_TYPE_NAME = 1 << 0,
+  MAIN_IDMAP_TYPE_UUID = 1 << 1,
+};
+
 struct IDNameLib_Map *BKE_main_idmap_create(struct Main *bmain,
                                             const bool create_valid_ids_set,
-                                            struct Main *old_bmain) ATTR_WARN_UNUSED_RESULT
+                                            struct Main *old_bmain,
+                                            const int idmap_types) ATTR_WARN_UNUSED_RESULT
     ATTR_NONNULL(1);
 void BKE_main_idmap_destroy(struct IDNameLib_Map *id_typemap) ATTR_NONNULL();
 struct Main *BKE_main_idmap_main_get(struct IDNameLib_Map *id_typemap) ATTR_WARN_UNUSED_RESULT
     ATTR_NONNULL();
-struct ID *BKE_main_idmap_lookup(struct IDNameLib_Map *id_typemap,
-                                 short id_type,
-                                 const char *name,
-                                 const struct Library *lib) ATTR_WARN_UNUSED_RESULT
+struct ID *BKE_main_idmap_lookup_name(struct IDNameLib_Map *id_typemap,
+                                      short id_type,
+                                      const char *name,
+                                      const struct Library *lib) ATTR_WARN_UNUSED_RESULT
     ATTR_NONNULL(1, 3);
 struct ID *BKE_main_idmap_lookup_id(struct IDNameLib_Map *id_typemap,
                                     const struct ID *id) ATTR_WARN_UNUSED_RESULT
     ATTR_NONNULL(1, 2);
 
+struct ID *BKE_main_idmap_lookup_uuid(struct IDNameLib_Map *id_typemap,
+                                      const uint session_uuid) ATTR_WARN_UNUSED_RESULT
+    ATTR_NONNULL(1);
+
 #endif /* __BKE_MAIN_IDMAP_H__ */
diff --git a/source/blender/blenkernel/intern/lib_id.c b/source/blender/blenkernel/intern/lib_id.c
index 5e7300b1e28..448ad754615 100644
--- a/source/blender/blenkernel/intern/lib_id.c
+++ b/source/blender/blenkernel/intern/lib_id.c
@@ -1377,7 +1377,7 @@ void BKE_libblock_init_empty(ID *id)
   }
 }
 
-static uint64_t session_uuid = 0;
+static uint session_uuid = 0;
 /** Generate a session-wise uuid for the given \a id. */
 void BKE_lib_libblock_uuid_generate(ID *id)
 {
diff --git a/source/blender/blenkernel/intern/main_idmap.c b/source/blender/blenkernel/intern/main_idmap.c
index a210961b212..4563f4ba1c6 100644
--- a/source/blender/blenkernel/intern/main_idmap.c
+++ b/source/blender/blenkernel/intern/main_idmap.c
@@ -26,6 +26,7 @@
 #include "DNA_ID.h"
 
 #include "BKE_idcode.h"
+#include "BKE_lib_id.h"
 #include "BKE_main.h"
 #include "BKE_main_idmap.h" /* own include */
 
@@ -65,16 +66,20 @@ struct IDNameLib_TypeMap {
  */
 struct IDNameLib_Map {
   struct IDNameLib_TypeMap type_maps[MAX_LIBARRAY];
+  struct GHash *uuid_map;
   struct Main *bmain;
   struct GSet *valid_id_pointers;
+  int idmap_types;
 };
 
 static struct IDNameLib_TypeMap *main_idmap_from_idcode(struct IDNameLib_Map *id_map,
                                                         short id_type)
 {
-  for (int i = 0; i < MAX_LIBARRAY; i++) {
-    if (id_map->type_maps[i].id_type == id_type) {
-      return &id_map->type_maps[i];
+  if (id_map->idmap_types & MAIN_IDMAP_TYPE_NAME) {
+    for (int i = 0; i < MAX_LIBARRAY; i++) {
+      if (id_map->type_maps[i].id_type == id_type) {
+        return &id_map->type_maps[i];
+      }
     }
   }
   return NULL;
@@ -94,9 +99,12 @@ static struct IDNameLib_TypeMap *main_idmap_from_idcode(struct IDNameLib_Map *id
  */
 struct IDNameLib_Map *BKE_main_idmap_create(struct Main *bmain,
                                             const bool create_valid_ids_set,
-                                            struct Main *old_bmain)
+                                            struct Main *old_bmain,
+                                            const int idmap_types)
 {
   struct IDNameLib_Map *id_map = MEM_mallocN(sizeof(*id_map), __func__);
+  id_map->bmain = bmain;
+  id_map->idmap_types = idmap_types;
 
   int index = 0;
   while (index < MAX_LIBARRAY) {
@@ -107,7 +115,20 @@ struct IDNameLib_Map *BKE_main_idmap_create(struct Main *bmain,
   }
   BLI_assert(index == MAX_LIBARRAY);
 
-  id_map->bmain = bmain;
+  if (idmap_types & MAIN_IDMAP_TYPE_UUID) {
+    ID *id;
+    id_map->uuid_map = BLI_ghash_int_new(__func__);
+    FOREACH_MAIN_ID_BEGIN (bmain, id) {
+      BLI_assert(id->session_uuid != BKE_MAIN_ID_UUID_UNSET);
+      void **id_ptr_v = BLI_ghash_lookup_p(id_map->uuid_map, POINTER_FROM_UINT(id->session_uuid));
+      BLI_assert(*id_ptr_v == NULL);
+      *id_ptr_v = id;
+    }
+    FOREACH_MAIN_ID_END;
+  }
+  else {
+    id_map->uuid_map = NULL;
+  }
 
   if (create_valid_ids_set) {
     id_map->valid_id_pointers = BKE_main_gset_create(bmain, NULL);
@@ -144,10 +165,10 @@ static bool idkey_cmp(const void *a, const void *b)
   return strcmp(idkey_a->name, idkey_b->name) || (idkey_a->lib != idkey_b->lib);
 }
 
-ID *BKE_main_idmap_lookup(struct IDNameLib_Map *id_map,
-                          short id_type,
-                          const char *name,
-                          const Library *lib)
+ID *BKE_main_idmap_lookup_name(struct IDNameLib_Map *id_map,
+                               short id_type,
+                               const char *name,
+                               const Library *lib)
 {
   struct IDNameLib_TypeMap *type_map = main_idmap_from_idcode(id_map, id_type);
 
@@ -188,7 +209,15 @@ ID *BKE_main_idmap_lookup_id(struct IDNameLib_Map *id_map, const ID *id)
    * when trying to get ID name).
    */
   if (id_map->valid_id_pointers == NULL || BLI_gset_haskey(id_map->valid_id_pointers, id)) {
-    return BKE_main_idmap_lookup(id_map, GS(id->name), id->name + 2, id->lib);
+    return BKE_main_idmap_lookup_name(id_map, GS(id->name), id->name + 2, id->lib);
+  }
+  return NULL;
+}
+
+ID *BKE_main_idmap_lookup_uuid(struct IDNameLib_Map *id_map, const uint session_uuid)
+{
+  if (id_map->idmap_types & MAIN_IDMAP_TYPE_UUID) {
+    return BLI_ghash_lookup(id_map->uuid_map, POINTER_FROM_UINT(session_uuid));
   }
   return NULL;
 }
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index 7da84475a51..7a1c9623b19 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -8056,7 +8056,8 @@ void blo_lib_link_restore(Main *oldmain,
                           Scene *curscene,
                           ViewLayer *cur_view_layer)
 {
-  struct IDNameLib_Map *id_map = BKE_main_idmap_create(newmain, true, oldmain);
+  struct IDNameLib_Map *id_map = BKE_main_idmap_create(
+      newmain, true, oldmain, MAIN_IDMAP_TYPE_NAME);
 
   for (WorkSpace *workspace = newmain->workspaces.first; workspace;
        workspace = workspace->id.next) {
diff --git a/source/blender/makesdna/DNA_ID.h b/source/blender/makesdna/DNA_ID.h
index 23d16b0b7f6..2d552703e69 100644
--- a/source/blender/makesdna/DNA_ID.h
+++ b/source/blender/makesdna/DNA_ID.h
@@ -245,17 +245,17 @@ typedef struct ID {
   int us;
   int icon_id;
   int recalc;
-  char _pad[4];
-  IDProperty *properties;
-
-  /** Reference linked ID which this one overrides. */
-  IDOverrideLibrary *override_library;
 
   /**
    * A session-wide unique identifier for a given ID, that remain the same across potential
    * re-allocations (e.g. due to undo/redo steps).
    */
-  uint64_t session_uuid;
+  unsigned int session_uuid;
+
+  IDProperty *properties;
+
+  /** Reference linked ID which this one overrides. */
+  IDOverrideLibrary *override_library;
 
   /**
    * Only set for data-blocks which are coming from copy-on-write, points to



More information about the Bf-blender-cvs mailing list