Files
bansonic_beta_main/Assets/scripts/_runtimecache/RuntimeResourcesCache.cs
T
2026-06-25 11:48:56 +08:00

165 lines
4.7 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
public static class RuntimeResourcesCache
{
private static readonly Dictionary<string, UnityEngine.Object[]> TypedLoadAllCache =
new Dictionary<string, UnityEngine.Object[]>(StringComparer.Ordinal);
private static readonly Dictionary<string, UnityEngine.Object> LoadCache =
new Dictionary<string, UnityEngine.Object>(StringComparer.Ordinal);
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticState()
{
TypedLoadAllCache.Clear();
LoadCache.Clear();
}
public static T[] LoadAll<T>(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>();
}
T[] loaded = Resources.LoadAll<T>(normalizedPath) ?? Array.Empty<T>();
TypedLoadAllCache[cacheKey] = loaded;
return loaded;
}
public static T Load<T>(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<T>(normalizedPath);
LoadCache[cacheKey] = loaded;
return loaded;
}
public static Player_SO LoadDefaultPlayerSo()
{
Player_SO player = Load<Player_SO>("playerdata/playerData_uniqueData");
if (player != null)
{
return player;
}
Player_SO[] players = LoadAll<Player_SO>(string.Empty);
return players.Length > 0 ? players[0] : null;
}
public static SongData[] LoadAllSongs()
{
return MergeArrays(LoadAll<SongData>(string.Empty), DlcRuntimeRegistry.GetAllSongs());
}
public static SongData[] LoadSongIndex()
{
return MergeArrays(LoadAll<SongData>("song_songIndex"), DlcRuntimeRegistry.GetAllSongs());
}
public static SongData[] LoadSongsFromPath(string path)
{
return MergeArrays(LoadAll<SongData>(path), DlcRuntimeRegistry.GetAllSongs());
}
public static AllyHero_SO[] LoadAllAllyHeroes()
{
return LoadAll<AllyHero_SO>(string.Empty);
}
public static storeItemSO[] LoadAllStoreItems()
{
return LoadAll<storeItemSO>("so/storeSO");
}
public static dlcData[] LoadAllDlcs()
{
return MergeArrays(LoadAll<dlcData>(string.Empty), DlcRuntimeRegistry.GetAllDlcs());
}
public static HeroSkinSO[] LoadAllHeroSkins()
{
return MergeArrays(LoadAll<HeroSkinSO>("so/heroSkins"), DlcRuntimeRegistry.GetAllHeroSkins());
}
public static SongDlcContentSO[] LoadAllSongDlcContents()
{
return MergeArrays(LoadAll<SongDlcContentSO>("so/songDlcContents"), DlcRuntimeRegistry.GetAllSongContents());
}
public static expBottlesSO[] LoadAllExpBottles()
{
return LoadAll<expBottlesSO>(string.Empty);
}
public static growthMaterialSO[] LoadAllGrowthMaterials()
{
return LoadAll<growthMaterialSO>(string.Empty);
}
public static equipmentConsumableSO[] LoadAllEquipmentConsumables()
{
return LoadAll<equipmentConsumableSO>(string.Empty);
}
public static mail_so[] LoadAllMailDefinitions()
{
return LoadAll<mail_so>("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>(T[] builtIn, T[] runtime) where T : UnityEngine.Object
{
if (runtime == null || runtime.Length == 0)
{
return builtIn ?? Array.Empty<T>();
}
if (builtIn == null || builtIn.Length == 0)
{
return runtime;
}
List<T> result = new List<T>(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();
}
}