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.

180 lines
4.1 KiB

  1. //========= Copyright c 1996-2008, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef DATA_LINKER_H
  8. #define DATA_LINKER_H
  9. #include "datalinker_interface.h"
  10. #include <map>
  11. #include <vector>
  12. #include <string>
  13. #include "tier1/UtlStringMap.h"
  14. namespace DataLinker
  15. {
  16. enum LinkTargetEnum
  17. {
  18. kTargetResolved,
  19. kTargetUnresolved,
  20. kTargetNull
  21. };
  22. struct LinkTargetDesc_t
  23. {
  24. LinkTargetEnum m_type;
  25. int m_resolvedOffset;
  26. LinkTargetDesc_t()
  27. {
  28. m_resolvedOffset = -1;
  29. m_type = kTargetUnresolved;
  30. }
  31. };
  32. struct LinkSourceDesc_t
  33. {
  34. int m_targetId; // when this is 0, we use the embedded structure
  35. OffsetTypeEnum m_type;
  36. const char *m_pDescription;
  37. LinkTargetDesc_t m_defaultTarget;
  38. };
  39. typedef std::map<int, LinkSourceDesc_t> LinkSourceMap;
  40. typedef std::map<int, LinkTargetDesc_t> LinkTargetMap;
  41. class Chunk
  42. {
  43. public:
  44. Chunk() {}
  45. Chunk( uint reserve, uint offset );
  46. void Free();
  47. uint GetAvailable()const
  48. {
  49. return m_capacity - m_size;
  50. }
  51. uint m_offset;
  52. uint m_size;
  53. uint m_capacity;
  54. uint m_recentSize;
  55. byte *m_data;
  56. };
  57. //////////////////////////////////////////////////////////////////////////
  58. // This writer uses malloc() to allocate memory for the binary data
  59. class Stream: public ILinkStream
  60. {
  61. public:
  62. Stream();
  63. ~Stream();
  64. void* WriteBytes( uint numBytes );
  65. void EnsureAvailable( uint addCapacity );
  66. void Link( LinkSource_t, LinkTarget_t, const char *szDescription );
  67. void Align( uint nAlignment, int nOffset = 0 );
  68. long Tell()
  69. {
  70. return m_size;
  71. }
  72. LinkSource_t WriteOffset( const char *szDescription );
  73. LinkSource_t WriteOffset( LinkTarget_t linkTarget, const char *szDescription );
  74. LinkSource_t WriteNullOffset( const char *szDescription )
  75. {
  76. return WriteOffset( LinkTargetNull_t(), szDescription );
  77. }
  78. LinkSource_t NewOffset( int *pOffset, const char *szDescription );
  79. LinkSource_t LinkToHere( int *pOffset, const char *szDescription );
  80. LinkSource_t Link( int32 *pOffset, LinkTarget_t linkTarget, const char *szDescription );
  81. void Link( int32 *pOffset, const void *pTarget );
  82. void Link( int16 *pOffset, const void *pTarget );
  83. LinkTarget_t NewTarget(); // create new, unresolved target
  84. LinkTarget_t NewTarget( void *pWhere );
  85. LinkTarget_t NewTargetHere(); // creates a target right here
  86. void SetTargetHere( LinkTarget_t ); // sets the given target to point to right here
  87. void SetTargetNull( LinkTarget_t ); // set this target to point to NULL
  88. // make the targets point to the same place
  89. //void Assign(LinkTarget_t assignee, LinkTarget_t assignor);
  90. bool Compile( void *pBuffer );
  91. uint GetTotalSize()const
  92. {
  93. return m_size;
  94. }
  95. bool IsDeclared( LinkTarget_t linkTarget )const;
  96. bool IsSet( LinkTarget_t linkTarget )const;
  97. bool IsDefined( LinkSource_t linkSource )const;
  98. bool IsLinked( LinkSource_t linkSource )const;
  99. const char* WriteAndLinkString( Offset_t<char> *pOffset, const char *pString );
  100. long Tell()const
  101. {
  102. return m_size;
  103. }
  104. void Begin( const char *nName, uint flags = 0 );
  105. void End();
  106. void PrintStats();
  107. void ClearStats();
  108. protected:
  109. LinkSource_t NewLinkSource( int offsetOfOffset, OffsetTypeEnum type, int linkTargetId, const char *szDescription );
  110. int GetOffsetTo( const void *ptr )const;
  111. void SetTarget( LinkTarget_t linkTarget, int offsetToTarget );
  112. protected:
  113. std::vector<Chunk> m_data;
  114. struct StringTableElement_t
  115. {
  116. char *m_pString;
  117. int m_nOffset;
  118. StringTableElement_t(): m_pString( NULL ), m_nOffset( NULL ) {}
  119. };
  120. CUtlStringMap<StringTableElement_t> m_stringTable; // each string offset
  121. uint m_size; // this is the currently used data
  122. int m_lastTarget; // the last id of LinkTarget
  123. uint m_alignBits; // the ( max alignment - 1 ) of the current block of data
  124. // LinkSource_t::m_offset -> ...
  125. LinkSourceMap m_mapSources;
  126. //
  127. LinkTargetMap m_mapTargets;
  128. struct BlockStats_t
  129. {
  130. BlockStats_t(): m_size( 0 ) {}
  131. long m_size;
  132. };
  133. typedef std::map<std::string, BlockStats_t>BlockStatsMap;
  134. BlockStatsMap m_mapBlockStats;
  135. struct Block_t
  136. {
  137. std::string m_name;
  138. long m_tell;
  139. };
  140. std::vector<Block_t> m_stackBlocks;
  141. };
  142. }
  143. #endif