Fix T34446: Make Local on linked mesh object: object gets removed if redo function...
[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_utildefines.h"
44 #include "BLI_path_util.h"
45 #include "BLI_ghash.h"
46 #include "BLI_linklist.h"
47 #include "BLI_listbase.h"
48 #include "BLI_string.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 BKE_main_free
56 #include "BKE_idcode.h"
57
58 #include "BLO_readfile.h"
59 #include "BLO_undofile.h"
60 #include "BLO_blend_defs.h"
61
62 #include "readfile.h"
63
64 #include "BLI_sys_types.h" // needed for intptr_t
65
66 #ifdef WIN32
67 #  include "BLI_winstuff.h"
68 #endif
69
70 /* local prototypes --------------------- */
71 void BLO_blendhandle_print_sizes(BlendHandle *, void *); 
72
73 /* Access routines used by filesel. */
74          
75 BlendHandle *BLO_blendhandle_from_file(const char *filepath, ReportList *reports)
76 {
77         BlendHandle *bh;
78
79         bh = (BlendHandle *)blo_openblenderfile(filepath, reports);
80
81         return bh;
82 }
83
84 BlendHandle *BLO_blendhandle_from_memory(const void *mem, int memsize)
85 {
86         BlendHandle *bh;
87
88         bh = (BlendHandle *)blo_openblendermemory(mem, memsize, NULL);
89
90         return bh;
91 }
92
93 void BLO_blendhandle_print_sizes(BlendHandle *bh, void *fp) 
94 {
95         FileData *fd = (FileData *) bh;
96         BHead *bhead;
97
98         fprintf(fp, "[\n");
99         for (bhead = blo_firstbhead(fd); bhead; bhead = blo_nextbhead(fd, bhead)) {
100                 if (bhead->code == ENDB)
101                         break;
102                 else {
103                         const short *sp = fd->filesdna->structs[bhead->SDNAnr];
104                         const char *name = fd->filesdna->types[sp[0]];
105                         char buf[4];
106                         
107                         buf[0] = (bhead->code >> 24) & 0xFF;
108                         buf[1] = (bhead->code >> 16) & 0xFF;
109                         buf[2] = (bhead->code >> 8) & 0xFF;
110                         buf[3] = (bhead->code >> 0) & 0xFF;
111
112                         buf[0] = buf[0] ? buf[0] : ' ';
113                         buf[1] = buf[1] ? buf[1] : ' ';
114                         buf[2] = buf[2] ? buf[2] : ' ';
115                         buf[3] = buf[3] ? buf[3] : ' ';
116                         
117                         fprintf(fp, "['%.4s', '%s', %d, %ld ],\n", buf, name, bhead->nr, (long int)(bhead->len + sizeof(BHead)));
118                 }
119         }
120         fprintf(fp, "]\n");
121 }
122
123 LinkNode *BLO_blendhandle_get_datablock_names(BlendHandle *bh, int ofblocktype, int *tot_names)
124 {
125         FileData *fd = (FileData *) bh;
126         LinkNode *names = NULL;
127         BHead *bhead;
128         int tot = 0;
129
130         for (bhead = blo_firstbhead(fd); bhead; bhead = blo_nextbhead(fd, bhead)) {
131                 if (bhead->code == ofblocktype) {
132                         const char *idname = bhead_id_name(fd, bhead);
133
134                         BLI_linklist_prepend(&names, strdup(idname + 2));
135                         tot++;
136                 }
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         PreviewImage *prv = NULL;
152         PreviewImage *new_prv = NULL;
153         int tot = 0;
154
155         for (bhead = blo_firstbhead(fd); bhead; bhead = blo_nextbhead(fd, bhead)) {
156                 if (bhead->code == ofblocktype) {
157                         const char *idname = bhead_id_name(fd, bhead);
158                         switch (GS(idname)) {
159                                 case ID_MA: /* fall through */
160                                 case ID_TE: /* fall through */
161                                 case ID_IM: /* fall through */
162                                 case ID_WO: /* fall through */
163                                 case ID_LA: /* fall through */
164                                 case ID_OB: /* fall through */
165                                 case ID_GR: /* fall through */
166                                 case ID_SCE: /* fall through */
167                                         new_prv = MEM_callocN(sizeof(PreviewImage), "newpreview");
168                                         BLI_linklist_prepend(&previews, new_prv);
169                                         tot++;
170                                         looking = 1;
171                                         break;
172                                 default:
173                                         break;
174                         }
175                 }
176                 else if (bhead->code == DATA) {
177                         if (looking) {
178                                 if (bhead->SDNAnr == DNA_struct_find_nr(fd->filesdna, "PreviewImage") ) {
179                                         prv = BLO_library_read_struct(fd, bhead, "PreviewImage");
180                                         if (prv) {
181                                                 memcpy(new_prv, prv, sizeof(PreviewImage));
182                                                 if (prv->rect[0] && prv->w[0] && prv->h[0]) {
183                                                         unsigned int *rect = NULL;
184                                                         size_t len = new_prv->w[0] * new_prv->h[0] * sizeof(unsigned int);
185                                                         new_prv->rect[0] = MEM_callocN(len, __func__);
186                                                         bhead = blo_nextbhead(fd, bhead);
187                                                         rect = (unsigned int *)(bhead + 1);
188                                                         BLI_assert(len == bhead->len);
189                                                         memcpy(new_prv->rect[0], rect, len);
190                                                 }
191                                                 else {
192                                                         /* This should not be needed, but can happen in 'broken' .blend files,
193                                                          * better handle this gracefully than crashing. */
194                                                         BLI_assert(prv->rect[0] == NULL && prv->w[0] == 0 && prv->h[0] == 0);
195                                                         new_prv->rect[0] = NULL;
196                                                         new_prv->w[0] = new_prv->h[0] = 0;
197                                                 }
198                                                 
199                                                 if (prv->rect[1] && prv->w[1] && prv->h[1]) {
200                                                         unsigned int *rect = NULL;
201                                                         size_t len = new_prv->w[1] * new_prv->h[1] * sizeof(unsigned int);
202                                                         new_prv->rect[1] = MEM_callocN(len, __func__);
203                                                         bhead = blo_nextbhead(fd, bhead);
204                                                         rect = (unsigned int *)(bhead + 1);
205                                                         BLI_assert(len == bhead->len);
206                                                         memcpy(new_prv->rect[1], rect, len);
207                                                 }
208                                                 else {
209                                                         /* This should not be needed, but can happen in 'broken' .blend files,
210                                                          * better handle this gracefully than crashing. */
211                                                         BLI_assert(prv->rect[1] == NULL && prv->w[1] == 0 && prv->h[1] == 0);
212                                                         new_prv->rect[1] = NULL;
213                                                         new_prv->w[1] = new_prv->h[1] = 0;
214                                                 }
215                                                 MEM_freeN(prv);
216                                         }
217                                 }
218                         }
219                 }
220                 else if (bhead->code == ENDB) {
221                         break;
222                 }
223                 else {
224                         looking = 0;
225                         new_prv = NULL;
226                         prv = NULL;
227                 }
228                 
229         }
230
231         *tot_prev = tot;
232         return previews;
233 }
234
235 LinkNode *BLO_blendhandle_get_linkable_groups(BlendHandle *bh) 
236 {
237         FileData *fd = (FileData *) bh;
238         GSet *gathered = BLI_gset_ptr_new("linkable_groups gh");
239         LinkNode *names = NULL;
240         BHead *bhead;
241         
242         for (bhead = blo_firstbhead(fd); bhead; bhead = blo_nextbhead(fd, bhead)) {
243                 if (bhead->code == ENDB) {
244                         break;
245                 }
246                 else if (BKE_idcode_is_valid(bhead->code)) {
247                         if (BKE_idcode_is_linkable(bhead->code)) {
248                                 const char *str = BKE_idcode_to_name(bhead->code);
249                                 
250                                 if (!BLI_gset_haskey(gathered, (void *)str)) {
251                                         BLI_linklist_prepend(&names, strdup(str));
252                                         BLI_gset_insert(gathered, (void *)str);
253                                 }
254                         }
255                 }
256         }
257         
258         BLI_gset_free(gathered, NULL);
259         
260         return names;
261 }               
262
263 void BLO_blendhandle_close(BlendHandle *bh)
264 {
265         FileData *fd = (FileData *) bh;
266         
267         blo_freefiledata(fd);
268 }
269
270 /**********/
271
272 BlendFileData *BLO_read_from_file(const char *filepath, ReportList *reports)
273 {
274         BlendFileData *bfd = NULL;
275         FileData *fd;
276                 
277         fd = blo_openblenderfile(filepath, reports);
278         if (fd) {
279                 fd->reports = reports;
280                 bfd = blo_read_file_internal(fd, filepath);
281                 blo_freefiledata(fd);
282         }
283
284         return bfd;
285 }
286
287 BlendFileData *BLO_read_from_memory(const void *mem, int memsize, ReportList *reports)
288 {
289         BlendFileData *bfd = NULL;
290         FileData *fd;
291                 
292         fd = blo_openblendermemory(mem, memsize,  reports);
293         if (fd) {
294                 fd->reports = reports;
295                 bfd = blo_read_file_internal(fd, "");
296                 blo_freefiledata(fd);
297         }
298
299         return bfd;
300 }
301
302 BlendFileData *BLO_read_from_memfile(Main *oldmain, const char *filename, MemFile *memfile, ReportList *reports)
303 {
304         BlendFileData *bfd = NULL;
305         FileData *fd;
306         ListBase old_mainlist;
307         
308         fd = blo_openblendermemfile(memfile, reports);
309         if (fd) {
310                 fd->reports = reports;
311                 BLI_strncpy(fd->relabase, filename, sizeof(fd->relabase));
312                 
313                 /* clear ob->proxy_from pointers in old main */
314                 blo_clear_proxy_pointers_from_lib(oldmain);
315
316                 /* separate libraries from old main */
317                 blo_split_main(&old_mainlist, oldmain);
318                 /* add the library pointers in oldmap lookup */
319                 blo_add_library_pointer_map(&old_mainlist, fd);
320                 
321                 /* makes lookup of existing images in old main */
322                 blo_make_image_pointer_map(fd, oldmain);
323                 
324                 /* makes lookup of existing video clips in old main */
325                 blo_make_movieclip_pointer_map(fd, oldmain);
326
327                 /* make lookups of existing sound data in old main */
328                 blo_make_sound_pointer_map(fd, oldmain);
329                 
330                 /* removed packed data from this trick - it's internal data that needs saves */
331                 
332                 bfd = blo_read_file_internal(fd, filename);
333                 
334                 /* ensures relinked images are not freed */
335                 blo_end_image_pointer_map(fd, oldmain);
336                 
337                 /* ensures relinked movie clips are not freed */
338                 blo_end_movieclip_pointer_map(fd, oldmain);
339                 
340                 /* ensures relinked sounds are not freed */
341                 blo_end_sound_pointer_map(fd, oldmain);
342
343                 /* Still in-use libraries have already been moved from oldmain to new mainlist,
344                  * but oldmain itself shall *never* be 'transferred' to new mainlist! */
345                 BLI_assert(old_mainlist.first == oldmain);
346
347                 if (bfd && old_mainlist.first != old_mainlist.last) {
348                         /* Even though directly used libs have been already moved to new main, indirect ones have not.
349                          * This is a bit annoying, but we have no choice but to keep them all for now - means some now unused
350                          * data may remain in memory, but think we'll have to live with it. */
351                         Main *libmain;
352                         Main *newmain = bfd->main;
353                         ListBase new_mainlist = {newmain, newmain};
354
355                         for (libmain = oldmain->next; libmain; libmain = libmain->next) {
356                                 /* Note that LIB_INDIRECT does not work with libraries themselves, so we use non-NULL parent
357                                  * to detect indirect-linked ones... */
358                                 if (libmain->curlib && (libmain->curlib->parent != NULL)) {
359                                         BLI_remlink(&old_mainlist, libmain);
360                                         BLI_addtail(&new_mainlist, libmain);
361                                 }
362 #if 0
363                                 else {
364                                         printf("Dropped Main for lib: %s\n", libmain->curlib->id.name);
365                                 }
366 #endif
367                         }
368                         /* In any case, we need to move all lib datablocks themselves - those are 'first level data',
369                          * getting rid of them would imply updating spaces & co to prevent invalid pointers access. */
370                         BLI_movelisttolist(&newmain->library, &oldmain->library);
371
372                         blo_join_main(&new_mainlist);
373                 }
374
375                 /* printf("Remaining mains/libs in oldmain: %d\n", BLI_listbase_count(&fd->old_mainlist) - 1); */
376
377                 /* That way, libs (aka mains) we did not reuse in new undone/redone state
378                  * will be cleared together with oldmain... */
379                 blo_join_main(&old_mainlist);
380
381                 blo_freefiledata(fd);
382         }
383
384         return bfd;
385 }
386
387 void BLO_blendfiledata_free(BlendFileData *bfd)
388 {
389         if (bfd->main) {
390                 BKE_main_free(bfd->main);
391         }
392         
393         if (bfd->user) {
394                 MEM_freeN(bfd->user);
395         }
396
397         MEM_freeN(bfd);
398 }
399