Skip to content

Commit

Permalink
dnspy context menu option improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Sep 23, 2024
1 parent 36ae073 commit b29d870
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
20 changes: 10 additions & 10 deletions RuntimeUnityEditor.Core/Features/ContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public class ContextMenu : FeatureBase<ContextMenu>
/// </summary>
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;
}
Expand All @@ -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")),
Expand All @@ -64,19 +64,19 @@ 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);
else
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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 5 additions & 10 deletions RuntimeUnityEditor.Core/Utils/Abstractions/DnSpyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 6 additions & 7 deletions RuntimeUnityEditor.Core/Windows/Inspector/Inspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit b29d870

Please sign in to comment.