From f5a994abc3c8bb34666dbdca2e3c8ab0caf05208 Mon Sep 17 00:00:00 2001 From: Az <56699741+AlchlcDvl@users.noreply.github.com> Date: Thu, 8 Oct 2026 00:30:57 -0400 Subject: [PATCH] avoid over-allocating objects when reading sprite sheets --- .../Assets/LoadableBundleSubAssetHolder.cs | 40 ++++++++----------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/MiraAPI/Utilities/Assets/LoadableBundleSubAssetHolder.cs b/MiraAPI/Utilities/Assets/LoadableBundleSubAssetHolder.cs index bc2e5c4..85c76ac 100644 --- a/MiraAPI/Utilities/Assets/LoadableBundleSubAssetHolder.cs +++ b/MiraAPI/Utilities/Assets/LoadableBundleSubAssetHolder.cs @@ -1,7 +1,5 @@ using System; -using System.Diagnostics.CodeAnalysis; -using System.Linq; -using HarmonyLib; +using System.Collections.Generic; using Il2CppInterop.Runtime; using Reactor.Utilities.Extensions; using UnityEngine; @@ -47,35 +45,31 @@ public class LoadableBundleSubAssetHolder /// Attempts to load all of the sprite assets within the sprite sheet. /// /// Thrown if the asset is not actually a sprite sheet. - [SuppressMessage("Style", "IDE0270:Use coalesce expression", Justification = "Null coalescing bypasses Unity lifetime checks.")] public void TryInit() { - if (SubSprites.Length == 0) + if (SubSprites.Length != 0) + return; + + var newSprites = new List(); + + foreach (var name in spriteNames) { - var newSprites = Array.Empty(); + var loadedAssets = bundle.LoadAssetWithSubAssets(name, Il2CppType.From(typeof(Sprite))) + ?? throw new InvalidOperationException($"INVALID ASSETS: {name}"); - foreach (var name in spriteNames) + foreach (var obj in loadedAssets) { - var loadedAssets = bundle.LoadAssetWithSubAssets(name, Il2CppType.From(typeof(Sprite))).ToArray(); + var img = obj.TryCast(); - if (loadedAssets == null) - { - throw new InvalidOperationException($"INVALID ASSETS: {name}"); - } + if (img == null) + continue; - foreach (var obj in loadedAssets) - { - var img = obj.TryCast(); - if (img != null) - { - img.DontDestroy().DontUnload(); - newSprites = newSprites.AddToArray(img); - } - } + img.DontDestroy().DontUnload(); + newSprites.Add(img); } - - SubSprites = newSprites; } + + SubSprites = [.. newSprites]; } ///