Skip to content

Commit c9a3150

Browse files
committed
Use userID for tracking networking players
1 parent 17e373b commit c9a3150

File tree

7 files changed

+56
-40
lines changed

7 files changed

+56
-40
lines changed

src/engine/detours/baseclient_connect.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ void __fastcall DetourClientConnect(
221221
info.netChannel = pNetChannel;
222222
connectedClients.Insert( userID, info );
223223

224-
g_pNetworkManager->BindClientServer( entityIndex, pNetChannel );
224+
g_pNetworkManager->BindClient( userID, pNetChannel );
225225
}
226226

227227
#pragma warning( default : 4189 ) // Enable warning about unused variable

src/game/server/gameinterface.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,16 @@ void CServerGameDLL::DLLShutdown( void )
918918
DisconnectTier1Libraries();
919919
}
920920

921+
void CServerGameDLL::FireGameEvent( IGameEvent *event )
922+
{
923+
const char *type = event->GetName();
924+
925+
if ( Q_strcmp( type, "player_disconnect" ) == 0 )
926+
{
927+
g_pNetworkManager->UnbindClient( event->GetInt( "userid" ) );
928+
}
929+
}
930+
921931
bool CServerGameDLL::ReplayInit( CreateInterfaceFn fnReplayFactory )
922932
{
923933
#if defined( REPLAY_ENABLED )
@@ -967,6 +977,8 @@ float CServerGameDLL::GetTickInterval( void ) const
967977
// This is called when a new game is started. (restart, map)
968978
bool CServerGameDLL::GameInit( void )
969979
{
980+
ListenForGameEvent( "player_disconnect" );
981+
970982
ResetGlobalState();
971983
engine->ServerCommand( "exec game.cfg\n" );
972984
engine->ServerExecute();

src/game/server/gameinterface.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extern INetworkStringTable *g_pStringTableServerPopFiles;
3131
// Most of this is implemented in gameinterface.cpp, but some of it is per-mod in files like cs_gameinterface.cpp, etc.
3232
class CServerGameClients : public IServerGameClients
3333
{
34-
public:
34+
public:
3535
virtual bool ClientConnect( edict_t *pEntity, char const *pszName, char const *pszAddress, char *reject, int maxrejectlen ) OVERRIDE;
3636
virtual void ClientActive( edict_t *pEntity, bool bLoadGame ) OVERRIDE;
3737
virtual void ClientDisconnect( edict_t *pEntity ) OVERRIDE;
@@ -64,9 +64,9 @@ class CServerGameClients : public IServerGameClients
6464
virtual void ClientVoice( edict_t *pEdict ) OVERRIDE;
6565
};
6666

67-
class CServerGameDLL : public IServerGameDLL
67+
class CServerGameDLL : public IServerGameDLL, public CGameEventListener
6868
{
69-
public:
69+
public:
7070
virtual bool DLLInit( CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn fileSystemFactory, CGlobalVars *pGlobals ) OVERRIDE;
7171
virtual void DLLShutdown( void ) OVERRIDE;
7272
// Get the simulation interval (must be compiled with identical values into both client and game .dll for MOD!!!)
@@ -149,9 +149,9 @@ class CServerGameDLL : public IServerGameDLL
149149
size_t nMapFileSize ) OVERRIDE;
150150

151151
virtual ePrepareLevelResourcesResult AsyncPrepareLevelResources( /* in/out */ char *pszMapName, size_t nMapNameSize,
152-
/* in/out */ char *pszMapFile,
153-
size_t nMapFileSize,
154-
float *flProgress = NULL ) OVERRIDE;
152+
/* in/out */ char *pszMapFile,
153+
size_t nMapFileSize,
154+
float *flProgress = NULL ) OVERRIDE;
155155

156156
virtual eCanProvideLevelResult CanProvideLevel( /* in/out */ char *pMapName, int nMapNameMax ) OVERRIDE;
157157

@@ -160,12 +160,15 @@ class CServerGameDLL : public IServerGameDLL
160160

161161
virtual bool GetWorkshopMap( uint32 uIndex, WorkshopMapDesc_t *pDesc ) OVERRIDE;
162162

163-
private:
163+
private:
164164
// This can just be a wrapper on MapEntity_ParseAllEntities, but CS does some tricks in here
165165
// with the entity list.
166166
void LevelInit_ParseAllEntities( const char *pMapEntities );
167167
void LoadMessageOfTheDay();
168168
void LoadSpecificMOTDMsg( const ConVar &convar, const char *pszStringName );
169+
170+
public: // IGameEventListener:
171+
virtual void FireGameEvent( IGameEvent *event );
169172
};
170173

