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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
39 #include "BLI_winstuff.h"
42 #include <stdio.h> // for printf fopen fwrite fclose sprintf FILE
43 #include <stdlib.h> // for getenv atoi
44 #include <fcntl.h> // for open
45 #include <string.h> // for strrchr strncmp strstr
46 #include <math.h> // for fabs
49 #include <unistd.h> // for read close
50 #include <sys/param.h> // for MAXPATHLEN
52 #include <io.h> // for open close read
57 #include "DNA_action_types.h"
58 #include "DNA_armature_types.h"
60 #include "DNA_actuator_types.h"
61 #include "DNA_camera_types.h"
62 #include "DNA_color_types.h"
63 #include "DNA_controller_types.h"
64 #include "DNA_constraint_types.h"
65 #include "DNA_curve_types.h"
66 #include "DNA_effect_types.h"
67 #include "DNA_fileglobal_types.h"
68 #include "DNA_group_types.h"
69 #include "DNA_ipo_types.h"
70 #include "DNA_image_types.h"
71 #include "DNA_key_types.h"
72 #include "DNA_lattice_types.h"
73 #include "DNA_lamp_types.h"
74 #include "DNA_meta_types.h"
75 #include "DNA_material_types.h"
76 #include "DNA_mesh_types.h"
77 #include "DNA_meshdata_types.h"
78 #include "DNA_modifier_types.h"
79 #include "DNA_nla_types.h"
80 #include "DNA_node_types.h"
81 #include "DNA_object_types.h"
82 #include "DNA_object_force.h"
83 #include "DNA_object_fluidsim.h" // NT
84 #include "DNA_oops_types.h"
85 #include "DNA_object_force.h"
86 #include "DNA_packedFile_types.h"
87 #include "DNA_property_types.h"
88 #include "DNA_text_types.h"
89 #include "DNA_view3d_types.h"
90 #include "DNA_screen_types.h"
91 #include "DNA_sensor_types.h"
92 #include "DNA_sdna_types.h"
93 #include "DNA_scene_types.h"
94 #include "DNA_sequence_types.h"
95 #include "DNA_sound_types.h"
96 #include "DNA_space_types.h"
97 #include "DNA_texture_types.h"
98 #include "DNA_userdef_types.h"
99 #include "DNA_vfont_types.h"
100 #include "DNA_world_types.h"
102 #include "MEM_guardedalloc.h"
103 #include "BLI_blenlib.h"
104 #include "BLI_arithb.h"
105 #include "BLI_storage_types.h" // for relname flags
107 #include "BKE_bad_level_calls.h" // for reopen_text build_seqar (from WHILE_SEQ) open_plugin_seq set_rects_butspace check_imasel_copy
109 #include "BKE_action.h"
110 #include "BKE_armature.h"
111 #include "BKE_constraint.h"
112 #include "BKE_curve.h"
113 #include "BKE_deform.h"
114 #include "BKE_depsgraph.h"
115 #include "BKE_effect.h" // for give_parteff
116 #include "BKE_global.h" // for G
117 #include "BKE_property.h" // for get_property
118 #include "BKE_lattice.h"
119 #include "BKE_library.h" // for wich_libbase
120 #include "BKE_main.h" // for Main
121 #include "BKE_mesh.h" // for ME_ defines (patching)
122 #include "BKE_modifier.h"
123 #include "BKE_node.h" // for tree type defines
124 #include "BKE_object.h"
125 #include "BKE_sca.h" // for init_actuator
126 #include "BKE_scene.h"
127 #include "BKE_softbody.h" // sbNew()
128 #include "BKE_texture.h" // for open_plugin_tex
129 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
131 #include "BIF_butspace.h" // for do_versions, patching event codes
132 #include "BIF_previewrender.h" // for struct RenderInfo
133 #include "BLO_readfile.h"
134 #include "BLO_undofile.h"
135 #include "BLO_readblenfile.h" // streaming read pipe, for BLO_readblenfile BLO_readblenfilememory
137 #include "readfile.h"
141 #include "mydevice.h"
147 Remark: still a weak point is the newadress() function, that doesnt solve reading from
148 multiple files at the same time
150 (added remark: oh, i thought that was solved? will look at that... (ton)
153 - Existing Library (Main) push or free
163 - read associated 'direct data'
164 - link direct data (internal and to LibBlock)
166 - read USER data, only when indicated (file is ~/.B.blend)
168 - per Library (per Main)
171 - find LibBlocks and attach IDs to Main
172 - if external LibBlock
174 - or it's already read,
179 - read associated direct data
180 - link direct data (internal and to LibBlock)
182 - per Library with unread LibBlocks
187 - read associated direct data
188 - link direct data (internal and to LibBlock)
191 - link all LibBlocks and indirect pointers to libblocks
192 - initialize FileGlobal and copy pointers to Global
195 /* also occurs in library.c */
196 /* GS reads the memory pointed at in a specific ordering. There are,
197 * however two definitions for it. I have jotted them down here, both,
198 * but I think the first one is actually used. The thing is that
199 * big-endian systems might read this the wrong way round. OTOH, we
200 * constructed the IDs that are read out with this macro explicitly as
201 * well. I expect we'll sort it out soon... */
204 #define GS(a) (*((short *)(a)))
206 /* from misc_util: flip the bytes from x */
207 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
209 // only used here in readfile.c
210 #define SWITCH_LONGINT(a) { \
213 s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
214 s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
215 s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
216 s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
217 // only used here in readfile.c
218 #define SWITCH_SHORT(a) { \
221 s_i=p_i[0]; p_i[0]=p_i[1]; p_i[1]=s_i; }
226 typedef struct OldNew {
231 typedef struct OldNewMap {
233 int nentries, entriessize;
239 /* local prototypes */
240 extern short freeN(void *vmemh); /* defined in util.h */
243 static OldNewMap *oldnewmap_new(void)
245 OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
247 onm->entriessize= 1024;
248 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
253 static int verg_oldnewmap(const void *v1, const void *v2)
255 const struct OldNew *x1=v1, *x2=v2;
257 if( x1->old > x2->old) return 1;
258 else if( x1->old < x2->old) return -1;
263 static void oldnewmap_sort(FileData *fd)
265 qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
266 fd->libmap->sorted= 1;
269 /* nr is zero for data, and ID code for libdata */
270 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
274 if(oldaddr==NULL || newaddr==NULL) return;
276 if (onm->nentries==onm->entriessize) {
277 int osize= onm->entriessize;
278 OldNew *oentries= onm->entries;
280 onm->entriessize*= 2;
281 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
283 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
287 entry= &onm->entries[onm->nentries++];
289 entry->newp= newaddr;
293 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
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 libdate, 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[100], *fromarray[100]; // define in library.c too
394 a= set_listbasepointers(mainvar, lbarray);
395 a= set_listbasepointers(from, fromarray);
397 addlisttolist(lbarray[a], fromarray[a]);
401 void blo_join_main(ListBase *mainlist)
403 Main *tojoin, *mainl= mainlist->first;
405 while ((tojoin= mainl->next)) {
406 add_main_to_main(mainl, tojoin);
407 BLI_remlink(mainlist, tojoin);
412 static void split_libdata(ListBase *lb, Main *first)
424 if(mainvar->curlib==id->lib) {
425 lbn= wich_libbase(mainvar, GS(id->name));
427 BLI_addtail(lbn, id);
430 mainvar= mainvar->next;
432 if(mainvar==0) printf("error split_libdata\n");
438 void blo_split_main(ListBase *mainlist)
440 Main *mainl= mainlist->first;
441 ListBase *lbarray[30];
445 for (lib= mainl->library.first; lib; lib= lib->id.next) {
446 Main *libmain= MEM_callocN(sizeof(Main), "libmain");
447 libmain->curlib= lib;
448 BLI_addtail(mainlist, libmain);
451 i= set_listbasepointers(mainl, lbarray);
453 split_libdata(lbarray[i], mainl->next);
456 /* removes things like /blah/blah/../../blah/ etc, then writes in *name the full path */
457 static void cleanup_path(const char *relabase, char *name)
459 char filename[FILE_MAXFILE];
461 BLI_splitdirstring(name, filename);
462 BLI_cleanup_dir(relabase, name);
463 strcat(name, filename);
466 static Main *blo_find_main(ListBase *mainlist, const char *name, const char *relabase)
470 char name1[FILE_MAXDIR+FILE_MAXFILE];
471 char libname1[FILE_MAXDIR+FILE_MAXFILE];
474 cleanup_path(relabase, name1);
475 // printf("blo_find_main: original in %s\n", name);
476 // printf("blo_find_main: converted to %s\n", name1);
478 for (m= mainlist->first; m; m= m->next) {
479 char *libname= (m->curlib)?m->curlib->filename:m->name;
481 if (BLI_streq(name1, libname)) {
482 printf("blo_find_main: found library %s\n", libname);
487 m= MEM_callocN(sizeof(Main), "find_main");
488 BLI_addtail(mainlist, m);
490 lib= alloc_libblock(&m->library, ID_LI, "lib");
491 strcpy(lib->name, name);
492 strcpy(lib->filename, name1);
496 printf("blo_find_main: added new lib %s\n", name);
501 /* ************ FILE PARSING ****************** */
503 static void switch_endian_bh4(BHead4 *bhead)
505 /* the ID_.. codes */
506 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
508 if (bhead->code != ENDB) {
509 SWITCH_INT(bhead->len);
510 SWITCH_INT(bhead->SDNAnr);
511 SWITCH_INT(bhead->nr);
515 static void switch_endian_bh8(BHead8 *bhead)
517 /* the ID_.. codes */
518 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
520 if (bhead->code != ENDB) {
521 SWITCH_INT(bhead->len);
522 SWITCH_INT(bhead->SDNAnr);
523 SWITCH_INT(bhead->nr);
527 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
529 BHead4 *bhead4 = (BHead4 *) bhead;
530 #if defined(WIN32) && !defined(FREE_WINDOWS)
536 bhead4->code= bhead8->code;
537 bhead4->len= bhead8->len;
539 if (bhead4->code != ENDB) {
541 // why is this here ??
542 if (do_endian_swap) {
543 SWITCH_LONGINT(bhead8->old);
546 /* this patch is to avoid a long long being read from not-eight aligned positions
547 is necessary on SGI with -n32 compiling (no, is necessary on
548 any modern 64bit architecture) */
549 memcpy(&old, &bhead8->old, 8);
550 bhead4->old = (int) (old >> 3);
552 bhead4->SDNAnr= bhead8->SDNAnr;
553 bhead4->nr= bhead8->nr;
557 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
559 BHead8 *bhead8 = (BHead8 *) bhead;
561 bhead8->code= bhead4->code;
562 bhead8->len= bhead4->len;
564 if (bhead8->code != ENDB) {
565 bhead8->old= bhead4->old;
566 bhead8->SDNAnr= bhead4->SDNAnr;
567 bhead8->nr= bhead4->nr;
571 static BHeadN *get_bhead(FileData *fd)
576 BHeadN *new_bhead = 0;
582 // First read the bhead structure.
583 // Depending on the platform the file was written on this can
584 // be a big or little endian BHead4 or BHead8 structure.
586 // As usual 'ENDB' (the last *partial* bhead of the file)
587 // needs some special handling. We don't want to EOF just yet.
589 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
591 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
593 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
594 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
595 switch_endian_bh4(&bhead4);
598 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
599 bh8_from_bh4(&bhead, &bhead4);
601 memcpy(&bhead, &bhead4, sizeof(bhead));
608 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
610 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
611 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
612 switch_endian_bh8(&bhead8);
615 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
616 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
618 memcpy(&bhead, &bhead8, sizeof(bhead));
625 /* make sure people are not trying to pass bad blend files */
626 if (bhead.len < 0) fd->eof = 1;
628 // bhead now contains the (converted) bhead structure. Now read
629 // the associated data and put everything in a BHeadN (creative naming !)
632 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
634 new_bhead->next = new_bhead->prev = 0;
635 new_bhead->bhead = bhead;
637 readsize = fd->read(fd, new_bhead + 1, bhead.len);
639 if (readsize != bhead.len) {
641 MEM_freeN(new_bhead);
651 // We've read a new block. Now add it to the list
655 BLI_addtail(&fd->listbase, new_bhead);
661 BHead *blo_firstbhead(FileData *fd)
667 // Read in a new block if necessary
669 new_bhead = fd->listbase.first;
670 if (new_bhead == 0) {
671 new_bhead = get_bhead(fd);
675 bhead = &new_bhead->bhead;
681 BHead *blo_prevbhead(FileData *fd, BHead *thisblock)
683 BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - (int) (&((BHeadN*)0)->bhead));
684 BHeadN *prev= bheadn->prev;
686 return prev?&prev->bhead:NULL;
689 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
691 BHeadN *new_bhead = 0;
695 // bhead is actually a sub part of BHeadN
696 // We calculate the BHeadN pointer from the BHead pointer below
697 new_bhead = (BHeadN *) (((char *) thisblock) - (int) (&((BHeadN*)0)->bhead));
699 // get the next BHeadN. If it doesn't exist we read in the next one
700 new_bhead = new_bhead->next;
701 if (new_bhead == 0) {
702 new_bhead = get_bhead(fd);
707 // here we do the reverse:
708 // go from the BHeadN pointer to the BHead pointer
709 bhead = &new_bhead->bhead;
715 static void decode_blender_header(FileData *fd)
717 char header[SIZEOFBLENDERHEADER], num[4];
720 // read in the header data
721 readsize = fd->read(fd, header, sizeof(header));
723 if (readsize == sizeof(header)) {
724 if(strncmp(header, "BLENDER", 7) == 0) {
725 int remove_this_endian_test= 1;
727 fd->flags |= FD_FLAGS_FILE_OK;
729 // what size are pointers in the file ?
731 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
732 if (sizeof(void *) != 4) {
733 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
736 if (sizeof(void *) != 8) {
737 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
741 // is the file saved in a different endian
743 if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
744 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
747 // get the version number
749 memcpy(num, header+9, 3);
751 fd->fileversion = atoi(num);
756 static int read_file_dna(FileData *fd)
760 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
761 if (bhead->code==DNA1) {
762 int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
764 fd->filesdna= dna_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
766 fd->compflags= dna_get_structDNA_compareflags(fd->filesdna, fd->memsdna);
769 } else if (bhead->code==ENDB)
775 static int fd_read_from_file(FileData *filedata, void *buffer, int size)
777 int readsize = read(filedata->filedes, buffer, size);
782 filedata->seek += readsize;
788 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, int size)
790 int readsize = gzread(filedata->gzfiledes, buffer, size);
795 filedata->seek += readsize;
801 static int fd_read_from_memory(FileData *filedata, void *buffer, int size)
803 // don't read more bytes then there are available in the buffer
804 int readsize = MIN2(size, filedata->buffersize - filedata->seek);
806 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
807 filedata->seek += readsize;
812 static int fd_read_from_memfile(FileData *filedata, void *buffer, int size)
814 static unsigned int seek= 1<<30; /* the current position */
815 static unsigned int offset= 0; /* size of previous chunks */
816 static MemFileChunk *chunk=NULL;
818 if(size==0) return 0;
820 if(seek != filedata->seek) {
821 chunk= filedata->memfile->chunks.first;
825 if(seek + chunk->size > (unsigned) filedata->seek) break;
830 seek= filedata->seek;
834 /* first check if it's on the end if current chunk */
835 if( seek-offset == chunk->size) {
836 offset+= chunk->size;
840 /* debug, should never happen */
842 printf("illegal read, chunk zero\n");
845 else if( (seek-offset)+size > chunk->size) {
846 size= chunk->size - (seek-offset);
847 printf("chunk too large, clipped to %d\n", size);
850 memcpy(buffer, chunk->buf + (seek-offset), size);
851 filedata->seek += size;
860 static FileData *filedata_new(void)
862 extern char DNAstr[]; /* DNA.c */
864 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
867 fd->gzfiledes = NULL;
869 /* XXX, this doesn't need to be done all the time,
870 * but it keeps us reentrant, remove once we have
871 * a lib that provides a nice lock. - zr
873 fd->memsdna = dna_sdna_from_data(DNAstr, DNAlen, 0);
875 fd->datamap = oldnewmap_new();
876 fd->globmap = oldnewmap_new();
877 fd->libmap = oldnewmap_new();
882 static FileData *blo_decode_and_check(FileData *fd, BlendReadError *error_r)
884 decode_blender_header(fd);
886 if (fd->flags & FD_FLAGS_FILE_OK) {
887 if (!read_file_dna(fd)) {
888 *error_r = BRE_INCOMPLETE;
889 blo_freefiledata(fd);
893 *error_r = BRE_NOT_A_BLEND;
894 blo_freefiledata(fd);
901 /* cannot be called with relative paths anymore! */
902 /* on each new library added, it now checks for the current FileData and expands relativeness */
903 FileData *blo_openblenderfile(char *name, BlendReadError *error_r)
907 gzfile= gzopen(name, "rb");
909 if (NULL == gzfile) {
910 *error_r = BRE_UNABLE_TO_OPEN;
913 FileData *fd = filedata_new();
914 fd->gzfiledes = gzfile;
915 BLI_strncpy(fd->filename, name, sizeof(fd->filename)); // now only in use by library append
916 fd->read = fd_read_gzip_from_file;
918 return blo_decode_and_check(fd, error_r);
922 FileData *blo_openblendermemory(void *mem, int memsize, BlendReadError *error_r)
924 if (!mem || memsize<SIZEOFBLENDERHEADER) {
925 *error_r = mem?BRE_UNABLE_TO_READ:BRE_UNABLE_TO_OPEN;
928 FileData *fd= filedata_new();
930 fd->buffersize= memsize;
931 fd->read= fd_read_from_memory;
932 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
934 return blo_decode_and_check(fd, error_r);
938 FileData *blo_openblendermemfile(MemFile *memfile, BlendReadError *error_r)
941 *error_r = BRE_UNABLE_TO_OPEN;
944 FileData *fd= filedata_new();
945 fd->memfile= memfile;
947 fd->read= fd_read_from_memfile;
948 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
950 return blo_decode_and_check(fd, error_r);
955 void blo_freefiledata(FileData *fd)
959 if (fd->filedes != -1) {
963 if (fd->gzfiledes != NULL)
965 gzclose(fd->gzfiledes);
968 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
969 MEM_freeN(fd->buffer);
973 // Free all BHeadN data blocks
974 BLI_freelistN(&fd->listbase);
977 dna_freestructDNA(fd->memsdna);
979 dna_freestructDNA(fd->filesdna);
981 MEM_freeN(fd->compflags);
984 oldnewmap_free(fd->datamap);
986 oldnewmap_free(fd->globmap);
987 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
988 oldnewmap_free(fd->libmap);
994 /* ************ DIV ****************** */
996 int BLO_has_bfile_extension(char *str)
998 return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend")||BLI_testextensie(str, ".blend.gz"));
1001 /* ************** OLD POINTERS ******************* */
1003 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1005 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1008 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1010 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1013 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1015 return oldnewmap_liblookup(fd->libmap, adr, lib);
1018 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1020 ID *id= newlibadr(fd, lib, adr);
1029 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1033 for (i=0; i<fd->libmap->nentries; i++) {
1034 OldNew *entry= &fd->libmap->entries[i];
1036 if (old==entry->newp && entry->nr==ID_ID) {
1038 if(new) entry->nr= GS( ((ID *)new)->name );
1044 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1048 for(main= mainlist->first; main; main= main->next) {
1051 if(main->curlib) fd= main->curlib->filedata;
1055 change_idid_adr_fd(fd, old, new);
1060 /* ********** END OLD POINTERS ****************** */
1061 /* ********** READ FILE ****************** */
1063 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1065 int blocksize, nblocks;
1068 data= (char *)(bhead+1); /* BHEAD+DATA dependancy */
1069 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1073 dna_switch_endian_struct(filesdna, bhead->SDNAnr, data);
1079 static void *read_struct(FileData *fd, BHead *bh, char *blockname)
1084 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1085 switch_endian_structs(fd->filesdna, bh);
1087 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1088 if(fd->compflags[bh->SDNAnr]==2) {
1089 temp= dna_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1091 temp= MEM_mallocN(bh->len, blockname);
1092 memcpy(temp, (bh+1), bh->len); /* BHEAD+DATA dependancy */
1100 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1104 if(lb->first==0) return;
1106 lb->first= newdataadr(fd, lb->first);
1110 ln->next= newdataadr(fd, ln->next);
1118 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1123 if(lb->first==0) return;
1124 poin= newdataadr(fd, lb->first);
1126 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1133 poin= newdataadr(fd, ln->next);
1135 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1145 static void test_pointer_array(FileData *fd, void **mat)
1147 #if defined(WIN32) && !defined(FREE_WINDOWS)
1148 __int64 *lpoin, *lmat;
1150 long long *lpoin, *lmat;
1152 int len, *ipoin, *imat;
1154 /* manually convert the pointer array in
1155 * the old dna format to a pointer array in
1156 * the new dna format.
1159 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1161 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1162 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1166 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1167 SWITCH_LONGINT(*lpoin);
1168 *ipoin= (int) ((*lpoin) >> 3);
1176 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1177 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1191 /* ************ READ CurveMapping *************** */
1193 /* cuma itself has been read! */
1194 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1198 for(a=0; a<CM_TOT; a++) {
1199 cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
1200 cumap->cm[a].table= NULL;
1204 /* ************ READ NODE TREE *************** */
1206 /* singe node tree, ntree is not NULL */
1207 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
1211 for(node= ntree->nodes.first; node; node= node->next)
1212 node->id= newlibadr_us(fd, id->lib, node->id);
1215 /* library linking after fileread */
1216 static void lib_link_nodetree(FileData *fd, Main *main)
1223 /* in multiple steps, first link ID pointers */
1224 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
1225 if(ntree->id.flag & LIB_NEEDLINK) {
1226 lib_link_ntree(fd, &ntree->id, ntree);
1230 /* now create the own typeinfo structs an verify nodes */
1231 /* here we still assume no groups in groups */
1232 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
1233 if(ntree->id.flag & LIB_NEEDLINK) {
1234 ntree->id.flag -= LIB_NEEDLINK;
1236 ntreeVerifyTypes(ntree); /* internal nodes, no groups! */
1237 ntreeMakeOwnType(ntree); /* for group usage */
1241 /* now verify all types in material trees, groups are set OK now */
1242 for(ma= main->mat.first; ma; ma= ma->id.next) {
1244 ntreeVerifyTypes(ma->nodetree);
1246 /* and scene trees */
1247 for(sce= main->scene.first; sce; sce= sce->id.next) {
1249 ntreeVerifyTypes(sce->nodetree);
1253 /* ntree itself has been read! */
1254 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
1256 /* note: writing and reading goes in sync, for speed */
1261 ntree->init= 0; /* to set callbacks */
1262 ntree->owntype= NULL;
1264 link_list(fd, &ntree->nodes);
1265 for(node= ntree->nodes.first; node; node= node->next) {
1266 node->storage= newdataadr(fd, node->storage);
1269 /* could be handlerized at some point, now only 1 exception still */
1270 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
1271 direct_link_curvemapping(fd, node->storage);
1272 else if(ntree->type==NTREE_COMPOSIT && (node->type==CMP_NODE_CURVE_VEC || node->type==CMP_NODE_CURVE_RGB))
1273 direct_link_curvemapping(fd, node->storage);
1276 link_list(fd, &node->inputs);
1277 link_list(fd, &node->outputs);
1279 link_list(fd, &ntree->links);
1281 /* and we connect the rest */
1282 for(node= ntree->nodes.first; node; node= node->next) {
1283 node->preview= NULL;
1286 for(sock= node->inputs.first; sock; sock= sock->next)
1287 sock->link= newdataadr(fd, sock->link);
1289 for(link= ntree->links.first; link; link= link->next) {
1290 link->fromnode= newdataadr(fd, link->fromnode);
1291 link->tonode= newdataadr(fd, link->tonode);
1292 link->fromsock= newdataadr(fd, link->fromsock);
1293 link->tosock= newdataadr(fd, link->tosock);
1296 /* type verification is in lib-link */
1299 /* ************ READ PACKEDFILE *************** */
1301 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1303 PackedFile *pf= newdataadr(fd, oldpf);
1306 pf->data= newdataadr(fd, pf->data);
1312 /* ************ READ SCRIPTLINK *************** */
1314 static void lib_link_scriptlink(FileData *fd, ID *id, ScriptLink *slink)
1318 for(i=0; i<slink->totscript; i++) {
1319 slink->scripts[i]= newlibadr(fd, id->lib, slink->scripts[i]);
1323 static void direct_link_scriptlink(FileData *fd, ScriptLink *slink)
1325 slink->scripts= newdataadr(fd, slink->scripts);
1326 test_pointer_array(fd, (void **)&slink->scripts);
1328 slink->flag= newdataadr(fd, slink->flag);
1330 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1333 for(a=0; a<slink->totscript; a++) {
1334 SWITCH_SHORT(slink->flag[a]);
1339 /* ************ READ ARMATURE ***************** */
1341 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1343 bActionStrip *strip;
1345 for (strip=striplist->first; strip; strip=strip->next){
1346 strip->object = newlibadr(fd, id->lib, strip->object);
1347 strip->act = newlibadr_us(fd, id->lib, strip->act);
1348 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1352 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1354 bConstraintChannel *chan;
1356 for (chan=chanbase->first; chan; chan=chan->next){
1357 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1361 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
1365 for (con = conlist->first; con; con=con->next) {
1366 /* patch for error introduced by changing constraints (dunno how) */
1367 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
1368 if(con->data==NULL) {
1369 con->type= CONSTRAINT_TYPE_NULL;
1372 switch (con->type) {
1373 case CONSTRAINT_TYPE_ACTION:
1375 bActionConstraint *data;
1376 data= ((bActionConstraint*)con->data);
1377 data->tar = newlibadr(fd, id->lib, data->tar);
1378 data->act = newlibadr(fd, id->lib, data->act);
1381 case CONSTRAINT_TYPE_LOCLIKE:
1383 bLocateLikeConstraint *data;
1384 data= ((bLocateLikeConstraint*)con->data);
1385 data->tar = newlibadr(fd, id->lib, data->tar);
1388 case CONSTRAINT_TYPE_ROTLIKE:
1390 bRotateLikeConstraint *data;
1391 data= ((bRotateLikeConstraint*)con->data);
1392 data->tar = newlibadr(fd, id->lib, data->tar);
1395 case CONSTRAINT_TYPE_KINEMATIC:
1397 bKinematicConstraint *data;
1398 data = ((bKinematicConstraint*)con->data);
1399 data->tar = newlibadr(fd, id->lib, data->tar);
1402 case CONSTRAINT_TYPE_TRACKTO:
1404 bTrackToConstraint *data;
1405 data = ((bTrackToConstraint*)con->data);
1406 data->tar = newlibadr(fd, id->lib, data->tar);
1409 case CONSTRAINT_TYPE_MINMAX:
1411 bMinMaxConstraint *data;
1412 data = ((bMinMaxConstraint*)con->data);
1413 data->tar = newlibadr(fd, id->lib, data->tar);
1416 case CONSTRAINT_TYPE_LOCKTRACK:
1418 bLockTrackConstraint *data;
1419 data= ((bLockTrackConstraint*)con->data);
1420 data->tar = newlibadr(fd, id->lib, data->tar);
1423 case CONSTRAINT_TYPE_FOLLOWPATH:
1425 bFollowPathConstraint *data;
1426 data= ((bFollowPathConstraint*)con->data);
1427 data->tar = newlibadr(fd, id->lib, data->tar);
1430 case CONSTRAINT_TYPE_DISTANCELIMIT:
1432 bDistanceLimitConstraint *data;
1433 data= ((bDistanceLimitConstraint*)con->data);
1434 data->tar = newlibadr(fd, id->lib, data->tar);
1437 case CONSTRAINT_TYPE_STRETCHTO:
1439 bStretchToConstraint *data;
1440 data= ((bStretchToConstraint*)con->data);
1441 data->tar = newlibadr(fd, id->lib, data->tar);
1445 case CONSTRAINT_TYPE_NULL:
1451 static void direct_link_constraints(FileData *fd, ListBase *lb)
1456 for (cons=lb->first; cons; cons=cons->next) {
1457 cons->data = newdataadr(fd, cons->data);
1461 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
1463 bPoseChannel *pchan;
1464 bArmature *arm= ob->data;
1469 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
1470 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
1471 // hurms... loop in a loop, but yah... later... (ton)
1472 pchan->bone= get_named_bone(arm, pchan->name);
1473 pchan->custom= newlibadr(fd, arm->id.lib, pchan->custom);
1474 if(pchan->bone==NULL)
1478 ob->recalc= OB_RECALC;
1479 pose->flag |= POSE_RECALC;
1483 static void lib_link_armature(FileData *fd, Main *main)
1487 arm= main->armature.first;
1490 if(arm->id.flag & LIB_NEEDLINK) {
1491 arm->id.flag -= LIB_NEEDLINK;
1497 static void lib_link_action(FileData *fd, Main *main)
1500 bActionChannel *chan;
1502 act= main->action.first;
1504 if(act->id.flag & LIB_NEEDLINK) {
1505 act->id.flag -= LIB_NEEDLINK;
1507 for (chan=act->chanbase.first; chan; chan=chan->next) {
1508 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1509 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1517 static void direct_link_bones(FileData *fd, Bone* bone)
1521 bone->parent= newdataadr(fd, bone->parent);
1523 link_list(fd, &bone->childbase);
1525 for (child=bone->childbase.first; child; child=child->next) {
1526 direct_link_bones(fd, child);
1531 static void direct_link_action(FileData *fd, bAction *act)
1533 bActionChannel *achan;
1535 link_list(fd, &act->chanbase);
1537 for (achan = act->chanbase.first; achan; achan=achan->next)
1538 link_list(fd, &achan->constraintChannels);
1542 static void direct_link_armature(FileData *fd, bArmature *arm)
1546 link_list(fd, &arm->bonebase);
1548 bone=arm->bonebase.first;
1550 direct_link_bones(fd, bone);
1555 /* ************ READ CAMERA ***************** */
1557 static void lib_link_camera(FileData *fd, Main *main)
1561 ca= main->camera.first;
1563 if(ca->id.flag & LIB_NEEDLINK) {
1565 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo);
1567 lib_link_scriptlink(fd, &ca->id, &ca->scriptlink);
1569 ca->id.flag -= LIB_NEEDLINK;
1575 static void direct_link_camera(FileData *fd, Camera *ca)
1577 direct_link_scriptlink(fd, &ca->scriptlink);
1581 /* ************ READ LATTICE ***************** */
1583 static void lib_link_latt(FileData *fd, Main *main)
1587 lt= main->latt.first;
1589 if(lt->id.flag & LIB_NEEDLINK) {
1591 lt->ipo= newlibadr_us(fd, lt->id.lib, lt->ipo);
1592 lt->key= newlibadr_us(fd, lt->id.lib, lt->key);
1594 lt->id.flag -= LIB_NEEDLINK;
1600 static void direct_link_latt(FileData *fd, Lattice *lt)
1602 lt->def= newdataadr(fd, lt->def);
1605 /* ************ READ LAMP ***************** */
1607 static void lib_link_lamp(FileData *fd, Main *main)
1613 la= main->lamp.first;
1615 if(la->id.flag & LIB_NEEDLINK) {
1617 for(a=0; a<MAX_MTEX; a++) {
1620 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
1621 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
1625 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo);
1627 lib_link_scriptlink(fd, &la->id, &la->scriptlink);
1629 la->id.flag -= LIB_NEEDLINK;
1635 static void direct_link_lamp(FileData *fd, Lamp *la)
1639 direct_link_scriptlink(fd, &la->scriptlink);
1641 for(a=0; a<MAX_MTEX; a++) {
1642 la->mtex[a]= newdataadr(fd, la->mtex[a]);
1646 /* ************ READ keys ***************** */
1648 static void lib_link_key(FileData *fd, Main *main)
1652 key= main->key.first;
1654 if(key->id.flag & LIB_NEEDLINK) {
1656 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo);
1657 key->from= newlibadr(fd, key->id.lib, key->from);
1659 key->id.flag -= LIB_NEEDLINK;
1665 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
1668 char *data, *poin, *cp;
1670 elemsize= key->elemsize;
1673 for(a=0; a<kb->totelem; a++) {
1678 while( cp[0] ) { /* cp[0]==amount */
1680 switch(cp[1]) { /* cp[1]= type */
1686 SWITCH_INT((*poin));
1699 static void direct_link_key(FileData *fd, Key *key)
1703 link_list(fd, &(key->block));
1705 key->refkey= newdataadr(fd, key->refkey);
1707 kb= key->block.first;
1710 kb->data= newdataadr(fd, kb->data);
1712 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
1713 switch_endian_keyblock(key, kb);
1719 /* ************ READ mball ***************** */
1721 static void lib_link_mball(FileData *fd, Main *main)
1726 mb= main->mball.first;
1728 if(mb->id.flag & LIB_NEEDLINK) {
1730 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
1732 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo);
1734 mb->id.flag -= LIB_NEEDLINK;
1740 static void direct_link_mball(FileData *fd, MetaBall *mb)
1742 mb->mat= newdataadr(fd, mb->mat);
1743 test_pointer_array(fd, (void **)&mb->mat);
1745 link_list(fd, &(mb->elems));
1747 mb->disp.first= mb->disp.last= 0;
1752 /* ************ READ WORLD ***************** */
1754 static void lib_link_world(FileData *fd, Main *main)
1760 wrld= main->world.first;
1762 if(wrld->id.flag & LIB_NEEDLINK) {
1764 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo);
1766 for(a=0; a<MAX_MTEX; a++) {
1767 mtex= wrld->mtex[a];
1769 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
1770 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
1774 lib_link_scriptlink(fd, &wrld->id, &wrld->scriptlink);
1776 wrld->id.flag -= LIB_NEEDLINK;
1778 wrld= wrld->id.next;
1782 static void direct_link_world(FileData *fd, World *wrld)
1786 direct_link_scriptlink(fd, &wrld->scriptlink);
1788 for(a=0; a<MAX_MTEX; a++) {
1789 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
1794 /* ************ READ IPO ***************** */
1796 static void lib_link_ipo(FileData *fd, Main *main)
1800 ipo= main->ipo.first;
1802 if(ipo->id.flag & LIB_NEEDLINK) {
1804 for(icu= ipo->curve.first; icu; icu= icu->next) {
1806 icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
1808 ipo->id.flag -= LIB_NEEDLINK;
1814 static void direct_link_ipo(FileData *fd, Ipo *ipo)
1818 link_list(fd, &(ipo->curve));
1819 icu= ipo->curve.first;
1821 icu->bezt= newdataadr(fd, icu->bezt);
1822 icu->bp= newdataadr(fd, icu->bp);
1823 icu->driver= newdataadr(fd, icu->driver);
1828 /* ************ READ VFONT ***************** */
1830 static void lib_link_vfont(FileData *fd, Main *main)
1834 vf= main->vfont.first;
1836 if(vf->id.flag & LIB_NEEDLINK) {
1837 vf->id.flag -= LIB_NEEDLINK;
1843 static void direct_link_vfont(FileData *fd, VFont *vf)
1846 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
1849 /* ************ READ TEXT ****************** */
1851 static void lib_link_text(FileData *fd, Main *main)
1855 text= main->text.first;
1857 if(text->id.flag & LIB_NEEDLINK) {
1858 text->id.flag -= LIB_NEEDLINK;
1860 text= text->id.next;
1864 static void direct_link_text(FileData *fd, Text *text)
1868 text->name= newdataadr(fd, text->name);
1871 text->undo_len= TXT_INIT_UNDO;
1872 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
1874 text->compiled= NULL;
1877 if(text->flags & TXT_ISEXT) {
1882 link_list(fd, &text->lines);
1884 text->curl= newdataadr(fd, text->curl);
1885 text->sell= newdataadr(fd, text->sell);
1887 ln= text->lines.first;
1889 ln->line= newdataadr(fd, ln->line);
1892 if (ln->len != (int) strlen(ln->line)) {
1893 printf("Error loading text, line lengths differ\n");
1894 ln->len = strlen(ln->line);
1900 text->flags = (text->flags|TXT_ISTMP) & ~TXT_ISEXT;
1905 /* ************ READ IMAGE ***************** */
1907 static void lib_link_image(FileData *fd, Main *main)
1911 ima= main->image.first;
1913 if(ima->id.flag & LIB_NEEDLINK) {
1915 ima->id.flag -= LIB_NEEDLINK;
1921 static void direct_link_image(FileData *fd, Image *ima)
1925 memset(ima->mipmap, 0, sizeof(ima->mipmap));
1929 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
1935 /* ************ READ CURVE ***************** */
1937 static void lib_link_curve(FileData *fd, Main *main)
1942 cu= main->curve.first;
1944 if(cu->id.flag & LIB_NEEDLINK) {
1946 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
1948 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
1949 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
1950 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
1951 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
1952 cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);
1953 cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
1954 cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
1956 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo);
1957 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
1959 cu->id.flag -= LIB_NEEDLINK;
1966 static void switch_endian_knots(Nurb *nu)
1973 SWITCH_INT(nu->knotsu[len]);
1979 SWITCH_INT(nu->knotsv[len]);
1984 static void direct_link_curve(FileData *fd, Curve *cu)
1989 cu->mat= newdataadr(fd, cu->mat);
1990 test_pointer_array(fd, (void **)&cu->mat);
1991 cu->str= newdataadr(fd, cu->str);
1992 cu->strinfo= newdataadr(fd, cu->strinfo);
1993 cu->tb= newdataadr(fd, cu->tb);
1995 if(cu->vfont==0) link_list(fd, &(cu->nurb));
1997 cu->nurb.first=cu->nurb.last= 0;
1999 tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
2001 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
2008 cu->tb[0].w = cu->linewidth;
2010 if (cu->wordspace == 0.0) cu->wordspace = 1.0;
2013 cu->bev.first=cu->bev.last= 0;
2014 cu->disp.first=cu->disp.last= 0;
2019 nu->bezt= newdataadr(fd, nu->bezt);
2020 nu->bp= newdataadr(fd, nu->bp);
2021 nu->knotsu= newdataadr(fd, nu->knotsu);
2022 nu->knotsv= newdataadr(fd, nu->knotsv);
2023 if (cu->vfont==0) nu->charidx= nu->mat_nr;
2025 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2026 switch_endian_knots(nu);
2034 /* ************ READ TEX ***************** */
2036 static void lib_link_texture(FileData *fd, Main *main)
2040 tex= main->tex.first;
2042 if(tex->id.flag & LIB_NEEDLINK) {
2044 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
2045 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
2046 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
2048 tex->id.flag -= LIB_NEEDLINK;
2054 static void direct_link_texture(FileData *fd, Tex *tex)
2056 tex->plugin= newdataadr(fd, tex->plugin);
2058 tex->plugin->handle= 0;
2059 open_plugin_tex(tex->plugin);
2061 tex->coba= newdataadr(fd, tex->coba);
2062 tex->env= newdataadr(fd, tex->env);
2065 memset(tex->env->cube, 0, 6*sizeof(void *));
2072 /* ************ READ MATERIAL ***************** */
2074 static void lib_link_material(FileData *fd, Main *main)
2080 ma= main->mat.first;
2082 if(ma->id.flag & LIB_NEEDLINK) {
2084 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
2085 ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
2087 for(a=0; a<MAX_MTEX; a++) {
2090 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
2091 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
2094 lib_link_scriptlink(fd, &ma->id, &ma->scriptlink);
2097 lib_link_ntree(fd, &ma->id, ma->nodetree);
2099 ma->id.flag -= LIB_NEEDLINK;
2105 static void direct_link_material(FileData *fd, Material *ma)
2109 for(a=0; a<MAX_MTEX; a++) {
2110 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
2113 ma->ramp_col= newdataadr(fd, ma->ramp_col);
2114 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
2116 direct_link_scriptlink(fd, &ma->scriptlink);
2118 ma->nodetree= newdataadr(fd, ma->nodetree);
2120 direct_link_nodetree(fd, ma->nodetree);
2123 /* ************ READ MESH ***************** */
2125 static void lib_link_mesh(FileData *fd, Main *main)
2129 me= main->mesh.first;
2131 if(me->id.flag & LIB_NEEDLINK) {
2134 /* this check added for python created meshes */
2136 for(i=0; i<me->totcol; i++) {
2137 me->mat[i]= newlibadr_us(fd, me->id.lib, me->mat[i]);
2142 me->ipo= newlibadr_us(fd, me->id.lib, me->ipo);
2143 me->key= newlibadr_us(fd, me->id.lib, me->key);
2144 me->texcomesh= newlibadr_us(fd, me->id.lib, me->texcomesh);
2147 TFace *tfaces= me->tface;
2149 for (i=0; i<me->totface; i++) {
2150 TFace *tf= &tfaces[i];
2152 tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
2154 Image *ima= tf->tpage;
2160 me->id.flag -= LIB_NEEDLINK;
2166 static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
2173 for (i=0; i<count; i++) {
2174 mdverts[i].dw=newdataadr(fd, mdverts[i].dw);
2176 mdverts[i].totweight=0;
2180 static void direct_link_mesh(FileData *fd, Mesh *mesh)
2182 mesh->mat= newdataadr(fd, mesh->mat);
2183 test_pointer_array(fd, (void **)&mesh->mat);
2185 mesh->mvert= newdataadr(fd, mesh->mvert);
2186 mesh->medge= newdataadr(fd, mesh->medge);
2187 mesh->mface= newdataadr(fd, mesh->mface);
2188 mesh->tface= newdataadr(fd, mesh->tface);
2189 mesh->mcol= newdataadr(fd, mesh->mcol);
2190 mesh->msticky= newdataadr(fd, mesh->msticky);
2192 mesh->dvert= newdataadr(fd, mesh->dvert);
2193 direct_link_dverts(fd, mesh->totvert, mesh->dvert);
2200 TFace *tfaces= mesh->tface;
2203 for (i=0; i<mesh->totface; i++) {
2204 TFace *tf= &tfaces[i];
2206 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2207 SWITCH_INT(tf->col[0]);
2208 SWITCH_INT(tf->col[1]);
2209 SWITCH_INT(tf->col[2]);
2210 SWITCH_INT(tf->col[3]);
2216 /* ************ READ OBJECT ***************** */
2218 static void lib_link_modifiers__linkModifiers(void *userData, Object *ob, Object **obpoin)
2220 FileData *fd = userData;
2222 *obpoin = newlibadr(fd, ob->id.lib, *obpoin);
2224 static void lib_link_modifiers(FileData *fd, Object *ob)
2226 modifiers_foreachObjectLink(ob, lib_link_modifiers__linkModifiers, fd);
2229 static void lib_link_object(FileData *fd, Main *main)
2239 ob= main->object.first;
2241 if(ob->id.flag & LIB_NEEDLINK) {
2243 ob->parent= newlibadr(fd, ob->id.lib, ob->parent);
2244 ob->track= newlibadr(fd, ob->id.lib, ob->track);
2245 ob->ipo= newlibadr_us(fd, ob->id.lib, ob->ipo);
2246 ob->action = newlibadr_us(fd, ob->id.lib, ob->action);
2247 ob->dup_group= newlibadr_us(fd, ob->id.lib, ob->dup_group);
2250 ob->data= newlibadr_us(fd, ob->id.lib, ob->data);
2252 if(ob->data==NULL && poin!=NULL) {
2255 if(ob->id.lib) printf("Can't find obdata of %s lib %s\n", ob->id.name+2, ob->id.lib->name);
2256 else printf("Object %s lost data. Lib:%x\n", ob->id.name+2, (unsigned int) ob->id.lib);
2258 for(a=0; a<ob->totcol; a++) ob->mat[a]= newlibadr_us(fd, ob->id.lib, ob->mat[a]);
2260 ob->id.flag -= LIB_NEEDLINK;
2261 /* if id.us==0 a new base will be created later on */
2263 /* WARNING! Also check expand_object(), should reflect the stuff below. */
2264 lib_link_pose(fd, ob, ob->pose);
2265 lib_link_constraints(fd, &ob->id, &ob->constraints);
2266 lib_link_nlastrips(fd, &ob->id, &ob->nlastrips);
2267 lib_link_constraint_channels(fd, &ob->id, &ob->constraintChannels);
2269 for(paf= ob->effect.first; paf; paf= paf->next) {
2270 if(paf->type==EFF_PARTICLE) {
2271 paf->group= newlibadr_us(fd, ob->id.lib, paf->group);
2275 sens= ob->sensors.first;
2277 if(ob->id.lib==NULL) { // done in expand_main
2278 for(a=0; a<sens->totlinks; a++) {
2279 sens->links[a]= newglobadr(fd, sens->links[a]);
2282 if(sens->type==SENS_TOUCH) {
2283 bTouchSensor *ts= sens->data;
2284 ts->ma= newlibadr(fd, ob->id.lib, ts->ma);
2286 else if(sens->type==SENS_MESSAGE) {
2287 bMessageSensor *ms= sens->data;
2289 newlibadr(fd, ob->id.lib, ms->fromObject);
2294 cont= ob->controllers.first;
2296 if(ob->id.lib==NULL) { // done in expand_main
2297 for(a=0; a<cont->totlinks; a++) {
2298 cont->links[a]= newglobadr(fd, cont->links[a]);
2301 if(cont->type==CONT_PYTHON) {
2302 bPythonCont *pc= cont->data;
2303 pc->text= newlibadr(fd, ob->id.lib, pc->text);
2311 act= ob->actuators.first;
2313 if(act->type==ACT_SOUND) {
2314 bSoundActuator *sa= act->data;
2315 sa->sound= newlibadr_us(fd, ob->id.lib, sa->sound);
2317 else if(act->type==ACT_CD) {
2318 /* bCDActuator *cda= act->data; */
2320 else if(act->type==ACT_GAME) {
2321 /* bGameActuator *ga= act->data; */
2323 else if(act->type==ACT_CAMERA) {
2324 bCameraActuator *ca= act->data;
2325 ca->ob= newlibadr(fd, ob->id.lib, ca->ob);
2327 /* leave this one, it's obsolete but necessary to read for conversion */
2328 else if(act->type==ACT_ADD_OBJECT) {
2329 bAddObjectActuator *eoa= act->data;
2330 if(eoa) eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
2332 else if(act->type==ACT_EDIT_OBJECT) {
2333 bEditObjectActuator *eoa= act->data;
2337 eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
2338 eoa->me= newlibadr(fd, ob->id.lib, eoa->me);
2340 else if(act->type==ACT_SCENE) {
2341 bSceneActuator *sa= act->data;
2342 sa->camera= newlibadr(fd, ob->id.lib, sa->camera);
2343 sa->scene= newlibadr(fd, ob->id.lib, sa->scene);
2345 else if(act->type==ACT_ACTION) {
2346 bActionActuator *aa= act->data;
2347 aa->act= newlibadr(fd, ob->id.lib, aa->act);
2349 else if(act->type==ACT_PROPERTY) {
2350 bPropertyActuator *pa= act->data;
2351 pa->ob= newlibadr(fd, ob->id.lib, pa->ob);
2353 else if(act->type==ACT_MESSAGE) {
2354 bMessageActuator *ma= act->data;
2355 ma->toObject= newlibadr(fd, ob->id.lib, ma->toObject);
2360 lib_link_scriptlink(fd, &ob->id, &ob->scriptlink);
2361 lib_link_modifiers(fd, ob);
2366 if(warn) error("WARNING IN CONSOLE");
2370 static void direct_link_pose(FileData *fd, bPose *pose) {
2372 bPoseChannel *pchan;
2377 link_list(fd, &pose->chanbase);
2379 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
2381 pchan->parent= newdataadr(fd, pchan->parent);
2382 pchan->child= newdataadr(fd, pchan->child);
2383 direct_link_constraints(fd, &pchan->constraints);
2384 pchan->iktree.first= pchan->iktree.last= NULL;
2390 static void direct_link_modifiers(FileData *fd, ListBase *lb)
2396 for (md=lb->first; md; md=md->next) {
2399 if (md->type==eModifierType_Subsurf) {
2400 SubsurfModifierData *smd = (SubsurfModifierData*) md;
2402 smd->emCache = smd->mCache = 0;
2403 } else if (md->type==eModifierType_Hook) {
2404 HookModifierData *hmd = (HookModifierData*) md;
2406 hmd->indexar= newdataadr(fd, hmd->indexar);
2407 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2409 for(a=0; a<hmd->totindex; a++) {
2410 SWITCH_INT(hmd->indexar[a]);
2417 static void direct_link_object(FileData *fd, Object *ob)
2426 ob->disp.first=ob->disp.last= NULL;
2428 ob->pose= newdataadr(fd, ob->pose);
2429 direct_link_pose(fd, ob->pose);
2431 link_list(fd, &ob->defbase);
2432 link_list(fd, &ob->nlastrips);
2433 link_list(fd, &ob->constraintChannels);
2435 direct_link_scriptlink(fd, &ob->scriptlink);
2437 ob->mat= newdataadr(fd, ob->mat);
2438 test_pointer_array(fd, (void **)&ob->mat);
2440 /* do it here, below old data gets converted */
2441 direct_link_modifiers(fd, &ob->modifiers);
2443 link_list(fd, &ob->effect);
2444 paf= ob->effect.first;
2446 if(paf->type==EFF_PARTICLE) {
2449 if(paf->type==EFF_WAVE) {
2450 WaveEff *wav = (WaveEff*) paf;
2451 PartEff *next = paf->next;
2452 WaveModifierData *wmd = (WaveModifierData*) modifier_new(eModifierType_Wave);
2454 wmd->damp = wav->damp;
2455 wmd->flag = wav->flag;
2456 wmd->height = wav->height;
2457 wmd->lifetime = wav->lifetime;
2458 wmd->narrow = wav->narrow;
2459 wmd->speed = wav->speed;
2460 wmd->startx = wav->startx;
2461 wmd->starty = wav->startx;
2462 wmd->timeoffs = wav->timeoffs;
2463 wmd->width = wav->width;
2465 BLI_addtail(&ob->modifiers, wmd);
2467 BLI_remlink(&ob->effect, paf);
2473 if(paf->type==EFF_BUILD) {
2474 BuildEff *baf = (BuildEff*) paf;
2475 PartEff *next = paf->next;
2476 BuildModifierData *bmd = (BuildModifierData*) modifier_new(eModifierType_Build);
2478 bmd->start = baf->sfra;
2479 bmd->length = baf->len;
2483 BLI_addtail(&ob->modifiers, bmd);
2485 BLI_remlink(&ob->effect, paf);
2494 ob->pd= newdataadr(fd, ob->pd);
2495 ob->soft= newdataadr(fd, ob->soft);
2497 SoftBody *sb= ob->soft;
2499 sb->bpoint= NULL; // init pointers so it gets rebuilt nicely
2502 sb->keys= newdataadr(fd, sb->keys);
2503 test_pointer_array(fd, (void **)&sb->keys);
2505 for(a=0; a<sb->totkey; a++) {
2506 sb->keys[a]= newdataadr(fd, sb->keys[a]);
2510 ob->fluidsimSettings= newdataadr(fd, ob->fluidsimSettings); /* NT */
2511 if(ob->fluidsimSettings) {
2512 // reinit mesh pointers
2513 ob->fluidsimSettings->orgMesh = NULL; //ob->data;
2514 ob->fluidsimSettings->meshSurface = NULL;
2515 ob->fluidsimSettings->meshBB = NULL;
2518 link_list(fd, &ob->prop);
2519 prop= ob->prop.first;
2521 prop->poin= newdataadr(fd, prop->poin);
2522 if(prop->poin==0) prop->poin= &prop->data;
2526 link_list(fd, &ob->sensors);
2527 sens= ob->sensors.first;
2529 sens->data= newdataadr(fd, sens->data);
2530 sens->links= newdataadr(fd, sens->links);
2531 test_pointer_array(fd, (void **)&sens->links);
2535 direct_link_constraints(fd, &ob->constraints);
2537 link_glob_list(fd, &ob->controllers);
2538 cont= ob->controllers.first;
2540 cont->data= newdataadr(fd, cont->data);
2541 cont->links= newdataadr(fd, cont->links);
2542 test_pointer_array(fd, (void **)&cont->links);
2546 link_glob_list(fd, &ob->actuators);
2547 act= ob->actuators.first;
2549 act->data= newdataadr(fd, act->data);
2553 link_list(fd, &ob->hooks);
2554 while (ob->hooks.first) {
2555 ObHook *hook = ob->hooks.first;
2556 HookModifierData *hmd = (HookModifierData*) modifier_new(eModifierType_Hook);
2558 hook->indexar= newdataadr(fd, hook->indexar);
2559 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2561 for(a=0; a<hook->totindex; a++) {
2562 SWITCH_INT(hook->indexar[a]);
2566 /* Do conversion here because if we have loaded
2567 * a hook we need to make sure it gets converted
2568 * and free'd, regardless of version.
2570 VECCOPY(hmd->cent, hook->cent);
2571 hmd->falloff = hook->falloff;
2572 hmd->force = hook->force;
2573 hmd->indexar = hook->indexar;
2574 hmd->object = hook->parent;
2575 memcpy(hmd->parentinv, hook->parentinv, sizeof(hmd->parentinv));
2576 hmd->totindex = hook->totindex;
2578 BLI_addhead(&ob->modifiers, hmd);
2579 BLI_remlink(&ob->hooks, hook);
2585 ob->derivedDeform= NULL;
2586 ob->derivedFinal= NULL;
2589 /* ************ READ SCENE ***************** */
2591 static void lib_link_scene(FileData *fd, Main *main)
2598 sce= main->scene.first;
2600 if(sce->id.flag & LIB_NEEDLINK) {
2602 sce->camera= newlibadr(fd, sce->id.lib, sce->camera);
2603 sce->world= newlibadr_us(fd, sce->id.lib, sce->world);
2604 sce->set= newlibadr(fd, sce->id.lib, sce->set);
2605 sce->ima= newlibadr_us(fd, sce->id.lib, sce->ima);
2607 base= sce->base.first;
2611 /* base->object= newlibadr_us(fd, sce->id.lib, base->object); */
2612 base->object= newlibadr_us(fd, sce->id.lib, base->object);
2614 /* when save during radiotool, needs cleared */
2615 base->flag &= ~OB_RADIO;
2617 if(base->object==NULL) {
2618 printf("LIB ERROR: base removed\n");
2619 BLI_remlink(&sce->base, base);
2620 if(base==sce->basact) sce->basact= 0;
2628 WHILE_SEQ(&ed->seqbase) {
2629 if(seq->ipo) seq->ipo= newlibadr_us(fd, sce->id.lib, seq->ipo);
2630 if(seq->scene) seq->scene= newlibadr(fd, sce->id.lib, seq->scene);
2632 seq->sound= newlibadr(fd, sce->id.lib, seq->sound);
2634 seq->sound->id.us++;
2635 seq->sound->flags |= SOUND_FLAGS_SEQUENCE;
2643 lib_link_scriptlink(fd, &sce->id, &sce->scriptlink);
2646 lib_link_ntree(fd, &sce->id, sce->nodetree);
2648 sce->id.flag -= LIB_NEEDLINK;
2655 static void link_recurs_seq(FileData *fd, ListBase *lb)
2662 if(seq->seqbase.first) link_recurs_seq(fd, &seq->seqbase);
2667 static void direct_link_scene(FileData *fd, Scene *sce)
2676 sce->dagisvalid = 0;
2678 link_list(fd, &(sce->base));
2680 sce->basact= newdataadr(fd, sce->basact);
2682 sce->radio= newdataadr(fd, sce->radio);
2684 sce->toolsettings= newdataadr(fd, sce->toolsettings);
2687 ed= sce->ed= newdataadr(fd, sce->ed);
2689 /* recursive link sequences, lb will be correctly initialized */
2690 link_recurs_seq(fd, &ed->seqbase);
2692 WHILE_SEQ(&ed->seqbase) {
2693 seq->seq1= newdataadr(fd, seq->seq1);
2694 seq->seq2= newdataadr(fd, seq->seq2);
2695 seq->seq3= newdataadr(fd, seq->seq3);
2696 /* a patch: after introduction of effects with 3 input strips */
2697 if(seq->seq3==0) seq->seq3= seq->seq2;
2701 seq->plugin= newdataadr(fd, seq->plugin);
2702 if(seq->plugin) open_plugin_seq(seq->plugin, seq->name+2);
2704 seq->effectdata= newdataadr(fd, seq->effectdata);
2706 seq->strip= newdataadr(fd, seq->strip);
2707 if(seq->strip && seq->strip->done==0) {
2708 seq->strip->done= 1;
2710 /* standard: strips from effects/metas are not written, but are mallocced */
2712 if(seq->type==SEQ_IMAGE) {
2713 seq->strip->stripdata= newdataadr(fd, seq->strip->stripdata);
2714 se= seq->strip->stripdata;
2716 for(a=0; a<seq->strip->len; a++, se++) {
2722 else if(seq->type==SEQ_MOVIE) {
2723 /* only first stripelem is in file */
2724 se= newdataadr(fd, seq->strip->stripdata);
2727 seq->strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
2728 *seq->strip->stripdata= *se;
2731 se= seq->strip->stripdata;
2733 for(a=0; a<seq->strip->len; a++, se++) {
2740 else if(seq->type==SEQ_SOUND) {
2741 /* only first stripelem is in file */
2742 se= newdataadr(fd, seq->strip->stripdata);
2745 seq->strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
2746 *seq->strip->stripdata= *se;
2749 se= seq->strip->stripdata;
2751 for(a=0; a<seq->strip->len; a++, se++) {
2752 se->ok= 2; /* why? */
2759 seq->strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
2765 /* link metastack, slight abuse of structs here, have to restore pointer to internal part in struct */
2770 /*int seted=0;*/ /*unused*/
2772 offset= ((long)&(temp.seqbase)) - ((long)&temp);
2775 poin= (char *)ed->seqbasep;
2777 poin= newdataadr(fd, poin);
2778 if(poin) ed->seqbasep= (ListBase *)(poin+offset);
2779 else ed->seqbasep= &ed->seqbase;
2782 link_list(fd, &(ed->metastack));
2784 for(ms= ed->metastack.first; ms; ms= ms->next) {
2785 ms->parseq= newdataadr(fd, ms->parseq);
2787 poin= (char *)ms->oldbasep;
2789 poin= newdataadr(fd, poin);
2790 if(poin) ms->oldbasep= (ListBase *)(poin+offset);
2791 else ms->oldbasep= &ed->seqbase;
2796 direct_link_scriptlink(fd, &sce->scriptlink);
2798 sce->r.avicodecdata = newdataadr(fd, sce->r.avicodecdata);
2799 if (sce->r.avicodecdata) {
2800 sce->r.avicodecdata->lpFormat = newdataadr(fd, sce->r.avicodecdata->lpFormat);
2801 sce->r.avicodecdata->lpParms = newdataadr(fd, sce->r.avicodecdata->lpParms);
2804 sce->r.qtcodecdata = newdataadr(fd, sce->r.qtcodecdata);
2805 if (sce->r.qtcodecdata) {
2806 sce->r.qtcodecdata->cdParms = newdataadr(fd, sce->r.qtcodecdata->cdParms);
2809 link_list(fd, &(sce->markers));
2811 sce->nodetree= newdataadr(fd, sce->nodetree);
2813 direct_link_nodetree(fd, sce->nodetree);
2817 /* ************ READ SCREEN ***************** */
2819 /* note: file read without screens option G_FILE_NO_UI;
2820 check lib pointers in call below */
2821 static void lib_link_screen(FileData *fd, Main *main)
2826 sc= main->screen.first;
2828 if(sc->id.flag & LIB_NEEDLINK) {
2830 sc->scene= newlibadr(fd, sc->id.lib, sc->scene);
2832 sa= sc->areabase.first;
2836 sa->full= newlibadr(fd, sc->id.lib, sa->full);
2838 /* space handler scriptlinks */
2839 lib_link_scriptlink(fd, &sc->id, &sa->scriptlink);
2841 for (sl= sa->spacedata.first; sl; sl= sl->next) {
2842 if(sl->spacetype==SPACE_VIEW3D) {
2843 View3D *v3d= (View3D*) sl;
2845 v3d->camera= newlibadr(fd, sc->id.lib, v3d->camera);
2848 v3d->bgpic->ima= newlibadr_us(fd, sc->id.lib, v3d->bgpic->ima);
2849 v3d->bgpic->tex= newlibadr_us(fd, sc->id.lib, v3d->bgpic->tex);
2850 v3d->bgpic->rect= NULL;
2853 v3d->localvd->camera= newlibadr(fd, sc->id.lib, v3d->localvd->camera);
2857 else if(sl->spacetype==SPACE_IPO) {
2858 SpaceIpo *sipo= (SpaceIpo *)sl;
2861 if(sipo->blocktype==ID_SEQ) sipo->from= NULL; // no libdata
2862 else sipo->from= newlibadr(fd, sc->id.lib, sipo->from);
2864 sipo->ipokey.first= sipo->ipokey.last= 0;
2865 sipo->ipo= newlibadr(fd, sc->id.lib, sipo->ipo);
2867 else if(sl->spacetype==SPACE_BUTS) {
2868 SpaceButs *sbuts= (SpaceButs *)sl;
2869 sbuts->lockpoin= NULL;
2871 if(main->versionfile<132) set_rects_butspace(sbuts);
2873 else if(sl->spacetype==SPACE_FILE) {
2874 SpaceFile *sfile= (SpaceFile *)sl;
2877 sfile->libfiledata= 0;
2878 sfile->returnfunc= 0;
2880 else if(sl->spacetype==SPACE_IMASEL) {
2881 check_imasel_copy((SpaceImaSel *)sl);
2883 else if(sl->spacetype==SPACE_ACTION) {
2884 SpaceAction *saction= (SpaceAction *)sl;
2885 saction->action = newlibadr(fd, sc->id.lib, saction->action);
2887 else if(sl->spacetype==SPACE_IMAGE) {
2888 SpaceImage *sima= (SpaceImage *)sl;
2890 sima->image= newlibadr_us(fd, sc->id.lib, sima->image);
2892 else if(sl->spacetype==SPACE_NLA){
2893 /* SpaceNla *snla= (SpaceNla *)sl; */
2895 else if(sl->spacetype==SPACE_TEXT) {
2896 SpaceText *st= (SpaceText *)sl;
2898 st->text= newlibadr(fd, sc->id.lib, st->text);
2901 else if(sl->spacetype==SPACE_SCRIPT) {
2902 SpaceScript *sc= (SpaceScript *)sl;
2906 else if(sl->spacetype==SPACE_OOPS) {
2907 SpaceOops *so= (SpaceOops *)sl;
2909 TreeStoreElem *tselem;
2912 oops= so->oops.first;
2914 oops->id= newlibadr(fd, NULL, oops->id);
2918 so->tree.first= so->tree.last= NULL;
2921 tselem= so->treestore->data;
2922 for(a=0; a<so->treestore->usedelem; a++, tselem++) {
2923 tselem->id= newlibadr(fd, NULL, tselem->id);
2927 else if(sl->spacetype==SPACE_SOUND) {
2928 SpaceSound *ssound= (SpaceSound *)sl;
2930 ssound->sound= newlibadr_us(fd, sc->id.lib, ssound->sound);
2935 sc->id.flag -= LIB_NEEDLINK;
2941 static void *restore_pointer_by_name(Main *mainp, ID *id, int user)
2947 lb= wich_libbase(mainp, GS(id->name));
2949 if(lb) { // there's still risk of checking corrupt mem (freed Ids in oops)
2952 if( strcmp(idn->name, id->name)==0) {
2953 if(user && idn->us==0) idn->us++;
2963 /* called from kernel/blender.c */
2964 /* used to link a file (without UI) to the current UI */
2965 void lib_link_screen_restore(Main *newmain, Scene *curscene)
2970 sc= newmain->screen.first;
2973 sc->scene= curscene;
2975 sa= sc->areabase.first;
2979 if (sa->scriptlink.totscript) {
2980 /* restore screen area script links */
2981 ScriptLink *slink = &sa->scriptlink;
2983 for (script_idx = 0; script_idx < slink->totscript; script_idx++) {
2984 slink->scripts[script_idx] = restore_pointer_by_name(newmain,
2985 (ID *)slink->scripts[script_idx], 1);
2989 for (sl= sa->spacedata.first; sl; sl= sl->next) {
2990 if(sl->spacetype==SPACE_VIEW3D) {
2991 View3D *v3d= (View3D*) sl;
2993 v3d->camera= sc->scene->camera;
2996 v3d->bgpic->ima= restore_pointer_by_name(newmain, (ID *)v3d->bgpic->ima, 1);
2997 v3d->bgpic->tex= restore_pointer_by_name(newmain, (ID *)v3d->bgpic->tex, 1);
2998 if(v3d->bgpic->rect) freeN(v3d->bgpic->rect);
2999 v3d->bgpic->rect= NULL;
3004 v3d->localvd->camera= sc->scene->camera;
3006 /* localview can become invalid during undo/redo steps, so we exit it when no could be found */
3007 for(base= sc->scene->base.first; base; base= base->next) {
3008 if(base->lay & v3d->lay) break;
3011 v3d->lay= v3d->localvd->lay;
3012 v3d->layact= v3d->localvd->layact;
3013 MEM_freeN(v3d->localvd);
3018 else if(v3d->scenelock) v3d->lay= sc->scene->lay;
3020 /* not very nice, but could help */
3021 if((v3d->layact & v3d->lay)==0) v3d->layact= v3d->lay;
3024 else if(sl->spacetype==SPACE_IPO) {
3025 SpaceIpo *sipo= (SpaceIpo *)sl;
3027 if(sipo->blocktype==ID_SEQ) sipo->from= NULL; // no libdata
3028 else sipo->from= restore_pointer_by_name(newmain, (ID *)sipo->from, 0);
3030 // not free sipo->ipokey, creates dependency with src/
3031 sipo->ipo= restore_pointer_by_name(newmain, (ID *)sipo->ipo, 0);
3032 if(sipo->editipo) MEM_freeN(sipo->editipo);
3033 sipo->editipo= NULL;
3035 else if(sl->spacetype==SPACE_BUTS) {
3036 SpaceButs *sbuts= (SpaceButs *)sl;
3037 sbuts->lockpoin= NULL;
3038 if (sbuts->ri) sbuts->ri->cury = 0;
3040 else if(sl->spacetype==SPACE_FILE) {
3041 SpaceFile *sfile= (SpaceFile *)sl;
3042 if(sfile->libfiledata)
3043 BLO_blendhandle_close(sfile->libfiledata);
3044 sfile->libfiledata= 0;
3046 else if(sl->spacetype==SPACE_IMASEL) {
3049 else if(sl->spacetype==SPACE_ACTION) {
3050 SpaceAction *saction= (SpaceAction *)sl;
3051 saction->action = restore_pointer_by_name(newmain, (ID *)saction->action, 1);
3053 else if(sl->spacetype==SPACE_IMAGE) {
3054 SpaceImage *sima= (SpaceImage *)sl;
3056 sima->image= restore_pointer_by_name(newmain, (ID *)sima->image, 1);
3058 else if(sl->spacetype==SPACE_NLA){
3059 /* SpaceNla *snla= (SpaceNla *)sl; */
3061 else if(sl->spacetype==SPACE_TEXT) {
3062 SpaceText *st= (SpaceText *)sl;
3064 st->text= restore_pointer_by_name(newmain, (ID *)st->text, 1);
3065 if(st->text==NULL) st->text= newmain->text.first;
3067 else if(sl->spacetype==SPACE_SCRIPT) {
3068 SpaceScript *sc= (SpaceScript *)sl;