4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
30 /** \file blender/blenloader/intern/readfile.c
38 #include <stdio.h> // for printf fopen fwrite fclose sprintf FILE
39 #include <stdlib.h> // for getenv atoi
40 #include <stddef.h> // for offsetof
41 #include <fcntl.h> // for open
42 #include <string.h> // for strrchr strncmp strstr
43 #include <math.h> // for fabs
46 #include <unistd.h> // for read close
48 #include <io.h> // for open close read
50 #include "BLI_winstuff.h"
53 #include "DNA_anim_types.h"
54 #include "DNA_armature_types.h"
55 #include "DNA_actuator_types.h"
56 #include "DNA_brush_types.h"
57 #include "DNA_camera_types.h"
58 #include "DNA_cloth_types.h"
59 #include "DNA_controller_types.h"
60 #include "DNA_constraint_types.h"
61 #include "DNA_effect_types.h"
62 #include "DNA_fileglobal_types.h"
63 #include "DNA_genfile.h"
64 #include "DNA_group_types.h"
65 #include "DNA_gpencil_types.h"
66 #include "DNA_ipo_types.h"
67 #include "DNA_key_types.h"
68 #include "DNA_lattice_types.h"
69 #include "DNA_lamp_types.h"
70 #include "DNA_meta_types.h"
71 #include "DNA_material_types.h"
72 #include "DNA_mesh_types.h"
73 #include "DNA_meshdata_types.h"
74 #include "DNA_nla_types.h"
75 #include "DNA_node_types.h"
76 #include "DNA_object_fluidsim.h" // NT
77 #include "DNA_packedFile_types.h"
78 #include "DNA_particle_types.h"
79 #include "DNA_property_types.h"
80 #include "DNA_text_types.h"
81 #include "DNA_view3d_types.h"
82 #include "DNA_screen_types.h"
83 #include "DNA_sensor_types.h"
84 #include "DNA_sdna_types.h"
85 #include "DNA_scene_types.h"
86 #include "DNA_sequence_types.h"
87 #include "DNA_smoke_types.h"
88 #include "DNA_sound_types.h"
89 #include "DNA_space_types.h"
90 #include "DNA_vfont_types.h"
91 #include "DNA_world_types.h"
93 #include "MEM_guardedalloc.h"
95 #include "BLI_utildefines.h"
96 #include "BLI_blenlib.h"
98 #include "BLI_cellalloc.h"
99 #include "BLI_edgehash.h"
101 #include "BKE_anim.h"
102 #include "BKE_action.h"
103 #include "BKE_armature.h"
104 #include "BKE_brush.h"
105 #include "BKE_colortools.h"
106 #include "BKE_constraint.h"
107 #include "BKE_context.h"
108 #include "BKE_curve.h"
109 #include "BKE_deform.h"
110 #include "BKE_effect.h" /* give_parteff */
111 #include "BKE_fcurve.h"
112 #include "BKE_global.h" // for G
113 #include "BKE_group.h"
114 #include "BKE_image.h"
115 #include "BKE_lattice.h"
116 #include "BKE_library.h" // for which_libbase
117 #include "BKE_idcode.h"
118 #include "BKE_material.h"
119 #include "BKE_main.h" // for Main
120 #include "BKE_mesh.h" // for ME_ defines (patching)
121 #include "BKE_modifier.h"
122 #include "BKE_multires.h"
123 #include "BKE_node.h" // for tree type defines
124 #include "BKE_object.h"
125 #include "BKE_paint.h"
126 #include "BKE_particle.h"
127 #include "BKE_pointcache.h"
128 #include "BKE_property.h" // for get_ob_property
129 #include "BKE_report.h"
130 #include "BKE_sca.h" // for init_actuator
131 #include "BKE_scene.h"
132 #include "BKE_screen.h"
133 #include "BKE_sequencer.h"
134 #include "BKE_texture.h" // for open_plugin_tex
135 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
136 #include "BKE_sound.h"
138 //XXX #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
139 //XXX #include "BIF_filelist.h" // badlevel too, where to move this? - elubie
140 //XXX #include "BIF_previewrender.h" // bedlelvel, for struct RenderInfo
141 #include "BLO_readfile.h"
142 #include "BLO_undofile.h"
144 #include "readfile.h"
146 #include "PIL_time.h"
151 Remark: still a weak point is the newaddress() function, that doesnt solve reading from
152 multiple files at the same time
154 (added remark: oh, i thought that was solved? will look at that... (ton)
157 - Existing Library (Main) push or free
167 - read associated 'direct data'
168 - link direct data (internal and to LibBlock)
170 - read USER data, only when indicated (file is ~/X.XX/startup.blend)
172 - per Library (per Main)
175 - find LibBlocks and attach IDs to Main
176 - if external LibBlock
178 - or it's already read,
183 - read associated direct data
184 - link direct data (internal and to LibBlock)
186 - per Library with unread LibBlocks
191 - read associated direct data
192 - link direct data (internal and to LibBlock)
195 - link all LibBlocks and indirect pointers to libblocks
196 - initialize FileGlobal and copy pointers to Global
199 /* also occurs in library.c */
200 /* GS reads the memory pointed at in a specific ordering. There are,
201 * however two definitions for it. I have jotted them down here, both,
202 * but I think the first one is actually used. The thing is that
203 * big-endian systems might read this the wrong way round. OTOH, we
204 * constructed the IDs that are read out with this macro explicitly as
205 * well. I expect we'll sort it out soon... */
208 #define GS(a) (*((short *)(a)))
210 /* from misc_util: flip the bytes from x */
211 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
213 // only used here in readfile.c
214 #define SWITCH_LONGINT(a) { \
217 s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
218 s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
219 s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
220 s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
224 typedef struct OldNew {
229 typedef struct OldNewMap {
231 int nentries, entriessize;
237 /* local prototypes */
238 static void *read_struct(FileData *fd, BHead *bh, const char *blockname);
239 static void direct_link_modifiers(FileData *fd, ListBase *lb);
241 static OldNewMap *oldnewmap_new(void)
243 OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
245 onm->entriessize= 1024;
246 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
251 static int verg_oldnewmap(const void *v1, const void *v2)
253 const struct OldNew *x1=v1, *x2=v2;
255 if( x1->old > x2->old) return 1;
256 else if( x1->old < x2->old) return -1;
261 static void oldnewmap_sort(FileData *fd)
263 qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
264 fd->libmap->sorted= 1;
267 /* nr is zero for data, and ID code for libdata */
268 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
272 if(oldaddr==NULL || newaddr==NULL) return;
274 if (onm->nentries==onm->entriessize) {
275 int osize= onm->entriessize;
276 OldNew *oentries= onm->entries;
278 onm->entriessize*= 2;
279 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
281 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
285 entry= &onm->entries[onm->nentries++];
287 entry->newp= newaddr;
291 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
295 if(addr==NULL) return NULL;
297 if (onm->lasthit<onm->nentries-1) {
298 OldNew *entry= &onm->entries[++onm->lasthit];
300 if (entry->old==addr) {
306 for (i=0; i<onm->nentries; i++) {
307 OldNew *entry= &onm->entries[i];
309 if (entry->old==addr) {
320 /* for libdata, nr has ID code, no increment */
321 static void *oldnewmap_liblookup(OldNewMap *onm, void *addr, void *lib)
325 if(addr==NULL) return NULL;
327 /* lasthit works fine for non-libdata, linking there is done in same sequence as writing */
329 OldNew entry_s, *entry;
333 entry= bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
337 if (id && (!lib || id->lib)) {
343 for (i=0; i<onm->nentries; i++) {
344 OldNew *entry= &onm->entries[i];
346 if (entry->old==addr) {
349 if (id && (!lib || id->lib)) {
358 static void oldnewmap_free_unused(OldNewMap *onm)
362 for (i=0; i<onm->nentries; i++) {
363 OldNew *entry= &onm->entries[i];
365 MEM_freeN(entry->newp);
371 static void oldnewmap_clear(OldNewMap *onm)
377 static void oldnewmap_free(OldNewMap *onm)
379 MEM_freeN(onm->entries);
385 static void read_libraries(FileData *basefd, ListBase *mainlist);
387 /* ************ help functions ***************** */
389 static void add_main_to_main(Main *mainvar, Main *from)
391 ListBase *lbarray[MAX_LIBARRAY], *fromarray[MAX_LIBARRAY];
394 set_listbasepointers(mainvar, lbarray);
395 a= set_listbasepointers(from, fromarray);
397 BLI_movelisttolist(lbarray[a], fromarray[a]);
401 void blo_join_main(ListBase *mainlist)
403 Main *tojoin, *mainl;
406 mainl= mainlist->first;
407 while ((tojoin= mainl->next)) {
408 add_main_to_main(mainl, tojoin);
409 BLI_remlink(mainlist, tojoin);
414 static void split_libdata(ListBase *lb, Main *first)
426 if(mainvar->curlib==id->lib) {
427 lbn= which_libbase(mainvar, GS(id->name));
429 BLI_addtail(lbn, id);
432 mainvar= mainvar->next;
434 if(mainvar==NULL) printf("error split_libdata\n");
440 void blo_split_main(ListBase *mainlist, Main *main)
442 ListBase *lbarray[MAX_LIBARRAY];
446 mainlist->first= mainlist->last= main;
449 if(main->library.first==NULL)
452 for (lib= main->library.first; lib; lib= lib->id.next) {
453 Main *libmain= MEM_callocN(sizeof(Main), "libmain");
454 libmain->curlib= lib;
455 BLI_addtail(mainlist, libmain);
458 i= set_listbasepointers(main, lbarray);
460 split_libdata(lbarray[i], main->next);
463 /* removes things like /blah/blah/../../blah/ etc, then writes in *name the full path */
464 static void cleanup_path(const char *relabase, char *name)
466 char filename[FILE_MAXFILE];
468 BLI_splitdirstring(name, filename);
469 BLI_cleanup_dir(relabase, name);
470 strcat(name, filename);
473 static void read_file_version(FileData *fd, Main *main)
477 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
478 if (bhead->code==GLOB) {
479 FileGlobal *fg= read_struct(fd, bhead, "Global");
481 main->subversionfile= fg->subversion;
482 main->minversionfile= fg->minversion;
483 main->minsubversionfile= fg->minsubversion;
486 else if (bhead->code==ENDB)
493 static Main *blo_find_main(FileData *fd, ListBase *mainlist, const char *filepath, const char *relabase)
497 char name1[FILE_MAXDIR+FILE_MAXFILE];
499 BLI_strncpy(name1, filepath, sizeof(name1));
500 cleanup_path(relabase, name1);
501 // printf("blo_find_main: original in %s\n", name);
502 // printf("blo_find_main: converted to %s\n", name1);
504 for (m= mainlist->first; m; m= m->next) {
505 char *libname= (m->curlib)?m->curlib->filepath:m->name;
507 if (BLI_path_cmp(name1, libname) == 0) {
508 if(G.f & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
513 m= MEM_callocN(sizeof(Main), "find_main");
514 BLI_addtail(mainlist, m);
516 lib= alloc_libblock(&m->library, ID_LI, "lib");
517 strncpy(lib->name, filepath, sizeof(lib->name)-1);
518 BLI_strncpy(lib->filepath, name1, sizeof(lib->filepath));
522 read_file_version(fd, m);
524 if(G.f & G_DEBUG) printf("blo_find_main: added new lib %s\n", filepath);
529 /* ************ FILE PARSING ****************** */
531 static void switch_endian_bh4(BHead4 *bhead)
533 /* the ID_.. codes */
534 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
536 if (bhead->code != ENDB) {
537 SWITCH_INT(bhead->len);
538 SWITCH_INT(bhead->SDNAnr);
539 SWITCH_INT(bhead->nr);
543 static void switch_endian_bh8(BHead8 *bhead)
545 /* the ID_.. codes */
546 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
548 if (bhead->code != ENDB) {
549 SWITCH_INT(bhead->len);
550 SWITCH_INT(bhead->SDNAnr);
551 SWITCH_INT(bhead->nr);
555 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
557 BHead4 *bhead4 = (BHead4 *) bhead;
558 #if defined(WIN32) && !defined(FREE_WINDOWS)
564 bhead4->code= bhead8->code;
565 bhead4->len= bhead8->len;
567 if (bhead4->code != ENDB) {
569 //perform a endian swap on 64bit pointers, otherwise the pointer might map to zero
570 //0x0000000000000000000012345678 would become 0x12345678000000000000000000000000
571 if (do_endian_swap) {
572 SWITCH_LONGINT(bhead8->old);
575 /* this patch is to avoid a long long being read from not-eight aligned positions
576 is necessary on any modern 64bit architecture) */
577 memcpy(&old, &bhead8->old, 8);
578 bhead4->old = (int) (old >> 3);
580 bhead4->SDNAnr= bhead8->SDNAnr;
581 bhead4->nr= bhead8->nr;
585 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
587 BHead8 *bhead8 = (BHead8 *) bhead;
589 bhead8->code= bhead4->code;
590 bhead8->len= bhead4->len;
592 if (bhead8->code != ENDB) {
593 bhead8->old= bhead4->old;
594 bhead8->SDNAnr= bhead4->SDNAnr;
595 bhead8->nr= bhead4->nr;
599 static BHeadN *get_bhead(FileData *fd)
601 BHeadN *new_bhead = NULL;
606 /* initializing to zero isn't strictly needed but shuts valgrind up
607 * since uninitialized memory gets compared */
612 // First read the bhead structure.
613 // Depending on the platform the file was written on this can
614 // be a big or little endian BHead4 or BHead8 structure.
616 // As usual 'ENDB' (the last *partial* bhead of the file)
617 // needs some special handling. We don't want to EOF just yet.
619 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
621 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
623 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
624 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
625 switch_endian_bh4(&bhead4);
628 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
629 bh8_from_bh4(&bhead, &bhead4);
631 memcpy(&bhead, &bhead4, sizeof(bhead));
639 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
641 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
642 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
643 switch_endian_bh8(&bhead8);
646 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
647 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
649 memcpy(&bhead, &bhead8, sizeof(bhead));
657 /* make sure people are not trying to pass bad blend files */
658 if (bhead.len < 0) fd->eof = 1;
660 // bhead now contains the (converted) bhead structure. Now read
661 // the associated data and put everything in a BHeadN (creative naming !)
664 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
666 new_bhead->next = new_bhead->prev = NULL;
667 new_bhead->bhead = bhead;
669 readsize = fd->read(fd, new_bhead + 1, bhead.len);
671 if (readsize != bhead.len) {
673 MEM_freeN(new_bhead);
683 // We've read a new block. Now add it to the list
687 BLI_addtail(&fd->listbase, new_bhead);
693 BHead *blo_firstbhead(FileData *fd)
699 // Read in a new block if necessary
701 new_bhead = fd->listbase.first;
702 if (new_bhead == NULL) {
703 new_bhead = get_bhead(fd);
707 bhead = &new_bhead->bhead;
713 BHead *blo_prevbhead(FileData *UNUSED(fd), BHead *thisblock)
715 BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
716 BHeadN *prev= bheadn->prev;
718 return prev?&prev->bhead:NULL;
721 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
723 BHeadN *new_bhead = NULL;
727 // bhead is actually a sub part of BHeadN
728 // We calculate the BHeadN pointer from the BHead pointer below
729 new_bhead = (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
731 // get the next BHeadN. If it doesn't exist we read in the next one
732 new_bhead = new_bhead->next;
733 if (new_bhead == NULL) {
734 new_bhead = get_bhead(fd);
739 // here we do the reverse:
740 // go from the BHeadN pointer to the BHead pointer
741 bhead = &new_bhead->bhead;
747 static void decode_blender_header(FileData *fd)
749 char header[SIZEOFBLENDERHEADER], num[4];
752 // read in the header data
753 readsize = fd->read(fd, header, sizeof(header));
755 if (readsize == sizeof(header)) {
756 if(strncmp(header, "BLENDER", 7) == 0) {
757 int remove_this_endian_test= 1;
759 fd->flags |= FD_FLAGS_FILE_OK;
761 // what size are pointers in the file ?
763 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
764 if (sizeof(void *) != 4) {
765 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
768 if (sizeof(void *) != 8) {
769 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
773 // is the file saved in a different endian
775 if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
776 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
779 // get the version number
781 memcpy(num, header+9, 3);
783 fd->fileversion = atoi(num);
788 static int read_file_dna(FileData *fd)
792 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
793 if (bhead->code==DNA1) {
794 int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
796 fd->filesdna= DNA_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
799 fd->compflags= DNA_struct_get_compareflags(fd->filesdna, fd->memsdna);
800 /* used to retrieve ID names from (bhead+1) */
801 fd->id_name_offs= DNA_elem_offset(fd->filesdna, "ID", "char", "name[]");
805 } else if (bhead->code==ENDB)
812 static int fd_read_from_file(FileData *filedata, void *buffer, unsigned int size)
814 int readsize = read(filedata->filedes, buffer, size);
819 filedata->seek += readsize;
825 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, unsigned int size)
827 int readsize = gzread(filedata->gzfiledes, buffer, size);
832 filedata->seek += readsize;
838 static int fd_read_from_memory(FileData *filedata, void *buffer, unsigned int size)
840 // don't read more bytes then there are available in the buffer
841 int readsize = (int)MIN2(size, (unsigned int)(filedata->buffersize - filedata->seek));
843 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
844 filedata->seek += readsize;
849 static int fd_read_from_memfile(FileData *filedata, void *buffer, unsigned int size)
851 static unsigned int seek= 1<<30; /* the current position */
852 static unsigned int offset= 0; /* size of previous chunks */
853 static MemFileChunk *chunk=NULL;
854 unsigned int chunkoffset, readsize, totread;
856 if(size==0) return 0;
858 if(seek != (unsigned int)filedata->seek) {
859 chunk= filedata->memfile->chunks.first;
863 if(seek + chunk->size > (unsigned) filedata->seek) break;
868 seek= filedata->seek;
875 /* first check if it's on the end if current chunk */
876 if(seek-offset == chunk->size) {
877 offset+= chunk->size;
881 /* debug, should never happen */
883 printf("illegal read, chunk zero\n");
887 chunkoffset= seek-offset;
888 readsize= size-totread;
890 /* data can be spread over multiple chunks, so clamp size
891 * to within this chunk, and then it will read further in
893 if(chunkoffset+readsize > chunk->size)
894 readsize= chunk->size-chunkoffset;
896 memcpy((char*)buffer+totread, chunk->buf+chunkoffset, readsize);
898 filedata->seek += readsize;
900 } while(totread < size);
908 static FileData *filedata_new(void)
910 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
913 fd->gzfiledes = NULL;
915 /* XXX, this doesn't need to be done all the time,
916 * but it keeps us reentrant, remove once we have
917 * a lib that provides a nice lock. - zr
919 fd->memsdna = DNA_sdna_from_data(DNAstr, DNAlen, 0);
921 fd->datamap = oldnewmap_new();
922 fd->globmap = oldnewmap_new();
923 fd->libmap = oldnewmap_new();
928 static FileData *blo_decode_and_check(FileData *fd, ReportList *reports)
930 decode_blender_header(fd);
932 if (fd->flags & FD_FLAGS_FILE_OK) {
933 if (!read_file_dna(fd)) {
934 BKE_reportf(reports, RPT_ERROR, "Failed to read blend file: \"%s\", incomplete", fd->relabase);
935 blo_freefiledata(fd);
940 BKE_reportf(reports, RPT_ERROR, "Failed to read blend file: \"%s\", not a blend file", fd->relabase);
941 blo_freefiledata(fd);
948 /* cannot be called with relative paths anymore! */
949 /* on each new library added, it now checks for the current FileData and expands relativeness */
950 FileData *blo_openblenderfile(const char *filepath, ReportList *reports)
954 gzfile= gzopen(filepath, "rb");
956 if (gzfile == (gzFile)Z_NULL) {
957 BKE_reportf(reports, RPT_ERROR, "Unable to open \"%s\": %s.", filepath, errno ? strerror(errno) : "Unknown error reading file");
960 FileData *fd = filedata_new();
961 fd->gzfiledes = gzfile;
962 fd->read = fd_read_gzip_from_file;
964 /* needed for library_append and read_libraries */
965 BLI_strncpy(fd->relabase, filepath, sizeof(fd->relabase));
967 return blo_decode_and_check(fd, reports);
971 FileData *blo_openblendermemory(void *mem, int memsize, ReportList *reports)
973 if (!mem || memsize<SIZEOFBLENDERHEADER) {
974 BKE_report(reports, RPT_ERROR, (mem)? "Unable to read": "Unable to open");
977 FileData *fd= filedata_new();
979 fd->buffersize= memsize;
980 fd->read= fd_read_from_memory;
981 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
983 return blo_decode_and_check(fd, reports);
987 FileData *blo_openblendermemfile(MemFile *memfile, ReportList *reports)
990 BKE_report(reports, RPT_ERROR, "Unable to open blend <memory>");
993 FileData *fd= filedata_new();
994 fd->memfile= memfile;
996 fd->read= fd_read_from_memfile;
997 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
999 return blo_decode_and_check(fd, reports);
1004 void blo_freefiledata(FileData *fd)
1008 if (fd->filedes != -1) {
1012 if (fd->gzfiledes != NULL)
1014 gzclose(fd->gzfiledes);
1017 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
1018 MEM_freeN(fd->buffer);
1022 // Free all BHeadN data blocks
1023 BLI_freelistN(&fd->listbase);
1026 DNA_sdna_free(fd->memsdna);
1028 DNA_sdna_free(fd->filesdna);
1030 MEM_freeN(fd->compflags);
1033 oldnewmap_free(fd->datamap);
1035 oldnewmap_free(fd->globmap);
1037 oldnewmap_free(fd->imamap);
1038 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
1039 oldnewmap_free(fd->libmap);
1041 MEM_freeN(fd->bheadmap);
1047 /* ************ DIV ****************** */
1049 int BLO_has_bfile_extension(char *str)
1051 return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend") || BLI_testextensie(str, ".blend.gz"));
1054 int BLO_is_a_library(const char *path, char *dir, char *group)
1056 /* return ok when a blenderfile, in dir is the filename,
1057 * in group the type of libdata
1065 if( dir[len-1] != '/' && dir[len-1] != '\\') return 0;
1070 /* Find the last slash */
1071 fd= BLI_last_slash(dir);
1073 if(fd==NULL) return 0;
1075 if(BLO_has_bfile_extension(fd+1)) {
1076 /* the last part of the dir is a .blend file, no group follows */
1077 *fd= '/'; /* put back the removed slash separating the dir and the .blend file name */
1080 char *gp = fd+1; // in case we have a .blend file, gp points to the group
1082 /* Find the last slash */
1083 fd= BLI_last_slash(dir);
1084 if (!fd || !BLO_has_bfile_extension(fd+1)) return 0;
1086 /* now we know that we are in a blend file and it is safe to
1087 assume that gp actually points to a group */
1088 if (strcmp("Screen", gp)!=0)
1089 BLI_strncpy(group, gp, GROUP_MAX);
1094 /* ************** OLD POINTERS ******************* */
1096 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1098 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1101 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1103 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1106 static void *newimaadr(FileData *fd, void *adr) /* used to restore image data after undo */
1108 if(fd->imamap && adr)
1109 return oldnewmap_lookup_and_inc(fd->imamap, adr);
1114 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1116 return oldnewmap_liblookup(fd->libmap, adr, lib);
1119 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1121 ID *id= newlibadr(fd, lib, adr);
1129 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1133 for (i=0; i<fd->libmap->nentries; i++) {
1134 OldNew *entry= &fd->libmap->entries[i];
1136 if (old==entry->newp && entry->nr==ID_ID) {
1138 if(new) entry->nr= GS( ((ID *)new)->name );
1144 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1148 for(mainptr= mainlist->first; mainptr; mainptr= mainptr->next) {
1151 if(mainptr->curlib) fd= mainptr->curlib->filedata;
1155 change_idid_adr_fd(fd, old, new);
1160 /* lib linked proxy objects point to our local data, we need
1161 * to clear that pointer before reading the undo memfile since
1162 * the object might be removed, it is set again in reading
1163 * if the local object still exists */
1164 void blo_clear_proxy_pointers_from_lib(Main *oldmain)
1166 Object *ob= oldmain->object.first;
1168 for(;ob; ob= ob->id.next)
1170 ob->proxy_from= NULL;
1173 void blo_make_image_pointer_map(FileData *fd, Main *oldmain)
1175 Image *ima= oldmain->image.first;
1176 Scene *sce= oldmain->scene.first;
1179 fd->imamap= oldnewmap_new();
1181 for(;ima; ima= ima->id.next) {
1182 Link *ibuf= ima->ibufs.first;
1183 for(; ibuf; ibuf= ibuf->next)
1184 oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
1186 oldnewmap_insert(fd->imamap, ima->gputexture, ima->gputexture, 0);
1187 for(a=0; a<IMA_MAX_RENDER_SLOT; a++)
1189 oldnewmap_insert(fd->imamap, ima->renders[a], ima->renders[a], 0);
1191 for(; sce; sce= sce->id.next) {
1194 for(node= sce->nodetree->nodes.first; node; node= node->next)
1195 oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
1200 /* set old main image ibufs to zero if it has been restored */
1201 /* this works because freeing old main only happens after this call */
1202 void blo_end_image_pointer_map(FileData *fd, Main *oldmain)
1204 OldNew *entry= fd->imamap->entries;
1205 Image *ima= oldmain->image.first;
1206 Scene *sce= oldmain->scene.first;
1209 /* used entries were restored, so we put them to zero */
1210 for (i=0; i<fd->imamap->nentries; i++, entry++) {
1215 for(;ima; ima= ima->id.next) {
1218 /* this mirrors direct_link_image */
1219 for(ibuf= ima->ibufs.first; ibuf; ibuf= next) {
1221 if(NULL==newimaadr(fd, ibuf)) { /* so was restored */
1222 BLI_remlink(&ima->ibufs, ibuf);
1224 ima->gputexture= NULL;
1227 for(i=0; i<IMA_MAX_RENDER_SLOT; i++)
1228 ima->renders[i]= newimaadr(fd, ima->renders[i]);
1230 ima->gputexture= newimaadr(fd, ima->gputexture);
1232 for(; sce; sce= sce->id.next) {
1235 for(node= sce->nodetree->nodes.first; node; node= node->next)
1236 node->preview= newimaadr(fd, node->preview);
1241 /* undo file support: add all library pointers in lookup */
1242 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
1244 Main *ptr= mainlist->first;
1245 ListBase *lbarray[MAX_LIBARRAY];
1247 for(ptr= ptr->next; ptr; ptr= ptr->next) {
1248 int i= set_listbasepointers(ptr, lbarray);
1251 for(id= lbarray[i]->first; id; id= id->next)
1252 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
1258 /* ********** END OLD POINTERS ****************** */
1259 /* ********** READ FILE ****************** */
1261 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1263 int blocksize, nblocks;
1266 data= (char *)(bhead+1);
1267 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1271 DNA_struct_switch_endian(filesdna, bhead->SDNAnr, data);
1277 static void *read_struct(FileData *fd, BHead *bh, const char *blockname)
1282 /* switch is based on file dna */
1283 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1284 switch_endian_structs(fd->filesdna, bh);
1286 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1287 if(fd->compflags[bh->SDNAnr]==2) {
1288 temp= DNA_struct_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1290 temp= MEM_mallocN(bh->len, blockname);
1291 memcpy(temp, (bh+1), bh->len);
1299 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1303 if(lb->first==NULL) return;
1305 lb->first= newdataadr(fd, lb->first);
1309 ln->next= newdataadr(fd, ln->next);
1317 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1322 if(lb->first==NULL) return;
1323 poin= newdataadr(fd, lb->first);
1325 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1332 poin= newdataadr(fd, ln->next);
1334 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1344 static void test_pointer_array(FileData *fd, void **mat)
1346 #if defined(WIN32) && !defined(FREE_WINDOWS)
1347 __int64 *lpoin, *lmat;
1349 long long *lpoin, *lmat;
1354 /* manually convert the pointer array in
1355 * the old dna format to a pointer array in
1356 * the new dna format.
1359 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1361 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1362 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1366 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1367 SWITCH_LONGINT(*lpoin);
1368 *ipoin= (int) ((*lpoin) >> 3);
1376 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1377 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1391 /* ************ READ ID Properties *************** */
1393 static void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1394 static void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1396 static void IDP_DirectLinkIDPArray(IDProperty *prop, int switch_endian, FileData *fd)
1401 /*since we didn't save the extra buffer, set totallen to len.*/
1402 prop->totallen = prop->len;
1403 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1405 array= (IDProperty*) prop->data.pointer;
1407 /* note!, idp-arrays didn't exist in 2.4x, so the pointer will be cleared
1408 * theres not really anything we can do to correct this, at least dont crash */
1415 for(i=0; i<prop->len; i++)
1416 IDP_DirectLinkProperty(&array[i], switch_endian, fd);
1419 static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, FileData *fd)
1424 /*since we didn't save the extra buffer, set totallen to len.*/
1425 prop->totallen = prop->len;
1426 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1428 if(prop->subtype == IDP_GROUP) {
1429 test_pointer_array(fd, prop->data.pointer);
1430 array= prop->data.pointer;
1432 for(i=0; i<prop->len; i++)
1433 IDP_DirectLinkProperty(array[i], switch_endian, fd);
1435 else if(prop->subtype == IDP_DOUBLE) {
1436 if (switch_endian) {
1437 for (i=0; i<prop->len; i++) {
1438 SWITCH_LONGINT(((double*)prop->data.pointer)[i]);
1443 if (switch_endian) {
1444 for (i=0; i<prop->len; i++) {
1445 SWITCH_INT(((int*)prop->data.pointer)[i]);
1451 static void IDP_DirectLinkString(IDProperty *prop, FileData *fd)
1453 /*since we didn't save the extra string buffer, set totallen to len.*/
1454 prop->totallen = prop->len;
1455 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1458 static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, FileData *fd)
1460 ListBase *lb = &prop->data.group;
1465 /*Link child id properties now*/
1466 for (loop=prop->data.group.first; loop; loop=loop->next) {
1467 IDP_DirectLinkProperty(loop, switch_endian, fd);
1471 static void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1473 switch (prop->type) {
1475 IDP_DirectLinkGroup(prop, switch_endian, fd);
1478 IDP_DirectLinkString(prop, fd);
1481 IDP_DirectLinkArray(prop, switch_endian, fd);
1484 IDP_DirectLinkIDPArray(prop, switch_endian, fd);
1487 /*erg, stupid doubles. since I'm storing them
1488 in the same field as int val; val2 in the
1489 IDPropertyData struct, they have to deal with
1490 endianness specifically
1492 in theory, val and val2 would've already been swapped
1493 if switch_endian is true, so we have to first unswap
1494 them then reswap them as a single 64-bit entity.
1497 if (switch_endian) {
1498 SWITCH_INT(prop->data.val);
1499 SWITCH_INT(prop->data.val2);
1500 SWITCH_LONGINT(prop->data.val);
1508 static void IDP_LibLinkProperty(IDProperty *UNUSED(prop), int UNUSED(switch_endian), FileData *UNUSED(fd))
1512 /* ************ READ CurveMapping *************** */
1514 /* cuma itself has been read! */
1515 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1519 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1520 cumap->flag &= ~CUMA_PREMULLED;
1522 for(a=0; a<CM_TOT; a++) {
1523 cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
1524 cumap->cm[a].table= NULL;
1525 cumap->cm[a].premultable= NULL;
1529 /* ************ READ Brush *************** */
1530 /* library brush linking after fileread */
1531 static void lib_link_brush(FileData *fd, Main *main)
1535 /* only link ID pointers */
1536 for(brush= main->brush.first; brush; brush= brush->id.next) {
1537 if(brush->id.flag & LIB_NEEDLINK) {
1538 brush->id.flag -= LIB_NEEDLINK;
1540 brush->mtex.tex= newlibadr_us(fd, brush->id.lib, brush->mtex.tex);
1541 brush->clone.image= newlibadr_us(fd, brush->id.lib, brush->clone.image);
1546 static void direct_link_brush(FileData *fd, Brush *brush)
1548 /* brush itself has been read */
1551 brush->curve= newdataadr(fd, brush->curve);
1553 direct_link_curvemapping(fd, brush->curve);
1555 brush_curve_preset(brush, CURVE_PRESET_SHARP);
1557 brush->preview= NULL;
1558 brush->icon_imbuf= NULL;
1561 static void direct_link_script(FileData *UNUSED(fd), Script *script)
1564 SCRIPT_SET_NULL(script)
1568 /* ************ READ PACKEDFILE *************** */
1570 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1572 PackedFile *pf= newdataadr(fd, oldpf);
1575 pf->data= newdataadr(fd, pf->data);
1581 /* ************ READ IMAGE PREVIEW *************** */
1583 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
1585 PreviewImage *prv= newdataadr(fd, old_prv);
1589 for (i=0; i < NUM_ICON_SIZES; ++i) {
1591 prv->rect[i] = newdataadr(fd, prv->rect[i]);
1599 /* ************ READ ANIMATION STUFF ***************** */
1601 /* Legacy Data Support (for Version Patching) ----------------------------- */
1603 // XXX depreceated - old animation system
1604 static void lib_link_ipo(FileData *fd, Main *main)
1608 ipo= main->ipo.first;
1610 if(ipo->id.flag & LIB_NEEDLINK) {
1612 for(icu= ipo->curve.first; icu; icu= icu->next) {
1614 icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
1616 ipo->id.flag -= LIB_NEEDLINK;
1622 // XXX depreceated - old animation system
1623 static void direct_link_ipo(FileData *fd, Ipo *ipo)
1627 link_list(fd, &(ipo->curve));
1628 icu= ipo->curve.first;
1630 icu->bezt= newdataadr(fd, icu->bezt);
1631 icu->bp= newdataadr(fd, icu->bp);
1632 icu->driver= newdataadr(fd, icu->driver);
1637 // XXX depreceated - old animation system
1638 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1640 bActionStrip *strip;
1641 bActionModifier *amod;
1643 for (strip=striplist->first; strip; strip=strip->next){
1644 strip->object = newlibadr(fd, id->lib, strip->object);
1645 strip->act = newlibadr_us(fd, id->lib, strip->act);
1646 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1647 for(amod= strip->modifiers.first; amod; amod= amod->next)
1648 amod->ob= newlibadr(fd, id->lib, amod->ob);
1652 // XXX depreceated - old animation system
1653 static void direct_link_nlastrips(FileData *fd, ListBase *strips)
1655 bActionStrip *strip;
1657 link_list(fd, strips);
1659 for(strip= strips->first; strip; strip= strip->next)
1660 link_list(fd, &strip->modifiers);
1663 // XXX depreceated - old animation system
1664 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1666 bConstraintChannel *chan;
1668 for (chan=chanbase->first; chan; chan=chan->next){
1669 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1673 /* Data Linking ----------------------------- */
1675 static void lib_link_fmodifiers(FileData *fd, ID *id, ListBase *list)
1679 for (fcm= list->first; fcm; fcm= fcm->next) {
1680 /* data for specific modifiers */
1681 switch (fcm->type) {
1682 case FMODIFIER_TYPE_PYTHON:
1684 FMod_Python *data= (FMod_Python *)fcm->data;
1685 data->script = newlibadr(fd, id->lib, data->script);
1692 static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list)
1699 /* relink ID-block references... */
1700 for (fcu= list->first; fcu; fcu= fcu->next) {
1703 ChannelDriver *driver= fcu->driver;
1706 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1707 DRIVER_TARGETS_LOOPER(dvar)
1709 /* only relink if still used */
1710 if (tarIndex < dvar->num_targets)
1711 dtar->id= newlibadr(fd, id->lib, dtar->id);
1715 DRIVER_TARGETS_LOOPER_END
1720 lib_link_fmodifiers(fd, id, &fcu->modifiers);
1725 /* NOTE: this assumes that link_list has already been called on the list */
1726 static void direct_link_fmodifiers(FileData *fd, ListBase *list)
1730 for (fcm= list->first; fcm; fcm= fcm->next) {
1731 /* relink general data */
1732 fcm->data = newdataadr(fd, fcm->data);
1735 /* do relinking of data for specific types */
1736 switch (fcm->type) {
1737 case FMODIFIER_TYPE_GENERATOR:
1739 FMod_Generator *data= (FMod_Generator *)fcm->data;
1741 data->coefficients= newdataadr(fd, data->coefficients);
1743 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1745 for(a = 0; a < data->arraysize; a++)
1746 SWITCH_INT(data->coefficients[a]);
1750 case FMODIFIER_TYPE_ENVELOPE:
1752 FMod_Envelope *data= (FMod_Envelope *)fcm->data;
1754 data->data= newdataadr(fd, data->data);
1757 case FMODIFIER_TYPE_PYTHON:
1759 FMod_Python *data= (FMod_Python *)fcm->data;
1761 data->prop = newdataadr(fd, data->prop);
1762 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1769 /* NOTE: this assumes that link_list has already been called on the list */
1770 static void direct_link_fcurves(FileData *fd, ListBase *list)
1774 /* link F-Curve data to F-Curve again (non ID-libs) */
1775 for (fcu= list->first; fcu; fcu= fcu->next) {
1777 fcu->bezt= newdataadr(fd, fcu->bezt);
1778 fcu->fpt= newdataadr(fd, fcu->fpt);
1781 fcu->rna_path= newdataadr(fd, fcu->rna_path);
1784 fcu->grp= newdataadr(fd, fcu->grp);
1787 fcu->driver= newdataadr(fd, fcu->driver);
1789 ChannelDriver *driver= fcu->driver;
1792 driver->expr_comp= NULL;
1794 /* relink variables, targets and their paths */
1795 link_list(fd, &driver->variables);
1796 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1797 DRIVER_TARGETS_LOOPER(dvar)
1799 /* only relink the targets being used */
1800 if (tarIndex < dvar->num_targets)
1801 dtar->rna_path= newdataadr(fd, dtar->rna_path);
1803 dtar->rna_path= NULL;
1805 DRIVER_TARGETS_LOOPER_END
1810 link_list(fd, &fcu->modifiers);
1811 direct_link_fmodifiers(fd, &fcu->modifiers);
1816 static void lib_link_action(FileData *fd, Main *main)
1819 bActionChannel *chan;
1821 for (act= main->action.first; act; act= act->id.next) {
1822 if (act->id.flag & LIB_NEEDLINK) {
1823 act->id.flag -= LIB_NEEDLINK;
1825 // XXX depreceated - old animation system <<<
1826 for (chan=act->chanbase.first; chan; chan=chan->next) {
1827 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1828 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1830 // >>> XXX depreceated - old animation system
1832 lib_link_fcurves(fd, &act->id, &act->curves);
1837 static void direct_link_action(FileData *fd, bAction *act)
1839 bActionChannel *achan; // XXX depreceated - old animation system
1842 link_list(fd, &act->curves);
1843 link_list(fd, &act->chanbase); // XXX depreceated - old animation system
1844 link_list(fd, &act->groups);
1845 link_list(fd, &act->markers);
1847 // XXX depreceated - old animation system <<<
1848 for (achan = act->chanbase.first; achan; achan=achan->next) {
1849 achan->grp= newdataadr(fd, achan->grp);
1851 link_list(fd, &achan->constraintChannels);
1853 // >>> XXX depreceated - old animation system
1855 direct_link_fcurves(fd, &act->curves);
1857 for (agrp = act->groups.first; agrp; agrp= agrp->next) {
1858 agrp->channels.first= newdataadr(fd, agrp->channels.first);
1859 agrp->channels.last= newdataadr(fd, agrp->channels.last);
1863 static void lib_link_nladata_strips(FileData *fd, ID *id, ListBase *list)
1867 for (strip= list->first; strip; strip= strip->next) {
1868 /* check strip's children */
1869 lib_link_nladata_strips(fd, id, &strip->strips);
1871 /* check strip's F-Curves */
1872 lib_link_fcurves(fd, id, &strip->fcurves);
1874 /* reassign the counted-reference to action */
1875 strip->act = newlibadr_us(fd, id->lib, strip->act);
1877 /* fix action id-root (i.e. if it comes from a pre 2.57 .blend file) */
1878 if ((strip->act) && (strip->act->idroot == 0))
1879 strip->act->idroot = GS(id->name);
1883 static void lib_link_nladata(FileData *fd, ID *id, ListBase *list)
1887 /* we only care about the NLA strips inside the tracks */
1888 for (nlt= list->first; nlt; nlt= nlt->next) {
1889 lib_link_nladata_strips(fd, id, &nlt->strips);
1893 /* This handles Animato NLA-Strips linking
1894 * NOTE: this assumes that link_list has already been called on the list
1896 static void direct_link_nladata_strips(FileData *fd, ListBase *list)
1900 for (strip= list->first; strip; strip= strip->next) {
1901 /* strip's child strips */
1902 link_list(fd, &strip->strips);
1903 direct_link_nladata_strips(fd, &strip->strips);
1905 /* strip's F-Curves */
1906 link_list(fd, &strip->fcurves);
1907 direct_link_fcurves(fd, &strip->fcurves);
1909 /* strip's F-Modifiers */
1910 link_list(fd, &strip->modifiers);
1911 direct_link_modifiers(fd, &strip->modifiers);
1915 /* NOTE: this assumes that link_list has already been called on the list */
1916 static void direct_link_nladata(FileData *fd, ListBase *list)
1920 for (nlt= list->first; nlt; nlt= nlt->next) {
1921 /* relink list of strips */
1922 link_list(fd, &nlt->strips);
1924 /* relink strip data */
1925 direct_link_nladata_strips(fd, &nlt->strips);
1931 static void lib_link_keyingsets(FileData *fd, ID *id, ListBase *list)
1936 /* here, we're only interested in the ID pointer stored in some of the paths */
1937 for (ks= list->first; ks; ks= ks->next) {
1938 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1939 ksp->id= newlibadr(fd, id->lib, ksp->id);
1944 /* NOTE: this assumes that link_list has already been called on the list */
1945 static void direct_link_keyingsets(FileData *fd, ListBase *list)
1950 /* link KeyingSet data to KeyingSet again (non ID-libs) */
1951 for (ks= list->first; ks; ks= ks->next) {
1953 link_list(fd, &ks->paths);
1955 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1957 ksp->rna_path= newdataadr(fd, ksp->rna_path);
1964 static void lib_link_animdata(FileData *fd, ID *id, AnimData *adt)
1969 /* link action data */
1970 adt->action= newlibadr_us(fd, id->lib, adt->action);
1971 adt->tmpact= newlibadr_us(fd, id->lib, adt->tmpact);
1973 /* fix action id-roots (i.e. if they come from a pre 2.57 .blend file) */
1974 if ((adt->action) && (adt->action->idroot == 0))
1975 adt->action->idroot = GS(id->name);
1976 if ((adt->tmpact) && (adt->tmpact->idroot == 0))
1977 adt->tmpact->idroot = GS(id->name);
1980 lib_link_fcurves(fd, id, &adt->drivers);
1982 /* overrides don't have lib-link for now, so no need to do anything */
1985 lib_link_nladata(fd, id, &adt->nla_tracks);
1988 static void direct_link_animdata(FileData *fd, AnimData *adt)
1990 /* NOTE: must have called newdataadr already before doing this... */
1995 link_list(fd, &adt->drivers);
1996 direct_link_fcurves(fd, &adt->drivers);
1998 /* link overrides */
2002 link_list(fd, &adt->nla_tracks);
2003 direct_link_nladata(fd, &adt->nla_tracks);
2005 /* relink active strip - even though strictly speaking this should only be used
2006 * if we're in 'tweaking mode', we need to be able to have this loaded back for
2007 * undo, but also since users may not exit tweakmode before saving (#24535)
2009 // TODO: it's not really nice that anyone should be able to save the file in this
2010 // state, but it's going to be too hard to enforce this single case...
2011 adt->actstrip= newdataadr(fd, adt->actstrip);
2014 /* ************ READ MOTION PATHS *************** */
2016 /* direct data for cache */
2017 static void direct_link_motionpath(FileData *fd, bMotionPath *mpath)
2023 /* relink points cache */
2024 mpath->points= newdataadr(fd, mpath->points);
2027 /* ************ READ NODE TREE *************** */
2029 /* singe node tree (also used for material/scene trees), ntree is not NULL */
2030 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
2034 if(ntree->adt) lib_link_animdata(fd, &ntree->id, ntree->adt);
2036 ntree->gpd= newlibadr_us(fd, id->lib, ntree->gpd);
2038 for(node= ntree->nodes.first; node; node= node->next)
2039 node->id= newlibadr_us(fd, id->lib, node->id);
2042 /* library ntree linking after fileread */
2043 static void lib_link_nodetree(FileData *fd, Main *main)
2047 /* only link ID pointers */
2048 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2049 if(ntree->id.flag & LIB_NEEDLINK) {
2050 ntree->id.flag -= LIB_NEEDLINK;
2051 lib_link_ntree(fd, &ntree->id, ntree);
2056 /* updates group node socket own_index so that
2057 * external links to/from the group node are preserved.
2059 static void lib_node_do_versions_group(bNode *gnode)
2061 bNodeTree *ngroup= (bNodeTree*)gnode->id;
2063 bNodeSocket *sock, *gsock, *intsock;
2066 for (sock=gnode->outputs.first; sock; sock=sock->next) {
2067 int old_index = sock->to_index;
2068 for (gsock=ngroup->outputs.first; gsock; gsock=gsock->next) {
2069 if (gsock->link && gsock->link->fromsock->own_index == old_index) {
2070 sock->own_index = gsock->own_index;
2075 for (sock=gnode->inputs.first; sock; sock=sock->next) {
2076 int old_index = sock->to_index;
2077 /* can't use break in double loop */
2079 for (intnode=ngroup->nodes.first; intnode && !found; intnode=intnode->next) {
2080 for (intsock=intnode->inputs.first; intsock; intsock=intsock->next) {
2081 if (intsock->own_index == old_index && intsock->link) {
2082 sock->own_index = intsock->link->fromsock->own_index;
2091 /* updates external links for all group nodes in a tree */
2092 static void lib_nodetree_do_versions_group(bNodeTree *ntree)
2096 for (node=ntree->nodes.first; node; node=node->next) {
2097 if (node->type==NODE_GROUP) {
2098 bNodeTree *ngroup= (bNodeTree*)node->id;
2099 if (ngroup && (ngroup->flag & NTREE_DO_VERSIONS))
2100 lib_node_do_versions_group(node);
2105 /* verify types for nodes and groups, all data has to be read */
2106 /* open = 0: appending/linking, open = 1: open new file (need to clean out dynamic
2108 static void lib_verify_nodetree(Main *main, int UNUSED(open))
2115 /* this crashes blender on undo/redo
2117 reinit_nodesystem();
2120 /* now create the own typeinfo structs an verify nodes */
2121 /* here we still assume no groups in groups */
2122 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2123 ntreeVerifyTypes(ntree); /* internal nodes, no groups! */
2127 /*int has_old_groups=0;*/ /*UNUSED*/
2128 /* XXX this should actually be part of do_versions, but since we need
2129 * finished library linking, it is not possible there. Instead in do_versions
2130 * we have set the NTREE_DO_VERSIONS flag, so at this point we can do the
2131 * actual group node updates.
2133 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2134 if (ntree->flag & NTREE_DO_VERSIONS) {
2135 /* this adds copies and links from all unlinked internal sockets to group inputs/outputs. */
2136 nodeGroupExposeAllSockets(ntree);
2137 /*has_old_groups = 1;*/ /*UNUSED*/
2140 /* now verify all types in material trees, groups are set OK now */
2141 for(ma= main->mat.first; ma; ma= ma->id.next) {
2143 lib_nodetree_do_versions_group(ma->nodetree);
2145 /* and scene trees */
2146 for(sce= main->scene.first; sce; sce= sce->id.next) {
2148 lib_nodetree_do_versions_group(sce->nodetree);
2150 /* and texture trees */
2151 for(tx= main->tex.first; tx; tx= tx->id.next) {
2153 lib_nodetree_do_versions_group(tx->nodetree);
2156 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next)
2157 ntree->flag &= ~NTREE_DO_VERSIONS;
2160 /* now verify all types in material trees, groups are set OK now */
2161 for(ma= main->mat.first; ma; ma= ma->id.next) {
2163 ntreeVerifyTypes(ma->nodetree);
2165 /* and scene trees */
2166 for(sce= main->scene.first; sce; sce= sce->id.next) {
2168 ntreeVerifyTypes(sce->nodetree);
2170 /* and texture trees */
2171 for(tx= main->tex.first; tx; tx= tx->id.next) {
2173 ntreeVerifyTypes(tx->nodetree);
2179 /* ntree itself has been read! */
2180 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
2182 /* note: writing and reading goes in sync, for speed */
2187 ntree->init= 0; /* to set callbacks and force setting types */
2188 ntree->progress= NULL;
2190 ntree->adt= newdataadr(fd, ntree->adt);
2191 direct_link_animdata(fd, ntree->adt);
2193 link_list(fd, &ntree->nodes);
2194 for(node= ntree->nodes.first; node; node= node->next) {
2195 if(node->type == NODE_DYNAMIC) {
2197 node->custom1= BSET(node->custom1, NODE_DYNAMIC_LOADED);
2198 node->typeinfo= NULL;
2201 node->storage= newdataadr(fd, node->storage);
2204 /* could be handlerized at some point */
2205 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
2206 direct_link_curvemapping(fd, node->storage);
2207 else if(ntree->type==NTREE_COMPOSIT) {
2208 if( ELEM4(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB, CMP_NODE_HUECORRECT))
2209 direct_link_curvemapping(fd, node->storage);
2210 else if(ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
2211 ((ImageUser *)node->storage)->ok= 1;
2213 else if( ntree->type==NTREE_TEXTURE) {
2214 if(node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME)
2215 direct_link_curvemapping(fd, node->storage);
2216 else if(node->type==TEX_NODE_IMAGE)
2217 ((ImageUser *)node->storage)->ok= 1;
2220 link_list(fd, &node->inputs);
2221 link_list(fd, &node->outputs);
2223 link_list(fd, &ntree->links);
2225 /* external sockets */
2226 link_list(fd, &ntree->inputs);
2227 link_list(fd, &ntree->outputs);
2229 /* and we connect the rest */
2230 for(node= ntree->nodes.first; node; node= node->next) {
2231 node->preview= newimaadr(fd, node->preview);
2233 for(sock= node->inputs.first; sock; sock= sock->next)
2234 sock->link= newdataadr(fd, sock->link);
2235 for(sock= node->outputs.first; sock; sock= sock->next)
2236 sock->ns.data= NULL;
2238 for(sock= ntree->outputs.first; sock; sock= sock->next)
2239 sock->link= newdataadr(fd, sock->link);
2241 for(link= ntree->links.first; link; link= link->next) {
2242 link->fromnode= newdataadr(fd, link->fromnode);
2243 link->tonode= newdataadr(fd, link->tonode);
2244 link->fromsock= newdataadr(fd, link->fromsock);
2245 link->tosock= newdataadr(fd, link->tosock);
2248 /* type verification is in lib-link */
2251 /* ************ READ ARMATURE ***************** */
2253 /* temp struct used to transport needed info to lib_link_constraint_cb() */
2254 typedef struct tConstraintLinkData {
2257 } tConstraintLinkData;
2258 /* callback function used to relink constraint ID-links */
2259 static void lib_link_constraint_cb(bConstraint *UNUSED(con), ID **idpoin, void *userdata)
2261 tConstraintLinkData *cld= (tConstraintLinkData *)userdata;
2262 *idpoin = newlibadr(cld->fd, cld->id->lib, *idpoin);
2265 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
2267 tConstraintLinkData cld;
2271 for (con = conlist->first; con; con=con->next) {
2272 /* patch for error introduced by changing constraints (dunno how) */
2273 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
2274 if(con->data==NULL) {
2275 con->type= CONSTRAINT_TYPE_NULL;
2277 /* own ipo, all constraints have it */
2278 con->ipo= newlibadr_us(fd, id->lib, con->ipo); // XXX depreceated - old animation system
2281 /* relink all ID-blocks used by the constraints */
2285 id_loop_constraints(conlist, lib_link_constraint_cb, &cld);
2288 static void direct_link_constraints(FileData *fd, ListBase *lb)
2293 for (con=lb->first; con; con=con->next) {
2294 con->data = newdataadr(fd, con->data);
2296 switch (con->type) {
2297 case CONSTRAINT_TYPE_PYTHON:
2299 bPythonConstraint *data= con->data;
2301 link_list(fd, &data->targets);
2303 data->prop = newdataadr(fd, data->prop);
2305 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2308 case CONSTRAINT_TYPE_SPLINEIK:
2310 bSplineIKConstraint *data= con->data;
2312 data->points= newdataadr(fd, data->points);
2315 case CONSTRAINT_TYPE_KINEMATIC:
2317 con->lin_error = 0.f;
2318 con->rot_error = 0.f;
2320 case CONSTRAINT_TYPE_CHILDOF:
2322 /* XXX version patch, in older code this flag wasn't always set, and is inherent to type */
2323 if(con->ownspace == CONSTRAINT_SPACE_POSE)
2324 con->flag |= CONSTRAINT_SPACEONCE;
2331 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
2333 bPoseChannel *pchan;
2334 bArmature *arm= ob->data;
2341 /* always rebuild to match proxy or lib changes */
2342 rebuild= ob->proxy || (ob->id.lib==NULL && arm->id.lib);
2345 /* sync proxy layer */
2346 if(pose->proxy_layer)
2347 arm->layer = pose->proxy_layer;
2349 /* sync proxy active bone */
2350 if(pose->proxy_act_bone[0]) {
2351 Bone *bone = get_named_bone(arm, pose->proxy_act_bone);
2353 arm->act_bone = bone;
2357 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
2358 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
2360 /* hurms... loop in a loop, but yah... later... (ton) */
2361 pchan->bone= get_named_bone(arm, pchan->name);
2363 pchan->custom= newlibadr_us(fd, arm->id.lib, pchan->custom);
2364 if(pchan->bone==NULL)
2366 else if(ob->id.lib==NULL && arm->id.lib) {
2367 /* local pose selection copied to armature, bit hackish */
2368 pchan->bone->flag &= ~BONE_SELECTED;
2369 pchan->bone->flag |= pchan->selectflag;
2374 ob->recalc= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
2375 pose->flag |= POSE_RECALC;
2379 static void lib_link_armature(FileData *fd, Main *main)
2383 arm= main->armature.first;
2386 if(arm->id.flag & LIB_NEEDLINK) {
2387 if (arm->adt) lib_link_animdata(fd, &arm->id, arm->adt);
2388 arm->id.flag -= LIB_NEEDLINK;
2394 static void direct_link_bones(FileData *fd, Bone* bone)
2398 bone->parent= newdataadr(fd, bone->parent);
2399 bone->prop= newdataadr(fd, bone->prop);
2401 IDP_DirectLinkProperty(bone->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2403 bone->flag &= ~BONE_DRAW_ACTIVE;
2405 link_list(fd, &bone->childbase);
2407 for(child=bone->childbase.first; child; child=child->next)
2408 direct_link_bones(fd, child);
2411 static void direct_link_armature(FileData *fd, bArmature *arm)
2415 link_list(fd, &arm->bonebase);
2419 arm->adt= newdataadr(fd, arm->adt);
2420 direct_link_animdata(fd, arm->adt);
2422 bone=arm->bonebase.first;
2424 direct_link_bones(fd, bone);
2428 arm->act_bone= newdataadr(fd, arm->act_bone);
2429 arm->act_edbone= NULL;
2432 /* ************ READ CAMERA ***************** */
2434 static void lib_link_camera(FileData *fd, Main *main)
2438 ca= main->camera.first;
2440 if(ca->id.flag & LIB_NEEDLINK) {
2441 if (ca->adt) lib_link_animdata(fd, &ca->id, ca->adt);
2443 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo); // XXX depreceated - old animation system
2445 ca->dof_ob= newlibadr_us(fd, ca->id.lib, ca->dof_ob);
2447 ca->id.flag -= LIB_NEEDLINK;
2453 static void direct_link_camera(FileData *fd, Camera *ca)
2455 ca->adt= newdataadr(fd, ca->adt);
2456 direct_link_animdata(fd, ca->adt);
2460 /* ************ READ LAMP ***************** */
2462 static void lib_link_lamp(FileData *fd, Main *main)
2468 la= main->lamp.first;
2470 if(la->id.flag & LIB_NEEDLINK) {
2471 if (la->adt) lib_link_animdata(fd, &la->id, la->adt);
2473 for(a=0; a<MAX_MTEX; a++) {
2476 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
2477 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
2481 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo); // XXX depreceated - old animation system
2483 la->id.flag -= LIB_NEEDLINK;
2489 static void direct_link_lamp(FileData *fd, Lamp *la)
2493 la->adt= newdataadr(fd, la->adt);
2494 direct_link_animdata(fd, la->adt);
2496 for(a=0; a<MAX_MTEX; a++) {
2497 la->mtex[a]= newdataadr(fd, la->mtex[a]);
2500 la->curfalloff= newdataadr(fd, la->curfalloff);
2502 direct_link_curvemapping(fd, la->curfalloff);
2504 la->preview = direct_link_preview_image(fd, la->preview);
2507 /* ************ READ keys ***************** */
2509 static void lib_link_key(FileData *fd, Main *main)
2513 key= main->key.first;
2515 /*check if we need to generate unique ids for the shapekeys*/
2520 for (block=key->block.first; block; block=block->next) {
2521 block->uid = key->uidgen++;
2525 if(key->id.flag & LIB_NEEDLINK) {
2526 if(key->adt) lib_link_animdata(fd, &key->id, key->adt);
2528 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo); // XXX depreceated - old animation system
2529 key->from= newlibadr(fd, key->id.lib, key->from);
2531 key->id.flag -= LIB_NEEDLINK;
2537 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
2540 char *data, *poin, *cp;
2542 elemsize= key->elemsize;
2545 for(a=0; a<kb->totelem; a++) {
2550 while( cp[0] ) { /* cp[0]==amount */
2552 switch(cp[1]) { /* cp[1]= type */
2558 SWITCH_INT((*poin));
2571 static void direct_link_key(FileData *fd, Key *key)
2575 link_list(fd, &(key->block));
2577 key->adt= newdataadr(fd, key->adt);
2578 direct_link_animdata(fd, key->adt);
2580 key->refkey= newdataadr(fd, key->refkey);
2582 kb= key->block.first;
2585 kb->data= newdataadr(fd, kb->data);
2587 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
2588 switch_endian_keyblock(key, kb);
2594 /* ************ READ mball ***************** */
2596 static void lib_link_mball(FileData *fd, Main *main)
2601 mb= main->mball.first;
2603 if(mb->id.flag & LIB_NEEDLINK) {
2604 if (mb->adt) lib_link_animdata(fd, &mb->id, mb->adt);
2606 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
2608 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo); // XXX depreceated - old animation system
2610 mb->id.flag -= LIB_NEEDLINK;
2616 static void direct_link_mball(FileData *fd, MetaBall *mb)
2618 mb->adt= newdataadr(fd, mb->adt);
2619 direct_link_animdata(fd, mb->adt);
2621 mb->mat= newdataadr(fd, mb->mat);
2622 test_pointer_array(fd, (void **)&mb->mat);
2624 link_list(fd, &(mb->elems));
2626 mb->disp.first= mb->disp.last= NULL;
2627 mb->editelems= NULL;
2629 /* mb->edit_elems.first= mb->edit_elems.last= NULL;*/
2633 /* ************ READ WORLD ***************** */
2635 static void lib_link_world(FileData *fd, Main *main)
2641 wrld= main->world.first;
2643 if(wrld->id.flag & LIB_NEEDLINK) {
2644 if (wrld->adt) lib_link_animdata(fd, &wrld->id, wrld->adt);
2646 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo); // XXX depreceated - old animation system
2648 for(a=0; a<MAX_MTEX; a++) {
2649 mtex= wrld->mtex[a];
2651 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
2652 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
2656 wrld->id.flag -= LIB_NEEDLINK;
2658 wrld= wrld->id.next;
2662 static void direct_link_world(FileData *fd, World *wrld)
2666 wrld->adt= newdataadr(fd, wrld->adt);
2667 direct_link_animdata(fd, wrld->adt);
2669 for(a=0; a<MAX_MTEX; a++) {
2670 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
2672 wrld->preview = direct_link_preview_image(fd, wrld->preview);
2676 /* ************ READ VFONT ***************** */
2678 static void lib_link_vfont(FileData *UNUSED(fd), Main *main)
2682 vf= main->vfont.first;
2684 if(vf->id.flag & LIB_NEEDLINK) {
2685 vf->id.flag -= LIB_NEEDLINK;
2691 static void direct_link_vfont(FileData *fd, VFont *vf)
2694 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
2697 /* ************ READ TEXT ****************** */
2699 static void lib_link_text(FileData *UNUSED(fd), Main *main)
2703 text= main->text.first;
2705 if(text->id.flag & LIB_NEEDLINK) {
2706 text->id.flag -= LIB_NEEDLINK;
2708 text= text->id.next;
2712 static void direct_link_text(FileData *fd, Text *text)
2716 text->name= newdataadr(fd, text->name);
2719 text->undo_len= TXT_INIT_UNDO;
2720 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
2722 text->compiled= NULL;
2725 if(text->flags & TXT_ISEXT) {
2730 link_list(fd, &text->lines);
2731 link_list(fd, &text->markers);
2733 text->curl= newdataadr(fd, text->curl);
2734 text->sell= newdataadr(fd, text->sell);
2736 ln= text->lines.first;
2738 ln->line= newdataadr(fd, ln->line);
2741 if (ln->len != (int) strlen(ln->line)) {
2742 printf("Error loading text, line lengths differ\n");
2743 ln->len = strlen(ln->line);
2749 text->flags = (text->flags) & ~TXT_ISEXT;
2754 /* ************ READ IMAGE ***************** */
2756 static void lib_link_image(FileData *fd, Main *main)
2760 ima= main->image.first;
2762 if(ima->id.flag & LIB_NEEDLINK) {
2763 if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2765 ima->id.flag -= LIB_NEEDLINK;
2771 static void link_ibuf_list(FileData *fd, ListBase *lb)
2775 if(lb->first==NULL) return;
2777 lb->first= newimaadr(fd, lb->first);
2781 ln->next= newimaadr(fd, ln->next);
2789 static void direct_link_image(FileData *fd, Image *ima)
2791 /* for undo system, pointers could be restored */
2793 link_ibuf_list(fd, &ima->ibufs);
2795 ima->ibufs.first= ima->ibufs.last= NULL;
2797 /* if not restored, we keep the binded opengl index */
2798 if(ima->ibufs.first==NULL) {
2800 ima->gputexture= NULL;
2807 /* undo system, try to restore render buffers */
2811 for(a=0; a<IMA_MAX_RENDER_SLOT; a++)
2812 ima->renders[a]= newimaadr(fd, ima->renders[a]);
2815 memset(ima->renders, 0, sizeof(ima->renders));
2816 ima->last_render_slot= ima->render_slot;
2819 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
2820 ima->preview = direct_link_preview_image(fd, ima->preview);
2825 /* ************ READ CURVE ***************** */
2827 static void lib_link_curve(FileData *fd, Main *main)
2832 cu= main->curve.first;
2834 if(cu->id.flag & LIB_NEEDLINK) {
2835 if(cu->adt) lib_link_animdata(fd, &cu->id, cu->adt);
2837 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
2839 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
2840 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
2841 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
2842 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
2843 cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);
2844 cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
2845 cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
2847 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo); // XXX depreceated - old animation system
2848 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
2850 cu->id.flag -= LIB_NEEDLINK;
2857 static void switch_endian_knots(Nurb *nu)
2864 SWITCH_INT(nu->knotsu[len]);
2870 SWITCH_INT(nu->knotsv[len]);
2875 static void direct_link_curve(FileData *fd, Curve *cu)
2880 cu->adt= newdataadr(fd, cu->adt);
2881 direct_link_animdata(fd, cu->adt);
2883 cu->mat= newdataadr(fd, cu->mat);
2884 test_pointer_array(fd, (void **)&cu->mat);
2885 cu->str= newdataadr(fd, cu->str);
2886 cu->strinfo= newdataadr(fd, cu->strinfo);
2887 cu->tb= newdataadr(fd, cu->tb);
2889 if(cu->vfont == NULL) link_list(fd, &(cu->nurb));
2891 cu->nurb.first=cu->nurb.last= NULL;
2893 tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
2895 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
2902 cu->tb[0].w = cu->linewidth;
2904 if (cu->wordspace == 0.0f) cu->wordspace = 1.0f;
2907 cu->bev.first=cu->bev.last= NULL;
2908 cu->disp.first=cu->disp.last= NULL;
2916 nu->bezt= newdataadr(fd, nu->bezt);
2917 nu->bp= newdataadr(fd, nu->bp);
2918 nu->knotsu= newdataadr(fd, nu->knotsu);
2919 nu->knotsv= newdataadr(fd, nu->knotsv);
2920 if (cu->vfont == NULL) nu->charidx= nu->mat_nr;
2922 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2923 switch_endian_knots(nu);
2931 /* ************ READ TEX ***************** */
2933 static void lib_link_texture(FileData *fd, Main *main)
2937 tex= main->tex.first;
2939 if(tex->id.flag & LIB_NEEDLINK) {
2940 if(tex->adt) lib_link_animdata(fd, &tex->id, tex->adt);
2942 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
2943 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
2944 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
2946 tex->pd->object= newlibadr(fd, tex->id.lib, tex->pd->object);
2947 if(tex->vd) tex->vd->object= newlibadr(fd, tex->id.lib, tex->vd->object);
2950 lib_link_ntree(fd, &tex->id, tex->nodetree);
2952 tex->id.flag -= LIB_NEEDLINK;
2958 static void direct_link_texture(FileData *fd, Tex *tex)
2960 tex->adt= newdataadr(fd, tex->adt);
2961 direct_link_animdata(fd, tex->adt);
2963 tex->plugin= newdataadr(fd, tex->plugin);
2965 tex->plugin->handle= NULL;
2966 open_plugin_tex(tex->plugin);
2967 /* initialize data for this instance, if an initialization
2970 if (tex->plugin->instance_init)
2971 tex->plugin->instance_init((void *) tex->plugin->data);
2973 tex->coba= newdataadr(fd, tex->coba);
2974 tex->env= newdataadr(fd, tex->env);
2976 tex->env->ima= NULL;
2977 memset(tex->env->cube, 0, 6*sizeof(void *));
2980 tex->pd= newdataadr(fd, tex->pd);
2982 tex->pd->point_tree = NULL;
2983 tex->pd->coba= newdataadr(fd, tex->pd->coba);
2984 tex->pd->falloff_curve= newdataadr(fd, tex->pd->falloff_curve);
2985 if(tex->pd->falloff_curve) {
2986 direct_link_curvemapping(fd, tex->pd->falloff_curve);
2990 tex->vd= newdataadr(fd, tex->vd);
2992 tex->vd->dataset = NULL;
2996 tex->nodetree= newdataadr(fd, tex->nodetree);
2998 direct_link_nodetree(fd, tex->nodetree);
3000 tex->preview = direct_link_preview_image(fd, tex->preview);
3007 /* ************ READ MATERIAL ***************** */
3009 static void lib_link_material(FileData *fd, Main *main)
3015 ma= main->mat.first;
3017 if(ma->id.flag & LIB_NEEDLINK) {
3018 if(ma->adt) lib_link_animdata(fd, &ma->id, ma->adt);
3020 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
3021 of library blocks that implement this.*/
3022 if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
3024 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
3025 ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
3027 for(a=0; a<MAX_MTEX; a++) {
3030 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
3031 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
3036 lib_link_ntree(fd, &ma->id, ma->nodetree);
3038 ma->id.flag -= LIB_NEEDLINK;
3044 static void direct_link_material(FileData *fd, Material *ma)
3048 ma->adt= newdataadr(fd, ma->adt);
3049 direct_link_animdata(fd, ma->adt);
3051 for(a=0; a<MAX_MTEX; a++) {
3052 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
3055 ma->ramp_col= newdataadr(fd, ma->ramp_col);
3056 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
3058 ma->nodetree= newdataadr(fd, ma->nodetree);
3060 direct_link_nodetree(fd, ma->nodetree);
3062 ma->preview = direct_link_preview_image(fd, ma->preview);
3063 ma->gpumaterial.first = ma->gpumaterial.last = NULL;
3066 /* ************ READ PARTICLE SETTINGS ***************** */
3067 /* update this also to writefile.c */
3068 static const char *ptcache_data_struct[] = {
3069 "", // BPHYS_DATA_INDEX
3070 "", // BPHYS_DATA_LOCATION
3071 "", // BPHYS_DATA_VELOCITY
3072 "", // BPHYS_DATA_ROTATION
3073 "", // BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST */
3074 "", // BPHYS_DATA_SIZE:
3075 "", // BPHYS_DATA_TIMES:
3076 "BoidData" // case BPHYS_DATA_BOIDS:
3078 static void direct_link_pointcache(FileData *fd, PointCache *cache)
3080 if((cache->flag & PTCACHE_DISK_CACHE)==0) {
3082 PTCacheExtra *extra;
3085 link_list(fd, &cache->mem_cache);
3087 pm = cache->mem_cache.first;
3089 for(; pm; pm=pm->next) {
3090 for(i=0; i<BPHYS_TOT_DATA; i++) {
3091 pm->data[i] = newdataadr(fd, pm->data[i]);
3093 /* the cache saves non-struct data without DNA */
3094 if(pm->data[i] && strcmp(ptcache_data_struct[i], "")==0 && (fd->flags & FD_FLAGS_SWITCH_ENDIAN)) {
3095 int j, tot= (BKE_ptcache_data_size (i) * pm->totpoint)/4; /* data_size returns bytes */
3096 int *poin= pm->data[i];
3098 for(j= 0; j<tot; j++)
3099 SWITCH_INT(poin[j]);
3103 link_list(fd, &pm->extradata);
3105 for(extra=pm->extradata.first; extra; extra=extra->next)
3106 extra->data = newdataadr(fd, extra->data);
3110 cache->mem_cache.first = cache->mem_cache.last = NULL;
3112 cache->flag &= ~PTCACHE_SIMULATION_VALID;
3115 cache->free_edit= NULL;
3116 cache->cached_frames= NULL;
3119 static void direct_link_pointcache_list(FileData *fd, ListBase *ptcaches, PointCache **ocache, int force_disk)
3121 if(ptcaches->first) {
3122 PointCache *cache= NULL;
3123 link_list(fd, ptcaches);
3124 for(cache=ptcaches->first; cache; cache=cache->next) {
3125 direct_link_pointcache(fd, cache);
3127 cache->flag |= PTCACHE_DISK_CACHE;
3132 *ocache = newdataadr(fd, *ocache);
3135 /* old "single" caches need to be linked too */
3136 *ocache = newdataadr(fd, *ocache);
3137 direct_link_pointcache(fd, *ocache);
3139 (*ocache)->flag |= PTCACHE_DISK_CACHE;
3140 (*ocache)->step = 1;
3143 ptcaches->first = ptcaches->last = *ocache;
3147 static void lib_link_partdeflect(FileData *fd, ID *id, PartDeflect *pd)
3150 pd->tex=newlibadr_us(fd, id->lib, pd->tex);
3153 static void lib_link_particlesettings(FileData *fd, Main *main)
3155 ParticleSettings *part;
3156 ParticleDupliWeight *dw;
3160 part= main->particle.first;
3162 if(part->id.flag & LIB_NEEDLINK) {
3163 if (part->adt) lib_link_animdata(fd, &part->id, part->adt);
3164 part->ipo= newlibadr_us(fd, part->id.lib, part->ipo); // XXX depreceated - old animation system
3166 part->dup_ob = newlibadr(fd, part->id.lib, part->dup_ob);
3167 part->dup_group = newlibadr(fd, part->id.lib, part->dup_group);
3168 part->eff_group = newlibadr(fd, part->id.lib, part->eff_group);
3169 part->bb_ob = newlibadr(fd, part->id.lib, part->bb_ob);
3171 lib_link_partdeflect(fd, &part->id, part->pd);
3172 lib_link_partdeflect(fd, &part->id, part->pd2);
3174 if(part->effector_weights)
3175 part->effector_weights->group = newlibadr(fd, part->id.lib, part->effector_weights->group);
3177 if(part->dupliweights.first && part->dup_group) {
3179 /* check for old files without indices (all indexes 0) */
3180 dw = part->dupliweights.first;
3181 if(part->dupliweights.first == part->dupliweights.last) {
3182 /* special case for only one object in the group */
3186 for(; dw; dw=dw->next) {
3195 /* if we have indexes, let's use them */
3196 dw = part->dupliweights.first;