2020-11-09 21:38:25 +11:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using UnityEngine;
|
2020-11-12 16:15:41 +11:00
|
|
|
|
using UnityEngine.EventSystems;
|
2020-11-11 20:16:43 +11:00
|
|
|
|
using UnityEngine.UI;
|
2020-11-09 21:38:25 +11:00
|
|
|
|
using UnityExplorer.Helpers;
|
2020-11-11 20:16:43 +11:00
|
|
|
|
using UnityExplorer.UI;
|
2020-11-09 21:38:25 +11:00
|
|
|
|
using UnityExplorer.UI.Shared;
|
2020-11-12 16:15:41 +11:00
|
|
|
|
using UnityExplorer.Unstrip;
|
2020-11-09 21:38:25 +11:00
|
|
|
|
|
|
|
|
|
namespace UnityExplorer.Inspectors.Reflection
|
|
|
|
|
{
|
|
|
|
|
public class InteractiveValue
|
|
|
|
|
{
|
|
|
|
|
public CacheObjectBase OwnerCacheObject;
|
|
|
|
|
|
|
|
|
|
public object Value { get; set; }
|
|
|
|
|
public Type ValueType;
|
|
|
|
|
|
2020-11-12 20:31:08 +11:00
|
|
|
|
// might not need
|
|
|
|
|
public virtual bool HasSubContent => false;
|
|
|
|
|
|
2020-11-13 18:46:36 +11:00
|
|
|
|
public string RichTextValue => m_richValue ?? GetLabelForValue();
|
2020-11-12 16:15:41 +11:00
|
|
|
|
internal string m_richValue;
|
|
|
|
|
internal string m_richValueType;
|
2020-11-09 21:38:25 +11:00
|
|
|
|
|
|
|
|
|
public MethodInfo ToStringMethod => m_toStringMethod ?? GetToStringMethod();
|
2020-11-12 16:15:41 +11:00
|
|
|
|
internal MethodInfo m_toStringMethod;
|
2020-11-09 21:38:25 +11:00
|
|
|
|
|
|
|
|
|
public virtual void Init()
|
|
|
|
|
{
|
|
|
|
|
UpdateValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void UpdateValue()
|
|
|
|
|
{
|
2020-11-11 20:16:43 +11:00
|
|
|
|
if (!m_text)
|
|
|
|
|
return;
|
2020-11-09 21:38:25 +11:00
|
|
|
|
|
2020-11-11 20:16:43 +11:00
|
|
|
|
if (OwnerCacheObject is CacheMember ownerMember && !string.IsNullOrEmpty(ownerMember.ReflectionException))
|
|
|
|
|
{
|
|
|
|
|
m_text.text = "<color=red>" + ownerMember.ReflectionException + "</color>";
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-11-09 21:38:25 +11:00
|
|
|
|
|
2020-11-13 18:46:36 +11:00
|
|
|
|
GetLabelForValue();
|
2020-11-12 16:15:41 +11:00
|
|
|
|
m_text.text = RichTextValue;
|
|
|
|
|
|
2020-11-13 23:14:57 +11:00
|
|
|
|
bool shouldShowInspect = !Value.IsNullOrDestroyed(true);
|
2020-11-13 18:46:36 +11:00
|
|
|
|
if (m_inspectButton.activeSelf != shouldShowInspect)
|
|
|
|
|
m_inspectButton.SetActive(shouldShowInspect);
|
2020-11-11 20:16:43 +11:00
|
|
|
|
}
|
2020-11-09 21:38:25 +11:00
|
|
|
|
|
2020-11-13 18:46:36 +11:00
|
|
|
|
public string GetLabelForValue()
|
2020-11-09 21:38:25 +11:00
|
|
|
|
{
|
2020-11-12 16:15:41 +11:00
|
|
|
|
if (Value != null)
|
|
|
|
|
ValueType = Value.GetType();
|
|
|
|
|
|
2020-11-13 18:46:36 +11:00
|
|
|
|
m_richValueType = UISyntaxHighlight.ParseFullSyntax(ValueType, true);
|
2020-11-09 21:38:25 +11:00
|
|
|
|
|
2020-11-12 20:31:08 +11:00
|
|
|
|
if (OwnerCacheObject is CacheMember cm && !cm.HasEvaluated)
|
|
|
|
|
return $"<i><color=grey>Not yet evaluated</color> ({m_richValueType})</i>";
|
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
if (Value == null) return $"<color=grey>null</color> ({m_richValueType})";
|
2020-11-09 21:38:25 +11:00
|
|
|
|
|
|
|
|
|
string label;
|
|
|
|
|
|
2020-11-12 20:31:08 +11:00
|
|
|
|
if (ValueType == typeof(TextAsset) && Value is TextAsset textAsset)
|
2020-11-09 21:38:25 +11:00
|
|
|
|
{
|
|
|
|
|
label = textAsset.text;
|
|
|
|
|
|
|
|
|
|
if (label.Length > 10)
|
|
|
|
|
label = $"{label.Substring(0, 10)}...";
|
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
label = $"\"{label}\" {textAsset.name} ({m_richValueType})";
|
|
|
|
|
}
|
|
|
|
|
else if (ValueType == typeof(EventSystem))
|
|
|
|
|
{
|
|
|
|
|
label = m_richValueType;
|
2020-11-09 21:38:25 +11:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-11-12 16:15:41 +11:00
|
|
|
|
var toString = (string)ToStringMethod.Invoke(Value, null);
|
2020-11-09 21:38:25 +11:00
|
|
|
|
|
2020-11-13 18:46:36 +11:00
|
|
|
|
var fullnametemp = ValueType.ToString();
|
|
|
|
|
if (fullnametemp.StartsWith("Il2CppSystem"))
|
|
|
|
|
fullnametemp = fullnametemp.Substring(6, fullnametemp.Length - 6);
|
|
|
|
|
|
|
|
|
|
var temp = toString.Replace(fullnametemp, "").Trim();
|
2020-11-09 21:38:25 +11:00
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
if (string.IsNullOrEmpty(temp))
|
2020-11-09 21:38:25 +11:00
|
|
|
|
{
|
2020-11-12 16:15:41 +11:00
|
|
|
|
label = m_richValueType;
|
2020-11-09 21:38:25 +11:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-11-12 16:15:41 +11:00
|
|
|
|
if (toString.Length > 200)
|
|
|
|
|
toString = toString.Substring(0, 200) + "...";
|
|
|
|
|
|
|
|
|
|
label = toString;
|
|
|
|
|
|
|
|
|
|
var unityType = $"({ValueType.FullName})";
|
|
|
|
|
if (Value is UnityEngine.Object && label.Contains(unityType))
|
|
|
|
|
label = label.Replace(unityType, $"({m_richValueType})");
|
2020-11-09 21:38:25 +11:00
|
|
|
|
else
|
2020-11-12 16:15:41 +11:00
|
|
|
|
label += $" ({m_richValueType})";
|
2020-11-09 21:38:25 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
return m_richValue = label;
|
2020-11-09 21:38:25 +11:00
|
|
|
|
}
|
2020-11-11 20:16:43 +11:00
|
|
|
|
|
2020-11-13 18:46:36 +11:00
|
|
|
|
private MethodInfo GetToStringMethod()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
m_toStringMethod = ReflectionHelpers.GetActualType(Value).GetMethod("ToString", new Type[0])
|
|
|
|
|
?? typeof(object).GetMethod("ToString", new Type[0]);
|
|
|
|
|
|
|
|
|
|
// test invoke
|
|
|
|
|
m_toStringMethod.Invoke(Value, null);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
m_toStringMethod = typeof(object).GetMethod("ToString", new Type[0]);
|
|
|
|
|
}
|
|
|
|
|
return m_toStringMethod;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region UI CONSTRUCTION
|
2020-11-11 20:16:43 +11:00
|
|
|
|
|
2020-11-13 18:46:36 +11:00
|
|
|
|
internal GameObject m_mainContent;
|
|
|
|
|
internal GameObject m_inspectButton;
|
2020-11-11 20:16:43 +11:00
|
|
|
|
internal Text m_text;
|
2020-11-12 20:31:08 +11:00
|
|
|
|
internal GameObject m_subContentParent;
|
2020-11-11 20:16:43 +11:00
|
|
|
|
|
2020-11-12 20:31:08 +11:00
|
|
|
|
public virtual void ConstructUI(GameObject parent, GameObject subGroup)
|
2020-11-11 20:16:43 +11:00
|
|
|
|
{
|
2020-11-13 18:46:36 +11:00
|
|
|
|
m_mainContent = UIFactory.CreateHorizontalGroup(parent, new Color(1, 1, 1, 0));
|
|
|
|
|
var mainGroup = m_mainContent.GetComponent<HorizontalLayoutGroup>();
|
|
|
|
|
|
|
|
|
|
mainGroup.childForceExpandWidth = true;
|
|
|
|
|
mainGroup.childControlWidth = true;
|
|
|
|
|
mainGroup.childForceExpandHeight = false;
|
|
|
|
|
mainGroup.childControlHeight = true;
|
|
|
|
|
mainGroup.spacing = 4;
|
|
|
|
|
mainGroup.childAlignment = TextAnchor.UpperLeft;
|
|
|
|
|
var mainLayout = m_mainContent.AddComponent<LayoutElement>();
|
|
|
|
|
mainLayout.flexibleWidth = 9000;
|
|
|
|
|
mainLayout.minWidth = 175;
|
2020-11-11 20:16:43 +11:00
|
|
|
|
mainLayout.minHeight = 25;
|
2020-11-13 18:46:36 +11:00
|
|
|
|
mainLayout.flexibleHeight = 0;
|
|
|
|
|
|
|
|
|
|
// inspect button
|
|
|
|
|
|
|
|
|
|
m_inspectButton = UIFactory.CreateButton(m_mainContent, new Color(0.3f, 0.3f, 0.3f, 0.2f));
|
|
|
|
|
var inspectLayout = m_inspectButton.AddComponent<LayoutElement>();
|
|
|
|
|
inspectLayout.minWidth = 60;
|
|
|
|
|
inspectLayout.minHeight = 25;
|
|
|
|
|
inspectLayout.flexibleHeight = 0;
|
|
|
|
|
inspectLayout.flexibleWidth = 0;
|
|
|
|
|
var inspectText = m_inspectButton.GetComponentInChildren<Text>();
|
|
|
|
|
inspectText.text = "Inspect";
|
|
|
|
|
var inspectBtn = m_inspectButton.GetComponent<Button>();
|
2020-11-13 23:14:57 +11:00
|
|
|
|
|
2020-11-13 18:46:36 +11:00
|
|
|
|
inspectBtn.onClick.AddListener(OnInspectClicked);
|
|
|
|
|
void OnInspectClicked()
|
|
|
|
|
{
|
2020-11-13 23:14:57 +11:00
|
|
|
|
if (!Value.IsNullOrDestroyed())
|
2020-11-13 18:46:36 +11:00
|
|
|
|
InspectorManager.Instance.Inspect(this.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_inspectButton.SetActive(false);
|
|
|
|
|
|
|
|
|
|
// value label / tostring
|
|
|
|
|
|
|
|
|
|
var labelObj = UIFactory.CreateLabel(m_mainContent, TextAnchor.MiddleLeft);
|
|
|
|
|
m_text = labelObj.GetComponent<Text>();
|
|
|
|
|
var labelLayout = labelObj.AddComponent<LayoutElement>();
|
|
|
|
|
labelLayout.flexibleWidth = 9000;
|
|
|
|
|
labelLayout.minHeight = 25;
|
2020-11-12 16:15:41 +11:00
|
|
|
|
|
2020-11-12 20:31:08 +11:00
|
|
|
|
m_subContentParent = subGroup;
|
2020-11-11 20:16:43 +11:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
#endregion
|
2020-11-09 21:38:25 +11:00
|
|
|
|
}
|
|
|
|
|
}
|