diff --git a/Engine.Core/Exceptions/BehaviourNotFoundException.cs b/Engine.Core/Exceptions/BehaviourNotFoundException.cs index 034d40e..36ca458 100644 --- a/Engine.Core/Exceptions/BehaviourNotFoundException.cs +++ b/Engine.Core/Exceptions/BehaviourNotFoundException.cs @@ -1,9 +1,3 @@ -using System; - namespace Syntriax.Engine.Core.Exceptions; -public class BehaviourNotFoundException(string? message) : Exception(message) -{ - public static NotAssignedException FromType() - => new($"{typeof(TBehaviour).FullName} was not found"); -} +public class BehaviourNotFoundException(string? message) : NotFoundException(message); diff --git a/Engine.Core/Exceptions/NotFoundException.cs b/Engine.Core/Exceptions/NotFoundException.cs new file mode 100644 index 0000000..b6b0d69 --- /dev/null +++ b/Engine.Core/Exceptions/NotFoundException.cs @@ -0,0 +1,9 @@ +using System; + +namespace Syntriax.Engine.Core.Exceptions; + +public class NotFoundException(string? message) : Exception(message) +{ + public static NotAssignedException FromType() + => new($"{typeof(T).FullName} was not found"); +} diff --git a/Engine.Core/Exceptions/UniverseObjectNotFoundException.cs b/Engine.Core/Exceptions/UniverseObjectNotFoundException.cs index b49d848..9b68b4d 100644 --- a/Engine.Core/Exceptions/UniverseObjectNotFoundException.cs +++ b/Engine.Core/Exceptions/UniverseObjectNotFoundException.cs @@ -1,9 +1,3 @@ -using System; - namespace Syntriax.Engine.Core.Exceptions; -public class UniverseObjectNotFoundException(string? message) : Exception(message) -{ - public static NotAssignedException FromType() - => new($"{typeof(TUniverseObject).FullName} was not found"); -} +public class UniverseObjectNotFoundException(string? message) : NotFoundException(message); diff --git a/Engine.Core/Extensions/UniverseExtensions.cs b/Engine.Core/Extensions/UniverseExtensions.cs index 4825fd7..d54c811 100644 --- a/Engine.Core/Extensions/UniverseExtensions.cs +++ b/Engine.Core/Extensions/UniverseExtensions.cs @@ -12,4 +12,7 @@ public static class UniverseExtensions public static T FindRequiredBehaviour(this IUniverse universe) where T : class => universe.FindBehaviour() ?? 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(this IUniverse universe) where T : class + => universe.Find() ?? throw new NotFoundException($"{universe.GetType().FullName}({universe.Id}) does not contain any {nameof(IUniverseObject)} or {nameof(IBehaviour)} of type {typeof(T).FullName}"); } diff --git a/Engine.Core/Extensions/UniverseObjectExtensions.cs b/Engine.Core/Extensions/UniverseObjectExtensions.cs index a47f4eb..e836195 100644 --- a/Engine.Core/Extensions/UniverseObjectExtensions.cs +++ b/Engine.Core/Extensions/UniverseObjectExtensions.cs @@ -41,11 +41,9 @@ public static class UniverseObjectExtensions if (universeObject is T @object) foundUniverseObjects.Add(@object); } - #endregion #region Universe Object Search In Parent - /// /// Tries to get a of the specified type in it's parents recursively. /// @@ -163,4 +161,36 @@ public static class UniverseObjectExtensions } } #endregion + + #region General Search + public static T? Find(this IEnumerable universeObjects) where T : class + { + if (universeObjects.GetUniverseObject() is T foundUniverseObject) + return foundUniverseObject; + + if (universeObjects.FindBehaviour() is T foundBehaviour) + return foundBehaviour; + + return null; + } + + public static bool TryFind(this IEnumerable universeObjects, [NotNullWhen(returnValue: true)] out T? behaviour) where T : class + { + behaviour = Find(universeObjects); + return behaviour is not null; + } + + public static void Find(this IEnumerable universeObjects, IList behaviours) where T : class + { + behaviours.Clear(); + List cache = []; + + foreach (IUniverseObject universeObject in universeObjects) + { + universeObject.Find(cache); + foreach (T behaviour in cache) + behaviours.Add(behaviour); + } + } + #endregion }