From 06b7099ba5e82690e7bc0c5dada83edd700cd1e4 Mon Sep 17 00:00:00 2001
From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com>
Date: Fri, 6 Nov 2026 14:04:04 -0700
Subject: [PATCH] Added InvokeOnSubype to RegisterEventAttribute
---
MiraAPI/Events/MiraEventHandle.cs | 19 ++++++++++++++++++-
MiraAPI/Events/MiraEventManager.cs | 22 +++++++++++++---------
MiraAPI/Events/RegisterEventAttribute.cs | 9 ++++++++-
MiraAPI/PluginLoading/MiraPluginManager.cs | 2 +-
4 files changed, 40 insertions(+), 12 deletions(-)
diff --git a/MiraAPI/Events/MiraEventHandle.cs b/MiraAPI/Events/MiraEventHandle.cs
index 86a2782f..9c820621 100644
--- a/MiraAPI/Events/MiraEventHandle.cs
+++ b/MiraAPI/Events/MiraEventHandle.cs
@@ -23,10 +23,27 @@ public class MiraEventHandle
///
internal Delegate EventHandler { get; }
- internal MiraEventHandle(Delegate eventHandler, int priority, Type type)
+ ///
+ /// Gets a value indicating whether the event handler should also be
+ /// tirggered by subclasses of .
+ ///
+ internal bool InvokeOnSubtype { get; }
+
+ internal MiraEventHandle(Delegate eventHandler, int priority, Type type, bool invokeOnSubtype)
{
EventHandler = eventHandler;
Priority = priority;
EventType = type;
+ InvokeOnSubtype = invokeOnSubtype;
+ }
+
+ internal bool IsTriggeredBy(Type type)
+ {
+ if (InvokeOnSubtype)
+ {
+ return EventType.IsAssignableTo(type);
+ }
+
+ return EventType == type;
}
}
diff --git a/MiraAPI/Events/MiraEventManager.cs b/MiraAPI/Events/MiraEventManager.cs
index 83b50aa6..e5d296e1 100644
--- a/MiraAPI/Events/MiraEventManager.cs
+++ b/MiraAPI/Events/MiraEventManager.cs
@@ -53,7 +53,7 @@ public static class MiraEventManager
return false;
}
- var callableHandlers = handlers.Where(x => x.EventType.IsAssignableTo(type)).ToList();
+ var callableHandlers = handlers.Where(x => x.IsTriggeredBy(type)).ToList();
if (callableHandlers.Count == 0)
{
return false;
@@ -141,8 +141,9 @@ public static class MiraEventManager
/// The type of event.
/// The of the event handler.
/// The priority of the event handler. Lower values are called first.
+ /// Whether the event handler should also be triggered by subclasses of the type of the handler.
/// A to use when unregistering the event.
- public static MiraEventHandle RegisterEventHandler(Type type, MethodInfo methodInfo, int priority = 0)
+ public static MiraEventHandle RegisterEventHandler(Type type, MethodInfo methodInfo, int priority = 0, bool invokeOnSubtype = true)
{
if (!type.IsAssignableTo(typeof(MiraEvent)))
{
@@ -150,7 +151,7 @@ public static class MiraEventManager
}
var @delegate = Delegate.CreateDelegate(typeof(Action<>).MakeGenericType(type), methodInfo);
- return RegisterEventHandler(type, @delegate, priority);
+ return RegisterEventHandler(type, @delegate, priority, invokeOnSubtype);
}
///
@@ -158,10 +159,11 @@ public static class MiraEventManager
///
/// The of the event handler.
/// The priority of the event handler. Lower values are called first.
+ /// Whether the event handler should also be triggered by subclasses of the type of the handler.
/// A to use when unregistering the event.
- public static MiraEventHandle RegisterEventHandler(MethodInfo methodInfo, int priority = 0)
+ public static MiraEventHandle RegisterEventHandler(MethodInfo methodInfo, int priority = 0, bool invokeOnSubtype = true)
{
- return RegisterEventHandler(methodInfo.GetParameters()[0].ParameterType, methodInfo, priority);
+ return RegisterEventHandler(methodInfo.GetParameters()[0].ParameterType, methodInfo, priority, invokeOnSubtype);
}
///
@@ -169,12 +171,13 @@ public static class MiraEventManager
///
/// The callback method/handler for the event.
/// The priority of the event handler. Lower values are called first.
+ /// Whether the event handler should also be triggered by subclasses of the type of the handler.
/// Type of event.
/// A to use when unregistering the event.
- public static MiraEventHandle RegisterEventHandler(Action handler, int priority = 0)
+ public static MiraEventHandle RegisterEventHandler(Action handler, int priority = 0, bool invokeOnSubtype = true)
where T : MiraEvent
{
- return RegisterEventHandler(typeof(T), handler, priority);
+ return RegisterEventHandler(typeof(T), handler, priority, invokeOnSubtype);
}
///
@@ -183,10 +186,11 @@ public static class MiraEventManager
/// The type of event.
/// The method/handler for the event.
/// The priority of the event handler. Lower values are called first.
+ /// Whether the event handler should also be triggered by subclasses of the type of the handler.
/// A to use when unregistering the event.
- private static MiraEventHandle RegisterEventHandler(Type type, Delegate @delegate, int priority)
+ private static MiraEventHandle RegisterEventHandler(Type type, Delegate @delegate, int priority, bool invokeOnSubtype)
{
- var eventHandler = new MiraEventHandle(@delegate, priority, type);
+ var eventHandler = new MiraEventHandle(@delegate, priority, type, invokeOnSubtype);
EventHandlerAdded(type, eventHandler);
return eventHandler;
}
diff --git a/MiraAPI/Events/RegisterEventAttribute.cs b/MiraAPI/Events/RegisterEventAttribute.cs
index 062f30ec..c3a6b736 100644
--- a/MiraAPI/Events/RegisterEventAttribute.cs
+++ b/MiraAPI/Events/RegisterEventAttribute.cs
@@ -6,11 +6,18 @@ namespace MiraAPI.Events;
/// Register a handler.
///
/// The priority of the event. Lower values are called first.
+/// Whether the event handler should also be triggered by subclasses of the type of the handler.
[AttributeUsage(AttributeTargets.Method)]
-public class RegisterEventAttribute(int priority = 0) : Attribute
+public class RegisterEventAttribute(int priority = 0, bool invokeOnSubtype = true) : Attribute
{
///
/// Gets the priority of the event. Lower values are called first.
///
public int Priority { get; } = priority;
+
+ ///
+ /// Gets a value indicating whether the event handler should also be
+ /// triggered by subclasses of the type of the handler.
+ ///
+ public bool InvokeOnSubtype { get; } = invokeOnSubtype;
}
diff --git a/MiraAPI/PluginLoading/MiraPluginManager.cs b/MiraAPI/PluginLoading/MiraPluginManager.cs
index 83a784e6..2fb5a5b8 100644
--- a/MiraAPI/PluginLoading/MiraPluginManager.cs
+++ b/MiraAPI/PluginLoading/MiraPluginManager.cs
@@ -197,7 +197,7 @@ public sealed class MiraPluginManager
continue;
}
- MiraEventManager.RegisterEventHandler(method, eventAttribute.Priority);
+ MiraEventManager.RegisterEventHandler(method, eventAttribute.Priority, eventAttribute.InvokeOnSubtype);
}
}