Client LuaCsForBarotrauma
SteamFriendProvider.cs
1 #nullable enable
2 using System;
3 using System.Collections.Immutable;
4 using System.Linq;
5 using System.Threading.Tasks;
7 using Barotrauma.Steam;
8 using Microsoft.Xna.Framework.Graphics;
9 
10 namespace Barotrauma
11 {
13  {
14  private FriendInfo FromSteamFriend(Steamworks.Friend steamFriend)
15  => new FriendInfo(
16  name: steamFriend.Name ?? "",
17  id: new SteamId(steamFriend.Id),
18  status: steamFriend.State switch
19  {
20  Steamworks.FriendState.Offline => FriendStatus.Offline,
21  Steamworks.FriendState.Invisible => FriendStatus.Offline,
22  _ when steamFriend.IsPlayingThisGame => FriendStatus.PlayingBarotrauma,
23  _ when steamFriend.GameInfo is { GameID: > 0 } => FriendStatus.PlayingAnotherGame,
24  _ => FriendStatus.NotPlaying
25  },
26  serverName: steamFriend.GetRichPresence("servername") ?? "",
27  connectCommand: steamFriend.GetRichPresence("connect") is { } connectCmd
28  ? ConnectCommand.Parse(ToolBox.SplitCommand(connectCmd))
29  : Option.None,
30  this);
31 
32  public override Task<Option<FriendInfo>> RetrieveFriend(AccountId id)
33  => Task.FromResult(id is SteamId steamId
34  ? Option.Some(FromSteamFriend(new Steamworks.Friend(steamId.Value)))
35  : Option.None);
36 
37  public override Task<ImmutableArray<FriendInfo>> RetrieveFriends()
38  => Task.FromResult(SteamManager.IsInitialized
39  ? Steamworks.SteamFriends.GetFriends().Select(FromSteamFriend).ToImmutableArray()
40  : ImmutableArray<FriendInfo>.Empty);
41 
42  public override async Task<Option<Sprite>> RetrieveAvatar(FriendInfo friend, int avatarSize)
43  {
44  if (friend.Id is not SteamId steamId) { return Option.None; }
45 
46  Func<Steamworks.SteamId, Task<Steamworks.Data.Image?>> avatarFunc = avatarSize switch
47  {
48  <= 24 => Steamworks.SteamFriends.GetSmallAvatarAsync,
49  <= 48 => Steamworks.SteamFriends.GetMediumAvatarAsync,
50  _ => Steamworks.SteamFriends.GetLargeAvatarAsync
51  };
52 
53  var img = await avatarFunc(steamId.Value).ToOptionTask();
54  if (!img.TryUnwrap(out var avatarImage)) { return Option.None; }
55 
56  if (friend.Avatar.TryUnwrap(out var prevAvatar))
57  {
58  prevAvatar.Remove();
59  }
60 
61  var avatarTexture = new Texture2D(GameMain.Instance.GraphicsDevice, (int)avatarImage.Width, (int)avatarImage.Height);
62  avatarTexture.SetData(avatarImage.Data);
63  return Option.Some(new Sprite(texture: avatarTexture, sourceRectangle: null, newOffset: null));
64  }
65 
66  public override Task<string> GetSelfUserName()
67  => Task.FromResult(SteamManager.GetUsername());
68  }
69 }
static GameMain Instance
Definition: GameMain.cs:144
override Task< string > GetSelfUserName()
override Task< ImmutableArray< FriendInfo > > RetrieveFriends()
override async Task< Option< Sprite > > RetrieveAvatar(FriendInfo friend, int avatarSize)
override Task< Option< FriendInfo > > RetrieveFriend(AccountId id)