void SaveWadincludeFile(const char* const filename) { char* fname; FILE* file; int x; unsigned len = strlen(filename) + 5; fname = (char*)Alloc(len); safe_snprintf(fname, len, "%s.wic", filename); _unlink(fname); file = SafeOpenWrite(fname); WadInclude_i it; for (it = g_WadInclude.begin(); it != g_WadInclude.end(); it++) { x = it->size(); if (x) { SafeWrite(file, it->c_str(), x); SafeWrite(file, ";", 1); } } Free(fname); fclose(file); }
/* ================ WriteBrushList ================ */ void WriteBrushList(char *name, bspBrush_t * brush, qboolean onlyvis) { int i; side_t *s; FILE *f; Sys_FPrintf(SYS_VRB, "writing %s\n", name); f = SafeOpenWrite(name); for(; brush; brush = brush->next) { for(i = 0; i < brush->numsides; i++) { s = &brush->sides[i]; if(!s->winding) continue; if(onlyvis && !s->visible) continue; OutputWinding(brush->sides[i].winding, f); } } fclose(f); }
/* ============= WriteBSPFile Swaps the bsp file in place, so it should not be referenced again ============= */ void WriteBSPFile (char *filename) { header = &outheader; memset (header, 0, sizeof(dheader_t)); SwapBSPFile (true); header->version = LittleLong (BSPVERSION); wadfile = SafeOpenWrite (filename); SafeWrite (wadfile, header, sizeof(dheader_t)); // overwritten later AddLump (LUMP_PLANES, dplanes, numplanes*sizeof(dplane_t)); AddLump (LUMP_LEAFS, dleafs, numleafs*sizeof(dleaf_t)); AddLump (LUMP_VERTEXES, dvertexes, numvertexes*sizeof(dvertex_t)); AddLump (LUMP_NODES, dnodes, numnodes*sizeof(dnode_t)); AddLump (LUMP_TEXINFO, texinfo, numtexinfo*sizeof(texinfo_t)); AddLump (LUMP_FACES, dfaces, numfaces*sizeof(dface_t)); AddLump (LUMP_CLIPNODES, dclipnodes, numclipnodes*sizeof(dclipnode_t)); AddLump (LUMP_MARKSURFACES, dmarksurfaces, nummarksurfaces*sizeof(dmarksurfaces[0])); AddLump (LUMP_SURFEDGES, dsurfedges, numsurfedges*sizeof(dsurfedges[0])); AddLump (LUMP_EDGES, dedges, numedges*sizeof(dedge_t)); AddLump (LUMP_MODELS, dmodels, nummodels*sizeof(dmodel_t)); AddLump (LUMP_LIGHTING, dlightdata, lightdatasize); AddLump (LUMP_VISIBILITY, dvisdata, visdatasize); AddLump (LUMP_ENTITIES, dentdata, entdatasize); AddLump (LUMP_TEXTURES, dtexdata, texdatasize); fseek (wadfile, 0, SEEK_SET); SafeWrite (wadfile, header, sizeof(dheader_t)); fclose (wadfile); }
/* ============= HL_WriteBSPFile Swaps the bsp file in place, so it should not be referenced again ============= */ void HL_WriteBSPFile (char *filename) { hl_header = &outheader; memset (hl_header, 0, sizeof(hl_dheader_t)); HL_SwapBSPFile (true); hl_header->version = LittleLong (HL_BSPVERSION); wadfile = SafeOpenWrite (filename); SafeWrite (wadfile, hl_header, sizeof(hl_dheader_t)); // overwritten later HL_AddLump (HL_LUMP_PLANES, hl_dplanes, hl_numplanes*sizeof(hl_dplane_t)); HL_AddLump (HL_LUMP_LEAFS, hl_dleafs, hl_numleafs*sizeof(hl_dleaf_t)); HL_AddLump (HL_LUMP_VERTEXES, hl_dvertexes, hl_numvertexes*sizeof(hl_dvertex_t)); HL_AddLump (HL_LUMP_NODES, hl_dnodes, hl_numnodes*sizeof(hl_dnode_t)); HL_AddLump (HL_LUMP_TEXINFO, hl_texinfo, hl_numtexinfo*sizeof(hl_texinfo_t)); HL_AddLump (HL_LUMP_FACES, hl_dfaces, hl_numfaces*sizeof(hl_dface_t)); HL_AddLump (HL_LUMP_CLIPNODES, hl_dclipnodes, hl_numclipnodes*sizeof(hl_dclipnode_t)); HL_AddLump (HL_LUMP_MARKSURFACES, hl_dmarksurfaces, hl_nummarksurfaces*sizeof(hl_dmarksurfaces[0])); HL_AddLump (HL_LUMP_SURFEDGES, hl_dsurfedges, hl_numsurfedges*sizeof(hl_dsurfedges[0])); HL_AddLump (HL_LUMP_EDGES, hl_dedges, hl_numedges*sizeof(hl_dedge_t)); HL_AddLump (HL_LUMP_MODELS, hl_dmodels, hl_nummodels*sizeof(hl_dmodel_t)); HL_AddLump (HL_LUMP_LIGHTING, hl_dlightdata, hl_lightdatasize); HL_AddLump (HL_LUMP_VISIBILITY, hl_dvisdata, hl_visdatasize); HL_AddLump (HL_LUMP_ENTITIES, hl_dentdata, hl_entdatasize); HL_AddLump (HL_LUMP_TEXTURES, hl_dtexdata, hl_texdatasize); fseek (wadfile, 0, SEEK_SET); SafeWrite (wadfile, hl_header, sizeof(hl_dheader_t)); fclose (wadfile); }
//=========================================================================== // // Parameter: - // Returns: - // Changes Globals: - //=========================================================================== void WriteBrushList( char *name, bspbrush_t *brush, qboolean onlyvis ) { int i; side_t *s; FILE *f; qprintf( "writing %s\n", name ); f = SafeOpenWrite( name ); for ( ; brush ; brush = brush->next ) { for ( i = 0 ; i < brush->numsides ; i++ ) { s = &brush->sides[i]; if ( !s->winding ) { continue; } if ( onlyvis && !( s->flags & SFL_VISIBLE ) ) { continue; } OutputWinding( brush->sides[i].winding, f ); } } fclose( f ); } //end of the function WriteBrushList
/* ============== FinishSprite ============== */ void FinishSprite( void ){ FILE *spriteouthandle; int i, curframe; dsprite_t spritetemp; char savename[1024]; if ( sprite.numframes == 0 ) { return; } if ( !strlen( spritename ) ) { Error( "Didn't name sprite file" ); } sprintf( savename, "%s%s.sp2", gamedir, spritename ); if ( g_release ) { char name[1024]; sprintf( name, "%s.sp2", spritename ); ReleaseFile( name ); spritename[0] = 0; // clear for a new sprite sprite.numframes = 0; return; } printf( "saving in %s\n", savename ); CreatePath( savename ); spriteouthandle = SafeOpenWrite( savename ); // // write out the sprite header // spritetemp.ident = LittleLong( IDSPRITEHEADER ); spritetemp.version = LittleLong( SPRITE_VERSION ); spritetemp.numframes = LittleLong( sprite.numframes ); SafeWrite( spriteouthandle, &spritetemp, 12 ); // // write out the frames // curframe = 0; for ( i = 0 ; i < sprite.numframes ; i++ ) { frames[i].width = LittleLong( frames[i].width ); frames[i].height = LittleLong( frames[i].height ); frames[i].origin_x = LittleLong( frames[i].origin_x ); frames[i].origin_y = LittleLong( frames[i].origin_y ); } SafeWrite( spriteouthandle, frames, sizeof( frames[0] ) * sprite.numframes ); fclose( spriteouthandle ); spritename[0] = 0; // clear for a new sprite sprite.numframes = 0; }
fileHandle_t FS_FOpenFileWrite( const char *filename ) { CString strPath = ValueForKey(g_qeglobals.d_project_entity, "basepath"); AddSlash(strPath); strPath += filename; // bad casting ptr to int return reinterpret_cast<fileHandle_t>(SafeOpenWrite(strPath)); }
/* * ============== * SaveFile * ============== */ void SaveFile(const char* const filename, const void* const buffer, int count) { FILE* f; f = SafeOpenWrite(filename); SafeWrite(f, buffer, count); fclose(f); }
void SaveFile (char *filename, void *buffer, long count) { int handle; handle = SafeOpenWrite (filename); SafeWrite (handle, buffer, count); close (handle); }
void WriteXBSPFile(const char *filename) { xbspHeader_t outheader, *header; FILE *file; time_t t; char marker[1024]; int size; /* set header */ header = &outheader; memset(header, 0, sizeof(*header)); //% Swapfile(); /* set up header */ *((int *)(bspHeader_t *) header->ident) = *((int *)game->bspIdent); header->version = LittleLong(game->bspVersion); /* write initial header */ file = SafeOpenWrite(filename); SafeWrite(file, (bspHeader_t *) header, sizeof(*header)); /* overwritten later */ /* add marker lump */ time(&t); sprintf(marker, "I LOVE MY OWMAP %s on %s)", Q3MAP_VERSION, asctime(localtime(&t))); AddLump(file, (bspHeader_t *) header, 0, marker, strlen(marker) + 1); /* add lumps */ AddLump(file, (bspHeader_t *) header, LUMP_SHADERS, bspShaders, numBSPShaders * sizeof(bspShader_t)); AddLump(file, (bspHeader_t *) header, LUMP_PLANES, bspPlanes, numBSPPlanes * sizeof(bspPlane_t)); AddLump(file, (bspHeader_t *) header, LUMP_LEAFS, bspLeafs, numBSPLeafs * sizeof(bspLeaf_t)); AddLump(file, (bspHeader_t *) header, LUMP_NODES, bspNodes, numBSPNodes * sizeof(bspNode_t)); AddLump(file, (bspHeader_t *) header, LUMP_BRUSHES, bspBrushes, numBSPBrushes * sizeof(bspBrush_t)); AddBrushSidesLump(file, header); AddLump(file, (bspHeader_t *) header, LUMP_LEAFSURFACES, bspLeafSurfaces, numBSPLeafSurfaces * sizeof(bspLeafSurfaces[0])); AddLump(file, (bspHeader_t *) header, LUMP_LEAFBRUSHES, bspLeafBrushes, numBSPLeafBrushes * sizeof(bspLeafBrushes[0])); AddLump(file, (bspHeader_t *) header, LUMP_MODELS, bspModels, numBSPModels * sizeof(bspModel_t)); AddDrawVertsLump(file, header); AddDrawSurfacesLump(file, header); AddLump(file, (bspHeader_t *) header, LUMP_VISIBILITY, bspVisBytes, numBSPVisBytes); AddLump(file, (bspHeader_t *) header, LUMP_LIGHTMAPS, bspLightBytes, numBSPLightBytes); AddLightGridLumps(file, header); AddLump(file, (bspHeader_t *) header, LUMP_ENTITIES, bspEntData, bspEntDataSize); AddLump(file, (bspHeader_t *) header, LUMP_FOGS, bspFogs, numBSPFogs * sizeof(bspFog_t)); AddLump(file, (bspHeader_t *) header, LUMP_DRAWINDEXES, bspDrawIndexes, numBSPDrawIndexes * sizeof(bspDrawIndexes[0])); /* emit bsp size */ size = ftell(file); Sys_Printf("Wrote %.1f MB (%d bytes)\n", (float)size / (1024 * 1024), size); /* write the completed header */ fseek(file, 0, SEEK_SET); SafeWrite(file, header, sizeof(*header)); /* close the file */ fclose(file); }
/* ============== BeginPak ============== */ void BeginPak (char *outname) { if (!g_pak) return; pakfile = SafeOpenWrite (outname); // leave space for header SafeWrite (pakfile, &pakheader, sizeof(pakheader)); pf = pfiles; }
void H_printf( char *fmt, ... ){ va_list argptr; char name[1024]; if ( !headerouthandle ) { sprintf( name, "%s/tris.h", cddir ); headerouthandle = SafeOpenWrite( name ); fprintf( headerouthandle, "// %s\n\n", cddir ); fprintf( headerouthandle, "// This file generated by qdata - Do NOT Modify\n\n" ); } va_start( argptr, fmt ); vfprintf( headerouthandle, fmt, argptr ); va_end( argptr ); }
void NewWad(char *pathname, qboolean bigendien) { outwad = SafeOpenWrite(pathname); fseek(outwad, sizeof(wadinfo_t), SEEK_SET); memset(outinfo, 0, sizeof(outinfo)); if (bigendien) { wadshort = BigShort; wadlong = BigLong; } else { wadshort = LittleShort; wadlong = LittleLong; } outlumps = 0; }
/* ============= WriteBSPFile Swaps the bsp file in place, so it should not be referenced again ============= */ void WriteBSPFile( const char *filename ) { dheader_t outheader, *header; FILE *bspfile; header = &outheader; memset( header, 0, sizeof(dheader_t) ); SwapBSPFile(); header->ident = LittleLong( BSP_IDENT ); header->version = LittleLong( BSP_VERSION ); bspfile = SafeOpenWrite( filename ); SafeWrite( bspfile, header, sizeof(dheader_t) ); // overwritten later AddLump( bspfile, header, LUMP_SHADERS, dshaders, numShaders*sizeof(dshader_t) ); AddLump( bspfile, header, LUMP_PLANES, dplanes, numplanes*sizeof(dplane_t) ); AddLump( bspfile, header, LUMP_LEAFS, dleafs, numleafs*sizeof(dleaf_t) ); AddLump( bspfile, header, LUMP_NODES, dnodes, numnodes*sizeof(dnode_t) ); AddLump( bspfile, header, LUMP_BRUSHES, dbrushes, numbrushes*sizeof(dbrush_t) ); AddLump( bspfile, header, LUMP_BRUSHSIDES, dbrushsides, numbrushsides*sizeof(dbrushside_t) ); AddLump( bspfile, header, LUMP_LEAFSURFACES, dleafsurfaces, numleafsurfaces*sizeof(dleafsurfaces[0]) ); AddLump( bspfile, header, LUMP_LEAFBRUSHES, dleafbrushes, numleafbrushes*sizeof(dleafbrushes[0]) ); AddLump( bspfile, header, LUMP_MODELS, dmodels, nummodels*sizeof(dmodel_t) ); AddLump( bspfile, header, LUMP_DRAWVERTS, drawVerts, numDrawVerts*sizeof(drawVert_t) ); AddLump( bspfile, header, LUMP_SURFACES, drawSurfaces, numDrawSurfaces*sizeof(dsurface_t) ); AddLump( bspfile, header, LUMP_VISIBILITY, visBytes, numVisBytes ); AddLump( bspfile, header, LUMP_LIGHTMAPS, lightBytes, numLightBytes ); // AddLump( bspfile, header, LUMP_LIGHTGRID, gridData, 8 * numGridPoints ); AddLump( bspfile, header, LUMP_ENTITIES, dentdata, entdatasize ); // AddLump( bspfile, header, LUMP_FOGS, dfogs, numFogs * sizeof(dfog_t) ); AddLump( bspfile, header, LUMP_DRAWINDEXES, drawIndexes, numDrawIndexes * sizeof(drawIndexes[0]) ); // su44: MoHAA lumps AddLump( bspfile, header, LUMP_STATICMODELDEF, staticModels, numStaticModels * sizeof(staticModels[0]) ); AddLump( bspfile, header, LUMP_STATICMODELINDEXES, staticModels, numLeafStaticModels * sizeof(dleafStaticModels[0]) ); AddLump( bspfile, header, LUMP_STATICMODELDATA, staticModelVertColors, numStaticModelVertColors * 3 ); AddLump( bspfile, header, LUMP_TERRAIN, terraPatches, numTerraPatches * sizeof(terraPatches[0]) ); AddLump( bspfile, header, LUMP_SIDEEQUATIONS, sideEquations, numSideEquations * sizeof(sideEquations[0]) ); AddLump( bspfile, header, LUMP_SPHERELIGHTS, sphereLights, numSphereLights * sizeof(sphereLights[0]) ); AddLump( bspfile, header, LUMP_LIGHTDEFS, lightDefs, numLightDefs * sizeof(lightDefs[0]) ); fseek (bspfile, 0, SEEK_SET); SafeWrite (bspfile, header, sizeof(dheader_t)); fclose (bspfile); }
/* ============= WriteBSPFile Swaps the bsp file in place, so it should not be referenced again ============= */ void Q2_WriteBSPFile (char *filename) { header = &outheader; memset (header, 0, sizeof(dheader_t)); Q2_SwapBSPFile (true); header->ident = LittleLong (IDBSPHEADER); header->version = LittleLong (BSPVERSION); wadfile = SafeOpenWrite (filename); SafeWrite (wadfile, header, sizeof(dheader_t)); // overwritten later Q2_AddLump (LUMP_PLANES, dplanes, numplanes*sizeof(dplane_t)); Q2_AddLump (LUMP_LEAFS, dleafs, numleafs*sizeof(dleaf_t)); Q2_AddLump (LUMP_VERTEXES, dvertexes, numvertexes*sizeof(dvertex_t)); Q2_AddLump (LUMP_NODES, dnodes, numnodes*sizeof(dnode_t)); Q2_AddLump (LUMP_TEXINFO, texinfo, numtexinfo*sizeof(texinfo_t)); Q2_AddLump (LUMP_FACES, dfaces, numfaces*sizeof(dface_t)); Q2_AddLump (LUMP_BRUSHES, dbrushes, numbrushes*sizeof(dbrush_t)); Q2_AddLump (LUMP_BRUSHSIDES, dbrushsides, numbrushsides*sizeof(dbrushside_t)); Q2_AddLump (LUMP_LEAFFACES, dleaffaces, numleaffaces*sizeof(dleaffaces[0])); Q2_AddLump (LUMP_LEAFBRUSHES, dleafbrushes, numleafbrushes*sizeof(dleafbrushes[0])); Q2_AddLump (LUMP_SURFEDGES, dsurfedges, numsurfedges*sizeof(dsurfedges[0])); Q2_AddLump (LUMP_EDGES, dedges, numedges*sizeof(dedge_t)); Q2_AddLump (LUMP_MODELS, dmodels, nummodels*sizeof(dmodel_t)); Q2_AddLump (LUMP_AREAS, dareas, numareas*sizeof(darea_t)); Q2_AddLump (LUMP_AREAPORTALS, dareaportals, numareaportals*sizeof(dareaportal_t)); Q2_AddLump (LUMP_LIGHTING, dlightdata, lightdatasize); Q2_AddLump (LUMP_VISIBILITY, dvisdata, visdatasize); Q2_AddLump (LUMP_ENTITIES, dentdata, entdatasize); Q2_AddLump (LUMP_POP, dpop, sizeof(dpop)); fseek (wadfile, 0, SEEK_SET); SafeWrite (wadfile, header, sizeof(dheader_t)); fclose (wadfile); } //end of the function Q2_WriteBSPFile
/* ============= Q3_WriteBSPFile Swaps the bsp file in place, so it should not be referenced again ============= */ void Q3_WriteBSPFile( char *filename ) { dheader_t outheader, *header; FILE *bspfile; header = &outheader; memset( header, 0, sizeof(dheader_t) ); Q3_SwapBSPFile(); header->ident = LittleLong( BSP_IDENT ); header->version = LittleLong( BSP_VERSION ); bspfile = SafeOpenWrite( filename ); SafeWrite( bspfile, header, sizeof(dheader_t) ); // overwritten later Q3_AddLump( bspfile, header, LUMP_SHADERS, dshaders, q3_numShaders*sizeof(dshader_t) ); Q3_AddLump( bspfile, header, LUMP_PLANES, dplanes, q3_numplanes*sizeof(dplane_t) ); Q3_AddLump( bspfile, header, LUMP_LEAFS, dleafs, q3_numleafs*sizeof(dleaf_t) ); Q3_AddLump( bspfile, header, LUMP_NODES, dnodes, q3_numnodes*sizeof(dnode_t) ); Q3_AddLump( bspfile, header, LUMP_BRUSHES, dbrushes, q3_numbrushes*sizeof(dbrush_t) ); Q3_AddLump( bspfile, header, LUMP_BRUSHSIDES, dbrushsides, q3_numbrushsides*sizeof(dbrushside_t) ); Q3_AddLump( bspfile, header, LUMP_LEAFSURFACES, dleafsurfaces, q3_numleafsurfaces*sizeof(dleafsurfaces[0]) ); Q3_AddLump( bspfile, header, LUMP_LEAFBRUSHES, dleafbrushes, q3_numleafbrushes*sizeof(dleafbrushes[0]) ); Q3_AddLump( bspfile, header, LUMP_MODELS, dmodels, q3_nummodels*sizeof(dmodel_t) ); Q3_AddLump( bspfile, header, LUMP_DRAWVERTS, drawVerts, q3_numDrawVerts*sizeof(drawVert_t) ); Q3_AddLump( bspfile, header, LUMP_SURFACES, drawSurfaces, q3_numDrawSurfaces*sizeof(dsurface_t) ); Q3_AddLump( bspfile, header, LUMP_VISIBILITY, q3_visBytes, q3_numVisBytes ); Q3_AddLump( bspfile, header, LUMP_LIGHTMAPS, q3_lightBytes, q3_numLightBytes ); // Q3_AddLump( bspfile, header, LUMP_LIGHTGRID, q3_gridData, 8 * q3_numGridPoints ); Q3_AddLump( bspfile, header, LUMP_ENTITIES, dentdata, q3_entdatasize ); // Q3_AddLump( bspfile, header, LUMP_FOGS, dfogs, q3_numFogs * sizeof(dfog_t) ); Q3_AddLump( bspfile, header, LUMP_DRAWINDEXES, drawIndexes, q3_numDrawIndexes * sizeof(drawIndexes[0]) ); fseek (bspfile, 0, SEEK_SET); SafeWrite (bspfile, header, sizeof(dheader_t)); fclose (bspfile); }
void WriteLitFile(const char *filename, int version) { FILE *l; char f[1024]; litheader_t h; strncpy(f, filename, 1019); /* 1024 - space for extension - '\0' */ f[1023] = '\0'; StripExtension(f); DefaultExtension(f, ".lit"); h.ident[0] = 'Q'; h.ident[1] = 'L'; h.ident[2] = 'I'; h.ident[3] = 'T'; h.version = LittleLong(version); l = SafeOpenWrite(f); SafeWrite(l, &h, sizeof(litheader_t)); SafeWrite(l, lit_filebase, lightdatasize * 3); fclose(l); }
void WriteData (int crc) { def_t *def; ddef_t *dd; dprograms_t progs; FILE *h; int i; for (def = pr.def_head.next ; def ; def = def->next) { if (def->type->type == ev_function) { // df = &functions[numfunctions]; // numfunctions++; } else if (def->type->type == ev_field) { dd = &fields[numfielddefs]; numfielddefs++; dd->type = def->type->aux_type->type; #ifdef HASH_DEF dd->s_name=def->s_name; #else dd->s_name = CopyString (def->name); #endif dd->ofs = G_INT(def->ofs); } dd = &globals[numglobaldefs]; numglobaldefs++; dd->type = def->type->type; if ( !def->initialized && def->type->type != ev_function && def->type->type != ev_field && def->scope == NULL) dd->type |= DEF_SAVEGLOBGAL; #ifdef HASH_DEF dd->s_name=def->s_name; #else dd->s_name = CopyString (def->name); #endif dd->ofs = def->ofs; } // PrintStrings (); // PrintFunctions (); // PrintFields (); // PrintGlobals (); strofs = (strofs+3)&~3; printf ("%6i strofs\n", strofs); printf ("%6i numstatements\n", numstatements); printf ("%6i numfunctions\n", numfunctions); printf ("%6i numglobaldefs\n", numglobaldefs); printf ("%6i numfielddefs\n", numfielddefs); printf ("%6i numpr_globals\n", numpr_globals); h = SafeOpenWrite (destfile); SafeWrite (h, &progs, sizeof(progs)); progs.ofs_strings = ftell (h); progs.numstrings = strofs; SafeWrite (h, strings, strofs); progs.ofs_statements = ftell (h); progs.numstatements = numstatements; for (i=0 ; i<numstatements ; i++) { statements[i].op = LittleShort(statements[i].op); statements[i].a = LittleShort(statements[i].a); statements[i].b = LittleShort(statements[i].b); statements[i].c = LittleShort(statements[i].c); } SafeWrite (h, statements, numstatements*sizeof(dstatement_t)); progs.ofs_functions = ftell (h); progs.numfunctions = numfunctions; for (i=0 ; i<numfunctions ; i++) { functions[i].first_statement = LittleLong (functions[i].first_statement); functions[i].parm_start = LittleLong (functions[i].parm_start); functions[i].s_name = LittleLong (functions[i].s_name); functions[i].s_file = LittleLong (functions[i].s_file); functions[i].numparms = LittleLong (functions[i].numparms); functions[i].locals = LittleLong (functions[i].locals); } SafeWrite (h, functions, numfunctions*sizeof(dfunction_t)); progs.ofs_globaldefs = ftell (h); progs.numglobaldefs = numglobaldefs; for (i=0 ; i<numglobaldefs ; i++) { globals[i].type = LittleShort (globals[i].type); globals[i].ofs = LittleShort (globals[i].ofs); globals[i].s_name = LittleLong (globals[i].s_name); } SafeWrite (h, globals, numglobaldefs*sizeof(ddef_t)); progs.ofs_fielddefs = ftell (h); progs.numfielddefs = numfielddefs; for (i=0 ; i<numfielddefs ; i++) { fields[i].type = LittleShort (fields[i].type); fields[i].ofs = LittleShort (fields[i].ofs); fields[i].s_name = LittleLong (fields[i].s_name); } SafeWrite (h, fields, numfielddefs*sizeof(ddef_t)); progs.ofs_globals = ftell (h); progs.numglobals = numpr_globals; for (i=0 ; i<numpr_globals ; i++) ((int *)pr_globals)[i] = LittleLong (((int *)pr_globals)[i]); SafeWrite (h, pr_globals, numpr_globals*4); printf ("%6i TOTAL SIZE\n", (int)ftell (h)); progs.entityfields = pr.size_fields; progs.version = PROG_VERSION; progs.crc = crc; // byte swap the header and write it out for (i=0 ; i<sizeof(progs)/4 ; i++) ((int *)&progs)[i] = LittleLong ( ((int *)&progs)[i] ); fseek (h, 0, SEEK_SET); SafeWrite (h, &progs, sizeof(progs)); fclose (h); }
static void WriteData (int crc) { def_t *def; ddef_t *dd; dstatement_v6_t *stmt6; ddef_v6_t *globals6, *fields6; qboolean v6able; dprograms_t progs; FILE *h; int i, localName; localName = (hcc_OptimizeNameTable) ? CopyString("LCL+") : 0; /* was "LOCAL+" in old HCC from the "H2_UTILS" package */ if (hcc_OptimizeStringHeap) printf ("compacting string heap\n"); for (def = pr.def_head.next; def; def = def->next) { if (def->type->type == ev_field) { dd = &fields[numfielddefs]; numfielddefs++; dd->type = def->type->aux_type->type; dd->s_name = (hcc_OptimizeStringHeap) ? CopyString(def->name) : /* name gets copied below : */ strofs; dd->ofs = G_INT(def->ofs); } dd = &globals[numglobaldefs]; numglobaldefs++; dd->type = def->type->type; if (!def->initialized && def->type->type != ev_function && def->type->type != ev_field && def->scope == NULL) { /* STR_ is a special case string constant */ if (strncmp(def->name,"STR_", 4) != 0 || hcc_Compat_STR_SAVEGLOBL) dd->type |= DEF_SAVEGLOBAL; } if (hcc_OptimizeNameTable && (def->scope != NULL || (!(dd->type & DEF_SAVEGLOBAL) && def->type->type < ev_field))) { dd->s_name = localName; } else { dd->s_name = CopyString(def->name); } dd->ofs = def->ofs; } // PrintStrings (); // PrintFunctions (); // PrintFields (); // PrintGlobals (); strofs = (strofs + 3) & ~3; printf("object file %s\n", destfile); printf(" registers: %10d / %10d (%10d bytes)\n", numpr_globals, MAX_REGS, numpr_globals*(int)sizeof(float)); printf(" statements: %10d / %10d (%10d bytes)\n", numstatements, MAX_STATEMENTS, numstatements*(int)sizeof(dstatement_t)); printf(" functions: %10d / %10d (%10d bytes)\n", numfunctions, MAX_FUNCTIONS, numfunctions*(int)sizeof(dfunction_t)); printf(" global defs: %10d / %10d (%10d bytes)\n", numglobaldefs, MAX_GLOBALS, numglobaldefs*(int)sizeof(ddef_t)); printf(" field defs: %10d / %10d (%10d bytes)\n", numfielddefs, MAX_FIELDS, numfielddefs*(int)sizeof(ddef_t)); printf(" string heap: %10d / %10d\n", strofs, MAX_STRINGS); printf(" entity fields: %10d\n", pr.size_fields); stmt6 = NULL; globals6 = NULL; fields6 = NULL; if (numpr_globals < 65536) v6able = true; else { v6able = false; if (hcc_version_req != PROG_VERSION_V7) printf("Too many registers: version 6 output not possible.\n"); } if (v6able && hcc_version_req != PROG_VERSION_V7) { v6able = (stmt6 = PR_GenV6Stmts(statements, numstatements)) != NULL; if ( v6able) v6able = (globals6 = PR_GenV6Defs(globals, numglobaldefs)) != NULL; if ( v6able) v6able = (fields6 = PR_GenV6Defs(fields, numfielddefs)) != NULL; if (!v6able) { if (stmt6) free (stmt6); if (globals6) free (globals6); } } if (!v6able) { if (hcc_version_req == PROG_VERSION_V6) COM_Error("Can not output version 6 progs: v6 limitations not complied with."); hcc_version_req = PROG_VERSION_V7; } else { if (hcc_version_req == -1) hcc_version_req = PROG_VERSION_V6; } h = SafeOpenWrite (destfile); SafeWrite (h, &progs, sizeof(progs)); progs.ofs_strings = ftell (h); progs.numstrings = strofs; SafeWrite (h, strings, strofs); progs.ofs_statements = ftell (h); progs.numstatements = numstatements; if (hcc_version_req == PROG_VERSION_V7) { for (i = 0; i < numstatements; i++) { statements[i].op = LittleShort(statements[i].op); statements[i].a = LittleLong(statements[i].a); statements[i].b = LittleLong(statements[i].b); statements[i].c = LittleLong(statements[i].c); } SafeWrite (h, statements, numstatements*sizeof(dstatement_t)); } else { SafeWrite (h, stmt6, numstatements*sizeof(dstatement_v6_t)); free (stmt6); } progs.ofs_functions = ftell (h); progs.numfunctions = numfunctions; for (i = 0; i < numfunctions; i++) { functions[i].first_statement = LittleLong (functions[i].first_statement); functions[i].parm_start = LittleLong (functions[i].parm_start); functions[i].s_name = LittleLong (functions[i].s_name); functions[i].s_file = LittleLong (functions[i].s_file); functions[i].numparms = LittleLong (functions[i].numparms); functions[i].locals = LittleLong (functions[i].locals); } SafeWrite (h, functions, numfunctions*sizeof(dfunction_t)); progs.ofs_globaldefs = ftell (h); progs.numglobaldefs = numglobaldefs; if (hcc_version_req == PROG_VERSION_V7) { for (i = 0; i < numglobaldefs; i++) { globals[i].type = LittleShort (globals[i].type); globals[i].ofs = LittleLong (globals[i].ofs); globals[i].s_name = LittleLong (globals[i].s_name); } SafeWrite (h, globals, numglobaldefs*sizeof(ddef_t)); } else { SafeWrite (h, globals6, numglobaldefs*sizeof(ddef_v6_t)); free (globals6); } progs.ofs_fielddefs = ftell (h); progs.numfielddefs = numfielddefs; if (hcc_version_req == PROG_VERSION_V7) { for (i = 0; i < numfielddefs; i++) { fields[i].type = LittleShort (fields[i].type); fields[i].ofs = LittleLong (fields[i].ofs); fields[i].s_name = LittleLong (fields[i].s_name); } SafeWrite (h, fields, numfielddefs*sizeof(ddef_t)); } else { SafeWrite (h, fields6, numfielddefs*sizeof(ddef_v6_t)); free (fields6); } progs.ofs_globals = ftell (h); progs.numglobals = numpr_globals; for (i = 0; i < numpr_globals; i++) ((int *)pr_globals)[i] = LittleLong (((int *)pr_globals)[i]); SafeWrite (h, pr_globals, numpr_globals*4); printf(" total size: %10d bytes\n", (int)ftell(h)); printf(" progs version: %10d\n", hcc_version_req); progs.entityfields = pr.size_fields; progs.version = hcc_version_req; progs.crc = crc; // byte swap the header and write it out for (i = 0; i < (int)sizeof(progs)/4; i++) ((int *)&progs)[i] = LittleLong ( ((int *)&progs)[i] ); fseek (h, 0, SEEK_SET); SafeWrite (h, &progs, sizeof(progs)); fclose (h); }
void WriteFile (void) { FILE *modelouthandle; int total = 0; int i; pStart = kalloc( 1, FILEBUFFER ); StripExtension (outname); for (i = 1; i < numseqgroups; i++) { // write the non-default sequence group data to separate files char groupname[128], localname[128]; sprintf( groupname, "%s%02d.mdl", outname, i ); printf ("writing %s:\n", groupname); modelouthandle = SafeOpenWrite (groupname); pseqhdr = (studioseqhdr_t *)pStart; pseqhdr->id = IDSTUDIOSEQHEADER; pseqhdr->version = STUDIO_VERSION; pData = pStart + sizeof( studioseqhdr_t ); pData = WriteAnimations( pData, pStart, i ); ExtractFileBase( groupname, localname ); sprintf( sequencegroup[i].name, "models\\%s.mdl", localname ); strcpy( pseqhdr->name, sequencegroup[i].name ); pseqhdr->length = pData - pStart; printf("total %6d\n", pseqhdr->length ); SafeWrite( modelouthandle, pStart, pseqhdr->length ); fclose (modelouthandle); memset( pStart, 0, pseqhdr->length ); } if (split_textures) { // write textures out to a separate file char texname[128]; sprintf( texname, "%sT.mdl", outname ); printf ("writing %s:\n", texname); modelouthandle = SafeOpenWrite (texname); phdr = (studiohdr_t *)pStart; phdr->id = IDSTUDIOHEADER; phdr->version = STUDIO_VERSION; pData = (byte *)phdr + sizeof( studiohdr_t ); WriteTextures( ); phdr->length = pData - pStart; printf("textures %6d bytes\n", phdr->length ); SafeWrite( modelouthandle, pStart, phdr->length ); fclose (modelouthandle); memset( pStart, 0, phdr->length ); pData = pStart; } // // write the model output file // strcat (outname, ".mdl"); printf ("---------------------\n"); printf ("writing %s:\n", outname); modelouthandle = SafeOpenWrite (outname); phdr = (studiohdr_t *)pStart; phdr->id = IDSTUDIOHEADER; phdr->version = STUDIO_VERSION; strcpy( phdr->name, outname ); VectorCopy( eyeposition, phdr->eyeposition ); VectorCopy( bbox[0], phdr->min ); VectorCopy( bbox[1], phdr->max ); VectorCopy( cbox[0], phdr->bbmin ); VectorCopy( cbox[1], phdr->bbmax ); phdr->flags = gflags; pData = (byte *)phdr + sizeof( studiohdr_t ); WriteBoneInfo( ); printf("bones %6d bytes (%d)\n", pData - pStart - total, numbones ); total = pData - pStart; pData = WriteAnimations( pData, pStart, 0 ); WriteSequenceInfo( ); printf("sequences %6d bytes (%d frames) [%d:%02d]\n", pData - pStart - total, totalframes, (int)totalseconds / 60, (int)totalseconds % 60 ); total = pData - pStart; WriteModel( ); printf("models %6d bytes\n", pData - pStart - total ); total = pData - pStart; if (!split_textures) { WriteTextures( ); printf("textures %6d bytes\n", pData - pStart - total ); } phdr->length = pData - pStart; printf("total %6d\n", phdr->length ); SafeWrite( modelouthandle, pStart, phdr->length ); fclose (modelouthandle); }
/* Swaps the bsp file in place, so it should not be referenced again */ void WriteBSPFile (char *filename) { unsigned int i; FILE *f; swappedbuffer_t sb; int index,j,bspsize; BSPLump_t lumps[HEADER_LUMPS],*lump; BSP_RemoveSkipSurfaces(); bspsize = BSP_HEADER_SIZE; bspsize += sizeof(lumps)+20*numplanes+(40+BSP_AMBIENT_END)*numleafs+12*numvertexes; bspsize += 44*numnodes+40*numtexinfo+(24+BSP_MAX_LIGHTMAPS)*numfaces+12*numclipnodes; bspsize += 4*nummarksurfaces+4*numsurfedges+8*numedges; bspsize += (48+4*BSP_MAX_HULLS)*nummodels+rgblightdatasize; bspsize += visdatasize+entdatasize+texdatasize; bspsize += 512; // extra case for safety and to compensate for the 4-byte padding of the lumps SB_Alloc (&sb, bspsize); printf ("Allocated %f MB (%d bytes) for file buffer\n", bspsize*(1.0f/(1024.0f*1024.0f)), bspsize); // write header SB_ZeroFill(&sb,sizeof(lumps)+BSP_HEADER_SIZE); // filled in later // write lumps and pad each one to a multiple of 4 bytes lump = &lumps[LUMP_PLANES]; lump->iFileOffset = SB_Tell(&sb); for (i = 0; i < numplanes; i++) { SB_WriteFloat(&sb,dplanes[i].fNormal[0]); SB_WriteFloat(&sb,dplanes[i].fNormal[1]); SB_WriteFloat(&sb,dplanes[i].fNormal[2]); SB_WriteFloat(&sb,dplanes[i].fDist); SB_WriteInt(&sb,dplanes[i].iType); } lump->iFileLength = SB_Tell(&sb) - lump->iFileOffset; SB_ZeroFill(&sb,((lump->iFileLength+3)&~3)-lump->iFileLength); lump = &lumps[LUMP_LEAFS]; lump->iFileOffset = SB_Tell(&sb); for (i = 0; i < numleafs; i++) { SB_WriteInt (&sb, dleafs[i].iContents); SB_WriteInt (&sb, dleafs[i].iVisibilityOffset); SB_WriteFloat (&sb, dleafs[i].fMins[0]); SB_WriteFloat (&sb, dleafs[i].fMins[1]); SB_WriteFloat (&sb, dleafs[i].fMins[2]); SB_WriteFloat (&sb, dleafs[i].fMaxs[0]); SB_WriteFloat (&sb, dleafs[i].fMaxs[1]); SB_WriteFloat (&sb, dleafs[i].fMaxs[2]); SB_WriteInt (&sb, dleafs[i].uiFirstMarkSurface); SB_WriteInt (&sb, dleafs[i].uiNumMarkSurfaces); for (j = 0; j < BSP_AMBIENT_END; j++) SB_WriteByte (&sb, dleafs[i].bAmbientLevel[j]); } lump->iFileLength = SB_Tell(&sb) - lump->iFileOffset; SB_ZeroFill (&sb, ((lump->iFileLength + 3) & ~3) - lump->iFileLength); lump = &lumps[LUMP_VERTEXES]; lump->iFileOffset = SB_Tell(&sb); for (i = 0; i < numvertexes; i++) { SB_WriteFloat (&sb, dvertexes[i].fPoint[0]); SB_WriteFloat (&sb, dvertexes[i].fPoint[1]); SB_WriteFloat (&sb, dvertexes[i].fPoint[2]); } lump->iFileLength = SB_Tell(&sb) - lump->iFileOffset; SB_ZeroFill (&sb, ((lump->iFileLength + 3) & ~3) - lump->iFileLength); lump = &lumps[LUMP_NODES]; lump->iFileOffset = SB_Tell(&sb); for (i = 0; i < numnodes; i++) { SB_WriteInt (&sb, dnodes[i].iPlaneNum); SB_WriteInt (&sb, dnodes[i].iChildren[0]); SB_WriteInt (&sb, dnodes[i].iChildren[1]); SB_WriteFloat (&sb, dnodes[i].fMins[0]); SB_WriteFloat (&sb, dnodes[i].fMins[1]); SB_WriteFloat (&sb, dnodes[i].fMins[2]); SB_WriteFloat (&sb, dnodes[i].fMaxs[0]); SB_WriteFloat (&sb, dnodes[i].fMaxs[1]); SB_WriteFloat (&sb, dnodes[i].fMaxs[2]); SB_WriteInt (&sb, dnodes[i].usFirstFace); SB_WriteInt (&sb, dnodes[i].usNumFaces); } lump->iFileLength = SB_Tell(&sb) - lump->iFileOffset; SB_ZeroFill (&sb, ((lump->iFileLength + 3) & ~3) - lump->iFileLength); lump = &lumps[LUMP_TEXINFO]; lump->iFileOffset = SB_Tell(&sb); for (i = 0; i < numtexinfo; i++) { SB_WriteFloat (&sb, texinfo[i].v[0][0]); SB_WriteFloat (&sb, texinfo[i].v[0][1]); SB_WriteFloat (&sb, texinfo[i].v[0][2]); SB_WriteFloat (&sb, texinfo[i].v[0][3]); SB_WriteFloat (&sb, texinfo[i].v[1][0]); SB_WriteFloat (&sb, texinfo[i].v[1][1]); SB_WriteFloat (&sb, texinfo[i].v[1][2]); SB_WriteFloat (&sb, texinfo[i].v[1][3]); SB_WriteInt (&sb, texinfo[i].iMipTex); SB_WriteInt (&sb, texinfo[i].iFlags); } lump->iFileLength = SB_Tell(&sb) - lump->iFileOffset; SB_ZeroFill (&sb, ((lump->iFileLength + 3) & ~3) - lump->iFileLength); lump = &lumps[LUMP_FACES]; lump->iFileOffset = SB_Tell(&sb); for (i = 0; i < numfaces; i++) { SB_WriteInt (&sb, dfaces[i].iPlaneNum); SB_WriteInt (&sb, dfaces[i].iSide); SB_WriteInt (&sb, dfaces[i].iFirstEdge); SB_WriteInt (&sb, dfaces[i].iNumEdges); SB_WriteInt (&sb, dfaces[i].iTexInfo); for (j = 0; j < BSP_MAX_LIGHTMAPS; j++) SB_WriteByte (&sb, dfaces[i].bStyles[j]); SB_WriteInt (&sb, dfaces[i].iLightOffset); } lump->iFileLength = SB_Tell(&sb) - lump->iFileOffset; SB_ZeroFill (&sb, ((lump->iFileLength + 3) & ~3) - lump->iFileLength); lump = &lumps[LUMP_CLIPNODES]; lump->iFileOffset = SB_Tell(&sb); for (i = 0; i < numclipnodes; i++) { SB_WriteInt (&sb, dclipnodes[i].iPlaneNum); SB_WriteInt (&sb, dclipnodes[i].iChildren[0]); SB_WriteInt (&sb, dclipnodes[i].iChildren[1]); } lump->iFileLength = SB_Tell(&sb) - lump->iFileOffset; SB_ZeroFill (&sb, ((lump->iFileLength + 3) & ~3) - lump->iFileLength); lump = &lumps[LUMP_MARKSURFACES]; lump->iFileOffset = SB_Tell(&sb); for (i = 0; i < nummarksurfaces; i++) SB_WriteInt (&sb, dmarksurfaces[i]); lump->iFileLength = SB_Tell(&sb) - lump->iFileOffset; SB_ZeroFill (&sb, ((lump->iFileLength + 3) & ~3) - lump->iFileLength); lump = &lumps[LUMP_SURFEDGES]; lump->iFileOffset = SB_Tell(&sb); for (i = 0; i < numsurfedges; i++) SB_WriteInt (&sb, dsurfedges[i]); lump->iFileLength = SB_Tell(&sb) - lump->iFileOffset; SB_ZeroFill (&sb, ((lump->iFileLength + 3) & ~3) - lump->iFileLength); lump = &lumps[LUMP_EDGES]; lump->iFileOffset = SB_Tell(&sb); for (i = 0; i < numedges; i++) { SB_WriteInt (&sb, dedges[i].v[0]); SB_WriteInt (&sb, dedges[i].v[1]); } lump->iFileLength = SB_Tell(&sb) - lump->iFileOffset; SB_ZeroFill (&sb, ((lump->iFileLength + 3) & ~3) - lump->iFileLength); lump = &lumps[LUMP_MODELS]; lump->iFileOffset = SB_Tell (&sb); for (i = 0; i < nummodels; i++) { SB_WriteFloat (&sb, dmodels[i].fMins[0]); SB_WriteFloat (&sb, dmodels[i].fMins[1]); SB_WriteFloat (&sb, dmodels[i].fMins[2]); SB_WriteFloat (&sb, dmodels[i].fMaxs[0]); SB_WriteFloat (&sb, dmodels[i].fMaxs[1]); SB_WriteFloat (&sb, dmodels[i].fMaxs[2]); SB_WriteFloat (&sb, dmodels[i].fOrigin[0]); SB_WriteFloat (&sb, dmodels[i].fOrigin[1]); SB_WriteFloat (&sb, dmodels[i].fOrigin[2]); for (j = 0; j < hullinfo.filehulls; j++) SB_WriteInt (&sb, dmodels[i].iHeadNode[j]); SB_WriteInt (&sb, dmodels[i].iVisLeafs); SB_WriteInt (&sb, dmodels[i].iFirstFace); SB_WriteInt (&sb, dmodels[i].iNumFaces); } lump->iFileLength = SB_Tell(&sb) - lump->iFileOffset; SB_ZeroFill (&sb, ((lump->iFileLength + 3) & ~3) - lump->iFileLength); lump = &lumps[LUMP_LIGHTING]; lump->iFileOffset = SB_Tell (&sb); SB_WriteData (&sb, drgblightdata, rgblightdatasize); lump->iFileLength = SB_Tell(&sb) - lump->iFileOffset; SB_ZeroFill (&sb, ((lump->iFileLength + 3) & ~3) - lump->iFileLength); lump = &lumps[LUMP_VISIBILITY]; lump->iFileOffset = SB_Tell (&sb); SB_WriteData (&sb, dvisdata, visdatasize); lump->iFileLength = SB_Tell(&sb) - lump->iFileOffset; SB_ZeroFill (&sb, ((lump->iFileLength + 3) & ~3) - lump->iFileLength); lump = &lumps[LUMP_ENTITIES]; lump->iFileOffset = SB_Tell (&sb); SB_WriteData (&sb, dentdata, entdatasize); lump->iFileLength = SB_Tell(&sb) - lump->iFileOffset; SB_ZeroFill (&sb, ((lump->iFileLength + 3) & ~3) - lump->iFileLength); // Save the entities output to a text file for later analysis. f = SafeOpenWrite(cFilenameEntity); SafeWrite(f, (void*)dentdata, (entdatasize-1)); fclose(f); lump = &lumps[LUMP_TEXTURES]; lump->iFileOffset = SB_Tell (&sb); SB_WriteData (&sb, dtexdata, texdatasize); lump->iFileLength = SB_Tell(&sb) - lump->iFileOffset; SB_ZeroFill (&sb, ((lump->iFileLength + 3) & ~3) - lump->iFileLength); // Go back and update the header. index = SB_Tell (&sb); SB_SeekAbsolute (&sb, 0); SB_WriteInt(&sb,BSP_HEADER); SB_WriteInt(&sb,BSP_VERSION); // always write full BSP2 lumps, this is for future-proofing for (i = 0; i < HEADER_LUMPS; i++) { SB_WriteInt(&sb,lumps[i].iFileOffset); SB_WriteInt(&sb,lumps[i].iFileLength); } SB_SeekAbsolute (&sb, index); // open a file and dump the buffer into it f = SafeOpenWrite (filename); SafeWrite (f, sb.start, (sb.index - sb.start)); fclose (f); // finish up SB_Free (&sb); }
void WriteData (int crc) { def_t *def; ddef_t *dd; dprograms_t progs; char tname[1024]; int h; unsigned int i; int size; for (def = pr.def_head.next ; def ; def = def->next) { if ((def->type->type == ev_field) && def->constant) { dd = &fields[numfielddefs]; numfielddefs++; dd->type = def->type->aux_type->type == ev_int ? ev_float : def->type->aux_type->type; if (def->save == 0) { strcpy(tname, def->name); strcat(tname, "__"); dd->s_name = CopyString(tname, 0); } else dd->s_name = CopyString (def->name, 0); dd->ofs = G_INT(def->ofs); } else if (pr_optimize_constant_names && def->constant && (def->type->type != ev_function)) { num_constant_names += strlen(def->name) + 1; num_constant_names += sizeof(ddef_t); continue; } else if (pr_optimize_unreferenced && pr_global_refs[def->ofs] <= 0) { if (!(def->type->type != ev_function)) { num_unreferenced += 1; continue; } } else if (pr_optimize_unreferenced && def->type->type == ev_vector) { if (pr_global_refs[def->ofs] + pr_global_refs[def->ofs + 1] + pr_global_refs[def->ofs +1] == 3) { num_unreferenced += 3; def = def->next; // def_x def = def->next; // def_y def = def->next; // def_z continue; } } dd = &globals[numglobaldefs]; dd->type = def->type->type == ev_int ? ev_float : def->type->type; if (def->save && ( dd->type != ev_field || def->constant != 1)) dd->type |= DEF_SAVEGLOBAL; if (def->name) { if (pr_optimize_locals && (def->scope || !(STRCMP(def->name, "IMMEDIATE")))) { num_locals_saved += strlen(def->name); dd->s_name = 0; } else dd->s_name = CopyString (def->name, 0); } dd->ofs = def->ofs; numglobaldefs++; } strofs = (strofs+3)&~3; if (strofs > INT_MAX) PR_ParseWarning(122, "strofs exceeds INT_MAX by %i", strofs - INT_MAX); if (numstatements > INT_MAX) PR_ParseWarning(123, "numstatements exceeds INT_MAX by %i", numstatements - INT_MAX); if (numfunctions > SHRT_MAX) PR_ParseWarning(124, "numfunctions exceeds SHRT_MAX by %i", numfunctions - SHRT_MAX); if (numglobaldefs > SHRT_MAX) PR_ParseWarning(125, "numglobaldefs exceeds SHRT_MAX by %i", numglobaldefs - SHRT_MAX); if (numfielddefs > SHRT_MAX) PR_ParseWarning(126, "numfielddefs exceeds SHRT_MAX by %i", numfielddefs - SHRT_MAX); if (numpr_globals > SHRT_MAX) PR_ParseWarning(127, "numpr_globals exceeds SHRT_MAX by %i", numpr_globals - SHRT_MAX); if (crc != NQ_PROGHEADER_CRC && crc != QW_PROGHEADER_CRC) PR_ParseWarning(208, "System defs do match internal crcs."); if (summary) { summary_print("----------- Summary -----------\n"); i = I_FloatTime() - StartTime; summary_print (" %02i:%02i elapsed time\n", (i / 60) % 59, i % 59); summary_print ("%6i strofs (MAX: %6i)\n", strofs, MAX_STRINGS ); summary_print ("%6i numstatements (MAX: %6i)\n", numstatements, MAX_STATEMENTS); summary_print ("%6i numfunctions (MAX: %6i)\n", numfunctions, SHRT_MAX); summary_print ("%6i numglobaldefs (MAX: %6i)\n", numglobaldefs, SHRT_MAX); summary_print ("%6i numfielddefs (MAX: %6i)\n", numfielddefs, SHRT_MAX); summary_print ("%6i numpr_globals (MAX: %6i)\n", numpr_globals, SHRT_MAX); } h = SafeOpenWrite (destfile); SafeWrite (h, &progs, sizeof(progs)); progs.ofs_strings = lseek (h, 0, SEEK_CUR); progs.numstrings = strofs; SafeWrite (h, strings, strofs); progs.ofs_statements = lseek (h, 0, SEEK_CUR); progs.numstatements = numstatements; for (i=0 ; i<numstatements ; i++) { statements[i].op = LittleShort(statements[i].op); statements[i].a = LittleShort(statements[i].a); statements[i].b = LittleShort(statements[i].b); statements[i].c = LittleShort(statements[i].c); } SafeWrite (h, statements, numstatements*sizeof(dstatement_t)); progs.ofs_functions = lseek (h, 0, SEEK_CUR); progs.numfunctions = numfunctions; for (i=0 ; i<numfunctions ; i++) { functions[i].first_statement = LittleLong (functions[i].first_statement); functions[i].parm_start = LittleLong (functions[i].parm_start); functions[i].s_name = LittleLong (functions[i].s_name < 0 || functions[i].s_name > strofs ? 0 : functions[i].s_name); functions[i].s_file = LittleLong (functions[i].s_file < 0 || functions[i].s_file > strofs ? 0 : functions[i].s_file); functions[i].numparms = LittleLong (functions[i].numparms > MAX_PARMS ? MAX_PARMS : functions[i].numparms); functions[i].locals = LittleLong (functions[i].locals); } SafeWrite (h, functions, numfunctions*sizeof(dfunction_t)); progs.ofs_globaldefs = lseek (h, 0, SEEK_CUR); progs.numglobaldefs = numglobaldefs; for (i=0 ; i<numglobaldefs ; i++) { globals[i].type = LittleShort (globals[i].type); globals[i].ofs = LittleShort (globals[i].ofs); globals[i].s_name = LittleLong (globals[i].s_name); } SafeWrite (h, globals, numglobaldefs*sizeof(ddef_t)); progs.ofs_fielddefs = lseek (h, 0, SEEK_CUR); progs.numfielddefs = numfielddefs; for (i=0 ; i<numfielddefs ; i++) { fields[i].type = LittleShort (fields[i].type); fields[i].ofs = LittleShort (fields[i].ofs); fields[i].s_name = LittleLong (fields[i].s_name < 0 || fields[i].s_name > strofs ? 0: fields[i].s_name); } SafeWrite (h, fields, numfielddefs*sizeof(ddef_t)); progs.ofs_globals = lseek (h, 0, SEEK_CUR); progs.numglobals = numpr_globals; for (i=0 ; i<numpr_globals ; i++) ((int *)pr_globals)[i] = LittleLong (((int *)pr_globals)[i]); SafeWrite (h, pr_globals, numpr_globals*4); i = (int)lseek(h, 0, SEEK_CUR); if (summary) summary_print ("%6i TOTAL SIZE\n", i); size = (i+16)&(~15); progs.entityfields = pr.size_fields; progs.version = PROG_VERSION; progs.crc = crc; if (summary) { summary_print("%6i Progheader CRC ", crc); if (crc == NQ_PROGHEADER_CRC) summary_print("( Quake )\n"); else if (crc == QW_PROGHEADER_CRC) summary_print("(Quake World)\n"); else summary_print("( UNKNOWN )\n"); } // byte swap the header and write it out for (i=0 ; i<sizeof(progs)/4 ; i++) ((int *)&progs)[i] = LittleLong ( ((int *)&progs)[i] ); lseek (h, 0, SEEK_SET); SafeWrite (h, &progs, sizeof(progs)); // look for progs if ((def = PR_GetDef(&type_entity, "progs", NULL, false, 0, 0))) { lseek(h, progs.ofs_globals + 4 * def->ofs, SEEK_SET); i = - (size + 112); SafeWrite (h, &i, 4); } for (def = pr.def_head.next ; def ; def = def->next) { if (def->type->arraysize) { lseek(h, progs.ofs_globals + 4 * def->ofs, SEEK_SET); i = (-(size + 112)) + progs.ofs_globals + 4 * (def->arraystart); //printf("filled in %s with %i\n", def->name, def->arraystart); SafeWrite (h, &i, 4); } } if (summary) { summary_print ("%6i precache_sounds(MAX: %6i)\n", numsounds, MAX_SOUNDS); summary_print ("%6i precache_models(MAX: %6i)\n", nummodels, MAX_MODELS); } close (h); if (summary) { if (pr_optimize_eliminate_temps || pr_optimize_shorten_ifs || pr_optimize_nonvec_parms || pr_optimize_constant_names || pr_optimize_defs || pr_optimize_hash_strings || pr_optimize_locals || pr_optimize_function_names || pr_optimize_filenames || pr_optimize_unreferenced || pr_optimize_logicops || pr_optimize_recycle || pr_optimize_constant_arithmetic) { summary_print("----------- Optimization Summary -----------\n"); if (pr_optimize_eliminate_temps) summary_print("%d stores shortened\n", num_stores_shortened); if (pr_optimize_shorten_ifs) summary_print("%d ifs shortened\n", num_ifs_shortened); if (pr_optimize_nonvec_parms) summary_print("%d non-vector parms\n", num_nonvec_parms); if (pr_optimize_constant_names) summary_print("%d bytes of constant defs/names eliminated\n", num_constant_names); if (pr_optimize_defs) summary_print("%d duplicate defs eliminated\n", num_defs); if (pr_optimize_hash_strings) summary_print("%d bytes of duplicate strings eliminated\n", num_strings); if (pr_optimize_locals) summary_print("%d bytes of immediate and local names eliminated\n", num_locals_saved); if (pr_optimize_function_names) summary_print("%d bytes of function names eliminated\n", num_funcs_saved); if (pr_optimize_filenames) summary_print("%d bytes of filenames eliminated\n", num_files_saved); if (pr_optimize_unreferenced) summary_print("%d unreferenced global defs eliminated\n", num_unreferenced); if (pr_optimize_logicops) summary_print("%d logic jumps added\n", num_logic_jumps); if (pr_optimize_recycle) summary_print("%d temporary globals recycled\n", num_recycled); if (pr_optimize_constant_arithmetic) summary_print("%d constant arithmetic statements eliminated\n", num_constant_ops_saved); } } }
/* ============= Sin_WriteBSPFile Swaps the bsp file in place, so it should not be referenced again ============= */ void Sin_WriteBSPFile( char *filename ) { header = &outheader; memset( header, 0, sizeof( sin_dheader_t ) ); Sin_SwapBSPFile( true ); header->ident = LittleLong( SIN_BSPHEADER ); header->version = LittleLong( SIN_BSPVERSION ); wadfile = SafeOpenWrite( filename ); SafeWrite( wadfile, header, sizeof( sin_dheader_t ) ); // overwritten later #ifdef SIN Sin_AddLump( SIN_LUMP_PLANES, sin_dplanes, sin_numplanes, sizeof( sin_dplane_t ), SIN_MAX_MAP_PLANES ); Sin_AddLump( SIN_LUMP_LEAFS, sin_dleafs, sin_numleafs, sizeof( sin_dleaf_t ), SIN_MAX_MAP_LEAFS ); Sin_AddLump( SIN_LUMP_VERTEXES, sin_dvertexes, sin_numvertexes, sizeof( sin_dvertex_t ), SIN_MAX_MAP_VERTS ); Sin_AddLump( SIN_LUMP_NODES, sin_dnodes, sin_numnodes, sizeof( sin_dnode_t ), SIN_MAX_MAP_NODES ); Sin_AddLump( SIN_LUMP_TEXINFO, sin_texinfo, sin_numtexinfo, sizeof( sin_texinfo_t ), SIN_MAX_MAP_TEXINFO ); Sin_AddLump( SIN_LUMP_FACES, sin_dfaces, sin_numfaces, sizeof( sin_dface_t ), SIN_MAX_MAP_FACES ); Sin_AddLump( SIN_LUMP_BRUSHES, sin_dbrushes, sin_numbrushes, sizeof( sin_dbrush_t ), SIN_MAX_MAP_BRUSHES ); Sin_AddLump( SIN_LUMP_BRUSHSIDES, sin_dbrushsides, sin_numbrushsides, sizeof( sin_dbrushside_t ), SIN_MAX_MAP_BRUSHSIDES ); Sin_AddLump( SIN_LUMP_LEAFFACES, sin_dleaffaces, sin_numleaffaces, sizeof( sin_dleaffaces[0] ), SIN_MAX_MAP_LEAFFACES ); Sin_AddLump( SIN_LUMP_LEAFBRUSHES, sin_dleafbrushes, sin_numleafbrushes, sizeof( sin_dleafbrushes[0] ), SIN_MAX_MAP_LEAFBRUSHES ); Sin_AddLump( SIN_LUMP_SURFEDGES, sin_dsurfedges, sin_numsurfedges, sizeof( sin_dsurfedges[0] ), SIN_MAX_MAP_SURFEDGES ); Sin_AddLump( SIN_LUMP_EDGES, sin_dedges, sin_numedges, sizeof( sin_dedge_t ), SIN_MAX_MAP_EDGES ); Sin_AddLump( SIN_LUMP_MODELS, sin_dmodels, sin_nummodels, sizeof( sin_dmodel_t ), SIN_MAX_MAP_MODELS ); Sin_AddLump( SIN_LUMP_AREAS, sin_dareas, sin_numareas, sizeof( sin_darea_t ), SIN_MAX_MAP_AREAS ); Sin_AddLump( SIN_LUMP_AREAPORTALS, sin_dareaportals, sin_numareaportals, sizeof( sin_dareaportal_t ), SIN_MAX_MAP_AREAPORTALS ); Sin_AddLump( SIN_LUMP_LIGHTINFO, sin_lightinfo, sin_numlightinfo, sizeof( sin_lightvalue_t ), SIN_MAX_MAP_LIGHTINFO ); Sin_AddLump( SIN_LUMP_LIGHTING, sin_dlightdata, sin_lightdatasize, 1, SIN_MAX_MAP_LIGHTING ); Sin_AddLump( SIN_LUMP_VISIBILITY, sin_dvisdata, sin_visdatasize, 1, SIN_MAX_MAP_VISIBILITY ); Sin_AddLump( SIN_LUMP_ENTITIES, sin_dentdata, sin_entdatasize, 1, SIN_MAX_MAP_ENTSTRING ); Sin_AddLump( SIN_LUMP_POP, sin_dpop, sizeof( sin_dpop ), 1, sizeof( sin_dpop ) ); #else Sin_AddLump( SIN_LUMP_PLANES, sin_dplanes, sin_numplanes * sizeof( sin_dplane_t ) ); Sin_AddLump( SIN_LUMP_LEAFS, sin_dleafs, sin_numleafs * sizeof( sin_dleaf_t ) ); Sin_AddLump( SIN_LUMP_VERTEXES, sin_dvertexes, sin_numvertexes * sizeof( sin_dvertex_t ) ); Sin_AddLump( SIN_LUMP_NODES, sin_dnodes, sin_numnodes * sizeof( sin_dnode_t ) ); Sin_AddLump( SIN_LUMP_TEXINFO, sin_texinfo, sin_numtexinfo * sizeof( sin_texinfo_t ) ); Sin_AddLump( SIN_LUMP_FACES, sin_dfaces, sin_numfaces * sizeof( sin_dface_t ) ); Sin_AddLump( SIN_LUMP_BRUSHES, sin_dbrushes, sin_numbrushes * sizeof( sin_dbrush_t ) ); Sin_AddLump( SIN_LUMP_BRUSHSIDES, sin_dbrushsides, sin_numbrushsides * sizeof( sin_dbrushside_t ) ); Sin_AddLump( SIN_LUMP_LEAFFACES, sin_dleaffaces, sin_numleaffaces * sizeof( sin_dleaffaces[0] ) ); Sin_AddLump( SIN_LUMP_LEAFBRUSHES, sin_dleafbrushes, sin_numleafbrushes * sizeof( sin_dleafbrushes[0] ) ); Sin_AddLump( SIN_LUMP_SURFEDGES, sin_dsurfedges, sin_numsurfedges * sizeof( sin_dsurfedges[0] ) ); Sin_AddLump( SIN_LUMP_EDGES, sin_dedges, sin_numedges * sizeof( sin_dedge_t ) ); Sin_AddLump( SIN_LUMP_MODELS, sin_dmodels, sin_nummodels * sizeof( sin_dmodel_t ) ); Sin_AddLump( SIN_LUMP_AREAS, sin_dareas, sin_numareas * sizeof( sin_darea_t ) ); Sin_AddLump( SIN_LUMP_AREAPORTALS, sin_dareaportals, sin_numareaportals * sizeof( sin_dareaportal_t ) ); Sin_AddLump( SIN_LUMP_LIGHTING, sin_dlightdata, sin_lightdatasize ); Sin_AddLump( SIN_LUMP_VISIBILITY, sin_dvisdata, sin_visdatasize ); Sin_AddLump( SIN_LUMP_ENTITIES, sin_dentdata, sin_entdatasize ); Sin_AddLump( SIN_LUMP_POP, sin_dpop, sizeof( sin_dpop ) ); #endif fseek( wadfile, 0, SEEK_SET ); SafeWrite( wadfile, header, sizeof( sin_dheader_t ) ); fclose( wadfile ); }
/* =========== CopyQFiles =========== */ void CopyQFiles (int blocknum) { int i, p; char srcfile[1024]; char destfile[1024]; char name[1024]; packheader_t header; int dirlen; unsigned short crc; // create a pak file pf = pfiles; sprintf (destfile, "%spak%i.pak", gamedir, blocknum); packhandle = SafeOpenWrite (destfile); SafeWrite (packhandle, &header, sizeof(header)); blocknum++; for (i=0 ; i<numsounds ; i++) { if (precache_sounds_block[i] != blocknum) continue; sprintf (name, "sound/%s", precache_sounds[i]); sprintf (srcfile,"%s%s",gamedir, name); PackFile (srcfile, name); } for (i=0 ; i<nummodels ; i++) { if (precache_models_block[i] != blocknum) continue; sprintf (srcfile,"%s%s",gamedir, precache_models[i]); PackFile (srcfile, precache_models[i]); } for (i=0 ; i<numfiles ; i++) { if (precache_files_block[i] != blocknum) continue; sprintf (srcfile,"%s%s",gamedir, precache_files[i]); PackFile (srcfile, precache_files[i]); } header.id[0] = 'P'; header.id[1] = 'A'; header.id[2] = 'C'; header.id[3] = 'K'; dirlen = (byte *)pf - (byte *)pfiles; header.dirofs = LittleLong(ftell (packhandle)); header.dirlen = LittleLong(dirlen); SafeWrite (packhandle, pfiles, dirlen); fseek (packhandle, 0, SEEK_SET); SafeWrite (packhandle, &header, sizeof(header)); fclose (packhandle); // do a crc of the file CRC_Init (&crc); for (i=0 ; i<dirlen ; i++) CRC_ProcessByte (&crc, ((byte *)pfiles)[i]); i = pf - pfiles; printf ("%i files packed in %i bytes (%i crc)\n",i, packbytes, crc); }
/* ============== SaveFile ============== */ void SaveFile (char *filename, void *buffer, int count) { FileHandle_t f = SafeOpenWrite (filename); SafeWrite (f, buffer, count); g_pFileSystem->Close (f); }
/* =============== FinishModel =============== */ void FinishModel (void) { FILE *modelouthandle; int i; char name[1024]; if (!model.num_frames) return; // // copy to release directory tree if doing a release build // if (g_release) { if (modelname[0]) sprintf (name, "%s", modelname); else sprintf (name, "%s/tris.md2", cdpartial); ReleaseFile (name); for (i=0 ; i<model.num_skins ; i++) { ReleaseFile (g_skins[i]); } model.num_frames = 0; return; } // // write the model output file // if (modelname[0]) sprintf (name, "%s%s", g_outputDir, modelname); else sprintf (name, "%s/tris.md2", g_outputDir); printf ("saving to %s\n", name); CreatePath (name); modelouthandle = SafeOpenWrite (name); #if 1 if(jointed != NOT_JOINTED) WriteJointedModelFile(modelouthandle); else #endif WriteModelFile(modelouthandle); printf ("%3dx%3d skin\n", model.skinwidth, model.skinheight); printf ("First frame boundaries:\n"); printf (" minimum x: %3f\n", g_frames[0].mins[0]); printf (" maximum x: %3f\n", g_frames[0].maxs[0]); printf (" minimum y: %3f\n", g_frames[0].mins[1]); printf (" maximum y: %3f\n", g_frames[0].maxs[1]); printf (" minimum z: %3f\n", g_frames[0].mins[2]); printf (" maximum z: %3f\n", g_frames[0].maxs[2]); printf ("%4d vertices\n", model.num_xyz); printf ("%4d triangles\n", model.num_tris); printf ("%4d frame\n", model.num_frames); printf ("%4d glverts\n", numglverts); printf ("%4d glcmd\n", model.num_glcmds); printf ("%4d skins\n", model.num_skins); printf ("file size: %d\n", (int)ftell (modelouthandle) ); printf ("---------------------\n"); fclose (modelouthandle); // finish writing header file H_printf("\n"); // scale_up is usefull to allow step distances to be adjusted H_printf("#define MODEL_SCALE\t\t%f\n", scale_up); fclose (headerouthandle); headerouthandle = NULL; }