eb12a7bd837d2eff0c4fda2ab3a891434fdbae7c
[blender.git] / source / blender / blenloader / intern / readblenentry.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  * .blend file reading entry point
27  */
28
29 /** \file blender/blenloader/intern/readblenentry.c
30  *  \ingroup blenloader
31  */
32
33
34 #include <stddef.h>
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <math.h>
40
41 #include "MEM_guardedalloc.h"
42
43 #include "BLI_fileops.h"
44 #include "BLI_ghash.h"
45 #include "BLI_linklist.h"
46 #include "BLI_listbase.h"
47 #include "BLI_string.h"
48 #include "BLI_utildefines.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                 }
138                 else if (bhead->code == ENDB)
139                         break;
140         }
141
142         *tot_names = tot;
143         return names;
144 }
145
146 LinkNode *BLO_blendhandle_get_previews(BlendHandle *bh, int ofblocktype, int *tot_prev)
147 {
148         FileData *fd = (FileData *) bh;
149         LinkNode *previews = NULL;
150         BHead *bhead;
151         int looking = 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                         char *idname = bhead_id_name(fd, bhead);
159                         switch (GS(idname)) {
160                                 case ID_MA: /* fall through */
161                                 case ID_TE: /* fall through */
162                                 case ID_IM: /* fall through */
163                                 case ID_WO: /* fall through */
164                                 case ID_LA: /* fall through */
165                                         new_prv = MEM_callocN(sizeof(PreviewImage), "newpreview");
166                                         BLI_linklist_prepend(&previews, new_prv);
167                                         tot++;
168                                         looking = 1;
169                                         break;
170                                 default:
171                                         break;
172                         }
173                 }
174                 else if (bhead->code == DATA) {
175                         if (looking) {
176                                 if (bhead->SDNAnr == DNA_struct_find_nr(fd->filesdna, "PreviewImage") ) {
177                                         prv = BLO_library_read_struct(fd, bhead, "PreviewImage");       
178                                         if (prv) {
179                                                 memcpy(new_prv, prv, sizeof(PreviewImage));
180                                                 if (prv->rect[0]) {
181                                                         unsigned int *rect = NULL;
182                                                         new_prv->rect[0] = MEM_callocN(new_prv->w[0] * new_prv->h[0] * sizeof(unsigned int), "prvrect");
183                                                         bhead = blo_nextbhead(fd, bhead);
184                                                         rect = (unsigned int *)(bhead + 1);
185                                                         memcpy(new_prv->rect[0], rect, bhead->len);                                     
186                                                 }
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                                                 }
198                                                 else {
199                                                         new_prv->rect[1] = NULL;
200                                                 }
201                                                 MEM_freeN(prv);
202                                         }
203                                 }
204                         }
205                 }
206                 else if (bhead->code == ENDB) {
207                         break;
208                 }
209                 else {
210                         looking = 0;
211                         new_prv = NULL;
212                         prv = NULL;
213                 }
214                 
215         }
216
217         *tot_prev = tot;
218         return previews;
219 }
220
221 LinkNode *BLO_blendhandle_get_linkable_groups(BlendHandle *bh) 
222 {
223         FileData *fd = (FileData *) bh;
224         GHash *gathered = BLI_ghash_ptr_new("linkable_groups gh");
225         LinkNode *names = NULL;
226         BHead *bhead;
227         
228         for (bhead = blo_firstbhead(fd); bhead; bhead = blo_nextbhead(fd, bhead)) {
229                 if (bhead->code == ENDB) {
230                         break;
231                 }
232                 else if (BKE_idcode_is_valid(bhead->code)) {
233                         if (BKE_idcode_is_linkable(bhead->code)) {
234                                 const char *str = BKE_idcode_to_name(bhead->code);
235                                 
236                                 if (!BLI_ghash_haskey(gathered, (void *)str)) {
237                                         BLI_linklist_prepend(&names, strdup(str));
238                                         BLI_ghash_insert(gathered, (void *)str, NULL);
239                                 }
240                         }
241                 }
242         }
243         
244         BLI_ghash_free(gathered, NULL, NULL);
245         
246         return names;
247 }               
248
249 void BLO_blendhandle_close(BlendHandle *bh)
250 {
251         FileData *fd = (FileData *) bh;
252         
253         blo_freefiledata(fd);
254 }
255
256 /**********/
257
258 BlendFileData *BLO_read_from_file(const char *filepath, ReportList *reports)
259 {
260         BlendFileData *bfd = NULL;
261         FileData *fd;
262                 
263         fd = blo_openblenderfile(filepath, reports);
264         if (fd) {
265                 fd->reports = reports;
266                 bfd = blo_read_file_internal(fd, filepath);
267                 blo_freefiledata(fd);                   
268         }
269
270         return bfd;     
271 }
272
273 BlendFileData *BLO_read_from_memory(void *mem, int memsize, ReportList *reports)
274 {
275         BlendFileData *bfd = NULL;
276         FileData *fd;
277                 
278         fd = blo_openblendermemory(mem, memsize,  reports);
279         if (fd) {
280                 fd->reports = reports;
281                 bfd = blo_read_file_internal(fd, "");
282                 blo_freefiledata(fd);                   
283         }
284
285         return bfd;     
286 }
287
288 BlendFileData *BLO_read_from_memfile(Main *oldmain, const char *filename, MemFile *memfile, ReportList *reports)
289 {
290         BlendFileData *bfd = NULL;
291         FileData *fd;
292         ListBase mainlist;
293         
294         fd = blo_openblendermemfile(memfile, reports);
295         if (fd) {
296                 fd->reports = reports;
297                 BLI_strncpy(fd->relabase, filename, sizeof(fd->relabase));
298                 
299                 /* clear ob->proxy_from pointers in old main */
300                 blo_clear_proxy_pointers_from_lib(oldmain);
301
302                 /* separate libraries from old main */
303                 blo_split_main(&mainlist, oldmain);
304                 /* add the library pointers in oldmap lookup */
305                 blo_add_library_pointer_map(&mainlist, fd);
306                 
307                 /* makes lookup of existing images in old main */
308                 blo_make_image_pointer_map(fd, oldmain);
309                 
310                 /* makes lookup of existing video clips in old main */
311                 blo_make_movieclip_pointer_map(fd, oldmain);
312                 
313                 bfd = blo_read_file_internal(fd, filename);
314                 
315                 /* ensures relinked images are not freed */
316                 blo_end_image_pointer_map(fd, oldmain);
317                 
318                 /* ensures relinked movie clips are not freed */
319                 blo_end_movieclip_pointer_map(fd, oldmain);
320                 
321                 /* move libraries from old main to new main */
322                 if (bfd && mainlist.first != mainlist.last) {
323                         
324                         /* Library structs themselves */
325                         bfd->main->library = oldmain->library;
326                         oldmain->library.first = oldmain->library.last = NULL;
327                         
328                         /* add the Library mainlist to the new main */
329                         BLI_remlink(&mainlist, oldmain);
330                         BLI_addhead(&mainlist, bfd->main);
331                 }
332                 blo_join_main(&mainlist);
333                 
334                 blo_freefiledata(fd);                   
335         }
336
337         return bfd;     
338 }
339
340 void BLO_blendfiledata_free(BlendFileData *bfd)
341 {
342         if (bfd->main) {
343                 free_main(bfd->main);
344         }
345         
346         if (bfd->user) {
347                 MEM_freeN(bfd->user);
348         }
349
350         MEM_freeN(bfd);
351 }
352