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.

125 lines
3.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "cbase.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. // In this test, the server makes an entity (call it A) that has an EHANDLE to another
  12. // entity (call it B). Intitially, A is sent to the client but B is not. Later,
  13. // the server decides to send B to the client too. At that point, without resending A's EHANDLE,
  14. // the client's EHANDLE should access B.
  15. #if defined( GAME_DLL )
  16. // ------------------------------------------------------------------------------------ //
  17. // The main entity class (class A).
  18. // ------------------------------------------------------------------------------------ //
  19. class CHandleTest : public CBaseEntity
  20. {
  21. public:
  22. DECLARE_CLASS( CHandleTest, CBaseEntity );
  23. DECLARE_SERVERCLASS();
  24. CHandleTest()
  25. {
  26. m_bSendHandle = false;
  27. }
  28. virtual int UpdateTransmitState()
  29. {
  30. // ALWAYS transmit to all clients.
  31. return SetTransmitState( FL_EDICT_ALWAYS );
  32. }
  33. virtual void SetTransmit( CCheckTransmitInfo *pInfo, bool bAlways )
  34. {
  35. if ( pInfo->m_pTransmitEdict->Get( entindex() ) )
  36. return;
  37. BaseClass::SetTransmit( pInfo, bAlways );
  38. // Force the thing we're pointing at to be sent too?
  39. if ( m_bSendHandle )
  40. m_Handle->SetTransmit( pInfo, bAlways );
  41. }
  42. CNetworkHandle( CBaseEntity, m_Handle );
  43. CNetworkVar( bool, m_bSendHandle );
  44. };
  45. IMPLEMENT_SERVERCLASS_ST( CHandleTest, DT_HandleTest )
  46. SendPropEHandle( SENDINFO( m_Handle ) ),
  47. SendPropInt( SENDINFO( m_bSendHandle ) )
  48. END_SEND_TABLE()
  49. LINK_ENTITY_TO_CLASS( handle_test, CHandleTest );
  50. // ------------------------------------------------------------------------------------ //
  51. // The class pointed to by the handle.
  52. // ------------------------------------------------------------------------------------ //
  53. class CHandleDummy : public CBaseEntity
  54. {
  55. DECLARE_CLASS( CHandleDummy, CBaseEntity );
  56. public:
  57. };
  58. LINK_ENTITY_TO_CLASS( handle_dummy, CHandleDummy );
  59. CHandle<CHandleTest> g_HandleTest;
  60. // The test runs this command.
  61. void CC_Test_EHandle()
  62. {
  63. if ( g_HandleTest.Get() )
  64. {
  65. g_HandleTest->m_bSendHandle = !g_HandleTest->m_bSendHandle;
  66. }
  67. else
  68. {
  69. CHandleTest *pHolder = CREATE_ENTITY( CHandleTest, "handle_test" );
  70. pHolder->m_Handle = CREATE_ENTITY( CHandleDummy, "handle_dummy" );
  71. pHolder->Spawn();
  72. g_HandleTest = pHolder;
  73. Msg( "Created EHANDLE test entity. Run this command again to transmit the second ent.\n" );
  74. }
  75. }
  76. ConCommand Test_EHandle( "Test_EHandle", CC_Test_EHandle, 0, FCVAR_CHEAT );
  77. #else
  78. class C_HandleTest : public C_BaseEntity
  79. {
  80. public:
  81. DECLARE_CLASS( C_HandleTest, C_BaseEntity );
  82. DECLARE_CLIENTCLASS();
  83. C_HandleTest()
  84. {
  85. }
  86. virtual void OnDataChanged( DataUpdateType_t type )
  87. {
  88. Msg( "m_bSendHandle: %d, m_Handle.Get: 0x%p\n", m_bSendHandle, m_Handle.Get() );
  89. }
  90. EHANDLE m_Handle;
  91. bool m_bSendHandle;
  92. };
  93. IMPLEMENT_CLIENTCLASS_DT( C_HandleTest, DT_HandleTest, CHandleTest )
  94. RecvPropEHandle( RECVINFO( m_Handle ) ),
  95. RecvPropInt( RECVINFO( m_bSendHandle ) )
  96. END_RECV_TABLE()
  97. #endif