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.

235 lines
8.6 KiB

  1. //============ Copyright (c) Valve Corporation, All rights reserved. ============
  2. //
  3. // Class to represent and read/write a BSP file.
  4. //
  5. //===============================================================================
  6. #ifndef SERIALIZESIMPLEBSPFILE_H
  7. #define SERIALIZESIMPLEBSPFILE_H
  8. #if defined( COMPILER_MSVC )
  9. #pragma once
  10. #endif
  11. #include "utlvector.h"
  12. #include "bspfile.h"
  13. #include "gamebspfile.h"
  14. #include "builddisp.h"
  15. #include "vbspmathutil.h"
  16. #include "simplemapfile.h"
  17. class CSimpleBSPFile;
  18. class CBSPNode;
  19. class CBSPFace;
  20. class CPhysCollisionEntry;
  21. class IPhysicsCollision;
  22. class IPhysicsSurfaceProps;
  23. class CUtlBuffer;
  24. //-----------------------------------------------------------------------------
  25. // Serializes a BSP file to a provided buffer.
  26. //-----------------------------------------------------------------------------
  27. void SaveToFile( CUtlBuffer *pOutputBuffer, const CSimpleBSPFile *pBSPFile );
  28. //-----------------------------------------------------------------------------
  29. // Class to build and manipulate a BSP file in its on-disk representation.
  30. //
  31. // This is a lower-level representation of a BSP than CSimpleBSPFile.
  32. //
  33. // Can be used in one of several modes:
  34. //
  35. // 1) Serializing a .bsp file -
  36. //
  37. // a) Given the expanded, in-memory representation
  38. // of a compiled map (CSimpleBSPFile), this class will build
  39. // intermediate representations of various structures where needed and
  40. // serialize the data to a .bsp file. It can be operated in a "memory
  41. // efficient" mode where lumps are written out and freed as soon as
  42. // possible.
  43. //
  44. // b) This class can also serialize a BSP file that was constructed
  45. // in memory or previously deserialized.
  46. //
  47. // 2) Deserializing a .bsp file - the recognized lump types will be loaded
  48. // and stored in memory, at which point they can be manipulated
  49. // as needed.
  50. // WARNING: only recognized lumps will be deserialized; this file
  51. // has incomplete support for many lump types.
  52. //
  53. //-----------------------------------------------------------------------------
  54. class CMemoryBSPFile
  55. {
  56. public:
  57. CMemoryBSPFile();
  58. //-----------------------------------------------------------------------------
  59. // Grabs data from the BSP class and serializes it to the output buffer.
  60. // The output buffer must be at the start of an empty output stream.
  61. //-----------------------------------------------------------------------------
  62. void ProcessAndSerialize( CUtlBuffer *pOutputBuffer, const CSimpleBSPFile *pSimpleBSPFile );
  63. //-----------------------------------------------------------------------------
  64. // Writes data to a .bsp file buffer.
  65. //-----------------------------------------------------------------------------
  66. void Serialize( CUtlBuffer *pOutputBuffer );
  67. //-----------------------------------------------------------------------------
  68. // Reads .bsp data from a buffer into lump-specific arrays.
  69. // Class can then be modified and re-saved to disk with Serialize().
  70. //-----------------------------------------------------------------------------
  71. void Deserialize( CUtlBuffer *pInputBuffer );
  72. bool TryGetFaceVertex( int nFace, int nVertexIndex, Vector *pPosition ) const;
  73. int GetLeafIndexFromPoint( int nNodeIndex, const Vector &vPosition ) const;
  74. private:
  75. bool IsHeaderValid() { return m_FileHeader.ident == IDBSPHEADER; }
  76. // Begin & End are used when you want to manually Put() data into the buffer
  77. void BeginWriteLump( int nLump, int nByteLength, int nVersion = 0 );
  78. void EndWriteLump();
  79. // Writes a whole lump (do not call Begin/EndWriteLump)
  80. void WriteLump( int nLump, const void *pData, int nByteLength, int nVersion = 0 );
  81. template< typename T >
  82. void WriteLump( int nLump, const CUtlVector< T > &data, int nVersion = 0 );
  83. // Begin & End are used when you want to manually Get() data from the buffer
  84. int BeginReadLump( int nLump ); // returns size of the lump
  85. void EndReadLump();
  86. // Read a lump into an array (do not call Begin/EndReadLump)
  87. template< typename T >
  88. void ReadLump( int nLump, CUtlVector< T > *pVector );
  89. void BuildTexInfo();
  90. void WriteTexInfo( bool bPurgeWhenComplete );
  91. void BuildTexData();
  92. int FindOrAddString( const char *pString );
  93. void WriteTexData( bool bPurgeWhenComplete );
  94. void BuildModelData();
  95. void WriteModelData( bool bPurgeWhenComplete );
  96. void WritePortalFaces( const CBSPNode *pNode );
  97. // There are 3 types of faces that can be emitted:
  98. // 1) Portal faces, emitted all at once when a model is written
  99. // 2) Detail faces, emitted after portal faces (also all at once when a model is written)
  100. // 3) Displacement faces, emitted only for the world model (model #0), written all at once with the world model
  101. void EmitFace( const CBSPFace *pBSPFace, bool bOnNode );
  102. void BuildBSPTreeData();
  103. void WriteBSPTreeData( bool bPurgeWhenComplete );
  104. int32 EmitLeaf( const CBSPNode *pNode );
  105. int32 EmitNode( const CBSPNode *pNode );
  106. void BuildBrushes();
  107. void WriteBrushes( bool bPurgeWhenComplete );
  108. void BuildPlanes();
  109. void WritePlanes( bool bPurgeWhenComplete );
  110. void WriteModels( bool bPurgeWhenComplete );
  111. void WriteDummyAreasAndAreaPortals();
  112. void BuildGameLumpData();
  113. void EmitStaticProp( const MapEntity_t *pEntity, CUtlVector< StaticPropLump_t > *pStaticPropLump, CUtlVector< StaticPropLeafLump_t > *pStaticPropLeafLump, CUtlVector< StaticPropDictLump_t > *pStaticPropDictionaryLump );
  114. int AddStaticPropDictionaryEntry( const char *pModelName, CUtlVector< StaticPropDictLump_t > *pStaticPropDictionaryLump );
  115. int AddStaticPropLeaves( const Vector &vOrigin, int nNodeIndex, CUtlVector< StaticPropLeafLump_t > *pStaticPropLeafLump );
  116. void WriteGameLumpData( bool bPurgeWhenComplete );
  117. void BuildEntityData();
  118. void StripTrailingJunkCharacters( char *pString );
  119. void WriteEntityData( bool bPurgeWhenComplete );
  120. void BuildVisibilityData();
  121. void WriteVisibilityData( bool bPurgeWhenComplete );
  122. void BuildDisplacements();
  123. void WriteDisplacements( bool bPurgeWhenComplete );
  124. void BuildPhysicsCollisionData();
  125. CPhysCollisionEntry * CreateWorldPhysicsModels(
  126. IPhysicsCollision *pPhysicsCollision,
  127. CUtlVector< int > *pWorldPropertyRemapList,
  128. const int *pSurfacePropertyList,
  129. const dmodel_t *pModel,
  130. MapBrushContentsFlags_t contentsMask );
  131. CPhysCollisionEntry * CreatePhysicsModel(
  132. IPhysicsCollision *pPhysicsCollision,
  133. IPhysicsSurfaceProps *pPhysicsProperties,
  134. const int *pSurfacePropertyList,
  135. const dmodel_t *pModel );
  136. void BuildDisplacementVirtualMesh( IPhysicsCollision *pPhysicsCollision );
  137. const MapBrushSide_t *FindClosestBrushSide( int nBrushIndex, const Vector &vNormal );
  138. int RemapWorldMaterial( CUtlVector< int > *pWorldPropertyRemapList, int nSurfacePropertyIndex );
  139. void WritePhysicsCollisionData( bool bPurgeWhenComplete );
  140. void WriteLightingData( bool bPurgeWhenComplete );
  141. CUtlBuffer *m_pSerialBuffer;
  142. const CSimpleBSPFile *m_pSimpleBSPFile;
  143. const CSimpleMapFile *m_pMapFile;
  144. CPlaneHash m_PlaneHash;
  145. BSPHeader_t m_FileHeader;
  146. public:
  147. // The following arrays map 1:1 with on-disk BSP data.
  148. CUtlVector< texinfo_t > m_TexInfoList;
  149. CUtlVector< dtexdata_t > m_TexDataList;
  150. CUtlVector< char > m_TexStringData;
  151. CUtlVector< int32 > m_TexStringIndices;
  152. CUtlVector< dmodel_t > m_ModelList;
  153. CVertexHash m_VertexHash;
  154. CUtlVector< dedge_t > m_EdgeList;
  155. CUtlVector< int32 > m_SurfEdgeList;
  156. CUtlVector< dface_t > m_FaceList;
  157. CUtlVector< Vector > m_VertexNormalList;
  158. CUtlVector< uint16 > m_VertexNormalIndexList;
  159. CUtlVector< dnode_t > m_NodeList;
  160. CUtlVector< dleaf_t > m_LeafList;
  161. CUtlVector< uint16 > m_LeafBrushList;
  162. CUtlVector< uint16 > m_LeafFaceList;
  163. CUtlVector< dbrush_t > m_BrushList;
  164. CUtlVector< dbrushside_t > m_BrushSideList;
  165. CUtlVector< dplane_t > m_Planes;
  166. CUtlVector< byte > m_GameLumpData;
  167. CUtlVector< byte > m_EntityData;
  168. CUtlVector< byte > m_VisibilityData;
  169. CUtlVector< CCoreDispInfo * > m_DisplacementHelperList;
  170. CUtlVector< ddispinfo_t > m_DisplacementList;
  171. CUtlVector< CDispVert > m_DisplacementVertexList;
  172. CUtlVector< CDispTri > m_DisplacementTriangleList;
  173. CUtlVector< CDispMultiBlend > m_DisplacementMultiBlendList;
  174. CUtlVector< byte > m_PhysicsDisplacementData;
  175. CUtlVector< byte > m_PhysicsCollideData;
  176. CUtlVector< byte > m_LightingData;
  177. CUtlVector< dworldlight_t > m_WorldLightsLDR;
  178. CUtlVector< dworldlight_t > m_WorldLightsHDR;
  179. // If entityExclusionFlags[N] is set to true, then do not write entity data for entity NF
  180. CBitVec< MAX_MAP_ENTITIES > m_EntityExclusionFlags;
  181. };
  182. #endif // SERIALIZESIMPLEBSPFILE_H