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