A few important fixes

* Reflection on Il2CppSystem-namespace instances has been fixed
* Type/Value Syntax highlighting generalized and improved globally
* Scene changes now refresh the scene-picker dropdown
* probably other minor stuff too
This commit is contained in:
sinaioutlander
2020-11-13 18:46:36 +11:00
parent dc449d4a1e
commit bc113e9093
21 changed files with 450 additions and 395 deletions

View File

@ -77,7 +77,7 @@ namespace UnityExplorer.Inspectors.GameObjects
var text = s_compListTexts[i];
text.text = UISyntaxHighlight.GetHighlight(ReflectionHelpers.GetActualType(comp), true);
text.text = UISyntaxHighlight.ParseFullSyntax(ReflectionHelpers.GetActualType(comp), true);
var toggle = s_compToggles[i];
if (comp is Behaviour behaviour)

View File

@ -8,7 +8,6 @@ namespace UnityExplorer.Inspectors
public abstract class InspectorBase
{
public object Target;
public UnityEngine.Object UnityTarget;
public abstract string TabLabel { get; }
@ -22,9 +21,8 @@ namespace UnityExplorer.Inspectors
public InspectorBase(object target)
{
Target = target;
UnityTarget = target as UnityEngine.Object;
if (ObjectNullOrDestroyed(Target, UnityTarget))
if (IsNullOrDestroyed(Target))
{
Destroy();
return;
@ -47,7 +45,7 @@ namespace UnityExplorer.Inspectors
public virtual void Update()
{
if (ObjectNullOrDestroyed(Target, UnityTarget))
if (IsNullOrDestroyed(Target))
{
Destroy();
return;
@ -86,14 +84,13 @@ namespace UnityExplorer.Inspectors
}
}
public static bool ObjectNullOrDestroyed(object obj, UnityEngine.Object unityObj, bool suppressWarning = false)
public static bool IsNullOrDestroyed(object obj, bool suppressWarning = false)
{
var unityObj = obj as UnityEngine.Object;
if (obj == null)
{
if (!suppressWarning)
{
ExplorerCore.LogWarning("The target instance is null!");
}
return true;
}
@ -102,9 +99,7 @@ namespace UnityExplorer.Inspectors
if (!unityObj)
{
if (!suppressWarning)
{
ExplorerCore.LogWarning("The target UnityEngine.Object was destroyed!");
}
return true;
}

View File

@ -45,13 +45,11 @@ namespace UnityExplorer.Inspectors
#endif
UnityEngine.Object unityObj = obj as UnityEngine.Object;
if (InspectorBase.ObjectNullOrDestroyed(obj, unityObj))
if (InspectorBase.IsNullOrDestroyed(obj))
{
return;
}
MainMenu.Instance.SetPage(HomePage.Instance);
// check if currently inspecting this object
foreach (InspectorBase tab in m_currentInspectors)
{
@ -84,7 +82,13 @@ namespace UnityExplorer.Inspectors
public void Inspect(Type type)
{
foreach (var tab in m_currentInspectors)
if (type == null)
{
ExplorerCore.LogWarning("The provided type was null!");
return;
}
foreach (var tab in m_currentInspectors.Where(x => x is StaticInspector))
{
if (ReferenceEquals(tab.Target as Type, type))
{
@ -101,8 +105,12 @@ namespace UnityExplorer.Inspectors
public void SetInspectorTab(InspectorBase inspector)
{
if (m_activeInspector == inspector)
return;
UnsetInspectorTab();
MainMenu.Instance.SetPage(HomePage.Instance);
m_activeInspector = inspector;
inspector.SetActive();

View File

@ -152,7 +152,7 @@ namespace UnityExplorer.Inspectors.Reflection
private string GetRichTextName()
{
return m_richTextName = UISyntaxHighlight.GetHighlight(MemInfo.DeclaringType, false, MemInfo);
return m_richTextName = UISyntaxHighlight.ParseFullSyntax(MemInfo.DeclaringType, false, MemInfo);
}
#if CPP
@ -179,12 +179,7 @@ namespace UnityExplorer.Inspectors.Reflection
if (!typeof(Il2CppSystem.Object).IsAssignableFrom(type))
return true;
var ptr = (IntPtr)typeof(Il2CppClassPointerStore<>)
.MakeGenericType(type)
.GetField("NativeClassPtr")
.GetValue(null);
if (ptr == IntPtr.Zero)
if (!ReflectionHelpers.Il2CppTypeNotNull(type, out IntPtr ptr))
return false;
return Il2CppSystem.Type.internal_from_handle(IL2CPP.il2cpp_class_get_type(ptr)) is Il2CppSystem.Type;
@ -207,7 +202,7 @@ namespace UnityExplorer.Inspectors.Reflection
var textGen = m_memLabelText.cachedTextGeneratorForLayout;
float preferredWidth = textGen.GetPreferredWidth(RichTextName, textGenSettings);
float max = scrollRect.rect.width * 0.5f;
float max = scrollRect.rect.width * 0.4f;
if (preferredWidth > max) preferredWidth = max;
@ -245,6 +240,10 @@ namespace UnityExplorer.Inspectors.Reflection
topGroup.childControlHeight = true;
topGroup.childControlWidth = true;
topGroup.spacing = 10;
topGroup.padding.left = 3;
topGroup.padding.right = 3;
topGroup.padding.top = 0;
topGroup.padding.bottom = 0;
// left group
@ -295,6 +294,8 @@ namespace UnityExplorer.Inspectors.Reflection
rightGroup.childControlHeight = true;
rightGroup.childControlWidth = true;
rightGroup.spacing = 4;
rightGroup.padding.top = 2;
rightGroup.padding.bottom = 2;
// evaluate button
if (this is CacheMethod || HasParameters)

View File

@ -22,7 +22,7 @@ namespace UnityExplorer.Inspectors.Reflection
// might not need
public virtual bool HasSubContent => false;
public string RichTextValue => m_richValue ?? GetRichTextValue();
public string RichTextValue => m_richValue ?? GetLabelForValue();
internal string m_richValue;
internal string m_richValueType;
@ -45,39 +45,20 @@ namespace UnityExplorer.Inspectors.Reflection
return;
}
GetRichTextValue();
GetLabelForValue();
m_text.text = RichTextValue;
//if (Value == null)
// m_text.text = $"<color=red>null</color> {m_richValueType}";
//else
// m_text.text = RichTextValue;
bool shouldShowInspect = !InspectorBase.IsNullOrDestroyed(this.Value, true);
if (m_inspectButton.activeSelf != shouldShowInspect)
m_inspectButton.SetActive(shouldShowInspect);
}
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;
}
public string GetRichTextValue()
public string GetLabelForValue()
{
if (Value != null)
ValueType = Value.GetType();
m_richValueType = UISyntaxHighlight.GetHighlight(ValueType, true);
m_richValueType = UISyntaxHighlight.ParseFullSyntax(ValueType, true);
if (OwnerCacheObject is CacheMember cm && !cm.HasEvaluated)
return $"<i><color=grey>Not yet evaluated</color> ({m_richValueType})</i>";
@ -103,7 +84,11 @@ namespace UnityExplorer.Inspectors.Reflection
{
var toString = (string)ToStringMethod.Invoke(Value, null);
var temp = toString.Replace(ValueType.FullName, "").Trim();
var fullnametemp = ValueType.ToString();
if (fullnametemp.StartsWith("Il2CppSystem"))
fullnametemp = fullnametemp.Substring(6, fullnametemp.Length - 6);
var temp = toString.Replace(fullnametemp, "").Trim();
if (string.IsNullOrEmpty(temp))
{
@ -127,20 +112,78 @@ namespace UnityExplorer.Inspectors.Reflection
return m_richValue = label;
}
#region UI CONSTRUCTION
private MethodInfo GetToStringMethod()
{
try
{
m_toStringMethod = ReflectionHelpers.GetActualType(Value).GetMethod("ToString", new Type[0])
?? typeof(object).GetMethod("ToString", new Type[0]);
internal GameObject m_UIContent;
// test invoke
m_toStringMethod.Invoke(Value, null);
}
catch
{
m_toStringMethod = typeof(object).GetMethod("ToString", new Type[0]);
}
return m_toStringMethod;
}
#region UI CONSTRUCTION
internal GameObject m_mainContent;
internal GameObject m_inspectButton;
internal Text m_text;
internal GameObject m_subContentParent;
public virtual void ConstructUI(GameObject parent, GameObject subGroup)
{
m_UIContent = UIFactory.CreateLabel(parent, TextAnchor.MiddleLeft);
var mainLayout = m_UIContent.AddComponent<LayoutElement>();
mainLayout.minWidth = 200;
mainLayout.flexibleWidth = 5000;
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;
mainLayout.minHeight = 25;
m_text = m_UIContent.GetComponent<Text>();
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>();
#if CPP
inspectBtn.onClick.AddListener(new Action(OnInspectClicked));
#else
inspectBtn.onClick.AddListener(OnInspectClicked);
#endif
void OnInspectClicked()
{
if (!InspectorBase.IsNullOrDestroyed(this.Value))
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;
m_subContentParent = subGroup;
}

View File

@ -40,11 +40,14 @@ namespace UnityExplorer.Inspectors
// Blacklists
private static readonly HashSet<string> s_typeAndMemberBlacklist = new HashSet<string>
{
// these cause a crash
#if CPP
// these cause a crash in IL2CPP
"Type.DeclaringMethod",
"Rigidbody2D.Cast",
"Collider2D.Cast",
"Collider2D.Raycast",
"Texture2D.SetPixelDataImpl",
#endif
};
private static readonly HashSet<string> s_methodStartsWithBlacklist = new HashSet<string>
{
@ -53,9 +56,9 @@ namespace UnityExplorer.Inspectors
"set_",
};
#endregion
#endregion
#region INSTANCE
#region INSTANCE
public override string TabLabel => m_targetTypeShortName;
@ -253,6 +256,9 @@ namespace UnityExplorer.Inspectors
{
try
{
//ExplorerCore.Log($"Trying to cache member {sig}...");
//ExplorerCore.Log(member.DeclaringType.FullName + "." + member.Name);
// make sure member type is Field, Method or Property (4 / 8 / 16)
int m = (int)member.MemberType;
if (m < 4 || m > 16)
@ -303,8 +309,6 @@ namespace UnityExplorer.Inspectors
try
{
//ExplorerCore.Log($"Trying to cache member {sig}...");
var cached = CacheFactory.GetCacheObject(member, target, m_scrollContent);
if (cached != null)
@ -337,7 +341,7 @@ namespace UnityExplorer.Inspectors
// ExplorerCore.Log("Cached " + m_allMembers.Length + " members");
}
#region UI CONSTRUCTION
#region UI CONSTRUCTION
internal void ConstructUI()
{
@ -391,13 +395,13 @@ namespace UnityExplorer.Inspectors
typeLabelLayout.minWidth = 150;
typeLabelLayout.flexibleWidth = 5000;
typeLabelInput.text = UISyntaxHighlight.GetHighlight(m_targetType, true);
typeLabelInput.text = UISyntaxHighlight.ParseFullSyntax(m_targetType, true);
// Helper tools
if (this is InstanceInspector ii)
if (this is InstanceInspector instanceInspector)
{
ii.ConstructInstanceHelpers(Content);
instanceInspector.ConstructInstanceHelpers(Content);
}
}
@ -422,9 +426,9 @@ namespace UnityExplorer.Inspectors
// Instance filters
if (this is InstanceInspector ii)
if (this is InstanceInspector instanceInspector)
{
ii.ConstructInstanceFilters(filterAreaObj);
instanceInspector.ConstructInstanceFilters(filterAreaObj);
}
}
@ -436,14 +440,16 @@ namespace UnityExplorer.Inspectors
var scrollGroup = m_scrollContent.GetComponent<VerticalLayoutGroup>();
scrollGroup.spacing = 3;
scrollGroup.padding.left = 0;
scrollGroup.padding.right = 0;
m_pageHandler = new PageHandler(m_sliderScroller);
m_pageHandler.ConstructUI(Content);
m_pageHandler.OnPageChanged += OnPageTurned;
}
#endregion
#endregion
#endregion
#endregion
}
}

View File

@ -62,7 +62,7 @@ namespace UnityExplorer.Inspectors
public void Init()
{
RefreshActiveScenes();
RefreshSceneSelector();
}
public void Update()
@ -72,7 +72,7 @@ namespace UnityExplorer.Inspectors
return;
}
RefreshActiveScenes();
RefreshSceneSelector();
if (!m_selectedSceneObject)
{
@ -104,10 +104,10 @@ namespace UnityExplorer.Inspectors
internal void OnSceneChange()
{
m_sceneDropdown.OnCancel(null);
RefreshActiveScenes();
RefreshSceneSelector();
}
private void RefreshActiveScenes()
private void RefreshSceneSelector()
{
var names = new List<string>();
var handles = new List<int>();
@ -132,16 +132,12 @@ namespace UnityExplorer.Inspectors
foreach (string scene in names)
{
m_sceneDropdown.options.Add(new Dropdown.OptionData
{
text = scene
});
m_sceneDropdown.options.Add(new Dropdown.OptionData { text = scene });
}
if (!handles.Contains(m_currentSceneHandle))
if (!names.Contains(m_sceneDropdown.itemText.text))
{
//m_sceneDropdown.transform.Find("Label").GetComponent<Text>().text = names[0];
m_sceneDropdown.itemText.text = names[0];
m_sceneDropdown.transform.Find("Label").GetComponent<Text>().text = names[0];
SetTargetScene(handles[0]);
}
}