code cleanup: split out defines in BKE_utildefines.h into BLO_blend_defs.h and ui...
[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 #include "BLO_blend_defs.h"
63
64 #include "readfile.h"
65
66 #include "BLO_sys_types.h" // needed for intptr_t
67
68 #ifdef WIN32
69 #  include "BLI_winstuff.h"
70 #endif
71
72 /* local prototypes --------------------- */
73 void BLO_blendhandle_print_sizes(BlendHandle *, void *); 
74
75 /* Access routines used by filesel. */
76          
77 BlendHandle *BLO_blendhandle_from_file(char *file, ReportList *reports)
78 {
79         BlendHandle *bh;
80
81         bh = (BlendHandle *)blo_openblenderfile(file, reports);
82
83         return bh;
84 }
85
86 BlendHandle *BLO_blendhandle_from_memory(void *mem, int memsize)
87 {
88         BlendHandle *bh;
89
90         bh = (BlendHandle *)blo_openblendermemory(mem, memsize, NULL);
91
92         return bh;
93 }
94
95 void BLO_blendhandle_print_sizes(BlendHandle *bh, void *fp) 
96 {
97         FileData *fd = (FileData *) bh;
98         BHead *bhead;
99
100         fprintf(fp, "[\n");
101         for (bhead = blo_firstbhead(fd); bhead; bhead = blo_nextbhead(fd, bhead)) {
102                 if (bhead->code == ENDB)
103                         break;
104                 else {
105                         short *sp = fd->filesdna->structs[bhead->SDNAnr];
106                         char *name = fd->filesdna->types[sp[0]];
107                         char buf[4];
108                         
109                         buf[0] = (bhead->code >> 24) & 0xFF;
110                         buf[1] = (bhead->code >> 16) & 0xFF;
111                         buf[2] = (bhead->code >> 8) & 0xFF;
112                         buf[3] = (bhead->code >> 0) & 0xFF;
113
114                         buf[0] = buf[0] ? buf[0] : ' ';
115                         buf[1] = buf[1] ? buf[1] : ' ';
116                         buf[2] = buf[2] ? buf[2] : ' ';
117                         buf[3] = buf[3] ? buf[3] : ' ';
118                         
119                         fprintf(fp, "['%.4s', '%s', %d, %ld ],\n", buf, name, bhead->nr, (long int)(bhead->len + sizeof(BHead)));
120                 }
121         }
122         fprintf(fp, "]\n");
123 }
124
125 LinkNode *BLO_blendhandle_get_datablock_names(BlendHandle *bh, int ofblocktype, int *tot_names)
126 {
127         FileData *fd = (FileData *) bh;
128         LinkNode *names = NULL;
129         BHead *bhead;
130         int tot = 0;
131
132         for (bhead = blo_firstbhead(fd); bhead; bhead = blo_nextbhead(fd, bhead)) {
133                 if (bhead->code == ofblocktype) {
134                         char *idname = bhead_id_name(fd, bhead);
135
136                         BLI_linklist_prepend(&names, strdup(idname + 2));
137                         tot++;
138                 }
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                                 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                 }
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                                                 }
188                                                 else {
189                                                         new_prv->rect[0] = NULL;
190                                                 }
191                                                 
192                                                 if (prv->rect[1]) {
193                                                         unsigned int *rect = NULL;
194                                                         new_prv->rect[1] = MEM_callocN(new_prv->w[1] * new_prv->h[1] * sizeof(unsigned int), "prvrect");
195                                                         bhead = blo_nextbhead(fd, bhead);
196                                                         rect = (unsigned int *)(bhead + 1);
197                                                         memcpy(new_prv->rect[1], rect, bhead->len);                                                     
198                                                 }
199                                                 else {
200                                                         new_prv->rect[1] = NULL;
201                                                 }
202                                                 MEM_freeN(prv);
203                                         }
204                                 }
205                         }
206                 }
207                 else if (bhead->code == ENDB) {
208                         break;
209                 }
210                 else {
211                         looking = 0;
212                         new_prv = NULL;
213                         prv = NULL;
214                 }
215                 
216         }
217
218         *tot_prev = tot;
219         return previews;
220 }
221
222 LinkNode *BLO_blendhandle_get_linkable_groups(BlendHandle *bh) 
223 {
224         FileData *fd = (FileData *) bh;
225         GHash *gathered = BLI_ghash_ptr_new("linkable_groups gh");
226         LinkNode *names = NULL;
227         BHead *bhead;
228         
229         for (bhead = blo_firstbhead(fd); bhead; bhead = blo_nextbhead(fd, bhead)) {
230                 if (bhead->code == ENDB) {
231                         break;
232                 }
233                 else if (BKE_idcode_is_valid(bhead->code)) {
234                         if (BKE_idcode_is_linkable(bhead->code)) {
235                                 const char *str = BKE_idcode_to_name(bhead->code);
236                                 
237                                 if (!BLI_ghash_haskey(gathered, (void *)str)) {
238                                         BLI_linklist_prepend(&names, strdup(str));
239                                         BLI_ghash_insert(gathered, (void *)str, NULL);
240                                 }
241                         }
242                 }
243         }
244         
245         BLI_ghash_free(gathered, NULL, NULL);
246         
247         return names;
248 }               
249
250 void BLO_blendhandle_close(BlendHandle *bh)
251 {
252         FileData *fd = (FileData *) bh;
253         
254         blo_freefiledata(fd);
255 }
256
257 /**********/
258
259 BlendFileData *BLO_read_from_file(const char *filepath, ReportList *reports)
260 {
261         BlendFileData *bfd = NULL;
262         FileData *fd;
263                 
264         fd = blo_openblenderfile(filepath, reports);
265         if (fd) {
266                 fd->reports = reports;
267                 bfd = blo_read_file_internal(fd, filepath);
268                 blo_freefiledata(fd);                   
269         }
270
271         return bfd;     
272 }
273
274 BlendFileData *BLO_read_from_memory(void *mem, int memsize, ReportList *reports)
275 {
276         BlendFileData *bfd = NULL;
277         FileData *fd;
278                 
279         fd = blo_openblendermemory(mem, memsize,  reports);
280         if (fd) {
281                 fd->reports = reports;
282                 bfd = blo_read_file_internal(fd, "");
283                 blo_freefiledata(fd);                   
284         }
285
286         return bfd;     
287 }
288
289 BlendFileData *BLO_read_from_memfile(Main *oldmain, const char *filename, MemFile *memfile, ReportList *reports)
290 {
291         BlendFileData *bfd = NULL;
292         FileData *fd;
293         ListBase mainlist;
294         
295         fd = blo_openblendermemfile(memfile, reports);
296         if (fd) {
297                 fd->reports = reports;
298                 BLI_strncpy(fd->relabase, filename, sizeof(fd->relabase));
299                 
300                 /* clear ob->proxy_from pointers in old main */
301                 blo_clear_proxy_pointers_from_lib(oldmain);
302
303                 /* separate libraries from old main */
304                 blo_split_main(&mainlist, oldmain);
305                 /* add the library pointers in oldmap lookup */
306                 blo_add_library_pointer_map(&mainlist, fd);
307                 
308                 /* makes lookup of existing images in old main */
309                 blo_make_image_pointer_map(fd, oldmain);
310                 
311                 /* makes lookup of existing video clips in old main */
312                 blo_make_movieclip_pointer_map(fd, oldmain);
313                 
314                 bfd = blo_read_file_internal(fd, filename);
315                 
316                 /* ensures relinked images are not freed */
317                 blo_end_image_pointer_map(fd, oldmain);
318                 
319                 /* ensures relinked movie clips are not freed */
320                 blo_end_movieclip_pointer_map(fd, oldmain);
321                 
322                 /* move libraries from old main to new main */
323                 if (bfd && mainlist.first != mainlist.last) {
324                         
325                         /* Library structs themselves */
326                         bfd->main->library = oldmain->library;
327                         oldmain->library.first = oldmain->library.last = NULL;
328                         
329                         /* add the Library mainlist to the new main */
330                         BLI_remlink(&mainlist, oldmain);
331                         BLI_addhead(&mainlist, bfd->main);
332                 }
333                 blo_join_main(&mainlist);
334                 
335                 blo_freefiledata(fd);                   
336         }
337
338         return bfd;     
339 }
340
341 void BLO_blendfiledata_free(BlendFileData *bfd)
342 {
343         if (bfd->main) {
344                 free_main(bfd->main);
345         }
346         
347         if (bfd->user) {
348                 MEM_freeN(bfd->user);
349         }
350
351         MEM_freeN(bfd);
352 }
353