2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 qboolean g_compress_pak;
26 qboolean g_release; // don't grab, copy output data to new tree
27 qboolean g_pak; // if true, copy to pak instead of release
28 char g_releasedir[1024]; // c:\quake2\baseq2, etc
29 qboolean g_archive; // don't grab, copy source data to new tree
31 char g_only[256]; // if set, only grab this cd
32 qboolean g_skipmodel; // set true when a cd is not g_only
34 char *ext_3ds = "3ds";
35 char *ext_tri = "tri";
38 char game[64] = "quake2";
40 void InitPaths( int *argc, char **argv );
43 =======================================================
47 =======================================================
50 unsigned Com_BlockChecksum( void *buffer, int length );
65 packfile_t pfiles[16384];
68 packheader_t pakheader;
77 void BeginPak( char *outname ){
82 pakfile = SafeOpenWrite( outname );
84 // leave space for header
85 SafeWrite( pakfile, &pakheader, sizeof( pakheader ) );
95 Filename should be gamedir reletive.
96 Either copies the file to the release dir, or adds it to
100 void ReleaseFile( char *filename ){
110 sprintf( source, "%s%s", gamedir, filename );
112 if ( !g_pak ) { // copy it
113 sprintf( dest, "%s/%s", g_releasedir, filename );
114 printf( "copying to %s\n", dest );
115 QCopyFile( source, dest );
120 printf( "paking %s\n", filename );
121 if ( strlen( filename ) >= sizeof( pf->name ) ) {
122 Error( "Filename too long for pak: %s", filename );
125 len = LoadFile( source, (void **)&buf );
127 if ( g_compress_pak && len < 4096 * 1024 ) {
129 cblock_t Huffman( cblock_t in );
136 if ( out.count < in.count ) {
137 printf( " compressed from %i to %i\n", in.count, out.count );
147 strcpy( pf->name, filename );
148 pf->filepos = LittleLong( ftell( pakfile ) );
149 pf->filelen = LittleLong( len );
152 SafeWrite( pakfile, buf, len );
163 void FinishPak( void ){
173 pakheader.id[0] = 'P';
174 pakheader.id[1] = 'A';
175 pakheader.id[2] = 'C';
176 pakheader.id[3] = 'K';
177 dirlen = (byte *)pf - (byte *)pfiles;
178 pakheader.dirofs = LittleLong( ftell( pakfile ) );
179 pakheader.dirlen = LittleLong( dirlen );
181 checksum = Com_BlockChecksum( (void *)pfiles, dirlen );
183 SafeWrite( pakfile, pfiles, dirlen );
185 i = ftell( pakfile );
187 fseek( pakfile, 0, SEEK_SET );
188 SafeWrite( pakfile, &pakheader, sizeof( pakheader ) );
192 printf( "%i files packed in %i bytes\n",d, i );
193 printf( "checksum: 0x%x\n", checksum );
201 This is only used to cause a file to be copied during a release
202 build (default.cfg, maps, etc)
205 void Cmd_File( void ){
207 ReleaseFile( token );
218 void PackDirectory_r( char *dir ){
219 struct _finddata_t fileinfo;
221 char dirstring[1024];
224 sprintf( dirstring, "%s%s/*.*", gamedir, dir );
226 handle = _findfirst( dirstring, &fileinfo );
227 if ( handle == -1 ) {
233 sprintf( filename, "%s/%s", dir, fileinfo.name );
234 if ( fileinfo.attrib & _A_SUBDIR ) { // directory
235 if ( fileinfo.name[0] != '.' ) { // don't pak . and ..
236 PackDirectory_r( filename );
240 // copy or pack the file
241 ReleaseFile( filename );
242 } while ( _findnext( handle, &fileinfo ) != -1 );
244 _findclose( handle );
248 #include <sys/types.h>
251 void PackDirectory_r( char *dir ){
253 struct direct **namelist, *ent;
255 struct dirent **namelist, *ent;
262 char dirstring[1024];
265 sprintf( dirstring, "%s%s", gamedir, dir );
266 count = scandir( dirstring, &namelist, NULL, NULL );
268 for ( i = 0 ; i < count ; i++ )
273 if ( name[0] == '.' ) {
277 sprintf( fullname, "%s/%s", dir, name );
278 sprintf( dirstring, "%s%s/%s", gamedir, dir, name );
280 if ( stat( dirstring, &st ) == -1 ) {
281 Error( "fstating %s", pf->name );
283 if ( st.st_mode & S_IFDIR ) { // directory
284 PackDirectory_r( fullname );
288 // copy or pack the file
289 ReleaseFile( fullname );
299 This is only used to cause a directory to be copied during a
300 release build (sounds, etc)
303 void Cmd_Dir( void ){
305 PackDirectory_r( token );
308 //========================================================================
310 #define MAX_RTEX 16384
312 char rtex[MAX_RTEX][64];
314 void ReleaseTexture( char *name ){
318 for ( i = 0 ; i < numrtex ; i++ )
319 if ( !Q_strncasecmp( name, rtex[i], strlen( name ) ) ) {
323 if ( numrtex == MAX_RTEX ) {
324 Error( "numrtex == MAX_RTEX" );
327 strcpy( rtex[i], name );
330 sprintf( path, "textures/%s.wal", name );
338 Only relevent for release and pak files.
339 Releases the .bsp files for the maps, and scans all of the files to
340 build a list of all textures used, which are then released.
343 void Cmd_Maps( void ){
347 while ( TokenAvailable() )
350 sprintf( map, "maps/%s.bsp", token );
357 // get all the texture references
358 sprintf( map, "%smaps/%s.bsp", gamedir, token );
359 LoadBSPFileTexinfo( map );
360 for ( i = 0 ; i < numtexinfo ; i++ )
361 ReleaseTexture( texinfo[i].texture );
366 //==============================================================
373 void ParseScript( void ){
377 { // look for a line starting with a $ command
382 if ( token[0] == '$' ) {
385 while ( TokenAvailable() )
392 if ( !strcmp( token, "$modelname" ) ) {
395 else if ( !strcmp( token, "$base" ) ) {
398 else if ( !strcmp( token, "$cd" ) ) {
401 else if ( !strcmp( token, "$origin" ) ) {
404 else if ( !strcmp( token, "$scale" ) ) {
407 else if ( !strcmp( token, "$frame" ) ) {
410 else if ( !strcmp( token, "$skin" ) ) {
413 else if ( !strcmp( token, "$skinsize" ) ) {
419 else if ( !strcmp( token, "$spritename" ) ) {
422 else if ( !strcmp( token, "$load" ) ) {
425 else if ( !strcmp( token, "$spriteframe" ) ) {
431 else if ( !strcmp( token, "$grab" ) ) {
434 else if ( !strcmp( token, "$raw" ) ) {
437 else if ( !strcmp( token, "$colormap" ) ) {
440 else if ( !strcmp( token, "$mippal" ) ) {
443 else if ( !strcmp( token, "$mipdir" ) ) {
446 else if ( !strcmp( token, "$mip" ) ) {
449 else if ( !strcmp( token, "$environment" ) ) {
455 else if ( !strcmp( token, "$video" ) ) {
461 else if ( !strcmp( token, "$file" ) ) {
464 else if ( !strcmp( token, "$dir" ) ) {
467 else if ( !strcmp( token, "$maps" ) ) {
470 else if ( !strcmp( token, "$alphalight" ) ) {
473 else if ( !strcmp( token, "$inverse16table" ) ) {
474 Cmd_Inverse16Table();
477 Error( "bad command %s\n", token );
482 //=======================================================
489 int main( int argc, char **argv ){
490 static int i; // VC4.2 compiler bug if auto...
493 ExpandWildcards( &argc, &argv );
495 InitPaths( &argc, argv );
497 for ( i = 1 ; i < argc ; i++ )
499 if ( !strcmp( argv[i], "-archive" ) ) {
500 // -archive f:/quake2/release/dump_11_30
502 strcpy( archivedir, argv[i + 1] );
503 printf( "Archiving source to: %s\n", archivedir );
506 else if ( !strcmp( argv[i], "-release" ) ) {
508 strcpy( g_releasedir, argv[i + 1] );
509 printf( "Copy output to: %s\n", g_releasedir );
512 else if ( !strcmp( argv[i], "-compress" ) ) {
513 g_compress_pak = true;
514 printf( "Compressing pakfile\n" );
516 else if ( !strcmp( argv[i], "-pak" ) ) {
519 printf( "Building pakfile: %s\n", argv[i + 1] );
520 BeginPak( argv[i + 1] );
523 else if ( !strcmp( argv[i], "-only" ) ) {
524 strcpy( g_only, argv[i + 1] );
525 printf( "Only grabbing %s\n", g_only );
528 else if ( !strcmp( argv[i], "-3ds" ) ) {
530 printf( "loading .3ds files\n" );
532 else if ( argv[i][0] == '-' ) {
533 Error( "Unknown option \"%s\"", argv[i] );
541 Error( "usage: %s [-archive <directory>] [-release <directory>] [-only <model>] [-3ds] file.qgr", argv[ 0 ] );
545 trifileext = ext_3ds;
548 trifileext = ext_tri;
551 for ( ; i < argc ; i++ )
553 printf( "--------------- %s ---------------\n", argv[i] );
555 strcpy( path, argv[i] );
556 DefaultExtension( path, ".qdt" );
557 SetQdirFromPath( path );
558 LoadScriptFile( ExpandArg( path ) );
565 // write out the last model