171174
// Normally, when the engine calls ClientPutInServer, it calls a global function in the game DLL
@@ -180,7 +183,7 @@ void ClientPutInServerOverride( ClientPutInServerOverrideFn fn );
180183
// These are created for map entities in order as the map entities are spawned.
181184
class CMapEntityRef
182185
{
183-
public:
186+
public:
184187
int m_iEdict; // Which edict slot this entity got. -1 if CreateEntityByName failed.
185188
int m_iSerialNumber; // The edict serial number. TODO used anywhere ?
186189
};
@@ -192,7 +195,7 @@ extern CUtlLinkedList< CMapEntityRef, unsigned short > g_MapEntityRefs;
192195
//-----------------------------------------------------------------------------
193196
class CMapLoadEntityFilter : public IMapEntityFilter
194197
{
195-
public:
198+
public:
196199
virtual bool ShouldCreateEntity( const char *pClassname )
197200
{
198201
// During map load, create all the entities.
@@ -223,7 +226,7 @@ bool IsEngineThreaded();
223226

224227
class CServerGameTags : public IServerGameTags
225228
{
226-
public:
229+
public:
227230
virtual void GetTaggedConVarList( KeyValues *pCvarTagList );
228231
};
229232
EXPOSE_SINGLE_INTERFACE( CServerGameTags, IServerGameTags, INTERFACEVERSION_SERVERGAMETAGS );

src/game/shared/experiment/util/lnetworks.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ LUA_BINDING_BEGIN( Networks, InternalSendToClients, "library", "Send a message t
3636

3737
if ( client )
3838
{
39-
g_pNetworkManager->SendServerToClientMessage( message, client->entindex() );
39+
g_pNetworkManager->SendServerToClientMessage( message, client->GetUserID() );
4040
}
4141
else
4242
{
@@ -52,7 +52,7 @@ LUA_BINDING_BEGIN( Networks, InternalSendToClients, "library", "Send a message t
5252

5353
if ( client )
5454
{
55-
g_pNetworkManager->SendServerToClientMessage( message, client->entindex() );
55+
g_pNetworkManager->SendServerToClientMessage( message, client->GetUserID() );
5656
}
5757
else
5858
{

src/game/shared/experiment/util/networkmanager.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,37 @@ static CNetworkManager s_NetworkManager;
1212
CNetworkManager *g_pNetworkManager = &s_NetworkManager;
1313

1414
#ifdef GAME_DLL
15-
bool CNetworkManager::BindClientServer( int playerIndex, INetChannel *netChannel )
15+
bool CNetworkManager::BindClient( int userId, INetChannel *netChannel )
1616
{
1717
if ( !netChannel )
1818
{
1919
return false;
2020
}
2121

22-
for ( int i = 0; i < m_ConnectedPlayers.Count(); i++ )
23-
{
24-
if ( m_ConnectedPlayers[i].playerIndex == playerIndex )
25-
{
26-
m_ConnectedPlayers[i].netChannel = netChannel;
27-
return true;
28-
}
29-
}
30-
3122
ConnectedPlayer_t player;
32-
player.playerIndex = playerIndex;
23+
player.userId = userId;
3324
player.netChannel = netChannel;
3425
m_ConnectedPlayers.AddToTail( player );
3526

3627
// Bind the handler that will get called on incoming messages
3728
CDynamicWriteNetworkMessage *netMessage = new CDynamicWriteNetworkMessage();
38-
netMessage->SetSender( playerIndex );
29+
netMessage->SetSender( userId );
3930
netChannel->RegisterMessage( netMessage );
4031

4132
return true;
4233
}
34+
35+
void CNetworkManager::UnbindClient( int userId )
36+
{
37+
for ( int i = 0; i < m_ConnectedPlayers.Count(); i++ )
38+
{
39+
if ( m_ConnectedPlayers[i].userId == userId )
40+
{
41+
m_ConnectedPlayers.Remove( i );
42+
break;
43+
}
44+
}
45+
}
4346
#else
4447
bool CNetworkManager::BindClient( INetChannel *netChannel )
4548
{
@@ -49,7 +52,7 @@ bool CNetworkManager::BindClient( INetChannel *netChannel )
4952
}
5053

5154
ConnectedPlayer_t player;
52-
player.playerIndex = -1;
55+
player.userId = -1;
5356
player.netChannel = netChannel;
5457
m_ConnectedPlayers.AddToTail( player );
5558

@@ -64,7 +67,7 @@ void CNetworkManager::UnbindClient()
6467
{
6568
for ( int i = 0; i < m_ConnectedPlayers.Count(); i++ )
6669
{
67-
if ( m_ConnectedPlayers[i].playerIndex == -1 )
70+
if ( m_ConnectedPlayers[i].userId == -1 )
6871
{
6972
m_ConnectedPlayers.Remove( i );
7073
return;
@@ -87,7 +90,7 @@ void CNetworkManager::SendClientToServerMessage( INetMessage *pMessage )
8790

8891
for ( int i = 0; i < m_ConnectedPlayers.Count(); i++ )
8992
{
90-
if ( m_ConnectedPlayers[i].playerIndex == -1 )
93+
if ( m_ConnectedPlayers[i].userId == -1 )
9194
{
9295
m_ConnectedPlayers[i].netChannel->SendNetMsg( *pMessage, true );
9396
return;
@@ -104,14 +107,11 @@ void CNetworkManager::BroadcastServerToClientsMessage( INetMessage *pMessage )
104107

105108
for ( int i = 0; i < m_ConnectedPlayers.Count(); i++ )
106109
{
107-
if ( m_ConnectedPlayers[i].playerIndex != -1 )
108-
{
109-
m_ConnectedPlayers[i].netChannel->SendNetMsg( *pMessage, true );
110-
}
110+
m_ConnectedPlayers[i].netChannel->SendNetMsg( *pMessage, true );
111111
}
112112
}
113113

114-
void CNetworkManager::SendServerToClientMessage( INetMessage *pMessage, int playerIndex )
114+
void CNetworkManager::SendServerToClientMessage( INetMessage *pMessage, int userId )
115115
{
116116
if ( !pMessage )
117117
{
@@ -120,7 +120,7 @@ void CNetworkManager::SendServerToClientMessage( INetMessage *pMessage, int play
120120

121121
for ( int i = 0; i < m_ConnectedPlayers.Count(); i++ )
122122
{
123-
if ( m_ConnectedPlayers[i].playerIndex == playerIndex )
123+
if ( m_ConnectedPlayers[i].userId == userId )
124124
{
125125
m_ConnectedPlayers[i].netChannel->SendNetMsg( *pMessage, true );
126126
return;

src/game/shared/experiment/util/networkmanager.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
struct ConnectedPlayer_t
2020
{
21-
int playerIndex;
21+
int userId;
2222
INetChannel *netChannel;
2323
};
2424

@@ -29,7 +29,8 @@ class CNetworkManager
2929
{
3030
public:
3131
#ifdef GAME_DLL
32-
virtual bool BindClientServer( int playerIndex, INetChannel *netChannel );
32+
virtual bool BindClient( int userId, INetChannel *netChannel );
33+
virtual void UnbindClient( int userId );
3334
#else
3435
virtual bool BindClient( INetChannel *netChannel );
3536
virtual void UnbindClient();
@@ -39,7 +40,7 @@ class CNetworkManager
3940

4041
virtual void SendClientToServerMessage( INetMessage *pMessage );
4142
virtual void BroadcastServerToClientsMessage( INetMessage *pMessage );
42-
virtual void SendServerToClientMessage( INetMessage *pMessage, int playerIndex );
43+
virtual void SendServerToClientMessage( INetMessage *pMessage, int userId );
4344

4445
private:
4546
CUtlVector< ConnectedPlayer_t > m_ConnectedPlayers;

src/game/shared/experiment/util/networkmessage.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ void CDynamicWriteNetworkMessage::SetBuffer( const char* buffer, int bufferLengt
7474
Q_memcpy( ( void* )m_pBuffer, buffer, bufferLength );
7575
}
7676

77-
void CDynamicWriteNetworkMessage::SetSender( int playerIndex )
77+
void CDynamicWriteNetworkMessage::SetSender( int userId )
7878
{
79-
m_iSender = playerIndex;
79+
m_iSender = userId;
8080
}
8181

8282
bool CDynamicWriteNetworkMessage::Process( void )
@@ -86,7 +86,7 @@ bool CDynamicWriteNetworkMessage::Process( void )
8686
#ifdef GAME_DLL
8787
if ( m_iSender > -1 )
8888
{
89-
player = UTIL_PlayerByIndex( m_iSender );
89+
player = UTIL_PlayerByUserId( m_iSender );
9090

9191
if ( !player )
9292
{

0 commit comments

Comments
 (0)