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_controller_types.h"
63 #include "DNA_constraint_types.h"
64 #include "DNA_curve_types.h"
65 #include "DNA_effect_types.h"
66 #include "DNA_fileglobal_types.h"
67 #include "DNA_group_types.h"
68 #include "DNA_ipo_types.h"
69 #include "DNA_image_types.h"
70 #include "DNA_key_types.h"
71 #include "DNA_lattice_types.h"
72 #include "DNA_lamp_types.h"
73 #include "DNA_meta_types.h"
74 #include "DNA_material_types.h"
75 #include "DNA_mesh_types.h"
76 #include "DNA_meshdata_types.h"
77 #include "DNA_modifier_types.h"
78 #include "DNA_nla_types.h"
79 #include "DNA_object_types.h"
80 #include "DNA_object_force.h"
81 #include "DNA_object_fluidsim.h" // NT
82 #include "DNA_oops_types.h"
83 #include "DNA_object_force.h"
84 #include "DNA_packedFile_types.h"
85 #include "DNA_property_types.h"
86 #include "DNA_text_types.h"
87 #include "DNA_view3d_types.h"
88 #include "DNA_screen_types.h"
89 #include "DNA_sensor_types.h"
90 #include "DNA_sdna_types.h"
91 #include "DNA_scene_types.h"
92 #include "DNA_sequence_types.h"
93 #include "DNA_sound_types.h"
94 #include "DNA_space_types.h"
95 #include "DNA_texture_types.h"
96 #include "DNA_userdef_types.h"
97 #include "DNA_vfont_types.h"
98 #include "DNA_world_types.h"
100 #include "MEM_guardedalloc.h"
101 #include "BLI_blenlib.h"
102 #include "BLI_arithb.h"
103 #include "BLI_storage_types.h" // for relname flags
105 #include "BKE_bad_level_calls.h" // for reopen_text build_seqar (from WHILE_SEQ) open_plugin_seq set_rects_butspace check_imasel_copy
107 #include "BKE_action.h"
108 #include "BKE_armature.h"
109 #include "BKE_constraint.h"
110 #include "BKE_curve.h"
111 #include "BKE_deform.h"
112 #include "BKE_depsgraph.h"
113 #include "BKE_effect.h" // for give_parteff
114 #include "BKE_global.h" // for G
115 #include "BKE_property.h" // for get_property
116 #include "BKE_lattice.h"
117 #include "BKE_library.h" // for wich_libbase
118 #include "BKE_main.h" // for Main
119 #include "BKE_mesh.h" // for ME_ defines (patching)
120 #include "BKE_modifier.h"
121 #include "BKE_object.h"
122 #include "BKE_sca.h" // for init_actuator
123 #include "BKE_scene.h"
124 #include "BKE_softbody.h" // sbNew()
125 #include "BKE_texture.h" // for open_plugin_tex
126 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
128 #include "BIF_butspace.h" // for do_versions, patching event codes
130 #include "BLO_readfile.h"
131 #include "BLO_undofile.h"
132 #include "BLO_readblenfile.h" // streaming read pipe, for BLO_readblenfile BLO_readblenfilememory
134 #include "readfile.h"
138 #include "mydevice.h"
144 Remark: still a weak point is the newadress() function, that doesnt solve reading from
145 multiple files at the same time
147 (added remark: oh, i thought that was solved? will look at that... (ton)
150 - Existing Library (Main) push or free
160 - read associated 'direct data'
161 - link direct data (internal and to LibBlock)
163 - read USER data, only when indicated (file is ~/.B.blend)
165 - per Library (per Main)
168 - find LibBlocks and attach IDs to Main
169 - if external LibBlock
171 - or it's already read,
176 - read associated direct data
177 - link direct data (internal and to LibBlock)
179 - per Library with unread LibBlocks
184 - read associated direct data
185 - link direct data (internal and to LibBlock)
188 - link all LibBlocks and indirect pointers to libblocks
189 - initialize FileGlobal and copy pointers to Global
192 /* also occurs in library.c */
193 /* GS reads the memory pointed at in a specific ordering. There are,
194 * however two definitions for it. I have jotted them down here, both,
195 * but I think the first one is actually used. The thing is that
196 * big-endian systems might read this the wrong way round. OTOH, we
197 * constructed the IDs that are read out with this macro explicitly as
198 * well. I expect we'll sort it out soon... */
201 #define GS(a) (*((short *)(a)))
203 /* from misc_util: flip the bytes from x */
204 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
206 // only used here in readfile.c
207 #define SWITCH_LONGINT(a) { \
210 s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
211 s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
212 s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
213 s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
214 // only used here in readfile.c
215 #define SWITCH_SHORT(a) { \
218 s_i=p_i[0]; p_i[0]=p_i[1]; p_i[1]=s_i; }
223 typedef struct OldNew {
228 typedef struct OldNewMap {
230 int nentries, entriessize;
236 /* local prototypes */
237 extern short freeN(void *vmemh); /* defined in util.h */
240 static OldNewMap *oldnewmap_new(void)
242 OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
244 onm->entriessize= 1024;
245 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
250 static int verg_oldnewmap(const void *v1, const void *v2)
252 const struct OldNew *x1=v1, *x2=v2;
254 if( x1->old > x2->old) return 1;
255 else if( x1->old < x2->old) return -1;
260 static void oldnewmap_sort(FileData *fd)
262 qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
263 fd->libmap->sorted= 1;
266 /* nr is zero for data, and ID code for libdata */
267 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
271 if(oldaddr==NULL || newaddr==NULL) return;
273 if (onm->nentries==onm->entriessize) {
274 int osize= onm->entriessize;
275 OldNew *oentries= onm->entries;
277 onm->entriessize*= 2;
278 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
280 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
284 entry= &onm->entries[onm->nentries++];
286 entry->newp= newaddr;
290 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
294 if (onm->lasthit<onm->nentries-1) {
295 OldNew *entry= &onm->entries[++onm->lasthit];
297 if (entry->old==addr) {
303 for (i=0; i<onm->nentries; i++) {
304 OldNew *entry= &onm->entries[i];
306 if (entry->old==addr) {
317 /* for libdate, nr has ID code, no increment */
318 static void *oldnewmap_liblookup(OldNewMap *onm, void *addr, void *lib)
322 if(addr==NULL) return NULL;
324 /* lasthit works fine for non-libdata, linking there is done in same sequence as writing */
326 OldNew entry_s, *entry;
330 entry= bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
334 if (id && (!lib || id->lib)) {
340 for (i=0; i<onm->nentries; i++) {
341 OldNew *entry= &onm->entries[i];
343 if (entry->old==addr) {
346 if (id && (!lib || id->lib)) {
355 static void oldnewmap_free_unused(OldNewMap *onm)
359 for (i=0; i<onm->nentries; i++) {
360 OldNew *entry= &onm->entries[i];
362 MEM_freeN(entry->newp);
368 static void oldnewmap_clear(OldNewMap *onm)
374 static void oldnewmap_free(OldNewMap *onm)
376 MEM_freeN(onm->entries);
382 static void read_libraries(FileData *basefd, ListBase *mainlist);
384 /* ************ help functions ***************** */
386 static void add_main_to_main(Main *mainvar, Main *from)
388 ListBase *lbarray[100], *fromarray[100]; // define in library.c too
391 a= set_listbasepointers(mainvar, lbarray);
392 a= set_listbasepointers(from, fromarray);
394 addlisttolist(lbarray[a], fromarray[a]);
398 void blo_join_main(ListBase *mainlist)
400 Main *tojoin, *mainl= mainlist->first;
402 while ((tojoin= mainl->next)) {
403 add_main_to_main(mainl, tojoin);
404 BLI_remlink(mainlist, tojoin);
409 static void split_libdata(ListBase *lb, Main *first)
421 if(mainvar->curlib==id->lib) {
422 lbn= wich_libbase(mainvar, GS(id->name));
424 BLI_addtail(lbn, id);
427 mainvar= mainvar->next;
429 if(mainvar==0) printf("error split_libdata\n");
435 void blo_split_main(ListBase *mainlist)
437 Main *mainl= mainlist->first;
438 ListBase *lbarray[30];
442 for (lib= mainl->library.first; lib; lib= lib->id.next) {
443 Main *libmain= MEM_callocN(sizeof(Main), "libmain");
444 libmain->curlib= lib;
445 BLI_addtail(mainlist, libmain);
448 i= set_listbasepointers(mainl, lbarray);
450 split_libdata(lbarray[i], mainl->next);
453 static void cleanup_path(const char *relabase, char *name)
455 char filename[FILE_MAXFILE];
457 BLI_splitdirstring(name, filename);
458 BLI_cleanup_dir(relabase, name);
459 strcat(name, filename);
462 static Main *blo_find_main(ListBase *mainlist, char *name)
466 char name1[FILE_MAXDIR+FILE_MAXFILE];
467 char libname1[FILE_MAXDIR+FILE_MAXFILE];
469 // printf("G.sce %s\n", G.sce);
470 /* everything in absolute paths now */
473 cleanup_path(G.sce, name1);
474 // printf("original in %s\n", name);
475 // printf("converted in %s\n", name1);
477 for (m= mainlist->first; m; m= m->next) {
478 char *libname= (m->curlib)?m->curlib->name:m->name;
480 // printf("libname %s\n", libname);
481 strcpy(libname1, libname);
482 cleanup_path(G.sce, libname1);
483 // printf("libname1 %s\n", libname1, name1);
485 if (BLI_streq(name1, libname1)) {
486 printf("found library %s\n", libname);
491 m= MEM_callocN(sizeof(Main), "find_main");
492 BLI_addtail(mainlist, m);
494 lib= alloc_libblock(&m->library, ID_LI, "lib");
495 strcpy(lib->name, name);
498 printf("added new lib %s\n", name);
503 /* ************ FILE PARSING ****************** */
505 static void switch_endian_bh4(BHead4 *bhead)
507 /* the ID_.. codes */
508 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
510 if (bhead->code != ENDB) {
511 SWITCH_INT(bhead->len);
512 SWITCH_INT(bhead->SDNAnr);
513 SWITCH_INT(bhead->nr);
517 static void switch_endian_bh8(BHead8 *bhead)
519 /* the ID_.. codes */
520 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
522 if (bhead->code != ENDB) {
523 SWITCH_INT(bhead->len);
524 SWITCH_INT(bhead->SDNAnr);
525 SWITCH_INT(bhead->nr);
529 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
531 BHead4 *bhead4 = (BHead4 *) bhead;
532 #if defined(WIN32) && !defined(FREE_WINDOWS)
538 bhead4->code= bhead8->code;
539 bhead4->len= bhead8->len;
541 if (bhead4->code != ENDB) {
543 // why is this here ??
544 if (do_endian_swap) {
545 SWITCH_LONGINT(bhead8->old);
548 /* this patch is to avoid a long long being read from not-eight aligned positions
549 is necessary on SGI with -n32 compiling (no, is necessary on
550 any modern 64bit architecture) */
551 memcpy(&old, &bhead8->old, 8);
552 bhead4->old = (int) (old >> 3);
554 bhead4->SDNAnr= bhead8->SDNAnr;
555 bhead4->nr= bhead8->nr;
559 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
561 BHead8 *bhead8 = (BHead8 *) bhead;
563 bhead8->code= bhead4->code;
564 bhead8->len= bhead4->len;
566 if (bhead8->code != ENDB) {
567 bhead8->old= bhead4->old;
568 bhead8->SDNAnr= bhead4->SDNAnr;
569 bhead8->nr= bhead4->nr;
573 static BHeadN *get_bhead(FileData *fd)
578 BHeadN *new_bhead = 0;
584 // First read the bhead structure.
585 // Depending on the platform the file was written on this can
586 // be a big or little endian BHead4 or BHead8 structure.
588 // As usual 'ENDB' (the last *partial* bhead of the file)
589 // needs some special handling. We don't want to EOF just yet.
591 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
593 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
595 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
596 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
597 switch_endian_bh4(&bhead4);
600 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
601 bh8_from_bh4(&bhead, &bhead4);
603 memcpy(&bhead, &bhead4, sizeof(bhead));
610 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
612 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
613 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
614 switch_endian_bh8(&bhead8);
617 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
618 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
620 memcpy(&bhead, &bhead8, sizeof(bhead));
627 // bhead now contains the (converted) bhead structure. Now read
628 // the associated data and put everything in a BHeadN (creative naming !)
631 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
633 new_bhead->next = new_bhead->prev = 0;
634 new_bhead->bhead = bhead;
636 readsize = fd->read(fd, new_bhead + 1, bhead.len);
638 if (readsize != bhead.len) {
640 MEM_freeN(new_bhead);
649 // We've read a new block. Now add it to the list
653 BLI_addtail(&fd->listbase, new_bhead);
659 BHead *blo_firstbhead(FileData *fd)
665 // Read in a new block if necessary
667 new_bhead = fd->listbase.first;
668 if (new_bhead == 0) {
669 new_bhead = get_bhead(fd);
673 bhead = &new_bhead->bhead;
679 BHead *blo_prevbhead(FileData *fd, BHead *thisblock)
681 BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - (int) (&((BHeadN*)0)->bhead));
682 BHeadN *prev= bheadn->prev;
684 return prev?&prev->bhead:NULL;
687 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
689 BHeadN *new_bhead = 0;
693 // bhead is actually a sub part of BHeadN
694 // We calculate the BHeadN pointer from the BHead pointer below
695 new_bhead = (BHeadN *) (((char *) thisblock) - (int) (&((BHeadN*)0)->bhead));
697 // get the next BHeadN. If it doesn't exist we read in the next one
698 new_bhead = new_bhead->next;
699 if (new_bhead == 0) {
700 new_bhead = get_bhead(fd);
705 // here we do the reverse:
706 // go from the BHeadN pointer to the BHead pointer
707 bhead = &new_bhead->bhead;
713 static void decode_blender_header(FileData *fd)
715 char header[SIZEOFBLENDERHEADER], num[4];
718 // read in the header data
719 readsize = fd->read(fd, header, sizeof(header));
721 if (readsize == sizeof(header)) {
722 if(strncmp(header, "BLENDER", 7) == 0) {
723 int remove_this_endian_test= 1;
725 fd->flags |= FD_FLAGS_FILE_OK;
727 // what size are pointers in the file ?
729 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
730 if (sizeof(void *) != 4) {
731 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
734 if (sizeof(void *) != 8) {
735 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
739 // is the file saved in a different endian
741 if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
742 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
745 // get the version number
747 memcpy(num, header+9, 3);
749 fd->fileversion = atoi(num);
754 static int read_file_dna(FileData *fd)
758 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
759 if (bhead->code==DNA1) {
760 int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
762 fd->filesdna= dna_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
764 fd->compflags= dna_get_structDNA_compareflags(fd->filesdna, fd->memsdna);
767 } else if (bhead->code==ENDB)
773 static int fd_read_from_file(FileData *filedata, void *buffer, int size)
775 int readsize = read(filedata->filedes, buffer, size);
780 filedata->seek += readsize;
786 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, int size)
788 int readsize = gzread(filedata->gzfiledes, buffer, size);
793 filedata->seek += readsize;
799 static int fd_read_from_memory(FileData *filedata, void *buffer, int size)
801 // don't read more bytes then there are available in the buffer
802 int readsize = MIN2(size, filedata->buffersize - filedata->seek);
804 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
805 filedata->seek += readsize;
810 static int fd_read_from_memfile(FileData *filedata, void *buffer, int size)
812 static unsigned int seek= 1<<30; /* the current position */
813 static unsigned int offset= 0; /* size of previous chunks */
814 static MemFileChunk *chunk=NULL;
816 if(size==0) return 0;
818 if(seek != filedata->seek) {
819 chunk= filedata->memfile->chunks.first;
823 if(seek + chunk->size > (unsigned) filedata->seek) break;
828 seek= filedata->seek;
832 /* first check if it's on the end if current chunk */
833 if( seek-offset == chunk->size) {
834 offset+= chunk->size;
838 /* debug, should never happen */
840 printf("illegal read, chunk zero\n");
843 else if( (seek-offset)+size > chunk->size) {
844 size= chunk->size - (seek-offset);
845 printf("chunk too large, clipped to %d\n", size);
848 memcpy(buffer, chunk->buf + (seek-offset), size);
849 filedata->seek += size;
858 static FileData *filedata_new(void)
860 extern char DNAstr[]; /* DNA.c */
862 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
865 fd->gzfiledes = NULL;
867 /* XXX, this doesn't need to be done all the time,
868 * but it keeps us reentrant, remove once we have
869 * a lib that provides a nice lock. - zr
871 fd->memsdna = dna_sdna_from_data(DNAstr, DNAlen, 0);
873 fd->datamap = oldnewmap_new();
874 fd->globmap = oldnewmap_new();
875 fd->libmap = oldnewmap_new();
880 static FileData *blo_decode_and_check(FileData *fd, BlendReadError *error_r)
882 decode_blender_header(fd);
884 if (fd->flags & FD_FLAGS_FILE_OK) {
885 if (!read_file_dna(fd)) {
886 *error_r = BRE_INCOMPLETE;
887 blo_freefiledata(fd);
891 *error_r = BRE_NOT_A_BLEND;
892 blo_freefiledata(fd);
899 FileData *blo_openblenderfile(char *name, BlendReadError *error_r)
902 char name1[FILE_MAXDIR+FILE_MAXFILE];
904 /* relative path rescue... */
906 int len= strlen(name);
907 printf("old %s\n", name);
909 if(name[len]=='/' && name[len-1]=='/') {
910 strcpy(name, name+len-1);
911 printf("new %s\n", name);
917 /* library files can have stringcodes */
919 if(name[0]=='/' && name[1]=='/')
920 BLI_convertstringcode(name1, G.sce, 0);
922 strcpy(G.sce, name); // global... is set in blender.c setup_app_data too. should be part of Main immediate?
924 gzfile= gzopen(name1, "rb");
926 if (NULL == gzfile) {
927 *error_r = BRE_UNABLE_TO_OPEN;
930 FileData *fd = filedata_new();
931 fd->gzfiledes = gzfile;
932 BLI_strncpy(fd->filename, name, sizeof(fd->filename)); // now only in use by library append
933 fd->read = fd_read_gzip_from_file;
935 return blo_decode_and_check(fd, error_r);
939 FileData *blo_openblendermemory(void *mem, int memsize, BlendReadError *error_r)
941 if (!mem || memsize<SIZEOFBLENDERHEADER) {
942 *error_r = mem?BRE_UNABLE_TO_READ:BRE_UNABLE_TO_OPEN;
945 FileData *fd= filedata_new();
947 fd->buffersize= memsize;
948 fd->read= fd_read_from_memory;
949 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
951 return blo_decode_and_check(fd, error_r);
955 FileData *blo_openblendermemfile(MemFile *memfile, BlendReadError *error_r)
958 *error_r = BRE_UNABLE_TO_OPEN;
961 FileData *fd= filedata_new();
962 fd->memfile= memfile;
964 fd->read= fd_read_from_memfile;
965 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
967 return blo_decode_and_check(fd, error_r);
972 void blo_freefiledata(FileData *fd)
976 if (fd->filedes != -1) {
980 if (fd->gzfiledes != NULL)
982 gzclose(fd->gzfiledes);
985 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
986 MEM_freeN(fd->buffer);
990 // Free all BHeadN data blocks
991 BLI_freelistN(&fd->listbase);
994 dna_freestructDNA(fd->memsdna);
996 dna_freestructDNA(fd->filesdna);
998 MEM_freeN(fd->compflags);
1001 oldnewmap_free(fd->datamap);
1003 oldnewmap_free(fd->globmap);
1004 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
1005 oldnewmap_free(fd->libmap);
1011 /* ************ DIV ****************** */
1013 int BLO_has_bfile_extension(char *str)
1015 return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend")||BLI_testextensie(str, ".blend.gz"));
1018 /* ************** OLD POINTERS ******************* */
1020 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1022 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1025 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1027 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1030 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1032 return oldnewmap_liblookup(fd->libmap, adr, lib);
1035 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1037 ID *id= newlibadr(fd, lib, adr);
1046 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1050 for (i=0; i<fd->libmap->nentries; i++) {
1051 OldNew *entry= &fd->libmap->entries[i];
1053 if (old==entry->newp && entry->nr==ID_ID) {
1055 if(new) entry->nr= GS( ((ID *)new)->name );
1061 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1065 for(main= mainlist->first; main; main= main->next) {
1068 if(main->curlib) fd= main->curlib->filedata;
1072 change_idid_adr_fd(fd, old, new);
1077 /* ********** END OLD POINTERS ****************** */
1078 /* ********** READ FILE ****************** */
1080 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1082 int blocksize, nblocks;
1085 data= (char *)(bhead+1); /* BHEAD+DATA dependancy */
1086 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1090 dna_switch_endian_struct(filesdna, bhead->SDNAnr, data);
1096 static void *read_struct(FileData *fd, BHead *bh, char *blockname)
1101 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1102 switch_endian_structs(fd->filesdna, bh);
1104 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1105 if(fd->compflags[bh->SDNAnr]==2) {
1106 temp= dna_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1108 temp= MEM_mallocN(bh->len, blockname);
1109 memcpy(temp, (bh+1), bh->len); /* BHEAD+DATA dependancy */
1117 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1121 if(lb->first==0) return;
1123 lb->first= newdataadr(fd, lb->first);
1127 ln->next= newdataadr(fd, ln->next);
1135 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1140 if(lb->first==0) return;
1141 poin= newdataadr(fd, lb->first);
1143 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1150 poin= newdataadr(fd, ln->next);
1152 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1162 static void test_pointer_array(FileData *fd, void **mat)
1164 #if defined(WIN32) && !defined(FREE_WINDOWS)
1165 __int64 *lpoin, *lmat;
1167 long long *lpoin, *lmat;
1169 int len, *ipoin, *imat;
1171 /* manually convert the pointer array in
1172 * the old dna format to a pointer array in
1173 * the new dna format.
1176 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1178 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1179 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1183 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1184 SWITCH_LONGINT(*lpoin);
1185 *ipoin= (int) ((*lpoin) >> 3);
1193 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1194 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1208 /* ************ READ PACKEDFILE *************** */
1210 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1212 PackedFile *pf= newdataadr(fd, oldpf);
1215 pf->data= newdataadr(fd, pf->data);
1221 /* ************ READ SCRIPTLINK *************** */
1223 static void lib_link_scriptlink(FileData *fd, ID *id, ScriptLink *slink)
1227 for(i=0; i<slink->totscript; i++) {
1228 slink->scripts[i]= newlibadr(fd, id->lib, slink->scripts[i]);
1232 static void direct_link_scriptlink(FileData *fd, ScriptLink *slink)
1234 slink->scripts= newdataadr(fd, slink->scripts);
1235 test_pointer_array(fd, (void **)&slink->scripts);
1237 slink->flag= newdataadr(fd, slink->flag);
1239 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1242 for(a=0; a<slink->totscript; a++) {
1243 SWITCH_SHORT(slink->flag[a]);
1248 /* ************ READ ARMATURE ***************** */
1250 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1252 bActionStrip *strip;
1254 for (strip=striplist->first; strip; strip=strip->next){
1255 strip->object = newlibadr(fd, id->lib, strip->object);
1256 strip->act = newlibadr_us(fd, id->lib, strip->act);
1257 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1261 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1263 bConstraintChannel *chan;
1265 for (chan=chanbase->first; chan; chan=chan->next){
1266 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1270 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
1274 for (con = conlist->first; con; con=con->next) {
1275 /* patch for error introduced by changing constraints (dunno how) */
1276 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
1277 if(con->data==NULL) {
1278 con->type= CONSTRAINT_TYPE_NULL;
1281 switch (con->type) {
1282 case CONSTRAINT_TYPE_ACTION:
1284 bActionConstraint *data;
1285 data= ((bActionConstraint*)con->data);
1286 data->tar = newlibadr(fd, id->lib, data->tar);
1287 data->act = newlibadr(fd, id->lib, data->act);
1290 case CONSTRAINT_TYPE_LOCLIKE:
1292 bLocateLikeConstraint *data;
1293 data= ((bLocateLikeConstraint*)con->data);
1294 data->tar = newlibadr(fd, id->lib, data->tar);
1297 case CONSTRAINT_TYPE_ROTLIKE:
1299 bRotateLikeConstraint *data;
1300 data= ((bRotateLikeConstraint*)con->data);
1301 data->tar = newlibadr(fd, id->lib, data->tar);
1304 case CONSTRAINT_TYPE_KINEMATIC:
1306 bKinematicConstraint *data;
1307 data = ((bKinematicConstraint*)con->data);
1308 data->tar = newlibadr(fd, id->lib, data->tar);
1311 case CONSTRAINT_TYPE_TRACKTO:
1313 bTrackToConstraint *data;
1314 data = ((bTrackToConstraint*)con->data);
1315 data->tar = newlibadr(fd, id->lib, data->tar);
1318 case CONSTRAINT_TYPE_MINMAX:
1320 bMinMaxConstraint *data;
1321 data = ((bMinMaxConstraint*)con->data);
1322 data->tar = newlibadr(fd, id->lib, data->tar);
1325 case CONSTRAINT_TYPE_LOCKTRACK:
1327 bLockTrackConstraint *data;
1328 data= ((bLockTrackConstraint*)con->data);
1329 data->tar = newlibadr(fd, id->lib, data->tar);
1332 case CONSTRAINT_TYPE_FOLLOWPATH:
1334 bFollowPathConstraint *data;
1335 data= ((bFollowPathConstraint*)con->data);
1336 data->tar = newlibadr(fd, id->lib, data->tar);
1339 case CONSTRAINT_TYPE_DISTANCELIMIT:
1341 bDistanceLimitConstraint *data;
1342 data= ((bDistanceLimitConstraint*)con->data);
1343 data->tar = newlibadr(fd, id->lib, data->tar);
1346 case CONSTRAINT_TYPE_STRETCHTO:
1348 bStretchToConstraint *data;
1349 data= ((bStretchToConstraint*)con->data);
1350 data->tar = newlibadr(fd, id->lib, data->tar);
1354 case CONSTRAINT_TYPE_NULL:
1360 static void direct_link_constraints(FileData *fd, ListBase *lb)
1365 for (cons=lb->first; cons; cons=cons->next) {
1366 cons->data = newdataadr(fd, cons->data);
1370 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
1372 bPoseChannel *pchan;
1373 bArmature *arm= ob->data;
1378 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
1379 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
1380 // hurms... loop in a loop, but yah... later... (ton)
1381 pchan->bone= get_named_bone(arm, pchan->name);
1382 pchan->custom= newlibadr(fd, arm->id.lib, pchan->custom);
1383 if(pchan->bone==NULL)
1387 ob->recalc= OB_RECALC;
1388 pose->flag |= POSE_RECALC;
1392 static void lib_link_armature(FileData *fd, Main *main)
1396 arm= main->armature.first;
1399 if(arm->id.flag & LIB_NEEDLINK) {
1400 arm->id.flag -= LIB_NEEDLINK;
1406 static void lib_link_action(FileData *fd, Main *main)
1409 bActionChannel *chan;
1411 act= main->action.first;
1413 if(act->id.flag & LIB_NEEDLINK) {
1414 act->id.flag -= LIB_NEEDLINK;
1416 for (chan=act->chanbase.first; chan; chan=chan->next) {
1417 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1418 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1426 static void direct_link_bones(FileData *fd, Bone* bone)
1430 bone->parent= newdataadr(fd, bone->parent);
1432 link_list(fd, &bone->childbase);
1434 for (child=bone->childbase.first; child; child=child->next) {
1435 direct_link_bones(fd, child);
1440 static void direct_link_action(FileData *fd, bAction *act)
1442 bActionChannel *achan;
1444 link_list(fd, &act->chanbase);
1446 for (achan = act->chanbase.first; achan; achan=achan->next)
1447 link_list(fd, &achan->constraintChannels);
1451 static void direct_link_armature(FileData *fd, bArmature *arm)
1455 link_list(fd, &arm->bonebase);
1457 bone=arm->bonebase.first;
1459 direct_link_bones(fd, bone);
1464 /* ************ READ CAMERA ***************** */
1466 static void lib_link_camera(FileData *fd, Main *main)
1470 ca= main->camera.first;
1472 if(ca->id.flag & LIB_NEEDLINK) {
1474 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo);
1476 lib_link_scriptlink(fd, &ca->id, &ca->scriptlink);
1478 ca->id.flag -= LIB_NEEDLINK;
1484 static void direct_link_camera(FileData *fd, Camera *ca)
1486 direct_link_scriptlink(fd, &ca->scriptlink);
1490 /* ************ READ LATTICE ***************** */
1492 static void lib_link_latt(FileData *fd, Main *main)
1496 lt= main->latt.first;
1498 if(lt->id.flag & LIB_NEEDLINK) {
1500 lt->ipo= newlibadr_us(fd, lt->id.lib, lt->ipo);
1501 lt->key= newlibadr_us(fd, lt->id.lib, lt->key);
1503 lt->id.flag -= LIB_NEEDLINK;
1509 static void direct_link_latt(FileData *fd, Lattice *lt)
1511 lt->def= newdataadr(fd, lt->def);
1514 /* ************ READ LAMP ***************** */
1516 static void lib_link_lamp(FileData *fd, Main *main)
1522 la= main->lamp.first;
1524 if(la->id.flag & LIB_NEEDLINK) {
1526 for(a=0; a<MAX_MTEX; a++) {
1529 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
1530 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
1534 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo);
1536 lib_link_scriptlink(fd, &la->id, &la->scriptlink);
1538 la->id.flag -= LIB_NEEDLINK;
1544 static void direct_link_lamp(FileData *fd, Lamp *la)
1548 direct_link_scriptlink(fd, &la->scriptlink);
1550 for(a=0; a<MAX_MTEX; a++) {
1551 la->mtex[a]= newdataadr(fd, la->mtex[a]);
1555 /* ************ READ keys ***************** */
1557 static void lib_link_key(FileData *fd, Main *main)
1561 key= main->key.first;
1563 if(key->id.flag & LIB_NEEDLINK) {
1565 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo);
1566 key->from= newlibadr(fd, key->id.lib, key->from);
1568 key->id.flag -= LIB_NEEDLINK;
1574 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
1577 char *data, *poin, *cp;
1579 elemsize= key->elemsize;
1582 for(a=0; a<kb->totelem; a++) {
1587 while( cp[0] ) { /* cp[0]==amount */
1589 switch(cp[1]) { /* cp[1]= type */
1595 SWITCH_INT((*poin));
1608 static void direct_link_key(FileData *fd, Key *key)
1612 link_list(fd, &(key->block));
1614 key->refkey= newdataadr(fd, key->refkey);
1616 kb= key->block.first;
1619 kb->data= newdataadr(fd, kb->data);
1621 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
1622 switch_endian_keyblock(key, kb);
1628 /* ************ READ mball ***************** */
1630 static void lib_link_mball(FileData *fd, Main *main)
1635 mb= main->mball.first;
1637 if(mb->id.flag & LIB_NEEDLINK) {
1639 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
1641 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo);
1643 mb->id.flag -= LIB_NEEDLINK;
1649 static void direct_link_mball(FileData *fd, MetaBall *mb)
1651 mb->mat= newdataadr(fd, mb->mat);
1652 test_pointer_array(fd, (void **)&mb->mat);
1654 link_list(fd, &(mb->elems));
1656 mb->disp.first= mb->disp.last= 0;
1661 /* ************ READ WORLD ***************** */
1663 static void lib_link_world(FileData *fd, Main *main)
1669 wrld= main->world.first;
1671 if(wrld->id.flag & LIB_NEEDLINK) {
1673 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo);
1675 for(a=0; a<MAX_MTEX; a++) {
1676 mtex= wrld->mtex[a];
1678 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
1679 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
1683 lib_link_scriptlink(fd, &wrld->id, &wrld->scriptlink);
1685 wrld->id.flag -= LIB_NEEDLINK;
1687 wrld= wrld->id.next;
1691 static void direct_link_world(FileData *fd, World *wrld)
1695 direct_link_scriptlink(fd, &wrld->scriptlink);
1697 for(a=0; a<MAX_MTEX; a++) {
1698 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
1703 /* ************ READ IPO ***************** */
1705 static void lib_link_ipo(FileData *fd, Main *main)
1709 ipo= main->ipo.first;
1711 if(ipo->id.flag & LIB_NEEDLINK) {
1713 for(icu= ipo->curve.first; icu; icu= icu->next) {
1715 icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
1717 ipo->id.flag -= LIB_NEEDLINK;
1723 static void direct_link_ipo(FileData *fd, Ipo *ipo)
1727 link_list(fd, &(ipo->curve));
1728 icu= ipo->curve.first;
1730 icu->bezt= newdataadr(fd, icu->bezt);
1731 icu->bp= newdataadr(fd, icu->bp);
1732 icu->driver= newdataadr(fd, icu->driver);
1737 /* ************ READ VFONT ***************** */
1739 static void lib_link_vfont(FileData *fd, Main *main)
1743 vf= main->vfont.first;
1745 if(vf->id.flag & LIB_NEEDLINK) {
1746 vf->id.flag -= LIB_NEEDLINK;
1752 static void direct_link_vfont(FileData *fd, VFont *vf)
1755 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
1758 /* ************ READ TEXT ****************** */
1760 static void lib_link_text(FileData *fd, Main *main)
1764 text= main->text.first;
1766 if(text->id.flag & LIB_NEEDLINK) {
1767 text->id.flag -= LIB_NEEDLINK;
1769 text= text->id.next;
1773 static void direct_link_text(FileData *fd, Text *text)
1777 text->name= newdataadr(fd, text->name);
1780 text->undo_len= TXT_INIT_UNDO;
1781 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
1783 text->compiled= NULL;
1786 if(text->flags & TXT_ISEXT) {
1791 link_list(fd, &text->lines);
1793 text->curl= newdataadr(fd, text->curl);
1794 text->sell= newdataadr(fd, text->sell);
1796 ln= text->lines.first;
1798 ln->line= newdataadr(fd, ln->line);
1801 if (ln->len != (int) strlen(ln->line)) {
1802 printf("Error loading text, line lengths differ\n");
1803 ln->len = strlen(ln->line);
1809 text->flags = (text->flags|TXT_ISTMP) & ~TXT_ISEXT;
1814 /* ************ READ IMAGE ***************** */
1816 static void lib_link_image(FileData *fd, Main *main)
1820 ima= main->image.first;
1822 if(ima->id.flag & LIB_NEEDLINK) {
1824 ima->id.flag -= LIB_NEEDLINK;
1830 static void direct_link_image(FileData *fd, Image *ima)
1834 memset(ima->mipmap, 0, sizeof(ima->mipmap));
1838 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
1844 /* ************ READ CURVE ***************** */
1846 static void lib_link_curve(FileData *fd, Main *main)
1851 cu= main->curve.first;
1853 if(cu->id.flag & LIB_NEEDLINK) {
1855 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
1857 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
1858 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
1859 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
1860 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
1861 cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);
1862 cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
1863 cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
1865 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo);
1866 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
1868 cu->id.flag -= LIB_NEEDLINK;
1875 static void switch_endian_knots(Nurb *nu)
1882 SWITCH_INT(nu->knotsu[len]);
1888 SWITCH_INT(nu->knotsv[len]);
1893 static void direct_link_curve(FileData *fd, Curve *cu)
1898 cu->mat= newdataadr(fd, cu->mat);
1899 test_pointer_array(fd, (void **)&cu->mat);
1900 cu->str= newdataadr(fd, cu->str);
1901 cu->strinfo= newdataadr(fd, cu->strinfo);
1902 cu->tb= newdataadr(fd, cu->tb);
1904 if(cu->vfont==0) link_list(fd, &(cu->nurb));
1906 cu->nurb.first=cu->nurb.last= 0;
1908 tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
1910 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
1917 cu->tb[0].w = cu->linewidth;
1919 if (cu->wordspace == 0.0) cu->wordspace = 1.0;
1922 cu->bev.first=cu->bev.last= 0;
1923 cu->disp.first=cu->disp.last= 0;
1928 nu->bezt= newdataadr(fd, nu->bezt);
1929 nu->bp= newdataadr(fd, nu->bp);
1930 nu->knotsu= newdataadr(fd, nu->knotsu);
1931 nu->knotsv= newdataadr(fd, nu->knotsv);
1932 if (cu->vfont==0) nu->charidx= nu->mat_nr;
1934 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1935 switch_endian_knots(nu);
1943 /* ************ READ TEX ***************** */
1945 static void lib_link_texture(FileData *fd, Main *main)
1949 tex= main->tex.first;
1951 if(tex->id.flag & LIB_NEEDLINK) {
1953 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
1954 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
1955 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
1957 tex->id.flag -= LIB_NEEDLINK;
1963 static void direct_link_texture(FileData *fd, Tex *tex)
1965 tex->plugin= newdataadr(fd, tex->plugin);
1967 tex->plugin->handle= 0;
1968 open_plugin_tex(tex->plugin);
1970 tex->coba= newdataadr(fd, tex->coba);
1971 tex->env= newdataadr(fd, tex->env);
1974 memset(tex->env->cube, 0, 6*sizeof(void *));
1981 /* ************ READ MATERIAL ***************** */
1983 static void lib_link_material(FileData *fd, Main *main)
1990 ma= main->mat.first;
1992 if(ma->id.flag & LIB_NEEDLINK) {
1994 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
1995 ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
1997 for(a=0; a<MAX_MTEX; a++) {
2000 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
2001 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
2004 lib_link_scriptlink(fd, &ma->id, &ma->scriptlink);
2006 for (ml=ma->layers.first; ml; ml=ml->next)
2007 ml->mat= newlibadr_us(fd, ma->id.lib, ml->mat);
2009 ma->id.flag -= LIB_NEEDLINK;
2015 static void direct_link_material(FileData *fd, Material *ma)
2019 for(a=0; a<MAX_MTEX; a++) {
2020 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
2023 ma->ramp_col= newdataadr(fd, ma->ramp_col);
2024 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
2026 direct_link_scriptlink(fd, &ma->scriptlink);
2028 link_list(fd, &ma->layers);
2032 /* ************ READ MESH ***************** */
2034 static void lib_link_mesh(FileData *fd, Main *main)
2038 me= main->mesh.first;
2040 if(me->id.flag & LIB_NEEDLINK) {
2043 /* this check added for python created meshes */
2045 for(i=0; i<me->totcol; i++) {
2046 me->mat[i]= newlibadr_us(fd, me->id.lib, me->mat[i]);
2051 me->ipo= newlibadr_us(fd, me->id.lib, me->ipo);
2052 me->key= newlibadr_us(fd, me->id.lib, me->key);
2053 me->texcomesh= newlibadr_us(fd, me->id.lib, me->texcomesh);
2056 TFace *tfaces= me->tface;
2058 for (i=0; i<me->totface; i++) {
2059 TFace *tf= &tfaces[i];
2061 tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
2063 Image *ima= tf->tpage;
2069 me->id.flag -= LIB_NEEDLINK;
2075 static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
2082 for (i=0; i<count; i++) {
2083 mdverts[i].dw=newdataadr(fd, mdverts[i].dw);
2085 mdverts[i].totweight=0;
2089 static void direct_link_mesh(FileData *fd, Mesh *mesh)
2091 mesh->mat= newdataadr(fd, mesh->mat);
2092 test_pointer_array(fd, (void **)&mesh->mat);
2094 mesh->mvert= newdataadr(fd, mesh->mvert);
2095 mesh->medge= newdataadr(fd, mesh->medge);
2096 mesh->mface= newdataadr(fd, mesh->mface);
2097 mesh->tface= newdataadr(fd, mesh->tface);
2098 mesh->mcol= newdataadr(fd, mesh->mcol);
2099 mesh->msticky= newdataadr(fd, mesh->msticky);
2101 mesh->dvert= newdataadr(fd, mesh->dvert);
2102 direct_link_dverts(fd, mesh->totvert, mesh->dvert);
2109 TFace *tfaces= mesh->tface;
2112 for (i=0; i<mesh->totface; i++) {
2113 TFace *tf= &tfaces[i];
2115 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2116 SWITCH_INT(tf->col[0]);
2117 SWITCH_INT(tf->col[1]);
2118 SWITCH_INT(tf->col[2]);
2119 SWITCH_INT(tf->col[3]);
2125 /* ************ READ OBJECT ***************** */
2127 static void lib_link_modifiers__linkModifiers(void *userData, Object *ob, Object **obpoin)
2129 FileData *fd = userData;
2131 *obpoin = newlibadr(fd, ob->id.lib, *obpoin);
2133 static void lib_link_modifiers(FileData *fd, Object *ob)
2135 modifiers_foreachObjectLink(ob, lib_link_modifiers__linkModifiers, fd);
2138 static void lib_link_object(FileData *fd, Main *main)
2148 ob= main->object.first;
2150 if(ob->id.flag & LIB_NEEDLINK) {
2152 ob->parent= newlibadr(fd, ob->id.lib, ob->parent);
2153 ob->track= newlibadr(fd, ob->id.lib, ob->track);
2154 ob->ipo= newlibadr_us(fd, ob->id.lib, ob->ipo);
2155 ob->action = newlibadr_us(fd, ob->id.lib, ob->action);
2156 ob->dup_group= newlibadr_us(fd, ob->id.lib, ob->dup_group);
2159 ob->data= newlibadr_us(fd, ob->id.lib, ob->data);
2161 if(ob->data==NULL && poin!=NULL) {
2164 if(ob->id.lib) printf("Can't find obdata of %s lib %s\n", ob->id.name+2, ob->id.lib->name);
2165 else printf("Object %s lost data. Lib:%x\n", ob->id.name+2, (unsigned int) ob->id.lib);
2167 for(a=0; a<ob->totcol; a++) ob->mat[a]= newlibadr_us(fd, ob->id.lib, ob->mat[a]);
2169 ob->id.flag -= LIB_NEEDLINK;
2170 /* if id.us==0 a new base will be created later on */
2172 /* WARNING! Also check expand_object(), should reflect the stuff below. */
2173 lib_link_pose(fd, ob, ob->pose);
2174 lib_link_constraints(fd, &ob->id, &ob->constraints);
2175 lib_link_nlastrips(fd, &ob->id, &ob->nlastrips);
2176 lib_link_constraint_channels(fd, &ob->id, &ob->constraintChannels);
2178 for(paf= ob->effect.first; paf; paf= paf->next) {
2179 if(paf->type==EFF_PARTICLE) {
2180 paf->group= newlibadr_us(fd, ob->id.lib, paf->group);
2184 sens= ob->sensors.first;
2186 if(ob->id.lib==NULL) { // done in expand_main
2187 for(a=0; a<sens->totlinks; a++) {
2188 sens->links[a]= newglobadr(fd, sens->links[a]);
2191 if(sens->type==SENS_TOUCH) {
2192 bTouchSensor *ts= sens->data;
2193 ts->ma= newlibadr(fd, ob->id.lib, ts->ma);
2195 else if(sens->type==SENS_MESSAGE) {
2196 bMessageSensor *ms= sens->data;
2198 newlibadr(fd, ob->id.lib, ms->fromObject);
2203 cont= ob->controllers.first;
2205 if(ob->id.lib==NULL) { // done in expand_main
2206 for(a=0; a<cont->totlinks; a++) {
2207 cont->links[a]= newglobadr(fd, cont->links[a]);
2210 if(cont->type==CONT_PYTHON) {
2211 bPythonCont *pc= cont->data;
2212 pc->text= newlibadr(fd, ob->id.lib, pc->text);
2220 act= ob->actuators.first;
2222 if(act->type==ACT_SOUND) {
2223 bSoundActuator *sa= act->data;
2224 sa->sound= newlibadr_us(fd, ob->id.lib, sa->sound);
2226 else if(act->type==ACT_CD) {
2227 /* bCDActuator *cda= act->data; */
2229 else if(act->type==ACT_GAME) {
2230 /* bGameActuator *ga= act->data; */
2232 else if(act->type==ACT_CAMERA) {
2233 bCameraActuator *ca= act->data;
2234 ca->ob= newlibadr(fd, ob->id.lib, ca->ob);
2236 /* leave this one, it's obsolete but necessary to read for conversion */
2237 else if(act->type==ACT_ADD_OBJECT) {
2238 bAddObjectActuator *eoa= act->data;
2239 if(eoa) eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
2241 else if(act->type==ACT_EDIT_OBJECT) {
2242 bEditObjectActuator *eoa= act->data;
2246 eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
2247 eoa->me= newlibadr(fd, ob->id.lib, eoa->me);
2249 else if(act->type==ACT_SCENE) {
2250 bSceneActuator *sa= act->data;
2251 sa->camera= newlibadr(fd, ob->id.lib, sa->camera);
2252 sa->scene= newlibadr(fd, ob->id.lib, sa->scene);
2254 else if(act->type==ACT_ACTION) {
2255 bActionActuator *aa= act->data;
2256 aa->act= newlibadr(fd, ob->id.lib, aa->act);
2258 else if(act->type==ACT_PROPERTY) {
2259 bPropertyActuator *pa= act->data;
2260 pa->ob= newlibadr(fd, ob->id.lib, pa->ob);
2262 else if(act->type==ACT_MESSAGE) {
2263 bMessageActuator *ma= act->data;
2264 ma->toObject= newlibadr(fd, ob->id.lib, ma->toObject);
2269 lib_link_scriptlink(fd, &ob->id, &ob->scriptlink);
2270 lib_link_modifiers(fd, ob);
2275 if(warn) error("WARNING IN CONSOLE");
2279 static void direct_link_pose(FileData *fd, bPose *pose) {
2281 bPoseChannel *pchan;
2286 link_list(fd, &pose->chanbase);
2288 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
2290 pchan->parent= newdataadr(fd, pchan->parent);
2291 pchan->child= newdataadr(fd, pchan->child);
2292 direct_link_constraints(fd, &pchan->constraints);
2293 pchan->iktree.first= pchan->iktree.last= NULL;
2299 static void direct_link_modifiers(FileData *fd, ListBase *lb)
2305 for (md=lb->first; md; md=md->next) {
2308 if (md->type==eModifierType_Subsurf) {
2309 SubsurfModifierData *smd = (SubsurfModifierData*) md;
2311 smd->emCache = smd->mCache = 0;
2312 } else if (md->type==eModifierType_Hook) {
2313 HookModifierData *hmd = (HookModifierData*) md;
2315 hmd->indexar= newdataadr(fd, hmd->indexar);
2316 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2318 for(a=0; a<hmd->totindex; a++) {
2319 SWITCH_INT(hmd->indexar[a]);
2326 static void direct_link_object(FileData *fd, Object *ob)
2335 ob->disp.first=ob->disp.last= NULL;
2337 ob->pose= newdataadr(fd, ob->pose);
2338 direct_link_pose(fd, ob->pose);
2340 link_list(fd, &ob->defbase);
2341 link_list(fd, &ob->nlastrips);
2342 link_list(fd, &ob->constraintChannels);
2344 direct_link_scriptlink(fd, &ob->scriptlink);
2346 ob->mat= newdataadr(fd, ob->mat);
2347 test_pointer_array(fd, (void **)&ob->mat);
2349 /* do it here, below old data gets converted */
2350 direct_link_modifiers(fd, &ob->modifiers);
2352 link_list(fd, &ob->effect);
2353 paf= ob->effect.first;
2355 if(paf->type==EFF_PARTICLE) {
2358 if(paf->type==EFF_WAVE) {
2359 WaveEff *wav = (WaveEff*) paf;
2360 PartEff *next = paf->next;
2361 WaveModifierData *wmd = (WaveModifierData*) modifier_new(eModifierType_Wave);
2363 wmd->damp = wav->damp;
2364 wmd->flag = wav->flag;
2365 wmd->height = wav->height;
2366 wmd->lifetime = wav->lifetime;
2367 wmd->narrow = wav->narrow;
2368 wmd->speed = wav->speed;
2369 wmd->startx = wav->startx;
2370 wmd->starty = wav->startx;
2371 wmd->timeoffs = wav->timeoffs;
2372 wmd->width = wav->width;
2374 BLI_addtail(&ob->modifiers, wmd);
2376 BLI_remlink(&ob->effect, paf);
2382 if(paf->type==EFF_BUILD) {
2383 BuildEff *baf = (BuildEff*) paf;
2384 PartEff *next = paf->next;
2385 BuildModifierData *bmd = (BuildModifierData*) modifier_new(eModifierType_Build);
2387 bmd->start = baf->sfra;
2388 bmd->length = baf->len;
2392 BLI_addtail(&ob->modifiers, bmd);
2394 BLI_remlink(&ob->effect, paf);
2403 ob->pd= newdataadr(fd, ob->pd);
2404 ob->soft= newdataadr(fd, ob->soft);
2406 SoftBody *sb= ob->soft;
2408 sb->bpoint= NULL; // init pointers so it gets rebuilt nicely
2411 sb->keys= newdataadr(fd, sb->keys);
2412 test_pointer_array(fd, (void **)&sb->keys);
2414 for(a=0; a<sb->totkey; a++) {
2415 sb->keys[a]= newdataadr(fd, sb->keys[a]);
2419 ob->fluidsimSettings= newdataadr(fd, ob->fluidsimSettings); /* NT */
2420 if(ob->fluidsimSettings) {
2421 // reinit mesh pointers
2422 ob->fluidsimSettings->orgMesh = NULL; //ob->data;
2423 ob->fluidsimSettings->meshSurface = NULL;
2424 ob->fluidsimSettings->meshBB = NULL;
2427 link_list(fd, &ob->prop);
2428 prop= ob->prop.first;
2430 prop->poin= newdataadr(fd, prop->poin);
2431 if(prop->poin==0) prop->poin= &prop->data;
2435 link_list(fd, &ob->sensors);
2436 sens= ob->sensors.first;
2438 sens->data= newdataadr(fd, sens->data);
2439 sens->links= newdataadr(fd, sens->links);
2440 test_pointer_array(fd, (void **)&sens->links);
2444 direct_link_constraints(fd, &ob->constraints);
2446 link_glob_list(fd, &ob->controllers);
2447 cont= ob->controllers.first;
2449 cont->data= newdataadr(fd, cont->data);
2450 cont->links= newdataadr(fd, cont->links);
2451 test_pointer_array(fd, (void **)&cont->links);
2455 link_glob_list(fd, &ob->actuators);
2456 act= ob->actuators.first;
2458 act->data= newdataadr(fd, act->data);
2462 link_list(fd, &ob->hooks);
2463 while (ob->hooks.first) {
2464 ObHook *hook = ob->hooks.first;
2465 HookModifierData *hmd = (HookModifierData*) modifier_new(eModifierType_Hook);
2467 hook->indexar= newdataadr(fd, hook->indexar);
2468 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2470 for(a=0; a<hook->totindex; a++) {
2471 SWITCH_INT(hook->indexar[a]);
2475 /* Do conversion here because if we have loaded
2476 * a hook we need to make sure it gets converted
2477 * and free'd, regardless of version.
2479 VECCOPY(hmd->cent, hook->cent);
2480 hmd->falloff = hook->falloff;
2481 hmd->force = hook->force;
2482 hmd->indexar = hook->indexar;
2483 hmd->object = hook->parent;
2484 memcpy(hmd->parentinv, hook->parentinv, sizeof(hmd->parentinv));
2485 hmd->totindex = hook->totindex;
2487 BLI_addhead(&ob->modifiers, hmd);
2488 BLI_remlink(&ob->hooks, hook);
2494 ob->derivedDeform= NULL;
2495 ob->derivedFinal= NULL;
2498 /* ************ READ SCENE ***************** */
2500 static void lib_link_scene(FileData *fd, Main *main)
2507 sce= main->scene.first;
2509 if(sce->id.flag & LIB_NEEDLINK) {
2511 sce->camera= newlibadr(fd, sce->id.lib, sce->camera);
2512 sce->world= newlibadr_us(fd, sce->id.lib, sce->world);
2513 sce->set= newlibadr(fd, sce->id.lib, sce->set);
2514 sce->ima= newlibadr_us(fd, sce->id.lib, sce->ima);
2516 base= sce->base.first;
2520 /* base->object= newlibadr_us(fd, sce->id.lib, base->object); */
2521 base->object= newlibadr_us(fd, sce->id.lib, base->object);
2523 /* when save during radiotool, needs cleared */
2524 base->flag &= ~OB_RADIO;
2526 if(base->object==NULL) {
2527 printf("LIB ERROR: base removed\n");
2528 BLI_remlink(&sce->base, base);
2529 if(base==sce->basact) sce->basact= 0;
2537 WHILE_SEQ(&ed->seqbase) {
2538 if(seq->ipo) seq->ipo= newlibadr_us(fd, sce->id.lib, seq->ipo);
2539 if(seq->scene) seq->scene= newlibadr(fd, sce->id.lib, seq->scene);
2541 seq->sound= newlibadr(fd, sce->id.lib, seq->sound);
2543 seq->sound->id.us++;
2544 seq->sound->flags |= SOUND_FLAGS_SEQUENCE;
2552 lib_link_scriptlink(fd, &sce->id, &sce->scriptlink);
2554 sce->id.flag -= LIB_NEEDLINK;
2561 static void link_recurs_seq(FileData *fd, ListBase *lb)
2568 if(seq->seqbase.first) link_recurs_seq(fd, &seq->seqbase);
2573 static void direct_link_scene(FileData *fd, Scene *sce)
2582 sce->dagisvalid = 0;
2584 link_list(fd, &(sce->base));
2586 sce->basact= newdataadr(fd, sce->basact);
2588 sce->radio= newdataadr(fd, sce->radio);
2590 sce->toolsettings= newdataadr(fd, sce->toolsettings);
2593 ed= sce->ed= newdataadr(fd, sce->ed);
2595 /* recursive link sequences, lb will be correctly initialized */
2596 link_recurs_seq(fd, &ed->seqbase);
2598 WHILE_SEQ(&ed->seqbase) {
2599 seq->seq1= newdataadr(fd, seq->seq1);
2600 seq->seq2= newdataadr(fd, seq->seq2);
2601 seq->seq3= newdataadr(fd, seq->seq3);
2602 /* a patch: after introduction of effects with 3 input strips */
2603 if(seq->seq3==0) seq->seq3= seq->seq2;
2607 seq->plugin= newdataadr(fd, seq->plugin);
2608 if(seq->plugin) open_plugin_seq(seq->plugin, seq->name+2);
2610 seq->effectdata= newdataadr(fd, seq->effectdata);
2612 seq->strip= newdataadr(fd, seq->strip);
2613 if(seq->strip && seq->strip->done==0) {
2614 seq->strip->done= 1;
2616 /* standard: strips from effects/metas are not written, but are mallocced */
2618 if(seq->type==SEQ_IMAGE) {
2619 seq->strip->stripdata= newdataadr(fd, seq->strip->stripdata);
2620 se= seq->strip->stripdata;
2622 for(a=0; a<seq->strip->len; a++, se++) {
2628 else if(seq->type==SEQ_MOVIE) {
2629 /* only first stripelem is in file */
2630 se= newdataadr(fd, seq->strip->stripdata);
2633 seq->strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
2634 *seq->strip->stripdata= *se;
2637 se= seq->strip->stripdata;
2639 for(a=0; a<seq->strip->len; a++, se++) {
2646 else if(seq->type==SEQ_SOUND) {
2647 /* only first stripelem is in file */
2648 se= newdataadr(fd, seq->strip->stripdata);
2651 seq->strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
2652 *seq->strip->stripdata= *se;
2655 se= seq->strip->stripdata;
2657 for(a=0; a<seq->strip->len; a++, se++) {
2658 se->ok= 2; /* why? */
2665 seq->strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
2671 /* link metastack, slight abuse of structs here, have to restore pointer to internal part in struct */
2676 /*int seted=0;*/ /*unused*/
2678 offset= ((long)&(temp.seqbase)) - ((long)&temp);
2681 poin= (char *)ed->seqbasep;
2683 poin= newdataadr(fd, poin);
2684 if(poin) ed->seqbasep= (ListBase *)(poin+offset);
2685 else ed->seqbasep= &ed->seqbase;
2688 link_list(fd, &(ed->metastack));
2690 for(ms= ed->metastack.first; ms; ms= ms->next) {
2691 ms->parseq= newdataadr(fd, ms->parseq);
2693 poin= (char *)ms->oldbasep;
2695 poin= newdataadr(fd, poin);
2696 if(poin) ms->oldbasep= (ListBase *)(poin+offset);
2697 else ms->oldbasep= &ed->seqbase;
2702 direct_link_scriptlink(fd, &sce->scriptlink);
2704 sce->r.avicodecdata = newdataadr(fd, sce->r.avicodecdata);
2705 if (sce->r.avicodecdata) {
2706 sce->r.avicodecdata->lpFormat = newdataadr(fd, sce->r.avicodecdata->lpFormat);
2707 sce->r.avicodecdata->lpParms = newdataadr(fd, sce->r.avicodecdata->lpParms);
2710 sce->r.qtcodecdata = newdataadr(fd, sce->r.qtcodecdata);
2711 if (sce->r.qtcodecdata) {
2712 sce->r.qtcodecdata->cdParms = newdataadr(fd, sce->r.qtcodecdata->cdParms);
2715 link_list(fd, &(sce->markers));
2718 /* ************ READ SCREEN ***************** */
2720 /* note: file read without screens option G_FILE_NO_UI;
2721 check lib pointers in call below */
2722 static void lib_link_screen(FileData *fd, Main *main)
2727 sc= main->screen.first;
2729 if(sc->id.flag & LIB_NEEDLINK) {
2731 sc->scene= newlibadr(fd, sc->id.lib, sc->scene);
2733 sa= sc->areabase.first;
2737 sa->full= newlibadr(fd, sc->id.lib, sa->full);
2739 /* space handler scriptlinks */
2740 lib_link_scriptlink(fd, &sc->id, &sa->scriptlink);
2742 for (sl= sa->spacedata.first; sl; sl= sl->next) {
2743 if(sl->spacetype==SPACE_VIEW3D) {
2744 View3D *v3d= (View3D*) sl;
2746 v3d->camera= newlibadr(fd, sc->id.lib, v3d->camera);
2749 v3d->bgpic->ima= newlibadr_us(fd, sc->id.lib, v3d->bgpic->ima);
2750 v3d->bgpic->tex= newlibadr_us(fd, sc->id.lib, v3d->bgpic->tex);
2751 v3d->bgpic->rect= NULL;
2754 v3d->localvd->camera= newlibadr(fd, sc->id.lib, v3d->localvd->camera);
2757 else if(sl->spacetype==SPACE_IPO) {
2758 SpaceIpo *sipo= (SpaceIpo *)sl;
2761 if(sipo->blocktype==ID_SEQ) sipo->from= NULL; // no libdata
2762 else sipo->from= newlibadr(fd, sc->id.lib, sipo->from);
2764 sipo->ipokey.first= sipo->ipokey.last= 0;
2765 sipo->ipo= newlibadr(fd, sc->id.lib, sipo->ipo);
2767 else if(sl->spacetype==SPACE_BUTS) {
2768 SpaceButs *sbuts= (SpaceButs *)sl;
2770 sbuts->lockpoin= NULL;
2771 if(main->versionfile<132) set_rects_butspace(sbuts);
2773 else if(sl->spacetype==SPACE_FILE) {
2774 SpaceFile *sfile= (SpaceFile *)sl;
2777 sfile->libfiledata= 0;
2778 sfile->returnfunc= 0;
2780 else if(sl->spacetype==SPACE_IMASEL) {
2781 check_imasel_copy((SpaceImaSel *)sl);
2783 else if(sl->spacetype==SPACE_ACTION) {
2784 SpaceAction *saction= (SpaceAction *)sl;
2785 saction->action = newlibadr(fd, sc->id.lib, saction->action);
2787 else if(sl->spacetype==SPACE_IMAGE) {
2788 SpaceImage *sima= (SpaceImage *)sl;
2790 sima->image= newlibadr_us(fd, sc->id.lib, sima->image);
2792 else if(sl->spacetype==SPACE_NLA){
2793 /* SpaceNla *snla= (SpaceNla *)sl; */
2795 else if(sl->spacetype==SPACE_TEXT) {
2796 SpaceText *st= (SpaceText *)sl;
2798 st->text= newlibadr(fd, sc->id.lib, st->text);
2801 else if(sl->spacetype==SPACE_SCRIPT) {
2802 SpaceScript *sc= (SpaceScript *)sl;
2806 else if(sl->spacetype==SPACE_OOPS) {
2807 SpaceOops *so= (SpaceOops *)sl;
2809 TreeStoreElem *tselem;
2812 oops= so->oops.first;
2814 oops->id= newlibadr(fd, NULL, oops->id);
2818 so->tree.first= so->tree.last= NULL;
2821 tselem= so->treestore->data;
2822 for(a=0; a<so->treestore->usedelem; a++, tselem++) {
2823 tselem->id= newlibadr(fd, NULL, tselem->id);
2827 else if(sl->spacetype==SPACE_SOUND) {
2828 SpaceSound *ssound= (SpaceSound *)sl;
2830 ssound->sound= newlibadr_us(fd, sc->id.lib, ssound->sound);
2835 sc->id.flag -= LIB_NEEDLINK;
2841 static void *restore_pointer_by_name(Main *mainp, ID *id, int user)
2847 lb= wich_libbase(mainp, GS(id->name));
2849 if(lb) { // there's still risk of checking corrupt mem (freed Ids in oops)
2852 if( strcmp(idn->name, id->name)==0) {
2853 if(user && idn->us==0) idn->us++;
2863 /* called from kernel/blender.c */
2864 /* used to link a file (without UI) to the current UI */
2865 void lib_link_screen_restore(Main *newmain, Scene *curscene)
2870 sc= newmain->screen.first;
2873 sc->scene= curscene;
2875 sa= sc->areabase.first;
2879 if (sa->scriptlink.totscript) {
2880 /* restore screen area script links */
2881 ScriptLink *slink = &sa->scriptlink;
2883 for (script_idx = 0; script_idx < slink->totscript; script_idx++) {
2884 slink->scripts[script_idx] = restore_pointer_by_name(newmain,
2885 (ID *)slink->scripts[script_idx], 1);
2889 for (sl= sa->spacedata.first; sl; sl= sl->next) {
2890 if(sl->spacetype==SPACE_VIEW3D) {
2891 View3D *v3d= (View3D*) sl;
2893 v3d->camera= sc->scene->camera;
2896 v3d->bgpic->ima= restore_pointer_by_name(newmain, (ID *)v3d->bgpic->ima, 1);
2897 v3d->bgpic->tex= restore_pointer_by_name(newmain, (ID *)v3d->bgpic->tex, 1);
2898 if(v3d->bgpic->rect) freeN(v3d->bgpic->rect);
2899 v3d->bgpic->rect= NULL;
2904 v3d->localvd->camera= sc->scene->camera;
2906 /* localview can become invalid during undo/redo steps, so we exit it when no could be found */
2907 for(base= sc->scene->base.first; base; base= base->next) {
2908 if(base->lay & v3d->lay) break;
2911 v3d->lay= v3d->localvd->lay;
2912 v3d->layact= v3d->localvd->layact;
2913 MEM_freeN(v3d->localvd);
2918 else if(v3d->scenelock) v3d->lay= sc->scene->lay;
2920 /* not very nice, but could help */
2921 if((v3d->layact & v3d->lay)==0) v3d->layact= v3d->lay;
2924 else if(sl->spacetype==SPACE_IPO) {
2925 SpaceIpo *sipo= (SpaceIpo *)sl;
2927 if(sipo->blocktype==ID_SEQ) sipo->from= NULL; // no libdata
2928 else sipo->from= restore_pointer_by_name(newmain, (ID *)sipo->from, 0);
2930 // not free sipo->ipokey, creates dependency with src/
2931 sipo->ipo= restore_pointer_by_name(newmain, (ID *)sipo->ipo, 0);
2932 if(sipo->editipo) MEM_freeN(sipo->editipo);
2933 sipo->editipo= NULL;
2935 else if(sl->spacetype==SPACE_BUTS) {
2936 SpaceButs *sbuts= (SpaceButs *)sl;
2937 sbuts->lockpoin= NULL;
2938 sbuts->cury= 0; // we leave rect, for nicer redraws
2940 else if(sl->spacetype==SPACE_FILE) {
2941 SpaceFile *sfile= (SpaceFile *)sl;
2942 if(sfile->libfiledata)
2943 BLO_blendhandle_close(sfile->libfiledata);
2944 sfile->libfiledata= 0;
2946 else if(sl->spacetype==SPACE_IMASEL) {
2949 else if(sl->spacetype==SPACE_ACTION) {
2950 SpaceAction *saction= (SpaceAction *)sl;
2951 saction->action = restore_pointer_by_name(newmain, (ID *)saction->action, 1);
2953 else if(sl->spacetype==SPACE_IMAGE) {
2954 SpaceImage *sima= (SpaceImage *)sl;
2956 sima->image= restore_pointer_by_name(newmain, (ID *)sima->image, 1);
2958 else if(sl->spacetype==SPACE_NLA){
2959 /* SpaceNla *snla= (SpaceNla *)sl; */
2961 else if(sl->spacetype==SPACE_TEXT) {
2962 SpaceText *st= (SpaceText *)sl;
2964 st->text= restore_pointer_by_name(newmain, (ID *)st->text, 1);
2965 if(st->text==NULL) st->text= newmain->text.first;
2967 else if(sl->spacetype==SPACE_SCRIPT) {
2968 SpaceScript *sc= (SpaceScript *)sl;
2972 else if(sl->spacetype==SPACE_OOPS) {
2973 SpaceOops *so= (SpaceOops *)sl;
2977 oops= so->oops.first;
2979 oops->id= restore_pointer_by_name(newmain, (ID *)oops->id, 0);
2985 TreeStore *ts= so->treestore;
2986 TreeStoreElem *tselem=ts->data;
2987 for(a=0; a<ts->usedelem; a++, tselem++) {
2988 tselem->id= restore_pointer_by_name(newmain, tselem->id, 0);
2992 else if(sl->spacetype==SPACE_SOUND) {
2993 SpaceSound *ssound= (SpaceSound *)sl;
2995 ssound->sound= restore_pointer_by_name(newmain, (ID *)ssound->sound, 1);
3006 static void direct_link_screen(FileData *fd, bScreen *sc)
3014 link_list(fd, &(sc->vertbase));
3015 link_list(fd, &(sc->edgebase));
3016 link_list(fd, &(sc->areabase));
3019 /* hacky patch... but people have been saving files with the verse-blender,
3020 causing the handler to keep running for ever, with no means to disable it */
3021 for(a=0; a<SCREEN_MAXHANDLER; a+=2) {
3022 if( sc->handler[a]==SCREEN_HANDLER_VERSE) {
3029 se= sc->edgebase.first;
3031 se->v1= newdataadr(fd, se->v1);
3032 se->v2= newdataadr(fd, se->v2);
3033 if( (long)se->v1 > (long)se->v2) {
3040 printf("error reading screen... file corrupt\n");
3047 sa= sc->areabase.first;
3052 link_list(fd, &(sa->spacedata));
3053 link_list(fd, &(sa->panels));
3055 for(pa= sa->panels.first; pa; pa=pa->next) {
3056 pa->paneltab= newdataadr(fd, pa->paneltab);
3061 for (sl= sa->spacedata.first; sl; sl= sl->next) {
3062 if (sl->spacetype==SPACE_VIEW3D) {
3063 View3D *v3d= (View3D*) sl;
3064 v3d->bgpic= newdataadr(fd, v3d->bgpic);
3065 v3d->localvd= newdataadr(fd, v3d->localvd);
3066 v3d->afterdraw.first= v3d->afterdraw.last= NULL;
3067 v3d->clipbb= newdataadr(fd, v3d->clipbb);
3069 else if (sl->spacetype==SPACE_OOPS) {
3070 SpaceOops *soops= (SpaceOops*) sl;
3072 link_list(fd, &(soops->oops));
3073 oops= soops->oops.first;
3075 oops->link.first= oops->link.last= 0;
3079 soops->treestore= newdataadr(fd, soops->treestore);
3080 if(soops->treestore) {
3081 soops->treestore->data= newdataadr(fd, soops->treestore->data);
3082 /* we only saved what was used */
3083 soops->treestore->totelem= soops->treestore->usedelem;
3084 soops->storeflag |= SO_TREESTORE_CLEANUP; // at first draw
3089 sa->v1= newdataadr(fd, sa->v1);
3090 sa->v2= newdataadr(fd, sa->v2);
3091 sa->v3= newdataadr(fd, sa->v3);
3092 sa->v4= newdataadr(fd, sa->v4);
3094 sa->win= sa->headwin= 0;
3096 sa->uiblocks.first= sa->uiblocks.last= NULL;
3098 /* space handler scriptlinks */
3099 direct_link_scriptlink(fd, &sa->scriptlink);
3105 /* ********** READ LIBRARY *************** */
3108 static void direct_link_library(FileData *fd, Library *lib, Main *main)
3113 for(newmain= fd->mainlist.first; newmain; newmain= newmain->next) {
3114 if(newmain->curlib) {
3115 if(strcmp(newmain->curlib->name, lib->name)==0) {
3116 printf("Fixed error in file; multiple instances of lib:\n %s\n", lib->name);
3118 change_idid_adr_fd(fd, lib, newmain->curlib);
3120 BLI_remlink(&main->library, lib);