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.

255 lines
6.1 KiB

  1. //========= Copyright � 1996-2008, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #if !defined( NO_ENTITY_PREDICTION )
  10. #include "igamesystem.h"
  11. #ifndef _PS3
  12. #ifdef WIN32
  13. #include <typeinfo.h>
  14. #else
  15. #include <typeinfo>
  16. #endif
  17. #endif
  18. #include "cdll_int.h"
  19. #endif
  20. #ifndef _PS3
  21. #include <memory.h>
  22. #endif
  23. #include <stdarg.h>
  24. #include "tier0/dbg.h"
  25. #include "tier1/strtools.h"
  26. #include "predictioncopy.h"
  27. #include "engine/ivmodelinfo.h"
  28. #include "tier1/fmtstr.h"
  29. #include "utlvector.h"
  30. // memdbgon must be the last include file in a .cpp file!!!
  31. #include "tier0/memdbgon.h"
  32. #if defined( COPY_CHECK_STRESSTEST )
  33. class CPredictionCopyTester : public CBaseGameSystem
  34. {
  35. public:
  36. virtual char const *Name() { return "CPredictionCopyTester"; }
  37. // Init, shutdown
  38. virtual bool Init()
  39. {
  40. RunTests();
  41. Remove( this );
  42. return true;
  43. }
  44. virtual void Shutdown() {}
  45. // Level init, shutdown
  46. virtual void LevelInit() {}
  47. // The level is shutdown in two parts
  48. virtual void LevelShutdownPreEntity() {}
  49. // Entities are deleted / released here...
  50. virtual void LevelShutdownPostEntity() {}
  51. // end of level shutdown
  52. // Called before rendering
  53. virtual void PreRender ( ) {}
  54. // Called after rendering
  55. virtual void PostRender() {}
  56. // Gets called each frame
  57. virtual void Update( float frametime ) {}
  58. private:
  59. void RunTests( void );
  60. };
  61. IGameSystem* GetPredictionCopyTester( void )
  62. {
  63. static CPredictionCopyTester s_PredictionCopyTesterSystem;
  64. return &s_PredictionCopyTesterSystem;
  65. }
  66. class CCopyTesterData
  67. {
  68. public:
  69. CCopyTesterData()
  70. {
  71. m_CharValue = 'a';
  72. m_ShortValue = (short)100;
  73. m_IntValue = (int)100;
  74. m_FloatValue = 1.0f;
  75. Q_strncpy( m_szValue, "primarydata", sizeof( m_szValue ) );
  76. m_Vector = Vector( 100, 100, 100 );
  77. AngleQuaternion( QAngle( 0, 45, 0 ), m_Quaternion );
  78. m_Bool = false;
  79. m_Clr.r = m_Clr.g = m_Clr.b = m_Clr.a = 255;
  80. m_Ptr = (void *)0xfedcba98;
  81. // m_hEHandle = NULL;
  82. }
  83. void MakeDifferent( void )
  84. {
  85. m_CharValue = 'd';
  86. m_ShortValue = (short)400;
  87. m_IntValue = (int)400;
  88. m_FloatValue = 4.0f;
  89. Q_strncpy( m_szValue, "secondarydata", sizeof( m_szValue ) );
  90. m_Vector = Vector( 400, 400, 400 );
  91. AngleQuaternion( QAngle( 60, 0, 0 ), m_Quaternion );
  92. m_Bool = true;
  93. m_Clr.r = m_Clr.g = m_Clr.b = m_Clr.a = 1;
  94. m_Ptr = (void *)0x00000001;
  95. // m_hEHandle = (C_BaseEntity *)0x00000001;
  96. }
  97. DECLARE_PREDICTABLE();
  98. int m_IntValue;
  99. float m_FloatValue;
  100. Vector m_Vector;
  101. char m_CharValue;
  102. Quaternion m_Quaternion;
  103. color32 m_Clr;
  104. bool m_Bool;
  105. void *m_Ptr;
  106. short m_ShortValue;
  107. char m_szValue[ 128 ];
  108. // EHANDLE m_hEHandle;
  109. };
  110. BEGIN_PREDICTION_DATA_NO_BASE( CCopyTesterData )
  111. DEFINE_FIELD( m_CharValue, FIELD_CHARACTER ),
  112. DEFINE_FIELD( m_ShortValue, FIELD_SHORT ),
  113. DEFINE_FIELD( m_IntValue, FIELD_INTEGER ),
  114. DEFINE_FIELD( m_FloatValue, FIELD_FLOAT ),
  115. DEFINE_FIELD( m_szValue, FIELD_STRING ),
  116. DEFINE_FIELD( m_Vector, FIELD_VECTOR ),
  117. DEFINE_FIELD( m_Quaternion, FIELD_QUATERNION ),
  118. DEFINE_FIELD( m_Bool, FIELD_BOOLEAN ),
  119. DEFINE_FIELD( m_Clr, FIELD_COLOR32 ),
  120. // DEFINE_FIELD( m_hEHandle, FIELD_EHANDLE ),
  121. END_PREDICTION_DATA()
  122. class CCopyTesterData2 : public C_BaseEntity
  123. {
  124. DECLARE_CLASS( CCopyTesterData2, C_BaseEntity );
  125. public:
  126. CCopyTesterData2()
  127. {
  128. m_CharValueA = 'b';
  129. m_ShortValueA = (short)200;
  130. m_IntValueA = (int)200;
  131. m_FloatValueA = 2.0f;
  132. }
  133. void MakeDifferent( void )
  134. {
  135. m_CharValueA = 'e';
  136. m_ShortValueA = (short)500;
  137. m_IntValueA = (int)500;
  138. m_FloatValueA = 5.0f;
  139. m_FooData.MakeDifferent();
  140. }
  141. DECLARE_PREDICTABLE();
  142. char m_CharValueA;
  143. CCopyTesterData m_FooData;
  144. int m_IntValueA;
  145. short m_ShortValueA;
  146. float m_FloatValueA;
  147. };
  148. BEGIN_PREDICTION_DATA_NO_BASE( CCopyTesterData2 )
  149. DEFINE_FIELD( m_CharValueA, FIELD_CHARACTER ),
  150. DEFINE_FIELD( m_ShortValueA, FIELD_SHORT ),
  151. DEFINE_PRED_TYPEDESCRIPTION( m_FooData, CCopyTesterData ),
  152. DEFINE_FIELD( m_IntValueA, FIELD_INTEGER ),
  153. DEFINE_FIELD( m_FloatValueA, FIELD_FLOAT ),
  154. END_PREDICTION_DATA()
  155. void CPredictionCopyTester::RunTests( void )
  156. {
  157. CCopyTesterData2 *foo1, *foo2, *foo3;
  158. foo1 = new CCopyTesterData2;
  159. foo2 = new CCopyTesterData2;
  160. foo3 = new CCopyTesterData2;
  161. foo2->MakeDifferent();
  162. CPredictionCopy::PrepareDataMap( foo1->GetPredDescMap() );
  163. CPredictionCopy::PrepareDataMap( foo2->GetPredDescMap() );
  164. CPredictionCopy::PrepareDataMap( foo2->GetPredDescMap() );
  165. // foo1 == foo3
  166. // foo1 != foo2
  167. {
  168. Msg( "Comparing and copying == objects, should have zero diffcount\n" );
  169. // Compare foo1 and foo3, should be equal
  170. CPredictionCopy tester( PC_NON_NETWORKED_ONLY, foo1, false, foo3, false, CPredictionCopy::TRANSFERDATA_ERRORCHECK_SPEW );
  171. int diff_count = 0;
  172. diff_count = tester.TransferData( "test1", -1, foo3->GetPredDescMap() );
  173. Msg( "diff_count == %i\n", diff_count );
  174. Assert( !diff_count );
  175. }
  176. {
  177. Msg( "Simple compare of != objects, should spew and have non-zero diffcount\n" );
  178. // Compare foo1 and foo2, should differ
  179. CPredictionCopy tester( PC_NON_NETWORKED_ONLY, foo1, false, foo2, false, CPredictionCopy::TRANSFERDATA_ERRORCHECK_SPEW );
  180. int diff_count = 0;
  181. diff_count = tester.TransferData( "test2", -1, foo2->GetPredDescMap() );
  182. Msg( "diff_count == %i (should be 13)\n", diff_count );
  183. Assert( diff_count == 13 );
  184. }
  185. {
  186. Msg( "Comparing and copying same objects, should spew and have non-zero diffcount\n" );
  187. // Compare foo1 and foo2 while overriting foo2, should have differences but leave objects ==
  188. CPredictionCopy tester( PC_NON_NETWORKED_ONLY, foo1, false, foo2, false, CPredictionCopy::TRANSFERDATA_COPYONLY );
  189. tester.TransferData( "test2", -1, foo1->GetPredDescMap() );
  190. }
  191. {
  192. Msg( "Comparing and copying objects which were just made to coincide, should have zero diffcount\n" );
  193. // Make sure foo1 is now == foo2
  194. CPredictionCopy tester( PC_NON_NETWORKED_ONLY, foo1, false, foo2, false, CPredictionCopy::TRANSFERDATA_ERRORCHECK_SPEW );
  195. int diff_count = 0;
  196. diff_count = tester.TransferData( "test4", -1, foo2->GetPredDescMap() );
  197. Msg( "diff_count == %i\n", diff_count );
  198. Assert( !diff_count );
  199. }
  200. delete foo3;
  201. delete foo2;
  202. delete foo1;
  203. }
  204. #endif // COPY_CHECK_STRESSTEST