028835ee0afa0891177f8ab19903cd51fd3cc04a
[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 /** \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_sys_types.h" // needed for intptr_t
66
67 #ifdef _WIN32
68 #include "BLI_winstuff.h"
69 #endif
70
71 /* local prototypes --------------------- */
72 void BLO_blendhandle_print_sizes(BlendHandle *, void *); 
73
74         /* Access routines used by filesel. */
75          
76 BlendHandle *BLO_blendhandle_from_file(char *file, ReportList *reports)
77 {
78         BlendHandle *bh;
79
80         bh= (BlendHandle*)blo_openblenderfile(file, reports);
81
82         return bh;
83 }
84
85 BlendHandle *BLO_blendhandle_from_memory(void *mem, int memsize)
86 {
87         BlendHandle *bh;
88
89         bh= (BlendHandle*)blo_openblendermemory(mem, memsize, NULL);
90
91         return bh;
92 }
93
94 void BLO_blendhandle_print_sizes(BlendHandle *bh, void *fp) 
95 {
96         FileData *fd= (FileData*) bh;
97         BHead *bhead;
98
99         fprintf(fp, "[\n");
100         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
101                 if (bhead->code==ENDB)
102                         break;
103                 else {
104                         short *sp= fd->filesdna->structs[bhead->SDNAnr];
105                         char *name= fd->filesdna->types[ sp[0] ];
106                         char buf[4];
107                         
108                         buf[0]= (bhead->code>>24)&0xFF;
109                         buf[1]= (bhead->code>>16)&0xFF;
110                         buf[2]= (bhead->code>>8)&0xFF;
111                         buf[3]= (bhead->code>>0)&0xFF;
112                         
113                         buf[0]= buf[0]?buf[0]:' ';
114                         buf[1]= buf[1]?buf[1]:' ';
115                         buf[2]= buf[2]?buf[2]:' ';
116                         buf[3]= buf[3]?buf[3]:' ';
117                         
118                         fprintf(fp, "['%.4s', '%s', %d, %ld ], \n", buf, name, bhead->nr, (long int)bhead->len+sizeof(BHead));
119                 }
120         }
121         fprintf(fp, "]\n");
122 }
123
124 LinkNode *BLO_blendhandle_get_datablock_names(BlendHandle *bh, int ofblocktype, int *tot_names)
125 {
126         FileData *fd= (FileData*) bh;
127         LinkNode *names= NULL;
128         BHead *bhead;
129         int tot= 0;
130
131         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
132                 if (bhead->code==ofblocktype) {
133                         char *idname= bhead_id_name(fd, bhead);
134
135                         BLI_linklist_prepend(&names, strdup(idname+2));
136                         tot++;
137                 } else if (bhead->code==ENDB)
138                         break;
139         }
140
141         *tot_names= tot;
142         return names;
143 }
144
145 LinkNode *BLO_blendhandle_get_previews(BlendHandle *bh, int ofblocktype, int *tot_prev)
146 {
147         FileData *fd= (FileData*) bh;
148         LinkNode *previews= NULL;
149         BHead *bhead;
150         int looking=0;
151         int npreviews = 0;
152         PreviewImage* prv = NULL;
153         PreviewImage* new_prv = NULL;
154         int tot= 0;
155         
156         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
157                 if (bhead->code==ofblocktype) {
158                         ID *id= (ID*) (bhead+1);
159                         switch(GS(id->name))
160                         {
161                                 case ID_MA: /* fall through */
162                                 case ID_TE: /* fall through */
163                                 case ID_IM: /* fall through */
164                                 case ID_WO: /* fall through */
165                                 case ID_LA: /* fall through */
166                                         new_prv = MEM_callocN(sizeof(PreviewImage), "newpreview");
167                                         BLI_linklist_prepend(&previews, new_prv);
168                                         tot++;
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 {
208                         looking = 0;
209                         new_prv = NULL;
210                         prv = NULL;
211                 }
212                 
213         }
214
215         *tot_prev= tot;
216         return previews;
217 }
218
219 LinkNode *BLO_blendhandle_get_linkable_groups(BlendHandle *bh) 
220 {
221         FileData *fd= (FileData*) bh;
222         GHash *gathered= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "linkable_groups gh");
223         LinkNode *names= NULL;
224         BHead *bhead;
225         
226         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
227                 if (bhead->code==ENDB) {
228                         break;
229                 } else if (BKE_idcode_is_valid(bhead->code)) {
230                         if (BKE_idcode_is_linkable(bhead->code)) {
231                                 const char *str= BKE_idcode_to_name(bhead->code);
232                                 
233                                 if (!BLI_ghash_haskey(gathered, (void *)str)) {
234                                         BLI_linklist_prepend(&names, strdup(str));
235                                         BLI_ghash_insert(gathered, (void *)str, NULL);
236                                 }
237                         }
238                 }
239         }
240         
241         BLI_ghash_free(gathered, NULL, NULL);
242         
243         return names;
244 }               
245
246 void BLO_blendhandle_close(BlendHandle *bh) {
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                 strcpy(fd->relabase, filename);
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