static void S3O_SaveObject (FILE *f, MdlObject *obj) { int startpos = ftell(f); S3OPiece piece; size_t write_result; memset(&piece, 0, sizeof(piece)); fseek (f, sizeof(S3OPiece), SEEK_CUR); piece.name = ftell(f); WriteZStr(f, obj->name); piece.xoffset = obj->position.x; piece.yoffset = obj->position.y; piece.zoffset = obj->position.z; piece.collisionData = 0; piece.vertexType = 0; PolyMesh* pm = obj->geometry ? obj->geometry->ToPolyMesh() : 0; if (pm) { S3O_WritePrimitives (&piece, f, pm); piece.numVertices = (uint) pm->verts.size(); piece.vertices = ftell(f); for (unsigned int a=0;a<pm->verts.size();a++) { Vertex *myVert=&pm->verts[a]; S3OVertex v; v.texu=myVert->tc[0].x; v.texv=myVert->tc[0].y; v.xnormal=myVert->normal.x; v.ynormal=myVert->normal.y; v.znormal=myVert->normal.z; v.xpos=myVert->pos.x; v.ypos=myVert->pos.y; v.zpos=myVert->pos.z; write_result = fwrite(&v,sizeof(S3OVertex),1,f); if (write_result != (size_t)1) throw std::runtime_error ("Couldn't write vertex."); } delete pm; } piece.numChilds = (uint)obj->childs.size(); ulong *childpos=new ulong[piece.numChilds]; for (unsigned int a=0;a<obj->childs.size();a++) { childpos[a] = ftell(f); S3O_SaveObject (f,obj->childs[a]); } piece.childs=ftell(f); write_result = fwrite (childpos,4,piece.numChilds,f); if (write_result != (size_t)(piece.numChilds)) throw std::runtime_error ("Couldn't write child."); delete[] childpos; int endpos=ftell(f); fseek (f,startpos,SEEK_SET); write_result = fwrite (&piece,sizeof(S3OPiece),1,f); if (write_result != (size_t)1) throw std::runtime_error ("Couldn't write piece."); fseek (f,endpos,SEEK_SET); }
bool Model::SaveS3O(const char *filename, IProgressCtl& /*progctl*/) { S3OHeader header; size_t write_result; memset (&header,0,sizeof(S3OHeader)); memcpy (header.magic, S3O_ID, 12); if (!root) return false; FILE *f = fopen (filename, "wb"); if (!f) return false; fseek (f, sizeof(S3OHeader), SEEK_SET); header.rootPiece = ftell(f); if (root) { MdlObject *cloned = root->Clone(); IterateObjects (cloned, ApplyOrientationAndScaling); MirrorX(cloned); S3O_SaveObject (f, cloned); delete cloned; } for (uint tex=0;tex<texBindings.size();tex++) { if (tex >= texBindings.size ()) break; TextureBinding &tb = texBindings[tex]; if (!tb.name.empty()) { if (tex==0) header.texture1 = ftell(f); if (tex==1) header.texture2 = ftell(f); WriteZStr (f, tb.name); } } header.radius = radius; header.height = height; header.midx = -mid.x; header.midy = mid.y; header.midz = mid.z; fseek (f, 0, SEEK_SET); write_result = fwrite (&header, sizeof(S3OHeader), 1, f); if (write_result != (size_t)1) throw std::runtime_error ("Couldn't write S3O header."); fclose (f); return true; }
void BuildTable::SaveCache (const char *fn) { FILE *f; f = fopen (fn, "wb"); if (!f) { logPrintf ("BuildTable::SaveCache(): Can't open %s for saving\n", fn); return; } fputc (BT_CACHE_VERSION, f); bool err=false; do { if (fwrite (&numDefs, sizeof(int), 1, f) != 1) { err=true; break; } for (int a=0;a<numDefs;a++) { if (!WriteZStr (f, deflist[a].name)) { err=true; break; } UDef *d = &deflist[a]; fwrite (&d->cost, sizeof(ResourceInfo), 1, f); fwrite (&d->make, sizeof(ResourceInfo), 1, f); fwrite (&d->storage, sizeof(ResourceInfo), 1, f); fwrite (&d->buildTime, sizeof(float),1 , f); fwrite (&d->metalExtractDepth, sizeof(float), 1, f); fwrite (&d->energyUse, sizeof(float), 1,f); fwrite (&d->buildSpeed, sizeof(float), 1,f); fwrite (&d->weaponDamage, sizeof(float),1,f); fwrite (&d->weaponRange, sizeof(float),1,f); fwrite (&d->flags, sizeof(ulong),1,f); fwrite (&d->numBuildOptions, sizeof(int),1,f); } if (err) break; for (int a=0;a<numDefs;a++) if (deflist[a].IsBuilder()) { if (fwrite (&table.data[numDefs*a], sizeof(Table::ent), numDefs, f) != numDefs) { err=true; break; } } // store the buildby table for (int a=0;a<numDefs;a++) { vector<int>& v=buildby[a]; short size = v.size(); if (fwrite (&size, sizeof(short), 1, f) != 1 || fwrite (&v[0], sizeof(int), v.size(), f) != v.size()) { err=true; break; } } if(fwrite (buildAssistTypes, sizeof(int), NUM_BUILD_ASSIST, f) != NUM_BUILD_ASSIST) err=true; } while(0); if (err) logPrintf ("ERROR: Failed to write to file %s\n", fn); fclose (f); }
static void save_object(FILE *f, MdlObject *parent, std::vector<MdlObject*>::iterator cur) { TA_Object n; MdlObject *obj = *cur++; memset (&n, 0, sizeof(TA_Object)); int header = ftell(f); fseek (f, sizeof(TA_Object), SEEK_CUR); n.VersionSignature = 1; n.NumberOfPrimitives = obj->poly.size(); n.XFromParent = TO_TA(obj->position.v[0]); n.YFromParent = TO_TA(obj->position.v[1]); n.ZFromParent = TO_TA(obj->position.v[2]); n.NumberOfVertexes = obj->verts.size(); n.OffsetToObjectName = ftell(f); WriteZStr (f, obj->name); n.OffsetToVertexArray = ftell(f); for (int a=0;a<obj->verts.size();a++) { long v[3]; Vector3 *p = &obj->verts[a].pos; for (int i=0;i<3;i++) v[i] = TO_TA(p->v[i]); fwrite (v, sizeof(long), 3, f); } n.OffsetToPrimitiveArray = ftell(f); TA_Polygon *tapl = new TA_Polygon [obj->poly.size()]; fseek (f, sizeof(TA_Polygon) * obj->poly.size(), SEEK_CUR); memset (tapl,0,sizeof(TA_Polygon)*obj->poly.size()); for (int a=0;a<obj->poly.size();a++) { Poly *pl = obj->poly [a]; if (pl->taColor >= 0) tapl[a].PaletteIndex = pl->taColor; else tapl[a].PaletteIndex = palette.FindIndex(pl->color); tapl[a].TexnameOfs = ftell(f); WriteZStr (f,pl->texname); tapl[a].VertOfs = ftell(f); for (int b=0;b<pl->verts.size();b++) { unsigned short v; v = pl->verts[b]; fwrite (&v, sizeof(short), 1, f); } tapl[a].VertNum = pl->verts.size(); tapl[a].Unknown_1 = 0; tapl[a].Unknown_2 = 0; tapl[a].Unknown_3 = 0; } int old = ftell(f); fseek (f, n.OffsetToPrimitiveArray, SEEK_SET); fwrite (tapl, sizeof(TA_Polygon), obj->poly.size(),f); fseek (f, old, SEEK_SET); if (parent) { if (cur != parent->childs.end()) { n.OffsetToSiblingObject = ftell(f); save_object (f, parent, cur); } } if (obj->childs.size()) { n.OffsetToChildObject = ftell(f); save_object (f, obj, obj->childs.begin()); } old=ftell(f); fseek (f, header, SEEK_SET); fwrite (&n, sizeof(TA_Object), 1, f); fseek (f,old, SEEK_SET); }