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.

203 lines
5.1 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include <stdarg.h>
  9. #include "gameui_util.h"
  10. #include "strtools.h"
  11. #include "EngineInterface.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. using namespace vgui;
  15. static int g_nGameUIActiveSplitscreenPlayerSlot = 0;
  16. int GetGameUIActiveSplitScreenPlayerSlot()
  17. {
  18. return g_nGameUIActiveSplitscreenPlayerSlot;
  19. }
  20. void SetGameUIActiveSplitScreenPlayerSlot( int nSlot )
  21. {
  22. if ( g_nGameUIActiveSplitscreenPlayerSlot != nSlot )
  23. {
  24. g_nGameUIActiveSplitscreenPlayerSlot = nSlot;
  25. }
  26. }
  27. //-----------------------------------------------------------------------------
  28. // Purpose: Scans player names
  29. //Passes the player name to be checked in a KeyValues pointer
  30. //with the keyname "name"
  31. // - replaces '&' with '&&' so they will draw in the scoreboard
  32. // - replaces '#' at the start of the name with '*'
  33. //-----------------------------------------------------------------------------
  34. void GameUI_MakeSafeName( const char *oldName, char *newName, int newNameBufSize )
  35. {
  36. if ( !oldName )
  37. {
  38. newName[0] = 0;
  39. return;
  40. }
  41. int newpos = 0;
  42. for( const char *p=oldName; *p != 0 && newpos < newNameBufSize-1; p++ )
  43. {
  44. //check for a '#' char at the beginning
  45. /*
  46. if( p == oldName && *p == '#' )
  47. {
  48. newName[newpos] = '*';
  49. newpos++;
  50. }
  51. else */
  52. if( *p == '%' )
  53. {
  54. // remove % chars
  55. newName[newpos] = '*';
  56. newpos++;
  57. }
  58. else if( *p == '&' )
  59. {
  60. //insert another & after this one
  61. if ( newpos+2 < newNameBufSize )
  62. {
  63. newName[newpos] = '&';
  64. newName[newpos+1] = '&';
  65. newpos+=2;
  66. }
  67. }
  68. else
  69. {
  70. newName[newpos] = *p;
  71. newpos++;
  72. }
  73. }
  74. newName[newpos] = 0;
  75. }
  76. //-----------------------------------------------------------------------------
  77. // This version is simply used to make reading convars simpler.
  78. // Writing convars isn't allowed in this mode
  79. //-----------------------------------------------------------------------------
  80. class CEmptyGameUIConVar : public ConVar
  81. {
  82. public:
  83. CEmptyGameUIConVar() : ConVar( "", "0" ) {}
  84. // Used for optimal read access
  85. virtual void SetValue( const char *pValue ) {}
  86. virtual void SetValue( float flValue ) {}
  87. virtual void SetValue( int nValue ) {}
  88. virtual const char *GetName( void ) const { return ""; }
  89. virtual bool IsFlagSet( int nFlags ) const { return false; }
  90. };
  91. static CEmptyGameUIConVar s_EmptyConVar;
  92. // Helper for splitscreen ConVars
  93. CGameUIConVarRef::CGameUIConVarRef( const char *pName )
  94. {
  95. Init( pName, false );
  96. }
  97. CGameUIConVarRef::CGameUIConVarRef( const char *pName, bool bIgnoreMissing )
  98. {
  99. Init( pName, bIgnoreMissing );
  100. }
  101. void CGameUIConVarRef::Init( const char *pName, bool bIgnoreMissing )
  102. {
  103. for ( int i = 0; i < MAX_SPLITSCREEN_CLIENTS; ++i )
  104. {
  105. cv_t &info = m_Info[ i ];
  106. char pchName[ 256 ];
  107. if ( i != 0 )
  108. {
  109. Q_snprintf( pchName, sizeof( pchName ), "%s%d", pName, i + 1 );
  110. }
  111. else
  112. {
  113. Q_strncpy( pchName, pName, sizeof( pchName ) );
  114. }
  115. info.m_pConVar = g_pCVar ? g_pCVar->FindVar( pchName ) : &s_EmptyConVar;
  116. if ( !info.m_pConVar )
  117. {
  118. info.m_pConVar = &s_EmptyConVar;
  119. if ( i > 0 )
  120. {
  121. // Point at slot zero instead, in case we got in here with a non FCVAR_SS var...
  122. info.m_pConVar = m_Info[ 0 ].m_pConVar;
  123. }
  124. }
  125. info.m_pConVarState = static_cast< ConVar * >( info.m_pConVar );
  126. }
  127. if ( !IsValid() )
  128. {
  129. static bool bFirst = true;
  130. if ( g_pCVar || bFirst )
  131. {
  132. if ( !bIgnoreMissing )
  133. {
  134. Warning( "CGameUIConVarRef %s doesn't point to an existing ConVar\n", pName );
  135. }
  136. bFirst = false;
  137. }
  138. }
  139. }
  140. CGameUIConVarRef::CGameUIConVarRef( IConVar *pConVar )
  141. {
  142. cv_t &info = m_Info[ 0 ];
  143. info.m_pConVar = pConVar ? pConVar : &s_EmptyConVar;
  144. info.m_pConVarState = static_cast< ConVar * >( info.m_pConVar );
  145. for ( int i = 1; i < MAX_SPLITSCREEN_CLIENTS; ++i )
  146. {
  147. info = m_Info[ i ];
  148. char pchName[ 256 ];
  149. Q_snprintf( pchName, sizeof( pchName ), "%s%d", pConVar->GetName(), i + 1 );
  150. info.m_pConVar = g_pCVar ? g_pCVar->FindVar( pchName ) : &s_EmptyConVar;
  151. if ( !info.m_pConVar )
  152. {
  153. info.m_pConVar = &s_EmptyConVar;
  154. if ( i > 0 )
  155. {
  156. // Point at slot zero instead, in case we got in here with a non FCVAR_SS var...
  157. info.m_pConVar = m_Info[ 0 ].m_pConVar;
  158. }
  159. }
  160. info.m_pConVarState = static_cast< ConVar * >( info.m_pConVar );
  161. }
  162. }
  163. bool CGameUIConVarRef::IsValid() const
  164. {
  165. return m_Info[ 0 ].m_pConVar != &s_EmptyConVar;
  166. }
  167. //-----------------------------------------------------------------------------
  168. CGameUiSetActiveSplitScreenPlayerGuard::CGameUiSetActiveSplitScreenPlayerGuard( int slot )
  169. {
  170. m_nSaveSlot = engine->GetActiveSplitScreenPlayerSlot();
  171. engine->SetActiveSplitScreenPlayerSlot( slot );
  172. }
  173. CGameUiSetActiveSplitScreenPlayerGuard::~CGameUiSetActiveSplitScreenPlayerGuard()
  174. {
  175. engine->SetActiveSplitScreenPlayerSlot( m_nSaveSlot );
  176. }