mirror of
https://github.com/Facepunch/Facepunch.Steamworks.git
synced 2024-12-25 06:05:46 +03:00
Friends.GetAvatar now takes a callback rather than returning an Image. Use GetCachedAvatar for legacy behaviour. Also added
This commit is contained in:
parent
54dab0c2a4
commit
6dc680cbab
@ -50,21 +50,43 @@ public void Avatar()
|
||||
Assert.IsTrue( client.IsValid );
|
||||
|
||||
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 );
|
||||
Assert.AreEqual( img.Height, 64 );
|
||||
DrawImage(avatar);
|
||||
passed = true;
|
||||
});
|
||||
|
||||
while ( !img.IsLoaded && !img.IsError )
|
||||
while (passed == false )
|
||||
{
|
||||
client.Update();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -145,6 +145,8 @@ public class Friends
|
||||
internal Friends( Client c )
|
||||
{
|
||||
client = c;
|
||||
|
||||
SteamNative.PersonaStateChange_t.RegisterCallback( client, OnPersonaStateChange );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -253,20 +255,23 @@ public enum AvatarSize
|
||||
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;
|
||||
|
||||
switch ( size )
|
||||
switch (size)
|
||||
{
|
||||
case AvatarSize.Small:
|
||||
imageid = client.native.friends.GetSmallFriendAvatar( steamid );
|
||||
imageid = client.native.friends.GetSmallFriendAvatar(steamid);
|
||||
break;
|
||||
case AvatarSize.Medium:
|
||||
imageid = client.native.friends.GetMediumFriendAvatar( steamid );
|
||||
imageid = client.native.friends.GetMediumFriendAvatar(steamid);
|
||||
break;
|
||||
case AvatarSize.Large:
|
||||
imageid = client.native.friends.GetLargeFriendAvatar( steamid );
|
||||
imageid = client.native.friends.GetLargeFriendAvatar(steamid);
|
||||
break;
|
||||
}
|
||||
|
||||
@ -275,19 +280,53 @@ public Image GetAvatar( AvatarSize size, ulong steamid )
|
||||
Id = imageid
|
||||
};
|
||||
|
||||
if ( imageid == 0 )
|
||||
if (imageid != 0 && img.TryLoad(client.native.utils))
|
||||
return img;
|
||||
|
||||
if ( img.TryLoad( client.native.utils ) )
|
||||
return img;
|
||||
|
||||
throw new System.NotImplementedException( "Deferred Avatar Loading Todo" );
|
||||
// Add to image loading list
|
||||
|
||||
//return img;
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/// <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 )
|
||||
{
|
||||
var f = new SteamFriend()
|
||||
@ -300,5 +339,11 @@ public SteamFriend Get( ulong steamid )
|
||||
|
||||
return f;
|
||||
}
|
||||
|
||||
private void OnPersonaStateChange( PersonaStateChange_t data, bool error )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -377,12 +377,14 @@ public enum MemberStateChange
|
||||
|
||||
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;
|
||||
ulong initiator = callback.SteamIDMakingChange;
|
||||
ulong affected = callback.SteamIDUserChanged;
|
||||
|
||||
if (OnLobbyStateChanged != null) { OnLobbyStateChanged(change, initiator, affected); }
|
||||
OnLobbyStateChanged?.Invoke(change, initiator, affected);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
Loading…
Reference in New Issue
Block a user