Skip to content

Commit 115b711

Browse files
committed
Sync NaughtyAttributes changes
1 parent 14f3dd0 commit 115b711

2 files changed

Lines changed: 171 additions & 36 deletions

File tree

Assets/Plugins/NaughtyAttributes/Scripts/Editor/Editors/InspectorEditor.cs

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,39 @@ public class InspectorEditor : UnityEditor.Editor
1313
{
1414
private SerializedProperty script;
1515

16-
private IEnumerable<FieldInfo> fields;
17-
private HashSet<FieldInfo> groupedFields;
18-
private Dictionary<string, List<FieldInfo>> groupedFieldsByGroupName;
19-
private IEnumerable<FieldInfo> nonSerializedFields;
20-
private IEnumerable<PropertyInfo> nativeProperties;
21-
private IEnumerable<MethodInfo> methods;
16+
private List<FieldInfo> fields;
17+
private readonly HashSet<FieldInfo> groupedFields;
18+
private readonly Dictionary<string, List<FieldInfo>> groupedFieldsByGroupName;
19+
private List<FieldInfo> nonSerializedFields;
20+
private List<PropertyInfo> nativeProperties;
21+
private List<MethodInfo> methods;
2222

23-
private Dictionary<string, SerializedProperty> serializedPropertiesByFieldName;
23+
private readonly Dictionary<string, SerializedProperty> serializedPropertiesByFieldName;
2424

2525
private bool useDefaultInspector;
2626

27+
bool FilterFields(FieldInfo f) => this.serializedObject.FindProperty(f.Name) != null;
28+
readonly Func<FieldInfo, bool> filterFieldsDelegate;
29+
30+
// Cache non-serialized fields
31+
bool NonSerializedFieldsFilter(FieldInfo f)
32+
{
33+
return f.GetCustomAttribute<DrawerAttribute>(true) != null && this.serializedObject.FindProperty(f.Name) == null;
34+
}
35+
36+
37+
public InspectorEditor()
38+
{
39+
groupedFieldsByGroupName = new Dictionary<string, List<FieldInfo>>();
40+
serializedPropertiesByFieldName = new Dictionary<string, SerializedProperty>();
41+
groupedFields = new HashSet<FieldInfo>();
42+
fields = new List<FieldInfo>(100);
43+
nonSerializedFields = new List<FieldInfo>(100);
44+
nativeProperties = new List<PropertyInfo>(100);
45+
methods = new List<MethodInfo>(100);
46+
filterFieldsDelegate = FilterFields;
47+
}
48+
2749
private void OnEnable()
2850
{
2951
try
@@ -38,10 +60,16 @@ private void OnEnable()
3860
}
3961

4062
// Cache serialized fields
41-
this.fields = ReflectionUtility.GetAllFields(this.target, f => this.serializedObject.FindProperty(f.Name) != null);
63+
this.fields = ReflectionUtility.GetAllFieldsEfficiently(this.target, filterFieldsDelegate, this.fields);
64+
// Cache serialized properties by field name
65+
this.serializedPropertiesByFieldName.Clear();
66+
foreach (var field in this.fields)
67+
{
68+
this.serializedPropertiesByFieldName[field.Name] = this.serializedObject.FindProperty(field.Name);
69+
}
4270

4371
// If there are no NaughtyAttributes use default inspector
44-
if (this.fields.All(f => f.GetCustomAttributes(typeof(NaughtyAttribute), true).Length == 0))
72+
if (this.fields.All(f => f.GetCustomAttribute<NaughtyAttribute>(true) == null))
4573
{
4674
this.useDefaultInspector = true;
4775
}
@@ -50,17 +78,24 @@ private void OnEnable()
5078
this.useDefaultInspector = false;
5179

5280
// Cache grouped fields
53-
this.groupedFields = new HashSet<FieldInfo>(this.fields.Where(f => f.GetCustomAttributes(typeof(GroupAttribute), true).Length > 0));
54-
81+
this.groupedFields.Clear();
82+
foreach (var fi in fields)
83+
{
84+
if (fi.GetCustomAttribute<GroupAttribute>(true) != null)
85+
{
86+
this.groupedFields.Add(fi);
87+
}
88+
}
89+
5590
// Cache grouped fields by group name
56-
this.groupedFieldsByGroupName = new Dictionary<string, List<FieldInfo>>();
91+
groupedFieldsByGroupName.Clear();
5792
foreach (var groupedField in this.groupedFields)
5893
{
59-
string groupName = (groupedField.GetCustomAttributes(typeof(GroupAttribute), true)[0] as GroupAttribute).Name;
94+
string groupName = (groupedField.GetCustomAttribute<GroupAttribute>(true)).Name;
6095

61-
if (this.groupedFieldsByGroupName.ContainsKey(groupName))
96+
if (this.groupedFieldsByGroupName.TryGetValue(groupName, out var list))
6297
{
63-
this.groupedFieldsByGroupName[groupName].Add(groupedField);
98+
list.Add(groupedField);
6499
}
65100
else
66101
{
@@ -71,25 +106,17 @@ private void OnEnable()
71106
}
72107
}
73108

74-
// Cache serialized properties by field name
75-
this.serializedPropertiesByFieldName = new Dictionary<string, SerializedProperty>();
76-
foreach (var field in this.fields)
77-
{
78-
this.serializedPropertiesByFieldName[field.Name] = this.serializedObject.FindProperty(field.Name);
79-
}
80109
}
81110

