From 456ecf77f73091d6a9e8c00401071bb9e98a52f1 Mon Sep 17 00:00:00 2001 From: FS <47338369+TCOSFS@users.noreply.github.com> Date: Mon, 15 Aug 2022 12:48:40 -0500 Subject: [PATCH] Fixed User Images throwing errors when adding to the queue --- VRCPlates/AssetManager.cs | 5 +- VRCPlates/Compatibility/Compat.cs | 3 +- VRCPlates/MonoScripts/OldNameplate.cs | 12 ++--- VRCPlates/NameplateManager.cs | 71 ++++++++++++++++++++++----- VRCPlates/Patching/Patching.cs | 3 +- VRCPlates/Reflection/PlayerUtils.cs | 14 +++++- VRCPlates/Utils.cs | 16 +----- VRCPlates/VRCPlates.cs | 5 +- 8 files changed, 83 insertions(+), 46 deletions(-) diff --git a/VRCPlates/AssetManager.cs b/VRCPlates/AssetManager.cs index beecaf9..fc099eb 100644 --- a/VRCPlates/AssetManager.cs +++ b/VRCPlates/AssetManager.cs @@ -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; diff --git a/VRCPlates/Compatibility/Compat.cs b/VRCPlates/Compatibility/Compat.cs index c9a7195..2505711 100644 --- a/VRCPlates/Compatibility/Compat.cs +++ b/VRCPlates/Compatibility/Compat.cs @@ -1,5 +1,4 @@ -using MelonLoader; - + namespace VRCPlates.Compatibility; public static class Compat diff --git a/VRCPlates/MonoScripts/OldNameplate.cs b/VRCPlates/MonoScripts/OldNameplate.cs index 1f859f9..cb4a867 100644 --- a/VRCPlates/MonoScripts/OldNameplate.cs +++ b/VRCPlates/MonoScripts/OldNameplate.cs @@ -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}); } } @@ -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}); } } @@ -295,7 +293,7 @@ public void Awake() var descriptor = Nameplate.GetComponentInParent(); if (descriptor == null) return; - var player = Utils.GetPlayerEntity(descriptor.ownerId); + var player = PlayerUtils.GetPlayerEntity(descriptor.ownerId); if (player == null) { VRCPlates.NameplateManager.RemoveNameplate(descriptor.ownerId); diff --git a/VRCPlates/NameplateManager.cs b/VRCPlates/NameplateManager.cs index b026a04..4bef985 100644 --- a/VRCPlates/NameplateManager.cs +++ b/VRCPlates/NameplateManager.cs @@ -8,6 +8,7 @@ using UnityEngine.Networking; using UnityEngine.UI; using VRCPlates.MonoScripts; +using VRCPlates.Reflection; using Object = UnityEngine.Object; namespace VRCPlates; @@ -15,25 +16,64 @@ namespace VRCPlates; public class NameplateManager { public readonly Dictionary Nameplates; - public static Dictionary? ImageQueue; + private static Dictionary? _imageCache; + private static Dictionary? _imageQueue; public NameplateManager() { Nameplates = new Dictionary(); - ImageQueue = new Dictionary(); + _imageCache = new Dictionary(); + _imageQueue = new Dictionary(); 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()).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(); @@ -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); @@ -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) { diff --git a/VRCPlates/Patching/Patching.cs b/VRCPlates/Patching/Patching.cs index 7a81cad..3708aaa 100644 --- a/VRCPlates/Patching/Patching.cs +++ b/VRCPlates/Patching/Patching.cs @@ -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; @@ -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)); diff --git a/VRCPlates/Reflection/PlayerUtils.cs b/VRCPlates/Reflection/PlayerUtils.cs index 15dc8ba..e539a18 100644 --- a/VRCPlates/Reflection/PlayerUtils.cs +++ b/VRCPlates/Reflection/PlayerUtils.cs @@ -1,4 +1,5 @@ -using System.Reflection; +using System.Diagnostics; +using System.Reflection; using ABI_RC.Core.Player; using UnityEngine; @@ -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; + } } \ No newline at end of file diff --git a/VRCPlates/Utils.cs b/VRCPlates/Utils.cs index 5288bf2..4ea655a 100644 --- a/VRCPlates/Utils.cs +++ b/VRCPlates/Utils.cs @@ -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; @@ -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] diff --git a/VRCPlates/VRCPlates.cs b/VRCPlates/VRCPlates.cs index 965f5bb..dc5a08d 100644 --- a/VRCPlates/VRCPlates.cs +++ b/VRCPlates/VRCPlates.cs @@ -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;