From b29d87047db71eef624da6e8ee6f870fc5727da6 Mon Sep 17 00:00:00 2001 From: ManlyMarco <39247311+ManlyMarco@users.noreply.github.com> Date: Tue, 24 Sep 2024 00:24:43 +0200 Subject: [PATCH] dnspy context menu option improvements --- .../Features/ContextMenu.cs | 20 +++++++++---------- .../Utils/Abstractions/DnSpyHelper.cs | 15 +++++--------- .../Windows/Inspector/Inspector.cs | 13 ++++++------ 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/RuntimeUnityEditor.Core/Features/ContextMenu.cs b/RuntimeUnityEditor.Core/Features/ContextMenu.cs index a4aa4bb..2d9522a 100644 --- a/RuntimeUnityEditor.Core/Features/ContextMenu.cs +++ b/RuntimeUnityEditor.Core/Features/ContextMenu.cs @@ -33,10 +33,10 @@ public class ContextMenu : FeatureBase /// public override bool Enabled { - get => base.Enabled && _obj != null; + get => base.Enabled && (_obj != null || _objMemberInfo != null); set { - if (value && _obj == null) + if (value && _obj == null && _objMemberInfo == null) value = false; base.Enabled = value; } @@ -55,7 +55,7 @@ protected override void Initialize(InitSettings initSettings) { new MenuEntry("! Destroyed unity Object !", obj => obj is UnityEngine.Object uobj && !uobj, null), - new MenuEntry("Preview", o => ObjectViewWindow.Initialized && ObjectViewWindow.Instance.CanPreview(o), o => ObjectViewWindow.Instance.SetShownObject(o, _objName)), + new MenuEntry("Preview", o => o != null && ObjectViewWindow.Initialized && ObjectViewWindow.Instance.CanPreview(o), o => ObjectViewWindow.Instance.SetShownObject(o, _objName)), new MenuEntry("Show event details", o => o is UnityEventBase && ObjectViewWindow.Initialized, o => ObjectViewWindow.Instance.SetShownObject(ReflectionUtils.GetEventDetails((UnityEventBase)o), o + " - Event details")), @@ -64,7 +64,7 @@ protected override void Initialize(InitSettings initSettings) new MenuEntry(), - new MenuEntry("Send to inspector", o => Inspector.Inspector.Initialized, o => + new MenuEntry("Send to inspector", o => o != null && Inspector.Inspector.Initialized, o => { if (o is Type t) Inspector.Inspector.Instance.Push(new StaticStackEntry(t, _objName), true); @@ -72,11 +72,11 @@ protected override void Initialize(InitSettings initSettings) Inspector.Inspector.Instance.Push(new InstanceStackEntry(o, _objName), true); }), - new MenuEntry("Send to REPL", o => REPL.ReplWindow.Initialized, o => REPL.ReplWindow.Instance.IngestObject(o)), + new MenuEntry("Send to REPL", o => o != null && REPL.ReplWindow.Initialized, o => REPL.ReplWindow.Instance.IngestObject(o)), new MenuEntry(), - new MenuEntry("Copy to clipboard", o => Clipboard.ClipboardWindow.Initialized, o => + new MenuEntry("Copy to clipboard", o => o != null && Clipboard.ClipboardWindow.Initialized, o => { if (Clipboard.ClipboardWindow.Contents.LastOrDefault() != o) Clipboard.ClipboardWindow.Contents.Add(o); @@ -150,16 +150,16 @@ o is Sprite || new MenuEntry("Export mesh to .obj (Baked)", o => o is Renderer r && MeshExport.CanExport(r), o => MeshExport.ExportObj((Renderer)o, true, false)), new MenuEntry("Export mesh to .obj (World)", o => o is Renderer r && MeshExport.CanExport(r), o => MeshExport.ExportObj((Renderer)o, true, true)), - new MenuEntry("Dump object to file...", o => true, o => Dumper.DumpToTempFile(o, _objName)), + new MenuEntry("Dump object to file...", o => o != null, o => Dumper.DumpToTempFile(o, _objName)), new MenuEntry("Destroy", o => o is UnityEngine.Object uo && uo, o => Change.Action("(ContextMenu)::UnityEngine.Object.Destroy({0})", o is Transform t ? t.gameObject : (UnityEngine.Object)o, UnityEngine.Object.Destroy)), new MenuEntry(), - new MenuEntry("Find references in scene", o => ObjectViewWindow.Initialized && o.GetType().IsClass, o => ObjectTreeViewer.Instance.FindReferencesInScene(o)), + new MenuEntry("Find references in scene", o => o != null && ObjectViewWindow.Initialized && o.GetType().IsClass, o => ObjectTreeViewer.Instance.FindReferencesInScene(o)), new MenuEntry("Find member in dnSpy", o => DnSpyHelper.IsAvailable && _objMemberInfo != null, o => DnSpyHelper.OpenInDnSpy(_objMemberInfo)), - new MenuEntry("Find member type in dnSpy", o => DnSpyHelper.IsAvailable, o => DnSpyHelper.OpenInDnSpy(o.GetType())) + new MenuEntry("Find member type in dnSpy", o => o != null && DnSpyHelper.IsAvailable, o => DnSpyHelper.OpenInDnSpy(o.GetType())) }); _windowId = base.GetHashCode(); @@ -192,7 +192,7 @@ public void Show(object obj, MemberInfo objMemberInfo, Vector2 clickPoint) _windowRect = new Rect(clickPoint.x, clickPoint.y, 100, 100); // Unity4 only has the 4xfloat constructor #endif - if (obj != null) + if (obj != null || objMemberInfo != null) { _obj = obj; _objMemberInfo = objMemberInfo; diff --git a/RuntimeUnityEditor.Core/Utils/Abstractions/DnSpyHelper.cs b/RuntimeUnityEditor.Core/Utils/Abstractions/DnSpyHelper.cs index 543cd95..e62e6f9 100644 --- a/RuntimeUnityEditor.Core/Utils/Abstractions/DnSpyHelper.cs +++ b/RuntimeUnityEditor.Core/Utils/Abstractions/DnSpyHelper.cs @@ -107,7 +107,7 @@ private static string GetTypeRefArgs(Type type) if (type == null) throw new ArgumentNullException(nameof(type)); if (type.ToString().Contains(',')) - throw new Exception("Unsupported type with generic parameters"); + throw new Exception("Unsupported type with generic parameters: " + type.FullName); var refString = $"\"{type.Assembly.Location}\" --select T:{type.FullName}"; return refString; } @@ -135,23 +135,18 @@ private static string GetMemberRefArgs(MemberInfo entry) if (declaringType.FullName == null) throw new ArgumentException("null DeclaringType.FullName"); // TODO support for generic types + if (declaringType.FullName.Contains(',')) throw new Exception("Unsupported type with generic parameters: " + declaringType.FullName); switch (entry) { case MethodBase m: - if (m.ToString().Contains(',') || declaringType.FullName.Contains(',')) - throw new Exception("Unsupported type or method with generic parameters"); - return $"\"{declaringType.Assembly.Location}\" --select M:{declaringType.FullName}.{m.ToString().Split(new[] { ' ' }, 2).Last()}"; + var methodNameStr = m.ToString(); + if (methodNameStr.Contains(',')) throw new Exception("Unsupported method with generic parameters: " + m); + return $"\"{declaringType.Assembly.Location}\" --select M:{declaringType.FullName}.{methodNameStr.Split(new[] { ' ' }, 2).Last()}"; case PropertyInfo p: - if (declaringType.FullName.Contains(',')) - throw new Exception("Unsupported type with generic parameters"); return $"\"{declaringType.Assembly.Location}\" --select P:{declaringType.FullName}.{p.Name}"; case FieldInfo f: - if (declaringType.FullName.Contains(',')) - throw new Exception("Unsupported type with generic parameters"); return $"\"{declaringType.Assembly.Location}\" --select F:{declaringType.FullName}.{f.Name}"; case EventInfo e: - if (declaringType.FullName.Contains(',')) - throw new Exception("Unsupported type with generic parameters"); return $"\"{declaringType.Assembly.Location}\" --select E:{declaringType.FullName}.{e.Name}"; default: throw new Exception("Unknown MemberInfo " + entry.GetType().FullName); diff --git a/RuntimeUnityEditor.Core/Windows/Inspector/Inspector.cs b/RuntimeUnityEditor.Core/Windows/Inspector/Inspector.cs index 160680d..bf547a1 100644 --- a/RuntimeUnityEditor.Core/Windows/Inspector/Inspector.cs +++ b/RuntimeUnityEditor.Core/Windows/Inspector/Inspector.cs @@ -87,8 +87,7 @@ private void DrawVariableNameEnterButton(ICacheEntry field) { if (IMGUIUtils.IsMouseRightClick()) { - if (val != null) - ContextMenu.Instance.Show(val, field.GetMemberInfo(false)); + ContextMenu.Instance.Show(val, field.GetMemberInfo(false)); } else if (canEnterValue || val is Exception) { @@ -202,9 +201,9 @@ protected override void DrawContents() _showProperties = GUILayout.Toggle(_showProperties, "Properties"); _showMethods = GUILayout.Toggle(_showMethods, "Methods"); _showEvents = GUILayout.Toggle(_showEvents, "Events"); - #if IL2CPP +#if IL2CPP _showNative = GUILayout.Toggle(_showNative, "Native"); - #endif +#endif _showDeclaredOnly = GUILayout.Toggle(_showDeclaredOnly, "Only declared"); /* todo @@ -400,11 +399,11 @@ private void DrawContentScrollView(InspectorTab tab) switch (x) { case PropertyCacheEntry p when !_showProperties || _showDeclaredOnly && !p.IsDeclared: - #if IL2CPP +#if IL2CPP case FieldCacheEntry f when !_showFields || _showDeclaredOnly && !f.IsDeclared || !_showNative && f.FieldInfo.IsStatic && f.Type() == typeof(IntPtr): - #else +#else case FieldCacheEntry f when !_showFields || _showDeclaredOnly && !f.IsDeclared: - #endif +#endif case MethodCacheEntry m when !_showMethods || _showDeclaredOnly && !m.IsDeclared: case EventCacheEntry e when !_showEvents || _showDeclaredOnly && !e.IsDeclared: return false;