Skip to content

Commit f9d3f27

Browse files
committed
Bug fixes
1 parent ec8db36 commit f9d3f27

File tree

6 files changed

+75
-38
lines changed

6 files changed

+75
-38
lines changed

UInspectorPlus/Helpers.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
using System;
44
using System.Collections;
55
using System.Collections.Generic;
6-
using System.Reflection;
76
using System.Linq;
7+
using System.Reflection;
88
using System.Text;
99
using UnityObject = UnityEngine.Object;
1010

@@ -619,6 +619,11 @@ private static MethodInfo FindMethod(Type fromType, string methodName, Type dele
619619
// Special checker to deal with "null" UnityEngine.Object (Internally null, but still exists in Mono heap)
620620
internal static bool IsInvalid(object obj) => obj is UnityObject uObj ? uObj == null : obj == null;
621621

622+
internal static bool IsInternalType(Type type) => !(
623+
type.IsSubclassOf(typeof(MonoBehaviour)) ||
624+
type.IsSubclassOf(typeof(StateMachineBehaviour))
625+
) || Attribute.IsDefined(type, typeof(ExecuteInEditMode));
626+
622627
public static IEnumerable<Type> LooseGetTypes(Assembly assembly) {
623628
for(int retries = 0; retries < 2; retries++)
624629
try {

UInspectorPlus/HexEdit.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
using UnityEditor;
55

66
namespace UInspectorPlus {
7+
[CustomInspectorDrawer(typeof(byte[]), -1)]
78
internal class HexEdit: InspectorDrawer {
89
[SerializeField] private Vector2 scrollPos;
910
public byte[] Data => target as byte[];
1011
public int columns = 16;
1112
private GUIContent temp = new GUIContent();
1213

13-
static HexEdit() => RegisterCustomInspectorDrawer<HexEdit>(typeof(byte[]), -1);
14-
1514

1615
public float Height {
1716
get {

UInspectorPlus/InspectorDrawer.cs

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,77 @@
55
using System.Reflection;
66
using System.Linq;
77
using UnityObject = UnityEngine.Object;
8-
using System.Runtime.CompilerServices;
98

109
namespace UInspectorPlus {
11-
internal struct TypedDrawer {
12-
public Type drawerType;
13-
public Type targetType;
14-
public int priority;
10+
internal struct TypedDrawer: IEquatable<TypedDrawer> {
11+
public readonly Type drawerType;
12+
public readonly Type targetType;
13+
public readonly int priority;
14+
15+
public TypedDrawer(Type drawerType, Type targetType, int priority) {
16+
this.drawerType = drawerType;
17+
this.targetType = targetType;
18+
this.priority = priority;
19+
}
20+
21+
public override int GetHashCode() {
22+
return drawerType.GetHashCode() ^ targetType.GetHashCode();
23+
}
24+
25+
public override bool Equals(object obj) => obj is TypedDrawer other && Equals(other);
26+
27+
public bool Equals(TypedDrawer other) =>
28+
drawerType.Equals(other.drawerType) &&
29+
targetType.Equals(other.targetType);
30+
}
31+
32+
[AttributeUsage(AttributeTargets.Class)]
33+
public class CustomInspectorDrawerAttribute: Attribute {
34+
public Type TargetType { get; private set; }
35+
public int Priority { get; private set; }
36+
37+
public CustomInspectorDrawerAttribute(Type targetType, int priority = 0) {
38+
TargetType = targetType;
39+
Priority = priority;
40+
}
41+
42+
internal TypedDrawer ToDrawer(Type drawerType) => new TypedDrawer(drawerType, TargetType, Priority);
43+
44+
internal static CustomInspectorDrawerAttribute[] GetAttributes(Type type) =>
45+
GetCustomAttributes(type, typeof(CustomInspectorDrawerAttribute)) as CustomInspectorDrawerAttribute[] ??
46+
new CustomInspectorDrawerAttribute[0];
1547
}
1648

1749
public class InspectorDrawer: IDisposable {
18-
private static readonly List<TypedDrawer> typedDrawers = new List<TypedDrawer>();
50+
private static readonly HashSet<TypedDrawer> typedDrawers = new HashSet<TypedDrawer>();
1951
public object target;
2052
internal readonly List<IReflectorDrawer> drawer = new List<IReflectorDrawer>();
2153
private readonly HashSet<IReflectorDrawer> removingDrawers = new HashSet<IReflectorDrawer>();
2254
public bool shown;
23-
public bool isInternalType;
2455
public bool changed;
2556
public string searchText;
2657
public event Action OnRequireRedraw;
2758
protected Type targetType;
2859
protected bool allowPrivate;
2960
protected readonly bool allowMethods;
3061

31-
public static void RegisterCustomInspectorDrawer<T>(Type targetType, int priority = 0) where T : InspectorDrawer {
32-
typedDrawers.Add(new TypedDrawer {
33-
targetType = targetType,
34-
drawerType = typeof(T),
35-
priority = priority
36-
});
62+
static InspectorDrawer() {
63+
var currentDomain = AppDomain.CurrentDomain;
64+
currentDomain.AssemblyLoad += OnAssemblyLoad;
65+
foreach(var assembly in currentDomain.GetAssemblies())
66+
RegisterInspectorDrawers(assembly);
3767
}
3868

69+
private static void OnAssemblyLoad(object sender, AssemblyLoadEventArgs args) => RegisterInspectorDrawers(args.LoadedAssembly);
70+
71+
private static void RegisterInspectorDrawers(Assembly assembly) {
72+
foreach(var type in assembly.GetTypes())
73+
if(type.IsClass && !type.IsAbstract && type.IsSubclassOf(typeof(InspectorDrawer)))
74+
typedDrawers.UnionWith(CustomInspectorDrawerAttribute.GetAttributes(type).Select(drawer => drawer.ToDrawer(type)));
75+
}
76+
77+
3978
public static InspectorDrawer GetDrawer(object target, Type targetType, bool shown, bool showProps, bool showPrivateFields, bool showObsolete, bool showMethods) {
40-
foreach(var type in AppDomain.CurrentDomain.GetAssemblies()
41-
.SelectMany(asm => asm.GetTypes())
42-
.Where(type => type.IsClass && !type.IsAbstract && type.IsSubclassOf(typeof(InspectorDrawer))))
43-
RuntimeHelpers.RunClassConstructor(type.TypeHandle);
4479
int lastPriority = int.MinValue;
4580
Type drawerType = null;
4681
try {
@@ -66,7 +101,6 @@ public InspectorDrawer(object target, Type targetType, bool shown, bool showProp
66101
this.targetType = targetType;
67102
var fields = targetType.GetFields(flag);
68103
var props = !showProps ? null : targetType.GetProperties(flag).Where(prop => prop.GetIndexParameters().Length == 0).ToArray();
69-
isInternalType = !targetType.IsSubclassOf(typeof(MonoBehaviour)) || Attribute.IsDefined(targetType, typeof(ExecuteInEditMode));
70104
foreach(var field in fields)
71105
try {
72106
if(!showObsolete && Attribute.IsDefined(field, typeof(ObsoleteAttribute)))
@@ -91,6 +125,7 @@ public InspectorDrawer(object target, Type targetType, bool shown, bool showProp
91125
continue;
92126
if(!showObsolete && Attribute.IsDefined(prop, typeof(ObsoleteAttribute)))
93127
continue;
128+
bool isInternalType = Helper.IsInternalType(prop.DeclaringType);
94129
drawer.Add(new MethodPropertyDrawer(prop, target, showPrivateFields, showObsolete, prop.CanRead && EditorApplication.isPlaying) {
95130
AllowReferenceMode = false,
96131
Updatable = isInternalType || Helper.GetState(prop, false),
@@ -177,6 +212,7 @@ private void DrawRequestRefs() {
177212
if(drawer.requiredType.IsAssignableFrom(targetType) &&
178213
GUILayout.Button($"Assign this object to {drawer.name}")) {
179214
drawer.Value = target;
215+
drawer.SetDirty();
180216
removal = drawer;
181217
}
182218
if(removal != null) MethodPropertyDrawer.drawerRequestingReferences.Remove(removal);
@@ -189,7 +225,7 @@ public virtual void UpdateValues(bool updateProps) {
189225
if(propDrawer == null)
190226
continue;
191227
var isPropInfo = propDrawer.Info is PropertyInfo;
192-
if(!isInternalType && (!updateProps || !propDrawer.Updatable) && isPropInfo)
228+
if(!Helper.IsInternalType(propDrawer.Info.DeclaringType) && (!updateProps || !propDrawer.Updatable) && isPropInfo)
193229
continue;
194230
propDrawer.UpdateValue();
195231
}

UInspectorPlus/ListInspectorDrawer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
using System.Collections.Generic;
77

88
namespace UInspectorPlus {
9+
[CustomInspectorDrawer(typeof(IList), -2)]
910
internal class ListInspectorDrawer: InspectorDrawer {
1011
private List<MethodPropertyDrawer> arrayContentDrawer;
1112
private ReorderableList arrayHandler;
1213
private bool showListEdit;
1314
private readonly Type elementType;
1415

15-
static ListInspectorDrawer() => RegisterCustomInspectorDrawer<ListInspectorDrawer>(typeof(IList), -2);
16-
1716
public ListInspectorDrawer(object target, Type targetType, bool shown, bool showProps, bool showPrivateFields, bool showObsolete, bool showMethods) :
1817
base(target, targetType, shown, showProps, showPrivateFields, showObsolete, showMethods) {
1918
elementType = Helper.GetGenericListType(targetType);

UInspectorPlus/MethodPropertyDrawer.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class MethodPropertyDrawer: IReflectorDrawer, IDisposable {
4242
private Exception getException;
4343

4444
public UnityObject Component {
45-
get { return component; }
45+
get => component;
4646
set {
4747
component = value;
4848
if(selectedField != null && selectedField.DeclaringType != null && !selectedField.DeclaringType.IsInstanceOfType(component))
@@ -53,7 +53,7 @@ public UnityObject Component {
5353
}
5454

5555
public MemberInfo Info {
56-
get { return memberInfo; }
56+
get => memberInfo;
5757
set {
5858
memberInfo = value;
5959
if(memberInfo != null)
@@ -62,7 +62,7 @@ public MemberInfo Info {
6262
}
6363

6464
public MemberInfo RefFieldInfo {
65-
get { return selectedField ?? selectedProperty as MemberInfo; }
65+
get => selectedField ?? selectedProperty as MemberInfo;
6666
set {
6767
if(component == null)
6868
return;
@@ -84,7 +84,7 @@ public MemberInfo RefFieldInfo {
8484
public bool Updatable { get; set; }
8585

8686
public bool AllowReferenceMode {
87-
get { return allowReferenceMode; }
87+
get => allowReferenceMode;
8888
set {
8989
allowReferenceMode = value;
9090
if(!value && referenceMode)
@@ -93,7 +93,7 @@ public bool AllowReferenceMode {
9393
}
9494

9595
public bool ReferenceMode {
96-
get { return referenceMode; }
96+
get => referenceMode;
9797
set {
9898
referenceMode = value && allowReferenceMode;
9999
fields.Clear();
@@ -111,9 +111,7 @@ public bool ReferenceMode {
111111
}
112112

113113
public bool AllowPrivateFields {
114-
get {
115-
return privateFields;
116-
}
114+
get => privateFields;
117115
set {
118116
privateFields = value;
119117
if(referenceMode)
@@ -124,9 +122,7 @@ public bool AllowPrivateFields {
124122
}
125123

126124
public bool AllowObsolete {
127-
get {
128-
return obsolete;
129-
}
125+
get => obsolete;
130126
set {
131127
obsolete = value;
132128
if(referenceMode)
@@ -158,7 +154,7 @@ public object Value {
158154
}
159155

160156
public Exception GetException {
161-
get { return getException; }
157+
get => getException;
162158
set {
163159
getException = value;
164160
if(Updatable)
@@ -352,6 +348,8 @@ public bool UpdateValue() {
352348
return false;
353349
}
354350

351+
public void SetDirty() => Changed = true;
352+
355353
public void Dispose() {
356354
drawerRequestingReferences.Remove(this);
357355
if(ctorDrawer != null) ctorDrawer.Dispose();
@@ -408,6 +406,7 @@ private void DrawCtorField() {
408406
EditorGUI.indentLevel--;
409407
if(ctorDrawer.Value != null) {
410408
rawValue = ctorDrawer.Value;
409+
Changed = true;
411410
grabValueMode = 0;
412411
RequireRedraw();
413412
}

UInspectorPlus/TypeInspectorDrawer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
using System;
33

44
namespace UInspectorPlus {
5+
[CustomInspectorDrawer(typeof(Type), -1)]
56
internal class TypeInspectorDrawer: InspectorDrawer {
6-
static TypeInspectorDrawer() => RegisterCustomInspectorDrawer<TypeInspectorDrawer>(typeof(Type), -1);
7-
87
public TypeInspectorDrawer(object target, Type targetType, bool shown, bool showProps, bool showPrivateFields, bool showObsolete, bool showMethods) :
98
base(target, targetType, shown, showProps, showPrivateFields, showObsolete, showMethods) {
109
}

0 commit comments

Comments
 (0)