2cb99493daf7cd7205e9c85d275cbec498c6a003
[blender.git] / source / blender / blenloader / intern / readfile.c
1 /*
2  * readfile.c
3  *
4  * .blend file reading
5  *
6  * $Id$
7  *
8  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
9  *
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
16  * about this.
17  *
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.
22  *
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.
26  *
27  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
28  * All rights reserved.
29  *
30  * The Original Code is: all of this file.
31  *
32  * Contributor(s): none yet.
33  *
34  * ***** END GPL/BL DUAL LICENSE BLOCK *****
35  *
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #ifdef WIN32
43 #include "winsock2.h"
44 #include "BLI_winstuff.h"
45 #endif
46
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 strcasecmp strrchr strncmp strstr
51 #include <math.h> // for fabs
52
53 #ifndef WIN32
54     #include <unistd.h> // for read close
55     #include <sys/param.h> // for MAXPATHLEN
56 #else
57     #include <io.h> // for open close read
58 #endif
59
60 #include "nla.h"
61
62 #include "DNA_ID.h"
63 #include "DNA_packedFile_types.h"
64 #include "DNA_property_types.h"
65 #include "DNA_actuator_types.h"
66 #include "DNA_controller_types.h"
67 #include "DNA_sensor_types.h"
68 #include "DNA_sdna_types.h"
69 #include "DNA_scene_types.h"
70 #include "DNA_sequence_types.h"
71 #include "DNA_ika_types.h"
72 #include "DNA_camera_types.h"
73 #include "DNA_lattice_types.h"
74 #include "DNA_texture_types.h"
75 #include "DNA_key_types.h"
76 #include "DNA_meta_types.h"
77 #include "DNA_lamp_types.h"
78 #include "DNA_object_types.h"
79 #include "DNA_world_types.h"
80 #include "DNA_ipo_types.h"
81 #include "DNA_mesh_types.h"
82 #include "DNA_meshdata_types.h"
83 #include "DNA_image_types.h"
84 #include "DNA_material_types.h"
85 #include "DNA_curve_types.h"
86 #include "DNA_vfont_types.h"
87 #include "DNA_effect_types.h"
88 #include "DNA_text_types.h"
89 #include "DNA_view3d_types.h"
90 #include "DNA_screen_types.h"
91 #include "DNA_sound_types.h"
92 #include "DNA_space_types.h"
93 #include "DNA_oops_types.h"
94 #include "DNA_group_types.h"
95 #include "DNA_userdef_types.h"
96 #include "DNA_fileglobal_types.h"
97 #include "DNA_constraint_types.h"
98 #include "DNA_action_types.h"
99 #include "DNA_armature_types.h"
100 #include "DNA_nla_types.h"
101
102 #include "MEM_guardedalloc.h"
103 #include "BLI_blenlib.h"
104 #include "BLI_storage_types.h" // for relname flags
105
106 #include "BKE_bad_level_calls.h" // for reopen_text build_seqar (from WHILE_SEQ) open_plugin_seq set_rects_butspace check_imasel_copy
107
108 #include "BKE_constraint.h"
109 #include "BKE_utildefines.h" // SWITCH_INT WHILE_SEQ END_SEQ DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
110 #include "BKE_main.h" // for Main
111 #include "BKE_global.h" // for G
112 #include "BKE_property.h" // for get_property
113 #include "BKE_library.h" // for wich_libbase
114 #include "BKE_texture.h" // for open_plugin_tex
115 #include "BKE_effect.h" // for give_parteff
116 #include "BKE_sca.h" // for init_actuator
117 #include "BKE_mesh.h" // for ME_ defines (patching)
118 #include "BKE_armature.h"       //      for precalc_bonelist_irestmats
119 #include "BKE_action.h"
120
121 #include "BIF_butspace.h" // for do_versions, patching event codes
122
123 #include "BLO_readfile.h"
124 #include "readfile.h"
125
126 #include "genfile.h"
127
128 #include "BLO_readblenfile.h" // streaming read pipe, for BLO_readblenfile BLO_readblenfilememory
129
130 #include "mydevice.h"
131
132 /*
133  Remark: still a weak point is the newadress() function, that doesnt solve reading from
134  multiple files at the same time
135
136  (added remark: oh, i thought that was solved? will look at that... (ton)
137
138 READ
139 - Existing Library (Main) push or free
140 - allocate new Main
141 - load file
142 - read SDNA
143 - for each LibBlock
144         - read LibBlock
145         - if a Library
146                 - make a new Main
147                 - attach ID's to it
148         - else
149                 - read associated 'direct data'
150                 - link direct data (internal and to LibBlock)
151 - read FileGlobal
152 - read USER data, only when indicated (file is ~/.B.blend)
153 - free file
154 - per Library (per Main)
155         - read file
156         - read SDNA
157         - find LibBlocks and attach IDs to Main
158                 - if external LibBlock
159                         - search all Main's
160                                 - or it's already read,
161                                 - or not read yet
162                                 - or make new Main
163         - per LibBlock
164                 - read recursive
165                 - read associated direct data
166                 - link direct data (internal and to LibBlock)
167         - free file
168 - per Library with unread LibBlocks
169         - read file
170         - read SDNA
171         - per LibBlock
172                - read recursive
173                - read associated direct data
174                - link direct data (internal and to LibBlock)
175         - free file
176 - join all Mains
177 - link all LibBlocks and indirect pointers to libblocks
178 - initialize FileGlobal and copy pointers to Global
179 */
180
181 /* also occurs in library.c */
182 /* GS reads the memory pointed at in a specific ordering. There are,
183  * however two definitions for it. I have jotted them down here, both,
184  * but I think the first one is actually used. The thing is that
185  * big-endian systems might read this the wrong way round. OTOH, we
186  * constructed the IDs that are read out with this macro explicitly as
187  * well. I expect we'll sort it out soon... */
188
189 /* from blendef: */
190 #define GS(a)   (*((short *)(a)))
191
192 /* from misc_util: flip the bytes from x  */
193 /*  #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
194
195 // only used here in readfile.c
196 #define SWITCH_LONGINT(a) { \
197     char s_i, *p_i; \
198     p_i= (char *)&(a);  \
199     s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
200     s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
201     s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
202     s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
203 // only used here in readfile.c
204 #define SWITCH_SHORT(a) { \
205     char s_i, *p_i; \
206     p_i= (char *)&(a); \
207     s_i=p_i[0]; p_i[0]=p_i[1]; p_i[1]=s_i; }
208
209
210 /***/
211
212 typedef struct OldNew {
213         void *old, *newp;
214         int nr;
215 } OldNew;
216
217 typedef struct OldNewMap {
218         OldNew *entries;
219         int nentries, entriessize;
220
221         int lasthit;
222 } OldNewMap;
223
224 static OldNewMap *oldnewmap_new(void) {
225         OldNewMap *onm= MEM_mallocN(sizeof(*onm), "OldNewMap");
226         onm->lasthit= 0;
227         onm->nentries= 0;
228         onm->entriessize= 1024;
229         onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
230
231         return onm;
232 }
233
234 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr) {
235         OldNew *entry;
236
237         if (onm->nentries==onm->entriessize) {
238                 int osize= onm->entriessize;
239                 OldNew *oentries= onm->entries;
240
241                 onm->entriessize*= 2;
242                 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
243
244                 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
245                 MEM_freeN(oentries);
246         }
247
248         entry= &onm->entries[onm->nentries++];
249         entry->old= oldaddr;
250         entry->newp= newaddr;
251         entry->nr= nr;
252 }
253
254 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr) {
255         int i;
256
257         if (onm->lasthit<onm->nentries-1) {
258                 OldNew *entry= &onm->entries[++onm->lasthit];
259
260                 if (entry->old==addr) {
261                         entry->nr++;
262                         return entry->newp;
263                 }
264         }
265
266         for (i=0; i<onm->nentries; i++) {
267                 OldNew *entry= &onm->entries[i];
268
269                 if (entry->old==addr) {
270                         onm->lasthit= i;
271
272                         entry->nr++;
273                         return entry->newp;
274                 }
275         }
276
277         return NULL;
278 }
279
280 static void *oldnewmap_liblookup_and_inc(OldNewMap *onm, void *addr, void *lib) {
281         int i;
282
283         if (onm->lasthit<onm->nentries-1) {
284                 OldNew *entry= &onm->entries[++onm->lasthit];
285
286                 if (entry->old==addr) {
287                         ID *id= entry->newp;
288
289                         if (id && (!lib || id->lib)) {
290                                 entry->nr++;
291
292                                 return entry->newp;
293                         }
294                 }
295         }
296
297         for (i=0; i<onm->nentries; i++) {
298                 OldNew *entry= &onm->entries[i];
299
300                 if (entry->old==addr) {
301                         ID *id= entry->newp;
302
303                         if (id && (!lib || id->lib)) {
304                                 entry->nr++;
305
306                                 return entry->newp;
307                         }
308                 }
309         }
310
311         return NULL;
312 }
313
314 static void *oldnewmap_typelookup_and_inc(OldNewMap *onm, void *addr, short type) {
315         int i;
316
317         if (onm->lasthit<onm->nentries-1) {
318                 OldNew *entry= &onm->entries[++onm->lasthit];
319
320                 if (entry->old==addr) {
321                         ID *id= entry->newp;
322
323                         if (id && (GS(id->name) == type)) {
324                                 entry->nr++;
325
326                                 return entry->newp;
327                         }
328                 }
329         }
330
331         for (i=0; i<onm->nentries; i++) {
332                 OldNew *entry= &onm->entries[i];
333
334                 if (entry->old==addr) {
335                         ID *id= entry->newp;
336
337                         if (id && (GS(id->name) == type)) {
338                                 entry->nr++;
339
340                                 return entry->newp;
341                         }
342                 }
343         }
344
345         return NULL;
346 }
347
348 static void oldnewmap_free_unused(OldNewMap *onm) {
349         int i;
350
351         for (i=0; i<onm->nentries; i++) {
352                 OldNew *entry= &onm->entries[i];
353                 if (entry->nr==0) {
354                         MEM_freeN(entry->newp);
355                         entry->newp= NULL;
356                 }
357         }
358 }
359
360 static void oldnewmap_clear(OldNewMap *onm) {
361         onm->nentries= 0;
362         onm->lasthit= 0;
363 }
364
365 static void oldnewmap_free(OldNewMap *onm) {
366         MEM_freeN(onm->entries);
367         MEM_freeN(onm);
368 }
369
370 /***/
371
372 static void read_libraries(FileData *basefd, ListBase *mainlist);
373
374 /* ************ help functions ***************** */
375
376 static void add_main_to_main(Main *mainvar, Main *from)
377 {
378         ListBase *lbarray[100], *fromarray[100];        // define in library.c too
379         int a;
380
381         a= set_listbasepointers(mainvar, lbarray);
382         a= set_listbasepointers(from, fromarray);
383         while(a--) {
384                 addlisttolist(lbarray[a], fromarray[a]);
385         }
386 }
387
388 void blo_join_main(ListBase *mainlist)
389 {
390         Main *tojoin, *mainl= mainlist->first;
391
392         while ((tojoin= mainl->next)) {
393                 add_main_to_main(mainl, tojoin);
394                 BLI_remlink(mainlist, tojoin);
395                 MEM_freeN(tojoin);
396         }
397 }
398
399 static void split_libdata(ListBase *lb, Main *first)
400 {
401         ListBase *lbn;
402         ID *id, *idnext;
403         Main *mainvar;
404
405         id= lb->first;
406         while(id) {
407                 idnext= id->next;
408                 if(id->lib) {
409                         mainvar= first;
410                         while(mainvar) {
411                                 if(mainvar->curlib==id->lib) {
412                                         lbn= wich_libbase(mainvar, GS(id->name));
413                                         BLI_remlink(lb, id);
414                                         BLI_addtail(lbn, id);
415                                         break;
416                                 }
417                                 mainvar= mainvar->next;
418                         }
419                         if(mainvar==0) printf("error split_libdata\n");
420                 }
421                 id= idnext;
422         }
423 }
424
425 void blo_split_main(ListBase *mainlist)
426 {
427         Main *mainl= mainlist->first;
428         ListBase *lbarray[30];
429         Library *lib;
430         int i;
431
432         for (lib= mainl->library.first; lib; lib= lib->id.next) {
433                 Main *libmain= MEM_callocN(sizeof(*libmain), "libmain");
434                 libmain->curlib= lib;
435
436                 BLI_addtail(mainlist, libmain);
437         }
438
439         i= set_listbasepointers(mainl, lbarray);
440         while(i--)
441                 split_libdata(lbarray[i], mainl->next);
442 }
443
444 static Main *blo_find_main(ListBase *mainlist, char *name)
445 {
446         Main *m;
447         Library *lib;
448         char name1[FILE_MAXDIR+FILE_MAXFILE];
449         char libname1[FILE_MAXDIR+FILE_MAXFILE];
450
451         /* name can be stringcode too */
452         strcpy(name1, name);
453         BLI_convertstringcode(name1, G.sce, 0);
454
455         for (m= mainlist->first; m; m= m->next) {
456                 char *libname= (m->curlib)?m->curlib->name:m->name;
457                 
458                 strcpy(libname1, libname);
459                 BLI_convertstringcode(libname1, G.sce, 0);
460                 
461                 if (BLI_streq(name1, libname1))
462                         return m;
463         }
464
465         m= MEM_callocN(sizeof(*m), "find_main");
466         BLI_addtail(mainlist, m);
467
468         lib= alloc_libblock(&m->library, ID_LI, "lib");
469         strcpy(lib->name, name);
470         m->curlib= lib;
471
472         return m;
473 }
474
475
476 /* ************ FILE PARSING ****************** */
477
478 static void switch_endian_bh4(BHead4 *bhead)
479 {
480         /* the ID_.. codes */
481         if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
482
483         if (bhead->code != ENDB) {
484                 SWITCH_INT(bhead->len);
485                 SWITCH_INT(bhead->SDNAnr);
486                 SWITCH_INT(bhead->nr);
487         }
488 }
489
490 static void switch_endian_bh8(BHead8 *bhead)
491 {
492         /* the ID_.. codes */
493         if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
494
495         if (bhead->code != ENDB) {
496                 SWITCH_INT(bhead->len);
497                 SWITCH_INT(bhead->SDNAnr);
498                 SWITCH_INT(bhead->nr);
499         }
500 }
501
502 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
503 {
504         BHead4 *bhead4 = (BHead4 *) bhead;
505 #if defined(WIN32) && !defined(FREE_WINDOWS)
506         __int64 old;
507 #else
508         long long old;
509 #endif
510
511         bhead4->code= bhead8->code;
512         bhead4->len= bhead8->len;
513
514         if (bhead4->code != ENDB) {
515
516                 // why is this here ??
517                 if (do_endian_swap) {
518                         SWITCH_LONGINT(bhead8->old);
519                 }
520
521                 /* this patch is to avoid a long long being read from not-eight aligned positions
522                    is necessary on SGI with -n32 compiling (no, is necessary on
523                    any modern 64bit architecture) */
524                 memcpy(&old, &bhead8->old, 8);
525                 bhead4->old = (int) (old >> 3);
526
527                 bhead4->SDNAnr= bhead8->SDNAnr;
528                 bhead4->nr= bhead8->nr;
529         }
530 }
531
532 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
533 {
534         BHead8 *bhead8 = (BHead8 *) bhead;
535
536         bhead8->code= bhead4->code;
537         bhead8->len= bhead4->len;
538
539         if (bhead8->code != ENDB) {
540                 bhead8->old= bhead4->old;
541                 bhead8->SDNAnr= bhead4->SDNAnr;
542                 bhead8->nr= bhead4->nr;
543         }
544 }
545
546 static BHeadN *get_bhead(FileData *fd)
547 {
548         BHead8 bhead8;
549         BHead4 bhead4;
550         BHead  bhead;
551         BHeadN *new_bhead = 0;
552         int readsize;
553
554         if (fd) {
555                 if ( ! fd->eof) {
556
557                         // First read the bhead structure.
558                         // Depending on the platform the file was written on this can
559                         // be a big or little endian BHead4 or BHead8 structure.
560
561                         // As usual 'ENDB' (the last *partial* bhead of the file)
562                         // needs some special handling. We don't want to EOF just yet.
563
564                         if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
565                                 bhead4.code = DATA;
566                                 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
567
568                                 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
569                                         if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
570                                                 switch_endian_bh4(&bhead4);
571                                         }
572
573                                         if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
574                                                 bh8_from_bh4(&bhead, &bhead4);
575                                         } else {
576                                                 memcpy(&bhead, &bhead4, sizeof(bhead));
577                                         }
578                                 } else {
579                                         fd->eof = 1;
580                                 }
581                         } else {
582                                 bhead8.code = DATA;
583                                 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
584
585                                 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
586                                         if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
587                                                 switch_endian_bh8(&bhead8);
588                                         }
589
590                                         if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
591                                                 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
592                                         } else {
593                                                 memcpy(&bhead, &bhead8, sizeof(bhead));
594                                         }
595                                 } else {
596                                         fd->eof = 1;
597                                 }
598                         }
599
600                         // bhead now contains the (converted) bhead structure. Now read
601                         // the associated data and put everything in a BHeadN (creative naming !)
602
603                         if ( ! fd->eof) {
604                                 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
605                                 if (new_bhead) {
606                                         new_bhead->next = new_bhead->prev = 0;
607                                         new_bhead->bhead = bhead;
608
609                                         readsize = fd->read(fd, new_bhead + 1, bhead.len);
610
611                                         if (readsize != bhead.len) {
612                                                 fd->eof = 1;
613                                                 MEM_freeN(new_bhead);
614                                         }
615                                 } else {
616                                         fd->eof = 1;
617                                 }
618                         }
619                 }
620         }
621
622         // We've read a new block. Now add it to the list
623         // of blocks.
624
625         if (new_bhead) {
626                 BLI_addtail(&fd->listbase, new_bhead);
627         }
628
629         return(new_bhead);
630 }
631
632 BHead *blo_firstbhead(FileData *fd)
633 {
634         BHeadN *new_bhead;
635         BHead *bhead = 0;
636
637         // Rewind the file
638         // Read in a new block if necessary
639
640         new_bhead = fd->listbase.first;
641         if (new_bhead == 0) {
642                 new_bhead = get_bhead(fd);
643         }
644
645         if (new_bhead) {
646                 bhead = &new_bhead->bhead;
647         }
648
649         return(bhead);
650 }
651
652 BHead *blo_prevbhead(FileData *fd, BHead *thisblock)
653 {
654         BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - (int) (&((BHeadN*)0)->bhead));
655         BHeadN *prev= bheadn->prev;
656
657         return prev?&prev->bhead:NULL;
658 }
659
660 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
661 {
662         BHeadN *new_bhead = 0;
663         BHead *bhead = 0;
664
665         if (thisblock) {
666                 // bhead is actually a sub part of BHeadN
667                 // We calculate the BHeadN pointer from the BHead pointer below
668                 new_bhead = (BHeadN *) (((char *) thisblock) - (int) (&((BHeadN*)0)->bhead));
669
670                 // get the next BHeadN. If it doesn't exist we read in the next one
671                 new_bhead = new_bhead->next;
672                 if (new_bhead == 0) {
673                         new_bhead = get_bhead(fd);
674                 }
675         }
676
677         if (new_bhead) {
678                 // here we do the reverse:
679                 // go from the BHeadN pointer to the BHead pointer
680                 bhead = &new_bhead->bhead;
681         }
682
683         return(bhead);
684 }
685
686 static void decode_blender_header(FileData *fd)
687 {
688         char header[SIZEOFBLENDERHEADER], num[4];
689         int readsize;
690
691         // read in the header data
692         readsize = fd->read(fd, header, sizeof(header));
693
694         if (readsize == sizeof(header)) {
695                 if(strncmp(header, "BLENDER", 7) == 0) {
696                         int remove_this_endian_test= 1;
697
698                         fd->flags |= FD_FLAGS_FILE_OK;
699
700                         // what size are pointers in the file ?
701                         if(header[7]=='_') {
702                                 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
703                                 if (sizeof(void *) != 4) {
704                                         fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
705                                 }
706                         } else {
707                                 if (sizeof(void *) != 8) {
708                                         fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
709                                 }
710                         }
711
712                         // is the file saved in a different endian
713                         // than we need ?
714                         if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
715                                 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
716                         }
717
718                         // get the version number
719
720                         memcpy(num, header+9, 3);
721                         num[3] = 0;
722                         fd->fileversion = atoi(num);
723                 }
724         }
725 }
726
727 static int read_file_dna(FileData *fd)
728 {
729         BHead *bhead;
730
731         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
732                 if (bhead->code==DNA1) {
733                         int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
734
735                         fd->filesdna= dna_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
736                         if (fd->filesdna)
737                                 fd->compflags= dna_get_structDNA_compareflags(fd->filesdna, fd->memsdna);
738
739                         return 1;
740                 } else if (bhead->code==ENDB)
741                         break;
742         }
743
744         return 0;
745 }
746 static int fd_read_from_file(FileData *filedata, void *buffer, int size)
747 {
748         int readsize = read(filedata->filedes, buffer, size);
749
750         if (readsize < 0) {
751                 readsize = EOF;
752         } else {
753                 filedata->seek += readsize;
754         }
755
756         return (readsize);
757 }
758
759 static int fd_read_from_memory(FileData *filedata, void *buffer, int size)
760 {
761                 // don't read more bytes then there are available in the buffer
762         int readsize = MIN2(size, filedata->buffersize - filedata->seek);
763
764         memcpy(buffer, filedata->buffer + filedata->seek, readsize);
765         filedata->seek += readsize;
766
767         return (readsize);
768 }
769
770 static FileData *filedata_new(void)
771 {
772         extern char DNAstr[];   /* DNA.c */
773         extern int DNAlen;
774         FileData *fd = MEM_callocN(sizeof(*fd), "FileData");
775
776         fd->filedes = -1;
777
778                 /* XXX, this doesn't need to be done all the time,
779                  * but it keeps us reentrant,  remove once we have
780                  * a lib that provides a nice lock. - zr
781                  */
782         fd->memsdna = dna_sdna_from_data(DNAstr,  DNAlen,  0);
783
784         fd->datamap = oldnewmap_new();
785         fd->globmap = oldnewmap_new();
786         fd->libmap = oldnewmap_new();
787
788         return fd;
789 }
790
791 FileData *blo_openblenderfile(char *name)
792 {
793         int file;
794         char name1[FILE_MAXDIR+FILE_MAXFILE];
795         
796         /* library files can have stringcodes */
797         strcpy(name1, name);
798         BLI_convertstringcode(name1, G.sce, 0);
799         
800         file= open(name1, O_BINARY|O_RDONLY);
801
802         if (file == -1) {
803                 return NULL;
804         } else {
805                 FileData *fd = filedata_new();
806                 fd->filedes = file;
807                 fd->buffersize = BLI_filesize(file);
808                 fd->read = fd_read_from_file;
809
810                 decode_blender_header(fd);
811
812                 if (fd->flags & FD_FLAGS_FILE_OK) {
813                         if (!read_file_dna(fd)) {
814                                 blo_freefiledata(fd);
815                                 fd= NULL;
816                         }
817                 } else {
818                         blo_freefiledata(fd);
819                         fd= NULL;
820                 }
821
822                 return fd;
823         }
824 }
825
826 FileData *blo_openblendermemory(void *mem, int memsize)
827 {
828         if (!mem || memsize<SIZEOFBLENDERHEADER) {
829                 return NULL;
830         } else {
831                 FileData *fd= filedata_new();
832                 fd->buffer= mem;
833                 fd->buffersize= memsize;
834                 fd->read= fd_read_from_memory;
835                 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
836
837                 decode_blender_header(fd);
838
839                 if (fd->flags & FD_FLAGS_FILE_OK) {
840                         if (!read_file_dna(fd)) {
841                                 blo_freefiledata(fd);
842                                 fd= NULL;
843                         }
844                 } else {
845                         blo_freefiledata(fd);
846                         fd= NULL;
847                 }
848
849                 return fd;
850         }
851 }
852
853 void blo_freefiledata(FileData *fd)
854 {
855         if (fd) {
856                 if (fd->filedes != -1) {
857                         close(fd->filedes);
858                 }
859
860                 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
861                         MEM_freeN(fd->buffer);
862                         fd->buffer = 0;
863                 }
864
865                 // Free all BHeadN data blocks
866                 BLI_freelistN(&fd->listbase);
867
868                 if (fd->memsdna)
869                         dna_freestructDNA(fd->memsdna);
870                 if (fd->filesdna)
871                         dna_freestructDNA(fd->filesdna);
872                 if (fd->compflags)
873                         MEM_freeN(fd->compflags);
874
875                 if (fd->datamap)
876                         oldnewmap_free(fd->datamap);
877                 if (fd->globmap)
878                         oldnewmap_free(fd->globmap);
879                 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
880                         oldnewmap_free(fd->libmap);
881
882                 MEM_freeN(fd);
883         }
884 }
885
886 /* ************ DIV ****************** */
887
888 int BLO_has_bfile_extension(char *str)
889 {
890         return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend"));
891 }
892
893 /* ************** OLD POINTERS ******************* */
894
895 static void *newdataadr(FileData *fd, void *adr)                /* only direct databocks */
896 {
897         return oldnewmap_lookup_and_inc(fd->datamap, adr);
898 }
899
900 static void *newglobadr(FileData *fd, void *adr)                /* direct datablocks with global linking */
901 {
902         return oldnewmap_lookup_and_inc(fd->globmap, adr);
903 }
904
905 static void *newlibadr(FileData *fd, void *lib, void *adr)              /* only lib data */
906 {
907         return oldnewmap_liblookup_and_inc(fd->libmap, adr, lib);
908 }
909
910 static void *newlibadr_us_type(FileData *fd, short type, void *adr)     /* only Lib data */
911 {
912         ID *id= oldnewmap_typelookup_and_inc(fd->libmap, adr, type);
913
914         if (id) {
915                 id->us++;
916         }
917
918         return id;
919 }
920
921 static void *newlibadr_us(FileData *fd, void *lib, void *adr)   /* increases user number */
922 {
923         ID *id= newlibadr(fd, lib, adr);
924
925         if(id) {
926                 id->us++;
927         }
928
929         return id;
930 }
931
932 static void change_libadr(FileData *fd, void *old, void *new)
933 {
934         int i;
935
936                 /* changed one thing here, the old change_libadr
937                  * only remapped addresses that had an id->lib,
938                  * but that doesn't make sense to me... its an
939                  * old pointer, period, it needs to be remapped. - zr
940                  */
941
942                 /*
943                  * Ton seemed to think it was necessary to look
944                  * through all entries, and not return after finding
945                  * a match, leaving this cryptic comment,
946                  * // no return, maybe there can be more?
947                  *
948                  * That doesn't make sense to me either but I am
949                  * too scared to remove it... it only would make
950                  * sense if two distinct old address map to the
951                  * same new address - obviously that shouldn't happen
952                  * because memory addresses are unique.
953                  *
954                  * The only case it might happen is when two distinct
955                  * libraries are mapped using the same table... this
956                  * won't work to start with... At some point this
957                  * all needs to be made sense of and made understandable,
958                  * but I'm afraid I don't have time now. -zr
959                  *
960                  */
961     /* the code is nasty, and needs a lot of energy to get into full understanding
962        again... i now translate dutch comments, maybe that gives me more insight!
963        But i guess it has to do with the assumption that 2 addresses can be allocated
964        in different sessions, and therefore be the same... like the remark in the top
965        of this c file (ton) */
966
967         for (i=0; i<fd->libmap->nentries; i++) {
968                 OldNew *entry= &fd->libmap->entries[i];
969
970                 if (old==entry->newp) {
971                         entry->newp= new;
972                         break;
973                 }
974         }
975 }
976
977
978 /* ********** END OLD POINTERS ****************** */
979 /* ********** READ FILE ****************** */
980
981 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
982 {
983         int blocksize, nblocks;
984         char *data;
985
986         data= (char *)(bhead+1); /*  BHEAD+DATA dependancy */
987         blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
988
989         nblocks= bhead->nr;
990         while(nblocks--) {
991                 dna_switch_endian_struct(filesdna, bhead->SDNAnr, data);
992
993                 data+= blocksize;
994         }
995 }
996
997 static void *read_struct(FileData *fd, BHead *bh, char *blockname)
998 {
999         void *temp= NULL;
1000
1001         if (bh->len) {
1002                 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1003                         switch_endian_structs(fd->filesdna, bh);
1004
1005                 if (fd->compflags[bh->SDNAnr]) {        /* flag==0: doesn't exist anymore */
1006                         if(fd->compflags[bh->SDNAnr]==2) {
1007                                 temp= dna_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1008                         } else {
1009                                 temp= MEM_mallocN(bh->len, blockname);
1010                                 memcpy(temp, (bh+1), bh->len); /*  BHEAD+DATA dependancy */
1011                         }
1012                 }
1013         }
1014
1015         return temp;
1016 }
1017
1018 static void link_list(FileData *fd, ListBase *lb)               /* only direct data */
1019 {
1020         Link *ln, *prev;
1021
1022         if(lb->first==0) return;
1023
1024         lb->first= newdataadr(fd, lb->first);
1025         ln= lb->first;
1026         prev= 0;
1027         while(ln) {
1028                 ln->next= newdataadr(fd, ln->next);
1029                 ln->prev= prev;
1030                 prev= ln;
1031                 ln= ln->next;
1032         }
1033         lb->last= prev;
1034 }
1035
1036 static void link_glob_list(FileData *fd, ListBase *lb)          /* for glob data */
1037 {
1038         Link *ln, *prev;
1039         void *poin;
1040
1041         if(lb->first==0) return;
1042
1043         poin= newdataadr(fd, lb->first);
1044         if(lb->first) {
1045                 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1046         }
1047         lb->first= poin;
1048
1049         ln= lb->first;
1050         prev= 0;
1051         while(ln) {
1052                 poin= newdataadr(fd, ln->next);
1053                 if(ln->next) {
1054                         oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1055                 }
1056                 ln->next= poin;
1057                 ln->prev= prev;
1058                 prev= ln;
1059                 ln= ln->next;
1060         }
1061         lb->last= prev;
1062 }
1063
1064 static void test_pointer_array(FileData *fd, void **mat)
1065 {
1066 #if defined(WIN32) && !defined(FREE_WINDOWS)
1067         __int64 *lpoin, *lmat;
1068 #else
1069         long long *lpoin, *lmat;
1070 #endif
1071         int len, *ipoin, *imat;
1072
1073                 /* manually convert the pointer array in
1074                  * the old dna format to a pointer array in
1075                  * the new dna format.
1076                  */
1077         if(*mat) {
1078                 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1079
1080                 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1081                         ipoin=imat= MEM_mallocN( len*4, "newmatar");
1082                         lpoin= *mat;
1083
1084                         while(len-- > 0) {
1085                                 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1086                                         SWITCH_LONGINT(*lpoin);
1087                                 *ipoin= (int) ((*lpoin) >> 3);
1088                                 ipoin++;
1089                                 lpoin++;
1090                         }
1091                         MEM_freeN(*mat);
1092                         *mat= imat;
1093                 }
1094
1095                 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1096                         lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1097                         ipoin= *mat;
1098
1099                         while(len-- > 0) {
1100                                 *lpoin= *ipoin;
1101                                 ipoin++;
1102                                 lpoin++;
1103                         }
1104                         MEM_freeN(*mat);
1105                         *mat= lmat;
1106                 }
1107         }
1108 }
1109
1110 /* ************ READ PACKEDFILE *************** */
1111
1112 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1113 {
1114         PackedFile *pf= newdataadr(fd, oldpf);
1115
1116         if (pf) {
1117                 pf->data= newdataadr(fd, pf->data);
1118         }
1119
1120         return pf;
1121 }
1122
1123 /* ************ READ SCRIPTLINK *************** */
1124
1125 static void lib_link_scriptlink(FileData *fd, ID *id, ScriptLink *slink)
1126 {
1127         int i;
1128
1129         for(i=0; i<slink->totscript; i++) {
1130                 slink->scripts[i]= newlibadr(fd, id->lib, slink->scripts[i]);
1131         }
1132 }
1133
1134 static void direct_link_scriptlink(FileData *fd, ScriptLink *slink)
1135 {
1136         slink->scripts= newdataadr(fd, slink->scripts);
1137         slink->flag= newdataadr(fd, slink->flag);
1138
1139         if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1140                 int a;
1141
1142                 for(a=0; a<slink->totscript; a++) {
1143                         SWITCH_SHORT(slink->flag[a]);
1144                 }
1145         }
1146 }
1147
1148 /* ************ READ IKA ***************** */
1149
1150 static void lib_link_ika(FileData *fd, Main *main)
1151 {
1152         Ika *ika;
1153         int a;
1154         Deform *def;
1155
1156         ika= main->ika.first;
1157         while(ika) {
1158                 if(ika->id.flag & LIB_NEEDLINK) {
1159
1160                         ika->parent= newlibadr(fd, ika->id.lib, ika->parent);
1161
1162                         a= ika->totdef;
1163                         def= ika->def;
1164                         while(a--) {
1165                                 def->ob=  newlibadr(fd, ika->id.lib, def->ob);
1166                                 def++;
1167                         }
1168                         ika->id.flag -= LIB_NEEDLINK;
1169                 }
1170                 ika= ika->id.next;
1171         }
1172 }
1173
1174 static void direct_link_ika(FileData *fd, Ika *ika)
1175 {
1176         link_list(fd, &ika->limbbase);
1177
1178         ika->def= newdataadr(fd, ika->def);
1179
1180         /* error from V.138 and older */
1181         if(ika->def==0) ika->totdef= 0;
1182 }
1183
1184 /* ************ READ ARMATURE ***************** */
1185
1186 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1187 {
1188         bActionStrip *strip;
1189
1190         for (strip=striplist->first; strip; strip=strip->next){
1191                 strip->act = newlibadr_us(fd, id->lib, strip->act);
1192                 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1193         };
1194 }
1195
1196 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1197 {
1198         bConstraintChannel *chan;
1199
1200         for (chan=chanbase->first; chan; chan=chan->next){
1201                 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1202         }
1203 }
1204
1205 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
1206 {
1207         bConstraint *con;
1208
1209         for (con = conlist->first; con; con=con->next) {
1210                 /*  patch for error introduced by changing constraints (dunno how) */
1211                 if(con->data==NULL) {
1212                         con->type= CONSTRAINT_TYPE_NULL;
1213                 }
1214
1215                 switch (con->type) {
1216                 case CONSTRAINT_TYPE_ACTION:
1217                         {
1218                                 bActionConstraint *data;
1219                                 data= ((bActionConstraint*)con->data);
1220                                 data->tar = newlibadr(fd, id->lib, data->tar);
1221                                 data->act = newlibadr(fd, id->lib, data->act);
1222                         }
1223                         break;
1224                 case CONSTRAINT_TYPE_LOCLIKE:
1225                         {
1226                                 bLocateLikeConstraint *data;
1227                                 data= ((bLocateLikeConstraint*)con->data);
1228                                 data->tar = newlibadr(fd, id->lib, data->tar);
1229                         };
1230                         break;
1231                 case CONSTRAINT_TYPE_ROTLIKE:
1232                         {
1233                                 bRotateLikeConstraint *data;
1234                                 data= ((bRotateLikeConstraint*)con->data);
1235                                 data->tar = newlibadr(fd, id->lib, data->tar);
1236                         };
1237                         break;
1238                 case CONSTRAINT_TYPE_KINEMATIC:
1239                         {
1240                                 bKinematicConstraint *data;
1241                                 data = ((bKinematicConstraint*)con->data);
1242                                 data->tar = newlibadr(fd, id->lib, data->tar);
1243                         }
1244                         break;
1245                 case CONSTRAINT_TYPE_TRACKTO:
1246                         {
1247                                 bTrackToConstraint *data;
1248                                 data = ((bTrackToConstraint*)con->data);
1249                                 data->tar = newlibadr(fd, id->lib, data->tar);
1250                         }
1251                         break;
1252                 case CONSTRAINT_TYPE_LOCKTRACK:
1253                         {
1254                                 bLockTrackConstraint *data;
1255                                 data= ((bLockTrackConstraint*)con->data);
1256                                 data->tar = newlibadr(fd, id->lib, data->tar);
1257                         };
1258                         break;
1259                 case CONSTRAINT_TYPE_FOLLOWPATH:
1260                         {
1261                                 bFollowPathConstraint *data;
1262                                 data= ((bFollowPathConstraint*)con->data);
1263                                 data->tar = newlibadr(fd, id->lib, data->tar);
1264                         };
1265                         break;
1266                 case CONSTRAINT_TYPE_DISTANCELIMIT:
1267                         {
1268                                 bDistanceLimitConstraint *data;
1269                                 data= ((bDistanceLimitConstraint*)con->data);
1270                                 data->tar = newlibadr(fd, id->lib, data->tar);
1271                         };
1272                         break;
1273                 case CONSTRAINT_TYPE_NULL:
1274                         break;
1275                 }
1276         }
1277 }
1278
1279 static void direct_link_constraints(FileData *fd, ListBase *lb)
1280 {
1281         bConstraint *cons;
1282
1283         link_list(fd, lb);
1284         for (cons=lb->first; cons; cons=cons->next) {
1285                 cons->data = newdataadr(fd, cons->data);
1286                 switch (cons->type) {
1287                 default:
1288                         break;
1289                 }
1290                 // Link data
1291         }
1292 }
1293
1294 static void lib_link_bone(FileData *fd, ID *id, Bone *bone)
1295 {
1296         Bone *curBone;
1297
1298 //      lib_link_constraints(fd, id, &bone->constraints);
1299
1300         for (curBone=bone->childbase.first; curBone; curBone=curBone->next) {
1301                 lib_link_bone(fd, id, curBone);
1302         }
1303 }
1304
1305
1306 static void lib_link_pose(FileData *fd, ID *id, bPose *pose)
1307 {
1308         bPoseChannel *chan;
1309
1310         if (!pose)
1311                 return;
1312
1313         for (chan = pose->chanbase.first; chan; chan=chan->next) {
1314                 lib_link_constraints(fd, id, &chan->constraints);
1315         }
1316 }
1317
1318 static void lib_link_armature(FileData *fd, Main *main)
1319 {
1320         bArmature *arm;
1321         Bone *bone;
1322
1323         arm= main->armature.first;
1324
1325         while(arm) {
1326                 if(arm->id.flag & LIB_NEEDLINK) {
1327                         arm->id.flag -= LIB_NEEDLINK;
1328                 }
1329
1330                 for (bone=arm->bonebase.first; bone; bone=bone->next) {
1331                         lib_link_bone(fd, &arm->id, bone);
1332                 }
1333
1334                 arm= arm->id.next;
1335         }
1336 }
1337
1338 static void lib_link_action(FileData *fd, Main *main)
1339 {
1340         bAction *act;
1341         bActionChannel *chan;
1342
1343         act= main->action.first;
1344         while(act) {
1345                 if(act->id.flag & LIB_NEEDLINK) {
1346                         act->id.flag -= LIB_NEEDLINK;
1347
1348                         for (chan=act->chanbase.first; chan; chan=chan->next) {
1349                                 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1350                                 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1351                         }
1352
1353                 }
1354                 act= act->id.next;
1355         }
1356 }
1357
1358 static void direct_link_bones(FileData *fd, Bone* bone)
1359 {
1360         Bone    *child;
1361
1362         bone->parent= newdataadr(fd, bone->parent);
1363
1364         link_list(fd, &bone->childbase);
1365
1366         for (child=bone->childbase.first; child; child=child->next) {
1367                 direct_link_bones(fd, child);
1368         }
1369 }
1370
1371
1372 static void direct_link_action(FileData *fd, bAction *act)
1373 {
1374         bActionChannel *achan;
1375
1376         link_list(fd, &act->chanbase);
1377
1378         for (achan = act->chanbase.first; achan; achan=achan->next)
1379                 link_list(fd, &achan->constraintChannels);
1380
1381 }
1382
1383 static void direct_link_armature(FileData *fd, bArmature *arm)
1384 {
1385         Bone    *bone;
1386
1387         link_list(fd, &arm->bonebase);
1388
1389         bone=arm->bonebase.first;
1390         while (bone) {
1391                 direct_link_bones(fd, bone);
1392                 bone=bone->next;
1393         }
1394 }
1395
1396 /* ************ READ CAMERA ***************** */
1397
1398 static void lib_link_camera(FileData *fd, Main *main)
1399 {
1400         Camera *ca;
1401
1402         ca= main->camera.first;
1403         while(ca) {
1404                 if(ca->id.flag & LIB_NEEDLINK) {
1405
1406                         ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo);
1407
1408                         lib_link_scriptlink(fd, &ca->id, &ca->scriptlink);
1409
1410                         ca->id.flag -= LIB_NEEDLINK;
1411                 }
1412                 ca= ca->id.next;
1413         }
1414 }
1415
1416 static void direct_link_camera(FileData *fd, Camera *ca)
1417 {
1418         direct_link_scriptlink(fd, &ca->scriptlink);
1419 }
1420
1421
1422 /* ************ READ LATTICE ***************** */
1423
1424 static void lib_link_latt(FileData *fd, Main *main)
1425 {
1426         Lattice *lt;
1427
1428         lt= main->latt.first;
1429         while(lt) {
1430                 if(lt->id.flag & LIB_NEEDLINK) {
1431
1432                         lt->ipo= newlibadr_us(fd, lt->id.lib, lt->ipo);
1433                         lt->key= newlibadr_us(fd, lt->id.lib, lt->key);
1434
1435                         lt->id.flag -= LIB_NEEDLINK;
1436                 }
1437                 lt= lt->id.next;
1438         }
1439 }
1440
1441 static void direct_link_latt(FileData *fd, Lattice *lt)
1442 {
1443         lt->def= newdataadr(fd, lt->def);
1444 }
1445
1446 /* ************ READ LAMP ***************** */
1447
1448 static void lib_link_lamp(FileData *fd, Main *main)
1449 {
1450         Lamp *la;
1451         MTex *mtex;
1452         int a;
1453
1454         la= main->lamp.first;
1455         while(la) {
1456                 if(la->id.flag & LIB_NEEDLINK) {
1457
1458                         for(a=0; a<8; a++) {
1459                                 mtex= la->mtex[a];
1460                                 if(mtex) {
1461                                         mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
1462                                         mtex->object= newlibadr(fd, la->id.lib, mtex->object);
1463                                 }
1464                         }
1465
1466                         la->ipo= newlibadr_us(fd, la->id.lib, la->ipo);
1467
1468                         lib_link_scriptlink(fd, &la->id, &la->scriptlink);
1469
1470                         la->id.flag -= LIB_NEEDLINK;
1471                 }
1472                 la= la->id.next;
1473         }
1474 }
1475
1476 static void direct_link_lamp(FileData *fd, Lamp *la)
1477 {
1478         int a;
1479
1480         direct_link_scriptlink(fd, &la->scriptlink);
1481
1482         for(a=0; a<8; a++) {
1483                 la->mtex[a]= newdataadr(fd, la->mtex[a]);
1484         }
1485 }
1486
1487 /* ************ READ keys ***************** */
1488
1489 static void lib_link_key(FileData *fd, Main *main)
1490 {
1491         Key *key;
1492
1493         key= main->key.first;
1494         while(key) {
1495                 if(key->id.flag & LIB_NEEDLINK) {
1496
1497                         key->ipo= newlibadr_us(fd, key->id.lib, key->ipo);
1498                         key->from= newlibadr(fd, key->id.lib, key->from);
1499
1500                         key->id.flag -= LIB_NEEDLINK;
1501                 }
1502                 key= key->id.next;
1503         }
1504 }
1505
1506 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
1507 {
1508         int elemsize, a, b;
1509         char *data, *poin, *cp;
1510
1511         elemsize= key->elemsize;
1512         data= kb->data;
1513
1514         for(a=0; a<kb->totelem; a++) {
1515
1516                 cp= key->elemstr;
1517                 poin= data;
1518
1519                 while( cp[0] ) {        /* cp[0]==amount */
1520
1521                         switch(cp[1]) {         /* cp[1]= type */
1522                         case IPO_FLOAT:
1523                         case IPO_BPOINT:
1524                         case IPO_BEZTRIPLE:
1525                                 b= cp[0];
1526                                 while(b--) {
1527                                         SWITCH_INT((*poin));
1528                                         poin+= 4;
1529                                 }
1530                                 break;
1531                         }
1532
1533                         cp+= 2;
1534
1535                 }
1536                 data+= elemsize;
1537         }
1538 }
1539
1540 static void direct_link_key(FileData *fd, Key *key)
1541 {
1542         KeyBlock *kb;
1543
1544         link_list(fd, &(key->block));
1545
1546         key->refkey= newdataadr(fd, key->refkey);
1547
1548         kb= key->block.first;
1549         while(kb) {
1550
1551                 kb->data= newdataadr(fd, kb->data);
1552
1553                 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
1554                         switch_endian_keyblock(key, kb);
1555
1556                 kb= kb->next;
1557         }
1558 }
1559
1560 /* ************ READ mball ***************** */
1561
1562 static void lib_link_mball(FileData *fd, Main *main)
1563 {
1564         MetaBall *mb;
1565         int a;
1566
1567         mb= main->mball.first;
1568         while(mb) {
1569                 if(mb->id.flag & LIB_NEEDLINK) {
1570
1571                         for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
1572
1573                         mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo);
1574
1575                         mb->id.flag -= LIB_NEEDLINK;
1576                 }
1577                 mb= mb->id.next;
1578         }
1579 }
1580
1581 static void direct_link_mball(FileData *fd, MetaBall *mb)
1582 {
1583         mb->mat= newdataadr(fd, mb->mat);
1584         test_pointer_array(fd, (void **)&mb->mat);
1585
1586         link_list(fd, &(mb->elems));
1587
1588         mb->disp.first= mb->disp.last= 0;
1589
1590         mb->bb= 0;
1591 }
1592
1593 /* ************ READ WORLD ***************** */
1594
1595 static void lib_link_world(FileData *fd, Main *main)
1596 {
1597         World *wrld;
1598         MTex *mtex;
1599         int a;
1600
1601         wrld= main->world.first;
1602         while(wrld) {
1603                 if(wrld->id.flag & LIB_NEEDLINK) {
1604
1605                         wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo);
1606
1607                         for(a=0; a<8; a++) {
1608                                 mtex= wrld->mtex[a];
1609                                 if(mtex) {
1610                                         mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
1611                                         mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
1612                                 }
1613                         }
1614
1615                         lib_link_scriptlink(fd, &wrld->id, &wrld->scriptlink);
1616
1617                         wrld->id.flag -= LIB_NEEDLINK;
1618                 }
1619                 wrld= wrld->id.next;
1620         }
1621 }
1622
1623 static void direct_link_world(FileData *fd, World *wrld)
1624 {
1625         int a;
1626
1627         direct_link_scriptlink(fd, &wrld->scriptlink);
1628
1629         for(a=0; a<8; a++) {
1630                 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
1631         }
1632 }
1633
1634
1635 /* ************ READ IPO ***************** */
1636
1637 static void lib_link_ipo(FileData *fd, Main *main)
1638 {
1639         Ipo *ipo;
1640
1641         ipo= main->ipo.first;
1642         while(ipo) {
1643                 if(ipo->id.flag & LIB_NEEDLINK) {
1644
1645                         ipo->id.flag -= LIB_NEEDLINK;
1646                 }
1647                 ipo= ipo->id.next;
1648         }
1649 }
1650
1651 static void direct_link_ipo(FileData *fd, Ipo *ipo)
1652 {
1653         IpoCurve *icu;
1654
1655         link_list(fd, &(ipo->curve));
1656         icu= ipo->curve.first;
1657         while(icu) {
1658                 icu->bezt= newdataadr(fd, icu->bezt);
1659                 icu->bp= newdataadr(fd, icu->bp);
1660                 icu= icu->next;
1661         }
1662 }
1663
1664 /* ************ READ VFONT ***************** */
1665
1666 static void lib_link_vfont(FileData *fd, Main *main)
1667 {
1668         VFont *vf;
1669
1670         vf= main->vfont.first;
1671         while(vf) {
1672                 if(vf->id.flag & LIB_NEEDLINK) {
1673                         vf->id.flag -= LIB_NEEDLINK;
1674                 }
1675                 vf= vf->id.next;
1676         }
1677 }
1678
1679 static void direct_link_vfont(FileData *fd, VFont *vf)
1680 {
1681         vf->data= NULL;
1682         vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
1683 }
1684
1685 /* ************ READ TEXT ****************** */
1686
1687 static void lib_link_text(FileData *fd, Main *main)
1688 {
1689         Text *text;
1690
1691         text= main->text.first;
1692         while(text) {
1693                 if(text->id.flag & LIB_NEEDLINK) {
1694                         text->id.flag -= LIB_NEEDLINK;
1695                 }
1696                 text= text->id.next;
1697         }
1698 }
1699
1700 static void direct_link_text(FileData *fd, Text *text)
1701 {
1702         TextLine *ln;
1703
1704         text->name= newdataadr(fd, text->name);
1705
1706         text->undo_pos= -1;
1707         text->undo_len= TXT_INIT_UNDO;
1708         text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
1709
1710         text->compiled= NULL;
1711
1712 /*
1713         if(text->flags & TXT_ISEXT) {
1714                 reopen_text(text);
1715         } else {
1716 */
1717
1718         link_list(fd, &text->lines);
1719
1720         text->curl= newdataadr(fd, text->curl);
1721         text->sell= newdataadr(fd, text->sell);
1722
1723         ln= text->lines.first;
1724         while(ln) {
1725                 ln->line= newdataadr(fd, ln->line);
1726
1727                 if (ln->len != (int) strlen(ln->line)) {
1728                         printf("Error loading text, line lengths differ\n");
1729                         ln->len = strlen(ln->line);
1730                 }
1731
1732                 ln= ln->next;
1733         }
1734
1735         text->flags = (text->flags|TXT_ISTMP) & ~TXT_ISEXT;
1736
1737         text->id.us= 1;
1738 }
1739
1740 /* ************ READ IMAGE ***************** */
1741
1742 static void lib_link_image(FileData *fd, Main *main)
1743 {
1744         Image *ima;
1745
1746         ima= main->image.first;
1747         while (ima) {
1748                 if(ima->id.flag & LIB_NEEDLINK) {
1749
1750                         ima->id.flag -= LIB_NEEDLINK;
1751                 }
1752                 ima= ima->id.next;
1753         }
1754 }
1755
1756 static void direct_link_image(FileData *fd, Image *ima)
1757 {
1758         ima->ibuf= 0;
1759         ima->anim= 0;
1760         memset(ima->mipmap, 0, sizeof(ima->mipmap));
1761         ima->repbind= 0;
1762         ima->bindcode= 0;
1763
1764         ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
1765
1766         ima->ok= 1;
1767 }
1768
1769
1770 /* ************ READ CURVE ***************** */
1771
1772 static void lib_link_curve(FileData *fd, Main *main)
1773 {
1774         Curve *cu;
1775         int a;
1776
1777         cu= main->curve.first;
1778         while(cu) {
1779                 if(cu->id.flag & LIB_NEEDLINK) {
1780
1781                         for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
1782
1783                         cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
1784                         cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
1785                         cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
1786
1787                         cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo);
1788                         cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
1789
1790                         cu->id.flag -= LIB_NEEDLINK;
1791                 }
1792                 cu= cu->id.next;
1793         }
1794 }
1795
1796
1797 static void switch_endian_knots(Nurb *nu)
1798 {
1799         int len;
1800
1801         if(nu->knotsu) {
1802                 len= KNOTSU(nu);
1803                 while(len--) {
1804                         SWITCH_INT(nu->knotsu[len]);
1805                 }
1806         }
1807         if(nu->knotsv) {
1808                 len= KNOTSV(nu);
1809                 while(len--) {
1810                         SWITCH_INT(nu->knotsv[len]);
1811                 }
1812         }
1813 }
1814
1815 static void direct_link_curve(FileData *fd, Curve *cu)
1816 {
1817         Nurb *nu;
1818
1819         cu->mat= newdataadr(fd, cu->mat);
1820         test_pointer_array(fd, (void **)&cu->mat);
1821         cu->str= newdataadr(fd, cu->str);
1822
1823         if(cu->vfont==0) link_list(fd, &(cu->nurb));
1824         else {
1825                 cu->nurb.first=cu->nurb.last= 0;
1826         }
1827
1828         cu->bev.first=cu->bev.last= 0;
1829         cu->disp.first=cu->disp.last= 0;
1830         cu->path= 0;
1831
1832         nu= cu->nurb.first;
1833         while(nu) {
1834                 nu->bezt= newdataadr(fd, nu->bezt);
1835                 nu->bp= newdataadr(fd, nu->bp);
1836                 nu->knotsu= newdataadr(fd, nu->knotsu);
1837                 nu->knotsv= newdataadr(fd, nu->knotsv);
1838
1839                 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1840                         switch_endian_knots(nu);
1841                 }
1842
1843                 nu= nu->next;
1844         }
1845         cu->bb= 0;
1846 }
1847
1848 /* ************ READ TEX ***************** */
1849
1850 static void lib_link_texture(FileData *fd, Main *main)
1851 {
1852         Tex *tex;
1853
1854         tex= main->tex.first;
1855         while(tex) {
1856                 if(tex->id.flag & LIB_NEEDLINK) {
1857
1858                         tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
1859                         tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
1860                         if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
1861
1862                         tex->id.flag -= LIB_NEEDLINK;
1863                 }
1864                 tex= tex->id.next;
1865         }
1866 }
1867
1868 static void direct_link_texture(FileData *fd, Tex *tex)
1869 {
1870         tex->plugin= newdataadr(fd, tex->plugin);
1871         if(tex->plugin) {
1872                 tex->plugin->handle= 0;
1873                 open_plugin_tex(tex->plugin);
1874         }
1875         tex->coba= newdataadr(fd, tex->coba);
1876         tex->env= newdataadr(fd, tex->env);
1877         if(tex->env) {
1878                 tex->env->ima= 0;
1879                 memset(tex->env->cube, 0, 6*sizeof(void *));
1880                 tex->env->ok= 0;
1881         }
1882 }
1883
1884
1885
1886 /* ************ READ MATERIAL ***************** */
1887
1888 static void lib_link_material(FileData *fd, Main *main)
1889 {
1890         Material *ma;
1891         MTex *mtex;
1892         int a;
1893
1894         ma= main->mat.first;
1895         while(ma) {
1896                 if(ma->id.flag & LIB_NEEDLINK) {
1897
1898                         ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
1899
1900                         for(a=0; a<8; a++) {
1901                                 mtex= ma->mtex[a];
1902                                 if(mtex) {
1903                                         mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
1904                                         mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
1905                                 }
1906                         }
1907                         lib_link_scriptlink(fd, &ma->id, &ma->scriptlink);
1908                         ma->id.flag -= LIB_NEEDLINK;
1909                 }
1910                 ma= ma->id.next;
1911         }
1912 }
1913
1914 static void direct_link_material(FileData *fd, Material *ma)
1915 {
1916         int a;
1917
1918         direct_link_scriptlink(fd, &ma->scriptlink);
1919
1920         for(a=0; a<8; a++) {
1921                 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
1922         }
1923
1924         ma->ramp_col= newdataadr(fd, ma->ramp_col);
1925         ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
1926         
1927         ma->ren= NULL;  /* should not be needed, nevertheless... */
1928 }
1929
1930 /* ************ READ MESH ***************** */
1931
1932 static void lib_link_mesh(FileData *fd, Main *main)
1933 {
1934         Mesh *me;
1935
1936         me= main->mesh.first;
1937         while(me) {
1938                 if(me->id.flag & LIB_NEEDLINK) {
1939                         int i;
1940
1941                         /* this check added for python created meshes */
1942                         if(me->mat) {
1943                                 for(i=0; i<me->totcol; i++)
1944                                         me->mat[i]= newlibadr_us(fd, me->id.lib, me->mat[i]);
1945                         }
1946                         else me->totcol= 0;
1947
1948                         me->ipo= newlibadr_us(fd, me->id.lib, me->ipo);
1949                         me->key= newlibadr_us(fd, me->id.lib, me->key);
1950                         me->texcomesh= newlibadr_us(fd, me->id.lib, me->texcomesh);
1951
1952                         if(me->tface) {
1953                                 TFace *tfaces= me->tface;
1954
1955                                 for (i=0; i<me->totface; i++) {
1956                                         TFace *tf= &tfaces[i];
1957
1958                                         tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
1959                                         if(tf->tpage) {
1960                                                 Image *ima= tf->tpage;
1961                                                 if(ima->id.us==0)
1962                                                         ima->id.us= 1;
1963                                         }
1964                                 }
1965                         }
1966                         me->id.flag -= LIB_NEEDLINK;
1967                 }
1968                 me= me->id.next;
1969         }
1970 }
1971
1972 static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
1973 {
1974         int     i, j;
1975
1976         if (!mdverts)
1977                 return;
1978
1979         for (i=0; i<count; i++) {
1980                 mdverts[i].dw=newdataadr(fd, mdverts[i].dw);
1981                 if (!mdverts[i].dw)
1982                         mdverts[i].totweight=0;
1983
1984                 for (j=0; j< mdverts[i].totweight; j++) {
1985                         mdverts[i].dw[j].data = NULL;   // not saved in file, clear pointer
1986                 }
1987         }
1988 }
1989
1990 static void direct_link_mesh(FileData *fd, Mesh *mesh)
1991 {
1992         mesh->mat= newdataadr(fd, mesh->mat);
1993         test_pointer_array(fd, (void **)&mesh->mat);
1994         mesh->mvert= newdataadr(fd, mesh->mvert);
1995
1996         mesh->dvert= newdataadr(fd, mesh->dvert);
1997         direct_link_dverts(fd, mesh->totvert, mesh->dvert);
1998
1999         mesh->mface= newdataadr(fd, mesh->mface);
2000         mesh->tface= newdataadr(fd, mesh->tface);
2001         mesh->mcol= newdataadr(fd, mesh->mcol);
2002         mesh->msticky= newdataadr(fd, mesh->msticky);
2003
2004         mesh->disp.first= mesh->disp.last= 0;
2005         mesh->bb= 0;
2006         mesh->oc= 0;
2007         mesh->dface= 0;
2008         mesh->orco= 0;
2009
2010         if (mesh->tface) {
2011                 TFace *tfaces= mesh->tface;
2012                 int i;
2013
2014                 for (i=0; i<mesh->totface; i++) {
2015                         TFace *tf= &tfaces[i];
2016
2017                         if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2018                                 SWITCH_INT(tf->col[0]);
2019                                 SWITCH_INT(tf->col[1]);
2020                                 SWITCH_INT(tf->col[2]);
2021                                 SWITCH_INT(tf->col[3]);
2022                         }
2023                 }
2024         }
2025 }
2026
2027 /* ************ READ OBJECT ***************** */
2028
2029 static void lib_link_object(FileData *fd, Main *main)
2030 {
2031         Object *ob;
2032         bSensor *sens;
2033         bController *cont;
2034         bActuator *act;
2035
2036         void *poin;
2037         int warn=0, a;
2038
2039         ob= main->object.first;
2040         while(ob) {
2041                 if(ob->id.flag & LIB_NEEDLINK) {
2042
2043                         ob->parent= newlibadr(fd, ob->id.lib, ob->parent);
2044                         ob->track= newlibadr(fd, ob->id.lib, ob->track);
2045                         ob->ipo= newlibadr_us(fd, ob->id.lib, ob->ipo);
2046                         ob->action = newlibadr_us(fd, ob->id.lib, ob->action);
2047
2048 //                      ob->activecon = newglobadr(fd, ob->activecon);
2049
2050                         poin= ob->data;
2051                         ob->data= newlibadr_us(fd, ob->id.lib, ob->data);
2052
2053                         if(ob->data==NULL && poin!=NULL) {
2054                                 ob->type= OB_EMPTY;
2055                                 warn= 1;
2056                                 if(ob->id.lib) printf("Can't find obdata of %s lib %s\n", ob->id.name+2, ob->id.lib->name);
2057                                 else printf("Object %s lost data. Lib:%x\n", ob->id.name+2, (unsigned int) ob->id.lib);
2058                         }
2059                         for(a=0; a<ob->totcol; a++) ob->mat[a]= newlibadr_us(fd, ob->id.lib, ob->mat[a]);
2060
2061                         ob->id.flag -= LIB_NEEDLINK;
2062                         /* if id.us==0 a new base will be created later on */
2063
2064                         /* WARNING! Also check expand_object(), should reflect the stuff below. */
2065                         lib_link_pose(fd, &ob->id, ob->pose);
2066                         lib_link_constraints(fd, &ob->id, &ob->constraints);
2067                         lib_link_nlastrips(fd, &ob->id, &ob->nlastrips);
2068                         lib_link_constraint_channels(fd, &ob->id, &ob->constraintChannels);
2069
2070
2071                         sens= ob->sensors.first;
2072                         while(sens) {
2073                                 for(a=0; a<sens->totlinks; a++) {
2074                                         sens->links[a]= newglobadr(fd, sens->links[a]);
2075                                 }
2076                                 if(sens->type==SENS_TOUCH) {
2077                                         bTouchSensor *ts= sens->data;
2078                                         ts->ma= newlibadr(fd, ob->id.lib, ts->ma);
2079                                 }
2080                                 else if(sens->type==SENS_MESSAGE) {
2081                                         bMessageSensor *ms= sens->data;
2082                                         ms->fromObject=
2083                                             newlibadr(fd, ob->id.lib, ms->fromObject);
2084                                 }
2085                                 sens= sens->next;
2086                         }
2087
2088                         cont= ob->controllers.first;
2089                         while(cont) {
2090                                 for(a=0; a<cont->totlinks; a++) {
2091                                         cont->links[a]= newglobadr(fd, cont->links[a]);
2092                                 }
2093                                 if(cont->type==CONT_PYTHON) {
2094                                         bPythonCont *pc= cont->data;
2095                                         pc->text= newlibadr(fd, ob->id.lib, pc->text);
2096                                 }
2097                                 cont->slinks= NULL;
2098                                 cont->totslinks= 0;
2099
2100                                 cont= cont->next;
2101                         }
2102
2103                         act= ob->actuators.first;
2104                         while(act) {
2105                                 if(act->type==ACT_SOUND) {
2106                                         bSoundActuator *sa= act->data;
2107                                         sa->sound= newlibadr_us(fd, ob->id.lib, sa->sound);
2108                                 }
2109                                 else if(act->type==ACT_CD) {
2110                                         /* bCDActuator *cda= act->data; */
2111                                 }
2112                                 else if(act->type==ACT_GAME) {
2113                                         /* bGameActuator *ga= act->data; */
2114                                 }
2115                                 else if(act->type==ACT_CAMERA) {
2116                                         bCameraActuator *ca= act->data;
2117                                         ca->ob= newlibadr(fd, ob->id.lib, ca->ob);
2118                                 }
2119                                         /* leave this one, it's obsolete but necessary to read for conversion */
2120                                 else if(act->type==ACT_ADD_OBJECT) {
2121                                         bAddObjectActuator *eoa= act->data;
2122                                         if(eoa) eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
2123                                 }
2124                                 else if(act->type==ACT_EDIT_OBJECT) {
2125                                         bEditObjectActuator *eoa= act->data;
2126                                         if(eoa==NULL) {
2127                                                 init_actuator(act);
2128                                         }
2129                                         eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
2130                                         eoa->me= newlibadr(fd, ob->id.lib, eoa->me);
2131                                 }
2132                                 else if(act->type==ACT_SCENE) {
2133                                         bSceneActuator *sa= act->data;
2134                                         sa->camera= newlibadr(fd, ob->id.lib, sa->camera);
2135                                         sa->scene= newlibadr(fd, ob->id.lib, sa->scene);
2136                                 }
2137                                 else if(act->type==ACT_ACTION) {
2138                                         bActionActuator *aa= act->data;
2139                                         aa->act= newlibadr(fd, ob->id.lib, aa->act);
2140                                 }
2141                                 else if(act->type==ACT_PROPERTY) {
2142                                         bPropertyActuator *pa= act->data;
2143                                         pa->ob= newlibadr(fd, ob->id.lib, pa->ob);
2144                                 }
2145                                 else if(act->type==ACT_MESSAGE) {
2146                                         bMessageActuator *ma= act->data;
2147                                         ma->toObject= newlibadr(fd, ob->id.lib, ma->toObject);
2148                                 }
2149                                 act= act->next;
2150                         }
2151
2152                         lib_link_scriptlink(fd, &ob->id, &ob->scriptlink);
2153                 }
2154                 ob= ob->id.next;
2155         }
2156
2157         if(warn) error("WARNING IN CONSOLE");
2158 }
2159
2160
2161 static void direct_link_pose(FileData *fd, bPose *pose) {
2162
2163         bPoseChannel *chan;
2164
2165         if (!pose)
2166                 return;
2167
2168         link_list(fd, &pose->chanbase);
2169
2170         for (chan = pose->chanbase.first; chan; chan=chan->next) {
2171                 direct_link_constraints(fd, &chan->constraints);
2172         }
2173
2174 }
2175
2176 static void direct_link_object(FileData *fd, Object *ob)
2177 {
2178         PartEff *paf;
2179         bProperty *prop;
2180         bSensor *sens;
2181         bController *cont;
2182         bActuator *act;
2183
2184         ob->disp.first=ob->disp.last= 0;
2185
2186         ob->pose= newdataadr(fd, ob->pose);
2187         direct_link_pose(fd, ob->pose);
2188
2189         link_list(fd, &ob->defbase);
2190         link_list(fd, &ob->nlastrips);
2191         link_list(fd, &ob->constraintChannels);
2192
2193         ob->activecon = newdataadr(fd, ob->activecon);
2194
2195         direct_link_scriptlink(fd, &ob->scriptlink);
2196
2197         ob->mat= newdataadr(fd, ob->mat);
2198         test_pointer_array(fd, (void **)&ob->mat);
2199         link_list(fd, &ob->effect);
2200         paf= ob->effect.first;
2201         while(paf) {
2202                 if(paf->type==EFF_PARTICLE) {
2203                         paf->keys= 0;
2204                 }
2205                 if(paf->type==EFF_WAVE) {
2206
2207                 }
2208                 paf= paf->next;
2209         }
2210
2211         ob->pd= newdataadr(fd, ob->pd);
2212
2213         link_list(fd, &ob->prop);
2214         prop= ob->prop.first;
2215         while(prop) {
2216                 prop->poin= newdataadr(fd, prop->poin);
2217                 if(prop->poin==0) prop->poin= &prop->data;
2218                 prop= prop->next;
2219         }
2220
2221         link_list(fd, &ob->sensors);
2222         sens= ob->sensors.first;
2223         while(sens) {
2224                 sens->data= newdataadr(fd, sens->data);
2225                 sens->links= newdataadr(fd, sens->links);
2226                 test_pointer_array(fd, (void **)&sens->links);
2227                 sens= sens->next;
2228         }
2229
2230         direct_link_constraints(fd, &ob->constraints);
2231
2232         link_glob_list(fd, &ob->controllers);
2233         cont= ob->controllers.first;
2234         while(cont) {
2235                 cont->data= newdataadr(fd, cont->data);
2236                 cont->links= newdataadr(fd, cont->links);
2237                 test_pointer_array(fd, (void **)&cont->links);
2238                 cont= cont->next;
2239         }
2240
2241         link_glob_list(fd, &ob->actuators);
2242         act= ob->actuators.first;
2243         while(act) {
2244                 act->data= newdataadr(fd, act->data);
2245                 act= act->next;
2246         }
2247
2248         ob->bb= 0;
2249 }
2250
2251 /* ************ READ SCENE ***************** */
2252
2253 static void lib_link_scene(FileData *fd, Main *main)
2254 {
2255         Scene *sce;
2256         Base *base, *next;
2257         Editing *ed;
2258         Sequence *seq;
2259
2260         sce= main->scene.first;
2261         while(sce) {
2262                 if(sce->id.flag & LIB_NEEDLINK) {
2263                         sce->id.us= 1;
2264                         sce->camera= newlibadr(fd, sce->id.lib, sce->camera);
2265                         sce->world= newlibadr_us(fd, sce->id.lib, sce->world);
2266                         sce->set= newlibadr(fd, sce->id.lib, sce->set);
2267                         sce->ima= newlibadr_us(fd, sce->id.lib, sce->ima);
2268                         sce->group= newlibadr_us(fd, sce->id.lib, sce->group);
2269
2270                         base= sce->base.first;
2271                         while(base) {
2272                                 next= base->next;
2273
2274                                 /* base->object= newlibadr_us(fd, sce->id.lib, base->object); */
2275
2276                                 base->object= newlibadr_us_type(fd, ID_OB, base->object);
2277
2278                                 if(base->object==0) {
2279                                         printf("LIB ERROR: base removed\n");
2280                                         BLI_remlink(&sce->base, base);
2281                                         if(base==sce->basact) sce->basact= 0;
2282                                         MEM_freeN(base);
2283                                 }
2284                                 base= next;
2285                         }
2286
2287                         ed= sce->ed;
2288                         if(ed) {
2289                                 WHILE_SEQ(ed->seqbasep) {
2290                                         if(seq->ipo) seq->ipo= newlibadr_us(fd, sce->id.lib, seq->ipo);
2291                                         if(seq->scene) seq->scene= newlibadr(fd, sce->id.lib, seq->scene);
2292                                         if(seq->sound) {
2293                                                 seq->sound= newlibadr(fd, sce->id.lib, seq->sound);
2294                                                 if (seq->sound) {
2295                                                         seq->sound->id.us++;
2296                                                         seq->sound->flags |= SOUND_FLAGS_SEQUENCE;
2297                                                 }
2298                                         }
2299                                         seq->anim= 0;
2300                                 }
2301                                 END_SEQ
2302                         }
2303                         sce->id.flag -= LIB_NEEDLINK;
2304                 }
2305
2306                 lib_link_scriptlink(fd, &sce->id, &sce->scriptlink);
2307
2308                 sce= sce->id.next;
2309         }
2310 }
2311
2312 static void link_recurs_seq(FileData *fd, ListBase *lb)
2313 {
2314         Sequence *seq;
2315
2316         link_list(fd, lb);
2317         seq= lb->first;
2318         while(seq) {
2319                 if(seq->seqbase.first) link_recurs_seq(fd, &seq->seqbase);
2320                 seq= seq->next;
2321         }
2322 }
2323
2324 static void direct_link_scene(FileData *fd, Scene *sce)
2325 {
2326         Editing *ed;
2327         Sequence *seq;
2328         StripElem *se;
2329         int a;
2330
2331         link_list(fd, &(sce->base));
2332
2333         sce->basact= newdataadr(fd, sce->basact);
2334
2335         sce->radio= newdataadr(fd, sce->radio);
2336         sce->fcam= newdataadr(fd, sce->fcam);
2337
2338         sce->r.avicodecdata = newdataadr(fd, sce->r.avicodecdata);
2339         if (sce->r.avicodecdata) {
2340                 sce->r.avicodecdata->lpFormat = newdataadr(fd, sce->r.avicodecdata->lpFormat);
2341                 sce->r.avicodecdata->lpParms = newdataadr(fd, sce->r.avicodecdata->lpParms);
2342         }
2343
2344         sce->r.qtcodecdata = newdataadr(fd, sce->r.qtcodecdata);
2345         if (sce->r.qtcodecdata) {
2346                 sce->r.qtcodecdata->cdParms = newdataadr(fd, sce->r.qtcodecdata->cdParms);
2347         }
2348
2349         if(sce->ed) {
2350                 ed= sce->ed= newdataadr(fd, sce->ed);
2351
2352                 ed->metastack.first= ed->metastack.last= 0;
2353
2354                 /* recursive link sequences, lb will be correctly initialized */
2355                 link_recurs_seq(fd, &ed->seqbase);
2356
2357                 ed->seqbasep= &ed->seqbase;
2358
2359                 WHILE_SEQ(ed->seqbasep) {
2360                         seq->seq1= newdataadr(fd, seq->seq1);
2361                         seq->seq2= newdataadr(fd, seq->seq2);
2362                         seq->seq3= newdataadr(fd, seq->seq3);
2363                         /* a patch: after introduction of effects with 3 input strips */
2364                         if(seq->seq3==0) seq->seq3= seq->seq2;
2365
2366                         seq->curelem= 0;
2367
2368                         seq->plugin= newdataadr(fd, seq->plugin);
2369                         if(seq->plugin) open_plugin_seq(seq->plugin, seq->name+2);
2370
2371                         seq->effectdata= newdataadr(fd, seq->effectdata);
2372
2373                         seq->strip= newdataadr(fd, seq->strip);
2374                         if(seq->strip && seq->strip->done==0) {
2375                                 seq->strip->done= 1;
2376
2377                                 /* standard: strips from effects/metas are not written, but are mallocced */
2378
2379                                 if(seq->type==SEQ_IMAGE) {
2380                                         seq->strip->stripdata= newdataadr(fd, seq->strip->stripdata);
2381                                         se= seq->strip->stripdata;
2382                                         if(se) {
2383                                                 for(a=0; a<seq->strip->len; a++, se++) {
2384                                                         se->ok= 1;
2385                                                         se->ibuf= 0;
2386                                                 }
2387                                         }
2388                                 }
2389                                 else if(seq->type==SEQ_MOVIE) {
2390                                         /* only first stripelem is in file */
2391                                         se= newdataadr(fd, seq->strip->stripdata);
2392
2393                                         if(se) {
2394                                                 seq->strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
2395                                                 *seq->strip->stripdata= *se;
2396                                                 MEM_freeN(se);
2397
2398                                                 se= seq->strip->stripdata;
2399
2400                                                 for(a=0; a<seq->strip->len; a++, se++) {
2401                                                         se->ok= 1;
2402                                                         se->ibuf= 0;
2403                                                         se->nr= a + 1;
2404                                                 }
2405                                         }
2406                                 }
2407                                 else if(seq->type==SEQ_SOUND) {
2408                                         /* only first stripelem is in file */
2409                                         se= newdataadr(fd, seq->strip->stripdata);
2410
2411                                         if(se) {
2412                                                 seq->strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
2413                                                 *seq->strip->stripdata= *se;
2414                                                 MEM_freeN(se);
2415
2416                                                 se= seq->strip->stripdata;
2417
2418                                                 for(a=0; a<seq->strip->len; a++, se++) {
2419                                                         se->ok= 2; /* why? */
2420                                                         se->ibuf= 0;
2421                                                         se->nr= a + 1;
2422                                                 }
2423                                         }
2424                                 }
2425                                 else if(seq->len>0)
2426                                         seq->strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
2427
2428                         }
2429                 }
2430                 END_SEQ
2431         }
2432
2433         direct_link_scriptlink(fd, &sce->scriptlink);
2434 }
2435
2436 /* ************ READ SCREEN ***************** */
2437
2438 static void lib_link_screen(FileData *fd, Main *main)
2439 {
2440         bScreen *sc;
2441         ScrArea *sa;
2442
2443         sc= main->screen.first;
2444         while(sc) {
2445                 if(sc->id.flag & LIB_NEEDLINK) {
2446                         sc->id.us= 1;
2447                         sc->scene= newlibadr(fd, sc->id.lib, sc->scene);
2448
2449                         sa= sc->areabase.first;
2450                         while(sa) {
2451                                 SpaceLink *sl;
2452
2453                                 sa->full= newlibadr(fd, sc->id.lib, sa->full);
2454
2455                                 for (sl= sa->spacedata.first; sl; sl= sl->next) {
2456                                         if(sl->spacetype==SPACE_VIEW3D) {
2457                                                 View3D *v3d= (View3D*) sl;
2458
2459                                                 v3d->camera= newlibadr(fd, sc->id.lib, v3d->camera);
2460
2461                                                 if(v3d->bgpic) {
2462                                                         v3d->bgpic->ima= newlibadr_us(fd, sc->id.lib, v3d->bgpic->ima);
2463                                                         v3d->bgpic->tex= newlibadr_us(fd, sc->id.lib, v3d->bgpic->tex);
2464                                                         v3d->bgpic->rect= 0;
2465                                                 }
2466                                                 if(v3d->localvd) {
2467                                                         v3d->localvd->camera= newlibadr(fd, sc->id.lib, v3d->localvd->camera);
2468                                                 }
2469                                         }
2470                                         else if(sl->spacetype==SPACE_IPO) {
2471                                                 SpaceIpo *sipo= (SpaceIpo *)sl;
2472                                                 sipo->editipo= 0;
2473                                                 sipo->from= newlibadr(fd, sc->id.lib, sipo->from);
2474                                                 sipo->ipokey.first= sipo->ipokey.last= 0;
2475                                                 sipo->ipo= newlibadr(fd, sc->id.lib, sipo->ipo);
2476                                         }
2477                                         else if(sl->spacetype==SPACE_BUTS) {
2478                                                 SpaceButs *sbuts= (SpaceButs *)sl;
2479                                                 sbuts->rect= 0;
2480                                                 sbuts->lockpoin= 0;
2481                                                 if(main->versionfile<132) set_rects_butspace(sbuts);
2482                                         }
2483                                         else if(sl->spacetype==SPACE_FILE) {
2484                                                 SpaceFile *sfile= (SpaceFile *)sl;
2485
2486                                                 sfile->filelist= 0;
2487                                                 sfile->libfiledata= 0;
2488                                                 sfile->returnfunc= 0;
2489                                         }
2490                                         else if(sl->spacetype==SPACE_IMASEL) {
2491                                                 check_imasel_copy((SpaceImaSel *)sl);
2492                                         }
2493                                         else if(sl->spacetype==SPACE_ACTION) {
2494                                                 SpaceAction *saction= (SpaceAction *)sl;
2495                                                 saction->action = newlibadr(fd, sc->id.lib, saction->action);
2496                                         }
2497                                         else if(sl->spacetype==SPACE_IMAGE) {
2498                                                 SpaceImage *sima= (SpaceImage *)sl;
2499
2500                                                 sima->image= newlibadr_us(fd, sc->id.lib, sima->image);
2501                                         }
2502                                         else if(sl->spacetype==SPACE_NLA){
2503                                                 /* SpaceNla *snla= (SpaceNla *)sl;      */
2504                                         }
2505                                         else if(sl->spacetype==SPACE_TEXT) {
2506                                                 SpaceText *st= (SpaceText *)sl;
2507
2508                                                 st->text= newlibadr(fd, sc->id.lib, st->text);
2509
2510                                         }
2511                                         else if(sl->spacetype==SPACE_SCRIPT) {
2512                                                 SpaceScript *sc= (SpaceScript *)sl;
2513
2514                                                 sc->script = NULL;
2515                                         }
2516                                         else if(sl->spacetype==SPACE_OOPS) {
2517                                                 SpaceOops *so= (SpaceOops *)sl;
2518                                                 Oops *oops;
2519
2520                                                 oops= so->oops.first;
2521                                                 while(oops) {
2522                                                         oops->id= newlibadr(fd, 0, oops->id);
2523                                                         oops= oops->next;
2524                                                 }
2525                                                 so->lockpoin= 0;
2526                                         }
2527                                         else if(sl->spacetype==SPACE_SOUND) {
2528                                                 SpaceSound *ssound= (SpaceSound *)sl;
2529
2530                                                 ssound->sound= newlibadr_us(fd, sc->id.lib, ssound->sound);
2531                                         }
2532                                 }
2533                                 sa= sa->next;
2534                         }
2535                         sc->id.flag -= LIB_NEEDLINK;
2536                 }
2537                 sc= sc->id.next;
2538         }
2539 }
2540
2541 static void direct_link_screen(FileData *fd, bScreen *sc)
2542 {
2543         ScrArea *sa;
2544         ScrVert *sv;
2545         ScrEdge *se;
2546         Oops *oops;
2547
2548         link_list(fd, &(sc->vertbase));
2549         link_list(fd, &(sc->edgebase));
2550         link_list(fd, &(sc->areabase));
2551         sc->winakt= 0;
2552
2553         /* edges */
2554         se= sc->edgebase.first;
2555         while(se) {
2556                 se->v1= newdataadr(fd, se->v1);
2557                 se->v2= newdataadr(fd, se->v2);
2558                 if( (long)se->v1 > (long)se->v2) {
2559                         sv= se->v1;
2560                         se->v1= se->v2;
2561                         se->v2= sv;
2562                 }
2563
2564                 if(se->v1==NULL) {
2565                         printf("error reading screen... file corrupt\n");
2566                         se->v1= se->v2;
2567                 }
2568                 se= se->next;
2569         }
2570
2571         /* areas */
2572         sa= sc->areabase.first;
2573         while(sa) {
2574                 Panel *pa;
2575                 SpaceLink *sl;
2576
2577                 link_list(fd, &(sa->spacedata));
2578                 link_list(fd, &(sa->panels));
2579
2580                 for(pa= sa->panels.first; pa; pa=pa->next) {
2581                         pa->paneltab= newdataadr(fd, pa->paneltab);
2582                         pa->active= 0;
2583                 }
2584
2585                 for (sl= sa->spacedata.first; sl; sl= sl->next) {
2586                         if (sl->spacetype==SPACE_VIEW3D) {
2587                                 View3D *v3d= (View3D*) sl;
2588                                 v3d->bgpic= newdataadr(fd, v3d->bgpic);
2589                                 v3d->localvd= newdataadr(fd, v3d->localvd);
2590                         }
2591                         else if (sl->spacetype==SPACE_OOPS) {
2592                                 SpaceOops *soops= (SpaceOops*) sl;
2593                                 link_list(fd, &(soops->oops));
2594                                 oops= soops->oops.first;
2595                                 while(oops) {
2596                                         oops->link.first= oops->link.last= 0;
2597                                         oops= oops->next;
2598                                 }
2599                         }
2600                 }
2601
2602                 sa->v1= newdataadr(fd, sa->v1);
2603                 sa->v2= newdataadr(fd, sa->v2);
2604                 sa->v3= newdataadr(fd, sa->v3);
2605                 sa->v4= newdataadr(fd, sa->v4);
2606
2607                 sa->win= sa->headwin= 0;
2608
2609                 sa->uiblocks.first= sa->uiblocks.last= NULL;
2610
2611                 sa= sa->next;
2612         }
2613 }
2614
2615 /* ********** READ LIBRARY *************** */
2616
2617
2618 static void direct_link_library(FileData *fd, Library *lib)
2619 {
2620         Main *newmain;
2621
2622         /* new main */
2623         newmain= MEM_callocN(sizeof(Main), "directlink");
2624         BLI_addtail(&fd->mainlist, newmain);
2625         newmain->curlib= lib;
2626 }
2627
2628 static void lib_link_library(FileData *fd, Main *main)
2629 {
2630         Library *lib;
2631
2632         lib= main->library.first;
2633         while(lib) {
2634                 lib->id.us= 1;
2635                 lib= lib->id.next;
2636         }
2637 }
2638
2639 /* ************** READ SOUND ******************* */
2640
2641 static void direct_link_sound(FileData *fd, bSound *sound)
2642 {
2643         sound->sample = NULL;
2644         sound->snd_sound = NULL;
2645
2646         sound->packedfile = direct_link_packedfile(fd, sound->packedfile);
2647         sound->newpackedfile = direct_link_packedfile(fd, sound->newpackedfile);
2648 }
2649
2650 static void lib_link_sound(FileData *fd, Main *main)
2651 {
2652         bSound *sound;
2653
2654         sound= main->sound.first;
2655         while(sound) {
2656                 if(sound->id.flag & LIB_NEEDLINK) {
2657                         sound->id.flag -= LIB_NEEDLINK;
2658                         sound->ipo= newlibadr_us(fd, sound->id.lib, sound->ipo);
2659                         sound->stream = 0;
2660                 }
2661                 sound= sound->id.next;
2662         }
2663 }
2664 /* ***************** READ GROUP *************** */
2665
2666 static void direct_link_group(FileData *fd, Group *group)
2667 {
2668         GroupObject *go;
2669         ObjectKey *ok;
2670
2671         link_list(fd, &group->gobject);
2672         link_list(fd, &group->gkey);
2673         group->active= newdataadr(fd, group->active);
2674
2675         go= group->gobject.first;
2676         while(go) {
2677                 link_list(fd, &go->okey);
2678                 ok= go->okey.first;
2679                 while(ok) {
2680                         ok->gkey= newdataadr(fd, ok->gkey);
2681                         ok= ok->next;
2682                 }
2683                 go= go->next;
2684         }
2685 }
2686
2687 static void lib_link_group(FileData *fd, Main *main)
2688 {
2689         Group *group= main->group.first;
2690         GroupObject *go;
2691         ObjectKey *ok;
2692
2693         while(group) {
2694                 if(group->id.flag & LIB_NEEDLINK) {
2695                         group->id.flag -= LIB_NEEDLINK;
2696
2697                         go= group->gobject.first;
2698                         while(go) {
2699                                 go->ob= newlibadr(fd, group->id.lib, go->ob);
2700                                 ok= go->okey.first;
2701                                 while(ok) {
2702                                         ok->parent= newlibadr(fd, group->id.lib, ok->parent);
2703                                         ok->track= newlibadr(fd, group->id.lib, ok->track);
2704                                         ok->ipo= newlibadr_us(fd, group->id.lib, ok->ipo);
2705                                         ok= ok->next;
2706                                 }
2707                                 go= go->next;
2708                         }
2709                 }
2710                 group= group->id.next;
2711         }
2712 }
2713
2714 /* ************** GENERAL & MAIN ******************** */
2715
2716 static BHead *read_libblock(FileData *fd, Main *main, BHead *bhead, int flag, ID **id_r)
2717 {
2718         /* this routine reads a libblock and its direct data. Use link functions
2719          * to connect it all
2720          */
2721
2722         ID *id;
2723         ListBase *lb;
2724         char *str = NULL;
2725
2726         if(bhead->code==ID_ID) {
2727                 ID *linkedid= (ID *)(bhead + 1); /*  BHEAD+DATA dependancy */
2728
2729                 lb= wich_libbase(main, GS(linkedid->name));
2730         }
2731         else {
2732                 lb= wich_libbase(main, bhead->code);
2733         }
2734
2735         /* read libblock */
2736         id = read_struct(fd, bhead, "lib block");
2737         if (id_r)
2738                 *id_r= id;
2739         if (!id)
2740                 return blo_nextbhead(fd, bhead);
2741
2742         oldnewmap_insert(fd->libmap, bhead->old, id, 1);
2743         BLI_addtail(lb, id);
2744
2745         /* clear first 8 bits */
2746         id->flag= (id->flag & 0xFF00) | flag | LIB_NEEDLINK;
2747         id->lib= main->curlib;
2748         if(id->flag & LIB_FAKEUSER) id->us= 1;
2749         else id->us= 0;
2750
2751         /* this case cannot be direct_linked: it's just the ID part */
2752         if(bhead->code==ID_ID) {
2753                 return blo_nextbhead(fd, bhead);
2754         }
2755
2756         bhead = blo_nextbhead(fd, bhead);
2757
2758         switch( GS(id->name) ) {
2759         case ID_OB: str= "ID_OB"; break;
2760         case ID_SCE: str= "ID_SCE"; break;
2761         case ID_LI: str= "ID_LI"; break;
2762         case ID_ME: str= "ID_ME"; break;
2763         case ID_CU: str= "ID_CU"; break;
2764         case ID_MB: str= "ID_MB"; break;
2765         case ID_MA: str= "ID_MA"; break;
2766         case ID_TE: str= "ID_TE"; break;
2767         case ID_IM: str= "ID_IM"; break;
2768         case ID_IK: str= "ID_IK"; break;
2769         case ID_WV: str= "ID_WV"; break;
2770         case ID_LT: str= "ID_LT"; break;
2771         case ID_SE: str= "ID_SE"; break;
2772         case ID_LF: str= "ID_LF"; break;
2773         case ID_LA: str= "ID_LA"; break;
2774         case ID_CA: str= "ID_CA"; break;
2775         case ID_IP: str= "ID_IP"; break;
2776         case ID_KE: str= "ID_KE"; break;
2777         case ID_WO: str= "ID_WO"; break;
2778         case ID_SCR: str= "ID_SCR"; break;
2779         case ID_VF: str= "ID_VF"; break;
2780         case ID_TXT     : str= "ID_TXT"; break;
2781         case ID_SO: str= "ID_SO"; break;
2782         case ID_SAMPLE: str= "ID_SAMPLE"; break;
2783         case ID_GR: str= "ID_GR"; break;
2784         case ID_ID: str= "ID_ID"; break;
2785         case ID_SEQ: str= "ID_SEQ"; break;
2786         case ID_AR: str= "ID_AR"; break;
2787         case ID_AC: str= "ID_AC"; break;
2788         case ID_SCRIPT: str= "ID_SCRIPT"; break;
2789         }
2790                 /* read all data */
2791         while(bhead && bhead->code==DATA) {
2792                 void *data= read_struct(fd, bhead, str);
2793
2794                 if (data) {
2795                         oldnewmap_insert(fd->datamap, bhead->old, data, 0);
2796                 }
2797
2798                 bhead = blo_nextbhead(fd, bhead);
2799         }
2800
2801         /* init pointers direct data */
2802         switch( GS(id->name) ) {
2803                 case ID_SCR:
2804                         direct_link_screen(fd, (bScreen *)id);
2805                         break;
2806                 case ID_SCE:
2807                         direct_link_scene(fd, (Scene *)id);
2808                         break;
2809                 case ID_OB:
2810                         direct_link_object(fd, (Object *)id);
2811                         break;
2812                 case ID_ME:
2813                         direct_link_mesh(fd, (Mesh *)id);
2814                         break;
2815                 case ID_CU:
2816                         direct_link_curve(fd, (Curve *)id);
2817                         break;
2818                 case ID_MB:
2819                         direct_link_mball(fd, (MetaBall *)id);
2820                         break;
2821                 case ID_MA:
2822                         direct_link_material(fd, (Material *)id);
2823                         break;
2824                 case ID_TE:
2825                         direct_link_texture(fd, (Tex *)id);
2826                         break;
2827                 case ID_IM:
2828                         direct_link_image(fd, (Image *)id);
2829                         break;
2830                 case ID_LA:
2831                         direct_link_lamp(fd, (Lamp *)id);
2832                         break;
2833                 case ID_VF:
2834                         direct_link_vfont(fd, (VFont *)id);
2835                         break;
2836                 case ID_TXT:
2837                         direct_link_text(fd, (Text *)id);
2838                         break;
2839                 case ID_IP:
2840                         direct_link_ipo(fd, (Ipo *)id);
2841                         break;
2842                 case ID_KE:
2843                         direct_link_key(fd, (Key *)id);
2844                         break;
2845                 case ID_LT:
2846                         direct_link_latt(fd, (Lattice *)id);
2847                         break;
2848                 case ID_IK:
2849                         direct_link_ika(fd, (Ika *)id);
2850                         break;
2851                 case ID_WO:
2852                         direct_link_world(fd, (World *)id);
2853                         break;
2854                 case ID_LI:
2855                         direct_link_library(fd, (Library *)id);
2856                         break;
2857                 case ID_CA:
2858                         direct_link_camera(fd, (Camera *)id);
2859                         break;
2860                 case ID_SO:
2861                         direct_link_sound(fd, (bSound *)id);
2862                         break;
2863                 case ID_GR:
2864                         direct_link_group(fd, (Group *)id);
2865                         break;
2866                 case ID_AR:
2867                         direct_link_armature(fd, (bArmature*)id);
2868                         break;
2869                 case ID_AC:
2870                         direct_link_action(fd, (bAction*)id);
2871                         break;
2872         }
2873
2874         oldnewmap_free_unused(fd->datamap);
2875         oldnewmap_clear(fd->datamap);
2876
2877         return (bhead);
2878 }
2879
2880 static void link_global(FileData *fd, BlendFileData *bfd, FileGlobal *fg)
2881 {
2882         // this is nonsense... will get rid of it once (ton)
2883         bfd->winpos= fg->winpos;
2884         bfd->fileflags= fg->fileflags;
2885         bfd->displaymode= fg->displaymode;
2886         bfd->globalf= fg->globalf;
2887         bfd->curscreen= newlibadr(fd, 0, fg->curscreen);
2888 }
2889
2890 static void vcol_to_fcol(Mesh *me)
2891 {
2892         MFace *mface;
2893         unsigned int *mcol, *mcoln, *mcolmain;
2894         int a;
2895
2896         if(me->totface==0 || me->mcol==0) return;
2897
2898         mcoln= mcolmain= MEM_mallocN(4*sizeof(int)*me->totface, "mcoln");
2899         mcol = (unsigned int *)me->mcol;
2900         mface= me->mface;
2901         for(a=me->totface; a>0; a--, mface++) {
2902                 mcoln[0]= mcol[mface->v1];
2903                 mcoln[1]= mcol[mface->v2];
2904                 mcoln[2]= mcol[mface->v3];
2905                 mcoln[3]= mcol[mface->v4];
2906                 mcoln+= 4;
2907         }
2908
2909         MEM_freeN(me->mcol);
2910         me->mcol= (MCol *)mcolmain;
2911 }
2912
2913 static int map_223_keybd_code_to_224_keybd_code(int code)
2914 {
2915         switch (code) {
2916         case 312:       return F12KEY;
2917         case 159:       return PADSLASHKEY;
2918         case 161:       return PAD0;
2919         case 154:       return PAD1;
2920         case 150:       return PAD2;
2921         case 155:       return PAD3;
2922         case 151:       return PAD4;
2923         case 156:       return PAD5;
2924         case 152:       return PAD6;
2925         case 157:       return PAD7;
2926         case 153:       return PAD8;
2927         case 158:       return PAD9;
2928         default: return code;
2929         }
2930 }
2931
2932 static void do_versions(Main *main)
2933 {
2934         /* watch it: pointers from libdata have not been converted */
2935
2936         if(main->versionfile == 100) {
2937                 /* tex->extend and tex->imageflag have changed: */
2938                 Tex *tex = main->tex.first;
2939                 while(tex) {
2940                         if(tex->id.flag & LIB_NEEDLINK) {
2941
2942                                 if(tex->extend==0) {
2943                                         if(tex->xrepeat || tex->yrepeat) tex->extend= TEX_REPEAT;
2944                                         else {
2945                                                 tex->extend= TEX_EXTEND;
2946                                                 tex->xrepeat= tex->yrepeat= 1;
2947                                         }
2948                                 }
2949
2950                                 if(tex->imaflag & TEX_ANIM5) {
2951                                         tex->imaflag |= TEX_MORKPATCH;
2952                                         tex->imaflag |= TEX_ANTIALI;
2953                                 }
2954                         }
2955                         tex= tex->id.next;
2956                 }
2957         }
2958         if(main->versionfile <= 101) {
2959                 /* frame mapping */
2960                 Scene *sce = main->scene.first;
2961                 while(sce) {
2962                         sce->r.framapto= 100;
2963                         sce->r.images= 100;
2964                         sce->r.framelen= 1.0;
2965                         sce= sce->id.next;
2966                 }
2967         }
2968         if(main->versionfile <= 102) {
2969                 /* init halo's at 1.0 */
2970                 Material *ma = main->mat.first;
2971                 while(ma) {
2972                         ma->add= 1.0;
2973                         ma= ma->id.next;
2974                 }
2975         }
2976         if(main->versionfile <= 103) {
2977                 /* new variable in object: colbits */
2978                 Object *ob = main->object.first;
2979                 int a;
2980                 while(ob) {
2981                         ob->colbits= 0;
2982                         if(ob->totcol) {
2983                                 for(a=0; a<ob->totcol; a++) {
2984                                         if(ob->mat[a]) ob->colbits |= (1<<a);
2985                                 }
2986                         }
2987                         ob= ob->id.next;
2988                 }
2989         }
2990         if(main->versionfile <= 104) {
2991                 /* timeoffs moved */
2992                 Object *ob = main->object.first;
2993                 while(ob) {
2994                         if(ob->transflag & 1) {
2995                                 ob->transflag -= 1;
2996                                 ob->ipoflag |= OB_OFFS_OB;
2997                         }
2998                         ob= ob->id.next;
2999                 }
3000         }
3001         if(main->versionfile <= 105) {
3002                 Object *ob = main->object.first;
3003                 while(ob) {
3004                         ob->dupon= 1; ob->dupoff= 0;
3005                         ob->dupsta= 1; ob->dupend= 100;
3006                         ob= ob->id.next;
3007                 }
3008         }
3009         if(main->versionfile <= 106) {
3010                 /* mcol changed */
3011                 Mesh *me = main->mesh.first;
3012                 while(me) {
3013                         if(me->mcol) vcol_to_fcol(me);
3014                         me= me->id.next;
3015                 }
3016
3017         }
3018         if(main->versionfile <= 107) {
3019                 Object *ob;
3020                 Scene *sce = main->scene.first;
3021                 while(sce) {
3022                         sce->r.mode |= R_GAMMA;
3023                         sce= sce->id.next;
3024                 }
3025                 ob= main->object.first;
3026                 while(ob) {
3027                         ob->ipoflag |= OB_OFFS_PARENT;
3028                         if(ob->dt==0) ob->dt= 3;
3029                         ob= ob->id.next;
3030                 }
3031
3032         }
3033         if(main->versionfile <= 109) {
3034                 /* new variable: gridlines */
3035                 bScreen *sc = main->screen.first;
3036                 while(sc) {
3037                         ScrArea *sa= sc->areabase.first;
3038                         while(sa) {
3039                                 SpaceLink *sl= sa->spacedata.first;
3040                                 while (sl) {
3041                                         if (sl->spacetype==SPACE_VIEW3D) {
3042                                                 View3D *v3d= (View3D*) sl;
3043
3044                                                 if (v3d->gridlines==0) v3d->gridlines= 20;
3045                                         }
3046                                         sl= sl->next;
3047                                 }
3048                                 sa= sa->next;
3049                         }
3050                         sc= sc->id.next;
3051                 }
3052         }
3053         if(main->versionfile <= 112) {
3054                 Mesh *me = main->mesh.first;
3055                 while(me) {
3056                         me->cubemapsize= 1.0;
3057                         me= me->id.next;
3058                 }
3059         }
3060         if(main->versionfile <= 113) {
3061                 Material *ma = main->mat.first;
3062                 while(ma) {
3063                         if(ma->flaresize==0.0) ma->flaresize= 1.0;
3064                         ma->subsize= 1.0;
3065                         ma->flareboost= 1.0;
3066                         ma= ma->id.next;
3067                 }
3068         }
3069         if(main->versionfile <= 114) {
3070                 Mesh *me= main->mesh.first;
3071                 MFace *mface;
3072                 int a_int;
3073
3074                 /* edge drawflags changed */
3075                 while(me) {
3076                         a_int= me->totface;
3077                         mface= me->mface;
3078                         while(a_int--) {
3079                                 if(mface->edcode & 16) {
3080                                         mface->edcode -= 16;
3081                                         mface->edcode |= ME_V3V1;
3082                                 }
3083                                 mface++;
3084                         }
3085                         me= me->id.next;
3086                 }
3087         }
3088
3089
3090         if(main->versionfile <= 134) {
3091                 Tex *tex = main->tex.first;
3092                 while (tex) {
3093                         if ((tex->rfac == 0.0) &&
3094                             (tex->gfac == 0.0) &&
3095                             (tex->bfac == 0.0)) {
3096                                 tex->rfac = 1.0;
3097                                 tex->gfac = 1.0;
3098                                 tex->bfac = 1.0;
3099                                 tex->filtersize = 1.0;
3100                         }
3101                         tex = tex->id.next;
3102                 }
3103         }
3104         if(main->versionfile <= 140) {
3105                 /* r-g-b-fac in texure */
3106                 Tex *tex = main->tex.first;
3107                 while (tex) {
3108                         if ((tex->rfac == 0.0) &&
3109                             (tex->gfac == 0.0) &&
3110                             (tex->bfac == 0.0)) {
3111                                 tex->rfac = 1.0;
3112                                 tex->gfac = 1.0;
3113                                 tex->bfac = 1.0;
3114                                 tex->filtersize = 1.0;
3115                         }
3116                         tex = tex->id.next;
3117                 }
3118         }
3119         if(main->versionfile <= 153) {
3120                 Scene *sce = main->scene.first;
3121                 while(sce) {
3122                         if(sce->r.blurfac==0.0) sce->r.blurfac= 1.0;
3123                         sce= sce->id.next;
3124                 }
3125         }
3126         if(main->versionfile <= 163) {
3127                 Scene *sce = main->scene.first;
3128                 while(sce) {
3129                         if(sce->r.frs_sec==0) sce->r.frs_sec= 25;
3130                         sce= sce->id.next;
3131                 }
3132         }
3133         if(main->versionfile <= 164) {
3134                 Mesh *me= main->mesh.first;
3135                 while(me) {
3136                         me->smoothresh= 30;
3137                         me= me->id.next;
3138                 }
3139         }
3140         if(main->versionfile <= 165) {
3141                 Mesh *me= main->mesh.first;
3142                 TFace *tface;
3143                 Ika *ika= main->ika.first;
3144                 Deform *def;
3145                 int nr;
3146                 char *cp;
3147
3148                 while(ika) {
3149                         ika->xyconstraint= .5;
3150
3151                         def= ika->def;
3152                         nr= ika->totdef;
3153                         while(nr--) {
3154                                 if(def->fac==0.0) def->fac= 1.0;
3155                                 def++;
3156                         }
3157                         ika= ika->id.next;
3158                 }
3159
3160                 while(me) {
3161                         if(me->tface) {
3162                                 nr= me->totface;
3163                                 tface= me->tface;
3164                                 while(nr--) {
3165                                         cp= (char *)&tface->col[0];
3166                                         if(cp[1]>126) cp[1]= 255; else cp[1]*=2;
3167                                         if(cp[2]>126) cp[2]= 255; else cp[2]*=2;
3168                                         if(cp[3]>126) cp[3]= 255; else cp[3]*=2;
3169                                         cp= (char *)&tface->col[1];
3170                                         if(cp[1]>126) cp[1]= 255; else cp[1]*=2;
3171                                         if(cp[2]>126) cp[2]= 255; else cp[2]*=2;
3172                                         if(cp[3]>126) cp[3]= 255; else cp[3]*=2;
3173                                         cp= (char *)&tface->col[2];
3174                                         if(cp[1]>126) cp[1]= 255; else cp[1]*=2;
3175                                         if(cp[2]>126) cp[2]= 255; else cp[2]*=2;
3176                                         if(cp[3]>126) cp[3]= 255; else cp[3]*=2;
3177                                         cp= (char *)&tface->col[3];
3178                                         if(cp[1]>126) cp[1]= 255; else cp[1]*=2;
3179                                         if(cp[2]>126) cp[2]= 255; else cp[2]*=2;
3180                                         if(cp[3]>126) cp[3]= 255; else cp[3]*=2;
3181
3182                                         tface++;
3183                                 }
3184                         }
3185                         me= me->id.next;
3186                 }
3187         }
3188
3189         if(main->versionfile <= 169) {
3190                 Mesh *me= main->mesh.first;
3191                 while(me) {
3192                         if(me->subdiv==0) me->subdiv= 3;
3193                         me= me->id.next;
3194                 }
3195         }