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