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.

406 lines
8.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. /*****************************************************************************
  3. DUMPOBJ.CPP
  4. *****************************************************************************/
  5. #include "..\toollib\toollib.h"
  6. typedef struct section_s
  7. {
  8. int size;
  9. char* name;
  10. section_s* nextPtr;
  11. } section_t;
  12. typedef struct obj_s
  13. {
  14. char* filename;
  15. section_t* sectionPtr;
  16. obj_s* nextPtr;
  17. } obj_t;
  18. obj_t* g_obj_head;
  19. char g_sectionName[128];
  20. bool g_bVerbose;
  21. typedef struct
  22. {
  23. char* filename;
  24. int size;
  25. } entry_t;
  26. /***********************************************************
  27. _SortCallback
  28. ***********************************************************/
  29. int _SortCallback(const void *a, const void *b)
  30. {
  31. entry_t* aPtr;
  32. entry_t* bPtr;
  33. aPtr = (entry_t*)a;
  34. bPtr = (entry_t*)b;
  35. return (aPtr->size-bPtr->size);
  36. }
  37. /***********************************************************
  38. FreeInfo
  39. ***********************************************************/
  40. void FreeInfo()
  41. {
  42. obj_t* objPtr;
  43. obj_t* nextObjPtr;
  44. section_t* sectionPtr;
  45. section_t* nextSectionPtr;
  46. objPtr = g_obj_head;
  47. while (objPtr)
  48. {
  49. nextObjPtr = objPtr->nextPtr;
  50. sectionPtr = objPtr->sectionPtr;
  51. while (sectionPtr)
  52. {
  53. nextSectionPtr = sectionPtr->nextPtr;
  54. TL_Free(sectionPtr->name);
  55. TL_Free(sectionPtr);
  56. sectionPtr = nextSectionPtr;
  57. }
  58. TL_Free(objPtr->filename);
  59. TL_Free(objPtr);
  60. objPtr = nextObjPtr;
  61. }
  62. }
  63. /***********************************************************
  64. AnalyzeData
  65. ***********************************************************/
  66. void AnalyzeData()
  67. {
  68. obj_t* objPtr;
  69. section_t* sectionPtr;
  70. char* sections[128];
  71. int totals[128];
  72. int numSections;
  73. int i;
  74. int index;
  75. int numEntries;
  76. entry_t* entries;
  77. int total;
  78. printf("\n");
  79. memset(totals, 0, sizeof(totals));
  80. // determine unique sections
  81. numSections = 0;
  82. objPtr = g_obj_head;
  83. while (objPtr)
  84. {
  85. sectionPtr = objPtr->sectionPtr;
  86. while (sectionPtr)
  87. {
  88. for (i=0; i<numSections; i++)
  89. {
  90. if (!stricmp(sections[i],sectionPtr->name))
  91. break;
  92. }
  93. if (i >= numSections)
  94. {
  95. // add it
  96. sections[numSections] = sectionPtr->name;
  97. numSections++;
  98. assert(numSections < 128);
  99. index = numSections-1;
  100. }
  101. else
  102. index = i;
  103. totals[index] += sectionPtr->size;
  104. sectionPtr = sectionPtr->nextPtr;
  105. }
  106. objPtr = objPtr->nextPtr;
  107. }
  108. if (g_sectionName[0])
  109. {
  110. // dump only matching section
  111. printf("Section: %s\n", g_sectionName);
  112. printf("-------------------------\n");
  113. // tally entries
  114. numEntries = 0;
  115. objPtr = g_obj_head;
  116. while (objPtr)
  117. {
  118. sectionPtr = objPtr->sectionPtr;
  119. while (sectionPtr)
  120. {
  121. if (!stricmp(g_sectionName, sectionPtr->name))
  122. numEntries++;
  123. sectionPtr = sectionPtr->nextPtr;
  124. }
  125. objPtr = objPtr->nextPtr;
  126. }
  127. // fill entries
  128. if (numEntries)
  129. {
  130. entries = (entry_t*)TL_Malloc(numEntries*sizeof(entry_t));
  131. numEntries = 0;
  132. objPtr = g_obj_head;
  133. while (objPtr)
  134. {
  135. sectionPtr = objPtr->sectionPtr;
  136. while (sectionPtr)
  137. {
  138. if (!stricmp(g_sectionName, sectionPtr->name))
  139. {
  140. entries[numEntries].size = sectionPtr->size;
  141. entries[numEntries].filename = objPtr->filename;
  142. numEntries++;
  143. }
  144. sectionPtr = sectionPtr->nextPtr;
  145. }
  146. objPtr = objPtr->nextPtr;
  147. }
  148. // sort
  149. qsort(entries, numEntries, sizeof(entry_t), _SortCallback);
  150. // display results
  151. int total = 0;
  152. for (i=0; i<numEntries; i++)
  153. {
  154. printf("%8d %s\n", entries[i].size, entries[i].filename);
  155. total += entries[i].size;
  156. }
  157. printf("-------------------------\n");
  158. printf("%8d Total\n", total);
  159. TL_Free(entries);
  160. }
  161. }
  162. else
  163. {
  164. // dump all sections
  165. printf("Sections:\n");
  166. printf("---------\n");
  167. total = 0;
  168. for (i=0; i<numSections; i++)
  169. {
  170. printf("%8d %s\n", totals[i], sections[i]);
  171. total += totals[i];
  172. }
  173. printf("---------\n");
  174. printf("%8d\n", total);
  175. }
  176. }
  177. /***********************************************************
  178. LocalExec
  179. ***********************************************************/
  180. int LocalExec(const char* batfilename, const char* arg1)
  181. {
  182. intptr_t pid;
  183. int errcode;
  184. const char* args[8];
  185. args[0] = batfilename;
  186. args[1] = arg1;
  187. args[2] = NULL;
  188. pid = _spawnvp(P_NOWAIT, batfilename, args);
  189. _cwait(&errcode, pid, 0);
  190. return (errcode);
  191. }
  192. /*****************************************************************************
  193. GetInfo
  194. *****************************************************************************/
  195. void GetInfo(char* objFilename, char* batFilename, char* logFilename)
  196. {
  197. char buff[TL_MAXPATH];
  198. int size;
  199. char name[128];
  200. char* ptr;
  201. int errcode;
  202. char* token;
  203. obj_t* objPtr;
  204. section_t* sectionPtr;
  205. if (g_bVerbose)
  206. printf("%s\n", objFilename);
  207. ptr = objFilename;
  208. if (!strnicmp(objFilename,".\\",2))
  209. {
  210. ptr += 2;
  211. getcwd(buff, sizeof(buff));
  212. TL_AddSeperatorToPath(buff, buff);
  213. strcat(buff, ptr);
  214. }
  215. else
  216. strcpy(buff, objFilename);
  217. // exec the batch file with the obj file
  218. errcode = LocalExec(batFilename, buff);
  219. if (errcode)
  220. {
  221. printf("Failed on %s\n", buff);
  222. return;
  223. }
  224. // add new object node
  225. objPtr = (obj_t*)TL_Malloc(sizeof(obj_t));
  226. objPtr->filename = (char*)TL_Malloc((int)strlen(objFilename)+1);
  227. strcpy(objPtr->filename, objFilename);
  228. // link it in
  229. objPtr->nextPtr = g_obj_head;
  230. g_obj_head = objPtr;
  231. // read the results
  232. TL_LoadScriptFile(logFilename);
  233. while (1)
  234. {
  235. token = TL_GetToken(true);
  236. if (!token || !token[0])
  237. break;
  238. if (!stricmp(token, "summary"))
  239. break;
  240. else
  241. TL_SkipRestOfLine();
  242. }
  243. while (1)
  244. {
  245. token = TL_GetToken(true);
  246. if (!token || !token[0])
  247. break;
  248. sscanf(token, "%x", &size);
  249. token = TL_GetToken(true);
  250. if (!token || !token[0])
  251. break;
  252. strcpy(name, token);
  253. // add new section node
  254. sectionPtr = (section_t*)TL_Malloc(sizeof(section_t));
  255. sectionPtr->name = (char*)TL_Malloc((int)strlen(name)+1);
  256. strcpy(sectionPtr->name, name);
  257. sectionPtr->size = size;
  258. // link it in
  259. sectionPtr->nextPtr = objPtr->sectionPtr;
  260. objPtr->sectionPtr = sectionPtr;
  261. }
  262. TL_FreeScriptFile();
  263. }
  264. /*****************************************************************************
  265. Usage
  266. *****************************************************************************/
  267. void Usage(void)
  268. {
  269. printf("usage: dumpobj <*.obj> [-s section] [-r] [-v]\n");
  270. exit(-1);
  271. }
  272. /*****************************************************************************
  273. main
  274. *****************************************************************************/
  275. int main(int argc, char* argv[])
  276. {
  277. tlfile_t** filelist;
  278. int filecount;
  279. int i;
  280. int recurse;
  281. int section;
  282. FILE* fp;
  283. char* vcvarsPath;
  284. char batFilename[TL_MAXPATH];
  285. char txtFilename[TL_MAXPATH];
  286. batFilename[0] = '\0';
  287. txtFilename[0] = '\0';
  288. TL_Setup("DUMPOBJ",argc,argv);
  289. // find critical path to vcvars32.bat
  290. vcvarsPath = "c:\\Program Files\\Microsoft Visual Studio .Net 2003\\Vc7\\bin\\vcvars32.bat";
  291. if (!TL_Exists(vcvarsPath))
  292. TL_Error("Cannot find: %s\n", vcvarsPath);
  293. if (argc < 2)
  294. Usage();
  295. recurse = TL_CheckParm("r");
  296. filecount = TL_FindFiles2(argv[1], recurse != 0, &filelist);
  297. g_bVerbose = TL_CheckParm("v") != 0;
  298. section = TL_CheckParm("s");
  299. if (section && section+1 < argc)
  300. strcpy(g_sectionName, argv[section+1]);
  301. else
  302. g_sectionName[0] = '\0';
  303. strcpy(batFilename,"c:\\");
  304. TL_TempFilename(batFilename);
  305. TL_ReplaceDosExtension(batFilename, ".bat");
  306. strcpy(txtFilename,"c:\\");
  307. TL_TempFilename(txtFilename);
  308. TL_ReplaceDosExtension(txtFilename, ".txt");
  309. if (filecount)
  310. {
  311. // create bat file
  312. fp = fopen(batFilename, "wt+");
  313. if (!fp)
  314. TL_Error("Could not open bat file '%s'", batFilename);
  315. fprintf(fp, "@echo off\n");
  316. fprintf(fp, "@call \"%s\" > nul: \n", vcvarsPath);
  317. fprintf(fp, "@dumpbin %%1 > %s\n", txtFilename);
  318. fclose(fp);
  319. }
  320. for (i=0; i<filecount; i++)
  321. GetInfo(filelist[i]->filename, batFilename, txtFilename);
  322. if (filecount)
  323. AnalyzeData();
  324. if (filecount)
  325. {
  326. FreeInfo();
  327. unlink(batFilename);
  328. unlink(txtFilename);
  329. }
  330. TL_FreeFileList(filecount,filelist);
  331. TL_End(false);
  332. return (0);
  333. }