4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
28 * .blend file reading entry point
32 #include "BLI_storage.h" /* _LARGEFILE_SOURCE */
39 #include "MEM_guardedalloc.h"
41 #include "BLI_utildefines.h"
42 #include "BLI_ghash.h"
43 #include "BLI_linklist.h"
45 #include "DNA_genfile.h"
46 #include "DNA_sdna_types.h"
50 #include "BKE_library.h" // for free_main
51 #include "BKE_idcode.h"
52 #include "BKE_report.h"
53 #include "BKE_utildefines.h"
55 #include "BLO_readfile.h"
56 #include "BLO_undofile.h"
60 #include "BLO_readblenfile.h"
62 #include "BLO_sys_types.h" // needed for intptr_t
65 #include "BLI_winstuff.h"
68 /* local prototypes --------------------- */
69 void BLO_blendhandle_print_sizes(BlendHandle *, void *);
71 /* Access routines used by filesel. */
73 BlendHandle *BLO_blendhandle_from_file(char *file)
77 bh= (BlendHandle*)blo_openblenderfile(file, NULL);
82 BlendHandle *BLO_blendhandle_from_memory(void *mem, int memsize)
86 bh= (BlendHandle*)blo_openblendermemory(mem, memsize, NULL);
91 void BLO_blendhandle_print_sizes(BlendHandle *bh, void *fp)
93 FileData *fd= (FileData*) bh;
97 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
98 if (bhead->code==ENDB)
101 short *sp= fd->filesdna->structs[bhead->SDNAnr];
102 char *name= fd->filesdna->types[ sp[0] ];
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;
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]:' ';
115 fprintf(fp, "['%.4s', '%s', %d, %ld ], \n", buf, name, bhead->nr, (long int)bhead->len+sizeof(BHead));
121 LinkNode *BLO_blendhandle_get_datablock_names(BlendHandle *bh, int ofblocktype)
123 FileData *fd= (FileData*) bh;
124 LinkNode *names= NULL;
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);
131 BLI_linklist_prepend(&names, strdup(idname+2));
132 } else if (bhead->code==ENDB)
139 LinkNode *BLO_blendhandle_get_previews(BlendHandle *bh, int ofblocktype)
141 FileData *fd= (FileData*) bh;
142 LinkNode *previews= NULL;
146 PreviewImage* prv = NULL;
147 PreviewImage* new_prv = NULL;
149 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
150 if (bhead->code==ofblocktype) {
151 ID *id= (ID*) (bhead+1);
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);
166 } else if (bhead->code==DATA) {
168 if (bhead->SDNAnr == DNA_struct_find_nr(fd->filesdna, "PreviewImage") ) {
169 prv = (PreviewImage*) (bhead+1);
171 memcpy(new_prv, prv, sizeof(PreviewImage));
173 unsigned int *rect = NULL;
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);
181 new_prv->rect[0] = NULL;
185 unsigned int *rect = NULL;
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);
193 new_prv->rect[1] = NULL;
197 } else if (bhead->code==ENDB) {
199 } else if (bhead->code==DATA) {
200 /* DATA blocks between IDBlock and Preview */
212 LinkNode *BLO_blendhandle_get_linkable_groups(BlendHandle *bh)
214 FileData *fd= (FileData*) bh;
215 GHash *gathered= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "linkable_groups gh");
216 LinkNode *names= NULL;
219 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
220 if (bhead->code==ENDB) {
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);
226 if (!BLI_ghash_haskey(gathered, (void *)str)) {
227 BLI_linklist_prepend(&names, strdup(str));
228 BLI_ghash_insert(gathered, (void *)str, NULL);
234 BLI_ghash_free(gathered, NULL, NULL);
239 void BLO_blendhandle_close(BlendHandle *bh) {
240 FileData *fd= (FileData*) bh;
242 blo_freefiledata(fd);
247 BlendFileData *BLO_read_from_file(const char *file, ReportList *reports)
249 BlendFileData *bfd = NULL;
252 fd = blo_openblenderfile(file, reports);
254 fd->reports= reports;
255 bfd= blo_read_file_internal(fd, file);
256 blo_freefiledata(fd);
262 BlendFileData *BLO_read_from_memory(void *mem, int memsize, ReportList *reports)
264 BlendFileData *bfd = NULL;
267 fd = blo_openblendermemory(mem, memsize, reports);
269 fd->reports= reports;
270 bfd= blo_read_file_internal(fd, "");
271 blo_freefiledata(fd);
277 BlendFileData *BLO_read_from_memfile(Main *oldmain, const char *filename, MemFile *memfile, ReportList *reports)
279 BlendFileData *bfd = NULL;
283 fd = blo_openblendermemfile(memfile, reports);
285 fd->reports= reports;
286 strcpy(fd->relabase, filename);
288 /* clear ob->proxy_from pointers in old main */
289 blo_clear_proxy_pointers_from_lib(oldmain);
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);
296 /* makes lookup of existing images in old main */
297 blo_make_image_pointer_map(fd, oldmain);
299 bfd= blo_read_file_internal(fd, filename);
301 /* ensures relinked images are not freed */
302 blo_end_image_pointer_map(fd, oldmain);
304 /* move libraries from old main to new main */
305 if(bfd && mainlist.first!=mainlist.last) {
307 /* Library structs themselves */
308 bfd->main->library= oldmain->library;
309 oldmain->library.first= oldmain->library.last= NULL;
311 /* add the Library mainlist to the new main */
312 BLI_remlink(&mainlist, oldmain);
313 BLI_addhead(&mainlist, bfd->main);
315 blo_join_main(&mainlist);
317 blo_freefiledata(fd);
323 void BLO_blendfiledata_free(BlendFileData *bfd)
326 free_main(bfd->main);
330 MEM_freeN(bfd->user);