Repository navigation
Expand file tree
/
Copy pathRequiredGetValidator.cs
More file actions
190 lines (161 loc) · 6.56 KB
/
Copy pathRequiredGetValidator.cs
File metadata and controls
190 lines (161 loc) · 6.56 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace TriInspector.Validators
{
[RegisterTriAttributeValidator(ApplyOnArrayElement = false)]
public class RequiredGetValidator : TriAttributeValidator<RequiredGetAttribute>
{
public override TriExtensionInitializationResult Initialize(TriPropertyDefinition propertyDefinition)
{
if (!typeof(Component).IsAssignableFrom(GetComponentType(propertyDefinition)))
{
return "RequiredGet attribute valid only on types derived from Component";
}
if (!Attribute.InChildren && !Attribute.InParents && !Attribute.IncludeSelf)
{
return "RequiredGet cannot be used when all InChildren,InParents,IncludeSelf is set to false";
}
return base.Initialize(propertyDefinition);
}
public override TriValidationResult Validate(TriProperty property)
{
for (var targetIndex = 0; targetIndex < property.PropertyTree.TargetsCount; targetIndex++)
{
var result = Validate(property, targetIndex);
if (result.MessageType != TriMessageType.None)
{
return result;
}
}
return TriValidationResult.Valid;
}
private TriValidationResult Validate(TriProperty property, int targetIndex)
{
if (property.PropertyTree.RootProperty.GetValue(targetIndex) is not Component rootComponent)
{
return TriValidationResult.Error("RequiredGet attribute can be used only in MonoBehaviour components");
}
var expectedComponents = EnumerateExpectedComponents(property, rootComponent);
switch (property.GetValue(targetIndex))
{
case IList list:
{
var actualComponents = from object element in list select element as Component;
return expectedComponents.SequenceEqual(actualComponents)
? TriValidationResult.Valid
: MakeErrorWithFix(property);
}
case Component actualComponent:
{
return expectedComponents.Contains(actualComponent)
? TriValidationResult.Valid
: MakeErrorWithFix(property);
}
default:
{
return MakeErrorWithFix(property);
}
}
}
private IEnumerable<Component> EnumerateExpectedComponents(TriProperty property, Component root)
{
var componentType = GetComponentType(property.Definition);
var selfComponent = root.GetComponent(componentType);
if (Attribute.IncludeSelf && selfComponent)
{
yield return selfComponent;
}
if (Attribute.InChildren)
{
foreach (var child in root.GetComponentsInChildren(componentType))
{
if (child != selfComponent)
{
yield return child;
}
}
}
if (Attribute.InParents)
{
foreach (var parent in root.GetComponentsInParent(componentType))
{
if (parent != selfComponent)
{
yield return parent;
}
}
}
}
private TriValidationResult MakeErrorWithFix(TriProperty property)
{
return TriValidationResult
.Error(GetErrorMessage(property))
.WithFix(() => Fix(property), "Get");
}
private string GetErrorMessage(TriProperty property)
{
var inSelf = Attribute.IncludeSelf;
var inParents = Attribute.InParents;
var inChildren = Attribute.InChildren;
var name = GetName(property);
var type = GetComponentType(property.Definition).Name;
if (property.IsArray)
{
var content = inChildren && inParents ? $"{(inSelf ? "self and " : "")}children and parents"
: inChildren ? $"{(inSelf ? "self and " : "")}children"
: inParents ? $"{(inSelf ? "self and " : "")}parents"
: inSelf ? "self"
: "";
return $"{name} required to contain all {type}s from {content}";
}
else
{
var content = inChildren && inParents ? $"{(inSelf ? "self or " : "")}child or parent"
: inChildren ? $"{(inSelf ? "self or " : "")}child"
: inParents ? $"{(inSelf ? "self or " : "")}parent"
: inSelf ? "self"
: "";
return $"{name} required to contain any {type} from {content}";
}
}
private void Fix(TriProperty property)
{
property.SetValues(targetIndex =>
{
var root = (Component) property.PropertyTree.RootProperty.GetValue(targetIndex);
var expectedComponents = EnumerateExpectedComponents(property, root).ToList();
if (property.IsArray && property.Definition.FieldType.IsArray)
{
var array = Array.CreateInstance(property.Definition.ArrayElementType, expectedComponents.Count);
((IList) expectedComponents).CopyTo(array, 0);
return array;
}
if (property.IsArray)
{
var list = (IList) Activator.CreateInstance(property.Definition.FieldType);
expectedComponents.ForEach(it => list.Add(it));
return list;
}
return expectedComponents.FirstOrDefault();
});
}
private static Type GetComponentType(TriPropertyDefinition propertyDefinition)
{
return propertyDefinition.IsArray
? propertyDefinition.ArrayElementType
: propertyDefinition.FieldType;
}
private static string GetName(TriProperty property)
{
var name = property.DisplayName;
if (string.IsNullOrEmpty(name))
{
name = property.RawName;
}
return name;
}
}
}