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