Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

515 lines
12 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "stdafx.h"
  8. #include <stdio.h>
  9. #include <windows.h>
  10. #include "mdlcheck_util.h"
  11. #include "tier0/dbg.h"
  12. #include "utldict.h"
  13. #include "tier1/utlstring.h"
  14. bool uselogfile = false;
  15. bool verbose = false;
  16. bool checkani = false;
  17. struct QCFile
  18. {
  19. char outputmodel[ MAX_PATH ];
  20. };
  21. struct ModelFile
  22. {
  23. char qcfile[ MAX_PATH ];
  24. int version;
  25. bool needsrecompile;
  26. int toobig;
  27. };
  28. struct AnalysisData
  29. {
  30. CUtlDict< QCFile, int > files; // .qc to modelname lookup
  31. CUtlDict< ModelFile, int > models; // .mdl to .qc/version lookup
  32. CUtlSymbolTable symbols;
  33. };
  34. static AnalysisData g_Analysis;
  35. SpewRetval_t SpewFunc( SpewType_t type, char const *pMsg )
  36. {
  37. printf( "%s", pMsg );
  38. OutputDebugString( pMsg );
  39. if ( type == SPEW_ERROR )
  40. {
  41. printf( "\n" );
  42. OutputDebugString( "\n" );
  43. }
  44. return SPEW_CONTINUE;
  45. }
  46. //-----------------------------------------------------------------------------
  47. // Purpose:
  48. //-----------------------------------------------------------------------------
  49. void printusage( void )
  50. {
  51. vprint( 0, "usage: mdlcheck <model source directory> <.mdl file directory>\n\
  52. \t-v = verbose output\n\
  53. \t-l = log to file log.txt\n\
  54. \t-a = check for large animation data\n\
  55. \ne.g.: mdlcheck -l u:/hl2/hl2/hl2models u:/hl2/hl2/models\n" );
  56. // Exit app
  57. exit( 1 );
  58. }
  59. void BuildFileList_R( CUtlVector< CUtlSymbol >& files, char const *dir, char const *extension )
  60. {
  61. WIN32_FIND_DATA wfd;
  62. char directory[ 256 ];
  63. char filename[ 256 ];
  64. HANDLE ff;
  65. sprintf( directory, "%s\\*.*", dir );
  66. if ( ( ff = FindFirstFile( directory, &wfd ) ) == INVALID_HANDLE_VALUE )
  67. return;
  68. int extlen = strlen( extension );
  69. do
  70. {
  71. if ( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
  72. {
  73. if ( wfd.cFileName[ 0 ] == '.' )
  74. continue;
  75. // Recurse down directory
  76. sprintf( filename, "%s\\%s", dir, wfd.cFileName );
  77. BuildFileList_R( files, filename, extension );
  78. }
  79. else
  80. {
  81. int len = strlen( wfd.cFileName );
  82. if ( len > extlen )
  83. {
  84. if ( strstr( wfd.cFileName, ".360." ) )
  85. {
  86. }
  87. else if ( !stricmp( &wfd.cFileName[ len - extlen ], extension ) )
  88. {
  89. char filename[ MAX_PATH ];
  90. Q_snprintf( filename, sizeof( filename ), "%s\\%s", dir, wfd.cFileName );
  91. _strlwr( filename );
  92. Q_FixSlashes( filename );
  93. CUtlSymbol sym = g_Analysis.symbols.AddString( filename );
  94. files.AddToTail( sym );
  95. }
  96. }
  97. }
  98. } while ( FindNextFile( ff, &wfd ) );
  99. }
  100. void BuildFileList( CUtlVector< CUtlSymbol >& files, char const *rootdir, char const *extension )
  101. {
  102. files.RemoveAll();
  103. BuildFileList_R( files, rootdir, extension );
  104. }
  105. //-----------------------------------------------------------------------------
  106. // This is here because scriplib.cpp is included in this project but cmdlib.cpp
  107. // is not, but scriplib.cpp uses some stuff from cmdlib.cpp, same with
  108. // LoadFile and ExpandPath above. The only thing that currently uses this
  109. // is $include in scriptlib, if this function returns 0, $include will
  110. // behave the way it did before this change
  111. //-----------------------------------------------------------------------------
  112. int CmdLib_ExpandWithBasePaths( CUtlVector< CUtlString > &expandedPathList, const char *pszPath )
  113. {
  114. return 0;
  115. }
  116. bool GetModelNameFromSourceFile( char const *filename, char *modelname, int maxlen )
  117. {
  118. modelname[0]=0;
  119. int filelength;
  120. char *buffer = (char *)COM_LoadFile( filename, &filelength );
  121. if ( !buffer )
  122. {
  123. vprint( 0, "Couldn't load %s\n", filename );
  124. return false;
  125. }
  126. bool valid = false;
  127. // Parse tokens
  128. char *current = buffer;
  129. while ( current )
  130. {
  131. current = CC_ParseToken( current );
  132. if ( strlen( com_token ) <= 0 )
  133. break;
  134. if ( stricmp( com_token, "$modelname" ) )
  135. continue;
  136. current = CC_ParseToken( current );
  137. strcpy( modelname, com_token );
  138. _strlwr( modelname );
  139. Q_FixSlashes( modelname );
  140. Q_DefaultExtension( modelname, ".mdl", maxlen );
  141. valid = true;
  142. break;
  143. }
  144. COM_FreeFile( (unsigned char *)buffer );
  145. if ( !valid )
  146. {
  147. vprint_queued( 0, ".qc file %s missing $modelname directive!!!\n", filename );
  148. }
  149. return valid;
  150. }
  151. bool AddModelNameFromSource( CUtlDict< ModelFile, int >& models, char const *filename, char const *modelname, int offset )
  152. {
  153. int idx = models.Find( modelname );
  154. if ( idx != models.InvalidIndex() )
  155. {
  156. char shortname[ MAX_PATH ];
  157. char shortname2[ MAX_PATH ];
  158. strcpy( shortname, &filename[ offset ] );
  159. strcpy( shortname2, &models[ idx ].qcfile[ offset ] );
  160. vprint_queued( 0, "multiple .qc's build %s\n %s\n %s\n",
  161. modelname,
  162. shortname,
  163. shortname2 );
  164. return false;
  165. }
  166. ModelFile mf;
  167. strcpy( mf.qcfile, filename );
  168. _strlwr( mf.qcfile );
  169. mf.version = 0;
  170. models.Insert( modelname, mf );
  171. return true;
  172. }
  173. //-----------------------------------------------------------------------------
  174. // Purpose:
  175. // Input : *sourcetreebase -
  176. // *subdir -
  177. // *baseentityclass -
  178. //-----------------------------------------------------------------------------
  179. void ProcessSourceDirectory( char const *basedir )
  180. {
  181. // vprint( 0, "building .qc list\n" );
  182. CUtlVector< CUtlSymbol > files;
  183. BuildFileList( files, basedir, ".qc" );
  184. // vprint( 0, "found %i .qc files\n\n", files.Count() );
  185. int offset = strlen( basedir ) + 1;
  186. // Add files to QC Files dictionary
  187. int c = files.Count();
  188. for ( int i = 0; i < c; i++ )
  189. {
  190. QCFile qcf;
  191. memset( &qcf, 0, sizeof( qcf ) );
  192. CUtlSymbol& sym = files[ i ];
  193. g_Analysis.files.Insert( g_Analysis.symbols.String( sym ), qcf );
  194. }
  195. vprint_queued( 0, "%s", "\n\n" );
  196. // Now iterate .qc files, looking into each to find the output model name
  197. c = g_Analysis.files.Count();
  198. int valid = 0;
  199. for ( int i = 0; i < c; i++ )
  200. {
  201. char modelname[ 256 ];
  202. char const *filename = g_Analysis.files.GetElementName( i );
  203. if ( verbose )
  204. {
  205. vprint( 0, "checking %i: %s\n", i, filename );
  206. }
  207. if ( GetModelNameFromSourceFile( filename, modelname, sizeof( modelname ) ) )
  208. {
  209. if ( AddModelNameFromSource( g_Analysis.models, filename, modelname, offset ) )
  210. {
  211. valid++;
  212. }
  213. }
  214. }
  215. int ecount = c - valid;
  216. if (ecount != 0)
  217. {
  218. // vprint( 0, "\n summary: found %i/%i (%.2f percent) .qc errors\n\n", ecount, c, 100.0 * ecount / max( c, 1 ) );
  219. vprint( 0, "\n summary: found %i .qc errors\n\n", ecount );
  220. }
  221. }
  222. #include "studio.h"
  223. #define IDSTUDIOHEADER (('T'<<24)+('S'<<16)+('D'<<8)+'I')
  224. // little-endian "IDST"
  225. #define IDSTUDIOANIMGROUPHEADER (('G'<<24)+('A'<<16)+('D'<<8)+'I')
  226. // little-endian "IDAG"
  227. byte buffer[1024*1024*4];
  228. bool ValidateModelFile( char const *modelname, int offset )
  229. {
  230. studiohdr_t *pHdr;
  231. FILE *fp;
  232. pHdr = (studiohdr_t *)buffer;
  233. fp = fopen( modelname, "rb" );
  234. if ( !fp )
  235. {
  236. vprint_queued( 0, "Unable to open .mdl file %s\n", modelname );
  237. return false;
  238. }
  239. // See if there's a .qc which builds this model
  240. char shortname[ MAX_PATH ];
  241. strcpy( shortname, &modelname[ offset ] );
  242. Q_FixSlashes( shortname );
  243. fread( buffer, sizeof( buffer ), 1, fp );
  244. fclose( fp );
  245. if ( pHdr->id != IDSTUDIOHEADER )
  246. {
  247. vprint_queued( 0, "Bogus studiomdl header for %s, expecting 'IDST' four cc code\n", shortname );
  248. return false;
  249. }
  250. bool valid = true;
  251. bool needsrecompile = false;
  252. int toobig = 0;
  253. // previous version is compatible
  254. if ( pHdr->version < 44 || pHdr->version > STUDIO_VERSION )
  255. {
  256. vprint_queued( 0, "Outdated model %s (ver %i != %i)\n", shortname, pHdr->version, STUDIO_VERSION );
  257. valid = false;
  258. }
  259. if (!Studio_ConvertStudioHdrToNewVersion( pHdr ))
  260. {
  261. // vprint( 0, "%s needs to be recompiled\n", pHdr->pszName() );
  262. needsrecompile = true;
  263. }
  264. if (checkani)
  265. {
  266. // HACK: since the sequence data is written after the animation data, this is rough way to determine how much anim data there really is
  267. int totalanimsize = pHdr->localseqindex - pHdr->localanimindex - pHdr->numlocalanim * sizeof( mstudioanimdesc_t );
  268. if (pHdr->pLocalAnimdesc( 0 )->animblock == 0 && totalanimsize > 1024 * 64)
  269. {
  270. toobig = totalanimsize;
  271. }
  272. }
  273. int idx = g_Analysis.models.Find( shortname );
  274. if ( idx == g_Analysis.models.InvalidIndex() )
  275. {
  276. vprint_queued( 0, "Couldn't find a .qc which builds %s\n", shortname );
  277. valid = false;
  278. }
  279. else
  280. {
  281. g_Analysis.models[idx].version = pHdr->version;
  282. g_Analysis.models[idx].needsrecompile = needsrecompile;
  283. g_Analysis.models[idx].toobig = toobig;
  284. }
  285. return valid;
  286. }
  287. void ProcessModelsDirectory( char const *basedir )
  288. {
  289. // vprint( 0, "building .mdl list\n" );
  290. CUtlVector< CUtlSymbol > models;
  291. BuildFileList( models, basedir, ".mdl" );
  292. // vprint( 0, "found %i .mdl files\n\n", models.Count() );
  293. int offset = strlen( basedir ) + 1;
  294. // Now iterate model files and check version tag and whether a .qc exists which builds the .mdl
  295. vprint_queued( 0, "%s", "\n\n" );
  296. // Add files to QC Files dictionary
  297. int c = models.Count();
  298. int valid = 0;
  299. for ( int i = 0; i < c; i++ )
  300. {
  301. QCFile qcf;
  302. memset( &qcf, 0, sizeof( qcf ) );
  303. CUtlSymbol& sym = models[ i ];
  304. char const *modelname = g_Analysis.symbols.String( sym );
  305. if ( verbose )
  306. {
  307. vprint( 0, "checking %i .mdl %s\n", i, modelname );
  308. }
  309. if ( ValidateModelFile( modelname, offset ) )
  310. {
  311. valid++;
  312. }
  313. }
  314. int ecount = c - valid;
  315. if (ecount != 0)
  316. {
  317. // vprint( 0, "\n summary: found %i/%i (%.2f percent) .mdl errors\n", ecount, c, 100.0 * ecount / max( c, 1 ) );
  318. vprint( 0, "\n summary: found %i .mdl errors\n", ecount );
  319. }
  320. }
  321. void CheckForUnbuiltModels( )
  322. {
  323. vprint_queued( 0, "%s", "\n\n" );
  324. int c = g_Analysis.models.Count();
  325. int valid = 0;
  326. for ( int i = 0; i < c; i++ )
  327. {
  328. if (g_Analysis.models[i].version == 0)
  329. {
  330. vprint_queued( 0, "Can't find %s,\n\tbuilt by %s\n", g_Analysis.models.GetElementName( i ), g_Analysis.models[i].qcfile );
  331. }
  332. else if (g_Analysis.models[i].needsrecompile)
  333. {
  334. vprint_queued( 0, "%s out of date,\n\tbuilt by %s\n", g_Analysis.models.GetElementName( i ), g_Analysis.models[i].qcfile );
  335. }
  336. else if (g_Analysis.models[i].toobig)
  337. {
  338. vprint_queued( 0, "%s needs $animblocksize command (%d of animdata),\n\tbuilt by %s\n", g_Analysis.models.GetElementName( i ), g_Analysis.models[i].toobig, g_Analysis.models[i].qcfile );
  339. }
  340. else
  341. {
  342. valid++;
  343. }
  344. }
  345. int ecount = c - valid;
  346. if (ecount != 0)
  347. {
  348. // vprint( 0, "\n summary: found %i/%i (%.2f percent) missing .mdl's\n", ecount, c, 100.0 * ecount / max( c, 1 ) );
  349. vprint( 0, "\n summary: found %i missing .mdl's\n", ecount );
  350. }
  351. }
  352. //-----------------------------------------------------------------------------
  353. // Purpose:
  354. //-----------------------------------------------------------------------------
  355. void CheckLogFile( void )
  356. {
  357. if ( uselogfile )
  358. {
  359. _unlink( "log.txt" );
  360. vprint( 0, " Outputting to log.txt\n" );
  361. }
  362. }
  363. //-----------------------------------------------------------------------------
  364. // Purpose:
  365. // Input : argc -
  366. // argv[] -
  367. // Output : int
  368. //-----------------------------------------------------------------------------
  369. int main( int argc, char* argv[] )
  370. {
  371. SpewOutputFunc( SpewFunc );
  372. int i = 1;
  373. for (i ; i<argc ; i++)
  374. {
  375. if ( argv[ i ][ 0 ] == '-' )
  376. {
  377. switch( argv[ i ][ 1 ] )
  378. {
  379. case 'l':
  380. uselogfile = true;
  381. break;
  382. case 'v':
  383. verbose = true;
  384. break;
  385. case 'a':
  386. checkani = true;
  387. break;
  388. default:
  389. printusage();
  390. break;
  391. }
  392. }
  393. }
  394. vprint( 0, "Valve Software - mdlcheck.exe (%s)\n", __DATE__ );
  395. vprint( 0, "--- Source Model Consistency Checker ---\n" );
  396. if ( argc < 3 || ( i != argc ) )
  397. {
  398. printusage();
  399. }
  400. CheckLogFile();
  401. char modelsources[ 256 ];
  402. strcpy( modelsources, argv[ i - 2 ] );
  403. char modelsdir[ 256 ];
  404. strcpy( modelsdir, argv[ i - 1 ] );
  405. if ( !strstr( modelsdir, "models" ) )
  406. {
  407. vprint( 0, "Models dir %s looks invalid (format: u:/tf2/hl2/models)\n", modelsdir );
  408. return 0;
  409. }
  410. Q_StripTrailingSlash( modelsources );
  411. Q_StripTrailingSlash( modelsdir );
  412. ProcessSourceDirectory( modelsources );
  413. ProcessModelsDirectory( modelsdir );
  414. CheckForUnbuiltModels( );
  415. dump_print_queue( );
  416. return 0;
  417. }