A game about forced loneliness, made by TACStudios
1#if UNITY_EDITOR
2using System;
3using System.Collections.Generic;
4using UnityEditor;
5
6namespace UnityEngine.InputSystem.Editor
7{
8 internal static class SerializedPropertyLinqExtensions
9 {
10 public static IEnumerable<T> Select<T>(this SerializedProperty property, Func<SerializedProperty, T> selector)
11 {
12 if (property == null)
13 throw new ArgumentNullException(nameof(property));
14
15 if (selector == null)
16 throw new ArgumentNullException(nameof(selector));
17
18 if (property.isArray == false)
19 yield break;
20
21 for (var i = 0; i < property.arraySize; i++)
22 {
23 yield return selector(property.GetArrayElementAtIndex(i));
24 }
25 }
26
27 public static IEnumerable<SerializedProperty> Where(this SerializedProperty property,
28 Func<SerializedProperty, bool> predicate)
29 {
30 if (property == null)
31 throw new ArgumentNullException(nameof(property));
32
33 if (predicate == null)
34 throw new ArgumentNullException(nameof(predicate));
35
36 if (property.isArray == false)
37 yield break;
38
39 for (var i = 0; i < property.arraySize; i++)
40 {
41 var element = property.GetArrayElementAtIndex(i);
42 if (predicate(element))
43 yield return element;
44 }
45 }
46
47 public static SerializedProperty FindLast(this SerializedProperty property, Func<SerializedProperty, bool> predicate)
48 {
49 Debug.Assert(predicate != null, "Missing predicate for FindLast function.");
50 Debug.Assert(property != null, "SerializedProperty missing for FindLast function.");
51
52 if (property.isArray == false)
53 return null;
54
55 for (int i = property.arraySize - 1; i >= 0; i--)
56 {
57 var element = property.GetArrayElementAtIndex(i);
58 if (predicate(element))
59 return element;
60 }
61 return null;
62 }
63
64 public static SerializedProperty FirstOrDefault(this SerializedProperty property)
65 {
66 if (property == null)
67 throw new ArgumentNullException(nameof(property));
68
69 if (property.isArray == false || property.arraySize == 0)
70 return null;
71
72 return property.GetArrayElementAtIndex(0);
73 }
74
75 public static SerializedProperty FirstOrDefault(this SerializedProperty property,
76 Func<SerializedProperty, bool> predicate)
77 {
78 if (property == null)
79 throw new ArgumentNullException(nameof(property));
80
81 if (predicate == null)
82 throw new ArgumentNullException(nameof(predicate));
83
84 if (property.isArray == false)
85 return null;
86
87 for (var i = 0; i < property.arraySize; i++)
88 {
89 var arrayElementAtIndex = property.GetArrayElementAtIndex(i);
90 if (predicate(arrayElementAtIndex) == false)
91 continue;
92
93 return arrayElementAtIndex;
94 }
95
96 return null;
97 }
98
99 public static IEnumerable<SerializedProperty> Skip(this SerializedProperty property, int count)
100 {
101 if (property == null)
102 throw new ArgumentNullException(nameof(property));
103
104 if (count < 0)
105 throw new ArgumentOutOfRangeException(nameof(count));
106
107 return SkipIterator(property, count);
108 }
109
110 public static IEnumerable<SerializedProperty> Take(this SerializedProperty property, int count)
111 {
112 if (property == null)
113 throw new ArgumentNullException(nameof(property));
114
115 if (count < 0 || count > property.arraySize)
116 throw new ArgumentOutOfRangeException(nameof(count));
117
118 return TakeIterator(property, count);
119 }
120
121 private static IEnumerable<SerializedProperty> SkipIterator(SerializedProperty source, int count)
122 {
123 var enumerator = source.GetEnumerator();
124 while (count > 0 && enumerator.MoveNext()) count--;
125 if (count <= 0)
126 {
127 while (enumerator.MoveNext())
128 yield return (SerializedProperty)enumerator.Current;
129 }
130 }
131
132 private static IEnumerable<SerializedProperty> TakeIterator(SerializedProperty source, int count)
133 {
134 if (count > 0)
135 {
136 foreach (SerializedProperty element in source)
137 {
138 yield return element;
139 if (--count == 0) break;
140 }
141 }
142 }
143 }
144}
145
146#endif