From 8410d61ac58487312d1dd94eeed6355bee3311f3 Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Tue, 3 Nov 2026 14:37:16 -0700 Subject: [PATCH] Registered MiraEvent types on plugins to track subclasses for inheritance calls --- MiraAPI/Events/MiraEventManager.cs | 61 ++++++++++++++++++++++ MiraAPI/PluginLoading/MiraPluginInfo.cs | 11 +++- MiraAPI/PluginLoading/MiraPluginManager.cs | 17 ++++++ 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/MiraAPI/Events/MiraEventManager.cs b/MiraAPI/Events/MiraEventManager.cs index a8b34f16..82ad7978 100644 --- a/MiraAPI/Events/MiraEventManager.cs +++ b/MiraAPI/Events/MiraEventManager.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Reflection; +using MiraAPI.PluginLoading; namespace MiraAPI.Events; @@ -11,6 +13,17 @@ public static class MiraEventManager { private static readonly Dictionary> EventWrappers = []; + /// + /// Gets the list of all registered types. + /// + public static IReadOnlyList EventTypes { get; internal set; } = []; + + internal static List InternalEventTypes { get; } = []; + + private static readonly Dictionary> TrackedEventTypes = []; + + private static readonly HashSet StoredEventTypes = []; + /// /// Invoke a . /// @@ -72,6 +85,54 @@ public static class MiraEventManager return true; } + internal static bool RegisterEventType(Type eventType, MiraPluginInfo info) + { + if (!typeof(MiraEvent).IsAssignableFrom(eventType)) + { + return false; + } + + info.InternalEventTypes.Add(eventType); + InternalEventTypes.Add(eventType); + + if (!eventType.IsAbstract) + { + if (!TrackedEventTypes.TryGetValue(eventType, out var trackedEvents)) + { + trackedEvents = []; + TrackedEventTypes.Add(eventType, trackedEvents); + } + + trackedEvents.Add(eventType); + } + + foreach (var superType in StoredEventTypes.Where(x => x.IsAssignableFrom(eventType))) + { + if (!eventType.IsAbstract) + { + TrackedEventTypes[eventType].Add(superType); + } + } + + foreach (var subType in StoredEventTypes.Where(x => x.IsAssignableTo(eventType))) + { + if (!subType.IsAbstract) + { + if (!TrackedEventTypes.TryGetValue(subType, out var trackedSubEvents)) + { + trackedSubEvents = []; + TrackedEventTypes.Add(subType, trackedSubEvents); + } + + trackedSubEvents.Add(eventType); + } + } + + StoredEventTypes.Add(eventType); + + return true; + } + /// /// Register a handler. /// diff --git a/MiraAPI/PluginLoading/MiraPluginInfo.cs b/MiraAPI/PluginLoading/MiraPluginInfo.cs index da121e7a..b0d7e434 100644 --- a/MiraAPI/PluginLoading/MiraPluginInfo.cs +++ b/MiraAPI/PluginLoading/MiraPluginInfo.cs @@ -1,7 +1,9 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using BepInEx; using BepInEx.Configuration; +using MiraAPI.Events; using MiraAPI.GameModes; using MiraAPI.GameOptions; using MiraAPI.Hud; @@ -85,6 +87,11 @@ public class MiraPluginInfo /// public IReadOnlyCollection Presets { get; internal set; } = null!; + /// + /// Gets a of this 's types. + /// + public IReadOnlyCollection EventTypes { get; internal set; } = null!; + /// /// Gets a read only dictionary of Game Mode IDs and the AbstractGameMode object they are associated with. /// @@ -104,6 +111,7 @@ public class MiraPluginInfo Buttons = [.. InternalButtons]; MeetingButtons = [.. InternalMeetingButtons]; TargetedMeetingButtons = [.. InternalTargetedMeetingButtons]; + EventTypes = [.. InternalEventTypes]; } internal List InternalPresets { get; } = []; @@ -113,6 +121,7 @@ public class MiraPluginInfo internal List InternalOptionGroups { get; } = []; internal List InternalMeetingButtons { get; } = []; internal List InternalTargetedMeetingButtons { get; } = []; + internal List InternalEventTypes { get; } = []; internal Dictionary GameModes { get; } = []; internal Dictionary InternalRoles { get; } = []; diff --git a/MiraAPI/PluginLoading/MiraPluginManager.cs b/MiraAPI/PluginLoading/MiraPluginManager.cs index f2c39f84..4c1c125a 100644 --- a/MiraAPI/PluginLoading/MiraPluginManager.cs +++ b/MiraAPI/PluginLoading/MiraPluginManager.cs @@ -89,6 +89,7 @@ public sealed class MiraPluginManager PluginsWithOptionsOrGameModes = [.. RegisteredPlugins.Where(m => m.MiraPlugin.DisplayOnOptionsMenu || m.GameModes.Count > 0)]; ModifierManager.Modifiers = new ReadOnlyCollection(ModifierManager.InternalModifiers); + MiraEventManager.EventTypes = new ReadOnlyCollection(MiraEventManager.InternalEventTypes); }; } @@ -125,6 +126,9 @@ public sealed class MiraPluginManager RegisterEventHandlers(type); + if (RegisterEventType(type, info)) + continue; + if (RegisterModifier(type, info)) continue; @@ -187,6 +191,19 @@ public sealed class MiraPluginManager } } + private static bool RegisterEventType(Type type, MiraPluginInfo info) + { + try + { + return MiraEventManager.RegisterEventType(type, info); + } + catch (Exception e) + { + Error($"Failed to register event type {type.Name}: {e}"); + return false; + } + } + /// /// Get a mira plugin by its GUID. ///