Counter Strike : Global Offensive Source Code
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.

141 lines
3.2 KiB

  1. //========= Copyright � 1996-2005, 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. if ( g_MapRevision == 0 )
  48. {
  49. // only take the first occurrence of this. func_instance will each have another occurrence but we want the root map's revision
  50. g_MapRevision = atoi( szValue );
  51. Msg("Map revision %d\n", g_MapRevision );
  52. }
  53. SetKeyValue( pLoadEntity->pEntity, szKey, szValue );
  54. return ( ChunkFile_Ok );
  55. }
  56. SetKeyValue( pLoadEntity->pEntity, szKey, szValue );
  57. return(ChunkFile_Ok);
  58. }
  59. static ChunkFileResult_t LoadEntityCallback( CChunkFile *pFile, int nParam )
  60. {
  61. if (num_entities == MAX_MAP_ENTITIES)
  62. {
  63. // Exits.
  64. g_MapError.ReportError ("num_entities == MAX_MAP_ENTITIES");
  65. }
  66. entity_t *mapent = &entities[num_entities];
  67. num_entities++;
  68. memset(mapent, 0, sizeof(*mapent));
  69. mapent->numbrushes = 0;
  70. LoadEntity_t LoadEntity;
  71. LoadEntity.pEntity = mapent;
  72. // No default flags/contents
  73. LoadEntity.nBaseFlags = 0;
  74. LoadEntity.nBaseContents = 0;
  75. //
  76. // Read the entity chunk.
  77. //
  78. ChunkFileResult_t eResult = pFile->ReadChunk((KeyHandler_t)LoadEntityKeyCallback, &LoadEntity);
  79. return eResult;
  80. }
  81. bool LoadEntsFromMapFile( char const *pFilename )
  82. {
  83. //
  84. // Dummy this up for the texture handling. This can be removed when old .MAP file
  85. // support is removed.
  86. //
  87. g_nMapFileVersion = 400;
  88. //
  89. // Open the file.
  90. //
  91. CChunkFile File;
  92. ChunkFileResult_t eResult = File.Open( pFilename, ChunkFile_Read );
  93. if(eResult == ChunkFile_Ok)
  94. {
  95. num_entities = 0;
  96. //
  97. // Set up handlers for the subchunks that we are interested in.
  98. //
  99. CChunkHandlerMap Handlers;
  100. Handlers.AddHandler("entity", (ChunkHandler_t)LoadEntityCallback, 0);
  101. File.PushHandlers(&Handlers);
  102. //
  103. // Read the sub-chunks. We ignore keys in the root of the file.
  104. //
  105. while (eResult == ChunkFile_Ok)
  106. {
  107. eResult = File.ReadChunk();
  108. }
  109. File.PopHandlers();
  110. return true;
  111. }
  112. else
  113. {
  114. Error("Error in LoadEntsFromMapFile (in-memory file): %s.\n", File.GetErrorText(eResult));
  115. return false;
  116. }
  117. }