mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-19 15:51:48 +00:00
avoid over-allocating objects
when reading sprite sheets
This commit is contained in:
1 file changed
+17
-23
@@ -1,7 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using HarmonyLib;
|
|
||||||
using Il2CppInterop.Runtime;
|
using Il2CppInterop.Runtime;
|
||||||
using Reactor.Utilities.Extensions;
|
using Reactor.Utilities.Extensions;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
@@ -47,35 +45,31 @@ public class LoadableBundleSubAssetHolder
|
|||||||
/// Attempts to load all of the sprite assets within the sprite sheet.
|
/// Attempts to load all of the sprite assets within the sprite sheet.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <exception cref="InvalidOperationException">Thrown if the asset is not actually a sprite sheet.</exception>
|
/// <exception cref="InvalidOperationException">Thrown if the asset is not actually a sprite sheet.</exception>
|
||||||
[SuppressMessage("Style", "IDE0270:Use coalesce expression", Justification = "Null coalescing bypasses Unity lifetime checks.")]
|
|
||||||
public void TryInit()
|
public void TryInit()
|
||||||
{
|
{
|
||||||
if (SubSprites.Length == 0)
|
if (SubSprites.Length != 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var newSprites = new List<Sprite>();
|
||||||
|
|
||||||
|
foreach (var name in spriteNames)
|
||||||
{
|
{
|
||||||
var newSprites = Array.Empty<Sprite>();
|
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<Sprite>();
|
||||||
|
|
||||||
if (loadedAssets == null)
|
if (img == null)
|
||||||
{
|
continue;
|
||||||
throw new InvalidOperationException($"INVALID ASSETS: {name}");
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var obj in loadedAssets)
|
img.DontDestroy().DontUnload();
|
||||||
{
|
newSprites.Add(img);
|
||||||
var img = obj.TryCast<Sprite>();
|
|
||||||
if (img != null)
|
|
||||||
{
|
|
||||||
img.DontDestroy().DontUnload();
|
|
||||||
newSprites = newSprites.AddToArray(img);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SubSprites = newSprites;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SubSprites = [.. newSprites];
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in new issue
Block a user