Friends.GetAvatar now takes a callback rather than returning an Image. Use GetCachedAvatar for legacy behaviour. Also added

This commit is contained in:
Garry Newman 2017-12-06 11:58:20 +00:00
parent 54dab0c2a4
commit 6dc680cbab
3 changed files with 90 additions and 21 deletions

View File

@ -50,21 +50,43 @@ public void Avatar()
Assert.IsTrue( client.IsValid ); Assert.IsTrue( client.IsValid );
var friend = client.Friends.All.First(); var friend = client.Friends.All.First();
bool passed = false;
var img = client.Friends.GetAvatar( Steamworks.Friends.AvatarSize.Medium, friend.Id ); client.Friends.GetAvatar( Steamworks.Friends.AvatarSize.Medium, friend.Id, (avatar) =>
{
Assert.AreEqual(avatar.Width, 64);
Assert.AreEqual(avatar.Height, 64);
Assert.AreEqual(avatar.Data.Length, avatar.Width * avatar.Height * 4);
Assert.AreEqual( img.Width, 64 ); DrawImage(avatar);
Assert.AreEqual( img.Height, 64 ); passed = true;
});
while ( !img.IsLoaded && !img.IsError ) while (passed == false )
{ {
client.Update(); client.Update();
System.Threading.Thread.Sleep( 10 ); System.Threading.Thread.Sleep( 10 );
} }
}
}
Assert.AreEqual( img.Data.Length, img.Width * img.Height * 4 ); [TestMethod]
public void CachedAvatar()
{
using (var client = new Facepunch.Steamworks.Client(252490))
{
Assert.IsTrue(client.IsValid);
DrawImage( img ); var friend = client.Friends.All.First();
var image = client.Friends.GetCachedAvatar( Steamworks.Friends.AvatarSize.Medium, friend.Id );
if (image != null)
{
Assert.AreEqual(image.Width, 64);
Assert.AreEqual(image.Height, 64);
Assert.AreEqual(image.Data.Length, image.Width * image.Height * 4);
}
} }
} }

View File

@ -145,6 +145,8 @@ public class Friends
internal Friends( Client c ) internal Friends( Client c )
{ {
client = c; client = c;
SteamNative.PersonaStateChange_t.RegisterCallback( client, OnPersonaStateChange );
} }
/// <summary> /// <summary>
@ -253,20 +255,23 @@ public enum AvatarSize
Large Large
} }
public Image GetAvatar( AvatarSize size, ulong steamid ) /// <summary>
/// Try to get the avatar immediately. This should work for people on your friends list.
/// </summary>
public Image GetCachedAvatar( AvatarSize size, ulong steamid )
{ {
var imageid = 0; var imageid = 0;
switch ( size ) switch (size)
{ {
case AvatarSize.Small: case AvatarSize.Small:
imageid = client.native.friends.GetSmallFriendAvatar( steamid ); imageid = client.native.friends.GetSmallFriendAvatar(steamid);
break; break;
case AvatarSize.Medium: case AvatarSize.Medium:
imageid = client.native.friends.GetMediumFriendAvatar( steamid ); imageid = client.native.friends.GetMediumFriendAvatar(steamid);
break; break;
case AvatarSize.Large: case AvatarSize.Large:
imageid = client.native.friends.GetLargeFriendAvatar( steamid ); imageid = client.native.friends.GetLargeFriendAvatar(steamid);
break; break;
} }
@ -275,19 +280,53 @@ public Image GetAvatar( AvatarSize size, ulong steamid )
Id = imageid Id = imageid
}; };
if ( imageid == 0 ) if (imageid != 0 && img.TryLoad(client.native.utils))
return img; return img;
if ( img.TryLoad( client.native.utils ) ) return null;
return img;
throw new System.NotImplementedException( "Deferred Avatar Loading Todo" );
// Add to image loading list
//return img;
} }
/// <summary>
/// Callback will be called when the avatar is ready. If we fail to get an
/// avatar, it'll be called with a null Image.
/// </summary>
public void GetAvatar( AvatarSize size, ulong steamid, Action<Image> callback )
{
// Maybe we already have it downloaded?
var image = GetCachedAvatar(size, steamid);
if ( image != null )
{
callback(image);
return;
}
// Lets request it from Steam
if (!client.native.friends.RequestUserInformation(steamid, false))
{
// Steam told us to get fucked
callback(null);
return;
}
PersonaCallbacks.Add( new PersonaCallback
{
SteamId = steamid,
Callback = () =>
{
callback( GetCachedAvatar(size, steamid) );
}
});
}
private class PersonaCallback
{
public ulong SteamId;
public Action Callback;
}
List<PersonaCallback> PersonaCallbacks = new List<PersonaCallback>();
public SteamFriend Get( ulong steamid ) public SteamFriend Get( ulong steamid )
{ {
var f = new SteamFriend() var f = new SteamFriend()
@ -300,5 +339,11 @@ public SteamFriend Get( ulong steamid )
return f; return f;
} }
private void OnPersonaStateChange( PersonaStateChange_t data, bool error )
{
}
} }
} }

View File

@ -377,12 +377,14 @@ public enum MemberStateChange
internal void OnLobbyStateUpdatedAPI(LobbyChatUpdate_t callback, bool error) internal void OnLobbyStateUpdatedAPI(LobbyChatUpdate_t callback, bool error)
{ {
if (error || callback.SteamIDLobby != CurrentLobby) { return; } if (error || callback.SteamIDLobby != CurrentLobby)
return;
MemberStateChange change = (MemberStateChange)callback.GfChatMemberStateChange; MemberStateChange change = (MemberStateChange)callback.GfChatMemberStateChange;
ulong initiator = callback.SteamIDMakingChange; ulong initiator = callback.SteamIDMakingChange;
ulong affected = callback.SteamIDUserChanged; ulong affected = callback.SteamIDUserChanged;
if (OnLobbyStateChanged != null) { OnLobbyStateChanged(change, initiator, affected); } OnLobbyStateChanged?.Invoke(change, initiator, affected);
} }
/// <summary> /// <summary>