using System; using System.Collections.Generic; using AlicizaX; namespace AlicizaX.UI.Runtime { internal sealed partial class UIModule { private readonly Dictionary m_CacheWindow = new(); private void CacheWindow(UIMetadata uiMetadata, bool force) { if (uiMetadata?.View?.Holder == null) { Log.Error("Cannot cache null UI metadata or holder"); return; } if (force || uiMetadata.MetaInfo.CacheTime == 0) { uiMetadata.Dispose(); return; } RemoveFromCache(uiMetadata.MetaInfo.RuntimeTypeHandle); int timerId = -1; uiMetadata.View.Holder.transform.SetParent(UICacheLayer); if (uiMetadata.MetaInfo.CacheTime > 0) { timerId = _timerModule.AddTimer( OnTimerDisposeWindow, uiMetadata.MetaInfo.CacheTime, isLoop: false, isUnscaled: true, uiMetadata ); if (timerId <= 0) { Log.Warning($"Failed to create cache timer for {uiMetadata.UILogicType.Name}"); } } uiMetadata.InCache = true; m_CacheWindow.Add(uiMetadata.MetaInfo.RuntimeTypeHandle, (uiMetadata, timerId)); } private void OnTimerDisposeWindow(object[] args) { if (args == null || args.Length == 0) return; UIMetadata meta = args[0] as UIMetadata; if (meta != null) { meta.Dispose(); RemoveFromCache(meta.MetaInfo.RuntimeTypeHandle); } } private void RemoveFromCache(RuntimeTypeHandle typeHandle) { if (m_CacheWindow.TryGetValue(typeHandle, out var result)) { m_CacheWindow.Remove(typeHandle); result.Item1.InCache = false; if (result.Item2 > 0) { _timerModule.RemoveTimer(result.Item2); } } } } }