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