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