8 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version. The Blender
14 * Foundation also sells licenses for use in proprietary software under
15 * the Blender License. See http://www.blender.org/BL/ for information
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software Foundation,
25 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
28 * All rights reserved.
30 * The Original Code is: all of this file.
32 * Contributor(s): none yet.
34 * ***** END GPL/BL DUAL LICENSE BLOCK *****
44 #include "BLI_winstuff.h"
47 #include <stdio.h> // for printf fopen fwrite fclose sprintf FILE
48 #include <stdlib.h> // for getenv atoi
49 #include <fcntl.h> // for open
50 #include <string.h> // for strrchr strncmp strstr
51 #include <math.h> // for fabs
54 #include <unistd.h> // for read close
55 #include <sys/param.h> // for MAXPATHLEN
57 #include <io.h> // for open close read
62 #include "DNA_action_types.h"
63 #include "DNA_armature_types.h"
65 #include "DNA_actuator_types.h"
66 #include "DNA_camera_types.h"
67 #include "DNA_controller_types.h"
68 #include "DNA_constraint_types.h"
69 #include "DNA_curve_types.h"
70 #include "DNA_effect_types.h"
71 #include "DNA_fileglobal_types.h"
72 #include "DNA_group_types.h"
73 #include "DNA_ipo_types.h"
74 #include "DNA_ika_types.h"
75 #include "DNA_image_types.h"
76 #include "DNA_key_types.h"
77 #include "DNA_lattice_types.h"
78 #include "DNA_lamp_types.h"
79 #include "DNA_meta_types.h"
80 #include "DNA_material_types.h"
81 #include "DNA_mesh_types.h"
82 #include "DNA_meshdata_types.h"
83 #include "DNA_nla_types.h"
84 #include "DNA_object_types.h"
85 #include "DNA_object_force.h"
86 #include "DNA_oops_types.h"
87 #include "DNA_object_force.h"
88 #include "DNA_packedFile_types.h"
89 #include "DNA_property_types.h"
90 #include "DNA_text_types.h"
91 #include "DNA_view3d_types.h"
92 #include "DNA_screen_types.h"
93 #include "DNA_sensor_types.h"
94 #include "DNA_sdna_types.h"
95 #include "DNA_scene_types.h"
96 #include "DNA_sequence_types.h"
97 #include "DNA_sound_types.h"
98 #include "DNA_space_types.h"
99 #include "DNA_texture_types.h"
100 #include "DNA_userdef_types.h"
101 #include "DNA_vfont_types.h"
102 #include "DNA_world_types.h"
104 #include "MEM_guardedalloc.h"
105 #include "BLI_blenlib.h"
106 #include "BLI_arithb.h"
107 #include "BLI_storage_types.h" // for relname flags
109 #include "BKE_bad_level_calls.h" // for reopen_text build_seqar (from WHILE_SEQ) open_plugin_seq set_rects_butspace check_imasel_copy
111 #include "BKE_armature.h" // for precalc_bonelist_irestmats
112 #include "BKE_action.h"
113 #include "BKE_constraint.h"
114 #include "BKE_curve.h"
115 #include "BKE_deform.h"
116 #include "BKE_effect.h" // for give_parteff
117 #include "BKE_global.h" // for G
118 #include "BKE_property.h" // for get_property
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_object.h"
123 #include "BKE_sca.h" // for init_actuator
124 #include "BKE_scene.h"
125 #include "BKE_softbody.h" // sbNew()
126 #include "BKE_texture.h" // for open_plugin_tex
127 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
129 #include "BIF_butspace.h" // for do_versions, patching event codes
131 #include "BLO_readfile.h"
132 #include "BLO_undofile.h"
133 #include "BLO_readblenfile.h" // streaming read pipe, for BLO_readblenfile BLO_readblenfilememory
135 #include "readfile.h"
139 #include "mydevice.h"
143 Remark: still a weak point is the newadress() function, that doesnt solve reading from
144 multiple files at the same time
146 (added remark: oh, i thought that was solved? will look at that... (ton)
149 - Existing Library (Main) push or free
159 - read associated 'direct data'
160 - link direct data (internal and to LibBlock)
162 - read USER data, only when indicated (file is ~/.B.blend)
164 - per Library (per Main)
167 - find LibBlocks and attach IDs to Main
168 - if external LibBlock
170 - or it's already read,
175 - read associated direct data
176 - link direct data (internal and to LibBlock)
178 - per Library with unread LibBlocks
183 - read associated direct data
184 - link direct data (internal and to LibBlock)
187 - link all LibBlocks and indirect pointers to libblocks
188 - initialize FileGlobal and copy pointers to Global
191 /* also occurs in library.c */
192 /* GS reads the memory pointed at in a specific ordering. There are,
193 * however two definitions for it. I have jotted them down here, both,
194 * but I think the first one is actually used. The thing is that
195 * big-endian systems might read this the wrong way round. OTOH, we
196 * constructed the IDs that are read out with this macro explicitly as
197 * well. I expect we'll sort it out soon... */
200 #define GS(a) (*((short *)(a)))
202 /* from misc_util: flip the bytes from x */
203 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
205 // only used here in readfile.c
206 #define SWITCH_LONGINT(a) { \
209 s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
210 s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
211 s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
212 s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
213 // only used here in readfile.c
214 #define SWITCH_SHORT(a) { \
217 s_i=p_i[0]; p_i[0]=p_i[1]; p_i[1]=s_i; }
222 typedef struct OldNew {
227 typedef struct OldNewMap {
229 int nentries, entriessize;
235 /* local prototypes */
236 void lib_link_screen_restore(Main *, char , Scene *);
237 extern short freeN(void *vmemh); /* defined in util.h */
240 static OldNewMap *oldnewmap_new(void) {
241 OldNewMap *onm= MEM_mallocN(sizeof(*onm), "OldNewMap");
244 onm->entriessize= 1024;
245 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
250 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr) {
253 if (onm->nentries==onm->entriessize) {
254 int osize= onm->entriessize;
255 OldNew *oentries= onm->entries;
257 onm->entriessize*= 2;
258 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
260 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
264 entry= &onm->entries[onm->nentries++];
266 entry->newp= newaddr;
270 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr) {
273 if (onm->lasthit<onm->nentries-1) {
274 OldNew *entry= &onm->entries[++onm->lasthit];
276 if (entry->old==addr) {
282 for (i=0; i<onm->nentries; i++) {
283 OldNew *entry= &onm->entries[i];
285 if (entry->old==addr) {
296 static void *oldnewmap_liblookup_and_inc(OldNewMap *onm, void *addr, void *lib) {
299 if (onm->lasthit<onm->nentries-1) {
300 OldNew *entry= &onm->entries[++onm->lasthit];
302 if (entry->old==addr) {
305 if (id && (!lib || id->lib)) {
313 for (i=0; i<onm->nentries; i++) {
314 OldNew *entry= &onm->entries[i];
316 if (entry->old==addr) {
319 if (id && (!lib || id->lib)) {
330 static void *oldnewmap_typelookup_and_inc(OldNewMap *onm, void *addr, short type) {
333 if (onm->lasthit<onm->nentries-1) {
334 OldNew *entry= &onm->entries[++onm->lasthit];
336 if (entry->old==addr) {
339 if (id && (GS(id->name) == type)) {
347 for (i=0; i<onm->nentries; i++) {
348 OldNew *entry= &onm->entries[i];
350 if (entry->old==addr) {
353 if (id && (GS(id->name) == type)) {
364 static void oldnewmap_free_unused(OldNewMap *onm) {
367 for (i=0; i<onm->nentries; i++) {
368 OldNew *entry= &onm->entries[i];
370 MEM_freeN(entry->newp);
376 static void oldnewmap_clear(OldNewMap *onm) {
381 static void oldnewmap_free(OldNewMap *onm) {
382 MEM_freeN(onm->entries);
388 static void read_libraries(FileData *basefd, ListBase *mainlist);
390 /* ************ help functions ***************** */
392 static void add_main_to_main(Main *mainvar, Main *from)
394 ListBase *lbarray[100], *fromarray[100]; // define in library.c too
397 a= set_listbasepointers(mainvar, lbarray);
398 a= set_listbasepointers(from, fromarray);
400 addlisttolist(lbarray[a], fromarray[a]);
404 void blo_join_main(ListBase *mainlist)
406 Main *tojoin, *mainl= mainlist->first;
408 while ((tojoin= mainl->next)) {
409 add_main_to_main(mainl, tojoin);
410 BLI_remlink(mainlist, tojoin);
415 static void split_libdata(ListBase *lb, Main *first)
427 if(mainvar->curlib==id->lib) {
428 lbn= wich_libbase(mainvar, GS(id->name));
430 BLI_addtail(lbn, id);
433 mainvar= mainvar->next;
435 if(mainvar==0) printf("error split_libdata\n");
441 void blo_split_main(ListBase *mainlist)
443 Main *mainl= mainlist->first;
444 ListBase *lbarray[30];
448 for (lib= mainl->library.first; lib; lib= lib->id.next) {
449 Main *libmain= MEM_callocN(sizeof(*libmain), "libmain");
450 libmain->curlib= lib;
452 BLI_addtail(mainlist, libmain);
455 i= set_listbasepointers(mainl, lbarray);
457 split_libdata(lbarray[i], mainl->next);
460 static Main *blo_find_main(ListBase *mainlist, char *name)
464 char name1[FILE_MAXDIR+FILE_MAXFILE];
465 char libname1[FILE_MAXDIR+FILE_MAXFILE];
467 /* name can be stringcode too */
469 BLI_convertstringcode(name1, G.sce, 0);
471 for (m= mainlist->first; m; m= m->next) {
472 char *libname= (m->curlib)?m->curlib->name:m->name;
474 strcpy(libname1, libname);
475 BLI_convertstringcode(libname1, G.sce, 0);
477 if (BLI_streq(name1, libname1))
481 m= MEM_callocN(sizeof(*m), "find_main");
482 BLI_addtail(mainlist, m);
484 lib= alloc_libblock(&m->library, ID_LI, "lib");
485 strcpy(lib->name, name);
492 /* ************ FILE PARSING ****************** */
494 static void switch_endian_bh4(BHead4 *bhead)
496 /* the ID_.. codes */
497 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
499 if (bhead->code != ENDB) {
500 SWITCH_INT(bhead->len);
501 SWITCH_INT(bhead->SDNAnr);
502 SWITCH_INT(bhead->nr);
506 static void switch_endian_bh8(BHead8 *bhead)
508 /* the ID_.. codes */
509 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
511 if (bhead->code != ENDB) {
512 SWITCH_INT(bhead->len);
513 SWITCH_INT(bhead->SDNAnr);
514 SWITCH_INT(bhead->nr);
518 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
520 BHead4 *bhead4 = (BHead4 *) bhead;
521 #if defined(WIN32) && !defined(FREE_WINDOWS)
527 bhead4->code= bhead8->code;
528 bhead4->len= bhead8->len;
530 if (bhead4->code != ENDB) {
532 // why is this here ??
533 if (do_endian_swap) {
534 SWITCH_LONGINT(bhead8->old);
537 /* this patch is to avoid a long long being read from not-eight aligned positions
538 is necessary on SGI with -n32 compiling (no, is necessary on
539 any modern 64bit architecture) */
540 memcpy(&old, &bhead8->old, 8);
541 bhead4->old = (int) (old >> 3);
543 bhead4->SDNAnr= bhead8->SDNAnr;
544 bhead4->nr= bhead8->nr;
548 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
550 BHead8 *bhead8 = (BHead8 *) bhead;
552 bhead8->code= bhead4->code;
553 bhead8->len= bhead4->len;
555 if (bhead8->code != ENDB) {
556 bhead8->old= bhead4->old;
557 bhead8->SDNAnr= bhead4->SDNAnr;
558 bhead8->nr= bhead4->nr;
562 static BHeadN *get_bhead(FileData *fd)
567 BHeadN *new_bhead = 0;
573 // First read the bhead structure.
574 // Depending on the platform the file was written on this can
575 // be a big or little endian BHead4 or BHead8 structure.
577 // As usual 'ENDB' (the last *partial* bhead of the file)
578 // needs some special handling. We don't want to EOF just yet.
580 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
582 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
584 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
585 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
586 switch_endian_bh4(&bhead4);
589 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
590 bh8_from_bh4(&bhead, &bhead4);
592 memcpy(&bhead, &bhead4, sizeof(bhead));
599 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
601 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
602 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
603 switch_endian_bh8(&bhead8);
606 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
607 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
609 memcpy(&bhead, &bhead8, sizeof(bhead));
616 // bhead now contains the (converted) bhead structure. Now read
617 // the associated data and put everything in a BHeadN (creative naming !)
620 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
622 new_bhead->next = new_bhead->prev = 0;
623 new_bhead->bhead = bhead;
625 readsize = fd->read(fd, new_bhead + 1, bhead.len);
627 if (readsize != bhead.len) {
629 MEM_freeN(new_bhead);
638 // We've read a new block. Now add it to the list
642 BLI_addtail(&fd->listbase, new_bhead);
648 BHead *blo_firstbhead(FileData *fd)
654 // Read in a new block if necessary
656 new_bhead = fd->listbase.first;
657 if (new_bhead == 0) {
658 new_bhead = get_bhead(fd);
662 bhead = &new_bhead->bhead;
668 BHead *blo_prevbhead(FileData *fd, BHead *thisblock)
670 BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - (int) (&((BHeadN*)0)->bhead));
671 BHeadN *prev= bheadn->prev;
673 return prev?&prev->bhead:NULL;
676 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
678 BHeadN *new_bhead = 0;
682 // bhead is actually a sub part of BHeadN
683 // We calculate the BHeadN pointer from the BHead pointer below
684 new_bhead = (BHeadN *) (((char *) thisblock) - (int) (&((BHeadN*)0)->bhead));
686 // get the next BHeadN. If it doesn't exist we read in the next one
687 new_bhead = new_bhead->next;
688 if (new_bhead == 0) {
689 new_bhead = get_bhead(fd);
694 // here we do the reverse:
695 // go from the BHeadN pointer to the BHead pointer
696 bhead = &new_bhead->bhead;
702 static void decode_blender_header(FileData *fd)
704 char header[SIZEOFBLENDERHEADER], num[4];
707 // read in the header data
708 readsize = fd->read(fd, header, sizeof(header));
710 if (readsize == sizeof(header)) {
711 if(strncmp(header, "BLENDER", 7) == 0) {
712 int remove_this_endian_test= 1;
714 fd->flags |= FD_FLAGS_FILE_OK;
716 // what size are pointers in the file ?
718 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
719 if (sizeof(void *) != 4) {
720 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
723 if (sizeof(void *) != 8) {
724 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
728 // is the file saved in a different endian
730 if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
731 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
734 // get the version number
736 memcpy(num, header+9, 3);
738 fd->fileversion = atoi(num);
743 static int read_file_dna(FileData *fd)
747 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
748 if (bhead->code==DNA1) {
749 int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
751 fd->filesdna= dna_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
753 fd->compflags= dna_get_structDNA_compareflags(fd->filesdna, fd->memsdna);
756 } else if (bhead->code==ENDB)
762 static int fd_read_from_file(FileData *filedata, void *buffer, int size)
764 int readsize = read(filedata->filedes, buffer, size);
769 filedata->seek += readsize;
775 static int fd_read_from_memory(FileData *filedata, void *buffer, int size)
777 // don't read more bytes then there are available in the buffer
778 int readsize = MIN2(size, filedata->buffersize - filedata->seek);
780 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
781 filedata->seek += readsize;
786 static int fd_read_from_memfile(FileData *filedata, void *buffer, int size)
788 static unsigned int seek= 1<<30; /* the current position */
789 static unsigned int offset= 0; /* size of previous chunks */
790 static MemFileChunk *chunk=NULL;
792 if(size==0) return 0;
794 if(seek != filedata->seek) {
795 chunk= filedata->memfile->chunks.first;
799 if(seek + chunk->size > filedata->seek) break;
804 seek= filedata->seek;
808 /* first check if it's on the end if current chunk */
809 if( seek-offset == chunk->size) {
810 offset+= chunk->size;
814 /* debug, should never happen */
816 printf("illegal read, chunk zero\n");
819 else if( (seek-offset)+size > chunk->size) {
820 size= chunk->size - (seek-offset);
821 printf("chunk too large, clipped to %d\n", size);
824 memcpy(buffer, chunk->buf + (seek-offset), size);
825 filedata->seek += size;
834 static FileData *filedata_new(void)
836 extern char DNAstr[]; /* DNA.c */
838 FileData *fd = MEM_callocN(sizeof(*fd), "FileData");
842 /* XXX, this doesn't need to be done all the time,
843 * but it keeps us reentrant, remove once we have
844 * a lib that provides a nice lock. - zr
846 fd->memsdna = dna_sdna_from_data(DNAstr, DNAlen, 0);
848 fd->datamap = oldnewmap_new();
849 fd->globmap = oldnewmap_new();
850 fd->libmap = oldnewmap_new();
855 FileData *blo_openblenderfile(char *name)
858 char name1[FILE_MAXDIR+FILE_MAXFILE];
860 /* library files can have stringcodes */
862 BLI_convertstringcode(name1, G.sce, 0);
864 file= open(name1, O_BINARY|O_RDONLY);
869 FileData *fd = filedata_new();
871 fd->buffersize = BLI_filesize(file);
872 fd->read = fd_read_from_file;
874 decode_blender_header(fd);
876 if (fd->flags & FD_FLAGS_FILE_OK) {
877 if (!read_file_dna(fd)) {
878 blo_freefiledata(fd);
882 blo_freefiledata(fd);
890 FileData *blo_openblendermemory(void *mem, int memsize)
892 if (!mem || memsize<SIZEOFBLENDERHEADER) {
895 FileData *fd= filedata_new();
897 fd->buffersize= memsize;
898 fd->read= fd_read_from_memory;
899 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
901 decode_blender_header(fd);
903 if (fd->flags & FD_FLAGS_FILE_OK) {
904 if (!read_file_dna(fd)) {
905 blo_freefiledata(fd);
909 blo_freefiledata(fd);
917 FileData *blo_openblendermemfile(MemFile *memfile)
922 FileData *fd= filedata_new();
923 fd->memfile= memfile;
925 fd->read= fd_read_from_memfile;
926 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
928 decode_blender_header(fd);
930 if (fd->flags & FD_FLAGS_FILE_OK) {
931 if (!read_file_dna(fd)) {
932 blo_freefiledata(fd);
936 blo_freefiledata(fd);
945 void blo_freefiledata(FileData *fd)
948 if (fd->filedes != -1) {
952 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
953 MEM_freeN(fd->buffer);
957 // Free all BHeadN data blocks
958 BLI_freelistN(&fd->listbase);
961 dna_freestructDNA(fd->memsdna);
963 dna_freestructDNA(fd->filesdna);
965 MEM_freeN(fd->compflags);
968 oldnewmap_free(fd->datamap);
970 oldnewmap_free(fd->globmap);
971 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
972 oldnewmap_free(fd->libmap);
978 /* ************ DIV ****************** */
980 int BLO_has_bfile_extension(char *str)
982 return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend"));
985 /* ************** OLD POINTERS ******************* */
987 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
989 return oldnewmap_lookup_and_inc(fd->datamap, adr);
992 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
994 return oldnewmap_lookup_and_inc(fd->globmap, adr);
997 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
999 return oldnewmap_liblookup_and_inc(fd->libmap, adr, lib);
1002 static void *newlibadr_us_type(FileData *fd, short type, void *adr) /* only Lib data */
1004 ID *id= oldnewmap_typelookup_and_inc(fd->libmap, adr, type);
1013 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1015 ID *id= newlibadr(fd, lib, adr);
1024 static void change_libadr(FileData *fd, void *old, void *new)
1028 /* changed one thing here, the old change_libadr
1029 * only remapped addresses that had an id->lib,
1030 * but that doesn't make sense to me... its an
1031 * old pointer, period, it needs to be remapped. - zr
1035 * Ton seemed to think it was necessary to look
1036 * through all entries, and not return after finding
1037 * a match, leaving this cryptic comment,
1038 * // no return, maybe there can be more?
1040 * That doesn't make sense to me either but I am
1041 * too scared to remove it... it only would make
1042 * sense if two distinct old address map to the
1043 * same new address - obviously that shouldn't happen
1044 * because memory addresses are unique.
1046 * The only case it might happen is when two distinct
1047 * libraries are mapped using the same table... this
1048 * won't work to start with... At some point this
1049 * all needs to be made sense of and made understandable,
1050 * but I'm afraid I don't have time now. -zr
1053 /* the code is nasty, and needs a lot of energy to get into full understanding
1054 again... i now translate dutch comments, maybe that gives me more insight!
1055 But i guess it has to do with the assumption that 2 addresses can be allocated
1056 in different sessions, and therefore be the same... like the remark in the top
1057 of this c file (ton) */
1059 for (i=0; i<fd->libmap->nentries; i++) {
1060 OldNew *entry= &fd->libmap->entries[i];
1062 if (old==entry->newp) {
1070 /* ********** END OLD POINTERS ****************** */
1071 /* ********** READ FILE ****************** */
1073 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1075 int blocksize, nblocks;
1078 data= (char *)(bhead+1); /* BHEAD+DATA dependancy */
1079 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1083 dna_switch_endian_struct(filesdna, bhead->SDNAnr, data);
1089 static void *read_struct(FileData *fd, BHead *bh, char *blockname)
1094 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1095 switch_endian_structs(fd->filesdna, bh);
1097 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1098 if(fd->compflags[bh->SDNAnr]==2) {
1099 temp= dna_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1101 temp= MEM_mallocN(bh->len, blockname);
1102 memcpy(temp, (bh+1), bh->len); /* BHEAD+DATA dependancy */
1110 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1114 if(lb->first==0) return;
1116 lb->first= newdataadr(fd, lb->first);
1120 ln->next= newdataadr(fd, ln->next);
1128 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1133 if(lb->first==0) return;
1134 poin= newdataadr(fd, lb->first);
1136 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1143 poin= newdataadr(fd, ln->next);
1145 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1155 static void test_pointer_array(FileData *fd, void **mat)
1157 #if defined(WIN32) && !defined(FREE_WINDOWS)
1158 __int64 *lpoin, *lmat;
1160 long long *lpoin, *lmat;
1162 int len, *ipoin, *imat;
1164 /* manually convert the pointer array in
1165 * the old dna format to a pointer array in
1166 * the new dna format.
1169 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1171 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1172 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1176 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1177 SWITCH_LONGINT(*lpoin);
1178 *ipoin= (int) ((*lpoin) >> 3);
1186 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1187 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1201 /* ************ READ PACKEDFILE *************** */
1203 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1205 PackedFile *pf= newdataadr(fd, oldpf);
1208 pf->data= newdataadr(fd, pf->data);
1214 /* ************ READ SCRIPTLINK *************** */
1216 static void lib_link_scriptlink(FileData *fd, ID *id, ScriptLink *slink)
1220 for(i=0; i<slink->totscript; i++) {
1221 slink->scripts[i]= newlibadr(fd, id->lib, slink->scripts[i]);
1225 static void direct_link_scriptlink(FileData *fd, ScriptLink *slink)
1227 slink->scripts= newdataadr(fd, slink->scripts);
1228 slink->flag= newdataadr(fd, slink->flag);
1230 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1233 for(a=0; a<slink->totscript; a++) {
1234 SWITCH_SHORT(slink->flag[a]);
1239 /* ************ READ IKA ***************** */
1241 static void lib_link_ika(FileData *fd, Main *main)
1247 ika= main->ika.first;
1249 if(ika->id.flag & LIB_NEEDLINK) {
1251 ika->parent= newlibadr(fd, ika->id.lib, ika->parent);
1256 def->ob= newlibadr(fd, ika->id.lib, def->ob);
1259 ika->id.flag -= LIB_NEEDLINK;
1265 static void direct_link_ika(FileData *fd, Ika *ika)
1267 link_list(fd, &ika->limbbase);
1269 ika->def= newdataadr(fd, ika->def);
1271 /* error from V.138 and older */
1272 if(ika->def==0) ika->totdef= 0;
1275 /* ************ READ ARMATURE ***************** */
1277 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1279 bActionStrip *strip;
1281 for (strip=striplist->first; strip; strip=strip->next){
1282 strip->act = newlibadr_us(fd, id->lib, strip->act);
1283 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1287 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1289 bConstraintChannel *chan;
1291 for (chan=chanbase->first; chan; chan=chan->next){
1292 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1296 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
1300 for (con = conlist->first; con; con=con->next) {
1301 /* patch for error introduced by changing constraints (dunno how) */
1302 if(con->data==NULL) {
1303 con->type= CONSTRAINT_TYPE_NULL;
1306 switch (con->type) {
1307 case CONSTRAINT_TYPE_ACTION:
1309 bActionConstraint *data;
1310 data= ((bActionConstraint*)con->data);
1311 data->tar = newlibadr(fd, id->lib, data->tar);
1312 data->act = newlibadr(fd, id->lib, data->act);
1315 case CONSTRAINT_TYPE_LOCLIKE:
1317 bLocateLikeConstraint *data;
1318 data= ((bLocateLikeConstraint*)con->data);
1319 data->tar = newlibadr(fd, id->lib, data->tar);
1322 case CONSTRAINT_TYPE_ROTLIKE:
1324 bRotateLikeConstraint *data;
1325 data= ((bRotateLikeConstraint*)con->data);
1326 data->tar = newlibadr(fd, id->lib, data->tar);
1329 case CONSTRAINT_TYPE_KINEMATIC:
1331 bKinematicConstraint *data;
1332 data = ((bKinematicConstraint*)con->data);
1333 data->tar = newlibadr(fd, id->lib, data->tar);
1336 case CONSTRAINT_TYPE_TRACKTO:
1338 bTrackToConstraint *data;
1339 data = ((bTrackToConstraint*)con->data);
1340 data->tar = newlibadr(fd, id->lib, data->tar);
1343 case CONSTRAINT_TYPE_LOCKTRACK:
1345 bLockTrackConstraint *data;
1346 data= ((bLockTrackConstraint*)con->data);
1347 data->tar = newlibadr(fd, id->lib, data->tar);
1350 case CONSTRAINT_TYPE_FOLLOWPATH:
1352 bFollowPathConstraint *data;
1353 data= ((bFollowPathConstraint*)con->data);
1354 data->tar = newlibadr(fd, id->lib, data->tar);
1357 case CONSTRAINT_TYPE_DISTANCELIMIT:
1359 bDistanceLimitConstraint *data;
1360 data= ((bDistanceLimitConstraint*)con->data);
1361 data->tar = newlibadr(fd, id->lib, data->tar);
1364 case CONSTRAINT_TYPE_STRETCHTO:
1366 bStretchToConstraint *data;
1367 data= ((bStretchToConstraint*)con->data);
1368 data->tar = newlibadr(fd, id->lib, data->tar);
1372 case CONSTRAINT_TYPE_NULL:
1378 static void direct_link_constraints(FileData *fd, ListBase *lb)
1383 for (cons=lb->first; cons; cons=cons->next) {
1384 cons->data = newdataadr(fd, cons->data);
1385 switch (cons->type) {
1393 static void lib_link_bone(FileData *fd, ID *id, Bone *bone)
1397 // lib_link_constraints(fd, id, &bone->constraints);
1399 for (curBone=bone->childbase.first; curBone; curBone=curBone->next) {
1400 lib_link_bone(fd, id, curBone);
1405 static void lib_link_pose(FileData *fd, ID *id, bPose *pose)
1412 for (chan = pose->chanbase.first; chan; chan=chan->next) {
1413 lib_link_constraints(fd, id, &chan->constraints);
1417 static void lib_link_armature(FileData *fd, Main *main)
1422 arm= main->armature.first;
1425 if(arm->id.flag & LIB_NEEDLINK) {
1426 arm->id.flag -= LIB_NEEDLINK;
1429 for (bone=arm->bonebase.first; bone; bone=bone->next) {
1430 lib_link_bone(fd, &arm->id, bone);
1437 static void lib_link_action(FileData *fd, Main *main)
1440 bActionChannel *chan;
1442 act= main->action.first;
1444 if(act->id.flag & LIB_NEEDLINK) {
1445 act->id.flag -= LIB_NEEDLINK;
1447 for (chan=act->chanbase.first; chan; chan=chan->next) {
1448 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1449 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1457 static void direct_link_bones(FileData *fd, Bone* bone)
1461 bone->parent= newdataadr(fd, bone->parent);
1463 link_list(fd, &bone->childbase);
1465 for (child=bone->childbase.first; child; child=child->next) {
1466 direct_link_bones(fd, child);
1471 static void direct_link_action(FileData *fd, bAction *act)
1473 bActionChannel *achan;
1475 link_list(fd, &act->chanbase);
1477 for (achan = act->chanbase.first; achan; achan=achan->next)
1478 link_list(fd, &achan->constraintChannels);
1482 static void direct_link_armature(FileData *fd, bArmature *arm)
1486 link_list(fd, &arm->bonebase);
1488 bone=arm->bonebase.first;
1490 direct_link_bones(fd, bone);
1495 /* ************ READ CAMERA ***************** */
1497 static void lib_link_camera(FileData *fd, Main *main)
1501 ca= main->camera.first;
1503 if(ca->id.flag & LIB_NEEDLINK) {
1505 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo);
1507 lib_link_scriptlink(fd, &ca->id, &ca->scriptlink);
1509 ca->id.flag -= LIB_NEEDLINK;
1515 static void direct_link_camera(FileData *fd, Camera *ca)
1517 direct_link_scriptlink(fd, &ca->scriptlink);
1521 /* ************ READ LATTICE ***************** */
1523 static void lib_link_latt(FileData *fd, Main *main)
1527 lt= main->latt.first;
1529 if(lt->id.flag & LIB_NEEDLINK) {
1531 lt->ipo= newlibadr_us(fd, lt->id.lib, lt->ipo);
1532 lt->key= newlibadr_us(fd, lt->id.lib, lt->key);
1534 lt->id.flag -= LIB_NEEDLINK;
1540 static void direct_link_latt(FileData *fd, Lattice *lt)
1542 lt->def= newdataadr(fd, lt->def);
1545 /* ************ READ LAMP ***************** */
1547 static void lib_link_lamp(FileData *fd, Main *main)
1553 la= main->lamp.first;
1555 if(la->id.flag & LIB_NEEDLINK) {
1557 for(a=0; a<MAX_MTEX; a++) {
1560 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
1561 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
1565 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo);
1567 lib_link_scriptlink(fd, &la->id, &la->scriptlink);
1569 la->id.flag -= LIB_NEEDLINK;
1575 static void direct_link_lamp(FileData *fd, Lamp *la)
1579 direct_link_scriptlink(fd, &la->scriptlink);
1581 for(a=0; a<MAX_MTEX; a++) {
1582 la->mtex[a]= newdataadr(fd, la->mtex[a]);
1586 /* ************ READ keys ***************** */
1588 static void lib_link_key(FileData *fd, Main *main)
1592 key= main->key.first;
1594 if(key->id.flag & LIB_NEEDLINK) {
1596 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo);
1597 key->from= newlibadr(fd, key->id.lib, key->from);
1599 key->id.flag -= LIB_NEEDLINK;
1605 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
1608 char *data, *poin, *cp;
1610 elemsize= key->elemsize;
1613 for(a=0; a<kb->totelem; a++) {
1618 while( cp[0] ) { /* cp[0]==amount */
1620 switch(cp[1]) { /* cp[1]= type */
1626 SWITCH_INT((*poin));
1639 static void direct_link_key(FileData *fd, Key *key)
1643 link_list(fd, &(key->block));
1645 key->refkey= newdataadr(fd, key->refkey);
1647 kb= key->block.first;
1650 kb->data= newdataadr(fd, kb->data);
1652 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
1653 switch_endian_keyblock(key, kb);
1659 /* ************ READ mball ***************** */
1661 static void lib_link_mball(FileData *fd, Main *main)
1666 mb= main->mball.first;
1668 if(mb->id.flag & LIB_NEEDLINK) {
1670 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
1672 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo);
1674 mb->id.flag -= LIB_NEEDLINK;
1680 static void direct_link_mball(FileData *fd, MetaBall *mb)
1682 mb->mat= newdataadr(fd, mb->mat);
1683 test_pointer_array(fd, (void **)&mb->mat);
1685 link_list(fd, &(mb->elems));
1687 mb->disp.first= mb->disp.last= 0;
1692 /* ************ READ WORLD ***************** */
1694 static void lib_link_world(FileData *fd, Main *main)
1700 wrld= main->world.first;
1702 if(wrld->id.flag & LIB_NEEDLINK) {
1704 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo);
1706 for(a=0; a<MAX_MTEX; a++) {
1707 mtex= wrld->mtex[a];
1709 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
1710 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
1714 lib_link_scriptlink(fd, &wrld->id, &wrld->scriptlink);
1716 wrld->id.flag -= LIB_NEEDLINK;
1718 wrld= wrld->id.next;
1722 static void direct_link_world(FileData *fd, World *wrld)
1726 direct_link_scriptlink(fd, &wrld->scriptlink);
1728 for(a=0; a<MAX_MTEX; a++) {
1729 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
1734 /* ************ READ IPO ***************** */
1736 static void lib_link_ipo(FileData *fd, Main *main)
1740 ipo= main->ipo.first;
1742 if(ipo->id.flag & LIB_NEEDLINK) {
1744 ipo->id.flag -= LIB_NEEDLINK;
1750 static void direct_link_ipo(FileData *fd, Ipo *ipo)
1754 link_list(fd, &(ipo->curve));
1755 icu= ipo->curve.first;
1757 icu->bezt= newdataadr(fd, icu->bezt);
1758 icu->bp= newdataadr(fd, icu->bp);
1763 /* ************ READ VFONT ***************** */
1765 static void lib_link_vfont(FileData *fd, Main *main)
1769 vf= main->vfont.first;
1771 if(vf->id.flag & LIB_NEEDLINK) {
1772 vf->id.flag -= LIB_NEEDLINK;
1778 static void direct_link_vfont(FileData *fd, VFont *vf)
1781 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
1784 /* ************ READ TEXT ****************** */
1786 static void lib_link_text(FileData *fd, Main *main)
1790 text= main->text.first;
1792 if(text->id.flag & LIB_NEEDLINK) {
1793 text->id.flag -= LIB_NEEDLINK;
1795 text= text->id.next;
1799 static void direct_link_text(FileData *fd, Text *text)
1803 text->name= newdataadr(fd, text->name);
1806 text->undo_len= TXT_INIT_UNDO;
1807 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
1809 text->compiled= NULL;
1812 if(text->flags & TXT_ISEXT) {
1817 link_list(fd, &text->lines);
1819 text->curl= newdataadr(fd, text->curl);
1820 text->sell= newdataadr(fd, text->sell);
1822 ln= text->lines.first;
1824 ln->line= newdataadr(fd, ln->line);
1826 if (ln->len != (int) strlen(ln->line)) {
1827 printf("Error loading text, line lengths differ\n");
1828 ln->len = strlen(ln->line);
1834 text->flags = (text->flags|TXT_ISTMP) & ~TXT_ISEXT;
1839 /* ************ READ IMAGE ***************** */
1841 static void lib_link_image(FileData *fd, Main *main)
1845 ima= main->image.first;
1847 if(ima->id.flag & LIB_NEEDLINK) {
1849 ima->id.flag -= LIB_NEEDLINK;
1855 static void direct_link_image(FileData *fd, Image *ima)
1859 memset(ima->mipmap, 0, sizeof(ima->mipmap));
1863 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
1869 /* ************ READ CURVE ***************** */
1871 static void lib_link_curve(FileData *fd, Main *main)
1876 cu= main->curve.first;
1878 if(cu->id.flag & LIB_NEEDLINK) {
1880 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
1882 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
1883 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
1884 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
1885 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
1887 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo);
1888 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
1890 cu->id.flag -= LIB_NEEDLINK;
1897 static void switch_endian_knots(Nurb *nu)
1904 SWITCH_INT(nu->knotsu[len]);
1910 SWITCH_INT(nu->knotsv[len]);
1915 static void direct_link_curve(FileData *fd, Curve *cu)
1919 cu->mat= newdataadr(fd, cu->mat);
1920 test_pointer_array(fd, (void **)&cu->mat);
1921 cu->str= newdataadr(fd, cu->str);
1923 if(cu->vfont==0) link_list(fd, &(cu->nurb));
1925 cu->nurb.first=cu->nurb.last= 0;
1928 cu->bev.first=cu->bev.last= 0;
1929 cu->disp.first=cu->disp.last= 0;
1934 nu->bezt= newdataadr(fd, nu->bezt);
1935 nu->bp= newdataadr(fd, nu->bp);
1936 nu->knotsu= newdataadr(fd, nu->knotsu);
1937 nu->knotsv= newdataadr(fd, nu->knotsv);
1939 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1940 switch_endian_knots(nu);
1948 /* ************ READ TEX ***************** */
1950 static void lib_link_texture(FileData *fd, Main *main)
1954 tex= main->tex.first;
1956 if(tex->id.flag & LIB_NEEDLINK) {
1958 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
1959 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
1960 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
1962 tex->id.flag -= LIB_NEEDLINK;
1968 static void direct_link_texture(FileData *fd, Tex *tex)
1970 tex->plugin= newdataadr(fd, tex->plugin);
1972 tex->plugin->handle= 0;
1973 open_plugin_tex(tex->plugin);
1975 tex->coba= newdataadr(fd, tex->coba);
1976 tex->env= newdataadr(fd, tex->env);
1979 memset(tex->env->cube, 0, 6*sizeof(void *));
1986 /* ************ READ MATERIAL ***************** */
1988 static void lib_link_material(FileData *fd, Main *main)
1994 ma= main->mat.first;
1996 if(ma->id.flag & LIB_NEEDLINK) {
1998 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
2000 for(a=0; a<MAX_MTEX; a++) {
2003 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
2004 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
2007 lib_link_scriptlink(fd, &ma->id, &ma->scriptlink);
2008 ma->id.flag -= LIB_NEEDLINK;
2014 static void direct_link_material(FileData *fd, Material *ma)
2018 direct_link_scriptlink(fd, &ma->scriptlink);
2020 for(a=0; a<MAX_MTEX; a++) {
2021 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
2024 ma->ramp_col= newdataadr(fd, ma->ramp_col);
2025 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
2029 /* ************ READ MESH ***************** */
2031 static void lib_link_mesh(FileData *fd, Main *main)
2035 me= main->mesh.first;
2037 if(me->id.flag & LIB_NEEDLINK) {
2040 /* this check added for python created meshes */
2042 for(i=0; i<me->totcol; i++)
2043 me->mat[i]= newlibadr_us(fd, me->id.lib, me->mat[i]);
2047 me->ipo= newlibadr_us(fd, me->id.lib, me->ipo);
2048 me->key= newlibadr_us(fd, me->id.lib, me->key);
2049 me->texcomesh= newlibadr_us(fd, me->id.lib, me->texcomesh);
2052 TFace *tfaces= me->tface;
2054 for (i=0; i<me->totface; i++) {
2055 TFace *tf= &tfaces[i];
2057 tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
2059 Image *ima= tf->tpage;
2065 me->id.flag -= LIB_NEEDLINK;
2071 static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
2078 for (i=0; i<count; i++) {
2079 mdverts[i].dw=newdataadr(fd, mdverts[i].dw);
2081 mdverts[i].totweight=0;
2083 for (j=0; j< mdverts[i].totweight; j++) {
2084 mdverts[i].dw[j].data = NULL; // not saved in file, clear pointer
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);
2104 mesh->disp.first= mesh->disp.last= NULL;
2109 mesh->decimated= NULL;
2110 mesh->derived= NULL;
2113 TFace *tfaces= mesh->tface;
2116 for (i=0; i<mesh->totface; i++) {
2117 TFace *tf= &tfaces[i];
2119 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2120 SWITCH_INT(tf->col[0]);
2121 SWITCH_INT(tf->col[1]);
2122 SWITCH_INT(tf->col[2]);
2123 SWITCH_INT(tf->col[3]);
2129 /* ************ READ OBJECT ***************** */
2131 static void lib_link_object(FileData *fd, Main *main)
2141 ob= main->object.first;
2143 if(ob->id.flag & LIB_NEEDLINK) {
2145 ob->parent= newlibadr(fd, ob->id.lib, ob->parent);
2146 ob->track= newlibadr(fd, ob->id.lib, ob->track);
2147 ob->ipo= newlibadr_us(fd, ob->id.lib, ob->ipo);
2148 ob->action = newlibadr_us(fd, ob->id.lib, ob->action);
2150 // ob->activecon = newglobadr(fd, ob->activecon);
2153 ob->data= newlibadr_us(fd, ob->id.lib, ob->data);
2155 if(ob->data==NULL && poin!=NULL) {
2158 if(ob->id.lib) printf("Can't find obdata of %s lib %s\n", ob->id.name+2, ob->id.lib->name);
2159 else printf("Object %s lost data. Lib:%x\n", ob->id.name+2, (unsigned int) ob->id.lib);
2161 for(a=0; a<ob->totcol; a++) ob->mat[a]= newlibadr_us(fd, ob->id.lib, ob->mat[a]);
2163 ob->id.flag -= LIB_NEEDLINK;
2164 /* if id.us==0 a new base will be created later on */
2166 /* WARNING! Also check expand_object(), should reflect the stuff below. */
2167 lib_link_pose(fd, &ob->id, ob->pose);
2168 lib_link_constraints(fd, &ob->id, &ob->constraints);
2169 lib_link_nlastrips(fd, &ob->id, &ob->nlastrips);
2170 lib_link_constraint_channels(fd, &ob->id, &ob->constraintChannels);
2173 sens= ob->sensors.first;
2175 if(ob->id.lib==NULL) { // done in expand_main
2176 for(a=0; a<sens->totlinks; a++) {
2177 sens->links[a]= newglobadr(fd, sens->links[a]);
2180 if(sens->type==SENS_TOUCH) {
2181 bTouchSensor *ts= sens->data;
2182 ts->ma= newlibadr(fd, ob->id.lib, ts->ma);
2184 else if(sens->type==SENS_MESSAGE) {
2185 bMessageSensor *ms= sens->data;
2187 newlibadr(fd, ob->id.lib, ms->fromObject);
2192 cont= ob->controllers.first;
2194 if(ob->id.lib==NULL) { // done in expand_main
2195 for(a=0; a<cont->totlinks; a++) {
2196 cont->links[a]= newglobadr(fd, cont->links[a]);
2199 if(cont->type==CONT_PYTHON) {
2200 bPythonCont *pc= cont->data;
2201 pc->text= newlibadr(fd, ob->id.lib, pc->text);
2209 act= ob->actuators.first;
2211 if(act->type==ACT_SOUND) {
2212 bSoundActuator *sa= act->data;
2213 sa->sound= newlibadr_us(fd, ob->id.lib, sa->sound);
2215 else if(act->type==ACT_CD) {
2216 /* bCDActuator *cda= act->data; */
2218 else if(act->type==ACT_GAME) {
2219 /* bGameActuator *ga= act->data; */
2221 else if(act->type==ACT_CAMERA) {
2222 bCameraActuator *ca= act->data;
2223 ca->ob= newlibadr(fd, ob->id.lib, ca->ob);
2225 /* leave this one, it's obsolete but necessary to read for conversion */
2226 else if(act->type==ACT_ADD_OBJECT) {
2227 bAddObjectActuator *eoa= act->data;
2228 if(eoa) eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
2230 else if(act->type==ACT_EDIT_OBJECT) {
2231 bEditObjectActuator *eoa= act->data;
2235 eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
2236 eoa->me= newlibadr(fd, ob->id.lib, eoa->me);
2238 else if(act->type==ACT_SCENE) {
2239 bSceneActuator *sa= act->data;
2240 sa->camera= newlibadr(fd, ob->id.lib, sa->camera);
2241 sa->scene= newlibadr(fd, ob->id.lib, sa->scene);
2243 else if(act->type==ACT_ACTION) {
2244 bActionActuator *aa= act->data;
2245 aa->act= newlibadr(fd, ob->id.lib, aa->act);
2247 else if(act->type==ACT_PROPERTY) {
2248 bPropertyActuator *pa= act->data;
2249 pa->ob= newlibadr(fd, ob->id.lib, pa->ob);
2251 else if(act->type==ACT_MESSAGE) {
2252 bMessageActuator *ma= act->data;
2253 ma->toObject= newlibadr(fd, ob->id.lib, ma->toObject);
2258 lib_link_scriptlink(fd, &ob->id, &ob->scriptlink);
2260 for(hook= ob->hooks.first; hook; hook= hook->next) {
2261 hook->parent= newlibadr(fd, ob->id.lib, hook->parent);
2267 if(warn) error("WARNING IN CONSOLE");
2271 static void direct_link_pose(FileData *fd, bPose *pose) {
2278 link_list(fd, &pose->chanbase);
2280 for (chan = pose->chanbase.first; chan; chan=chan->next) {
2281 direct_link_constraints(fd, &chan->constraints);
2286 static void direct_link_object(FileData *fd, Object *ob)
2296 ob->disp.first=ob->disp.last= NULL;
2298 ob->pose= newdataadr(fd, ob->pose);
2299 direct_link_pose(fd, ob->pose);
2301 link_list(fd, &ob->defbase);
2302 link_list(fd, &ob->nlastrips);
2303 link_list(fd, &ob->constraintChannels);
2305 ob->activecon = newdataadr(fd, ob->activecon);
2307 direct_link_scriptlink(fd, &ob->scriptlink);
2309 ob->mat= newdataadr(fd, ob->mat);
2310 test_pointer_array(fd, (void **)&ob->mat);
2311 link_list(fd, &ob->effect);
2312 paf= ob->effect.first;
2314 if(paf->type==EFF_PARTICLE) {
2317 if(paf->type==EFF_WAVE) {
2323 ob->pd= newdataadr(fd, ob->pd);
2324 ob->soft= newdataadr(fd, ob->soft);
2326 SoftBody *sb= ob->soft;
2328 sb->bpoint= NULL; // init pointers so it gets rebuilt nicely
2331 sb->keys= newdataadr(fd, sb->keys);
2332 test_pointer_array(fd, (void **)&sb->keys);
2334 for(a=0; a<sb->totkey; a++) {
2335 sb->keys[a]= newdataadr(fd, sb->keys[a]);
2340 link_list(fd, &ob->prop);
2341 prop= ob->prop.first;
2343 prop->poin= newdataadr(fd, prop->poin);
2344 if(prop->poin==0) prop->poin= &prop->data;
2348 link_list(fd, &ob->sensors);
2349 sens= ob->sensors.first;
2351 sens->data= newdataadr(fd, sens->data);
2352 sens->links= newdataadr(fd, sens->links);
2353 test_pointer_array(fd, (void **)&sens->links);
2357 direct_link_constraints(fd, &ob->constraints);
2359 link_glob_list(fd, &ob->controllers);
2360 cont= ob->controllers.first;
2362 cont->data= newdataadr(fd, cont->data);
2363 cont->links= newdataadr(fd, cont->links);
2364 test_pointer_array(fd, (void **)&cont->links);
2368 link_glob_list(fd, &ob->actuators);
2369 act= ob->actuators.first;
2371 act->data= newdataadr(fd, act->data);
2375 link_list(fd, &ob->hooks);
2376 for(hook= ob->hooks.first; hook; hook= hook->next) {
2377 hook->indexar= newdataadr(fd, hook->indexar);
2378 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2380 for(a=0; a<hook->totindex; a++) {
2381 SWITCH_INT(hook->indexar[a]);
2389 /* ************ READ SCENE ***************** */
2391 static void lib_link_scene(FileData *fd, Main *main)
2398 sce= main->scene.first;
2400 if(sce->id.flag & LIB_NEEDLINK) {
2402 sce->camera= newlibadr(fd, sce->id.lib, sce->camera);
2403 sce->world= newlibadr_us(fd, sce->id.lib, sce->world);
2404 sce->set= newlibadr(fd, sce->id.lib, sce->set);
2405 sce->ima= newlibadr_us(fd, sce->id.lib, sce->ima);
2406 sce->group= newlibadr_us(fd, sce->id.lib, sce->group);
2408 base= sce->base.first;
2412 /* base->object= newlibadr_us(fd, sce->id.lib, base->object); */
2413 base->object= newlibadr_us_type(fd, ID_OB, base->object);
2415 /* when save during radiotool, needs cleared */
2416 base->flag &= ~OB_RADIO;
2418 if(base->object==0) {
2419 printf("LIB ERROR: base removed\n");
2420 BLI_remlink(&sce->base, base);
2421 if(base==sce->basact) sce->basact= 0;
2429 WHILE_SEQ(&ed->seqbase) {
2430 if(seq->ipo) seq->ipo= newlibadr_us(fd, sce->id.lib, seq->ipo);
2431 if(seq->scene) seq->scene= newlibadr(fd, sce->id.lib, seq->scene);
2433 seq->sound= newlibadr(fd, sce->id.lib, seq->sound);
2435 seq->sound->id.us++;
2436 seq->sound->flags |= SOUND_FLAGS_SEQUENCE;
2444 lib_link_scriptlink(fd, &sce->id, &sce->scriptlink);
2446 sce->id.flag -= LIB_NEEDLINK;
2453 static void link_recurs_seq(FileData *fd, ListBase *lb)
2460 if(seq->seqbase.first) link_recurs_seq(fd, &seq->seqbase);
2465 static void direct_link_scene(FileData *fd, Scene *sce)
2473 link_list(fd, &(sce->base));
2475 sce->basact= newdataadr(fd, sce->basact);
2477 sce->radio= newdataadr(fd, sce->radio);
2479 sce->r.avicodecdata = newdataadr(fd, sce->r.avicodecdata);
2480 if (sce->r.avicodecdata) {
2481 sce->r.avicodecdata->lpFormat = newdataadr(fd, sce->r.avicodecdata->lpFormat);
2482 sce->r.avicodecdata->lpParms = newdataadr(fd, sce->r.avicodecdata->lpParms);
2485 sce->r.qtcodecdata = newdataadr(fd, sce->r.qtcodecdata);
2486 if (sce->r.qtcodecdata) {
2487 sce->r.qtcodecdata->cdParms = newdataadr(fd, sce->r.qtcodecdata->cdParms);
2491 ed= sce->ed= newdataadr(fd, sce->ed);
2493 /* recursive link sequences, lb will be correctly initialized */
2494 link_recurs_seq(fd, &ed->seqbase);
2496 WHILE_SEQ(&ed->seqbase) {
2497 seq->seq1= newdataadr(fd, seq->seq1);
2498 seq->seq2= newdataadr(fd, seq->seq2);
2499 seq->seq3= newdataadr(fd, seq->seq3);
2500 /* a patch: after introduction of effects with 3 input strips */
2501 if(seq->seq3==0) seq->seq3= seq->seq2;
2505 seq->plugin= newdataadr(fd, seq->plugin);
2506 if(seq->plugin) open_plugin_seq(seq->plugin, seq->name+2);
2508 seq->effectdata= newdataadr(fd, seq->effectdata);
2510 seq->strip= newdataadr(fd, seq->strip);
2511 if(seq->strip && seq->strip->done==0) {
2512 seq->strip->done= 1;
2514 /* standard: strips from effects/metas are not written, but are mallocced */
2516 if(seq->type==SEQ_IMAGE) {
2517 seq->strip->stripdata= newdataadr(fd, seq->strip->stripdata);
2518 se= seq->strip->stripdata;
2520 for(a=0; a<seq->strip->len; a++, se++) {
2526 else if(seq->type==SEQ_MOVIE) {
2527 /* only first stripelem is in file */
2528 se= newdataadr(fd, seq->strip->stripdata);
2531 seq->strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
2532 *seq->strip->stripdata= *se;
2535 se= seq->strip->stripdata;
2537 for(a=0; a<seq->strip->len; a++, se++) {
2544 else if(seq->type==SEQ_SOUND) {
2545 /* only first stripelem is in file */
2546 se= newdataadr(fd, seq->strip->stripdata);
2549 seq->strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
2550 *seq->strip->stripdata= *se;
2553 se= seq->strip->stripdata;
2555 for(a=0; a<seq->strip->len; a++, se++) {
2556 se->ok= 2; /* why? */
2563 seq->strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
2569 /* link metastack, slight abuse of structs here, have to restore pointer to internal part in struct */
2574 /*int seted=0;*/ /*unused*/
2576 offset= ((long)&(temp.seqbase)) - ((long)&temp);
2579 poin= (char *)ed->seqbasep;
2581 poin= newdataadr(fd, poin);
2582 if(poin) ed->seqbasep= (ListBase *)(poin+offset);
2583 else ed->seqbasep= &ed->seqbase;
2586 link_list(fd, &(ed->metastack));
2588 for(ms= ed->metastack.first; ms; ms= ms->next) {
2589 ms->parseq= newdataadr(fd, ms->parseq);
2591 poin= (char *)ms->oldbasep;
2593 poin= newdataadr(fd, poin);
2594 if(poin) ms->oldbasep= (ListBase *)(poin+offset);
2595 else ms->oldbasep= &ed->seqbase;
2600 direct_link_scriptlink(fd, &sce->scriptlink);
2603 /* ************ READ SCREEN ***************** */
2605 /* note: file read without screens option G_FILE_NO_UI;
2606 check lib pointers in call below */
2607 static void lib_link_screen(FileData *fd, Main *main)
2612 sc= main->screen.first;
2614 if(sc->id.flag & LIB_NEEDLINK) {
2616 sc->scene= newlibadr(fd, sc->id.lib, sc->scene);
2618 sa= sc->areabase.first;
2622 sa->full= newlibadr(fd, sc->id.lib, sa->full);
2624 for (sl= sa->spacedata.first; sl; sl= sl->next) {
2625 if(sl->spacetype==SPACE_VIEW3D) {
2626 View3D *v3d= (View3D*) sl;
2628 v3d->camera= newlibadr(fd, sc->id.lib, v3d->camera);
2631 v3d->bgpic->ima= newlibadr_us(fd, sc->id.lib, v3d->bgpic->ima);
2632 v3d->bgpic->tex= newlibadr_us(fd, sc->id.lib, v3d->bgpic->tex);
2633 v3d->bgpic->rect= NULL;
2636 v3d->localvd->camera= newlibadr(fd, sc->id.lib, v3d->localvd->camera);
2639 else if(sl->spacetype==SPACE_IPO) {
2640 SpaceIpo *sipo= (SpaceIpo *)sl;
2643 if(sipo->blocktype==ID_SEQ) sipo->from= NULL; // no libdata
2644 else sipo->from= newlibadr(fd, sc->id.lib, sipo->from);
2646 sipo->ipokey.first= sipo->ipokey.last= 0;
2647 sipo->ipo= newlibadr(fd, sc->id.lib, sipo->ipo);
2649 else if(sl->spacetype==SPACE_BUTS) {
2650 SpaceButs *sbuts= (SpaceButs *)sl;
2652 sbuts->lockpoin= NULL;
2653 if(main->versionfile<132) set_rects_butspace(sbuts);
2655 else if(sl->spacetype==SPACE_FILE) {
2656 SpaceFile *sfile= (SpaceFile *)sl;
2659 sfile->libfiledata= 0;
2660 sfile->returnfunc= 0;
2662 else if(sl->spacetype==SPACE_IMASEL) {
2663 check_imasel_copy((SpaceImaSel *)sl);
2665 else if(sl->spacetype==SPACE_ACTION) {
2666 SpaceAction *saction= (SpaceAction *)sl;
2667 saction->action = newlibadr(fd, sc->id.lib, saction->action);
2669 else if(sl->spacetype==SPACE_IMAGE) {
2670 SpaceImage *sima= (SpaceImage *)sl;
2672 sima->image= newlibadr_us(fd, sc->id.lib, sima->image);
2674 else if(sl->spacetype==SPACE_NLA){
2675 /* SpaceNla *snla= (SpaceNla *)sl; */
2677 else if(sl->spacetype==SPACE_TEXT) {
2678 SpaceText *st= (SpaceText *)sl;
2680 st->text= newlibadr(fd, sc->id.lib, st->text);
2683 else if(sl->spacetype==SPACE_SCRIPT) {
2684 SpaceScript *sc= (SpaceScript *)sl;
2688 else if(sl->spacetype==SPACE_OOPS) {
2689 SpaceOops *so= (SpaceOops *)sl;
2691 TreeStoreElem *tselem;
2694 oops= so->oops.first;
2696 oops->id= newlibadr(fd, NULL, oops->id);
2700 so->tree.first= so->tree.last= NULL;
2703 tselem= so->treestore->data;
2704 for(a=0; a<so->treestore->usedelem; a++, tselem++) {
2705 tselem->id= newlibadr(fd, NULL, tselem->id);
2709 else if(sl->spacetype==SPACE_SOUND) {
2710 SpaceSound *ssound= (SpaceSound *)sl;
2712 ssound->sound= newlibadr_us(fd, sc->id.lib, ssound->sound);
2717 sc->id.flag -= LIB_NEEDLINK;
2723 static void *restore_pointer_by_name(Main *mainp, ID *id, int user)
2729 lb= wich_libbase(mainp, GS(id->name));
2731 if(lb) { // there's still risk of checking corrupt mem (freed Ids in oops)
2734 if( strcmp(idn->name, id->name)==0) {
2735 if(user && idn->us==0) idn->us++;
2745 /* called from kernel/blender.c */
2746 void lib_link_screen_restore(Main *newmain, char mode, Scene *curscene)
2751 sc= newmain->screen.first;
2754 if(mode=='u') sc->scene= restore_pointer_by_name(newmain, (ID *)sc->scene, 1);
2755 if(sc->scene==NULL || mode=='n') sc->scene= curscene;
2757 sa= sc->areabase.first;
2761 for (sl= sa->spacedata.first; sl; sl= sl->next) {
2762 if(sl->spacetype==SPACE_VIEW3D) {
2763 View3D *v3d= (View3D*) sl;
2765 if(mode=='u') v3d->camera= restore_pointer_by_name(newmain, (ID *)v3d->camera, 1);
2766 if(v3d->camera==NULL || mode=='n') v3d->camera= sc->scene->camera;
2769 v3d->bgpic->ima= restore_pointer_by_name(newmain, (ID *)v3d->bgpic->ima, 1);
2770 v3d->bgpic->tex= restore_pointer_by_name(newmain, (ID *)v3d->bgpic->tex, 1);
2771 if(v3d->bgpic->rect) freeN(v3d->bgpic->rect);
2772 v3d->bgpic->rect= NULL;
2776 if(mode=='u') v3d->localvd->camera= restore_pointer_by_name(newmain, (ID *)v3d->localvd->camera, 1);
2777 if(v3d->localvd->camera==NULL || mode=='n') v3d->localvd->camera= sc->scene->camera;
2779 /* localview can become invalid during undo/redo steps, so we exit it when no could be found */
2780 for(base= sc->scene->base.first; base; base= base->next) {
2781 if(base->lay & v3d->lay) break;
2784 v3d->lay= v3d->localvd->lay;
2785 v3d->layact= v3d->localvd->layact;
2786 MEM_freeN(v3d->localvd);
2791 else if(v3d->scenelock) v3d->lay= sc->scene->lay;
2794 else if(sl->spacetype==SPACE_IPO) {
2795 SpaceIpo *sipo= (SpaceIpo *)sl;
2797 if(sipo->blocktype==ID_SEQ) sipo->from= NULL; // no libdata
2798 else sipo->from= restore_pointer_by_name(newmain, (ID *)sipo->from, 0);
2800 // not free sipo->ipokey, creates dependency with src/
2801 sipo->ipo= restore_pointer_by_name(newmain, (ID *)sipo->ipo, 0);
2802 if(sipo->editipo) MEM_freeN(sipo->editipo);
2803 sipo->editipo= NULL;
2805 else if(sl->spacetype==SPACE_BUTS) {
2806 SpaceButs *sbuts= (SpaceButs *)sl;
2807 sbuts->lockpoin= NULL;
2808 sbuts->cury= 0; // we leave rect, for nicer redraws
2810 else if(sl->spacetype==SPACE_FILE) {
2811 SpaceFile *sfile= (SpaceFile *)sl;
2812 if(sfile->libfiledata)
2813 BLO_blendhandle_close(sfile->libfiledata);
2814 sfile->libfiledata= 0;
2816 else if(sl->spacetype==SPACE_IMASEL) {
2819 else if(sl->spacetype==SPACE_ACTION) {
2820 SpaceAction *saction= (SpaceAction *)sl;
2821 saction->action = restore_pointer_by_name(newmain, (ID *)saction->action, 1);
2823 else if(sl->spacetype==SPACE_IMAGE) {
2824 SpaceImage *sima= (SpaceImage *)sl;
2826 sima->image= restore_pointer_by_name(newmain, (ID *)sima->image, 1);
2828 else if(sl->spacetype==SPACE_NLA){
2829 /* SpaceNla *snla= (SpaceNla *)sl; */
2831 else if(sl->spacetype==SPACE_TEXT) {
2832 SpaceText *st= (SpaceText *)sl;
2834 st->text= restore_pointer_by_name(newmain, (ID *)st->text, 1);
2835 if(st->text==NULL) st->text= newmain->text.first;
2837 else if(sl->spacetype==SPACE_SCRIPT) {
2838 SpaceScript *sc= (SpaceScript *)sl;
2842 else if(sl->spacetype==SPACE_OOPS) {
2843 SpaceOops *so= (SpaceOops *)sl;
2847 oops= so->oops.first;
2849 oops->id= restore_pointer_by_name(newmain, (ID *)oops->id, 0);
2855 TreeStore *ts= so->treestore;
2856 TreeStoreElem *tselem=ts->data;
2857 for(a=0; a<ts->usedelem; a++, tselem++) {
2858 tselem->id= restore_pointer_by_name(newmain, tselem->id, 0);
2862 else if(sl->spacetype==SPACE_SOUND) {
2863 SpaceSound *ssound= (SpaceSound *)sl;
2865 ssound->sound= restore_pointer_by_name(newmain, (ID *)ssound->sound, 1);
2876 static void direct_link_screen(FileData *fd, bScreen *sc)
2883 link_list(fd, &(sc->vertbase));
2884 link_list(fd, &(sc->edgebase));
2885 link_list(fd, &(sc->areabase));
2889 se= sc->edgebase.first;
2891 se->v1= newdataadr(fd, se->v1);
2892 se->v2= newdataadr(fd, se->v2);
2893 if( (long)se->v1 > (long)se->v2) {
2900 printf("error reading screen... file corrupt\n");
2907 sa= sc->areabase.first;
2912 link_list(fd, &(sa->spacedata));
2913 link_list(fd, &(sa->panels));
2915 for(pa= sa->panels.first; pa; pa=pa->next) {
2916 pa->paneltab= newdataadr(fd, pa->paneltab);
2921 for (sl= sa->spacedata.first; sl; sl= sl->next) {
2922 if (sl->spacetype==SPACE_VIEW3D) {
2923 View3D *v3d= (View3D*) sl;
2924 v3d->bgpic= newdataadr(fd, v3d->bgpic);
2925 v3d->localvd= newdataadr(fd, v3d->localvd);
2927 else if (sl->spacetype==SPACE_OOPS) {
2928 SpaceOops *soops= (SpaceOops*) sl;
2930 link_list(fd, &(soops->oops));
2931 oops= soops->oops.first;
2933 oops->link.first= oops->link.last= 0;
2937 soops->treestore= newdataadr(fd, soops->treestore);
2938 if(soops->treestore) {
2939 soops->treestore->data= newdataadr(fd, soops->treestore->data);
2940 /* we only saved what was used */
2941 soops->treestore->totelem= soops->treestore->usedelem;
2942 soops->storeflag |= SO_TREESTORE_CLEANUP; // at first draw
2947 sa->v1= newdataadr(fd, sa->v1);
2948 sa->v2= newdataadr(fd, sa->v2);
2949 sa->v3= newdataadr(fd, sa->v3);
2950 sa->v4= newdataadr(fd, sa->v4);
2952 sa->win= sa->headwin= 0;
2954 sa->uiblocks.first= sa->uiblocks.last= NULL;
2960 /* ********** READ LIBRARY *************** */
2963 static void direct_link_library(FileData *fd, Library *lib)
2968 newmain= MEM_callocN(sizeof(Main), "directlink");
2969 BLI_addtail(&fd->mainlist, newmain);
2970 newmain->curlib= lib;
2973 static void lib_link_library(FileData *fd, Main *main)
2977 lib= main->library.first;
2984 /* ************** READ SOUND ******************* */
2986 static void direct_link_sound(FileData *fd, bSound *sound)
2988 sound->sample = NULL;
2989 sound->snd_sound = NULL;
2991 sound->packedfile = direct_link_packedfile(fd, sound->packedfile);
2992 sound->newpackedfile = direct_link_packedfile(fd, sound->newpackedfile);
2995 static void lib_link_sound(FileData *fd, Main *main)
2999 sound= main->sound.first;
3001 if(sound->id.flag & LIB_NEEDLINK) {
3002 sound->id.flag -= LIB_NEEDLINK;
3003 sound->ipo= newlibadr_us(fd, sound->id.lib, sound->ipo);
3006 sound= sound->id.next;
3009 /* ***************** READ GROUP *************** */
3011 static void direct_link_group(FileData *fd, Group *group)
3016 link_list(fd, &group->gobject);
3017 link_list(fd, &group->gkey);
3018 group->active= newdataadr(fd, group->active);
3020 go= group->gobject.first;
3022 link_list(fd, &go->okey);
3025 ok->gkey= newdataadr(fd, ok->gkey);
3032 static void lib_link_group(FileData *fd, Main *main)
3034 Group *group= main->group.first;
3039 if(group->id.flag & LIB_NEEDLINK) {
3040 group->id.flag -= LIB_NEEDLINK;
3042 go= group->gobject.first;
3044 go->ob= newlibadr(fd, group->id.lib, go->ob);
3047 ok->parent= newlibadr(fd, group->id.lib, ok->parent);
3048 ok->track= newlibadr(fd, group->id.lib, ok->track);
3049 ok->ipo= newlibadr_us(fd, group->id.lib, ok->ipo);
3055 group= group->id.next;
3059 /* ************** GENERAL & MAIN ******************** */
3061 static BHead *read_libblock(FileData *fd, Main *main, BHead *bhead, int flag, ID **id_r)
3063 /* this routine reads a libblock and its direct data. Use link functions
3071 if(bhead->code==ID_ID) {
3072 ID *linkedid= (ID *)(bhead + 1); /* BHEAD+DATA dependancy */
3074 lb= wich_libbase(main, GS(linkedid->name));
3077 lb= wich_libbase(main, bhead->code);
3081 id = read_struct(fd, bhead, "lib block");
3085 return blo_nextbhead(fd, bhead);
3087 oldnewmap_insert(fd->libmap, bhead->old, id, 1);
3088 BLI_addtail(lb, id);
3090 /* clear first 8 bits */
3091 id->flag= (id->flag & 0xFF00) | flag | LIB_NEEDLINK;
3092 id->lib= main->curlib;
3093 if(id->flag & LIB_FAKEUSER) id->us= 1;
3096 /* this case cannot be direct_linked: it's just the ID part */
3097 if(bhead->code==ID_ID) {
3098 return blo_nextbhead(fd, bhead);
3101 bhead = blo_nextbhead(fd, bhead);
3103 switch( GS(id->name) ) {
3104 case ID_OB: str= "ID_OB"; break;
3105 case ID_SCE: str= "ID_SCE"; break;
3106 case ID_LI: str= "ID_LI"; break;
3107 case ID_ME: str= "ID_ME"; break;
3108 case ID_CU: str= "ID_CU"; break;
3109 case ID_MB: str= "ID_MB"; break;
3110 case ID_MA: str= "ID_MA"; break;
3111 case ID_TE: str= "ID_TE"; break;
3112 case ID_IM: str= "ID_IM"; break;
3113 case ID_IK: str= "ID_IK"; break;
3114 case ID_WV: str= "ID_WV"; break;
3115 case ID_LT: str= "ID_LT"; break;
3116 case ID_SE: str= "ID_SE"; break;
3117 case ID_LF: str= "ID_LF"; break;
3118 case ID_LA: str= "ID_LA"; break;
3119 case ID_CA: str= "ID_CA"; break;
3120 case ID_IP: str= "ID_IP"; break;
3121 case ID_KE: str= "ID_KE"; break;
3122 case ID_WO: str= "ID_WO"; break;
3123 case ID_SCR: str= "ID_SCR"; break;
3124 case ID_VF: str= "ID_VF"; break;
3125 case ID_TXT : str= "ID_TXT"; break;
3126 case ID_SO: str= "ID_SO"; break;
3127 case ID_SAMPLE: str= "ID_SAMPLE"; break;
3128 case ID_GR: str= "ID_GR"; break;
3129 case ID_ID: str= "ID_ID"; break;
3130 case ID_SEQ: str= "ID_SEQ"; break;
3131 case ID_AR: str= "ID_AR"; break;
3132 case ID_AC: str= "ID_AC"; break;
3133 case ID_SCRIPT: str= "ID_SCRIPT"; break;
3136 while(bhead && bhead->code==DATA) {
3137 void *data= read_struct(fd, bhead, str);
3140 oldnewmap_insert(fd->datamap, bhead->old, data, 0);
3143 bhead = blo_nextbhead(fd, bhead);
3146 /* init pointers direct data */
3147 switch( GS(id->name) ) {
3149 direct_link_screen(fd, (bScreen *)id);
3152 direct_link_scene(fd, (Scene *)id);
3155 direct_link_object(fd, (Object *)id);