Skip to content

Commit

Permalink
Fixed User Images throwing errors when adding to the queue
Browse files Browse the repository at this point in the history
  • Loading branch information
fs-sys committed Aug 15, 2022
1 parent db4cbc4 commit 456ecf7
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 46 deletions.
5 changes: 1 addition & 4 deletions VRCPlates/AssetManager.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Collections;
using System.Reflection;
using MelonLoader;
using UnityEngine;
Expand Down
3 changes: 1 addition & 2 deletions VRCPlates/Compatibility/Compat.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using MelonLoader;


namespace VRCPlates.Compatibility;

public static class Compat
Expand Down
12 changes: 5 additions & 7 deletions VRCPlates/MonoScripts/OldNameplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ public string? ProfilePicture
{
_profilePicture = value;
if (string.IsNullOrEmpty(_profilePicture)) return;
if (_profilePicture is null or "https://files.abidata.io/user_images/00default.png" || _userIcon == null) return;
NameplateManager.ImageQueue?.Add(_profilePicture, _userIcon);
if (_profilePicture is null || _userIcon == null) return;
NameplateManager.AddImageToQueue(_profilePicture, new [] {_userIcon});
}
}

Expand All @@ -243,10 +243,8 @@ public string? PlateBackground

if (_plateBackground == null)
return;
if (_mainBackground != null)
NameplateManager.ImageQueue?.Add(_plateBackground, _mainBackground);
if (_vipBackground != null)
NameplateManager.ImageQueue?.Add(_plateBackground, _vipBackground);
if (_mainBackground != null && _vipBackground != null)
NameplateManager.AddImageToQueue(_plateBackground, new [] {_mainBackground, _vipBackground});
}
}

Expand Down Expand Up @@ -295,7 +293,7 @@ public void Awake()
var descriptor = Nameplate.GetComponentInParent<PlayerDescriptor>();
if (descriptor == null) return;

