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.

188 lines
3.3 KiB

  1. //====== Copyright (c) 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef KEYVALUESCOMPILER_H
  7. #define KEYVALUESCOMPILER_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "tier0/platform.h"
  12. #include "tier1/utlbuffer.h"
  13. #include "tier1/utlsymbol.h"
  14. #include "tier1/utldict.h"
  15. class KeyValues;
  16. #define COMPILED_KEYVALUES_ID MAKEID( 'V', 'K', 'V', 'F' )
  17. #define COMPILED_KEYVALUES_VERSION 1
  18. struct KVHeader_t
  19. {
  20. int fileid;
  21. int version;
  22. int numStrings;
  23. };
  24. #pragma pack(1)
  25. struct KVFile_t
  26. {
  27. KVFile_t() :
  28. filename( 0 ),
  29. firstElement( 0 ),
  30. numElements( 0 )
  31. {
  32. }
  33. short filename;
  34. short firstElement;
  35. short numElements;
  36. };
  37. struct KVInfo_t
  38. {
  39. KVInfo_t() :
  40. key( 0 ),
  41. value( 0 ),
  42. parentIndex( -1 ),
  43. issubtree( false )
  44. {
  45. }
  46. inline void SetParent( int index )
  47. {
  48. Assert( index <= 32768 );
  49. parentIndex = index;
  50. }
  51. inline short GetParent() const
  52. {
  53. return parentIndex;
  54. }
  55. inline void SetSubTree( bool state )
  56. {
  57. issubtree = state;
  58. }
  59. inline bool IsSubTree() const
  60. {
  61. return issubtree;
  62. }
  63. short key;
  64. short value;
  65. private:
  66. short parentIndex;
  67. bool issubtree;
  68. };
  69. #pragma pack()
  70. //-----------------------------------------------------------------------------
  71. // Purpose: stringtable is a session global string table.
  72. //-----------------------------------------------------------------------------
  73. class CCompiledKeyValuesWriter
  74. {
  75. public:
  76. CCompiledKeyValuesWriter()
  77. {
  78. m_StringTable.AddString( "" );
  79. }
  80. void AppendKeyValuesFile( char const *filename );
  81. void WriteFile( char const *outfile );
  82. private:
  83. void Describe( const KVFile_t& file );
  84. void BuildKVData_R( KeyValues *kv, int parent );
  85. void WriteStringTable( CUtlBuffer& buf );
  86. void WriteData( CUtlBuffer& buf );
  87. void WriteFiles( CUtlBuffer &buf );
  88. CUtlVector< KVFile_t > m_Files;
  89. CUtlVector< KVInfo_t > m_Data;
  90. CUtlSymbolTable m_StringTable;
  91. };
  92. class CRunTimeKeyValuesStringTable
  93. {
  94. public:
  95. bool ReadStringTable( int numStrings, CUtlBuffer& buf );
  96. inline int Count() const
  97. {
  98. return m_Strings.Count();
  99. }
  100. inline char const *Lookup( short index )
  101. {
  102. return m_Strings[ index ];
  103. }
  104. private:
  105. CUtlVector< const char * > m_Strings;
  106. };
  107. class CCompiledKeyValuesReader
  108. {
  109. public:
  110. CCompiledKeyValuesReader();
  111. bool LoadFile( char const *filename );
  112. KeyValues *Instance( char const *kvfilename );
  113. bool InstanceInPlace( KeyValues& head, char const *kvfilename );
  114. bool LookupKeyValuesRootKeyName( char const *kvfilename, char *outbuf, size_t bufsize );
  115. int First() const;
  116. int Next( int i ) const;
  117. int InvalidIndex() const;
  118. void GetFileName( int index, char *buf, size_t bufsize );
  119. private:
  120. struct FileInfo_t
  121. {
  122. FileInfo_t() :
  123. hFile( 0 ),
  124. nFirstIndex( 0 ),
  125. nCount( 0 )
  126. {
  127. }
  128. FileNameHandle_t hFile;
  129. short nFirstIndex;
  130. short nCount;
  131. static bool Less( const FileInfo_t& lhs, const FileInfo_t& rhs )
  132. {
  133. return lhs.hFile < rhs.hFile;
  134. }
  135. };
  136. KeyValues *CreateFromData( const FileInfo_t& info );
  137. bool CreateInPlaceFromData( KeyValues& head, const FileInfo_t& info );
  138. // Now get the actual files
  139. CUtlRBTree< FileInfo_t, unsigned short > m_Dict;
  140. CUtlVector< KVInfo_t > m_Data;
  141. CRunTimeKeyValuesStringTable m_StringTable;
  142. CUtlBuffer m_LoadBuffer;
  143. };
  144. #endif // KEYVALUESCOMPILER_H