Initial revision
[blender.git] / source / blender / blenloader / intern / readblenentry.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  * .blend file reading entry point
32  */
33
34 #ifdef WIN32
35 #include "BLI_winstuff.h"
36 #endif
37
38 #include <stdlib.h>
39 #include <string.h>
40 #include <stdio.h>
41 #include <math.h>
42
43 #include "MEM_guardedalloc.h"
44
45 #include "BLI_blenlib.h"
46 #include "BLI_ghash.h"
47 #include "BLI_linklist.h"
48
49 #include "DNA_sdna_types.h"
50 #include "DNA_space_types.h"
51 #include "DNA_userdef_types.h"
52 #include "DNA_ID.h"
53
54 #include "BKE_utildefines.h" // for ENDB
55
56 #include "BKE_main.h"
57 #include "BKE_library.h" // for free_main
58
59 #include "BLO_readfile.h"
60 #include "readfile.h"
61
62 #include "BLO_readblenfile.h"
63
64         /**
65          * IDType stuff, I plan to move this
66          * out into its own file + prefix, and
67          * make sure all IDType handling goes through
68          * these routines.
69          */
70
71 typedef struct {
72         unsigned short code;
73         char *name;
74         
75         int flags;
76 #define IDTYPE_FLAGS_ISLINKABLE (1<<0)
77 } IDType;
78
79 static IDType idtypes[]= {
80         { ID_AC,                "Action",       IDTYPE_FLAGS_ISLINKABLE}, 
81         { ID_AR,                "Armature", IDTYPE_FLAGS_ISLINKABLE}, 
82         { ID_CA,                "Camera",       IDTYPE_FLAGS_ISLINKABLE}, 
83         { ID_CU,                "Curve",        IDTYPE_FLAGS_ISLINKABLE}, 
84         { ID_GR,                "Group",        0}, 
85         { ID_ID,                "ID",           0}, 
86         { ID_IK,                "Ika",          IDTYPE_FLAGS_ISLINKABLE}, 
87         { ID_IM,                "Image",        IDTYPE_FLAGS_ISLINKABLE}, 
88         { ID_IP,                "Ipo",          IDTYPE_FLAGS_ISLINKABLE}, 
89         { ID_KE,                "Key",          0}, 
90         { ID_LA,                "Lamp",         IDTYPE_FLAGS_ISLINKABLE}, 
91         { ID_LF,                "Life",         0}, 
92         { ID_LI,                "Library",      0}, 
93         { ID_LT,                "Lattice",      IDTYPE_FLAGS_ISLINKABLE}, 
94         { ID_MA,                "Material", IDTYPE_FLAGS_ISLINKABLE}, 
95         { ID_MB,                "Metaball", IDTYPE_FLAGS_ISLINKABLE}, 
96         { ID_ME,                "Mesh",         IDTYPE_FLAGS_ISLINKABLE}, 
97         { ID_OB,                "Object",       IDTYPE_FLAGS_ISLINKABLE}, 
98         { ID_SAMPLE,    "Sample",       0}, 
99         { ID_SCE,               "Scene",        IDTYPE_FLAGS_ISLINKABLE}, 
100         { ID_SCR,               "Screen",       0}, 
101         { ID_SEQ,               "Sequence",     0}, 
102         { ID_SE,                "Sector",       0}, 
103         { ID_SO,                "Sound",        IDTYPE_FLAGS_ISLINKABLE}, 
104         { ID_TE,                "Texture",      IDTYPE_FLAGS_ISLINKABLE}, 
105         { ID_TXT,               "Text",         IDTYPE_FLAGS_ISLINKABLE}, 
106         { ID_VF,                "VFont",        IDTYPE_FLAGS_ISLINKABLE}, 
107         { ID_WO,                "World",        IDTYPE_FLAGS_ISLINKABLE}, 
108         { ID_WV,                "Wave",         0}, 
109 };
110 static int nidtypes= sizeof(idtypes)/sizeof(idtypes[0]);
111
112 static IDType *idtype_from_name(char *str) {
113         int i= nidtypes;
114         
115         while (i--)
116                 if (BLI_streq(str, idtypes[i].name))
117                         return &idtypes[i];
118         
119         return NULL;
120 }
121 static IDType *idtype_from_code(int code) {
122         int i= nidtypes;
123         
124         while (i--)
125                 if (code==idtypes[i].code)
126                         return &idtypes[i];
127         
128         return NULL;
129 }
130
131 static int bheadcode_is_idcode(int code) {
132         return idtype_from_code(code)?1:0;
133 }
134
135 static int idcode_is_linkable(int code) {
136         IDType *idt= idtype_from_code(code);
137         return idt?(idt->flags&IDTYPE_FLAGS_ISLINKABLE):0;
138 }
139
140 char *BLO_idcode_to_name(int code) {
141         IDType *idt= idtype_from_code(code);
142         
143         return idt?idt->name:NULL;
144 }
145
146 int BLO_idcode_from_name(char *name) {
147         IDType *idt= idtype_from_name(name);
148         
149         return idt?idt->code:0;
150 }
151         
152         /* Access routines used by filesel. */
153          
154 BlendHandle *BLO_blendhandle_from_file(char *file) {
155         return (BlendHandle*) blo_openblenderfile(file);
156 }
157
158 void BLO_blendhandle_print_sizes(BlendHandle *bh, void *fp) {
159         FileData *fd= (FileData*) bh;
160         BHead *bhead;
161
162         fprintf(fp, "[\n");
163         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
164                 if (bhead->code==ENDB)
165                         break;
166                 else {
167                         short *sp= fd->filesdna->structs[bhead->SDNAnr];
168                         char *name= fd->filesdna->types[ sp[0] ];
169                         char buf[4];
170                         
171                         buf[0]= (bhead->code>>24)&0xFF;
172                         buf[1]= (bhead->code>>16)&0xFF;
173                         buf[2]= (bhead->code>>8)&0xFF;
174                         buf[3]= (bhead->code>>0)&0xFF;
175                         
176                         buf[0]= buf[0]?buf[0]:' ';
177                         buf[1]= buf[1]?buf[1]:' ';
178                         buf[2]= buf[2]?buf[2]:' ';
179                         buf[3]= buf[3]?buf[3]:' ';
180                         
181                         fprintf(fp, "['%.4s', '%s', %d, %d], \n", buf, name, bhead->nr, bhead->len+sizeof(BHead));
182                 }
183         }
184         fprintf(fp, "]\n");
185 }
186
187 LinkNode *BLO_blendhandle_get_datablock_names(BlendHandle *bh, int ofblocktype) {
188         FileData *fd= (FileData*) bh;
189         LinkNode *names= NULL;
190         BHead *bhead;
191
192         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
193                 if (bhead->code==ofblocktype) {
194                         ID *id= (ID*) (bhead+1);
195                         
196                         BLI_linklist_prepend(&names, strdup(id->name+2));
197                 } else if (bhead->code==ENDB)
198                         break;
199         }
200         
201         return names;
202 }
203
204 LinkNode *BLO_blendhandle_get_linkable_groups(BlendHandle *bh) {
205         FileData *fd= (FileData*) bh;
206         GHash *gathered= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
207         LinkNode *names= NULL;
208         BHead *bhead;
209         
210         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
211                 if (bhead->code==ENDB) {
212                         break;
213                 } else if (bheadcode_is_idcode(bhead->code)) {
214                         if (idcode_is_linkable(bhead->code)) {
215                                 char *str= BLO_idcode_to_name(bhead->code);
216                                 
217                                 if (!BLI_ghash_haskey(gathered, str)) {
218                                         BLI_linklist_prepend(&names, strdup(str));
219                                         BLI_ghash_insert(gathered, str, NULL);
220                                 }
221                         }
222                 }
223         }
224         
225         BLI_ghash_free(gathered, NULL, NULL);
226         
227         return names;
228 }               
229
230 void BLO_blendhandle_close(BlendHandle *bh) {
231         FileData *fd= (FileData*) bh;
232         
233         blo_freefiledata(fd);
234 }
235
236         /**********/
237
238 BlendFileData *BLO_read_from_file(char *file, BlendReadError *error_r) {
239         BlendFileData *bfd = NULL;
240         FileData *fd;
241                 
242         fd = blo_openblenderfile(file);
243         if (fd) {
244                 bfd= blo_read_file_internal(fd, error_r);
245                 if (bfd) {
246                         bfd->type= BLENFILETYPE_BLEND;
247                         strcpy(bfd->main->name, file);
248                 }
249                 blo_freefiledata(fd);                   
250         } else if (blo_is_a_runtime(file)) {
251                 bfd= blo_read_runtime(file, error_r);
252                 if (bfd) {
253                         bfd->type= BLENFILETYPE_RUNTIME;
254                         strcpy(bfd->main->name, file);
255                 }
256         } else {
257                 bfd= BLO_readblenfilename(file, error_r);
258                 if (bfd) {
259                         bfd->type= BLENFILETYPE_PUB;
260                         strcpy(bfd->main->name, file);
261                 }
262         }
263
264         return bfd;     
265 }
266
267 BlendFileData *BLO_read_from_memory(void *mem, int memsize, BlendReadError *error_r) {
268         BlendFileData *bfd = NULL;
269         FileData *fd;
270                 
271         fd = blo_openblendermemory(mem, memsize);
272         if (fd) {
273                 bfd= blo_read_file_internal(fd, error_r);
274                 if (bfd) {
275                         bfd->type= BLENFILETYPE_BLEND;
276                         strcpy(bfd->main->name, "");
277                 }
278                 blo_freefiledata(fd);                   
279         } else {
280                 bfd= BLO_readblenfilememory(mem, memsize, error_r);
281                 if (bfd) {
282                         bfd->type= BLENFILETYPE_PUB;
283                         strcpy(bfd->main->name, "");
284                 }
285         }
286
287         return bfd;     
288 }
289
290 void BLO_blendfiledata_free(BlendFileData *bfd) {
291         if (bfd->main) {
292                 free_main(bfd->main);
293         }
294         
295         if (bfd->user) {
296                 MEM_freeN(bfd->user);
297         }
298
299         MEM_freeN(bfd);
300 }
301
302 char *BLO_bre_as_string(BlendReadError error) {
303         switch (error) {
304         case BRE_NONE:
305                 return "No error";
306         
307         case BRE_UNABLE_TO_OPEN:
308                 return "Unable to open";
309         case BRE_UNABLE_TO_READ:
310                 return "Unable to read";
311                 
312         case BRE_OUT_OF_MEMORY:
313                 return "Out of memory";
314         case BRE_INTERNAL_ERROR:
315                 return "<internal error>";
316
317         case BRE_NOT_A_BLEND:
318                 return "File is not a Blender file";
319         case BRE_NOT_A_PUBFILE:
320                 return "File is not a compressed, locked or signed Blender file";
321         case BRE_INCOMPLETE:
322                 return "File incomplete";
323         case BRE_CORRUPT:
324                 return "File corrupt";
325
326         case BRE_TOO_NEW:
327                 return "File needs newer Blender version, please upgrade";
328         case BRE_NOT_ALLOWED:
329                 return "File is locked";
330                                                 
331         case BRE_NO_SCREEN:
332                 return "File has no screen";
333         case BRE_NO_SCENE:
334                 return "File has no scene";
335                 
336         default:
337         case BRE_INVALID:
338                 return "<invalid read error>";
339         }
340 }