Skip to content

Commit f7f69dd

Browse files
committed
Merge branch 'list-fixes' into main
2 parents aabe48b + e13c84d commit f7f69dd

File tree

3 files changed

+34
-57
lines changed

3 files changed

+34
-57
lines changed

EnumerableExtensions.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ namespace KBCore.Refs
44
{
55
public static class EnumerableExtensions
66
{
7-
public static bool HaveSameCount(this IEnumerable enumerable1, IEnumerable enumerable2)
8-
{
9-
int count1 = enumerable1.CountEnumerable();
10-
int count2 = enumerable2.CountEnumerable();
11-
12-
return count1 == count2;
13-
}
14-
157
public static int CountEnumerable(this IEnumerable enumerable)
168
{
179
int count = 0;
@@ -21,10 +13,5 @@ public static int CountEnumerable(this IEnumerable enumerable)
2113
}
2214
return count;
2315
}
24-
25-
public static bool Any(this IEnumerable enumerable)
26-
{
27-
return enumerable.GetEnumerator().MoveNext();
28-
}
2916
}
3017
}

SceneRefAttributeValidator.cs

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ object existingValue
213213
)
214214
{
215215
Type fieldType = field.FieldType;
216-
bool isArray = typeof(IEnumerable).IsAssignableFrom(fieldType);
216+
bool isCollection = IsCollectionType(fieldType, out bool _, out bool isList);
217217
bool includeInactive = attr.HasFlags(Flag.IncludeInactive);
218218

219219
ISerializableRef iSerializable = null;
@@ -223,12 +223,10 @@ object existingValue
223223
fieldType = iSerializable.RefType;
224224
existingValue = iSerializable.SerializedObject;
225225
}
226-
IEnumerable existingValueEnumerable = existingValue as IEnumerable;
227226

