mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-11-19 13:17:10 +00:00
Merge branch 'dev' into no-boxing
This commit is contained in:
31 files changed
+414
-131
No files matched your search
@@ -3,7 +3,8 @@
|
||||
namespace MiraAPI.Events;
|
||||
|
||||
/// <summary>
|
||||
/// Event handle to use when unregistering a <see cref="MiraEvent"/>.
|
||||
/// Wrapper for <see cref="MiraEvent"/> handlers.
|
||||
/// Also used when unregistering a <see cref="MiraEvent"/>.
|
||||
/// </summary>
|
||||
public class MiraEventHandle
|
||||
{
|
||||
@@ -12,11 +13,37 @@ public class MiraEventHandle
|
||||
/// </summary>
|
||||
public Type EventType { get; }
|
||||
|
||||
internal MiraEventWrapper EventWrapper { get; }
|
||||
/// <summary>
|
||||
/// Gets the priority of the handler. Lower values are called first.
|
||||
/// </summary>
|
||||
public int Priority { get; }
|
||||
|
||||
internal MiraEventHandle(Type type, MiraEventWrapper eventWrapper)
|
||||
/// <summary>
|
||||
/// Gets the event handler delegate.
|
||||
/// </summary>
|
||||
public Delegate EventHandler { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the event handler should also be
|
||||
/// triggered by subclasses of <see cref="EventType"/>.
|
||||
/// </summary>
|
||||
public bool InvokeOnSubtype { get; }
|
||||
|
||||
internal MiraEventHandle(Delegate eventHandler, int priority, Type type, bool invokeOnSubtype)
|
||||
{
|
||||
EventHandler = eventHandler;
|
||||
Priority = priority;
|
||||
EventType = type;
|
||||
EventWrapper = eventWrapper;
|
||||
InvokeOnSubtype = invokeOnSubtype;
|
||||
}
|
||||
|
||||
internal bool IsTriggeredBy(Type type)
|
||||
{
|
||||
if (InvokeOnSubtype)
|
||||
{
|
||||
return EventType.IsAssignableTo(type);
|
||||
}
|
||||
|
||||
return EventType == type;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using MiraAPI.PluginLoading;
|
||||
using MiraAPI.Utilities;
|
||||
@@ -11,7 +13,21 @@ namespace MiraAPI.Events;
|
||||
/// </summary>
|
||||
public static class MiraEventManager
|
||||
{
|
||||
private static readonly Dictionary<Type, List<MiraEventWrapper>> EventWrappers = [];
|
||||
private static readonly Dictionary<Type, List<MiraEventHandle>> EventHandlers = [];
|
||||
private static readonly Dictionary<Type, List<MiraEventHandle>> EventHandlersToLoad = [];
|
||||
private static readonly Comparer<MiraEventHandle> EventComparer = Comparer<MiraEventHandle>.Create((a, b) => a.Priority.CompareTo(b.Priority));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of all registered <see cref="MiraEvent"/> types.
|
||||
/// Generic event types are registered as their generic type definitions.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<Type> EventTypes { get; internal set; } = [];
|
||||
|
||||
internal static List<Type> InternalEventTypes { get; } = [];
|
||||
|
||||
private static readonly Dictionary<Type, HashSet<Type>> TrackedEventTypes = [];
|
||||
|
||||
private static bool loadFinalized = false;
|
||||
|
||||
/// <summary>
|
||||
/// Invoke a <see cref="MiraEvent"/>.
|
||||
@@ -22,26 +38,7 @@ public static class MiraEventManager
|
||||
public static bool InvokeEvent<T>(this T eventInstance)
|
||||
where T : MiraEvent
|
||||
{
|
||||
EventWrappers.TryGetValue(typeof(T), out var handlers);
|
||||
|
||||
if (handlers == null || handlers.Count == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var handler in handlers)
|
||||
{
|
||||
try
|
||||
{
|
||||
((Action<T>)handler.EventHandler).Invoke(eventInstance);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Error($"Error invoking event handler for {typeof(T).Name}: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return eventInstance.InvokeEvent(typeof(T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -52,14 +49,27 @@ public static class MiraEventManager
|
||||
/// <returns>If there was an event handler invoked for this event, return <see langword="true"/>. Otherwise, return <see langword="false"/>.</returns>
|
||||
public static bool InvokeEvent(this MiraEvent eventInstance, Type type)
|
||||
{
|
||||
EventWrappers.TryGetValue(type, out var handlers);
|
||||
var trackedType = type.GetTrackedType();
|
||||
var callableHandlers = new List<MiraEventHandle>();
|
||||
|
||||
if (handlers == null || handlers.Count == 0)
|
||||
if (EventHandlers.TryGetValue(trackedType, out var handlers))
|
||||
{
|
||||
callableHandlers.AddRange(handlers.Where(x => x.IsTriggeredBy(type)));
|
||||
}
|
||||
|
||||
if (!loadFinalized && EventHandlersToLoad.TryGetValue(trackedType, out var pendingHandlers))
|
||||
{
|
||||
callableHandlers.AddRange(pendingHandlers.Where(x => x.IsTriggeredBy(type)));
|
||||
}
|
||||
|
||||
if (callableHandlers.Count == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var handler in handlers)
|
||||
callableHandlers.Sort(EventComparer);
|
||||
|
||||
foreach (var handler in callableHandlers)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -74,35 +84,113 @@ public static class MiraEventManager
|
||||
return true;
|
||||
}
|
||||
|
||||
internal static bool RegisterEventType(in MiraPluginManager.RegistrationContext context)
|
||||
{
|
||||
var eventType = context.Type;
|
||||
var pluginInfo = context.PluginInfo;
|
||||
|
||||
if (eventType.IsAbstract || !eventType.IsSubclassOf(typeof(MiraEvent)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
AddTypeToTracked(eventType);
|
||||
|
||||
// subtypes are added to their supertypes, regardless of which were added first
|
||||
TrackedEventTypes[eventType].UnionWith(InternalEventTypes.Where(x => x.IsSubclassOfWithGeneric(eventType)));
|
||||
foreach (var supertype in InternalEventTypes.Where(x => eventType.IsSubclassOfWithGeneric(x)))
|
||||
{
|
||||
AddTypeToTracked(supertype, eventType);
|
||||
}
|
||||
|
||||
pluginInfo.InternalEventTypes.Add(eventType);
|
||||
InternalEventTypes.Add(eventType);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void AddTypeToTracked(Type eventType, Type trackedAs = null!)
|
||||
{
|
||||
if (eventType.IsAbstract)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!TrackedEventTypes.TryGetValue(eventType, out var trackedEvents))
|
||||
{
|
||||
trackedEvents = [];
|
||||
TrackedEventTypes.Add(eventType, trackedEvents);
|
||||
}
|
||||
|
||||
trackedAs ??= eventType;
|
||||
trackedEvents.Add(trackedAs);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register a <see cref="MiraEvent"/> handler.
|
||||
/// </summary>
|
||||
/// <param name="type">The type of <see cref="MiraEvent"/> event.</param>
|
||||
/// <param name="methodInfo">The <see cref="MethodInfo"/> of the event handler.</param>
|
||||
/// <param name="priority">The priority of the event handler. Lower values are called first.</param>
|
||||
/// <param name="invokeOnSubtype">Whether the event handler should also be triggered by subclasses of the <see cref="MiraEvent"/> type of the handler.</param>
|
||||
/// <returns>A <see cref="MiraEventHandle"/> to use when unregistering the event.</returns>
|
||||
public static MiraEventHandle RegisterEventHandler(Type type, MethodInfo methodInfo, int priority = 0)
|
||||
public static MiraEventHandle RegisterEventHandler(Type type, MethodInfo methodInfo, int priority, bool invokeOnSubtype)
|
||||
{
|
||||
if (!type.IsSubclassOf(typeof(MiraEvent)))
|
||||
if (!type.IsAssignableTo(typeof(MiraEvent)))
|
||||
{
|
||||
throw new InvalidOperationException($"Type must be a subclass of MiraEvent: {type.FullName}");
|
||||
}
|
||||
|
||||
EventWrappers.TryAdd(type, []);
|
||||
var handlers = EventWrappers[type];
|
||||
|
||||
var @delegate = Delegate.CreateDelegate(typeof(Action<>).MakeGenericType(type), methodInfo);
|
||||
var eventWrapper = new MiraEventWrapper(@delegate, priority);
|
||||
return RegisterEventHandler(type, @delegate, priority, invokeOnSubtype);
|
||||
}
|
||||
|
||||
var index = handlers.BinarySearch(eventWrapper, Comparer<MiraEventWrapper>.Create((a, b) => a.Priority.CompareTo(b.Priority)));
|
||||
/// <inheritdoc cref="RegisterEventHandler(Type, MethodInfo, int, bool)"/>
|
||||
public static MiraEventHandle RegisterEventHandler(Type type, MethodInfo methodInfo, int priority)
|
||||
{
|
||||
return RegisterEventHandler(type, methodInfo, priority, false);
|
||||
}
|
||||
|
||||
if (index < 0)
|
||||
{
|
||||
index = ~index;
|
||||
}
|
||||
/// <inheritdoc cref="RegisterEventHandler(Type, MethodInfo, int, bool)"/>
|
||||
public static MiraEventHandle RegisterEventHandler(Type type, MethodInfo methodInfo, bool invokeOnSubtype)
|
||||
{
|
||||
return RegisterEventHandler(type, methodInfo, 0, invokeOnSubtype);
|
||||
}
|
||||
|
||||
handlers.Insert(index, eventWrapper);
|
||||
return new MiraEventHandle(type, eventWrapper);
|
||||
/// <inheritdoc cref="RegisterEventHandler(Type, MethodInfo, int, bool)"/>
|
||||
public static MiraEventHandle RegisterEventHandler(Type type, MethodInfo methodInfo)
|
||||
{
|
||||
return RegisterEventHandler(type, methodInfo, 0, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register a <see cref="MiraEvent"/> handler.
|
||||
/// </summary>
|
||||
/// <param name="methodInfo">The <see cref="MethodInfo"/> of the event handler.</param>
|
||||
/// <param name="priority">The priority of the event handler. Lower values are called first.</param>
|
||||
/// <param name="invokeOnSubtype">Whether the event handler should also be triggered by subclasses of the <see cref="MiraEvent"/> type of the handler.</param>
|
||||
/// <returns>A <see cref="MiraEventHandle"/> to use when unregistering the event.</returns>
|
||||
public static MiraEventHandle RegisterEventHandler(MethodInfo methodInfo, int priority, bool invokeOnSubtype)
|
||||
{
|
||||
return RegisterEventHandler(methodInfo.GetParameters()[0].ParameterType, methodInfo, priority, invokeOnSubtype);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="RegisterEventHandler(MethodInfo, int, bool)"/>
|
||||
public static MiraEventHandle RegisterEventHandler(MethodInfo methodInfo, int priority)
|
||||
{
|
||||
return RegisterEventHandler(methodInfo, priority, false);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="RegisterEventHandler(MethodInfo, int, bool)"/>
|
||||
public static MiraEventHandle RegisterEventHandler(MethodInfo methodInfo, bool invokeOnSubtype)
|
||||
{
|
||||
return RegisterEventHandler(methodInfo, 0, invokeOnSubtype);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="RegisterEventHandler(MethodInfo, int, bool)"/>
|
||||
public static MiraEventHandle RegisterEventHandler(MethodInfo methodInfo)
|
||||
{
|
||||
return RegisterEventHandler(methodInfo, 0, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -110,25 +198,87 @@ public static class MiraEventManager
|
||||
/// </summary>
|
||||
/// <param name="handler">The callback method/handler for the event.</param>
|
||||
/// <param name="priority">The priority of the event handler. Lower values are called first.</param>
|
||||
/// <param name="invokeOnSubtype">Whether the event handler should also be triggered by subclasses of the <see cref="MiraEvent"/> type of the handler.</param>
|
||||
/// <typeparam name="T">Type of <see cref="MiraEvent"/> event.</typeparam>
|
||||
/// <returns>A <see cref="MiraEventHandle"/> to use when unregistering the event.</returns>
|
||||
public static MiraEventHandle RegisterEventHandler<T>(Action<T> handler, int priority = 0)
|
||||
public static MiraEventHandle RegisterEventHandler<T>(Action<T> handler, int priority, bool invokeOnSubtype)
|
||||
where T : MiraEvent
|
||||
{
|
||||
EventWrappers.TryAdd(typeof(T), []);
|
||||
return RegisterEventHandler(typeof(T), handler, priority, invokeOnSubtype);
|
||||
}
|
||||
|
||||
var handlers = EventWrappers[typeof(T)];
|
||||
var eventWrapper = new MiraEventWrapper(handler, priority);
|
||||
/// <inheritdoc cref="RegisterEventHandler{T}(Action{T}, int, bool)"/>
|
||||
public static MiraEventHandle RegisterEventHandler<T>(Action<T> handler, int priority)
|
||||
where T : MiraEvent
|
||||
{
|
||||
return RegisterEventHandler(handler, priority, false);
|
||||
}
|
||||
|
||||
var index = handlers.BinarySearch(eventWrapper, Comparer<MiraEventWrapper>.Create((a, b) => a.Priority.CompareTo(b.Priority)));
|
||||
/// <inheritdoc cref="RegisterEventHandler{T}(Action{T}, int, bool)"/>
|
||||
public static MiraEventHandle RegisterEventHandler<T>(Action<T> handler, bool invokeOnSubtype)
|
||||
where T : MiraEvent
|
||||
{
|
||||
return RegisterEventHandler(handler, 0, invokeOnSubtype);
|
||||
}
|
||||
|
||||
if (index < 0)
|
||||
/// <inheritdoc cref="RegisterEventHandler{T}(Action{T}, int, bool)"/>
|
||||
public static MiraEventHandle RegisterEventHandler<T>(Action<T> handler)
|
||||
where T : MiraEvent
|
||||
{
|
||||
return RegisterEventHandler(handler, 0, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register a <see cref="MiraEvent"/> handler.
|
||||
/// </summary>
|
||||
/// <param name="type">The type of <see cref="MiraEvent"/> event.</param>
|
||||
/// <param name="delegate">The <see cref="Delegate"/> method/handler for the event.</param>
|
||||
/// <param name="priority">The priority of the event handler. Lower values are called first.</param>
|
||||
/// <param name="invokeOnSubtype">Whether the event handler should also be triggered by subclasses of the <see cref="MiraEvent"/> type of the handler.</param>
|
||||
/// <returns>A <see cref="MiraEventHandle"/> to use when unregistering the event.</returns>
|
||||
private static MiraEventHandle RegisterEventHandler(Type type, Delegate @delegate, int priority, bool invokeOnSubtype)
|
||||
{
|
||||
var eventHandler = new MiraEventHandle(@delegate, priority, type, invokeOnSubtype);
|
||||
EventHandlerAdded(type, eventHandler);
|
||||
return eventHandler;
|
||||
}
|
||||
|
||||
private static Type GetTrackedType(this Type type)
|
||||
{
|
||||
if (type.IsGenericType)
|
||||
{
|
||||
index = ~index;
|
||||
return type.GetGenericTypeDefinition();
|
||||
}
|
||||
|
||||
handlers.Insert(index, eventWrapper);
|
||||
return new MiraEventHandle(typeof(T), eventWrapper);
|
||||
return type;
|
||||
}
|
||||
|
||||
private static void EventHandlerAdded(Type type, MiraEventHandle eventHandler)
|
||||
{
|
||||
if (!loadFinalized)
|
||||
{
|
||||
EventHandlersToLoad.TryAdd(type, []);
|
||||
EventHandlersToLoad[type].Add(eventHandler);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!TrackedEventTypes.TryGetValue(type.GetTrackedType(), out var trackedEvents))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (Type eventType in trackedEvents)
|
||||
{
|
||||
var handlers = EventHandlers[eventType];
|
||||
var index = handlers.BinarySearch(eventHandler, EventComparer);
|
||||
|
||||
if (index < 0)
|
||||
{
|
||||
index = ~index;
|
||||
}
|
||||
|
||||
handlers.Insert(index, eventHandler);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -138,22 +288,59 @@ public static class MiraEventManager
|
||||
/// <returns><see langword="true"/> if the event was unregistered successfully, <see langword="false"/> otherwise.</returns>
|
||||
public static bool UnregisterEventHandler(MiraEventHandle eventHandle)
|
||||
{
|
||||
if (!EventWrappers.TryGetValue(eventHandle.EventType, out var handlers))
|
||||
if (!loadFinalized)
|
||||
{
|
||||
if (!EventHandlersToLoad.TryGetValue(eventHandle.EventType, out var handlersToLoad))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!handlersToLoad.Remove(eventHandle))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!TrackedEventTypes.TryGetValue(eventHandle.EventType.GetTrackedType(), out var eventTypes))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!handlers.Remove(eventHandle.EventWrapper))
|
||||
var removed = false;
|
||||
foreach (var type in eventTypes)
|
||||
{
|
||||
return false;
|
||||
if (EventHandlers.TryGetValue(type, out var handlers))
|
||||
{
|
||||
removed |= handlers.Remove(eventHandle);
|
||||
}
|
||||
}
|
||||
|
||||
if (handlers.Count == 0)
|
||||
return removed;
|
||||
}
|
||||
|
||||
internal static void OnLoadFinalize()
|
||||
{
|
||||
loadFinalized = true;
|
||||
EventTypes = new ReadOnlyCollection<Type>(InternalEventTypes);
|
||||
|
||||
foreach (var eventType in EventTypes)
|
||||
{
|
||||
EventWrappers.Remove(eventHandle.EventType);
|
||||
EventHandlers.Add(eventType, []);
|
||||
}
|
||||
|
||||
return true;
|
||||
foreach (var (concreteType, eventHandlers) in EventHandlersToLoad)
|
||||
{
|
||||
foreach (var eventHandler in eventHandlers)
|
||||
{
|
||||
EventHandlerAdded(concreteType, eventHandler);
|
||||
}
|
||||
|
||||
eventHandlers.Clear();
|
||||
}
|
||||
|
||||
EventHandlersToLoad.Clear();
|
||||
}
|
||||
|
||||
internal static bool RegisterEvents(in MiraPluginManager.RegistrationContext context)
|
||||
@@ -181,8 +368,7 @@ public static class MiraEventManager
|
||||
continue;
|
||||
}
|
||||
|
||||
var paramType = parameters[0].ParameterType;
|
||||
RegisterEventHandler(paramType, method, eventAttribute.Priority);
|
||||
RegisterEventHandler(method, eventAttribute.Priority, eventAttribute.InvokeOnSubtype);
|
||||
}
|
||||
|
||||
return hasContainerAttribute;
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace MiraAPI.Events;
|
||||
/// </summary>
|
||||
/// <param name="eventHandler">The action for the event handler.</param>
|
||||
/// <param name="priority">The priority of the handler.</param>
|
||||
[Obsolete("Use MiraEventHandle instead. This will be removed in a future version.")]
|
||||
public class MiraEventWrapper(Delegate eventHandler, int priority)
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -6,11 +6,36 @@ namespace MiraAPI.Events;
|
||||
/// Register a <see cref="MiraEvent"/> handler.
|
||||
/// </summary>
|
||||
/// <param name="priority">The priority of the event. Lower values are called first.</param>
|
||||
/// <param name="invokeOnSubtype">Whether the event handler should also be triggered by subclasses of the <see cref="MiraEvent"/> type of the handler.</param>
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public class RegisterEventAttribute(int priority = 0) : Attribute
|
||||
public class RegisterEventAttribute(int priority, bool invokeOnSubtype) : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the priority of the event. Lower values are called first.
|
||||
/// </summary>
|
||||
public int Priority => priority;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the event handler should also be
|
||||
/// triggered by subclasses of the <see cref="MiraEvent"/> type of the handler.
|
||||
/// </summary>
|
||||
public bool InvokeOnSubtype => invokeOnSubtype;
|
||||
|
||||
/// <inheritdoc cref="RegisterEventAttribute"/>
|
||||
public RegisterEventAttribute(int priority)
|
||||
: this(priority, false)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="RegisterEventAttribute"/>
|
||||
public RegisterEventAttribute(bool invokeOnSubtype)
|
||||
: this(0, invokeOnSubtype)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="RegisterEventAttribute"/>
|
||||
public RegisterEventAttribute()
|
||||
: this(0, false)
|
||||
{
|
||||
}
|
||||
}
|
||||
File renamed without changes.
@@ -165,28 +165,22 @@ public abstract class CustomActionButton
|
||||
{
|
||||
// Invoke the generic button click event.
|
||||
var genericEvent = new MiraButtonClickEvent(this);
|
||||
MiraEventManager.InvokeEvent(genericEvent);
|
||||
if (genericEvent.IsCancelled) MiraEventManager.InvokeEvent(new MiraButtonCancelledEvent(this));
|
||||
genericEvent.InvokeEvent();
|
||||
if (genericEvent.IsCancelled) new MiraButtonCancelledEvent(this).InvokeEvent();
|
||||
|
||||
// Invoke the button click event for specific button.
|
||||
var eventType = CustomButtonManager.ButtonEventTypes[GetType()];
|
||||
var @event = (MiraCancelableEvent)Activator.CreateInstance(eventType, this, genericEvent)!;
|
||||
var specificInvoked = MiraEventManager.InvokeEvent(@event, eventType);
|
||||
var specificInvoked = @event.InvokeEvent(eventType);
|
||||
if (@event.IsCancelled)
|
||||
{
|
||||
var cancelEventType = CustomButtonManager.ButtonCancelledEventTypes[GetType()];
|
||||
var cancelEvent = (MiraEvent)Activator.CreateInstance(cancelEventType, this)!;
|
||||
MiraEventManager.InvokeEvent(cancelEvent, cancelEventType);
|
||||
cancelEvent.InvokeEvent(cancelEventType);
|
||||
}
|
||||
|
||||
if (specificInvoked)
|
||||
{
|
||||
if (!@event.IsCancelled) ClickHandler();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!genericEvent.IsCancelled) ClickHandler();
|
||||
}
|
||||
MiraCancelableEvent eventCheck = specificInvoked ? @event : genericEvent;
|
||||
if (!eventCheck.IsCancelled) ClickHandler();
|
||||
}));
|
||||
|
||||
if (Keybind == null) return;
|
||||
|
||||
@@ -96,10 +96,10 @@ public class MiraApiSettings(ConfigFile config) : LocalSettingsTab(config)
|
||||
return;
|
||||
|
||||
var genericEvent = new UiButtonResetEvent();
|
||||
MiraEventManager.InvokeEvent(genericEvent);
|
||||
genericEvent.InvokeEvent();
|
||||
|
||||
var genericEvent2 = new UiButtonPostResetEvent(topUi, extraTopUi);
|
||||
MiraEventManager.InvokeEvent(genericEvent2);
|
||||
genericEvent2.InvokeEvent();
|
||||
MiraHudHelper.UiGrid.ArrangeChilds();
|
||||
MiraHudHelper.ExtraUiGrid.ArrangeChilds();
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ public static class CustomMurderRpc
|
||||
var murderResultFlags = didSucceed ? MurderResultFlags.Succeeded : MurderResultFlags.FailedError;
|
||||
|
||||
var beforeMurderEvent = new BeforeMurderEvent(source, target, inMeeting);
|
||||
MiraEventManager.InvokeEvent(beforeMurderEvent);
|
||||
beforeMurderEvent.InvokeEvent();
|
||||
var defenseFlag = beforeMurderEvent.IgnoreDefense;
|
||||
var indirectFlag = beforeMurderEvent.IsIndirectAttack;
|
||||
var isMeetingActive = MeetingHud.Instance != null || ExileController.Instance != null;
|
||||
@@ -227,7 +227,7 @@ public static class CustomMurderRpc
|
||||
var murderResultFlags = didSucceed ? MurderResultFlags.Succeeded : MurderResultFlags.FailedError;
|
||||
|
||||
var beforeMurderEvent = new BeforeMurderEvent(source, target, isIndirect, ignoreDefense, inMeeting);
|
||||
MiraEventManager.InvokeEvent(beforeMurderEvent);
|
||||
beforeMurderEvent.InvokeEvent();
|
||||
var defenseFlag = beforeMurderEvent.IgnoreDefense;
|
||||
var indirectFlag = beforeMurderEvent.IsIndirectAttack;
|
||||
var isMeetingActive = MeetingHud.Instance != null || ExileController.Instance != null;
|
||||
@@ -357,7 +357,7 @@ public static class CustomMurderRpc
|
||||
var murderResultFlags = didSucceed ? MurderResultFlags.Succeeded : MurderResultFlags.FailedError;
|
||||
|
||||
var beforeMurderEvent = new BeforeMurderEvent(source, target, framed, isIndirect, ignoreDefense, inMeeting);
|
||||
MiraEventManager.InvokeEvent(beforeMurderEvent);
|
||||
beforeMurderEvent.InvokeEvent();
|
||||
var defenseFlag = beforeMurderEvent.IgnoreDefense;
|
||||
var indirectFlag = beforeMurderEvent.IsIndirectAttack;
|
||||
var isMeetingActive = MeetingHud.Instance != null || ExileController.Instance != null;
|
||||
@@ -821,7 +821,7 @@ public static class CustomMurderRpc
|
||||
}
|
||||
|
||||
var afterMurderEvent = framed != null ? new AfterMurderEvent(source, target, framed, deadBody, isIndirect, ignoreDefense) : new AfterMurderEvent(source, target, deadBody, isIndirect, ignoreDefense);
|
||||
MiraEventManager.InvokeEvent(afterMurderEvent);
|
||||
afterMurderEvent.InvokeEvent();
|
||||
|
||||
if (!isParticipant)
|
||||
{
|
||||
|
||||
@@ -31,7 +31,7 @@ internal static class AmongUsClientSyncPatch
|
||||
var wrapper = new StateMachineWrapper<AmongUsClient>(__instance);
|
||||
var clientData = wrapper.GetParameter<ClientData>("clientData");
|
||||
var joinEvent = new PlayerJoinEvent(clientData);
|
||||
MiraEventManager.InvokeEvent(joinEvent);
|
||||
joinEvent.InvokeEvent();
|
||||
|
||||
if (!AmongUsClient.Instance.AmHost)
|
||||
{
|
||||
@@ -56,6 +56,6 @@ internal static class AmongUsClientSyncPatch2
|
||||
public static void PlayerLeftPatch(ClientData data, DisconnectReasons reason)
|
||||
{
|
||||
var leftEvent = new PlayerLeaveEvent(data, reason);
|
||||
MiraEventManager.InvokeEvent(leftEvent);
|
||||
leftEvent.InvokeEvent();
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,7 @@ public static class CanUsePatches
|
||||
var usable = __instance.TryCast<IUsable>();
|
||||
if (usable == null) return true;
|
||||
var @event = new PlayerCanUseEvent(usable);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
|
||||
return !@event.IsCancelled;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ internal static class EndGamePatches
|
||||
public static bool Prefix(GameOverReason endReason)
|
||||
{
|
||||
var @event = new BeforeGameEndEvent(endReason);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
|
||||
return !@event.IsCancelled;
|
||||
}
|
||||
@@ -42,7 +42,7 @@ internal static class EndGamePatches
|
||||
}
|
||||
|
||||
var @event = new GameEndEvent(__instance);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,6 @@ public static class FreeplayRoundStartPatch
|
||||
{
|
||||
if (__result) return;
|
||||
MeetingButtonManager.OnGameStart();
|
||||
MiraEventManager.InvokeEvent(new RoundStartEvent(true));
|
||||
new RoundStartEvent(true).InvokeEvent();
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,6 @@ public static class HnsFinishTaskPatch
|
||||
public static void FinishTaskPostfix(HideAndSeekManager __instance, PlayerTask task)
|
||||
{
|
||||
var completeTaskEvent = new CompleteHnsTaskEvent(__instance, task.Owner, task);
|
||||
MiraEventManager.InvokeEvent(completeTaskEvent);
|
||||
completeTaskEvent.InvokeEvent();
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,6 @@ public static class KillAnimationMurderEventPatch
|
||||
var target = wrapper.GetParameter<PlayerControl>("target");
|
||||
|
||||
var afterMurderEvent = new AfterMurderEvent(source, target, Helpers.GetBodyById(target.PlayerId));
|
||||
MiraEventManager.InvokeEvent(afterMurderEvent);
|
||||
afterMurderEvent.InvokeEvent();
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ public static class MapBehaviourPatches
|
||||
public static bool MapShowSabotagePatch(MapBehaviour __instance)
|
||||
{
|
||||
var @event = new PlayerOpenSabotageEvent(__instance);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
return !@event.IsCancelled;
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ public static class ReportBodyEventPatch
|
||||
var body = target != null ? Helpers.GetBodyById(target.PlayerId) : null;
|
||||
|
||||
var @event = new ReportBodyEvent(source, target, body);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
return !@event.IsCancelled;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ public static class SabotageEventPatches
|
||||
public static bool ShipStatusUpdateSystemPrefix(SystemTypes systemType, byte amount)
|
||||
{
|
||||
var @event = new UpdateSystemEvent(systemType, PlayerControl.LocalPlayer, amount);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
return !@event.IsCancelled;
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ public static class SabotageEventPatches
|
||||
public static bool ShipStatusCloseDoorsOfTypePrefix(SystemTypes type)
|
||||
{
|
||||
var @event = new CloseDoorsEvent(type);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
return !@event.IsCancelled;
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@ public static class UsePatches
|
||||
public static bool UsePatch(MonoBehaviour __instance)
|
||||
{
|
||||
var @event = new PlayerUseEvent(__instance.Cast<IUsable>());
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
|
||||
return !@event.IsCancelled;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using AmongUs.GameOptions;
|
||||
using System;
|
||||
using AmongUs.GameOptions;
|
||||
using HarmonyLib;
|
||||
using MiraAPI.Events;
|
||||
using MiraAPI.Events.Vanilla.Gameplay;
|
||||
@@ -40,14 +41,7 @@ public static class VanillaButtonPatches
|
||||
ventTarget = role.Cast<EngineerRole>().currentTarget;
|
||||
}
|
||||
|
||||
var genericEvent = new VanillaButtonClickEvent(__instance, playerTarget, ventTarget);
|
||||
MiraEventManager.InvokeEvent(genericEvent);
|
||||
if (genericEvent.IsCancelled)
|
||||
{
|
||||
MiraEventManager.InvokeEvent(new VanillaButtonCancelledEvent(__instance));
|
||||
}
|
||||
|
||||
return !genericEvent.IsCancelled;
|
||||
return InvokeEvents(__instance, playerTarget, ventTarget);
|
||||
}
|
||||
|
||||
[HarmonyPrefix]
|
||||
@@ -66,13 +60,29 @@ public static class VanillaButtonPatches
|
||||
playerTarget = role.Cast<SpiritGuideRole>().currentTarget;
|
||||
}
|
||||
|
||||
var genericEvent = new VanillaButtonClickEvent(__instance, playerTarget);
|
||||
MiraEventManager.InvokeEvent(genericEvent);
|
||||
return InvokeEvents(__instance, playerTarget);
|
||||
}
|
||||
|
||||
private static bool InvokeEvents(AbilityButton __instance, PlayerControl? playerTarget, Vent? ventTarget = null)
|
||||
{
|
||||
var genericEvent = new VanillaButtonClickEvent(__instance, playerTarget, ventTarget);
|
||||
genericEvent.InvokeEvent();
|
||||
if (genericEvent.IsCancelled)
|
||||
{
|
||||
MiraEventManager.InvokeEvent(new VanillaButtonCancelledEvent(__instance));
|
||||
new VanillaButtonCancelledEvent(__instance).InvokeEvent();
|
||||
}
|
||||
|
||||
return !genericEvent.IsCancelled;
|
||||
var eventType = typeof(VanillaButtonClickEvent<>).MakeGenericType(__instance.GetType());
|
||||
var @event = (MiraCancelableEvent)Activator.CreateInstance(eventType, [__instance])!;
|
||||
var specificInvoked = @event.InvokeEvent(eventType);
|
||||
if (@event.IsCancelled)
|
||||
{
|
||||
var cancelEventType = typeof(VanillaButtonCancelledEvent<>).MakeGenericType(__instance.GetType());
|
||||
var cancelEvent = (MiraEvent)Activator.CreateInstance(cancelEventType, [__instance])!;
|
||||
cancelEvent.InvokeEvent(cancelEventType);
|
||||
}
|
||||
|
||||
MiraCancelableEvent eventCheck = specificInvoked ? @event : genericEvent;
|
||||
return !eventCheck.IsCancelled;
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@ public static class VentEventPatches
|
||||
var vent = ShipStatus.Instance.AllVents.FirstOrDefault(v => v.Id == ventId);
|
||||
|
||||
var @event = new EnterVentEvent(pc, vent);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
|
||||
if (pc.AmOwner)
|
||||
{
|
||||
@@ -47,7 +47,7 @@ public static class VentEventPatches
|
||||
var vent = ShipStatus.Instance.AllVents.First(v => v.Id == ventId);
|
||||
|
||||
var @event = new ExitVentEvent(pc, vent);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
|
||||
if (pc.AmOwner)
|
||||
{
|
||||
|
||||
@@ -31,7 +31,7 @@ public static class IntroCutscenePatches
|
||||
public static void IntroBeginPatch(IntroCutscene __instance)
|
||||
{
|
||||
var @event = new IntroBeginEvent(__instance);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
|
||||
if (CustomGameModeManager.ActiveMode != null)
|
||||
{
|
||||
@@ -61,7 +61,7 @@ public static class IntroCutscenePatches
|
||||
|
||||
Info("IntroCutscene ShowRole reached");
|
||||
var @event = new IntroRoleRevealEvent(introCutscene);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,13 +135,13 @@ public static class IntroCutscenePatches
|
||||
Info("IntroCutscene ended");
|
||||
|
||||
MeetingButtonManager.OnGameStart();
|
||||
MiraEventManager.InvokeEvent(new IntroEndEvent(introCutscene));
|
||||
new IntroEndEvent(introCutscene).InvokeEvent();
|
||||
|
||||
var @event = new BeforeRoundStartEvent(true);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
|
||||
if (@event.IsCancelled) return;
|
||||
MiraEventManager.InvokeEvent(new RoundStartEvent(true));
|
||||
new RoundStartEvent(true).InvokeEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,7 @@ internal static class PlayerControlPatches
|
||||
public static void PlayerControlDiePostfix(PlayerControl __instance, DeathReason reason)
|
||||
{
|
||||
var deathEvent = new PlayerDeathEvent(__instance, reason, Helpers.GetBodyById(__instance.PlayerId));
|
||||
MiraEventManager.InvokeEvent(deathEvent);
|
||||
deathEvent.InvokeEvent();
|
||||
|
||||
var modifiersComponent = __instance.GetComponent<ModifierComponent>();
|
||||
|
||||
@@ -59,7 +59,7 @@ internal static class PlayerControlPatches
|
||||
if (playerTask != null)
|
||||
{
|
||||
var completeTaskEvent = new CompleteTaskEvent(__instance, playerTask);
|
||||
MiraEventManager.InvokeEvent(completeTaskEvent);
|
||||
completeTaskEvent.InvokeEvent();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ internal static class PlayerControlPatches
|
||||
}
|
||||
|
||||
var beforeMurderEvent = new BeforeMurderEvent(__instance, target);
|
||||
MiraEventManager.InvokeEvent(beforeMurderEvent);
|
||||
beforeMurderEvent.InvokeEvent();
|
||||
|
||||
if (beforeMurderEvent.IsCancelled)
|
||||
{
|
||||
|
||||
@@ -17,7 +17,7 @@ internal static class EjectionPatches
|
||||
public static void BeginPostfix(ExileController __instance)
|
||||
{
|
||||
var @event = new EjectionEvent(__instance);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
|
||||
if (!__instance.initData.networkedPlayer || !__instance.initData.networkedPlayer.Role ||
|
||||
__instance.initData.networkedPlayer.Role is not ICustomRole role)
|
||||
@@ -39,12 +39,12 @@ internal static class EjectionPatches
|
||||
public static void Postfix()
|
||||
{
|
||||
var @event = new BeforeRoundStartEvent(false);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
|
||||
if (@event.IsCancelled) return;
|
||||
|
||||
var @event2 = new RoundStartEvent(false);
|
||||
MiraEventManager.InvokeEvent(@event2);
|
||||
@event2.InvokeEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,7 +61,7 @@ public static class RoleManagerPatches
|
||||
}
|
||||
|
||||
var @event = new SetRoleEvent(targetPlayer, roleType);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ internal static class VentCanUsePatch
|
||||
public static void Postfix(Vent __instance, ref float __result, [HarmonyArgument(0)] NetworkedPlayerInfo pc, [HarmonyArgument(1)] ref bool canUse, [HarmonyArgument(2)] ref bool couldUse)
|
||||
{
|
||||
var @event = new PlayerCanUseEvent(__instance.Cast<IUsable>());
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
|
||||
if (@event.IsCancelled)
|
||||
{
|
||||
|
||||
@@ -43,7 +43,7 @@ internal static class DummyBehaviourPatches
|
||||
yield return new WaitForSeconds(dummy.voteTime.Next());
|
||||
|
||||
var dummyVoteEvent = new DummyVoteEvent(dummy);
|
||||
MiraEventManager.InvokeEvent(dummyVoteEvent);
|
||||
dummyVoteEvent.InvokeEvent();
|
||||
|
||||
if (dummyVoteEvent.IsCancelled)
|
||||
{
|
||||
|
||||
@@ -24,7 +24,7 @@ internal static class MeetingHudPatches
|
||||
public static bool VotePatch(PlayerVoteArea __instance)
|
||||
{
|
||||
var beforeVoteEvent = new BeforeVoteEvent(__instance, PlayerControl.LocalPlayer);
|
||||
MiraEventManager.InvokeEvent(beforeVoteEvent);
|
||||
beforeVoteEvent.InvokeEvent();
|
||||
|
||||
if (beforeVoteEvent.IsCancelled)
|
||||
{
|
||||
@@ -32,7 +32,7 @@ internal static class MeetingHudPatches
|
||||
}
|
||||
|
||||
var afterVoteEvent = new AfterVoteEvent(__instance, PlayerControl.LocalPlayer);
|
||||
MiraEventManager.InvokeEvent(afterVoteEvent);
|
||||
afterVoteEvent.InvokeEvent();
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -99,7 +99,7 @@ internal static class MeetingHudPatches
|
||||
}
|
||||
|
||||
var @event = new StartMeetingEvent(__instance);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
}
|
||||
|
||||
[HarmonyPostfix]
|
||||
@@ -107,7 +107,7 @@ internal static class MeetingHudPatches
|
||||
public static void OnDestroyPatch(MeetingHud __instance)
|
||||
{
|
||||
MeetingVoteAreaVisibility.RestoreAll();
|
||||
MiraEventManager.InvokeEvent(new EndMeetingEvent(__instance));
|
||||
new EndMeetingEvent(__instance).InvokeEvent();
|
||||
}
|
||||
|
||||
[HarmonyPostfix]
|
||||
@@ -125,7 +125,7 @@ internal static class MeetingHudPatches
|
||||
}
|
||||
}
|
||||
|
||||
MiraEventManager.InvokeEvent(new VotingCompleteEvent(__instance));
|
||||
new VotingCompleteEvent(__instance).InvokeEvent();
|
||||
}
|
||||
|
||||
// this is necessary because the actual ForceSkipAll method is inlined.
|
||||
@@ -179,7 +179,7 @@ internal static class MeetingHudPatches
|
||||
var hasVotedFor = voteData.VotedFor((byte)suspectStateIdx);
|
||||
|
||||
var @event = new MeetingSelectEvent(voteData, suspectStateIdx, hasVotes && !hasVotedFor);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
|
||||
return @event.AllowSelect;
|
||||
}
|
||||
@@ -231,7 +231,7 @@ internal static class MeetingHudPatches
|
||||
var shouldEnd = !Helpers.GetAlivePlayers().Exists(plr => plr.GetVoteData().VotesRemaining > 0);
|
||||
|
||||
var checkEndEvent = new CheckForEndVotingEvent(shouldEnd);
|
||||
MiraEventManager.InvokeEvent(checkEndEvent);
|
||||
checkEndEvent.InvokeEvent();
|
||||
shouldEnd = checkEndEvent.ForceEndVoting || checkEndEvent is { IsCancelled: false, IsVotingComplete: true };
|
||||
|
||||
if (!shouldEnd)
|
||||
@@ -243,7 +243,7 @@ internal static class MeetingHudPatches
|
||||
var exiled = VotingUtils.GetExiled(votes, out var isTie);
|
||||
|
||||
var @event = new ProcessVotesEvent(votes, exiled);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
|
||||
if (@event.VotesModified)
|
||||
{
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Reflection;
|
||||
using BepInEx;
|
||||
using BepInEx.Configuration;
|
||||
using MiraAPI.Events;
|
||||
using MiraAPI.GameModes;
|
||||
using MiraAPI.GameOptions;
|
||||
using MiraAPI.Hud;
|
||||
@@ -93,10 +95,15 @@ public class MiraPluginInfo
|
||||
public IReadOnlyCollection<MeetingActionButton> MeetingButtons { get; private set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="ReadOnlyDictionary{TKey, TValue}"/> of this <see cref="IMiraPlugin"/>'s <see cref="OptionPreset"/>s.
|
||||
/// Gets a <see cref="IReadOnlyCollection{T}"/> of this <see cref="IMiraPlugin"/>'s <see cref="OptionPreset"/>s.
|
||||
/// </summary>
|
||||
public IReadOnlyCollection<OptionPreset> Presets { get; internal set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="IReadOnlyCollection{T}"/> of this <see cref="IMiraPlugin"/>'s <see cref="MiraEvent"/> types.
|
||||
/// </summary>
|
||||
public IReadOnlyCollection<Type> EventTypes { get; internal set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a read only dictionary of Game Mode IDs and the AbstractGameMode object they are associated with.
|
||||
/// </summary>
|
||||
@@ -116,6 +123,7 @@ public class MiraPluginInfo
|
||||
Buttons = [.. InternalButtons];
|
||||
MeetingButtons = [.. InternalMeetingButtons];
|
||||
TargetedMeetingButtons = [.. InternalTargetedMeetingButtons];
|
||||
EventTypes = [.. InternalEventTypes];
|
||||
}
|
||||
|
||||
internal List<OptionPreset> InternalPresets { get; } = [];
|
||||
@@ -125,6 +133,7 @@ public class MiraPluginInfo
|
||||
internal List<AbstractOptionGroup> InternalOptionGroups { get; } = [];
|
||||
internal List<MeetingActionButton> InternalMeetingButtons { get; } = [];
|
||||
internal List<TargetedMeetingButton> InternalTargetedMeetingButtons { get; } = [];
|
||||
internal List<Type> InternalEventTypes { get; } = [];
|
||||
|
||||
internal Dictionary<uint, AbstractGameMode> GameModes { get; } = [];
|
||||
internal Dictionary<ushort, RoleBehaviour> InternalRoles { get; } = [];
|
||||
|
||||
@@ -55,6 +55,7 @@ public sealed class MiraPluginManager
|
||||
private static readonly (string LogContext, TypeRegistrationHandler Handler)[] RegistrationHandlers =
|
||||
[
|
||||
("events in", MiraEventManager.RegisterEvents),
|
||||
("event type", MiraEventManager.RegisterEventType),
|
||||
("options for", ModdedOptionsManager.RegisterGroup),
|
||||
("options for", LocalSettingsManager.RegisterLocalTabs),
|
||||
("color class", PaletteManager.RegisterColors),
|
||||
@@ -113,6 +114,7 @@ public sealed class MiraPluginManager
|
||||
PaletteManager.RegisterAllColors();
|
||||
CustomButtonManager.RegisterAllButtons();
|
||||
ModifierManager.RegisterAllModifiers();
|
||||
MiraEventManager.OnLoadFinalize();
|
||||
|
||||
// Cache all the registered plugins into an array for easy access
|
||||
RegisteredPlugins = [.. _registeredPlugins.Values];
|
||||
|
||||
@@ -602,6 +602,35 @@ public static class Extensions
|
||||
return type is { IsClass: true, IsAbstract: true, IsSealed: true };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if <paramref name="type"/> is a subclass of <paramref name="super"/>, including
|
||||
/// account for generics, or given generic type definitions.
|
||||
/// </summary>
|
||||
/// <param name="type">The type to check.</param>
|
||||
/// <param name="super">The type to compare against.</param>
|
||||
/// <returns><see langword="true"/> if <paramref name="type"/> is a sublass of <paramref name="type"/>, <see langword="false"/> otherwise.</returns>
|
||||
public static bool IsSubclassOfWithGeneric(this Type? type, Type super)
|
||||
{
|
||||
if (type == null || type == typeof(object))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (type == super)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (super.IsGenericTypeDefinition &&
|
||||
type.IsGenericType &&
|
||||
type.GetGenericTypeDefinition() == super)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return type.BaseType.IsSubclassOfWithGeneric(super);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a darkened version of a <see cref="Color32"/>.
|
||||
/// </summary>
|
||||
|
||||
@@ -47,7 +47,7 @@ public static class VotingUtils
|
||||
private static void HandleVote(PlayerVoteData voteData, byte suspectIdx, bool byJudge, out bool cancelVote)
|
||||
{
|
||||
var @event = new HandleVoteEvent(voteData, suspectIdx, byJudge);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
|
||||
cancelVote = @event.PreventVote;
|
||||
|
||||
@@ -290,7 +290,7 @@ public static class VotingUtils
|
||||
public static void HandlePopulateResults(List<CustomVote> votes)
|
||||
{
|
||||
var @event = new PopulateResultsEvent(votes);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
@event.InvokeEvent();
|
||||
|
||||
if (@event.IsCancelled)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user