svn merge -r 21041:21301 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[blender.git] / source / blender / blenloader / intern / readblenentry.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * .blend file reading entry point
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include "BLI_storage.h" /* _LARGEFILE_SOURCE */
36
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdio.h>
40 #include <math.h>
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_ghash.h"
46 #include "BLI_linklist.h"
47
48 #include "DNA_genfile.h"
49 #include "DNA_sdna_types.h"
50 #include "DNA_space_types.h"
51 #include "DNA_userdef_types.h"
52 #include "DNA_ID.h"
53 #include "DNA_material_types.h"
54
55 #include "BKE_utildefines.h" // for ENDB
56
57 #include "BKE_main.h"
58 #include "BKE_library.h" // for free_main
59 #include "BKE_report.h"
60
61 #include "BLO_readfile.h"
62 #include "BLO_undofile.h"
63
64 #include "readfile.h"
65
66 #include "BLO_readblenfile.h"
67
68 #include "BLO_sys_types.h" // needed for intptr_t
69
70         /**
71          * IDType stuff, I plan to move this
72          * out into its own file + prefix, and
73          * make sure all IDType handling goes through
74          * these routines.
75          */
76
77 typedef struct {
78         unsigned short code;
79         char *name;
80         
81         int flags;
82 #define IDTYPE_FLAGS_ISLINKABLE (1<<0)
83 } IDType;
84
85 static IDType idtypes[]= {
86         { ID_AC,                "Action",       IDTYPE_FLAGS_ISLINKABLE}, 
87         { ID_AR,                "Armature", IDTYPE_FLAGS_ISLINKABLE}, 
88         { ID_BR,                "Brush",        IDTYPE_FLAGS_ISLINKABLE}, 
89         { ID_CA,                "Camera",       IDTYPE_FLAGS_ISLINKABLE}, 
90         { ID_CU,                "Curve",        IDTYPE_FLAGS_ISLINKABLE}, 
91         { ID_GD,                "GPencil",      IDTYPE_FLAGS_ISLINKABLE}, 
92         { ID_GR,                "Group",        IDTYPE_FLAGS_ISLINKABLE}, 
93         { ID_ID,                "ID",           0}, 
94         { ID_IM,                "Image",        IDTYPE_FLAGS_ISLINKABLE}, 
95         { ID_IP,                "Ipo",          IDTYPE_FLAGS_ISLINKABLE}, 
96         { ID_KE,                "Key",          0}, 
97         { ID_LA,                "Lamp",         IDTYPE_FLAGS_ISLINKABLE}, 
98         { ID_LF,                "Life",         0}, 
99         { ID_LI,                "Library",      0}, 
100         { ID_LT,                "Lattice",      IDTYPE_FLAGS_ISLINKABLE}, 
101         { ID_MA,                "Material", IDTYPE_FLAGS_ISLINKABLE}, 
102         { ID_MB,                "Metaball", IDTYPE_FLAGS_ISLINKABLE}, 
103         { ID_ME,                "Mesh",         IDTYPE_FLAGS_ISLINKABLE}, 
104         { ID_NT,                "NodeTree",     IDTYPE_FLAGS_ISLINKABLE}, 
105         { ID_OB,                "Object",       IDTYPE_FLAGS_ISLINKABLE}, 
106         { ID_SCE,               "Scene",        IDTYPE_FLAGS_ISLINKABLE}, 
107         { ID_SCR,               "Screen",       0}, 
108         { ID_SEQ,               "Sequence",     0}, 
109         { ID_SE,                "Sector",       0}, 
110         { ID_SO,                "Sound",        IDTYPE_FLAGS_ISLINKABLE}, 
111         { ID_TE,                "Texture",      IDTYPE_FLAGS_ISLINKABLE}, 
112         { ID_TXT,               "Text",         IDTYPE_FLAGS_ISLINKABLE}, 
113         { ID_VF,                "VFont",        IDTYPE_FLAGS_ISLINKABLE}, 
114         { ID_WO,                "World",        IDTYPE_FLAGS_ISLINKABLE}, 
115         { ID_WV,                "Wave",         0}, 
116 };
117 static int nidtypes= sizeof(idtypes)/sizeof(idtypes[0]);
118
119 /* local prototypes --------------------- */
120 void BLO_blendhandle_print_sizes(BlendHandle *, void *); 
121
122
123 static IDType *idtype_from_name(char *str) 
124 {
125         int i= nidtypes;
126         
127         while (i--)
128                 if (BLI_streq(str, idtypes[i].name))
129                         return &idtypes[i];
130         
131         return NULL;
132 }
133 static IDType *idtype_from_code(int code) 
134 {
135         int i= nidtypes;
136         
137         while (i--)
138                 if (code==idtypes[i].code)
139                         return &idtypes[i];
140         
141         return NULL;
142 }
143
144 static int bheadcode_is_idcode(int code) 
145 {
146         return idtype_from_code(code)?1:0;
147 }
148
149 static int idcode_is_linkable(int code) {
150         IDType *idt= idtype_from_code(code);
151         return idt?(idt->flags&IDTYPE_FLAGS_ISLINKABLE):0;
152 }
153
154 char *BLO_idcode_to_name(int code) 
155 {
156         IDType *idt= idtype_from_code(code);
157         
158         return idt?idt->name:NULL;
159 }
160
161 int BLO_idcode_from_name(char *name) 
162 {
163         IDType *idt= idtype_from_name(name);
164         
165         return idt?idt->code:0;
166 }
167         
168         /* Access routines used by filesel. */
169          
170 BlendHandle *BLO_blendhandle_from_file(char *file) 
171 {
172         BlendHandle *bh;
173
174         bh= (BlendHandle*)blo_openblenderfile(file, NULL);
175
176         return bh;
177 }
178
179 void BLO_blendhandle_print_sizes(BlendHandle *bh, void *fp) 
180 {
181         FileData *fd= (FileData*) bh;
182         BHead *bhead;
183
184         fprintf(fp, "[\n");
185         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
186                 if (bhead->code==ENDB)
187                         break;
188                 else {
189                         short *sp= fd->filesdna->structs[bhead->SDNAnr];
190                         char *name= fd->filesdna->types[ sp[0] ];
191                         char buf[4];
192                         
193                         buf[0]= (bhead->code>>24)&0xFF;
194                         buf[1]= (bhead->code>>16)&0xFF;
195                         buf[2]= (bhead->code>>8)&0xFF;
196                         buf[3]= (bhead->code>>0)&0xFF;
197                         
198                         buf[0]= buf[0]?buf[0]:' ';
199                         buf[1]= buf[1]?buf[1]:' ';
200                         buf[2]= buf[2]?buf[2]:' ';
201                         buf[3]= buf[3]?buf[3]:' ';
202                         
203                         fprintf(fp, "['%.4s', '%s', %d, %ld ], \n", buf, name, bhead->nr, (intptr_t)bhead->len+sizeof(BHead));
204                 }
205         }
206         fprintf(fp, "]\n");
207 }
208
209 LinkNode *BLO_blendhandle_get_datablock_names(BlendHandle *bh, int ofblocktype) 
210 {
211         FileData *fd= (FileData*) bh;
212         LinkNode *names= NULL;
213         BHead *bhead;
214
215         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
216                 if (bhead->code==ofblocktype) {
217                         char *idname= bhead_id_name(fd, bhead);
218                         
219                         BLI_linklist_prepend(&names, strdup(idname+2));
220                 } else if (bhead->code==ENDB)
221                         break;
222         }
223         
224         return names;
225 }
226
227 LinkNode *BLO_blendhandle_get_previews(BlendHandle *bh, int ofblocktype) 
228 {
229         FileData *fd= (FileData*) bh;
230         LinkNode *previews= NULL;
231         BHead *bhead;
232         int looking=0;
233         int npreviews = 0;
234         PreviewImage* prv = NULL;
235         PreviewImage* new_prv = NULL;
236         
237         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
238                 if (bhead->code==ofblocktype) {
239                         ID *id= (ID*) (bhead+1);
240                         if ( (GS(id->name) == ID_MA) || (GS(id->name) == ID_TE)) {
241                                 new_prv = MEM_callocN(sizeof(PreviewImage), "newpreview");
242                                 BLI_linklist_prepend(&previews, new_prv);
243                                 looking = 1;
244                         }
245                 } else if (bhead->code==DATA) {
246                         if (looking) {
247                                 if (bhead->SDNAnr == DNA_struct_find_nr(fd->filesdna, "PreviewImage") ) {
248                                         prv = (PreviewImage*) (bhead+1);
249                                         npreviews = 0;                          
250                                         memcpy(new_prv, prv, sizeof(PreviewImage));
251                                         if (prv->rect[0]) {
252                                                 unsigned int *rect = NULL;
253                                                 int rectlen = 0;
254                                                 new_prv->rect[0] = MEM_callocN(new_prv->w[0]*new_prv->h[0]*sizeof(unsigned int), "prvrect");
255                                                 bhead= blo_nextbhead(fd, bhead);
256                                                 rect = (unsigned int*)(bhead+1);
257                                                 rectlen = new_prv->w[0]*new_prv->h[0]*sizeof(unsigned int);
258                                                 memcpy(new_prv->rect[0], rect, bhead->len);                                     
259                                         } else {
260                                                 new_prv->rect[0] = NULL;
261                                         }
262                                         
263                                         if (prv->rect[1]) {
264                                                 unsigned int *rect = NULL;
265                                                 int rectlen = 0;
266                                                 new_prv->rect[1] = MEM_callocN(new_prv->w[1]*new_prv->h[1]*sizeof(unsigned int), "prvrect");
267                                                 bhead= blo_nextbhead(fd, bhead);
268                                                 rect = (unsigned int*)(bhead+1);
269                                                 rectlen = new_prv->w[1]*new_prv->h[1]*sizeof(unsigned int);                                     
270                                                 memcpy(new_prv->rect[1], rect, bhead->len);                                                     
271                                         } else {
272                                                 new_prv->rect[1] = NULL;
273                                         }
274                                 }
275                         }
276                 } else if (bhead->code==ENDB) {
277                         break;
278                 } else if (bhead->code==DATA) {
279                         /* DATA blocks between IDBlock and Preview */
280                 } else {
281                         looking = 0;
282                         new_prv = NULL;
283                         prv = NULL;
284                 }
285                 
286         }
287         
288         return previews;
289 }
290
291 LinkNode *BLO_blendhandle_get_linkable_groups(BlendHandle *bh) 
292 {
293         FileData *fd= (FileData*) bh;
294         GHash *gathered= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
295         LinkNode *names= NULL;
296         BHead *bhead;
297         
298         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
299                 if (bhead->code==ENDB) {
300                         break;
301                 } else if (bheadcode_is_idcode(bhead->code)) {
302                         if (idcode_is_linkable(bhead->code)) {
303                                 char *str= BLO_idcode_to_name(bhead->code);
304                                 
305                                 if (!BLI_ghash_haskey(gathered, str)) {
306                                         BLI_linklist_prepend(&names, strdup(str));
307                                         BLI_ghash_insert(gathered, str, NULL);
308                                 }
309                         }
310                 }
311         }
312         
313         BLI_ghash_free(gathered, NULL, NULL);
314         
315         return names;
316 }               
317
318 void BLO_blendhandle_close(BlendHandle *bh) {
319         FileData *fd= (FileData*) bh;
320         
321         blo_freefiledata(fd);
322 }
323
324         /**********/
325
326 BlendFileData *BLO_read_from_file(char *file, ReportList *reports)
327 {
328         BlendFileData *bfd = NULL;
329         FileData *fd;
330                 
331         fd = blo_openblenderfile(file, reports);
332         if (fd) {
333                 fd->reports= reports;
334                 bfd= blo_read_file_internal(fd);
335                 if (bfd) {
336                         bfd->type= BLENFILETYPE_BLEND;
337                         strncpy(bfd->main->name, file, sizeof(bfd->main->name)-1);
338                 }
339                 blo_freefiledata(fd);                   
340         }
341
342         return bfd;     
343 }
344
345 BlendFileData *BLO_read_from_memory(void *mem, int memsize, ReportList *reports)
346 {
347         BlendFileData *bfd = NULL;
348         FileData *fd;
349                 
350         fd = blo_openblendermemory(mem, memsize,  reports);
351         if (fd) {
352                 fd->reports= reports;
353                 bfd= blo_read_file_internal(fd);
354                 if (bfd) {
355                         bfd->type= BLENFILETYPE_BLEND;
356                         strcpy(bfd->main->name, "");
357                 }
358                 blo_freefiledata(fd);                   
359         }
360
361         return bfd;     
362 }
363
364 BlendFileData *BLO_read_from_memfile(Main *oldmain, const char *filename, MemFile *memfile, ReportList *reports)
365 {
366         BlendFileData *bfd = NULL;
367         FileData *fd;
368         ListBase mainlist;
369         
370         fd = blo_openblendermemfile(memfile, reports);
371         if (fd) {
372                 fd->reports= reports;
373                 strcpy(fd->filename, filename);
374                 
375                 /* clear ob->proxy_from pointers in old main */
376                 blo_clear_proxy_pointers_from_lib(fd, oldmain);
377
378                 /* separate libraries from old main */
379                 blo_split_main(&mainlist, oldmain);
380                 /* add the library pointers in oldmap lookup */
381                 blo_add_library_pointer_map(&mainlist, fd);
382                 
383                 /* makes lookup of existing images in old main */
384                 blo_make_image_pointer_map(fd, oldmain);
385                 
386                 bfd= blo_read_file_internal(fd);
387                 if (bfd) {
388                         bfd->type= BLENFILETYPE_BLEND;
389                         strcpy(bfd->main->name, "");
390                 }
391                 
392                 /* ensures relinked images are not freed */
393                 blo_end_image_pointer_map(fd, oldmain);
394                 
395                 /* move libraries from old main to new main */
396                 if(bfd && mainlist.first!=mainlist.last) {
397                         
398                         /* Library structs themselves */
399                         bfd->main->library= oldmain->library;
400                         oldmain->library.first= oldmain->library.last= NULL;
401                         
402                         /* add the Library mainlist to the new main */
403                         BLI_remlink(&mainlist, oldmain);
404                         BLI_addhead(&mainlist, bfd->main);
405                 }
406                 blo_join_main(&mainlist);
407                 
408                 blo_freefiledata(fd);                   
409         }
410
411         return bfd;     
412 }
413
414 void BLO_blendfiledata_free(BlendFileData *bfd)
415 {
416         if (bfd->main) {
417                 free_main(bfd->main);
418         }
419         
420         if (bfd->user) {
421                 MEM_freeN(bfd->user);
422         }
423
424         MEM_freeN(bfd);
425 }
426