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.

561 lines
13 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "mathlib/mathlib.h"
  9. #include "bsplib.h"
  10. #include "tier0/icommandline.h"
  11. #include "iscratchpad3d.h"
  12. #include "filesystem_tools.h"
  13. #include "tier2/fileutils.h"
  14. #include "gamebspfile.h"
  15. #include "tier1/utlstringmap.h"
  16. #include "tools_minidump.h"
  17. #include "cmdlib.h"
  18. bool g_bTreeInfo = false;
  19. bool g_bDrawTree = false;
  20. float g_nOptimumDepth;
  21. int g_nMinTreeDepth;
  22. int g_nMaxTreeDepth;
  23. int g_TotalTreeDepth;
  24. float g_TotalVariance;
  25. float g_ySpacing = -1; // (set by code)
  26. double g_xSpacing = 1.0;
  27. void CalculateTreeInfo_R( int iNode, int depth )
  28. {
  29. dnode_t *pNode = &dnodes[iNode];
  30. if ( iNode < 0 ) // (is this a leaf)
  31. {
  32. g_nMinTreeDepth = min( g_nMinTreeDepth, depth );
  33. g_nMaxTreeDepth = max( g_nMaxTreeDepth, depth );
  34. g_TotalTreeDepth += depth;
  35. g_TotalVariance += fabs( depth - g_nOptimumDepth );
  36. }
  37. else
  38. {
  39. CalculateTreeInfo_R( pNode->children[0], depth+1 );
  40. CalculateTreeInfo_R( pNode->children[1], depth+1 );
  41. }
  42. }
  43. void DrawTreeToScratchPad_R(
  44. IScratchPad3D *pPad,
  45. int iNode, // Which node we're drawing.
  46. int iLevel, // (used to get Y coordinate)
  47. float flXMin,
  48. float flXMax,
  49. const Vector *pParentPos // Parent node position to draw connecting line (if there is a parent).
  50. )
  51. {
  52. float flMyX = (flXMin + flXMax) * 0.5f;
  53. Vector vMyPos;
  54. vMyPos.x = 0;
  55. vMyPos.y = flMyX;
  56. vMyPos.z = -iLevel * g_ySpacing;
  57. // Draw the connecting line.
  58. if ( pParentPos )
  59. {
  60. pPad->DrawLine( CSPVert( *pParentPos, Vector(1,1,1) ), CSPVert( vMyPos, Vector(1,0,0) ) );
  61. }
  62. dnode_t *pNode = &dnodes[iNode];
  63. if ( iNode < 0 )
  64. {
  65. // This is a leaf.
  66. pPad->DrawPoint( CSPVert( vMyPos, Vector(1,0,0) ), 6 );
  67. }
  68. else
  69. {
  70. pPad->DrawPoint( CSPVert( vMyPos, Vector(1,1,1) ), 2 );
  71. DrawTreeToScratchPad_R(
  72. pPad,
  73. pNode->children[0],
  74. iLevel+1,
  75. flXMin,
  76. flMyX,
  77. &vMyPos );
  78. DrawTreeToScratchPad_R(
  79. pPad,
  80. pNode->children[1],
  81. iLevel+1,
  82. flMyX,
  83. flXMax,
  84. &vMyPos );
  85. }
  86. }
  87. void CalcTreeDepth_R( int iNode, int iLevel, int &iMaxDepth )
  88. {
  89. iMaxDepth = max( iLevel, iMaxDepth );
  90. if ( iNode < 0 )
  91. return;
  92. CalcTreeDepth_R( dnodes[iNode].children[0], iLevel+1, iMaxDepth );
  93. CalcTreeDepth_R( dnodes[iNode].children[1], iLevel+1, iMaxDepth );
  94. }
  95. void DrawTreeToScratchPad()
  96. {
  97. IScratchPad3D *pPad = ScratchPad3D_Create();
  98. pPad->SetAutoFlush( false );
  99. int maxDepth = 0;
  100. CalcTreeDepth_R( dmodels[0].headnode, 0, maxDepth );
  101. float flXSpace = (1 << min( maxDepth, 14 )) * g_xSpacing;
  102. g_ySpacing = (flXSpace / maxDepth) / 4;
  103. DrawTreeToScratchPad_R(
  104. pPad,
  105. dmodels[0].headnode,
  106. 0, // start on level 0
  107. -flXSpace/2,
  108. flXSpace/2,
  109. NULL );
  110. pPad->Release();
  111. }
  112. struct WorldTextureStats_t
  113. {
  114. int texdataID;
  115. int refCount;
  116. };
  117. int WorldTextureCompareFunc( const void *t1, const void *t2 )
  118. {
  119. WorldTextureStats_t *pStat1 = ( WorldTextureStats_t * )t1;
  120. WorldTextureStats_t *pStat2 = ( WorldTextureStats_t * )t2;
  121. if( pStat1->refCount < pStat2->refCount )
  122. {
  123. return 1;
  124. }
  125. if( pStat1->refCount > pStat2->refCount )
  126. {
  127. return -1;
  128. }
  129. return 0;
  130. }
  131. void PrintWorldTextureStats( FILE *fp )
  132. {
  133. static WorldTextureStats_t stats[MAX_MAP_TEXDATA];
  134. int i;
  135. for( i = 0; i < numtexdata; i++ )
  136. {
  137. stats[i].texdataID = i;
  138. stats[i].refCount = 0;
  139. }
  140. for( i = 0; i < numfaces; i++ )
  141. {
  142. dface_t *pFace = &dfaces[i];
  143. int texinfoID = pFace->texinfo;
  144. Assert( texinfoID >= 0 && texinfoID < texinfo.Count() );
  145. int texdataID = texinfo[texinfoID].texdata;
  146. Assert( texdataID >= 0 && texdataID < numtexdata );
  147. stats[texdataID].refCount++;
  148. }
  149. qsort( stats, numtexdata, sizeof( WorldTextureStats_t ), WorldTextureCompareFunc );
  150. for( i = 0; i < numtexdata; i++ )
  151. {
  152. const char *pTextureName = TexDataStringTable_GetString( dtexdata[stats[i].texdataID].nameStringTableID );
  153. fprintf( fp, "%5d surface(s) use material \"%s\"\n", stats[i].refCount, pTextureName );
  154. }
  155. }
  156. void PrintModelStats( FILE *fp )
  157. {
  158. CUtlStringMap<int> modelMap;
  159. // -------------------------------------------------------
  160. // Deal with static props
  161. // -------------------------------------------------------
  162. GameLumpHandle_t handle = g_GameLumps.GetGameLumpHandle( GAMELUMP_STATIC_PROPS );
  163. // int nLumpSize = g_GameLumps.GameLumpSize( handle );
  164. void *pStaticPropLump = g_GameLumps.GetGameLump( handle );
  165. unsigned char *pScan = ( unsigned char * )pStaticPropLump;
  166. // fprintf( fp, "nLumpSize: %d\n", nLumpSize );
  167. // read dictionary
  168. int nDictCount = ( ( int * )pScan )[0];
  169. pScan += sizeof( int );
  170. StaticPropDictLump_t *pDictLump = ( StaticPropDictLump_t * )pScan;
  171. pScan += nDictCount * sizeof( StaticPropDictLump_t );
  172. // read leaves
  173. int nLeafCount = ( ( int * )pScan )[0];
  174. pScan += sizeof( int );
  175. // StaticPropLeafLump_t *pLeafLump = ( StaticPropLeafLump_t * )pScan;
  176. pScan += nLeafCount * sizeof( StaticPropLeafLump_t );
  177. // read objects
  178. int nObjCount = ( ( int * )pScan )[0];
  179. pScan += sizeof( int );
  180. StaticPropLump_t *pStaticPropLumpData = ( StaticPropLump_t * )pScan;
  181. pScan += nObjCount * sizeof( StaticPropLump_t );
  182. int i;
  183. for( i = 0; i < nObjCount; i++ )
  184. {
  185. StaticPropLump_t &pData = pStaticPropLumpData[i];
  186. const char *pName = pDictLump[pData.m_PropType].m_Name;
  187. if( modelMap.Defined( pName ) )
  188. {
  189. modelMap[pName]++;
  190. }
  191. else
  192. {
  193. modelMap[pName] = 1;
  194. }
  195. }
  196. extern int num_entities;
  197. extern entity_t entities[MAX_MAP_ENTITIES];
  198. ParseEntities();
  199. for( i = 0; i < num_entities; i++ )
  200. {
  201. const entity_t *pEnt = &entities[i];
  202. const epair_t *pEPair = pEnt->epairs;
  203. const char *pClassName = NULL;
  204. const char *pModelName = NULL;
  205. for( ; pEPair; pEPair = pEPair->next )
  206. {
  207. if ( Q_stricmp( pEPair->key, "classname" ) == 0 )
  208. {
  209. pClassName = pEPair->value;
  210. }
  211. else if( Q_stricmp( pEPair->key, "model" ) == 0 )
  212. {
  213. if( StringHasPrefix( pEPair->value, "models" ) )
  214. {
  215. pModelName = pEPair->value;
  216. }
  217. }
  218. }
  219. if( pClassName && pModelName )
  220. {
  221. if( modelMap.Defined( pModelName ) )
  222. {
  223. modelMap[pModelName]++;
  224. }
  225. else
  226. {
  227. modelMap[pModelName] = 1;
  228. }
  229. }
  230. }
  231. for( i = 0; i < modelMap.GetNumStrings(); i++ )
  232. {
  233. printf( "%s,%d\n", modelMap.String( i ), modelMap[modelMap.String( i )] );
  234. }
  235. }
  236. void PrintListStaticProps( FILE *fp )
  237. {
  238. // -------------------------------------------------------
  239. // Deal with static props
  240. // -------------------------------------------------------
  241. GameLumpHandle_t handle = g_GameLumps.GetGameLumpHandle( GAMELUMP_STATIC_PROPS );
  242. // int nLumpSize = g_GameLumps.GameLumpSize( handle );
  243. void *pStaticPropLump = g_GameLumps.GetGameLump( handle );
  244. unsigned char *pScan = ( unsigned char * )pStaticPropLump;
  245. // fprintf( fp, "nLumpSize: %d\n", nLumpSize );
  246. // read dictionary
  247. int nDictCount = ( ( int * )pScan )[0];
  248. pScan += sizeof( int );
  249. StaticPropDictLump_t *pDictLump = ( StaticPropDictLump_t * )pScan;
  250. pScan += nDictCount * sizeof( StaticPropDictLump_t );
  251. // read leaves
  252. int nLeafCount = ( ( int * )pScan )[0];
  253. pScan += sizeof( int );
  254. // StaticPropLeafLump_t *pLeafLump = ( StaticPropLeafLump_t * )pScan;
  255. pScan += nLeafCount * sizeof( StaticPropLeafLump_t );
  256. // read objects
  257. int nObjCount = ( ( int * )pScan )[0];
  258. pScan += sizeof( int );
  259. StaticPropLump_t *pStaticPropLumpData = ( StaticPropLump_t * )pScan;
  260. pScan += nObjCount * sizeof( StaticPropLump_t );
  261. int i;
  262. for( i = 0; i < nObjCount; i++ )
  263. {
  264. StaticPropLump_t &pData = pStaticPropLumpData[i];
  265. const char *pName = pDictLump[pData.m_PropType].m_Name;
  266. printf( "%03d %s\n", i, pName );
  267. }
  268. }
  269. void PrintCommandLine( int argc, char **argv )
  270. {
  271. Warning( "Command line: " );
  272. for ( int z=0; z < argc; z++ )
  273. {
  274. Warning( "\"%s\" ", argv[z] );
  275. }
  276. Warning( "\n\n" );
  277. }
  278. void main (int argc, char **argv)
  279. {
  280. // Install an exception handler.
  281. SetupDefaultToolsMinidumpHandler();
  282. int i;
  283. char source[1024];
  284. int size;
  285. FILE *f;
  286. bool extractlumps[HEADER_LUMPS];
  287. memset( extractlumps, 0, sizeof(extractlumps) );
  288. bool bHaveAnyToExtract = false;
  289. ::SetHDRMode( false );
  290. CommandLine()->CreateCmdLine( argc, argv );
  291. InitCommandLineProgram( argc, argv );
  292. g_pFileSystem = g_pFullFileSystem;
  293. MathLib_Init( 2.2f, 2.2f, 0.0f, 2.0f, false, false, false, false );
  294. PrintCommandLine( argc, argv );
  295. if (argc == 1)
  296. {
  297. printf( "vbspinfo: build date(" __DATE__ ")\n" );
  298. printf("usage: vbspinfo [parameters] bspfile [bspfiles]\n");
  299. printf(" -treeinfo \n");
  300. // printf(" -drawtree \n"); Remove for now until the option can be fixed
  301. printf(" -worldtexturestats \n");
  302. printf(" -modelstats \n");
  303. printf(" -liststaticprops \n");
  304. printf(" -X[lump ID] Extract BSP lump to file. i.e -X0 extracts entity lump.\n");
  305. printf(" -size Show .bsp worldmodel bounds\n");
  306. Error("Incorrect syntax.");
  307. }
  308. bool bWorldTextureStats = false;
  309. bool bModelStats = false;
  310. bool bListStaticProps = false;
  311. bool bShowMapBounds = false;
  312. for (i=1 ; i<argc ; i++)
  313. {
  314. if ( stricmp( argv[i], "-treeinfo" ) == 0 )
  315. {
  316. g_bTreeInfo = true;
  317. continue;
  318. }
  319. else if ( stricmp( argv[i], "-drawtree" ) == 0 )
  320. {
  321. g_bDrawTree = true;
  322. continue;
  323. }
  324. else if( stricmp( argv[i], "-worldtexturestats" ) == 0 )
  325. {
  326. bWorldTextureStats = true;
  327. continue;
  328. }
  329. else if( stricmp( argv[i], "-modelstats" ) == 0 )
  330. {
  331. bModelStats = true;
  332. continue;
  333. }
  334. else if( stricmp( argv[i], "-liststaticprops" ) == 0 )
  335. {
  336. bListStaticProps = true;
  337. continue;
  338. }
  339. else if( stricmp( argv[i], "-steamlocal" ) == 0 )
  340. {
  341. continue;
  342. }
  343. else if( stricmp( argv[i], "-steam" ) == 0 )
  344. {
  345. continue;
  346. }
  347. else if( strnicmp( argv[i], "-X", 2 ) == 0 )
  348. {
  349. int iLump = atoi( argv[i]+2 );
  350. extractlumps[iLump] = true;
  351. bHaveAnyToExtract = true;
  352. continue;
  353. }
  354. else if ( stricmp( argv[ i ], "-size" ) == 0 )
  355. {
  356. bShowMapBounds = true;
  357. continue;
  358. }
  359. if( !bWorldTextureStats && !bModelStats && !bListStaticProps )
  360. {
  361. printf ("---------------------\n");
  362. }
  363. strcpy (source, argv[i]);
  364. Q_DefaultExtension (source, ".bsp", sizeof( source ) );
  365. strcpy( source, ExpandPath( source ) );
  366. f = fopen (source, "rb");
  367. if (f)
  368. {
  369. fseek( f, 0, SEEK_END );
  370. size = ftell( f );
  371. fclose (f);
  372. }
  373. else
  374. size = 0;
  375. if( !bWorldTextureStats && !bModelStats && !bListStaticProps )
  376. {
  377. Msg ("reading %s (%d)\n", source, size);
  378. }
  379. // If we're extracting, do that and quit.
  380. if ( bHaveAnyToExtract )
  381. {
  382. OpenBSPFile(source);
  383. // If the filename doesn't have a path, prepend with the current directory
  384. char fullbspname[MAX_PATH];
  385. _fullpath( fullbspname, source, sizeof( fullbspname ) );
  386. for ( int extract = 0; extract < HEADER_LUMPS; extract++ )
  387. {
  388. if ( extractlumps[extract] )
  389. {
  390. printf ("Extracting lump %d.\n", extract );
  391. WriteLumpToFile( fullbspname, extract );
  392. }
  393. }
  394. CloseBSPFile();
  395. printf ("Finished extraction.\n" );
  396. return;
  397. }
  398. LoadBSPFile (source);
  399. if( bWorldTextureStats )
  400. {
  401. PrintWorldTextureStats( stdout );
  402. }
  403. else if( bModelStats )
  404. {
  405. PrintModelStats( stdout );
  406. }
  407. else if( bListStaticProps )
  408. {
  409. PrintListStaticProps( stdout );
  410. }
  411. else if ( bShowMapBounds )
  412. {
  413. dmodel_t *world = &dmodels[ 0 ];
  414. printf( "Full : (%8.3f %8.3f %8.3f) - (%8.3f %8.3f %8.3f)\n",
  415. world->mins.x,
  416. world->mins.y,
  417. world->mins.z,
  418. world->maxs.x,
  419. world->maxs.y,
  420. world->maxs.z );
  421. if ( !num_entities )
  422. ParseEntities();
  423. for ( int e = 0; e < num_entities; ++i )
  424. {
  425. char* pEntity = ValueForKey(&entities[e], "classname");
  426. if ( strcmp(pEntity, "worldspawn" ) )
  427. continue;
  428. Vector wmins;
  429. Vector wmaxs;
  430. wmins.Init();
  431. wmaxs.Init();
  432. char* pchMins = ValueForKey(&entities[e], "world_mins");
  433. sscanf( pchMins, "%f %f %f", &wmins.x, &wmins.y, &wmins.z );
  434. char* pchMaxs = ValueForKey(&entities[e], "world_maxs");
  435. sscanf( pchMaxs, "%f %f %f", &wmaxs.x, &wmaxs.y, &wmaxs.z );
  436. printf( "No Skybox: (%8.3f %8.3f %8.3f) - (%8.3f %8.3f %8.3f)\n",
  437. wmins.x,
  438. wmins.y,
  439. wmins.z,
  440. wmaxs.x,
  441. wmaxs.y,
  442. wmaxs.z );
  443. break;
  444. }
  445. }
  446. else
  447. {
  448. PrintBSPFileSizes ();
  449. }
  450. if ( g_bTreeInfo )
  451. {
  452. g_nOptimumDepth = (int)( log( ( float )numnodes ) / log( 2.0f ) );
  453. g_nMinTreeDepth = 999999;
  454. g_nMaxTreeDepth = -999999;
  455. g_TotalTreeDepth = 0;
  456. g_TotalVariance = 0;
  457. CalculateTreeInfo_R( dmodels[0].headnode, 0 );
  458. printf( "\n"
  459. "\t-------------------\n"
  460. "\tTREE INFO:\n"
  461. "\t-------------------\n"
  462. "\tNumber of nodes ------------------ : %d\n"
  463. "\tOptimum tree depth (logN) -------- : %.3f\n"
  464. "\tMinimum tree depth --------------- : %d\n"
  465. "\tMaximum tree depth --------------- : %d\n"
  466. "\tAverage tree depth --------------- : %.3f\n"
  467. "\tAverage leaf variance from optimum : %.3f\n\n",
  468. numnodes,
  469. g_nOptimumDepth,
  470. g_nMinTreeDepth,
  471. g_nMaxTreeDepth,
  472. (float)g_TotalTreeDepth / numnodes,
  473. (float)g_TotalVariance / numnodes );
  474. }
  475. if ( g_bDrawTree )
  476. {
  477. DrawTreeToScratchPad();
  478. }
  479. if( !bWorldTextureStats && !bModelStats && !bListStaticProps )
  480. {
  481. printf ("---------------------\n");
  482. }
  483. }
  484. }