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