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.

232 lines
4.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "quakedef.h"
  8. #include "networkstringtableitem.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. //-----------------------------------------------------------------------------
  12. // Purpose:
  13. //-----------------------------------------------------------------------------
  14. CNetworkStringTableItem::CNetworkStringTableItem( void )
  15. {
  16. m_pUserData = NULL;
  17. m_nUserDataLength = 0;
  18. m_nTickChanged = 0;
  19. #ifndef SHARED_NET_STRING_TABLES
  20. m_nTickCreated = 0;
  21. m_pChangeList = NULL;
  22. #endif
  23. }
  24. //-----------------------------------------------------------------------------
  25. // Purpose:
  26. //-----------------------------------------------------------------------------
  27. CNetworkStringTableItem::~CNetworkStringTableItem( void )
  28. {
  29. #ifndef SHARED_NET_STRING_TABLES
  30. if ( m_pChangeList )
  31. {
  32. // free changelist and elements
  33. for ( int i=0; i < m_pChangeList->Count(); i++ )
  34. {
  35. itemchange_s item = m_pChangeList->Element( i );
  36. if ( item.data )
  37. delete[] item.data;
  38. }
  39. delete m_pChangeList; // destructor calls Purge()
  40. m_pUserData = NULL;
  41. }
  42. #endif
  43. if ( m_pUserData )
  44. {
  45. delete[] m_pUserData;
  46. }
  47. }
  48. #ifndef SHARED_NET_STRING_TABLES
  49. void CNetworkStringTableItem::EnableChangeHistory( void )
  50. {
  51. if ( m_pChangeList )
  52. return; // already enabled
  53. m_pChangeList = new CUtlVector<itemchange_s>;
  54. Assert ( m_pChangeList );
  55. }
  56. void CNetworkStringTableItem::UpdateChangeList( int tick, int length, const void *userData )
  57. {
  58. int count = m_pChangeList->Count();
  59. itemchange_s item;
  60. if ( count > 0 )
  61. {
  62. // check if different from last change in list
  63. item = m_pChangeList->Element( count-1 );
  64. if ( !item.data && !userData )
  65. return; // both NULL data
  66. if ( item.length == length )
  67. {
  68. if ( item.data && userData )
  69. {
  70. if ( Q_memcmp( (void*)userData, (void*)item.data, length ) == 0 )
  71. {
  72. return; // no data or size change
  73. }
  74. }
  75. }
  76. if ( item.tick == tick )
  77. {
  78. // two changes within same tick frame, remove last change from list
  79. if ( item.data )
  80. {
  81. delete[] item.data;
  82. }
  83. m_pChangeList->Remove( count-1 );
  84. }
  85. }
  86. item.tick = tick;
  87. // add new user data and time stamp
  88. if ( userData && length )
  89. {
  90. item.data = new unsigned char[length];
  91. item.length = length;
  92. Q_memcpy( item.data, userData, length );
  93. }
  94. else
  95. {
  96. item.data = NULL;
  97. item.length = 0;
  98. }
  99. m_pChangeList->AddToTail( item );
  100. }
  101. int CNetworkStringTableItem::RestoreTick( int tick )
  102. {
  103. Assert( m_pChangeList->Count()>0 );
  104. int index = 1;
  105. itemchange_s *item = &m_pChangeList->Element( 0 );
  106. while ( index < m_pChangeList->Count() )
  107. {
  108. itemchange_s *nextitem = &m_pChangeList->Element( index );
  109. if ( nextitem->tick > tick )
  110. {
  111. break;
  112. }
  113. item = nextitem;
  114. index++;
  115. }
  116. if ( item->tick > tick )
  117. {
  118. // this string was created after tick, so ignore it right now
  119. m_pUserData = NULL;
  120. m_nUserDataLength = 0;
  121. m_nTickChanged = 0;
  122. }
  123. else
  124. {
  125. // restore user data for this string
  126. m_pUserData = item->data;
  127. m_nUserDataLength = item->length;
  128. m_nTickChanged = item->tick;
  129. }
  130. return m_nTickChanged;
  131. }
  132. #endif
  133. //-----------------------------------------------------------------------------
  134. // Purpose:
  135. // Input : *string -
  136. //-----------------------------------------------------------------------------
  137. bool CNetworkStringTableItem::SetUserData( int tick, int length, const void *userData )
  138. {
  139. #ifndef SHARED_NET_STRING_TABLES
  140. if ( m_pChangeList )
  141. {
  142. UpdateChangeList( tick, length, userData );
  143. return false;
  144. }
  145. Assert ( m_nTickCreated > 0 && m_nTickCreated <= tick );
  146. #endif
  147. Assert ( m_nTickChanged > 0 && m_nTickChanged <= tick );
  148. Assert ( length < CNetworkStringTableItem::MAX_USERDATA_SIZE );
  149. // no old or new data
  150. if ( !userData && !m_pUserData )
  151. return false;
  152. if ( m_pUserData &&
  153. length == m_nUserDataLength &&
  154. !Q_memcmp( m_pUserData, (void*)userData, length ) )
  155. {
  156. return false; // old & new data are equal
  157. }
  158. if ( m_pUserData )
  159. delete[] m_pUserData;
  160. m_nUserDataLength = length;
  161. if ( length > 0 )
  162. {
  163. m_pUserData = new unsigned char[ length ];
  164. Q_memcpy( m_pUserData, userData, length );
  165. }
  166. else
  167. {
  168. m_pUserData = NULL;
  169. }
  170. m_nTickChanged = tick;
  171. return true;
  172. }
  173. //-----------------------------------------------------------------------------
  174. // Purpose:
  175. // Input : stringNumber -
  176. // Output : const void
  177. //-----------------------------------------------------------------------------
  178. const void *CNetworkStringTableItem::GetUserData( int *length )
  179. {
  180. if ( length )
  181. *length = m_nUserDataLength;
  182. return ( const void * )m_pUserData;
  183. }