Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 deletions Source/Client/Networking/SteamIntegration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using LudeonTK;
using Multiplayer.Client.Networking;
using Multiplayer.Client.Util;
using Multiplayer.Client.Windows;
Expand All @@ -18,6 +19,7 @@ public static class SteamIntegration
private static Callback<FriendRichPresenceUpdate_t> friendRchpUpdate;
private static Callback<GameRichPresenceJoinRequested_t> gameJoinReq;
private static Callback<PersonaStateChange_t> personaChange;
private static Callback<AvatarImageLoaded_t> avatarLoaded;

public static AppId_t RimWorldAppId;

Expand Down Expand Up @@ -68,7 +70,14 @@ public static void InitCallbacks()

personaChange = Callback<PersonaStateChange_t>.Create(change =>
{
// When a persona's avatar changes, the avatar id changes too. It's not a problem for us because we
// query the avatar id every frame. It'd be nice to remove the old avatar from the SteamImages cache,
// but realistically it's not an issue. (Also, it'd require keeping track of the avatar's owner because
// I don't think there's a way to query the old avatar id to easily remove it.)
});

avatarLoaded =
Callback<AvatarImageLoaded_t>.Create(loaded => SteamImages.GetTexture(loaded.m_iImage, force: true));
}

public static void AcceptPlayerJoinRequest(CSteamID id)
Expand Down Expand Up @@ -122,17 +131,24 @@ public static CSteamID GetConnectHostId(CSteamID friend)

public static class SteamImages
{
public static Dictionary<int, Texture2D> cache = new();
private static readonly Dictionary<int, Texture2D> Cache = new();

[DebugAction(category = MpDebugActions.MultiplayerCategory, name = "Clear image cache",
allowedGameStates = AllowedGameStates.Entry)]
private static void ClearCache()
{
foreach (var tex in Cache.Values) UnityEngine.Object.Destroy(tex);
Cache.Clear();
}

// Remember to flip it
public static Texture2D GetTexture(int id)
public static Texture2D GetTexture(int id, bool force = false)
{
if (cache.TryGetValue(id, out Texture2D tex))
if (Cache.TryGetValue(id, out Texture2D tex) && !force)
return tex;

if (!SteamUtils.GetImageSize(id, out uint width, out uint height))
{
cache[id] = null;
Cache[id] = null;
return null;
}

Expand All @@ -141,18 +157,36 @@ public static Texture2D GetTexture(int id)

if (!SteamUtils.GetImageRGBA(id, data, (int)sizeInBytes))
{
cache[id] = null;
Cache[id] = null;
return null;
}

tex = new Texture2D((int)width, (int)height, TextureFormat.RGBA32, false);
tex.LoadRawTextureData(data);
FlipVertically(tex);
tex.Apply();

cache[id] = tex;
if (Cache.TryGetValue(id, out var oldTex)) UnityEngine.Object.Destroy(oldTex);
Cache[id] = tex;

return tex;
}

private static void FlipVertically(Texture2D tex)
{
var pixels = tex.GetPixels32();
var buf = new Color32[tex.width];

for (int y = 0; y < tex.height / 2; y++)
{
var reversedY = tex.height - y - 1;
Array.Copy(pixels, y * tex.width, buf, 0, tex.width);
Array.Copy(pixels, reversedY * tex.width, pixels, y * tex.width, tex.width);
Array.Copy(buf, 0, pixels, reversedY * tex.width, tex.width);
}

tex.SetPixels32(pixels);
}
}

}
2 changes: 1 addition & 1 deletion Source/Client/Windows/PendingPlayerWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public override void DoWindowContents(Rect inRect)
var avatarRect = new Rect(0, 0, 80, 80).CenteredOnYIn(inRect).Right(4);
InvisibleOpenSteamProfileButton(avatarRect, req.steamId, doMouseoverSound: false);
if (avatarTex != null)
GUI.DrawTextureWithTexCoords(avatarRect, avatarTex, new Rect(0, 1, 1, -1));
GUI.DrawTexture(avatarRect, avatarTex);
inRect.xMin = avatarRect.xMax + 6f;
}
else
Expand Down
2 changes: 1 addition & 1 deletion Source/Client/Windows/ServerBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ private void DrawSteam(Rect inRect)
Widgets.DrawAltRect(entryRect);

if (Event.current.type == EventType.Repaint)
GUI.DrawTextureWithTexCoords(new Rect(5, entryRect.y + 4, 32, 32), SteamImages.GetTexture(friend.avatar), new Rect(0, 1, 1, -1));
GUI.DrawTexture(new Rect(5, entryRect.y + 4, 32, 32), SteamImages.GetTexture(friend.avatar));

using (MpStyle.Set(TextAnchor.MiddleLeft))
Widgets.Label(entryRect.Right(45).Up(5), friend.username);
Expand Down
Loading