mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-11-19 13:17:10 +00:00
Registered MiraEvent types on plugins to track subclasses for inheritance calls
This commit is contained in:
1 parent
82b3200f9b
commit
8410d61ac5
3 files changed
+88
-1
No files matched your search
@@ -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<Type, List<MiraEventWrapper>> EventWrappers = [];
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of all registered <see cref="MiraEvent"/> types.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<Type> EventTypes { get; internal set; } = [];
|
||||
|
||||
internal static List<Type> InternalEventTypes { get; } = [];
|
||||
|
||||
private static readonly Dictionary<Type, List<Type>> TrackedEventTypes = [];
|
||||
|
||||
private static readonly HashSet<Type> StoredEventTypes = [];
|
||||
|
||||
/// <summary>
|
||||
/// Invoke a <see cref="MiraEvent"/>.
|
||||
/// </summary>
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register a <see cref="MiraEvent"/> handler.
|
||||
/// </summary>
|
||||
|
||||
@@ -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
|
||||
/// </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>
|
||||
@@ -104,6 +111,7 @@ public class MiraPluginInfo
|
||||
Buttons = [.. InternalButtons];
|
||||
MeetingButtons = [.. InternalMeetingButtons];
|
||||
TargetedMeetingButtons = [.. InternalTargetedMeetingButtons];
|
||||
EventTypes = [.. InternalEventTypes];
|
||||
}
|
||||
|
||||
internal List<OptionPreset> InternalPresets { get; } = [];
|
||||
@@ -113,6 +121,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; } = [];
|
||||
|
||||
@@ -89,6 +89,7 @@ public sealed class MiraPluginManager
|
||||
PluginsWithOptionsOrGameModes = [.. RegisteredPlugins.Where(m => m.MiraPlugin.DisplayOnOptionsMenu || m.GameModes.Count > 0)];
|
||||
|
||||
ModifierManager.Modifiers = new ReadOnlyCollection<BaseModifier>(ModifierManager.InternalModifiers);
|
||||
MiraEventManager.EventTypes = new ReadOnlyCollection<Type>(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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a mira plugin by its GUID.
|
||||
/// </summary>
|
||||
|
||||
Reference in new issue
Block a user