using System; using System.Collections.Generic; using UnityEngine; public static class RuntimeResourcesCache { private static readonly Dictionary TypedLoadAllCache = new Dictionary(StringComparer.Ordinal); private static readonly Dictionary LoadCache = new Dictionary(StringComparer.Ordinal); [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] private static void ResetStaticState() { TypedLoadAllCache.Clear(); LoadCache.Clear(); } public static T[] LoadAll(string path) where T : UnityEngine.Object { string normalizedPath = NormalizePath(path); string cacheKey = typeof(T).FullName + "|" + normalizedPath; if (TypedLoadAllCache.TryGetValue(cacheKey, out UnityEngine.Object[] cached)) { return cached as T[] ?? Array.Empty(); } T[] loaded = Resources.LoadAll(normalizedPath) ?? Array.Empty(); TypedLoadAllCache[cacheKey] = loaded; return loaded; } public static T Load(string path) where T : UnityEngine.Object { string normalizedPath = NormalizePath(path); string cacheKey = typeof(T).FullName + "|" + normalizedPath; if (LoadCache.TryGetValue(cacheKey, out UnityEngine.Object cached)) { return cached as T; } T loaded = Resources.Load(normalizedPath); LoadCache[cacheKey] = loaded; return loaded; } public static Player_SO LoadDefaultPlayerSo() { Player_SO player = Load("playerdata/playerData_uniqueData"); if (player != null) { return player; } Player_SO[] players = LoadAll(string.Empty); return players.Length > 0 ? players[0] : null; } public static SongData[] LoadAllSongs() { return LoadAll(string.Empty); } public static SongData[] LoadSongIndex() { return LoadAll("song_songIndex"); } public static SongData[] LoadSongsFromPath(string path) { return LoadAll(path); } public static AllyHero_SO[] LoadAllAllyHeroes() { return LoadAll(string.Empty); } public static storeItemSO[] LoadAllStoreItems() { return LoadAll("so/storeSO"); } public static dlcData[] LoadAllDlcs() { return LoadAll(string.Empty); } public static expBottlesSO[] LoadAllExpBottles() { return LoadAll(string.Empty); } public static growthMaterialSO[] LoadAllGrowthMaterials() { return LoadAll(string.Empty); } public static equipmentConsumableSO[] LoadAllEquipmentConsumables() { return LoadAll(string.Empty); } public static mail_so[] LoadAllMailDefinitions() { return LoadAll("so/mail_so"); } public static void InvalidateAll() { TypedLoadAllCache.Clear(); LoadCache.Clear(); } private static string NormalizePath(string path) { return string.IsNullOrWhiteSpace(path) ? string.Empty : path.Trim().Replace("\\", "/"); } }