summaryrefslogtreecommitdiff
path: root/engine/dt_stack.h
blob: 9fa48d4e34205da1db549977dc40e98f39c9991a (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#ifndef DATATABLE_STACK_H
#define DATATABLE_STACK_H
#ifdef _WIN32
#pragma once
#endif


#include "dt.h"
#include "dt_recv_decoder.h"


class CSendNode;
static 	CSendProxyRecipients s_Recipients; // avoid calling constructor each time


// ----------------------------------------------------------------------------- //
//
// CDatatableStack
//
// CDatatableStack is used to walk through a datatable's tree, calling proxies
// along the way to update the current data pointer.
//
// ----------------------------------------------------------------------------- //

abstract_class CDatatableStack
{
public:
	
							CDatatableStack( CSendTablePrecalc *pPrecalc, unsigned char *pStructBase, int objectID );

	// This must be called before accessing properties.
	void Init( bool bExplicitRoutes=false );

	// The stack is meant to be used by calling SeekToProp with increasing property
	// numbers.
	void			SeekToProp( int iProp );

	bool			IsCurProxyValid() const;
	bool			IsPropProxyValid(int iProp ) const;
	int				GetCurPropIndex() const;
	
	unsigned char*	GetCurStructBase() const;
	
	int				GetObjectID() const;

	// Derived classes must implement this. The server gets one and the client gets one.
	// It calls the proxy to move to the next datatable's data.
	virtual void RecurseAndCallProxies( CSendNode *pNode, unsigned char *pStructBase ) = 0;


public:
	CSendTablePrecalc *m_pPrecalc;
	
	enum
	{
		MAX_PROXY_RESULTS = 256
	};

	// These point at the various values that the proxies returned. They are setup once, then 
	// the properties index them.
	unsigned char *m_pProxies[MAX_PROXY_RESULTS];
	unsigned char *m_pStructBase;
	int m_iCurProp;

protected:

	const SendProp *m_pCurProp;
	
	int m_ObjectID;

	bool m_bInitted;
};

inline bool CDatatableStack::IsPropProxyValid(int iProp ) const
{
	return m_pProxies[m_pPrecalc->m_PropProxyIndices[iProp]] != 0;
}

inline bool CDatatableStack::IsCurProxyValid() const
{
	return m_pProxies[m_pPrecalc->m_PropProxyIndices[m_iCurProp]] != 0;
}

inline int CDatatableStack::GetCurPropIndex() const
{
	return m_iCurProp;
}

inline unsigned char* CDatatableStack::GetCurStructBase() const
{
	return m_pProxies[m_pPrecalc->m_PropProxyIndices[m_iCurProp]]; 
}

inline void CDatatableStack::SeekToProp( int iProp )
{
	Assert( m_bInitted );
	
	m_iCurProp = iProp;
	m_pCurProp = m_pPrecalc->GetProp( iProp );
}

inline int CDatatableStack::GetObjectID() const
{
	return m_ObjectID;
}


// This can be used IF you called Init() with true for bExplicitRoutes.
// It is faster to use this route if you only are going to ask for a couple props.
// If you're going to ask for all the props, then you shouldn't use the "explicit" route.
template< class DTStack, class ProxyCaller >
inline unsigned char* UpdateRoutesExplicit_Template( DTStack *pStack, ProxyCaller *caller )
{
	// Early out.
	unsigned short iPropProxyIndex = pStack->m_pPrecalc->m_PropProxyIndices[pStack->m_iCurProp];
	unsigned char **pTest = &pStack->m_pProxies[iPropProxyIndex];
	if ( *pTest != (unsigned char*)0xFFFFFFFF )
		return *pTest;
	
	// Ok.. setup this proxy.
	unsigned char *pStructBase = pStack->m_pStructBase;
	
	CSendTablePrecalc::CProxyPath &proxyPath = pStack->m_pPrecalc->m_ProxyPaths[iPropProxyIndex];
	for ( unsigned short i=0; i < proxyPath.m_nEntries; i++ )
	{
		CSendTablePrecalc::CProxyPathEntry *pEntry = &pStack->m_pPrecalc->m_ProxyPathEntries[proxyPath.m_iFirstEntry + i];
		int iProxy = pEntry->m_iProxy;
		
		if ( pStack->m_pProxies[iProxy] == (unsigned char*)0xFFFFFFFF )
		{
			pStack->m_pProxies[iProxy] = ProxyCaller::CallProxy( pStack, pStructBase, pEntry->m_iDatatableProp );
			if ( !pStack->m_pProxies[iProxy] )
			{
				*pTest = NULL;
				break;
			}			
		}
		
		pStructBase = pStack->m_pProxies[iProxy];
	}
	
	return pStructBase;
}


// ------------------------------------------------------------------------------------ //
// The datatable stack for a RecvTable.
// ------------------------------------------------------------------------------------ //
class CClientDatatableStack : public CDatatableStack
{
public:
						CClientDatatableStack( CRecvDecoder *pDecoder, unsigned char *pStructBase, int objectID ) :
							CDatatableStack( &pDecoder->m_Precalc, pStructBase, objectID )
						{
							m_pDecoder = pDecoder;
						}

	inline unsigned char*	CallPropProxy( CSendNode *pNode, int iProp, unsigned char *pStructBase )
	{
		const RecvProp *pProp = m_pDecoder->GetDatatableProp( iProp );

		void *pVal = NULL;

		Assert( pProp );

		// We may crash later for doing this, but at least this will allow users to watch their demos
		if ( !pProp )
			return NULL;

		pProp->GetDataTableProxyFn()( 
			pProp,
			&pVal,
			pStructBase + pProp->GetOffset(), 
			GetObjectID()
			);

		return (unsigned char*)pVal;
	}

	virtual void RecurseAndCallProxies( CSendNode *pNode, unsigned char *pStructBase )
	{
		// Remember where the game code pointed us for this datatable's data so 
		m_pProxies[pNode->GetRecursiveProxyIndex()] = pStructBase;

		for ( int iChild=0; iChild < pNode->GetNumChildren(); iChild++ )
		{
			CSendNode *pCurChild = pNode->GetChild( iChild );
			
			unsigned char *pNewStructBase = NULL;
			if ( pStructBase )
			{
				pNewStructBase = CallPropProxy( pCurChild, pCurChild->m_iDatatableProp, pStructBase );
			}

			RecurseAndCallProxies( pCurChild, pNewStructBase );
		}
	}

	class CRecvProxyCaller
	{
	public:
		static inline unsigned char* CallProxy( CClientDatatableStack *pStack, unsigned char *pStructBase, unsigned short iDatatableProp )
		{
			const RecvProp *pProp = pStack->m_pDecoder->GetDatatableProp( iDatatableProp );

			void *pVal = NULL;
			pProp->GetDataTableProxyFn()( 
				pProp,
				&pVal, 
				pStructBase + pProp->GetOffset(), 
				pStack->m_ObjectID
				);
				
			return (unsigned char*)pVal;
		}
	};
	
	inline unsigned char* UpdateRoutesExplicit()
	{
		return UpdateRoutesExplicit_Template( this, (CRecvProxyCaller*)NULL );
	}
			

public:
	
	CRecvDecoder	*m_pDecoder;
};


class CServerDatatableStack : public CDatatableStack
{
public:
						CServerDatatableStack( CSendTablePrecalc *pPrecalc, unsigned char *pStructBase, int objectID ) :
							CDatatableStack( pPrecalc, pStructBase, objectID )
						{
							m_pPrecalc = pPrecalc;
							m_pRecipients = NULL;
						}

	inline unsigned char*	CallPropProxy( CSendNode *pNode, int iProp, unsigned char *pStructBase )
	{
		const SendProp *pProp = m_pPrecalc->GetDatatableProp( iProp );

		CSendProxyRecipients *pRecipients;

		if ( m_pRecipients && pNode->GetDataTableProxyIndex() != DATATABLE_PROXY_INDEX_NOPROXY )
		{
			// set recipients pointer and all clients by default
			pRecipients = &m_pRecipients->Element( pNode->GetDataTableProxyIndex() );
			pRecipients->SetAllRecipients();
		}
		else
		{
			// we don't care about recipients, just provide a valid pointer
			pRecipients = &s_Recipients; 
		}

		unsigned char *pRet = (unsigned char*)pProp->GetDataTableProxyFn()( 
			pProp,
			pStructBase, 
			pStructBase + pProp->GetOffset(), 
			pRecipients,
			GetObjectID()
			);
	
		return pRet;
	}

	virtual void RecurseAndCallProxies( CSendNode *pNode, unsigned char *pStructBase )
	{
		// Remember where the game code pointed us for this datatable's data so 
		m_pProxies[pNode->GetRecursiveProxyIndex()] = pStructBase;

		for ( int iChild=0; iChild < pNode->GetNumChildren(); iChild++ )
		{
			CSendNode *pCurChild = pNode->GetChild( iChild );
			
			unsigned char *pNewStructBase = NULL;
			if ( pStructBase )
			{
				pNewStructBase = CallPropProxy( pCurChild, pCurChild->m_iDatatableProp, pStructBase );
			}

			RecurseAndCallProxies( pCurChild, pNewStructBase );
		}
	}

	// This can be used IF you called Init() with true for bExplicitRoutes.
	// It is faster to use this route if you only are going to ask for a couple props.
	// If you're going to ask for all the props, then you shouldn't use the "explicit" route.
	class CSendProxyCaller
	{
	public:
		static inline unsigned char* CallProxy( CServerDatatableStack *pStack, unsigned char *pStructBase, unsigned short iDatatableProp )
		{
			const SendProp *pProp = pStack->m_pPrecalc->GetDatatableProp( iDatatableProp );
			
			return (unsigned char*)pProp->GetDataTableProxyFn()( 
				pProp,
				pStructBase, 
				pStructBase + pProp->GetOffset(), 
				&s_Recipients,
				pStack->GetObjectID()
				);
		}
	};
	
	inline unsigned char* UpdateRoutesExplicit()
	{
		return UpdateRoutesExplicit_Template( this, (CSendProxyCaller*)NULL );
	}

	
	const SendProp*	GetCurProp() const;


public:
	
	CSendTablePrecalc					*m_pPrecalc;
	CUtlMemory<CSendProxyRecipients>	*m_pRecipients;
};


inline const SendProp* CServerDatatableStack::GetCurProp() const
{
	return m_pPrecalc->GetProp( GetCurPropIndex() );
}


#endif // DATATABLE_STACK_H