228227
if (attr.HasFlags(Flag.Editable))
229228
{
230-
var enumerator = existingValueEnumerable.GetEnumerator();
231-
bool isFilledArray = isArray && enumerator.MoveNext();
229+
bool isFilledArray = isCollection && (existingValue as IEnumerable).CountEnumerable() > 0;
232230
if (isFilledArray || existingValue is Object)
233231
{
234232
// If the field is editable and the value has already been set, keep it.
@@ -237,7 +235,7 @@ object existingValue
237235
}
238236

239237
Type elementType = fieldType;
240-
if (isArray)
238+
if (isCollection)
241239
{
242240
elementType = GetElementType(fieldType);
243241
if (typeof(ISerializableRef).IsAssignableFrom(elementType))
@@ -260,47 +258,47 @@ object existingValue
260258
switch (attr.Loc)
261259
{
262260
case RefLoc.Anywhere:
263-
if (isArray ? typeof(ISerializableRef).IsAssignableFrom(fieldType.GetElementType()) : iSerializable != null)
261+
if (isCollection ? typeof(ISerializableRef).IsAssignableFrom(fieldType.GetElementType()) : iSerializable != null)
264262
{
265-
value = isArray
263+
value = isCollection
266264
? (existingValue as ISerializableRef[])?.Select(existingRef => GetComponentIfWrongType(existingRef.SerializedObject, elementType)).ToArray()
267265
: GetComponentIfWrongType(existingValue, elementType);
268266
}
269267
break;
270268

271269
case RefLoc.Self:
272-
value = isArray
270+
value = isCollection
273271
? (object)component.GetComponents(elementType)
274272
: (object)component.GetComponent(elementType);
275273
break;
276274

277275
case RefLoc.Parent:
278276
#if UNITY_2020_OR_NEWER
279-
value = isArray
277+
value = isCollection
280278
? c.GetComponentsInParent(elementType, includeInactive)
281279
: (object)c.GetComponentInParent(elementType, includeInactive);
282280
#else
283-
value = isArray
281+
value = isCollection
284282
? (object)component.GetComponentsInParent(elementType, includeInactive)
285283
: (object)component.GetComponentInParent(elementType, includeInactive);
286284
#endif
287285

288286
break;
289287

290288
case RefLoc.Child:
291-
value = isArray
289+
value = isCollection
292290
? (object)component.GetComponentsInChildren(elementType, includeInactive)
293291
: (object)component.GetComponentInChildren(elementType, includeInactive);
294292
break;
295293

296294
case RefLoc.Scene:
297295
#if UNITY_2020_OR_NEWER
298-
value = isArray
296+
value = isCollection
299297
? (object)Object.FindObjectsOfType(elementType, includeInactive)
300298
: (object)Object.FindObjectOfType(elementType, includeInactive);
301299
#else
302300
FindObjectsInactive includeInactiveObjects = includeInactive ? FindObjectsInactive.Include : FindObjectsInactive.Exclude;
303-
value = isArray
301+
value = isCollection
304302
? (object)Object.FindObjectsOfType(elementType)
305303
: (object)Object.FindObjectOfType(elementType);
306304
#endif
@@ -317,7 +315,7 @@ object existingValue
317315

318316
SceneRefFilter filter = attr.Filter;
319317

320-
if (isArray)
318+
if (isCollection)
321319
{
322320
Type realElementType = GetElementType(fieldType);
323321

@@ -371,13 +369,14 @@ object existingValue
371369

372370
if (iSerializable == null)
373371
{
374-
bool valueIsEqual = isArray ? Enumerable.SequenceEqual((IEnumerable<object>)value, (IEnumerable<object>)existingValue) : value.Equals(existingValue);
375-
if (existingValue != null && valueIsEqual)
372+
bool valueIsEqual = existingValue != null &&
373+
isCollection ? Enumerable.SequenceEqual((IEnumerable<object>)value, (IEnumerable<object>)existingValue) : value.Equals(existingValue);
374+
if (valueIsEqual)
376375
{
377376
return existingValue;
378377
}
379378

380-
if (!fieldType.IsArray && typeof(IEnumerable).IsAssignableFrom(fieldType))
379+
if (isList)
381380
{
382381
Type listType = typeof(List<>);
383382
Type[] typeArgs = { fieldType.GenericTypeArguments[0] };
@@ -419,10 +418,7 @@ private static Type GetElementType(Type fieldType)
419418
{
420419
return fieldType.GetElementType();
421420
}
422-
else
423-
{
424-
return fieldType.GenericTypeArguments[0];
425-
}
421+
return fieldType.GenericTypeArguments[0];
426422
}
427423

428424
private static object GetComponentIfWrongType(object existingValue, Type elementType)
@@ -438,28 +434,28 @@ private static object GetComponentIfWrongType(object existingValue, Type element
438434
private static void ValidateRef(SceneRefAttribute attr, Component c, FieldInfo field, object value)
439435
{
440436
Type fieldType = field.FieldType;
441-
bool isArray = typeof(IEnumerable).IsAssignableFrom(fieldType);
437+
bool isCollection = IsCollectionType(fieldType, out bool _, out bool _);
442438

443439
if (value is ISerializableRef ser)
444440
{
445441
value = ser.SerializedObject;
446442
}
447443

448-
if (IsEmptyOrNull(value, isArray))
444+
if (IsEmptyOrNull(value, isCollection))
449445
{
450446
if (!attr.HasFlags(Flag.Optional))
451447
{
452-
Type elementType = isArray ? fieldType.GetElementType() : fieldType;
448+
Type elementType = isCollection ? fieldType.GetElementType() : fieldType;
453449
elementType = typeof(ISerializableRef).IsAssignableFrom(elementType) ? elementType?.GetGenericArguments()[0] : elementType;
454-
Debug.LogError($"{c.GetType().Name} missing required {elementType?.Name + (isArray ? "[]" : "")} ref '{field.Name}'", c.gameObject);
450+
Debug.LogError($"{c.GetType().Name} missing required {elementType?.Name + (isCollection ? "[]" : "")} ref '{field.Name}'", c.gameObject);
455451
}
456452
return;
457453
}
458454

459-
if (isArray)
455+
if (isCollection)
460456
{
461457
IEnumerable a = (IEnumerable)value;
462-
var enumerator = a.GetEnumerator();
458+
IEnumerator enumerator = a.GetEnumerator();
463459
while (enumerator.MoveNext())
464460
{
465461
object o = enumerator.Current;
@@ -479,7 +475,7 @@ private static void ValidateRef(SceneRefAttribute attr, Component c, FieldInfo f
479475
}
480476
}
481477
else
482-
{
478+
{
483479
ValidateRefLocation(attr.Loc, c, field, value);
484480
}
485481
}
@@ -514,42 +510,29 @@ private static void ValidateRefLocation(RefLoc loc, Component c, FieldInfo field
514510

515511
case RefLoc.Self:
516512
if (refObj.gameObject != c.gameObject)
517-
{
518513
Debug.LogError($"{c.GetType().Name} requires {field.FieldType.Name} ref '{field.Name}' to be on Self", c.gameObject);
519-
}
520-
521514
break;
522515

523516
case RefLoc.Parent:
524517
if (!c.transform.IsChildOf(refObj.transform))
525-
{
526518
Debug.LogError($"{c.GetType().Name} requires {field.FieldType.Name} ref '{field.Name}' to be a Parent", c.gameObject);
527-
}
528-
529519
break;
530520

531521
case RefLoc.Child:
532522
if (!refObj.transform.IsChildOf(c.transform))
533-
{
534523
Debug.LogError($"{c.GetType().Name} requires {field.FieldType.Name} ref '{field.Name}' to be a Child", c.gameObject);
535-
}
536-
537524
break;
538525

539526
case RefLoc.Scene:
540527
if (c == null)
541-
{
542528
Debug.LogError($"{c.GetType().Name} requires {field.FieldType.Name} ref '{field.Name}' to be in the scene", c.gameObject);
543-
}
544-
545529
break;
546530

547531
default:
548532
throw new Exception($"Unhandled Loc={loc}");
549533
}
550534
}
551535

552-
// ReSharper disable once UnusedParameter.Local
553536
private static void ValidateRefLocationAnywhere(RefLoc loc, Component c, FieldInfo field)
554537
{
555538
switch (loc)
@@ -569,14 +552,21 @@ private static void ValidateRefLocationAnywhere(RefLoc loc, Component c, FieldIn
569552
}
570553
}
571554

572-
private static bool IsEmptyOrNull(object obj, bool isArray)
555+
private static bool IsEmptyOrNull(object obj, bool isCollection)
573556
{
574557
if (obj is ISerializableRef ser)
575558
{
576559
return !ser.HasSerializedObject;
577560
}
578561

579-
return obj == null || obj.Equals(null) || (isArray && ((IEnumerable)obj).CountEnumerable() == 0);
562+
return obj == null || obj.Equals(null) || (isCollection && ((IEnumerable) obj).CountEnumerable() == 0);
563+
}
564+
565+
private static bool IsCollectionType(Type t, out bool isArray, out bool isList)
566+
{
567+
isList = t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<>);
568+
isArray = t.IsArray;
569+
return isList || isArray;
580570
}
581571
}
582572
}

SceneRefFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public abstract class SceneRefFilter<T> : SceneRefFilter
1111
{
1212

1313
internal override bool IncludeSceneRef(object obj)
14-
=> this.IncludeSceneRef(obj as T);
14+
=> this.IncludeSceneRef((T) obj);
1515

1616
/// <summary>
1717
/// Returns true if the given object should be included as a reference.

0 commit comments

Comments
 (0)