Get player avatar using online subsystem, and not using Steam API

Ah, sorry for late reply. I need to turn back notifications from this forum :slight_smile:

Steam:
First request user info and wait for it to finish

	if (SteamAPI_Init())
	{
		const uint64 Int64Id = FCString::Atoi64(*UniqueNetId);
		SteamFriends()->RequestUserInformation(Int64Id, false);
	}

Then download avatar

UTexture2D* USteamHelper::GetSteamFriendAvatar(const uint64 UniqueNetId, PlayerAvatarSize PlayerAvatarSize)
{
	UTexture2D** CachedTexture = TextureCacheMap.Find(UniqueNetId);
	if (CachedTexture != nullptr)
	{
		return *CachedTexture;
	}
	
	uint32 Width = 0;
	uint32 Height = 0;

	if (SteamAPI_Init())
	{
		//Getting the PictureID from the SteamAPI and getting the Size with the ID
		//virtual bool RequestUserInformation( CSteamID steamIDUser, bool bRequireNameOnly ) = 0;
		
		int Picture = 0;
		
		switch(PlayerAvatarSize)
		{
		case PlayerAvatarSize::PlayerAvatar_Small: Picture = SteamFriends()->GetSmallFriendAvatar(UniqueNetId); break;
		case PlayerAvatarSize::PlayerAvatar_Medium: Picture = SteamFriends()->GetMediumFriendAvatar(UniqueNetId); break;
		case PlayerAvatarSize::PlayerAvatar_Large: Picture = SteamFriends()->GetLargeFriendAvatar(UniqueNetId); break;
		default: break;
		}

		if (Picture == -1)
		{
			return NULL;
		}

		SteamUtils()->GetImageSize(Picture, &Width, &Height);

		if (Width > 0 && Height > 0)
		{
			//Creating the buffer "oAvatarRGBA" and then filling it with the RGBA Stream from the Steam Avatar
			uint8 *oAvatarRGBA = new uint8[Width * Height * 4];


			//Filling the buffer with the RGBA Stream from the Steam Avatar and creating a UTextur2D to parse the RGBA Steam in
			SteamUtils()->GetImageRGBA(Picture, (uint8*)oAvatarRGBA, 4 * Height * Width * sizeof(char));
			
			UTexture2D* Avatar = UTexture2D::CreateTransient(Width, Height, PF_R8G8B8A8);
			// Switched to a Memcpy instead of byte by byte transer
			uint8* MipData = (uint8*)Avatar->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
			FMemory::Memcpy(MipData, (void*)oAvatarRGBA, Height * Width * 4);
			Avatar->PlatformData->Mips[0].BulkData.Unlock();

			// Original implementation was missing this!!
			// the hell man......
			delete[] oAvatarRGBA;

			//Setting some Parameters for the Texture and finally returning it
			Avatar->PlatformData->SetNumSlices(1);
			Avatar->NeverStream = true;
			//Avatar->CompressionSettings = TC_EditorIcon;

			Avatar->UpdateResource();

			TextureCacheMap.Add(UniqueNetId, Avatar);
			return Avatar;
		}
		else
		{
			UE_LOG(LogTemp, Warning, TEXT("Bad Height / Width with steam avatar!"));
		}
		
		return nullptr;
	}
	return nullptr;
}

GOG

First request user info and wait for it to finish

galaxy::api::Friends()->RequestUserInformation(MyGalaxyID, PlayerAvatarType, GogUserInfoListener);

Then download avatar

UTexture2D* UGogHelper::GetGogFriendAvatar(uint64 UserId, galaxy::api::AvatarType AvatarType)
{
	UTexture2D** CachedTexture = TextureCacheMap.Find(UserId);
	if (CachedTexture != nullptr)
	{
		return *CachedTexture;
	}
	
	uint32 Width;
	uint32 Height;

	switch (AvatarType)
	{
	case galaxy::api::AVATAR_TYPE_SMALL:
		{
			Width = 32;
			Height = 32;
		}
		break;
	case galaxy::api::AVATAR_TYPE_MEDIUM:
		{
			Width = 64;
			Height = 64;
		}
		break;
	case galaxy::api::AVATAR_TYPE_LARGE:
		{
			Width = 184;
			Height = 184;
		}
		break;
	default:
		{
			Width = 0;
			Height = 0;
		}
	}

	if (Width > 0 && Height > 0)
	{
		const galaxy::api::GalaxyID UserGalaxyId = galaxy::api::GalaxyID::FromRealID(
			galaxy::api::GalaxyID::ID_TYPE_USER, UserId);

		//Creating the buffer "oAvatarRGBA" and then filling it with the RGBA Stream from the Steam Avatar
		uint8* oAvatarRGBA = new uint8[Width * Height * 4];


		//Filling the buffer with the RGBA Stream from the Steam Avatar and creating a UTexture2D to parse the RGBA Steam in		
		galaxy::api::Friends()->GetFriendAvatarImageRGBA(UserGalaxyId, AvatarType, oAvatarRGBA,
		                                                 4 * Width * Height * sizeof(char));


		UTexture2D* Avatar = UTexture2D::CreateTransient(Width, Height, PF_R8G8B8A8);
		// Switched to a Memcpy instead of byte by byte transfer
		uint8* MipData = (uint8*)Avatar->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
		FMemory::Memcpy(MipData, (void*)oAvatarRGBA, Height * Width * 4);
		Avatar->PlatformData->Mips[0].BulkData.Unlock();

		delete[] oAvatarRGBA;

		//Setting some Parameters for the Texture and finally returning it
		Avatar->PlatformData->SetNumSlices(1);
		Avatar->NeverStream = true;

		Avatar->UpdateResource();
		TextureCacheMap.Add(UserId, Avatar);
		return Avatar;
	}
	UE_LOG(LogTemp, Warning, TEXT("Bad Height / Width with GOG avatar!"));
	return nullptr;
}
2 Likes