From 70c1513c192f751940ebe720d4ba6588cc285850 Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Thu, 15 Feb 2018 12:32:01 +0000 Subject: [PATCH] Fixed avatar downloading --- .../Client/FriendsTest.cs | 20 ++++-- Facepunch.Steamworks/Client.cs | 1 + Facepunch.Steamworks/Client/Friends.cs | 64 ++++++++++++++++--- 3 files changed, 69 insertions(+), 16 deletions(-) diff --git a/Facepunch.Steamworks.Test/Client/FriendsTest.cs b/Facepunch.Steamworks.Test/Client/FriendsTest.cs index d8fc65a..bcaf8a4 100644 --- a/Facepunch.Steamworks.Test/Client/FriendsTest.cs +++ b/Facepunch.Steamworks.Test/Client/FriendsTest.cs @@ -50,16 +50,23 @@ public void Avatar() { Assert.IsTrue( client.IsValid ); - var friend = client.Friends.All.First(); + ulong id = (ulong)( 76561197960279927 + (new Random().Next() % 10000)); bool passed = false; - client.Friends.GetAvatar( Steamworks.Friends.AvatarSize.Medium, friend.Id, (avatar) => + client.Friends.GetAvatar( Steamworks.Friends.AvatarSize.Medium, id, ( avatar) => { - Assert.AreEqual(avatar.Width, 64); - Assert.AreEqual(avatar.Height, 64); - Assert.AreEqual(avatar.Data.Length, avatar.Width * avatar.Height * 4); + if ( avatar == null ) + { + Console.WriteLine( "No Avatar" ); + } + else + { + Assert.AreEqual( avatar.Width, 64 ); + Assert.AreEqual( avatar.Height, 64 ); + Assert.AreEqual( avatar.Data.Length, avatar.Width * avatar.Height * 4 ); - DrawImage(avatar); + DrawImage( avatar ); + } passed = true; }); @@ -105,6 +112,7 @@ public static void DrawImage( Image img ) var brightness = 1 - ((float)(p.r + p.g + p.b) / (255.0f * 3.0f)); var c = (int) ((grad.Length) * brightness); + if ( c > 3 ) c = 3; str += grad[c]; } diff --git a/Facepunch.Steamworks/Client.cs b/Facepunch.Steamworks/Client.cs index 6ffee97..badfc21 100644 --- a/Facepunch.Steamworks/Client.cs +++ b/Facepunch.Steamworks/Client.cs @@ -145,6 +145,7 @@ public override void Update() RunCallbacks(); Voice.Update(); + Friends.Cycle(); base.Update(); } diff --git a/Facepunch.Steamworks/Client/Friends.cs b/Facepunch.Steamworks/Client/Friends.cs index 0aea558..3dbe954 100644 --- a/Facepunch.Steamworks/Client/Friends.cs +++ b/Facepunch.Steamworks/Client/Friends.cs @@ -292,21 +292,24 @@ public Image GetCachedAvatar( AvatarSize size, ulong steamid ) break; } + if ( imageid == 0 || imageid == 2 ) + return null; + var img = new Image() { Id = imageid }; - if (imageid != 0 && img.TryLoad(client.native.utils)) - return img; + if ( !img.TryLoad( client.native.utils ) ) + return null; - return null; + return img; } /// /// Callback will be called when the avatar is ready. If we fail to get an - /// avatar, it'll be called with a null Image. + /// avatar, might be called with a null Image. /// public void GetAvatar( AvatarSize size, ulong steamid, Action callback ) { @@ -329,17 +332,18 @@ public void GetAvatar( AvatarSize size, ulong steamid, Action callback ) PersonaCallbacks.Add( new PersonaCallback { SteamId = steamid, - Callback = () => - { - callback( GetCachedAvatar(size, steamid) ); - } + Size = size, + Callback = callback, + Time = DateTime.Now }); } private class PersonaCallback { public ulong SteamId; - public Action Callback; + public AvatarSize Size; + public Action Callback; + public DateTime Time; } List PersonaCallbacks = new List(); @@ -357,9 +361,49 @@ public SteamFriend Get( ulong steamid ) return f; } + internal void Cycle() + { + if ( PersonaCallbacks.Count == 0 ) return; + + var timeOut = DateTime.Now.AddSeconds( -10 ); + + for ( int i = 0; i < PersonaCallbacks.Count; i++ ) + { + var cb = PersonaCallbacks[i]; + + // Timeout + if ( cb.Time < timeOut ) + { + if ( cb.Callback != null ) + { + cb.Callback( null ); + } + + PersonaCallbacks.Remove( cb ); + continue; + } + } + } + private void OnPersonaStateChange( PersonaStateChange_t data ) { - // HUH + if ( (data.ChangeFlags & 0x0040) != 0x0040 ) return; // wait for k_EPersonaChangeAvatar + + for ( int i=0; i< PersonaCallbacks.Count; i++ ) + { + var cb = PersonaCallbacks[i]; + if ( cb.SteamId != data.SteamID ) continue; + + var image = GetCachedAvatar( cb.Size, cb.SteamId ); + if ( image == null ) continue; + + PersonaCallbacks.Remove( cb ); + + if ( cb.Callback != null ) + { + cb.Callback( image ); + } + } } }