1
+ using System . IO ;
2
+ using System . Reflection ;
3
+ using System . Collections ;
4
+ using System . Collections . Generic ;
5
+ using System . Text . RegularExpressions ;
6
+ using UnityEngine ;
7
+ using UnityEditor ;
8
+ using UnityEditor . Callbacks ;
9
+ using Type = System . Type ;
10
+
11
+ public static class SOArchitecture_EditorUtility
12
+ {
13
+ static SOArchitecture_EditorUtility ( )
14
+ {
15
+ //We use this as a default since it'll be Assembly-CSharp-Editor
16
+ _defaultTargetType = typeof ( SOArchitecture_EditorUtility ) . Assembly ;
17
+ }
18
+
19
+ private static PropertyDrawerGraph _propertyDrawerGraph ;
20
+ private static Regex _removeWhiteSpace = new Regex ( @"\s+" ) ;
21
+ private static Assembly _defaultTargetType ;
22
+ private static BindingFlags _fieldBindingsFlag = BindingFlags . Instance | BindingFlags . NonPublic ;
23
+
24
+ public static bool HasPropertyDrawer ( Type type )
25
+ {
26
+ return HasPropertyDrawer ( type , _defaultTargetType ) ;
27
+ }
28
+ public static bool HasPropertyDrawer ( Type type , Assembly assembly )
29
+ {
30
+ if ( HasBuiltinPropertyDrawer ( type ) )
31
+ return true ;
32
+
33
+ if ( _propertyDrawerGraph == null )
34
+ _propertyDrawerGraph = new PropertyDrawerGraph ( ) ;
35
+
36
+ _propertyDrawerGraph . CreateGraph ( assembly ) ;
37
+
38
+ return _propertyDrawerGraph . HasPropertyDrawer ( type ) ;
39
+ }
40
+ private static bool HasBuiltinPropertyDrawer ( Type type )
41
+ {
42
+ if ( type . IsPrimitive || type == typeof ( string ) || typeof ( Object ) . IsAssignableFrom ( type ) )
43
+ return true ;
44
+
45
+ return false ;
46
+ }
47
+ [ DidReloadScripts ]
48
+ private static void OnProjectReloaded ( )
49
+ {
50
+ _propertyDrawerGraph = null ;
51
+ }
52
+
53
+ /// <summary>
54
+ /// Goes through the entirety of the project and collects data about custom property drawers
55
+ /// </summary>
56
+ private class PropertyDrawerGraph
57
+ {
58
+ private List < Type > _supportedTypes = new List < Type > ( ) ;
59
+ private List < Type > _supportedInheritedTypes = new List < Type > ( ) ;
60
+ private List < Assembly > _checkedAssemblies = new List < Assembly > ( ) ;
61
+
62
+ public bool HasPropertyDrawer ( Type type )
63
+ {
64
+ foreach ( Type supportedType in _supportedTypes )
65
+ {
66
+ if ( supportedType == type )
67
+ return true ;
68
+ }
69
+
70
+ foreach ( Type inheritedSupportedType in _supportedInheritedTypes )
71
+ {
72
+ if ( type . IsSubclassOf ( inheritedSupportedType ) )
73
+ return true ;
74
+ }
75
+
76
+ return false ;
77
+ }
78
+ public void CreateGraph ( Assembly assembly )
79
+ {
80
+ if ( _checkedAssemblies . Contains ( assembly ) )
81
+ return ;
82
+
83
+ _checkedAssemblies . Add ( assembly ) ;
84
+
85
+ foreach ( Type type in assembly . GetTypes ( ) )
86
+ {
87
+ object [ ] attributes = type . GetCustomAttributes ( typeof ( CustomPropertyDrawer ) , false ) ;
88
+
89
+ foreach ( object attribute in attributes )
90
+ {
91
+ if ( attribute is CustomPropertyDrawer )
92
+ {
93
+ CustomPropertyDrawer drawerData = attribute as CustomPropertyDrawer ;
94
+
95
+ bool useForChildren = ( bool ) typeof ( CustomPropertyDrawer ) . GetField ( "m_UseForChildren" , _fieldBindingsFlag ) . GetValue ( drawerData ) ;
96
+ Type targetType = ( Type ) typeof ( CustomPropertyDrawer ) . GetField ( "m_Type" , _fieldBindingsFlag ) . GetValue ( drawerData ) ;
97
+
98
+ if ( useForChildren )
99
+ {
100
+ _supportedInheritedTypes . Add ( targetType ) ;
101
+ }
102
+ else
103
+ {
104
+ _supportedTypes . Add ( targetType ) ;
105
+ }
106
+ }
107
+ }
108
+ }
109
+ }
110
+ }
111
+ }
0 commit comments