Fixed avatar downloading

This commit is contained in:
Garry Newman 2018-02-15 12:32:01 +00:00
parent 8d410969c3
commit 70c1513c19
3 changed files with 69 additions and 16 deletions

View File

@ -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];
}

View File

@ -145,6 +145,7 @@ public override void Update()
RunCallbacks();
Voice.Update();
Friends.Cycle();
base.Update();
}

View File

@ -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;
}
/// <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.
/// avatar, might be called with a null Image.
/// </summary>
public void GetAvatar( AvatarSize size, ulong steamid, Action<Image> callback )
{
@ -329,17 +332,18 @@ public void GetAvatar( AvatarSize size, ulong steamid, Action<Image> 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<Image> Callback;
public DateTime Time;
}
List<PersonaCallback> PersonaCallbacks = new List<PersonaCallback>();
@ -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 );
}
}
}
}