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 MergeArrays(LoadAll(string.Empty), DlcRuntimeRegistry.GetAllSongs()); } public static SongData[] LoadSongIndex() { return MergeArrays(LoadAll("song_songIndex"), DlcRuntimeRegistry.GetAllSongs()); } public static SongData[] LoadSongsFromPath(string path) { return MergeArrays(LoadAll(path), DlcRuntimeRegistry.GetAllSongs()); } public static AllyHero_SO[] LoadAllAllyHeroes() { return MergeArrays(LoadAll(string.Empty), DlcRuntimeRegistry.GetAllHeroes()); } public static storeItemSO[] LoadAllStoreItems() { return LoadAll("so/storeSO"); } public static dlcData[] LoadAllDlcs() { return MergeArrays(LoadAll(string.Empty), DlcRuntimeRegistry.GetAllDlcs()); } public static HeroSkinSO[] LoadAllHeroSkins() { return MergeArrays(LoadAll("so/heroSkins"), DlcRuntimeRegistry.GetAllHeroSkins()); } public static SongDlcContentSO[] LoadAllSongDlcContents() { return MergeArrays(LoadAll("so/songDlcContents"), DlcRuntimeRegistry.GetAllSongContents()); } 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("\\", "/"); } private static T[] MergeArrays(T[] builtIn, T[] runtime) where T : UnityEngine.Object { if (runtime == null || runtime.Length == 0) { return builtIn ?? Array.Empty(); } if (builtIn == null || builtIn.Length == 0) { return runtime; } List result = new List(builtIn.Length + runtime.Length); for (int i = 0; i < builtIn.Length; i++) { T item = builtIn[i]; if (item != null && !result.Contains(item)) { result.Add(item); } } for (int i = 0; i < runtime.Length; i++) { T item = runtime[i]; if (item != null && !result.Contains(item)) { result.Add(item); } } return result.ToArray(); } }