forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerBrowser.cpp
493 lines (408 loc) · 15 KB
/
ServerBrowser.cpp
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================
#include "pch_serverbrowser.h"
// expose the server browser interfaces
CServerBrowser g_ServerBrowserSingleton;
EXPOSE_SINGLE_INTERFACE_GLOBALVAR(CServerBrowser, IServerBrowser, SERVERBROWSER_INTERFACE_VERSION, g_ServerBrowserSingleton);
EXPOSE_SINGLE_INTERFACE_GLOBALVAR(CServerBrowser, IVGuiModule, "VGuiModuleServerBrowser001", g_ServerBrowserSingleton); // the interface loaded by PlatformMenu.vdf
// singleton accessor
CServerBrowser &ServerBrowser()
{
return g_ServerBrowserSingleton;
}
IRunGameEngine *g_pRunGameEngine = NULL;
IServersInfo *g_pServersInfo = NULL;
static CSteamAPIContext g_SteamAPIContext;
CSteamAPIContext *steamapicontext = &g_SteamAPIContext;
IEngineReplay *g_pEngineReplay = NULL;
ConVar sb_firstopentime( "sb_firstopentime", "0", FCVAR_DEVELOPMENTONLY, "Indicates the time the server browser was first opened." );
ConVar sb_numtimesopened( "sb_numtimesopened", "0", FCVAR_DEVELOPMENTONLY, "Indicates the number of times the server browser was opened this session." );
// the original author of this code felt strdup was not acceptible.
inline char *CloneString( const char *str )
{
char *cloneStr = new char [ strlen(str)+1 ];
strcpy( cloneStr, str );
return cloneStr;
}
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CServerBrowser::CServerBrowser()
{
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CServerBrowser::~CServerBrowser()
{
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CServerBrowser::CreateDialog()
{
if (!m_hInternetDlg.Get())
{
m_hInternetDlg = new CServerBrowserDialog(NULL); // SetParent() call below fills this in
m_hInternetDlg->Initialize();
}
}
//-----------------------------------------------------------------------------
// Purpose: links to vgui and engine interfaces
//-----------------------------------------------------------------------------
bool CServerBrowser::Initialize(CreateInterfaceFn *factorylist, int factoryCount)
{
ConnectTier1Libraries( factorylist, factoryCount );
ConVar_Register();
ConnectTier2Libraries( factorylist, factoryCount );
ConnectTier3Libraries( factorylist, factoryCount );
g_pRunGameEngine = NULL;
for ( int i = 0; i < factoryCount; ++i )
{
if ( !g_pEngineReplay )
g_pEngineReplay = ( IEngineReplay * )factorylist[ i ]( ENGINE_REPLAY_INTERFACE_VERSION, NULL );
if( !g_pServersInfo )
g_pServersInfo = ( IServersInfo * )factorylist[ i ]( SERVERLIST_INTERFACE_VERSION, NULL );
}
SteamAPI_InitSafe();
SteamAPI_SetTryCatchCallbacks( false ); // We don't use exceptions, so tell steam not to use try/catch in callback handlers
steamapicontext->Init();
for (int i = 0; i < factoryCount; i++)
{
if (!g_pRunGameEngine)
{
g_pRunGameEngine = (IRunGameEngine *)(factorylist[i])(RUNGAMEENGINE_INTERFACE_VERSION, NULL);
}
}
// load the vgui interfaces
#if defined( STEAM ) || defined( HL1 )
if ( !vgui::VGuiControls_Init("ServerBrowser", factorylist, factoryCount) )
#else
if ( !vgui::VGui_InitInterfacesList("ServerBrowser", factorylist, factoryCount) )
#endif
return false;
// load localization file
g_pVGuiLocalize->AddFile( "servers/serverbrowser_%language%.txt" );
return true;
}
//-----------------------------------------------------------------------------
// Purpose: links to other modules interfaces (tracker)
//-----------------------------------------------------------------------------
bool CServerBrowser::PostInitialize(CreateInterfaceFn *modules, int factoryCount)
{
// find the interfaces we need
for (int i = 0; i < factoryCount; i++)
{
if (!g_pRunGameEngine)
{
g_pRunGameEngine = (IRunGameEngine *)(modules[i])(RUNGAMEENGINE_INTERFACE_VERSION, NULL);
}
}
CreateDialog();
m_hInternetDlg->SetVisible(false);
return g_pRunGameEngine;
}
//-----------------------------------------------------------------------------
// Purpose: true if the user can't play a game due to VAC banning
//-----------------------------------------------------------------------------
bool CServerBrowser::IsVACBannedFromGame( int nAppID )
{
return false;
}
//-----------------------------------------------------------------------------
// Purpose: Marks that the tool/game loading us intends to feed us workshop information
//-----------------------------------------------------------------------------
void CServerBrowser::SetWorkshopEnabled( bool bManaged )
{
m_bWorkshopEnabled = bManaged;
}
//-----------------------------------------------------------------------------
// Purpose: Add a mapname to our known user-subscribed workshop maps list
//-----------------------------------------------------------------------------
void CServerBrowser::AddWorkshopSubscribedMap( const char *pszMapName )
{
CUtlString strMap( pszMapName );
if ( !IsWorkshopSubscribedMap( strMap ) )
{
m_vecWorkshopSubscribedMaps.AddToTail( strMap );
}
}
//-----------------------------------------------------------------------------
// Purpose: remove a mapname to our known user-subscribed workshop maps list
//-----------------------------------------------------------------------------
void CServerBrowser::RemoveWorkshopSubscribedMap( const char *pszMapName )
{
m_vecWorkshopSubscribedMaps.FindAndFastRemove( CUtlString( pszMapName ) );
}
//-----------------------------------------------------------------------------
// Purpose: Well, is it?
//-----------------------------------------------------------------------------
bool CServerBrowser::IsWorkshopEnabled()
{
return m_bWorkshopEnabled;
}
//-----------------------------------------------------------------------------
// Purpose: Check if this map is in our subscribed list
//-----------------------------------------------------------------------------
bool CServerBrowser::IsWorkshopSubscribedMap( const char *pszMapName )
{
return m_vecWorkshopSubscribedMaps.HasElement( CUtlString( pszMapName ) );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CServerBrowser::IsValid()
{
return ( g_pRunGameEngine );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CServerBrowser::Activate()
{
static bool firstTimeOpening = true;
if ( firstTimeOpening )
{
m_hInternetDlg->LoadUserData(); // reload the user data the first time the dialog is made visible, helps with the lag between module load and
// steamui getting Deactivate() call
firstTimeOpening = false;
}
int numTimesOpened = sb_numtimesopened.GetInt() + 1;
sb_numtimesopened.SetValue( numTimesOpened );
if ( numTimesOpened == 1 )
{
time_t aclock;
time( &aclock );
sb_firstopentime.SetValue( (int) aclock );
}
Open();
return true;
}
//-----------------------------------------------------------------------------
// Purpose: called when the server browser gets used in the game
//-----------------------------------------------------------------------------
void CServerBrowser::Deactivate()
{
if (m_hInternetDlg.Get())
{
m_hInternetDlg->SaveUserData();
}
}
//-----------------------------------------------------------------------------
// Purpose: called when the server browser is no longer being used in the game
//-----------------------------------------------------------------------------
void CServerBrowser::Reactivate()
{
if (m_hInternetDlg.Get())
{
m_hInternetDlg->LoadUserData();
if (m_hInternetDlg->IsVisible())
{
m_hInternetDlg->RefreshCurrentPage();
}
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CServerBrowser::Open()
{
m_hInternetDlg->Open();
}
//-----------------------------------------------------------------------------
// Purpose: returns direct handle to main server browser dialog
//-----------------------------------------------------------------------------
vgui::VPANEL CServerBrowser::GetPanel()
{
return m_hInternetDlg.Get() ? m_hInternetDlg->GetVPanel() : NULL;
}
//-----------------------------------------------------------------------------
// Purpose: sets the parent panel of the main module panel
//-----------------------------------------------------------------------------
void CServerBrowser::SetParent(vgui::VPANEL parent)
{
if (m_hInternetDlg.Get())
{
m_hInternetDlg->SetParent(parent);
}
}
//-----------------------------------------------------------------------------
// Purpose: Closes down the server browser for good
//-----------------------------------------------------------------------------
void CServerBrowser::Shutdown()
{
if (m_hInternetDlg.Get())
{
m_hInternetDlg->Close();
m_hInternetDlg->MarkForDeletion();
}
#if defined( STEAM )
vgui::VGuiControls_Shutdown();
#endif
DisconnectTier3Libraries();
DisconnectTier2Libraries();
ConVar_Unregister();
DisconnectTier1Libraries();
}
//-----------------------------------------------------------------------------
// Purpose: opens a game info dialog to watch the specified server; associated with the friend 'userName'
//-----------------------------------------------------------------------------
bool CServerBrowser::OpenGameInfoDialog( uint64 ulSteamIDFriend, const char *pszConnectCode )
{
#if 0
if ( m_hInternetDlg.Get() )
{
// activate an already-existing dialog
CDialogGameInfo *pDialogGameInfo = m_hInternetDlg->GetDialogGameInfoForFriend( ulSteamIDFriend );
if ( pDialogGameInfo )
{
pDialogGameInfo->Activate();
return true;
}
// none yet, create a new dialog
FriendGameInfo_t friendGameInfo;
if ( steamapicontext->SteamFriends()->GetFriendGamePlayed( ulSteamIDFriend, &friendGameInfo ) )
{
uint16 usConnPort = friendGameInfo.m_usGamePort;
if ( friendGameInfo.m_usQueryPort < QUERY_PORT_ERROR )
usConnPort = friendGameInfo.m_usQueryPort;
CDialogGameInfo *pDialogGameInfo = m_hInternetDlg->OpenGameInfoDialog( friendGameInfo.m_unGameIP, friendGameInfo.m_usGamePort, usConnPort, pszConnectCode );
pDialogGameInfo->SetFriend( ulSteamIDFriend );
return true;
}
}
#endif
return false;
}
//-----------------------------------------------------------------------------
// Purpose: joins a specified game - game info dialog will only be opened if the server is fully or passworded
//-----------------------------------------------------------------------------
bool CServerBrowser::JoinGame( uint64 ulSteamIDFriend, const char *pszConnectCode )
{
/* if ( OpenGameInfoDialog( ulSteamIDFriend, pszConnectCode ) )
{
CDialogGameInfo *pDialogGameInfo = m_hInternetDlg->GetDialogGameInfoForFriend( ulSteamIDFriend );
pDialogGameInfo->Connect();
}*/
return false;
}
//-----------------------------------------------------------------------------
// Purpose: joins a game by IP/Port
//-----------------------------------------------------------------------------
bool CServerBrowser::JoinGame( uint32 unGameIP, uint16 usGamePort, const char *pszConnectCode )
{
m_hInternetDlg->JoinGame( unGameIP, usGamePort, pszConnectCode );
return true;
}
//-----------------------------------------------------------------------------
// Purpose: forces the game info dialog closed
//-----------------------------------------------------------------------------
void CServerBrowser::CloseGameInfoDialog( uint64 ulSteamIDFriend )
{
#if 0
CDialogGameInfo *pDialogGameInfo = m_hInternetDlg->GetDialogGameInfoForFriend( ulSteamIDFriend );
if ( pDialogGameInfo )
{
pDialogGameInfo->Close();
}
#endif
}
//-----------------------------------------------------------------------------
// Purpose: closes all the game info dialogs
//-----------------------------------------------------------------------------
void CServerBrowser::CloseAllGameInfoDialogs()
{
if ( m_hInternetDlg.Get() )
{
m_hInternetDlg->CloseAllGameInfoDialogs();
}
}
CUtlVector< gametypes_t > g_GameTypes;
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void LoadGameTypes( void )
{
if ( g_GameTypes.Count() > 0 )
return;
#define GAMETYPES_FILE "servers/ServerBrowserGameTypes.txt"
KeyValues * kv = new KeyValues( GAMETYPES_FILE );
if ( !kv->LoadFromFile( g_pFullFileSystem, GAMETYPES_FILE, "MOD" ) )
{
kv->deleteThis();
return;
}
g_GameTypes.RemoveAll();
for ( KeyValues *pData = kv->GetFirstSubKey(); pData != NULL; pData = pData->GetNextKey() )
{
gametypes_t gametype;
gametype.pPrefix = CloneString( pData->GetString( "prefix", "" ) );
gametype.pGametypeName = CloneString( pData->GetString( "name", "" ) );
g_GameTypes.AddToTail( gametype );
}
kv->deleteThis();
}
const char *GetGameTypeName( const char *pMapName )
{
LoadGameTypes();
for ( int i = 0; i < g_GameTypes.Count(); i++ )
{
int iLength = strlen( g_GameTypes[i].pPrefix );
if ( !Q_strncmp( pMapName, g_GameTypes[i].pPrefix, iLength ) )
{
return g_GameTypes[i].pGametypeName;
}
}
return "";
}
//-----------------------------------------------------------------------------
// Purpose of comments like these: none
//-----------------------------------------------------------------------------
const char *CServerBrowser::GetMapFriendlyNameAndGameType( const char *pszMapName, char *szFriendlyMapName, int cchFriendlyName )
{
// Make sure game types are loaded
LoadGameTypes();
// Scan list
const char *pszFriendlyGameTypeName = "";
for ( int i = 0; i < g_GameTypes.Count(); i++ )
{
int iLength = strlen( g_GameTypes[i].pPrefix );
if ( !Q_strnicmp( pszMapName, g_GameTypes[i].pPrefix, iLength ) )
{
pszMapName += iLength;
pszFriendlyGameTypeName = g_GameTypes[i].pGametypeName;
break;
}
}
// See how many characters from the name to copy.
// Start by assuming we'll copy the whole thing.
// (After any prefix we just skipped)
int l = V_strlen( pszMapName );
const char *pszFinal = Q_stristr( pszMapName, "_final" );
if ( pszFinal )
{
// truncate the _final (or _final1) part of the filename if it's at the end of the name
const char *pszNextChar = pszFinal + Q_strlen( "_final" );
if ( ( *pszNextChar == '\0' ) ||
( ( *pszNextChar == '1' ) && ( *(pszNextChar+1) == '\0' ) ) )
{
l = pszFinal - pszMapName;
}
}
// Safety check against buffer size
if ( l >= cchFriendlyName )
{
Assert( !"Map name too long for buffer!" );
l = cchFriendlyName-1;
}
// Copy friendly portion of name only
V_memcpy( szFriendlyMapName, pszMapName, l );
// It's like the Alamo. We never forget.
szFriendlyMapName[l] = '\0';
// Result should be the friendly game type name
return pszFriendlyGameTypeName;
}