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.

136 lines
3.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "map_shared.h"
  8. #include "bsplib.h"
  9. #include "cmdlib.h"
  10. CMapError g_MapError;
  11. int g_nMapFileVersion;
  12. //-----------------------------------------------------------------------------
  13. // Purpose:
  14. // Input : *szKey -
  15. // *szValue -
  16. // *pLoadEntity -
  17. // Output : ChunkFileResult_t
  18. //-----------------------------------------------------------------------------
  19. ChunkFileResult_t LoadEntityKeyCallback(const char *szKey, const char *szValue, LoadEntity_t *pLoadEntity)
  20. {
  21. if (!stricmp(szKey, "classname"))
  22. {
  23. if (!stricmp(szValue, "func_detail"))
  24. {
  25. pLoadEntity->nBaseContents = CONTENTS_DETAIL;
  26. }
  27. else if (!stricmp(szValue, "func_ladder"))
  28. {
  29. pLoadEntity->nBaseContents = CONTENTS_LADDER;
  30. }
  31. else if (!stricmp(szValue, "func_water"))
  32. {
  33. pLoadEntity->nBaseContents = CONTENTS_WATER;
  34. }
  35. }
  36. else if (!stricmp(szKey, "id"))
  37. {
  38. // UNDONE: flag entity errors by ID instead of index
  39. //g_MapError.EntityState( atoi( szValue ) );
  40. // rename this field since DME code uses this name
  41. SetKeyValue( pLoadEntity->pEntity, "hammerid", szValue );
  42. return(ChunkFile_Ok);
  43. }
  44. else if( !stricmp( szKey, "mapversion" ) )
  45. {
  46. // .vmf map revision number
  47. g_MapRevision = atoi( szValue );
  48. SetKeyValue( pLoadEntity->pEntity, szKey, szValue );
  49. return ( ChunkFile_Ok );
  50. }
  51. SetKeyValue( pLoadEntity->pEntity, szKey, szValue );
  52. return(ChunkFile_Ok);
  53. }
  54. static ChunkFileResult_t LoadEntityCallback( CChunkFile *pFile, int nParam )
  55. {
  56. if (num_entities == MAX_MAP_ENTITIES)
  57. {
  58. // Exits.
  59. g_MapError.ReportError ("num_entities == MAX_MAP_ENTITIES");
  60. }
  61. entity_t *mapent = &entities[num_entities];
  62. num_entities++;
  63. memset(mapent, 0, sizeof(*mapent));
  64. mapent->numbrushes = 0;
  65. LoadEntity_t LoadEntity;
  66. LoadEntity.pEntity = mapent;
  67. // No default flags/contents
  68. LoadEntity.nBaseFlags = 0;
  69. LoadEntity.nBaseContents = 0;
  70. //
  71. // Read the entity chunk.
  72. //
  73. ChunkFileResult_t eResult = pFile->ReadChunk((KeyHandler_t)LoadEntityKeyCallback, &LoadEntity);
  74. return eResult;
  75. }
  76. bool LoadEntsFromMapFile( char const *pFilename )
  77. {
  78. //
  79. // Dummy this up for the texture handling. This can be removed when old .MAP file
  80. // support is removed.
  81. //
  82. g_nMapFileVersion = 400;
  83. //
  84. // Open the file.
  85. //
  86. CChunkFile File;
  87. ChunkFileResult_t eResult = File.Open( pFilename, ChunkFile_Read );
  88. if(eResult == ChunkFile_Ok)
  89. {
  90. num_entities = 0;
  91. //
  92. // Set up handlers for the subchunks that we are interested in.
  93. //
  94. CChunkHandlerMap Handlers;
  95. Handlers.AddHandler("entity", (ChunkHandler_t)LoadEntityCallback, 0);
  96. File.PushHandlers(&Handlers);
  97. //
  98. // Read the sub-chunks. We ignore keys in the root of the file.
  99. //
  100. while (eResult == ChunkFile_Ok)
  101. {
  102. eResult = File.ReadChunk();
  103. }
  104. File.PopHandlers();
  105. return true;
  106. }
  107. else
  108. {
  109. Error("Error in LoadEntsFromMapFile (in-memory file): %s.\n", File.GetErrorText(eResult));
  110. return false;
  111. }
  112. }