82-
// Cache non-serialized fields
83-
this.nonSerializedFields = ReflectionUtility.GetAllFields(
84-
this.target, f => f.GetCustomAttributes(typeof(DrawerAttribute), true).Length > 0 && this.serializedObject.FindProperty(f.Name) == null);
111+
this.nonSerializedFields = ReflectionUtility.GetAllFieldsEfficiently(this.target, NonSerializedFieldsFilter, nonSerializedFields);
85112

86113
// Cache the native properties
87-
this.nativeProperties = ReflectionUtility.GetAllProperties(
88-
this.target, p => p.GetCustomAttributes(typeof(DrawerAttribute), true).Length > 0);
114+
this.nativeProperties = ReflectionUtility.GetAllPropertiesEfficiently(
115+
this.target, p => p.GetCustomAttribute<DrawerAttribute>(true) != null, nativeProperties);
89116

90117
// Cache methods with DrawerAttribute
91-
this.methods = ReflectionUtility.GetAllMethods(
92-
this.target, m => m.GetCustomAttributes(typeof(DrawerAttribute), true).Length > 0);
118+
this.methods = ReflectionUtility.GetAllMethodsEfficiently(
119+
this.target, m => m.GetCustomAttribute<DrawerAttribute>(true) != null, methods);
93120
}
94121

95122
private void OnDisable()

Assets/Plugins/NaughtyAttributes/Scripts/Editor/Utility/ReflectionUtility.cs

Lines changed: 116 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ public static IEnumerable<FieldInfo> GetAllFields(object target, Func<FieldInfo,
2222
for (int i = types.Count - 1; i >= 0; i--)
2323
{
2424
IEnumerable<FieldInfo> fieldInfos = types[i]
25-
.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly)
26-
.Where(predicate);
25+
.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public |
26+
BindingFlags.DeclaredOnly)
27+
.Where(predicate);
2728

2829
foreach (var fieldInfo in fieldInfos)
2930
{
@@ -32,6 +33,46 @@ public static IEnumerable<FieldInfo> GetAllFields(object target, Func<FieldInfo,
3233
}
3334
}
3435

