Debian Codegraph @

tools/quake3/q3data/q3data.c

raw symbols SCIP
1 /*
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
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.
11
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.
16
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
20 */
21
22 #include "globaldefs.h"
23
24 #if GDEF_OS_WINDOWS
25 #include <io.h>
26 #endif // GDEF_OS_WINDOWS
27
28 #include "q3data.h"
29 #include "md3lib.h"
30
31 #include "vfs.h"
32
33 qboolean g_verbose;
34 qboolean g_stripify = qtrue;
35 qboolean g_release; // don't grab, copy output data to new tree
36 char g_releasedir[1024]; // c:\quake2\baseq2, etc
37 qboolean g_archive; // don't grab, copy source data to new tree
38 char g_only[256]; // if set, only grab this cd
39 qboolean g_skipmodel; // set true when a cd is not g_only
40
41 // bogus externs for some TA hacks (common/ using them against q3map)
42 char *moddir = NULL;
43
44 #if !GDEF_OS_WINDOWS
45 #define strlwr strlower
46 #endif // !GDEF_OS_WINDOWS
47
48 /*
49 =======================================================
50
51 PAK FILES
52
53 =======================================================
54 */
55
56 unsigned Com_BlockChecksum( void *buffer, int length );
57
58 typedef struct
59 {
60 char name[56];
61 int filepos, filelen;
62 } packfile_t;
63
64 typedef struct
65 {
66 char id[4];
67 int dirofs;
68 int dirlen;
69 } packheader_t;
70
71 packfile_t pfiles[16384];
72 FILE *pakfile;
73 packfile_t *pf;
74 packheader_t pakheader;
75
76 /*
77 ==============
78 ReleaseFile
79
80 Filename should be gamedir reletive.
81 Either copies the file to the release dir, or adds it to
82 the pak file.
83 ==============
84 */
85 void ReleaseFile( char *filename ){
86 char source[1024];
87 char dest[1024];
88
89 if ( !g_release ) {
90 return;
91 }
92
93 sprintf( source, "%s%s", gamedir, filename );
94 sprintf( dest, "%s/%s", g_releasedir, filename );
95 printf( "copying to %s\n", dest );
96 QCopyFile( source, dest );
97 return;
98 }
99
100 typedef struct
101 {
102 // shader
103 // opaque
104 // opaque 2
105 // blend
106 // blend 2
107 char names[5][1024];
108 int num;
109 } ShaderFiles_t;
110
111 ShaderFiles_t s_shaderFiles;
112
113 void FindShaderFiles( char *filename ){
114 char buffer[1024];
115 char stripped[1024];
116 char linebuffer[1024];
117 int len, i;
118 char *buf;
119 char *diffuseExtensions[] =
120 {
121 ".TGA",
122 ".WAL",
123 ".PCX",
124 0
125 };
126 char *otherExtensions[] =
127 {
128 ".specular.TGA",
129 ".blend.TGA",
130 ".alpha.TGA",
131 0
132 };
133
134 s_shaderFiles.num = 0;
135
136 strcpy( stripped, filename );
137 if ( strrchr( stripped, '.' ) ) {
138 *strrchr( stripped, '.' ) = 0;
139 }
140 strcat( stripped, ".shader" );
141
142 if ( FileExists( stripped ) ) {
143 char *p;
144 char mapa[512], mapb[512];
145
146 strcpy( s_shaderFiles.names[s_shaderFiles.num], stripped );
147 s_shaderFiles.num++;
148
149 // load and parse
150 len = LoadFile( stripped, (void **)&buf );
151
152 p = buf;
153
154 while ( p - buf < len )
155 {
156 i = 0;
157
158 // skip spaces
159 while ( *p == ' ' || *p == '\n' || *p == '\t' )
160 p++;
161
162 // grab rest of the line
163 while ( *p != 0 && *p != '\n' )
164 {
165 linebuffer[i] = *p;
166 i++;
167 p++;
168 }
169 if ( *p == '\n' ) {
170 p++;
171 }
172 linebuffer[i] = 0;
173
174 strlwr( linebuffer );
175
176 // see if the line specifies an opaque map or blendmap
177 if ( strstr( linebuffer, "opaquemap" ) == linebuffer ||
178 strstr( linebuffer, "blendmap" ) == linebuffer ) {
179 int j;
180
181 i = 0;
182
183 mapa[0] = mapb[0] = 0;
184
185 // skip past the keyword
186 while ( linebuffer[i] != ' ' && linebuffer[i] != '\t' && linebuffer[i] )
187 i++;
188 // skip past spaces
189 while ( ( linebuffer[i] == ' ' || linebuffer[i] == '\t' ) && linebuffer[i] )
190 i++;
191
192 // grab first map name
193 j = 0;
194 while ( linebuffer[i] != ' ' && linebuffer[i] != '\t' && linebuffer[i] )
195 {
196 mapa[j] = linebuffer[i];
197 j++;
198 i++;
199 }
200 mapa[j] = 0;
201
202 // skip past spaces
203 while ( ( linebuffer[i] == ' ' || linebuffer[i] == '\t' ) && linebuffer[i] )
204 i++;
205
206 // grab second map name
207 j = 0;
208 while ( linebuffer[i] != ' ' && linebuffer[i] != '\t' && linebuffer[i] )
209 {
210 mapb[j] = linebuffer[i];
211 j++;
212 i++;
213 }
214 mapb[j] = 0;
215
216 // store map names
217 if ( mapa[0] != 0 && mapa[0] != '-' ) {
218 sprintf( s_shaderFiles.names[s_shaderFiles.num], "%s%s", gamedir, mapa );
219 s_shaderFiles.num++;
220 }
221 if ( mapb[0] != 0 && mapb[0] != '-' && mapb[0] != '^' && mapb[0] != '*' ) {
222 sprintf( s_shaderFiles.names[s_shaderFiles.num], "%s%s", gamedir, mapb );
223 s_shaderFiles.num++;
224 }
225 }
226 }
227 }
228 else
229 {
230 if ( strrchr( stripped, '.' ) ) {
231 *strrchr( stripped, '.' ) = 0;
232 }
233
234 // look for diffuse maps
235 for ( i = 0; i < 3; i++ )
236 {
237 strcpy( buffer, stripped );
238 strcat( buffer, diffuseExtensions[i] );
239 if ( FileExists( buffer ) ) {
240 strcpy( s_shaderFiles.names[s_shaderFiles.num], buffer );
241 s_shaderFiles.num++;
242 break;
243 }
244 }
245 for ( i = 0; i < 3; i++ )
246 {
247 strcpy( buffer, stripped );
248 strcat( buffer, otherExtensions[i] );
249 if ( FileExists( buffer ) ) {
250 strcpy( s_shaderFiles.names[s_shaderFiles.num], buffer );
251 s_shaderFiles.num++;
252 }
253 }
254 }
255 }
256
257 /*
258 ==============
259 ReleaseShader
260
261 Copies all needed files for a shader to the release directory
262 ==============
263 */
264 void ReleaseShader( char *filename ){
265 char fullpath[1024];
266 char dest[1024];
267 char stripped[1024];
268 int i;
269
270 sprintf( fullpath, "%s%s", gamedir, filename );
271
272 FindShaderFiles( fullpath );
273
274 for ( i = 0; i < s_shaderFiles.num; i++ )
275 {
276 strcpy( stripped, s_shaderFiles.names[i] );
277 if ( strstr( stripped, gamedir ) ) {
278 memmove( stripped, stripped + strlen( gamedir ), strlen( stripped ) );
279 }
280 sprintf( dest, "%s/%s", g_releasedir, stripped );
281 printf( "copying to %s\n", dest );
282 QCopyFile( s_shaderFiles.names[i], dest );
283 }
284 }
285
286 /*
287 ===============
288 Cmd_File
289
290 This is only used to cause a file to be copied during a release
291 build (default.cfg, maps, etc)
292 ===============
293 */
294 void Cmd_File( void ){
295 GetToken( qfalse );
296 ReleaseFile( token );
297 }
298
299 /*
300 ===============
301 PackDirectory_r
302
303 ===============
304 */
305 #if GDEF_OS_WINDOWS
306 #include "io.h"
307 void PackDirectory_r( char *dir ){
308 struct _finddata_t fileinfo;
309 int handle;
310 char dirstring[1024];
311 char filename[1024];
312
313 sprintf( dirstring, "%s%s/*.*", gamedir, dir );
314
315 handle = _findfirst( dirstring, &fileinfo );
316 if ( handle == -1 ) {
317 return;
318 }
319
320 do
321 {
322 sprintf( filename, "%s/%s", dir, fileinfo.name );
323 if ( fileinfo.attrib & _A_SUBDIR ) { // directory
324 if ( fileinfo.name[0] != '.' ) { // don't pak . and ..
325 PackDirectory_r( filename );
326 }
327 continue;
328 }
329 // copy or pack the file
330 ReleaseFile( filename );
331 } while ( _findnext( handle, &fileinfo ) != -1 );
332
333 _findclose( handle );
334 }
335 #else
336
337 #include <sys/types.h>
338
339 #include <dirent.h>
340
341 void PackDirectory_r( char *dir ){
342 #ifdef NeXT
343 struct direct **namelist, *ent;
344 #else
345 struct dirent **namelist, *ent;
346 #endif
347 int count;
348 struct stat st;
349 int i;
350 int len;
351 char fullname[1024];
352 char dirstring[1024];
353 char *name;
354
355 sprintf( dirstring, "%s%s", gamedir, dir );
356 count = scandir( dirstring, &namelist, NULL, NULL );
357
358 for ( i = 0 ; i < count ; i++ )
359 {
360 ent = namelist[i];
361 name = ent->d_name;
362
363 if ( name[0] == '.' ) {
364 continue;
365 }
366
367 sprintf( fullname, "%s/%s", dir, name );
368 sprintf( dirstring, "%s%s/%s", gamedir, dir, name );
369
370 if ( stat( dirstring, &st ) == -1 ) {
371 Error( "fstating %s", pf->name );
372 }
373 if ( st.st_mode & S_IFDIR ) { // directory
374 PackDirectory_r( fullname );
375 continue;
376 }
377
378 // copy or pack the file
379 ReleaseFile( fullname );
380 }
381 }
382 #endif
383
384
385 /*
386 ===============
387 Cmd_Dir
388
389 This is only used to cause a directory to be copied during a
390 release build (sounds, etc)
391 ===============
392 */
393 void Cmd_Dir( void ){
394 GetToken( qfalse );
395 PackDirectory_r( token );
396 }
397
398 //========================================================================
399
400 #define MAX_RTEX 16384
401 int numrtex;
402 char rtex[MAX_RTEX][64];
403
404 void ReleaseTexture( char *name ){
405 int i;
406 char path[1024];
407
408 for ( i = 0 ; i < numrtex ; i++ )
409 if ( !Q_stricmp( name, rtex[i] ) ) {
410 return;
411 }
412
413 if ( numrtex == MAX_RTEX ) {
414 Error( "numrtex == MAX_RTEX" );
415 }
416
417 strcpy( rtex[i], name );
418 numrtex++;
419
420 sprintf( path, "textures/%s.wal", name );
421 ReleaseFile( path );
422 }
423
424 /*
425 ===============
426 Cmd_Maps
427
428 Only relevent for release and pak files.
429 Releases the .bsp files for the maps, and scans all of the files to
430 build a list of all textures used, which are then released.
431 ===============
432 */
433 void Cmd_Maps( void ){
434 char map[1024];
435
436 while ( TokenAvailable() )
437 {
438 GetToken( qfalse );
439 sprintf( map, "maps/%s.bsp", token );
440 ReleaseFile( map );
441
442 if ( !g_release ) {
443 continue;
444 }
445
446 // get all the texture references
447 sprintf( map, "%smaps/%s.bsp", gamedir, token );
448 LoadBSPFile( map );
449 }
450 }
451
452
453 //==============================================================
454
455 /*
456 ===============
457 ParseScript
458 ===============
459 */
460 void ParseScript( void ){
461 while ( 1 )
462 {
463 do
464 { // look for a line starting with a $ command
465 GetToken( qtrue );
466 if ( endofscript ) {
467 return;
468 }
469 if ( token[0] == '$' ) {
470 break;
471 }
472 while ( TokenAvailable() )
473 GetToken( qfalse );
474 } while ( 1 );
475
476 //
477 // model commands
478 //
479 if ( !strcmp( token, "$modelname" ) ) {
480 Cmd_Modelname();
481 }
482 else if ( !strcmp( token, "$base" ) ) {
483 Cmd_Base();
484 }
485 else if ( !strcmp( token, "$exit" ) ) {
486 break;
487 }
488 else if ( !strcmp( token, "$3dsconvert" ) ) {
489 Cmd_3DSConvert();
490 }
491 else if ( !strcmp( token, "$spritebase" ) ) {
492 Cmd_SpriteBase();
493 }
494 else if ( !strcmp( token, "$cd" ) ) {
495 Cmd_Cd();
496 }
497 else if ( !strcmp( token, "$origin" ) ) {
498 Cmd_Origin();
499 }
500 else if ( !strcmp( token, "$scale" ) ) {
501 Cmd_ScaleUp();
502 }
503 else if ( !strcmp( token, "$frame" ) ) {
504 Cmd_Frame();
505 }
506 else if ( !strcmp( token, "$skin" ) ) {
507 Cmd_Skin();
508 }
509 else if ( !strcmp( token, "$spriteshader" ) ) {
510 Cmd_SpriteShader();
511 }
512 else if ( !strcmp( token, "$aseconvert" ) ) {
513 Cmd_ASEConvert( qfalse );
514 }
515 else if ( !strcmp( token, "$aseanimconvert" ) ) {
516 Cmd_ASEConvert( qtrue );
517 }
518
519 //
520 // image commands
521 //
522 else if ( !strcmp( token, "$grab" ) ) {
523 Cmd_Grab();
524 }
525 else if ( !strcmp( token, "$raw" ) ) {
526 Cmd_Raw();
527 }
528 else if ( !strcmp( token, "$colormap" ) ) {
529 Cmd_Colormap();
530 }
531 else if ( !strcmp( token, "$environment" ) ) {
532 Cmd_Environment();
533 }
534
535 //
536 // video
537 //
538 else if ( !strcmp( token, "$video" ) ) {
539 Cmd_Video();
540 }
541 //
542 // misc
543 //
544 else if ( !strcmp( token, "$file" ) ) {
545 Cmd_File();
546 }
547 else if ( !strcmp( token, "$dir" ) ) {
548 Cmd_Dir();
549 }
550 else if ( !strcmp( token, "$maps" ) ) {
551 Cmd_Maps();
552 }
553 else{
554 Error( "bad command %s\n", token );
555 }
556 }
557 }
558
559 //=======================================================
560
561 /*
562 ==============
563 main
564 ==============
565 */
566 int main( int argc, char **argv ){
567 static int i; // VC4.2 compiler bug if auto...
568 char path[1024];
569
570 // using GtkRadiant's versioning next to Id's versioning
571 printf( "Q3Data - (c) 1999 Id Software Inc.\n" );
572 printf( RADIANT_NAME " - v" RADIANT_VERSION " " __DATE__ "\n" );
573
574 ExpandWildcards( &argc, &argv );
575
576 for ( i = 1 ; i < argc ; i++ )
577 {
578 if ( !strcmp( argv[i], "-archive" ) ) {
579 archive = qtrue;
580 strcpy( archivedir, argv[i + 1] );
581 printf( "Archiving source to: %s\n", archivedir );
582 i++;
583 }
584 else if ( !strcmp( argv[i], "-release" ) ) {
585 g_release = qtrue;
586 strcpy( g_releasedir, argv[i + 1] );
587 printf( "Copy output to: %s\n", g_releasedir );
588 i++;
589 }
590 else if ( !strcmp( argv[i], "-nostrips" ) ) {
591 g_stripify = qfalse;
592 printf( "Not optimizing for strips\n" );
593 }
594 else if ( !strcmp( argv[i], "-writedir" ) ) {
595 strcpy( writedir, argv[i + 1] );
596 printf( "Write output to: %s\n", writedir );
597 i++;
598 }
599 else if ( !strcmp( argv[i], "-verbose" ) ) {
600 g_verbose = qtrue;
601 }
602 else if ( !strcmp( argv[i], "-dump" ) ) {
603 printf( "Dumping contents of: '%s'\n", argv[i + 1] );
604 if ( strstr( argv[i + 1], ".md3" ) ) {
605 MD3_Dump( argv[i + 1] );
606 }
607 else
608 {
609 Error( "Do not know how to dump the contents of '%s'\n", argv[i + 1] );
610 }
611 i++;
612 }
613 else if ( !strcmp( argv[i], "-3dsconvert" ) ) {
614 // NOTE TTimo this is broken, tried on a sample .3ds
615 // what happens .. it calls the Convert3DStoMD3,
616 // which calls the scriptlib function in non initialized state .. and crashes
617 printf( "Converting %s.3DS to %s.MD3\n", argv[i + 1], argv[i + 1] );
618 SetQdirFromPath( argv[i + 1] );
619 vfsInitDirectory( gamedir );
620 Convert3DStoMD3( argv[i + 1] );
621 i++;
622 }
623 else if ( !strcmp( argv[i], "-only" ) ) {
624 strcpy( g_only, argv[i + 1] );
625 printf( "Only grabbing %s\n", g_only );
626 i++;
627 }
628 else if ( !strcmp( argv[i], "-gamedir" ) ) {
629 strcpy( gamedir, argv[i + 1] );
630 i++;
631 }
632 else if ( argv[i][0] == '-' ) {
633 Error( "Unknown option \"%s\"", argv[i] );
634 }
635 else{
636 break;
637 }
638 }
639
640 if ( i == argc ) {
641 Error( "usage: q3data [-archive <directory>] [-dump <file.md3>] [-release <directory>] [-only <model>] [-3dsconvert <file.3ds>] [-verbose] [file.qdt]" );
642 }
643
644 for ( ; i < argc ; i++ )
645 {
646 printf( "--------------- %s ---------------\n", argv[i] );
647 // load the script
648 strcpy( path, argv[i] );
649 DefaultExtension( path, ".qdt" );
650 if ( !gamedir[0] ) {
651 SetQdirFromPath( path );
652 }
653 // NOTE TTimo
654 // q3data went through a partial conversion to use the vfs
655 // it was never actually tested before 1.1.1
656 // the code is still mostly using direct file access calls
657 vfsInitDirectory( gamedir );
658 LoadScriptFile( ExpandArg( path ), -1 );
659
660 //
661 // parse it
662 //
663 ParseScript();
664
665 // write out the last model
666 FinishModel( TYPE_UNKNOWN );
667 }
668
669 return 0;
670 }