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_speaker_types.h"
89 #include "DNA_sound_types.h"
90 #include "DNA_space_types.h"
91 #include "DNA_vfont_types.h"
92 #include "DNA_world_types.h"
94 #include "MEM_guardedalloc.h"
96 #include "BLI_utildefines.h"
97 #include "BLI_blenlib.h"
99 #include "BLI_cellalloc.h"
100 #include "BLI_edgehash.h"
102 #include "BKE_anim.h"
103 #include "BKE_action.h"
104 #include "BKE_armature.h"
105 #include "BKE_brush.h"
106 #include "BKE_colortools.h"
107 #include "BKE_constraint.h"
108 #include "BKE_context.h"
109 #include "BKE_curve.h"
110 #include "BKE_deform.h"
111 #include "BKE_effect.h" /* give_parteff */
112 #include "BKE_fcurve.h"
113 #include "BKE_global.h" // for G
114 #include "BKE_group.h"
115 #include "BKE_image.h"
116 #include "BKE_lattice.h"
117 #include "BKE_library.h" // for which_libbase
118 #include "BKE_idcode.h"
119 #include "BKE_material.h"
120 #include "BKE_main.h" // for Main
121 #include "BKE_mesh.h" // for ME_ defines (patching)
122 #include "BKE_modifier.h"
123 #include "BKE_multires.h"
124 #include "BKE_node.h" // for tree type defines
125 #include "BKE_object.h"
126 #include "BKE_paint.h"
127 #include "BKE_particle.h"
128 #include "BKE_pointcache.h"
129 #include "BKE_property.h" // for get_ob_property
130 #include "BKE_report.h"
131 #include "BKE_sca.h" // for init_actuator
132 #include "BKE_scene.h"
133 #include "BKE_screen.h"
134 #include "BKE_sequencer.h"
135 #include "BKE_texture.h" // for open_plugin_tex
136 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
137 #include "BKE_sound.h"
139 #include "NOD_socket.h"
141 //XXX #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
142 //XXX #include "BIF_filelist.h" // badlevel too, where to move this? - elubie
143 //XXX #include "BIF_previewrender.h" // bedlelvel, for struct RenderInfo
144 #include "BLO_readfile.h"
145 #include "BLO_undofile.h"
147 #include "readfile.h"
149 #include "PIL_time.h"
154 Remark: still a weak point is the newaddress() function, that doesnt solve reading from
155 multiple files at the same time
157 (added remark: oh, i thought that was solved? will look at that... (ton)
160 - Existing Library (Main) push or free
170 - read associated 'direct data'
171 - link direct data (internal and to LibBlock)
173 - read USER data, only when indicated (file is ~/X.XX/startup.blend)
175 - per Library (per Main)
178 - find LibBlocks and attach IDs to Main
179 - if external LibBlock
181 - or it's already read,
186 - read associated direct data
187 - link direct data (internal and to LibBlock)
189 - per Library with unread LibBlocks
194 - read associated direct data
195 - link direct data (internal and to LibBlock)
198 - link all LibBlocks and indirect pointers to libblocks
199 - initialize FileGlobal and copy pointers to Global
202 /* also occurs in library.c */
203 /* GS reads the memory pointed at in a specific ordering. There are,
204 * however two definitions for it. I have jotted them down here, both,
205 * but I think the first one is actually used. The thing is that
206 * big-endian systems might read this the wrong way round. OTOH, we
207 * constructed the IDs that are read out with this macro explicitly as
208 * well. I expect we'll sort it out soon... */
211 #define GS(a) (*((short *)(a)))
213 /* from misc_util: flip the bytes from x */
214 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
216 // only used here in readfile.c
217 #define SWITCH_LONGINT(a) { \
220 s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
221 s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
222 s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
223 s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
227 typedef struct OldNew {
232 typedef struct OldNewMap {
234 int nentries, entriessize;
240 /* local prototypes */
241 static void *read_struct(FileData *fd, BHead *bh, const char *blockname);
242 static void direct_link_modifiers(FileData *fd, ListBase *lb);
244 static OldNewMap *oldnewmap_new(void)
246 OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
248 onm->entriessize= 1024;
249 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
254 static int verg_oldnewmap(const void *v1, const void *v2)
256 const struct OldNew *x1=v1, *x2=v2;
258 if( x1->old > x2->old) return 1;
259 else if( x1->old < x2->old) return -1;
264 static void oldnewmap_sort(FileData *fd)
266 qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
267 fd->libmap->sorted= 1;
270 /* nr is zero for data, and ID code for libdata */
271 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
275 if(oldaddr==NULL || newaddr==NULL) return;
277 if (onm->nentries==onm->entriessize) {
278 int osize= onm->entriessize;
279 OldNew *oentries= onm->entries;
281 onm->entriessize*= 2;
282 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
284 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
288 entry= &onm->entries[onm->nentries++];
290 entry->newp= newaddr;
294 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
298 if(addr==NULL) return NULL;
300 if (onm->lasthit<onm->nentries-1) {
301 OldNew *entry= &onm->entries[++onm->lasthit];
303 if (entry->old==addr) {
309 for (i=0; i<onm->nentries; i++) {
310 OldNew *entry= &onm->entries[i];
312 if (entry->old==addr) {
323 /* for libdata, nr has ID code, no increment */
324 static void *oldnewmap_liblookup(OldNewMap *onm, void *addr, void *lib)
328 if(addr==NULL) return NULL;
330 /* lasthit works fine for non-libdata, linking there is done in same sequence as writing */
332 OldNew entry_s, *entry;
336 entry= bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
340 if (id && (!lib || id->lib)) {
346 for (i=0; i<onm->nentries; i++) {
347 OldNew *entry= &onm->entries[i];
349 if (entry->old==addr) {
352 if (id && (!lib || id->lib)) {
361 static void oldnewmap_free_unused(OldNewMap *onm)
365 for (i=0; i<onm->nentries; i++) {
366 OldNew *entry= &onm->entries[i];
368 MEM_freeN(entry->newp);
374 static void oldnewmap_clear(OldNewMap *onm)
380 static void oldnewmap_free(OldNewMap *onm)
382 MEM_freeN(onm->entries);
388 static void read_libraries(FileData *basefd, ListBase *mainlist);
390 /* ************ help functions ***************** */
392 static void add_main_to_main(Main *mainvar, Main *from)
394 ListBase *lbarray[MAX_LIBARRAY], *fromarray[MAX_LIBARRAY];
397 set_listbasepointers(mainvar, lbarray);
398 a= set_listbasepointers(from, fromarray);
400 BLI_movelisttolist(lbarray[a], fromarray[a]);
404 void blo_join_main(ListBase *mainlist)
406 Main *tojoin, *mainl;
409 mainl= mainlist->first;
410 while ((tojoin= mainl->next)) {
411 add_main_to_main(mainl, tojoin);
412 BLI_remlink(mainlist, tojoin);
417 static void split_libdata(ListBase *lb, Main *first)
429 if(mainvar->curlib==id->lib) {
430 lbn= which_libbase(mainvar, GS(id->name));
432 BLI_addtail(lbn, id);
435 mainvar= mainvar->next;
437 if(mainvar==NULL) printf("error split_libdata\n");
443 void blo_split_main(ListBase *mainlist, Main *main)
445 ListBase *lbarray[MAX_LIBARRAY];
449 mainlist->first= mainlist->last= main;
452 if(main->library.first==NULL)
455 for (lib= main->library.first; lib; lib= lib->id.next) {
456 Main *libmain= MEM_callocN(sizeof(Main), "libmain");
457 libmain->curlib= lib;
458 BLI_addtail(mainlist, libmain);
461 i= set_listbasepointers(main, lbarray);
463 split_libdata(lbarray[i], main->next);
466 /* removes things like /blah/blah/../../blah/ etc, then writes in *name the full path */
467 static void cleanup_path(const char *relabase, char *name)
469 char filename[FILE_MAXFILE];
471 BLI_splitdirstring(name, filename);
472 BLI_cleanup_dir(relabase, name);
473 strcat(name, filename);
476 static void read_file_version(FileData *fd, Main *main)
480 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
481 if (bhead->code==GLOB) {
482 FileGlobal *fg= read_struct(fd, bhead, "Global");
484 main->subversionfile= fg->subversion;
485 main->minversionfile= fg->minversion;
486 main->minsubversionfile= fg->minsubversion;
489 else if (bhead->code==ENDB)
496 static Main *blo_find_main(FileData *fd, ListBase *mainlist, const char *filepath, const char *relabase)
500 char name1[FILE_MAXDIR+FILE_MAXFILE];
502 BLI_strncpy(name1, filepath, sizeof(name1));
503 cleanup_path(relabase, name1);
504 // printf("blo_find_main: original in %s\n", name);
505 // printf("blo_find_main: converted to %s\n", name1);
507 for (m= mainlist->first; m; m= m->next) {
508 char *libname= (m->curlib)?m->curlib->filepath:m->name;
510 if (BLI_path_cmp(name1, libname) == 0) {
511 if(G.f & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
516 m= MEM_callocN(sizeof(Main), "find_main");
517 BLI_addtail(mainlist, m);
519 lib= alloc_libblock(&m->library, ID_LI, "lib");
520 strncpy(lib->name, filepath, sizeof(lib->name)-1);
521 BLI_strncpy(lib->filepath, name1, sizeof(lib->filepath));
525 read_file_version(fd, m);
527 if(G.f & G_DEBUG) printf("blo_find_main: added new lib %s\n", filepath);
532 /* ************ FILE PARSING ****************** */
534 static void switch_endian_bh4(BHead4 *bhead)
536 /* the ID_.. codes */
537 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
539 if (bhead->code != ENDB) {
540 SWITCH_INT(bhead->len);
541 SWITCH_INT(bhead->SDNAnr);
542 SWITCH_INT(bhead->nr);
546 static void switch_endian_bh8(BHead8 *bhead)
548 /* the ID_.. codes */
549 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
551 if (bhead->code != ENDB) {
552 SWITCH_INT(bhead->len);
553 SWITCH_INT(bhead->SDNAnr);
554 SWITCH_INT(bhead->nr);
558 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
560 BHead4 *bhead4 = (BHead4 *) bhead;
561 #if defined(WIN32) && !defined(FREE_WINDOWS)
567 bhead4->code= bhead8->code;
568 bhead4->len= bhead8->len;
570 if (bhead4->code != ENDB) {
572 //perform a endian swap on 64bit pointers, otherwise the pointer might map to zero
573 //0x0000000000000000000012345678 would become 0x12345678000000000000000000000000
574 if (do_endian_swap) {
575 SWITCH_LONGINT(bhead8->old);
578 /* this patch is to avoid a long long being read from not-eight aligned positions
579 is necessary on any modern 64bit architecture) */
580 memcpy(&old, &bhead8->old, 8);
581 bhead4->old = (int) (old >> 3);
583 bhead4->SDNAnr= bhead8->SDNAnr;
584 bhead4->nr= bhead8->nr;
588 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
590 BHead8 *bhead8 = (BHead8 *) bhead;
592 bhead8->code= bhead4->code;
593 bhead8->len= bhead4->len;
595 if (bhead8->code != ENDB) {
596 bhead8->old= bhead4->old;
597 bhead8->SDNAnr= bhead4->SDNAnr;
598 bhead8->nr= bhead4->nr;
602 static BHeadN *get_bhead(FileData *fd)
604 BHeadN *new_bhead = NULL;
609 /* initializing to zero isn't strictly needed but shuts valgrind up
610 * since uninitialized memory gets compared */
615 // First read the bhead structure.
616 // Depending on the platform the file was written on this can
617 // be a big or little endian BHead4 or BHead8 structure.
619 // As usual 'ENDB' (the last *partial* bhead of the file)
620 // needs some special handling. We don't want to EOF just yet.
622 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
624 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
626 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
627 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
628 switch_endian_bh4(&bhead4);
631 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
632 bh8_from_bh4(&bhead, &bhead4);
634 memcpy(&bhead, &bhead4, sizeof(bhead));
642 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
644 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
645 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
646 switch_endian_bh8(&bhead8);
649 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
650 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
652 memcpy(&bhead, &bhead8, sizeof(bhead));
660 /* make sure people are not trying to pass bad blend files */
661 if (bhead.len < 0) fd->eof = 1;
663 // bhead now contains the (converted) bhead structure. Now read
664 // the associated data and put everything in a BHeadN (creative naming !)
667 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
669 new_bhead->next = new_bhead->prev = NULL;
670 new_bhead->bhead = bhead;
672 readsize = fd->read(fd, new_bhead + 1, bhead.len);
674 if (readsize != bhead.len) {
676 MEM_freeN(new_bhead);
686 // We've read a new block. Now add it to the list
690 BLI_addtail(&fd->listbase, new_bhead);
696 BHead *blo_firstbhead(FileData *fd)
702 // Read in a new block if necessary
704 new_bhead = fd->listbase.first;
705 if (new_bhead == NULL) {
706 new_bhead = get_bhead(fd);
710 bhead = &new_bhead->bhead;
716 BHead *blo_prevbhead(FileData *UNUSED(fd), BHead *thisblock)
718 BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
719 BHeadN *prev= bheadn->prev;
721 return prev?&prev->bhead:NULL;
724 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
726 BHeadN *new_bhead = NULL;
730 // bhead is actually a sub part of BHeadN
731 // We calculate the BHeadN pointer from the BHead pointer below
732 new_bhead = (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
734 // get the next BHeadN. If it doesn't exist we read in the next one
735 new_bhead = new_bhead->next;
736 if (new_bhead == NULL) {
737 new_bhead = get_bhead(fd);
742 // here we do the reverse:
743 // go from the BHeadN pointer to the BHead pointer
744 bhead = &new_bhead->bhead;
750 static void decode_blender_header(FileData *fd)
752 char header[SIZEOFBLENDERHEADER], num[4];
755 // read in the header data
756 readsize = fd->read(fd, header, sizeof(header));
758 if (readsize == sizeof(header)) {
759 if(strncmp(header, "BLENDER", 7) == 0) {
760 int remove_this_endian_test= 1;
762 fd->flags |= FD_FLAGS_FILE_OK;
764 // what size are pointers in the file ?
766 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
767 if (sizeof(void *) != 4) {
768 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
771 if (sizeof(void *) != 8) {
772 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
776 // is the file saved in a different endian
778 if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
779 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
782 // get the version number
784 memcpy(num, header+9, 3);
786 fd->fileversion = atoi(num);
791 static int read_file_dna(FileData *fd)
795 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
796 if (bhead->code==DNA1) {
797 int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
799 fd->filesdna= DNA_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
802 fd->compflags= DNA_struct_get_compareflags(fd->filesdna, fd->memsdna);
803 /* used to retrieve ID names from (bhead+1) */
804 fd->id_name_offs= DNA_elem_offset(fd->filesdna, "ID", "char", "name[]");
808 } else if (bhead->code==ENDB)
815 static int fd_read_from_file(FileData *filedata, void *buffer, unsigned int size)
817 int readsize = read(filedata->filedes, buffer, size);
822 filedata->seek += readsize;
828 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, unsigned int size)
830 int readsize = gzread(filedata->gzfiledes, buffer, size);
835 filedata->seek += readsize;
841 static int fd_read_from_memory(FileData *filedata, void *buffer, unsigned int size)
843 // don't read more bytes then there are available in the buffer
844 int readsize = (int)MIN2(size, (unsigned int)(filedata->buffersize - filedata->seek));
846 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
847 filedata->seek += readsize;
852 static int fd_read_from_memfile(FileData *filedata, void *buffer, unsigned int size)
854 static unsigned int seek= 1<<30; /* the current position */
855 static unsigned int offset= 0; /* size of previous chunks */
856 static MemFileChunk *chunk=NULL;
857 unsigned int chunkoffset, readsize, totread;
859 if(size==0) return 0;
861 if(seek != (unsigned int)filedata->seek) {
862 chunk= filedata->memfile->chunks.first;
866 if(seek + chunk->size > (unsigned) filedata->seek) break;
871 seek= filedata->seek;
878 /* first check if it's on the end if current chunk */
879 if(seek-offset == chunk->size) {
880 offset+= chunk->size;
884 /* debug, should never happen */
886 printf("illegal read, chunk zero\n");
890 chunkoffset= seek-offset;
891 readsize= size-totread;
893 /* data can be spread over multiple chunks, so clamp size
894 * to within this chunk, and then it will read further in
896 if(chunkoffset+readsize > chunk->size)
897 readsize= chunk->size-chunkoffset;
899 memcpy((char*)buffer+totread, chunk->buf+chunkoffset, readsize);
901 filedata->seek += readsize;
903 } while(totread < size);
911 static FileData *filedata_new(void)
913 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
916 fd->gzfiledes = NULL;
918 /* XXX, this doesn't need to be done all the time,
919 * but it keeps us reentrant, remove once we have
920 * a lib that provides a nice lock. - zr
922 fd->memsdna = DNA_sdna_from_data(DNAstr, DNAlen, 0);
924 fd->datamap = oldnewmap_new();
925 fd->globmap = oldnewmap_new();
926 fd->libmap = oldnewmap_new();
931 static FileData *blo_decode_and_check(FileData *fd, ReportList *reports)
933 decode_blender_header(fd);
935 if (fd->flags & FD_FLAGS_FILE_OK) {
936 if (!read_file_dna(fd)) {
937 BKE_reportf(reports, RPT_ERROR, "Failed to read blend file: \"%s\", incomplete", fd->relabase);
938 blo_freefiledata(fd);
943 BKE_reportf(reports, RPT_ERROR, "Failed to read blend file: \"%s\", not a blend file", fd->relabase);
944 blo_freefiledata(fd);
951 /* cannot be called with relative paths anymore! */
952 /* on each new library added, it now checks for the current FileData and expands relativeness */
953 FileData *blo_openblenderfile(const char *filepath, ReportList *reports)
957 gzfile= gzopen(filepath, "rb");
959 if (gzfile == (gzFile)Z_NULL) {
960 BKE_reportf(reports, RPT_ERROR, "Unable to open \"%s\": %s.", filepath, errno ? strerror(errno) : "Unknown error reading file");
963 FileData *fd = filedata_new();
964 fd->gzfiledes = gzfile;
965 fd->read = fd_read_gzip_from_file;
967 /* needed for library_append and read_libraries */
968 BLI_strncpy(fd->relabase, filepath, sizeof(fd->relabase));
970 return blo_decode_and_check(fd, reports);
974 FileData *blo_openblendermemory(void *mem, int memsize, ReportList *reports)
976 if (!mem || memsize<SIZEOFBLENDERHEADER) {
977 BKE_report(reports, RPT_ERROR, (mem)? "Unable to read": "Unable to open");
980 FileData *fd= filedata_new();
982 fd->buffersize= memsize;
983 fd->read= fd_read_from_memory;
984 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
986 return blo_decode_and_check(fd, reports);
990 FileData *blo_openblendermemfile(MemFile *memfile, ReportList *reports)
993 BKE_report(reports, RPT_ERROR, "Unable to open blend <memory>");
996 FileData *fd= filedata_new();
997 fd->memfile= memfile;
999 fd->read= fd_read_from_memfile;
1000 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
1002 return blo_decode_and_check(fd, reports);
1007 void blo_freefiledata(FileData *fd)
1011 if (fd->filedes != -1) {
1015 if (fd->gzfiledes != NULL)
1017 gzclose(fd->gzfiledes);
1020 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
1021 MEM_freeN(fd->buffer);
1025 // Free all BHeadN data blocks
1026 BLI_freelistN(&fd->listbase);
1029 DNA_sdna_free(fd->memsdna);
1031 DNA_sdna_free(fd->filesdna);
1033 MEM_freeN(fd->compflags);
1036 oldnewmap_free(fd->datamap);
1038 oldnewmap_free(fd->globmap);
1040 oldnewmap_free(fd->imamap);
1041 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
1042 oldnewmap_free(fd->libmap);
1044 MEM_freeN(fd->bheadmap);
1050 /* ************ DIV ****************** */
1052 int BLO_has_bfile_extension(char *str)
1054 return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend") || BLI_testextensie(str, ".blend.gz"));
1057 int BLO_is_a_library(const char *path, char *dir, char *group)
1059 /* return ok when a blenderfile, in dir is the filename,
1060 * in group the type of libdata
1068 if( dir[len-1] != '/' && dir[len-1] != '\\') return 0;
1073 /* Find the last slash */
1074 fd= BLI_last_slash(dir);
1076 if(fd==NULL) return 0;
1078 if(BLO_has_bfile_extension(fd+1)) {
1079 /* the last part of the dir is a .blend file, no group follows */
1080 *fd= '/'; /* put back the removed slash separating the dir and the .blend file name */
1083 char *gp = fd+1; // in case we have a .blend file, gp points to the group
1085 /* Find the last slash */
1086 fd= BLI_last_slash(dir);
1087 if (!fd || !BLO_has_bfile_extension(fd+1)) return 0;
1089 /* now we know that we are in a blend file and it is safe to
1090 assume that gp actually points to a group */
1091 if (strcmp("Screen", gp)!=0)
1092 BLI_strncpy(group, gp, GROUP_MAX);
1097 /* ************** OLD POINTERS ******************* */
1099 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1101 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1104 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1106 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1109 static void *newimaadr(FileData *fd, void *adr) /* used to restore image data after undo */
1111 if(fd->imamap && adr)
1112 return oldnewmap_lookup_and_inc(fd->imamap, adr);
1117 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1119 return oldnewmap_liblookup(fd->libmap, adr, lib);
1122 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1124 ID *id= newlibadr(fd, lib, adr);
1132 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1136 for (i=0; i<fd->libmap->nentries; i++) {
1137 OldNew *entry= &fd->libmap->entries[i];
1139 if (old==entry->newp && entry->nr==ID_ID) {
1141 if(new) entry->nr= GS( ((ID *)new)->name );
1147 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1151 for(mainptr= mainlist->first; mainptr; mainptr= mainptr->next) {
1154 if(mainptr->curlib) fd= mainptr->curlib->filedata;
1158 change_idid_adr_fd(fd, old, new);
1163 /* lib linked proxy objects point to our local data, we need
1164 * to clear that pointer before reading the undo memfile since
1165 * the object might be removed, it is set again in reading
1166 * if the local object still exists */
1167 void blo_clear_proxy_pointers_from_lib(Main *oldmain)
1169 Object *ob= oldmain->object.first;
1171 for(;ob; ob= ob->id.next)
1173 ob->proxy_from= NULL;
1176 void blo_make_image_pointer_map(FileData *fd, Main *oldmain)
1178 Image *ima= oldmain->image.first;
1179 Scene *sce= oldmain->scene.first;
1182 fd->imamap= oldnewmap_new();
1184 for(;ima; ima= ima->id.next) {
1185 Link *ibuf= ima->ibufs.first;
1186 for(; ibuf; ibuf= ibuf->next)
1187 oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
1189 oldnewmap_insert(fd->imamap, ima->gputexture, ima->gputexture, 0);
1190 for(a=0; a<IMA_MAX_RENDER_SLOT; a++)
1192 oldnewmap_insert(fd->imamap, ima->renders[a], ima->renders[a], 0);
1194 for(; sce; sce= sce->id.next) {
1197 for(node= sce->nodetree->nodes.first; node; node= node->next)
1198 oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
1203 /* set old main image ibufs to zero if it has been restored */
1204 /* this works because freeing old main only happens after this call */
1205 void blo_end_image_pointer_map(FileData *fd, Main *oldmain)
1207 OldNew *entry= fd->imamap->entries;
1208 Image *ima= oldmain->image.first;
1209 Scene *sce= oldmain->scene.first;
1212 /* used entries were restored, so we put them to zero */
1213 for (i=0; i<fd->imamap->nentries; i++, entry++) {
1218 for(;ima; ima= ima->id.next) {
1221 /* this mirrors direct_link_image */
1222 for(ibuf= ima->ibufs.first; ibuf; ibuf= next) {
1224 if(NULL==newimaadr(fd, ibuf)) { /* so was restored */
1225 BLI_remlink(&ima->ibufs, ibuf);
1227 ima->gputexture= NULL;
1230 for(i=0; i<IMA_MAX_RENDER_SLOT; i++)
1231 ima->renders[i]= newimaadr(fd, ima->renders[i]);
1233 ima->gputexture= newimaadr(fd, ima->gputexture);
1235 for(; sce; sce= sce->id.next) {
1238 for(node= sce->nodetree->nodes.first; node; node= node->next)
1239 node->preview= newimaadr(fd, node->preview);
1244 /* undo file support: add all library pointers in lookup */
1245 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
1247 Main *ptr= mainlist->first;
1248 ListBase *lbarray[MAX_LIBARRAY];
1250 for(ptr= ptr->next; ptr; ptr= ptr->next) {
1251 int i= set_listbasepointers(ptr, lbarray);
1254 for(id= lbarray[i]->first; id; id= id->next)
1255 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
1261 /* ********** END OLD POINTERS ****************** */
1262 /* ********** READ FILE ****************** */
1264 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1266 int blocksize, nblocks;
1269 data= (char *)(bhead+1);
1270 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1274 DNA_struct_switch_endian(filesdna, bhead->SDNAnr, data);
1280 static void *read_struct(FileData *fd, BHead *bh, const char *blockname)
1285 /* switch is based on file dna */
1286 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1287 switch_endian_structs(fd->filesdna, bh);
1289 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1290 if(fd->compflags[bh->SDNAnr]==2) {
1291 temp= DNA_struct_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1293 temp= MEM_mallocN(bh->len, blockname);
1294 memcpy(temp, (bh+1), bh->len);
1302 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1306 if(lb->first==NULL) return;
1308 lb->first= newdataadr(fd, lb->first);
1312 ln->next= newdataadr(fd, ln->next);
1320 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1325 if(lb->first==NULL) return;
1326 poin= newdataadr(fd, lb->first);
1328 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1335 poin= newdataadr(fd, ln->next);
1337 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1347 static void test_pointer_array(FileData *fd, void **mat)
1349 #if defined(WIN32) && !defined(FREE_WINDOWS)
1350 __int64 *lpoin, *lmat;
1352 long long *lpoin, *lmat;
1357 /* manually convert the pointer array in
1358 * the old dna format to a pointer array in
1359 * the new dna format.
1362 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1364 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1365 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1369 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1370 SWITCH_LONGINT(*lpoin);
1371 *ipoin= (int) ((*lpoin) >> 3);
1379 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1380 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1394 /* ************ READ ID Properties *************** */
1396 static void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1397 static void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1399 static void IDP_DirectLinkIDPArray(IDProperty *prop, int switch_endian, FileData *fd)
1404 /*since we didn't save the extra buffer, set totallen to len.*/
1405 prop->totallen = prop->len;
1406 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1408 array= (IDProperty*) prop->data.pointer;
1410 /* note!, idp-arrays didn't exist in 2.4x, so the pointer will be cleared
1411 * theres not really anything we can do to correct this, at least dont crash */
1418 for(i=0; i<prop->len; i++)
1419 IDP_DirectLinkProperty(&array[i], switch_endian, fd);
1422 static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, FileData *fd)
1427 /*since we didn't save the extra buffer, set totallen to len.*/
1428 prop->totallen = prop->len;
1429 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1431 if(prop->subtype == IDP_GROUP) {
1432 test_pointer_array(fd, prop->data.pointer);
1433 array= prop->data.pointer;
1435 for(i=0; i<prop->len; i++)
1436 IDP_DirectLinkProperty(array[i], switch_endian, fd);
1438 else if(prop->subtype == IDP_DOUBLE) {
1439 if (switch_endian) {
1440 for (i=0; i<prop->len; i++) {
1441 SWITCH_LONGINT(((double*)prop->data.pointer)[i]);
1446 if (switch_endian) {
1447 for (i=0; i<prop->len; i++) {
1448 SWITCH_INT(((int*)prop->data.pointer)[i]);
1454 static void IDP_DirectLinkString(IDProperty *prop, FileData *fd)
1456 /*since we didn't save the extra string buffer, set totallen to len.*/
1457 prop->totallen = prop->len;
1458 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1461 static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, FileData *fd)
1463 ListBase *lb = &prop->data.group;
1468 /*Link child id properties now*/
1469 for (loop=prop->data.group.first; loop; loop=loop->next) {
1470 IDP_DirectLinkProperty(loop, switch_endian, fd);
1474 static void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1476 switch (prop->type) {
1478 IDP_DirectLinkGroup(prop, switch_endian, fd);
1481 IDP_DirectLinkString(prop, fd);
1484 IDP_DirectLinkArray(prop, switch_endian, fd);
1487 IDP_DirectLinkIDPArray(prop, switch_endian, fd);
1490 /*erg, stupid doubles. since I'm storing them
1491 in the same field as int val; val2 in the
1492 IDPropertyData struct, they have to deal with
1493 endianness specifically
1495 in theory, val and val2 would've already been swapped
1496 if switch_endian is true, so we have to first unswap
1497 them then reswap them as a single 64-bit entity.
1500 if (switch_endian) {
1501 SWITCH_INT(prop->data.val);
1502 SWITCH_INT(prop->data.val2);
1503 SWITCH_LONGINT(prop->data.val);
1511 static void IDP_LibLinkProperty(IDProperty *UNUSED(prop), int UNUSED(switch_endian), FileData *UNUSED(fd))
1515 /* ************ READ CurveMapping *************** */
1517 /* cuma itself has been read! */
1518 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1522 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1523 cumap->flag &= ~CUMA_PREMULLED;
1525 for(a=0; a<CM_TOT; a++) {
1526 cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
1527 cumap->cm[a].table= NULL;
1528 cumap->cm[a].premultable= NULL;
1532 /* ************ READ Brush *************** */
1533 /* library brush linking after fileread */
1534 static void lib_link_brush(FileData *fd, Main *main)
1538 /* only link ID pointers */
1539 for(brush= main->brush.first; brush; brush= brush->id.next) {
1540 if(brush->id.flag & LIB_NEEDLINK) {
1541 brush->id.flag -= LIB_NEEDLINK;
1543 brush->mtex.tex= newlibadr_us(fd, brush->id.lib, brush->mtex.tex);
1544 brush->clone.image= newlibadr_us(fd, brush->id.lib, brush->clone.image);
1549 static void direct_link_brush(FileData *fd, Brush *brush)
1551 /* brush itself has been read */
1554 brush->curve= newdataadr(fd, brush->curve);
1556 direct_link_curvemapping(fd, brush->curve);
1558 brush_curve_preset(brush, CURVE_PRESET_SHARP);
1560 brush->preview= NULL;
1561 brush->icon_imbuf= NULL;
1564 static void direct_link_script(FileData *UNUSED(fd), Script *script)
1567 SCRIPT_SET_NULL(script)
1571 /* ************ READ PACKEDFILE *************** */
1573 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1575 PackedFile *pf= newdataadr(fd, oldpf);
1578 pf->data= newdataadr(fd, pf->data);
1584 /* ************ READ IMAGE PREVIEW *************** */
1586 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
1588 PreviewImage *prv= newdataadr(fd, old_prv);
1592 for (i=0; i < NUM_ICON_SIZES; ++i) {
1594 prv->rect[i] = newdataadr(fd, prv->rect[i]);
1602 /* ************ READ ANIMATION STUFF ***************** */
1604 /* Legacy Data Support (for Version Patching) ----------------------------- */
1606 // XXX depreceated - old animation system
1607 static void lib_link_ipo(FileData *fd, Main *main)
1611 ipo= main->ipo.first;
1613 if(ipo->id.flag & LIB_NEEDLINK) {
1615 for(icu= ipo->curve.first; icu; icu= icu->next) {
1617 icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
1619 ipo->id.flag -= LIB_NEEDLINK;
1625 // XXX depreceated - old animation system
1626 static void direct_link_ipo(FileData *fd, Ipo *ipo)
1630 link_list(fd, &(ipo->curve));
1631 icu= ipo->curve.first;
1633 icu->bezt= newdataadr(fd, icu->bezt);
1634 icu->bp= newdataadr(fd, icu->bp);
1635 icu->driver= newdataadr(fd, icu->driver);
1640 // XXX depreceated - old animation system
1641 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1643 bActionStrip *strip;
1644 bActionModifier *amod;
1646 for (strip=striplist->first; strip; strip=strip->next){
1647 strip->object = newlibadr(fd, id->lib, strip->object);
1648 strip->act = newlibadr_us(fd, id->lib, strip->act);
1649 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1650 for(amod= strip->modifiers.first; amod; amod= amod->next)
1651 amod->ob= newlibadr(fd, id->lib, amod->ob);
1655 // XXX depreceated - old animation system
1656 static void direct_link_nlastrips(FileData *fd, ListBase *strips)
1658 bActionStrip *strip;
1660 link_list(fd, strips);
1662 for(strip= strips->first; strip; strip= strip->next)
1663 link_list(fd, &strip->modifiers);
1666 // XXX depreceated - old animation system
1667 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1669 bConstraintChannel *chan;
1671 for (chan=chanbase->first; chan; chan=chan->next){
1672 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1676 /* Data Linking ----------------------------- */
1678 static void lib_link_fmodifiers(FileData *fd, ID *id, ListBase *list)
1682 for (fcm= list->first; fcm; fcm= fcm->next) {
1683 /* data for specific modifiers */
1684 switch (fcm->type) {
1685 case FMODIFIER_TYPE_PYTHON:
1687 FMod_Python *data= (FMod_Python *)fcm->data;
1688 data->script = newlibadr(fd, id->lib, data->script);
1695 static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list)
1702 /* relink ID-block references... */
1703 for (fcu= list->first; fcu; fcu= fcu->next) {
1706 ChannelDriver *driver= fcu->driver;
1709 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1710 DRIVER_TARGETS_LOOPER(dvar)
1712 /* only relink if still used */
1713 if (tarIndex < dvar->num_targets)
1714 dtar->id= newlibadr(fd, id->lib, dtar->id);
1718 DRIVER_TARGETS_LOOPER_END
1723 lib_link_fmodifiers(fd, id, &fcu->modifiers);
1728 /* NOTE: this assumes that link_list has already been called on the list */
1729 static void direct_link_fmodifiers(FileData *fd, ListBase *list)
1733 for (fcm= list->first; fcm; fcm= fcm->next) {
1734 /* relink general data */
1735 fcm->data = newdataadr(fd, fcm->data);
1738 /* do relinking of data for specific types */
1739 switch (fcm->type) {
1740 case FMODIFIER_TYPE_GENERATOR:
1742 FMod_Generator *data= (FMod_Generator *)fcm->data;
1744 data->coefficients= newdataadr(fd, data->coefficients);
1746 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1748 for(a = 0; a < data->arraysize; a++)
1749 SWITCH_INT(data->coefficients[a]);
1753 case FMODIFIER_TYPE_ENVELOPE:
1755 FMod_Envelope *data= (FMod_Envelope *)fcm->data;
1757 data->data= newdataadr(fd, data->data);
1760 case FMODIFIER_TYPE_PYTHON:
1762 FMod_Python *data= (FMod_Python *)fcm->data;
1764 data->prop = newdataadr(fd, data->prop);
1765 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1772 /* NOTE: this assumes that link_list has already been called on the list */
1773 static void direct_link_fcurves(FileData *fd, ListBase *list)
1777 /* link F-Curve data to F-Curve again (non ID-libs) */
1778 for (fcu= list->first; fcu; fcu= fcu->next) {
1780 fcu->bezt= newdataadr(fd, fcu->bezt);
1781 fcu->fpt= newdataadr(fd, fcu->fpt);
1784 fcu->rna_path= newdataadr(fd, fcu->rna_path);
1787 fcu->grp= newdataadr(fd, fcu->grp);
1790 fcu->driver= newdataadr(fd, fcu->driver);
1792 ChannelDriver *driver= fcu->driver;
1795 driver->expr_comp= NULL;
1797 /* relink variables, targets and their paths */
1798 link_list(fd, &driver->variables);
1799 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1800 DRIVER_TARGETS_LOOPER(dvar)
1802 /* only relink the targets being used */
1803 if (tarIndex < dvar->num_targets)
1804 dtar->rna_path= newdataadr(fd, dtar->rna_path);
1806 dtar->rna_path= NULL;
1808 DRIVER_TARGETS_LOOPER_END
1813 link_list(fd, &fcu->modifiers);
1814 direct_link_fmodifiers(fd, &fcu->modifiers);
1819 static void lib_link_action(FileData *fd, Main *main)
1822 bActionChannel *chan;
1824 for (act= main->action.first; act; act= act->id.next) {
1825 if (act->id.flag & LIB_NEEDLINK) {
1826 act->id.flag -= LIB_NEEDLINK;
1828 // XXX depreceated - old animation system <<<
1829 for (chan=act->chanbase.first; chan; chan=chan->next) {
1830 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1831 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1833 // >>> XXX depreceated - old animation system
1835 lib_link_fcurves(fd, &act->id, &act->curves);
1840 static void direct_link_action(FileData *fd, bAction *act)
1842 bActionChannel *achan; // XXX depreceated - old animation system
1845 link_list(fd, &act->curves);
1846 link_list(fd, &act->chanbase); // XXX depreceated - old animation system
1847 link_list(fd, &act->groups);
1848 link_list(fd, &act->markers);
1850 // XXX depreceated - old animation system <<<
1851 for (achan = act->chanbase.first; achan; achan=achan->next) {
1852 achan->grp= newdataadr(fd, achan->grp);
1854 link_list(fd, &achan->constraintChannels);
1856 // >>> XXX depreceated - old animation system
1858 direct_link_fcurves(fd, &act->curves);
1860 for (agrp = act->groups.first; agrp; agrp= agrp->next) {
1861 agrp->channels.first= newdataadr(fd, agrp->channels.first);
1862 agrp->channels.last= newdataadr(fd, agrp->channels.last);
1866 static void lib_link_nladata_strips(FileData *fd, ID *id, ListBase *list)
1870 for (strip= list->first; strip; strip= strip->next) {
1871 /* check strip's children */
1872 lib_link_nladata_strips(fd, id, &strip->strips);
1874 /* check strip's F-Curves */
1875 lib_link_fcurves(fd, id, &strip->fcurves);
1877 /* reassign the counted-reference to action */
1878 strip->act = newlibadr_us(fd, id->lib, strip->act);
1880 /* fix action id-root (i.e. if it comes from a pre 2.57 .blend file) */
1881 if ((strip->act) && (strip->act->idroot == 0))
1882 strip->act->idroot = GS(id->name);
1886 static void lib_link_nladata(FileData *fd, ID *id, ListBase *list)
1890 /* we only care about the NLA strips inside the tracks */
1891 for (nlt= list->first; nlt; nlt= nlt->next) {
1892 lib_link_nladata_strips(fd, id, &nlt->strips);
1896 /* This handles Animato NLA-Strips linking
1897 * NOTE: this assumes that link_list has already been called on the list
1899 static void direct_link_nladata_strips(FileData *fd, ListBase *list)
1903 for (strip= list->first; strip; strip= strip->next) {
1904 /* strip's child strips */
1905 link_list(fd, &strip->strips);
1906 direct_link_nladata_strips(fd, &strip->strips);
1908 /* strip's F-Curves */
1909 link_list(fd, &strip->fcurves);
1910 direct_link_fcurves(fd, &strip->fcurves);
1912 /* strip's F-Modifiers */
1913 link_list(fd, &strip->modifiers);
1914 direct_link_modifiers(fd, &strip->modifiers);
1918 /* NOTE: this assumes that link_list has already been called on the list */
1919 static void direct_link_nladata(FileData *fd, ListBase *list)
1923 for (nlt= list->first; nlt; nlt= nlt->next) {
1924 /* relink list of strips */
1925 link_list(fd, &nlt->strips);
1927 /* relink strip data */
1928 direct_link_nladata_strips(fd, &nlt->strips);
1934 static void lib_link_keyingsets(FileData *fd, ID *id, ListBase *list)
1939 /* here, we're only interested in the ID pointer stored in some of the paths */
1940 for (ks= list->first; ks; ks= ks->next) {
1941 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1942 ksp->id= newlibadr(fd, id->lib, ksp->id);
1947 /* NOTE: this assumes that link_list has already been called on the list */
1948 static void direct_link_keyingsets(FileData *fd, ListBase *list)
1953 /* link KeyingSet data to KeyingSet again (non ID-libs) */
1954 for (ks= list->first; ks; ks= ks->next) {
1956 link_list(fd, &ks->paths);
1958 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1960 ksp->rna_path= newdataadr(fd, ksp->rna_path);
1967 static void lib_link_animdata(FileData *fd, ID *id, AnimData *adt)
1972 /* link action data */
1973 adt->action= newlibadr_us(fd, id->lib, adt->action);
1974 adt->tmpact= newlibadr_us(fd, id->lib, adt->tmpact);
1976 /* fix action id-roots (i.e. if they come from a pre 2.57 .blend file) */
1977 if ((adt->action) && (adt->action->idroot == 0))
1978 adt->action->idroot = GS(id->name);
1979 if ((adt->tmpact) && (adt->tmpact->idroot == 0))
1980 adt->tmpact->idroot = GS(id->name);
1983 lib_link_fcurves(fd, id, &adt->drivers);
1985 /* overrides don't have lib-link for now, so no need to do anything */
1988 lib_link_nladata(fd, id, &adt->nla_tracks);
1991 static void direct_link_animdata(FileData *fd, AnimData *adt)
1993 /* NOTE: must have called newdataadr already before doing this... */
1998 link_list(fd, &adt->drivers);
1999 direct_link_fcurves(fd, &adt->drivers);
2001 /* link overrides */
2005 link_list(fd, &adt->nla_tracks);
2006 direct_link_nladata(fd, &adt->nla_tracks);
2008 /* relink active strip - even though strictly speaking this should only be used
2009 * if we're in 'tweaking mode', we need to be able to have this loaded back for
2010 * undo, but also since users may not exit tweakmode before saving (#24535)
2012 // TODO: it's not really nice that anyone should be able to save the file in this
2013 // state, but it's going to be too hard to enforce this single case...
2014 adt->actstrip= newdataadr(fd, adt->actstrip);
2017 /* ************ READ MOTION PATHS *************** */
2019 /* direct data for cache */
2020 static void direct_link_motionpath(FileData *fd, bMotionPath *mpath)
2026 /* relink points cache */
2027 mpath->points= newdataadr(fd, mpath->points);
2030 /* ************ READ NODE TREE *************** */
2032 /* singe node tree (also used for material/scene trees), ntree is not NULL */
2033 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
2037 if(ntree->adt) lib_link_animdata(fd, &ntree->id, ntree->adt);
2039 ntree->gpd= newlibadr_us(fd, id->lib, ntree->gpd);
2041 for(node= ntree->nodes.first; node; node= node->next)
2042 node->id= newlibadr_us(fd, id->lib, node->id);
2045 /* library ntree linking after fileread */
2046 static void lib_link_nodetree(FileData *fd, Main *main)
2050 /* only link ID pointers */
2051 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2052 if(ntree->id.flag & LIB_NEEDLINK) {
2053 ntree->id.flag -= LIB_NEEDLINK;
2054 lib_link_ntree(fd, &ntree->id, ntree);
2059 static void lib_nodetree_init_types_cb(void *UNUSED(data), ID *UNUSED(id), bNodeTree *ntree)
2063 ntreeInitTypes(ntree);
2065 /* XXX could be replaced by do_versions for new nodes */
2066 for (node=ntree->nodes.first; node; node=node->next)
2067 node_verify_socket_templates(ntree, node);
2070 /* updates group node socket own_index so that
2071 * external links to/from the group node are preserved.
2073 static void lib_node_do_versions_group_indices(bNode *gnode)
2075 bNodeTree *ngroup= (bNodeTree*)gnode->id;
2077 bNodeSocket *sock, *gsock, *intsock;
2080 for (sock=gnode->outputs.first; sock; sock=sock->next) {
2081 int old_index = sock->to_index;
2082 for (gsock=ngroup->outputs.first; gsock; gsock=gsock->next) {
2083 if (gsock->link && gsock->link->fromsock->own_index == old_index) {
2084 sock->own_index = gsock->own_index;
2089 for (sock=gnode->inputs.first; sock; sock=sock->next) {
2090 int old_index = sock->to_index;
2091 /* can't use break in double loop */
2093 for (intnode=ngroup->nodes.first; intnode && !found; intnode=intnode->next) {
2094 for (intsock=intnode->inputs.first; intsock; intsock=intsock->next) {
2095 if (intsock->own_index == old_index && intsock->link) {
2096 sock->own_index = intsock->link->fromsock->own_index;
2105 /* updates external links for all group nodes in a tree */
2106 static void lib_nodetree_do_versions_group_indices_cb(void *UNUSED(data), ID *UNUSED(id), bNodeTree *ntree)
2110 for (node=ntree->nodes.first; node; node=node->next) {
2111 if (node->type==NODE_GROUP) {
2112 bNodeTree *ngroup= (bNodeTree*)node->id;
2113 if (ngroup && (ngroup->flag & NTREE_DO_VERSIONS_GROUP_EXPOSE))
2114 lib_node_do_versions_group_indices(node);
2119 /* make an update call for the tree */
2120 static void lib_nodetree_do_versions_update_cb(void *UNUSED(data), ID *UNUSED(id), bNodeTree *ntree)
2123 ntreeUpdateTree(ntree);
2126 /* verify types for nodes and groups, all data has to be read */
2127 /* open = 0: appending/linking, open = 1: open new file (need to clean out dynamic
2129 static void lib_verify_nodetree(Main *main, int UNUSED(open))
2133 bNodeTreeType *ntreetype;
2135 /* this crashes blender on undo/redo
2137 reinit_nodesystem();
2140 /* set node->typeinfo pointers */
2141 for (i=0; i < NUM_NTREE_TYPES; ++i) {
2142 ntreetype= ntreeGetType(i);
2143 if (ntreetype && ntreetype->foreach_nodetree)
2144 ntreetype->foreach_nodetree(main, NULL, lib_nodetree_init_types_cb);
2146 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next)
2147 lib_nodetree_init_types_cb(NULL, NULL, ntree);
2150 int has_old_groups=0;
2151 /* XXX this should actually be part of do_versions, but since we need
2152 * finished library linking, it is not possible there. Instead in do_versions
2153 * we have set the NTREE_DO_VERSIONS flag, so at this point we can do the
2154 * actual group node updates.
2156 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2157 if (ntree->flag & NTREE_DO_VERSIONS_GROUP_EXPOSE) {
2158 /* this adds copies and links from all unlinked internal sockets to group inputs/outputs. */
2159 node_group_expose_all_sockets(ntree);
2164 if (has_old_groups) {
2165 for (i=0; i < NUM_NTREE_TYPES; ++i) {
2166 ntreetype= ntreeGetType(i);
2167 if (ntreetype && ntreetype->foreach_nodetree)
2168 ntreetype->foreach_nodetree(main, NULL, lib_nodetree_do_versions_group_indices_cb);
2172 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next)
2173 ntree->flag &= ~NTREE_DO_VERSIONS_GROUP_EXPOSE;
2176 /* verify all group user nodes */
2177 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2178 ntreeVerifyNodes(main, &ntree->id);
2181 /* make update calls where necessary */
2183 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next)
2185 ntreeUpdateTree(ntree);
2186 for (i=0; i < NUM_NTREE_TYPES; ++i) {
2187 ntreetype= ntreeGetType(i);
2188 if (ntreetype && ntreetype->foreach_nodetree)
2189 ntreetype->foreach_nodetree(main, NULL, lib_nodetree_do_versions_update_cb);
2194 static void direct_link_node_socket(FileData *fd, bNodeSocket *sock)
2196 sock->link= newdataadr(fd, sock->link);
2197 sock->storage= newdataadr(fd, sock->storage);
2198 sock->default_value= newdataadr(fd, sock->default_value);
2202 /* ntree itself has been read! */
2203 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
2205 /* note: writing and reading goes in sync, for speed */
2210 ntree->init= 0; /* to set callbacks and force setting types */
2211 ntree->progress= NULL;
2212 ntree->execdata= NULL;
2214 ntree->adt= newdataadr(fd, ntree->adt);
2215 direct_link_animdata(fd, ntree->adt);
2217 link_list(fd, &ntree->nodes);
2218 for(node= ntree->nodes.first; node; node= node->next) {
2219 if(node->type == NODE_DYNAMIC) {
2221 node->custom1= BSET(node->custom1, NODE_DYNAMIC_LOADED);
2224 node->typeinfo= NULL;
2226 link_list(fd, &node->inputs);
2227 link_list(fd, &node->outputs);
2229 node->storage= newdataadr(fd, node->storage);
2231 /* could be handlerized at some point */
2232 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
2233 direct_link_curvemapping(fd, node->storage);
2234 else if(ntree->type==NTREE_COMPOSIT) {
2235 if( ELEM4(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB, CMP_NODE_HUECORRECT))
2236 direct_link_curvemapping(fd, node->storage);
2237 else if(ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
2238 ((ImageUser *)node->storage)->ok= 1;
2240 else if( ntree->type==NTREE_TEXTURE) {
2241 if(node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME)
2242 direct_link_curvemapping(fd, node->storage);
2243 else if(node->type==TEX_NODE_IMAGE)
2244 ((ImageUser *)node->storage)->ok= 1;
2248 link_list(fd, &ntree->links);
2250 /* external sockets */
2251 link_list(fd, &ntree->inputs);
2252 link_list(fd, &ntree->outputs);
2254 /* and we connect the rest */
2255 for(node= ntree->nodes.first; node; node= node->next) {
2256 node->parent = newdataadr(fd, node->parent);
2257 node->preview= newimaadr(fd, node->preview);
2260 for(sock= node->inputs.first; sock; sock= sock->next)
2261 direct_link_node_socket(fd, sock);
2262 for(sock= node->outputs.first; sock; sock= sock->next)
2263 direct_link_node_socket(fd, sock);
2265 for(sock= ntree->inputs.first; sock; sock= sock->next)
2266 direct_link_node_socket(fd, sock);
2267 for(sock= ntree->outputs.first; sock; sock= sock->next)
2268 direct_link_node_socket(fd, sock);
2270 for(link= ntree->links.first; link; link= link->next) {
2271 link->fromnode= newdataadr(fd, link->fromnode);
2272 link->tonode= newdataadr(fd, link->tonode);
2273 link->fromsock= newdataadr(fd, link->fromsock);
2274 link->tosock= newdataadr(fd, link->tosock);
2277 /* type verification is in lib-link */
2280 /* ************ READ ARMATURE ***************** */
2282 /* temp struct used to transport needed info to lib_link_constraint_cb() */
2283 typedef struct tConstraintLinkData {
2286 } tConstraintLinkData;
2287 /* callback function used to relink constraint ID-links */
2288 static void lib_link_constraint_cb(bConstraint *UNUSED(con), ID **idpoin, void *userdata)
2290 tConstraintLinkData *cld= (tConstraintLinkData *)userdata;
2291 *idpoin = newlibadr(cld->fd, cld->id->lib, *idpoin);
2294 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
2296 tConstraintLinkData cld;
2300 for (con = conlist->first; con; con=con->next) {
2301 /* patch for error introduced by changing constraints (dunno how) */
2302 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
2303 if(con->data==NULL) {
2304 con->type= CONSTRAINT_TYPE_NULL;
2306 /* own ipo, all constraints have it */
2307 con->ipo= newlibadr_us(fd, id->lib, con->ipo); // XXX depreceated - old animation system
2310 /* relink all ID-blocks used by the constraints */
2314 id_loop_constraints(conlist, lib_link_constraint_cb, &cld);
2317 static void direct_link_constraints(FileData *fd, ListBase *lb)
2322 for (con=lb->first; con; con=con->next) {
2323 con->data = newdataadr(fd, con->data);
2325 switch (con->type) {
2326 case CONSTRAINT_TYPE_PYTHON:
2328 bPythonConstraint *data= con->data;
2330 link_list(fd, &data->targets);
2332 data->prop = newdataadr(fd, data->prop);
2334 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2337 case CONSTRAINT_TYPE_SPLINEIK:
2339 bSplineIKConstraint *data= con->data;
2341 data->points= newdataadr(fd, data->points);
2344 case CONSTRAINT_TYPE_KINEMATIC:
2346 con->lin_error = 0.f;
2347 con->rot_error = 0.f;
2349 case CONSTRAINT_TYPE_CHILDOF:
2351 /* XXX version patch, in older code this flag wasn't always set, and is inherent to type */
2352 if(con->ownspace == CONSTRAINT_SPACE_POSE)
2353 con->flag |= CONSTRAINT_SPACEONCE;
2360 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
2362 bPoseChannel *pchan;
2363 bArmature *arm= ob->data;
2370 /* always rebuild to match proxy or lib changes */
2371 rebuild= ob->proxy || (ob->id.lib==NULL && arm->id.lib);
2374 /* sync proxy layer */
2375 if(pose->proxy_layer)
2376 arm->layer = pose->proxy_layer;
2378 /* sync proxy active bone */
2379 if(pose->proxy_act_bone[0]) {
2380 Bone *bone = get_named_bone(arm, pose->proxy_act_bone);
2382 arm->act_bone = bone;
2386 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
2387 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
2389 /* hurms... loop in a loop, but yah... later... (ton) */
2390 pchan->bone= get_named_bone(arm, pchan->name);
2392 pchan->custom= newlibadr_us(fd, arm->id.lib, pchan->custom);
2393 if(pchan->bone==NULL)
2395 else if(ob->id.lib==NULL && arm->id.lib) {
2396 /* local pose selection copied to armature, bit hackish */
2397 pchan->bone->flag &= ~BONE_SELECTED;
2398 pchan->bone->flag |= pchan->selectflag;
2403 ob->recalc= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
2404 pose->flag |= POSE_RECALC;
2408 static void lib_link_armature(FileData *fd, Main *main)
2412 arm= main->armature.first;
2415 if(arm->id.flag & LIB_NEEDLINK) {
2416 if (arm->adt) lib_link_animdata(fd, &arm->id, arm->adt);
2417 arm->id.flag -= LIB_NEEDLINK;
2423 static void direct_link_bones(FileData *fd, Bone* bone)
2427 bone->parent= newdataadr(fd, bone->parent);
2428 bone->prop= newdataadr(fd, bone->prop);
2430 IDP_DirectLinkProperty(bone->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2432 bone->flag &= ~BONE_DRAW_ACTIVE;
2434 link_list(fd, &bone->childbase);
2436 for(child=bone->childbase.first; child; child=child->next)
2437 direct_link_bones(fd, child);
2440 static void direct_link_armature(FileData *fd, bArmature *arm)
2444 link_list(fd, &arm->bonebase);
2448 arm->adt= newdataadr(fd, arm->adt);
2449 direct_link_animdata(fd, arm->adt);
2451 bone=arm->bonebase.first;
2453 direct_link_bones(fd, bone);
2457 arm->act_bone= newdataadr(fd, arm->act_bone);
2458 arm->act_edbone= NULL;
2461 /* ************ READ CAMERA ***************** */
2463 static void lib_link_camera(FileData *fd, Main *main)
2467 ca= main->camera.first;
2469 if(ca->id.flag & LIB_NEEDLINK) {
2470 if (ca->adt) lib_link_animdata(fd, &ca->id, ca->adt);
2472 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo); // XXX depreceated - old animation system
2474 ca->dof_ob= newlibadr_us(fd, ca->id.lib, ca->dof_ob);
2476 ca->id.flag -= LIB_NEEDLINK;
2482 static void direct_link_camera(FileData *fd, Camera *ca)
2484 ca->adt= newdataadr(fd, ca->adt);
2485 direct_link_animdata(fd, ca->adt);
2489 /* ************ READ LAMP ***************** */
2491 static void lib_link_lamp(FileData *fd, Main *main)
2497 la= main->lamp.first;
2499 if(la->id.flag & LIB_NEEDLINK) {
2500 if (la->adt) lib_link_animdata(fd, &la->id, la->adt);
2502 for(a=0; a<MAX_MTEX; a++) {
2505 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
2506 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
2510 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo); // XXX depreceated - old animation system
2512 la->id.flag -= LIB_NEEDLINK;
2518 static void direct_link_lamp(FileData *fd, Lamp *la)
2522 la->adt= newdataadr(fd, la->adt);
2523 direct_link_animdata(fd, la->adt);
2525 for(a=0; a<MAX_MTEX; a++) {
2526 la->mtex[a]= newdataadr(fd, la->mtex[a]);
2529 la->curfalloff= newdataadr(fd, la->curfalloff);
2531 direct_link_curvemapping(fd, la->curfalloff);
2533 la->preview = direct_link_preview_image(fd, la->preview);
2536 /* ************ READ keys ***************** */
2538 static void lib_link_key(FileData *fd, Main *main)
2542 key= main->key.first;
2544 /*check if we need to generate unique ids for the shapekeys*/
2549 for (block=key->block.first; block; block=block->next) {
2550 block->uid = key->uidgen++;
2554 if(key->id.flag & LIB_NEEDLINK) {
2555 if(key->adt) lib_link_animdata(fd, &key->id, key->adt);
2557 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo); // XXX depreceated - old animation system
2558 key->from= newlibadr(fd, key->id.lib, key->from);
2560 key->id.flag -= LIB_NEEDLINK;
2566 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
2569 char *data, *poin, *cp;
2571 elemsize= key->elemsize;
2574 for(a=0; a<kb->totelem; a++) {
2579 while( cp[0] ) { /* cp[0]==amount */
2581 switch(cp[1]) { /* cp[1]= type */
2587 SWITCH_INT((*poin));
2600 static void direct_link_key(FileData *fd, Key *key)
2604 link_list(fd, &(key->block));
2606 key->adt= newdataadr(fd, key->adt);
2607 direct_link_animdata(fd, key->adt);
2609 key->refkey= newdataadr(fd, key->refkey);
2611 kb= key->block.first;
2614 kb->data= newdataadr(fd, kb->data);
2616 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
2617 switch_endian_keyblock(key, kb);
2623 /* ************ READ mball ***************** */
2625 static void lib_link_mball(FileData *fd, Main *main)
2630 mb= main->mball.first;
2632 if(mb->id.flag & LIB_NEEDLINK) {
2633 if (mb->adt) lib_link_animdata(fd, &mb->id, mb->adt);
2635 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
2637 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo); // XXX depreceated - old animation system
2639 mb->id.flag -= LIB_NEEDLINK;
2645 static void direct_link_mball(FileData *fd, MetaBall *mb)
2647 mb->adt= newdataadr(fd, mb->adt);
2648 direct_link_animdata(fd, mb->adt);
2650 mb->mat= newdataadr(fd, mb->mat);
2651 test_pointer_array(fd, (void **)&mb->mat);
2653 link_list(fd, &(mb->elems));
2655 mb->disp.first= mb->disp.last= NULL;
2656 mb->editelems= NULL;
2658 /* mb->edit_elems.first= mb->edit_elems.last= NULL;*/
2662 /* ************ READ WORLD ***************** */
2664 static void lib_link_world(FileData *fd, Main *main)
2670 wrld= main->world.first;
2672 if(wrld->id.flag & LIB_NEEDLINK) {
2673 if (wrld->adt) lib_link_animdata(fd, &wrld->id, wrld->adt);
2675 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo); // XXX depreceated - old animation system
2677 for(a=0; a<MAX_MTEX; a++) {
2678 mtex= wrld->mtex[a];
2680 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
2681 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
2685 wrld->id.flag -= LIB_NEEDLINK;
2687 wrld= wrld->id.next;
2691 static void direct_link_world(FileData *fd, World *wrld)
2695 wrld->adt= newdataadr(fd, wrld->adt);
2696 direct_link_animdata(fd, wrld->adt);
2698 for(a=0; a<MAX_MTEX; a++) {
2699 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
2701 wrld->preview = direct_link_preview_image(fd, wrld->preview);
2705 /* ************ READ VFONT ***************** */
2707 static void lib_link_vfont(FileData *UNUSED(fd), Main *main)
2711 vf= main->vfont.first;
2713 if(vf->id.flag & LIB_NEEDLINK) {
2714 vf->id.flag -= LIB_NEEDLINK;
2720 static void direct_link_vfont(FileData *fd, VFont *vf)
2723 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
2726 /* ************ READ TEXT ****************** */
2728 static void lib_link_text(FileData *UNUSED(fd), Main *main)
2732 text= main->text.first;
2734 if(text->id.flag & LIB_NEEDLINK) {
2735 text->id.flag -= LIB_NEEDLINK;
2737 text= text->id.next;
2741 static void direct_link_text(FileData *fd, Text *text)
2745 text->name= newdataadr(fd, text->name);
2748 text->undo_len= TXT_INIT_UNDO;
2749 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
2751 text->compiled= NULL;
2754 if(text->flags & TXT_ISEXT) {
2759 link_list(fd, &text->lines);
2760 link_list(fd, &text->markers);
2762 text->curl= newdataadr(fd, text->curl);
2763 text->sell= newdataadr(fd, text->sell);
2765 ln= text->lines.first;
2767 ln->line= newdataadr(fd, ln->line);
2770 if (ln->len != (int) strlen(ln->line)) {
2771 printf("Error loading text, line lengths differ\n");
2772 ln->len = strlen(ln->line);
2778 text->flags = (text->flags) & ~TXT_ISEXT;
2783 /* ************ READ IMAGE ***************** */
2785 static void lib_link_image(FileData *fd, Main *main)
2789 ima= main->image.first;
2791 if(ima->id.flag & LIB_NEEDLINK) {
2792 if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2794 ima->id.flag -= LIB_NEEDLINK;
2800 static void link_ibuf_list(FileData *fd, ListBase *lb)
2804 if(lb->first==NULL) return;
2806 lb->first= newimaadr(fd, lb->first);
2810 ln->next= newimaadr(fd, ln->next);
2818 static void direct_link_image(FileData *fd, Image *ima)
2820 /* for undo system, pointers could be restored */
2822 link_ibuf_list(fd, &ima->ibufs);
2824 ima->ibufs.first= ima->ibufs.last= NULL;
2826 /* if not restored, we keep the binded opengl index */
2827 if(ima->ibufs.first==NULL) {
2829 ima->gputexture= NULL;
2836 /* undo system, try to restore render buffers */
2840 for(a=0; a<IMA_MAX_RENDER_SLOT; a++)
2841 ima->renders[a]= newimaadr(fd, ima->renders[a]);
2844 memset(ima->renders, 0, sizeof(ima->renders));
2845 ima->last_render_slot= ima->render_slot;
2848 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
2849 ima->preview = direct_link_preview_image(fd, ima->preview);
2854 /* ************ READ CURVE ***************** */
2856 static void lib_link_curve(FileData *fd, Main *main)
2861 cu= main->curve.first;
2863 if(cu->id.flag & LIB_NEEDLINK) {
2864 if(cu->adt) lib_link_animdata(fd, &cu->id, cu->adt);
2866 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
2868 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
2869 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
2870 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
2871 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
2872 cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);
2873 cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
2874 cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
2876 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo); // XXX depreceated - old animation system
2877 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
2879 cu->id.flag -= LIB_NEEDLINK;
2886 static void switch_endian_knots(Nurb *nu)
2893 SWITCH_INT(nu->knotsu[len]);
2899 SWITCH_INT(nu->knotsv[len]);
2904 static void direct_link_curve(FileData *fd, Curve *cu)
2909 cu->adt= newdataadr(fd, cu->adt);
2910 direct_link_animdata(fd, cu->adt);
2912 cu->mat= newdataadr(fd, cu->mat);
2913 test_pointer_array(fd, (void **)&cu->mat);
2914 cu->str= newdataadr(fd, cu->str);
2915 cu->strinfo= newdataadr(fd, cu->strinfo);
2916 cu->tb= newdataadr(fd, cu->tb);
2918 if(cu->vfont == NULL) link_list(fd, &(cu->nurb));
2920 cu->nurb.first=cu->nurb.last= NULL;
2922 tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
2924 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
2931 cu->tb[0].w = cu->linewidth;
2933 if (cu->wordspace == 0.0f) cu->wordspace = 1.0f;
2936 cu->bev.first=cu->bev.last= NULL;
2937 cu->disp.first=cu->disp.last= NULL;
2945 nu->bezt= newdataadr(fd, nu->bezt);
2946 nu->bp= newdataadr(fd, nu->bp);
2947 nu->knotsu= newdataadr(fd, nu->knotsu);
2948 nu->knotsv= newdataadr(fd, nu->knotsv);
2949 if (cu->vfont == NULL) nu->charidx= nu->mat_nr;
2951 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2952 switch_endian_knots(nu);
2960 /* ************ READ TEX ***************** */
2962 static void lib_link_texture(FileData *fd, Main *main)
2966 tex= main->tex.first;
2968 if(tex->id.flag & LIB_NEEDLINK) {
2969 if(tex->adt) lib_link_animdata(fd, &tex->id, tex->adt);
2971 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
2972 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
2973 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
2975 tex->pd->object= newlibadr(fd, tex->id.lib, tex->pd->object);
2976 if(tex->vd) tex->vd->object= newlibadr(fd, tex->id.lib, tex->vd->object);
2979 lib_link_ntree(fd, &tex->id, tex->nodetree);
2981 tex->id.flag -= LIB_NEEDLINK;
2987 static void direct_link_texture(FileData *fd, Tex *tex)
2989 tex->adt= newdataadr(fd, tex->adt);
2990 direct_link_animdata(fd, tex->adt);
2992 tex->plugin= newdataadr(fd, tex->plugin);
2994 tex->plugin->handle= NULL;
2995 open_plugin_tex(tex->plugin);
2996 /* initialize data for this instance, if an initialization
2999 if (tex->plugin->instance_init)
3000 tex->plugin->instance_init((void *) tex->plugin->data);
3002 tex->coba= newdataadr(fd, tex->coba);
3003 tex->env= newdataadr(fd, tex->env);
3005 tex->env->ima= NULL;
3006 memset(tex->env->cube, 0, 6*sizeof(void *));
3009 tex->pd= newdataadr(fd, tex->pd);
3011 tex->pd->point_tree = NULL;
3012 tex->pd->coba= newdataadr(fd, tex->pd->coba);
3013 tex->pd->falloff_curve= newdataadr(fd, tex->pd->falloff_curve);
3014 if(tex->pd->falloff_curve) {
3015 direct_link_curvemapping(fd, tex->pd->falloff_curve);
3019 tex->vd= newdataadr(fd, tex->vd);
3021 tex->vd->dataset = NULL;
3025 tex->nodetree= newdataadr(fd, tex->nodetree);
3027 direct_link_nodetree(fd, tex->nodetree);
3029 tex->preview = direct_link_preview_image(fd, tex->preview);
3036 /* ************ READ MATERIAL ***************** */
3038 static void lib_link_material(FileData *fd, Main *main)
3044 ma= main->mat.first;
3046 if(ma->id.flag & LIB_NEEDLINK) {
3047 if(ma->adt) lib_link_animdata(fd, &ma->id, ma->adt);
3049 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
3050 of library blocks that implement this.*/
3051 if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
3053 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
3054 ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
3056 for(a=0; a<MAX_MTEX; a++) {
3059 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
3060 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
3065 lib_link_ntree(fd, &ma->id, ma->nodetree);
3067 ma->id.flag -= LIB_NEEDLINK;
3073 static void direct_link_material(FileData *fd, Material *ma)
3077 ma->adt= newdataadr(fd, ma->adt);
3078 direct_link_animdata(fd, ma->adt);
3080 for(a=0; a<MAX_MTEX; a++) {
3081 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
3084 ma->ramp_col= newdataadr(fd, ma->ramp_col);
3085 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
3087 ma->nodetree= newdataadr(fd, ma->nodetree);
3089 direct_link_nodetree(fd, ma->nodetree);
3091 ma->preview = direct_link_preview_image(fd, ma->preview);
3092 ma->gpumaterial.first = ma->gpumaterial.last = NULL;
3095 /* ************ READ PARTICLE SETTINGS ***************** */
3096 /* update this also to writefile.c */
3097 static const char *ptcache_data_struct[] = {
3098 "", // BPHYS_DATA_INDEX
3099 "", // BPHYS_DATA_LOCATION
3100 "", // BPHYS_DATA_VELOCITY
3101 "", // BPHYS_DATA_ROTATION
3102 "", // BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST */
3103 "", // BPHYS_DATA_SIZE:
3104 "", // BPHYS_DATA_TIMES:
3105 "BoidData" // case BPHYS_DATA_BOIDS:
3107 static void direct_link_pointcache(FileData *fd, PointCache *cache)
3109 if((cache->flag & PTCACHE_DISK_CACHE)==0) {
3111 PTCacheExtra *extra;
3114 link_list(fd, &cache->mem_cache);
3116 pm = cache->mem_cache.first;
3118 for(; pm; pm=pm->next) {
3119 for(i=0; i<BPHYS_TOT_DATA; i++) {
3120 pm->data[i] = newdataadr(fd, pm->data[i]);
3122 /* the cache saves non-struct data without DNA */
3123 if(pm->data[i] && strcmp(ptcache_data_struct[i], "")==0 && (fd->flags & FD_FLAGS_SWITCH_ENDIAN)) {
3124 int j, tot= (BKE_ptcache_data_size (i) * pm->totpoint)/4; /* data_size returns bytes */
3125 int *poin= pm->data[i];
3127 for(j= 0; j<tot; j++)
3128 SWITCH_INT(poin[j]);
3132 link_list(fd, &pm->extradata);
3134 for(extra=pm->extradata.first; extra; extra=extra->next)
3135 extra->data = newdataadr(fd, extra->data);
3139 cache->mem_cache.first = cache->mem_cache.last = NULL;
3141 cache->flag &= ~PTCACHE_SIMULATION_VALID;
3144 cache->free_edit= NULL;
3145 cache->cached_frames= NULL;
3148 static void direct_link_pointcache_list(FileData *fd, ListBase *ptcaches, PointCache **ocache, int force_disk)
3150 if(ptcaches->first) {
3151 PointCache *cache= NULL;
3152 link_list(fd, ptcaches);
3153 for(cache=ptcaches->first; cache; cache=cache->next) {
3154 direct_link_pointcache(fd, cache);
3156 cache->flag |= PTCACHE_DISK_CACHE;
3161 *ocache = newdataadr(fd, *ocache);
3164 /* old "single" caches need to be linked too */
3165 *ocache = newdataadr(fd, *ocache);
3166 direct_link_pointcache(fd, *ocache);
3168 (*ocache)->flag |= PTCACHE_DISK_CACHE;
3169 (*ocache)->step = 1;
3172 ptcaches->first = ptcaches->last = *ocache;
3176 static void lib_link_partdeflect(FileData *fd, ID *id, PartDeflect *pd)
3179 pd->tex=newlibadr_us(fd, id->lib, pd->tex);
3182 static void lib_link_particlesettings(FileData *fd, Main *main)
3184 ParticleSettings *part;
3185 ParticleDupliWeight *dw;
3189 part= main->particle.first;
3191 if(part->id.flag & LIB_NEEDLINK) {
3192 if (part->adt) lib_link_animdata(fd, &part->id, part->adt);
3193 part->ipo= newlibadr_us(fd, part->id.lib, part->ipo); // XXX depreceated - old animation system
3195 part->dup_ob = newlibadr(fd, part->id.lib, part->dup_ob);
3196 part->dup_group = newlibadr(fd, part->id.lib, part->dup_group);
3197 part->eff_group = newlibadr(fd, part->id.lib, part->eff_group);
3198 part->bb_ob = newlibadr(fd, part->id.lib, part->bb_ob);
3200 lib_link_partdeflect(fd, &part->id, part->pd);
3201 lib_link_partdeflect(fd, &part->id, part->pd2);
3203 if(part->effector_weights)
3204 part->effector_weights->group = newlibadr(fd, part->id.lib, part->effector_weights->group);
3206 if(part->dupliweights.first && part->dup_group) {