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"
162 #include "readfile.h"
171 int tot, count, error;
174 static WriteData *writedata_new(int file)
176 extern char DNAstr[]; /* DNA.c */
179 WriteData *wd= MEM_callocN(sizeof(*wd), "writedata");
181 /* XXX, see note about this in readfile.c, remove
182 * once we have an xp lock - zr
184 wd->sdna= dna_sdna_from_data(DNAstr, DNAlen, 0);
188 wd->buf= MEM_mallocN(100000, "wd->buf");
193 static void writedata_do_write(WriteData *wd, void *mem, int memlen)
195 if (wd->error) return;
196 if (write(wd->file, mem, memlen) != memlen)
200 static void writedata_free(WriteData *wd)
202 dna_freestructDNA(wd->sdna);
213 * Low level WRITE(2) wrapper that buffers data
214 * @param adr Pointer to new chunk of data
215 * @param len Length of new chunk of data
216 * @warning Talks to other functions with global parameters
224 if (wd->error) return;
230 writedata_do_write(wd, wd->buf, wd->count);
233 writedata_do_write(wd, adr, len);
236 if(len+wd->count>99999) {
237 writedata_do_write(wd, wd->buf, wd->count);
240 memcpy(&wd->buf[wd->count], adr, len);
245 * BeGiN initializer for mywrite
246 * @param file File descriptor
247 * @param write_flags Write parameters
248 * @warning Talks to other functions with global parameters
255 WriteData *wd= writedata_new(file);
261 * END the mywrite wrapper
262 * @return 1 if write failed
263 * @return unknown global variable otherwise
264 * @warning Talks to other functions with global parameters
273 writedata_do_write(wd, wd->buf, wd->count);
283 /* ********** WRITE FILE ****************** */
285 static void writestruct(WriteData *wd, int filecode, char *structname, int nr, void *adr)
290 if(adr==0 || nr==0) return;
297 bh.SDNAnr= dna_findstruct_nr(wd->sdna, structname);
299 printf("error: can't find SDNA code %s\n", structname);
302 sp= wd->sdna->structs[bh.SDNAnr];
304 bh.len= nr*wd->sdna->typelens[sp[0]];
306 if(bh.len==0) return;
308 mywrite(wd, &bh, sizeof(BHead));
309 mywrite(wd, adr, bh.len);
312 static void writedata(WriteData *wd, int filecode, int len, void *adr) /* do not use for structs */
329 mywrite(wd, &bh, sizeof(BHead));
330 if(len) mywrite(wd, adr, len);
333 static void write_scriptlink(WriteData *wd, ScriptLink *slink)
335 writedata(wd, DATA, sizeof(void *)*slink->totscript, slink->scripts);
336 writedata(wd, DATA, sizeof(short)*slink->totscript, slink->flag);
339 static void write_renderinfo(WriteData *wd) /* for renderdaemon */
344 sce= G.main->scene.first;
346 if(sce->id.lib==0 && ( sce==G.scene || (sce->r.scemode & R_BG_RENDER)) ) {
347 data[0]= sce->r.sfra;
348 data[1]= sce->r.efra;
350 strncpy((char *)(data+2), sce->id.name+2, 23);
352 writedata(wd, REND, 32, data);
358 static void write_userdef(WriteData *wd)
362 writestruct(wd, USER, "UserDef", 1, &U);
364 btheme= U.themes.first;
366 writestruct(wd, DATA, "bTheme", 1, btheme);
367 btheme= btheme->next;
371 static void write_effects(WriteData *wd, ListBase *lb)
380 writestruct(wd, DATA, "BuildEff", 1, eff);
383 writestruct(wd, DATA, "PartEff", 1, eff);
386 writestruct(wd, DATA, "WaveEff", 1, eff);
389 writedata(wd, DATA, MEM_allocN_len(eff), eff);
396 static void write_properties(WriteData *wd, ListBase *lb)
402 writestruct(wd, DATA, "bProperty", 1, prop);
404 if(prop->poin && prop->poin != &prop->data)
405 writedata(wd, DATA, MEM_allocN_len(prop->poin), prop->poin);
411 static void write_sensors(WriteData *wd, ListBase *lb)
417 writestruct(wd, DATA, "bSensor", 1, sens);
419 writedata(wd, DATA, sizeof(void *)*sens->totlinks, sens->links);
423 writestruct(wd, DATA, "bNearSensor", 1, sens->data);
426 writestruct(wd, DATA, "bMouseSensor", 1, sens->data);
429 writestruct(wd, DATA, "bTouchSensor", 1, sens->data);
432 writestruct(wd, DATA, "bKeyboardSensor", 1, sens->data);
435 writestruct(wd, DATA, "bPropertySensor", 1, sens->data);
438 writestruct(wd, DATA, "bCollisionSensor", 1, sens->data);
441 writestruct(wd, DATA, "bRadarSensor", 1, sens->data);
444 writestruct(wd, DATA, "bRandomSensor", 1, sens->data);
447 writestruct(wd, DATA, "bRaySensor", 1, sens->data);
450 writestruct(wd, DATA, "bMessageSensor", 1, sens->data);
453 ; /* error: don't know how to write this file */
460 static void write_controllers(WriteData *wd, ListBase *lb)
466 writestruct(wd, DATA, "bController", 1, cont);
468 writedata(wd, DATA, sizeof(void *)*cont->totlinks, cont->links);
471 case CONT_EXPRESSION:
472 writestruct(wd, DATA, "bExpressionCont", 1, cont->data);
475 writestruct(wd, DATA, "bPythonCont", 1, cont->data);
478 ; /* error: don't know how to write this file */
485 static void write_actuators(WriteData *wd, ListBase *lb)
491 writestruct(wd, DATA, "bActuator", 1, act);
495 writestruct(wd, DATA, "bActionActuator", 1, act->data);
498 writestruct(wd, DATA, "bSoundActuator", 1, act->data);
501 writestruct(wd, DATA, "bCDActuator", 1, act->data);
504 writestruct(wd, DATA, "bObjectActuator", 1, act->data);
507 writestruct(wd, DATA, "bIpoActuator", 1, act->data);
510 writestruct(wd, DATA, "bPropertyActuator", 1, act->data);
513 writestruct(wd, DATA, "bCameraActuator", 1, act->data);
516 writestruct(wd, DATA, "bConstraintActuator", 1, act->data);
518 case ACT_EDIT_OBJECT:
519 writestruct(wd, DATA, "bEditObjectActuator", 1, act->data);
522 writestruct(wd, DATA, "bSceneActuator", 1, act->data);
525 writestruct(wd, DATA, "bGroupActuator", 1, act->data);
528 writestruct(wd, DATA, "bRandomActuator", 1, act->data);
531 writestruct(wd, DATA, "bMessageActuator", 1, act->data);
534 writestruct(wd, DATA, "bGameActuator", 1, act->data);
537 writestruct(wd, DATA, "bVisibilityActuator", 1, act->data);
540 ; /* error: don't know how to write this file */
547 static void write_nlastrips(WriteData *wd, ListBase *nlabase)
551 for (strip=nlabase->first; strip; strip=strip->next)
552 writestruct(wd, DATA, "bActionStrip", 1, strip);
555 static void write_constraints(WriteData *wd, ListBase *conlist)
559 for (con=conlist->first; con; con=con->next) {
560 /* Write the specific data */
562 case CONSTRAINT_TYPE_NULL:
564 case CONSTRAINT_TYPE_TRACKTO:
565 writestruct(wd, DATA, "bTrackToConstraint", 1, con->data);
567 case CONSTRAINT_TYPE_KINEMATIC:
568 writestruct(wd, DATA, "bKinematicConstraint", 1, con->data);
570 case CONSTRAINT_TYPE_ROTLIKE:
571 writestruct(wd, DATA, "bRotateLikeConstraint", 1, con->data);
573 case CONSTRAINT_TYPE_LOCLIKE:
574 writestruct(wd, DATA, "bLocateLikeConstraint", 1, con->data);
576 case CONSTRAINT_TYPE_ACTION:
577 writestruct(wd, DATA, "bActionConstraint", 1, con->data);
579 case CONSTRAINT_TYPE_LOCKTRACK:
580 writestruct(wd, DATA, "bLockTrackConstraint", 1, con->data);
582 case CONSTRAINT_TYPE_FOLLOWPATH:
583 writestruct(wd, DATA, "bFollowPathConstraint", 1, con->data);
588 /* Write the constraint */
589 writestruct(wd, DATA, "bConstraint", 1, con);
593 static void write_pose(WriteData *wd, bPose *pose)
597 /* Write each channel */
603 for (chan=pose->chanbase.first; chan; chan=chan->next) {
604 write_constraints(wd, &chan->constraints);
605 writestruct(wd, DATA, "bPoseChannel", 1, chan);
609 writestruct(wd, DATA, "bPose", 1, pose);
612 static void write_defgroups(WriteData *wd, ListBase *defbase)
614 bDeformGroup *defgroup;
616 for (defgroup=defbase->first; defgroup; defgroup=defgroup->next)
617 writestruct(wd, DATA, "bDeformGroup", 1, defgroup);
620 static void write_constraint_channels(WriteData *wd, ListBase *chanbase)
622 bConstraintChannel *chan;
624 for (chan = chanbase->first; chan; chan=chan->next)
625 writestruct(wd, DATA, "bConstraintChannel", 1, chan);
629 static void write_objects(WriteData *wd, ListBase *idbase)
637 writestruct(wd, ID_OB, "Object", 1, ob);
640 writedata(wd, DATA, sizeof(void *)*ob->totcol, ob->mat);
641 write_effects(wd, &ob->effect);
642 write_properties(wd, &ob->prop);
643 write_sensors(wd, &ob->sensors);
644 write_controllers(wd, &ob->controllers);
645 write_actuators(wd, &ob->actuators);
646 write_scriptlink(wd, &ob->scriptlink);
647 write_pose(wd, ob->pose);
648 write_defgroups(wd, &ob->defbase);
649 write_constraints(wd, &ob->constraints);
650 write_constraint_channels(wd, &ob->constraintChannels);
651 write_nlastrips(wd, &ob->nlastrips);
653 writestruct(wd, DATA, "PartDeflect", 1, ob->pd);
660 static void write_vfonts(WriteData *wd, ListBase *idbase)
669 writestruct(wd, ID_VF, "VFont", 1, vf);
673 if (vf->packedfile) {
675 writestruct(wd, DATA, "PackedFile", 1, pf);
676 writedata(wd, DATA, pf->size, pf->data);
684 static void write_ipos(WriteData *wd, ListBase *idbase)
693 writestruct(wd, ID_IP, "Ipo", 1, ipo);
696 icu= ipo->curve.first;
698 writestruct(wd, DATA, "IpoCurve", 1, icu);
702 icu= ipo->curve.first;
704 if(icu->bezt) writestruct(wd, DATA, "BezTriple", icu->totvert, icu->bezt);
705 if(icu->bp) writestruct(wd, DATA, "BPoint", icu->totvert, icu->bp);
714 static void write_keys(WriteData *wd, ListBase *idbase)
723 writestruct(wd, ID_KE, "Key", 1, key);
726 kb= key->block.first;
728 writestruct(wd, DATA, "KeyBlock", 1, kb);
729 if(kb->data) writedata(wd, DATA, kb->totelem*key->elemsize, kb->data);
738 static void write_cameras(WriteData *wd, ListBase *idbase)
746 writestruct(wd, ID_CA, "Camera", 1, cam);
749 write_scriptlink(wd, &cam->scriptlink);
756 static void write_mballs(WriteData *wd, ListBase *idbase)
765 writestruct(wd, ID_MB, "MetaBall", 1, mb);
768 writedata(wd, DATA, sizeof(void *)*mb->totcol, mb->mat);
772 writestruct(wd, DATA, "MetaElem", 1, ml);
780 static void write_curves(WriteData *wd, ListBase *idbase)
789 writestruct(wd, ID_CU, "Curve", 1, cu);
792 writedata(wd, DATA, sizeof(void *)*cu->totcol, cu->mat);
795 writedata(wd, DATA, cu->len+1, cu->str);
798 /* is also the order of reading */
801 writestruct(wd, DATA, "Nurb", 1, nu);
806 if( (nu->type & 7)==CU_BEZIER)
807 writestruct(wd, DATA, "BezTriple", nu->pntsu, nu->bezt);
809 writestruct(wd, DATA, "BPoint", nu->pntsu*nu->pntsv, nu->bp);
810 if(nu->knotsu) writedata(wd, DATA, KNOTSU(nu)*sizeof(float), nu->knotsu);
811 if(nu->knotsv) writedata(wd, DATA, KNOTSV(nu)*sizeof(float), nu->knotsv);
821 static void write_dverts(WriteData *wd, int count, MDeformVert *dvlist)
825 /* Write the dvert list */
826 writestruct(wd, DATA, "MDeformVert", count, dvlist);
828 /* Write deformation data for each dvert */
830 for (i=0; i<count; i++) {
832 writestruct(wd, DATA, "MDeformWeight", dvlist[i].totweight, dvlist[i].dw);
837 static void write_meshs(WriteData *wd, ListBase *idbase)
845 writestruct(wd, ID_ME, "Mesh", 1, mesh);
848 writedata(wd, DATA, sizeof(void *)*mesh->totcol, mesh->mat);
849 writestruct(wd, DATA, "MVert", mesh->totvert, mesh->mvert);
850 write_dverts(wd, mesh->totvert, mesh->dvert);
851 writestruct(wd, DATA, "MFace", mesh->totface, mesh->mface);
852 writestruct(wd, DATA, "TFace", mesh->totface, mesh->tface);
853 writestruct(wd, DATA, "MCol", 4*mesh->totface, mesh->mcol);
854 writestruct(wd, DATA, "MSticky", mesh->totvert, mesh->msticky);
861 static void write_images(WriteData *wd, ListBase *idbase)
870 writestruct(wd, ID_IM, "Image", 1, ima);
872 if (ima->packedfile) {
873 pf = ima->packedfile;
874 writestruct(wd, DATA, "PackedFile", 1, pf);
875 writedata(wd, DATA, pf->size, pf->data);
882 static void write_textures(WriteData *wd, ListBase *idbase)
890 writestruct(wd, ID_TE, "Tex", 1, tex);
893 if(tex->plugin) writestruct(wd, DATA, "PluginTex", 1, tex->plugin);
894 if(tex->coba) writestruct(wd, DATA, "ColorBand", 1, tex->coba);
895 if(tex->env) writestruct(wd, DATA, "EnvMap", 1, tex->env);
901 static void write_materials(WriteData *wd, ListBase *idbase)
910 writestruct(wd, ID_MA, "Material", 1, ma);
913 if(ma->mtex[a]) writestruct(wd, DATA, "MTex", 1, ma->mtex[a]);
916 write_scriptlink(wd, &ma->scriptlink);
922 static void write_worlds(WriteData *wd, ListBase *idbase)
931 writestruct(wd, ID_WO, "World", 1, wrld);
934 if(wrld->mtex[a]) writestruct(wd, DATA, "MTex", 1, wrld->mtex[a]);
937 write_scriptlink(wd, &wrld->scriptlink);
943 static void write_lamps(WriteData *wd, ListBase *idbase)
952 writestruct(wd, ID_LA, "Lamp", 1, la);
956 if(la->mtex[a]) writestruct(wd, DATA, "MTex", 1, la->mtex[a]);
959 write_scriptlink(wd, &la->scriptlink);
965 static void write_lattices(WriteData *wd, ListBase *idbase)
973 writestruct(wd, ID_LT, "Lattice", 1, lt);
976 writestruct(wd, DATA, "BPoint", lt->pntsu*lt->pntsv*lt->pntsw, lt->def);
982 static void write_ikas(WriteData *wd, ListBase *idbase)
991 writestruct(wd, ID_IK, "Ika", 1, ika);
994 li= ika->limbbase.first;
996 writestruct(wd, DATA, "Limb", 1, li);
1000 writestruct(wd, DATA, "Deform", ika->totdef, ika->def);
1006 static void write_scenes(WriteData *wd, ListBase *scebase)
1014 sce= scebase->first;
1017 writestruct(wd, ID_SCE, "Scene", 1, sce);
1020 base= sce->base.first;
1022 writestruct(wd, DATA, "Base", 1, base);
1026 writestruct(wd, DATA, "Radio", 1, sce->radio);
1027 writestruct(wd, DATA, "FreeCamera", 1, sce->fcam);
1031 writestruct(wd, DATA, "Editing", 1, ed);
1033 /* reset write flags too */
1034 WHILE_SEQ(&ed->seqbase) {
1035 if(seq->strip) seq->strip->done= 0;
1036 writestruct(wd, DATA, "Sequence", 1, seq);
1040 WHILE_SEQ(&ed->seqbase) {
1041 if(seq->strip && seq->strip->done==0) {
1042 /* write strip with 'done' at 0 because readfile */
1044 if(seq->plugin) writestruct(wd, DATA, "PluginSeq", 1, seq->plugin);
1045 if(seq->effectdata) {
1048 writestruct(wd, DATA, "SweepVars", 1, seq->effectdata);
1051 writestruct(wd, DATA, "GlowVars", 1, seq->effectdata);
1057 writestruct(wd, DATA, "Strip", 1, strip);
1059 if(seq->type==SEQ_IMAGE)
1060 writestruct(wd, DATA, "StripElem", strip->len, strip->stripdata);
1061 else if(seq->type==SEQ_MOVIE || seq->type==SEQ_SOUND)
1062 writestruct(wd, DATA, "StripElem", 1, strip->stripdata);
1070 write_scriptlink(wd, &sce->scriptlink);
1072 if (sce->r.avicodecdata) {
1073 writestruct(wd, DATA, "AviCodecData", 1, sce->r.avicodecdata);
1074 if (sce->r.avicodecdata->lpFormat) writedata(wd, DATA, sce->r.avicodecdata->cbFormat, sce->r.avicodecdata->lpFormat);
1075 if (sce->r.avicodecdata->lpParms) writedata(wd, DATA, sce->r.avicodecdata->cbParms, sce->r.avicodecdata->lpParms);
1078 if (sce->r.qtcodecdata) {
1079 writestruct(wd, DATA, "QuicktimeCodecData", 1, sce->r.qtcodecdata);
1080 if (sce->r.qtcodecdata->cdParms) writedata(wd, DATA, sce->r.qtcodecdata->cdSize, sce->r.qtcodecdata->cdParms);
1087 static void write_screens(WriteData *wd, ListBase *scrbase)
1097 writestruct(wd, ID_SCR, "Screen", 1, sc);
1100 sv= sc->vertbase.first;
1102 writestruct(wd, DATA, "ScrVert", 1, sv);
1106 se= sc->edgebase.first;
1108 writestruct(wd, DATA, "ScrEdge", 1, se);
1112 sa= sc->areabase.first;
1117 writestruct(wd, DATA, "ScrArea", 1, sa);
1119 pa= sa->panels.first;
1121 writestruct(wd, DATA, "Panel", 1, pa);
1125 sl= sa->spacedata.first;
1127 if(sl->spacetype==SPACE_VIEW3D) {
1128 View3D *v3d= (View3D*) sl;
1129 writestruct(wd, DATA, "View3D", 1, v3d);
1130 if(v3d->bgpic) writestruct(wd, DATA, "BGpic", 1, v3d->bgpic);
1131 if(v3d->localvd) writestruct(wd, DATA, "View3D", 1, v3d->localvd);
1133 else if(sl->spacetype==SPACE_IPO) {
1134 writestruct(wd, DATA, "SpaceIpo", 1, sl);
1136 else if(sl->spacetype==SPACE_BUTS) {
1137 writestruct(wd, DATA, "SpaceButs", 1, sl);
1139 else if(sl->spacetype==SPACE_FILE) {
1140 writestruct(wd, DATA, "SpaceFile", 1, sl);
1142 else if(sl->spacetype==SPACE_SEQ) {
1143 writestruct(wd, DATA, "SpaceSeq", 1, sl);
1145 else if(sl->spacetype==SPACE_OOPS) {
1146 SpaceOops *so= (SpaceOops *)sl;
1150 oops= so->oops.first;
1152 Oops *oopsn= oops->next;
1154 BLI_remlink(&so->oops, oops);
1160 /* ater cleanup, because of listbase! */
1161 writestruct(wd, DATA, "SpaceOops", 1, so);
1163 oops= so->oops.first;
1165 writestruct(wd, DATA, "Oops", 1, oops);
1169 else if(sl->spacetype==SPACE_IMAGE) {
1170 writestruct(wd, DATA, "SpaceImage", 1, sl);
1172 else if(sl->spacetype==SPACE_IMASEL) {
1173 writestruct(wd, DATA, "SpaceImaSel", 1, sl);
1175 else if(sl->spacetype==SPACE_TEXT) {
1176 writestruct(wd, DATA, "SpaceText", 1, sl);
1178 else if(sl->spacetype==SPACE_SCRIPT) {
1179 writestruct(wd, DATA, "SpaceScript", 1, sl);
1181 else if(sl->spacetype==SPACE_ACTION) {
1182 writestruct(wd, DATA, "SpaceAction", 1, sl);
1184 else if(sl->spacetype==SPACE_SOUND) {
1185 writestruct(wd, DATA, "SpaceSound", 1, sl);
1187 else if(sl->spacetype==SPACE_NLA){
1188 writestruct(wd, DATA, "SpaceNla", 1, sl);
1200 static void write_libraries(WriteData *wd, Main *main)
1202 ListBase *lbarray[30];
1204 int a, tot, foundone;
1208 a=tot= set_listbasepointers(main, lbarray);
1210 /* test: is lib being used */
1213 id= lbarray[tot]->first;
1215 if(id->us>0 && (id->flag & LIB_EXTERN)) {
1225 writestruct(wd, ID_LI, "Library", 1, main->curlib);
1228 id= lbarray[a]->first;
1230 if(id->us>0 && (id->flag & LIB_EXTERN)) {
1232 writestruct(wd, ID_ID, "ID", 1, id);
1243 static void write_bone(WriteData *wd, Bone* bone)
1247 // write_constraints(wd, &bone->constraints);
1250 writestruct(wd, DATA, "Bone", 1, bone);
1253 cbone= bone->childbase.first;
1255 write_bone(wd, cbone);
1260 static void write_armatures(WriteData *wd, ListBase *idbase)
1268 writestruct(wd, ID_AR, "bArmature", 1, arm);
1271 bone= arm->bonebase.first;
1273 write_bone(wd, bone);
1281 static void write_actions(WriteData *wd, ListBase *idbase)
1284 bActionChannel *chan;
1288 writestruct(wd, ID_AC, "bAction", 1, act);
1290 for (chan=act->chanbase.first; chan; chan=chan->next) {
1291 writestruct(wd, DATA, "bActionChannel", 1, chan);
1292 write_constraint_channels(wd, &chan->constraintChannels);
1299 static void write_texts(WriteData *wd, ListBase *idbase)
1304 text= idbase->first;
1306 if ( (text->flags & TXT_ISMEM) && (text->flags & TXT_ISEXT)) text->flags &= ~TXT_ISEXT;
1309 writestruct(wd, ID_TXT, "Text", 1, text);
1310 if(text->name) writedata(wd, DATA, strlen(text->name)+1, text->name);
1312 if(!(text->flags & TXT_ISEXT)) {
1313 /* now write the text data, in two steps for optimization in the readfunction */
1314 tmp= text->lines.first;
1316 writestruct(wd, DATA, "TextLine", 1, tmp);
1320 tmp= text->lines.first;
1322 writedata(wd, DATA, tmp->len+1, tmp->line);
1326 text= text->id.next;
1330 static void write_sounds(WriteData *wd, ListBase *idbase)
1337 // set all samples to unsaved status
1339 sample = samples->first;
1341 sample->flags |= SAMPLE_NEEDS_SAVE;
1342 sample = sample->id.next;
1345 sound= idbase->first;
1347 if(sound->id.us>0) {
1348 // do we need to save the packedfile as well ?
1349 sample = sound->sample;
1351 if (sample->flags & SAMPLE_NEEDS_SAVE) {
1352 sound->newpackedfile = sample->packedfile;
1353 sample->flags &= ~SAMPLE_NEEDS_SAVE;
1355 sound->newpackedfile = NULL;
1360 writestruct(wd, ID_SO, "bSound", 1, sound);
1362 if (sound->newpackedfile) {
1363 pf = sound->newpackedfile;
1364 writestruct(wd, DATA, "PackedFile", 1, pf);
1365 writedata(wd, DATA, pf->size, pf->data);
1369 sound->newpackedfile = sample->packedfile;
1372 sound= sound->id.next;
1376 static void write_groups(WriteData *wd, ListBase *idbase)
1383 group= idbase->first;
1385 if(group->id.us>0) {
1387 writestruct(wd, ID_GR, "Group", 1, group);
1389 gk= group->gkey.first;
1391 writestruct(wd, DATA, "GroupKey", 1, gk);
1395 go= group->gobject.first;
1397 writestruct(wd, DATA, "GroupObject", 1, go);
1400 go= group->gobject.first;
1404 writestruct(wd, DATA, "ObjectKey", 1, ok);
1411 group= group->id.next;
1415 static void write_global(WriteData *wd)
1419 fg.curscreen= G.curscreen;
1420 fg.displaymode= R.displaymode;
1421 fg.winpos= R.winpos;
1422 fg.fileflags= G.fileflags;
1425 writestruct(wd, GLOB, "FileGlobal", 1, &fg);
1428 static int write_file_handle(int handle, int write_user_block, int write_flags)
1435 mainlist.first= mainlist.last= G.main;
1438 blo_split_main(&mainlist);
1440 wd= bgnwrite(handle, write_flags);
1442 sprintf(buf, "BLENDER%c%c%.3d", (sizeof(void*)==8)?'-':'_', (G.order==B_ENDIAN)?'V':'v', G.version);
1443 mywrite(wd, buf, 12);
1445 write_renderinfo(wd);
1447 write_screens (wd, &G.main->screen);
1448 write_scenes (wd, &G.main->scene);
1449 write_objects (wd, &G.main->object);
1450 write_meshs (wd, &G.main->mesh);
1451 write_curves (wd, &G.main->curve);
1452 write_mballs (wd, &G.main->mball);
1453 write_materials(wd, &G.main->mat);
1454 write_textures (wd, &G.main->tex);
1455 write_images (wd, &G.main->image);
1456 write_cameras (wd, &G.main->camera);
1457 write_lamps (wd, &G.main->lamp);
1458 write_lattices (wd, &G.main->latt);
1459 write_ikas (wd, &G.main->ika);
1460 write_vfonts (wd, &G.main->vfont);
1461 write_ipos (wd, &G.main->ipo);
1462 write_keys (wd, &G.main->key);
1463 write_worlds (wd, &G.main->world);
1464 write_texts (wd, &G.main->text);
1465 write_sounds (wd, &G.main->sound);
1466 write_groups (wd, &G.main->group);
1467 write_armatures(wd, &G.main->armature);
1468 write_actions (wd, &G.main->action);
1469 write_libraries(wd, G.main->next);
1472 if (write_user_block) {
1476 /* dna as last, because (to be implemented) test for which structs are written */
1477 writedata(wd, DNA1, wd->sdna->datalen, wd->sdna->data);
1480 mywrite(wd, &data, 4);
1483 mywrite(wd, &data, 4);
1485 blo_join_main(&mainlist);
1486 G.main= mainlist.first;
1488 return endwrite(wd);
1491 int BLO_write_file(char *dir, int write_flags, char **error_r)
1493 char userfilename[FILE_MAXDIR+FILE_MAXFILE];
1494 char tempname[FILE_MAXDIR+FILE_MAXFILE];
1495 int file, fout, write_user_block;
1497 char tmpdir[FILE_MAXDIR+FILE_MAXFILE];
1500 sprintf(tempname, "%s@", dir);
1502 file = open(tempname,O_BINARY+O_WRONLY+O_CREAT+O_TRUNC, 0666);
1504 *error_r= "Unable to open";
1508 BLI_make_file_string(G.sce, userfilename, BLI_gethome(), ".B.blend");
1510 write_user_block= BLI_streq(dir, userfilename);
1512 fout= write_file_handle(file, write_user_block, write_flags);
1516 if(BLI_rename(tempname, dir) < 0) {
1517 *error_r= "Can't change old file. File saved with @";
1523 *error_r= "Not enough diskspace";
1530 /* Runtime writing */
1533 #define PATHSEPERATOR "\\"
1535 #define PATHSEPERATOR "/"
1538 static char *get_install_dir(void) {
1539 extern char bprogname[];
1540 char *tmpname = BLI_strdup(bprogname);
1544 cut = strstr(tmpname, ".app");
1545 if (cut) cut[0] = 0;
1548 cut = BLI_last_slash(tmpname);
1559 static char *get_runtime_path(char *exename) {
1560 char *installpath= get_install_dir();
1565 char *path= MEM_mallocN(strlen(installpath)+strlen(PATHSEPERATOR)+strlen(exename)+1, "runtimepath");
1566 strcpy(path, installpath);
1567 strcat(path, PATHSEPERATOR);
1568 strcat(path, exename);
1570 MEM_freeN(installpath);
1578 static int recursive_copy_runtime(char *outname, char *exename, char **cause_r) {
1579 char *cause = NULL, *runtime = get_runtime_path(exename);
1580 char command[2 * (FILE_MAXDIR+FILE_MAXFILE) + 32];
1584 cause= "Unable to find runtime";
1588 progfd= open(runtime, O_BINARY|O_RDONLY, 0);
1590 cause= "Unable to find runtime";
1594 sprintf(command, "/bin/cp -R %s %s", runtime, outname);
1595 if (system(command) == -1) {
1596 cause = "Couldn't copy runtime";
1612 void BLO_write_runtime(char *file, char *exename) {
1613 char gamename[FILE_MAXDIR+FILE_MAXFILE];
1617 // remove existing file / bundle
1618 BLI_delete(file, NULL, TRUE);
1620 if (!recursive_copy_runtime(file, exename, &cause))
1623 strcpy(gamename, file);
1624 strcat(gamename, "/Contents/Resources/game.blend");
1626 outfd= open(gamename, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, 0777);
1629 write_file_handle(outfd, 0, G.fileflags);
1631 if (write(outfd, " ", 1) != 1) {
1632 cause= "Unable to write to output file";
1636 cause = "Unable to open blenderfile";
1644 error("Unable to make runtime: %s", cause);
1647 #else /* !__APPLE__ */
1649 static int handle_append_runtime(int handle, char *exename, char **cause_r) {
1650 char *cause= NULL, *runtime= get_runtime_path(exename);
1651 unsigned char buf[1024];
1652 int count, progfd= -1;
1655 cause= "Unable to find runtime";
1659 progfd= open(runtime, O_BINARY|O_RDONLY, 0);
1661 cause= "Unable to find runtime";
1665 while ((count= read(progfd, buf, sizeof(buf)))>0) {
1666 if (write(handle, buf, count)!=count) {
1667 cause= "Unable to write to output file";
1685 static int handle_write_msb_int(int handle, int i) {
1686 unsigned char buf[4];
1687 buf[0]= (i>>24)&0xFF;
1688 buf[1]= (i>>16)&0xFF;
1689 buf[2]= (i>>8)&0xFF;
1690 buf[3]= (i>>0)&0xFF;
1692 return (write(handle, buf, 4)==4);
1695 void BLO_write_runtime(char *file, char *exename) {
1696 int outfd= open(file, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, 0777);
1701 cause= "Unable to open output file";
1704 if (!handle_append_runtime(outfd, exename, &cause))
1707 datastart= lseek(outfd, 0, SEEK_CUR);
1709 write_file_handle(outfd, 0, G.fileflags);
1711 if (!handle_write_msb_int(outfd, datastart) || (write(outfd, "BRUNTIME", 8)!=8)) {
1712 cause= "Unable to write to output file";
1721 error("Unable to make runtime: %s", cause);
1724 #endif /* !__APPLE__ */