The revised patch from Leon for new particle effects.
[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         ma->ren= 0;     /* should not be needed, nevertheless... */
1924 }
1925
1926 /* ************ READ MESH ***************** */
1927
1928 static void lib_link_mesh(FileData *fd, Main *main)
1929 {
1930         Mesh *me;
1931
1932         me= main->mesh.first;
1933         while(me) {
1934                 if(me->id.flag & LIB_NEEDLINK) {
1935                         int i;
1936
1937                         /* this check added for python created meshes */
1938                         if(me->mat) {
1939                                 for(i=0; i<me->totcol; i++)
1940                                         me->mat[i]= newlibadr_us(fd, me->id.lib, me->mat[i]);
1941                         }
1942                         else me->totcol= 0;
1943
1944                         me->ipo= newlibadr_us(fd, me->id.lib, me->ipo);
1945                         me->key= newlibadr_us(fd, me->id.lib, me->key);
1946                         me->texcomesh= newlibadr_us(fd, me->id.lib, me->texcomesh);
1947
1948                         if(me->tface) {
1949                                 TFace *tfaces= me->tface;
1950
1951                                 for (i=0; i<me->totface; i++) {
1952                                         TFace *tf= &tfaces[i];
1953
1954                                         tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
1955                                         if(tf->tpage) {
1956                                                 Image *ima= tf->tpage;
1957                                                 if(ima->id.us==0)
1958                                                         ima->id.us= 1;
1959                                         }
1960                                 }
1961                         }
1962                         me->id.flag -= LIB_NEEDLINK;
1963                 }
1964                 me= me->id.next;
1965         }
1966 }
1967
1968 static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
1969 {
1970         int     i, j;
1971
1972         if (!mdverts)
1973                 return;
1974
1975         for (i=0; i<count; i++) {
1976                 mdverts[i].dw=newdataadr(fd, mdverts[i].dw);
1977                 if (!mdverts[i].dw)
1978                         mdverts[i].totweight=0;
1979
1980                 for (j=0; j< mdverts[i].totweight; j++) {
1981                         mdverts[i].dw[j].data = NULL;   // not saved in file, clear pointer
1982                 }
1983         }
1984 }
1985
1986 static void direct_link_mesh(FileData *fd, Mesh *mesh)
1987 {
1988         mesh->mat= newdataadr(fd, mesh->mat);
1989         test_pointer_array(fd, (void **)&mesh->mat);
1990         mesh->mvert= newdataadr(fd, mesh->mvert);
1991
1992         mesh->dvert= newdataadr(fd, mesh->dvert);
1993         direct_link_dverts(fd, mesh->totvert, mesh->dvert);
1994
1995         mesh->mface= newdataadr(fd, mesh->mface);
1996         mesh->tface= newdataadr(fd, mesh->tface);
1997         mesh->mcol= newdataadr(fd, mesh->mcol);
1998         mesh->msticky= newdataadr(fd, mesh->msticky);
1999
2000         mesh->disp.first= mesh->disp.last= 0;
2001         mesh->bb= 0;
2002         mesh->oc= 0;
2003         mesh->dface= 0;
2004         mesh->orco= 0;
2005
2006         if (mesh->tface) {
2007                 TFace *tfaces= mesh->tface;
2008                 int i;
2009
2010                 for (i=0; i<mesh->totface; i++) {
2011                         TFace *tf= &tfaces[i];
2012
2013                         if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2014                                 SWITCH_INT(tf->col[0]);
2015                                 SWITCH_INT(tf->col[1]);
2016                                 SWITCH_INT(tf->col[2]);
2017                                 SWITCH_INT(tf->col[3]);
2018                         }
2019                 }
2020         }
2021 }
2022
2023 /* ************ READ OBJECT ***************** */
2024
2025 static void lib_link_object(FileData *fd, Main *main)
2026 {
2027         Object *ob;
2028         bSensor *sens;
2029         bController *cont;
2030         bActuator *act;
2031
2032         void *poin;
2033         int warn=0, a;
2034
2035         ob= main->object.first;
2036         while(ob) {
2037                 if(ob->id.flag & LIB_NEEDLINK) {
2038
2039                         ob->parent= newlibadr(fd, ob->id.lib, ob->parent);
2040                         ob->track= newlibadr(fd, ob->id.lib, ob->track);
2041                         ob->ipo= newlibadr_us(fd, ob->id.lib, ob->ipo);
2042                         ob->action = newlibadr_us(fd, ob->id.lib, ob->action);
2043
2044 //                      ob->activecon = newglobadr(fd, ob->activecon);
2045
2046                         poin= ob->data;
2047                         ob->data= newlibadr_us(fd, ob->id.lib, ob->data);
2048
2049                         if(ob->data==NULL && poin!=NULL) {
2050                                 ob->type= OB_EMPTY;
2051                                 warn= 1;
2052                                 if(ob->id.lib) printf("Can't find obdata of %s lib %s\n", ob->id.name+2, ob->id.lib->name);
2053                                 else printf("Object %s lost data. Lib:%x\n", ob->id.name+2, (unsigned int) ob->id.lib);
2054                         }
2055                         for(a=0; a<ob->totcol; a++) ob->mat[a]= newlibadr_us(fd, ob->id.lib, ob->mat[a]);
2056
2057                         ob->id.flag -= LIB_NEEDLINK;
2058                         /* if id.us==0 a new base will be created later on */
2059
2060                         /* WARNING! Also check expand_object(), should reflect the stuff below. */
2061                         lib_link_pose(fd, &ob->id, ob->pose);
2062                         lib_link_constraints(fd, &ob->id, &ob->constraints);
2063                         lib_link_nlastrips(fd, &ob->id, &ob->nlastrips);
2064                         lib_link_constraint_channels(fd, &ob->id, &ob->constraintChannels);
2065
2066
2067                         sens= ob->sensors.first;
2068                         while(sens) {
2069                                 for(a=0; a<sens->totlinks; a++) {
2070                                         sens->links[a]= newglobadr(fd, sens->links[a]);
2071                                 }
2072                                 if(sens->type==SENS_TOUCH) {
2073                                         bTouchSensor *ts= sens->data;
2074                                         ts->ma= newlibadr(fd, ob->id.lib, ts->ma);
2075                                 }
2076                                 else if(sens->type==SENS_MESSAGE) {
2077                                         bMessageSensor *ms= sens->data;
2078                                         ms->fromObject=
2079                                             newlibadr(fd, ob->id.lib, ms->fromObject);
2080                                 }
2081                                 sens= sens->next;
2082                         }
2083
2084                         cont= ob->controllers.first;
2085                         while(cont) {
2086                                 for(a=0; a<cont->totlinks; a++) {
2087                                         cont->links[a]= newglobadr(fd, cont->links[a]);
2088                                 }
2089                                 if(cont->type==CONT_PYTHON) {
2090                                         bPythonCont *pc= cont->data;
2091                                         pc->text= newlibadr(fd, ob->id.lib, pc->text);
2092                                 }
2093                                 cont->slinks= NULL;
2094                                 cont->totslinks= 0;
2095
2096                                 cont= cont->next;
2097                         }
2098
2099                         act= ob->actuators.first;
2100                         while(act) {
2101                                 if(act->type==ACT_SOUND) {
2102                                         bSoundActuator *sa= act->data;
2103                                         sa->sound= newlibadr_us(fd, ob->id.lib, sa->sound);
2104                                 }
2105                                 else if(act->type==ACT_CD) {
2106                                         /* bCDActuator *cda= act->data; */
2107                                 }
2108                                 else if(act->type==ACT_GAME) {
2109                                         /* bGameActuator *ga= act->data; */
2110                                 }
2111                                 else if(act->type==ACT_CAMERA) {
2112                                         bCameraActuator *ca= act->data;
2113                                         ca->ob= newlibadr(fd, ob->id.lib, ca->ob);
2114                                 }
2115                                         /* leave this one, it's obsolete but necessary to read for conversion */
2116                                 else if(act->type==ACT_ADD_OBJECT) {
2117                                         bAddObjectActuator *eoa= act->data;
2118                                         if(eoa) eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
2119                                 }
2120                                 else if(act->type==ACT_EDIT_OBJECT) {
2121                                         bEditObjectActuator *eoa= act->data;
2122                                         if(eoa==NULL) {
2123                                                 init_actuator(act);
2124                                         }
2125                                         eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
2126                                         eoa->me= newlibadr(fd, ob->id.lib, eoa->me);
2127                                 }
2128                                 else if(act->type==ACT_SCENE) {
2129                                         bSceneActuator *sa= act->data;
2130                                         sa->camera= newlibadr(fd, ob->id.lib, sa->camera);
2131                                         sa->scene= newlibadr(fd, ob->id.lib, sa->scene);
2132                                 }
2133                                 else if(act->type==ACT_ACTION) {
2134                                         bActionActuator *aa= act->data;
2135                                         aa->act= newlibadr(fd, ob->id.lib, aa->act);
2136                                 }
2137                                 else if(act->type==ACT_PROPERTY) {
2138                                         bPropertyActuator *pa= act->data;
2139                                         pa->ob= newlibadr(fd, ob->id.lib, pa->ob);
2140                                 }
2141                                 else if(act->type==ACT_MESSAGE) {
2142                                         bMessageActuator *ma= act->data;
2143                                         ma->toObject= newlibadr(fd, ob->id.lib, ma->toObject);
2144                                 }
2145                                 act= act->next;
2146                         }
2147
2148                         lib_link_scriptlink(fd, &ob->id, &ob->scriptlink);
2149                 }
2150                 ob= ob->id.next;
2151         }
2152
2153         if(warn) error("WARNING IN CONSOLE");
2154 }
2155
2156
2157 static void direct_link_pose(FileData *fd, bPose *pose) {
2158
2159         bPoseChannel *chan;
2160
2161         if (!pose)
2162                 return;
2163
2164         link_list(fd, &pose->chanbase);
2165
2166         for (chan = pose->chanbase.first; chan; chan=chan->next) {
2167                 direct_link_constraints(fd, &chan->constraints);
2168         }
2169
2170 }
2171
2172 static void direct_link_object(FileData *fd, Object *ob)
2173 {
2174         PartEff *paf;
2175         bProperty *prop;
2176         bSensor *sens;
2177         bController *cont;
2178         bActuator *act;
2179
2180         ob->disp.first=ob->disp.last= 0;
2181
2182         ob->pose= newdataadr(fd, ob->pose);
2183         direct_link_pose(fd, ob->pose);
2184
2185         link_list(fd, &ob->defbase);
2186         link_list(fd, &ob->nlastrips);
2187         link_list(fd, &ob->constraintChannels);
2188
2189         ob->activecon = newdataadr(fd, ob->activecon);
2190
2191         direct_link_scriptlink(fd, &ob->scriptlink);
2192
2193         ob->mat= newdataadr(fd, ob->mat);
2194         test_pointer_array(fd, (void **)&ob->mat);
2195         link_list(fd, &ob->effect);
2196         paf= ob->effect.first;
2197         while(paf) {
2198                 if(paf->type==EFF_PARTICLE) {
2199                         paf->keys= 0;
2200                 }
2201                 if(paf->type==EFF_WAVE) {
2202
2203                 }
2204                 paf= paf->next;
2205         }
2206
2207         ob->pd= newdataadr(fd, ob->pd);
2208
2209         link_list(fd, &ob->prop);
2210         prop= ob->prop.first;
2211         while(prop) {
2212                 prop->poin= newdataadr(fd, prop->poin);
2213                 if(prop->poin==0) prop->poin= &prop->data;
2214                 prop= prop->next;
2215         }
2216
2217         link_list(fd, &ob->sensors);
2218         sens= ob->sensors.first;
2219         while(sens) {
2220                 sens->data= newdataadr(fd, sens->data);
2221                 sens->links= newdataadr(fd, sens->links);
2222                 test_pointer_array(fd, (void **)&sens->links);
2223                 sens= sens->next;
2224         }
2225
2226         direct_link_constraints(fd, &ob->constraints);
2227
2228         link_glob_list(fd, &ob->controllers);
2229         cont= ob->controllers.first;
2230         while(cont) {
2231                 cont->data= newdataadr(fd, cont->data);
2232                 cont->links= newdataadr(fd, cont->links);
2233                 test_pointer_array(fd, (void **)&cont->links);
2234                 cont= cont->next;
2235         }
2236
2237         link_glob_list(fd, &ob->actuators);
2238         act= ob->actuators.first;
2239         while(act) {
2240                 act->data= newdataadr(fd, act->data);
2241                 act= act->next;
2242         }
2243
2244         ob->bb= 0;
2245 }
2246
2247 /* ************ READ SCENE ***************** */
2248
2249 static void lib_link_scene(FileData *fd, Main *main)
2250 {
2251         Scene *sce;
2252         Base *base, *next;
2253         Editing *ed;
2254         Sequence *seq;
2255
2256         sce= main->scene.first;
2257         while(sce) {
2258                 if(sce->id.flag & LIB_NEEDLINK) {
2259                         sce->id.us= 1;
2260                         sce->camera= newlibadr(fd, sce->id.lib, sce->camera);
2261                         sce->world= newlibadr_us(fd, sce->id.lib, sce->world);
2262                         sce->set= newlibadr(fd, sce->id.lib, sce->set);
2263                         sce->ima= newlibadr_us(fd, sce->id.lib, sce->ima);
2264                         sce->group= newlibadr_us(fd, sce->id.lib, sce->group);
2265
2266                         base= sce->base.first;
2267                         while(base) {
2268                                 next= base->next;
2269
2270                                 /* base->object= newlibadr_us(fd, sce->id.lib, base->object); */
2271
2272                                 base->object= newlibadr_us_type(fd, ID_OB, base->object);
2273
2274                                 if(base->object==0) {
2275                                         printf("LIB ERROR: base removed\n");
2276                                         BLI_remlink(&sce->base, base);
2277                                         if(base==sce->basact) sce->basact= 0;
2278                                         MEM_freeN(base);
2279                                 }
2280                                 base= next;
2281                         }
2282
2283                         ed= sce->ed;
2284                         if(ed) {
2285                                 WHILE_SEQ(ed->seqbasep) {
2286                                         if(seq->ipo) seq->ipo= newlibadr_us(fd, sce->id.lib, seq->ipo);
2287                                         if(seq->scene) seq->scene= newlibadr(fd, sce->id.lib, seq->scene);
2288                                         if(seq->sound) {
2289                                                 seq->sound= newlibadr(fd, sce->id.lib, seq->sound);
2290                                                 if (seq->sound) {
2291                                                         seq->sound->id.us++;
2292                                                         seq->sound->flags |= SOUND_FLAGS_SEQUENCE;
2293                                                 }
2294                                         }
2295                                         seq->anim= 0;
2296                                 }
2297                                 END_SEQ
2298                         }
2299                         sce->id.flag -= LIB_NEEDLINK;
2300                 }
2301
2302                 lib_link_scriptlink(fd, &sce->id, &sce->scriptlink);
2303
2304                 sce= sce->id.next;
2305         }
2306 }
2307
2308 static void link_recurs_seq(FileData *fd, ListBase *lb)
2309 {
2310         Sequence *seq;
2311
2312         link_list(fd, lb);
2313         seq= lb->first;
2314         while(seq) {
2315                 if(seq->seqbase.first) link_recurs_seq(fd, &seq->seqbase);
2316                 seq= seq->next;
2317         }
2318 }
2319
2320 static void direct_link_scene(FileData *fd, Scene *sce)
2321 {
2322         Editing *ed;
2323         Sequence *seq;
2324         StripElem *se;
2325         int a;
2326
2327         link_list(fd, &(sce->base));
2328
2329         sce->basact= newdataadr(fd, sce->basact);
2330
2331         sce->radio= newdataadr(fd, sce->radio);
2332         sce->fcam= newdataadr(fd, sce->fcam);
2333
2334         sce->r.avicodecdata = newdataadr(fd, sce->r.avicodecdata);
2335         if (sce->r.avicodecdata) {
2336                 sce->r.avicodecdata->lpFormat = newdataadr(fd, sce->r.avicodecdata->lpFormat);
2337                 sce->r.avicodecdata->lpParms = newdataadr(fd, sce->r.avicodecdata->lpParms);
2338         }
2339
2340         sce->r.qtcodecdata = newdataadr(fd, sce->r.qtcodecdata);
2341         if (sce->r.qtcodecdata) {
2342                 sce->r.qtcodecdata->cdParms = newdataadr(fd, sce->r.qtcodecdata->cdParms);
2343         }
2344
2345         if(sce->ed) {
2346                 ed= sce->ed= newdataadr(fd, sce->ed);
2347
2348                 ed->metastack.first= ed->metastack.last= 0;
2349
2350                 /* recursive link sequences, lb will be correctly initialized */
2351                 link_recurs_seq(fd, &ed->seqbase);
2352
2353                 ed->seqbasep= &ed->seqbase;
2354
2355                 WHILE_SEQ(ed->seqbasep) {
2356                         seq->seq1= newdataadr(fd, seq->seq1);
2357                         seq->seq2= newdataadr(fd, seq->seq2);
2358                         seq->seq3= newdataadr(fd, seq->seq3);
2359                         /* a patch: after introduction of effects with 3 input strips */
2360                         if(seq->seq3==0) seq->seq3= seq->seq2;
2361
2362                         seq->curelem= 0;
2363
2364                         seq->plugin= newdataadr(fd, seq->plugin);
2365                         if(seq->plugin) open_plugin_seq(seq->plugin, seq->name+2);
2366
2367                         seq->effectdata= newdataadr(fd, seq->effectdata);
2368
2369                         seq->strip= newdataadr(fd, seq->strip);
2370                         if(seq->strip && seq->strip->done==0) {
2371                                 seq->strip->done= 1;
2372
2373                                 /* standard: strips from effects/metas are not written, but are mallocced */
2374
2375                                 if(seq->type==SEQ_IMAGE) {
2376                                         seq->strip->stripdata= newdataadr(fd, seq->strip->stripdata);
2377                                         se= seq->strip->stripdata;
2378                                         if(se) {
2379                                                 for(a=0; a<seq->strip->len; a++, se++) {
2380                                                         se->ok= 1;
2381                                                         se->ibuf= 0;
2382                                                 }
2383                                         }
2384                                 }
2385                                 else if(seq->type==SEQ_MOVIE) {
2386                                         /* only first stripelem is in file */
2387                                         se= newdataadr(fd, seq->strip->stripdata);
2388
2389                                         if(se) {
2390                                                 seq->strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
2391                                                 *seq->strip->stripdata= *se;
2392                                                 MEM_freeN(se);
2393
2394                                                 se= seq->strip->stripdata;
2395
2396                                                 for(a=0; a<seq->strip->len; a++, se++) {
2397                                                         se->ok= 1;
2398                                                         se->ibuf= 0;
2399                                                         se->nr= a + 1;
2400                                                 }
2401                                         }
2402                                 }
2403                                 else if(seq->type==SEQ_SOUND) {
2404                                         /* only first stripelem is in file */
2405                                         se= newdataadr(fd, seq->strip->stripdata);
2406
2407                                         if(se) {
2408                                                 seq->strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
2409                                                 *seq->strip->stripdata= *se;
2410                                                 MEM_freeN(se);
2411
2412                                                 se= seq->strip->stripdata;
2413
2414                                                 for(a=0; a<seq->strip->len; a++, se++) {
2415                                                         se->ok= 2; /* why? */
2416                                                         se->ibuf= 0;
2417                                                         se->nr= a + 1;
2418                                                 }
2419                                         }
2420                                 }
2421                                 else if(seq->len>0)
2422                                         seq->strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
2423
2424                         }
2425                 }
2426                 END_SEQ
2427         }
2428
2429         direct_link_scriptlink(fd, &sce->scriptlink);
2430 }
2431
2432 /* ************ READ SCREEN ***************** */
2433
2434 static void lib_link_screen(FileData *fd, Main *main)
2435 {
2436         bScreen *sc;
2437         ScrArea *sa;
2438
2439         sc= main->screen.first;
2440         while(sc) {
2441                 if(sc->id.flag & LIB_NEEDLINK) {
2442                         sc->id.us= 1;
2443                         sc->scene= newlibadr(fd, sc->id.lib, sc->scene);
2444
2445                         sa= sc->areabase.first;
2446                         while(sa) {
2447                                 SpaceLink *sl;
2448
2449                                 sa->full= newlibadr(fd, sc->id.lib, sa->full);
2450
2451                                 for (sl= sa->spacedata.first; sl; sl= sl->next) {
2452                                         if(sl->spacetype==SPACE_VIEW3D) {
2453                                                 View3D *v3d= (View3D*) sl;
2454
2455                                                 v3d->camera= newlibadr(fd, sc->id.lib, v3d->camera);
2456
2457                                                 if(v3d->bgpic) {
2458                                                         v3d->bgpic->ima= newlibadr_us(fd, sc->id.lib, v3d->bgpic->ima);
2459                                                         v3d->bgpic->tex= newlibadr_us(fd, sc->id.lib, v3d->bgpic->tex);
2460                                                         v3d->bgpic->rect= 0;
2461                                                 }
2462                                                 if(v3d->localvd) {
2463                                                         v3d->localvd->camera= newlibadr(fd, sc->id.lib, v3d->localvd->camera);
2464                                                 }
2465                                         }
2466                                         else if(sl->spacetype==SPACE_IPO) {
2467                                                 SpaceIpo *sipo= (SpaceIpo *)sl;
2468                                                 sipo->editipo= 0;
2469                                                 sipo->from= newlibadr(fd, sc->id.lib, sipo->from);
2470                                                 sipo->ipokey.first= sipo->ipokey.last= 0;
2471                                                 sipo->ipo= newlibadr(fd, sc->id.lib, sipo->ipo);
2472                                         }
2473                                         else if(sl->spacetype==SPACE_BUTS) {
2474                                                 SpaceButs *sbuts= (SpaceButs *)sl;
2475                                                 sbuts->rect= 0;
2476                                                 sbuts->lockpoin= 0;
2477                                                 if(main->versionfile<132) set_rects_butspace(sbuts);
2478                                         }
2479                                         else if(sl->spacetype==SPACE_FILE) {
2480                                                 SpaceFile *sfile= (SpaceFile *)sl;
2481
2482                                                 sfile->filelist= 0;
2483                                                 sfile->libfiledata= 0;
2484                                                 sfile->returnfunc= 0;
2485                                         }
2486                                         else if(sl->spacetype==SPACE_IMASEL) {
2487                                                 check_imasel_copy((SpaceImaSel *)sl);
2488                                         }
2489                                         else if(sl->spacetype==SPACE_ACTION) {
2490                                                 SpaceAction *saction= (SpaceAction *)sl;
2491                                                 saction->action = newlibadr(fd, sc->id.lib, saction->action);
2492                                         }
2493                                         else if(sl->spacetype==SPACE_IMAGE) {
2494                                                 SpaceImage *sima= (SpaceImage *)sl;
2495
2496                                                 sima->image= newlibadr_us(fd, sc->id.lib, sima->image);
2497                                         }
2498                                         else if(sl->spacetype==SPACE_NLA){
2499                                                 /* SpaceNla *snla= (SpaceNla *)sl;      */
2500                                         }
2501                                         else if(sl->spacetype==SPACE_TEXT) {
2502                                                 SpaceText *st= (SpaceText *)sl;
2503
2504                                                 st->text= newlibadr(fd, sc->id.lib, st->text);
2505
2506                                         }
2507                                         else if(sl->spacetype==SPACE_SCRIPT) {
2508                                                 SpaceScript *sc= (SpaceScript *)sl;
2509
2510                                                 sc->script = NULL;
2511                                         }
2512                                         else if(sl->spacetype==SPACE_OOPS) {
2513                                                 SpaceOops *so= (SpaceOops *)sl;
2514                                                 Oops *oops;
2515
2516                                                 oops= so->oops.first;
2517                                                 while(oops) {
2518                                                         oops->id= newlibadr(fd, 0, oops->id);
2519                                                         oops= oops->next;
2520                                                 }
2521                                                 so->lockpoin= 0;
2522                                         }
2523                                         else if(sl->spacetype==SPACE_SOUND) {
2524                                                 SpaceSound *ssound= (SpaceSound *)sl;
2525
2526                                                 ssound->sound= newlibadr_us(fd, sc->id.lib, ssound->sound);
2527                                         }
2528                                 }
2529                                 sa= sa->next;
2530                         }
2531                         sc->id.flag -= LIB_NEEDLINK;
2532                 }
2533                 sc= sc->id.next;
2534         }
2535 }
2536
2537 static void direct_link_screen(FileData *fd, bScreen *sc)
2538 {
2539         ScrArea *sa;
2540         ScrVert *sv;
2541         ScrEdge *se;
2542         Oops *oops;
2543
2544         link_list(fd, &(sc->vertbase));
2545         link_list(fd, &(sc->edgebase));
2546         link_list(fd, &(sc->areabase));
2547         sc->winakt= 0;
2548
2549         /* edges */
2550         se= sc->edgebase.first;
2551         while(se) {
2552                 se->v1= newdataadr(fd, se->v1);
2553                 se->v2= newdataadr(fd, se->v2);
2554                 if( (long)se->v1 > (long)se->v2) {
2555                         sv= se->v1;
2556                         se->v1= se->v2;
2557                         se->v2= sv;
2558                 }
2559
2560                 if(se->v1==NULL) {
2561                         printf("error reading screen... file corrupt\n");
2562                         se->v1= se->v2;
2563                 }
2564                 se= se->next;
2565         }
2566
2567         /* areas */
2568         sa= sc->areabase.first;
2569         while(sa) {
2570                 Panel *pa;
2571                 SpaceLink *sl;
2572
2573                 link_list(fd, &(sa->spacedata));
2574                 link_list(fd, &(sa->panels));
2575
2576                 for(pa= sa->panels.first; pa; pa=pa->next) {
2577                         pa->paneltab= newdataadr(fd, pa->paneltab);
2578                         pa->active= 0;
2579                 }
2580
2581                 for (sl= sa->spacedata.first; sl; sl= sl->next) {
2582                         if (sl->spacetype==SPACE_VIEW3D) {
2583                                 View3D *v3d= (View3D*) sl;
2584                                 v3d->bgpic= newdataadr(fd, v3d->bgpic);
2585                                 v3d->localvd= newdataadr(fd, v3d->localvd);
2586                         }
2587                         else if (sl->spacetype==SPACE_OOPS) {
2588                                 SpaceOops *soops= (SpaceOops*) sl;
2589                                 link_list(fd, &(soops->oops));
2590                                 oops= soops->oops.first;
2591                                 while(oops) {
2592                                         oops->link.first= oops->link.last= 0;
2593                                         oops= oops->next;
2594                                 }
2595                         }
2596                 }
2597
2598                 sa->v1= newdataadr(fd, sa->v1);
2599                 sa->v2= newdataadr(fd, sa->v2);
2600                 sa->v3= newdataadr(fd, sa->v3);
2601                 sa->v4= newdataadr(fd, sa->v4);
2602
2603                 sa->win= sa->headwin= 0;
2604
2605                 sa->uiblocks.first= sa->uiblocks.last= NULL;
2606
2607                 sa= sa->next;
2608         }
2609 }
2610
2611 /* ********** READ LIBRARY *************** */
2612
2613
2614 static void direct_link_library(FileData *fd, Library *lib)
2615 {
2616         Main *newmain;
2617
2618         /* new main */
2619         newmain= MEM_callocN(sizeof(Main), "directlink");
2620         BLI_addtail(&fd->mainlist, newmain);
2621         newmain->curlib= lib;
2622 }
2623
2624 static void lib_link_library(FileData *fd, Main *main)
2625 {
2626         Library *lib;
2627
2628         lib= main->library.first;
2629         while(lib) {
2630                 lib->id.us= 1;
2631                 lib= lib->id.next;
2632         }
2633 }
2634
2635 /* ************** READ SOUND ******************* */
2636
2637 static void direct_link_sound(FileData *fd, bSound *sound)
2638 {
2639         sound->sample = NULL;
2640         sound->snd_sound = NULL;
2641
2642         sound->packedfile = direct_link_packedfile(fd, sound->packedfile);
2643         sound->newpackedfile = direct_link_packedfile(fd, sound->newpackedfile);
2644 }
2645
2646 static void lib_link_sound(FileData *fd, Main *main)
2647 {
2648         bSound *sound;
2649
2650         sound= main->sound.first;
2651         while(sound) {
2652                 if(sound->id.flag & LIB_NEEDLINK) {
2653                         sound->id.flag -= LIB_NEEDLINK;
2654                         sound->ipo= newlibadr_us(fd, sound->id.lib, sound->ipo);
2655                         sound->stream = 0;
2656                 }
2657                 sound= sound->id.next;
2658         }
2659 }
2660 /* ***************** READ GROUP *************** */
2661
2662 static void direct_link_group(FileData *fd, Group *group)
2663 {
2664         GroupObject *go;
2665         ObjectKey *ok;
2666
2667         link_list(fd, &group->gobject);
2668         link_list(fd, &group->gkey);
2669         group->active= newdataadr(fd, group->active);
2670
2671         go= group->gobject.first;
2672         while(go) {
2673                 link_list(fd, &go->okey);
2674                 ok= go->okey.first;
2675                 while(ok) {
2676                         ok->gkey= newdataadr(fd, ok->gkey);
2677                         ok= ok->next;
2678                 }
2679                 go= go->next;
2680         }
2681 }
2682
2683 static void lib_link_group(FileData *fd, Main *main)
2684 {
2685         Group *group= main->group.first;
2686         GroupObject *go;
2687         ObjectKey *ok;
2688
2689         while(group) {
2690                 if(group->id.flag & LIB_NEEDLINK) {
2691                         group->id.flag -= LIB_NEEDLINK;
2692
2693                         go= group->gobject.first;
2694                         while(go) {
2695                                 go->ob= newlibadr(fd, group->id.lib, go->ob);
2696                                 ok= go->okey.first;
2697                                 while(ok) {
2698                                         ok->parent= newlibadr(fd, group->id.lib, ok->parent);
2699                                         ok->track= newlibadr(fd, group->id.lib, ok->track);
2700                                         ok->ipo= newlibadr_us(fd, group->id.lib, ok->ipo);
2701                                         ok= ok->next;
2702                                 }
2703                                 go= go->next;
2704                         }
2705                 }
2706                 group= group->id.next;
2707         }
2708 }
2709
2710 /* ************** GENERAL & MAIN ******************** */
2711
2712 static BHead *read_libblock(FileData *fd, Main *main, BHead *bhead, int flag, ID **id_r)
2713 {
2714         /* this routine reads a libblock and its direct data. Use link functions
2715          * to connect it all
2716          */
2717
2718         ID *id;
2719         ListBase *lb;
2720         char *str = NULL;
2721
2722         if(bhead->code==ID_ID) {
2723                 ID *linkedid= (ID *)(bhead + 1); /*  BHEAD+DATA dependancy */
2724
2725                 lb= wich_libbase(main, GS(linkedid->name));
2726         }
2727         else {
2728                 lb= wich_libbase(main, bhead->code);
2729         }
2730
2731         /* read libblock */
2732         id = read_struct(fd, bhead, "lib block");
2733         if (id_r)
2734                 *id_r= id;
2735         if (!id)
2736                 return blo_nextbhead(fd, bhead);
2737
2738         oldnewmap_insert(fd->libmap, bhead->old, id, 1);
2739         BLI_addtail(lb, id);
2740
2741         /* clear first 8 bits */
2742         id->flag= (id->flag & 0xFF00) | flag | LIB_NEEDLINK;
2743         id->lib= main->curlib;
2744         if(id->flag & LIB_FAKEUSER) id->us= 1;
2745         else id->us= 0;
2746
2747         /* this case cannot be direct_linked: it's just the ID part */
2748         if(bhead->code==ID_ID) {
2749                 return blo_nextbhead(fd, bhead);
2750         }
2751
2752         bhead = blo_nextbhead(fd, bhead);
2753
2754         switch( GS(id->name) ) {
2755         case ID_OB: str= "ID_OB"; break;
2756         case ID_SCE: str= "ID_SCE"; break;
2757         case ID_LI: str= "ID_LI"; break;
2758         case ID_ME: str= "ID_ME"; break;
2759         case ID_CU: str= "ID_CU"; break;
2760         case ID_MB: str= "ID_MB"; break;
2761         case ID_MA: str= "ID_MA"; break;
2762         case ID_TE: str= "ID_TE"; break;
2763         case ID_IM: str= "ID_IM"; break;
2764         case ID_IK: str= "ID_IK"; break;
2765         case ID_WV: str= "ID_WV"; break;
2766         case ID_LT: str= "ID_LT"; break;
2767         case ID_SE: str= "ID_SE"; break;
2768         case ID_LF: str= "ID_LF"; break;
2769         case ID_LA: str= "ID_LA"; break;
2770         case ID_CA: str= "ID_CA"; break;
2771         case ID_IP: str= "ID_IP"; break;
2772         case ID_KE: str= "ID_KE"; break;
2773         case ID_WO: str= "ID_WO"; break;
2774         case ID_SCR: str= "ID_SCR"; break;
2775         case ID_VF: str= "ID_VF"; break;
2776         case ID_TXT     : str= "ID_TXT"; break;
2777         case ID_SO: str= "ID_SO"; break;
2778         case ID_SAMPLE: str= "ID_SAMPLE"; break;
2779         case ID_GR: str= "ID_GR"; break;
2780         case ID_ID: str= "ID_ID"; break;
2781         case ID_SEQ: str= "ID_SEQ"; break;
2782         case ID_AR: str= "ID_AR"; break;
2783         case ID_AC: str= "ID_AC"; break;
2784         case ID_SCRIPT: str= "ID_SCRIPT"; break;
2785         }
2786                 /* read all data */
2787         while(bhead && bhead->code==DATA) {
2788                 void *data= read_struct(fd, bhead, str);
2789
2790                 if (data) {
2791                         oldnewmap_insert(fd->datamap, bhead->old, data, 0);
2792                 }
2793
2794                 bhead = blo_nextbhead(fd, bhead);
2795         }
2796
2797         /* init pointers direct data */
2798         switch( GS(id->name) ) {
2799                 case ID_SCR:
2800                         direct_link_screen(fd, (bScreen *)id);
2801                         break;
2802                 case ID_SCE:
2803                         direct_link_scene(fd, (Scene *)id);
2804                         break;
2805                 case ID_OB:
2806                         direct_link_object(fd, (Object *)id);
2807                         break;
2808                 case ID_ME:
2809                         direct_link_mesh(fd, (Mesh *)id);
2810                         break;
2811                 case ID_CU:
2812                         direct_link_curve(fd, (Curve *)id);
2813                         break;
2814                 case ID_MB:
2815                         direct_link_mball(fd, (MetaBall *)id);
2816                         break;
2817                 case ID_MA:
2818                         direct_link_material(fd, (Material *)id);
2819                         break;
2820                 case ID_TE:
2821                         direct_link_texture(fd, (Tex *)id);
2822                         break;
2823                 case ID_IM:
2824                         direct_link_image(fd, (Image *)id);
2825                         break;
2826                 case ID_LA:
2827                         direct_link_lamp(fd, (Lamp *)id);
2828                         break;
2829                 case ID_VF:
2830                         direct_link_vfont(fd, (VFont *)id);
2831                         break;
2832                 case ID_TXT:
2833                         direct_link_text(fd, (Text *)id);
2834                         break;
2835                 case ID_IP:
2836                         direct_link_ipo(fd, (Ipo *)id);
2837                         break;
2838                 case ID_KE:
2839                         direct_link_key(fd, (Key *)id);
2840                         break;
2841                 case ID_LT:
2842                         direct_link_latt(fd, (Lattice *)id);
2843                         break;
2844                 case ID_IK:
2845                         direct_link_ika(fd, (Ika *)id);
2846                         break;
2847                 case ID_WO:
2848                         direct_link_world(fd, (World *)id);
2849                         break;
2850                 case ID_LI:
2851                         direct_link_library(fd, (Library *)id);
2852                         break;
2853                 case ID_CA:
2854                         direct_link_camera(fd, (Camera *)id);
2855                         break;
2856                 case ID_SO:
2857                         direct_link_sound(fd, (bSound *)id);
2858                         break;
2859                 case ID_GR:
2860                         direct_link_group(fd, (Group *)id);
2861                         break;
2862                 case ID_AR:
2863                         direct_link_armature(fd, (bArmature*)id);
2864                         break;
2865                 case ID_AC:
2866                         direct_link_action(fd, (bAction*)id);
2867                         break;
2868         }
2869
2870         oldnewmap_free_unused(fd->datamap);
2871         oldnewmap_clear(fd->datamap);
2872
2873         return (bhead);
2874 }
2875
2876 static void link_global(FileData *fd, BlendFileData *bfd, FileGlobal *fg)
2877 {
2878         // this is nonsense... will get rid of it once (ton)
2879         bfd->winpos= fg->winpos;
2880         bfd->fileflags= fg->fileflags;
2881         bfd->displaymode= fg->displaymode;
2882         bfd->globalf= fg->globalf;
2883         bfd->curscreen= newlibadr(fd, 0, fg->curscreen);
2884 }
2885
2886 static void vcol_to_fcol(Mesh *me)
2887 {
2888         MFace *mface;
2889         unsigned int *mcol, *mcoln, *mcolmain;
2890         int a;
2891
2892         if(me->totface==0 || me->mcol==0) return;
2893
2894         mcoln= mcolmain= MEM_mallocN(4*sizeof(int)*me->totface, "mcoln");
2895         mcol = (unsigned int *)me->mcol;
2896         mface= me->mface;
2897         for(a=me->totface; a>0; a--, mface++) {
2898                 mcoln[0]= mcol[mface->v1];
2899                 mcoln[1]= mcol[mface->v2];
2900                 mcoln[2]= mcol[mface->v3];
2901                 mcoln[3]= mcol[mface->v4];
2902                 mcoln+= 4;
2903         }
2904
2905         MEM_freeN(me->mcol);
2906         me->mcol= (MCol *)mcolmain;
2907 }
2908
2909 static int map_223_keybd_code_to_224_keybd_code(int code)
2910 {
2911         switch (code) {
2912         case 312:       return F12KEY;
2913         case 159:       return PADSLASHKEY;
2914         case 161:       return PAD0;
2915         case 154:       return PAD1;
2916         case 150:       return PAD2;
2917         case 155:       return PAD3;
2918         case 151:       return PAD4;
2919         case 156:       return PAD5;
2920         case 152:       return PAD6;
2921         case 157:       return PAD7;
2922         case 153:       return PAD8;
2923         case 158:       return PAD9;
2924         default: return code;
2925         }
2926 }
2927
2928 static void do_versions(Main *main)
2929 {
2930         /* watch it: pointers from libdata have not been converted */
2931
2932         if(main->versionfile == 100) {
2933                 /* tex->extend and tex->imageflag have changed: */
2934                 Tex *tex = main->tex.first;
2935                 while(tex) {
2936                         if(tex->id.flag & LIB_NEEDLINK) {
2937
2938                                 if(tex->extend==0) {
2939                                         if(tex->xrepeat || tex->yrepeat) tex->extend= TEX_REPEAT;
2940                                         else {
2941                                                 tex->extend= TEX_EXTEND;
2942                                                 tex->xrepeat= tex->yrepeat= 1;
2943                                         }
2944                                 }
2945
2946                                 if(tex->imaflag & TEX_ANIM5) {
2947                                         tex->imaflag |= TEX_MORKPATCH;
2948                                         tex->imaflag |= TEX_ANTIALI;
2949                                 }
2950                         }
2951                         tex= tex->id.next;
2952                 }
2953         }
2954         if(main->versionfile <= 101) {
2955                 /* frame mapping */
2956                 Scene *sce = main->scene.first;
2957                 while(sce) {
2958                         sce->r.framapto= 100;
2959                         sce->r.images= 100;
2960                         sce->r.framelen= 1.0;
2961                         sce= sce->id.next;
2962                 }
2963         }
2964         if(main->versionfile <= 102) {
2965                 /* init halo's at 1.0 */
2966                 Material *ma = main->mat.first;
2967                 while(ma) {
2968                         ma->add= 1.0;
2969                         ma= ma->id.next;
2970                 }
2971         }
2972         if(main->versionfile <= 103) {
2973                 /* new variable in object: colbits */
2974                 Object *ob = main->object.first;
2975                 int a;
2976                 while(ob) {
2977                         ob->colbits= 0;
2978                         if(ob->totcol) {
2979                                 for(a=0; a<ob->totcol; a++) {
2980                                         if(ob->mat[a]) ob->colbits |= (1<<a);
2981                                 }
2982                         }
2983                         ob= ob->id.next;
2984                 }
2985         }
2986         if(main->versionfile <= 104) {
2987                 /* timeoffs moved */
2988                 Object *ob = main->object.first;
2989                 while(ob) {
2990                         if(ob->transflag & 1) {
2991                                 ob->transflag -= 1;
2992                                 ob->ipoflag |= OB_OFFS_OB;
2993                         }
2994                         ob= ob->id.next;
2995                 }
2996         }
2997         if(main->versionfile <= 105) {
2998                 Object *ob = main->object.first;
2999                 while(ob) {
3000                         ob->dupon= 1; ob->dupoff= 0;
3001                         ob->dupsta= 1; ob->dupend= 100;
3002                         ob= ob->id.next;
3003                 }
3004         }
3005         if(main->versionfile <= 106) {
3006                 /* mcol changed */
3007                 Mesh *me = main->mesh.first;
3008                 while(me) {
3009                         if(me->mcol) vcol_to_fcol(me);
3010                         me= me->id.next;
3011                 }
3012
3013         }
3014         if(main->versionfile <= 107) {
3015                 Object *ob;
3016                 Scene *sce = main->scene.first;
3017                 while(sce) {
3018                         sce->r.mode |= R_GAMMA;
3019                         sce= sce->id.next;
3020                 }
3021                 ob= main->object.first;
3022                 while(ob) {
3023                         ob->ipoflag |= OB_OFFS_PARENT;
3024                         if(ob->dt==0) ob->dt= 3;
3025                         ob= ob->id.next;
3026                 }
3027
3028         }
3029         if(main->versionfile <= 109) {
3030                 /* new variable: gridlines */
3031                 bScreen *sc = main->screen.first;
3032                 while(sc) {
3033                         ScrArea *sa= sc->areabase.first;
3034                         while(sa) {
3035                                 SpaceLink *sl= sa->spacedata.first;
3036                                 while (sl) {
3037                                         if (sl->spacetype==SPACE_VIEW3D) {
3038                                                 View3D *v3d= (View3D*) sl;
3039
3040                                                 if (v3d->gridlines==0) v3d->gridlines= 20;
3041                                         }
3042                                         sl= sl->next;
3043                                 }
3044                                 sa= sa->next;
3045                         }
3046                         sc= sc->id.next;
3047                 }
3048         }
3049         if(main->versionfile <= 112) {
3050                 Mesh *me = main->mesh.first;
3051                 while(me) {
3052                         me->cubemapsize= 1.0;
3053                         me= me->id.next;
3054                 }
3055         }
3056         if(main->versionfile <= 113) {
3057                 Material *ma = main->mat.first;
3058                 while(ma) {
3059                         if(ma->flaresize==0.0) ma->flaresize= 1.0;
3060                         ma->subsize= 1.0;
3061                         ma->flareboost= 1.0;
3062                         ma= ma->id.next;
3063                 }
3064         }
3065         if(main->versionfile <= 114) {
3066                 Mesh *me= main->mesh.first;
3067                 MFace *mface;
3068                 int a_int;
3069
3070                 /* edge drawflags changed */
3071                 while(me) {
3072                         a_int= me->totface;
3073                         mface= me->mface;
3074                         while(a_int--) {
3075                                 if(mface->edcode & 16) {
3076                                         mface->edcode -= 16;
3077                                         mface->edcode |= ME_V3V1;
3078                                 }
3079                                 mface++;
3080                         }
3081                         me= me->id.next;
3082                 }
3083         }
3084
3085
3086         if(main->versionfile <= 134) {
3087                 Tex *tex = main->tex.first;
3088                 while (tex) {
3089                         if ((tex->rfac == 0.0) &&
3090                             (tex->gfac == 0.0) &&
3091                             (tex->bfac == 0.0)) {
3092                                 tex->rfac = 1.0;
3093                                 tex->gfac = 1.0;
3094                                 tex->bfac = 1.0;
3095                                 tex->filtersize = 1.0;
3096                         }
3097                         tex = tex->id.next;
3098                 }
3099         }
3100         if(main->versionfile <= 140) {
3101                 /* r-g-b-fac in texure */
3102                 Tex *tex = main->tex.first;
3103                 while (tex) {
3104                         if ((tex->rfac == 0.0) &&
3105                             (tex->gfac == 0.0) &&
3106                             (tex->bfac == 0.0)) {
3107                                 tex->rfac = 1.0;
3108                                 tex->gfac = 1.0;
3109                                 tex->bfac = 1.0;
3110                                 tex->filtersize = 1.0;
3111                         }
3112                         tex = tex->id.next;
3113                 }
3114         }
3115         if(main->versionfile <= 153) {
3116                 Scene *sce = main->scene.first;
3117                 while(sce) {
3118                         if(sce->r.blurfac==0.0) sce->r.blurfac= 1.0;
3119                         sce= sce->id.next;
3120                 }
3121         }
3122         if(main->versionfile <= 163) {
3123                 Scene *sce = main->scene.first;
3124                 while(sce) {
3125                         if(sce->r.frs_sec==0) sce->r.frs_sec= 25;
3126                         sce= sce->id.next;
3127                 }
3128         }
3129         if(main->versionfile <= 164) {
3130                 Mesh *me= main->mesh.first;
3131                 while(me) {
3132                         me->smoothresh= 30;
3133                         me= me->id.next;
3134                 }
3135         }
3136         if(main->versionfile <= 165) {
3137                 Mesh *me= main->mesh.first;
3138                 TFace *tface;
3139                 Ika *ika= main->ika.first;
3140                 Deform *def;
3141                 int nr;
3142                 char *cp;
3143
3144                 while(ika) {
3145                         ika->xyconstraint= .5;
3146
3147                         def= ika->def;
3148                         nr= ika->totdef;
3149                         while(nr--) {
3150                                 if(def->fac==0.0) def->fac= 1.0;
3151                                 def++;
3152                         }
3153                         ika= ika->id.next;
3154                 }
3155
3156                 while(me) {
3157                         if(me->tface) {
3158                                 nr= me->totface;
3159                                 tface= me->tface;
3160                                 while(nr--) {
3161                                         cp= (char *)&tface->col[0];
3162                                         if(cp[1]>126) cp[1]= 255; else cp[1]*=2;
3163                                         if(cp[2]>126) cp[2]= 255; else cp[2]*=2;
3164                                         if(cp[3]>126) cp[3]= 255; else cp[3]*=2;
3165                                         cp= (char *)&tface->col[1];
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[2];
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[3];
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
3178                                         tface++;
3179                                 }
3180                         }
3181                         me= me->id.next;
3182                 }
3183         }
3184
3185         if(main->versionfile <= 169) {
3186                 Mesh *me= main->mesh.first;
3187                 while(me) {
3188                         if(me->subdiv==0) me->subdiv= 3;
3189                         me= me->id.next;
3190                 }
3191         }
3192
3193         if(main->versionfile <= 169) {
3194                 bScreen *sc= main->screen.first;
3195                 while(sc) {
3196                         ScrArea *sa= sc->areabase.first;
3197                         while(sa) {
3198                                 SpaceLink *sl= sa->spacedata.first;
3199                                 while(sl) {