feat: Find & FindRequired for general type search

This commit is contained in:
Syntriax 2025-05-25 12:59:37 +03:00
parent bcce427376
commit 114fa82b9d
5 changed files with 46 additions and 16 deletions

View File

@ -1,9 +1,3 @@
using System;
namespace Syntriax.Engine.Core.Exceptions;
public class BehaviourNotFoundException(string? message) : Exception(message)
{
public static NotAssignedException FromType<TBehaviour>()
=> new($"{typeof(TBehaviour).FullName} was not found");
}
public class BehaviourNotFoundException(string? message) : NotFoundException(message);

View File

@ -0,0 +1,9 @@
using System;
namespace Syntriax.Engine.Core.Exceptions;
public class NotFoundException(string? message) : Exception(message)
{
public static NotAssignedException FromType<T>()
=> new($"{typeof(T).FullName} was not found");
}

View File

@ -1,9 +1,3 @@
using System;
namespace Syntriax.Engine.Core.Exceptions;
public class UniverseObjectNotFoundException(string? message) : Exception(message)
{
public static NotAssignedException FromType<TUniverseObject>()
=> new($"{typeof(TUniverseObject).FullName} was not found");
}
public class UniverseObjectNotFoundException(string? message) : NotFoundException(message);

View File

@ -12,4 +12,7 @@ public static class UniverseExtensions
public static T FindRequiredBehaviour<T>(this IUniverse universe) where T : class
=> universe.FindBehaviour<T>() ?? throw new BehaviourNotFoundException($"{universe.GetType().FullName}({universe.Id}) does not contain any {nameof(IUniverseObject)} with {nameof(IBehaviour)} of type {typeof(T).FullName}");
public static T FindRequired<T>(this IUniverse universe) where T : class
=> universe.Find<T>() ?? throw new NotFoundException($"{universe.GetType().FullName}({universe.Id}) does not contain any {nameof(IUniverseObject)} or {nameof(IBehaviour)} of type {typeof(T).FullName}");
}

View File

@ -41,11 +41,9 @@ public static class UniverseObjectExtensions
if (universeObject is T @object)
foundUniverseObjects.Add(@object);
}
#endregion
#region Universe Object Search In Parent
/// <summary>
/// Tries to get a <see cref="IUniverseObject"/> of the specified type in it's parents recursively.
/// </summary>
@ -163,4 +161,36 @@ public static class UniverseObjectExtensions
}
}
#endregion
#region General Search
public static T? Find<T>(this IEnumerable<IUniverseObject> universeObjects) where T : class
{
if (universeObjects.GetUniverseObject<T>() is T foundUniverseObject)
return foundUniverseObject;
if (universeObjects.FindBehaviour<T>() is T foundBehaviour)
return foundBehaviour;
return null;
}
public static bool TryFind<T>(this IEnumerable<IUniverseObject> universeObjects, [NotNullWhen(returnValue: true)] out T? behaviour) where T : class
{
behaviour = Find<T>(universeObjects);
return behaviour is not null;
}
public static void Find<T>(this IEnumerable<IUniverseObject> universeObjects, IList<T> behaviours) where T : class
{
behaviours.Clear();
List<T> cache = [];
foreach (IUniverseObject universeObject in universeObjects)
{
universeObject.Find(cache);
foreach (T behaviour in cache)
behaviours.Add(behaviour);
}
}
#endregion
}