aboutsummaryrefslogtreecommitdiff
path: root/mp/src/game/shared/test_ehandle.cpp
blob: 3e5a0d2bae72888195c3d15c27dda459b1cdb5af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//

#include "cbase.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

// In this test, the server makes an entity (call it A) that has an EHANDLE to another 
// entity (call it B). Intitially, A is sent to the client but B is not. Later, 
// the server decides to send B to the client too. At that point, without resending A's EHANDLE,
// the client's EHANDLE should access B.

#if defined( GAME_DLL )

	// ------------------------------------------------------------------------------------ //
	// The main entity class (class A).
	// ------------------------------------------------------------------------------------ //
	class CHandleTest : public CBaseEntity
	{
	public:
		DECLARE_CLASS( CHandleTest, CBaseEntity );
		DECLARE_SERVERCLASS();

		CHandleTest()
		{
			m_bSendHandle = false;
		}
		
		virtual int UpdateTransmitState()
		{
			// ALWAYS transmit to all clients.
			return SetTransmitState( FL_EDICT_ALWAYS );
		}

		virtual void SetTransmit( CCheckTransmitInfo *pInfo, bool bAlways )
		{
			if ( pInfo->m_pTransmitEdict->Get( entindex() ) )
				return;

			BaseClass::SetTransmit( pInfo, bAlways );
			
			// Force the thing we're pointing at to be sent too?
			if ( m_bSendHandle )
				m_Handle->SetTransmit( pInfo, bAlways );
		}

		CNetworkHandle( CBaseEntity, m_Handle );
		CNetworkVar( bool, m_bSendHandle );
	};

	IMPLEMENT_SERVERCLASS_ST( CHandleTest, DT_HandleTest )
		SendPropEHandle( SENDINFO( m_Handle ) ),
		SendPropInt( SENDINFO( m_bSendHandle ) )
	END_SEND_TABLE()
	
	LINK_ENTITY_TO_CLASS( handle_test, CHandleTest );


	// ------------------------------------------------------------------------------------ //
	// The class pointed to by the handle.
	// ------------------------------------------------------------------------------------ //
	class CHandleDummy : public CBaseEntity
	{
		DECLARE_CLASS( CHandleDummy, CBaseEntity );
	public:
	};
	LINK_ENTITY_TO_CLASS( handle_dummy, CHandleDummy );

	CHandle<CHandleTest> g_HandleTest;

	// The test runs this command.
	void CC_Test_EHandle()
	{
		if ( g_HandleTest.Get() )
		{
			g_HandleTest->m_bSendHandle = !g_HandleTest->m_bSendHandle;
		}
		else
		{
			CHandleTest *pHolder = CREATE_ENTITY( CHandleTest, "handle_test" );
			pHolder->m_Handle = CREATE_ENTITY( CHandleDummy, "handle_dummy" );
			pHolder->Spawn();
			g_HandleTest = pHolder;
			Msg( "Created EHANDLE test entity. Run this command again to transmit the second ent.\n" );
		}
	}
	ConCommand Test_EHandle( "Test_EHandle", CC_Test_EHandle, 0, FCVAR_CHEAT );
	

#else

	class C_HandleTest : public C_BaseEntity
	{
	public:
		DECLARE_CLASS( C_HandleTest, C_BaseEntity );
		DECLARE_CLIENTCLASS();
		
		C_HandleTest()
		{
		}

		virtual void OnDataChanged( DataUpdateType_t type )
		{
			Msg( "m_bSendHandle: %d, m_Handle.Get: 0x%p\n", m_bSendHandle, m_Handle.Get() );
		}

		EHANDLE m_Handle;
		bool m_bSendHandle;
	};

	IMPLEMENT_CLIENTCLASS_DT( C_HandleTest, DT_HandleTest, CHandleTest )
		RecvPropEHandle( RECVINFO( m_Handle ) ),
		RecvPropInt( RECVINFO( m_bSendHandle ) )
	END_RECV_TABLE()


#endif