7 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version. The Blender
13 * Foundation also sells licenses for use in proprietary software under
14 * the Blender License. See http://www.blender.org/BL/ for information
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
27 * All rights reserved.
29 * The Original Code is: all of this file.
31 * Contributor(s): none yet.
33 * ***** END GPL/BL DUAL LICENSE BLOCK *****
37 FILEFORMAT: IFF-style structure (but not IFF compatible!)
40 BLENDER_V100 12 bytes (versie 1.00)
41 V = big endian, v = little endian
42 _ = 4 byte pointer, - = 8 byte pointer
44 datablocks: also see struct BHead
46 <bh.len> int, len data after BHead
47 <bh.old> void, old pointer
49 <bh.nr> int, in case of array: amount of structs
54 Almost all data in Blender are structures. Each struct saved
55 gets a BHead header. With BHead the struct can be linked again
56 and compared with StructDNA .
60 Preferred writing order: (not really a must, but why would you do it random?)
61 Any case: direct data is ALWAYS after the lib block
66 - write associated direct data
71 - write the ID of LibBlock
72 - write FileGlobal (some global vars)
74 - write USER if filename is ~/.B.blend
78 Important to know is that 'streaming' has been added to files, for Blender Publisher
90 #include "BLI_winstuff.h"
92 #include <process.h> // for getpid
101 #include "nla.h" // __NLA is defined
103 #include "DNA_packedFile_types.h"
104 #include "DNA_sdna_types.h"
105 #include "DNA_property_types.h"
106 #include "DNA_sensor_types.h"
107 #include "DNA_controller_types.h"
108 #include "DNA_actuator_types.h"
109 #include "DNA_effect_types.h"
110 #include "DNA_object_types.h"
111 #include "DNA_userdef_types.h"
112 #include "DNA_vfont_types.h"
113 #include "DNA_ipo_types.h"
114 #include "DNA_curve_types.h"
115 #include "DNA_camera_types.h"
116 #include "DNA_meta_types.h"
117 #include "DNA_mesh_types.h"
118 #include "DNA_meshdata_types.h"
119 #include "DNA_material_types.h"
120 #include "DNA_lattice_types.h"
121 #include "DNA_armature_types.h"
122 #include "DNA_sequence_types.h"
123 #include "DNA_ika_types.h"
124 #include "DNA_group_types.h"
125 #include "DNA_oops_types.h"
126 #include "DNA_space_types.h"
127 #include "DNA_screen_types.h"
128 #include "DNA_view3d_types.h"
129 #include "DNA_lamp_types.h"
130 #include "DNA_fileglobal_types.h"
131 #include "DNA_sound_types.h"
132 #include "DNA_texture_types.h"
133 #include "DNA_text_types.h"
134 #include "DNA_image_types.h"
135 #include "DNA_key_types.h"
136 #include "DNA_scene_types.h"
137 #include "DNA_constraint_types.h"
138 #include "DNA_listBase.h" /* for Listbase, the type of samples, ...*/
139 #include "DNA_action_types.h"
140 #include "DNA_nla_types.h"
142 #include "MEM_guardedalloc.h" // MEM_freeN
143 #include "BLI_blenlib.h"
144 #include "BLI_linklist.h"
146 #include "BKE_action.h"
147 #include "BKE_utildefines.h" // for KNOTSU KNOTSV WHILE_SEQ END_SEQ defines
148 #include "BKE_bad_level_calls.h" // build_seqar (from WHILE_SEQ) free_oops error
149 #include "BKE_constraint.h"
150 #include "BKE_main.h" // G.main
151 #include "BKE_global.h" // for G
152 #include "BKE_screen.h" // for waitcursor
153 #include "BKE_packedFile.h" // for packAll
154 #include "BKE_library.h" // for set_listbasepointers
155 #include "BKE_sound.h" /* ... and for samples */
157 #include "GEN_messaging.h"
159 #include "BLO_writefile.h"
160 #include "BLO_readfile.h"
161 #include "BLO_undofile.h"
163 #include "readfile.h"
167 /* ********* my write, buffered writing with minimum 50k chunks ************ */
174 MemFile *compare, *current;
176 int tot, count, error, memsize;
179 static WriteData *writedata_new(int file)
181 extern char DNAstr[]; /* DNA.c */
184 WriteData *wd= MEM_callocN(sizeof(*wd), "writedata");
186 /* XXX, see note about this in readfile.c, remove
187 * once we have an xp lock - zr
189 wd->sdna= dna_sdna_from_data(DNAstr, DNAlen, 0);
193 wd->buf= MEM_mallocN(100000, "wd->buf");
198 static void writedata_do_write(WriteData *wd, void *mem, int memlen)
200 if (wd->error) return;
202 /* memory based save */
204 add_memfilechunk(NULL, wd->current, mem, memlen);
207 if (write(wd->file, mem, memlen) != memlen)
213 static void writedata_free(WriteData *wd)
215 dna_freestructDNA(wd->sdna);
226 * Low level WRITE(2) wrapper that buffers data
227 * @param adr Pointer to new chunk of data
228 * @param len Length of new chunk of data
229 * @warning Talks to other functions with global parameters
232 #define MYWRITE_FLUSH NULL
234 static void mywrite( WriteData *wd, void *adr, int len)
236 if (wd->error) return;
238 if(adr==MYWRITE_FLUSH) {
240 writedata_do_write(wd, wd->buf, wd->count);
250 writedata_do_write(wd, wd->buf, wd->count);
253 writedata_do_write(wd, adr, len);
256 if(len+wd->count>99999) {
257 writedata_do_write(wd, wd->buf, wd->count);
260 memcpy(&wd->buf[wd->count], adr, len);
266 * BeGiN initializer for mywrite
267 * @param file File descriptor
268 * @param write_flags Write parameters
269 * @warning Talks to other functions with global parameters
271 static WriteData *bgnwrite(int file, MemFile *compare, MemFile *current, int write_flags)
273 WriteData *wd= writedata_new(file);
275 wd->compare= compare;
276 wd->current= current;
277 /* this inits comparing */
278 add_memfilechunk(compare, NULL, NULL, 0);
284 * END the mywrite wrapper
285 * @return 1 if write failed
286 * @return unknown global variable otherwise
287 * @warning Talks to other functions with global parameters
289 static int endwrite(WriteData *wd)
294 writedata_do_write(wd, wd->buf, wd->count);
300 /* blender gods may live forever but this parent pointer died in the statement above
301 if(wd->current) printf("undo size %d\n", wd->current->size);
306 /* ********** WRITE FILE ****************** */
308 static void writestruct(WriteData *wd, int filecode, char *structname, int nr, void *adr)
313 if(adr==0 || nr==0) return;
320 bh.SDNAnr= dna_findstruct_nr(wd->sdna, structname);
322 printf("error: can't find SDNA code %s\n", structname);
325 sp= wd->sdna->structs[bh.SDNAnr];
327 bh.len= nr*wd->sdna->typelens[sp[0]];
329 if(bh.len==0) return;
331 mywrite(wd, &bh, sizeof(BHead));
332 mywrite(wd, adr, bh.len);
335 static void writedata(WriteData *wd, int filecode, int len, void *adr) /* do not use for structs */
352 mywrite(wd, &bh, sizeof(BHead));
353 if(len) mywrite(wd, adr, len);
356 static void write_scriptlink(WriteData *wd, ScriptLink *slink)
358 writedata(wd, DATA, sizeof(void *)*slink->totscript, slink->scripts);
359 writedata(wd, DATA, sizeof(short)*slink->totscript, slink->flag);
362 static void write_renderinfo(WriteData *wd) /* for renderdaemon */
367 sce= G.main->scene.first;
369 if(sce->id.lib==0 && ( sce==G.scene || (sce->r.scemode & R_BG_RENDER)) ) {
370 data[0]= sce->r.sfra;
371 data[1]= sce->r.efra;
373 strncpy((char *)(data+2), sce->id.name+2, 23);
375 writedata(wd, REND, 32, data);
381 static void write_userdef(WriteData *wd)
385 writestruct(wd, USER, "UserDef", 1, &U);
387 btheme= U.themes.first;
389 writestruct(wd, DATA, "bTheme", 1, btheme);
390 btheme= btheme->next;
394 static void write_effects(WriteData *wd, ListBase *lb)
403 writestruct(wd, DATA, "BuildEff", 1, eff);
406 writestruct(wd, DATA, "PartEff", 1, eff);
409 writestruct(wd, DATA, "WaveEff", 1, eff);
412 writedata(wd, DATA, MEM_allocN_len(eff), eff);
419 static void write_properties(WriteData *wd, ListBase *lb)
425 writestruct(wd, DATA, "bProperty", 1, prop);
427 if(prop->poin && prop->poin != &prop->data)
428 writedata(wd, DATA, MEM_allocN_len(prop->poin), prop->poin);
434 static void write_sensors(WriteData *wd, ListBase *lb)
440 writestruct(wd, DATA, "bSensor", 1, sens);
442 writedata(wd, DATA, sizeof(void *)*sens->totlinks, sens->links);
446 writestruct(wd, DATA, "bNearSensor", 1, sens->data);
449 writestruct(wd, DATA, "bMouseSensor", 1, sens->data);
452 writestruct(wd, DATA, "bTouchSensor", 1, sens->data);
455 writestruct(wd, DATA, "bKeyboardSensor", 1, sens->data);
458 writestruct(wd, DATA, "bPropertySensor", 1, sens->data);
461 writestruct(wd, DATA, "bCollisionSensor", 1, sens->data);
464 writestruct(wd, DATA, "bRadarSensor", 1, sens->data);
467 writestruct(wd, DATA, "bRandomSensor", 1, sens->data);
470 writestruct(wd, DATA, "bRaySensor", 1, sens->data);
473 writestruct(wd, DATA, "bMessageSensor", 1, sens->data);
476 ; /* error: don't know how to write this file */
483 static void write_controllers(WriteData *wd, ListBase *lb)
489 writestruct(wd, DATA, "bController", 1, cont);
491 writedata(wd, DATA, sizeof(void *)*cont->totlinks, cont->links);
494 case CONT_EXPRESSION:
495 writestruct(wd, DATA, "bExpressionCont", 1, cont->data);
498 writestruct(wd, DATA, "bPythonCont", 1, cont->data);
501 ; /* error: don't know how to write this file */
508 static void write_actuators(WriteData *wd, ListBase *lb)
514 writestruct(wd, DATA, "bActuator", 1, act);
518 writestruct(wd, DATA, "bActionActuator", 1, act->data);
521 writestruct(wd, DATA, "bSoundActuator", 1, act->data);
524 writestruct(wd, DATA, "bCDActuator", 1, act->data);
527 writestruct(wd, DATA, "bObjectActuator", 1, act->data);
530 writestruct(wd, DATA, "bIpoActuator", 1, act->data);
533 writestruct(wd, DATA, "bPropertyActuator", 1, act->data);
536 writestruct(wd, DATA, "bCameraActuator", 1, act->data);
539 writestruct(wd, DATA, "bConstraintActuator", 1, act->data);
541 case ACT_EDIT_OBJECT:
542 writestruct(wd, DATA, "bEditObjectActuator", 1, act->data);
545 writestruct(wd, DATA, "bSceneActuator", 1, act->data);
548 writestruct(wd, DATA, "bGroupActuator", 1, act->data);
551 writestruct(wd, DATA, "bRandomActuator", 1, act->data);
554 writestruct(wd, DATA, "bMessageActuator", 1, act->data);
557 writestruct(wd, DATA, "bGameActuator", 1, act->data);
560 writestruct(wd, DATA, "bVisibilityActuator", 1, act->data);
563 ; /* error: don't know how to write this file */
570 static void write_nlastrips(WriteData *wd, ListBase *nlabase)
574 for (strip=nlabase->first; strip; strip=strip->next)
575 writestruct(wd, DATA, "bActionStrip", 1, strip);
578 static void write_constraints(WriteData *wd, ListBase *conlist)
582 for (con=conlist->first; con; con=con->next) {
583 /* Write the specific data */
585 case CONSTRAINT_TYPE_NULL:
587 case CONSTRAINT_TYPE_TRACKTO:
588 writestruct(wd, DATA, "bTrackToConstraint", 1, con->data);
590 case CONSTRAINT_TYPE_KINEMATIC:
591 writestruct(wd, DATA, "bKinematicConstraint", 1, con->data);
593 case CONSTRAINT_TYPE_ROTLIKE:
594 writestruct(wd, DATA, "bRotateLikeConstraint", 1, con->data);
596 case CONSTRAINT_TYPE_LOCLIKE:
597 writestruct(wd, DATA, "bLocateLikeConstraint", 1, con->data);
599 case CONSTRAINT_TYPE_ACTION:
600 writestruct(wd, DATA, "bActionConstraint", 1, con->data);
602 case CONSTRAINT_TYPE_LOCKTRACK:
603 writestruct(wd, DATA, "bLockTrackConstraint", 1, con->data);
605 case CONSTRAINT_TYPE_FOLLOWPATH:
606 writestruct(wd, DATA, "bFollowPathConstraint", 1, con->data);
608 case CONSTRAINT_TYPE_STRETCHTO:
609 writestruct(wd, DATA, "bStretchToConstraint", 1, con->data);
614 /* Write the constraint */
615 writestruct(wd, DATA, "bConstraint", 1, con);
619 static void write_pose(WriteData *wd, bPose *pose)
623 /* Write each channel */
629 for (chan=pose->chanbase.first; chan; chan=chan->next) {
630 write_constraints(wd, &chan->constraints);
631 writestruct(wd, DATA, "bPoseChannel", 1, chan);
635 writestruct(wd, DATA, "bPose", 1, pose);
638 static void write_defgroups(WriteData *wd, ListBase *defbase)
640 bDeformGroup *defgroup;
642 for (defgroup=defbase->first; defgroup; defgroup=defgroup->next)
643 writestruct(wd, DATA, "bDeformGroup", 1, defgroup);
646 static void write_constraint_channels(WriteData *wd, ListBase *chanbase)
648 bConstraintChannel *chan;
650 for (chan = chanbase->first; chan; chan=chan->next)
651 writestruct(wd, DATA, "bConstraintChannel", 1, chan);
655 static void write_objects(WriteData *wd, ListBase *idbase)
662 if(ob->id.us>0 || wd->current) {
664 writestruct(wd, ID_OB, "Object", 1, ob);
667 writedata(wd, DATA, sizeof(void *)*ob->totcol, ob->mat);
668 write_effects(wd, &ob->effect);
669 write_properties(wd, &ob->prop);
670 write_sensors(wd, &ob->sensors);
671 write_controllers(wd, &ob->controllers);
672 write_actuators(wd, &ob->actuators);
673 write_scriptlink(wd, &ob->scriptlink);
674 write_pose(wd, ob->pose);
675 write_defgroups(wd, &ob->defbase);
676 write_constraints(wd, &ob->constraints);
677 write_constraint_channels(wd, &ob->constraintChannels);
678 write_nlastrips(wd, &ob->nlastrips);
680 writestruct(wd, DATA, "PartDeflect", 1, ob->pd);
682 for(hook= ob->hooks.first; hook; hook= hook->next) {
683 writestruct(wd, DATA, "ObHook", 1, hook);
684 writedata(wd, DATA, sizeof(int)*hook->totindex, hook->indexar);
690 /* flush helps the compression for undo-save */
691 mywrite(wd, MYWRITE_FLUSH, 0);
695 static void write_vfonts(WriteData *wd, ListBase *idbase)
702 if(vf->id.us>0 || wd->current) {
704 writestruct(wd, ID_VF, "VFont", 1, vf);
708 if (vf->packedfile) {
710 writestruct(wd, DATA, "PackedFile", 1, pf);
711 writedata(wd, DATA, pf->size, pf->data);
719 static void write_ipos(WriteData *wd, ListBase *idbase)
726 if(ipo->id.us>0 || wd->current) {
728 writestruct(wd, ID_IP, "Ipo", 1, ipo);
731 icu= ipo->curve.first;
733 writestruct(wd, DATA, "IpoCurve", 1, icu);
737 icu= ipo->curve.first;
739 if(icu->bezt) writestruct(wd, DATA, "BezTriple", icu->totvert, icu->bezt);
740 if(icu->bp) writestruct(wd, DATA, "BPoint", icu->totvert, icu->bp);
748 /* flush helps the compression for undo-save */
749 mywrite(wd, MYWRITE_FLUSH, 0);
752 static void write_keys(WriteData *wd, ListBase *idbase)
759 if(key->id.us>0 || wd->current) {
761 writestruct(wd, ID_KE, "Key", 1, key);
764 kb= key->block.first;
766 writestruct(wd, DATA, "KeyBlock", 1, kb);
767 if(kb->data) writedata(wd, DATA, kb->totelem*key->elemsize, kb->data);
774 /* flush helps the compression for undo-save */
775 mywrite(wd, MYWRITE_FLUSH, 0);
778 static void write_cameras(WriteData *wd, ListBase *idbase)
784 if(cam->id.us>0 || wd->current) {
786 writestruct(wd, ID_CA, "Camera", 1, cam);
789 write_scriptlink(wd, &cam->scriptlink);
796 static void write_mballs(WriteData *wd, ListBase *idbase)
803 if(mb->id.us>0 || wd->current) {
805 writestruct(wd, ID_MB, "MetaBall", 1, mb);
808 writedata(wd, DATA, sizeof(void *)*mb->totcol, mb->mat);
812 writestruct(wd, DATA, "MetaElem", 1, ml);
820 static void write_curves(WriteData *wd, ListBase *idbase)
827 if(cu->id.us>0 || wd->current) {
829 writestruct(wd, ID_CU, "Curve", 1, cu);
832 writedata(wd, DATA, sizeof(void *)*cu->totcol, cu->mat);
835 writedata(wd, DATA, cu->len+1, cu->str);
838 /* is also the order of reading */
841 writestruct(wd, DATA, "Nurb", 1, nu);
846 if( (nu->type & 7)==CU_BEZIER)
847 writestruct(wd, DATA, "BezTriple", nu->pntsu, nu->bezt);
849 writestruct(wd, DATA, "BPoint", nu->pntsu*nu->pntsv, nu->bp);
850 if(nu->knotsu) writedata(wd, DATA, KNOTSU(nu)*sizeof(float), nu->knotsu);
851 if(nu->knotsv) writedata(wd, DATA, KNOTSV(nu)*sizeof(float), nu->knotsv);
860 /* flush helps the compression for undo-save */
861 mywrite(wd, MYWRITE_FLUSH, 0);
864 static void write_dverts(WriteData *wd, int count, MDeformVert *dvlist)
868 /* Write the dvert list */
869 writestruct(wd, DATA, "MDeformVert", count, dvlist);
871 /* Write deformation data for each dvert */
873 for (i=0; i<count; i++) {
875 writestruct(wd, DATA, "MDeformWeight", dvlist[i].totweight, dvlist[i].dw);
880 static void write_meshs(WriteData *wd, ListBase *idbase)
886 if(mesh->id.us>0 || wd->current) {
888 writestruct(wd, ID_ME, "Mesh", 1, mesh);
891 writedata(wd, DATA, sizeof(void *)*mesh->totcol, mesh->mat);
893 writestruct(wd, DATA, "MVert", mesh->totvert, mesh->mvert);
894 writestruct(wd, DATA, "MEdge", mesh->totedge, mesh->medge);
895 writestruct(wd, DATA, "MFace", mesh->totface, mesh->mface);
896 writestruct(wd, DATA, "TFace", mesh->totface, mesh->tface);
897 writestruct(wd, DATA, "MCol", 4*mesh->totface, mesh->mcol);
898 writestruct(wd, DATA, "MSticky", mesh->totvert, mesh->msticky);
900 write_dverts(wd, mesh->totvert, mesh->dvert);
907 static void write_images(WriteData *wd, ListBase *idbase)
914 if(ima->id.us>0 || wd->current) {
916 writestruct(wd, ID_IM, "Image", 1, ima);
918 if (ima->packedfile) {
919 pf = ima->packedfile;
920 writestruct(wd, DATA, "PackedFile", 1, pf);
921 writedata(wd, DATA, pf->size, pf->data);
926 /* flush helps the compression for undo-save */
927 mywrite(wd, MYWRITE_FLUSH, 0);
930 static void write_textures(WriteData *wd, ListBase *idbase)
936 if(tex->id.us>0 || wd->current) {
938 writestruct(wd, ID_TE, "Tex", 1, tex);
941 if(tex->plugin) writestruct(wd, DATA, "PluginTex", 1, tex->plugin);
942 if(tex->coba) writestruct(wd, DATA, "ColorBand", 1, tex->coba);
943 if(tex->env) writestruct(wd, DATA, "EnvMap", 1, tex->env);
948 /* flush helps the compression for undo-save */
949 mywrite(wd, MYWRITE_FLUSH, 0);
952 static void write_materials(WriteData *wd, ListBase *idbase)
959 if(ma->id.us>0 || wd->current) {
961 writestruct(wd, ID_MA, "Material", 1, ma);
963 for(a=0; a<MAX_MTEX; a++) {
964 if(ma->mtex[a]) writestruct(wd, DATA, "MTex", 1, ma->mtex[a]);
967 if(ma->ramp_col) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_col);
968 if(ma->ramp_spec) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_spec);
970 write_scriptlink(wd, &ma->scriptlink);
976 static void write_worlds(WriteData *wd, ListBase *idbase)
983 if(wrld->id.us>0 || wd->current) {
985 writestruct(wd, ID_WO, "World", 1, wrld);
987 for(a=0; a<MAX_MTEX; a++) {
988 if(wrld->mtex[a]) writestruct(wd, DATA, "MTex", 1, wrld->mtex[a]);
991 write_scriptlink(wd, &wrld->scriptlink);
997 static void write_lamps(WriteData *wd, ListBase *idbase)
1004 if(la->id.us>0 || wd->current) {
1006 writestruct(wd, ID_LA, "Lamp", 1, la);
1009 for(a=0; a<MAX_MTEX; a++) {
1010 if(la->mtex[a]) writestruct(wd, DATA, "MTex", 1, la->mtex[a]);
1013 write_scriptlink(wd, &la->scriptlink);
1019 static void write_lattices(WriteData *wd, ListBase *idbase)
1025 if(lt->id.us>0 || wd->current) {
1027 writestruct(wd, ID_LT, "Lattice", 1, lt);
1030 writestruct(wd, DATA, "BPoint", lt->pntsu*lt->pntsv*lt->pntsw, lt->def);
1036 static void write_ikas(WriteData *wd, ListBase *idbase)
1043 if(ika->id.us>0 || wd->current) {
1045 writestruct(wd, ID_IK, "Ika", 1, ika);
1048 li= ika->limbbase.first;
1050 writestruct(wd, DATA, "Limb", 1, li);
1054 writestruct(wd, DATA, "Deform", ika->totdef, ika->def);
1060 static void write_scenes(WriteData *wd, ListBase *scebase)
1069 sce= scebase->first;
1072 writestruct(wd, ID_SCE, "Scene", 1, sce);
1075 base= sce->base.first;
1077 writestruct(wd, DATA, "Base", 1, base);
1081 writestruct(wd, DATA, "Radio", 1, sce->radio);
1085 writestruct(wd, DATA, "Editing", 1, ed);
1087 /* reset write flags too */
1088 WHILE_SEQ(&ed->seqbase) {
1089 if(seq->strip) seq->strip->done= 0;
1090 writestruct(wd, DATA, "Sequence", 1, seq);
1094 WHILE_SEQ(&ed->seqbase) {
1095 if(seq->strip && seq->strip->done==0) {
1096 /* write strip with 'done' at 0 because readfile */
1098 if(seq->plugin) writestruct(wd, DATA, "PluginSeq", 1, seq->plugin);
1099 if(seq->effectdata) {
1102 writestruct(wd, DATA, "WipeVars", 1, seq->effectdata);
1105 writestruct(wd, DATA, "GlowVars", 1, seq->effectdata);
1111 writestruct(wd, DATA, "Strip", 1, strip);
1113 if(seq->type==SEQ_IMAGE)
1114 writestruct(wd, DATA, "StripElem", strip->len, strip->stripdata);
1115 else if(seq->type==SEQ_MOVIE || seq->type==SEQ_SOUND)
1116 writestruct(wd, DATA, "StripElem", 1, strip->stripdata);
1123 /* new; meta stack too, even when its nasty restore code */
1124 for(ms= ed->metastack.first; ms; ms= ms->next) {
1125 writestruct(wd, DATA, "MetaStack", 1, ms);
1129 write_scriptlink(wd, &sce->scriptlink);
1131 if (sce->r.avicodecdata) {
1132 writestruct(wd, DATA, "AviCodecData", 1, sce->r.avicodecdata);
1133 if (sce->r.avicodecdata->lpFormat) writedata(wd, DATA, sce->r.avicodecdata->cbFormat, sce->r.avicodecdata->lpFormat);
1134 if (sce->r.avicodecdata->lpParms) writedata(wd, DATA, sce->r.avicodecdata->cbParms, sce->r.avicodecdata->lpParms);
1137 if (sce->r.qtcodecdata) {
1138 writestruct(wd, DATA, "QuicktimeCodecData", 1, sce->r.qtcodecdata);
1139 if (sce->r.qtcodecdata->cdParms) writedata(wd, DATA, sce->r.qtcodecdata->cdSize, sce->r.qtcodecdata->cdParms);
1144 /* flush helps the compression for undo-save */
1145 mywrite(wd, MYWRITE_FLUSH, 0);
1148 static void write_screens(WriteData *wd, ListBase *scrbase)
1158 writestruct(wd, ID_SCR, "Screen", 1, sc);
1161 sv= sc->vertbase.first;
1163 writestruct(wd, DATA, "ScrVert", 1, sv);
1167 se= sc->edgebase.first;
1169 writestruct(wd, DATA, "ScrEdge", 1, se);
1173 sa= sc->areabase.first;
1178 writestruct(wd, DATA, "ScrArea", 1, sa);
1180 pa= sa->panels.first;
1182 writestruct(wd, DATA, "Panel", 1, pa);
1186 sl= sa->spacedata.first;
1188 if(sl->spacetype==SPACE_VIEW3D) {
1189 View3D *v3d= (View3D*) sl;
1190 writestruct(wd, DATA, "View3D", 1, v3d);
1191 if(v3d->bgpic) writestruct(wd, DATA, "BGpic", 1, v3d->bgpic);
1192 if(v3d->localvd) writestruct(wd, DATA, "View3D", 1, v3d->localvd);
1194 else if(sl->spacetype==SPACE_IPO) {
1195 writestruct(wd, DATA, "SpaceIpo", 1, sl);
1197 else if(sl->spacetype==SPACE_BUTS) {
1198 writestruct(wd, DATA, "SpaceButs", 1, sl);
1200 else if(sl->spacetype==SPACE_FILE) {
1201 writestruct(wd, DATA, "SpaceFile", 1, sl);
1203 else if(sl->spacetype==SPACE_SEQ) {
1204 writestruct(wd, DATA, "SpaceSeq", 1, sl);
1206 else if(sl->spacetype==SPACE_OOPS) {
1207 SpaceOops *so= (SpaceOops *)sl;
1211 oops= so->oops.first;
1213 Oops *oopsn= oops->next;
1215 BLI_remlink(&so->oops, oops);
1221 /* ater cleanup, because of listbase! */
1222 writestruct(wd, DATA, "SpaceOops", 1, so);
1224 oops= so->oops.first;
1226 writestruct(wd, DATA, "Oops", 1, oops);
1231 writestruct(wd, DATA, "TreeStore", 1, so->treestore);
1232 if(so->treestore->data)
1233 writestruct(wd, DATA, "TreeStoreElem", so->treestore->usedelem, so->treestore->data);
1236 else if(sl->spacetype==SPACE_IMAGE) {
1237 writestruct(wd, DATA, "SpaceImage", 1, sl);
1239 else if(sl->spacetype==SPACE_IMASEL) {
1240 writestruct(wd, DATA, "SpaceImaSel", 1, sl);
1242 else if(sl->spacetype==SPACE_TEXT) {
1243 writestruct(wd, DATA, "SpaceText", 1, sl);
1245 else if(sl->spacetype==SPACE_SCRIPT) {
1246 writestruct(wd, DATA, "SpaceScript", 1, sl);
1248 else if(sl->spacetype==SPACE_ACTION) {
1249 writestruct(wd, DATA, "SpaceAction", 1, sl);
1251 else if(sl->spacetype==SPACE_SOUND) {
1252 writestruct(wd, DATA, "SpaceSound", 1, sl);
1254 else if(sl->spacetype==SPACE_NLA){
1255 writestruct(wd, DATA, "SpaceNla", 1, sl);
1267 static void write_libraries(WriteData *wd, Main *main)
1269 ListBase *lbarray[30];
1271 int a, tot, foundone;
1275 a=tot= set_listbasepointers(main, lbarray);
1277 /* test: is lib being used */
1280 id= lbarray[tot]->first;
1282 if(id->us>0 && (id->flag & LIB_EXTERN)) {
1292 writestruct(wd, ID_LI, "Library", 1, main->curlib);
1295 id= lbarray[a]->first;
1297 if(id->us>0 && (id->flag & LIB_EXTERN)) {
1299 writestruct(wd, ID_ID, "ID", 1, id);
1310 static void write_bone(WriteData *wd, Bone* bone)
1314 // write_constraints(wd, &bone->constraints);
1317 writestruct(wd, DATA, "Bone", 1, bone);
1320 cbone= bone->childbase.first;
1322 write_bone(wd, cbone);
1327 static void write_armatures(WriteData *wd, ListBase *idbase)
1334 if (arm->id.us>0 || wd->current) {
1335 writestruct(wd, ID_AR, "bArmature", 1, arm);
1338 bone= arm->bonebase.first;
1340 write_bone(wd, bone);
1347 /* flush helps the compression for undo-save */
1348 mywrite(wd, MYWRITE_FLUSH, 0);
1351 static void write_actions(WriteData *wd, ListBase *idbase)
1354 bActionChannel *chan;
1357 if (act->id.us>0 || wd->current) {
1358 writestruct(wd, ID_AC, "bAction", 1, act);
1360 for (chan=act->chanbase.first; chan; chan=chan->next) {
1361 writestruct(wd, DATA, "bActionChannel", 1, chan);
1362 write_constraint_channels(wd, &chan->constraintChannels);
1369 static void write_texts(WriteData *wd, ListBase *idbase)
1374 text= idbase->first;
1376 if ( (text->flags & TXT_ISMEM) && (text->flags & TXT_ISEXT)) text->flags &= ~TXT_ISEXT;
1379 writestruct(wd, ID_TXT, "Text", 1, text);
1380 if(text->name) writedata(wd, DATA, strlen(text->name)+1, text->name);
1382 if(!(text->flags & TXT_ISEXT)) {
1383 /* now write the text data, in two steps for optimization in the readfunction */
1384 tmp= text->lines.first;
1386 writestruct(wd, DATA, "TextLine", 1, tmp);
1390 tmp= text->lines.first;
1392 writedata(wd, DATA, tmp->len+1, tmp->line);
1396 text= text->id.next;
1399 /* flush helps the compression for undo-save */
1400 mywrite(wd, MYWRITE_FLUSH, 0);
1403 static void write_sounds(WriteData *wd, ListBase *idbase)
1410 // set all samples to unsaved status
1412 sample = samples->first;
1414 sample->flags |= SAMPLE_NEEDS_SAVE;
1415 sample = sample->id.next;
1418 sound= idbase->first;
1420 if(sound->id.us>0 || wd->current) {
1421 // do we need to save the packedfile as well ?
1422 sample = sound->sample;
1424 if (sample->flags & SAMPLE_NEEDS_SAVE) {
1425 sound->newpackedfile = sample->packedfile;
1426 sample->flags &= ~SAMPLE_NEEDS_SAVE;
1428 sound->newpackedfile = NULL;
1433 writestruct(wd, ID_SO, "bSound", 1, sound);
1435 if (sound->newpackedfile) {
1436 pf = sound->newpackedfile;
1437 writestruct(wd, DATA, "PackedFile", 1, pf);
1438 writedata(wd, DATA, pf->size, pf->data);
1442 sound->newpackedfile = sample->packedfile;
1445 sound= sound->id.next;
1448 /* flush helps the compression for undo-save */
1449 mywrite(wd, MYWRITE_FLUSH, 0);
1452 static void write_groups(WriteData *wd, ListBase *idbase)
1459 group= idbase->first;
1461 if(group->id.us>0 || wd->current) {
1463 writestruct(wd, ID_GR, "Group", 1, group);
1465 gk= group->gkey.first;
1467 writestruct(wd, DATA, "GroupKey", 1, gk);
1471 go= group->gobject.first;
1473 writestruct(wd, DATA, "GroupObject", 1, go);
1476 go= group->gobject.first;
1480 writestruct(wd, DATA, "ObjectKey", 1, ok);
1487 group= group->id.next;
1491 static void write_global(WriteData *wd)
1495 fg.curscreen= G.curscreen;
1496 fg.curscene= G.scene;
1497 fg.displaymode= R.displaymode;
1498 fg.winpos= R.winpos;
1499 fg.fileflags= (G.fileflags & ~G_FILE_NO_UI); // prevent to save this, is not good convention, and feature with concerns...
1502 writestruct(wd, GLOB, "FileGlobal", 1, &fg);
1505 /* if *mem there's filesave to memory */
1506 static int write_file_handle(int handle, MemFile *compare, MemFile *current, int write_user_block, int write_flags)
1514 mainlist.first= mainlist.last= G.main;
1517 blo_split_main(&mainlist);
1519 wd= bgnwrite(handle, compare, current, write_flags);
1521 sprintf(buf, "BLENDER%c%c%.3d", (sizeof(void*)==8)?'-':'_', (G.order==B_ENDIAN)?'V':'v', G.version);
1522 mywrite(wd, buf, 12);
1524 write_renderinfo(wd);
1527 write_screens (wd, &G.main->screen); // no UI save
1528 write_scenes (wd, &G.main->scene);
1529 write_curves (wd, &G.main->curve);
1530 write_mballs (wd, &G.main->mball);
1531 write_images (wd, &G.main->image);
1532 write_cameras (wd, &G.main->camera);
1533 write_lamps (wd, &G.main->lamp);
1534 write_lattices (wd, &G.main->latt);
1535 write_ikas (wd, &G.main->ika);
1536 write_vfonts (wd, &G.main->vfont);
1537 write_ipos (wd, &G.main->ipo);
1538 write_keys (wd, &G.main->key);
1539 write_worlds (wd, &G.main->world);
1540 write_texts (wd, &G.main->text);
1541 write_sounds (wd, &G.main->sound);
1542 write_groups (wd, &G.main->group);
1543 write_armatures(wd, &G.main->armature);
1544 write_actions (wd, &G.main->action);
1545 write_objects (wd, &G.main->object);
1546 write_materials(wd, &G.main->mat);
1547 write_textures (wd, &G.main->tex);
1548 write_meshs (wd, &G.main->mesh);
1549 write_libraries(wd, G.main->next);
1552 if (write_user_block) {
1556 /* dna as last, because (to be implemented) test for which structs are written */
1557 writedata(wd, DNA1, wd->sdna->datalen, wd->sdna->data);
1560 memset(&bhead, 0, sizeof(BHead));
1562 mywrite(wd, &bhead, sizeof(BHead));
1564 blo_join_main(&mainlist);
1565 G.main= mainlist.first;
1567 return endwrite(wd);
1570 /* return: success (1) */
1571 int BLO_write_file(char *dir, int write_flags, char **error_r)
1573 char userfilename[FILE_MAXDIR+FILE_MAXFILE];
1574 char tempname[FILE_MAXDIR+FILE_MAXFILE];
1575 int file, fout, write_user_block;
1577 char tmpdir[FILE_MAXDIR+FILE_MAXFILE];
1580 sprintf(tempname, "%s@", dir);
1582 file = open(tempname,O_BINARY+O_WRONLY+O_CREAT+O_TRUNC, 0666);
1584 *error_r= "Unable to open";
1588 BLI_make_file_string(G.sce, userfilename, BLI_gethome(), ".B.blend");
1590 write_user_block= BLI_streq(dir, userfilename);
1592 fout= write_file_handle(file, NULL,NULL, write_user_block, write_flags);
1596 if(BLI_rename(tempname, dir) < 0) {
1597 *error_r= "Can't change old file. File saved with @";
1603 *error_r= "Not enough diskspace";
1610 /* return: success (1) */
1611 int BLO_write_file_mem(MemFile *compare, MemFile *current, int write_flags, char **error_r)
1615 err= write_file_handle(0, compare, current, 0, write_flags);
1617 if(err==0) return 1;
1622 /* Runtime writing */
1625 #define PATHSEPERATOR "\\"
1627 #define PATHSEPERATOR "/"
1630 static char *get_install_dir(void) {
1631 extern char bprogname[];
1632 char *tmpname = BLI_strdup(bprogname);
1636 cut = strstr(tmpname, ".app");
1637 if (cut) cut[0] = 0;
1640 cut = BLI_last_slash(tmpname);
1651 static char *get_runtime_path(char *exename) {
1652 char *installpath= get_install_dir();
1657 char *path= MEM_mallocN(strlen(installpath)+strlen(PATHSEPERATOR)+strlen(exename)+1, "runtimepath");
1658 strcpy(path, installpath);
1659 strcat(path, PATHSEPERATOR);
1660 strcat(path, exename);
1662 MEM_freeN(installpath);
1670 static int recursive_copy_runtime(char *outname, char *exename, char **cause_r)
1672 char *cause = NULL, *runtime = get_runtime_path(exename);
1673 char command[2 * (FILE_MAXDIR+FILE_MAXFILE) + 32];
1677 cause= "Unable to find runtime";
1680 //printf("runtimepath %s\n", runtime);
1682 progfd= open(runtime, O_BINARY|O_RDONLY, 0);
1684 cause= "Unable to find runtime";
1688 sprintf(command, "/bin/cp -R \"%s\" \"%s\"", runtime, outname);
1689 //printf("command %s\n", command);
1690 if (system(command) == -1) {
1691 cause = "Couldn't copy runtime";
1707 void BLO_write_runtime(char *file, char *exename) {
1708 char gamename[FILE_MAXDIR+FILE_MAXFILE];
1712 // remove existing file / bundle
1713 //printf("Delete file %s\n", file);
1714 BLI_delete(file, NULL, TRUE);
1716 if (!recursive_copy_runtime(file, exename, &cause))
1719 strcpy(gamename, file);
1720 strcat(gamename, "/Contents/Resources/game.blend");
1721 //printf("gamename %s\n", gamename);
1722 outfd= open(gamename, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, 0777);
1725 write_file_handle(outfd, NULL,NULL, 0, G.fileflags);
1727 if (write(outfd, " ", 1) != 1) {
1728 cause= "Unable to write to output file";
1732 cause = "Unable to open blenderfile";
1740 error("Unable to make runtime: %s", cause);
1743 #else /* !__APPLE__ */
1745 static int handle_append_runtime(int handle, char *exename, char **cause_r) {
1746 char *cause= NULL, *runtime= get_runtime_path(exename);
1747 unsigned char buf[1024];
1748 int count, progfd= -1;
1751 cause= "Unable to find runtime";
1755 progfd= open(runtime, O_BINARY|O_RDONLY, 0);
1757 cause= "Unable to find runtime";
1761 while ((count= read(progfd, buf, sizeof(buf)))>0) {
1762 if (write(handle, buf, count)!=count) {
1763 cause= "Unable to write to output file";
1781 static int handle_write_msb_int(int handle, int i) {
1782 unsigned char buf[4];
1783 buf[0]= (i>>24)&0xFF;
1784 buf[1]= (i>>16)&0xFF;
1785 buf[2]= (i>>8)&0xFF;
1786 buf[3]= (i>>0)&0xFF;
1788 return (write(handle, buf, 4)==4);
1791 void BLO_write_runtime(char *file, char *exename) {
1792 int outfd= open(file, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, 0777);
1797 cause= "Unable to open output file";
1800 if (!handle_append_runtime(outfd, exename, &cause))
1803 datastart= lseek(outfd, 0, SEEK_CUR);
1805 write_file_handle(outfd, NULL,NULL, 0, G.fileflags);
1807 if (!handle_write_msb_int(outfd, datastart) || (write(outfd, "BRUNTIME", 8)!=8)) {
1808 cause= "Unable to write to output file";
1817 error("Unable to make runtime: %s", cause);
1820 #endif /* !__APPLE__ */