var player = Utils.GetPlayerEntity(descriptor.ownerId);
var player = PlayerUtils.GetPlayerEntity(descriptor.ownerId);
if (player == null)
{
VRCPlates.NameplateManager.RemoveNameplate(descriptor.ownerId);
Expand Down
71 changes: 59 additions & 12 deletions VRCPlates/NameplateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,72 @@
using UnityEngine.Networking;
using UnityEngine.UI;
using VRCPlates.MonoScripts;
using VRCPlates.Reflection;
using Object = UnityEngine.Object;

namespace VRCPlates;

public class NameplateManager
{
public readonly Dictionary<string, OldNameplate?> Nameplates;
public static Dictionary<string, RawImage>? ImageQueue;
private static Dictionary<string, Texture>? _imageCache;
private static Dictionary<string, RawImage[]>? _imageQueue;

public NameplateManager()
{
Nameplates = new Dictionary<string, OldNameplate?>();
ImageQueue = new Dictionary<string, RawImage>();
_imageCache = new Dictionary<string, Texture>();
_imageQueue = new Dictionary<string, RawImage[]>();

MelonCoroutines.Start(ImageRequestLoop());
}

public static void AddImageToQueue(string id, RawImage[] image)
{
if (_imageQueue != null && _imageCache != null)
{
if (id is not "" or "https://files.abidata.io/user_images/00default.png")
{
if (_imageCache.TryGetValue(id, out var cachedImage))
{
foreach (var im in image)
{
im.texture = cachedImage;
im.transform.parent.gameObject.SetActive(true);
}
}
else
{
if (!_imageQueue.ContainsKey(id))
{
_imageQueue.Add(id, image);
}
}
}
else
{
foreach (var im in image)
{
im.transform.parent.gameObject.SetActive(false);
}
}
}
else
{
VRCPlates.Error("Image Queue is Null");
}
}

private static IEnumerator ImageRequestLoop()
{
while (true)
{
var rateLimit = Settings.RateLimit == null ? 1f : Settings.RateLimit.Value;
if (ImageQueue is {Count: > 0})
_imageQueue = (_imageQueue ?? new Dictionary<string, RawImage[]>()).Where(w => w.Key != null).ToDictionary(w => w.Key, w => w.Value);
if (_imageQueue is {Count: > 0})
{
var pair = ImageQueue.First();
if (pair.Key is not null or "" or "https://files.abidata.io/user_images/00default.png")
var pair = _imageQueue.First(w => w.Key != null);
if (pair.Key != null)
{
using var uwr = UnityWebRequest.Get(pair.Key);
uwr.downloadHandler = new DownloadHandlerTexture();
Expand All @@ -47,21 +87,28 @@ private static IEnumerator ImageRequestLoop()
if (uwr.isNetworkError || uwr.isHttpError)
{
VRCPlates.Warning("Unable to set profile picture: " + uwr.error + "\n" + new StackTrace());
ImageQueue.Remove(pair.Key);
_imageQueue.Remove(pair.Key);
}
else
{
pair.Value.texture = ((DownloadHandlerTexture) uwr.downloadHandler).texture;
pair.Value.transform.parent.gameObject.SetActive(true);
foreach (var im in pair.Value)
{
im.texture = ((DownloadHandlerTexture) uwr.downloadHandler).texture;
im.transform.parent.gameObject.SetActive(true);
}
}
_imageCache?.Add(pair.Key, ((DownloadHandlerTexture) uwr.downloadHandler).texture);
_imageQueue.Remove(pair.Key);
uwr.Dispose();
}
else
{
pair.Value.transform.parent.gameObject.SetActive(false);
VRCPlates.Error("Image Queue Key is Null");
foreach (var im in pair.Value)
{
im.transform.parent.gameObject.SetActive(false);
}
}

if (pair.Key != null) ImageQueue.Remove(pair.Key);
}

yield return new WaitForSeconds(rateLimit);
Expand Down Expand Up @@ -154,7 +201,7 @@ public static void InitializePlate(OldNameplate oldNameplate, PlayerDescriptor?
{
if (playerDescriptor != null)
{
oldNameplate.Player = Utils.GetPlayerEntity(playerDescriptor.ownerId);
oldNameplate.Player = PlayerUtils.GetPlayerEntity(playerDescriptor.ownerId);

if (oldNameplate.Player != null)
{
Expand Down
3 changes: 1 addition & 2 deletions VRCPlates/Patching/Patching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using ABI_RC.Core.InteractionSystem;
using ABI_RC.Core.Networking.IO.Social;
using ABI_RC.Core.Player;
using DarkRift;
using HarmonyLib;
using MelonLoader;
using VRCPlates.Reflection;
Expand Down Expand Up @@ -187,7 +186,7 @@ private static void OnAvatarInstantiated(PuppetMaster __instance)
var descriptor = __instance.GetPlayerDescriptor();
if (descriptor != null)
{
var entity = Utils.GetPlayerEntity(descriptor.ownerId);
var entity = PlayerUtils.GetPlayerEntity(descriptor.ownerId);
if (entity != null)
{
if (VRCPlates.NameplateManager != null) MelonCoroutines.Start(VRCPlates.NameplateManager.CreateNameplate(entity));
Expand Down
14 changes: 13 additions & 1 deletion VRCPlates/Reflection/PlayerUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using System.Diagnostics;
using System.Reflection;
using ABI_RC.Core.Player;
using UnityEngine;

Expand All @@ -18,4 +19,15 @@ public static Animator GetAnimator(this PuppetMaster puppetMaster)
{
return (Animator) AnimatorField.GetValue(puppetMaster);
}

public static CVRPlayerEntity? GetPlayerEntity(string? userID)
{
var player = CVRPlayerManager.Instance.NetworkPlayers.Find(p=> p.Uuid == userID);
if (player != null)
{
return player;
}
VRCPlates.Error("Could not find player entity for user ID: " + userID + "\n" + new StackTrace());
return null;
}
}
16 changes: 1 addition & 15 deletions VRCPlates/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;
using System.Text;
using ABI_RC.Core.Player;
using UnityEngine;
using static UnityEngine.Mathf;

Expand Down Expand Up @@ -46,17 +43,6 @@ public static Color GetColorForSocialRank(string playerApiUserRank) =>
"Developer" => "DEV",
_ => null
};

public static CVRPlayerEntity? GetPlayerEntity(string? userID)
{
var player = CVRPlayerManager.Instance.NetworkPlayers.Find(p=> p.Uuid == userID);
if (player != null)
{
return player;
}
VRCPlates.Error("Could not find player entity for user ID: " + userID + "\n" + new StackTrace());
return null;
}
}

[Serializable]
Expand Down
5 changes: 2 additions & 3 deletions VRCPlates/VRCPlates.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.Collections;
using System.Collections;
using MelonLoader;
using UnityEngine;
using VRCPlates.Compatibility;

[assembly: MelonInfo(typeof(VRCPlates.VRCPlates), "VRCPlates", "1.0.4", ".FS.#8519")]
[assembly: MelonInfo(typeof(VRCPlates.VRCPlates), "VRCPlates", "1.0.5", ".FS.#8519")]
[assembly: MelonGame("Alpha Blend Interactive", "ChilloutVR")]
namespace VRCPlates;

Expand Down

0 comments on commit 456ecf7

Please sign in to comment.