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