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.

139 lines
4.3 KiB

  1. //===== Copyright � 1996-2009, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose: common routines to operate on matchmaking sessions and members
  4. // Assumptions: caller should include all required headers before including mm_helpers.h
  5. //
  6. //===========================================================================//
  7. #ifndef __COMMON__MM_HELPERS_H_
  8. #define __COMMON__MM_HELPERS_H_
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "tier1/keyvalues.h"
  13. #include "tier1/fmtstr.h"
  14. //
  15. // Contains inline functions to deal with common tasks involving matchmaking and sessions
  16. //
  17. inline KeyValues * SessionMembersFindPlayer( KeyValues *pSessionSettings, XUID xuidPlayer, KeyValues **ppMachine = NULL )
  18. {
  19. if ( ppMachine )
  20. *ppMachine = NULL;
  21. if ( !pSessionSettings )
  22. return NULL;
  23. KeyValues *pMembers = pSessionSettings->FindKey( "Members" );
  24. if ( !pMembers )
  25. return NULL;
  26. int numMachines = pMembers->GetInt( "numMachines" );
  27. for ( int k = 0; k < numMachines; ++ k )
  28. {
  29. KeyValues *pMachine = pMembers->FindKey( CFmtStr( "machine%d", k ) );
  30. if ( !pMachine )
  31. continue;
  32. int numPlayers = pMachine->GetInt( "numPlayers" );
  33. for ( int j = 0; j < numPlayers; ++ j )
  34. {
  35. KeyValues *pPlayer = pMachine->FindKey( CFmtStr( "player%d", j ) );
  36. if ( !pPlayer )
  37. continue;
  38. if ( pPlayer->GetUint64( "xuid" ) == xuidPlayer )
  39. {
  40. if ( ppMachine )
  41. *ppMachine = pMachine;
  42. return pPlayer;
  43. }
  44. }
  45. }
  46. return NULL;
  47. }
  48. inline XUID SessionMembersFindNonGuestXuid( XUID xuid )
  49. {
  50. #ifdef _X360
  51. if ( !g_pMatchFramework )
  52. return xuid;
  53. if ( !g_pMatchFramework->GetMatchSession() )
  54. return xuid;
  55. KeyValues *pMachine = NULL;
  56. KeyValues *pPlayer = SessionMembersFindPlayer( g_pMatchFramework->GetMatchSession()->GetSessionSettings(), xuid, &pMachine );
  57. if ( !pPlayer || !pMachine )
  58. return xuid;
  59. if ( !strchr( pPlayer->GetString( "name" ), '(' ) )
  60. return xuid;
  61. int numPlayers = pMachine->GetInt( "numPlayers" );
  62. for ( int k = 0; k < numPlayers; ++ k )
  63. {
  64. XUID xuidOtherPlayer = pMachine->GetUint64( CFmtStr( "player%d/xuid", k ) );
  65. if ( xuidOtherPlayer && !strchr( pMachine->GetString( CFmtStr( "player%d/xuid", k ) ), '(' ) )
  66. return xuidOtherPlayer; // found a replacement that is not guest
  67. }
  68. #endif
  69. return xuid;
  70. }
  71. inline TitleDataFieldsDescription_t const * TitleDataFieldsDescriptionFindByString( TitleDataFieldsDescription_t const *fields, char const *szString )
  72. {
  73. if ( !szString )
  74. return NULL;
  75. for ( ; fields && fields->m_szFieldName; ++ fields )
  76. {
  77. if ( !Q_stricmp( fields->m_szFieldName, szString ) )
  78. return fields;
  79. }
  80. return NULL;
  81. }
  82. inline bool TitleDataFieldsDescriptionGetBit( TitleDataFieldsDescription_t const *fdKey, IPlayerLocal *pPlayer )
  83. {
  84. Assert( pPlayer );
  85. Assert( fdKey );
  86. Assert( fdKey->m_eDataType == TitleDataFieldsDescription_t::DT_BITFIELD );
  87. return !!( ( *( const uint8 * )( ( ( const char * )pPlayer->GetPlayerTitleData( fdKey->m_iTitleDataBlock ) ) + (fdKey->m_numBytesOffset/8) ) ) & ( 1 << ( fdKey->m_numBytesOffset%8 ) ) );
  88. }
  89. inline void TitleDataFieldsDescriptionSetBit( TitleDataFieldsDescription_t const *fdKey, IPlayerLocal *pPlayer, bool bBitValue )
  90. {
  91. Assert( pPlayer );
  92. Assert( fdKey );
  93. Assert( fdKey->m_eDataType == TitleDataFieldsDescription_t::DT_BITFIELD );
  94. uint8 uiValue = static_cast< uint8 >( bBitValue ? (~0u) : 0u );
  95. pPlayer->UpdatePlayerTitleData( fdKey, &uiValue, sizeof( uiValue ) );
  96. }
  97. template < typename T >
  98. inline T TitleDataFieldsDescriptionGetValue( TitleDataFieldsDescription_t const *fdKey, IPlayerLocal *pPlayer )
  99. {
  100. Assert( pPlayer );
  101. Assert( fdKey );
  102. Assert( ( fdKey->m_eDataType != TitleDataFieldsDescription_t::DT_BITFIELD ) && ( fdKey->m_eDataType != TitleDataFieldsDescription_t::DT_0 ) );
  103. return *( T const * )(&( (char const*) pPlayer->GetPlayerTitleData( fdKey->m_iTitleDataBlock ) )[(fdKey->m_numBytesOffset)]);
  104. }
  105. template < typename T > // T is primitive type, passing by value instead of constref to avoid compiler troubles when passing temporaries
  106. inline void TitleDataFieldsDescriptionSetValue( TitleDataFieldsDescription_t const *fdKey, IPlayerLocal *pPlayer, T val )
  107. {
  108. Assert( pPlayer );
  109. Assert( fdKey );
  110. Assert( ( fdKey->m_eDataType != TitleDataFieldsDescription_t::DT_BITFIELD ) && ( fdKey->m_eDataType != TitleDataFieldsDescription_t::DT_0 ) );
  111. pPlayer->UpdatePlayerTitleData( fdKey, &val, sizeof( T ) );
  112. }
  113. #endif // __COMMON__MM_HELPERS_H_