2021-04-26 19:56:21 +10:00
|
|
|
|
using System;
|
2021-04-27 21:22:48 +10:00
|
|
|
|
using System.Collections;
|
2021-04-26 19:56:21 +10:00
|
|
|
|
using System.Collections.Generic;
|
2021-04-27 21:22:48 +10:00
|
|
|
|
using System.IO;
|
2021-04-26 19:56:21 +10:00
|
|
|
|
using System.Linq;
|
2021-04-27 21:22:48 +10:00
|
|
|
|
using System.Reflection;
|
2021-04-26 19:56:21 +10:00
|
|
|
|
using System.Text;
|
|
|
|
|
using UnityEngine;
|
2021-04-27 21:22:48 +10:00
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
using UnityExplorer.UI.Inspectors.CacheObject;
|
|
|
|
|
using UnityExplorer.UI.Inspectors.CacheObject.Views;
|
|
|
|
|
using UnityExplorer.UI.ObjectPool;
|
|
|
|
|
using UnityExplorer.UI.Panels;
|
|
|
|
|
using UnityExplorer.UI.Utility;
|
|
|
|
|
using UnityExplorer.UI.Widgets;
|
2021-04-26 19:56:21 +10:00
|
|
|
|
|
|
|
|
|
namespace UnityExplorer.UI.Inspectors
|
|
|
|
|
{
|
2021-05-03 01:29:02 +10:00
|
|
|
|
public class ReflectionInspector : InspectorBase, IPoolDataSource<CacheMemberCell>, ICacheObjectController
|
2021-04-27 21:22:48 +10:00
|
|
|
|
{
|
2021-05-03 01:29:02 +10:00
|
|
|
|
public CacheObjectBase ParentCacheObject { get; set; }
|
|
|
|
|
|
2021-04-27 21:22:48 +10:00
|
|
|
|
public bool StaticOnly { get; internal set; }
|
2021-04-26 19:56:21 +10:00
|
|
|
|
|
2021-05-03 01:29:02 +10:00
|
|
|
|
//public object Target { get; private set; }
|
2021-04-27 21:22:48 +10:00
|
|
|
|
public Type TargetType { get; private set; }
|
2021-05-03 01:29:02 +10:00
|
|
|
|
public bool CanWrite => true;
|
2021-04-26 19:56:21 +10:00
|
|
|
|
|
2021-04-27 21:22:48 +10:00
|
|
|
|
public ScrollPool<CacheMemberCell> MemberScrollPool { get; private set; }
|
|
|
|
|
|
|
|
|
|
private List<CacheMember> members = new List<CacheMember>();
|
|
|
|
|
private readonly List<CacheMember> filteredMembers = new List<CacheMember>();
|
2021-04-29 21:01:08 +10:00
|
|
|
|
private readonly HashSet<CacheMember> displayedMembers = new HashSet<CacheMember>();
|
2021-04-27 21:22:48 +10:00
|
|
|
|
|
|
|
|
|
public Text NameText;
|
|
|
|
|
public Text AssemblyText;
|
|
|
|
|
|
|
|
|
|
private LayoutElement memberTitleLayout;
|
|
|
|
|
|
2021-05-01 20:55:27 +10:00
|
|
|
|
public bool AutoUpdateWanted { get; set; }
|
2021-04-30 21:34:50 +10:00
|
|
|
|
private Toggle autoUpdateToggle;
|
|
|
|
|
|
2021-04-27 21:22:48 +10:00
|
|
|
|
public override void OnBorrowedFromPool(object target)
|
|
|
|
|
{
|
|
|
|
|
base.OnBorrowedFromPool(target);
|
|
|
|
|
|
|
|
|
|
SetTitleLayouts();
|
|
|
|
|
SetTarget(target);
|
|
|
|
|
|
2021-04-30 23:12:18 +10:00
|
|
|
|
MemberScrollPool.Refresh(true, true);
|
2021-04-27 21:22:48 +10:00
|
|
|
|
RuntimeProvider.Instance.StartCoroutine(InitCoroutine());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerator InitCoroutine()
|
|
|
|
|
{
|
|
|
|
|
yield return null;
|
|
|
|
|
|
|
|
|
|
LayoutRebuilder.ForceRebuildLayoutImmediate(InspectorPanel.Instance.ContentRect);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 21:01:08 +10:00
|
|
|
|
public override void OnReturnToPool()
|
|
|
|
|
{
|
|
|
|
|
foreach (var member in members)
|
2021-04-30 23:12:18 +10:00
|
|
|
|
member.ReleasePooledObjects();
|
2021-04-29 21:01:08 +10:00
|
|
|
|
|
|
|
|
|
members.Clear();
|
|
|
|
|
filteredMembers.Clear();
|
|
|
|
|
displayedMembers.Clear();
|
|
|
|
|
|
2021-04-30 21:34:50 +10:00
|
|
|
|
autoUpdateToggle.isOn = false;
|
2021-05-01 20:55:27 +10:00
|
|
|
|
AutoUpdateWanted = false;
|
2021-04-30 21:34:50 +10:00
|
|
|
|
|
2021-04-29 21:01:08 +10:00
|
|
|
|
base.OnReturnToPool();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 21:22:48 +10:00
|
|
|
|
private void SetTarget(object target)
|
|
|
|
|
{
|
|
|
|
|
string prefix;
|
|
|
|
|
if (StaticOnly)
|
|
|
|
|
{
|
|
|
|
|
Target = null;
|
|
|
|
|
TargetType = target as Type;
|
|
|
|
|
prefix = "[S]";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
TargetType = target.GetActualType();
|
|
|
|
|
prefix = "[R]";
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-03 01:29:02 +10:00
|
|
|
|
Tab.TabText.text = $"{prefix} {SignatureHighlighter.ParseFullType(TargetType)}";
|
|
|
|
|
|
2021-04-27 21:22:48 +10:00
|
|
|
|
NameText.text = SignatureHighlighter.ParseFullSyntax(TargetType, true);
|
|
|
|
|
|
|
|
|
|
string asmText;
|
|
|
|
|
if (TargetType.Assembly != null && !string.IsNullOrEmpty(TargetType.Assembly.Location))
|
|
|
|
|
asmText = Path.GetFileName(TargetType.Assembly.Location);
|
|
|
|
|
else
|
|
|
|
|
asmText = $"{TargetType.Assembly.GetName().Name} <color=grey><i>(in memory)</i></color>";
|
|
|
|
|
AssemblyText.text = $"<color=grey>Assembly:</color> {asmText}";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.members = CacheMember.GetCacheMembers(Target, TargetType, this);
|
|
|
|
|
FilterMembers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void FilterMembers()
|
|
|
|
|
{
|
|
|
|
|
// todo
|
|
|
|
|
for (int i = 0; i < members.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var member = members[i];
|
|
|
|
|
filteredMembers.Add(member);
|
|
|
|
|
}
|
2021-04-30 21:34:50 +10:00
|
|
|
|
|
2021-05-01 20:55:27 +10:00
|
|
|
|
//MemberScrollPool.Refresh
|
2021-04-27 21:22:48 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnSetActive()
|
|
|
|
|
{
|
|
|
|
|
base.OnSetActive();
|
|
|
|
|
}
|
2021-04-26 19:56:21 +10:00
|
|
|
|
|
2021-04-27 21:22:48 +10:00
|
|
|
|
public override void OnSetInactive()
|
2021-04-26 19:56:21 +10:00
|
|
|
|
{
|
2021-04-27 21:22:48 +10:00
|
|
|
|
base.OnSetInactive();
|
2021-04-26 19:56:21 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 21:01:08 +10:00
|
|
|
|
protected override void OnCloseClicked()
|
|
|
|
|
{
|
|
|
|
|
InspectorManager.ReleaseInspector(this);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 21:22:48 +10:00
|
|
|
|
private float timeOfLastUpdate;
|
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
public override void Update()
|
|
|
|
|
{
|
2021-04-27 21:22:48 +10:00
|
|
|
|
if (!this.IsActive)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!StaticOnly && Target.IsNullOrDestroyed(false))
|
|
|
|
|
{
|
|
|
|
|
InspectorManager.ReleaseInspector(this);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-03 21:02:01 +10:00
|
|
|
|
if (timeOfLastUpdate.OccuredEarlierThan(1))
|
2021-04-27 21:22:48 +10:00
|
|
|
|
{
|
2021-05-03 21:02:01 +10:00
|
|
|
|
timeOfLastUpdate = Time.realtimeSinceStartup;
|
2021-04-27 21:22:48 +10:00
|
|
|
|
|
2021-05-01 20:55:27 +10:00
|
|
|
|
if (AutoUpdateWanted)
|
|
|
|
|
UpdateDisplayedMembers();// true);
|
2021-04-27 21:22:48 +10:00
|
|
|
|
}
|
2021-04-26 19:56:21 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-01 20:55:27 +10:00
|
|
|
|
private void UpdateDisplayedMembers()// bool onlyAutoUpdate)
|
2021-04-27 21:22:48 +10:00
|
|
|
|
{
|
2021-04-29 21:01:08 +10:00
|
|
|
|
bool shouldRefresh = false;
|
|
|
|
|
foreach (var member in displayedMembers)
|
|
|
|
|
{
|
2021-05-01 20:55:27 +10:00
|
|
|
|
if (member.ShouldAutoEvaluate) // && (!onlyAutoUpdate || member.AutoUpdateWanted))
|
2021-04-29 21:01:08 +10:00
|
|
|
|
{
|
|
|
|
|
shouldRefresh = true;
|
|
|
|
|
member.Evaluate();
|
2021-04-30 21:34:50 +10:00
|
|
|
|
member.SetCell(member.CellView);
|
2021-04-29 21:01:08 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-27 21:22:48 +10:00
|
|
|
|
|
2021-04-29 21:01:08 +10:00
|
|
|
|
if (shouldRefresh)
|
2021-04-30 23:12:18 +10:00
|
|
|
|
MemberScrollPool.Refresh(false);
|
2021-04-27 21:22:48 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 21:01:08 +10:00
|
|
|
|
// Member cells
|
2021-04-28 20:47:48 +10:00
|
|
|
|
|
2021-04-29 21:01:08 +10:00
|
|
|
|
public int ItemCount => filteredMembers.Count;
|
2021-04-27 21:22:48 +10:00
|
|
|
|
|
2021-04-29 21:01:08 +10:00
|
|
|
|
public void OnCellBorrowed(CacheMemberCell cell)
|
2021-04-28 20:47:48 +10:00
|
|
|
|
{
|
2021-04-30 23:12:18 +10:00
|
|
|
|
cell.Owner = this;
|
2021-04-27 21:22:48 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetCell(CacheMemberCell cell, int index)
|
|
|
|
|
{
|
|
|
|
|
if (index < 0 || index >= filteredMembers.Count)
|
|
|
|
|
{
|
2021-04-30 21:34:50 +10:00
|
|
|
|
if (cell.Occupant != null)
|
2021-04-29 21:01:08 +10:00
|
|
|
|
{
|
2021-04-30 21:34:50 +10:00
|
|
|
|
if (displayedMembers.Contains(cell.MemberOccupant))
|
|
|
|
|
displayedMembers.Remove(cell.MemberOccupant);
|
2021-04-29 21:01:08 +10:00
|
|
|
|
|
2021-04-30 21:34:50 +10:00
|
|
|
|
cell.Occupant.CellView = null;
|
2021-05-01 20:55:27 +10:00
|
|
|
|
cell.Occupant = null;
|
2021-04-29 21:01:08 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 21:22:48 +10:00
|
|
|
|
cell.Disable();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 20:47:48 +10:00
|
|
|
|
var member = filteredMembers[index];
|
2021-04-29 21:01:08 +10:00
|
|
|
|
|
2021-04-30 21:34:50 +10:00
|
|
|
|
if (member != cell.Occupant)
|
2021-04-29 21:01:08 +10:00
|
|
|
|
{
|
2021-04-30 21:34:50 +10:00
|
|
|
|
if (cell.Occupant != null)
|
2021-04-29 21:01:08 +10:00
|
|
|
|
{
|
2021-04-30 21:34:50 +10:00
|
|
|
|
cell.Occupant.HideIValue();
|
|
|
|
|
displayedMembers.Remove(cell.MemberOccupant);
|
|
|
|
|
cell.Occupant.CellView = null;
|
2021-05-01 20:55:27 +10:00
|
|
|
|
cell.Occupant = null;
|
2021-04-29 21:01:08 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-30 21:34:50 +10:00
|
|
|
|
cell.Occupant = member;
|
|
|
|
|
member.CellView = cell;
|
2021-04-29 21:01:08 +10:00
|
|
|
|
displayedMembers.Add(member);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 20:47:48 +10:00
|
|
|
|
member.SetCell(cell);
|
2021-04-27 21:22:48 +10:00
|
|
|
|
|
|
|
|
|
SetCellLayout(cell);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 20:47:48 +10:00
|
|
|
|
// Cell layout (fake table alignment)
|
|
|
|
|
|
2021-05-03 01:29:02 +10:00
|
|
|
|
private static int LeftGroupWidth { get; set; }
|
|
|
|
|
private static int RightGroupWidth { get; set; }
|
2021-04-27 21:22:48 +10:00
|
|
|
|
|
|
|
|
|
private void SetTitleLayouts()
|
|
|
|
|
{
|
2021-04-28 20:47:48 +10:00
|
|
|
|
// Calculate sizes
|
2021-05-03 01:29:02 +10:00
|
|
|
|
LeftGroupWidth = (int)Math.Max(200, (0.45f * InspectorManager.PanelWidth) - 5);// Math.Min(450f, 0.4f * InspectorManager.PanelWidth - 5));
|
|
|
|
|
RightGroupWidth = (int)Math.Max(200, InspectorManager.PanelWidth - LeftGroupWidth - 55);
|
2021-04-28 20:47:48 +10:00
|
|
|
|
|
2021-05-03 01:29:02 +10:00
|
|
|
|
memberTitleLayout.minWidth = LeftGroupWidth;
|
2021-04-27 21:22:48 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-30 21:34:50 +10:00
|
|
|
|
private void SetCellLayout(CacheObjectCell cell)
|
2021-04-27 21:22:48 +10:00
|
|
|
|
{
|
2021-05-03 01:29:02 +10:00
|
|
|
|
cell.NameLayout.minWidth = LeftGroupWidth;
|
2021-04-27 21:22:48 +10:00
|
|
|
|
cell.RightGroupLayout.minWidth = RightGroupWidth;
|
2021-05-03 01:29:02 +10:00
|
|
|
|
|
|
|
|
|
if (cell.Occupant?.IValue != null)
|
|
|
|
|
cell.Occupant.IValue.SetLayout();
|
2021-04-27 21:22:48 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void SetLayouts()
|
|
|
|
|
{
|
|
|
|
|
SetTitleLayouts();
|
|
|
|
|
|
|
|
|
|
foreach (var cell in MemberScrollPool.CellPool)
|
|
|
|
|
SetCellLayout(cell);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override GameObject CreateContent(GameObject parent)
|
|
|
|
|
{
|
2021-05-01 20:55:27 +10:00
|
|
|
|
UIRoot = UIFactory.CreateVerticalGroup(parent, "ReflectionInspector", true, true, true, true, 5,
|
2021-04-27 21:22:48 +10:00
|
|
|
|
new Vector4(4, 4, 4, 4), new Color(0.12f, 0.12f, 0.12f));
|
|
|
|
|
|
2021-04-29 21:01:08 +10:00
|
|
|
|
// Class name, assembly. TODO more details
|
|
|
|
|
|
2021-05-01 20:55:27 +10:00
|
|
|
|
NameText = UIFactory.CreateLabel(UIRoot, "Title", "not set", TextAnchor.MiddleLeft, fontSize: 20);
|
2021-04-27 21:22:48 +10:00
|
|
|
|
UIFactory.SetLayoutElement(NameText.gameObject, minHeight: 25, flexibleHeight: 0);
|
|
|
|
|
|
2021-05-01 20:55:27 +10:00
|
|
|
|
AssemblyText = UIFactory.CreateLabel(UIRoot, "AssemblyLabel", "not set", TextAnchor.MiddleLeft);
|
2021-04-27 21:22:48 +10:00
|
|
|
|
UIFactory.SetLayoutElement(AssemblyText.gameObject, minHeight: 25, flexibleWidth: 9999);
|
|
|
|
|
|
2021-04-29 21:01:08 +10:00
|
|
|
|
// TODO filter row
|
|
|
|
|
|
|
|
|
|
|
2021-05-01 20:55:27 +10:00
|
|
|
|
|
|
|
|
|
// Member list titles
|
|
|
|
|
|
|
|
|
|
var listTitles = UIFactory.CreateUIObject("ListTitles", UIRoot);
|
2021-04-27 21:22:48 +10:00
|
|
|
|
UIFactory.SetLayoutElement(listTitles, minHeight: 25);
|
|
|
|
|
UIFactory.SetLayoutGroup<HorizontalLayoutGroup>(listTitles, true, true, true, true, 5, 1, 1, 1, 1);
|
|
|
|
|
|
|
|
|
|
var memberTitle = UIFactory.CreateLabel(listTitles, "MemberTitle", "Member Name", TextAnchor.LowerLeft, Color.grey, fontSize: 15);
|
|
|
|
|
memberTitleLayout = memberTitle.gameObject.AddComponent<LayoutElement>();
|
|
|
|
|
|
|
|
|
|
var valueTitle = UIFactory.CreateLabel(listTitles, "ValueTitle", "Value", TextAnchor.LowerLeft, Color.grey, fontSize: 15);
|
2021-04-30 21:34:50 +10:00
|
|
|
|
UIFactory.SetLayoutElement(valueTitle.gameObject, minWidth: 50, flexibleWidth: 9999);
|
2021-04-27 21:22:48 +10:00
|
|
|
|
|
2021-05-01 20:55:27 +10:00
|
|
|
|
var updateButton = UIFactory.CreateButton(listTitles, "UpdateButton", "Update displayed values", new Color(0.22f, 0.28f, 0.22f));
|
|
|
|
|
UIFactory.SetLayoutElement(updateButton.Button.gameObject, minHeight: 25, minWidth: 160, flexibleWidth: 0);
|
2021-04-29 21:01:08 +10:00
|
|
|
|
updateButton.OnClick += UpdateDisplayedMembers;
|
|
|
|
|
|
2021-04-30 21:34:50 +10:00
|
|
|
|
var toggleObj = UIFactory.CreateToggle(listTitles, "AutoUpdateToggle", out autoUpdateToggle, out Text toggleText);
|
2021-05-01 20:55:27 +10:00
|
|
|
|
//GameObject.DestroyImmediate(toggleText);
|
|
|
|
|
UIFactory.SetLayoutElement(toggleObj, minWidth: 185, minHeight: 25);
|
2021-04-30 21:34:50 +10:00
|
|
|
|
autoUpdateToggle.isOn = false;
|
2021-05-01 20:55:27 +10:00
|
|
|
|
autoUpdateToggle.onValueChanged.AddListener((bool val) => { AutoUpdateWanted = val; });
|
|
|
|
|
toggleText.text = "Auto-update displayed";
|
2021-04-30 21:34:50 +10:00
|
|
|
|
|
2021-04-29 21:01:08 +10:00
|
|
|
|
// Member scroll pool
|
|
|
|
|
|
2021-05-01 20:55:27 +10:00
|
|
|
|
MemberScrollPool = UIFactory.CreateScrollPool<CacheMemberCell>(UIRoot, "MemberList", out GameObject scrollObj,
|
2021-04-29 21:01:08 +10:00
|
|
|
|
out GameObject _, new Color(0.09f, 0.09f, 0.09f));
|
2021-04-27 21:22:48 +10:00
|
|
|
|
UIFactory.SetLayoutElement(scrollObj, flexibleHeight: 9999);
|
2021-04-30 23:12:18 +10:00
|
|
|
|
MemberScrollPool.Initialize(this);
|
2021-04-27 21:22:48 +10:00
|
|
|
|
|
2021-04-30 21:34:50 +10:00
|
|
|
|
//InspectorPanel.Instance.UIRoot.GetComponent<Mask>().enabled = false;
|
|
|
|
|
//MemberScrollPool.Viewport.GetComponent<Mask>().enabled = false;
|
|
|
|
|
//MemberScrollPool.Viewport.GetComponent<Image>().color = new Color(0.12f, 0.12f, 0.12f);
|
2021-04-27 21:22:48 +10:00
|
|
|
|
|
2021-05-01 20:55:27 +10:00
|
|
|
|
return UIRoot;
|
2021-04-26 19:56:21 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|