1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2005 by the Blender Foundation.
17  * All rights reserved.
18  * Modifier stack implementation.
19  *
20  * BKE_modifier.h contains the function prototypes for this file.
21  */
22 
23 /** \file
24  * \ingroup bke
25  */
26 
27 #include <string.h>
28 
29 #include "MEM_guardedalloc.h"
30 
31 #include "BLI_ghash.h"
32 #include "BLI_utildefines.h"
33 
34 #include "CLG_log.h"
35 
36 #include "BLT_translation.h"
37 
38 #include "DNA_ID.h"
39 #include "DNA_node_types.h"
40 #include "DNA_scene_types.h"
41 
42 #include "BKE_main.h"
43 #include "BKE_node.h"
44 
45 #include "BKE_idtype.h"
46 
47 // static CLG_LogRef LOG = {"bke.idtype"};
48 
BKE_idtype_cache_key_hash(const void * key_v)49 uint BKE_idtype_cache_key_hash(const void *key_v)
50 {
51   const IDCacheKey *key = key_v;
52   size_t hash = BLI_ghashutil_uinthash(key->id_session_uuid);
53   hash = BLI_ghashutil_combine_hash(hash, BLI_ghashutil_uinthash((uint)key->offset_in_ID));
54   return (uint)BLI_ghashutil_combine_hash(hash, BLI_ghashutil_ptrhash(key->cache_v));
55 }
56 
BKE_idtype_cache_key_cmp(const void * key_a_v,const void * key_b_v)57 bool BKE_idtype_cache_key_cmp(const void *key_a_v, const void *key_b_v)
58 {
59   const IDCacheKey *key_a = key_a_v;
60   const IDCacheKey *key_b = key_b_v;
61   return (key_a->id_session_uuid != key_b->id_session_uuid) ||
62          (key_a->offset_in_ID != key_b->offset_in_ID) || (key_a->cache_v != key_b->cache_v);
63 }
64 
65 static IDTypeInfo *id_types[MAX_LIBARRAY] = {NULL};
66 
id_type_init(void)67 static void id_type_init(void)
68 {
69 #define INIT_TYPE(_id_code) \
70   { \
71     BLI_assert(IDType_##_id_code.main_listbase_index == INDEX_##_id_code); \
72     id_types[INDEX_##_id_code] = &IDType_##_id_code; \
73   } \
74   (void)0
75 
76   INIT_TYPE(ID_SCE);
77   INIT_TYPE(ID_LI);
78   INIT_TYPE(ID_OB);
79   INIT_TYPE(ID_ME);
80   INIT_TYPE(ID_CU);
81   INIT_TYPE(ID_MB);
82   INIT_TYPE(ID_MA);
83   INIT_TYPE(ID_TE);
84   INIT_TYPE(ID_IM);
85   INIT_TYPE(ID_LT);
86   INIT_TYPE(ID_LA);
87   INIT_TYPE(ID_CA);
88   INIT_TYPE(ID_IP);
89   INIT_TYPE(ID_KE);
90   INIT_TYPE(ID_WO);
91   INIT_TYPE(ID_SCR);
92   INIT_TYPE(ID_VF);
93   INIT_TYPE(ID_TXT);
94   INIT_TYPE(ID_SPK);
95   INIT_TYPE(ID_SO);
96   INIT_TYPE(ID_GR);
97   INIT_TYPE(ID_AR);
98   INIT_TYPE(ID_AC);
99   INIT_TYPE(ID_NT);
100   INIT_TYPE(ID_BR);
101   INIT_TYPE(ID_PA);
102   INIT_TYPE(ID_GD);
103   INIT_TYPE(ID_WM);
104   INIT_TYPE(ID_MC);
105   INIT_TYPE(ID_MSK);
106   INIT_TYPE(ID_LS);
107   INIT_TYPE(ID_PAL);
108   INIT_TYPE(ID_PC);
109   INIT_TYPE(ID_CF);
110   INIT_TYPE(ID_WS);
111   INIT_TYPE(ID_LP);
112   INIT_TYPE(ID_HA);
113   INIT_TYPE(ID_PT);
114   INIT_TYPE(ID_VO);
115   INIT_TYPE(ID_SIM);
116 
117   /* Special naughty boy... */
118   BLI_assert(IDType_ID_LINK_PLACEHOLDER.main_listbase_index == INDEX_ID_NULL);
119   id_types[INDEX_ID_NULL] = &IDType_ID_LINK_PLACEHOLDER;
120 
121 #undef INIT_TYPE
122 }
123 
BKE_idtype_init(void)124 void BKE_idtype_init(void)
125 {
126   /* Initialize data-block types. */
127   id_type_init();
128 }
129 
BKE_idtype_get_info_from_idcode(const short id_code)130 const IDTypeInfo *BKE_idtype_get_info_from_idcode(const short id_code)
131 {
132   int id_index = BKE_idtype_idcode_to_index(id_code);
133 
134   if (id_index >= 0 && id_index < ARRAY_SIZE(id_types) && id_types[id_index] != NULL &&
135       id_types[id_index]->name[0] != '\0') {
136     return id_types[id_index];
137   }
138 
139   return NULL;
140 }
141 
BKE_idtype_get_info_from_id(const ID * id)142 const IDTypeInfo *BKE_idtype_get_info_from_id(const ID *id)
143 {
144   return BKE_idtype_get_info_from_idcode(GS(id->name));
145 }
146 
idtype_get_info_from_name(const char * idtype_name)147 static const IDTypeInfo *idtype_get_info_from_name(const char *idtype_name)
148 {
149   for (int i = ARRAY_SIZE(id_types); i--;) {
150     if (id_types[i] != NULL && STREQ(idtype_name, id_types[i]->name)) {
151       return id_types[i];
152     }
153   }
154 
155   return NULL;
156 }
157 
158 /* Various helpers/wrappers around #IDTypeInfo structure. */
159 
160 /**
161  * Convert an \a idcode into a name.
162  *
163  * \param idcode: The code to convert.
164  * \return A static string representing the name of
165  * the code.
166  */
BKE_idtype_idcode_to_name(const short idcode)167 const char *BKE_idtype_idcode_to_name(const short idcode)
168 {
169   const IDTypeInfo *id_type = BKE_idtype_get_info_from_idcode(idcode);
170   BLI_assert(id_type != NULL);
171   return id_type != NULL ? id_type->name : NULL;
172 }
173 
174 /**
175  * Convert an \a idcode into a name (plural).
176  *
177  * \param idcode: The code to convert.
178  * \return A static string representing the name of
179  * the code.
180  */
BKE_idtype_idcode_to_name_plural(const short idcode)181 const char *BKE_idtype_idcode_to_name_plural(const short idcode)
182 {
183   const IDTypeInfo *id_type = BKE_idtype_get_info_from_idcode(idcode);
184   BLI_assert(id_type != NULL);
185   return id_type != NULL ? id_type->name_plural : NULL;
186 }
187 
188 /**
189  * Convert an \a idcode into its translations' context.
190  *
191  * \param idcode: The code to convert.
192  * \return A static string representing the i18n context of the code.
193  */
BKE_idtype_idcode_to_translation_context(const short idcode)194 const char *BKE_idtype_idcode_to_translation_context(const short idcode)
195 {
196   const IDTypeInfo *id_type = BKE_idtype_get_info_from_idcode(idcode);
197   BLI_assert(id_type != NULL);
198   return id_type != NULL ? id_type->translation_context : BLT_I18NCONTEXT_DEFAULT;
199 }
200 
201 /**
202  * Convert an ID-type name into an \a idcode (ie. #ID_SCE)
203  *
204  * \param idtype_name: The ID-type's "user visible name" to convert.
205  * \return The \a idcode for the name, or 0 if invalid.
206  */
BKE_idtype_idcode_from_name(const char * idtype_name)207 short BKE_idtype_idcode_from_name(const char *idtype_name)
208 {
209   const IDTypeInfo *id_type = idtype_get_info_from_name(idtype_name);
210   BLI_assert(id_type);
211   return id_type != NULL ? id_type->id_code : 0;
212 }
213 
214 /**
215  * Return if the ID code is a valid ID code.
216  *
217  * \param idcode: The code to check.
218  * \return Boolean, 0 when invalid.
219  */
BKE_idtype_idcode_is_valid(const short idcode)220 bool BKE_idtype_idcode_is_valid(const short idcode)
221 {
222   return BKE_idtype_get_info_from_idcode(idcode) != NULL ? true : false;
223 }
224 
225 /**
226  * Return non-zero when an ID type is linkable.
227  *
228  * \param idcode: The code to check.
229  * \return Boolean, 0 when non linkable.
230  */
BKE_idtype_idcode_is_linkable(const short idcode)231 bool BKE_idtype_idcode_is_linkable(const short idcode)
232 {
233   const IDTypeInfo *id_type = BKE_idtype_get_info_from_idcode(idcode);
234   BLI_assert(id_type != NULL);
235   return id_type != NULL ? (id_type->flags & IDTYPE_FLAGS_NO_LIBLINKING) == 0 : false;
236 }
237 
238 /**
239  * Convert an \a idcode into an \a idfilter (e.g. ID_OB -> FILTER_ID_OB).
240  */
BKE_idtype_idcode_to_idfilter(const short idcode)241 uint64_t BKE_idtype_idcode_to_idfilter(const short idcode)
242 {
243 #define CASE_IDFILTER(_id) \
244   case ID_##_id: \
245     return FILTER_ID_##_id
246 
247   switch (idcode) {
248     CASE_IDFILTER(AC);
249     CASE_IDFILTER(AR);
250     CASE_IDFILTER(BR);
251     CASE_IDFILTER(CA);
252     CASE_IDFILTER(CF);
253     CASE_IDFILTER(CU);
254     CASE_IDFILTER(GD);
255     CASE_IDFILTER(GR);
256     CASE_IDFILTER(HA);
257     CASE_IDFILTER(IM);
258     CASE_IDFILTER(LA);
259     CASE_IDFILTER(LS);
260     CASE_IDFILTER(LT);
261     CASE_IDFILTER(MA);
262     CASE_IDFILTER(MB);
263     CASE_IDFILTER(MC);
264     CASE_IDFILTER(ME);
265     CASE_IDFILTER(MSK);
266     CASE_IDFILTER(NT);
267     CASE_IDFILTER(OB);
268     CASE_IDFILTER(PA);
269     CASE_IDFILTER(PAL);
270     CASE_IDFILTER(PC);
271     CASE_IDFILTER(PT);
272     CASE_IDFILTER(LP);
273     CASE_IDFILTER(SCE);
274     CASE_IDFILTER(SIM);
275     CASE_IDFILTER(SPK);
276     CASE_IDFILTER(SO);
277     CASE_IDFILTER(TE);
278     CASE_IDFILTER(TXT);
279     CASE_IDFILTER(VF);
280     CASE_IDFILTER(VO);
281     CASE_IDFILTER(WO);
282     CASE_IDFILTER(WS);
283     default:
284       return 0;
285   }
286 
287 #undef CASE_IDFILTER
288 }
289 
290 /**
291  * Convert an \a idfilter into an \a idcode (e.g. #FILTER_ID_OB -> #ID_OB).
292  */
BKE_idtype_idcode_from_idfilter(const uint64_t idfilter)293 short BKE_idtype_idcode_from_idfilter(const uint64_t idfilter)
294 {
295 #define CASE_IDFILTER(_id) \
296   case FILTER_ID_##_id: \
297     return ID_##_id
298 
299   switch (idfilter) {
300     CASE_IDFILTER(AC);
301     CASE_IDFILTER(AR);
302     CASE_IDFILTER(BR);
303     CASE_IDFILTER(CA);
304     CASE_IDFILTER(CF);
305     CASE_IDFILTER(CU);
306     CASE_IDFILTER(GD);
307     CASE_IDFILTER(GR);
308     CASE_IDFILTER(HA);
309     CASE_IDFILTER(IM);
310     CASE_IDFILTER(LA);
311     CASE_IDFILTER(LS);
312     CASE_IDFILTER(LT);
313     CASE_IDFILTER(MA);
314     CASE_IDFILTER(MB);
315     CASE_IDFILTER(MC);
316     CASE_IDFILTER(ME);
317     CASE_IDFILTER(MSK);
318     CASE_IDFILTER(NT);
319     CASE_IDFILTER(OB);
320     CASE_IDFILTER(PA);
321     CASE_IDFILTER(PAL);
322     CASE_IDFILTER(PC);
323     CASE_IDFILTER(PT);
324     CASE_IDFILTER(LP);
325     CASE_IDFILTER(SCE);
326     CASE_IDFILTER(SIM);
327     CASE_IDFILTER(SPK);
328     CASE_IDFILTER(SO);
329     CASE_IDFILTER(TE);
330     CASE_IDFILTER(TXT);
331     CASE_IDFILTER(VF);
332     CASE_IDFILTER(VO);
333     CASE_IDFILTER(WO);
334     default:
335       return 0;
336   }
337 
338 #undef CASE_IDFILTER
339 }
340 
341 /**
342  * Convert an \a idcode into an index (e.g. #ID_OB -> #INDEX_ID_OB).
343  */
BKE_idtype_idcode_to_index(const short idcode)344 int BKE_idtype_idcode_to_index(const short idcode)
345 {
346 #define CASE_IDINDEX(_id) \
347   case ID_##_id: \
348     return INDEX_ID_##_id
349 
350   switch ((ID_Type)idcode) {
351     CASE_IDINDEX(AC);
352     CASE_IDINDEX(AR);
353     CASE_IDINDEX(BR);
354     CASE_IDINDEX(CA);
355     CASE_IDINDEX(CF);
356     CASE_IDINDEX(CU);
357     CASE_IDINDEX(GD);
358     CASE_IDINDEX(GR);
359     CASE_IDINDEX(HA);
360     CASE_IDINDEX(IM);
361     CASE_IDINDEX(IP);
362     CASE_IDINDEX(KE);
363     CASE_IDINDEX(LA);
364     CASE_IDINDEX(LI);
365     CASE_IDINDEX(LS);
366     CASE_IDINDEX(LT);
367     CASE_IDINDEX(MA);
368     CASE_IDINDEX(MB);
369     CASE_IDINDEX(MC);
370     CASE_IDINDEX(ME);
371     CASE_IDINDEX(MSK);
372     CASE_IDINDEX(NT);
373     CASE_IDINDEX(OB);
374     CASE_IDINDEX(PA);
375     CASE_IDINDEX(PAL);
376     CASE_IDINDEX(PC);
377     CASE_IDINDEX(PT);
378     CASE_IDINDEX(LP);
379     CASE_IDINDEX(SCE);
380     CASE_IDINDEX(SCR);
381     CASE_IDINDEX(SIM);
382     CASE_IDINDEX(SPK);
383     CASE_IDINDEX(SO);
384     CASE_IDINDEX(TE);
385     CASE_IDINDEX(TXT);
386     CASE_IDINDEX(VF);
387     CASE_IDINDEX(VO);
388     CASE_IDINDEX(WM);
389     CASE_IDINDEX(WO);
390     CASE_IDINDEX(WS);
391   }
392 
393   /* Special naughty boy... */
394   if (idcode == ID_LINK_PLACEHOLDER) {
395     return INDEX_ID_NULL;
396   }
397 
398   return -1;
399 
400 #undef CASE_IDINDEX
401 }
402 
403 /**
404  * Get an \a idcode from an index (e.g. #INDEX_ID_OB -> #ID_OB).
405  */
BKE_idtype_idcode_from_index(const int index)406 short BKE_idtype_idcode_from_index(const int index)
407 {
408 #define CASE_IDCODE(_id) \
409   case INDEX_ID_##_id: \
410     return ID_##_id
411 
412   switch (index) {
413     CASE_IDCODE(AC);
414     CASE_IDCODE(AR);
415     CASE_IDCODE(BR);
416     CASE_IDCODE(CA);
417     CASE_IDCODE(CF);
418     CASE_IDCODE(CU);
419     CASE_IDCODE(GD);
420     CASE_IDCODE(GR);
421     CASE_IDCODE(HA);
422     CASE_IDCODE(IM);
423     CASE_IDCODE(IP);
424     CASE_IDCODE(KE);
425     CASE_IDCODE(LA);
426     CASE_IDCODE(LI);
427     CASE_IDCODE(LS);
428     CASE_IDCODE(LT);
429     CASE_IDCODE(MA);
430     CASE_IDCODE(MB);
431     CASE_IDCODE(MC);
432     CASE_IDCODE(ME);
433     CASE_IDCODE(MSK);
434     CASE_IDCODE(NT);
435     CASE_IDCODE(OB);
436     CASE_IDCODE(PA);
437     CASE_IDCODE(PAL);
438     CASE_IDCODE(PC);
439     CASE_IDCODE(PT);
440     CASE_IDCODE(LP);
441     CASE_IDCODE(SCE);
442     CASE_IDCODE(SCR);
443     CASE_IDCODE(SIM);
444     CASE_IDCODE(SPK);
445     CASE_IDCODE(SO);
446     CASE_IDCODE(TE);
447     CASE_IDCODE(TXT);
448     CASE_IDCODE(VF);
449     CASE_IDCODE(VO);
450     CASE_IDCODE(WM);
451     CASE_IDCODE(WO);
452     CASE_IDCODE(WS);
453   }
454 
455   /* Special naughty boy... */
456   if (index == INDEX_ID_NULL) {
457     return ID_LINK_PLACEHOLDER;
458   }
459 
460   return -1;
461 
462 #undef CASE_IDCODE
463 }
464 
465 /**
466  * Return an ID code and steps the index forward 1.
467  *
468  * \param index: start as 0.
469  * \return the code, 0 when all codes have been returned.
470  */
BKE_idtype_idcode_iter_step(int * index)471 short BKE_idtype_idcode_iter_step(int *index)
472 {
473   return (*index < ARRAY_SIZE(id_types)) ? BKE_idtype_idcode_from_index((*index)++) : 0;
474 }
475 
476 /**
477  * Wrapper around #IDTypeInfo foreach_cache that also handles embedded IDs.
478  */
BKE_idtype_id_foreach_cache(struct ID * id,IDTypeForeachCacheFunctionCallback function_callback,void * user_data)479 void BKE_idtype_id_foreach_cache(struct ID *id,
480                                  IDTypeForeachCacheFunctionCallback function_callback,
481                                  void *user_data)
482 {
483   const IDTypeInfo *type_info = BKE_idtype_get_info_from_id(id);
484   if (type_info->foreach_cache != NULL) {
485     type_info->foreach_cache(id, function_callback, user_data);
486   }
487 
488   /* Handle 'private IDs'. */
489   bNodeTree *nodetree = ntreeFromID(id);
490   if (nodetree != NULL) {
491     type_info = BKE_idtype_get_info_from_id(&nodetree->id);
492     if (type_info == NULL) {
493       /* Very old .blend file seem to have empty names for their embedded node trees, see
494        * `blo_do_versions_250()`. Assume those are nodetrees then. */
495       type_info = BKE_idtype_get_info_from_idcode(ID_NT);
496     }
497     if (type_info->foreach_cache != NULL) {
498       type_info->foreach_cache(&nodetree->id, function_callback, user_data);
499     }
500   }
501 
502   if (GS(id->name) == ID_SCE) {
503     Scene *scene = (Scene *)id;
504     if (scene->master_collection != NULL) {
505       type_info = BKE_idtype_get_info_from_id(&scene->master_collection->id);
506       if (type_info->foreach_cache != NULL) {
507         type_info->foreach_cache(&scene->master_collection->id, function_callback, user_data);
508       }
509     }
510   }
511 }
512