Merge branch 'blender2.7'
[blender.git] / source / blender / blenloader / intern / readfile.c
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) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup blenloader
22  */
23
24 #include "zlib.h"
25
26 #include <limits.h>
27 #include <stdio.h> // for printf fopen fwrite fclose sprintf FILE
28 #include <stdlib.h> // for getenv atoi
29 #include <stddef.h> // for offsetof
30 #include <fcntl.h> // for open
31 #include <string.h> // for strrchr strncmp strstr
32 #include <math.h> // for fabs
33 #include <stdarg.h> /* for va_start/end */
34 #include <time.h> /* for gmtime */
35 #include <ctype.h> /* for isdigit */
36
37 #include "BLI_utildefines.h"
38 #ifndef WIN32
39 #  include <unistd.h> // for read close
40 #else
41 #  include <io.h> // for open close read
42 #  include "winsock2.h"
43 #  include "BLI_winstuff.h"
44 #endif
45
46 /* allow readfile to use deprecated functionality */
47 #define DNA_DEPRECATED_ALLOW
48 /* Allow using DNA struct members that are marked as private for read/write.
49  * Note: Each header that uses this needs to define its own way of handling
50  * it. There's no generic implementation, direct use does nothing. */
51 #define DNA_PRIVATE_READ_WRITE_ALLOW
52
53 #include "DNA_anim_types.h"
54 #include "DNA_armature_types.h"
55 #include "DNA_brush_types.h"
56 #include "DNA_camera_types.h"
57 #include "DNA_cachefile_types.h"
58 #include "DNA_cloth_types.h"
59 #include "DNA_collection_types.h"
60 #include "DNA_constraint_types.h"
61 #include "DNA_dynamicpaint_types.h"
62 #include "DNA_effect_types.h"
63 #include "DNA_fileglobal_types.h"
64 #include "DNA_genfile.h"
65 #include "DNA_gpencil_types.h"
66 #include "DNA_gpencil_modifier_types.h"
67 #include "DNA_shader_fx_types.h"
68 #include "DNA_ipo_types.h"
69 #include "DNA_key_types.h"
70 #include "DNA_lattice_types.h"
71 #include "DNA_layer_types.h"
72 #include "DNA_lamp_types.h"
73 #include "DNA_linestyle_types.h"
74 #include "DNA_meta_types.h"
75 #include "DNA_material_types.h"
76 #include "DNA_mesh_types.h"
77 #include "DNA_meshdata_types.h"
78 #include "DNA_nla_types.h"
79 #include "DNA_node_types.h"
80 #include "DNA_object_fluidsim_types.h"
81 #include "DNA_object_types.h"
82 #include "DNA_packedFile_types.h"
83 #include "DNA_particle_types.h"
84 #include "DNA_lightprobe_types.h"
85 #include "DNA_rigidbody_types.h"
86 #include "DNA_text_types.h"
87 #include "DNA_view3d_types.h"
88 #include "DNA_screen_types.h"
89 #include "DNA_sdna_types.h"
90 #include "DNA_scene_types.h"
91 #include "DNA_sequence_types.h"
92 #include "DNA_smoke_types.h"
93 #include "DNA_speaker_types.h"
94 #include "DNA_sound_types.h"
95 #include "DNA_space_types.h"
96 #include "DNA_vfont_types.h"
97 #include "DNA_workspace_types.h"
98 #include "DNA_world_types.h"
99 #include "DNA_movieclip_types.h"
100 #include "DNA_mask_types.h"
101
102 #include "RNA_access.h"
103
104 #include "MEM_guardedalloc.h"
105
106 #include "BLI_endian_switch.h"
107 #include "BLI_blenlib.h"
108 #include "BLI_math.h"
109 #include "BLI_threads.h"
110 #include "BLI_mempool.h"
111 #include "BLI_ghash.h"
112
113 #include "BLT_translation.h"
114
115 #include "BKE_action.h"
116 #include "BKE_armature.h"
117 #include "BKE_brush.h"
118 #include "BKE_cachefile.h"
119 #include "BKE_cloth.h"
120 #include "BKE_collection.h"
121 #include "BKE_colortools.h"
122 #include "BKE_constraint.h"
123 #include "BKE_curve.h"
124 #include "BKE_effect.h"
125 #include "BKE_fcurve.h"
126 #include "BKE_global.h" // for G
127 #include "BKE_gpencil.h"
128 #include "BKE_gpencil_modifier.h"
129 #include "BKE_idcode.h"
130 #include "BKE_idprop.h"
131 #include "BKE_layer.h"
132 #include "BKE_library.h"
133 #include "BKE_library_idmap.h"
134 #include "BKE_library_override.h"
135 #include "BKE_library_query.h"
136 #include "BKE_main.h" // for Main
137 #include "BKE_material.h"
138 #include "BKE_mesh.h" // for ME_ defines (patching)
139 #include "BKE_mesh_runtime.h"
140 #include "BKE_modifier.h"
141 #include "BKE_multires.h"
142 #include "BKE_node.h" // for tree type defines
143 #include "BKE_object.h"
144 #include "BKE_ocean.h"
145 #include "BKE_outliner_treehash.h"
146 #include "BKE_paint.h"
147 #include "BKE_particle.h"
148 #include "BKE_pointcache.h"
149 #include "BKE_report.h"
150 #include "BKE_scene.h"
151 #include "BKE_screen.h"
152 #include "BKE_sequencer.h"
153 #include "BKE_shader_fx.h"
154 #include "BKE_sound.h"
155 #include "BKE_workspace.h"
156
157 #include "DRW_engine.h"
158
159 #include "DEG_depsgraph.h"
160
161 #include "NOD_common.h"
162 #include "NOD_socket.h"
163
164 #include "BLO_blend_defs.h"
165 #include "BLO_blend_validate.h"
166 #include "BLO_readfile.h"
167 #include "BLO_undofile.h"
168
169 #include "RE_engine.h"
170
171 #include "readfile.h"
172
173 #include <errno.h>
174
175 /**
176  * READ
177  * ====
178  *
179  * - Existing Library (#Main) push or free
180  * - allocate new #Main
181  * - load file
182  * - read #SDNA
183  * - for each LibBlock
184  *   - read LibBlock
185  *   - if a Library
186  *     - make a new #Main
187  *     - attach ID's to it
188  *   - else
189  *     - read associated 'direct data'
190  *     - link direct data (internal and to LibBlock)
191  * - read #FileGlobal
192  * - read #USER data, only when indicated (file is ``~/X.XX/startup.blend``)
193  * - free file
194  * - per Library (per #Main)
195  *   - read file
196  *   - read #SDNA
197  *   - find LibBlocks and attach #ID's to #Main
198  *     - if external LibBlock
199  *       - search all #Main's
200  *         - or it's already read,
201  *         - or not read yet
202  *         - or make new #Main
203  *   - per LibBlock
204  *     - read recursive
205  *     - read associated direct data
206  *     - link direct data (internal and to LibBlock)
207  *   - free file
208  * - per Library with unread LibBlocks
209  *   - read file
210  *   - read #SDNA
211  *   - per LibBlock
212  *     - read recursive
213  *     - read associated direct data
214  *     - link direct data (internal and to LibBlock)
215  *   - free file
216  * - join all #Main's
217  * - link all LibBlocks and indirect pointers to libblocks
218  * - initialize #FileGlobal and copy pointers to #Global
219  *
220  * \note Still a weak point is the new-address function, that doesnt solve reading from
221  * multiple files at the same time.
222  * (added remark: oh, i thought that was solved? will look at that... (ton).
223  */
224
225 /**
226  * Delay reading blocks we might not use (especially applies to library linking).
227  * which keeps large arrays in memory from data-blocks we may not even use.
228  *
229  * \note This is disabled when using compression,
230  * while zlib supports seek ist's unusably slow, see: T61880.
231  */
232 #define USE_BHEAD_READ_ON_DEMAND
233
234 /* use GHash for BHead name-based lookups (speeds up linking) */
235 #define USE_GHASH_BHEAD
236
237 /* Use GHash for restoring pointers by name */
238 #define USE_GHASH_RESTORE_POINTER
239
240 /* Define this to have verbose debug prints. */
241 #define USE_DEBUG_PRINT
242
243 #ifdef USE_DEBUG_PRINT
244 #  define DEBUG_PRINTF(...) printf(__VA_ARGS__)
245 #else
246 #  define DEBUG_PRINTF(...)
247 #endif
248
249
250 /* local prototypes */
251 static void read_libraries(FileData *basefd, ListBase *mainlist);
252 static void *read_struct(FileData *fd, BHead *bh, const char *blockname);
253 static void direct_link_modifiers(FileData *fd, ListBase *lb);
254 static BHead *find_bhead_from_code_name(FileData *fd, const short idcode, const char *name);
255 static BHead *find_bhead_from_idname(FileData *fd, const char *idname);
256
257 #ifdef USE_COLLECTION_COMPAT_28
258 static void expand_scene_collection(FileData *fd, Main *mainvar, SceneCollection *sc);
259 #endif
260 static void direct_link_animdata(FileData *fd, AnimData *adt);
261 static void lib_link_animdata(FileData *fd, ID *id, AnimData *adt);
262
263 typedef struct BHeadN {
264         struct BHeadN *next, *prev;
265 #ifdef USE_BHEAD_READ_ON_DEMAND
266         /** Use to read the data from the file directly into memory as needed. */
267         off_t file_offset;
268         /** When set, the remainder of this allocation is the data, otherwise it needs to be read. */
269         bool has_data;
270 #endif
271         struct BHead bhead;
272 } BHeadN;
273
274 #define BHEADN_FROM_BHEAD(bh) ((BHeadN *)POINTER_OFFSET(bh, -offsetof(BHeadN, bhead)))
275
276 /* We could change this in the future, for now it's simplest if only data is delayed
277  * because ID names are used in lookup tables. */
278 #define BHEAD_USE_READ_ON_DEMAND(bhead) \
279         ((bhead)->code == DATA)
280
281 /* this function ensures that reports are printed,
282  * in the case of libraray linking errors this is important!
283  *
284  * bit kludge but better then doubling up on prints,
285  * we could alternatively have a versions of a report function which forces printing - campbell
286  */
287
288 void blo_reportf_wrap(ReportList *reports, ReportType type, const char *format, ...)
289 {
290         char fixed_buf[1024]; /* should be long enough */
291
292         va_list args;
293
294         va_start(args, format);
295         vsnprintf(fixed_buf, sizeof(fixed_buf), format, args);
296         va_end(args);
297
298         fixed_buf[sizeof(fixed_buf) - 1] = '\0';
299
300         BKE_report(reports, type, fixed_buf);
301
302         if (G.background == 0) {
303                 printf("%s: %s\n", BKE_report_type_str(type), fixed_buf);
304         }
305 }
306
307 /* for reporting linking messages */
308 static const char *library_parent_filepath(Library *lib)
309 {
310         return lib->parent ? lib->parent->filepath : "<direct>";
311 }
312
313 /* -------------------------------------------------------------------- */
314 /** \name OldNewMap API
315  * \{ */
316
317 typedef struct OldNew {
318         const void *oldp;
319         void *newp;
320         /* `nr` is "user count" for data, and ID code for libdata. */
321         int nr;
322 } OldNew;
323
324 typedef struct OldNewMap {
325         /* Array that stores the actual entries. */
326         OldNew *entries;
327         int nentries;
328         /* Hashmap that stores indices into the `entries` array. */
329         int32_t *map;
330
331         int capacity_exp;
332 } OldNewMap;
333
334 #define ENTRIES_CAPACITY(onm) (1 << (onm)->capacity_exp)
335 #define MAP_CAPACITY(onm) (1 << ((onm)->capacity_exp + 1))
336 #define SLOT_MASK(onm) (MAP_CAPACITY(onm) - 1)
337 #define DEFAULT_SIZE_EXP 6
338 #define PERTURB_SHIFT 5
339
340 /* based on the probing algorithm used in Python dicts. */
341 #define ITER_SLOTS(onm, KEY, SLOT_NAME, INDEX_NAME) \
342         uint32_t hash = BLI_ghashutil_ptrhash(KEY); \
343         uint32_t mask = SLOT_MASK(onm); \
344         uint perturb = hash; \
345         int SLOT_NAME = mask & hash; \
346         int INDEX_NAME = onm->map[SLOT_NAME]; \
347         for (;;SLOT_NAME = mask & ((5 * SLOT_NAME) + 1 + perturb), perturb >>= PERTURB_SHIFT, INDEX_NAME = onm->map[SLOT_NAME])
348
349 static void oldnewmap_insert_index_in_map(OldNewMap *onm, const void *ptr, int index)
350 {
351         ITER_SLOTS(onm, ptr, slot, stored_index) {
352                 if (stored_index == -1) {
353                         onm->map[slot] = index;
354                         break;
355                 }
356         }
357 }
358
359 static void oldnewmap_insert_or_replace(OldNewMap *onm, OldNew entry)
360 {
361         ITER_SLOTS(onm, entry.oldp, slot, index) {
362                 if (index == -1) {
363                         onm->entries[onm->nentries] = entry;
364                         onm->map[slot] = onm->nentries;
365                         onm->nentries++;
366                         break;
367                 }
368                 else if (onm->entries[index].oldp == entry.oldp) {
369                         onm->entries[index] = entry;
370                         break;
371                 }
372         }
373 }
374
375 static OldNew *oldnewmap_lookup_entry(const OldNewMap *onm, const void *addr)
376 {
377         ITER_SLOTS(onm, addr, slot, index) {
378                 if (index >= 0) {
379                         OldNew *entry = &onm->entries[index];
380                         if (entry->oldp == addr) {
381                                 return entry;
382                         }
383                 }
384                 else {
385                         return NULL;
386                 }
387         }
388 }
389
390 static void oldnewmap_clear_map(OldNewMap *onm)
391 {
392         memset(onm->map, 0xFF, MAP_CAPACITY(onm) * sizeof(*onm->map));
393 }
394
395 static void oldnewmap_increase_size(OldNewMap *onm)
396 {
397         onm->capacity_exp++;
398         onm->entries = MEM_reallocN(onm->entries, sizeof(*onm->entries) * ENTRIES_CAPACITY(onm));
399         onm->map = MEM_reallocN(onm->map, sizeof(*onm->map) * MAP_CAPACITY(onm));
400         oldnewmap_clear_map(onm);
401         for (int i = 0; i < onm->nentries; i++) {
402                 oldnewmap_insert_index_in_map(onm, onm->entries[i].oldp, i);
403         }
404 }
405
406
407 /* Public OldNewMap API */
408
409 static OldNewMap *oldnewmap_new(void)
410 {
411         OldNewMap *onm = MEM_callocN(sizeof(*onm), "OldNewMap");
412
413         onm->capacity_exp = DEFAULT_SIZE_EXP;
414         onm->entries = MEM_malloc_arrayN(ENTRIES_CAPACITY(onm), sizeof(*onm->entries), "OldNewMap.entries");
415         onm->map = MEM_malloc_arrayN(MAP_CAPACITY(onm), sizeof(*onm->map), "OldNewMap.map");
416         oldnewmap_clear_map(onm);
417
418         return onm;
419 }
420
421 static void oldnewmap_insert(OldNewMap *onm, const void *oldaddr, void *newaddr, int nr)
422 {
423         if (oldaddr == NULL || newaddr == NULL) return;
424
425         if (UNLIKELY(onm->nentries == ENTRIES_CAPACITY(onm))) {
426                 oldnewmap_increase_size(onm);
427         }
428
429         OldNew entry;
430         entry.oldp = oldaddr;
431         entry.newp = newaddr;
432         entry.nr = nr;
433         oldnewmap_insert_or_replace(onm, entry);
434 }
435
436 void blo_do_versions_oldnewmap_insert(OldNewMap *onm, const void *oldaddr, void *newaddr, int nr)
437 {
438         oldnewmap_insert(onm, oldaddr, newaddr, nr);
439 }
440
441 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, const void *addr, bool increase_users)
442 {
443         OldNew *entry = oldnewmap_lookup_entry(onm, addr);
444         if (entry == NULL) return NULL;
445         if (increase_users) entry->nr++;
446         return entry->newp;
447 }
448
449 /* for libdata, OldNew.nr has ID code, no increment */
450 static void *oldnewmap_liblookup(OldNewMap *onm, const void *addr, const void *lib)
451 {
452         if (addr == NULL) return NULL;
453
454         ID *id = oldnewmap_lookup_and_inc(onm, addr, false);
455         if (id == NULL) return NULL;
456         if (!lib || id->lib) return id;
457         return NULL;
458 }
459
460 static void oldnewmap_free_unused(OldNewMap *onm)
461 {
462         for (int i = 0; i < onm->nentries; i++) {
463                 OldNew *entry = &onm->entries[i];
464                 if (entry->nr == 0) {
465                         MEM_freeN(entry->newp);
466                         entry->newp = NULL;
467                 }
468         }
469 }
470
471 static void oldnewmap_clear(OldNewMap *onm)
472 {
473         onm->capacity_exp = DEFAULT_SIZE_EXP;
474         oldnewmap_clear_map(onm);
475         onm->nentries = 0;
476 }
477
478 static void oldnewmap_free(OldNewMap *onm)
479 {
480         MEM_freeN(onm->entries);
481         MEM_freeN(onm->map);
482         MEM_freeN(onm);
483 }
484
485 #undef ENTRIES_CAPACITY
486 #undef MAP_CAPACITY
487 #undef SLOT_MASK
488 #undef DEFAULT_SIZE_EXP
489 #undef PERTURB_SHIFT
490 #undef ITER_SLOTS
491
492 /** \} */
493
494 /* -------------------------------------------------------------------- */
495 /** \name Helper Functions
496  * \{ */
497
498 static void add_main_to_main(Main *mainvar, Main *from)
499 {
500         ListBase *lbarray[MAX_LIBARRAY], *fromarray[MAX_LIBARRAY];
501         int a;
502
503         set_listbasepointers(mainvar, lbarray);
504         a = set_listbasepointers(from, fromarray);
505         while (a--) {
506                 BLI_movelisttolist(lbarray[a], fromarray[a]);
507         }
508 }
509
510 void blo_join_main(ListBase *mainlist)
511 {
512         Main *tojoin, *mainl;
513
514         mainl = mainlist->first;
515         while ((tojoin = mainl->next)) {
516                 add_main_to_main(mainl, tojoin);
517                 BLI_remlink(mainlist, tojoin);
518                 BKE_main_free(tojoin);
519         }
520 }
521
522 static void split_libdata(ListBase *lb_src, Main **lib_main_array, const uint lib_main_array_len)
523 {
524         for (ID *id = lb_src->first, *idnext; id; id = idnext) {
525                 idnext = id->next;
526
527                 if (id->lib) {
528                         if (((uint)id->lib->temp_index < lib_main_array_len) &&
529                             /* this check should never fail, just incase 'id->lib' is a dangling pointer. */
530                             (lib_main_array[id->lib->temp_index]->curlib == id->lib))
531                         {
532                                 Main *mainvar = lib_main_array[id->lib->temp_index];
533                                 ListBase *lb_dst = which_libbase(mainvar, GS(id->name));
534                                 BLI_remlink(lb_src, id);
535                                 BLI_addtail(lb_dst, id);
536                         }
537                         else {
538                                 printf("%s: invalid library for '%s'\n", __func__, id->name);
539                                 BLI_assert(0);
540                         }
541                 }
542         }
543 }
544
545 void blo_split_main(ListBase *mainlist, Main *main)
546 {
547         mainlist->first = mainlist->last = main;
548         main->next = NULL;
549
550         if (BLI_listbase_is_empty(&main->library))
551                 return;
552
553         /* (Library.temp_index -> Main), lookup table */
554         const uint lib_main_array_len = BLI_listbase_count(&main->library);
555         Main     **lib_main_array     = MEM_malloc_arrayN(lib_main_array_len, sizeof(*lib_main_array), __func__);
556
557         int i = 0;
558         for (Library *lib = main->library.first; lib; lib = lib->id.next, i++) {
559                 Main *libmain = BKE_main_new();
560                 libmain->curlib = lib;
561                 libmain->versionfile = lib->versionfile;
562                 libmain->subversionfile = lib->subversionfile;
563                 BLI_addtail(mainlist, libmain);
564                 lib->temp_index = i;
565                 lib_main_array[i] = libmain;
566         }
567
568         ListBase *lbarray[MAX_LIBARRAY];
569         i = set_listbasepointers(main, lbarray);
570         while (i--) {
571                 ID *id = lbarray[i]->first;
572                 if (id == NULL || GS(id->name) == ID_LI) {
573                         continue;  /* no ID_LI datablock should ever be linked anyway, but just in case, better be explicit. */
574                 }
575                 split_libdata(lbarray[i], lib_main_array, lib_main_array_len);
576         }
577
578         MEM_freeN(lib_main_array);
579 }
580
581 static void read_file_version(FileData *fd, Main *main)
582 {
583         BHead *bhead;
584
585         for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
586                 if (bhead->code == GLOB) {
587                         FileGlobal *fg = read_struct(fd, bhead, "Global");
588                         if (fg) {
589                                 main->subversionfile = fg->subversion;
590                                 main->minversionfile = fg->minversion;
591                                 main->minsubversionfile = fg->minsubversion;
592                                 MEM_freeN(fg);
593                         }
594                         else if (bhead->code == ENDB)
595                                 break;
596                 }
597         }
598         if (main->curlib) {
599                 main->curlib->versionfile = main->versionfile;
600                 main->curlib->subversionfile = main->subversionfile;
601         }
602 }
603
604 #ifdef USE_GHASH_BHEAD
605 static void read_file_bhead_idname_map_create(FileData *fd)
606 {
607         BHead *bhead;
608
609         /* dummy values */
610         bool is_link = false;
611         int code_prev = ENDB;
612         uint reserve = 0;
613
614         for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
615                 if (code_prev != bhead->code) {
616                         code_prev = bhead->code;
617                         is_link = BKE_idcode_is_valid(code_prev) ? BKE_idcode_is_linkable(code_prev) : false;
618                 }
619
620                 if (is_link) {
621                         reserve += 1;
622                 }
623         }
624
625         BLI_assert(fd->bhead_idname_hash == NULL);
626
627         fd->bhead_idname_hash = BLI_ghash_str_new_ex(__func__, reserve);
628
629         for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
630                 if (code_prev != bhead->code) {
631                         code_prev = bhead->code;
632                         is_link = BKE_idcode_is_valid(code_prev) ? BKE_idcode_is_linkable(code_prev) : false;
633                 }
634
635                 if (is_link) {
636                         BLI_ghash_insert(fd->bhead_idname_hash, (void *)blo_bhead_id_name(fd, bhead), bhead);
637                 }
638         }
639 }
640 #endif
641
642
643 static Main *blo_find_main(FileData *fd, const char *filepath, const char *relabase)
644 {
645         ListBase *mainlist = fd->mainlist;
646         Main *m;
647         Library *lib;
648         char name1[FILE_MAX];
649
650         BLI_strncpy(name1, filepath, sizeof(name1));
651         BLI_cleanup_path(relabase, name1);
652
653 //      printf("blo_find_main: relabase  %s\n", relabase);
654 //      printf("blo_find_main: original in  %s\n", filepath);
655 //      printf("blo_find_main: converted to %s\n", name1);
656
657         for (m = mainlist->first; m; m = m->next) {
658                 const char *libname = (m->curlib) ? m->curlib->filepath : m->name;
659
660                 if (BLI_path_cmp(name1, libname) == 0) {
661                         if (G.debug & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
662                         return m;
663                 }
664         }
665
666         m = BKE_main_new();
667         BLI_addtail(mainlist, m);
668
669         /* Add library datablock itself to 'main' Main, since libraries are **never** linked data.
670          * Fixes bug where you could end with all ID_LI datablocks having the same name... */
671         lib = BKE_libblock_alloc(mainlist->first, ID_LI, BLI_path_basename(filepath), 0);
672         lib->id.us = ID_FAKE_USERS(lib);  /* Important, consistency with main ID reading code from read_libblock(). */
673         BLI_strncpy(lib->name, filepath, sizeof(lib->name));
674         BLI_strncpy(lib->filepath, name1, sizeof(lib->filepath));
675
676         m->curlib = lib;
677
678         read_file_version(fd, m);
679
680         if (G.debug & G_DEBUG) printf("blo_find_main: added new lib %s\n", filepath);
681         return m;
682 }
683
684 /** \} */
685
686 /* -------------------------------------------------------------------- */
687 /** \name File Parsing
688  * \{ */
689
690 static void switch_endian_bh4(BHead4 *bhead)
691 {
692         /* the ID_.. codes */
693         if ((bhead->code & 0xFFFF) == 0) bhead->code >>= 16;
694
695         if (bhead->code != ENDB) {
696                 BLI_endian_switch_int32(&bhead->len);
697                 BLI_endian_switch_int32(&bhead->SDNAnr);
698                 BLI_endian_switch_int32(&bhead->nr);
699         }
700 }
701
702 static void switch_endian_bh8(BHead8 *bhead)
703 {
704         /* the ID_.. codes */
705         if ((bhead->code & 0xFFFF) == 0) bhead->code >>= 16;
706
707         if (bhead->code != ENDB) {
708                 BLI_endian_switch_int32(&bhead->len);
709                 BLI_endian_switch_int32(&bhead->SDNAnr);
710                 BLI_endian_switch_int32(&bhead->nr);
711         }
712 }
713
714 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
715 {
716         BHead4 *bhead4 = (BHead4 *)bhead;
717         int64_t old;
718
719         bhead4->code = bhead8->code;
720         bhead4->len = bhead8->len;
721
722         if (bhead4->code != ENDB) {
723                 /* perform a endian swap on 64bit pointers, otherwise the pointer might map to zero
724                  * 0x0000000000000000000012345678 would become 0x12345678000000000000000000000000
725                  */
726                 if (do_endian_swap) {
727                         BLI_endian_switch_int64(&bhead8->old);
728                 }
729
730                 /* this patch is to avoid a long long being read from not-eight aligned positions
731                  * is necessary on any modern 64bit architecture) */
732                 memcpy(&old, &bhead8->old, 8);
733                 bhead4->old = (int)(old >> 3);
734
735                 bhead4->SDNAnr = bhead8->SDNAnr;
736                 bhead4->nr = bhead8->nr;
737         }
738 }
739
740 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
741 {
742         BHead8 *bhead8 = (BHead8 *)bhead;
743
744         bhead8->code = bhead4->code;
745         bhead8->len = bhead4->len;
746
747         if (bhead8->code != ENDB) {
748                 bhead8->old = bhead4->old;
749                 bhead8->SDNAnr = bhead4->SDNAnr;
750                 bhead8->nr = bhead4->nr;
751         }
752 }
753
754 static BHeadN *get_bhead(FileData *fd)
755 {
756         BHeadN *new_bhead = NULL;
757         int readsize;
758
759         if (fd) {
760                 if (!fd->is_eof) {
761                         /* initializing to zero isn't strictly needed but shuts valgrind up
762                          * since uninitialized memory gets compared */
763                         BHead8 bhead8 = {0};
764                         BHead4 bhead4 = {0};
765                         BHead bhead = {0};
766
767                         /* First read the bhead structure.
768                          * Depending on the platform the file was written on this can
769                          * be a big or little endian BHead4 or BHead8 structure.
770                          *
771                          * As usual 'ENDB' (the last *partial* bhead of the file)
772                          * needs some special handling. We don't want to EOF just yet.
773                          */
774                         if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
775                                 bhead4.code = DATA;
776                                 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
777
778                                 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
779                                         if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
780                                                 switch_endian_bh4(&bhead4);
781                                         }
782
783                                         if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
784                                                 bh8_from_bh4(&bhead, &bhead4);
785                                         }
786                                         else {
787                                                 /* MIN2 is only to quiet '-Warray-bounds' compiler warning. */
788                                                 BLI_assert(sizeof(bhead) == sizeof(bhead4));
789                                                 memcpy(&bhead, &bhead4, MIN2(sizeof(bhead), sizeof(bhead4)));
790                                         }
791                                 }
792                                 else {
793                                         fd->is_eof = true;
794                                         bhead.len = 0;
795                                 }
796                         }
797                         else {
798                                 bhead8.code = DATA;
799                                 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
800
801                                 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
802                                         if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
803                                                 switch_endian_bh8(&bhead8);
804                                         }
805
806                                         if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
807                                                 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
808                                         }
809                                         else {
810                                                 /* MIN2 is only to quiet '-Warray-bounds' compiler warning. */
811                                                 BLI_assert(sizeof(bhead) == sizeof(bhead8));
812                                                 memcpy(&bhead, &bhead8, MIN2(sizeof(bhead), sizeof(bhead8)));
813                                         }
814                                 }
815                                 else {
816                                         fd->is_eof = true;
817                                         bhead.len = 0;
818                                 }
819                         }
820
821                         /* make sure people are not trying to pass bad blend files */
822                         if (bhead.len < 0) {
823                                 fd->is_eof = true;
824                         }
825
826                         /* bhead now contains the (converted) bhead structure. Now read
827                          * the associated data and put everything in a BHeadN (creative naming !)
828                          */
829                         if (fd->is_eof) {
830                                 /* pass */
831                         }
832 #ifdef USE_BHEAD_READ_ON_DEMAND
833                         else if (fd->seek != NULL && BHEAD_USE_READ_ON_DEMAND(&bhead)) {
834                                 /* Delay reading bhead content. */
835                                 new_bhead = MEM_mallocN(sizeof(BHeadN), "new_bhead");
836                                 if (new_bhead) {
837                                         new_bhead->next = new_bhead->prev = NULL;
838                                         new_bhead->file_offset = fd->file_offset;
839                                         new_bhead->has_data = false;
840                                         new_bhead->bhead = bhead;
841                                         off_t seek_new = fd->seek(fd, bhead.len, SEEK_CUR);
842                                         if (seek_new == -1) {
843                                                 fd->is_eof = true;
844                                                 MEM_freeN(new_bhead);
845                                                 new_bhead = NULL;
846                                         }
847                                         BLI_assert(fd->file_offset == seek_new);
848                                 }
849                                 else {
850                                         fd->is_eof = true;
851                                 }
852                         }
853 #endif
854                         else {
855                                 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
856                                 if (new_bhead) {
857                                         new_bhead->next = new_bhead->prev = NULL;
858 #ifdef USE_BHEAD_READ_ON_DEMAND
859                                         new_bhead->file_offset = 0;  /* don't seek. */
860                                         new_bhead->has_data = true;
861 #endif
862                                         new_bhead->bhead = bhead;
863
864                                         readsize = fd->read(fd, new_bhead + 1, bhead.len);
865
866                                         if (readsize != bhead.len) {
867                                                 fd->is_eof = true;
868                                                 MEM_freeN(new_bhead);
869                                                 new_bhead = NULL;
870                                         }
871                                 }
872                                 else {
873                                         fd->is_eof = true;
874                                 }
875                         }
876                 }
877         }
878
879         /* We've read a new block. Now add it to the list
880          * of blocks.
881          */
882         if (new_bhead) {
883                 BLI_addtail(&fd->listbase, new_bhead);
884         }
885
886         return new_bhead;
887 }
888
889 BHead *blo_bhead_first(FileData *fd)
890 {
891         BHeadN *new_bhead;
892         BHead *bhead = NULL;
893
894         /* Rewind the file
895          * Read in a new block if necessary
896          */
897         new_bhead = fd->listbase.first;
898         if (new_bhead == NULL) {
899                 new_bhead = get_bhead(fd);
900         }
901
902         if (new_bhead) {
903                 bhead = &new_bhead->bhead;
904         }
905
906         return bhead;
907 }
908
909 BHead *blo_bhead_prev(FileData *UNUSED(fd), BHead *thisblock)
910 {
911         BHeadN *bheadn = BHEADN_FROM_BHEAD(thisblock);
912         BHeadN *prev = bheadn->prev;
913
914         return (prev) ? &prev->bhead : NULL;
915 }
916
917 BHead *blo_bhead_next(FileData *fd, BHead *thisblock)
918 {
919         BHeadN *new_bhead = NULL;
920         BHead *bhead = NULL;
921
922         if (thisblock) {
923                 /* bhead is actually a sub part of BHeadN
924                  * We calculate the BHeadN pointer from the BHead pointer below */
925                 new_bhead = BHEADN_FROM_BHEAD(thisblock);
926
927                 /* get the next BHeadN. If it doesn't exist we read in the next one */
928                 new_bhead = new_bhead->next;
929                 if (new_bhead == NULL) {
930                         new_bhead = get_bhead(fd);
931                 }
932         }
933
934         if (new_bhead) {
935                 /* here we do the reverse:
936                  * go from the BHeadN pointer to the BHead pointer */
937                 bhead = &new_bhead->bhead;
938         }
939
940         return bhead;
941 }
942
943 #ifdef USE_BHEAD_READ_ON_DEMAND
944 static bool blo_bhead_read_data(FileData *fd, BHead *thisblock, void *buf)
945 {
946         bool success = true;
947         BHeadN *new_bhead = BHEADN_FROM_BHEAD(thisblock);
948         BLI_assert(new_bhead->has_data == false && new_bhead->file_offset != 0);
949         off_t offset_backup = fd->file_offset;
950         if (UNLIKELY(fd->seek(fd, new_bhead->file_offset, SEEK_SET) == -1)) {
951                 success = false;
952         }
953         else {
954                 if (fd->read(fd, buf, new_bhead->bhead.len) != new_bhead->bhead.len) {
955                         success = false;
956                 }
957         }
958         if (fd->seek(fd, offset_backup, SEEK_SET) == -1) {
959                 success = false;
960         }
961         return success;
962 }
963
964 static BHead *blo_bhead_read_full(FileData *fd, BHead *thisblock)
965 {
966         BHeadN *new_bhead = BHEADN_FROM_BHEAD(thisblock);
967         BHeadN *new_bhead_data = MEM_mallocN(sizeof(BHeadN) + new_bhead->bhead.len, "new_bhead");
968         new_bhead_data->bhead = new_bhead->bhead;
969         new_bhead_data->file_offset = new_bhead->file_offset;
970         new_bhead_data->has_data = true;
971         if (!blo_bhead_read_data(fd, thisblock, new_bhead_data + 1)) {
972                 MEM_freeN(new_bhead_data);
973                 return NULL;
974         }
975         return &new_bhead_data->bhead;
976 }
977 #endif  /* USE_BHEAD_READ_ON_DEMAND */
978
979 /* Warning! Caller's responsibility to ensure given bhead **is** and ID one! */
980 const char *blo_bhead_id_name(const FileData *fd, const BHead *bhead)
981 {
982         return (const char *)POINTER_OFFSET(bhead, sizeof(*bhead) + fd->id_name_offs);
983 }
984
985 static void decode_blender_header(FileData *fd)
986 {
987         char header[SIZEOFBLENDERHEADER], num[4];
988         int readsize;
989
990         /* read in the header data */
991         readsize = fd->read(fd, header, sizeof(header));
992
993         if (readsize == sizeof(header) &&
994             STREQLEN(header, "BLENDER", 7) &&
995             ELEM(header[7], '_', '-') &&
996             ELEM(header[8], 'v', 'V') &&
997             (isdigit(header[9]) && isdigit(header[10]) && isdigit(header[11])))
998         {
999                 fd->flags |= FD_FLAGS_FILE_OK;
1000
1001                 /* what size are pointers in the file ? */
1002                 if (header[7] == '_') {
1003                         fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
1004                         if (sizeof(void *) != 4) {
1005                                 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
1006                         }
1007                 }
1008                 else {
1009                         if (sizeof(void *) != 8) {
1010                                 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
1011                         }
1012                 }
1013
1014                 /* is the file saved in a different endian
1015                  * than we need ?
1016                  */
1017                 if (((header[8] == 'v') ? L_ENDIAN : B_ENDIAN) != ENDIAN_ORDER) {
1018                         fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
1019                 }
1020
1021                 /* get the version number */
1022                 memcpy(num, header + 9, 3);
1023                 num[3] = 0;
1024                 fd->fileversion = atoi(num);
1025         }
1026 }
1027
1028 /**
1029  * \return Success if the file is read correctly, else set \a r_error_message.
1030  */
1031 static bool read_file_dna(FileData *fd, const char **r_error_message)
1032 {
1033         BHead *bhead;
1034         int subversion = 0;
1035
1036         for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
1037                 if (bhead->code == GLOB) {
1038                         /* Before this, the subversion didn't exist in 'FileGlobal' so the subversion
1039                          * value isn't accessible for the purpose of DNA versioning in this case. */
1040                         if (fd->fileversion <= 242) {
1041                                 continue;
1042                         }
1043                         /* We can't use read_global because this needs 'DNA1' to be decoded,
1044                          * however the first 4 chars are _always_ the subversion. */
1045                         FileGlobal *fg = (void *)&bhead[1];
1046                         BLI_STATIC_ASSERT(offsetof(FileGlobal, subvstr) == 0, "Must be first: subvstr");
1047                         char num[5];
1048                         memcpy(num, fg->subvstr, 4);
1049                         num[4] = 0;
1050                         subversion = atoi(num);
1051                 }
1052                 else if (bhead->code == DNA1) {
1053                         const bool do_endian_swap = (fd->flags & FD_FLAGS_SWITCH_ENDIAN) != 0;
1054
1055                         fd->filesdna = DNA_sdna_from_data(&bhead[1], bhead->len, do_endian_swap, true, r_error_message);
1056                         if (fd->filesdna) {
1057                                 blo_do_versions_dna(fd->filesdna, fd->fileversion, subversion);
1058                                 fd->compflags = DNA_struct_get_compareflags(fd->filesdna, fd->memsdna);
1059                                 /* used to retrieve ID names from (bhead+1) */
1060                                 fd->id_name_offs = DNA_elem_offset(fd->filesdna, "ID", "char", "name[]");
1061
1062                                 return true;
1063                         }
1064                         else {
1065                                 return false;
1066                         }
1067
1068                 }
1069                 else if (bhead->code == ENDB)
1070                         break;
1071         }
1072
1073         *r_error_message = "Missing DNA block";
1074         return false;
1075 }
1076
1077 static int *read_file_thumbnail(FileData *fd)
1078 {
1079         BHead *bhead;
1080         int *blend_thumb = NULL;
1081
1082         for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
1083                 if (bhead->code == TEST) {
1084                         const bool do_endian_swap = (fd->flags & FD_FLAGS_SWITCH_ENDIAN) != 0;
1085                         int *data = (int *)(bhead + 1);
1086
1087                         if (bhead->len < (2 * sizeof(int))) {
1088                                 break;
1089                         }
1090
1091                         if (do_endian_swap) {
1092                                 BLI_endian_switch_int32(&data[0]);
1093                                 BLI_endian_switch_int32(&data[1]);
1094                         }
1095
1096                         const int width = data[0];
1097                         const int height = data[1];
1098                         if (!BLEN_THUMB_MEMSIZE_IS_VALID(width, height)) {
1099                                 break;
1100                         }
1101                         if (bhead->len < BLEN_THUMB_MEMSIZE_FILE(width, height)) {
1102                                 break;
1103                         }
1104
1105                         blend_thumb = data;
1106                         break;
1107                 }
1108                 else if (bhead->code != REND) {
1109                         /* Thumbnail is stored in TEST immediately after first REND... */
1110                         break;
1111                 }
1112         }
1113
1114         return blend_thumb;
1115 }
1116
1117 /** \} */
1118
1119 /* -------------------------------------------------------------------- */
1120 /** \name File Data API
1121  * \{ */
1122
1123 /* Regular file reading. */
1124
1125 static int fd_read_data_from_file(FileData *filedata, void *buffer, uint size)
1126 {
1127         int readsize = read(filedata->filedes, buffer, size);
1128
1129         if (readsize < 0) {
1130                 readsize = EOF;
1131         }
1132         else {
1133                 filedata->file_offset += readsize;
1134         }
1135
1136         return (readsize);
1137 }
1138
1139 static off_t fd_seek_data_from_file(FileData *filedata, off_t offset, int whence)
1140 {
1141         filedata->file_offset = lseek(filedata->filedes, offset, whence);
1142         return filedata->file_offset;
1143 }
1144
1145 /* GZip file reading. */
1146
1147 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, uint size)
1148 {
1149         int readsize = gzread(filedata->gzfiledes, buffer, size);
1150
1151         if (readsize < 0) {
1152                 readsize = EOF;
1153         }
1154         else {
1155                 filedata->file_offset += readsize;
1156         }
1157
1158         return (readsize);
1159 }
1160
1161 /* Memory reading. */
1162
1163 static int fd_read_from_memory(FileData *filedata, void *buffer, uint size)
1164 {
1165         /* don't read more bytes then there are available in the buffer */
1166         int readsize = (int)MIN2(size, (uint)(filedata->buffersize - filedata->file_offset));
1167
1168         memcpy(buffer, filedata->buffer + filedata->file_offset, readsize);
1169         filedata->file_offset += readsize;
1170
1171         return (readsize);
1172 }
1173
1174 /* MemFile reading. */
1175
1176 static int fd_read_from_memfile(FileData *filedata, void *buffer, uint size)
1177 {
1178         static size_t seek = SIZE_MAX; /* the current position */
1179         static size_t offset = 0;      /* size of previous chunks */
1180         static MemFileChunk *chunk = NULL;
1181         size_t chunkoffset, readsize, totread;
1182
1183         if (size == 0) return 0;
1184
1185         if (seek != (size_t)filedata->file_offset) {
1186                 chunk = filedata->memfile->chunks.first;
1187                 seek = 0;
1188
1189                 while (chunk) {
1190                         if (seek + chunk->size > (size_t)filedata->file_offset) {
1191                                 break;
1192                         }
1193                         seek += chunk->size;
1194                         chunk = chunk->next;
1195                 }
1196                 offset = seek;
1197                 seek = filedata->file_offset;
1198         }
1199
1200         if (chunk) {
1201                 totread = 0;
1202
1203                 do {
1204                         /* first check if it's on the end if current chunk */
1205                         if (seek - offset == chunk->size) {
1206                                 offset += chunk->size;
1207                                 chunk = chunk->next;
1208                         }
1209
1210                         /* debug, should never happen */
1211                         if (chunk == NULL) {
1212                                 printf("illegal read, chunk zero\n");
1213                                 return 0;
1214                         }
1215
1216                         chunkoffset = seek - offset;
1217                         readsize = size - totread;
1218
1219                         /* data can be spread over multiple chunks, so clamp size
1220                          * to within this chunk, and then it will read further in
1221                          * the next chunk */
1222                         if (chunkoffset + readsize > chunk->size)
1223                                 readsize = chunk->size - chunkoffset;
1224
1225                         memcpy(POINTER_OFFSET(buffer, totread), chunk->buf + chunkoffset, readsize);
1226                         totread += readsize;
1227                         filedata->file_offset += readsize;
1228                         seek += readsize;
1229                 } while (totread < size);
1230
1231                 return totread;
1232         }
1233
1234         return 0;
1235 }
1236
1237 static FileData *filedata_new(void)
1238 {
1239         FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
1240
1241         fd->filedes = -1;
1242         fd->gzfiledes = NULL;
1243
1244         fd->memsdna = DNA_sdna_current_get();
1245
1246         fd->datamap = oldnewmap_new();
1247         fd->globmap = oldnewmap_new();
1248         fd->libmap = oldnewmap_new();
1249
1250         return fd;
1251 }
1252
1253 static FileData *blo_decode_and_check(FileData *fd, ReportList *reports)
1254 {
1255         decode_blender_header(fd);
1256
1257         if (fd->flags & FD_FLAGS_FILE_OK) {
1258                 const char *error_message = NULL;
1259                 if (read_file_dna(fd, &error_message) == false) {
1260                         BKE_reportf(reports, RPT_ERROR,
1261                                     "Failed to read blend file '%s': %s",
1262                                     fd->relabase, error_message);
1263                         blo_filedata_free(fd);
1264                         fd = NULL;
1265                 }
1266         }
1267         else {
1268                 BKE_reportf(reports, RPT_ERROR, "Failed to read blend file '%s', not a blend file", fd->relabase);
1269                 blo_filedata_free(fd);
1270                 fd = NULL;
1271         }
1272
1273         return fd;
1274 }
1275
1276 static FileData *blo_filedata_from_file_descriptor(const char *filepath, ReportList *reports, int file)
1277 {
1278         FileDataReadFn *read_fn = NULL;
1279         FileDataSeekFn *seek_fn = NULL;  /* Optional. */
1280
1281         gzFile gzfile = (gzFile)Z_NULL;
1282
1283         char header[7];
1284
1285         /* Regular file. */
1286         errno = 0;
1287         if (read(file, header, sizeof(header)) != sizeof(header)) {
1288                 BKE_reportf(reports, RPT_WARNING, "Unable to read '%s': %s",
1289                             filepath, errno ? strerror(errno) : TIP_("insufficient content"));
1290                 return NULL;
1291         }
1292         else {
1293                 lseek(file, 0, SEEK_SET);
1294         }
1295
1296         /* Regular file. */
1297         if (memcmp(header, "BLENDER", sizeof(header)) == 0) {
1298                 read_fn = fd_read_data_from_file;
1299                 seek_fn = fd_seek_data_from_file;
1300         }
1301
1302         /* Gzip file. */
1303         errno = 0;
1304         if ((read_fn == NULL) &&
1305             /* Check header magic. */
1306             (header[0] == 0x1f && header[1] == 0x8b))
1307         {
1308                 gzfile = BLI_gzopen(filepath, "rb");
1309                 if (gzfile == (gzFile)Z_NULL) {
1310                         BKE_reportf(reports, RPT_WARNING, "Unable to open '%s': %s",
1311                                     filepath, errno ? strerror(errno) : TIP_("unknown error reading file"));
1312                         return NULL;
1313                 }
1314                 else {
1315                         /* 'seek_fn' is too slow for gzip, don't set it. */
1316                         read_fn = fd_read_gzip_from_file;
1317                         /* Caller must close. */
1318                         file = -1;
1319                 }
1320         }
1321
1322         if (read_fn == NULL) {
1323                 BKE_reportf(reports, RPT_WARNING, "Unrecognized file format '%s'", filepath);
1324                 return NULL;
1325         }
1326
1327         FileData *fd = filedata_new();
1328
1329         fd->filedes = file;
1330         fd->gzfiledes = gzfile;
1331
1332         fd->read = read_fn;
1333         fd->seek = seek_fn;
1334
1335         return fd;
1336 }
1337
1338 static FileData *blo_filedata_from_file_open(const char *filepath, ReportList *reports)
1339 {
1340         errno = 0;
1341         const int file = BLI_open(filepath, O_BINARY | O_RDONLY, 0);
1342         if (file == -1) {
1343                 BKE_reportf(reports, RPT_WARNING, "Unable to open '%s': %s",
1344                             filepath, errno ? strerror(errno) : TIP_("unknown error reading file"));
1345                 return NULL;
1346         }
1347         FileData *fd = blo_filedata_from_file_descriptor(filepath, reports, file);
1348         if ((fd == NULL) || (fd->filedes == -1)) {
1349                 close(file);
1350         }
1351         return fd;
1352 }
1353
1354 /* cannot be called with relative paths anymore! */
1355 /* on each new library added, it now checks for the current FileData and expands relativeness */
1356 FileData *blo_filedata_from_file(const char *filepath, ReportList *reports)
1357 {
1358         FileData *fd = blo_filedata_from_file_open(filepath, reports);
1359         if (fd != NULL) {
1360                 /* needed for library_append and read_libraries */
1361                 BLI_strncpy(fd->relabase, filepath, sizeof(fd->relabase));
1362
1363                 return blo_decode_and_check(fd, reports);
1364         }
1365         return NULL;
1366 }
1367
1368 /**
1369  * Same as blo_filedata_from_file(), but does not reads DNA data, only header. Use it for light access
1370  * (e.g. thumbnail reading).
1371  */
1372 static FileData *blo_filedata_from_file_minimal(const char *filepath)
1373 {
1374         FileData *fd = blo_filedata_from_file_open(filepath, NULL);
1375         if (fd != NULL) {
1376                 decode_blender_header(fd);
1377                 if (fd->flags & FD_FLAGS_FILE_OK) {
1378                         return fd;
1379                 }
1380                 blo_filedata_free(fd);
1381         }
1382         return NULL;
1383 }
1384
1385 static int fd_read_gzip_from_memory(FileData *filedata, void *buffer, uint size)
1386 {
1387         int err;
1388
1389         filedata->strm.next_out = (Bytef *)buffer;
1390         filedata->strm.avail_out = size;
1391
1392         // Inflate another chunk.
1393         err = inflate(&filedata->strm, Z_SYNC_FLUSH);
1394
1395         if (err == Z_STREAM_END) {
1396                 return 0;
1397         }
1398         else if (err != Z_OK) {
1399                 printf("fd_read_gzip_from_memory: zlib error\n");
1400                 return 0;
1401         }
1402
1403         filedata->file_offset += size;
1404
1405         return (size);
1406 }
1407
1408 static int fd_read_gzip_from_memory_init(FileData *fd)
1409 {
1410
1411         fd->strm.next_in = (Bytef *)fd->buffer;
1412         fd->strm.avail_in = fd->buffersize;
1413         fd->strm.total_out = 0;
1414         fd->strm.zalloc = Z_NULL;
1415         fd->strm.zfree = Z_NULL;
1416
1417         if (inflateInit2(&fd->strm, (16 + MAX_WBITS)) != Z_OK)
1418                 return 0;
1419
1420         fd->read = fd_read_gzip_from_memory;
1421
1422         return 1;
1423 }
1424
1425 FileData *blo_filedata_from_memory(const void *mem, int memsize, ReportList *reports)
1426 {
1427         if (!mem || memsize < SIZEOFBLENDERHEADER) {
1428                 BKE_report(reports, RPT_WARNING, (mem) ? TIP_("Unable to read") : TIP_("Unable to open"));
1429                 return NULL;
1430         }
1431         else {
1432                 FileData *fd = filedata_new();
1433                 const char *cp = mem;
1434
1435                 fd->buffer = mem;
1436                 fd->buffersize = memsize;
1437
1438                 /* test if gzip */
1439                 if (cp[0] == 0x1f && cp[1] == 0x8b) {
1440                         if (0 == fd_read_gzip_from_memory_init(fd)) {
1441                                 blo_filedata_free(fd);
1442                                 return NULL;
1443                         }
1444                 }
1445                 else
1446                         fd->read = fd_read_from_memory;
1447
1448                 fd->flags |= FD_FLAGS_NOT_MY_BUFFER;
1449
1450                 return blo_decode_and_check(fd, reports);
1451         }
1452 }
1453
1454 FileData *blo_filedata_from_memfile(MemFile *memfile, ReportList *reports)
1455 {
1456         if (!memfile) {
1457                 BKE_report(reports, RPT_WARNING, "Unable to open blend <memory>");
1458                 return NULL;
1459         }
1460         else {
1461                 FileData *fd = filedata_new();
1462                 fd->memfile = memfile;
1463
1464                 fd->read = fd_read_from_memfile;
1465                 fd->flags |= FD_FLAGS_NOT_MY_BUFFER;
1466
1467                 return blo_decode_and_check(fd, reports);
1468         }
1469 }
1470
1471
1472 void blo_filedata_free(FileData *fd)
1473 {
1474         if (fd) {
1475                 if (fd->filedes != -1) {
1476                         close(fd->filedes);
1477                 }
1478
1479                 if (fd->gzfiledes != NULL) {
1480                         gzclose(fd->gzfiledes);
1481                 }
1482
1483                 if (fd->strm.next_in) {
1484                         if (inflateEnd(&fd->strm) != Z_OK) {
1485                                 printf("close gzip stream error\n");
1486                         }
1487                 }
1488
1489                 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
1490                         MEM_freeN((void *)fd->buffer);
1491                         fd->buffer = NULL;
1492                 }
1493
1494                 /* Free all BHeadN data blocks */
1495 #ifndef NDEBUG
1496                 BLI_freelistN(&fd->listbase);
1497 #else
1498                 /* Sanity check we're not keeping memory we don't need. */
1499                 LISTBASE_FOREACH_MUTABLE (BHeadN *, new_bhead, &fd->listbase) {
1500                         if (fd->seek != NULL && BHEAD_USE_READ_ON_DEMAND(&new_bhead->bhead)) {
1501                                 BLI_assert(new_bhead->has_data == 0);
1502                         }
1503                         MEM_freeN(new_bhead);
1504                 }
1505 #endif
1506
1507                 if (fd->filesdna)
1508                         DNA_sdna_free(fd->filesdna);
1509                 if (fd->compflags)
1510                         MEM_freeN((void *)fd->compflags);
1511
1512                 if (fd->datamap)
1513                         oldnewmap_free(fd->datamap);
1514                 if (fd->globmap)
1515                         oldnewmap_free(fd->globmap);
1516                 if (fd->imamap)
1517                         oldnewmap_free(fd->imamap);
1518                 if (fd->movieclipmap)
1519                         oldnewmap_free(fd->movieclipmap);
1520                 if (fd->scenemap)
1521                         oldnewmap_free(fd->scenemap);
1522                 if (fd->soundmap)
1523                         oldnewmap_free(fd->soundmap);
1524                 if (fd->packedmap)
1525                         oldnewmap_free(fd->packedmap);
1526                 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
1527                         oldnewmap_free(fd->libmap);
1528                 if (fd->bheadmap)
1529                         MEM_freeN(fd->bheadmap);
1530
1531 #ifdef USE_GHASH_BHEAD
1532                 if (fd->bhead_idname_hash) {
1533                         BLI_ghash_free(fd->bhead_idname_hash, NULL, NULL);
1534                 }
1535 #endif
1536
1537                 MEM_freeN(fd);
1538         }
1539 }
1540
1541 /** \} */
1542
1543 /* -------------------------------------------------------------------- */
1544 /** \name Public Utilities
1545  * \{ */
1546
1547 /**
1548  * Check whether given path ends with a blend file compatible extension
1549  * (`.blend`, `.ble` or `.blend.gz`).
1550  *
1551  * \param str: The path to check.
1552  * \return true is this path ends with a blender file extension.
1553  */
1554 bool BLO_has_bfile_extension(const char *str)
1555 {
1556         const char *ext_test[4] = {".blend", ".ble", ".blend.gz", NULL};
1557         return BLI_path_extension_check_array(str, ext_test);
1558 }
1559
1560 /**
1561  * Try to explode given path into its 'library components'
1562  * (i.e. a .blend file, id type/group, and data-block itself).
1563  *
1564  * \param path: the full path to explode.
1565  * \param r_dir: the string that'll contain path up to blend file itself ('library' path).
1566  * WARNING! Must be #FILE_MAX_LIBEXTRA long (it also stores group and name strings)!
1567  * \param r_group: the string that'll contain 'group' part of the path, if any. May be NULL.
1568  * \param r_name: the string that'll contain data's name part of the path, if any. May be NULL.
1569  * \return true if path contains a blend file.
1570  */
1571 bool BLO_library_path_explode(const char *path, char *r_dir, char **r_group, char **r_name)
1572 {
1573         /* We might get some data names with slashes, so we have to go up in path until we find blend file itself,
1574          * then we now next path item is group, and everything else is data name. */
1575         char *slash = NULL, *prev_slash = NULL, c = '\0';
1576
1577         r_dir[0] = '\0';
1578         if (r_group) {
1579                 *r_group = NULL;
1580         }
1581         if (r_name) {
1582                 *r_name = NULL;
1583         }
1584
1585         /* if path leads to an existing directory, we can be sure we're not (in) a library */
1586         if (BLI_is_dir(path)) {
1587                 return false;
1588         }
1589
1590         strcpy(r_dir, path);
1591
1592         while ((slash = (char *)BLI_last_slash(r_dir))) {
1593                 char tc = *slash;
1594                 *slash = '\0';
1595                 if (BLO_has_bfile_extension(r_dir) && BLI_is_file(r_dir)) {
1596                         break;
1597                 }
1598                 else if (STREQ(r_dir, BLO_EMBEDDED_STARTUP_BLEND)) {
1599                         break;
1600                 }
1601
1602                 if (prev_slash) {
1603                         *prev_slash = c;
1604                 }
1605                 prev_slash = slash;
1606                 c = tc;
1607         }
1608
1609         if (!slash) {
1610                 return false;
1611         }
1612
1613         if (slash[1] != '\0') {
1614                 BLI_assert(strlen(slash + 1) < BLO_GROUP_MAX);
1615                 if (r_group) {
1616                         *r_group = slash + 1;
1617                 }
1618         }
1619
1620         if (prev_slash && (prev_slash[1] != '\0')) {
1621                 BLI_assert(strlen(prev_slash + 1) < MAX_ID_NAME - 2);
1622                 if (r_name) {
1623                         *r_name = prev_slash + 1;
1624                 }
1625         }
1626
1627         return true;
1628 }
1629
1630 /**
1631  * Does a very light reading of given .blend file to extract its stored thumbnail.
1632  *
1633  * \param filepath: The path of the file to extract thumbnail from.
1634  * \return The raw thumbnail
1635  * (MEM-allocated, as stored in file, use BKE_main_thumbnail_to_imbuf() to convert it to ImBuf image).
1636  */
1637 BlendThumbnail *BLO_thumbnail_from_file(const char *filepath)
1638 {
1639         FileData *fd;
1640         BlendThumbnail *data = NULL;
1641         int *fd_data;
1642
1643         fd = blo_filedata_from_file_minimal(filepath);
1644         fd_data = fd ? read_file_thumbnail(fd) : NULL;
1645
1646         if (fd_data) {
1647                 const int width = fd_data[0];
1648                 const int height = fd_data[1];
1649                 if (BLEN_THUMB_MEMSIZE_IS_VALID(width, height)) {
1650                         const size_t sz = BLEN_THUMB_MEMSIZE(width, height);
1651                         data = MEM_mallocN(sz, __func__);
1652                         if (data) {
1653                                 BLI_assert((sz - sizeof(*data)) == (BLEN_THUMB_MEMSIZE_FILE(width, height) - (sizeof(*fd_data) * 2)));
1654                                 data->width = width;
1655                                 data->height = height;
1656                                 memcpy(data->rect, &fd_data[2], sz - sizeof(*data));
1657                         }
1658                 }
1659         }
1660
1661         blo_filedata_free(fd);
1662
1663         return data;
1664 }
1665
1666 /** \} */
1667
1668 /* -------------------------------------------------------------------- */
1669 /** \name Old/New Pointer Map
1670  * \{ */
1671
1672 static void *newdataadr(FileData *fd, const void *adr)      /* only direct databocks */
1673 {
1674         return oldnewmap_lookup_and_inc(fd->datamap, adr, true);
1675 }
1676
1677 static void *newdataadr_no_us(FileData *fd, const void *adr)        /* only direct databocks */
1678 {
1679         return oldnewmap_lookup_and_inc(fd->datamap, adr, false);
1680 }
1681
1682 static void *newglobadr(FileData *fd, const void *adr)      /* direct datablocks with global linking */
1683 {
1684         return oldnewmap_lookup_and_inc(fd->globmap, adr, true);
1685 }
1686
1687 static void *newimaadr(FileData *fd, const void *adr)           /* used to restore image data after undo */
1688 {
1689         if (fd->imamap && adr)
1690                 return oldnewmap_lookup_and_inc(fd->imamap, adr, true);
1691         return NULL;
1692 }
1693
1694 static void *newsceadr(FileData *fd, const void *adr)           /* used to restore scene data after undo */
1695 {
1696         if (fd->scenemap && adr)
1697                 return oldnewmap_lookup_and_inc(fd->scenemap, adr, true);
1698         return NULL;
1699 }
1700
1701 static void *newmclipadr(FileData *fd, const void *adr)      /* used to restore movie clip data after undo */
1702 {
1703         if (fd->movieclipmap && adr)
1704                 return oldnewmap_lookup_and_inc(fd->movieclipmap, adr, true);
1705         return NULL;
1706 }
1707
1708 static void *newsoundadr(FileData *fd, const void *adr)      /* used to restore sound data after undo */
1709 {
1710         if (fd->soundmap && adr)
1711                 return oldnewmap_lookup_and_inc(fd->soundmap, adr, true);
1712         return NULL;
1713 }
1714
1715 static void *newpackedadr(FileData *fd, const void *adr)      /* used to restore packed data after undo */
1716 {
1717         if (fd->packedmap && adr)
1718                 return oldnewmap_lookup_and_inc(fd->packedmap, adr, true);
1719
1720         return oldnewmap_lookup_and_inc(fd->datamap, adr, true);
1721 }
1722
1723
1724 static void *newlibadr(FileData *fd, const void *lib, const void *adr)      /* only lib data */
1725 {
1726         return oldnewmap_liblookup(fd->libmap, adr, lib);
1727 }
1728
1729 void *blo_do_versions_newlibadr(FileData *fd, const void *lib, const void *adr)     /* only lib data */
1730 {
1731         return newlibadr(fd, lib, adr);
1732 }
1733
1734 static void *newlibadr_us(FileData *fd, const void *lib, const void *adr)   /* increases user number */
1735 {
1736         ID *id = newlibadr(fd, lib, adr);
1737
1738         id_us_plus_no_lib(id);
1739
1740         return id;
1741 }
1742
1743 void *blo_do_versions_newlibadr_us(FileData *fd, const void *lib, const void *adr)  /* increases user number */
1744 {
1745         return newlibadr_us(fd, lib, adr);
1746 }
1747
1748 static void *newlibadr_real_us(FileData *fd, const void *lib, const void *adr)  /* ensures real user */
1749 {
1750         ID *id = newlibadr(fd, lib, adr);
1751
1752         id_us_ensure_real(id);
1753
1754         return id;
1755 }
1756
1757 static void change_idid_adr_fd(FileData *fd, const void *old, void *new)
1758 {
1759         for (int i = 0; i < fd->libmap->nentries; i++) {
1760                 OldNew *entry = &fd->libmap->entries[i];
1761
1762                 if (old == entry->newp && entry->nr == ID_ID) {
1763                         entry->newp = new;
1764                         if (new) entry->nr = GS( ((ID *)new)->name);
1765                 }
1766         }
1767 }
1768
1769 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1770 {
1771         Main *mainptr;
1772
1773         for (mainptr = mainlist->first; mainptr; mainptr = mainptr->next) {
1774                 FileData *fd;
1775
1776                 if (mainptr->curlib)
1777                         fd = mainptr->curlib->filedata;
1778                 else
1779                         fd = basefd;
1780
1781                 if (fd) {
1782                         change_idid_adr_fd(fd, old, new);
1783                 }
1784         }
1785 }
1786
1787 /* lib linked proxy objects point to our local data, we need
1788  * to clear that pointer before reading the undo memfile since
1789  * the object might be removed, it is set again in reading
1790  * if the local object still exists.
1791  * This is only valid for local proxy objects though, linked ones should not be affected here.
1792  */
1793 void blo_clear_proxy_pointers_from_lib(Main *oldmain)
1794 {
1795         Object *ob = oldmain->object.first;
1796
1797         for (; ob; ob = ob->id.next) {
1798                 if (ob->id.lib != NULL && ob->proxy_from != NULL && ob->proxy_from->id.lib == NULL) {
1799                         ob->proxy_from = NULL;
1800                 }
1801         }
1802 }
1803
1804 void blo_make_scene_pointer_map(FileData *fd, Main *oldmain)
1805 {
1806         Scene *sce = oldmain->scene.first;
1807
1808         fd->scenemap = oldnewmap_new();
1809
1810         for (; sce; sce = sce->id.next) {
1811                 if (sce->eevee.light_cache) {
1812                         struct LightCache *light_cache = sce->eevee.light_cache;
1813                         oldnewmap_insert(fd->scenemap, light_cache, light_cache, 0);
1814                 }
1815         }
1816 }
1817
1818 void blo_end_scene_pointer_map(FileData *fd, Main *oldmain)
1819 {
1820         OldNew *entry = fd->scenemap->entries;
1821         Scene *sce = oldmain->scene.first;
1822         int i;
1823
1824         /* used entries were restored, so we put them to zero */
1825         for (i = 0; i < fd->scenemap->nentries; i++, entry++) {
1826                 if (entry->nr > 0)
1827                         entry->newp = NULL;
1828         }
1829
1830         for (; sce; sce = sce->id.next) {
1831                 sce->eevee.light_cache = newsceadr(fd, sce->eevee.light_cache);
1832         }
1833 }
1834
1835 void blo_make_image_pointer_map(FileData *fd, Main *oldmain)
1836 {
1837         Image *ima = oldmain->image.first;
1838         Scene *sce = oldmain->scene.first;
1839         int a;
1840
1841         fd->imamap = oldnewmap_new();
1842
1843         for (; ima; ima = ima->id.next) {
1844                 if (ima->cache) {
1845                         oldnewmap_insert(fd->imamap, ima->cache, ima->cache, 0);
1846                 }
1847                 for (a = 0; a < TEXTARGET_COUNT; a++) {
1848                         if (ima->gputexture[a]) {
1849                                 oldnewmap_insert(fd->imamap, ima->gputexture[a], ima->gputexture[a], 0);
1850                         }
1851                 }
1852                 if (ima->rr) {
1853                         oldnewmap_insert(fd->imamap, ima->rr, ima->rr, 0);
1854                 }
1855                 LISTBASE_FOREACH(RenderSlot *, slot, &ima->renderslots) {
1856                         if (slot->render) {
1857                                 oldnewmap_insert(fd->imamap, slot->render, slot->render, 0);
1858                         }
1859                 }
1860         }
1861         for (; sce; sce = sce->id.next) {
1862                 if (sce->nodetree && sce->nodetree->previews) {
1863                         bNodeInstanceHashIterator iter;
1864                         NODE_INSTANCE_HASH_ITER(iter, sce->nodetree->previews) {
1865                                 bNodePreview *preview = BKE_node_instance_hash_iterator_get_value(&iter);
1866                                 oldnewmap_insert(fd->imamap, preview, preview, 0);
1867                         }
1868                 }
1869         }
1870 }
1871
1872 /* set old main image ibufs to zero if it has been restored */
1873 /* this works because freeing old main only happens after this call */
1874 void blo_end_image_pointer_map(FileData *fd, Main *oldmain)
1875 {
1876         OldNew *entry = fd->imamap->entries;
1877         Image *ima = oldmain->image.first;
1878         Scene *sce = oldmain->scene.first;
1879         int i;
1880
1881         /* used entries were restored, so we put them to zero */
1882         for (i = 0; i < fd->imamap->nentries; i++, entry++) {
1883                 if (entry->nr > 0)
1884                         entry->newp = NULL;
1885         }
1886
1887         for (; ima; ima = ima->id.next) {
1888                 ima->cache = newimaadr(fd, ima->cache);
1889                 if (ima->cache == NULL) {
1890                         ima->gpuflag = 0;
1891                         for (i = 0; i < TEXTARGET_COUNT; i++) {
1892                                 ima->gputexture[i] = NULL;
1893                         }
1894                         ima->rr = NULL;
1895                 }
1896                 LISTBASE_FOREACH(RenderSlot *, slot, &ima->renderslots) {
1897                         slot->render = newimaadr(fd, slot->render);
1898                 }
1899
1900                 for (i = 0; i < TEXTARGET_COUNT; i++)
1901                         ima->gputexture[i] = newimaadr(fd, ima->gputexture[i]);
1902                 ima->rr = newimaadr(fd, ima->rr);
1903         }
1904         for (; sce; sce = sce->id.next) {
1905                 if (sce->nodetree && sce->nodetree->previews) {
1906                         bNodeInstanceHash *new_previews = BKE_node_instance_hash_new("node previews");
1907                         bNodeInstanceHashIterator iter;
1908
1909                         /* reconstruct the preview hash, only using remaining pointers */
1910                         NODE_INSTANCE_HASH_ITER(iter, sce->nodetree->previews) {
1911                                 bNodePreview *preview = BKE_node_instance_hash_iterator_get_value(&iter);
1912                                 if (preview) {
1913                                         bNodePreview *new_preview = newimaadr(fd, preview);
1914                                         if (new_preview) {
1915                                                 bNodeInstanceKey key = BKE_node_instance_hash_iterator_get_key(&iter);
1916                                                 BKE_node_instance_hash_insert(new_previews, key, new_preview);
1917                                         }
1918                                 }
1919                         }
1920                         BKE_node_instance_hash_free(sce->nodetree->previews, NULL);
1921                         sce->nodetree->previews = new_previews;
1922                 }
1923         }
1924 }
1925
1926 void blo_make_movieclip_pointer_map(FileData *fd, Main *oldmain)
1927 {
1928         MovieClip *clip = oldmain->movieclip.first;
1929         Scene *sce = oldmain->scene.first;
1930
1931         fd->movieclipmap = oldnewmap_new();
1932
1933         for (; clip; clip = clip->id.next) {
1934                 if (clip->cache)
1935                         oldnewmap_insert(fd->movieclipmap, clip->cache, clip->cache, 0);
1936
1937                 if (clip->tracking.camera.intrinsics)
1938                         oldnewmap_insert(fd->movieclipmap, clip->tracking.camera.intrinsics, clip->tracking.camera.intrinsics, 0);
1939         }
1940
1941         for (; sce; sce = sce->id.next) {
1942                 if (sce->nodetree) {
1943                         bNode *node;
1944                         for (node = sce->nodetree->nodes.first; node; node = node->next)
1945                                 if (node->type == CMP_NODE_MOVIEDISTORTION)
1946                                         oldnewmap_insert(fd->movieclipmap, node->storage, node->storage, 0);
1947                 }
1948         }
1949 }
1950
1951 /* set old main movie clips caches to zero if it has been restored */
1952 /* this works because freeing old main only happens after this call */
1953 void blo_end_movieclip_pointer_map(FileData *fd, Main *oldmain)
1954 {
1955         OldNew *entry = fd->movieclipmap->entries;
1956         MovieClip *clip = oldmain->movieclip.first;
1957         Scene *sce = oldmain->scene.first;
1958         int i;
1959
1960         /* used entries were restored, so we put them to zero */
1961         for (i = 0; i < fd->movieclipmap->nentries; i++, entry++) {
1962                 if (entry->nr > 0)
1963                         entry->newp = NULL;
1964         }
1965
1966         for (; clip; clip = clip->id.next) {
1967                 clip->cache = newmclipadr(fd, clip->cache);
1968                 clip->tracking.camera.intrinsics = newmclipadr(fd, clip->tracking.camera.intrinsics);
1969         }
1970
1971         for (; sce; sce = sce->id.next) {
1972                 if (sce->nodetree) {
1973                         bNode *node;
1974                         for (node = sce->nodetree->nodes.first; node; node = node->next)
1975                                 if (node->type == CMP_NODE_MOVIEDISTORTION)
1976                                         node->storage = newmclipadr(fd, node->storage);
1977                 }
1978         }
1979 }
1980
1981 void blo_make_sound_pointer_map(FileData *fd, Main *oldmain)
1982 {
1983         bSound *sound = oldmain->sound.first;
1984
1985         fd->soundmap = oldnewmap_new();
1986
1987         for (; sound; sound = sound->id.next) {
1988                 if (sound->waveform)
1989                         oldnewmap_insert(fd->soundmap, sound->waveform, sound->waveform, 0);
1990         }
1991 }
1992
1993 /* set old main sound caches to zero if it has been restored */
1994 /* this works because freeing old main only happens after this call */
1995 void blo_end_sound_pointer_map(FileData *fd, Main *oldmain)
1996 {
1997         OldNew *entry = fd->soundmap->entries;
1998         bSound *sound = oldmain->sound.first;
1999         int i;
2000
2001         /* used entries were restored, so we put them to zero */
2002         for (i = 0; i < fd->soundmap->nentries; i++, entry++) {
2003                 if (entry->nr > 0)
2004                         entry->newp = NULL;
2005         }
2006
2007         for (; sound; sound = sound->id.next) {
2008                 sound->waveform = newsoundadr(fd, sound->waveform);
2009         }
2010 }
2011
2012 /* XXX disabled this feature - packed files also belong in temp saves and quit.blend, to make restore work */
2013
2014 static void insert_packedmap(FileData *fd, PackedFile *pf)
2015 {
2016         oldnewmap_insert(fd->packedmap, pf, pf, 0);
2017         oldnewmap_insert(fd->packedmap, pf->data, pf->data, 0);
2018 }
2019
2020 void blo_make_packed_pointer_map(FileData *fd, Main *oldmain)
2021 {
2022         Image *ima;
2023         VFont *vfont;
2024         bSound *sound;
2025         Library *lib;
2026
2027         fd->packedmap = oldnewmap_new();
2028
2029         for (ima = oldmain->image.first; ima; ima = ima->id.next) {
2030                 ImagePackedFile *imapf;
2031
2032                 if (ima->packedfile)
2033                         insert_packedmap(fd, ima->packedfile);
2034
2035                 for (imapf = ima->packedfiles.first; imapf; imapf = imapf->next)
2036                         if (imapf->packedfile)
2037                                 insert_packedmap(fd, imapf->packedfile);
2038         }
2039
2040         for (vfont = oldmain->vfont.first; vfont; vfont = vfont->id.next)
2041                 if (vfont->packedfile)
2042                         insert_packedmap(fd, vfont->packedfile);
2043
2044         for (sound = oldmain->sound.first; sound; sound = sound->id.next)
2045                 if (sound->packedfile)
2046                         insert_packedmap(fd, sound->packedfile);
2047
2048         for (lib = oldmain->library.first; lib; lib = lib->id.next)
2049                 if (lib->packedfile)
2050                         insert_packedmap(fd, lib->packedfile);
2051
2052 }
2053
2054 /* set old main packed data to zero if it has been restored */
2055 /* this works because freeing old main only happens after this call */
2056 void blo_end_packed_pointer_map(FileData *fd, Main *oldmain)
2057 {
2058         Image *ima;
2059         VFont *vfont;
2060         bSound *sound;
2061         Library *lib;
2062         OldNew *entry = fd->packedmap->entries;
2063         int i;
2064
2065         /* used entries were restored, so we put them to zero */
2066         for (i = 0; i < fd->packedmap->nentries; i++, entry++) {
2067                 if (entry->nr > 0)
2068                         entry->newp = NULL;
2069         }
2070
2071         for (ima = oldmain->image.first; ima; ima = ima->id.next) {
2072                 ImagePackedFile *imapf;
2073
2074                 ima->packedfile = newpackedadr(fd, ima->packedfile);
2075
2076                 for (imapf = ima->packedfiles.first; imapf; imapf = imapf->next)
2077                         imapf->packedfile = newpackedadr(fd, imapf->packedfile);
2078         }
2079
2080         for (vfont = oldmain->vfont.first; vfont; vfont = vfont->id.next)
2081                 vfont->packedfile = newpackedadr(fd, vfont->packedfile);
2082
2083         for (sound = oldmain->sound.first; sound; sound = sound->id.next)
2084                 sound->packedfile = newpackedadr(fd, sound->packedfile);
2085
2086         for (lib = oldmain->library.first; lib; lib = lib->id.next)
2087                 lib->packedfile = newpackedadr(fd, lib->packedfile);
2088 }
2089
2090
2091 /* undo file support: add all library pointers in lookup */
2092 void blo_add_library_pointer_map(ListBase *old_mainlist, FileData *fd)
2093 {
2094         Main *ptr = old_mainlist->first;
2095         ListBase *lbarray[MAX_LIBARRAY];
2096
2097         for (ptr = ptr->next; ptr; ptr = ptr->next) {
2098                 int i = set_listbasepointers(ptr, lbarray);
2099                 while (i--) {
2100                         ID *id;
2101                         for (id = lbarray[i]->first; id; id = id->next)
2102                                 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
2103                 }
2104         }
2105
2106         fd->old_mainlist = old_mainlist;
2107 }
2108
2109 /** \} */
2110
2111 /* -------------------------------------------------------------------- */
2112 /** \name DNA Struct Loading
2113  * \{ */
2114
2115 static void switch_endian_structs(const struct SDNA *filesdna, BHead *bhead)
2116 {
2117         int blocksize, nblocks;
2118         char *data;
2119
2120         data = (char *)(bhead + 1);
2121         blocksize = filesdna->typelens[filesdna->structs[bhead->SDNAnr][0]];
2122
2123         nblocks = bhead->nr;
2124         while (nblocks--) {
2125                 DNA_struct_switch_endian(filesdna, bhead->SDNAnr, data);
2126
2127                 data += blocksize;
2128         }
2129 }
2130
2131 static void *read_struct(FileData *fd, BHead *bh, const char *blockname)
2132 {
2133         void *temp = NULL;
2134
2135         if (bh->len) {
2136 #ifdef USE_BHEAD_READ_ON_DEMAND
2137                 BHead *bh_orig = bh;
2138 #endif
2139
2140                 /* switch is based on file dna */
2141                 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN)) {
2142 #ifdef USE_BHEAD_READ_ON_DEMAND
2143                         if (BHEADN_FROM_BHEAD(bh)->has_data == false) {
2144                                 bh = blo_bhead_read_full(fd, bh);
2145                                 if (UNLIKELY(bh == NULL)) {
2146                                         fd->flags &= ~FD_FLAGS_FILE_OK;
2147                                         return NULL;
2148                                 }
2149                         }
2150 #endif
2151                         switch_endian_structs(fd->filesdna, bh);
2152                 }
2153
2154                 if (fd->compflags[bh->SDNAnr] != SDNA_CMP_REMOVED) {
2155                         if (fd->compflags[bh->SDNAnr] == SDNA_CMP_NOT_EQUAL) {
2156 #ifdef USE_BHEAD_READ_ON_DEMAND
2157                                 if (BHEADN_FROM_BHEAD(bh)->has_data == false) {
2158                                         bh = blo_bhead_read_full(fd, bh);
2159                                         if (UNLIKELY(bh == NULL)) {
2160                                                 fd->flags &= ~FD_FLAGS_FILE_OK;
2161                                                 return NULL;
2162                                         }
2163                                 }
2164 #endif
2165                                 temp = DNA_struct_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh + 1));
2166                         }
2167                         else {
2168                                 /* SDNA_CMP_EQUAL */
2169                                 temp = MEM_mallocN(bh->len, blockname);
2170 #ifdef USE_BHEAD_READ_ON_DEMAND
2171                                 if (BHEADN_FROM_BHEAD(bh)->has_data) {
2172                                         memcpy(temp, (bh + 1), bh->len);
2173                                 }
2174                                 else {
2175                                         /* Instead of allocating the bhead, then copying it,
2176                                          * read the data from the file directly into the memory. */
2177                                         if (UNLIKELY(!blo_bhead_read_data(fd, bh, temp))) {
2178                                                 fd->flags &= ~FD_FLAGS_FILE_OK;
2179                                                 MEM_freeN(temp);
2180                                                 temp = NULL;
2181                                         }
2182                                 }
2183 #else
2184                                 memcpy(temp, (bh + 1), bh->len);
2185 #endif
2186                         }
2187                 }
2188 #ifdef USE_BHEAD_READ_ON_DEMAND
2189                 if (bh_orig != bh) {
2190                         MEM_freeN(BHEADN_FROM_BHEAD(bh));
2191                 }
2192 #endif
2193         }
2194
2195         return temp;
2196 }
2197
2198 typedef void (*link_list_cb)(FileData *fd, void *data);
2199
2200 static void link_list_ex(FileData *fd, ListBase *lb, link_list_cb callback)     /* only direct data */
2201 {
2202         Link *ln, *prev;
2203
2204         if (BLI_listbase_is_empty(lb)) return;
2205
2206         lb->first = newdataadr(fd, lb->first);
2207         if (callback != NULL) {
2208                 callback(fd, lb->first);
2209         }
2210         ln = lb->first;
2211         prev = NULL;
2212         while (ln) {
2213                 ln->next = newdataadr(fd, ln->next);
2214                 if (ln->next != NULL && callback != NULL) {
2215                         callback(fd, ln->next);
2216                 }
2217                 ln->prev = prev;
2218                 prev = ln;
2219                 ln = ln->next;
2220         }
2221         lb->last = prev;
2222 }
2223
2224 static void link_list(FileData *fd, ListBase *lb)       /* only direct data */
2225 {
2226         link_list_ex(fd, lb, NULL);
2227 }
2228
2229 static void link_glob_list(FileData *fd, ListBase *lb)      /* for glob data */
2230 {
2231         Link *ln, *prev;
2232         void *poin;
2233
2234         if (BLI_listbase_is_empty(lb)) return;
2235         poin = newdataadr(fd, lb->first);
2236         if (lb->first) {
2237                 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
2238         }
2239         lb->first = poin;
2240
2241         ln = lb->first;
2242         prev = NULL;
2243         while (ln) {
2244                 poin = newdataadr(fd, ln->next);
2245                 if (ln->next) {
2246                         oldnewmap_insert(fd->globmap, ln->next, poin, 0);
2247                 }
2248                 ln->next = poin;
2249                 ln->prev = prev;
2250                 prev = ln;
2251                 ln = ln->next;
2252         }
2253         lb->last = prev;
2254 }
2255
2256 static void test_pointer_array(FileData *fd, void **mat)
2257 {
2258         int64_t *lpoin, *lmat;
2259         int *ipoin, *imat;
2260         size_t len;
2261
2262         /* manually convert the pointer array in
2263          * the old dna format to a pointer array in
2264          * the new dna format.
2265          */
2266         if (*mat) {
2267                 len = MEM_allocN_len(*mat) / fd->filesdna->pointerlen;
2268
2269                 if (fd->filesdna->pointerlen == 8 && fd->memsdna->pointerlen == 4) {
2270                         ipoin = imat = MEM_malloc_arrayN(len, 4, "newmatar");
2271                         lpoin = *mat;
2272
2273                         while (len-- > 0) {
2274                                 if ((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
2275                                         BLI_endian_switch_int64(lpoin);
2276                                 *ipoin = (int)((*lpoin) >> 3);
2277                                 ipoin++;
2278                                 lpoin++;
2279                         }
2280                         MEM_freeN(*mat);
2281                         *mat = imat;
2282                 }
2283
2284                 if (fd->filesdna->pointerlen == 4 && fd->memsdna->pointerlen == 8) {
2285                         lpoin = lmat = MEM_malloc_arrayN(len, 8, "newmatar");
2286                         ipoin = *mat;
2287
2288                         while (len-- > 0) {
2289                                 *lpoin = *ipoin;
2290                                 ipoin++;
2291                                 lpoin++;
2292                         }
2293                         MEM_freeN(*mat);
2294                         *mat = lmat;
2295                 }
2296         }
2297 }
2298
2299 /** \} */
2300
2301 /* -------------------------------------------------------------------- */
2302 /** \name Read ID Properties
2303  * \{ */
2304
2305 static void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
2306 static void IDP_LibLinkProperty(IDProperty *prop, FileData *fd);
2307
2308 static void IDP_DirectLinkIDPArray(IDProperty *prop, int switch_endian, FileData *fd)
2309 {
2310         IDProperty *array;
2311         int i;
2312
2313         /* since we didn't save the extra buffer, set totallen to len */
2314         prop->totallen = prop->len;
2315         prop->data.pointer = newdataadr(fd, prop->data.pointer);
2316
2317         array = (IDProperty *)prop->data.pointer;
2318
2319         /* note!, idp-arrays didn't exist in 2.4x, so the pointer will be cleared
2320          * theres not really anything we can do to correct this, at least don't crash */
2321         if (array == NULL) {
2322                 prop->len = 0;
2323                 prop->totallen = 0;
2324         }
2325
2326
2327         for (i = 0; i < prop->len; i++)
2328                 IDP_DirectLinkProperty(&array[i], switch_endian, fd);
2329 }
2330
2331 static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, FileData *fd)
2332 {
2333         IDProperty **array;
2334         int i;
2335
2336         /* since we didn't save the extra buffer, set totallen to len */
2337         prop->totallen = prop->len;
2338         prop->data.pointer = newdataadr(fd, prop->data.pointer);
2339
2340         if (prop->subtype == IDP_GROUP) {
2341                 test_pointer_array(fd, prop->data.pointer);
2342                 array = prop->data.pointer;
2343
2344                 for (i = 0; i < prop->len; i++)
2345                         IDP_DirectLinkProperty(array[i], switch_endian, fd);
2346         }
2347         else if (prop->subtype == IDP_DOUBLE) {
2348                 if (switch_endian) {
2349                         BLI_endian_switch_double_array(prop->data.pointer, prop->len);
2350                 }
2351         }
2352         else {
2353                 if (switch_endian) {
2354                         /* also used for floats */
2355                         BLI_endian_switch_int32_array(prop->data.pointer, prop->len);
2356                 }
2357         }
2358 }
2359
2360 static void IDP_DirectLinkString(IDProperty *prop, FileData *fd)
2361 {
2362         /*since we didn't save the extra string buffer, set totallen to len.*/
2363         prop->totallen = prop->len;
2364         prop->data.pointer = newdataadr(fd, prop->data.pointer);
2365 }
2366
2367 static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, FileData *fd)
2368 {
2369         ListBase *lb = &prop->data.group;
2370         IDProperty *loop;
2371
2372         link_list(fd, lb);
2373
2374         /*Link child id properties now*/
2375         for (loop = prop->data.group.first; loop; loop = loop->next) {
2376                 IDP_DirectLinkProperty(loop, switch_endian, fd);
2377         }
2378 }
2379
2380 static void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
2381 {
2382         switch (prop->type) {
2383                 case IDP_GROUP:
2384                         IDP_DirectLinkGroup(prop, switch_endian, fd);
2385                         break;
2386                 case IDP_STRING:
2387                         IDP_DirectLinkString(prop, fd);
2388                         break;
2389                 case IDP_ARRAY:
2390                         IDP_DirectLinkArray(prop, switch_endian, fd);
2391                         break;
2392                 case IDP_IDPARRAY:
2393                         IDP_DirectLinkIDPArray(prop, switch_endian, fd);
2394                         break;
2395                 case IDP_DOUBLE:
2396                         /* erg, stupid doubles.  since I'm storing them
2397                          * in the same field as int val; val2 in the
2398                          * IDPropertyData struct, they have to deal with
2399                          * endianness specifically
2400                          *
2401                          * in theory, val and val2 would've already been swapped
2402                          * if switch_endian is true, so we have to first unswap
2403                          * them then reswap them as a single 64-bit entity.
2404                          */
2405
2406                         if (switch_endian) {
2407                                 BLI_endian_switch_int32(&prop->data.val);
2408                                 BLI_endian_switch_int32(&prop->data.val2);
2409                                 BLI_endian_switch_int64((int64_t *)&prop->data.val);
2410                         }
2411                         break;
2412                 case IDP_INT:
2413                 case IDP_FLOAT:
2414                 case IDP_ID:
2415                         break;  /* Nothing special to do here. */
2416                 default:
2417                         /* Unknown IDP type, nuke it (we cannot handle unknown types everywhere in code,
2418                          * IDP are way too polymorphic to do it safely. */
2419                         printf("%s: found unknown IDProperty type %d, reset to Integer one !\n", __func__, prop->type);
2420                         /* Note: we do not attempt to free unknown prop, we have no way to know how to do that! */
2421                         prop->type = IDP_INT;
2422                         prop->subtype = 0;
2423                         IDP_Int(prop) = 0;
2424         }
2425 }
2426
2427 #define IDP_DirectLinkGroup_OrFree(prop, switch_endian, fd) \
2428        _IDP_DirectLinkGroup_OrFree(prop, switch_endian, fd, __func__)
2429
2430 static void _IDP_DirectLinkGroup_OrFree(
2431         IDProperty **prop, int switch_endian, FileData *fd,
2432         const char *caller_func_id)
2433 {
2434         if (*prop) {
2435                 if ((*prop)->type == IDP_GROUP) {
2436                         IDP_DirectLinkGroup(*prop, switch_endian, fd);
2437                 }
2438                 else {
2439                         /* corrupt file! */
2440                         printf("%s: found non group data, freeing type %d!\n",
2441                                caller_func_id, (*prop)->type);
2442                         /* don't risk id, data's likely corrupt. */
2443                         // IDP_FreeProperty(*prop);
2444                         *prop = NULL;
2445                 }
2446         }
2447 }
2448
2449 static void IDP_LibLinkProperty(IDProperty *prop, FileData *fd)
2450 {
2451         if (!prop)
2452                 return;
2453
2454         switch (prop->type) {
2455                 case IDP_ID: /* PointerProperty */
2456                 {
2457                         void *newaddr = newlibadr_us(fd, NULL, IDP_Id(prop));
2458                         if (IDP_Id(prop) && !newaddr && G.debug) {
2459                                 printf("Error while loading \"%s\". Data not found in file!\n", prop->name);
2460                         }
2461                         prop->data.pointer = newaddr;
2462                         break;
2463                 }
2464                 case IDP_IDPARRAY: /* CollectionProperty */
2465                 {
2466                         IDProperty *idp_array = IDP_IDPArray(prop);
2467                         for (int i = 0; i < prop->len; i++) {
2468                                 IDP_LibLinkProperty(&(idp_array[i]), fd);
2469                         }
2470                         break;
2471                 }
2472                 case IDP_GROUP: /* PointerProperty */
2473                 {
2474                         for (IDProperty *loop = prop->data.group.first; loop; loop = loop->next) {
2475                                 IDP_LibLinkProperty(loop, fd);
2476                         }
2477                         break;
2478                 }
2479                 default:
2480                         break;  /* Nothing to do for other IDProps. */
2481         }
2482 }
2483
2484 /** \} */
2485
2486 /* -------------------------------------------------------------------- */
2487 /** \name Read Image Preview
2488  * \{ */
2489
2490 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
2491 {
2492         PreviewImage *prv = newdataadr(fd, old_prv);
2493
2494         if (prv) {
2495                 int i;
2496                 for (i = 0; i < NUM_ICON_SIZES; ++i) {
2497                         if (prv->rect[i]) {
2498                                 prv->rect[i] = newdataadr(fd, prv->rect[i]);
2499                         }
2500                         prv->gputexture[i] = NULL;
2501                 }
2502                 prv->icon_id = 0;
2503                 prv->tag = 0;
2504         }
2505
2506         return prv;
2507 }
2508
2509 /** \} */
2510
2511 /* -------------------------------------------------------------------- */
2512 /** \name Read ID
2513  * \{ */
2514
2515 static void lib_link_id(FileData *fd, Main *main)
2516 {
2517         ListBase *lbarray[MAX_LIBARRAY];
2518         int base_count, i;
2519
2520         base_count = set_listbasepointers(main, lbarray);
2521
2522         for (i = 0; i < base_count; i++) {
2523                 ListBase *lb = lbarray[i];
2524                 ID *id;
2525
2526                 for (id = lb->first; id; id = id->next) {
2527                         if (id->override_static) {
2528                                 id->override_static->reference = newlibadr_us(fd, id->lib, id->override_static->reference);
2529                                 id->override_static->storage = newlibadr_us(fd, id->lib, id->override_static->storage);
2530                         }
2531                 }
2532         }
2533 }
2534
2535 static void direct_link_id_override_property_operation_cb(FileData *fd, void *data)
2536 {
2537         IDOverrideStaticPropertyOperation *opop = data;
2538
2539         opop->subitem_reference_name = newdataadr(fd, opop->subitem_reference_name);
2540         opop->subitem_local_name = newdataadr(fd, opop->subitem_local_name);
2541 }
2542
2543 static void direct_link_id_override_property_cb(FileData *fd, void *data)
2544 {
2545         IDOverrideStaticProperty *op = data;
2546
2547         op->rna_path = newdataadr(fd, op->rna_path);
2548         link_list_ex(fd, &op->operations, direct_link_id_override_property_operation_cb);
2549 }
2550
2551 static void direct_link_id(FileData *fd, ID *id)
2552 {
2553         /*link direct data of ID properties*/
2554         if (id->properties) {
2555                 id->properties = newdataadr(fd, id->properties);
2556                 /* this case means the data was written incorrectly, it should not happen */
2557                 IDP_DirectLinkGroup_OrFree(&id->properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2558         }
2559         id->py_instance = NULL;
2560
2561         /* That way datablock reading not going through main read_libblock() function are still in a clear tag state.
2562          * (glowering at certain nodetree fake datablock here...). */
2563         id->tag = 0;
2564
2565         /* Link direct data of overrides. */
2566         if (id->override_static) {
2567                 id->override_static = newdataadr(fd, id->override_static);
2568                 link_list_ex(fd, &id->override_static->properties, direct_link_id_override_property_cb);
2569         }
2570
2571         DrawDataList *drawdata = DRW_drawdatalist_from_id(id);
2572         if (drawdata) {
2573                 BLI_listbase_clear((ListBase *)drawdata);
2574         }
2575 }
2576
2577 /** \} */
2578
2579 /* -------------------------------------------------------------------- */
2580 /** \name Read CurveMapping
2581  * \{ */
2582
2583 /* cuma itself has been read! */
2584 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
2585 {
2586         int a;
2587
2588         /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
2589         cumap->flag &= ~CUMA_PREMULLED;
2590
2591         for (a = 0; a < CM_TOT; a++) {
2592                 cumap->cm[a].curve = newdataadr(fd, cumap->cm[a].curve);
2593                 cumap->cm[a].table = NULL;
2594                 cumap->cm[a].premultable = NULL;
2595         }
2596 }
2597
2598 /** \} */
2599
2600 /* -------------------------------------------------------------------- */
2601 /** \name Read ID: Brush
2602  * \{ */
2603
2604 /* library brush linking after fileread */
2605 static void lib_link_brush(FileData *fd, Main *main)
2606 {
2607         /* only link ID pointers */
2608         for (Brush *brush = main->brush.first; brush; brush = brush->id.next) {
2609                 if (brush->id.tag & LIB_TAG_NEED_LINK) {
2610                         IDP_LibLinkProperty(brush->id.properties, fd);
2611
2612                         /* brush->(mask_)mtex.obj is ignored on purpose? */
2613                         brush->mtex.tex = newlibadr_us(fd, brush->id.lib, brush->mtex.tex);
2614                         brush->mask_mtex.tex = newlibadr_us(fd, brush->id.lib, brush->mask_mtex.tex);
2615                         brush->clone.image = newlibadr(fd, brush->id.lib, brush->clone.image);
2616                         brush->toggle_brush = newlibadr(fd, brush->id.lib, brush->toggle_brush);
2617                         brush->paint_curve = newlibadr_us(fd, brush->id.lib, brush->paint_curve);
2618
2619                         /* link default grease pencil palette */
2620                         if (brush->gpencil_settings != NULL) {
2621                                 brush->gpencil_settings->material = newlibadr_us(fd, brush->id.lib, brush->gpencil_settings->material);
2622                         }
2623
2624                         brush->id.tag &= ~LIB_TAG_NEED_LINK;
2625                 }
2626         }
2627 }
2628
2629 static void direct_link_brush(FileData *fd, Brush *brush)
2630 {
2631         /* brush itself has been read */
2632
2633         /* fallof curve */
2634         brush->curve = newdataadr(fd, brush->curve);
2635
2636         brush->gradient = newdataadr(fd, brush->gradient);
2637
2638         if (brush->curve)
2639                 direct_link_curvemapping(fd, brush->curve);
2640         else
2641                 BKE_brush_curve_preset(brush, CURVE_PRESET_SHARP);
2642
2643         /* grease pencil */
2644         brush->gpencil_settings = newdataadr(fd, brush->gpencil_settings);
2645         if (brush->gpencil_settings != NULL) {
2646                 brush->gpencil_settings->curve_sensitivity = newdataadr(fd, brush->gpencil_settings->curve_sensitivity);
2647                 brush->gpencil_settings->curve_strength = newdataadr(fd, brush->gpencil_settings->curve_strength);
2648                 brush->gpencil_settings->curve_jitter = newdataadr(fd, brush->gpencil_settings->curve_jitter);
2649
2650                 if (brush->gpencil_settings->curve_sensitivity)
2651                         direct_link_curvemapping(fd, brush->gpencil_settings->curve_sensitivity);
2652
2653                 if (brush->gpencil_settings->curve_strength)
2654                         direct_link_curvemapping(fd, brush->gpencil_settings->curve_strength);
2655
2656                 if (brush->gpencil_settings->curve_jitter)
2657                         direct_link_curvemapping(fd, brush->gpencil_settings->curve_jitter);
2658         }
2659
2660         brush->preview = NULL;
2661         brush->icon_imbuf = NULL;
2662 }
2663
2664 /** \} */
2665
2666 /* -------------------------------------------------------------------- */
2667 /** \name Read ID: Palette
2668  * \{ */
2669
2670 static void lib_link_palette(FileData *fd, Main *main)
2671 {
2672         /* only link ID pointers */
2673         for (Palette *palette = main->palettes.first; palette; palette = palette->id.next) {
2674                 if (palette->id.tag & LIB_TAG_NEED_LINK) {
2675                         IDP_LibLinkProperty(palette->id.properties, fd);
2676
2677                         palette->id.tag &= ~LIB_TAG_NEED_LINK;
2678                 }
2679         }
2680 }
2681
2682 static void direct_link_palette(FileData *fd, Palette *palette)
2683 {
2684
2685         /* palette itself has been read */
2686         link_list(fd, &palette->colors);
2687 }
2688
2689 static void lib_link_paint_curve(FileData *fd, Main *main)
2690 {
2691         /* only link ID pointers */
2692         for (PaintCurve *pc = main->paintcurves.first; pc; pc = pc->id.next) {
2693                 if (pc->id.tag & LIB_TAG_NEED_LINK) {
2694                         IDP_LibLinkProperty(pc->id.properties, fd);
2695
2696                         pc->id.tag &= ~LIB_TAG_NEED_LINK;
2697                 }
2698         }
2699 }
2700
2701 static void direct_link_paint_curve(FileData *fd, PaintCurve *pc)
2702 {
2703         pc->points = newdataadr(fd, pc->points);
2704 }
2705
2706 /** \} */
2707
2708 /* -------------------------------------------------------------------- */
2709 /** \name Read PackedFile
2710  * \{ */
2711
2712 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
2713 {
2714         PackedFile *pf = newpackedadr(fd, oldpf);
2715
2716         if (pf) {
2717                 pf->data = newpackedadr(fd, pf->data);
2718         }
2719
2720         return pf;
2721 }
2722
2723 /** \} */
2724
2725 /* -------------------------------------------------------------------- */
2726 /** \name Read Animation (legacy for version patching)
2727  * \{ */
2728
2729 // XXX deprecated - old animation system
2730 static void lib_link_ipo(FileData *fd, Main *main)
2731 {
2732         Ipo *ipo;
2733
2734         for (ipo = main->ipo.first; ipo; ipo = ipo->id.next) {
2735                 if (ipo->id.tag & LIB_TAG_NEED_LINK) {
2736                         IpoCurve *icu;
2737                         for (icu = ipo->curve.first; icu; icu = icu->next) {
2738                                 if (icu->driver)
2739                                         icu->driver->ob = newlibadr(fd, ipo->id.lib, icu->driver->ob);
2740                         }
2741                         ipo->id.tag &= ~LIB_TAG_NEED_LINK;
2742                 }
2743         }
2744 }
2745
2746 // XXX deprecated - old animation system
2747 static void direct_link_ipo(FileData *fd, Ipo *ipo)
2748 {
2749         IpoCurve *icu;
2750
2751         link_list(fd, &(ipo->curve));
2752
2753         for (icu = ipo->curve.first; icu; icu = icu->next) {
2754                 icu->bezt = newdataadr(fd, icu->bezt);
2755                 icu->bp = newdataadr(fd, icu->bp);
2756                 icu->driver = newdataadr(fd, icu->driver);
2757         }
2758 }
2759
2760 // XXX deprecated - old animation system
2761 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
2762 {
2763         bActionStrip *strip;
2764         bActionModifier *amod;
2765
2766         for (strip = striplist->first; strip; strip = strip->next) {
2767                 strip->object = newlibadr(fd, id->lib, strip->object);
2768                 strip->act = newlibadr_us(fd, id->lib, strip->act);
2769                 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
2770                 for (amod = strip->modifiers.first; amod; amod = amod->next)
2771                         amod->ob = newlibadr(fd, id->lib, amod->ob);
2772         }
2773 }
2774
2775 // XXX deprecated - old animation system
2776 static void direct_link_nlastrips(FileData *fd, ListBase *strips)
2777 {
2778         bActionStrip *strip;
2779
2780         link_list(fd, strips);
2781
2782         for (strip = strips->first; strip; strip = strip->next)
2783                 link_list(fd, &strip->modifiers);
2784 }
2785
2786 // XXX deprecated - old animation system
2787 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
2788 {
2789         bConstraintChannel *chan;
2790
2791         for (chan = chanbase->first; chan; chan = chan->next) {
2792                 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
2793         }
2794 }
2795
2796 /** \} */
2797
2798 /* -------------------------------------------------------------------- */
2799 /** \name Read ID: Action
2800  * \{ */
2801
2802 static void lib_link_fmodifiers(FileData *fd, ID *id, ListBase *list)
2803 {
2804         FModifier *fcm;
2805
2806         for (fcm = list->first; fcm; fcm = fcm->next) {
2807                 /* data for specific modifiers */
2808                 switch (fcm->type) {
2809                         case FMODIFIER_TYPE_PYTHON:
2810                         {
2811                                 FMod_Python *data = (FMod_Python *)fcm->data;
2812                                 data->script = newlibadr(fd, id->lib, data->script);
2813
2814                                 break;
2815                         }
2816                 }
2817         }
2818 }
2819
2820 static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list)
2821 {
2822         FCurve *fcu;
2823
2824         if (list == NULL)
2825                 return;
2826
2827         /* relink ID-block references... */
2828         for (fcu = list->first; fcu; fcu = fcu->next) {
2829                 /* driver data */
2830                 if (fcu->driver) {
2831                         ChannelDriver *driver = fcu->driver;
2832                         DriverVar *dvar;
2833
2834                         for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
2835                                 DRIVER_TARGETS_LOOPER_BEGIN(dvar)
2836                                 {
2837                                         /* only relink if still used */
2838                                         if (tarIndex < dvar->num_targets)
2839                                                 dtar->id = newlibadr(fd, id->lib, dtar->id);
2840                                         else
2841                                                 dtar->id = NULL;
2842                                 }
2843                                 DRIVER_TARGETS_LOOPER_END;
2844                         }
2845                 }
2846
2847                 /* modifiers */
2848                 lib_link_fmodifiers(fd, id, &fcu->modifiers);
2849         }
2850 }
2851
2852
2853 /* NOTE: this assumes that link_list has already been called on the list */
2854 static void direct_link_fmodifiers(FileData *fd, ListBase *list, FCurve *curve)
2855 {
2856         FModifier *fcm;
2857
2858         for (fcm = list->first; fcm; fcm = fcm->next) {
2859                 /* relink general data */
2860                 fcm->data  = newdataadr(fd, fcm->data);
2861                 fcm->curve = curve;
2862
2863                 /* do relinking of data for specific types */
2864                 switch (fcm->type) {
2865                         case FMODIFIER_TYPE_GENERATOR:
2866                         {
2867                                 FMod_Generator *data = (FMod_Generator *)fcm->data;
2868
2869                                 data->coefficients = newdataadr(fd, data->coefficients);
2870
2871                                 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2872                                         BLI_endian_switch_float_array(data->coefficients, data->arraysize);
2873                                 }
2874
2875                                 break;
2876                         }
2877                         case FMODIFIER_TYPE_ENVELOPE:
2878                         {
2879                                 FMod_Envelope *data =  (FMod_Envelope *)fcm->data;
2880
2881                                 data->data = newdataadr(fd, data->data);
2882
2883                                 break;
2884                         }
2885                         case FMODIFIER_TYPE_PYTHON:
2886                         {
2887                                 FMod_Python *data = (FMod_Python *)fcm->data;
2888
2889                                 data->prop = newdataadr(fd, data->prop);
2890                                 IDP_DirectLinkGroup_OrFree(&data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2891
2892                                 break;
2893                         }
2894                 }
2895         }
2896 }
2897
2898 /* NOTE: this assumes that link_list has already been called on the list */
2899 static void direct_link_fcurves(FileData *fd, ListBase *list)
2900 {
2901         FCurve *fcu;
2902
2903         /* link F-Curve data to F-Curve again (non ID-libs) */
2904         for (fcu = list->first; fcu; fcu = fcu->next) {
2905                 /* curve data */
2906                 fcu->bezt = newdataadr(fd, fcu->bezt);
2907                 fcu->fpt = newdataadr(fd, fcu->fpt);
2908
2909                 /* rna path */
2910                 fcu->rna_path = newdataadr(fd, fcu->rna_path);
2911
2912                 /* group */
2913                 fcu->grp = newdataadr(fd, fcu->grp);
2914
2915                 /* clear disabled flag - allows disabled drivers to be tried again ([#32155]),
2916                  * but also means that another method for "reviving disabled F-Curves" exists
2917                  */
2918                 fcu->flag &= ~FCURVE_DISABLED;
2919
2920                 /* driver */
2921                 fcu->driver = newdataadr(fd, fcu->driver);
2922                 if (fcu->driver) {
2923                         ChannelDriver *driver = fcu->driver;
2924                         DriverVar *dvar;
2925
2926                         /* compiled expression data will need to be regenerated (old pointer may still be set here) */
2927                         driver->expr_comp = NULL;
2928                         driver->expr_simple = NULL;
2929
2930                         /* give the driver a fresh chance - the operating environment may be different now
2931                          * (addons, etc. may be different) so the driver namespace may be sane now [#32155]
2932                          */
2933                         driver->flag &= ~DRIVER_FLAG_INVALID;
2934
2935                         /* relink variables, targets and their paths */
2936                         link_list(fd, &driver->variables);
2937                         for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
2938                                 DRIVER_TARGETS_LOOPER_BEGIN(dvar)
2939                                 {
2940                                         /* only relink the targets being used */
2941                                         if (tarIndex < dvar->num_targets)
2942                                                 dtar->rna_path = newdataadr(fd, dtar->rna_path);
2943                                         else
2944                                                 dtar->rna_path = NULL;
2945                                 }
2946                                 DRIVER_TARGETS_LOOPER_END;
2947                         }
2948                 }
2949
2950                 /* modifiers */
2951                 link_list(fd, &fcu->modifiers);
2952                 direct_link_fmodifiers(fd, &fcu->modifiers, fcu);
2953         }
2954 }
2955
2956
2957 static void lib_link_action(FileData *fd, Main *main)
2958 {
2959         for (bAction *act = main->action.first; act; act = act->id.next) {
2960                 if (act->id.tag & LIB_TAG_NEED_LINK) {
2961                         IDP_LibLinkProperty(act->id.properties, fd);
2962
2963 // XXX deprecated - old animation system <<<
2964                         for (bActionChannel *chan = act->chanbase.first; chan; chan = chan->next) {
2965                                 chan->ipo = newlibadr_us(fd, act->id.lib, chan->ipo);
2966                                 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
2967                         }
2968 // >>> XXX deprecated - old animation system
2969
2970                         lib_link_fcurves(fd, &act->id, &act->curves);
2971
2972                         for (TimeMarker *marker = act->markers.first; marker; marker = marker->next) {
2973                                 if (marker->camera) {
2974                                         marker->camera = newlibadr(fd, act->id.lib, marker->camera);
2975                                 }
2976                         }
2977
2978                         act->id.tag &= ~LIB_TAG_NEED_LINK;
2979                 }
2980         }
2981 }
2982
2983 static void direct_link_action(FileData *fd, bAction *act)
2984 {
2985         bActionChannel *achan; // XXX deprecated - old animation system
2986         bActionGroup *agrp;
2987
2988         link_list(fd, &act->curves);
2989         link_list(fd, &act->chanbase); // XXX deprecated - old animation system
2990         link_list(fd, &act->groups);
2991         link_list(fd, &act->markers);
2992
2993 // XXX deprecated - old animation system <<<
2994         for (achan = act->chanbase.first; achan; achan = achan->next) {
2995                 achan->grp = newdataadr(fd, achan->grp);
2996
2997                 link_list(fd, &achan->constraintChannels);
2998         }
2999 // >>> XXX deprecated - old animation system
3000
3001         direct_link_fcurves(fd, &act->curves);
3002
3003         for (agrp = act->groups.first; agrp; agrp = agrp->next) {
3004                 agrp->channels.first = newdataadr(fd, agrp->channels.first);
3005                 agrp->channels.last = newdataadr(fd, agrp->channels.last);
3006         }
3007 }
3008
3009 static void lib_link_nladata_strips(FileData *fd, ID *id, ListBase *list)
3010 {
3011         NlaStrip *strip;
3012
3013         for (strip = list->first; strip; strip = strip->next) {
3014                 /* check strip's children */
3015                 lib_link_nladata_strips(fd, id, &strip->strips);
3016
3017                 /* check strip's F-Curves */
3018                 lib_link_fcurves(fd, id, &strip->fcurves);
3019
3020                 /* reassign the counted-reference to action */
3021                 strip->act = newlibadr_us(fd, id->lib, strip->act);
3022
3023                 /* fix action id-root (i.e. if it comes from a pre 2.57 .blend file) */
3024                 if ((strip->act) && (strip->act->idroot == 0))
3025                         strip->act->idroot = GS(id->name);
3026         }
3027 }
3028
3029 static void lib_link_nladata(FileData *fd, ID *id, ListBase *list)
3030 {
3031         NlaTrack *nlt;
3032
3033         /* we only care about the NLA strips inside the tracks */
3034         for (nlt = list->first; nlt; nlt = nlt->next) {
3035                 lib_link_nladata_strips(fd, id, &nlt->strips);
3036         }
3037 }
3038
3039 /* This handles Animato NLA-Strips linking
3040  * NOTE: this assumes that link_list has already been called on the list
3041  */
3042 static void direct_link_nladata_strips(FileData *fd, ListBase *list)
3043 {
3044         NlaStrip *strip;
3045
3046         for (strip = list->first; strip; strip = strip->next) {
3047                 /* strip's child strips */
3048                 link_list(fd, &strip->strips);
3049                 direct_link_nladata_strips(fd, &strip->strips);
3050
3051                 /* strip's F-Curves */
3052                 link_list(fd, &strip->fcurves);
3053                 direct_link_fcurves(fd, &strip->fcurves);
3054
3055                 /* strip's F-Modifiers */
3056                 link_list(fd, &strip->modifiers);
3057                 direct_link_fmodifiers(fd, &strip->modifiers, NULL);
3058         }
3059 }
3060
3061 /* NOTE: this assumes that link_list has already been called on the list */
3062 static void direct_link_nladata(FileData *fd, ListBase *list)
3063 {
3064         NlaTrack *nlt;
3065
3066         for (nlt = list->first; nlt; nlt = nlt->next) {
3067                 /* relink list of strips */
3068                 link_list(fd, &nlt->strips);
3069
3070                 /* relink strip data */
3071                 direct_link_nladata_strips(fd, &nlt->strips);
3072         }
3073 }
3074
3075 /* ------- */
3076
3077 static void lib_link_keyingsets(FileData *fd, ID *id, ListBase *list)
3078 {
3079         KeyingSet *ks;
3080         KS_Path *ksp;
3081
3082         /* here, we're only interested in the ID pointer stored in some of the paths */
3083         for (ks = list->first; ks; ks = ks->next) {
3084                 for (ksp = ks->paths.first; ksp; ksp = ksp->next) {
3085                         ksp->id = newlibadr(fd, id->lib, ksp->id);
3086                 }
3087         }
3088 }
3089
3090 /* NOTE: this assumes that link_list has already been called on the list */
3091 static void direct_link_keyingsets(FileData *fd, ListBase *list)
3092 {
3093         KeyingSet *ks;
3094         KS_Path *ksp;
3095
3096         /* link KeyingSet data to KeyingSet again (non ID-libs) */
3097         for (ks = list->first; ks; ks = ks->next) {
3098                 /* paths */
3099                 link_list(fd, &ks->paths);
3100
3101                 for (ksp = ks->paths.first; ksp; ksp = ksp->next) {
3102                         /* rna path */
3103                         ksp->rna_path = newdataadr(fd, ksp->rna_path);
3104                 }
3105         }
3106 }
3107
3108 /* ------- */
3109
3110 static void lib_link_animdata(FileData *fd, ID *id, AnimData *adt)
3111 {
3112         if (adt == NULL)
3113                 return;
3114
3115         /* link action data */
3116         adt->action = newlibadr_us(fd, id->lib, adt->action);
3117         adt->tmpact = newlibadr_us(fd, id->lib, adt->tmpact);
3118
3119         /* fix action id-roots (i.e. if they come from a pre 2.57 .blend file) */
3120         if ((adt->action) && (adt->action->idroot == 0))
3121                 adt->action->idroot = GS(id->name);
3122         if ((adt->tmpact) && (adt->tmpact->idroot == 0))
3123                 adt->tmpact->idroot = GS(id->name);
3124
3125         /* link drivers */
3126         lib_link_fcurves(fd, id, &adt->drivers);
3127
3128         /* overrides don't have lib-link for now, so no need to do anything */
3129
3130         /* link NLA-data */
3131         lib_link_nladata(fd, id, &adt->nla_tracks);
3132 }
3133
3134 static void direct_link_animdata(FileData *fd, AnimData *adt)
3135 {
3136         /* NOTE: must have called newdataadr already before doing this... */
3137         if (adt == NULL)
3138                 return;
3139
3140         /* link drivers */
3141         link_list(fd, &adt->drivers);
3142         direct_link_fcurves(fd, &adt->drivers);
3143         adt->driver_array = NULL;
3144
3145         /* link overrides */
3146         // TODO...
3147
3148         /* link NLA-data */
3149         link_list(fd, &adt->nla_tracks);
3150         direct_link_nladata(fd, &adt->nla_tracks);
3151
3152         /* relink active track/strip - even though strictly speaking this should only be used
3153          * if we're in 'tweaking mode', we need to be able to have this loaded back for
3154          * undo, but also since users may not exit tweakmode before saving (#24535)
3155          */
3156         // TODO: it's not really nice that anyone should be able to save the file in this
3157         //              state, but it's going to be too hard to enforce this single case...
3158         adt->act_track = newdataadr(fd, adt->act_track);
3159         adt->actstrip = newdataadr(fd, adt->actstrip);
3160 }
3161
3162 /** \} */
3163
3164 /* -------------------------------------------------------------------- */
3165 /** \name Read ID: CacheFiles
3166  * \{ */
3167
3168 static void lib_link_cachefiles(FileData *fd, Main *bmain)
3169 {
3170         /* only link ID pointers */
3171         for (CacheFile *cache_file = bmain->cachefiles.first; cache_file; cache_file = cache_file->id.next) {
3172                 if (cache_file->id.tag & LIB_TAG_NEED_LINK) {
3173                         IDP_LibLinkProperty(cache_file->id.properties, fd);
3174                         lib_link_animdata(fd, &cache_file->id, cache_file->adt);
3175
3176                         cache_file->id.tag &= ~LIB_TAG_NEED_LINK;
3177                 }
3178         }
3179 }
3180
3181 static void direct_link_cachefile(FileData *fd, CacheFile *cache_file)
3182 {
3183         BLI_listbase_clear(&cache_file->object_paths);
3184         cache_file->handle = NULL;
3185         cache_file->handle_mutex = NULL;
3186
3187         /* relink animdata */
3188         cache_file->adt = newdataadr(fd, cache_file->adt);
3189         direct_link_animdata(fd, cache_file->adt);
3190 }
3191
3192 /** \} */
3193
3194 /* -------------------------------------------------------------------- */
3195 /** \name Read ID: WorkSpace
3196  * \{ */
3197
3198 static void lib_link_workspaces(FileData *fd, Main *bmain)
3199 {
3200         for (WorkSpace *workspace = bmain->workspaces.first; workspace; workspace = workspace->id.next) {
3201                 ListBase *layouts = BKE_workspace_layouts_get(workspace);
3202                 ID *id = (ID *)workspace;
3203
3204                 if ((id->tag & LIB_TAG_NEED_LINK) == 0) {
3205                         continue;
3206                 }
3207                 IDP_LibLinkProperty(id->properties, fd);
3208                 id_us_ensure_real(id);
3209
3210                 for (WorkSpaceLayout *layout = layouts->first, *layout_next; layout; layout = layout_next) {
3211                         layout->screen = newlibadr_us(fd, id->lib, layout->screen);
3212
3213                         layout_next = layout->next;
3214                         if (layout->screen) {
3215                                 if (ID_IS_LINKED(id)) {
3216                                         layout->screen->winid = 0;
3217                                         if (layout->screen->temp) {
3218                                                 /* delete temp layouts when appending */
3219                                                 BKE_workspace_layout_remove(bmain, workspace, layout);
3220                                         }
3221                                 }
3222                         }
3223                 }
3224
3225                 id->tag &= ~LIB_TAG_NEED_LINK;
3226         }
3227 }
3228
3229 static void direct_link_workspace(FileData *fd, WorkSpace *workspace, const Main *main)
3230 {
3231         link_list(fd, BKE_workspace_layouts_get(workspace));
3232         link_list(fd, &workspace->hook_layout_relations);
3233         link_list(fd, &workspace->owner_ids);
3234         link_list(fd, &workspace->tools);
3235
3236         for (WorkSpaceDataRelation *relation = workspace->hook_layout_relations.first;
3237              relation;
3238              relation = relation->next)
3239         {
3240                 relation->parent = newglobadr(fd, relation->parent); /* data from window - need to access through global oldnew-map */
3241                 relation->value = newdataadr(fd, relation->value);
3242         }
3243
3244         /* Same issue/fix as in direct_link_workspace_link_scene_data: Can't read workspace data
3245          * when reading windows, so have to update windows after/when reading workspaces. */
3246         for (wmWindowManager *wm = main->wm.first; wm; wm = wm->id.next) {
3247                 for (wmWindow *win = wm->windows.first; win; win = win->next) {
3248                         WorkSpaceLayout *act_layout = newdataadr(fd, BKE_workspace_active_layout_get(win->workspace_hook));
3249                         if (act_layout) {
3250                                 BKE_workspace_active_layout_set(win->workspace_hook, act_layout);
3251                         }
3252                 }
3253         }
3254
3255         for (bToolRef *tref = workspace->tools.first; tref; tref = tref->next) {
3256                 tref->runtime = NULL;
3257                 tref->properties = newdataadr(fd, tref->properties);
3258                 IDP_DirectLinkGroup_OrFree(&tref->properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
3259         }
3260
3261         workspace->status_text = NULL;
3262 }
3263
3264 static void lib_link_workspace_instance_hook(FileData *fd, WorkSpaceInstanceHook *hook, ID *id)
3265 {
3266         WorkSpace *workspace = BKE_workspace_active_get(hook);
3267         BKE_workspace_active_set(hook, newlibadr(fd, id->lib, workspace));
3268 }
3269
3270 /** \} */
3271
3272 /* -------------------------------------------------------------------- */
3273 /** \name Read ID: Node Tree
3274  * \{ */
3275
3276 /* Single node tree (also used for material/scene trees), ntree is not NULL */
3277 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
3278 {
3279         bNode *node;
3280         bNodeSocket *sock;
3281
3282         IDP_LibLinkProperty(ntree->id.properties, fd);
3283         lib_link_animdata(fd, &ntree->id, ntree->adt);
3284
3285         ntree->gpd = newlibadr_us(fd, id->lib, ntree->gpd);
3286
3287         for (node = ntree->nodes.first; node; node = node->next) {
3288                 /* Link ID Properties -- and copy this comment EXACTLY for easy finding
3289                  * of library blocks that implement this.*/
3290                 IDP_LibLinkProperty(node->prop, fd);
3291
3292                 node->id = newlibadr_us(fd, id->lib, node->id);
3293
3294                 for (sock = node->inputs.first; sock; sock = sock->next) {
3295                         IDP_LibLinkProperty(sock->prop, fd);
3296                 }
3297                 for (sock = node->outputs.first; sock; sock = sock->next) {
3298                         IDP_LibLinkProperty(sock->prop, fd);
3299                 }
3300         }
3301
3302         for (sock = ntree->inputs.first; sock; sock = sock->next) {