少年修仙传客户端基础资源
dabaoji
2023-11-13 0a23e1b83f8fda8ab9f9e32fe71c26c431faf63a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using qtools.qhierarchy.pcomponent.pbase;
using qtools.qhierarchy.phierarchy;
using qtools.qhierarchy.phelper;
using qtools.qhierarchy.pdata;
using System.Reflection;
using System.Collections;
using UnityEditorInternal;
using System.Text;
 
namespace qtools.qhierarchy.pcomponent
{
    public class QErrorComponent: QBaseComponent
    {
        // PRIVATE
        private Texture2D errorIconTexture;
        private Texture2D errorChildIconTexture;
        private Color backgroundColor;
        private bool showErrorOfChildren;
        private bool showErrorTypeReferenceIsNull;
        private bool showErrorTypeStringIsEmpty;
        private bool showErrorIconScriptIsMissing;
        private bool showErrorIconWhenTagIsUndefined;
        private bool showErrorForDisabledComponents;
        private bool showErrorIconMissingEventMethod;
        private List<string> ignoreErrorOfMonoBehaviours;
        private StringBuilder errorStringBuilder;
        private int errorCount;
 
        // CONSTRUCTOR
        public QErrorComponent ()
        {
            errorIconTexture = QResources.getInstance().getTexture(QTexture.QErrorIcon);
            errorChildIconTexture = QResources.getInstance().getTexture(QTexture.QErrorChildIcon);
            backgroundColor = QResources.getInstance().getColor(QColor.Background);
 
            QSettings.getInstance().addEventListener(QSetting.ShowErrorIconParent             , settingsChanged);
            QSettings.getInstance().addEventListener(QSetting.ShowErrorIconReferenceIsNull    , settingsChanged);
            QSettings.getInstance().addEventListener(QSetting.ShowErrorIconStringIsEmpty      , settingsChanged);
            QSettings.getInstance().addEventListener(QSetting.ShowErrorIconScriptIsMissing    , settingsChanged);
            QSettings.getInstance().addEventListener(QSetting.ShowErrorForDisabledComponents  , settingsChanged);
            QSettings.getInstance().addEventListener(QSetting.ShowErrorIconMissingEventMethod , settingsChanged);
            QSettings.getInstance().addEventListener(QSetting.ShowErrorIconWhenTagOrLayerIsUndefined , settingsChanged);
            QSettings.getInstance().addEventListener(QSetting.ShowErrorComponent              , settingsChanged);
            QSettings.getInstance().addEventListener(QSetting.ShowErrorComponentDuringPlayMode, settingsChanged);
            QSettings.getInstance().addEventListener(QSetting.IgnoreErrorOfMonoBehaviours     , settingsChanged);
            settingsChanged();
        }
 
        // PRIVATE
        private void settingsChanged()
        {
            showErrorOfChildren = QSettings.getInstance().get<bool>(QSetting.ShowErrorIconParent);
            showErrorTypeReferenceIsNull = QSettings.getInstance().get<bool>(QSetting.ShowErrorIconReferenceIsNull);
            showErrorTypeStringIsEmpty = QSettings.getInstance().get<bool>(QSetting.ShowErrorIconStringIsEmpty);
            showErrorIconScriptIsMissing = QSettings.getInstance().get<bool>(QSetting.ShowErrorIconScriptIsMissing);
            showErrorForDisabledComponents = QSettings.getInstance().get<bool>(QSetting.ShowErrorForDisabledComponents);
            showErrorIconMissingEventMethod = QSettings.getInstance().get<bool>(QSetting.ShowErrorIconMissingEventMethod);
            showErrorIconWhenTagIsUndefined = QSettings.getInstance().get<bool>(QSetting.ShowErrorIconWhenTagOrLayerIsUndefined);
            enabled = QSettings.getInstance().get<bool>(QSetting.ShowErrorComponent);
            showComponentDuringPlayMode = QSettings.getInstance().get<bool>(QSetting.ShowErrorComponentDuringPlayMode);
            string ignoreErrorOfMonoBehavioursString = QSettings.getInstance().get<string>(QSetting.IgnoreErrorOfMonoBehaviours);
            if (ignoreErrorOfMonoBehavioursString != "") 
            {
                ignoreErrorOfMonoBehaviours = new List<string>(ignoreErrorOfMonoBehavioursString.Split(new char[] { ',', ';', '.', ' ' }));
                ignoreErrorOfMonoBehaviours.RemoveAll(item => item == "");
            }
            else ignoreErrorOfMonoBehaviours = null;
        }
 
