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.

235 lines
7.5 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef PREDICTABLE_ENTITY_H
  8. #define PREDICTABLE_ENTITY_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. // For introspection
  13. #include "tier0/platform.h"
  14. #include "predictioncopy.h"
  15. #include "shared_classnames.h"
  16. #ifndef NO_ENTITY_PREDICTION
  17. #define UsePrediction() 1
  18. #else
  19. #define UsePrediction() 0
  20. #endif
  21. // CLIENT DLL includes
  22. #if defined( CLIENT_DLL ) || defined( TOOL_DLL )
  23. #include "iclassmap.h"
  24. #include "recvproxy.h"
  25. class SendTable;
  26. // Game DLL includes
  27. #else
  28. #include "sendproxy.h"
  29. #endif // !CLIENT_DLL
  30. #if defined( CLIENT_DLL )
  31. #define DECLARE_NETWORKCLASS() \
  32. DECLARE_CLIENTCLASS()
  33. #define DECLARE_NETWORKCLASS_NOBASE() \
  34. DECLARE_CLIENTCLASS_NOBASE()
  35. #else
  36. #define DECLARE_NETWORKCLASS() \
  37. DECLARE_SERVERCLASS()
  38. #define DECLARE_NETWORKCLASS_NOBASE() \
  39. DECLARE_SERVERCLASS_NOBASE()
  40. #endif
  41. #if defined( CLIENT_DLL )
  42. #ifndef NO_ENTITY_PREDICTION
  43. #define DECLARE_PREDICTABLE() \
  44. public: \
  45. static typedescription_t m_PredDesc[]; \
  46. static datamap_t m_PredMap; \
  47. virtual datamap_t *GetPredDescMap( void ); \
  48. template <typename T> friend datamap_t *PredMapInit(T *)
  49. #else
  50. #define DECLARE_PREDICTABLE() template <typename T> friend datamap_t *PredMapInit(T *)
  51. #endif
  52. #ifndef NO_ENTITY_PREDICTION
  53. #define BEGIN_PREDICTION_DATA( className ) \
  54. datamap_t className::m_PredMap = { 0, 0, #className, &BaseClass::m_PredMap }; \
  55. datamap_t *className::GetPredDescMap( void ) { return &m_PredMap; } \
  56. BEGIN_PREDICTION_DATA_GUTS( className )
  57. #define BEGIN_PREDICTION_DATA_NO_BASE( className ) \
  58. datamap_t className::m_PredMap = { 0, 0, #className, NULL }; \
  59. datamap_t *className::GetPredDescMap( void ) { return &m_PredMap; } \
  60. BEGIN_PREDICTION_DATA_GUTS( className )
  61. #define BEGIN_PREDICTION_DATA_GUTS( className ) \
  62. template <typename T> datamap_t *PredMapInit(T *); \
  63. template <> datamap_t *PredMapInit<className>( className * ); \
  64. namespace className##_PredDataDescInit \
  65. { \
  66. datamap_t *g_PredMapHolder = PredMapInit( (className *)NULL ); /* This can/will be used for some clean up duties later */ \
  67. } \
  68. \
  69. template <> datamap_t *PredMapInit<className>( className * ) \
  70. { \
  71. typedef className classNameTypedef; \
  72. static typedescription_t predDesc[] = \
  73. { \
  74. { FIELD_VOID,0,0,0,0,0,0,0,0}, /* so you can define "empty" tables */
  75. #define END_PREDICTION_DATA() \
  76. }; \
  77. \
  78. if ( sizeof( predDesc ) > sizeof( predDesc[0] ) ) \
  79. { \
  80. classNameTypedef::m_PredMap.dataNumFields = ARRAYSIZE( predDesc ) - 1; \
  81. classNameTypedef::m_PredMap.dataDesc = &predDesc[1]; \
  82. } \
  83. else \
  84. { \
  85. classNameTypedef::m_PredMap.dataNumFields = 1; \
  86. classNameTypedef::m_PredMap.dataDesc = predDesc; \
  87. } \
  88. return &classNameTypedef::m_PredMap; \
  89. }
  90. #else
  91. #define BEGIN_PREDICTION_DATA( className ) \
  92. template <> inline datamap_t *PredMapInit<className>( className * ) \
  93. { \
  94. if ( 0 ) \
  95. { \
  96. typedef className classNameTypedef; \
  97. typedescription_t predDesc[] = \
  98. { \
  99. { FIELD_VOID,0,0,0,0,0,0,0,0},
  100. #define BEGIN_PREDICTION_DATA_NO_BASE( className ) BEGIN_PREDICTION_DATA( className )
  101. #define END_PREDICTION_DATA() \
  102. }; \
  103. predDesc[0].flags = 0; /* avoid compiler warning of unused data */ \
  104. } \
  105. }
  106. #endif
  107. #else
  108. // nothing, only client has a prediction system
  109. #define DECLARE_PREDICTABLE()
  110. #define BEGIN_PREDICTION_DATA( className )
  111. #define END_PREDICTION_DATA()
  112. #endif
  113. #if defined( CLIENT_DLL )
  114. // On the client .dll this creates a mapping between a classname and
  115. // a client side class. Probably could be templatized at some point.
  116. #define LINK_ENTITY_TO_CLASS( localName, className ) \
  117. static C_BaseEntity *C##className##Factory( void ) \
  118. { \
  119. return static_cast< C_BaseEntity * >( new className ); \
  120. }; \
  121. class C##localName##Foo \
  122. { \
  123. public: \
  124. C##localName##Foo( void ) \
  125. { \
  126. GetClassMap().Add( #localName, #className, sizeof( className ), \
  127. &C##className##Factory ); \
  128. __g_##className##ClientClass.m_pMapClassname = #localName; \
  129. } \
  130. }; \
  131. static C##localName##Foo g_C##localName##Foo;
  132. #define LINK_ENTITY_TO_CLASS_CLIENTONLY( localName, className ) \
  133. static C_BaseEntity *C##className##Factory( void ) \
  134. { \
  135. return static_cast< C_BaseEntity * >( new className ); \
  136. }; \
  137. class C##localName##Foo \
  138. { \
  139. public: \
  140. C##localName##Foo( void ) \
  141. { \
  142. GetClassMap().Add( #localName, #className, sizeof( className ), \
  143. &C##className##Factory ); \
  144. } \
  145. }; \
  146. static C##localName##Foo g_C##localName##Foo;
  147. #define BEGIN_NETWORK_TABLE( className, tableName ) BEGIN_RECV_TABLE( className, tableName )
  148. #define BEGIN_NETWORK_TABLE_NOBASE( className, tableName ) BEGIN_RECV_TABLE_NOBASE( className, tableName )
  149. #define END_NETWORK_TABLE END_RECV_TABLE
  150. #define LINK_ENTITY_TO_CLASS_ALIASED( localName, className ) LINK_ENTITY_TO_CLASS(localName, C_##className )
  151. #define IMPLEMENT_NETWORKCLASS_ALIASED(className, dataTable) \
  152. IMPLEMENT_CLIENTCLASS( C_##className, dataTable, C##className )
  153. #define IMPLEMENT_NETWORKCLASS(className, dataTable) \
  154. IMPLEMENT_CLIENTCLASS(className, dataTable, className)
  155. #define IMPLEMENT_NETWORKCLASS_DT(className, dataTable) \
  156. IMPLEMENT_CLIENTCLASS_DT(className, dataTable, className)
  157. #define LINK_ENTITY_TO_CLASS_SIMPLE_DERIVED( classNameDerived, classNameBase, dataTableName, entity_name ) \
  158. class C_##classNameDerived : public C_##classNameBase \
  159. { \
  160. public: \
  161. DECLARE_CLASS( C_##classNameDerived, C_##classNameBase ); \
  162. DECLARE_NETWORKCLASS(); \
  163. }; \
  164. IMPLEMENT_NETWORKCLASS_ALIASED( classNameDerived, dataTableName ) \
  165. BEGIN_NETWORK_TABLE( C_##classNameDerived, dataTableName ) \
  166. END_NETWORK_TABLE() \
  167. LINK_ENTITY_TO_CLASS_ALIASED( entity_name, classNameDerived )
  168. #else
  169. #define BEGIN_NETWORK_TABLE( className, tableName ) BEGIN_SEND_TABLE( className, tableName )
  170. #define BEGIN_NETWORK_TABLE_NOBASE( className, tableName ) BEGIN_SEND_TABLE_NOBASE( className, tableName )
  171. #define END_NETWORK_TABLE END_SEND_TABLE
  172. #define LINK_ENTITY_TO_CLASS_ALIASED( localName, className ) LINK_ENTITY_TO_CLASS(localName, C##className )
  173. #define IMPLEMENT_NETWORKCLASS_ALIASED(className, dataTable) \
  174. IMPLEMENT_SERVERCLASS( C##className, dataTable )
  175. #define IMPLEMENT_NETWORKCLASS(className, dataTable) \
  176. IMPLEMENT_SERVERCLASS(className, dataTable)
  177. #define IMPLEMENT_NETWORKCLASS_DT(className, dataTable) \
  178. IMPLEMENT_SERVERCLASS_ST(className, dataTable)
  179. #define LINK_ENTITY_TO_CLASS_SIMPLE_DERIVED( classNameDerived, classNameBase, dataTableName, entity_name ) \
  180. class C##classNameDerived : public C##classNameBase \
  181. { \
  182. public: \
  183. DECLARE_CLASS( C##classNameDerived, C##classNameBase ); \
  184. DECLARE_NETWORKCLASS(); \
  185. }; \
  186. IMPLEMENT_NETWORKCLASS_ALIASED( classNameDerived, dataTableName ) \
  187. BEGIN_NETWORK_TABLE( C##classNameDerived, dataTableName ) \
  188. END_NETWORK_TABLE() \
  189. LINK_ENTITY_TO_CLASS_ALIASED( entity_name, classNameDerived )
  190. #endif
  191. #endif // PREDICTABLE_ENTITY_H