-
Notifications
You must be signed in to change notification settings - Fork 45
/
SceneRefAttributeValidator.cs
686 lines (585 loc) · 25.9 KB
/
SceneRefAttributeValidator.cs
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using Object = UnityEngine.Object;
using System.Collections;
using System.Diagnostics.CodeAnalysis;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace KBCore.Refs
{
public static class SceneRefAttributeValidator
{
private static readonly IList<ReflectionUtil.AttributedField<SceneRefAttribute>> ATTRIBUTED_FIELDS_CACHE = new List<ReflectionUtil.AttributedField<SceneRefAttribute>>();
#if UNITY_EDITOR
/// <summary>
/// Validate all references for every script and every game object in the scene.
/// </summary>
[MenuItem("Tools/KBCore/Validate All Refs")]
public static bool ValidateAllRefs()
{
var validationSuccess = true;
MonoScript[] scripts = MonoImporter.GetAllRuntimeMonoScripts();
for (int i = 0; i < scripts.Length; i++)
{
MonoScript runtimeMonoScript = scripts[i];
Type scriptType = runtimeMonoScript.GetClass();
if (scriptType == null)
{
continue;
}
try
{
ReflectionUtil.GetFieldsWithAttributeFromType(
scriptType,
ATTRIBUTED_FIELDS_CACHE,
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance
);
if (ATTRIBUTED_FIELDS_CACHE.Count == 0)
{
continue;
}
#if UNITY_2021_3_18_OR_NEWER
Object[] objects = Object.FindObjectsByType(scriptType, FindObjectsInactive.Include, FindObjectsSortMode.InstanceID);
#elif UNITY_2020_1_OR_NEWER
Object[] objects = Object.FindObjectsOfType(scriptType, true);
#else
Object[] objects = Object.FindObjectsOfType(scriptType);
#endif
if (objects.Length == 0)
{
continue;
}
Debug.Log($"Validating {ATTRIBUTED_FIELDS_CACHE.Count} field(s) on {objects.Length} {objects[0].GetType().Name} instance(s)");
for (int o = 0; o < objects.Length; o++)
{
validationSuccess &= Validate(objects[o] as MonoBehaviour, ATTRIBUTED_FIELDS_CACHE, false);
}
}
finally
{
ATTRIBUTED_FIELDS_CACHE.Clear();
}
}
return validationSuccess;
}
/// <summary>
/// Validate a single components references, attempting to assign missing references
/// and logging errors as necessary.
/// </summary>
[MenuItem("CONTEXT/Component/Validate Refs")]
[SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "Used as menu item action")]
private static void ValidateRefs(MenuCommand menuCommand)
=> Validate(menuCommand.context as Component);
/// <summary>
/// Clean and validate a single components references. Useful in instances where (for example) Unity has
/// incorrectly serialized a scene reference within a prefab.
/// </summary>
[MenuItem("CONTEXT/Component/Clean and Validate Refs")]
[SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "Used as menu item action")]
private static void CleanValidateRefs(MenuCommand menuCommand)
=> CleanValidate(menuCommand.context as Component);
#endif
/// <summary>
/// Validate a single components references, attempting to assign missing references
/// and logging errors as necessary.
/// </summary>
public static void ValidateRefs(this Component c, bool updateAtRuntime = false)
=> Validate(c, updateAtRuntime);
/// <summary>
/// Validate a single components references, attempting to assign missing references
/// and logging errors as necessary.
/// </summary>
public static void Validate(Component c, bool updateAtRuntime = false)
{
try
{
ReflectionUtil.GetFieldsWithAttributeFromType(
c.GetType(),
ATTRIBUTED_FIELDS_CACHE,
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance
);
Validate(c, ATTRIBUTED_FIELDS_CACHE, updateAtRuntime);
}
finally
{
ATTRIBUTED_FIELDS_CACHE.Clear();
}
}
/// <summary>
/// Clean and validate a single components references. Useful in instances where (for example) Unity has
/// incorrectly serialized a scene reference within a prefab.
/// </summary>
public static void CleanValidate(Component c, bool updateAtRuntime = false)
{
try
{
ReflectionUtil.GetFieldsWithAttributeFromType(
c.GetType(),
ATTRIBUTED_FIELDS_CACHE,
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance
);
Clean(c, ATTRIBUTED_FIELDS_CACHE);
Validate(c, ATTRIBUTED_FIELDS_CACHE, updateAtRuntime);
}
finally
{
ATTRIBUTED_FIELDS_CACHE.Clear();
}
}
private static bool Validate(
Component c,
IList<ReflectionUtil.AttributedField<SceneRefAttribute>> requiredFields,
bool updateAtRuntime
)
{
if (requiredFields.Count == 0)
{
Debug.LogWarning($"{c.GetType().Name} has no required fields", c.gameObject);
return true;
}
var validationSuccess = true;
bool isUninstantiatedPrefab = PrefabUtil.IsUninstantiatedPrefab(c.gameObject);
for (int i = 0; i < requiredFields.Count; i++)
{
ReflectionUtil.AttributedField<SceneRefAttribute> attributedField = requiredFields[i];
SceneRefAttribute attribute = attributedField.Attribute;
FieldInfo field = attributedField.FieldInfo;
if (field.FieldType.IsInterface)
{
throw new Exception($"{c.GetType().Name} cannot serialize interface {field.Name} directly, use InterfaceRef instead");
}
object fieldValue = field.GetValue(c);
if (updateAtRuntime || !Application.isPlaying)
{
fieldValue = UpdateRef(attribute, c, field, fieldValue);
}
if (isUninstantiatedPrefab)
{
continue;
}
validationSuccess &= ValidateRef(attribute, c, field, fieldValue);
}
return validationSuccess;
}
private static void Clean(
Component c,
IList<ReflectionUtil.AttributedField<SceneRefAttribute>> requiredFields
)
{
for (int i = 0; i < requiredFields.Count; i++)
{
ReflectionUtil.AttributedField<SceneRefAttribute> attributedField = requiredFields[i];
SceneRefAttribute attribute = attributedField.Attribute;
if (attribute.Loc == RefLoc.Anywhere)
{
continue;
}
FieldInfo field = attributedField.FieldInfo;
field.SetValue(c, null);
#if UNITY_EDITOR
EditorUtility.SetDirty(c);
#endif
}
}
private static object UpdateRef(
SceneRefAttribute attr,
Component component,
FieldInfo field,
object existingValue
)
{
Type fieldType = field.FieldType;
bool excludeSelf = attr.HasFlags(Flag.ExcludeSelf);
bool isCollection = IsCollectionType(fieldType, out bool _, out bool isList);
bool includeInactive = attr.HasFlags(Flag.IncludeInactive);
ISerializableRef iSerializable = null;
if (typeof(ISerializableRef).IsAssignableFrom(fieldType))
{
iSerializable = (ISerializableRef)(existingValue ?? Activator.CreateInstance(fieldType));
fieldType = iSerializable.RefType;
existingValue = iSerializable.SerializedObject;
}
if (attr.HasFlags(Flag.Editable))
{
bool isFilledArray = isCollection && (existingValue as IEnumerable).CountEnumerable() > 0;
if (isFilledArray || existingValue is Object)
{
// If the field is editable and the value has already been set, keep it.
return existingValue;
}
}
Type elementType = fieldType;
if (isCollection)
{
elementType = GetElementType(fieldType);
if (typeof(ISerializableRef).IsAssignableFrom(elementType))
{
Type interfaceType = elementType?
.GetInterfaces()
.FirstOrDefault(type =>
type.IsGenericType &&
type.GetGenericTypeDefinition() == typeof(ISerializableRef<>));
if (interfaceType != null)
{
elementType = interfaceType.GetGenericArguments()[0];
}
}
}
object value = null;
//INFO: when minimal unity version will be sufficiently high, explicit casts to object will not be necessary.
switch (attr.Loc)
{
case RefLoc.Anywhere:
if (isCollection ? typeof(ISerializableRef).IsAssignableFrom(fieldType.GetElementType()) : iSerializable != null)
{
value = isCollection
? (object)(existingValue as ISerializableRef[])?.Select(existingRef => GetComponentIfWrongType(existingRef.SerializedObject, elementType)).ToArray()
: (object)GetComponentIfWrongType(existingValue, elementType);
}
break;
case RefLoc.Self:
value = isCollection
? (object)component.GetComponents(elementType)
: (object)component.GetComponent(elementType);
break;
case RefLoc.Parent:
value = isCollection
? (object)GetComponentsInParent(component, elementType, includeInactive, excludeSelf)
: (object)GetComponentInParent(component, elementType, includeInactive, excludeSelf);
break;
case RefLoc.Child:
value = isCollection
? (object)GetComponentsInChildren(component, elementType, includeInactive, excludeSelf)
: (object)GetComponentInChildren(component, elementType, includeInactive, excludeSelf);
break;
case RefLoc.Scene:
value = GetComponentsInScene(elementType, includeInactive, isCollection, excludeSelf);
break;
default:
throw new Exception($"Unhandled Loc={attr.Loc}");
}
if (value == null)
{
return existingValue;
}
SceneRefFilter filter = attr.Filter;
if (isCollection)
{
Type realElementType = GetElementType(fieldType);
Array componentArray = (Array)value;
if (filter != null)
{
// TODO: probably a better way to do this without allocating a list
IList<object> list = new List<object>();
foreach (object o in componentArray)
{
if (filter.IncludeSceneRef(o))
{
list.Add(o);
}
}
componentArray = list.ToArray();
}
Array typedArray = Array.CreateInstance(
realElementType ?? throw new InvalidOperationException(),
componentArray.Length
);
if (elementType == realElementType)
{
Array.Copy(componentArray, typedArray, typedArray.Length);
value = typedArray;
}
else if (typeof(ISerializableRef).IsAssignableFrom(realElementType))
{
for (int i = 0; i < typedArray.Length; i++)
{
ISerializableRef elementValue = Activator.CreateInstance(realElementType) as ISerializableRef;
elementValue?.OnSerialize(componentArray.GetValue(i));
typedArray.SetValue(elementValue, i);
}
value = typedArray;
}
}
else if (filter?.IncludeSceneRef(value) == false)
{
iSerializable?.Clear();
#if UNITY_EDITOR
if (existingValue != null)
{
EditorUtility.SetDirty(component);
}
#endif
return null;
}
if (iSerializable == null)
{
bool valueIsEqual = existingValue != null &&
isCollection ? Enumerable.SequenceEqual((IEnumerable<object>)value, (IEnumerable<object>)existingValue) : value.Equals(existingValue);
if (valueIsEqual)
{
return existingValue;
}
if (isList)
{
Type listType = typeof(List<>);
Type[] typeArgs = { fieldType.GenericTypeArguments[0] };
Type constructedType = listType.MakeGenericType(typeArgs);
object newList = Activator.CreateInstance(constructedType);
MethodInfo addMethod = newList.GetType().GetMethod(nameof(List<object>.Add));
foreach (object s in (IEnumerable)value)
{
addMethod.Invoke(newList, new [] { s });
}
field.SetValue(component, newList);
}
else
{
field.SetValue(component, value);
}
}
else
{
if (!iSerializable.OnSerialize(value))
{
return existingValue;
}
}
#if UNITY_EDITOR
EditorUtility.SetDirty(component);
#endif
return value;
}
private static Type GetElementType(Type fieldType)
{
if (fieldType.IsArray)
{
return fieldType.GetElementType();
}
return fieldType.GenericTypeArguments[0];
}
private static object GetComponentIfWrongType(object existingValue, Type elementType)
{
if (existingValue is Component existingComponent && existingComponent && !elementType.IsInstanceOfType(existingValue))
{
return existingComponent.GetComponent(elementType);
}
return existingValue;
}
private static bool ValidateRef(SceneRefAttribute attr, Component c, FieldInfo field, object value)
{
Type fieldType = field.FieldType;
bool isCollection = IsCollectionType(fieldType, out bool _, out bool _);
if (value is ISerializableRef ser)
{
value = ser.SerializedObject;
}
if (IsEmptyOrNull(value, isCollection))
{
if (attr.HasFlags(Flag.Optional))
return true;
Type elementType = isCollection ? fieldType.GetElementType() : fieldType;
elementType = typeof(ISerializableRef).IsAssignableFrom(elementType) ? elementType?.GetGenericArguments()[0] : elementType;
Debug.LogError($"{c.GetType().Name} missing required {elementType?.Name + (isCollection ? "[]" : "")} ref '{field.Name}'", c.gameObject);
return false;
}
if (isCollection)
{
var validationSuccess = true;
IEnumerable a = (IEnumerable)value;
IEnumerator enumerator = a.GetEnumerator();
Type elementType = fieldType.GetElementType();
while (enumerator.MoveNext())
{
object o = enumerator.Current;
if (o is ISerializableRef serObj)
{
o = serObj.SerializedObject;
}
if (o != null)
{
if (attr.HasFlags(Flag.ExcludeSelf) && o is Component valueC &&
valueC.gameObject == c.gameObject)
Debug.LogError($"{c.GetType().Name} {elementType?.Name}[] ref '{field.Name}' cannot contain component from the same GameObject", c.gameObject);
validationSuccess &= ValidateRefLocation(attr.Loc, c, field, o);
}
else
{
Debug.LogError($"{c.GetType().Name} missing required element ref in array '{field.Name}'", c.gameObject);
validationSuccess = false;
}
}
return validationSuccess;
}
else
{
if (attr.HasFlags(Flag.ExcludeSelf) && value is Component valueC && valueC.gameObject == c.gameObject)
Debug.LogError($"{c.GetType().Name} {fieldType.Name} ref '{field.Name}' cannot be on the same GameObject", c.gameObject);
return ValidateRefLocation(attr.Loc, c, field, value);
}
}
private static bool ValidateRefLocation(RefLoc loc, Component c, FieldInfo field, object refObj)
{
switch (refObj)
{
case Component valueC:
return ValidateRefLocation(loc, c, field, valueC);
case ScriptableObject _:
return ValidateRefLocationAnywhere(loc, c, field);
case GameObject _:
return ValidateRefLocationAnywhere(loc, c, field);
default:
throw new Exception($"{c.GetType().Name} has unexpected reference type {refObj?.GetType().Name}");
}
}
private static bool ValidateRefLocation(RefLoc loc, Component c, FieldInfo field, Component refObj)
{
switch (loc)
{
case RefLoc.Anywhere:
break;
case RefLoc.Self:
if (refObj.gameObject != c.gameObject)
{
Debug.LogError($"{c.GetType().Name} requires {field.FieldType.Name} ref '{field.Name}' to be on Self", c.gameObject);
return false;
}
break;
case RefLoc.Parent:
if (!c.transform.IsChildOf(refObj.transform))
{
Debug.LogError($"{c.GetType().Name} requires {field.FieldType.Name} ref '{field.Name}' to be a Parent", c.gameObject);
return false;
}
break;
case RefLoc.Child:
if (!refObj.transform.IsChildOf(c.transform))
{
Debug.LogError($"{c.GetType().Name} requires {field.FieldType.Name} ref '{field.Name}' to be a Child", c.gameObject);
return false;
}
break;
case RefLoc.Scene:
if (c == null)
{
Debug.LogError($"{c.GetType().Name} requires {field.FieldType.Name} ref '{field.Name}' to be in the scene", c.gameObject);
return false;
}
break;
default:
throw new Exception($"Unhandled Loc={loc}");
}
return true;
}
private static bool ValidateRefLocationAnywhere(RefLoc loc, Component c, FieldInfo field)
{
switch (loc)
{
case RefLoc.Anywhere:
return true;
case RefLoc.Self:
case RefLoc.Parent:
case RefLoc.Child:
case RefLoc.Scene:
Debug.LogError($"{c.GetType().Name} requires {field.FieldType.Name} ref '{field.Name}' to be Anywhere", c.gameObject);
return false;
default:
throw new Exception($"Unhandled Loc={loc}");
}
}
private static bool IsEmptyOrNull(object obj, bool isCollection)
{
if (obj is ISerializableRef ser)
{
return !ser.HasSerializedObject;
}
return obj == null || obj.Equals(null) || (isCollection && ((IEnumerable) obj).CountEnumerable() == 0);
}
private static bool IsCollectionType(Type t, out bool isArray, out bool isList)
{
isList = t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<>);
isArray = t.IsArray;
return isList || isArray;
}
private static Component[] GetComponentsInParent(Component c, Type elementType, bool includeInactive, bool excludeSelf)
{
var element = c;
if (excludeSelf)
element = c.transform.parent;
return element == null
? Array.Empty<Component>()
: element.GetComponentsInParent(elementType, includeInactive);
}
private static Component GetComponentInParent(Component c, Type elementType, bool includeInactive,
bool excludeSelf)
{
var element = c;
if (excludeSelf)
element = c.transform.parent;
return element == null
? null
: element.GetComponentInParent(elementType, includeInactive);
}
private static Component[] GetComponentsInChildren(Component c, Type elementType, bool includeInactive, bool excludeSelf)
{
if (!excludeSelf)
return c.GetComponentsInChildren(elementType, includeInactive);
List<Component> components = new List<Component>();
var transform = c.transform;
int childCount = transform.childCount;
for (int i = 0; i < childCount; ++i)
{
var child = transform.GetChild(i);
components.AddRange(child.GetComponentsInChildren(elementType, includeInactive));
}
return components.ToArray();
}
private static Component GetComponentInChildren(Component c, Type elementType, bool includeInactive,
bool excludeSelf)
{
if (!excludeSelf)
return c.GetComponentInChildren(elementType, includeInactive);
var transform = c.transform;
int childCount = transform.childCount;
for (int i = 0; i < childCount; ++i)
{
var child = transform.GetChild(i);
var component = child.GetComponentInChildren(elementType, includeInactive);
if (component != null)
return component;
}
return null;
}
private static object GetComponentsInScene(Type elementType, bool includeInactive, bool isCollection, bool excludeSelf)
{
bool isUnityType = elementType.IsSubclassOf(typeof(Object));
#if UNITY_2021_3_18_OR_NEWER
if (isUnityType)
return isCollection
? (object)Object.FindObjectsByType(elementType, includeInactive ? FindObjectsInactive.Include : FindObjectsInactive.Exclude, FindObjectsSortMode.InstanceID)
: (object)Object.FindFirstObjectByType(elementType, includeInactive ? FindObjectsInactive.Include : FindObjectsInactive.Exclude);
var elements = Object.FindObjectsByType<MonoBehaviour>(includeInactive ? FindObjectsInactive.Include : FindObjectsInactive.Exclude, FindObjectsSortMode.InstanceID);
#elif UNITY_2020_1_OR_NEWER
if (isUnityType)
return isCollection
? (object)Object.FindObjectsOfType(elementType, includeInactive)
: (object)Object.FindObjectOfType(elementType, includeInactive);
var elements = Object.FindObjectsOfType<MonoBehaviour>(includeInactive);
#else
if (isUnityType)
return isCollection
? (object)Object.FindObjectsOfType(elementType)
: (object)Object.FindObjectOfType(elementType);
var elements = Object.FindObjectsOfType<MonoBehaviour>();
#endif
elements = elements.Where(IsCorrectType).ToArray();
if (isCollection)
return (object)elements;
return (object)(elements.Length > 0 ? elements[0] : null);
bool IsCorrectType(MonoBehaviour e) => elementType.IsInterface ? e.GetType().GetInterfaces().Contains(elementType) : e.GetType().IsSubclassOf(elementType);
}
}
}