Fix issues with icon loading
This commit is contained in:
@@ -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<Image>()); }
|
||||
catch { Tools.RenderFromBase64(Tools.FixIconData(cachedIcon.Data).Item1, obj.GetComponent<SpriteRenderer>()); }
|
||||
var (d, h) = Tools.FixIconData(cachedIcon.Data);
|
||||
if (d != null && h != null && Tools.Sha512Sum(Convert.FromBase64String(d)) == h)
|
||||
{
|
||||
try { Tools.RenderFromBase64(d, obj.GetComponent<Image>()); }
|
||||
catch { Tools.RenderFromBase64(d, obj.GetComponent<SpriteRenderer>()); }
|
||||
}
|
||||
}
|
||||
Destroy(obj);
|
||||
}
|
||||
|
||||
@@ -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<Image>());
|
||||
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<Image>());
|
||||
newIcon.transform.GetChild(1).GetComponent<TMP_Text>().text = "Bird Name: " + entry.Name;
|
||||
newIcon.transform.GetChild(2).GetComponent<TMP_Text>().text = "Price " + Tools.FormatWithCommas(entry.Price) + " coins";
|
||||
newIcon.transform.GetChild(3).GetComponent<TMP_Text>().text = "Designer Name: " + entry.CreatorUsername;
|
||||
|
||||
@@ -24,7 +24,7 @@ public class Iconsmenu : MonoBehaviour
|
||||
[SerializeField] private GameObject previewBirdObject;
|
||||
[SerializeField] private ColorPanel iconColorPanel;
|
||||
[SerializeField] private ColorPanel overlayColorPanel;
|
||||
private Dictionary<string, Button> customIcons = new();
|
||||
private readonly Dictionary<string, Button> 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<WaitingForCustomIcon>(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<Sprite>("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>();
|
||||
waitingForCustomIcon.ID = icon;
|
||||
CustomIconLoader.Init(new[] { waitingForCustomIcon });
|
||||
previewBird.AddComponent<WaitingForCustomIcon>().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<WaitingForCustomIcon>(FindObjectsSortMode.None);
|
||||
if (objects.Length != 0) CustomIconLoader.Init(objects);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user