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_bad_level_calls.h" // build_seqar (from WHILE_SEQ) free_oops error
148 #include "BKE_curve.h"
149 #include "BKE_constraint.h"
150 #include "BKE_global.h" // for G
151 #include "BKE_library.h" // for set_listbasepointers
152 #include "BKE_main.h" // G.main
153 #include "BKE_packedFile.h" // for packAll
154 #include "BKE_screen.h" // for waitcursor
155 #include "BKE_scene.h" // for do_seq
156 #include "BKE_sound.h" /* ... and for samples */
157 #include "BKE_utildefines.h" // for defines
159 #include "GEN_messaging.h"
161 #include "BLO_writefile.h"
162 #include "BLO_readfile.h"
163 #include "BLO_undofile.h"
165 #include "readfile.h"
169 /* ********* my write, buffered writing with minimum 50k chunks ************ */
176 MemFile *compare, *current;
178 int tot, count, error, memsize;
181 static WriteData *writedata_new(int file)
183 extern char DNAstr[]; /* DNA.c */
186 WriteData *wd= MEM_callocN(sizeof(*wd), "writedata");
188 /* XXX, see note about this in readfile.c, remove
189 * once we have an xp lock - zr
191 wd->sdna= dna_sdna_from_data(DNAstr, DNAlen, 0);
195 wd->buf= MEM_mallocN(100000, "wd->buf");
200 static void writedata_do_write(WriteData *wd, void *mem, int memlen)
202 if (wd->error) return;
204 /* memory based save */
206 add_memfilechunk(NULL, wd->current, mem, memlen);
209 if (write(wd->file, mem, memlen) != memlen)
215 static void writedata_free(WriteData *wd)
217 dna_freestructDNA(wd->sdna);
228 * Low level WRITE(2) wrapper that buffers data
229 * @param adr Pointer to new chunk of data
230 * @param len Length of new chunk of data
231 * @warning Talks to other functions with global parameters
234 #define MYWRITE_FLUSH NULL
236 static void mywrite( WriteData *wd, void *adr, int len)
238 if (wd->error) return;
240 if(adr==MYWRITE_FLUSH) {
242 writedata_do_write(wd, wd->buf, wd->count);
252 writedata_do_write(wd, wd->buf, wd->count);
255 writedata_do_write(wd, adr, len);
258 if(len+wd->count>99999) {
259 writedata_do_write(wd, wd->buf, wd->count);
262 memcpy(&wd->buf[wd->count], adr, len);
268 * BeGiN initializer for mywrite
269 * @param file File descriptor
270 * @param write_flags Write parameters
271 * @warning Talks to other functions with global parameters
273 static WriteData *bgnwrite(int file, MemFile *compare, MemFile *current, int write_flags)
275 WriteData *wd= writedata_new(file);
277 wd->compare= compare;
278 wd->current= current;
279 /* this inits comparing */
280 add_memfilechunk(compare, NULL, NULL, 0);
286 * END the mywrite wrapper
287 * @return 1 if write failed
288 * @return unknown global variable otherwise
289 * @warning Talks to other functions with global parameters
291 static int endwrite(WriteData *wd)
296 writedata_do_write(wd, wd->buf, wd->count);
302 /* blender gods may live forever but this parent pointer died in the statement above
303 if(wd->current) printf("undo size %d\n", wd->current->size);
308 /* ********** WRITE FILE ****************** */
310 static void writestruct(WriteData *wd, int filecode, char *structname, int nr, void *adr)
315 if(adr==0 || nr==0) return;
322 bh.SDNAnr= dna_findstruct_nr(wd->sdna, structname);
324 printf("error: can't find SDNA code %s\n", structname);
327 sp= wd->sdna->structs[bh.SDNAnr];
329 bh.len= nr*wd->sdna->typelens[sp[0]];
331 if(bh.len==0) return;
333 mywrite(wd, &bh, sizeof(BHead));
334 mywrite(wd, adr, bh.len);
337 static void writedata(WriteData *wd, int filecode, int len, void *adr) /* do not use for structs */
354 mywrite(wd, &bh, sizeof(BHead));
355 if(len) mywrite(wd, adr, len);
358 static void write_scriptlink(WriteData *wd, ScriptLink *slink)
360 writedata(wd, DATA, sizeof(void *)*slink->totscript, slink->scripts);
361 writedata(wd, DATA, sizeof(short)*slink->totscript, slink->flag);
364 static void write_renderinfo(WriteData *wd) /* for renderdaemon */
369 sce= G.main->scene.first;
371 if(sce->id.lib==0 && ( sce==G.scene || (sce->r.scemode & R_BG_RENDER)) ) {
372 data[0]= sce->r.sfra;
373 data[1]= sce->r.efra;
375 strncpy((char *)(data+2), sce->id.name+2, 23);
377 writedata(wd, REND, 32, data);
383 static void write_userdef(WriteData *wd)
387 writestruct(wd, USER, "UserDef", 1, &U);
389 btheme= U.themes.first;
391 writestruct(wd, DATA, "bTheme", 1, btheme);
392 btheme= btheme->next;
396 static void write_effects(WriteData *wd, ListBase *lb)
405 writestruct(wd, DATA, "BuildEff", 1, eff);
408 writestruct(wd, DATA, "PartEff", 1, eff);
411 writestruct(wd, DATA, "WaveEff", 1, eff);
414 writedata(wd, DATA, MEM_allocN_len(eff), eff);
421 static void write_properties(WriteData *wd, ListBase *lb)
427 writestruct(wd, DATA, "bProperty", 1, prop);
429 if(prop->poin && prop->poin != &prop->data)
430 writedata(wd, DATA, MEM_allocN_len(prop->poin), prop->poin);
436 static void write_sensors(WriteData *wd, ListBase *lb)
442 writestruct(wd, DATA, "bSensor", 1, sens);
444 writedata(wd, DATA, sizeof(void *)*sens->totlinks, sens->links);
448 writestruct(wd, DATA, "bNearSensor", 1, sens->data);
451 writestruct(wd, DATA, "bMouseSensor", 1, sens->data);
454 writestruct(wd, DATA, "bTouchSensor", 1, sens->data);
457 writestruct(wd, DATA, "bKeyboardSensor", 1, sens->data);
460 writestruct(wd, DATA, "bPropertySensor", 1, sens->data);
463 writestruct(wd, DATA, "bCollisionSensor", 1, sens->data);
466 writestruct(wd, DATA, "bRadarSensor", 1, sens->data);
469 writestruct(wd, DATA, "bRandomSensor", 1, sens->data);
472 writestruct(wd, DATA, "bRaySensor", 1, sens->data);
475 writestruct(wd, DATA, "bMessageSensor", 1, sens->data);
478 writestruct(wd, DATA, "bJoystickSensor", 1, sens->data);
481 ; /* error: don't know how to write this file */
488 static void write_controllers(WriteData *wd, ListBase *lb)
494 writestruct(wd, DATA, "bController", 1, cont);
496 writedata(wd, DATA, sizeof(void *)*cont->totlinks, cont->links);
499 case CONT_EXPRESSION:
500 writestruct(wd, DATA, "bExpressionCont", 1, cont->data);
503 writestruct(wd, DATA, "bPythonCont", 1, cont->data);
506 ; /* error: don't know how to write this file */
513 static void write_actuators(WriteData *wd, ListBase *lb)
519 writestruct(wd, DATA, "bActuator", 1, act);
523 writestruct(wd, DATA, "bActionActuator", 1, act->data);
526 writestruct(wd, DATA, "bSoundActuator", 1, act->data);
529 writestruct(wd, DATA, "bCDActuator", 1, act->data);
532 writestruct(wd, DATA, "bObjectActuator", 1, act->data);
535 writestruct(wd, DATA, "bIpoActuator", 1, act->data);
538 writestruct(wd, DATA, "bPropertyActuator", 1, act->data);
541 writestruct(wd, DATA, "bCameraActuator", 1, act->data);
544 writestruct(wd, DATA, "bConstraintActuator", 1, act->data);
546 case ACT_EDIT_OBJECT:
547 writestruct(wd, DATA, "bEditObjectActuator", 1, act->data);
550 writestruct(wd, DATA, "bSceneActuator", 1, act->data);
553 writestruct(wd, DATA, "bGroupActuator", 1, act->data);
556 writestruct(wd, DATA, "bRandomActuator", 1, act->data);
559 writestruct(wd, DATA, "bMessageActuator", 1, act->data);
562 writestruct(wd, DATA, "bGameActuator", 1, act->data);
565 writestruct(wd, DATA, "bVisibilityActuator", 1, act->data);
568 ; /* error: don't know how to write this file */
575 static void write_nlastrips(WriteData *wd, ListBase *nlabase)
579 for (strip=nlabase->first; strip; strip=strip->next)
580 writestruct(wd, DATA, "bActionStrip", 1, strip);
583 static void write_constraints(WriteData *wd, ListBase *conlist)
587 for (con=conlist->first; con; con=con->next) {
588 /* Write the specific data */
590 case CONSTRAINT_TYPE_NULL:
592 case CONSTRAINT_TYPE_TRACKTO:
593 writestruct(wd, DATA, "bTrackToConstraint", 1, con->data);
595 case CONSTRAINT_TYPE_KINEMATIC:
596 writestruct(wd, DATA, "bKinematicConstraint", 1, con->data);
598 case CONSTRAINT_TYPE_ROTLIKE:
599 writestruct(wd, DATA, "bRotateLikeConstraint", 1, con->data);
601 case CONSTRAINT_TYPE_LOCLIKE:
602 writestruct(wd, DATA, "bLocateLikeConstraint", 1, con->data);
604 case CONSTRAINT_TYPE_ACTION:
605 writestruct(wd, DATA, "bActionConstraint", 1, con->data);
607 case CONSTRAINT_TYPE_LOCKTRACK:
608 writestruct(wd, DATA, "bLockTrackConstraint", 1, con->data);
610 case CONSTRAINT_TYPE_FOLLOWPATH:
611 writestruct(wd, DATA, "bFollowPathConstraint", 1, con->data);
613 case CONSTRAINT_TYPE_STRETCHTO:
614 writestruct(wd, DATA, "bStretchToConstraint", 1, con->data);
619 /* Write the constraint */
620 writestruct(wd, DATA, "bConstraint", 1, con);
624 static void write_pose(WriteData *wd, bPose *pose)
628 /* Write each channel */
634 for (chan=pose->chanbase.first; chan; chan=chan->next) {
635 write_constraints(wd, &chan->constraints);
636 writestruct(wd, DATA, "bPoseChannel", 1, chan);
640 writestruct(wd, DATA, "bPose", 1, pose);
643 static void write_defgroups(WriteData *wd, ListBase *defbase)
645 bDeformGroup *defgroup;
647 for (defgroup=defbase->first; defgroup; defgroup=defgroup->next)
648 writestruct(wd, DATA, "bDeformGroup", 1, defgroup);
651 static void write_constraint_channels(WriteData *wd, ListBase *chanbase)
653 bConstraintChannel *chan;
655 for (chan = chanbase->first; chan; chan=chan->next)
656 writestruct(wd, DATA, "bConstraintChannel", 1, chan);
660 static void write_objects(WriteData *wd, ListBase *idbase)
667 if(ob->id.us>0 || wd->current) {
669 writestruct(wd, ID_OB, "Object", 1, ob);
672 writedata(wd, DATA, sizeof(void *)*ob->totcol, ob->mat);
673 write_effects(wd, &ob->effect);
674 write_properties(wd, &ob->prop);
675 write_sensors(wd, &ob->sensors);
676 write_controllers(wd, &ob->controllers);
677 write_actuators(wd, &ob->actuators);
678 write_scriptlink(wd, &ob->scriptlink);
679 write_pose(wd, ob->pose);
680 write_defgroups(wd, &ob->defbase);
681 write_constraints(wd, &ob->constraints);
682 write_constraint_channels(wd, &ob->constraintChannels);
683 write_nlastrips(wd, &ob->nlastrips);
685 writestruct(wd, DATA, "PartDeflect", 1, ob->pd);
687 for(hook= ob->hooks.first; hook; hook= hook->next) {
688 writestruct(wd, DATA, "ObHook", 1, hook);
689 writedata(wd, DATA, sizeof(int)*hook->totindex, hook->indexar);
695 /* flush helps the compression for undo-save */
696 mywrite(wd, MYWRITE_FLUSH, 0);
700 static void write_vfonts(WriteData *wd, ListBase *idbase)
707 if(vf->id.us>0 || wd->current) {
709 writestruct(wd, ID_VF, "VFont", 1, vf);
713 if (vf->packedfile) {
715 writestruct(wd, DATA, "PackedFile", 1, pf);
716 writedata(wd, DATA, pf->size, pf->data);
724 static void write_ipos(WriteData *wd, ListBase *idbase)
731 if(ipo->id.us>0 || wd->current) {
733 writestruct(wd, ID_IP, "Ipo", 1, ipo);
736 icu= ipo->curve.first;
738 writestruct(wd, DATA, "IpoCurve", 1, icu);
742 icu= ipo->curve.first;
744 if(icu->bezt) writestruct(wd, DATA, "BezTriple", icu->totvert, icu->bezt);
745 if(icu->bp) writestruct(wd, DATA, "BPoint", icu->totvert, icu->bp);
753 /* flush helps the compression for undo-save */
754 mywrite(wd, MYWRITE_FLUSH, 0);
757 static void write_keys(WriteData *wd, ListBase *idbase)
764 if(key->id.us>0 || wd->current) {
766 writestruct(wd, ID_KE, "Key", 1, key);
769 kb= key->block.first;
771 writestruct(wd, DATA, "KeyBlock", 1, kb);
772 if(kb->data) writedata(wd, DATA, kb->totelem*key->elemsize, kb->data);
779 /* flush helps the compression for undo-save */
780 mywrite(wd, MYWRITE_FLUSH, 0);
783 static void write_cameras(WriteData *wd, ListBase *idbase)
789 if(cam->id.us>0 || wd->current) {
791 writestruct(wd, ID_CA, "Camera", 1, cam);
794 write_scriptlink(wd, &cam->scriptlink);
801 static void write_mballs(WriteData *wd, ListBase *idbase)
808 if(mb->id.us>0 || wd->current) {
810 writestruct(wd, ID_MB, "MetaBall", 1, mb);
813 writedata(wd, DATA, sizeof(void *)*mb->totcol, mb->mat);
817 writestruct(wd, DATA, "MetaElem", 1, ml);
825 static void write_curves(WriteData *wd, ListBase *idbase)
832 if(cu->id.us>0 || wd->current) {
834 writestruct(wd, ID_CU, "Curve", 1, cu);
837 writedata(wd, DATA, sizeof(void *)*cu->totcol, cu->mat);
840 writedata(wd, DATA, cu->len+1, cu->str);
843 /* is also the order of reading */
846 writestruct(wd, DATA, "Nurb", 1, nu);
851 if( (nu->type & 7)==CU_BEZIER)
852 writestruct(wd, DATA, "BezTriple", nu->pntsu, nu->bezt);
854 writestruct(wd, DATA, "BPoint", nu->pntsu*nu->pntsv, nu->bp);
855 if(nu->knotsu) writedata(wd, DATA, KNOTSU(nu)*sizeof(float), nu->knotsu);
856 if(nu->knotsv) writedata(wd, DATA, KNOTSV(nu)*sizeof(float), nu->knotsv);
865 /* flush helps the compression for undo-save */
866 mywrite(wd, MYWRITE_FLUSH, 0);
869 static void write_dverts(WriteData *wd, int count, MDeformVert *dvlist)
873 /* Write the dvert list */
874 writestruct(wd, DATA, "MDeformVert", count, dvlist);
876 /* Write deformation data for each dvert */
878 for (i=0; i<count; i++) {
880 writestruct(wd, DATA, "MDeformWeight", dvlist[i].totweight, dvlist[i].dw);
885 static void write_meshs(WriteData *wd, ListBase *idbase)
891 if(mesh->id.us>0 || wd->current) {
893 writestruct(wd, ID_ME, "Mesh", 1, mesh);
896 writedata(wd, DATA, sizeof(void *)*mesh->totcol, mesh->mat);
898 writestruct(wd, DATA, "MVert", mesh->totvert, mesh->mvert);
899 writestruct(wd, DATA, "MEdge", mesh->totedge, mesh->medge);
900 writestruct(wd, DATA, "MFace", mesh->totface, mesh->mface);
901 writestruct(wd, DATA, "TFace", mesh->totface, mesh->tface);
902 writestruct(wd, DATA, "MCol", 4*mesh->totface, mesh->mcol);
903 writestruct(wd, DATA, "MSticky", mesh->totvert, mesh->msticky);
905 write_dverts(wd, mesh->totvert, mesh->dvert);
912 static void write_images(WriteData *wd, ListBase *idbase)
919 if(ima->id.us>0 || wd->current) {
921 writestruct(wd, ID_IM, "Image", 1, ima);
923 if (ima->packedfile) {
924 pf = ima->packedfile;
925 writestruct(wd, DATA, "PackedFile", 1, pf);
926 writedata(wd, DATA, pf->size, pf->data);
931 /* flush helps the compression for undo-save */
932 mywrite(wd, MYWRITE_FLUSH, 0);
935 static void write_textures(WriteData *wd, ListBase *idbase)
941 if(tex->id.us>0 || wd->current) {
943 writestruct(wd, ID_TE, "Tex", 1, tex);
946 if(tex->plugin) writestruct(wd, DATA, "PluginTex", 1, tex->plugin);
947 if(tex->coba) writestruct(wd, DATA, "ColorBand", 1, tex->coba);
948 if(tex->env) writestruct(wd, DATA, "EnvMap", 1, tex->env);
953 /* flush helps the compression for undo-save */
954 mywrite(wd, MYWRITE_FLUSH, 0);
957 static void write_materials(WriteData *wd, ListBase *idbase)
964 if(ma->id.us>0 || wd->current) {
966 writestruct(wd, ID_MA, "Material", 1, ma);
968 for(a=0; a<MAX_MTEX; a++) {
969 if(ma->mtex[a]) writestruct(wd, DATA, "MTex", 1, ma->mtex[a]);
972 if(ma->ramp_col) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_col);
973 if(ma->ramp_spec) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_spec);
975 write_scriptlink(wd, &ma->scriptlink);
981 static void write_worlds(WriteData *wd, ListBase *idbase)
988 if(wrld->id.us>0 || wd->current) {
990 writestruct(wd, ID_WO, "World", 1, wrld);
992 for(a=0; a<MAX_MTEX; a++) {
993 if(wrld->mtex[a]) writestruct(wd, DATA, "MTex", 1, wrld->mtex[a]);
996 write_scriptlink(wd, &wrld->scriptlink);
1002 static void write_lamps(WriteData *wd, ListBase *idbase)
1009 if(la->id.us>0 || wd->current) {
1011 writestruct(wd, ID_LA, "Lamp", 1, la);
1014 for(a=0; a<MAX_MTEX; a++) {
1015 if(la->mtex[a]) writestruct(wd, DATA, "MTex", 1, la->mtex[a]);
1018 write_scriptlink(wd, &la->scriptlink);
1024 static void write_lattices(WriteData *wd, ListBase *idbase)
1030 if(lt->id.us>0 || wd->current) {
1032 writestruct(wd, ID_LT, "Lattice", 1, lt);
1035 writestruct(wd, DATA, "BPoint", lt->pntsu*lt->pntsv*lt->pntsw, lt->def);
1041 static void write_ikas(WriteData *wd, ListBase *idbase)
1048 if(ika->id.us>0 || wd->current) {
1050 writestruct(wd, ID_IK, "Ika", 1, ika);
1053 li= ika->limbbase.first;
1055 writestruct(wd, DATA, "Limb", 1, li);
1059 writestruct(wd, DATA, "Deform", ika->totdef, ika->def);
1065 static void write_scenes(WriteData *wd, ListBase *scebase)
1074 sce= scebase->first;
1077 writestruct(wd, ID_SCE, "Scene", 1, sce);
1080 base= sce->base.first;
1082 writestruct(wd, DATA, "Base", 1, base);
1086 writestruct(wd, DATA, "Radio", 1, sce->radio);
1090 writestruct(wd, DATA, "Editing", 1, ed);
1092 /* reset write flags too */
1093 WHILE_SEQ(&ed->seqbase) {
1094 if(seq->strip) seq->strip->done= 0;
1095 writestruct(wd, DATA, "Sequence", 1, seq);
1099 WHILE_SEQ(&ed->seqbase) {
1100 if(seq->strip && seq->strip->done==0) {
1101 /* write strip with 'done' at 0 because readfile */
1103 if(seq->plugin) writestruct(wd, DATA, "PluginSeq", 1, seq->plugin);
1104 if(seq->effectdata) {
1107 writestruct(wd, DATA, "WipeVars", 1, seq->effectdata);
1110 writestruct(wd, DATA, "GlowVars", 1, seq->effectdata);
1116 writestruct(wd, DATA, "Strip", 1, strip);
1118 if(seq->type==SEQ_IMAGE)
1119 writestruct(wd, DATA, "StripElem", strip->len, strip->stripdata);
1120 else if(seq->type==SEQ_MOVIE || seq->type==SEQ_SOUND)
1121 writestruct(wd, DATA, "StripElem", 1, strip->stripdata);
1128 /* new; meta stack too, even when its nasty restore code */
1129 for(ms= ed->metastack.first; ms; ms= ms->next) {
1130 writestruct(wd, DATA, "MetaStack", 1, ms);
1134 write_scriptlink(wd, &sce->scriptlink);
1136 if (sce->r.avicodecdata) {
1137 writestruct(wd, DATA, "AviCodecData", 1, sce->r.avicodecdata);
1138 if (sce->r.avicodecdata->lpFormat) writedata(wd, DATA, sce->r.avicodecdata->cbFormat, sce->r.avicodecdata->lpFormat);
1139 if (sce->r.avicodecdata->lpParms) writedata(wd, DATA, sce->r.avicodecdata->cbParms, sce->r.avicodecdata->lpParms);
1142 if (sce->r.qtcodecdata) {
1143 writestruct(wd, DATA, "QuicktimeCodecData", 1, sce->r.qtcodecdata);
1144 if (sce->r.qtcodecdata->cdParms) writedata(wd, DATA, sce->r.qtcodecdata->cdSize, sce->r.qtcodecdata->cdParms);
1149 /* flush helps the compression for undo-save */
1150 mywrite(wd, MYWRITE_FLUSH, 0);
1153 static void write_screens(WriteData *wd, ListBase *scrbase)
1163 writestruct(wd, ID_SCR, "Screen", 1, sc);
1166 sv= sc->vertbase.first;
1168 writestruct(wd, DATA, "ScrVert", 1, sv);
1172 se= sc->edgebase.first;
1174 writestruct(wd, DATA, "ScrEdge", 1, se);
1178 sa= sc->areabase.first;
1183 writestruct(wd, DATA, "ScrArea", 1, sa);
1185 pa= sa->panels.first;
1187 writestruct(wd, DATA, "Panel", 1, pa);
1191 sl= sa->spacedata.first;
1193 if(sl->spacetype==SPACE_VIEW3D) {
1194 View3D *v3d= (View3D*) sl;
1195 writestruct(wd, DATA, "View3D", 1, v3d);
1196 if(v3d->bgpic) writestruct(wd, DATA, "BGpic", 1, v3d->bgpic);
1197 if(v3d->localvd) writestruct(wd, DATA, "View3D", 1, v3d->localvd);
1199 else if(sl->spacetype==SPACE_IPO) {
1200 writestruct(wd, DATA, "SpaceIpo", 1, sl);
1202 else if(sl->spacetype==SPACE_BUTS) {
1203 writestruct(wd, DATA, "SpaceButs", 1, sl);
1205 else if(sl->spacetype==SPACE_FILE) {
1206 writestruct(wd, DATA, "SpaceFile", 1, sl);
1208 else if(sl->spacetype==SPACE_SEQ) {
1209 writestruct(wd, DATA, "SpaceSeq", 1, sl);
1211 else if(sl->spacetype==SPACE_OOPS) {
1212 SpaceOops *so= (SpaceOops *)sl;
1216 oops= so->oops.first;
1218 Oops *oopsn= oops->next;
1220 BLI_remlink(&so->oops, oops);
1226 /* ater cleanup, because of listbase! */
1227 writestruct(wd, DATA, "SpaceOops", 1, so);
1229 oops= so->oops.first;
1231 writestruct(wd, DATA, "Oops", 1, oops);
1236 writestruct(wd, DATA, "TreeStore", 1, so->treestore);
1237 if(so->treestore->data)
1238 writestruct(wd, DATA, "TreeStoreElem", so->treestore->usedelem, so->treestore->data);
1241 else if(sl->spacetype==SPACE_IMAGE) {
1242 writestruct(wd, DATA, "SpaceImage", 1, sl);
1244 else if(sl->spacetype==SPACE_IMASEL) {
1245 writestruct(wd, DATA, "SpaceImaSel", 1, sl);
1247 else if(sl->spacetype==SPACE_TEXT) {
1248 writestruct(wd, DATA, "SpaceText", 1, sl);
1250 else if(sl->spacetype==SPACE_SCRIPT) {
1251 writestruct(wd, DATA, "SpaceScript", 1, sl);
1253 else if(sl->spacetype==SPACE_ACTION) {
1254 writestruct(wd, DATA, "SpaceAction", 1, sl);
1256 else if(sl->spacetype==SPACE_SOUND) {
1257 writestruct(wd, DATA, "SpaceSound", 1, sl);
1259 else if(sl->spacetype==SPACE_NLA){
1260 writestruct(wd, DATA, "SpaceNla", 1, sl);
1272 static void write_libraries(WriteData *wd, Main *main)
1274 ListBase *lbarray[30];
1276 int a, tot, foundone;
1280 a=tot= set_listbasepointers(main, lbarray);
1282 /* test: is lib being used */
1285 id= lbarray[tot]->first;
1287 if(id->us>0 && (id->flag & LIB_EXTERN)) {
1297 writestruct(wd, ID_LI, "Library", 1, main->curlib);
1300 id= lbarray[a]->first;
1302 if(id->us>0 && (id->flag & LIB_EXTERN)) {
1304 writestruct(wd, ID_ID, "ID", 1, id);
1315 static void write_bone(WriteData *wd, Bone* bone)
1319 // write_constraints(wd, &bone->constraints);
1322 writestruct(wd, DATA, "Bone", 1, bone);
1325 cbone= bone->childbase.first;
1327 write_bone(wd, cbone);
1332 static void write_armatures(WriteData *wd, ListBase *idbase)
1339 if (arm->id.us>0 || wd->current) {
1340 writestruct(wd, ID_AR, "bArmature", 1, arm);
1343 bone= arm->bonebase.first;
1345 write_bone(wd, bone);
1352 /* flush helps the compression for undo-save */
1353 mywrite(wd, MYWRITE_FLUSH, 0);
1356 static void write_actions(WriteData *wd, ListBase *idbase)
1359 bActionChannel *chan;
1362 if (act->id.us>0 || wd->current) {
1363 writestruct(wd, ID_AC, "bAction", 1, act);
1365 for (chan=act->chanbase.first; chan; chan=chan->next) {
1366 writestruct(wd, DATA, "bActionChannel", 1, chan);
1367 write_constraint_channels(wd, &chan->constraintChannels);
1374 static void write_texts(WriteData *wd, ListBase *idbase)
1379 text= idbase->first;
1381 if ( (text->flags & TXT_ISMEM) && (text->flags & TXT_ISEXT)) text->flags &= ~TXT_ISEXT;
1384 writestruct(wd, ID_TXT, "Text", 1, text);
1385 if(text->name) writedata(wd, DATA, strlen(text->name)+1, text->name);
1387 if(!(text->flags & TXT_ISEXT)) {
1388 /* now write the text data, in two steps for optimization in the readfunction */
1389 tmp= text->lines.first;
1391 writestruct(wd, DATA, "TextLine", 1, tmp);
1395 tmp= text->lines.first;
1397 writedata(wd, DATA, tmp->len+1, tmp->line);
1401 text= text->id.next;
1404 /* flush helps the compression for undo-save */
1405 mywrite(wd, MYWRITE_FLUSH, 0);
1408 static void write_sounds(WriteData *wd, ListBase *idbase)
1415 // set all samples to unsaved status
1417 sample = samples->first;
1419 sample->flags |= SAMPLE_NEEDS_SAVE;
1420 sample = sample->id.next;
1423 sound= idbase->first;
1425 if(sound->id.us>0 || wd->current) {
1426 // do we need to save the packedfile as well ?
1427 sample = sound->sample;
1429 if (sample->flags & SAMPLE_NEEDS_SAVE) {
1430 sound->newpackedfile = sample->packedfile;
1431 sample->flags &= ~SAMPLE_NEEDS_SAVE;
1433 sound->newpackedfile = NULL;
1438 writestruct(wd, ID_SO, "bSound", 1, sound);
1440 if (sound->newpackedfile) {
1441 pf = sound->newpackedfile;
1442 writestruct(wd, DATA, "PackedFile", 1, pf);
1443 writedata(wd, DATA, pf->size, pf->data);
1447 sound->newpackedfile = sample->packedfile;
1450 sound= sound->id.next;
1453 /* flush helps the compression for undo-save */
1454 mywrite(wd, MYWRITE_FLUSH, 0);
1457 static void write_groups(WriteData *wd, ListBase *idbase)
1464 group= idbase->first;
1466 if(group->id.us>0 || wd->current) {
1468 writestruct(wd, ID_GR, "Group", 1, group);
1470 gk= group->gkey.first;
1472 writestruct(wd, DATA, "GroupKey", 1, gk);
1476 go= group->gobject.first;
1478 writestruct(wd, DATA, "GroupObject", 1, go);
1481 go= group->gobject.first;
1485 writestruct(wd, DATA, "ObjectKey", 1, ok);
1492 group= group->id.next;
1496 static void write_global(WriteData *wd)
1500 fg.curscreen= G.curscreen;
1501 fg.curscene= G.scene;
1502 fg.displaymode= G.displaymode;
1503 fg.winpos= G.winpos;
1504 fg.fileflags= (G.fileflags & ~G_FILE_NO_UI); // prevent to save this, is not good convention, and feature with concerns...
1507 writestruct(wd, GLOB, "FileGlobal", 1, &fg);
1510 /* if *mem there's filesave to memory */
1511 static int write_file_handle(int handle, MemFile *compare, MemFile *current, int write_user_block, int write_flags)
1519 mainlist.first= mainlist.last= G.main;
1522 blo_split_main(&mainlist);
1524 wd= bgnwrite(handle, compare, current, write_flags);
1526 sprintf(buf, "BLENDER%c%c%.3d", (sizeof(void*)==8)?'-':'_', (G.order==B_ENDIAN)?'V':'v', G.version);
1527 mywrite(wd, buf, 12);
1529 write_renderinfo(wd);
1532 write_screens (wd, &G.main->screen); // no UI save
1533 write_scenes (wd, &G.main->scene);
1534 write_curves (wd, &G.main->curve);
1535 write_mballs (wd, &G.main->mball);
1536 write_images (wd, &G.main->image);
1537 write_cameras (wd, &G.main->camera);
1538 write_lamps (wd, &G.main->lamp);
1539 write_lattices (wd, &G.main->latt);
1540 write_ikas (wd, &G.main->ika);
1541 write_vfonts (wd, &G.main->vfont);
1542 write_ipos (wd, &G.main->ipo);
1543 write_keys (wd, &G.main->key);
1544 write_worlds (wd, &G.main->world);
1545 write_texts (wd, &G.main->text);
1546 write_sounds (wd, &G.main->sound);
1547 write_groups (wd, &G.main->group);
1548 write_armatures(wd, &G.main->armature);
1549 write_actions (wd, &G.main->action);
1550 write_objects (wd, &G.main->object);
1551 write_materials(wd, &G.main->mat);
1552 write_textures (wd, &G.main->tex);
1553 write_meshs (wd, &G.main->mesh);
1554 write_libraries(wd, G.main->next);
1557 if (write_user_block) {
1561 /* dna as last, because (to be implemented) test for which structs are written */
1562 writedata(wd, DNA1, wd->sdna->datalen, wd->sdna->data);
1565 memset(&bhead, 0, sizeof(BHead));
1567 mywrite(wd, &bhead, sizeof(BHead));
1569 blo_join_main(&mainlist);
1570 G.main= mainlist.first;
1572 return endwrite(wd);
1575 /* return: success (1) */
1576 int BLO_write_file(char *dir, int write_flags, char **error_r)
1578 char userfilename[FILE_MAXDIR+FILE_MAXFILE];
1579 char tempname[FILE_MAXDIR+FILE_MAXFILE];
1580 int file, fout, write_user_block;
1582 char tmpdir[FILE_MAXDIR+FILE_MAXFILE];
1585 sprintf(tempname, "%s@", dir);
1587 file = open(tempname,O_BINARY+O_WRONLY+O_CREAT+O_TRUNC, 0666);
1589 *error_r= "Unable to open";
1593 BLI_make_file_string(G.sce, userfilename, BLI_gethome(), ".B.blend");
1595 write_user_block= BLI_streq(dir, userfilename);
1597 fout= write_file_handle(file, NULL,NULL, write_user_block, write_flags);
1601 if(BLI_rename(tempname, dir) < 0) {
1602 *error_r= "Can't change old file. File saved with @";
1608 *error_r= "Not enough diskspace";
1615 /* return: success (1) */
1616 int BLO_write_file_mem(MemFile *compare, MemFile *current, int write_flags, char **error_r)
1620 err= write_file_handle(0, compare, current, 0, write_flags);
1622 if(err==0) return 1;
1627 /* Runtime writing */
1630 #define PATHSEPERATOR "\\"
1632 #define PATHSEPERATOR "/"
1635 static char *get_install_dir(void) {
1636 extern char bprogname[];
1637 char *tmpname = BLI_strdup(bprogname);
1641 cut = strstr(tmpname, ".app");
1642 if (cut) cut[0] = 0;
1645 cut = BLI_last_slash(tmpname);
1656 static char *get_runtime_path(char *exename) {
1657 char *installpath= get_install_dir();
1662 char *path= MEM_mallocN(strlen(installpath)+strlen(PATHSEPERATOR)+strlen(exename)+1, "runtimepath");
1663 strcpy(path, installpath);
1664 strcat(path, PATHSEPERATOR);
1665 strcat(path, exename);
1667 MEM_freeN(installpath);
1675 static int recursive_copy_runtime(char *outname, char *exename, char **cause_r)
1677 char *cause = NULL, *runtime = get_runtime_path(exename);
1678 char command[2 * (FILE_MAXDIR+FILE_MAXFILE) + 32];
1682 cause= "Unable to find runtime";
1685 //printf("runtimepath %s\n", runtime);
1687 progfd= open(runtime, O_BINARY|O_RDONLY, 0);
1689 cause= "Unable to find runtime";
1693 sprintf(command, "/bin/cp -R \"%s\" \"%s\"", runtime, outname);
1694 //printf("command %s\n", command);
1695 if (system(command) == -1) {
1696 cause = "Couldn't copy runtime";
1712 void BLO_write_runtime(char *file, char *exename) {
1713 char gamename[FILE_MAXDIR+FILE_MAXFILE];
1717 // remove existing file / bundle
1718 //printf("Delete file %s\n", file);
1719 BLI_delete(file, NULL, TRUE);
1721 if (!recursive_copy_runtime(file, exename, &cause))
1724 strcpy(gamename, file);
1725 strcat(gamename, "/Contents/Resources/game.blend");
1726 //printf("gamename %s\n", gamename);
1727 outfd= open(gamename, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, 0777);
1730 write_file_handle(outfd, NULL,NULL, 0, G.fileflags);
1732 if (write(outfd, " ", 1) != 1) {
1733 cause= "Unable to write to output file";
1737 cause = "Unable to open blenderfile";
1745 error("Unable to make runtime: %s", cause);
1748 #else /* !__APPLE__ */
1750 static int handle_append_runtime(int handle, char *exename, char **cause_r) {
1751 char *cause= NULL, *runtime= get_runtime_path(exename);
1752 unsigned char buf[1024];
1753 int count, progfd= -1;
1756 cause= "Unable to find runtime";
1760 progfd= open(runtime, O_BINARY|O_RDONLY, 0);
1762 cause= "Unable to find runtime";
1766 while ((count= read(progfd, buf, sizeof(buf)))>0) {
1767 if (write(handle, buf, count)!=count) {
1768 cause= "Unable to write to output file";
1786 static int handle_write_msb_int(int handle, int i) {
1787 unsigned char buf[4];
1788 buf[0]= (i>>24)&0xFF;
1789 buf[1]= (i>>16)&0xFF;
1790 buf[2]= (i>>8)&0xFF;
1791 buf[3]= (i>>0)&0xFF;
1793 return (write(handle, buf, 4)==4);
1796 void BLO_write_runtime(char *file, char *exename) {
1797 int outfd= open(file, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, 0777);
1802 cause= "Unable to open output file";
1805 if (!handle_append_runtime(outfd, exename, &cause))
1808 datastart= lseek(outfd, 0, SEEK_CUR);
1810 write_file_handle(outfd, NULL,NULL, 0, G.fileflags);
1812 if (!handle_write_msb_int(outfd, datastart) || (write(outfd, "BRUNTIME", 8)!=8)) {
1813 cause= "Unable to write to output file";
1822 error("Unable to make runtime: %s", cause);
1825 #endif /* !__APPLE__ */