From 8c763e2a57e58ca915b89a8db5db1faa9df56654 Mon Sep 17 00:00:00 2001 From: XtraCube <72575280+XtraCube@users.noreply.github.com> Date: Tue, 1 Apr 2025 21:22:34 -0400 Subject: [PATCH] fix mira event priorities --- MiraAPI/Events/MiraEventManager.cs | 46 ++++++++++++---------- MiraAPI/Events/MiraEventWrapper.cs | 2 +- MiraAPI/Events/RegisterEventAttribute.cs | 4 +- MiraAPI/PluginLoading/MiraPluginManager.cs | 1 - 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/MiraAPI/Events/MiraEventManager.cs b/MiraAPI/Events/MiraEventManager.cs index b375504..52df8dd 100644 --- a/MiraAPI/Events/MiraEventManager.cs +++ b/MiraAPI/Events/MiraEventManager.cs @@ -21,7 +21,7 @@ public static class MiraEventManager public static bool InvokeEvent(T eventInstance) where T : MiraEvent { EventWrappers.TryGetValue(typeof(T), out var handlers); - if (handlers == null) + if (handlers == null || handlers.Count == 0) { return false; } @@ -43,7 +43,7 @@ public static class MiraEventManager public static bool InvokeEvent(MiraEvent eventInstance, Type type) { EventWrappers.TryGetValue(type, out var handlers); - if (handlers == null) + if (handlers == null || handlers.Count == 0) { return false; } @@ -61,47 +61,51 @@ public static class MiraEventManager /// /// The type of event. /// The MethodInfo of the event handler. - /// The priority of the event handler. Higher values are called first. + /// The priority of the event handler. Lower values are called first. public static void RegisterEventHandler(Type type, MethodInfo methodInfo, int priority = 0) { if (!type.IsSubclassOf(typeof(MiraEvent))) { - Logger.Error($"Type must be a subclass of MiraEvent: {nameof(type)}"); + Logger.Error($"Type must be a subclass of MiraEvent: {type.FullName}"); return; } - if (!EventWrappers.TryGetValue(type, out var handlers)) - { - handlers = []; - EventWrappers.Add(type, handlers); - } + EventWrappers.TryAdd(type, []); + var handlers = EventWrappers[type]; var @delegate = Delegate.CreateDelegate(typeof(Action<>).MakeGenericType(type), methodInfo); - handlers.Add(new MiraEventWrapper(@delegate, priority)); + var eventWrapper = new MiraEventWrapper(@delegate, priority); + + var index = handlers.BinarySearch(eventWrapper, Comparer.Create((a, b) => a.Priority.CompareTo(b.Priority))); + + if (index < 0) + { + index = ~index; + } + + handlers.Insert(index, eventWrapper); } /// /// Register an event. /// /// The callback method/handler for the event. - /// The priority of the event handler. Higher values are called first. + /// The priority of the event handler. Lower values are called first. /// Type of event. public static void RegisterEventHandler(Action handler, int priority = 0) where T : MiraEvent { - if (!EventWrappers.ContainsKey(typeof(T))) - { - EventWrappers.Add(typeof(T), []); - } + EventWrappers.TryAdd(typeof(T), []); var handlers = EventWrappers[typeof(T)]; - handlers.Add(new MiraEventWrapper(handler, priority)); - } + var eventWrapper = new MiraEventWrapper(handler, priority); - internal static void SortAllHandlers() - { - foreach (var handlers in EventWrappers.Values) + var index = handlers.BinarySearch(eventWrapper, Comparer.Create((a, b) => a.Priority.CompareTo(b.Priority))); + + if (index < 0) { - handlers.Sort((a, b) => a.Priority.CompareTo(b.Priority)); + index = ~index; } + + handlers.Insert(index, eventWrapper); } } diff --git a/MiraAPI/Events/MiraEventWrapper.cs b/MiraAPI/Events/MiraEventWrapper.cs index 0251ea9..7e754c3 100644 --- a/MiraAPI/Events/MiraEventWrapper.cs +++ b/MiraAPI/Events/MiraEventWrapper.cs @@ -13,7 +13,7 @@ public class MiraEventWrapper public Delegate EventHandler { get; } /// - /// Gets the priority of the handler. Higher values are called first. + /// Gets the priority of the handler. Lower values are called first. /// public int Priority { get; } diff --git a/MiraAPI/Events/RegisterEventAttribute.cs b/MiraAPI/Events/RegisterEventAttribute.cs index 6988d79..169c125 100644 --- a/MiraAPI/Events/RegisterEventAttribute.cs +++ b/MiraAPI/Events/RegisterEventAttribute.cs @@ -9,14 +9,14 @@ namespace MiraAPI.Events; public class RegisterEventAttribute : Attribute { /// - /// Gets the priority of the event. + /// Gets the priority of the event. Lower values are called first. /// public int Priority { get; } /// /// Initializes a new instance of the class. /// - /// The priority of the event. + /// The priority of the event. Lower values are called first. public RegisterEventAttribute(int priority = 0) { Priority = priority; diff --git a/MiraAPI/PluginLoading/MiraPluginManager.cs b/MiraAPI/PluginLoading/MiraPluginManager.cs index a40ee75..12ff874 100644 --- a/MiraAPI/PluginLoading/MiraPluginManager.cs +++ b/MiraAPI/PluginLoading/MiraPluginManager.cs @@ -109,7 +109,6 @@ public sealed class MiraPluginManager Logger.Info($"Registering mod {pluginInfo.Metadata.GUID} with Mira API."); }; IL2CPPChainloader.Instance.Finished += PaletteManager.RegisterAllColors; - IL2CPPChainloader.Instance.Finished += MiraEventManager.SortAllHandlers; IL2CPPChainloader.Instance.Finished += () => { CustomButtonManager.Buttons = new ReadOnlyCollection(CustomButtonManager.CustomButtons);