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
|
|
|
|
|
|
{
|
2026-03-09 20:13:40 +08:00
|
|
|
|
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,
|
|
|
|
|
|
uiMetadata.MetaInfo.CacheTime,
|
|
|
|
|
|
isLoop: false,
|
|
|
|
|
|
isUnscaled: true,
|
|
|
|
|
|
uiMetadata
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2026-03-09 20:13:40 +08:00
|
|
|
|
m_CacheWindow.Add(uiMetadata.MetaInfo.RuntimeTypeHandle, new CacheEntry(uiMetadata, timerId));
|
2025-09-05 19:46:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-24 20:44:36 +08:00
|
|
|
|
private void OnTimerDisposeWindow(object[] args)
|
2025-09-05 19:46:30 +08:00
|
|
|
|
{
|
2025-12-24 20:44:36 +08:00
|
|
|
|
if (args == null || args.Length == 0) return;
|
|
|
|
|
|
|
2025-09-05 19:46:30 +08:00
|
|
|
|
UIMetadata meta = args[0] as UIMetadata;
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void RemoveFromCache(RuntimeTypeHandle typeHandle)
|
|
|
|
|
|
{
|
2026-03-09 20:13:40 +08:00
|
|
|
|
if (m_CacheWindow.TryGetValue(typeHandle, out var entry))
|
2025-09-05 19:46:30 +08:00
|
|
|
|
{
|
|
|
|
|
|
m_CacheWindow.Remove(typeHandle);
|
2026-03-09 20:13:40 +08:00
|
|
|
|
entry.Metadata.InCache = false;
|
|
|
|
|
|
if (entry.TimerId > 0)
|
2025-09-05 19:46:30 +08:00
|
|
|
|
{
|
2026-03-09 20:13:40 +08:00
|
|
|
|
_timerModule.RemoveTimer(entry.TimerId);
|
2025-09-05 19:46:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|