Team Fortress 2 Source Code as on 22/4/2020
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.

212 lines
6.5 KiB

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