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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include <stdarg.h>
#include "dt_send.h"
#include "dt.h"
#include "dt_recv.h"
#include "dt_encode.h"
#include "convar.h"
#include "commonmacros.h"
#include "tier1/strtools.h"
#include "tier0/dbg.h"
#include "dt_stack.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#define PROPINDEX_NUMBITS 12
#define MAX_TOTAL_SENDTABLE_PROPS (1 << PROPINDEX_NUMBITS)
ConVar g_CV_DTWatchEnt( "dtwatchent", "-1", 0, "Watch this entities data table encoding." );
ConVar g_CV_DTWatchVar( "dtwatchvar", "", 0, "Watch the named variable." );
ConVar g_CV_DTWarning( "dtwarning", "0", 0, "Print data table warnings?" );
ConVar g_CV_DTWatchClass( "dtwatchclass", "", 0, "Watch all fields encoded with this table." );
// ----------------------------------------------------------------------------- //
//
// CBuildHierarchyStruct
//
// Used while building a CSendNode hierarchy.
//
// ----------------------------------------------------------------------------- //
class CBuildHierarchyStruct
{
public:
const ExcludeProp *m_pExcludeProps;
int m_nExcludeProps;
const SendProp *m_pDatatableProps[MAX_TOTAL_SENDTABLE_PROPS];
int m_nDatatableProps;
const SendProp *m_pProps[MAX_TOTAL_SENDTABLE_PROPS];
unsigned char m_PropProxyIndices[MAX_TOTAL_SENDTABLE_PROPS];
int m_nProps;
unsigned char m_nPropProxies;
};
// ----------------------------------------------------------------------------- //
// CSendNode.
// ----------------------------------------------------------------------------- //
CSendNode::CSendNode()
{
m_iDatatableProp = -1;
m_pTable = NULL;
m_iFirstRecursiveProp = m_nRecursiveProps = 0;
m_DataTableProxyIndex = DATATABLE_PROXY_INDEX_INVALID; // set it to a questionable value.
}
CSendNode::~CSendNode()
{
int c = GetNumChildren();
for ( int i = c - 1 ; i >= 0 ; i-- )
{
delete GetChild( i );
}
m_Children.Purge();
}
// ----------------------------------------------------------------------------- //
// CSendTablePrecalc
// ----------------------------------------------------------------------------- //
bool PropOffsetLT( const unsigned short &a, const unsigned short &b )
{
return a < b;
}
CSendTablePrecalc::CSendTablePrecalc() :
m_PropOffsetToIndexMap( 0, 0, PropOffsetLT )
{
m_pDTITable = NULL;
m_pSendTable = 0;
m_nDataTableProxies = 0;
}
CSendTablePrecalc::~CSendTablePrecalc()
{
if ( m_pSendTable )
m_pSendTable->m_pPrecalc = 0;
}
const ExcludeProp* FindExcludeProp(
char const *pTableName,
char const *pPropName,
const ExcludeProp *pExcludeProps,
int nExcludeProps)
{
for ( int i=0; i < nExcludeProps; i++ )
{
if ( stricmp(pExcludeProps[i].m_pTableName, pTableName) == 0 && stricmp(pExcludeProps[i].m_pPropName, pPropName ) == 0 )
return &pExcludeProps[i];
}
return NULL;
}
// Fill in a list of all the excluded props.
static bool SendTable_GetPropsExcluded( const SendTable *pTable, ExcludeProp *pExcludeProps, int &nExcludeProps, int nMaxExcludeProps )
{
for(int i=0; i < pTable->m_nProps; i++)
{
SendProp *pProp = &pTable->m_pProps[i];
if ( pProp->IsExcludeProp() )
{
char const *pName = pProp->GetExcludeDTName();
ErrorIfNot( pName,
("Found an exclude prop missing a name.")
);
ErrorIfNot( nExcludeProps < nMaxExcludeProps,
("SendTable_GetPropsExcluded: Overflowed max exclude props with %s.", pName)
);
pExcludeProps[nExcludeProps].m_pTableName = pName;
pExcludeProps[nExcludeProps].m_pPropName = pProp->GetName();
nExcludeProps++;
}
else if ( pProp->GetDataTable() )
{
if( !SendTable_GetPropsExcluded( pProp->GetDataTable(), pExcludeProps, nExcludeProps, nMaxExcludeProps ) )
return false;
}
}
return true;
}
// Set the datatable proxy indices in all datatable SendProps.
static void SetDataTableProxyIndices_R(
CSendTablePrecalc *pMainTable,
CSendNode *pCurTable,
CBuildHierarchyStruct *bhs )
{
for ( int i=0; i < pCurTable->GetNumChildren(); i++ )
{
CSendNode *pNode = pCurTable->GetChild( i );
const SendProp *pProp = bhs->m_pDatatableProps[pNode->m_iDatatableProp];
if ( pProp->GetFlags() & SPROP_PROXY_ALWAYS_YES )
{
pNode->SetDataTableProxyIndex( DATATABLE_PROXY_INDEX_NOPROXY );
}
else
{
pNode->SetDataTableProxyIndex( pMainTable->GetNumDataTableProxies() );
pMainTable->SetNumDataTableProxies( pMainTable->GetNumDataTableProxies() + 1 );
}
SetDataTableProxyIndices_R( pMainTable, pNode, bhs );
}
}
// Set the datatable proxy indices in all datatable SendProps.
static void SetRecursiveProxyIndices_R(
SendTable *pBaseTable,
CSendNode *pCurTable,
int &iCurProxyIndex )
{
if ( iCurProxyIndex >= CDatatableStack::MAX_PROXY_RESULTS )
Error( "Too many proxies for datatable %s.", pBaseTable->GetName() );
pCurTable->SetRecursiveProxyIndex( iCurProxyIndex );
iCurProxyIndex++;
for ( int i=0; i < pCurTable->GetNumChildren(); i++ )
{
CSendNode *pNode = pCurTable->GetChild( i );
SetRecursiveProxyIndices_R( pBaseTable, pNode, iCurProxyIndex );
}
}
void SendTable_BuildHierarchy(
CSendNode *pNode,
const SendTable *pTable,
CBuildHierarchyStruct *bhs
);
void SendTable_BuildHierarchy_IterateProps(
CSendNode *pNode,
const SendTable *pTable,
CBuildHierarchyStruct *bhs,
const SendProp *pNonDatatableProps[MAX_TOTAL_SENDTABLE_PROPS],
int &nNonDatatableProps )
{
int i;
for ( i=0; i < pTable->m_nProps; i++ )
{
const SendProp *pProp = &pTable->m_pProps[i];
if ( pProp->IsExcludeProp() ||
pProp->IsInsideArray() ||
FindExcludeProp( pTable->GetName(), pProp->GetName(), bhs->m_pExcludeProps, bhs->m_nExcludeProps ) )
{
continue;
}
if ( pProp->GetType() == DPT_DataTable )
{
if ( pProp->GetFlags() & SPROP_COLLAPSIBLE )
{
// This is a base class.. no need to make a new CSendNode (and trigger a bunch of
// unnecessary send proxy calls in the datatable stacks).
SendTable_BuildHierarchy_IterateProps(
pNode,
pProp->GetDataTable(),
bhs,
pNonDatatableProps,
nNonDatatableProps );
}
else
{
// Setup a child datatable reference.
CSendNode *pChild = new CSendNode;
// Setup a datatable prop for this node to reference (so the recursion
// routines can get at the proxy).
if ( bhs->m_nDatatableProps >= ARRAYSIZE( bhs->m_pDatatableProps ) )
Error( "Overflowed datatable prop list in SendTable '%s'.", pTable->GetName() );
bhs->m_pDatatableProps[bhs->m_nDatatableProps] = pProp;
pChild->m_iDatatableProp = bhs->m_nDatatableProps;
++bhs->m_nDatatableProps;
pNode->m_Children.AddToTail( pChild );
// Recurse into the new child datatable.
SendTable_BuildHierarchy( pChild, pProp->GetDataTable(), bhs );
}
}
else
{
if ( nNonDatatableProps >= MAX_TOTAL_SENDTABLE_PROPS )
Error( "SendTable_BuildHierarchy: overflowed non-datatable props with '%s'.", pProp->GetName() );
pNonDatatableProps[nNonDatatableProps] = pProp;
++nNonDatatableProps;
}
}
}
void SendTable_BuildHierarchy(
CSendNode *pNode,
const SendTable *pTable,
CBuildHierarchyStruct *bhs
)
{
pNode->m_pTable = pTable;
pNode->m_iFirstRecursiveProp = bhs->m_nProps;
Assert( bhs->m_nPropProxies < 255 );
unsigned char curPropProxy = bhs->m_nPropProxies;
++bhs->m_nPropProxies;
const SendProp *pNonDatatableProps[MAX_TOTAL_SENDTABLE_PROPS];
int nNonDatatableProps = 0;
// First add all the child datatables.
SendTable_BuildHierarchy_IterateProps(
pNode,
pTable,
bhs,
pNonDatatableProps,
nNonDatatableProps );
// Now add the properties.
// Make sure there's room, then just copy the pointers from the loop above.
ErrorIfNot( bhs->m_nProps + nNonDatatableProps < ARRAYSIZE( bhs->m_pProps ),
("SendTable_BuildHierarchy: overflowed prop buffer.")
);
for ( int i=0; i < nNonDatatableProps; i++ )
{
bhs->m_pProps[bhs->m_nProps] = pNonDatatableProps[i];
bhs->m_PropProxyIndices[bhs->m_nProps] = curPropProxy;
++bhs->m_nProps;
}
pNode->m_nRecursiveProps = bhs->m_nProps - pNode->m_iFirstRecursiveProp;
}
void SendTable_SortByPriority(CBuildHierarchyStruct *bhs)
{
int i, start = 0;
while( true )
{
for ( i = start; i < bhs->m_nProps; i++ )
{
const SendProp *p = bhs->m_pProps[i];
unsigned char c = bhs->m_PropProxyIndices[i];
if ( p->GetFlags() & SPROP_CHANGES_OFTEN )
{
bhs->m_pProps[i] = bhs->m_pProps[start];
bhs->m_PropProxyIndices[i] = bhs->m_PropProxyIndices[start];
bhs->m_pProps[start] = p;
bhs->m_PropProxyIndices[start] = c;
start++;
break;
}
}
if ( i == bhs->m_nProps )
return;
}
}
void CalcPathLengths_R( CSendNode *pNode, CUtlVector<int> &pathLengths, int curPathLength, int &totalPathLengths )
{
pathLengths[pNode->GetRecursiveProxyIndex()] = curPathLength;
totalPathLengths += curPathLength;
for ( int i=0; i < pNode->GetNumChildren(); i++ )
{
CalcPathLengths_R( pNode->GetChild( i ), pathLengths, curPathLength+1, totalPathLengths );
}
}
void FillPathEntries_R( CSendTablePrecalc *pPrecalc, CSendNode *pNode, CSendNode *pParent, int &iCurEntry )
{
// Fill in this node's path.
CSendTablePrecalc::CProxyPath &outProxyPath = pPrecalc->m_ProxyPaths[ pNode->GetRecursiveProxyIndex() ];
outProxyPath.m_iFirstEntry = (unsigned short)iCurEntry;
// Copy all the proxies leading to the parent.
if ( pParent )
{
CSendTablePrecalc::CProxyPath &parentProxyPath = pPrecalc->m_ProxyPaths[pParent->GetRecursiveProxyIndex()];
outProxyPath.m_nEntries = parentProxyPath.m_nEntries + 1;
for ( int i=0; i < parentProxyPath.m_nEntries; i++ )
pPrecalc->m_ProxyPathEntries[iCurEntry++] = pPrecalc->m_ProxyPathEntries[parentProxyPath.m_iFirstEntry+i];
// Now add this node's own proxy.
pPrecalc->m_ProxyPathEntries[iCurEntry].m_iProxy = pNode->GetRecursiveProxyIndex();
pPrecalc->m_ProxyPathEntries[iCurEntry].m_iDatatableProp = pNode->m_iDatatableProp;
++iCurEntry;
}
else
{
outProxyPath.m_nEntries = 0;
}
for ( int i=0; i < pNode->GetNumChildren(); i++ )
{
FillPathEntries_R( pPrecalc, pNode->GetChild( i ), pNode, iCurEntry );
}
}
void SendTable_GenerateProxyPaths( CSendTablePrecalc *pPrecalc, int nProxyIndices )
{
// Initialize the array.
pPrecalc->m_ProxyPaths.SetSize( nProxyIndices );
for ( int i=0; i < nProxyIndices; i++ )
pPrecalc->m_ProxyPaths[i].m_iFirstEntry = pPrecalc->m_ProxyPaths[i].m_nEntries = 0xFFFF;
// Figure out how long the path down the tree is to each node.
int totalPathLengths = 0;
CUtlVector<int> pathLengths;
pathLengths.SetSize( nProxyIndices );
memset( pathLengths.Base(), 0, sizeof( pathLengths[0] ) * nProxyIndices );
CalcPathLengths_R( pPrecalc->GetRootNode(), pathLengths, 0, totalPathLengths );
//
int iCurEntry = 0;
pPrecalc->m_ProxyPathEntries.SetSize( totalPathLengths );
FillPathEntries_R( pPrecalc, pPrecalc->GetRootNode(), NULL, iCurEntry );
}
bool CSendTablePrecalc::SetupFlatPropertyArray()
{
SendTable *pTable = GetSendTable();
// First go through and set SPROP_INSIDEARRAY when appropriate, and set array prop pointers.
SetupArrayProps_R<SendTable, SendTable::PropType>( pTable );
// Make a list of which properties are excluded.
ExcludeProp excludeProps[MAX_EXCLUDE_PROPS];
int nExcludeProps = 0;
if( !SendTable_GetPropsExcluded( pTable, excludeProps, nExcludeProps, MAX_EXCLUDE_PROPS ) )
return false;
// Now build the hierarchy.
CBuildHierarchyStruct bhs;
bhs.m_pExcludeProps = excludeProps;
bhs.m_nExcludeProps = nExcludeProps;
bhs.m_nProps = bhs.m_nDatatableProps = 0;
bhs.m_nPropProxies = 0;
SendTable_BuildHierarchy( GetRootNode(), pTable, &bhs );
SendTable_SortByPriority( &bhs );
// Copy the SendProp pointers into the precalc.
MEM_ALLOC_CREDIT();
m_Props.CopyArray( bhs.m_pProps, bhs.m_nProps );
m_DatatableProps.CopyArray( bhs.m_pDatatableProps, bhs.m_nDatatableProps );
m_PropProxyIndices.CopyArray( bhs.m_PropProxyIndices, bhs.m_nProps );
// Assign the datatable proxy indices.
SetNumDataTableProxies( 0 );
SetDataTableProxyIndices_R( this, GetRootNode(), &bhs );
int nProxyIndices = 0;
SetRecursiveProxyIndices_R( pTable, GetRootNode(), nProxyIndices );
SendTable_GenerateProxyPaths( this, nProxyIndices );
return true;
}
// ---------------------------------------------------------------------------------------- //
// Helpers.
// ---------------------------------------------------------------------------------------- //
// Compares two arrays of bits.
// Returns true if they are equal.
bool AreBitArraysEqual(
void const *pvBits1,
void const *pvBits2,
int nBits )
{
unsigned int const *pBits1 = (unsigned int const *)pvBits1;
unsigned int const *pBits2 = (unsigned int const *)pvBits2;
// Compare words.
int nWords = nBits >> 5;
for ( int i = 0 ; i < nWords; ++i )
{
if ( pBits1[i] != pBits2[i] )
return false;
}
if ( nBits & 31 )
{
// Compare remaining bits.
unsigned int mask = (1 << (nBits & 31)) - 1;
return ((pBits1[nWords] ^ pBits2[nWords]) & mask) == 0;
}
return true;
}
// Does a fast memcmp-based test to determine if the two bit arrays are different.
// Returns true if they are equal.
bool CompareBitArrays(
void const *pPacked1,
void const *pPacked2,
int nBits1,
int nBits2
)
{
if( nBits1 >= 0 && nBits1 == nBits2 )
{
if ( pPacked1 == pPacked2 )
{
return true;
}
else
{
return AreBitArraysEqual( pPacked1, pPacked2, nBits1 );
}
}
else
return false;
}
// Looks at the DTWatchEnt and DTWatchProp console variables and returns true
// if the user wants to watch this property.
bool ShouldWatchThisProp( const SendTable *pTable, int objectID, const char *pPropName )
{
if(g_CV_DTWatchEnt.GetInt() != -1 &&
g_CV_DTWatchEnt.GetInt() == objectID)
{
const char *pStr = g_CV_DTWatchVar.GetString();
if ( pStr && pStr[0] != 0 )
{
return stricmp( pStr, pPropName ) == 0;
}
else
{
return true;
}
}
if ( g_CV_DTWatchClass.GetString()[ 0 ] && Q_stristr( pTable->GetName(), g_CV_DTWatchClass.GetString() ) )
return true;
return false;
}
bool Sendprop_UsingDebugWatch()
{
if ( g_CV_DTWatchEnt.GetInt() != -1 )
return true;
if ( g_CV_DTWatchClass.GetString()[ 0 ] )
return true;
return false;
}
// Prints a datatable warning into the console.
void DataTable_Warning( const char *pInMessage, ... )
{
char msg[4096];
va_list marker;
#if 0
#if !defined(_DEBUG)
if(!g_CV_DTWarning.GetInt())
return;
#endif
#endif
va_start(marker, pInMessage);
Q_vsnprintf( msg, sizeof( msg ), pInMessage, marker);
va_end(marker);
Warning( "DataTable warning: %s", msg );
}
|