com.alicizax.unity.framework/Runtime/Debugger/DebuggerComponent.ReferencePoolInformationWindow.cs

107 lines
5.3 KiB
C#
Raw Normal View History

using System;
2025-09-05 19:46:30 +08:00
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
2025-09-05 19:46:30 +08:00
namespace AlicizaX.Debugger.Runtime
{
public sealed partial class DebuggerComponent
{
private sealed class ReferencePoolInformationWindow : PollingDebuggerWindowBase
2025-09-05 19:46:30 +08:00
{
private readonly Dictionary<string, List<MemoryPoolInfo>> m_ReferencePoolInfos = new Dictionary<string, List<MemoryPoolInfo>>(StringComparer.Ordinal);
private readonly Comparison<MemoryPoolInfo> m_NormalClassNameComparer = NormalClassNameComparer;
private readonly Comparison<MemoryPoolInfo> m_FullClassNameComparer = FullClassNameComparer;
private bool m_ShowFullClassName;
private Toggle showFullClassNameToggle;
2025-09-05 19:46:30 +08:00
protected override void BuildWindow(VisualElement root)
2025-09-05 19:46:30 +08:00
{
VisualElement overview = CreateSection("Reference Pool Overview", out VisualElement overviewCard);
overviewCard.Add(CreateRow("Enable Strict Check", MemoryPool.EnableStrictCheck.ToString()));
overviewCard.Add(CreateRow("Reference Pool Count", MemoryPool.Count.ToString()));
showFullClassNameToggle = ScrollableDebuggerWindowBase.CreateConsoleFilterToggle("Show Full ClassName", m_ShowFullClassName, DebuggerTheme.PrimaryText, value => m_ShowFullClassName = value);
overviewCard.Add(showFullClassNameToggle);
root.Add(overview);
2025-09-05 19:46:30 +08:00
m_ReferencePoolInfos.Clear();
MemoryPoolInfo[] referencePoolInfos = MemoryPool.GetAllMemoryPoolInfos();
foreach (MemoryPoolInfo referencePoolInfo in referencePoolInfos)
{
string assemblyName = referencePoolInfo.Type.Assembly.GetName().Name;
if (!m_ReferencePoolInfos.TryGetValue(assemblyName, out List<MemoryPoolInfo> results))
2025-09-05 19:46:30 +08:00
{
results = new List<MemoryPoolInfo>();
m_ReferencePoolInfos.Add(assemblyName, results);
}
results.Add(referencePoolInfo);
}
foreach (KeyValuePair<string, List<MemoryPoolInfo>> assemblyReferencePoolInfo in m_ReferencePoolInfos)
{
assemblyReferencePoolInfo.Value.Sort(m_ShowFullClassName ? m_FullClassNameComparer : m_NormalClassNameComparer);
VisualElement section = CreateSection(Utility.Text.Format("Assembly: {0}", assemblyReferencePoolInfo.Key), out VisualElement card);
if (assemblyReferencePoolInfo.Value.Count <= 0)
2025-09-05 19:46:30 +08:00
{
card.Add(CreateRow("State", "Reference Pool is Empty ..."));
}
else
{
for (int i = 0; i < assemblyReferencePoolInfo.Value.Count; i++)
2025-09-05 19:46:30 +08:00
{
MemoryPoolInfo info = assemblyReferencePoolInfo.Value[i];
string title = m_ShowFullClassName ? info.Type.FullName : info.Type.Name;
string content = Utility.Text.Format(
"Unused {0} | Using {1} | Acquire {2} | Release {3} | Add {4} | Remove {5}",
info.UnusedMemoryCount,
info.UsingMemoryCount,
info.AcquireMemoryCount,
info.ReleaseMemoryCount,
info.AddMemoryCount,
info.RemoveMemoryCount);
card.Add(CreatePoolInfoItem(title, content));
2025-09-05 19:46:30 +08:00
}
}
root.Add(section);
2025-09-05 19:46:30 +08:00
}
}
private static int NormalClassNameComparer(MemoryPoolInfo a, MemoryPoolInfo b)
{
return a.Type.Name.CompareTo(b.Type.Name);
}
private static int FullClassNameComparer(MemoryPoolInfo a, MemoryPoolInfo b)
{
return a.Type.FullName.CompareTo(b.Type.FullName);
}
private static VisualElement CreatePoolInfoItem(string title, string content)
{
float scale = DebuggerComponent.Instance != null ? DebuggerComponent.Instance.GetUiScale() : 1f;
VisualElement item = CreateCard();
item.style.marginBottom = 8f * scale;
item.style.backgroundColor = DebuggerTheme.PanelSurfaceAlt;
Label titleLabel = new Label(title ?? string.Empty);
titleLabel.style.color = DebuggerTheme.PrimaryText;
titleLabel.style.fontSize = 16f * scale;
titleLabel.style.unityFontStyleAndWeight = FontStyle.Bold;
titleLabel.style.whiteSpace = WhiteSpace.Normal;
titleLabel.style.marginBottom = 6f * scale;
Label contentLabel = new Label(content ?? string.Empty);
contentLabel.style.color = DebuggerTheme.SecondaryText;
contentLabel.style.fontSize = 14f * scale;
contentLabel.style.whiteSpace = WhiteSpace.Normal;
item.Add(titleLabel);
item.Add(contentLabel);
return item;
}
2025-09-05 19:46:30 +08:00
}
}
}