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.

215 lines
7.2 KiB

  1. //========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #ifndef DATATABLE_COMMON_H
  9. #define DATATABLE_COMMON_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "basetypes.h"
  14. #include "tier0/dbg.h"
  15. #include "tier1/strtools.h"
  16. #include <stddef.h>
  17. #ifdef LINUX
  18. #undef offsetof
  19. #define offsetof(s,m) (size_t)&(((s *)0)->m)
  20. #endif
  21. // Max number of properties in a datatable and its children.
  22. #define MAX_DATATABLES 1024 // must be a power of 2.
  23. #define MAX_DATATABLE_PROPS 4096
  24. #define MAX_ARRAY_ELEMENTS 2048 // a network array should have more that 1024 elements
  25. #define HIGH_DEFAULT -121121.121121f
  26. #define BITS_FULLRES -1 // Use the full resolution of the type being encoded.
  27. #define BITS_WORLDCOORD -2 // Encode as a world coordinate.
  28. #define DT_MAX_STRING_BITS 9
  29. #define DT_MAX_STRING_BUFFERSIZE (1<<DT_MAX_STRING_BITS) // Maximum length of a string that can be sent.
  30. #define STRINGBUFSIZE(className, varName) sizeof( ((className*)0)->varName )
  31. // Gets the size of a variable in a class.
  32. #define PROPSIZEOF(className, varName) sizeof(((className*)0)->varName)
  33. // SendProp::m_Flags.
  34. #define SPROP_UNSIGNED (1<<0) // Unsigned integer data.
  35. #define SPROP_COORD (1<<1) // If this is set, the float/vector is treated like a world coordinate.
  36. // Note that the bit count is ignored in this case.
  37. #define SPROP_NOSCALE (1<<2) // For floating point, don't scale into range, just take value as is.
  38. #define SPROP_ROUNDDOWN (1<<3) // For floating point, limit high value to range minus one bit unit
  39. #define SPROP_ROUNDUP (1<<4) // For floating point, limit low value to range minus one bit unit
  40. #define SPROP_NORMAL (1<<5) // If this is set, the vector is treated like a normal (only valid for vectors)
  41. #define SPROP_EXCLUDE (1<<6) // This is an exclude prop (not excludED, but it points at another prop to be excluded).
  42. #define SPROP_XYZE (1<<7) // Use XYZ/Exponent encoding for vectors.
  43. #define SPROP_INSIDEARRAY (1<<8) // This tells us that the property is inside an array, so it shouldn't be put into the
  44. // flattened property list. Its array will point at it when it needs to.
  45. #define SPROP_PROXY_ALWAYS_YES (1<<9) // Set for datatable props using one of the default datatable proxies like
  46. // SendProxy_DataTableToDataTable that always send the data to all clients.
  47. #define SPROP_IS_A_VECTOR_ELEM (1<<10) // Set automatically if SPROP_VECTORELEM is used.
  48. #define SPROP_COLLAPSIBLE (1<<11) // Set automatically if it's a datatable with an offset of 0 that doesn't change the pointer
  49. // (ie: for all automatically-chained base classes).
  50. // In this case, it can get rid of this SendPropDataTable altogether and spare the
  51. // trouble of walking the hierarchy more than necessary.
  52. #define SPROP_COORD_MP (1<<12) // Like SPROP_COORD, but special handling for multiplayer games
  53. #define SPROP_COORD_MP_LOWPRECISION (1<<13) // Like SPROP_COORD, but special handling for multiplayer games where the fractional component only gets a 3 bits instead of 5
  54. #define SPROP_COORD_MP_INTEGRAL (1<<14) // SPROP_COORD_MP, but coordinates are rounded to integral boundaries
  55. #define SPROP_CELL_COORD (1<<15) // Like SPROP_COORD, but special encoding for cell coordinates that can't be negative, bit count indicate maximum value
  56. #define SPROP_CELL_COORD_LOWPRECISION (1<<16) // Like SPROP_CELL_COORD, but special handling where the fractional component only gets a 3 bits instead of 5
  57. #define SPROP_CELL_COORD_INTEGRAL (1<<17) // SPROP_CELL_COORD, but coordinates are rounded to integral boundaries
  58. #define SPROP_CHANGES_OFTEN (1<<18) // this is an often changed field, moved to head of sendtable so it gets a small index
  59. #define SPROP_VARINT (1<<19) // use var int encoded (google protobuf style), note you want to include SPROP_UNSIGNED if needed, its more efficient
  60. #define SPROP_NUMFLAGBITS_NETWORKED 20
  61. // This is server side only, it's used to mark properties whose SendProxy_* functions encode against gpGlobals->tickcount (the only ones that currently do this are
  62. // m_flAnimTime and m_flSimulationTime. MODs shouldn't need to mess with this probably
  63. #define SPROP_ENCODED_AGAINST_TICKCOUNT (1<<20)
  64. // See SPROP_NUMFLAGBITS_NETWORKED for the ones which are networked
  65. #define SPROP_NUMFLAGBITS 21
  66. // Used by the SendProp and RecvProp functions to disable debug checks on type sizes.
  67. #define SIZEOF_IGNORE -1
  68. // Use this to extern send and receive datatables, and reference them.
  69. #define EXTERN_SEND_TABLE(tableName) namespace tableName {extern SendTable g_SendTable;}
  70. #define EXTERN_RECV_TABLE(tableName) namespace tableName {extern RecvTable g_RecvTable;}
  71. #define REFERENCE_SEND_TABLE(tableName) tableName::g_SendTable
  72. #define REFERENCE_RECV_TABLE(tableName) tableName::g_RecvTable
  73. class SendProp;
  74. typedef enum
  75. {
  76. DPT_Int=0,
  77. DPT_Float,
  78. DPT_Vector,
  79. DPT_VectorXY, // Only encodes the XY of a vector, ignores Z
  80. DPT_String,
  81. DPT_Array, // An array of the base types (can't be of datatables).
  82. DPT_DataTable,
  83. #if 0 // We can't ship this since it changes the size of DTVariant to be 20 bytes instead of 16 and that breaks MODs!!!
  84. DPT_Quaternion,
  85. #endif
  86. DPT_Int64,
  87. DPT_NUMSendPropTypes
  88. } SendPropType;
  89. class DVariant
  90. {
  91. public:
  92. DVariant() {m_Type = DPT_Float;}
  93. explicit DVariant(float val) {m_Type = DPT_Float; m_Float = val;}
  94. const char *ToString()
  95. {
  96. static char text[128];
  97. switch ( m_Type )
  98. {
  99. case DPT_Int :
  100. Q_snprintf( text, sizeof(text), "%i", (int)m_Int );
  101. break;
  102. case DPT_Float :
  103. Q_snprintf( text, sizeof(text), "%.3f", m_Float );
  104. break;
  105. case DPT_Vector :
  106. Q_snprintf( text, sizeof(text), "(%.3f,%.3f,%.3f)",
  107. m_Vector[0], m_Vector[1], m_Vector[2] );
  108. break;
  109. case DPT_VectorXY :
  110. Q_snprintf( text, sizeof(text), "(%.3f,%.3f)",
  111. m_Vector[0], m_Vector[1] );
  112. break;
  113. #if 0 // We can't ship this since it changes the size of DTVariant to be 20 bytes instead of 16 and that breaks MODs!!!
  114. case DPT_Quaternion :
  115. Q_snprintf( text, sizeof(text), "(%.3f,%.3f,%.3f %.3f)",
  116. m_Vector[0], m_Vector[1], m_Vector[2], m_Vector[3] );
  117. break;
  118. #endif
  119. case DPT_String :
  120. if ( m_pString )
  121. return m_pString;
  122. else
  123. return "NULL";
  124. break;
  125. case DPT_Array :
  126. Q_snprintf( text, sizeof(text), "Array" );
  127. break;
  128. case DPT_DataTable :
  129. Q_snprintf( text, sizeof(text), "DataTable" );
  130. break;
  131. case DPT_Int64:
  132. Q_snprintf( text, sizeof(text), "%lld", m_Int64 );
  133. break;
  134. default :
  135. Q_snprintf( text, sizeof(text), "DVariant type %i unknown", m_Type );
  136. break;
  137. }
  138. return text;
  139. }
  140. union
  141. {
  142. float m_Float;
  143. long m_Int;
  144. char *m_pString;
  145. void *m_pData; // For DataTables.
  146. #if 0 // We can't ship this since it changes the size of DTVariant to be 20 bytes instead of 16 and that breaks MODs!!!
  147. float m_Vector[4];
  148. #else
  149. float m_Vector[3];
  150. #endif
  151. int64 m_Int64;
  152. };
  153. SendPropType m_Type;
  154. };
  155. // This can be used to set the # of bits used to transmit a number between 0 and nMaxElements-1.
  156. inline int NumBitsForCount( int nMaxElements )
  157. {
  158. int nBits = 0;
  159. while ( nMaxElements > 0 )
  160. {
  161. ++nBits;
  162. nMaxElements >>= 1;
  163. }
  164. return nBits;
  165. }
  166. #endif // DATATABLE_COMMON_H