com.alicizax.unity.framework/Runtime/UI/Manager/UIModule.Cache.cs

84 lines
2.5 KiB
C#
Raw Normal View History

2025-09-05 19:46:30 +08:00
using System;
using System.Collections.Generic;
using AlicizaX;
namespace AlicizaX.UI.Runtime
{
internal sealed partial class UIModule
{
private readonly struct CacheEntry
{
public readonly UIMetadata Metadata;
public readonly int TimerId;
public CacheEntry(UIMetadata metadata, int timerId)
{
Metadata = metadata;
TimerId = timerId;
}
}
private readonly Dictionary<RuntimeTypeHandle, CacheEntry> m_CacheWindow = new();
2025-09-05 19:46:30 +08:00
private void CacheWindow(UIMetadata uiMetadata, bool force)
{
2025-12-24 20:44:36 +08:00
if (uiMetadata?.View?.Holder == null)
2025-09-05 19:46:30 +08:00
{
2025-12-24 20:44:36 +08:00
Log.Error("Cannot cache null UI metadata or holder");
2025-09-05 19:46:30 +08:00
return;
}
if (force || uiMetadata.MetaInfo.CacheTime == 0)
{
uiMetadata.Dispose();
return;
}
RemoveFromCache(uiMetadata.MetaInfo.RuntimeTypeHandle);
2025-12-24 20:44:36 +08:00
int timerId = -1;
2025-09-05 19:46:30 +08:00
uiMetadata.View.Holder.transform.SetParent(UICacheLayer);
if (uiMetadata.MetaInfo.CacheTime > 0)
{
2025-12-24 20:44:36 +08:00
timerId = _timerModule.AddTimer(
OnTimerDisposeWindow,
2026-03-24 17:45:15 +08:00
uiMetadata,
2025-12-24 20:44:36 +08:00
uiMetadata.MetaInfo.CacheTime,
isLoop: false,
2026-03-24 17:45:15 +08:00
isUnscaled: true
2025-12-24 20:44:36 +08:00
);
if (timerId <= 0)
{
Log.Warning($"Failed to create cache timer for {uiMetadata.UILogicType.Name}");
}
2025-09-05 19:46:30 +08:00
}
uiMetadata.InCache = true;
m_CacheWindow.Add(uiMetadata.MetaInfo.RuntimeTypeHandle, new CacheEntry(uiMetadata, timerId));
2025-09-05 19:46:30 +08:00
}
2026-03-24 17:45:15 +08:00
private void OnTimerDisposeWindow(UIMetadata meta)
2025-09-05 19:46:30 +08:00
{
2025-12-24 20:44:36 +08:00
if (meta != null)
{
meta.Dispose();
RemoveFromCache(meta.MetaInfo.RuntimeTypeHandle);
}
2025-09-05 19:46:30 +08:00
}
2026-03-24 17:45:15 +08:00
private void RemoveFromCache(RuntimeTypeHandle typeHandle)
2025-09-05 19:46:30 +08:00
{
if (m_CacheWindow.TryGetValue(typeHandle, out var entry))
2025-09-05 19:46:30 +08:00
{
m_CacheWindow.Remove(typeHandle);
entry.Metadata.InCache = false;
if (entry.TimerId > 0)
2025-09-05 19:46:30 +08:00
{
_timerModule.RemoveTimer(entry.TimerId);
2025-09-05 19:46:30 +08:00
}
}
}
}
}