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.

140 lines
3.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Utility function for creating HalfLife1-style prefab libraries from
  4. // a directory full of RMF and MAP files.
  5. //
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "stdafx.h"
  9. #include "hammer.h"
  10. #include "Prefabs.h"
  11. #include "Prefab3d.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include <tier0/memdbgon.h>
  14. //-----------------------------------------------------------------------------
  15. // Purpose: One function - gets all the .rmf and .map files in the current dir,
  16. // merges them with same-name .txt files, and creates a prefab library
  17. // as specified in pszName.
  18. // Input : pszName -
  19. //-----------------------------------------------------------------------------
  20. void MakePrefabLibrary(LPCTSTR pszName)
  21. {
  22. CPrefabLibrary *pLibrary = new CPrefabLibraryRMF;
  23. int nPrefabs = 0;
  24. printf("Making prefab library %s.ol\n", pszName);
  25. pLibrary->SetName(pszName);
  26. // disable caching of prefabs
  27. CPrefab::EnableCaching(FALSE);
  28. // get files
  29. static BOOL bFirst = TRUE;
  30. Again:
  31. WIN32_FIND_DATA fd;
  32. HANDLE hnd = FindFirstFile(bFirst ? "*.rmf" : "*.map", &fd);
  33. if(hnd != INVALID_HANDLE_VALUE) do
  34. {
  35. // check file type
  36. CPrefab *pPrefab = NULL;
  37. int iLoadResult = -1;
  38. switch (CPrefab::CheckFileType(fd.cFileName))
  39. {
  40. case CPrefab::pftUnknown:
  41. {
  42. continue;
  43. }
  44. case CPrefab::pftRMF:
  45. {
  46. CPrefabRMF *pNew = new CPrefabRMF;
  47. iLoadResult = pNew->Init(fd.cFileName, TRUE, CPrefab::lsRMF);
  48. pPrefab = (CPrefab *)pNew;
  49. break;
  50. }
  51. case CPrefab::pftMAP:
  52. {
  53. CPrefabRMF *pNew = new CPrefabRMF;
  54. iLoadResult = pNew->Init(fd.cFileName, TRUE, CPrefab::lsMAP);
  55. pPrefab = (CPrefab *)pNew;
  56. break;
  57. }
  58. case CPrefab::pftScript:
  59. {
  60. Assert(0); // not supported yet
  61. break;
  62. }
  63. }
  64. if(iLoadResult == -1)
  65. {
  66. // pPrefab might be null but delete doesn't care
  67. delete pPrefab;
  68. pPrefab = NULL;
  69. }
  70. if(!pPrefab)
  71. continue;
  72. printf(" including %s\n", fd.cFileName);
  73. ++nPrefabs;
  74. // find text file - set info with it
  75. CString strTextFile = fd.cFileName;
  76. int iPos = strTextFile.Find('.');
  77. strTextFile.GetBuffer(0)[iPos] = 0;
  78. strTextFile.ReleaseBuffer();
  79. strTextFile += ".txt";
  80. if(GetFileAttributes(strTextFile) != 0xFFFFFFFF)
  81. {
  82. std::ifstream tfile(strTextFile);
  83. char szBuffer[1024], szBuffer2[1024];
  84. memset(szBuffer, 0, sizeof szBuffer);
  85. // read file
  86. tfile.read(szBuffer, 1023);
  87. // get rid of \r and \n chars
  88. char *p1 = szBuffer, *p2 = szBuffer2;
  89. while(p1[0])
  90. {
  91. if(p1[0] != '\n' && p1[0] != '\r')
  92. {
  93. p2[0] = p1[0];
  94. ++p2;
  95. }
  96. ++p1;
  97. }
  98. p2[0] = 0;
  99. // set the prefab's info
  100. pPrefab->SetNotes(szBuffer2);
  101. }
  102. // add to new library
  103. pLibrary->Add(pPrefab);
  104. } while(FindNextFile(hnd, &fd));
  105. if(bFirst)
  106. {
  107. bFirst = FALSE;
  108. goto Again;
  109. }
  110. // now rewrite library
  111. pLibrary->Save();
  112. CPrefab::FreeAllData(); // free memory
  113. // re-enable prefab caching
  114. CPrefab::EnableCaching(TRUE);
  115. printf("%d prefabs in library.\n", nPrefabs);
  116. }