36+
public static List<FieldInfo> GetAllFieldsEfficiently(object target, Func<FieldInfo, bool> predicate, List<FieldInfo> buffer)
37+
{
38+
if (buffer != null)
39+
{
40+
buffer.Clear();
41+
}
42+
else
43+
{
44+
buffer = new List<FieldInfo>();
45+
}
46+
47+
var types = new List<Type>()
48+
{
49+
target.GetType()
50+
};
51+
52+
while (types.Last().BaseType != null)
53+
{
54+
types.Add(types.Last().BaseType);
55+
}
56+
57+
for (int i = types.Count - 1; i >= 0; i--)
58+
{
59+
var fieldInfos = types[i]
60+
.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly);
61+
62+
foreach (var fi in fieldInfos)
63+
{
64+
if (!predicate(fi))
65+
{
66+
continue;
67+
}
68+
69+
buffer.Add(fi);
70+
}
71+
}
72+
73+
return buffer;
74+
}
75+
3576
public static IEnumerable<PropertyInfo> GetAllProperties(object target, Func<PropertyInfo, bool> predicate)
3677
{
3778
List<Type> types = new List<Type>()
@@ -47,8 +88,9 @@ public static IEnumerable<PropertyInfo> GetAllProperties(object target, Func<Pro
4788
for (int i = types.Count - 1; i >= 0; i--)
4889
{
4990
IEnumerable<PropertyInfo> propertyInfos = types[i]
50-
.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly)
51-
.Where(predicate);
91+
.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public |
92+
BindingFlags.DeclaredOnly)
93+
.Where(predicate);
5294

5395
foreach (var propertyInfo in propertyInfos)
5496
{
@@ -57,15 +99,81 @@ public static IEnumerable<PropertyInfo> GetAllProperties(object target, Func<Pro
5799
}
58100
}
59101

102+
public static List<PropertyInfo> GetAllPropertiesEfficiently(object target, Func<PropertyInfo, bool> predicate, List<PropertyInfo> buffer)
103+
{
104+
if (buffer != null)
105+
{
106+
buffer.Clear();
107+
}
108+
else
109+
{
110+
buffer = new List<PropertyInfo>();
111+
}
112+
113+
List<Type> types = new List<Type>()
114+
{
115+
target.GetType()
116+
};
117+
118+
while (types.Last().BaseType != null)
119+
{
120+
types.Add(types.Last().BaseType);
121+
}
122+
123+
for (int i = types.Count - 1; i >= 0; i--)
124+
{
125+
var propertyInfos = types[i].GetProperties(BindingFlags.Instance |
126+
BindingFlags.Static |
127+
BindingFlags.NonPublic |
128+
BindingFlags.Public |
129+
BindingFlags.DeclaredOnly);
130+
131+
foreach (var propertyInfo in propertyInfos)
132+
{
133+
if (predicate(propertyInfo))
134+
{
135+
buffer.Add(propertyInfo);
136+
}
137+
}
138+
}
139+
140+
return buffer;
141+
}
142+
60143
public static IEnumerable<MethodInfo> GetAllMethods(object target, Func<MethodInfo, bool> predicate)
61144
{
62-
IEnumerable<MethodInfo> methodInfos = target.GetType()
63-
.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)
64-
.Where(predicate);
145+
var methodInfos = target.GetType()
146+
.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)
147+
.Where(predicate);
65148

66149
return methodInfos;
67150
}
68151

152+
public static List<MethodInfo> GetAllMethodsEfficiently(object target, Func<MethodInfo, bool> predicate, List<MethodInfo> buffer)
153+
{
154+
155+
if (buffer != null)
156+
{
157+
buffer.Clear();
158+
}
159+
else
160+
{
161+
buffer = new List<MethodInfo>();
162+
}
163+
164+
var methodInfos = target.GetType()
165+
.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
166+
167+
foreach (var m in methodInfos)
168+
{
169+
if (predicate(m))
170+
{
171+
buffer.Add(m);
172+
}
173+
}
174+
return buffer;
175+
}
176+
69177
public static FieldInfo GetField(object target, string fieldName)
70178
{
71179
return GetAllFields(target, f => f.Name.Equals(fieldName, StringComparison.InvariantCulture)).FirstOrDefault();
@@ -81,4 +189,4 @@ public static MethodInfo GetMethod(object target, string methodName)
81189
return GetAllMethods(target, m => m.Name.Equals(methodName, StringComparison.InvariantCulture)).FirstOrDefault();
82190
}
83191
}
84-
}
192+
}

0 commit comments

Comments
 (0)