2 ===========================================================================
3 Copyright (C) 1997-2006 Id Software, Inc.
5 This file is part of Quake 2 Tools source code.
7 Quake 2 Tools source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 Quake 2 Tools source code is distributed in the hope that it will be
13 useful, 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 Quake 2 Tools source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
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";
39 =======================================================
43 =======================================================
46 unsigned Com_BlockChecksum (void *buffer, int length);
61 packfile_t pfiles[16384];
64 packheader_t pakheader;
73 void BeginPak (char *outname)
78 pakfile = SafeOpenWrite (outname);
80 // leave space for header
81 SafeWrite (pakfile, &pakheader, sizeof(pakheader));
91 Filename should be gamedir reletive.
92 Either copies the file to the release dir, or adds it to
96 void ReleaseFile (char *filename)
106 sprintf (source, "%s%s", gamedir, filename);
110 sprintf (dest, "%s/%s", g_releasedir, filename);
111 printf ("copying to %s\n", dest);
112 QCopyFile (source, dest);
117 printf ("paking %s\n", filename);
118 if (strlen(filename) >= sizeof(pf->name))
119 Error ("Filename too long for pak: %s", filename);
121 len = LoadFile (source, (void **)&buf);
123 if (g_compress_pak && len < 4096*1024 )
126 cblock_t Huffman (cblock_t in);
133 if (out.count < in.count)
135 printf (" compressed from %i to %i\n", in.count, out.count);
144 strcpy (pf->name, filename);
145 pf->filepos = LittleLong(ftell(pakfile));
146 pf->filelen = LittleLong(len);
149 SafeWrite (pakfile, buf, len);
160 void FinishPak (void)
170 pakheader.id[0] = 'P';
171 pakheader.id[1] = 'A';
172 pakheader.id[2] = 'C';
173 pakheader.id[3] = 'K';
174 dirlen = (byte *)pf - (byte *)pfiles;
175 pakheader.dirofs = LittleLong(ftell(pakfile));
176 pakheader.dirlen = LittleLong(dirlen);
178 checksum = Com_BlockChecksum ( (void *)pfiles, dirlen );
180 SafeWrite (pakfile, pfiles, dirlen);
184 fseek (pakfile, 0, SEEK_SET);
185 SafeWrite (pakfile, &pakheader, sizeof(pakheader));
189 printf ("%i files packed in %i bytes\n",d, i);
190 printf ("checksum: 0x%x\n", checksum);
198 This is only used to cause a file to be copied during a release
199 build (default.cfg, maps, etc)
216 void PackDirectory_r (char *dir)
218 struct _finddata_t fileinfo;
220 char dirstring[1024];
223 sprintf (dirstring, "%s%s/*.*", gamedir, dir);
225 handle = _findfirst (dirstring, &fileinfo);
231 sprintf (filename, "%s/%s", dir, fileinfo.name);
232 if (fileinfo.attrib & _A_SUBDIR)
234 if (fileinfo.name[0] != '.') // don't pak . and ..
235 PackDirectory_r (filename);
238 // copy or pack the file
239 ReleaseFile (filename);
240 } while (_findnext( handle, &fileinfo ) != -1);
246 #include <sys/types.h>
250 #include <sys/dirent.h>
253 void PackDirectory_r (char *dir)
256 struct direct **namelist, *ent;
258 struct dirent **namelist, *ent;
265 char dirstring[1024];
268 sprintf (dirstring, "%s%s", gamedir, dir);
269 count = scandir(dirstring, &namelist, NULL, NULL);
271 for (i=0 ; i<count ; i++)
279 sprintf (fullname, "%s/%s", dir, name);
280 sprintf (dirstring, "%s%s/%s", gamedir, dir, name);
282 if (stat (dirstring, &st) == -1)
283 Error ("fstating %s", pf->name);
284 if (st.st_mode & S_IFDIR)
286 PackDirectory_r (fullname);
290 // copy or pack the file
291 ReleaseFile (fullname);
301 This is only used to cause a directory to be copied during a
302 release build (sounds, etc)
308 PackDirectory_r (token);
311 //========================================================================
313 #define MAX_RTEX 16384
315 char rtex[MAX_RTEX][64];
317 void ReleaseTexture (char *name)
322 for (i=0 ; i<numrtex ; i++)
323 if (!Q_strcasecmp(name, rtex[i]))
326 if (numrtex == MAX_RTEX)
327 Error ("numrtex == MAX_RTEX");
329 strcpy (rtex[i], name);
332 sprintf (path, "textures/%s.wal", name);
340 Only relevent for release and pak files.
341 Releases the .bsp files for the maps, and scans all of the files to
342 build a list of all textures used, which are then released.
350 while (TokenAvailable ())
353 sprintf (map, "maps/%s.bsp", token);
359 // get all the texture references
360 sprintf (map, "%smaps/%s.bsp", gamedir, token);
361 LoadBSPFileTexinfo (map);
362 for (i=0 ; i<numtexinfo ; i++)
363 ReleaseTexture (texinfo[i].texture);
368 //==============================================================
375 void ParseScript (void)
380 { // look for a line starting with a $ command
386 while (TokenAvailable())
393 if (!strcmp (token, "$modelname"))
395 else if (!strcmp (token, "$base"))
397 else if (!strcmp (token, "$cd"))
399 else if (!strcmp (token, "$origin"))
401 else if (!strcmp (token, "$scale"))
403 else if (!strcmp (token, "$frame"))
405 else if (!strcmp (token, "$skin"))
407 else if (!strcmp (token, "$skinsize"))
412 else if (!strcmp (token, "$spritename"))
414 else if (!strcmp (token, "$load"))
416 else if (!strcmp (token, "$spriteframe"))
421 else if (!strcmp (token, "$grab"))
423 else if (!strcmp (token, "$raw"))
425 else if (!strcmp (token, "$colormap"))
427 else if (!strcmp (token, "$mippal"))
429 else if (!strcmp (token, "$mipdir"))
431 else if (!strcmp (token, "$mip"))
433 else if (!strcmp (token, "$environment"))
438 else if (!strcmp (token, "$video"))
443 else if (!strcmp (token, "$file"))
445 else if (!strcmp (token, "$dir"))
447 else if (!strcmp (token, "$maps"))
449 else if (!strcmp (token, "$alphalight"))
451 else if (!strcmp (token, "$inverse16table" ))
452 Cmd_Inverse16Table();
454 Error ("bad command %s\n", token);
458 //=======================================================
465 int main (int argc, char **argv)
467 static int i; // VC4.2 compiler bug if auto...
470 ExpandWildcards (&argc, &argv);
472 for (i=1 ; i<argc ; i++)
474 if (!strcmp(argv[i], "-archive"))
476 // -archive f:/quake2/release/dump_11_30
478 strcpy (archivedir, argv[i+1]);
479 printf ("Archiving source to: %s\n", archivedir);
482 else if (!strcmp(argv[i], "-release"))
485 strcpy (g_releasedir, argv[i+1]);
486 printf ("Copy output to: %s\n", g_releasedir);
489 else if (!strcmp(argv[i], "-compress"))
491 g_compress_pak = true;
492 printf ("Compressing pakfile\n");
494 else if (!strcmp(argv[i], "-pak"))
498 printf ("Building pakfile: %s\n", argv[i+1]);
499 BeginPak (argv[i+1]);
502 else if (!strcmp(argv[i], "-only"))
504 strcpy (g_only, argv[i+1]);
505 printf ("Only grabbing %s\n", g_only);
508 else if (!strcmp(argv[i], "-3ds"))
511 printf ("loading .3ds files\n");
513 else if (argv[i][0] == '-')
514 Error ("Unknown option \"%s\"", argv[i]);
520 Error ("usage: qgrab [-archive <directory>] [-release <directory>] [-only <model>] [-3ds] file.qgr");
523 trifileext = ext_3ds;
525 trifileext = ext_tri;
527 for ( ; i<argc ; i++)
529 printf ("--------------- %s ---------------\n", argv[i]);
531 strcpy (path, argv[i]);
532 DefaultExtension (path, ".qdt");
533 SetQdirFromPath (path);
534 LoadScriptFile (ExpandArg(path));
541 // write out the last model