Merged changes in the trunk up to revision 27992.
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 "BLI_storage.h" /* _LARGEFILE_SOURCE */
36
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdio.h>
40 #include <math.h>
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BLI_ghash.h"
45 #include "BLI_linklist.h"
46
47 #include "DNA_genfile.h"
48 #include "DNA_sdna_types.h"
49
50
51 #include "BKE_main.h"
52 #include "BKE_library.h" // for free_main
53 #include "BKE_report.h"
54
55 #include "BLO_readfile.h"
56 #include "BLO_undofile.h"
57
58 #include "readfile.h"
59
60 #include "BLO_readblenfile.h"
61
62 #include "BLO_sys_types.h" // needed for intptr_t
63
64 #ifdef _WIN32
65 #include "BLI_winstuff.h"
66 #endif
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_GD,                "GPencil",      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_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_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         BlendHandle *bh;
169
170         bh= (BlendHandle*)blo_openblenderfile(file, NULL);
171
172         return bh;
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 int)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_struct_find_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, ReportList *reports)
323 {
324         BlendFileData *bfd = NULL;
325         FileData *fd;
326                 
327         fd = blo_openblenderfile(file, reports);
328         if (fd) {
329                 fd->reports= reports;
330                 bfd= blo_read_file_internal(fd, file);
331                 blo_freefiledata(fd);                   
332         }
333
334         return bfd;     
335 }
336
337 BlendFileData *BLO_read_from_memory(void *mem, int memsize, ReportList *reports)
338 {
339         BlendFileData *bfd = NULL;
340         FileData *fd;
341                 
342         fd = blo_openblendermemory(mem, memsize,  reports);
343         if (fd) {
344                 fd->reports= reports;
345                 bfd= blo_read_file_internal(fd, "");
346                 blo_freefiledata(fd);                   
347         }
348
349         return bfd;     
350 }
351
352 BlendFileData *BLO_read_from_memfile(Main *oldmain, const char *filename, MemFile *memfile, ReportList *reports)
353 {
354         BlendFileData *bfd = NULL;
355         FileData *fd;
356         ListBase mainlist;
357         
358         fd = blo_openblendermemfile(memfile, reports);
359         if (fd) {
360                 fd->reports= reports;
361                 strcpy(fd->relabase, filename);
362                 
363                 /* clear ob->proxy_from pointers in old main */
364                 blo_clear_proxy_pointers_from_lib(fd, oldmain);
365
366                 /* separate libraries from old main */
367                 blo_split_main(&mainlist, oldmain);
368                 /* add the library pointers in oldmap lookup */
369                 blo_add_library_pointer_map(&mainlist, fd);
370                 
371                 /* makes lookup of existing images in old main */
372                 blo_make_image_pointer_map(fd, oldmain);
373                 
374                 bfd= blo_read_file_internal(fd, filename);
375                 
376                 /* ensures relinked images are not freed */
377                 blo_end_image_pointer_map(fd, oldmain);
378                 
379                 /* move libraries from old main to new main */
380                 if(bfd && mainlist.first!=mainlist.last) {
381                         
382                         /* Library structs themselves */
383                         bfd->main->library= oldmain->library;
384                         oldmain->library.first= oldmain->library.last= NULL;
385                         
386                         /* add the Library mainlist to the new main */
387                         BLI_remlink(&mainlist, oldmain);
388                         BLI_addhead(&mainlist, bfd->main);
389                 }
390                 blo_join_main(&mainlist);
391                 
392                 blo_freefiledata(fd);                   
393         }
394
395         return bfd;     
396 }
397
398 void BLO_blendfiledata_free(BlendFileData *bfd)
399 {
400         if (bfd->main) {
401                 free_main(bfd->main);
402         }
403         
404         if (bfd->user) {
405                 MEM_freeN(bfd->user);
406         }
407
408         MEM_freeN(bfd);
409 }
410