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.

146 lines
5.0 KiB

  1. //========= Copyright � 1996-2008, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=====================================================================================//
  6. #include "stdafx.h"
  7. #include "sfuiavatarimage.h"
  8. // memdbgon must be the last include file in a .cpp file!!!
  9. #include "tier0/memdbgon.h"
  10. #ifndef NO_STEAM
  11. extern CSteamAPIContext *steamapicontext;
  12. #endif
  13. #pragma warning( disable: 4355 ) // disables ' 'this' : used in base member initializer list'
  14. using namespace SF::Render;
  15. IScaleformAvatarImageProvider * ScaleformUIAvatarImage::sm_pProvider = NULL; // static provider initialized upon first available avatar data arriving
  16. //-----------------------------------------------------------------------------
  17. // Purpose:
  18. //-----------------------------------------------------------------------------
  19. ScaleformUIAvatarImage::ScaleformUIAvatarImage( XUID xuid, const byte* defaultRgba, int defaultWidth, int defaultHeight, ::ImageFormat defaultFormat, SF::GFx::TextureManager* pTextureManager )
  20. : ScaleformUIImage( defaultRgba, defaultWidth, defaultHeight, defaultFormat, pTextureManager )
  21. {
  22. m_xXUID = xuid;
  23. if ( ( m_xXUID & 0xFFFFFFFFull ) == m_xXUID )
  24. {
  25. static EUniverse eUniverse = steamapicontext->SteamUtils()->GetConnectedUniverse();
  26. m_xXUID = CSteamID( uint32( m_xXUID ), eUniverse, k_EAccountTypeIndividual ).ConvertToUint64();
  27. }
  28. }
  29. //-----------------------------------------------------------------------------
  30. // Purpose:
  31. //-----------------------------------------------------------------------------
  32. bool ScaleformUIAvatarImage::LoadAvatarImage( IScaleformAvatarImageProvider *pProvider /* = NULL */ )
  33. {
  34. #ifndef NO_STEAM
  35. if ( steamapicontext->SteamFriends() && steamapicontext->SteamUtils() && steamapicontext->SteamUser() )
  36. {
  37. static XUID s_myXuid = steamapicontext->SteamUser()->GetSteamID().ConvertToUint64();
  38. bool bLarge = ( s_myXuid == m_xXUID );
  39. #if 0
  40. // For debugging we can set to always use entity avatar data to test our own avatar
  41. // and always keep own avatar the same size
  42. bLarge = false;
  43. #endif
  44. static ConVarRef sv_reliableavatardata( "sv_reliableavatardata", true );
  45. if ( bLarge && sv_reliableavatardata.IsValid() && ( sv_reliableavatardata.GetInt() == 2 ) )
  46. bLarge = false; // Server is overriding all avatars, force update own avatar from server data too
  47. if ( pProvider && !sm_pProvider )
  48. sm_pProvider = pProvider;
  49. if ( !pProvider && sm_pProvider )
  50. pProvider = sm_pProvider;
  51. IScaleformAvatarImageProvider::ImageInfo_t ii = {};
  52. if ( !bLarge && pProvider && pProvider->GetImageInfo( m_xXUID, &ii ) )
  53. {
  54. // Expect 64x64 RGB images here
  55. if ( ii.m_cbImageData == 64*64*3 )
  56. {
  57. uint32 wide = 64, tall = 64;
  58. int cubImage = wide * tall * 4;
  59. m_bufRgbaBuffer.EnsureCapacity( cubImage );
  60. memset( m_bufRgbaBuffer.Base(), 0xFF, cubImage );
  61. for ( int y = 0; y < 64; ++ y ) for ( int x = 0; x < 64; ++ x )
  62. { // Explode RGB into RGBA
  63. V_memcpy( ( ( byte * ) m_bufRgbaBuffer.Base() ) + y*64*4 + x*4, ( ( byte const * ) ii.m_pvImageData ) + y*64*3 + x*3, 3 );
  64. }
  65. InitFromBuffer( ( byte* ) m_bufRgbaBuffer.Base(), wide, tall, IMAGE_FORMAT_RGBA8888 );
  66. return true;
  67. }
  68. }
  69. #if 0
  70. // For debugging we never use Steam avatars, always try to use engine.dll provider
  71. return true;
  72. #endif
  73. if ( bLarge )
  74. {
  75. // For the local user force to download the local medium avatar as well as large
  76. int iAvatar = steamapicontext->SteamFriends()->GetMediumFriendAvatar( m_xXUID );
  77. if ( !iAvatar || ( iAvatar == -1 ) )
  78. {
  79. ( void ) steamapicontext->SteamFriends()->RequestUserInformation( m_xXUID, false );
  80. }
  81. }
  82. int iAvatar = bLarge
  83. ? steamapicontext->SteamFriends()->GetLargeFriendAvatar( m_xXUID )
  84. : steamapicontext->SteamFriends()->GetMediumFriendAvatar( m_xXUID );
  85. if ( !iAvatar || ( iAvatar == -1 ) )
  86. {
  87. bool bRequestEnqueuedAsync = steamapicontext->SteamFriends()->RequestUserInformation( m_xXUID, false );
  88. if ( !bRequestEnqueuedAsync ) // try again one more time, Steam says everything is available
  89. iAvatar = bLarge
  90. ? steamapicontext->SteamFriends()->GetLargeFriendAvatar( m_xXUID )
  91. : steamapicontext->SteamFriends()->GetMediumFriendAvatar( m_xXUID );
  92. }
  93. if ( iAvatar != 0 )
  94. {
  95. /*
  96. // See if it's in our list already
  97. */
  98. uint32 wide, tall;
  99. if ( steamapicontext->SteamUtils()->GetImageSize( iAvatar, &wide, &tall ) )
  100. {
  101. bool bUseSteamImage = true;
  102. if ( wide == 0 || tall == 0 )
  103. {
  104. // attempt to handle rare data integrity issue, avatar got lost
  105. bUseSteamImage = false;
  106. // mock up solid white as 64x64
  107. wide = tall = 64;
  108. }
  109. int cubImage = wide * tall * 4;
  110. m_bufRgbaBuffer.EnsureCapacity( cubImage );
  111. memset( m_bufRgbaBuffer.Base(), 0xFF, cubImage );
  112. if ( bUseSteamImage )
  113. {
  114. steamapicontext->SteamUtils()->GetImageRGBA( iAvatar, (byte*)m_bufRgbaBuffer.Base(), cubImage );
  115. }
  116. InitFromBuffer( (byte*)m_bufRgbaBuffer.Base(), wide, tall, IMAGE_FORMAT_RGBA8888 );
  117. }
  118. }
  119. }
  120. #elif defined( _X360 )
  121. X360_ResetAsyncImageState();
  122. return true;
  123. #endif
  124. return true;
  125. }