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