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.

165 lines
4.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef DT_RECV_DECODER_H
  7. #define DT_RECV_DECODER_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "dt.h"
  12. class CDTIRecvTable;
  13. class RecvProp;
  14. // ------------------------------------------------------------------------------------ //
  15. // CClientSendTable and CClientSendProp. This is the data that we receive a SendTable into.
  16. //
  17. // For the most part, it's just a SendTable, but we have slots for extra
  18. // data we need to store.
  19. // ------------------------------------------------------------------------------------ //
  20. class CClientSendProp
  21. {
  22. public:
  23. CClientSendProp();
  24. ~CClientSendProp();
  25. const char* GetTableName() { return m_pTableName; }
  26. void SetTableName( char *pName ) { m_pTableName = pName; }
  27. private:
  28. char *m_pTableName; // For DPT_DataTable properties.. this tells the table name.
  29. };
  30. //
  31. // CClientSendTables are the client's version of the SendTables that are sent down
  32. // from the server. It stores these, then builds CRecvDecoders that allow it to
  33. // decode packets of data.
  34. //
  35. class CClientSendTable
  36. {
  37. public:
  38. CClientSendTable();
  39. ~CClientSendTable();
  40. int GetNumProps() const { return m_SendTable.m_nProps; }
  41. CClientSendProp* GetClientProp( int i ) { return &m_Props[i]; }
  42. const char* GetName() { return m_SendTable.GetName(); }
  43. SendTable* GetSendTable() { return &m_SendTable; }
  44. public:
  45. SendTable m_SendTable;
  46. CUtlVector<CClientSendProp> m_Props; // Extra data for the properties.
  47. };
  48. // ------------------------------------------------------------------------------------ //
  49. // CRecvDecoder.
  50. // ------------------------------------------------------------------------------------ //
  51. class CRecvDecoder
  52. {
  53. public:
  54. CRecvDecoder();
  55. const char* GetName() const;
  56. SendTable* GetSendTable() const;
  57. RecvTable* GetRecvTable() const;
  58. int GetNumProps() const;
  59. const RecvProp* GetProp( int i ) const;
  60. const SendProp* GetSendProp( int i ) const;
  61. int GetNumDatatableProps() const;
  62. const RecvProp* GetDatatableProp( int i ) const;
  63. public:
  64. RecvTable *m_pTable;
  65. CClientSendTable *m_pClientSendTable;
  66. // This is from the data that we've received from the server.
  67. CSendTablePrecalc m_Precalc;
  68. // This mirrors m_Precalc.m_Props.
  69. CUtlVector<const RecvProp*> m_Props;
  70. CUtlVector<const RecvProp*> m_DatatableProps;
  71. CDTIRecvTable *m_pDTITable;
  72. };
  73. // ------------------------------------------------------------------------------------ //
  74. // Inlines.
  75. // ------------------------------------------------------------------------------------ //
  76. inline const char* CRecvDecoder::GetName() const
  77. {
  78. return m_pTable->GetName();
  79. }
  80. inline SendTable* CRecvDecoder::GetSendTable() const
  81. {
  82. return m_Precalc.GetSendTable();
  83. }
  84. inline RecvTable* CRecvDecoder::GetRecvTable() const
  85. {
  86. return m_pTable;
  87. }
  88. inline int CRecvDecoder::GetNumProps() const
  89. {
  90. return m_Props.Count();
  91. }
  92. inline const RecvProp* CRecvDecoder::GetProp( int i ) const
  93. {
  94. // When called from RecvTable_Decode it is expected that this will
  95. // return NULL if 'i' is out of range, but for two years there has been
  96. // nothing to ensure that this is true. This has caused significant
  97. // crashes in RecvTable_Decode. Putting the check here rather
  98. // than in RecvTable_Decode helps check for other functions that might
  99. // have this same assumption. If the cost is too great then this check
  100. // can be moved to RecvTable_Decode. Initial testing suggests that this
  101. // function is called ~1,200 times per second, so the cost of the branch is
  102. // not significant.
  103. // Do the check using unsigned math to check for < 0 simultaneously.
  104. if ( (unsigned)i < (unsigned)GetNumProps() )
  105. return m_Props[i];
  106. return NULL;
  107. }
  108. inline const SendProp* CRecvDecoder::GetSendProp( int i ) const
  109. {
  110. return m_Precalc.GetProp( i );
  111. }
  112. inline int CRecvDecoder::GetNumDatatableProps() const
  113. {
  114. return m_DatatableProps.Count();
  115. }
  116. inline const RecvProp* CRecvDecoder::GetDatatableProp( int i ) const
  117. {
  118. return m_DatatableProps[i];
  119. }
  120. #endif // DT_RECV_DECODER_H