PageRenderTime 36ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/quake2/qdata/qdata.c

https://gitlab.com/illwieckz/netradiant
C | 574 lines | 376 code | 82 blank | 116 comment | 100 complexity | a9c2b487873dc6640a5aaf181285b852 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-2.0
  1. /*
  2. Copyright (C) 1999-2007 id Software, Inc. and contributors.
  3. For a list of contributors, see the accompanying CONTRIBUTORS file.
  4. This file is part of GtkRadiant.
  5. GtkRadiant is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. GtkRadiant is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GtkRadiant; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #include "qdata.h"
  18. #include "inout.h"
  19. #include "md4.h"
  20. qboolean g_compress_pak;
  21. qboolean g_release; // don't grab, copy output data to new tree
  22. qboolean g_pak; // if true, copy to pak instead of release
  23. char g_releasedir[1024]; // c:\quake2\baseq2, etc
  24. qboolean g_archive; // don't grab, copy source data to new tree
  25. qboolean do3ds;
  26. char g_only[256]; // if set, only grab this cd
  27. qboolean g_skipmodel; // set true when a cd is not g_only
  28. char *ext_3ds = "3ds";
  29. char *ext_tri = "tri";
  30. char *trifileext;
  31. char game[64] = "quake2";
  32. void InitPaths( int *argc, char **argv );
  33. /*
  34. =======================================================
  35. PAK FILES
  36. =======================================================
  37. */
  38. typedef struct
  39. {
  40. char name[56];
  41. int filepos, filelen;
  42. } packfile_t;
  43. typedef struct
  44. {
  45. char id[4];
  46. int dirofs;
  47. int dirlen;
  48. } packheader_t;
  49. packfile_t pfiles[16384];
  50. FILE *pakfile;
  51. packfile_t *pf;
  52. packheader_t pakheader;
  53. /*
  54. ==============
  55. BeginPak
  56. ==============
  57. */
  58. void BeginPak( char *outname ){
  59. if ( !g_pak ) {
  60. return;
  61. }
  62. pakfile = SafeOpenWrite( outname );
  63. // leave space for header
  64. SafeWrite( pakfile, &pakheader, sizeof( pakheader ) );
  65. pf = pfiles;
  66. }
  67. /*
  68. ==============
  69. ReleaseFile
  70. Filename should be gamedir reletive.
  71. Either copies the file to the release dir, or adds it to
  72. the pak file.
  73. ==============
  74. */
  75. void ReleaseFile( char *filename ){
  76. int len;
  77. byte *buf;
  78. char source[1024];
  79. char dest[1024];
  80. if ( !g_release ) {
  81. return;
  82. }
  83. sprintf( source, "%s%s", gamedir, filename );
  84. if ( !g_pak ) { // copy it
  85. sprintf( dest, "%s/%s", g_releasedir, filename );
  86. printf( "copying to %s\n", dest );
  87. QCopyFile( source, dest );
  88. return;
  89. }
  90. // pak it
  91. printf( "paking %s\n", filename );
  92. if ( strlen( filename ) >= sizeof( pf->name ) ) {
  93. Error( "Filename too long for pak: %s", filename );
  94. }
  95. len = LoadFile( source, (void **)&buf );
  96. if ( g_compress_pak && len < 4096 * 1024 ) {
  97. cblock_t in, out;
  98. cblock_t Huffman( cblock_t in );
  99. in.count = len;
  100. in.data = buf;
  101. out = Huffman( in );
  102. if ( out.count < in.count ) {
  103. printf( " compressed from %i to %i\n", in.count, out.count );
  104. free( in.data );
  105. buf = out.data;
  106. len = out.count;
  107. }
  108. else{
  109. free( out.data );
  110. }
  111. }
  112. strcpy( pf->name, filename );
  113. pf->filepos = LittleLong( ftell( pakfile ) );
  114. pf->filelen = LittleLong( len );
  115. pf++;
  116. SafeWrite( pakfile, buf, len );
  117. free( buf );
  118. }
  119. /*
  120. ==============
  121. FinishPak
  122. ==============
  123. */
  124. void FinishPak( void ){
  125. int dirlen;
  126. int d;
  127. int i;
  128. unsigned checksum;
  129. if ( !g_pak ) {
  130. return;
  131. }
  132. pakheader.id[0] = 'P';
  133. pakheader.id[1] = 'A';
  134. pakheader.id[2] = 'C';
  135. pakheader.id[3] = 'K';
  136. dirlen = (byte *)pf - (byte *)pfiles;
  137. pakheader.dirofs = LittleLong( ftell( pakfile ) );
  138. pakheader.dirlen = LittleLong( dirlen );
  139. checksum = Com_BlockChecksum( (void *)pfiles, dirlen );
  140. SafeWrite( pakfile, pfiles, dirlen );
  141. i = ftell( pakfile );
  142. fseek( pakfile, 0, SEEK_SET );
  143. SafeWrite( pakfile, &pakheader, sizeof( pakheader ) );
  144. fclose( pakfile );
  145. d = pf - pfiles;
  146. printf( "%i files packed in %i bytes\n",d, i );
  147. printf( "checksum: 0x%x\n", checksum );
  148. }
  149. /*
  150. ===============
  151. Cmd_File
  152. This is only used to cause a file to be copied during a release
  153. build (default.cfg, maps, etc)
  154. ===============
  155. */
  156. void Cmd_File( void ){
  157. GetToken( false );
  158. ReleaseFile( token );
  159. }
  160. /*
  161. ===============
  162. PackDirectory_r
  163. ===============
  164. */
  165. #if GDEF_OS_WINDOWS
  166. #include "io.h"
  167. void PackDirectory_r( char *dir ){
  168. struct _finddata_t fileinfo;
  169. int handle;
  170. char dirstring[1024];
  171. char filename[1024];
  172. sprintf( dirstring, "%s%s/*.*", gamedir, dir );
  173. handle = _findfirst( dirstring, &fileinfo );
  174. if ( handle == -1 ) {
  175. return;
  176. }
  177. do
  178. {
  179. sprintf( filename, "%s/%s", dir, fileinfo.name );
  180. if ( fileinfo.attrib & _A_SUBDIR ) { // directory
  181. if ( fileinfo.name[0] != '.' ) { // don't pak . and ..
  182. PackDirectory_r( filename );
  183. }
  184. continue;
  185. }
  186. // copy or pack the file
  187. ReleaseFile( filename );
  188. } while ( _findnext( handle, &fileinfo ) != -1 );
  189. _findclose( handle );
  190. }
  191. #else
  192. #include <sys/types.h>
  193. #include <sys/dir.h>
  194. void PackDirectory_r( char *dir ){
  195. #ifdef NeXT
  196. struct direct **namelist, *ent;
  197. #else
  198. struct dirent **namelist, *ent;
  199. #endif
  200. int count;
  201. struct stat st;
  202. int i;
  203. int len;
  204. char fullname[1024];
  205. char dirstring[1024];
  206. char *name;
  207. sprintf( dirstring, "%s%s", gamedir, dir );
  208. count = scandir( dirstring, &namelist, NULL, NULL );
  209. for ( i = 0 ; i < count ; i++ )
  210. {
  211. ent = namelist[i];
  212. name = ent->d_name;
  213. if ( name[0] == '.' ) {
  214. continue;
  215. }
  216. sprintf( fullname, "%s/%s", dir, name );
  217. sprintf( dirstring, "%s%s/%s", gamedir, dir, name );
  218. if ( stat( dirstring, &st ) == -1 ) {
  219. Error( "fstating %s", pf->name );
  220. }
  221. if ( st.st_mode & S_IFDIR ) { // directory
  222. PackDirectory_r( fullname );
  223. continue;
  224. }
  225. // copy or pack the file
  226. ReleaseFile( fullname );
  227. }
  228. }
  229. #endif
  230. /*
  231. ===============
  232. Cmd_Dir
  233. This is only used to cause a directory to be copied during a
  234. release build (sounds, etc)
  235. ===============
  236. */
  237. void Cmd_Dir( void ){
  238. GetToken( false );
  239. PackDirectory_r( token );
  240. }
  241. //========================================================================
  242. #define MAX_RTEX 16384
  243. int numrtex;
  244. char rtex[MAX_RTEX][64];
  245. void ReleaseTexture( char *name ){
  246. int i;
  247. char path[1024];
  248. for ( i = 0 ; i < numrtex ; i++ )
  249. if ( !Q_strncasecmp( name, rtex[i], strlen( name ) ) ) {
  250. return;
  251. }
  252. if ( numrtex == MAX_RTEX ) {
  253. Error( "numrtex == MAX_RTEX" );
  254. }
  255. strcpy( rtex[i], name );
  256. numrtex++;
  257. sprintf( path, "textures/%s.wal", name );
  258. ReleaseFile( path );
  259. }
  260. /*
  261. ===============
  262. Cmd_Maps
  263. Only relevent for release and pak files.
  264. Releases the .bsp files for the maps, and scans all of the files to
  265. build a list of all textures used, which are then released.
  266. ===============
  267. */
  268. void Cmd_Maps( void ){
  269. char map[1024];
  270. int i;
  271. while ( TokenAvailable() )
  272. {
  273. GetToken( false );
  274. sprintf( map, "maps/%s.bsp", token );
  275. ReleaseFile( map );
  276. if ( !g_release ) {
  277. continue;
  278. }
  279. // get all the texture references
  280. sprintf( map, "%smaps/%s.bsp", gamedir, token );
  281. LoadBSPFileTexinfo( map );
  282. for ( i = 0 ; i < numtexinfo ; i++ )
  283. ReleaseTexture( texinfo[i].texture );
  284. }
  285. }
  286. //==============================================================
  287. /*
  288. ===============
  289. ParseScript
  290. ===============
  291. */
  292. void ParseScript( void ){
  293. while ( 1 )
  294. {
  295. do
  296. { // look for a line starting with a $ command
  297. GetToken( true );
  298. if ( endofscript ) {
  299. return;
  300. }
  301. if ( token[0] == '$' ) {
  302. break;
  303. }
  304. while ( TokenAvailable() )
  305. GetToken( false );
  306. } while ( 1 );
  307. //
  308. // model commands
  309. //
  310. if ( !strcmp( token, "$modelname" ) ) {
  311. Cmd_Modelname();
  312. }
  313. else if ( !strcmp( token, "$base" ) ) {
  314. Cmd_Base();
  315. }
  316. else if ( !strcmp( token, "$cd" ) ) {
  317. Cmd_Cd();
  318. }
  319. else if ( !strcmp( token, "$origin" ) ) {
  320. Cmd_Origin();
  321. }
  322. else if ( !strcmp( token, "$scale" ) ) {
  323. Cmd_ScaleUp();
  324. }
  325. else if ( !strcmp( token, "$frame" ) ) {
  326. Cmd_Frame();
  327. }
  328. else if ( !strcmp( token, "$skin" ) ) {
  329. Cmd_Skin();
  330. }
  331. else if ( !strcmp( token, "$skinsize" ) ) {
  332. Cmd_Skinsize();
  333. }
  334. //
  335. // sprite commands
  336. //
  337. else if ( !strcmp( token, "$spritename" ) ) {
  338. Cmd_SpriteName();
  339. }
  340. else if ( !strcmp( token, "$load" ) ) {
  341. Cmd_Load();
  342. }
  343. else if ( !strcmp( token, "$spriteframe" ) ) {
  344. Cmd_SpriteFrame();
  345. }
  346. //
  347. // image commands
  348. //
  349. else if ( !strcmp( token, "$grab" ) ) {
  350. Cmd_Grab();
  351. }
  352. else if ( !strcmp( token, "$raw" ) ) {
  353. Cmd_Raw();
  354. }
  355. else if ( !strcmp( token, "$colormap" ) ) {
  356. Cmd_Colormap();
  357. }
  358. else if ( !strcmp( token, "$mippal" ) ) {
  359. Cmd_Mippal();
  360. }
  361. else if ( !strcmp( token, "$mipdir" ) ) {
  362. Cmd_Mipdir();
  363. }
  364. else if ( !strcmp( token, "$mip" ) ) {
  365. Cmd_Mip();
  366. }
  367. else if ( !strcmp( token, "$environment" ) ) {
  368. Cmd_Environment();
  369. }
  370. //
  371. // video
  372. //
  373. else if ( !strcmp( token, "$video" ) ) {
  374. Cmd_Video();
  375. }
  376. //
  377. // misc
  378. //
  379. else if ( !strcmp( token, "$file" ) ) {
  380. Cmd_File();
  381. }
  382. else if ( !strcmp( token, "$dir" ) ) {
  383. Cmd_Dir();
  384. }
  385. else if ( !strcmp( token, "$maps" ) ) {
  386. Cmd_Maps();
  387. }
  388. else if ( !strcmp( token, "$alphalight" ) ) {
  389. Cmd_Alphalight();
  390. }
  391. else if ( !strcmp( token, "$inverse16table" ) ) {
  392. Cmd_Inverse16Table();
  393. }
  394. else{
  395. Error( "bad command %s\n", token );
  396. }
  397. }
  398. }
  399. //=======================================================
  400. /*
  401. ==============
  402. main
  403. ==============
  404. */
  405. int main( int argc, char **argv ){
  406. static int i; // VC4.2 compiler bug if auto...
  407. char path[1024];
  408. ExpandWildcards( &argc, &argv );
  409. InitPaths( &argc, argv );
  410. for ( i = 1 ; i < argc ; i++ )
  411. {
  412. if ( !strcmp( argv[i], "-archive" ) ) {
  413. // -archive f:/quake2/release/dump_11_30
  414. archive = true;
  415. strcpy( archivedir, argv[i + 1] );
  416. printf( "Archiving source to: %s\n", archivedir );
  417. i++;
  418. }
  419. else if ( !strcmp( argv[i], "-release" ) ) {
  420. g_release = true;
  421. strcpy( g_releasedir, argv[i + 1] );
  422. printf( "Copy output to: %s\n", g_releasedir );
  423. i++;
  424. }
  425. else if ( !strcmp( argv[i], "-compress" ) ) {
  426. g_compress_pak = true;
  427. printf( "Compressing pakfile\n" );
  428. }
  429. else if ( !strcmp( argv[i], "-pak" ) ) {
  430. g_release = true;
  431. g_pak = true;
  432. printf( "Building pakfile: %s\n", argv[i + 1] );
  433. BeginPak( argv[i + 1] );
  434. i++;
  435. }
  436. else if ( !strcmp( argv[i], "-only" ) ) {
  437. strcpy( g_only, argv[i + 1] );
  438. printf( "Only grabbing %s\n", g_only );
  439. i++;
  440. }
  441. else if ( !strcmp( argv[i], "-3ds" ) ) {
  442. do3ds = true;
  443. printf( "loading .3ds files\n" );
  444. }
  445. else if ( argv[i][0] == '-' ) {
  446. Error( "Unknown option \"%s\"", argv[i] );
  447. }
  448. else{
  449. break;
  450. }
  451. }
  452. if ( i >= argc ) {
  453. Error( "usage: %s [-archive <directory>] [-release <directory>] [-only <model>] [-3ds] file.qgr", argv[ 0 ] );
  454. }
  455. if ( do3ds ) {
  456. trifileext = ext_3ds;
  457. }
  458. else{
  459. trifileext = ext_tri;
  460. }
  461. for ( ; i < argc ; i++ )
  462. {
  463. printf( "--------------- %s ---------------\n", argv[i] );
  464. // load the script
  465. strcpy( path, argv[i] );
  466. DefaultExtension( path, ".qdt" );
  467. SetQdirFromPath( path );
  468. LoadScriptFile( ExpandArg( path ) );
  469. //
  470. // parse it
  471. //
  472. ParseScript();
  473. // write out the last model
  474. FinishModel();
  475. FinishSprite();
  476. }
  477. if ( g_pak ) {
  478. FinishPak();
  479. }
  480. return 0;
  481. }