mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-11-19 13:17:10 +00:00
new comparer util + minor optimisations
This commit is contained in:
7 files changed
+286
-14
No files matched your search
@@ -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;
|
||||
|
||||
@@ -39,6 +39,7 @@ public static class ModdedOptionsManager
|
||||
internal static readonly Dictionary<Type, List<AbstractOptionGroup>> GameModeOptionGroups = [];
|
||||
internal static readonly Dictionary<uint, IModdedOption> ModdedOptions = [];
|
||||
internal static readonly List<AbstractOptionGroup> Groups = [];
|
||||
internal static readonly Comparer<AbstractOptionGroup> Comparer = Comparer<AbstractOptionGroup>.Create((x, y) => x.GroupPriority.CompareTo(y.GroupPriority));
|
||||
|
||||
internal static uint NextId => _nextId++;
|
||||
private static uint _nextId = 1;
|
||||
|
||||
@@ -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<NetworkedPlayerInfo> Comparer = Comparer<NetworkedPlayerInfo>.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();
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace MiraAPI.Utilities;
|
||||
|
||||
/// <summary>
|
||||
/// Defines a method that a type implements to compare two objects using extra contextual information.
|
||||
/// </summary>
|
||||
/// <typeparam name="TValue">The type of objects to compare.</typeparam>
|
||||
/// <typeparam name="TParam">The type of the context.</typeparam>
|
||||
public interface IComparer<in TValue, in TParam>
|
||||
{
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="x">The first object to compare.</param>
|
||||
/// <param name="y">The second object to compare.</param>
|
||||
/// <param name="context">The extra information to aid in the comparison.</param>
|
||||
/// <returns>
|
||||
/// A signed integer that indicates the relative values of x and y, as shown in the
|
||||
/// following table.<br/><br/>
|
||||
/// Value – Meaning<br/>
|
||||
/// Less than zero – x is less than y.<br/>
|
||||
/// Zero – x equals y.<br/>
|
||||
/// Greater than zero – x is greater than y.<br/>
|
||||
/// </returns>
|
||||
int Compare(TValue? x, TValue? y, TParam? context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides a base class for implementations of the <see cref="IComparer{TValue, TParam}"/>
|
||||
/// generic interface.
|
||||
/// </summary>
|
||||
/// <typeparam name="TValue">The type of objects to compare.</typeparam>
|
||||
/// <typeparam name="TParam">The type of the context.</typeparam>
|
||||
[SuppressMessage("Design", "CA1000:Do not declare static members on generic types", Justification = "Mimics Comparer<T> design.")]
|
||||
public abstract class Comparer<TValue, TParam> : IComparer<TValue, TParam>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a default contextual comparer that ignores the parameter and falls back to standard comparison.
|
||||
/// </summary>
|
||||
public static Comparer<TValue, TParam> Default { get; } = new DefaultComparer();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a contextual comparer by using the specified comparison delegate.
|
||||
/// </summary>
|
||||
/// <param name="comparison">The comparison to use.</param>
|
||||
/// <returns>The new comparer.</returns>
|
||||
public static Comparer<TValue, TParam> Create(Func<TValue?, TValue?, TParam?, int>? comparison)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(comparison);
|
||||
return new FuncComparer(comparison);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public abstract int Compare(TValue? x, TValue? y, TParam? context);
|
||||
|
||||
private sealed class DefaultComparer : Comparer<TValue, TParam>
|
||||
{
|
||||
public override int Compare(TValue? x, TValue? y, TParam? context)
|
||||
{
|
||||
return Comparer<TValue>.Default.Compare(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class FuncComparer(Func<TValue?, TValue?, TParam?, int> comparison) : Comparer<TValue, TParam>
|
||||
{
|
||||
public override int Compare(TValue? x, TValue? y, TParam? context)
|
||||
{
|
||||
return comparison(x, y, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace MiraAPI.Utilities;
|
||||
|
||||
/// <summary>
|
||||
/// Provides extension methods to integrate <see cref="IComparer{TValue, TParam}"/> with standard .NET collections and LINQ.
|
||||
/// </summary>
|
||||
public static class ContextualComparerExtensions
|
||||
{
|
||||
private readonly struct BoundComparer<T, TParam>(IComparer<T, TParam> comparer, TParam? context) : IComparer<T>
|
||||
{
|
||||
public int Compare(T? x, T? y)
|
||||
{
|
||||
return comparer.Compare(x, y, context);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Binds a context parameter to a contextual comparer, returning a standard <see cref="IComparer{T}"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of objects to compare.</typeparam>
|
||||
/// <typeparam name="TParam">The type of the context.</typeparam>
|
||||
/// <param name="comparer">The contextual comparer.</param>
|
||||
/// <param name="context">The context to bind to the comparer.</param>
|
||||
/// <returns>A standard comparer that implicitly uses the provided context.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="comparer"/> is null.</exception>
|
||||
public static IComparer<T> Bind<T, TParam>(this IComparer<T, TParam> comparer, TParam? context)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(comparer);
|
||||
return new BoundComparer<T, TParam>(comparer, context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sorts the elements in the entire <see cref="List{T}"/> using the specified contextual comparer and context.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of elements in the list.</typeparam>
|
||||
/// <typeparam name="TParam">The type of the context.</typeparam>
|
||||
/// <param name="list">The list to sort.</param>
|
||||
/// <param name="comparer">The contextual comparer to use when comparing elements.</param>
|
||||
/// <param name="context">The context to aid in the comparison.</param>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="list"/> or <paramref name="comparer"/> is null.</exception>
|
||||
public static void Sort<T, TParam>(this List<T> list, IComparer<T, TParam> comparer, TParam? context)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(list);
|
||||
list.Sort(comparer.Bind(context));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sorts the elements in an entire one-dimensional array using the specified contextual comparer and context.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of elements in the array.</typeparam>
|
||||
/// <typeparam name="TParam">The type of the context.</typeparam>
|
||||
/// <param name="array">The one-dimensional array to sort.</param>
|
||||
/// <param name="comparer">The contextual comparer to use when comparing elements.</param>
|
||||
/// <param name="context">The context to aid in the comparison.</param>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="array"/> or <paramref name="comparer"/> is null.</exception>
|
||||
public static void Sort<T, TParam>(this T[] array, IComparer<T, TParam> comparer, TParam? context)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(array);
|
||||
Array.Sort(array, comparer.Bind(context));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Searches the entire sorted <see cref="List{T}"/> for an element using the specified contextual comparer and context,
|
||||
/// and returns the zero-based index of the element.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of elements in the list.</typeparam>
|
||||
/// <typeparam name="TParam">The type of the context.</typeparam>
|
||||
/// <param name="list">The sorted list to search.</param>
|
||||
/// <param name="item">The object to locate. The value can be null for reference types.</param>
|
||||
/// <param name="comparer">The contextual comparer to use when comparing elements.</param>
|
||||
/// <param name="context">The context to aid in the comparison.</param>
|
||||
/// <returns>
|
||||
/// 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.
|
||||
/// </returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="list"/> or <paramref name="comparer"/> is null.</exception>
|
||||
public static int BinarySearch<T, TParam>(this List<T> list, T item, IComparer<T, TParam> comparer, TParam? context)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(list);
|
||||
return list.BinarySearch(item, comparer.Bind(context));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Searches an entire one-dimensional sorted array for a specific element using the specified contextual comparer and context.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of elements in the array.</typeparam>
|
||||
/// <typeparam name="TParam">The type of the context.</typeparam>
|
||||
/// <param name="array">The sorted one-dimensional array to search.</param>
|
||||
/// <param name="item">The object to locate. The value can be null for reference types.</param>
|
||||
/// <param name="comparer">The contextual comparer to use when comparing elements.</param>
|
||||
/// <param name="context">The context to aid in the comparison.</param>
|
||||
/// <returns>
|
||||
/// The index of the specified value in the specified array, if value is found; otherwise, a negative number.
|
||||
/// </returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="array"/> or <paramref name="comparer"/> is null.</exception>
|
||||
public static int BinarySearch<T, TParam>(this T[] array, T item, IComparer<T, TParam> comparer, TParam? context)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(array);
|
||||
return Array.BinarySearch(array, item, comparer.Bind(context));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sorts the elements of a sequence in ascending order according to a key and a contextual comparer.
|
||||
/// </summary>
|
||||
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
|
||||
/// <typeparam name="TParam">The type of the context.</typeparam>
|
||||
/// <param name="source">A sequence of values to order.</param>
|
||||
/// <param name="keySelector">A function to extract a key from an element.</param>
|
||||
/// <param name="comparer">The contextual comparer to compare keys.</param>
|
||||
/// <param name="context">The context to aid in the comparison.</param>
|
||||
/// <returns>An <see cref="IOrderedEnumerable{TElement}"/> whose elements are sorted according to a key.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="source"/>, <paramref name="keySelector"/>, or <paramref name="comparer"/> is null.</exception>
|
||||
public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey, TParam>(
|
||||
this IEnumerable<TSource> source,
|
||||
Func<TSource, TKey> keySelector,
|
||||
IComparer<TKey, TParam> comparer,
|
||||
TParam? context)
|
||||
{
|
||||
return source.OrderBy(keySelector, comparer.Bind(context));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sorts the elements of a sequence in descending order according to a key and a contextual comparer.
|
||||
/// </summary>
|
||||
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
|
||||
/// <typeparam name="TParam">The type of the context.</typeparam>
|
||||
/// <param name="source">A sequence of values to order.</param>
|
||||
/// <param name="keySelector">A function to extract a key from an element.</param>
|
||||
/// <param name="comparer">The contextual comparer to compare keys.</param>
|
||||
/// <param name="context">The context to aid in the comparison.</param>
|
||||
/// <returns>An <see cref="IOrderedEnumerable{TElement}"/> whose elements are sorted in descending order according to a key.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="source"/>, <paramref name="keySelector"/>, or <paramref name="comparer"/> is null.</exception>
|
||||
public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey, TParam>(
|
||||
this IEnumerable<TSource> source,
|
||||
Func<TSource, TKey> keySelector,
|
||||
IComparer<TKey, TParam> comparer,
|
||||
TParam? context)
|
||||
{
|
||||
return source.OrderByDescending(keySelector, comparer.Bind(context));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs a subsequent ordering of the elements in a sequence in ascending order according to a key and a contextual comparer.
|
||||
/// </summary>
|
||||
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
|
||||
/// <typeparam name="TParam">The type of the context.</typeparam>
|
||||
/// <param name="source">An <see cref="IOrderedEnumerable{TElement}"/> that contains elements to sort.</param>
|
||||
/// <param name="keySelector">A function to extract a key from an element.</param>
|
||||
/// <param name="comparer">The contextual comparer to compare keys.</param>
|
||||
/// <param name="context">The context to aid in the comparison.</param>
|
||||
/// <returns>An <see cref="IOrderedEnumerable{TElement}"/> whose elements are sorted according to a key.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="source"/>, <paramref name="keySelector"/>, or <paramref name="comparer"/> is null.</exception>
|
||||
public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey, TParam>(
|
||||
this IOrderedEnumerable<TSource> source,
|
||||
Func<TSource, TKey> keySelector,
|
||||
IComparer<TKey, TParam> comparer,
|
||||
TParam? context)
|
||||
{
|
||||
return source.ThenBy(keySelector, comparer.Bind(context));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs a subsequent ordering of the elements in a sequence in descending order, according to a key and a contextual comparer.
|
||||
/// </summary>
|
||||
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
|
||||
/// <typeparam name="TParam">The type of the context.</typeparam>
|
||||
/// <param name="source">An <see cref="IOrderedEnumerable{TElement}"/> that contains elements to sort.</param>
|
||||
/// <param name="keySelector">A function to extract a key from an element.</param>
|
||||
/// <param name="comparer">The contextual comparer to compare keys.</param>
|
||||
/// <param name="context">The context to aid in the comparison.</param>
|
||||
/// <returns>An <see cref="IOrderedEnumerable{TElement}"/> whose elements are sorted in descending order according to a key.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="source"/>, <paramref name="keySelector"/>, or <paramref name="comparer"/> is null.</exception>
|
||||
public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey, TParam>(
|
||||
this IOrderedEnumerable<TSource> source,
|
||||
Func<TSource, TKey> keySelector,
|
||||
IComparer<TKey, TParam> comparer,
|
||||
TParam? context)
|
||||
{
|
||||
return source.ThenByDescending(keySelector, comparer.Bind(context));
|
||||
}
|
||||
}
|
||||
@@ -504,6 +504,17 @@ public static class Helpers
|
||||
return ignoreSource ? [.. players.Where(plr => plr.PlayerId != source.PlayerId)] : players;
|
||||
}
|
||||
|
||||
private static readonly Comparer<PlayerControl, Vector2> PlayerPosComparer = Comparer<PlayerControl, Vector2>.Create((a, b, source) =>
|
||||
{
|
||||
var magnitude2 = (a!.GetTruePosition() - source).sqrMagnitude;
|
||||
var magnitude3 = (b!.GetTruePosition() - source).sqrMagnitude;
|
||||
return magnitude2 > magnitude3
|
||||
? 1
|
||||
: magnitude2 < magnitude3
|
||||
? -1
|
||||
: 0;
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// Gets the closest <see cref="PlayerControl"/>s to a specific point.
|
||||
/// </summary>
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user