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