42 lines
1.4 KiB
C#
42 lines
1.4 KiB
C#
using System.Collections.Generic;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
using Syntriax.Engine.Core.Abstract;
|
|
|
|
namespace Syntriax.Engine.Core;
|
|
|
|
public static class HierarchyObjectExtensions
|
|
{
|
|
public static T SetHierarchyObject<T>(this T hierarchyObject, string? name = "", IHierarchyObject? parent = null) where T : IHierarchyObject
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(name))
|
|
hierarchyObject.Name = name;
|
|
if (parent is not null)
|
|
hierarchyObject.SetParent(parent);
|
|
return hierarchyObject;
|
|
}
|
|
|
|
public static T? FindHierarchyObject<T>(this IEnumerable<IHierarchyObject> hierarchyObjects) where T : class
|
|
{
|
|
foreach (IHierarchyObject hierarchyObject in hierarchyObjects)
|
|
if (hierarchyObject is T @object)
|
|
return @object;
|
|
|
|
return default;
|
|
}
|
|
|
|
public static bool TryFindHierarchyObject<T>(this IEnumerable<IHierarchyObject> hierarchyObjects, [NotNullWhen(returnValue: true)] out T? behaviour) where T : class
|
|
{
|
|
behaviour = FindHierarchyObject<T>(hierarchyObjects);
|
|
return behaviour is not null;
|
|
}
|
|
|
|
public static void FindHierarchyObjects<T>(this IEnumerable<IHierarchyObject> hierarchyObjects, List<T> behaviours) where T : class
|
|
{
|
|
behaviours.Clear();
|
|
foreach (IHierarchyObject hierarchyObject in hierarchyObjects)
|
|
if (hierarchyObject is T @object)
|
|
behaviours.Add(@object);
|
|
}
|
|
}
|