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.

211 lines
5.3 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. // Purpose: Performs a var args printf into a static return buffer
  78. // Input : *format -
  79. // ... -
  80. // Output : char
  81. //-----------------------------------------------------------------------------
  82. // Unused :: char *VarArgs( PRINTF_FORMAT_STRING const char *format, ... );
  83. //-----------------------------------------------------------------------------
  84. // This version is simply used to make reading convars simpler.
  85. // Writing convars isn't allowed in this mode
  86. //-----------------------------------------------------------------------------
  87. class CEmptyGameUIConVar : public ConVar
  88. {
  89. public:
  90. CEmptyGameUIConVar() : ConVar( "", "0" ) {}
  91. // Used for optimal read access
  92. virtual void SetValue( const char *pValue ) {}
  93. virtual void SetValue( float flValue ) {}
  94. virtual void SetValue( int nValue ) {}
  95. virtual const char *GetName( void ) const { return ""; }
  96. virtual bool IsFlagSet( int nFlags ) const { return false; }
  97. };
  98. static CEmptyGameUIConVar s_EmptyConVar;
  99. // Helper for splitscreen ConVars
  100. CGameUIConVarRef::CGameUIConVarRef( const char *pName )
  101. {
  102. Init( pName, false );
  103. }
  104. CGameUIConVarRef::CGameUIConVarRef( const char *pName, bool bIgnoreMissing )
  105. {
  106. Init( pName, bIgnoreMissing );
  107. }
  108. void CGameUIConVarRef::Init( const char *pName, bool bIgnoreMissing )
  109. {
  110. for ( int i = 0; i < MAX_SPLITSCREEN_CLIENTS; ++i )
  111. {
  112. cv_t &info = m_Info[ i ];
  113. char pchName[ 256 ];
  114. if ( i != 0 )
  115. {
  116. Q_snprintf( pchName, sizeof( pchName ), "%s%d", pName, i + 1 );
  117. }
  118. else
  119. {
  120. Q_strncpy( pchName, pName, sizeof( pchName ) );
  121. }
  122. info.m_pConVar = g_pCVar ? g_pCVar->FindVar( pchName ) : &s_EmptyConVar;
  123. if ( !info.m_pConVar )
  124. {
  125. info.m_pConVar = &s_EmptyConVar;
  126. if ( i > 0 )
  127. {
  128. // Point at slot zero instead, in case we got in here with a non FCVAR_SS var...
  129. info.m_pConVar = m_Info[ 0 ].m_pConVar;
  130. }
  131. }
  132. info.m_pConVarState = static_cast< ConVar * >( info.m_pConVar );
  133. }
  134. if ( !IsValid() )
  135. {
  136. static bool bFirst = true;
  137. if ( g_pCVar || bFirst )
  138. {
  139. if ( !bIgnoreMissing )
  140. {
  141. Warning( "CGameUIConVarRef %s doesn't point to an existing ConVar\n", pName );
  142. }
  143. bFirst = false;
  144. }
  145. }
  146. }
  147. CGameUIConVarRef::CGameUIConVarRef( IConVar *pConVar )
  148. {
  149. cv_t &info = m_Info[ 0 ];
  150. info.m_pConVar = pConVar ? pConVar : &s_EmptyConVar;
  151. info.m_pConVarState = static_cast< ConVar * >( info.m_pConVar );
  152. for ( int i = 1; i < MAX_SPLITSCREEN_CLIENTS; ++i )
  153. {
  154. info = m_Info[ i ];
  155. char pchName[ 256 ];
  156. Q_snprintf( pchName, sizeof( pchName ), "%s%d", pConVar->GetName(), i + 1 );
  157. info.m_pConVar = g_pCVar ? g_pCVar->FindVar( pchName ) : &s_EmptyConVar;
  158. if ( !info.m_pConVar )
  159. {
  160. info.m_pConVar = &s_EmptyConVar;
  161. if ( i > 0 )
  162. {
  163. // Point at slot zero instead, in case we got in here with a non FCVAR_SS var...
  164. info.m_pConVar = m_Info[ 0 ].m_pConVar;
  165. }
  166. }
  167. info.m_pConVarState = static_cast< ConVar * >( info.m_pConVar );
  168. }
  169. }
  170. bool CGameUIConVarRef::IsValid() const
  171. {
  172. return m_Info[ 0 ].m_pConVar != &s_EmptyConVar;
  173. }
  174. //-----------------------------------------------------------------------------
  175. CGameUiSetActiveSplitScreenPlayerGuard::CGameUiSetActiveSplitScreenPlayerGuard( int slot )
  176. {
  177. m_nSaveSlot = engine->GetActiveSplitScreenPlayerSlot();
  178. engine->SetActiveSplitScreenPlayerSlot( slot );
  179. }
  180. CGameUiSetActiveSplitScreenPlayerGuard::~CGameUiSetActiveSplitScreenPlayerGuard()
  181. {
  182. engine->SetActiveSplitScreenPlayerSlot( m_nSaveSlot );
  183. }