Final merge of HEAD (bf-blender) into the orange branch.
[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
54 #include "BKE_utildefines.h" // for ENDB
55
56 #include "BKE_main.h"
57 #include "BKE_library.h" // for free_main
58
59 #include "BLO_readfile.h"
60 #include "BLO_undofile.h"
61
62 #include "readfile.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_CA,                "Camera",       IDTYPE_FLAGS_ISLINKABLE}, 
85         { ID_CU,                "Curve",        IDTYPE_FLAGS_ISLINKABLE}, 
86         { ID_GR,                "Group",        IDTYPE_FLAGS_ISLINKABLE}, 
87         { ID_ID,                "ID",           0}, 
88         { ID_IM,                "Image",        IDTYPE_FLAGS_ISLINKABLE}, 
89         { ID_IP,                "Ipo",          IDTYPE_FLAGS_ISLINKABLE}, 
90         { ID_KE,                "Key",          0}, 
91         { ID_LA,                "Lamp",         IDTYPE_FLAGS_ISLINKABLE}, 
92         { ID_LF,                "Life",         0}, 
93         { ID_LI,                "Library",      0}, 
94         { ID_LT,                "Lattice",      IDTYPE_FLAGS_ISLINKABLE}, 
95         { ID_MA,                "Material", IDTYPE_FLAGS_ISLINKABLE}, 
96         { ID_MB,                "Metaball", IDTYPE_FLAGS_ISLINKABLE}, 
97         { ID_ME,                "Mesh",         IDTYPE_FLAGS_ISLINKABLE}, 
98         { ID_NT,                "NodeTree",     IDTYPE_FLAGS_ISLINKABLE}, 
99         { ID_OB,                "Object",       IDTYPE_FLAGS_ISLINKABLE}, 
100         { ID_SAMPLE,    "Sample",       0}, 
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, 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                         ID *id= (ID*) (bhead+1);
211                         
212                         BLI_linklist_prepend(&names, strdup(id->name+2));
213                 } else if (bhead->code==ENDB)
214                         break;
215         }
216         
217         return names;
218 }
219
220 LinkNode *BLO_blendhandle_get_linkable_groups(BlendHandle *bh) 
221 {
222         FileData *fd= (FileData*) bh;
223         GHash *gathered= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
224         LinkNode *names= NULL;
225         BHead *bhead;
226         
227         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
228                 if (bhead->code==ENDB) {
229                         break;
230                 } else if (bheadcode_is_idcode(bhead->code)) {
231                         if (idcode_is_linkable(bhead->code)) {
232                                 char *str= BLO_idcode_to_name(bhead->code);
233                                 
234                                 if (!BLI_ghash_haskey(gathered, str)) {
235                                         BLI_linklist_prepend(&names, strdup(str));
236                                         BLI_ghash_insert(gathered, str, NULL);
237                                 }
238                         }
239                 }
240         }
241         
242         BLI_ghash_free(gathered, NULL, NULL);
243         
244         return names;
245 }               
246
247 void BLO_blendhandle_close(BlendHandle *bh) {
248         FileData *fd= (FileData*) bh;
249         
250         blo_freefiledata(fd);
251 }
252
253         /**********/
254
255 BlendFileData *BLO_read_from_file(char *file, BlendReadError *error_r) 
256 {
257         BlendFileData *bfd = NULL;
258         FileData *fd;
259                 
260         fd = blo_openblenderfile(file, error_r);
261         if (fd) {
262                 bfd= blo_read_file_internal(fd, error_r);
263                 if (bfd) {
264                         bfd->type= BLENFILETYPE_BLEND;
265                         strcpy(bfd->main->name, file);
266                 }
267                 blo_freefiledata(fd);                   
268         }
269
270         return bfd;     
271 }
272
273 BlendFileData *BLO_read_from_memory(void *mem, int memsize, BlendReadError *error_r) 
274 {
275         BlendFileData *bfd = NULL;
276         FileData *fd;
277                 
278         fd = blo_openblendermemory(mem, memsize,  error_r);
279         if (fd) {
280                 bfd= blo_read_file_internal(fd, error_r);
281                 if (bfd) {
282                         bfd->type= BLENFILETYPE_BLEND;
283                         strcpy(bfd->main->name, "");
284                 }
285                 blo_freefiledata(fd);                   
286         }
287
288         return bfd;     
289 }
290
291 BlendFileData *BLO_read_from_memfile(const char *filename, MemFile *memfile, BlendReadError *error_r) 
292 {
293         BlendFileData *bfd = NULL;
294         FileData *fd;
295                 
296         fd = blo_openblendermemfile(memfile, error_r);
297         if (fd) {
298                 strcpy(fd->filename, filename);
299                 bfd= blo_read_file_internal(fd, error_r);
300                 if (bfd) {
301                         bfd->type= BLENFILETYPE_BLEND;
302                         strcpy(bfd->main->name, "");
303                 }
304                 blo_freefiledata(fd);                   
305         }
306
307         return bfd;     
308 }
309
310 void BLO_blendfiledata_free(BlendFileData *bfd)
311 {
312         if (bfd->main) {
313                 free_main(bfd->main);
314         }
315         
316         if (bfd->user) {
317                 MEM_freeN(bfd->user);
318         }
319
320         MEM_freeN(bfd);
321 }
322
323 char *BLO_bre_as_string(BlendReadError error) 
324 {
325         switch (error) {
326         case BRE_NONE:
327                 return "No error";
328         
329         case BRE_UNABLE_TO_OPEN:
330                 return "Unable to open";
331         case BRE_UNABLE_TO_READ:
332                 return "Unable to read";
333                 
334         case BRE_OUT_OF_MEMORY:
335                 return "Out of memory";
336         case BRE_INTERNAL_ERROR:
337                 return "<internal error>";
338
339         case BRE_NOT_A_BLEND:
340                 return "File is not a Blender file";
341         case BRE_NOT_A_PUBFILE:
342                 return "File is not a compressed, locked or signed Blender file";
343         case BRE_INCOMPLETE:
344                 return "File incomplete";
345         case BRE_CORRUPT:
346                 return "File corrupt";
347
348         case BRE_TOO_NEW:
349                 return "File needs newer Blender version, please upgrade";
350         case BRE_NOT_ALLOWED:
351                 return "File is locked";
352                                                 
353         case BRE_NO_SCREEN:
354                 return "File has no screen";
355         case BRE_NO_SCENE:
356                 return "File has no scene";
357                 
358         default:
359         case BRE_INVALID:
360                 return "<invalid read error>";
361         }
362 }