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.

220 lines
8.0 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "dt_utlvector_send.h"
  9. #include "tier0/memdbgon.h"
  10. extern char *s_ElementNames[MAX_ARRAY_ELEMENTS];
  11. // This gets associated with SendProps inside a utlvector and stores extra data needed to make it work.
  12. class CSendPropExtra_UtlVector
  13. {
  14. public:
  15. CSendPropExtra_UtlVector() :
  16. m_DataTableProxyFn( NULL ),
  17. m_ProxyFn( NULL ),
  18. m_EnsureCapacityFn( NULL ),
  19. m_ElementStride( 0 ),
  20. m_Offset( 0 ),
  21. m_nMaxElements( 0 )
  22. {
  23. }
  24. SendTableProxyFn m_DataTableProxyFn; // If it's a datatable, then this is the proxy they specified.
  25. SendVarProxyFn m_ProxyFn; // If it's a non-datatable, then this is the proxy they specified.
  26. EnsureCapacityFn m_EnsureCapacityFn;
  27. int m_ElementStride; // Distance between each element in the array.
  28. int m_Offset; // # bytes from the parent structure to its utlvector.
  29. int m_nMaxElements; // For debugging...
  30. };
  31. void SendProxy_UtlVectorElement(
  32. const SendProp *pProp,
  33. const void *pStruct,
  34. const void *pData,
  35. DVariant *pOut,
  36. int iElement,
  37. int objectID )
  38. {
  39. CSendPropExtra_UtlVector *pExtra = (CSendPropExtra_UtlVector*)pProp->GetExtraData();
  40. Assert( pExtra );
  41. // Kind of lame overloading element stride to hold the element index,
  42. // but we can easily move it into its SetExtraData stuff if we need to.
  43. iElement = pProp->GetElementStride();
  44. // NOTE: this is cheesy, but it does the trick.
  45. CUtlVector<int> *pUtlVec = (CUtlVector<int>*)((char*)pStruct + pExtra->m_Offset);
  46. if ( iElement >= pUtlVec->Count() )
  47. {
  48. // Pass in zero value.
  49. memset( pOut, 0, sizeof( *pOut ) );
  50. }
  51. else
  52. {
  53. // Call through to the proxy they passed in, making pStruct=the CUtlVector and forcing iElement to 0.
  54. pExtra->m_ProxyFn( pProp, pData, (char*)pUtlVec->Base() + iElement*pExtra->m_ElementStride, pOut, 0, objectID );
  55. }
  56. }
  57. void* SendProxy_UtlVectorElement_DataTable(
  58. const SendProp *pProp,
  59. const void *pStructBase,
  60. const void *pData,
  61. CSendProxyRecipients *pRecipients,
  62. int objectID )
  63. {
  64. CSendPropExtra_UtlVector *pExtra = (CSendPropExtra_UtlVector*)pProp->GetExtraData();
  65. int iElement = pProp->m_ElementStride;
  66. Assert( iElement < pExtra->m_nMaxElements );
  67. // This should have gotten called in SendProxy_LengthTable before we get here, so
  68. // the capacity should be correct.
  69. #ifdef _DEBUG
  70. pExtra->m_EnsureCapacityFn( (void*)pStructBase, pExtra->m_Offset, pExtra->m_nMaxElements );
  71. #endif
  72. // NOTE: this is cheesy because we're assuming the type of the template class, but it does the trick.
  73. CUtlVector<int> *pUtlVec = (CUtlVector<int>*)((char*)pStructBase + pExtra->m_Offset);
  74. // Call through to the proxy they passed in, making pStruct=the CUtlVector and forcing iElement to 0.
  75. return pExtra->m_DataTableProxyFn( pProp, pData, (char*)pUtlVec->Base() + iElement*pExtra->m_ElementStride, pRecipients, objectID );
  76. }
  77. void SendProxy_UtlVectorLength(
  78. const SendProp *pProp,
  79. const void *pStruct,
  80. const void *pData,
  81. DVariant *pOut,
  82. int iElement,
  83. int objectID )
  84. {
  85. CSendPropExtra_UtlVector *pExtra = (CSendPropExtra_UtlVector*)pProp->GetExtraData();
  86. // NOTE: this is cheesy because we're assuming the type of the template class, but it does the trick.
  87. CUtlVector<int> *pUtlVec = (CUtlVector<int>*)((char*)pStruct + pExtra->m_Offset);
  88. // Don't let them overflow the buffer because they might expect that to get transmitted to the client.
  89. pOut->m_Int = pUtlVec->Count();
  90. if ( pOut->m_Int > pExtra->m_nMaxElements )
  91. {
  92. Assert( false );
  93. pOut->m_Int = pExtra->m_nMaxElements;
  94. }
  95. }
  96. void* SendProxy_LengthTable( const SendProp *pProp, const void *pStructBase, const void *pData, CSendProxyRecipients *pRecipients, int objectID )
  97. {
  98. // Make sure the array has space to hold all the elements.
  99. CSendPropExtra_UtlVector *pExtra = (CSendPropExtra_UtlVector*)pProp->GetExtraData();
  100. pExtra->m_EnsureCapacityFn( (void*)pStructBase, pExtra->m_Offset, pExtra->m_nMaxElements );
  101. return (void*)pData;
  102. }
  103. // Note: your pArrayProp will NOT get iElement set to anything other than 0, because this function installs its
  104. // own proxy in front of yours. pStruct will point at the CUtlVector and pData will point at the element in the CUtlVector.It will pass you the direct pointer to the element inside the CUtlVector.
  105. //
  106. // You can skip the first 3 parameters in pArrayProp because they're ignored. So your array specification
  107. // could look like this:
  108. // SendPropUtlVector(
  109. // SENDINFO_UTLVECTOR( m_FloatArray ),
  110. // SendPropFloat( NULL, 0, 0, 0 [# bits], SPROP_NOSCALE [flags] ) );
  111. //
  112. // Note: you have to be DILIGENT about calling NetworkStateChanged whenever an element in your CUtlVector changes
  113. // since CUtlVector doesn't do this automatically.
  114. SendProp SendPropUtlVector(
  115. char *pVarName, // Use SENDINFO_UTLVECTOR to generate these 4.
  116. int offset, // Used to generate pData in the function specified in varProxy.
  117. int sizeofVar, // The size of each element in the utlvector.
  118. EnsureCapacityFn ensureFn, // This is the value returned for elements out of the array's current range.
  119. int nMaxElements, // Max # of elements in the array. Keep this as low as possible.
  120. SendProp pArrayProp, // Describe the data inside of each element in the array.
  121. SendTableProxyFn varProxy // This can be overridden to control who the array is sent to.
  122. )
  123. {
  124. SendProp ret;
  125. Assert( nMaxElements <= MAX_ARRAY_ELEMENTS );
  126. ret.m_Type = DPT_DataTable;
  127. ret.m_pVarName = pVarName;
  128. ret.SetOffset( 0 );
  129. ret.SetDataTableProxyFn( varProxy );
  130. // Handle special proxy types where they always let all clients get the results.
  131. if ( varProxy == SendProxy_DataTableToDataTable || varProxy == SendProxy_DataTablePtrToDataTable )
  132. {
  133. ret.SetFlags( SPROP_PROXY_ALWAYS_YES );
  134. }
  135. // Extra data bound to each of the properties.
  136. CSendPropExtra_UtlVector *pExtraData = new CSendPropExtra_UtlVector;
  137. pExtraData->m_nMaxElements = nMaxElements;
  138. pExtraData->m_ElementStride = sizeofVar;
  139. pExtraData->m_EnsureCapacityFn = ensureFn;
  140. pExtraData->m_Offset = offset;
  141. if ( pArrayProp.m_Type == DPT_DataTable )
  142. pExtraData->m_DataTableProxyFn = pArrayProp.GetDataTableProxyFn();
  143. else
  144. pExtraData->m_ProxyFn = pArrayProp.GetProxyFn();
  145. SendProp *pProps = new SendProp[nMaxElements+1]; // TODO free that again
  146. // The first property is datatable with an int that tells the length of the array.
  147. // It has to go in a datatable, otherwise if this array holds datatable properties, it will be received last.
  148. SendProp *pLengthProp = new SendProp;
  149. *pLengthProp = SendPropInt( AllocateStringHelper( "lengthprop%d", nMaxElements ), 0, 0, NumBitsForCount( nMaxElements ), SPROP_UNSIGNED, SendProxy_UtlVectorLength );
  150. pLengthProp->SetExtraData( pExtraData );
  151. char *pLengthProxyTableName = AllocateUniqueDataTableName( true, "_LPT_%s_%d", pVarName, nMaxElements );
  152. SendTable *pLengthTable = new SendTable( pLengthProp, 1, pLengthProxyTableName );
  153. pProps[0] = SendPropDataTable( "lengthproxy", 0, pLengthTable, SendProxy_LengthTable );
  154. pProps[0].SetExtraData( pExtraData );
  155. // The first element is a sub-datatable.
  156. for ( int i = 1; i < nMaxElements+1; i++ )
  157. {
  158. pProps[i] = pArrayProp; // copy array element property setting
  159. pProps[i].SetOffset( 0 ); // leave offset at 0 so pStructBase is always a pointer to the CUtlVector
  160. pProps[i].m_pVarName = s_ElementNames[i-1]; // give unique name
  161. pProps[i].SetExtraData( pExtraData );
  162. pProps[i].m_ElementStride = i-1; // Kind of lame overloading element stride to hold the element index,
  163. // but we can easily move it into its SetExtraData stuff if we need to.
  164. // We provide our own proxy here.
  165. if ( pArrayProp.m_Type == DPT_DataTable )
  166. {
  167. pProps[i].SetDataTableProxyFn( SendProxy_UtlVectorElement_DataTable );
  168. pProps[i].SetFlags( SPROP_PROXY_ALWAYS_YES );
  169. }
  170. else
  171. {
  172. pProps[i].SetProxyFn( SendProxy_UtlVectorElement );
  173. }
  174. }
  175. SendTable *pTable = new SendTable(
  176. pProps,
  177. nMaxElements+1,
  178. AllocateUniqueDataTableName( true, "_ST_%s_%d", pVarName, nMaxElements )
  179. );
  180. ret.SetDataTable( pTable );
  181. return ret;
  182. }