Cycles: svn merge -r41182:41205 ^/trunk/blender
[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., 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 /** \file blender/blenloader/intern/readblenentry.c
32  *  \ingroup blenloader
33  */
34
35
36 #include <stddef.h>
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_fileops.h"
46 #include "BLI_ghash.h"
47 #include "BLI_linklist.h"
48 #include "BLI_listbase.h"
49 #include "BLI_string.h"
50 #include "BLI_utildefines.h"
51
52 #include "DNA_genfile.h"
53 #include "DNA_sdna_types.h"
54
55
56 #include "BKE_main.h"
57 #include "BKE_library.h" // for free_main
58 #include "BKE_idcode.h"
59 #include "BKE_report.h"
60 #include "BKE_utildefines.h"
61
62 #include "BLO_readfile.h"
63 #include "BLO_undofile.h"
64
65 #include "readfile.h"
66
67 #include "BLO_sys_types.h" // needed for intptr_t
68
69 #ifdef _WIN32
70 #include "BLI_winstuff.h"
71 #endif
72
73 /* local prototypes --------------------- */
74 void BLO_blendhandle_print_sizes(BlendHandle *, void *); 
75
76         /* Access routines used by filesel. */
77          
78 BlendHandle *BLO_blendhandle_from_file(char *file, ReportList *reports)
79 {
80         BlendHandle *bh;
81
82         bh= (BlendHandle*)blo_openblenderfile(file, reports);
83
84         return bh;
85 }
86
87 BlendHandle *BLO_blendhandle_from_memory(void *mem, int memsize)
88 {
89         BlendHandle *bh;
90
91         bh= (BlendHandle*)blo_openblendermemory(mem, memsize, NULL);
92
93         return bh;
94 }
95
96 void BLO_blendhandle_print_sizes(BlendHandle *bh, void *fp) 
97 {
98         FileData *fd= (FileData*) bh;
99         BHead *bhead;
100
101         fprintf(fp, "[\n");
102         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
103                 if (bhead->code==ENDB)
104                         break;
105                 else {
106                         short *sp= fd->filesdna->structs[bhead->SDNAnr];
107                         char *name= fd->filesdna->types[ sp[0] ];
108                         char buf[4];
109                         
110                         buf[0]= (bhead->code>>24)&0xFF;
111                         buf[1]= (bhead->code>>16)&0xFF;
112                         buf[2]= (bhead->code>>8)&0xFF;
113                         buf[3]= (bhead->code>>0)&0xFF;
114                         
115                         buf[0]= buf[0]?buf[0]:' ';
116                         buf[1]= buf[1]?buf[1]:' ';
117                         buf[2]= buf[2]?buf[2]:' ';
118                         buf[3]= buf[3]?buf[3]:' ';
119                         
120                         fprintf(fp, "['%.4s', '%s', %d, %ld ], \n", buf, name, bhead->nr, (long int)(bhead->len+sizeof(BHead)));
121                 }
122         }
123         fprintf(fp, "]\n");
124 }
125
126 LinkNode *BLO_blendhandle_get_datablock_names(BlendHandle *bh, int ofblocktype, int *tot_names)
127 {
128         FileData *fd= (FileData*) bh;
129         LinkNode *names= NULL;
130         BHead *bhead;
131         int tot= 0;
132
133         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
134                 if (bhead->code==ofblocktype) {
135                         char *idname= bhead_id_name(fd, bhead);
136
137                         BLI_linklist_prepend(&names, strdup(idname+2));
138                         tot++;
139                 } else if (bhead->code==ENDB)
140                         break;
141         }
142
143         *tot_names= tot;
144         return names;
145 }
146
147 LinkNode *BLO_blendhandle_get_previews(BlendHandle *bh, int ofblocktype, int *tot_prev)
148 {
149         FileData *fd= (FileData*) bh;
150         LinkNode *previews= NULL;
151         BHead *bhead;
152         int looking=0;
153         PreviewImage* prv = NULL;
154         PreviewImage* new_prv = NULL;
155         int tot= 0;
156         
157         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
158                 if (bhead->code==ofblocktype) {
159                         char *idname= bhead_id_name(fd, bhead);
160                         switch(GS(idname))
161                         {
162                                 case ID_MA: /* fall through */
163                                 case ID_TE: /* fall through */
164                                 case ID_IM: /* fall through */
165                                 case ID_WO: /* fall through */
166                                 case ID_LA: /* fall through */
167                                         new_prv = MEM_callocN(sizeof(PreviewImage), "newpreview");
168                                         BLI_linklist_prepend(&previews, new_prv);
169                                         tot++;
170                                         looking = 1;
171                                         break;
172                                 default:
173                                         break;
174                         }
175                 } else if (bhead->code==DATA) {
176                         if (looking) {
177                                 if (bhead->SDNAnr == DNA_struct_find_nr(fd->filesdna, "PreviewImage") ) {
178                                         prv = BLO_library_read_struct(fd, bhead, "PreviewImage");       
179                                         if (prv) {
180                                                 memcpy(new_prv, prv, sizeof(PreviewImage));
181                                                 if (prv->rect[0]) {
182                                                         unsigned int *rect = NULL;
183                                                         new_prv->rect[0] = MEM_callocN(new_prv->w[0]*new_prv->h[0]*sizeof(unsigned int), "prvrect");
184                                                         bhead= blo_nextbhead(fd, bhead);
185                                                         rect = (unsigned int*)(bhead+1);
186                                                         memcpy(new_prv->rect[0], rect, bhead->len);                                     
187                                                 } else {
188                                                         new_prv->rect[0] = NULL;
189                                                 }
190                                                 
191                                                 if (prv->rect[1]) {
192                                                         unsigned int *rect = NULL;
193                                                         new_prv->rect[1] = MEM_callocN(new_prv->w[1]*new_prv->h[1]*sizeof(unsigned int), "prvrect");
194                                                         bhead= blo_nextbhead(fd, bhead);
195                                                         rect = (unsigned int*)(bhead+1);
196                                                         memcpy(new_prv->rect[1], rect, bhead->len);                                                     
197                                                 } else {
198                                                         new_prv->rect[1] = NULL;
199                                                 }
200                                                 MEM_freeN(prv);
201                                         }
202                                 }
203                         }
204                 } else if (bhead->code==ENDB) {
205                         break;
206                 } else {
207                         looking = 0;
208                         new_prv = NULL;
209                         prv = NULL;
210                 }
211                 
212         }
213
214         *tot_prev= tot;
215         return previews;
216 }
217
218 LinkNode *BLO_blendhandle_get_linkable_groups(BlendHandle *bh) 
219 {
220         FileData *fd= (FileData*) bh;
221         GHash *gathered= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "linkable_groups gh");
222         LinkNode *names= NULL;
223         BHead *bhead;
224         
225         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
226                 if (bhead->code==ENDB) {
227                         break;
228                 } else if (BKE_idcode_is_valid(bhead->code)) {
229                         if (BKE_idcode_is_linkable(bhead->code)) {
230                                 const char *str= BKE_idcode_to_name(bhead->code);
231                                 
232                                 if (!BLI_ghash_haskey(gathered, (void *)str)) {
233                                         BLI_linklist_prepend(&names, strdup(str));
234                                         BLI_ghash_insert(gathered, (void *)str, NULL);
235                                 }
236                         }
237                 }
238         }
239         
240         BLI_ghash_free(gathered, NULL, NULL);
241         
242         return names;
243 }               
244
245 void BLO_blendhandle_close(BlendHandle *bh)
246 {
247         FileData *fd= (FileData*) bh;
248         
249         blo_freefiledata(fd);
250 }
251
252         /**********/
253
254 BlendFileData *BLO_read_from_file(const char *filepath, ReportList *reports)
255 {
256         BlendFileData *bfd = NULL;
257         FileData *fd;
258                 
259         fd = blo_openblenderfile(filepath, reports);
260         if (fd) {
261                 fd->reports= reports;
262                 bfd= blo_read_file_internal(fd, filepath);
263                 blo_freefiledata(fd);                   
264         }
265
266         return bfd;     
267 }
268
269 BlendFileData *BLO_read_from_memory(void *mem, int memsize, ReportList *reports)
270 {
271         BlendFileData *bfd = NULL;
272         FileData *fd;
273                 
274         fd = blo_openblendermemory(mem, memsize,  reports);
275         if (fd) {
276                 fd->reports= reports;
277                 bfd= blo_read_file_internal(fd, "");
278                 blo_freefiledata(fd);                   
279         }
280
281         return bfd;     
282 }
283
284 BlendFileData *BLO_read_from_memfile(Main *oldmain, const char *filename, MemFile *memfile, ReportList *reports)
285 {
286         BlendFileData *bfd = NULL;
287         FileData *fd;
288         ListBase mainlist;
289         
290         fd = blo_openblendermemfile(memfile, reports);
291         if (fd) {
292                 fd->reports= reports;
293                 BLI_strncpy(fd->relabase, filename, sizeof(fd->relabase));
294                 
295                 /* clear ob->proxy_from pointers in old main */
296                 blo_clear_proxy_pointers_from_lib(oldmain);
297
298                 /* separate libraries from old main */
299                 blo_split_main(&mainlist, oldmain);
300                 /* add the library pointers in oldmap lookup */
301                 blo_add_library_pointer_map(&mainlist, fd);
302                 
303                 /* makes lookup of existing images in old main */
304                 blo_make_image_pointer_map(fd, oldmain);
305                 
306                 bfd= blo_read_file_internal(fd, filename);
307                 
308                 /* ensures relinked images are not freed */
309                 blo_end_image_pointer_map(fd, oldmain);
310                 
311                 /* move libraries from old main to new main */
312                 if(bfd && mainlist.first!=mainlist.last) {
313                         
314                         /* Library structs themselves */
315                         bfd->main->library= oldmain->library;
316                         oldmain->library.first= oldmain->library.last= NULL;
317                         
318                         /* add the Library mainlist to the new main */
319                         BLI_remlink(&mainlist, oldmain);
320                         BLI_addhead(&mainlist, bfd->main);
321                 }
322                 blo_join_main(&mainlist);
323                 
324                 blo_freefiledata(fd);                   
325         }
326
327         return bfd;     
328 }
329
330 void BLO_blendfiledata_free(BlendFileData *bfd)
331 {
332         if (bfd->main) {
333                 free_main(bfd->main);
334         }
335         
336         if (bfd->user) {
337                 MEM_freeN(bfd->user);
338         }
339
340         MEM_freeN(bfd);
341 }
342