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.

145 lines
5.7 KiB

  1. //===== Copyright c 1996-2008, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose: Describes our resource format. All resource files have a simplistic
  4. // dictionary of resource "blocks", which can be looked up using a block type id.
  5. // Each resource block type is expected to be associated with a well defined
  6. // structure. The macro DEFINE_RESOURCE_BLOCK_TYPE is used to create this
  7. // association.
  8. //
  9. // The current design choice is that we expect files using this resource format
  10. // to be small. Large-scale files, like sound or texture bits, are expected to
  11. // exist in a file parallel to the file containing these resource blocks owing
  12. // to issues of alignment or streaming. We therefore expect users of files
  13. // containing the data described in this header to load the entire file in
  14. // so that all blocks are in memory.
  15. //
  16. // $NoKeywords: $
  17. //===========================================================================//
  18. #ifndef RESOURCEFILE_H
  19. #define RESOURCEFILE_H
  20. #pragma once
  21. #include "resourcefile/schema.h"
  22. #include "tier0/platform.h"
  23. #include "resourcefile/resourcestream.h"
  24. #include "datamap.h"
  25. //-----------------------------------------------------------------------------
  26. //
  27. // On-disk structures related to the resource file format
  28. //
  29. //-----------------------------------------------------------------------------
  30. //-----------------------------------------------------------------------------
  31. // Resource block types
  32. //-----------------------------------------------------------------------------
  33. typedef uint32 ResourceBlockId_t;
  34. //-----------------------------------------------------------------------------
  35. // A block of resource data
  36. //-----------------------------------------------------------------------------
  37. struct ResourceBlockEntry_t
  38. {
  39. DECLARE_BYTESWAP_DATADESC();
  40. ResourceBlockId_t m_nBlockType;
  41. CResourcePointer<void> m_pBlockData;
  42. };
  43. //-----------------------------------------------------------------------------
  44. // Structure of resource file header
  45. //-----------------------------------------------------------------------------
  46. enum ResourceFileHeaderVersion_t
  47. {
  48. RESOURCE_FILE_HEADER_VERSION = 1,
  49. };
  50. struct ResourceFileHeader_t
  51. {
  52. DECLARE_BYTESWAP_DATADESC();
  53. uint32 m_nVersion; // see ResourceFileHeaderVersion_t
  54. uint32 m_nSizeInBytes; // Size in bytes of entire file
  55. CResourceArray< ResourceBlockEntry_t > m_ResourceBlocks;
  56. };
  57. //-----------------------------------------------------------------------------
  58. //
  59. // Methods used to define resource blocks/read them from files
  60. //
  61. //-----------------------------------------------------------------------------
  62. //-----------------------------------------------------------------------------
  63. // Resource block IDs
  64. //-----------------------------------------------------------------------------
  65. #define RSRC_BYTE_POS( byteVal, shft ) ResourceBlockId_t( uint32(uint8(byteVal)) << uint8(shft * 8) )
  66. #if !defined( PLATFORM_X360 )
  67. #define MK_RSRC_BLOCK_ID(a, b, c, d) ResourceBlockId_t( RSRC_BYTE_POS(a, 0) | RSRC_BYTE_POS(b, 1) | RSRC_BYTE_POS(c, 2) | RSRC_BYTE_POS(d, 3) )
  68. #else
  69. #define MK_RSRC_BLOCK_ID(a, b, c, d) ResourceBlockId_t( RSRC_BYTE_POS(a, 3) | RSRC_BYTE_POS(b, 2) | RSRC_BYTE_POS(c, 1) | RSRC_BYTE_POS(d, 0) )
  70. #endif
  71. #define RESOURCE_BLOCK_ID_INVALID 0xFFFFFFFF
  72. //-----------------------------------------------------------------------------
  73. // Helpers used to define resource block IDs + associated structs
  74. //-----------------------------------------------------------------------------
  75. template< class T >
  76. struct ResourceBlockIdSelector_t
  77. {
  78. enum { RESOURCE_BLOCK_ID = RESOURCE_BLOCK_ID_INVALID };
  79. };
  80. #define DEFINE_RESOURCE_BLOCK_TYPE( _class, _ida, _idb, _idc, _idd ) \
  81. template <> struct ResourceBlockIdSelector_t< _class > { enum { RESOURCE_BLOCK_ID = MK_RSRC_BLOCK_ID( _ida, _idb, _idc, _idd ) }; };
  82. //-----------------------------------------------------------------------------
  83. // Does this resource file contain a particular block?
  84. //-----------------------------------------------------------------------------
  85. bool Resource_IsBlockDefined( const ResourceFileHeader_t *pHeader, ResourceBlockId_t id );
  86. //-----------------------------------------------------------------------------
  87. // Gets the data associated with a particular data block
  88. //-----------------------------------------------------------------------------
  89. const void *Resource_GetBlock( const ResourceFileHeader_t *pHeader, ResourceBlockId_t id );
  90. template< class T > inline const T* Resource_GetBlock( const ResourceFileHeader_t *pHeader )
  91. {
  92. return (const T*)Resource_GetBlock( pHeader, ResourceBlockIdSelector_t< T >::RESOURCE_BLOCK_ID );
  93. }
  94. //-----------------------------------------------------------------------------
  95. // Helper methods to write block information
  96. //-----------------------------------------------------------------------------
  97. inline ResourceFileHeader_t *Resource_AllocateHeader( CResourceStream *pStream, int nBlockCount )
  98. {
  99. ResourceFileHeader_t *pHeader = pStream->Allocate< ResourceFileHeader_t >();
  100. pHeader->m_nVersion = RESOURCE_FILE_HEADER_VERSION;
  101. pHeader->m_ResourceBlocks = pStream->Allocate< ResourceBlockEntry_t >( nBlockCount );
  102. return pHeader;
  103. }
  104. //-----------------------------------------------------------------------------
  105. // Helper methods to write block information
  106. //-----------------------------------------------------------------------------
  107. template< class T > inline T* Resource_AllocateBlock( CResourceStream *pStream, ResourceFileHeader_t *pHeader, int nBlockIndex )
  108. {
  109. pHeader->m_ResourceBlocks[nBlockIndex].m_nBlockType = ResourceBlockIdSelector_t< T >::RESOURCE_BLOCK_ID;
  110. T *pBlockData = pStream->Allocate< T >( 1 );
  111. pHeader->m_ResourceBlocks[nBlockIndex].m_pBlockData = pBlockData;
  112. return pBlockData;
  113. }
  114. #endif // RESOURCEFILE_H