Skip to content

Commit 45eb5b5

Browse files
committed
Handle Steam's AvatarImageLoaded callback
Improves the robustness of avatar loading
1 parent cdddade commit 45eb5b5

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

Source/Client/Networking/SteamIntegration.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static class SteamIntegration
1818
private static Callback<FriendRichPresenceUpdate_t> friendRchpUpdate;
1919
private static Callback<GameRichPresenceJoinRequested_t> gameJoinReq;
2020
private static Callback<PersonaStateChange_t> personaChange;
21+
private static Callback<AvatarImageLoaded_t> avatarLoaded;
2122

2223
public static AppId_t RimWorldAppId;
2324

@@ -69,6 +70,9 @@ public static void InitCallbacks()
6970
personaChange = Callback<PersonaStateChange_t>.Create(change =>
7071
{
7172
});
73+
74+
avatarLoaded =
75+
Callback<AvatarImageLoaded_t>.Create(loaded => SteamImages.GetTexture(loaded.m_iImage, force: true));
7276
}
7377

7478
public static void AcceptPlayerJoinRequest(CSteamID id)
@@ -122,16 +126,16 @@ public static CSteamID GetConnectHostId(CSteamID friend)
122126

123127
public static class SteamImages
124128
{
125-
public static Dictionary<int, Texture2D> cache = new();
129+
private static readonly Dictionary<int, Texture2D> Cache = new();
126130

127-
public static Texture2D GetTexture(int id)
131+
public static Texture2D GetTexture(int id, bool force = false)
128132
{
129-
if (cache.TryGetValue(id, out Texture2D tex))
133+
if (Cache.TryGetValue(id, out Texture2D tex) && !force)
130134
return tex;
131135

132136
if (!SteamUtils.GetImageSize(id, out uint width, out uint height))
133137
{
134-
cache[id] = null;
138+
Cache[id] = null;
135139
return null;
136140
}
137141

@@ -140,7 +144,7 @@ public static Texture2D GetTexture(int id)
140144

141145
if (!SteamUtils.GetImageRGBA(id, data, (int)sizeInBytes))
142146
{
143-
cache[id] = null;
147+
Cache[id] = null;
144148
return null;
145149
}
146150

@@ -149,7 +153,8 @@ public static Texture2D GetTexture(int id)
149153
FlipVertically(tex);
150154
tex.Apply();
151155

152-
cache[id] = tex;
156+
if (Cache.TryGetValue(id, out var oldTex)) UnityEngine.Object.Destroy(oldTex);
157+
Cache[id] = tex;
153158

154159
return tex;
155160
}

0 commit comments

Comments
 (0)