        // DRAW
        public override void layout(GameObject gameObject, QObjectList objectList, ref Rect rect)
        {
            rect.x -= 16;
            rect.width = 16;
        }
 
        public override void draw(GameObject gameObject, QObjectList objectList, Rect selectionRect, Rect curRect)
        {
            bool errorFound = findError(gameObject, gameObject.GetComponents<MonoBehaviour>());
 
            EditorGUI.DrawRect(curRect, backgroundColor);
            if (errorFound)
            {           
                GUI.DrawTexture(curRect, errorIconTexture);
            }
            else if (showErrorOfChildren) 
            {
                errorFound = findError(gameObject, gameObject.GetComponentsInChildren<MonoBehaviour>(true));
                if (errorFound) GUI.DrawTexture(curRect, errorChildIconTexture);
            }            
        }
 
        public override void eventHandler(GameObject gameObject, QObjectList objectList, Event currentEvent, Rect curRect)
        {
            if (currentEvent.isMouse && currentEvent.type == EventType.MouseDown && currentEvent.button == 0 && curRect.Contains(currentEvent.mousePosition))
            {
                currentEvent.Use();
 
                errorCount = 0;
                errorStringBuilder = new StringBuilder();
                findError(gameObject, gameObject.GetComponents<MonoBehaviour>(), true);
 
                if (errorCount > 0)
                {
                    EditorUtility.DisplayDialog(errorCount + (errorCount == 1 ? " error was found" : " errors were found"), errorStringBuilder.ToString(), "OK");
                }
            }
        }
 
