-
Notifications
You must be signed in to change notification settings - Fork 35
/
ServerBrowser.cpp
699 lines (578 loc) · 16.5 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
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/*
Copyright (C) 1997-2001 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "Framework.h"
#include "Bitmap.h"
#include "YesNoMessageBox.h"
#include "Table.h"
#include "keydefs.h"
#include "Switch.h"
#include "Field.h"
#include "utlvector.h"
#include "CheckBox.h"
#define ART_BANNER_INET "gfx/shell/head_inetgames"
#define ART_BANNER_LAN "gfx/shell/head_lan"
#define ART_BANNER_LOCK "gfx/shell/lock"
class CMenuServerBrowser;
enum
{
COLUMN_PASSWORD = 0,
COLUMN_NAME,
COLUMN_MAP,
COLUMN_PLAYERS,
COLUMN_PING,
COLUMN_IP
};
struct server_t
{
netadr_t adr;
char info[256];
float ping;
int numcl;
int maxcl;
char name[64];
char mapname[64];
char clientsstr[64];
char pingstr[64];
char ipstr[64];
bool havePassword;
bool isLegacy;
server_t( netadr_t adr, const char *info );
void UpdateData();
void SetPing( float ping );
int Rank( const server_t &other ) const
{
if( isLegacy > other.isLegacy ) return 100;
else if( isLegacy < other.isLegacy ) return -100;
return 0;
}
int NameCmp( const server_t &other ) const
{
return colorstricmp( name, other.name );
}
int AdrCmp( const server_t &other ) const
{
return EngFuncs::NET_CompareAdr( &adr, &other.adr );
}
int MapCmp( const server_t &other ) const
{
return stricmp( mapname, other.mapname );
}
int ClientCmp( const server_t &other ) const
{
if( numcl > other.numcl ) return 1;
else if( numcl < other.numcl ) return -1;
return 0;
}
int PingCmp( const server_t &other ) const
{
if( ping > other.ping ) return 1;
else if( ping < other.ping ) return -1;
return 0;
}
// make generic
// always rank new servers higher, even when sorting in reverse order
#define GENERATE_COMPAR_FN( method ) \
static int method ## Ascend( const void *a, const void *b ) \
{\
return (( const server_t *)a)->Rank( *(( const server_t *)b) ) + (( const server_t *)a)->method( *(( const server_t *)b) );\
}\
static int method ## Descend( const void *a, const void *b ) \
{\
return (( const server_t *)a)->Rank( *(( const server_t *)b) ) + (( const server_t *)b)->method( *(( const server_t *)a) );\
}\
GENERATE_COMPAR_FN( NameCmp )
GENERATE_COMPAR_FN( AdrCmp )
GENERATE_COMPAR_FN( MapCmp )
GENERATE_COMPAR_FN( ClientCmp )
GENERATE_COMPAR_FN( PingCmp )
#undef GENERATE_COMPAR_FN
};
class CMenuGameListModel : public CMenuBaseModel
{
public:
CMenuGameListModel( CMenuServerBrowser *parent ) : CMenuBaseModel(), parent( parent ), m_iSortingColumn(-1) {}
void Update() override;
int GetColumns() const override
{
return 6; // havePassword, game, mapname, maxcl, ping, (hidden)ip
}
int GetRows() const override
{
return servers.Count();
}
ECellType GetCellType( int line, int column ) override
{
if( column == 0 )
return CELL_IMAGE_ADDITIVE;
return CELL_TEXT;
}
const char *GetCellText( int line, int column ) override
{
switch( column )
{
case COLUMN_PASSWORD: return servers[line].havePassword ? ART_BANNER_LOCK : NULL;
case COLUMN_NAME: return servers[line].name;
case COLUMN_MAP: return servers[line].mapname;
case COLUMN_PLAYERS: return servers[line].clientsstr;
case COLUMN_PING: return servers[line].pingstr;
case COLUMN_IP: return servers[line].ipstr;
default: return NULL;
}
}
bool GetCellColors( int line, int column, unsigned int &textColor, bool &force) const override
{
if( servers[line].isLegacy )
{
CColor color = uiPromptTextColor;
color.a = color.a * 0.5;
textColor = color;
// allow colorstrings only in server name
force = column != COLUMN_NAME;
return true;
}
return false;
}
void OnActivateEntry( int line ) override;
void Flush()
{
servers.RemoveAll();
serversRefreshTime = gpGlobals->time;
}
bool IsHavePassword( int line )
{
return servers[line].havePassword;
}
void AddServerToList( netadr_t adr, const char *info );
bool Sort( int column, bool ascend ) override;
float serversRefreshTime;
CUtlVector<server_t> servers;
private:
CMenuServerBrowser *parent;
int m_iSortingColumn;
bool m_bAscend;
};
class CMenuServerBrowser: public CMenuFramework
{
public:
CMenuServerBrowser() : CMenuFramework( "CMenuServerBrowser" ), gameListModel( this ) { }
void Draw() override;
void Show() override;
bool KeyUp( int key ) override;
void SetLANOnly( bool lanOnly )
{
m_bLanOnly = lanOnly;
}
void GetGamesList( void );
void ClearList( void );
void RefreshList( void );
void JoinGame( void );
void ResetPing( void )
{
gameListModel.serversRefreshTime = EngFuncs::DoubleTime();
}
void AddServerToList( netadr_t adr, const char *info );
static void Connect( server_t &server );
CMenuPicButton *joinGame;
CMenuPicButton *createGame;
CMenuPicButton *refresh;
CMenuSwitch natOrDirect;
CMenuYesNoMessageBox msgBox;
CMenuTable gameList;
CMenuGameListModel gameListModel;
CMenuCheckBox showip;
CMenuYesNoMessageBox askPassword;
CMenuField password;
int refreshTime;
int refreshTime2;
bool m_bLanOnly;
private:
void _Init() override;
void _VidInit() override;
};
static server_t staticServerSelect( netadr_t(), "" );
static bool staticWaitingPassword = false;
ADD_MENU3( menu_internetgames, CMenuServerBrowser, UI_InternetGames_Menu );
ADD_MENU4( menu_langame, NULL, UI_LanGame_Menu, NULL );
/*
=================
CMenuServerBrowser::Menu
=================
*/
void UI_ServerBrowser_Menu( void )
{
if ( gMenu.m_gameinfo.gamemode == GAME_SINGLEPLAYER_ONLY )
return;
// stop demos to allow network sockets to open
if ( gpGlobals->demoplayback && EngFuncs::GetCvarFloat( "cl_background" ))
{
uiStatic.m_iOldMenuDepth = uiStatic.menu.Count();
EngFuncs::ClientCmd( FALSE, "stop\n" );
uiStatic.m_fDemosPlayed = true;
}
menu_internetgames->Show();
}
void UI_InternetGames_Menu( void )
{
menu_internetgames->SetLANOnly( false );
UI_ServerBrowser_Menu();
}
void UI_LanGame_Menu( void )
{
menu_internetgames->SetLANOnly( true );
UI_ServerBrowser_Menu();
}
server_t::server_t( netadr_t adr, const char *info ) :
adr( adr )
{
Q_strncpy( this->info, info, sizeof( this->info ));
}
void server_t::UpdateData( void )
{
Q_strncpy( name, Info_ValueForKey( info, "host" ), sizeof( name ));
Q_strncpy( mapname, Info_ValueForKey( info, "map" ), sizeof( mapname ));
Q_strncpy( ipstr, EngFuncs::NET_AdrToString( adr ), sizeof( ipstr ));
numcl = atoi( Info_ValueForKey( info, "numcl" ));
maxcl = atoi( Info_ValueForKey( info, "maxcl" ));
snprintf( clientsstr, sizeof( clientsstr ), "%d\\%d", numcl, maxcl );
havePassword = !strcmp( Info_ValueForKey( info, "password" ), "1" );
isLegacy = !strcmp( Info_ValueForKey( info, "legacy" ), "1" );
}
void server_t::SetPing( float ping )
{
ping = bound( 0.0f, ping, 9.999f );
if( isLegacy )
ping /= 2;
this->ping = ping;
snprintf( pingstr, sizeof( pingstr ), "%.f ms", ping * 1000 );
}
bool CMenuGameListModel::Sort(int column, bool ascend)
{
m_iSortingColumn = column;
if( column == -1 )
return false; // disabled
m_bAscend = ascend;
switch( column )
{
case COLUMN_PASSWORD:
return false;
case COLUMN_NAME:
qsort( servers.Base(), servers.Count(), sizeof( server_t ),
ascend ? server_t::NameCmpAscend : server_t::NameCmpDescend );
return true;
case COLUMN_MAP:
qsort( servers.Base(), servers.Count(), sizeof( server_t ),
ascend ? server_t::MapCmpAscend : server_t::MapCmpDescend );
return true;
case COLUMN_PLAYERS:
qsort( servers.Base(), servers.Count(), sizeof( server_t ),
ascend ? server_t::ClientCmpAscend : server_t::ClientCmpDescend );
return true;
case COLUMN_PING:
qsort( servers.Base(), servers.Count(), sizeof( server_t ),
ascend ? server_t::PingCmpAscend : server_t::PingCmpDescend );
return true;
case COLUMN_IP:
qsort( servers.Base(), servers.Count(), sizeof( server_t ),
ascend ? server_t::AdrCmpAscend : server_t::AdrCmpDescend );
return true;
}
return false;
}
/*
=================
CMenuServerBrowser::GetGamesList
=================
*/
void CMenuGameListModel::Update( void )
{
int i;
const char *info;
// regenerate table data
for( i = 0; i < servers.Count(); i++ )
servers[i].UpdateData();
if( servers.Count() )
{
parent->joinGame->SetGrayed( false );
if( m_iSortingColumn != -1 )
Sort( m_iSortingColumn, m_bAscend );
}
}
void CMenuGameListModel::OnActivateEntry( int line )
{
CMenuServerBrowser::Connect( servers[line] );
}
void CMenuGameListModel::AddServerToList( netadr_t adr, const char *info )
{
int i;
// ignore if duplicated
for( i = 0; i < servers.Count(); i++ )
{
if( !EngFuncs::NET_CompareAdr( &servers[i].adr, &adr ))
return;
if( !stricmp( servers[i].info, info ))
return;
}
server_t server( adr, info );
server.UpdateData();
server.SetPing( EngFuncs::DoubleTime() - serversRefreshTime );
servers.AddToTail( server );
if( m_iSortingColumn != -1 )
Sort( m_iSortingColumn, m_bAscend );
}
void CMenuServerBrowser::Connect( server_t &server )
{
char buf[256];
// prevent refresh during connect
menu_internetgames->refreshTime = uiStatic.realTime + 999999;
// ask user for password
if( server.havePassword )
{
// if dialog window is still open, then user have entered the password
if( !staticWaitingPassword )
{
// save current select
staticServerSelect = server;
staticWaitingPassword = true;
// show password request window
menu_internetgames->askPassword.Show();
return;
}
}
else
{
// remove password, as server don't require it
EngFuncs::CvarSetString( "password", "" );
}
staticWaitingPassword = false;
snprintf( buf, sizeof( buf ), "connect %s %s",
EngFuncs::NET_AdrToString( server.adr ),
server.isLegacy ? "legacy" : "" );
buf[sizeof( buf ) - 1] = 0;
EngFuncs::ClientCmd( FALSE, buf );
UI_ConnectionProgress_Connect( "" );
}
/*
=================
CMenuServerBrowser::JoinGame
=================
*/
void CMenuServerBrowser::JoinGame()
{
gameListModel.OnActivateEntry( gameList.GetCurrentIndex() );
}
void CMenuServerBrowser::ClearList()
{
gameListModel.Flush();
joinGame->SetGrayed( true );
}
void CMenuServerBrowser::RefreshList()
{
ClearList();
if( m_bLanOnly )
{
EngFuncs::ClientCmd( FALSE, "localservers\n" );
}
else
{
if( uiStatic.realTime > refreshTime2 )
{
EngFuncs::ClientCmd( FALSE, "internetservers\n" );
refreshTime2 = uiStatic.realTime + (EngFuncs::GetCvarFloat("cl_nat") ? 4000:1000);
refresh->SetGrayed( true );
if( uiStatic.realTime + 20000 < refreshTime )
refreshTime = uiStatic.realTime + 20000;
}
}
}
/*
=================
UI_Background_Ownerdraw
=================
*/
void CMenuServerBrowser::Draw( void )
{
CMenuFramework::Draw();
if( uiStatic.realTime > refreshTime )
{
RefreshList();
refreshTime = uiStatic.realTime + 20000; // refresh every 10 secs
}
if( uiStatic.realTime > refreshTime2 )
{
refresh->SetGrayed( false );
}
}
bool CMenuServerBrowser::KeyUp( int key )
{
if( key == 'i' )
{
gameList.SetColumnWidth( COLUMN_IP, 300, true );
gameList.VidInit();
gameListModel.Update();
}
return CMenuFramework::KeyUp( key );
}
/*
=================
CMenuServerBrowser::Init
=================
*/
void CMenuServerBrowser::_Init( void )
{
AddItem( banner );
joinGame = AddButton( L( "Join game" ), L( "Join to selected game" ), PC_JOIN_GAME,
VoidCb( &CMenuServerBrowser::JoinGame ), QMF_GRAYED );
joinGame->onReleasedClActive = msgBox.MakeOpenEvent();
createGame = AddButton( L( "GameUI_GameMenu_CreateServer" ), NULL, PC_CREATE_GAME );
SET_EVENT_MULTI( createGame->onReleased,
{
if( ((CMenuServerBrowser*)pSelf->Parent())->m_bLanOnly )
EngFuncs::CvarSetValue( "public", 0.0f );
else EngFuncs::CvarSetValue( "public", 1.0f );
UI_CreateGame_Menu();
});
// TODO: implement!
AddButton( L( "View game info" ), L( "Get detail game info" ), PC_VIEW_GAME_INFO, CEventCallback::NoopCb, QMF_GRAYED );
refresh = AddButton( L( "Refresh" ), L( "Refresh servers list" ), PC_REFRESH, VoidCb( &CMenuServerBrowser::RefreshList ) );
AddButton( L( "Done" ), L( "Return to main menu" ), PC_DONE, VoidCb( &CMenuServerBrowser::Hide ) );
msgBox.SetMessage( L( "Join a network game will exit any current game, OK to exit?" ) );
msgBox.SetPositiveButton( L( "GameUI_OK" ), PC_OK );
msgBox.HighlightChoice( CMenuYesNoMessageBox::HIGHLIGHT_YES );
msgBox.onPositive = VoidCb( &CMenuServerBrowser::JoinGame );
msgBox.Link( this );
gameList.SetCharSize( QM_SMALLFONT );
gameList.SetupColumn( COLUMN_PASSWORD, NULL, 32.0f, true );
gameList.SetupColumn( COLUMN_NAME, L( "Name" ), 0.40f );
gameList.SetupColumn( COLUMN_MAP, L( "GameUI_Map" ), 0.25f );
gameList.SetupColumn( COLUMN_PLAYERS, L( "Players" ), 100.0f, true );
gameList.SetupColumn( COLUMN_PING, L( "Ping" ), 120.0f, true );
gameList.SetupColumn( COLUMN_IP, L( "IP" ), 0, true );
gameList.SetModel( &gameListModel );
gameList.bFramedHintText = true;
gameList.bAllowSorting = true;
natOrDirect.AddSwitch( L( "Direct" ) );
natOrDirect.AddSwitch( "NAT" );
natOrDirect.eTextAlignment = QM_CENTER;
natOrDirect.bMouseToggle = false;
natOrDirect.LinkCvar( "cl_nat" );
natOrDirect.iSelectColor = uiInputFgColor;
// bit darker
natOrDirect.iFgTextColor = uiInputFgColor - 0x00151515;
SET_EVENT_MULTI( natOrDirect.onChanged,
{
CMenuSwitch *self = (CMenuSwitch*)pSelf;
CMenuServerBrowser *parent = (CMenuServerBrowser*)self->Parent();
self->WriteCvar();
parent->ClearList();
parent->RefreshList();
});
// server.dll needs for reading savefiles or startup newgame
if( !EngFuncs::CheckGameDll( ))
createGame->SetGrayed( true ); // server.dll is missed - remote servers only
password.bHideInput = true;
password.bAllowColorstrings = false;
password.bNumbersOnly = false;
password.szName = L( "GameUI_Password" );
password.iMaxLength = 16;
password.SetRect( 188, 140, 270, 32 );
SET_EVENT_MULTI( askPassword.onPositive,
{
CMenuServerBrowser *parent = (CMenuServerBrowser*)pSelf->Parent();
EngFuncs::CvarSetString( "password", parent->password.GetBuffer() );
parent->password.Clear(); // we don't need entered password anymore
CMenuServerBrowser::Connect( staticServerSelect );
});
SET_EVENT_MULTI( askPassword.onNegative,
{
CMenuServerBrowser *parent = (CMenuServerBrowser*)pSelf->Parent();
EngFuncs::CvarSetString( "password", "" );
parent->password.Clear(); // we don't need entered password anymore
staticWaitingPassword = false;
});
askPassword.SetMessage( L( "GameUI_PasswordPrompt" ) );
askPassword.Link( this );
askPassword.Init();
askPassword.AddItem( password );
AddItem( gameList );
AddItem( natOrDirect );
}
/*
=================
CMenuServerBrowser::VidInit
=================
*/
void CMenuServerBrowser::_VidInit()
{
if( m_bLanOnly )
{
banner.SetPicture( ART_BANNER_LAN );
createGame->szStatusText = ( L( "Create new LAN game" ) );
natOrDirect.Hide();
}
else
{
banner.SetPicture( ART_BANNER_INET );
createGame->szStatusText = ( L( "Create new Internet game" ) );
natOrDirect.Show();
}
gameList.SetRect( 360, 230, -20, 465 );
natOrDirect.SetCoord( -20 - natOrDirect.size.w, gameList.pos.y - UI_OUTLINE_WIDTH - natOrDirect.size.h );
refreshTime = uiStatic.realTime + 500; // delay before update 0.5 sec
refreshTime2 = uiStatic.realTime + 500;
}
void CMenuServerBrowser::Show()
{
CMenuFramework::Show();
// clear out server table
staticWaitingPassword = false;
gameListModel.Flush();
gameList.SetSortingColumn( COLUMN_PING );
joinGame->SetGrayed( true );
}
void CMenuServerBrowser::AddServerToList( netadr_t adr, const char *info )
{
#ifndef XASH_ALL_SERVERS
if( stricmp( gMenu.m_gameinfo.gamefolder, Info_ValueForKey( info, "gamedir" )) != 0 )
return;
#endif
if( !WasInit() )
return;
if( !IsVisible() )
return;
gameListModel.AddServerToList( adr, info );
joinGame->SetGrayed( false );
}
/*
=================
UI_AddServerToList
=================
*/
void UI_AddServerToList( netadr_t adr, const char *info )
{
if( !menu_internetgames )
return;
menu_internetgames->AddServerToList( adr, info );
}
/*
=================
UI_MenuResetPing_f
=================
*/
void UI_MenuResetPing_f( void )
{
if( menu_internetgames )
menu_internetgames->ResetPing();
}
ADD_COMMAND( menu_resetping, UI_MenuResetPing_f );