From 5011b32a5fdb6f88845c638d8cda9ce7bbaaae1e Mon Sep 17 00:00:00 2001 From: Arne Kiesewetter Date: Mon, 7 Oct 2024 21:52:29 +0200 Subject: [PATCH 1/2] Add (Try)FindNearestParent extension methods for identifiables --- MonkeyLoader/IdentifiableExtensions.cs | 89 ++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/MonkeyLoader/IdentifiableExtensions.cs b/MonkeyLoader/IdentifiableExtensions.cs index cbe400b..b730259 100644 --- a/MonkeyLoader/IdentifiableExtensions.cs +++ b/MonkeyLoader/IdentifiableExtensions.cs @@ -1,14 +1,52 @@ using MonkeyLoader.Meta; using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MonkeyLoader { + /// + /// Contains extension methods dealing with s. + /// public static class IdentifiableExtensions { + /// + /// Finds the nearest parent of this + /// that satisfies the given . + /// + /// The type of the parent to look for. + /// The to start looking from. + /// The predicate that a potential parent must satisfy. + /// The parent satisfying the . + /// When no suitable parent was found. + public static TParentIdentifiable FindNearestParent(this IIdentifiable identifiable, Predicate predicate) + where TParentIdentifiable : IIdentifiable + { + if (!identifiable.TryFindNearestParent(predicate, out var parentIdentifiable)) + throw new InvalidOperationException("No suitable parent found!"); + + return parentIdentifiable; + } + + /// + /// Finds the nearest parent of this . + /// + /// The type of the parent to look for. + /// The to start looking from. + /// The found parent. + /// When no suitable parent was found. + public static TParentIdentifiable FindNearestParent(this IIdentifiable identifiable) + where TParentIdentifiable : IIdentifiable + { + if (!identifiable.TryFindNearestParent(out TParentIdentifiable? parentIdentifiable)) + throw new InvalidOperationException("No suitable parent found!"); + + return parentIdentifiable; + } + public static IIdentifiableSearch Get(this IIdentifiableCollection identifiableCollection) where TIdentifiable : IIdentifiable => new IdentifiableSearch(identifiableCollection.Items); @@ -34,6 +72,57 @@ public static IEnumerable GetAll(this INestedIdent where TIdentifiable : INestedIdentifiable => nestedIdentifiableCollection.Items; + /// + /// Tries to find the nearest parent of this + /// that satisfies the given . + /// + /// The type of the parent to look for. + /// The to start looking from. + /// The predicate that a potential parent must satisfy. + /// The parent satisfying the if found; otherwise, default. + /// true if a parent satisfying the was found; otherwise, false. + public static bool TryFindNearestParent(this IIdentifiable identifiable, + Predicate predicate, [NotNullWhen(true)] out TParentIdentifiable? parentIdentifiable) + where TParentIdentifiable : IIdentifiable + { + while (identifiable.TryFindNearestParent(out parentIdentifiable)) + { + if (predicate(parentIdentifiable)) + return true; + + if (parentIdentifiable is not INestedIdentifiable) + break; + + identifiable = (INestedIdentifiable)parentIdentifiable; + } + + parentIdentifiable = default; + return false; + } + + /// + /// Tries to find the nearest parent of this . + /// + /// The type of the parent to look for. + /// The to start looking from. + /// The parent if found; otherwise, default. + /// true if a parent was found; otherwise, false. + public static bool TryFindNearestParent(this IIdentifiable identifiable, [NotNullWhen(true)] out TParentIdentifiable? parentIdentifiable) + where TParentIdentifiable : IIdentifiable + { + while (identifiable is not TParentIdentifiable and INestedIdentifiable nestedCurrent) + identifiable = nestedCurrent.Parent; + + if (identifiable is TParentIdentifiable parent) + { + parentIdentifiable = parent; + return true; + } + + parentIdentifiable = default; + return false; + } + public static INestedIdentifiableOwnerTrySearch TryGet(this INestedIdentifiableOwner nestedIdentifiableOwner) where TIdentifiable : INestedIdentifiable => new NestedIdentifiableSearch(nestedIdentifiableOwner.Items, nestedIdentifiableOwner.FullId); From 7a0a78111f0e3682bfe870205121434fb9efdc25 Mon Sep 17 00:00:00 2001 From: Arne Kiesewetter Date: Fri, 22 Nov 2024 19:53:39 +0100 Subject: [PATCH 2/2] Add documentation to the other IdentifiableExtensions methods --- MonkeyLoader/IdentifiableExtensions.cs | 38 ++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/MonkeyLoader/IdentifiableExtensions.cs b/MonkeyLoader/IdentifiableExtensions.cs index b730259..5a15f6e 100644 --- a/MonkeyLoader/IdentifiableExtensions.cs +++ b/MonkeyLoader/IdentifiableExtensions.cs @@ -47,31 +47,57 @@ public static TParentIdentifiable FindNearestParent(this II return parentIdentifiable; } + /// The identifiable collection to start from. + /// public static IIdentifiableSearch Get(this IIdentifiableCollection identifiableCollection) where TIdentifiable : IIdentifiable => new IdentifiableSearch(identifiableCollection.Items); + /// The nested identifiable collection to start from. + /// public static INestedIdentifiableSearch Get(this INestedIdentifiableCollection nestedIdentifiableCollection) where TIdentifiable : INestedIdentifiable => new NestedIdentifiableSearch(nestedIdentifiableCollection.Items); + /// + /// Starts a search for a child from this object. + /// + /// The type of the identifiable owner to start from. + /// The type of the nested to find. + /// The identifiable owner to start from. + /// The search object for it. public static IIdentifiableOwnerSearch Get(this IIdentifiableOwner identifiableOwner) where TOwner : IIdentifiableOwner where TIdentifiable : INestedIdentifiable => new NestedIdentifiableSearch(identifiableOwner.Items, identifiableOwner.FullId); + /// public static IIdentifiableOwnerSearch Get(this IIdentifiableOwner identifiableOwner) where TIdentifiable : INestedIdentifiable => new NestedIdentifiableSearch(identifiableOwner.Items, identifiableOwner.FullId); + /// The nested identifiable owner to start from. + /// public static INestedIdentifiableOwnerSearch Get(this INestedIdentifiableOwner nestedIdentifiableOwner) where TIdentifiable : INestedIdentifiable => new NestedIdentifiableSearch(nestedIdentifiableOwner.Items, nestedIdentifiableOwner.FullId); + /// The nested identifiable collection to start from. + /// public static IEnumerable GetAll(this INestedIdentifiableCollection nestedIdentifiableCollection) where TIdentifiable : INestedIdentifiable => nestedIdentifiableCollection.Items; + /// + /// Gets an enumerable of all child from this object. + /// + /// The type of the nested s to enumerate. + /// The identifiable collection to start from. + /// An enumerable of all child . + public static IEnumerable GetAll(this IIdentifiableCollection identifiableCollection) + where TIdentifiable : IIdentifiable + => identifiableCollection.Items; + /// /// Tries to find the nearest parent of this /// that satisfies the given . @@ -123,23 +149,35 @@ public static bool TryFindNearestParent(this IIdentifiable return false; } + /// The nested identifiable owner to start from. + /// public static INestedIdentifiableOwnerTrySearch TryGet(this INestedIdentifiableOwner nestedIdentifiableOwner) where TIdentifiable : INestedIdentifiable => new NestedIdentifiableSearch(nestedIdentifiableOwner.Items, nestedIdentifiableOwner.FullId); + /// + /// Starts a try-search for a child from this object. + /// + /// The try-search object for it. + /// public static IIdentifiableOwnerTrySearch TryGet(this IIdentifiableOwner identifiableOwner) where TOwner : IIdentifiableOwner where TIdentifiable : INestedIdentifiable => new NestedIdentifiableSearch(identifiableOwner.Items, identifiableOwner.FullId); + /// public static IIdentifiableOwnerTrySearch TryGet(this IIdentifiableOwner identifiableOwner) where TIdentifiable : INestedIdentifiable => new NestedIdentifiableSearch(identifiableOwner.Items, identifiableOwner.FullId); + /// The identifiable collection to start from. + /// public static IIdentifiableTrySearch TryGet(this IIdentifiableCollection identifiableCollection) where TIdentifiable : IIdentifiable => new IdentifiableSearch(identifiableCollection.Items); + /// The nested identifiable collection to start from. + /// public static INestedIdentifiableTrySearch TryGet(this INestedIdentifiableCollection nestedIdentifiableCollection) where TIdentifiable : INestedIdentifiable => new NestedIdentifiableSearch(nestedIdentifiableCollection.Items);