From 44b594a7f205fed6e09e20fa3c6b1738ecea8f76 Mon Sep 17 00:00:00 2001 From: Lncvrt Date: Sat, 24 Jan 2026 16:55:54 -0700 Subject: [PATCH] Fix issues with icon loading --- Assets/Scripts/CustomIconLoader.cs | 9 ++- .../IconMarketplaceDownloadIcon.cs | 4 +- Assets/Scripts/IconsMenu.cs | 20 ++++--- Assets/Scripts/Tools.cs | 57 +++++++++++++------ 4 files changed, 60 insertions(+), 30 deletions(-) diff --git a/Assets/Scripts/CustomIconLoader.cs b/Assets/Scripts/CustomIconLoader.cs index cca72d8..46d2e69 100644 --- a/Assets/Scripts/CustomIconLoader.cs +++ b/Assets/Scripts/CustomIconLoader.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using Newtonsoft.Json.Linq; @@ -40,8 +41,12 @@ public class CustomIconLoader : MonoBehaviour var cachedIcon = BazookaManager.Instance.iconCache.FirstOrDefault(icon => icon.ID == obj.ID); if (cachedIcon != null) { - try { Tools.RenderFromBase64(Tools.FixIconData(cachedIcon.Data).Item1, obj.GetComponent()); } - catch { Tools.RenderFromBase64(Tools.FixIconData(cachedIcon.Data).Item1, obj.GetComponent()); } + var (d, h) = Tools.FixIconData(cachedIcon.Data); + if (d != null && h != null && Tools.Sha512Sum(Convert.FromBase64String(d)) == h) + { + try { Tools.RenderFromBase64(d, obj.GetComponent()); } + catch { Tools.RenderFromBase64(d, obj.GetComponent()); } + } } Destroy(obj); } diff --git a/Assets/Scripts/IconMarketplace/IconMarketplaceDownloadIcon.cs b/Assets/Scripts/IconMarketplace/IconMarketplaceDownloadIcon.cs index 7594165..2923efe 100644 --- a/Assets/Scripts/IconMarketplace/IconMarketplaceDownloadIcon.cs +++ b/Assets/Scripts/IconMarketplace/IconMarketplaceDownloadIcon.cs @@ -166,8 +166,8 @@ public class IconMarketplaceDownloadIcon : MonoBehaviour { GameObject newIcon = Instantiate(sample, content.transform); newIcon.name = "IconEntry"; - - Tools.RenderFromBase64(Tools.FixIconData(entry.Data).Item1, newIcon.transform.GetChild(0).GetChild(0).GetComponent()); + var (d, h) = Tools.FixIconData(entry.Data); + if (d != null && h != null && Tools.Sha512Sum(Convert.FromBase64String(d)) == h) Tools.RenderFromBase64(d, newIcon.transform.GetChild(0).GetChild(0).GetComponent()); newIcon.transform.GetChild(1).GetComponent().text = "Bird Name: " + entry.Name; newIcon.transform.GetChild(2).GetComponent().text = "Price " + Tools.FormatWithCommas(entry.Price) + " coins"; newIcon.transform.GetChild(3).GetComponent().text = "Designer Name: " + entry.CreatorUsername; diff --git a/Assets/Scripts/IconsMenu.cs b/Assets/Scripts/IconsMenu.cs index 24bf694..fd4f7ac 100644 --- a/Assets/Scripts/IconsMenu.cs +++ b/Assets/Scripts/IconsMenu.cs @@ -24,7 +24,7 @@ public class Iconsmenu : MonoBehaviour [SerializeField] private GameObject previewBirdObject; [SerializeField] private ColorPanel iconColorPanel; [SerializeField] private ColorPanel overlayColorPanel; - private Dictionary customIcons = new(); + private readonly Dictionary customIcons = new(); private void Start() { @@ -136,12 +136,11 @@ public class Iconsmenu : MonoBehaviour if (customIconData.Selected != null) foreach (var icon in customIconData.Purchased) { - if (icon == customIconData.Selected) SelectCustomIcon(icon); + if (icon == customIconData.Selected) SelectCustomIcon(icon, false); customIcons[icon].interactable = icon != customIconData.Selected; } - var objects = FindObjectsByType(FindObjectsSortMode.None); - if (objects.Length != 0) CustomIconLoader.Init(objects); + RenderIcons(); } private void ToggleKit() @@ -217,14 +216,13 @@ public class Iconsmenu : MonoBehaviour else previewOverlay.sprite = Resources.Load("Icons/Overlays/overlay_" + overlayID); } - void SelectCustomIcon(string icon) + void SelectCustomIcon(string icon, bool renderIcons = true) { var customData = BazookaManager.Instance.GetCustomBirdIconData(); customData.Selected = icon; BazookaManager.Instance.SetCustomBirdIconData(customData); - var waitingForCustomIcon = previewBird.AddComponent(); - waitingForCustomIcon.ID = icon; - CustomIconLoader.Init(new[] { waitingForCustomIcon }); + previewBird.AddComponent().ID = icon; + if (renderIcons) RenderIcons(); previewBird.color = Color.white; previewOverlay.gameObject.SetActive(false); previewOverlay.sprite = null; @@ -239,4 +237,10 @@ public class Iconsmenu : MonoBehaviour { if (Keyboard.current.escapeKey.wasPressedThisFrame) await SceneManager.LoadSceneAsync("MainMenu"); } + + void RenderIcons() + { + var objects = FindObjectsByType(FindObjectsSortMode.None); + if (objects.Length != 0) CustomIconLoader.Init(objects); + } } \ No newline at end of file diff --git a/Assets/Scripts/Tools.cs b/Assets/Scripts/Tools.cs index 09f0662..39b1eba 100644 --- a/Assets/Scripts/Tools.cs +++ b/Assets/Scripts/Tools.cs @@ -1,5 +1,7 @@ using System; using System.Numerics; +using System.Security.Cryptography; +using System.Text; using TMPro; using UnityEngine; using UnityEngine.UI; @@ -66,26 +68,45 @@ public static class Tools public static (string, string) FixIconData(string i) { - int t = i.Length; - int h = 128; - int d = t - h; - int a = d / 4; - int b = h / 4; + try + { + int t = i.Length; + int h = 128; + int d = t - h; + int a = d / 4; + int b = h / 4; - string D = string.Concat( - i.Substring(0, a), - i.Substring(a + b, a), - i.Substring(a * 2 + b * 2, a), - i.Substring(a * 3 + b * 3, d - a * 3) - ); + string D = string.Concat( + i.Substring(0, a), + i.Substring(a + b, a), + i.Substring(a * 2 + b * 2, a), + i.Substring(a * 3 + b * 3, d - a * 3) + ); - string H = string.Concat( - i.Substring(a, b), - i.Substring(a * 2 + b, b), - i.Substring(a * 3 + b * 2, b), - i.Substring(a * 4 + b * 3, b) - ); + string H = string.Concat( + i.Substring(a, b), + i.Substring(a * 2 + b, b), + i.Substring(a * 3 + b * 2, b), + i.Substring(a * 4 + b * 3, b) + ); - return (D, H); + return (D, H); + } + catch + { + return (null, null); + } + } + + public static string Sha512Sum(byte[] data) + { + using var sha512 = SHA512.Create(); + var hash = sha512.ComputeHash(data); + + var sb = new StringBuilder(hash.Length * 2); + foreach (var b in hash) + sb.Append(b.ToString("x2")); + + return sb.ToString(); } } \ No newline at end of file