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.

194 lines
4.8 KiB

  1. //====== Copyright � 1996-2007, Valve Corporation, All rights reserved. =======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <sys/stat.h>
  11. #ifndef POSIX
  12. #include <conio.h>
  13. #include <direct.h>
  14. #include <io.h>
  15. #endif
  16. #include "mm_framework.h"
  17. // memdbgon must be the last include file in a .cpp file!!!
  18. #include "tier0/memdbgon.h"
  19. // GCSDK uses the console log channel
  20. DEFINE_LOGGING_CHANNEL_NO_TAGS( LOG_CONSOLE, "Console" );
  21. void LinkMatchmakingLib()
  22. {
  23. // This function is required for the linker to include CMatchFramework
  24. }
  25. static CMatchFramework g_MatchFramework;
  26. CMatchFramework *g_pMMF = &g_MatchFramework;
  27. EXPOSE_SINGLE_INTERFACE_GLOBALVAR( CMatchFramework, IMatchFramework,
  28. IMATCHFRAMEWORK_VERSION_STRING, g_MatchFramework );
  29. //
  30. // IAppSystem implementation
  31. //
  32. static CreateInterfaceFn s_pfnDelegateFactory;
  33. static void * InternalFactory( const char *pName, int *pReturnCode )
  34. {
  35. if ( pReturnCode )
  36. {
  37. *pReturnCode = IFACE_OK;
  38. }
  39. // Try to get interface via delegate
  40. if ( void *pInterface = s_pfnDelegateFactory ? s_pfnDelegateFactory( pName, pReturnCode ) : NULL )
  41. {
  42. return pInterface;
  43. }
  44. // Try to get internal interface
  45. if ( void *pInterface = Sys_GetFactoryThis()( pName, pReturnCode ) )
  46. {
  47. return pInterface;
  48. }
  49. // Failed
  50. if ( pReturnCode )
  51. {
  52. *pReturnCode = IFACE_FAILED;
  53. }
  54. return NULL;
  55. }
  56. namespace
  57. {
  58. typedef void * (CMatchExtensions::* ExtFn_t)();
  59. struct MatchExtInterface_t
  60. {
  61. char const *m_szName;
  62. ExtFn_t m_pfnGetInterface;
  63. bool m_bConnected;
  64. };
  65. static MatchExtInterface_t s_table[] =
  66. {
  67. { LOCALIZE_INTERFACE_VERSION, (ExtFn_t) &CMatchExtensions::GetILocalize, false },
  68. { INETSUPPORT_VERSION_STRING, (ExtFn_t) &CMatchExtensions::GetINetSupport, false },
  69. { IENGINEVOICE_INTERFACE_VERSION, (ExtFn_t) &CMatchExtensions::GetIEngineVoice, false },
  70. { VENGINE_CLIENT_INTERFACE_VERSION, (ExtFn_t) &CMatchExtensions::GetIVEngineClient, false },
  71. { INTERFACEVERSION_VENGINESERVER, (ExtFn_t) &CMatchExtensions::GetIVEngineServer, false },
  72. { INTERFACEVERSION_GAMEEVENTSMANAGER2, (ExtFn_t) &CMatchExtensions::GetIGameEventManager2, false },
  73. #ifdef _X360
  74. { XBOXSYSTEM_INTERFACE_VERSION, (ExtFn_t) &CMatchExtensions::GetIXboxSystem, false },
  75. { XONLINE_INTERFACE_VERSION, (ExtFn_t) &CMatchExtensions::GetIXOnline, false },
  76. #endif
  77. { NULL, NULL, NULL }
  78. };
  79. };
  80. bool CMatchFramework::Connect( CreateInterfaceFn factory )
  81. {
  82. Assert( !s_pfnDelegateFactory );
  83. s_pfnDelegateFactory = factory;
  84. CreateInterfaceFn ourFactory = InternalFactory;
  85. ConnectTier1Libraries( &ourFactory, 1 );
  86. ConnectTier2Libraries( &ourFactory, 1 );
  87. ConVar_Register();
  88. // Get our extension interfaces
  89. for ( MatchExtInterface_t *ptr = s_table; ptr->m_szName; ++ ptr )
  90. {
  91. if ( !ptr->m_bConnected
  92. // && !(g_pMatchExtensions->*(ptr->m_pfnGetInterface))()
  93. )
  94. {
  95. void *pvInterface = ourFactory( ptr->m_szName, NULL );
  96. if ( pvInterface )
  97. {
  98. g_pMatchExtensions->RegisterExtensionInterface( ptr->m_szName, pvInterface );
  99. ptr->m_bConnected = true;
  100. }
  101. }
  102. }
  103. s_pfnDelegateFactory = NULL;
  104. MathLib_Init( 2.2f, 2.2f, 0.0f, 2.0f );
  105. SteamApiContext_Init();
  106. #if !defined( _GAMECONSOLE ) && !defined( SWDS )
  107. // Trigger intialization from Steam users
  108. if ( g_pPlayerManager )
  109. g_pPlayerManager->OnGameUsersChanged();
  110. #endif
  111. return true;
  112. }
  113. void CMatchFramework::Disconnect()
  114. {
  115. SteamApiContext_Shutdown();
  116. for ( MatchExtInterface_t *ptr = s_table; ptr->m_szName; ++ ptr )
  117. {
  118. if ( ptr->m_bConnected )
  119. {
  120. void *pvInterface = (g_pMatchExtensions->*(ptr->m_pfnGetInterface))();
  121. Assert( pvInterface );
  122. g_pMatchExtensions->UnregisterExtensionInterface( ptr->m_szName, pvInterface );
  123. ptr->m_bConnected = false;
  124. }
  125. }
  126. DisconnectTier2Libraries();
  127. ConVar_Unregister();
  128. DisconnectTier1Libraries();
  129. }
  130. void * CMatchFramework::QueryInterface( const char *pInterfaceName )
  131. {
  132. if ( !Q_stricmp( pInterfaceName, IMATCHFRAMEWORK_VERSION_STRING ) )
  133. return static_cast< IMatchFramework* >( this );
  134. return NULL;
  135. }
  136. const char *COM_GetModDirectory()
  137. {
  138. static char modDir[MAX_PATH];
  139. if ( Q_strlen( modDir ) == 0 )
  140. {
  141. const char *gamedir = CommandLine()->ParmValue("-game", CommandLine()->ParmValue( "-defaultgamedir", "hl2" ) );
  142. Q_strncpy( modDir, gamedir, sizeof(modDir) );
  143. if ( strchr( modDir, '/' ) || strchr( modDir, '\\' ) )
  144. {
  145. Q_StripLastDir( modDir, sizeof(modDir) );
  146. int dirlen = Q_strlen( modDir );
  147. Q_strncpy( modDir, gamedir + dirlen, sizeof(modDir) - dirlen );
  148. }
  149. }
  150. return modDir;
  151. }
  152. bool IsLocalClientConnectedToServer()
  153. {
  154. return
  155. ( g_pMatchExtensions &&
  156. g_pMatchExtensions->GetIVEngineClient() &&
  157. ( g_pMatchExtensions->GetIVEngineClient()->IsConnected() ||
  158. g_pMatchExtensions->GetIVEngineClient()->IsDrawingLoadingImage() ||
  159. g_pMatchExtensions->GetIVEngineClient()->IsTransitioningToLoad() ) );
  160. }