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.

236 lines
7.1 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef NETWORKSTRINGTABLE_H
  8. #define NETWORKSTRINGTABLE_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "networkstringtabledefs.h"
  13. #include "networkstringtableitem.h"
  14. #include "checksum_crc.h"
  15. #include "netmessages.h"
  16. #include "tier1/utldict.h"
  17. #include "tier1/utlbuffer.h"
  18. #include "tier1/bitbuf.h"
  19. class SVC_CreateStringTable;
  20. class CBaseClient;
  21. abstract_class INetworkStringDict
  22. {
  23. public:
  24. virtual ~INetworkStringDict() {}
  25. virtual unsigned int Count() = 0;
  26. virtual void Purge() = 0;
  27. virtual const char *String( int index ) = 0;
  28. virtual bool IsValidIndex( int index ) = 0;
  29. virtual int Insert( const char *pString ) = 0;
  30. virtual int Find( const char *pString ) = 0;
  31. virtual void UpdateDictionary( int index ) = 0;
  32. virtual int DictionaryIndex( int index ) = 0;
  33. virtual CNetworkStringTableItem &Element( int index ) = 0;
  34. virtual const CNetworkStringTableItem &Element( int index ) const = 0;
  35. };
  36. abstract_class INetworkStringTableDictionaryMananger
  37. {
  38. public:
  39. virtual bool OnLevelLoadStart( char const *pchMapName, CRC32_t *pStringTableCRC ) = 0;
  40. // Indicates .bsp file is fully unloaded, safe to overwrite.
  41. virtual void OnBSPFullyUnloaded() = 0;
  42. virtual CRC32_t GetCRC() = 0;
  43. };
  44. extern INetworkStringTableDictionaryMananger *g_pStringTableDictionary;
  45. //-----------------------------------------------------------------------------
  46. // Purpose: Client/Server shared string table definition
  47. //-----------------------------------------------------------------------------
  48. class CNetworkStringTable : public INetworkStringTable
  49. {
  50. enum ConstEnum_t {MIRROR_TABLE_MAX_COUNT = 2};
  51. public:
  52. // Construction
  53. CNetworkStringTable( TABLEID id, const char *tableName, int maxentries, int userdatafixedsize, int userdatanetworkbits, int flags );
  54. virtual ~CNetworkStringTable( void );
  55. public:
  56. // INetworkStringTable interface:
  57. const char *GetTableName( void ) const;
  58. TABLEID GetTableId( void ) const;
  59. int GetNumStrings( void ) const;
  60. int GetMaxStrings( void ) const;
  61. int GetEntryBits( void ) const;
  62. // Networking
  63. void SetTick( int tick );
  64. bool ChangedSinceTick( int tick ) const;
  65. int AddString( bool bIsServer, const char *value, int length = -1, const void *userdata = NULL );
  66. const char *GetString( int stringNumber ) const;
  67. void SetStringUserData( int stringNumber, int length, const void *userdata );
  68. const void *GetStringUserData( int stringNumber, int *length ) const;
  69. int FindStringIndex( char const *string );
  70. void SetStringChangedCallback( void *object, pfnStringChanged changeFunc );
  71. // other public apis
  72. bool IsUserDataFixedSize() const;
  73. int GetUserDataSizeBits() const;
  74. int GetUserDataSize() const;
  75. bool IsUsingDictionary() const;
  76. void UpdateDictionaryString( int stringNumber );
  77. public:
  78. #ifndef SHARED_NET_STRING_TABLES
  79. int WriteUpdate( CBaseClient *client, bf_write &buf, int tick_ack ) const;
  80. void ParseUpdate( bf_read &buf, int entries );
  81. // HLTV change history & rollback
  82. void EnableRollback();
  83. void RestoreTick(int tick);
  84. // local backdoor tables
  85. void SetMirrorTable( uint nIndex, INetworkStringTable *table );
  86. void UpdateMirrorTable( int tick_ack );
  87. void CopyStringTable(CNetworkStringTable * table);
  88. // buffer IO
  89. void WriteStringTable( bf_write& buf );
  90. bool ReadStringTable( bf_read& buf );
  91. bool WriteBaselines( CSVCMsg_CreateStringTable_t &msg );
  92. #endif
  93. void TriggerCallbacks( int tick_ack );
  94. CNetworkStringTableItem *GetItem( int i );
  95. // debug ouptput
  96. virtual void Dump( void );
  97. virtual bool Lock( bool bLock );
  98. void SetAllowClientSideAddString( bool state );
  99. pfnStringChanged GetCallback();
  100. protected:
  101. void DataChanged( int stringNumber, CNetworkStringTableItem *item );
  102. // Destroy string table
  103. void DeleteAllStrings( void );
  104. void CheckDictionary( int stringNumber );
  105. CNetworkStringTable( const CNetworkStringTable & ); // not implemented, not allowed
  106. TABLEID m_id;
  107. char *m_pszTableName;
  108. // Must be a power of 2, so encoding can determine # of bits to use based on log2
  109. int m_nMaxEntries;
  110. int m_nEntryBits;
  111. int m_nTickCount;
  112. int m_nLastChangedTick;
  113. bool m_bChangeHistoryEnabled : 1;
  114. bool m_bLocked : 1;
  115. bool m_bAllowClientSideAddString : 1;
  116. bool m_bUserDataFixedSize : 1;
  117. int m_nFlags; // NSF_*
  118. int m_nUserDataSize;
  119. int m_nUserDataSizeBits;
  120. // Change function callback
  121. pfnStringChanged m_changeFunc;
  122. // Optional context/object
  123. void *m_pObject;
  124. // pointer to local backdoor table
  125. INetworkStringTable *m_pMirrorTable[ MIRROR_TABLE_MAX_COUNT ];
  126. INetworkStringDict *m_pItems;
  127. INetworkStringDict *m_pItemsClientSide; // For m_bAllowClientSideAddString, these items are non-networked and are referenced by a negative string index!!!
  128. };
  129. //-----------------------------------------------------------------------------
  130. // Purpose: Implements game .dll string table interface
  131. //-----------------------------------------------------------------------------
  132. class CNetworkStringTableContainer : public INetworkStringTableContainer
  133. {
  134. public:
  135. // Construction
  136. CNetworkStringTableContainer( void );
  137. ~CNetworkStringTableContainer( void );
  138. public:
  139. // Implement INetworkStringTableContainer
  140. INetworkStringTable *CreateStringTable( const char *tableName, int maxentries, int userdatafixedsize = 0, int userdatanetworkbits = 0, int flags = NSF_NONE );
  141. void RemoveAllTables( void );
  142. // table infos
  143. INetworkStringTable *FindTable( const char *tableName ) const ;
  144. INetworkStringTable *GetTable( TABLEID stringTable ) const;
  145. int GetNumTables( void ) const;
  146. virtual void SetAllowClientSideAddString( INetworkStringTable *table, bool bAllowClientSideAddString );
  147. virtual void CreateDictionary( char const *pchMapName );
  148. void UpdateDictionaryStrings();
  149. public:
  150. // Update a client (called once during packet sending per client)
  151. void SetTick( int tick_count);
  152. #ifndef SHARED_NET_STRING_TABLES
  153. // rollback feature
  154. void EnableRollback( bool bState );
  155. void RestoreTick( int tick );
  156. // Buffer I/O
  157. void WriteStringTables( bf_write& buf );
  158. bool ReadStringTables( bf_read& buf );
  159. void WriteUpdateMessage( CBaseClient *client, int tick_ack, bf_write &buf );
  160. void WriteBaselines( char const *pchMapName, bf_write &buf );
  161. void DirectUpdate( int tick_ack ); // fill mirror table directly with updates
  162. #endif
  163. void TriggerCallbacks( int tick_ack ); // fire callback functions
  164. // Guards so game .dll can't create tables at the wrong time
  165. void AllowCreation( bool state );
  166. // Print table data to console
  167. void Dump( void );
  168. // Sets the lock and returns the previous lock state
  169. bool Lock( bool bLock );
  170. void SetAllowClientSideAddString( bool state );
  171. private:
  172. bool m_bAllowCreation; // creat guard Guard
  173. int m_nTickCount; // current tick
  174. bool m_bLocked; // currently locked?
  175. bool m_bEnableRollback; // enables rollback feature
  176. CUtlVector < CNetworkStringTable* > m_Tables; // the string tables
  177. };
  178. #endif // NETWORKSTRINGTABLE_H