From 7a99aea17d99dade71a94b60a879c634d9fbf777 Mon Sep 17 00:00:00 2001 From: Gustas Date: Tue, 26 Sep 2023 22:51:36 +0300 Subject: [PATCH] DistinctBy is mono incompatible --- OpenRA.Mods.CA/Assets/OffsetUtils.cs | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/OpenRA.Mods.CA/Assets/OffsetUtils.cs b/OpenRA.Mods.CA/Assets/OffsetUtils.cs index 87126706..148f74a2 100644 --- a/OpenRA.Mods.CA/Assets/OffsetUtils.cs +++ b/OpenRA.Mods.CA/Assets/OffsetUtils.cs @@ -11,7 +11,10 @@ #endregion +using System; +using System.Collections.Generic; using System.IO; +using System.Linq; using OpenRA.Primitives; namespace OpenRA.Mods.CA.Assets @@ -27,5 +30,38 @@ public static uint ReadUInt32Offset(Stream stream) return offset; } + +#if !NET5_0_OR_GREATER + public static IEnumerable DistinctBy(this IEnumerable source, Func keySelector) + { + if (source is null) + throw new ArgumentNullException(nameof(source)); + + if (keySelector is null) + throw new ArgumentNullException(nameof(keySelector)); + + return DistinctByIterator(source, keySelector); + } + + static IEnumerable DistinctByIterator(IEnumerable source, Func keySelector) + { + using (IEnumerator enumerator = source.GetEnumerator()) + { + if (enumerator.MoveNext()) + { + var set = new HashSet(); + do + { + TSource element = enumerator.Current; + if (set.Add(keySelector(element))) + { + yield return element; + } + } + while (enumerator.MoveNext()); + } + } + } +#endif } }