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.

137 lines
3.8 KiB

  1. //====== Copyright c 1996-2007, Valve Corporation, All rights reserved. =======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "platform.h"
  9. #include "utlvector.h"
  10. #include "chunkfile.h"
  11. #include "vmfentitysupport.h"
  12. //////////////////////////////////////////////////////////////////////////
  13. //
  14. // Additional custom save load handlers for entities
  15. //
  16. //////////////////////////////////////////////////////////////////////////
  17. VmfSaveLoadHandlers & VmfGetSaveLoadHandlers()
  18. {
  19. static VmfSaveLoadHandlers s_arrHandlers;
  20. return s_arrHandlers;
  21. }
  22. void VmfInstallMapEntitySaveLoadHandler( IMapEntitySaveLoadHandler *pHandler )
  23. {
  24. VmfGetSaveLoadHandlers().AddToTail( pHandler );
  25. }
  26. void VmfUninstallMapEntitySaveLoadHandler( IMapEntitySaveLoadHandler *pHandler )
  27. {
  28. VmfSaveLoadHandlers &arr = VmfGetSaveLoadHandlers();
  29. for ( int k = 0; k < arr.Count(); ++ k )
  30. {
  31. if ( arr.Element( k ) == pHandler )
  32. {
  33. arr.Remove( k );
  34. return;
  35. }
  36. }
  37. }
  38. struct VmfSaveLoadHandlerInfo_t
  39. {
  40. IMapEntity_Type_t *pEntity;
  41. IMapEntitySaveLoadHandler *pHandler;
  42. };
  43. static ChunkFileResult_t VmfMapEntitySaveLoadHandlerCallback( CChunkFile *pFile, VmfSaveLoadHandlerInfo_t *pInfo )
  44. {
  45. pInfo->pHandler->SetCurrentEntity( pInfo->pEntity );
  46. return pInfo->pHandler->LoadVMF( pFile );
  47. }
  48. static ChunkFileResult_t MapEntityLoadKVHandlerCallback( const char *szKey, const char *szValue, VmfSaveLoadHandlerInfo_t *pInfo )
  49. {
  50. // pInfo->pHandler->SetCurrentEntity( pInfo->pEntity );
  51. return pInfo->pHandler->LoadKeyValue( szKey, szValue );
  52. }
  53. void VmfAddMapEntityHandlers( CChunkHandlerMap *pHandlerMap, IMapEntity_Type_t *pEntity )
  54. {
  55. // Add all custom handlers
  56. VmfSaveLoadHandlers &arrHandlers = VmfGetSaveLoadHandlers();
  57. static CUtlVector< VmfSaveLoadHandlerInfo_t > arrHandlerInfos;
  58. arrHandlerInfos.SetCount( arrHandlers.Count() );
  59. for ( int idxHandler = 0; idxHandler < arrHandlers.Count(); ++ idxHandler )
  60. {
  61. IMapEntitySaveLoadHandler *pHandler = arrHandlers.Element( idxHandler );
  62. VmfSaveLoadHandlerInfo_t &info = arrHandlerInfos.Element( idxHandler );
  63. info.pEntity = pEntity;
  64. info.pHandler = pHandler;
  65. pHandlerMap->AddHandler( pHandler->GetCustomSectionName(), (ChunkHandler_t)VmfMapEntitySaveLoadHandlerCallback, &info );
  66. }
  67. }
  68. ChunkFileResult_t VmfSaveVmfEntityHandlers( CChunkFile *pFile,
  69. IMapEntity_Type_t *pEntity,
  70. IMapEntity_SaveInfo_t *pSaveInfo )
  71. {
  72. ChunkFileResult_t eResult = ChunkFile_Ok;
  73. VmfSaveLoadHandlers &arrHandlers = VmfGetSaveLoadHandlers();
  74. for ( int idxHandler = 0;
  75. (eResult == ChunkFile_Ok) && ( idxHandler < arrHandlers.Count() );
  76. ++ idxHandler )
  77. {
  78. IMapEntitySaveLoadHandler *pHandler = arrHandlers.Element( idxHandler );
  79. pHandler->SetCurrentEntity( pEntity );
  80. eResult = pHandler->SaveVMF( pFile, pSaveInfo );
  81. }
  82. return eResult;
  83. }
  84. void IMapEntitySaveLoadHandler::SetCurrentEntity( IMapEntity_Type_t *pEntity )
  85. {
  86. }
  87. ChunkFileResult_t IMapEntitySaveLoadHandler::SaveVMF( CChunkFile *pFile, IMapEntity_SaveInfo_t *pSaveInfo )
  88. {
  89. return ChunkFile_Ok;
  90. }
  91. ChunkFileResult_t IMapEntitySaveLoadHandler::LoadVMF( CChunkFile *pFile )
  92. {
  93. VmfSaveLoadHandlerInfo_t info;
  94. // info.pEntity = pEntity;
  95. info.pHandler = this;
  96. ChunkFileResult_t eResult = LoadKeyValueBegin( pFile );
  97. if ( eResult == ChunkFile_Ok )
  98. {
  99. eResult = pFile->ReadChunk( (KeyHandler_t)MapEntityLoadKVHandlerCallback, &info );
  100. }
  101. return LoadKeyValueEnd( pFile, eResult );
  102. }
  103. ChunkFileResult_t IMapEntitySaveLoadHandler::LoadKeyValueBegin( CChunkFile *pFile )
  104. {
  105. return ChunkFile_Ok;
  106. }
  107. ChunkFileResult_t IMapEntitySaveLoadHandler::LoadKeyValue( const char *szKey, const char *szValue )
  108. {
  109. return ChunkFile_Ok;
  110. }
  111. ChunkFileResult_t IMapEntitySaveLoadHandler::LoadKeyValueEnd( CChunkFile *pFile, ChunkFileResult_t eLoadResult )
  112. {
  113. return eLoadResult;
  114. }