2 using System.Collections.Immutable;
3 using System.Globalization;
6 using System.Threading.Tasks;
9 using Microsoft.Xna.Framework;
10 using Microsoft.Xna.Framework.Graphics;
11 using Vector2 = Microsoft.Xna.Framework.Vector2;
17 private FriendInfo EgsFriendToFriendInfo(EosInterface.EgsFriend egsFriend)
20 name: egsFriend.DisplayName,
21 id: egsFriend.EpicAccountId,
22 status: egsFriend.Status,
23 serverName: egsFriend.ServerName,
24 connectCommand: ConnectCommand.Parse(egsFriend.ConnectCommand),
30 if (
id is not EpicAccountId friendEaid) {
return Option.None; }
31 var selfEaidOption = Eos.EosAccount.SelfAccountIds.OfType<EpicAccountId>().FirstOrNone();
32 if (!selfEaidOption.TryUnwrap(out var selfEaid)) {
return Option.None; }
34 var friendResult = await EosInterface.Friends.GetFriend(selfEaid, friendEaid);
35 if (!friendResult.TryUnwrapSuccess(out var f)) {
return Option.None; }
37 return Option.Some(EgsFriendToFriendInfo(f));
42 var epicAccountIdOption = Eos.EosAccount.SelfAccountIds.OfType<EpicAccountId>().FirstOrNone();
43 if (!epicAccountIdOption.TryUnwrap(out var epicAccountId)) {
return ImmutableArray<FriendInfo>.Empty; }
45 var friendsResult = await EosInterface.Friends.GetFriends(epicAccountId);
46 if (!friendsResult.TryUnwrapSuccess(out var friends)) {
return ImmutableArray<FriendInfo>.Empty; }
48 return friends.Select(EgsFriendToFriendInfo).ToImmutableArray();
51 private static readonly ImmutableArray<Color> egsProfileColors =
new[]
54 new Color(0xfff0a950),
57 new Color(0xff2b9850),
60 new Color(0xff2ba08e),
63 new Color(0xff951249),
66 new Color(0xff9a0c71),
69 new Color(0xff3e29c6),
72 new Color(0xff3875ed),
80 if (
friend.Id is not EpicAccountId epicAccount) {
return Task.FromResult<Option<Sprite>>(Option.None); }
85 Color color = Color.Black;
86 if (ulong.TryParse(epicAccount.EosStringRepresentation[..16], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var mostSignificant64Bits)
87 && ulong.TryParse(epicAccount.EosStringRepresentation[16..], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var leastSignificant64Bits))
89 BigInteger fullId = mostSignificant64Bits;
91 fullId |= leastSignificant64Bits;
93 BigInteger idMaxValue = ulong.MaxValue;
95 idMaxValue |= ulong.MaxValue;
97 BigInteger middleRangeSize = idMaxValue / 7;
98 BigInteger firstRangeSize = middleRangeSize / 2;
99 if (fullId <= firstRangeSize)
101 color = egsProfileColors[0];
105 color = egsProfileColors[(int)((fullId - firstRangeSize) / middleRangeSize) + 1];
109 char glyphChar =
friend.Name.FallbackNullOrEmpty(
"?")[0];
110 var font = GUIStyle.UnscaledSmallFont.GetFontForStr(glyphChar.ToString());
112 Texture2D tex =
null;
115 var (glyphData, glyphTexture) = font.GetGlyphDataAndTextureForChar(glyphChar);
116 var glyphSize =
new Vector2(glyphData.TexCoords.Width, glyphData.TexCoords.Height);
117 int texSize = (int)Math.Max(
118 MathUtils.RoundUpToPowerOfTwo((uint)(font.LineHeight * 1.5f)),
119 MathUtils.RoundUpToPowerOfTwo((uint)(font.LineHeight * 1.5f)));
121 if (glyphTexture is not
null)
123 var glyphTextureData =
new Color[(int)glyphSize.X * (
int)glyphSize.Y];
124 glyphTexture.GetData(
126 rect: glyphData.TexCoords,
127 data: glyphTextureData,
129 elementCount: glyphTextureData.Length);
131 var texData = Enumerable.Range(0, texSize * texSize).Select(_ => color).ToArray();
132 var start = (
new Vector2(texSize, texSize) / 2 - glyphSize / 2).ToPoint();
133 var end = start + glyphSize.ToPoint();
135 for (
int x = start.X; x < end.X; x++)
137 for (
int y = start.Y; y < end.Y; y++)
139 texData[x + y * texSize] =
143 glyphTextureData[(x - start.X) + (y - start.Y) * (
int)glyphSize.X].A / 255f);
147 tex =
new Texture2D(GameMain.GraphicsDeviceManager.GraphicsDevice, texSize, texSize);
148 tex.SetData(texData);
154 tex =
new Texture2D(GameMain.GraphicsDeviceManager.GraphicsDevice, 2, 2);
155 tex.SetData(
new[] { color, color, color, color });
159 var sprite =
new Sprite(tex,
null,
null);
160 return Task.FromResult(Option.Some(sprite));
165 var epicAccountIdOption = Eos.EosAccount.SelfAccountIds.OfType<EpicAccountId>().FirstOrNone();
166 if (!epicAccountIdOption.TryUnwrap(out var epicAccountId)) {
return ""; }
168 var selfInfoResult = await EosInterface.Friends.GetSelfUserInfo(epicAccountId);
169 if (!selfInfoResult.TryUnwrapSuccess(out var selfInfo)) {
return ""; }
171 return selfInfo.DisplayName;
override Task< Option< Sprite > > RetrieveAvatar(FriendInfo friend, int avatarSize)
override async Task< string > GetSelfUserName()
override async Task< Option< FriendInfo > > RetrieveFriend(AccountId id)
override async Task< ImmutableArray< FriendInfo > > RetrieveFriends()