ae4ba6629a66fd5c4151dd2c8285e29062acc825
[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 #include "BLI_storage.h" /* _LARGEFILE_SOURCE */
38
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdio.h>
42 #include <math.h>
43
44 #include "MEM_guardedalloc.h"
45
46 #include "BLI_utildefines.h"
47 #include "BLI_ghash.h"
48 #include "BLI_linklist.h"
49
50 #include "DNA_genfile.h"
51 #include "DNA_sdna_types.h"
52
53
54 #include "BKE_main.h"
55 #include "BKE_library.h" // for free_main
56 #include "BKE_idcode.h"
57 #include "BKE_report.h"
58 #include "BKE_utildefines.h"
59
60 #include "BLO_readfile.h"
61 #include "BLO_undofile.h"
62
63 #include "readfile.h"
64
65 #include "BLO_readblenfile.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) 
148 {
149         FileData *fd= (FileData*) bh;
150         LinkNode *previews= NULL;
151         BHead *bhead;
152         int looking=0;
153         int npreviews = 0;
154         PreviewImage* prv = NULL;
155         PreviewImage* new_prv = NULL;
156         
157         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
158                 if (bhead->code==ofblocktype) {
159                         ID *id= (ID*) (bhead+1);
160                         switch(GS(id->name))
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                                         looking = 1;
170                                         break;
171                                 default:
172                                         break;
173                         }
174                 } else if (bhead->code==DATA) {
175                         if (looking) {
176                                 if (bhead->SDNAnr == DNA_struct_find_nr(fd->filesdna, "PreviewImage") ) {
177                                         prv = (PreviewImage*) (bhead+1);
178                                         npreviews = 0;                          
179                                         memcpy(new_prv, prv, sizeof(PreviewImage));
180                                         if (prv->rect[0]) {
181                                                 unsigned int *rect = NULL;
182                                                 // int rectlen = 0;
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                                                 // rectlen = new_prv->w[0]*new_prv->h[0]*sizeof(unsigned int);
187                                                 memcpy(new_prv->rect[0], rect, bhead->len);                                     
188                                         } else {
189                                                 new_prv->rect[0] = NULL;
190                                         }
191                                         
192                                         if (prv->rect[1]) {
193                                                 unsigned int *rect = NULL;
194                                                 // int rectlen = 0;
195                                                 new_prv->rect[1] = MEM_callocN(new_prv->w[1]*new_prv->h[1]*sizeof(unsigned int), "prvrect");
196                                                 bhead= blo_nextbhead(fd, bhead);
197                                                 rect = (unsigned int*)(bhead+1);
198                                                 // rectlen = new_prv->w[1]*new_prv->h[1]*sizeof(unsigned int);
199                                                 memcpy(new_prv->rect[1], rect, bhead->len);                                                     
200                                         } else {
201                                                 new_prv->rect[1] = NULL;
202                                         }
203                                 }
204                         }
205                 } else if (bhead->code==ENDB) {
206                         break;
207                 } else if (bhead->code==DATA) {
208                         /* DATA blocks between IDBlock and Preview */
209                 } else {
210                         looking = 0;
211                         new_prv = NULL;
212                         prv = NULL;
213                 }
214                 
215         }
216         
217         return previews;
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, "linkable_groups gh");
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 (BKE_idcode_is_valid(bhead->code)) {
231                         if (BKE_idcode_is_linkable(bhead->code)) {
232                                 const char *str= BKE_idcode_to_name(bhead->code);
233                                 
234                                 if (!BLI_ghash_haskey(gathered, (void *)str)) {
235                                         BLI_linklist_prepend(&names, strdup(str));
236                                         BLI_ghash_insert(gathered, (void *)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(const char *file, ReportList *reports)
256 {
257         BlendFileData *bfd = NULL;
258         FileData *fd;
259                 
260         fd = blo_openblenderfile(file, reports);
261         if (fd) {
262                 fd->reports= reports;
263                 bfd= blo_read_file_internal(fd, file);
264                 blo_freefiledata(fd);                   
265         }
266
267         return bfd;     
268 }
269
270 BlendFileData *BLO_read_from_memory(void *mem, int memsize, ReportList *reports)
271 {
272         BlendFileData *bfd = NULL;
273         FileData *fd;
274                 
275         fd = blo_openblendermemory(mem, memsize,  reports);
276         if (fd) {
277                 fd->reports= reports;
278                 bfd= blo_read_file_internal(fd, "");
279                 blo_freefiledata(fd);                   
280         }
281
282         return bfd;     
283 }
284
285 BlendFileData *BLO_read_from_memfile(Main *oldmain, const char *filename, MemFile *memfile, ReportList *reports)
286 {
287         BlendFileData *bfd = NULL;
288         FileData *fd;
289         ListBase mainlist;
290         
291         fd = blo_openblendermemfile(memfile, reports);
292         if (fd) {
293                 fd->reports= reports;
294                 strcpy(fd->relabase, filename);
295                 
296                 /* clear ob->proxy_from pointers in old main */
297                 blo_clear_proxy_pointers_from_lib(oldmain);
298
299                 /* separate libraries from old main */
300                 blo_split_main(&mainlist, oldmain);
301                 /* add the library pointers in oldmap lookup */
302                 blo_add_library_pointer_map(&mainlist, fd);
303                 
304                 /* makes lookup of existing images in old main */
305                 blo_make_image_pointer_map(fd, oldmain);
306                 
307                 bfd= blo_read_file_internal(fd, filename);
308                 
309                 /* ensures relinked images are not freed */
310                 blo_end_image_pointer_map(fd, oldmain);
311                 
312                 /* move libraries from old main to new main */
313                 if(bfd && mainlist.first!=mainlist.last) {
314                         
315                         /* Library structs themselves */
316                         bfd->main->library= oldmain->library;
317                         oldmain->library.first= oldmain->library.last= NULL;
318                         
319                         /* add the Library mainlist to the new main */
320                         BLI_remlink(&mainlist, oldmain);
321                         BLI_addhead(&mainlist, bfd->main);
322                 }
323                 blo_join_main(&mainlist);
324                 
325                 blo_freefiledata(fd);                   
326         }
327
328         return bfd;     
329 }
330
331 void BLO_blendfiledata_free(BlendFileData *bfd)
332 {
333         if (bfd->main) {
334                 free_main(bfd->main);
335         }
336         
337         if (bfd->user) {
338                 MEM_freeN(bfd->user);
339         }
340
341         MEM_freeN(bfd);
342 }
343