        // PRIVATE
        private bool findError(GameObject gameObject, MonoBehaviour[] components, bool printError = false)
        {
            if (showErrorIconWhenTagIsUndefined)
            {
                try
                { 
                    gameObject.tag.CompareTo(null); 
                }
                catch 
                {
                    if (printError)
                    {
                        appendErrorLine("Tag is undefined");
                    }
                    else
                    {
                        return true;
                    }
                }
 
                if (LayerMask.LayerToName(gameObject.layer).Equals("")) 
                {
                    if (printError)
                    {
                        appendErrorLine("Layer is undefined");
                    }
                    else
                    {
                        return true;
                    }
                }
            }
 
            for (int i = 0; i < components.Length; i++)
            {
                MonoBehaviour monoBehaviour = components[i];
                if (monoBehaviour == null)
                {
                    if (showErrorIconScriptIsMissing)
                    {
                        if (printError)
                        {
                            appendErrorLine("Component #" + i + " is missing");
                        }
                        else
                        {
                            return true;
                        }
                    }
                }
                else
                {
                    if (ignoreErrorOfMonoBehaviours != null)
                    {
                        for (int j = ignoreErrorOfMonoBehaviours.Count - 1; j >= 0; j--)
                        {
                            if (monoBehaviour.GetType().FullName.Contains(ignoreErrorOfMonoBehaviours[j]))
                            {
                                return false;
                            }
                        }
                    }
 
                    if (showErrorIconMissingEventMethod)
                    {
                        if (monoBehaviour.gameObject.activeSelf || showErrorForDisabledComponents)
                        {
                            try
                            {
                                if (isUnityEventsNullOrMissing(monoBehaviour, printError))
                                {
                                    return true;
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
 
                    if (showErrorTypeReferenceIsNull || showErrorTypeStringIsEmpty)
                    {                       
                        if (!(monoBehaviour.enabled && monoBehaviour.gameObject.activeSelf) && !showErrorForDisabledComponents) continue;
                        
                        FieldInfo[] fieldArray = monoBehaviour.GetType().GetFields();
                        for (int j = 0; j < fieldArray.Length; j++)
                        {
                            FieldInfo field = fieldArray[j];
                            
                            try
                            {
                                if (System.Attribute.IsDefined(field, typeof(HideInInspector)) || field.IsStatic) continue;
                                
                                object value = field.GetValue(monoBehaviour);
                                
                                if (showErrorTypeReferenceIsNull && (value == null || value.Equals(null)))
                                {           
                                    if (printError)
                                    {
                                        appendErrorLine(monoBehaviour.GetType().Name + "." + field.Name + ": Reference is null");
                                    }
                                    else
                                    {
                                        return true;
                                    }
                                }
                                else if (field.FieldType == typeof(string))
                                {                                
                                    if (showErrorTypeStringIsEmpty && value != null && ((string)value).Equals(""))
                                    {
                                        if (printError)
                                        {
                                            appendErrorLine(monoBehaviour.GetType().Name + "." + field.Name + ": String value is empty");
                                        }
                                        else
                                        {
                                            return true;                                 
                                        }
                                    }
                                }
                                else 
                                {
                                    if (showErrorTypeReferenceIsNull && (value is IEnumerable))
                                    {
                                        foreach (var item in (IEnumerable)value)
                                        {
                                            if (item == null)
                                            {
                                                if (printError)
                                                {
                                                    appendErrorLine(monoBehaviour.GetType().Name + "." + field.Name + ": IEnumerable has value with null reference");
                                                }
                                                else
                                                {
                                                    return true;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                }
            }
            return false;
        }
 
        private List<string> targetPropertiesNames = new List<string>(10);
        
        private bool isUnityEventsNullOrMissing(MonoBehaviour monoBehaviour, bool printError) 
        {
            targetPropertiesNames.Clear();
            FieldInfo[] fieldArray = monoBehaviour.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); 
   
            for (int i = fieldArray.Length - 1; i >= 0; i--) 
            {
                FieldInfo field = fieldArray[i];                    
                if (field.FieldType == typeof(UnityEvent) || field.FieldType.IsSubclassOf(typeof(UnityEvent))) 
                {
                    targetPropertiesNames.Add(field.Name);
                }
            }
            
            if (targetPropertiesNames.Count > 0) 
            {
                SerializedObject serializedMonoBehaviour = new SerializedObject(monoBehaviour); 
                for (int i = targetPropertiesNames.Count - 1; i >= 0; i--) 
                {
                    string targetProperty = targetPropertiesNames[i];
 
                    SerializedProperty property = serializedMonoBehaviour.FindProperty(targetProperty);
                    SerializedProperty propertyRelativeArrray = property.FindPropertyRelative("m_PersistentCalls.m_Calls");
                    
                    for (int j = propertyRelativeArrray.arraySize - 1; j >= 0; j--)
                    {
                        SerializedProperty arrayElementAtIndex = propertyRelativeArrray.GetArrayElementAtIndex(j);
 
                        SerializedProperty propertyTarget       = arrayElementAtIndex.FindPropertyRelative("m_Target");
                        if (propertyTarget.objectReferenceValue == null)
                        {
                            if (printError)
                            {
                                appendErrorLine(monoBehaviour.GetType().Name + ": Event object reference is null");
                            }
                            else
                            {
                                return true;
                            }
                        }
 
                        SerializedProperty propertyMethodName   = arrayElementAtIndex.FindPropertyRelative("m_MethodName");
                        if (string.IsNullOrEmpty(propertyMethodName.stringValue)) 
                        {
                            if (printError)
                            {
                                appendErrorLine(monoBehaviour.GetType().Name + ": Event handler function is not selected");
                                continue;
                            }
                            else
                            {
                                return true;
                            }
                        }
                         
                        string argumentAssemblyTypeName = arrayElementAtIndex.FindPropertyRelative("m_Arguments").FindPropertyRelative("m_ObjectArgumentAssemblyTypeName").stringValue;
                        System.Type argumentAssemblyType;
                        if (!string.IsNullOrEmpty(argumentAssemblyTypeName)) argumentAssemblyType = System.Type.GetType(argumentAssemblyTypeName, false) ?? typeof(UnityEngine.Object);
                        else argumentAssemblyType = typeof(UnityEngine.Object);
 
                        UnityEventBase dummyEvent;
                        System.Type propertyTypeName = System.Type.GetType(property.FindPropertyRelative("m_TypeName").stringValue, false);
                        if (propertyTypeName == null) dummyEvent = (UnityEventBase) new UnityEvent();
                        else dummyEvent = Activator.CreateInstance(propertyTypeName) as UnityEventBase;
 
                        if (!UnityEventDrawer.IsPersistantListenerValid(dummyEvent, propertyMethodName.stringValue, propertyTarget.objectReferenceValue, (PersistentListenerMode)arrayElementAtIndex.FindPropertyRelative("m_Mode").enumValueIndex, argumentAssemblyType))
                        { 
                            if (printError)
                            {
                                appendErrorLine(monoBehaviour.GetType().Name + ": Event handler function is missing");
                            }
                            else
                            {
                                return true;
                            }
                        }
                    }
                }   
            }            
            return false;
        }
 
        private void appendErrorLine(string error)
        {
            errorCount++;
            errorStringBuilder.Append(errorCount.ToString());
            errorStringBuilder.Append(") ");
            errorStringBuilder.AppendLine(error);
        }
    }
}