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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#include "LocalNetworkBackdoor.h"
#include "server_class.h"
#include "client_class.h"
#include "server.h"
#include "eiface.h"
#include "cdll_engine_int.h"
#include "dt_localtransfer.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
CLocalNetworkBackdoor *g_pLocalNetworkBackdoor = NULL;
#ifndef SWDS
// This is called
void CLocalNetworkBackdoor::InitFastCopy()
{
if ( !cl.m_NetChannel->IsLoopback() )
return;
const CStandardSendProxies *pSendProxies = NULL;
// If the game server is greater than v4, then it is using the new proxy format.
if ( g_iServerGameDLLVersion >= 5 ) // check server version
{
pSendProxies = serverGameDLL->GetStandardSendProxies();
}
else
{
// If the game server is older than v4, it is using the old proxy; we set the new proxy members to the
// engine's copy.
static CStandardSendProxies compatSendProxy = *serverGameDLL->GetStandardSendProxies();
compatSendProxy.m_DataTableToDataTable = g_StandardSendProxies.m_DataTableToDataTable;
compatSendProxy.m_SendLocalDataTable = g_StandardSendProxies.m_SendLocalDataTable;
compatSendProxy.m_ppNonModifiedPointerProxies = g_StandardSendProxies.m_ppNonModifiedPointerProxies;
pSendProxies = &compatSendProxy;
}
const CStandardRecvProxies *pRecvProxies = g_ClientDLL->GetStandardRecvProxies();
int nFastCopyProps = 0;
int nSlowCopyProps = 0;
for ( int iClass=0; iClass < cl.m_nServerClasses; iClass++ )
{
ClientClass *pClientClass = cl.GetClientClass(iClass);
if ( !pClientClass )
Error( "InitFastCopy - missing client class %d (Should be equivelent of server class: %s)", iClass, cl.m_pServerClasses[iClass].m_ClassName );
ServerClass *pServerClass = SV_FindServerClass( pClientClass->GetName() );
if ( !pServerClass )
Error( "InitFastCopy - missing server class %s", pClientClass->GetName() );
LocalTransfer_InitFastCopy(
pServerClass->m_pTable,
pSendProxies,
pClientClass->m_pRecvTable,
pRecvProxies,
nSlowCopyProps,
nFastCopyProps
);
}
int percentFast = (nFastCopyProps * 100 ) / (nSlowCopyProps + nFastCopyProps + 1);
if ( percentFast <= 55 )
{
// This may not be a real problem, but at the time this code was added, 67% of the
// properties were able to be copied without proxies. If percentFast goes to 0 or some
// really low number suddenly, then something probably got screwed up.
Assert( false );
Warning( "InitFastCopy: only %d%% fast props. Bug?\n", percentFast );
}
}
#endif
void CLocalNetworkBackdoor::StartEntityStateUpdate()
{
m_EntsAlive.ClearAll();
m_nEntsCreated = 0;
m_nEntsChanged = 0;
// signal client that we start updating entities
ClientDLL_FrameStageNotify( FRAME_NET_UPDATE_START );
}
void CLocalNetworkBackdoor::EndEntityStateUpdate()
{
ClientDLL_FrameStageNotify( FRAME_NET_UPDATE_POSTDATAUPDATE_START );
// Handle entities created.
int i;
for ( i=0; i < m_nEntsCreated; i++ )
{
MDLCACHE_CRITICAL_SECTION_( g_pMDLCache );
int iEdict = m_EntsCreatedIndices[i];
CCachedEntState *pCached = &m_CachedEntState[iEdict];
IClientNetworkable *pNet = pCached->m_pNetworkable;
pNet->PostDataUpdate( DATA_UPDATE_CREATED );
pNet->NotifyShouldTransmit( SHOULDTRANSMIT_START );
pCached->m_bDormant = false;
}
// Handle entities changed.
for ( i=0; i < m_nEntsChanged; i++ )
{
MDLCACHE_CRITICAL_SECTION_( g_pMDLCache );
int iEdict = m_EntsChangedIndices[i];
m_CachedEntState[iEdict].m_pNetworkable->PostDataUpdate( DATA_UPDATE_DATATABLE_CHANGED );
}
ClientDLL_FrameStageNotify( FRAME_NET_UPDATE_POSTDATAUPDATE_END );
// Handle entities removed (= SV_WriteDeletions() in normal mode)
int nDWords = m_PrevEntsAlive.GetNumDWords();
// Handle entities removed.
for ( i=0; i < nDWords; i++ )
{
unsigned long prevEntsAlive = m_PrevEntsAlive.GetDWord( i );
unsigned long entsAlive = m_EntsAlive.GetDWord( i );
unsigned long toDelete = (prevEntsAlive ^ entsAlive) & prevEntsAlive;
if ( toDelete )
{
for ( int iBit=0; iBit < 32; iBit++ )
{
if ( toDelete & (1 << iBit) )
{
int iEdict = (i<<5) + iBit;
if ( iEdict >= 0 && iEdict < MAX_EDICTS )
{
if ( m_CachedEntState[iEdict].m_pNetworkable )
{
m_CachedEntState[iEdict].m_pNetworkable->Release();
m_CachedEntState[iEdict].m_pNetworkable = NULL;
}
else
{
AssertOnce( !"EndEntityStateUpdate: Would have crashed with NULL m_pNetworkable\n" );
}
}
else
{
AssertOnce( !"EndEntityStateUpdate: Would have crashed with entity out of range\n" );
}
}
}
}
}
// Remember the previous state of which entities were around.
m_PrevEntsAlive = m_EntsAlive;
// end of all entity update activity
ClientDLL_FrameStageNotify( FRAME_NET_UPDATE_END );
/*
#ifdef _DEBUG
for ( i=0; i <= highest_index; i++ )
{
if ( !( m_EntsAlive[i>>5] & (1 << (i & 31)) ) )
Assert( !m_CachedEntState[i].m_pNetworkable );
if ( ( m_EntsAlive[i>>5] & (1 << (i & 31)) ) &&
( m_EntsCreated[i>>5] & (1 << (i & 31)) ) )
{
Assert( FindInList( m_EntsCreatedIndices, m_nEntsCreated, i ) );
}
if ( (m_EntsAlive[i>>5] & (1 << (i & 31))) &&
!(m_EntsCreated[i>>5] & (1 << (i & 31))) &&
(m_EntsChanged[i>>5] & (1 << (i & 31)))
)
{
Assert( FindInList( m_EntsChangedIndices, m_nEntsChanged, i ) );
}
}
#endif
*/
}
void CLocalNetworkBackdoor::EntityDormant( int iEnt, int iSerialNum )
{
CCachedEntState *pCached = &m_CachedEntState[iEnt];
IClientNetworkable *pNet = pCached->m_pNetworkable;
Assert( pNet == entitylist->GetClientNetworkable( iEnt ) );
if ( pNet )
{
Assert( pCached->m_iSerialNumber == pNet->GetIClientUnknown()->GetRefEHandle().GetSerialNumber() );
if ( pCached->m_iSerialNumber == iSerialNum )
{
m_EntsAlive.Set( iEnt );
// Tell the game code that this guy is now dormant.
Assert( pCached->m_bDormant == pNet->IsDormant() );
if ( !pCached->m_bDormant )
{
pNet->NotifyShouldTransmit( SHOULDTRANSMIT_END );
pCached->m_bDormant = true;
}
}
else
{
pNet->Release();
pCached->m_pNetworkable = NULL;
m_PrevEntsAlive.Clear( iEnt );
}
}
}
void CLocalNetworkBackdoor::AddToPendingDormantEntityList( unsigned short iEdict )
{
edict_t *e = &sv.edicts[iEdict];
if ( !( e->m_fStateFlags & FL_EDICT_PENDING_DORMANT_CHECK ) )
{
e->m_fStateFlags |= FL_EDICT_PENDING_DORMANT_CHECK;
m_PendingDormantEntities.AddToTail( iEdict );
}
}
void CLocalNetworkBackdoor::ProcessDormantEntities()
{
FOR_EACH_LL( m_PendingDormantEntities, i )
{
int iEdict = m_PendingDormantEntities[i];
edict_t *e = &sv.edicts[iEdict];
// Make sure the entity still exists and stil has the dontsend flag set.
if ( e->IsFree() || !(e->m_fStateFlags & FL_EDICT_DONTSEND) )
{
e->m_fStateFlags &= ~FL_EDICT_PENDING_DORMANT_CHECK;
continue;
}
EntityDormant( iEdict, e->m_NetworkSerialNumber );
e->m_fStateFlags &= ~FL_EDICT_PENDING_DORMANT_CHECK;
}
m_PendingDormantEntities.Purge();
}
void CLocalNetworkBackdoor::EntState(
int iEnt,
int iSerialNum,
int iClass,
const SendTable *pSendTable,
const void *pSourceEnt,
bool bChanged,
bool bShouldTransmit )
{
CCachedEntState *pCached = &m_CachedEntState[iEnt];
// Remember that this ent is alive.
m_EntsAlive.Set(iEnt);
ClientClass *pClientClass = cl.GetClientClass(iClass);
if ( !pClientClass )
Error( "CLocalNetworkBackdoor::EntState - missing client class %d", iClass );
IClientNetworkable *pNet = pCached->m_pNetworkable;
Assert( pNet == entitylist->GetClientNetworkable( iEnt ) );
if ( !bShouldTransmit )
{
if ( pNet )
{
Assert( pCached->m_iSerialNumber == pNet->GetIClientUnknown()->GetRefEHandle().GetSerialNumber() );
if ( pCached->m_iSerialNumber == iSerialNum )
{
// Tell the game code that this guy is now dormant.
Assert( pCached->m_bDormant == pNet->IsDormant() );
if ( !pCached->m_bDormant )
{
pNet->NotifyShouldTransmit( SHOULDTRANSMIT_END );
pCached->m_bDormant = true;
}
}
else
{
pNet->Release();
pNet = NULL;
pCached->m_pNetworkable = NULL;
// Since we set this above, need to clear it now to avoid assertion in EndEntityStateUpdate()
m_EntsAlive.Clear(iEnt);
m_PrevEntsAlive.Clear( iEnt );
}
}
else
{
m_EntsAlive.Clear( iEnt );
}
return;
}
// Do we have an entity here already?
bool bExistedAndWasDormant = false;
if ( pNet )
{
// If the serial numbers are different, make it recreate the ent.
Assert( pCached->m_iSerialNumber == pNet->GetIClientUnknown()->GetRefEHandle().GetSerialNumber() );
if ( iSerialNum == pCached->m_iSerialNumber )
{
bExistedAndWasDormant = pCached->m_bDormant;
}
else
{
pNet->Release();
pNet = NULL;
m_PrevEntsAlive.Clear(iEnt);
}
}
// Create the entity?
bool bCreated = false;
DataUpdateType_t updateType;
if ( pNet )
{
updateType = DATA_UPDATE_DATATABLE_CHANGED;
}
else
{
updateType = DATA_UPDATE_CREATED;
pNet = pClientClass->m_pCreateFn( iEnt, iSerialNum );
bCreated = true;
m_EntsCreatedIndices[m_nEntsCreated++] = iEnt;
pCached->m_iSerialNumber = iSerialNum;
pCached->m_pDataPointer = pNet->GetDataTableBasePtr();
pCached->m_pNetworkable = pNet;
// Tracker 73192: ywb 8/1/07: We used to get an assertion that the pCached->m_bDormant was not equal to pNet->IsDormant() in ProcessDormantEntities.
// This appears to be the case if when we get here, the entity is set for Transmit still, but is a dormant entity on the server.
// Seems safe to go ahead an fill in the cache with the correct data. Probably was just an oversight.
pCached->m_bDormant = pNet->IsDormant();
}
if ( bChanged || bCreated || bExistedAndWasDormant )
{
pNet->PreDataUpdate( updateType );
Assert( pCached->m_pDataPointer == pNet->GetDataTableBasePtr() );
LocalTransfer_TransferEntity(
&sv.edicts[iEnt],
pSendTable,
pSourceEnt,
pClientClass->m_pRecvTable,
pCached->m_pDataPointer,
bCreated,
bExistedAndWasDormant,
iEnt );
if ( bExistedAndWasDormant )
{
// Set this so we use DATA_UPDATE_CREATED logic
m_EntsCreatedIndices[m_nEntsCreated++] = iEnt;
}
else
{
if ( !bCreated )
{
m_EntsChangedIndices[m_nEntsChanged++] = iEnt;
}
}
}
}
void CLocalNetworkBackdoor::ClearState()
{
// Clear the cache for all the entities.
for ( int i=0; i < MAX_EDICTS; i++ )
{
CCachedEntState &ces = m_CachedEntState[i];
ces.m_pNetworkable = NULL;
ces.m_iSerialNumber = -1;
ces.m_bDormant = false;
ces.m_pDataPointer = NULL;
}
m_PrevEntsAlive.ClearAll();
}
void CLocalNetworkBackdoor::StartBackdoorMode()
{
ClearState();
for ( int i=0; i < MAX_EDICTS; i++ )
{
IClientNetworkable *pNet = entitylist->GetClientNetworkable( i );
CCachedEntState &ces = m_CachedEntState[i];
if ( pNet )
{
ces.m_pNetworkable = pNet;
ces.m_iSerialNumber = pNet->GetIClientUnknown()->GetRefEHandle().GetSerialNumber();
ces.m_bDormant = pNet->IsDormant();
ces.m_pDataPointer = pNet->GetDataTableBasePtr();
m_PrevEntsAlive.Set( i );
}
}
}
void CLocalNetworkBackdoor::StopBackdoorMode()
{
ClearState();
}
|