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