diff --git a/MiraAPI.Example/Roles/ChameleonRole.cs b/MiraAPI.Example/Roles/ChameleonRole.cs index e6e3adf9..8d8151cb 100644 --- a/MiraAPI.Example/Roles/ChameleonRole.cs +++ b/MiraAPI.Example/Roles/ChameleonRole.cs @@ -40,7 +40,7 @@ public class ChameleonRole : CrewmateRole, ICustomRole return; } - if (Player.MyPhysics.Velocity.magnitude > 0) + if (Player.MyPhysics.Velocity.sqrMagnitude > 0) { var rend = Player.cosmetics.currentBodySprite.BodySprite; var tmp = Player.cosmetics.nameText; diff --git a/MiraAPI/GameOptions/ModdedOptionsManager.cs b/MiraAPI/GameOptions/ModdedOptionsManager.cs index 07d21c28..76c0a0e1 100644 --- a/MiraAPI/GameOptions/ModdedOptionsManager.cs +++ b/MiraAPI/GameOptions/ModdedOptionsManager.cs @@ -39,6 +39,7 @@ public static class ModdedOptionsManager internal static readonly Dictionary> GameModeOptionGroups = []; internal static readonly Dictionary ModdedOptions = []; internal static readonly List Groups = []; + internal static readonly Comparer Comparer = Comparer.Create((x, y) => x.GroupPriority.CompareTo(y.GroupPriority)); internal static uint NextId => _nextId++; private static uint _nextId = 1; diff --git a/MiraAPI/Patches/Options/PlayerOptionPatches.cs b/MiraAPI/Patches/Options/PlayerOptionPatches.cs index ab19fced..596e73aa 100644 --- a/MiraAPI/Patches/Options/PlayerOptionPatches.cs +++ b/MiraAPI/Patches/Options/PlayerOptionPatches.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System.Collections.Generic; +using System.Linq; using HarmonyLib; using MiraAPI.GameOptions; using MiraAPI.Utilities; @@ -9,6 +10,8 @@ namespace MiraAPI.Patches.Options; [HarmonyPatch(typeof(PlayerOption))] public static class PlayerOptionPatches { + private static readonly Comparer Comparer = Comparer.Create((a, b) => a.PlayerId.CompareTo(b.PlayerId)); + // Updates the list of players according to the filter. [HarmonyPrefix] [HarmonyPatch(nameof(PlayerOption.FixedUpdate))] @@ -24,7 +27,7 @@ public static class PlayerOptionPatches var filteredPlayers = moddedPlrOpt.GetFilteredPlayers(); if (filteredPlayers.Count != __instance.Values.Count) { - filteredPlayers.Sort((a, b) => a.PlayerId.CompareTo(b.PlayerId)); + filteredPlayers.Sort(Comparer); moddedPlrOpt.Values = filteredPlayers; __instance.Values = filteredPlayers.ToIl2CppList(); diff --git a/MiraAPI/PluginLoading/MiraPluginManager.cs b/MiraAPI/PluginLoading/MiraPluginManager.cs index bebdb109..5fcf4c03 100644 --- a/MiraAPI/PluginLoading/MiraPluginManager.cs +++ b/MiraAPI/PluginLoading/MiraPluginManager.cs @@ -135,7 +135,8 @@ public sealed class MiraPluginManager info.PluginConfig.Save(); info.PluginConfig.SaveOnConfigSet = oldConfigSetting; - info.InternalOptionGroups.Sort((x, y) => x.GroupPriority.CompareTo(y.GroupPriority)); + info.InternalOptionGroups.Sort(ModdedOptionsManager.Comparer); + QueuedRoleRegistrations.Add(info, roles); _registeredPlugins.Add(pluginInfo.Metadata.GUID, info); diff --git a/MiraAPI/Utilities/ContextualComparer.cs b/MiraAPI/Utilities/ContextualComparer.cs new file mode 100644 index 00000000..f6cf52b8 --- /dev/null +++ b/MiraAPI/Utilities/ContextualComparer.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; + +namespace MiraAPI.Utilities; + +/// +/// Defines a method that a type implements to compare two objects using extra contextual information. +/// +/// The type of objects to compare. +/// The type of the context. +public interface IComparer +{ + /// + /// When overridden in a derived class, performs a comparison of two objects of the + /// same type using the provided context and returns a value indicating whether one + /// object is less than, equal or greater than the other. + /// + /// The first object to compare. + /// The second object to compare. + /// The extra information to aid in the comparison. + /// + /// A signed integer that indicates the relative values of x and y, as shown in the + /// following table.

+ /// Value – Meaning
+ /// Less than zero – x is less than y.
+ /// Zero – x equals y.
+ /// Greater than zero – x is greater than y.
+ ///
+ int Compare(TValue? x, TValue? y, TParam? context); +} + +/// +/// Provides a base class for implementations of the +/// generic interface. +/// +/// The type of objects to compare. +/// The type of the context. +[SuppressMessage("Design", "CA1000:Do not declare static members on generic types", Justification = "Mimics Comparer design.")] +public abstract class Comparer : IComparer +{ + /// + /// Gets a default contextual comparer that ignores the parameter and falls back to standard comparison. + /// + public static Comparer Default { get; } = new DefaultComparer(); + + /// + /// Creates a contextual comparer by using the specified comparison delegate. + /// + /// The comparison to use. + /// The new comparer. + public static Comparer Create(Func? comparison) + { + ArgumentNullException.ThrowIfNull(comparison); + return new FuncComparer(comparison); + } + + /// + public abstract int Compare(TValue? x, TValue? y, TParam? context); + + private sealed class DefaultComparer : Comparer + { + public override int Compare(TValue? x, TValue? y, TParam? context) + { + return Comparer.Default.Compare(x, y); + } + } + + private sealed class FuncComparer(Func comparison) : Comparer + { + public override int Compare(TValue? x, TValue? y, TParam? context) + { + return comparison(x, y, context); + } + } +} diff --git a/MiraAPI/Utilities/ContextualComparerExtensions.cs b/MiraAPI/Utilities/ContextualComparerExtensions.cs new file mode 100644 index 00000000..254c9140 --- /dev/null +++ b/MiraAPI/Utilities/ContextualComparerExtensions.cs @@ -0,0 +1,189 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace MiraAPI.Utilities; + +/// +/// Provides extension methods to integrate with standard .NET collections and LINQ. +/// +public static class ContextualComparerExtensions +{ + private readonly struct BoundComparer(IComparer comparer, TParam? context) : IComparer + { + public int Compare(T? x, T? y) + { + return comparer.Compare(x, y, context); + } + } + + /// + /// Binds a context parameter to a contextual comparer, returning a standard . + /// + /// The type of objects to compare. + /// The type of the context. + /// The contextual comparer. + /// The context to bind to the comparer. + /// A standard comparer that implicitly uses the provided context. + /// is null. + public static IComparer Bind(this IComparer comparer, TParam? context) + { + ArgumentNullException.ThrowIfNull(comparer); + return new BoundComparer(comparer, context); + } + + /// + /// Sorts the elements in the entire using the specified contextual comparer and context. + /// + /// The type of elements in the list. + /// The type of the context. + /// The list to sort. + /// The contextual comparer to use when comparing elements. + /// The context to aid in the comparison. + /// or is null. + public static void Sort(this List list, IComparer comparer, TParam? context) + { + ArgumentNullException.ThrowIfNull(list); + list.Sort(comparer.Bind(context)); + } + + /// + /// Sorts the elements in an entire one-dimensional array using the specified contextual comparer and context. + /// + /// The type of elements in the array. + /// The type of the context. + /// The one-dimensional array to sort. + /// The contextual comparer to use when comparing elements. + /// The context to aid in the comparison. + /// or is null. + public static void Sort(this T[] array, IComparer comparer, TParam? context) + { + ArgumentNullException.ThrowIfNull(array); + Array.Sort(array, comparer.Bind(context)); + } + + /// + /// Searches the entire sorted for an element using the specified contextual comparer and context, + /// and returns the zero-based index of the element. + /// + /// The type of elements in the list. + /// The type of the context. + /// The sorted list to search. + /// The object to locate. The value can be null for reference types. + /// The contextual comparer to use when comparing elements. + /// The context to aid in the comparison. + /// + /// The zero-based index of item in the sorted list, if item is found; otherwise, a negative number that is the + /// bitwise complement of the index of the next element that is larger than item or, if there is no larger element, + /// the bitwise complement of Count. + /// + /// or is null. + public static int BinarySearch(this List list, T item, IComparer comparer, TParam? context) + { + ArgumentNullException.ThrowIfNull(list); + return list.BinarySearch(item, comparer.Bind(context)); + } + + /// + /// Searches an entire one-dimensional sorted array for a specific element using the specified contextual comparer and context. + /// + /// The type of elements in the array. + /// The type of the context. + /// The sorted one-dimensional array to search. + /// The object to locate. The value can be null for reference types. + /// The contextual comparer to use when comparing elements. + /// The context to aid in the comparison. + /// + /// The index of the specified value in the specified array, if value is found; otherwise, a negative number. + /// + /// or is null. + public static int BinarySearch(this T[] array, T item, IComparer comparer, TParam? context) + { + ArgumentNullException.ThrowIfNull(array); + return Array.BinarySearch(array, item, comparer.Bind(context)); + } + + /// + /// Sorts the elements of a sequence in ascending order according to a key and a contextual comparer. + /// + /// The type of the elements of source. + /// The type of the key returned by keySelector. + /// The type of the context. + /// A sequence of values to order. + /// A function to extract a key from an element. + /// The contextual comparer to compare keys. + /// The context to aid in the comparison. + /// An whose elements are sorted according to a key. + /// , , or is null. + public static IOrderedEnumerable OrderBy( + this IEnumerable source, + Func keySelector, + IComparer comparer, + TParam? context) + { + return source.OrderBy(keySelector, comparer.Bind(context)); + } + + /// + /// Sorts the elements of a sequence in descending order according to a key and a contextual comparer. + /// + /// The type of the elements of source. + /// The type of the key returned by keySelector. + /// The type of the context. + /// A sequence of values to order. + /// A function to extract a key from an element. + /// The contextual comparer to compare keys. + /// The context to aid in the comparison. + /// An whose elements are sorted in descending order according to a key. + /// , , or is null. + public static IOrderedEnumerable OrderByDescending( + this IEnumerable source, + Func keySelector, + IComparer comparer, + TParam? context) + { + return source.OrderByDescending(keySelector, comparer.Bind(context)); + } + + /// + /// Performs a subsequent ordering of the elements in a sequence in ascending order according to a key and a contextual comparer. + /// + /// The type of the elements of source. + /// The type of the key returned by keySelector. + /// The type of the context. + /// An that contains elements to sort. + /// A function to extract a key from an element. + /// The contextual comparer to compare keys. + /// The context to aid in the comparison. + /// An whose elements are sorted according to a key. + /// , , or is null. + public static IOrderedEnumerable ThenBy( + this IOrderedEnumerable source, + Func keySelector, + IComparer comparer, + TParam? context) + { + return source.ThenBy(keySelector, comparer.Bind(context)); + } + + /// + /// Performs a subsequent ordering of the elements in a sequence in descending order, according to a key and a contextual comparer. + /// + /// The type of the elements of source. + /// The type of the key returned by keySelector. + /// The type of the context. + /// An that contains elements to sort. + /// A function to extract a key from an element. + /// The contextual comparer to compare keys. + /// The context to aid in the comparison. + /// An whose elements are sorted in descending order according to a key. + /// , , or is null. + public static IOrderedEnumerable ThenByDescending( + this IOrderedEnumerable source, + Func keySelector, + IComparer comparer, + TParam? context) + { + return source.ThenByDescending(keySelector, comparer.Bind(context)); + } +} diff --git a/MiraAPI/Utilities/Helpers.cs b/MiraAPI/Utilities/Helpers.cs index d08a4a00..f4e7ce33 100644 --- a/MiraAPI/Utilities/Helpers.cs +++ b/MiraAPI/Utilities/Helpers.cs @@ -504,6 +504,17 @@ public static class Helpers return ignoreSource ? [.. players.Where(plr => plr.PlayerId != source.PlayerId)] : players; } + private static readonly Comparer PlayerPosComparer = Comparer.Create((a, b, source) => + { + var magnitude2 = (a!.GetTruePosition() - source).sqrMagnitude; + var magnitude3 = (b!.GetTruePosition() - source).sqrMagnitude; + return magnitude2 > magnitude3 + ? 1 + : magnitude2 < magnitude3 + ? -1 + : 0; + }); + /// /// Gets the closest s to a specific point. /// @@ -537,16 +548,7 @@ public static class Helpers Constants.ShipAndObjectsMask)) select playerControl); - outputList.Sort((a, b) => - { - var magnitude2 = (a.GetTruePosition() - source).magnitude; - var magnitude3 = (b.GetTruePosition() - source).magnitude; - return magnitude2 > magnitude3 - ? 1 - : magnitude2 < magnitude3 - ? -1 - : 0; - }); + outputList.Sort(PlayerPosComparer, source); return outputList; }