Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

336 lines
10 KiB

  1. //========= Copyright � 1996-2001, Valve LLC, All rights reserved. ============
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #include "pch_serverbrowser.h"
  8. #include <GameUI/IGameUI.h>
  9. #include "cdll_int.h"
  10. // expose the server browser interfaces
  11. CServerBrowser g_ServerBrowserSingleton;
  12. EXPOSE_SINGLE_INTERFACE_GLOBALVAR(CServerBrowser, IServerBrowser, SERVERBROWSER_INTERFACE_VERSION, g_ServerBrowserSingleton);
  13. EXPOSE_SINGLE_INTERFACE_GLOBALVAR(CServerBrowser, IVGuiModule, "VGuiModuleServerBrowser001", g_ServerBrowserSingleton); // the interface loaded by PlatformMenu.vdf
  14. // singleton accessor
  15. CServerBrowser &ServerBrowser()
  16. {
  17. return g_ServerBrowserSingleton;
  18. }
  19. IRunGameEngine *g_pRunGameEngine = NULL;
  20. IGameUI *pGameUI = NULL;
  21. IVEngineClient *engine = NULL;
  22. static CSteamAPIContext g_SteamAPIContext;
  23. CSteamAPIContext *steamapicontext = &g_SteamAPIContext;
  24. ConVar sb_firstopentime( "sb_firstopentime", "0", FCVAR_DEVELOPMENTONLY, "Indicates the time the server browser was first opened." );
  25. ConVar sb_numtimesopened( "sb_numtimesopened", "0", FCVAR_DEVELOPMENTONLY, "Indicates the number of times the server browser was opened this session." );
  26. //-----------------------------------------------------------------------------
  27. // Purpose: Constructor
  28. //-----------------------------------------------------------------------------
  29. CServerBrowser::CServerBrowser()
  30. {
  31. }
  32. //-----------------------------------------------------------------------------
  33. // Purpose: Destructor
  34. //-----------------------------------------------------------------------------
  35. CServerBrowser::~CServerBrowser()
  36. {
  37. }
  38. //-----------------------------------------------------------------------------
  39. // Purpose:
  40. //-----------------------------------------------------------------------------
  41. void CServerBrowser::CreateDialog()
  42. {
  43. if (!m_hInternetDlg.Get())
  44. {
  45. m_hInternetDlg = new CServerBrowserDialog(NULL); // SetParent() call below fills this in
  46. m_hInternetDlg->Initialize();
  47. }
  48. }
  49. //-----------------------------------------------------------------------------
  50. // Purpose: links to vgui and engine interfaces
  51. //-----------------------------------------------------------------------------
  52. bool CServerBrowser::Initialize(CreateInterfaceFn *factorylist, int factoryCount)
  53. {
  54. ConnectTier1Libraries( factorylist, factoryCount );
  55. ConVar_Register();
  56. ConnectTier2Libraries( factorylist, factoryCount );
  57. ConnectTier3Libraries( factorylist, factoryCount );
  58. g_pRunGameEngine = NULL;
  59. SteamAPI_InitSafe();
  60. SteamAPI_SetTryCatchCallbacks( false ); // We don't use exceptions, so tell steam not to use try/catch in callback handlers
  61. steamapicontext->Init();
  62. for (int i = 0; i < factoryCount; i++)
  63. {
  64. if (!g_pRunGameEngine)
  65. {
  66. g_pRunGameEngine = (IRunGameEngine *)(factorylist[i])(RUNGAMEENGINE_INTERFACE_VERSION, NULL);
  67. }
  68. if ( !pGameUI )
  69. {
  70. pGameUI = ( IGameUI * )( factorylist[i] )( GAMEUI_INTERFACE_VERSION, NULL );
  71. }
  72. if ( !engine )
  73. {
  74. engine = (IVEngineClient *)( factorylist[i] )( VENGINE_CLIENT_INTERFACE_VERSION, NULL );
  75. }
  76. }
  77. // load the vgui interfaces
  78. #if defined( STEAM ) || defined( HL1 )
  79. if ( !vgui::VGuiControls_Init("ServerBrowser", factorylist, factoryCount) )
  80. #else
  81. if ( !vgui::VGui_InitInterfacesList("ServerBrowser", factorylist, factoryCount) )
  82. #endif
  83. return false;
  84. // load localization file
  85. g_pVGuiLocalize->AddFile( "servers/serverbrowser_%language%.txt" );
  86. return true;
  87. }
  88. //-----------------------------------------------------------------------------
  89. // Purpose: links to other modules interfaces (tracker)
  90. //-----------------------------------------------------------------------------
  91. bool CServerBrowser::PostInitialize(CreateInterfaceFn *modules, int factoryCount)
  92. {
  93. // find the interfaces we need
  94. for (int i = 0; i < factoryCount; i++)
  95. {
  96. if (!g_pRunGameEngine)
  97. {
  98. g_pRunGameEngine = (IRunGameEngine *)(modules[i])(RUNGAMEENGINE_INTERFACE_VERSION, NULL);
  99. }
  100. }
  101. CreateDialog();
  102. m_hInternetDlg->SetVisible(false);
  103. return g_pRunGameEngine ? true : false;
  104. }
  105. //-----------------------------------------------------------------------------
  106. // Purpose: true if the user can't play a game due to VAC banning
  107. //-----------------------------------------------------------------------------
  108. bool CServerBrowser::IsVACBannedFromGame( int nAppID )
  109. {
  110. return false;
  111. }
  112. //-----------------------------------------------------------------------------
  113. // Purpose:
  114. //-----------------------------------------------------------------------------
  115. bool CServerBrowser::IsValid()
  116. {
  117. return ( g_pRunGameEngine ? true : false );
  118. }
  119. //-----------------------------------------------------------------------------
  120. // Purpose:
  121. //-----------------------------------------------------------------------------
  122. bool CServerBrowser::Activate()
  123. {
  124. static bool firstTimeOpening = true;
  125. if ( firstTimeOpening )
  126. {
  127. m_hInternetDlg->LoadUserData(); // reload the user data the first time the dialog is made visible, helps with the lag between module load and
  128. // steamui getting Deactivate() call
  129. firstTimeOpening = false;
  130. }
  131. int numTimesOpened = sb_numtimesopened.GetInt() + 1;
  132. sb_numtimesopened.SetValue( numTimesOpened );
  133. if ( numTimesOpened == 1 )
  134. {
  135. time_t aclock;
  136. time( &aclock );
  137. sb_firstopentime.SetValue( (int) aclock );
  138. }
  139. Open();
  140. return true;
  141. }
  142. //-----------------------------------------------------------------------------
  143. // Purpose: called when the server browser gets used in the game
  144. //-----------------------------------------------------------------------------
  145. void CServerBrowser::Deactivate()
  146. {
  147. if (m_hInternetDlg.Get())
  148. {
  149. m_hInternetDlg->SaveUserData();
  150. }
  151. }
  152. //-----------------------------------------------------------------------------
  153. // Purpose: called when the server browser is no longer being used in the game
  154. //-----------------------------------------------------------------------------
  155. void CServerBrowser::Reactivate()
  156. {
  157. if (m_hInternetDlg.Get())
  158. {
  159. m_hInternetDlg->LoadUserData();
  160. if (m_hInternetDlg->IsVisible())
  161. {
  162. m_hInternetDlg->RefreshCurrentPage();
  163. }
  164. }
  165. }
  166. //-----------------------------------------------------------------------------
  167. // Purpose:
  168. //-----------------------------------------------------------------------------
  169. void CServerBrowser::Open()
  170. {
  171. m_hInternetDlg->Open();
  172. }
  173. //-----------------------------------------------------------------------------
  174. // Purpose: returns direct handle to main server browser dialog
  175. //-----------------------------------------------------------------------------
  176. vgui::VPANEL CServerBrowser::GetPanel()
  177. {
  178. return m_hInternetDlg.Get() ? m_hInternetDlg->GetVPanel() : NULL;
  179. }
  180. //-----------------------------------------------------------------------------
  181. // Purpose: sets the parent panel of the main module panel
  182. //-----------------------------------------------------------------------------
  183. void CServerBrowser::SetParent(vgui::VPANEL parent)
  184. {
  185. if (m_hInternetDlg.Get())
  186. {
  187. m_hInternetDlg->SetParent(parent);
  188. }
  189. }
  190. //-----------------------------------------------------------------------------
  191. // Purpose: Closes down the server browser for good
  192. //-----------------------------------------------------------------------------
  193. void CServerBrowser::Shutdown()
  194. {
  195. if (m_hInternetDlg.Get())
  196. {
  197. m_hInternetDlg->Close();
  198. m_hInternetDlg->MarkForDeletion();
  199. }
  200. #if defined( STEAM )
  201. vgui::VGuiControls_Shutdown();
  202. #endif
  203. DisconnectTier3Libraries();
  204. DisconnectTier2Libraries();
  205. ConVar_Unregister();
  206. DisconnectTier1Libraries();
  207. }
  208. //-----------------------------------------------------------------------------
  209. // Purpose: opens a game info dialog to watch the specified server; associated with the friend 'userName'
  210. //-----------------------------------------------------------------------------
  211. bool CServerBrowser::OpenGameInfoDialog( uint64 ulSteamIDFriend )
  212. {
  213. #if !defined( _X360 ) // X360TBD: SteamFriends()
  214. if ( m_hInternetDlg.Get() )
  215. {
  216. // activate an already-existing dialog
  217. CDialogGameInfo *pDialogGameInfo = m_hInternetDlg->GetDialogGameInfoForFriend( ulSteamIDFriend );
  218. if ( pDialogGameInfo )
  219. {
  220. pDialogGameInfo->Activate();
  221. return true;
  222. }
  223. // none yet, create a new dialog
  224. FriendGameInfo_t friendGameInfo;
  225. if ( steamapicontext->SteamFriends()->GetFriendGamePlayed( ulSteamIDFriend, &friendGameInfo ) )
  226. {
  227. uint16 usConnPort = friendGameInfo.m_usGamePort;
  228. if ( friendGameInfo.m_usQueryPort < QUERY_PORT_ERROR )
  229. usConnPort = friendGameInfo.m_usQueryPort;
  230. CDialogGameInfo *pDialogGameInfo = m_hInternetDlg->OpenGameInfoDialog( friendGameInfo.m_unGameIP, friendGameInfo.m_usGamePort, usConnPort );
  231. pDialogGameInfo->SetFriend( ulSteamIDFriend );
  232. return true;
  233. }
  234. }
  235. #endif
  236. return false;
  237. }
  238. //-----------------------------------------------------------------------------
  239. // Purpose: joins a specified game - game info dialog will only be opened if the server is fully or passworded
  240. //-----------------------------------------------------------------------------
  241. bool CServerBrowser::JoinGame( uint64 ulSteamIDFriend )
  242. {
  243. if ( OpenGameInfoDialog( ulSteamIDFriend ) )
  244. {
  245. CDialogGameInfo *pDialogGameInfo = m_hInternetDlg->GetDialogGameInfoForFriend( ulSteamIDFriend );
  246. pDialogGameInfo->Connect( "ServerBrowserJoinFriend" );
  247. }
  248. return false;
  249. }
  250. //-----------------------------------------------------------------------------
  251. // Purpose: joins a game by IP/Port
  252. //-----------------------------------------------------------------------------
  253. bool CServerBrowser::JoinGame( uint32 unGameIP, uint16 usGamePort )
  254. {
  255. m_hInternetDlg->JoinGame( unGameIP, usGamePort );
  256. return true;
  257. }
  258. //-----------------------------------------------------------------------------
  259. // Purpose: forces the game info dialog closed
  260. //-----------------------------------------------------------------------------
  261. void CServerBrowser::CloseGameInfoDialog( uint64 ulSteamIDFriend )
  262. {
  263. CDialogGameInfo *pDialogGameInfo = m_hInternetDlg->GetDialogGameInfoForFriend( ulSteamIDFriend );
  264. if ( pDialogGameInfo )
  265. {
  266. pDialogGameInfo->Close();
  267. }
  268. }
  269. //-----------------------------------------------------------------------------
  270. // Purpose: closes all the game info dialogs
  271. //-----------------------------------------------------------------------------
  272. void CServerBrowser::CloseAllGameInfoDialogs()
  273. {
  274. if ( m_hInternetDlg.Get() )
  275. {
  276. m_hInternetDlg->CloseAllGameInfoDialogs();
  277. }
  278. }