diff --git a/MiraAPI.Example/ExampleEventHandlers.cs b/MiraAPI.Example/ExampleEventHandlers.cs
index c14710a9..65265ad1 100644
--- a/MiraAPI.Example/ExampleEventHandlers.cs
+++ b/MiraAPI.Example/ExampleEventHandlers.cs
@@ -14,6 +14,7 @@ using Reactor.Utilities;
namespace MiraAPI.Example;
+[RegisterEventContainer]
public static class ExampleEventHandlers
{
public static void Initialize()
diff --git a/MiraAPI.Example/ExamplePlugin.cs b/MiraAPI.Example/ExamplePlugin.cs
index eadc271d..57812ded 100644
--- a/MiraAPI.Example/ExamplePlugin.cs
+++ b/MiraAPI.Example/ExamplePlugin.cs
@@ -26,6 +26,9 @@ public partial class ExamplePlugin : BasePlugin, IMiraPlugin
public string OptionsTitleText { get; } = "Mira API\nExample Mod";
public string CustomOptionMenuNameTwo { get; } = "Example Options";
+ ///
+ public bool ForceRegisterEvents => false;
+
public ConfigFile GetConfigFile()
{
return Config;
diff --git a/MiraAPI/Colors/PaletteManager.cs b/MiraAPI/Colors/PaletteManager.cs
index fb0106a8..837e6bc2 100644
--- a/MiraAPI/Colors/PaletteManager.cs
+++ b/MiraAPI/Colors/PaletteManager.cs
@@ -53,12 +53,6 @@ public static class PaletteManager
return false;
}
- if (!type.IsStatic())
- {
- Error($"Color class {type.Name} must be static.");
- return false;
- }
-
foreach (var property in type.GetProperties(MiraPluginManager.DeclaredStaticMembers))
{
if (!property.PropertyType.IsAssignableTo(typeof(CustomColor)))
diff --git a/MiraAPI/Colors/VariableColor.cs b/MiraAPI/Colors/VariableColor.cs
index 6a6eae1c..4f178471 100644
--- a/MiraAPI/Colors/VariableColor.cs
+++ b/MiraAPI/Colors/VariableColor.cs
@@ -40,7 +40,7 @@ public sealed class VariableColor(
///
/// Gets a delegate that takes in a time value and returns the dynamic shadow color.
///
- public Func ShadowEvaluator => shadowEvaluator ?? (t => mainEvaluator(t).DarkenAbsolute(0.24f));
+ public Func ShadowEvaluator { get; } = shadowEvaluator ?? (t => mainEvaluator(t).DarkenAbsolute(0.24f));
///
public VariableColor(
@@ -91,6 +91,7 @@ public sealed class VariableColor(
}
///
+ [Obsolete("Use the constructor that takes in a CustomColorBrightness parameter. This will be removed in a future version.")]
public VariableColor(
StringNames nameId,
Func mainEvaluator,
@@ -103,6 +104,7 @@ public sealed class VariableColor(
}
///
+ [Obsolete("Use the constructor that takes in a CustomColorBrightness parameter. This will be removed in a future version.")]
public VariableColor(
StringNames nameId,
Func mainEvaluator,
@@ -113,6 +115,7 @@ public sealed class VariableColor(
}
///
+ [Obsolete("Use the constructor that takes in a CustomColorBrightness parameter. This will be removed in a future version.")]
public VariableColor(
string rawId,
Func mainEvaluator,
@@ -125,6 +128,7 @@ public sealed class VariableColor(
}
///
+ [Obsolete("Use the constructor that takes in a CustomColorBrightness parameter. This will be removed in a future version.")]
public VariableColor(
string rawId,
Func mainEvaluator,
diff --git a/MiraAPI/Events/MiraEventManager.cs b/MiraAPI/Events/MiraEventManager.cs
index 1f4cf74c..299a4b8d 100644
--- a/MiraAPI/Events/MiraEventManager.cs
+++ b/MiraAPI/Events/MiraEventManager.cs
@@ -156,8 +156,13 @@ public static class MiraEventManager
return true;
}
- internal static void RegisterEvents(Type type)
+ internal static void RegisterEvents(Type type, IMiraPlugin miraPlugin)
{
+ if (!miraPlugin.ForceRegisterEvents && !type.IsDefined())
+ {
+ return;
+ }
+
foreach (var method in type.GetMethods(MiraPluginManager.DeclaredStaticMembers))
{
if (!method.TryGetCustomAttribute(out var eventAttribute))
diff --git a/MiraAPI/Events/RegisterEventsAttribute.cs b/MiraAPI/Events/RegisterEventsAttribute.cs
new file mode 100644
index 00000000..9e6a1e63
--- /dev/null
+++ b/MiraAPI/Events/RegisterEventsAttribute.cs
@@ -0,0 +1,9 @@
+using System;
+
+namespace MiraAPI.Events;
+
+///
+/// Register a container of s.
+///
+[AttributeUsage(AttributeTargets.Class)]
+public class RegisterEventContainerAttribute : Attribute;
diff --git a/MiraAPI/GameEnd/GameOverManager.cs b/MiraAPI/GameEnd/GameOverManager.cs
index f536c49b..0a6288d8 100644
--- a/MiraAPI/GameEnd/GameOverManager.cs
+++ b/MiraAPI/GameEnd/GameOverManager.cs
@@ -14,13 +14,7 @@ public static class GameOverManager
private static int nextId = Enum.GetNames().Length;
- ///
- /// Register a .
- ///
- /// Type of the .
- /// Thrown when the type is not a subclass of , is abstract, or does not have a parameterless constructor.
- /// if the game over was registered successfully, otherwise.
- public static bool RegisterGameOver(Type gameOverType)
+ internal static bool RegisterGameOver(Type gameOverType)
{
if (!typeof(CustomGameOver).IsAssignableFrom(gameOverType))
{
diff --git a/MiraAPI/GameModes/CustomGameModeManager.cs b/MiraAPI/GameModes/CustomGameModeManager.cs
index 8a74f4f1..b6e90e39 100644
--- a/MiraAPI/GameModes/CustomGameModeManager.cs
+++ b/MiraAPI/GameModes/CustomGameModeManager.cs
@@ -19,12 +19,6 @@ public static class CustomGameModeManager
return ++lastId;
}
- ///
- /// Register gamemode from type.
- ///
- /// Type of gamemode class, should inherit from .
- /// The custom plugin info of the mod.
- /// Whether the gamemode was successfully registered.
internal static bool RegisterGameMode(Type gameModeType, MiraPluginInfo pluginInfo)
{
if (!typeof(AbstractGameMode).IsAssignableFrom(gameModeType))
diff --git a/MiraAPI/Keybinds/KeybindManager.cs b/MiraAPI/Keybinds/KeybindManager.cs
index 057b3243..1d71254a 100644
--- a/MiraAPI/Keybinds/KeybindManager.cs
+++ b/MiraAPI/Keybinds/KeybindManager.cs
@@ -85,12 +85,6 @@ public static class KeybindManager
return false;
}
- if (!type.IsStatic())
- {
- Error($"Keybinds class {type.Name} must be static.");
- return false;
- }
-
foreach (var property in type.GetProperties(MiraPluginManager.DeclaredStaticMembers))
{
if (property.PropertyType != typeof(MiraKeybind))
diff --git a/MiraAPI/MiraApiPlugin.cs b/MiraAPI/MiraApiPlugin.cs
index 7e744781..633145be 100644
--- a/MiraAPI/MiraApiPlugin.cs
+++ b/MiraAPI/MiraApiPlugin.cs
@@ -8,7 +8,6 @@ using MiraAPI.Patches;
using MiraAPI.Patches.Roles;
using MiraAPI.PluginLoading;
using MiraAPI.Translation;
-using MiraAPI.VanillaEvents;
using Reactor;
using Reactor.Localization;
using Reactor.Networking;
@@ -44,6 +43,9 @@ public partial class MiraApiPlugin : BasePlugin, IMiraPlugin
///
public bool DebugMode => IsDevBuild;
+ ///
+ public bool ForceRegisterEvents => false;
+
///
/// Gets a value indicating whether the current device is running Starlight (on mobile).
///
@@ -96,8 +98,6 @@ public partial class MiraApiPlugin : BasePlugin, IMiraPlugin
"Dictates the unit of distance used in numeric option displays representing a distance.");
Harmony.PatchAll();
RoleOptionsCollectionPatch.PatchRoleMethods(Harmony);
- UiResetEvents.Initialize();
- JudgeEvents.Initialize();
ReactorCredits.Register("Mira API", Version, IsDevBuild, ReactorCredits.AlwaysShow);
diff --git a/MiraAPI/Modifiers/ModifierManager.cs b/MiraAPI/Modifiers/ModifierManager.cs
index f12d2172..aa6c5120 100644
--- a/MiraAPI/Modifiers/ModifierManager.cs
+++ b/MiraAPI/Modifiers/ModifierManager.cs
@@ -68,8 +68,9 @@ public static class ModifierManager
return false;
}
- IdToTypeModifierMap.Add(GetNextTypeId(), modifierType);
- TypeToIdModifierMap.Add(modifierType, _nextTypeId);
+ var id = GetNextTypeId();
+ IdToTypeModifierMap.Add(id, modifierType);
+ TypeToIdModifierMap.Add(modifierType, id);
BaseModifier modifier;
var ctor = modifierType.GetConstructor(Type.EmptyTypes);
@@ -105,7 +106,7 @@ public static class ModifierManager
PrioritiesToIdsMap[priority] = list = [];
}
- list.Add(_nextTypeId);
+ list.Add(id);
return true;
}
diff --git a/MiraAPI/PluginLoading/IMiraPlugin.cs b/MiraAPI/PluginLoading/IMiraPlugin.cs
index f1f9fc30..4ffb044d 100644
--- a/MiraAPI/PluginLoading/IMiraPlugin.cs
+++ b/MiraAPI/PluginLoading/IMiraPlugin.cs
@@ -1,6 +1,8 @@
-using System.Globalization;
+using System;
+using System.Globalization;
using System.Linq;
using BepInEx.Configuration;
+using MiraAPI.Events;
namespace MiraAPI.PluginLoading;
@@ -63,6 +65,17 @@ public interface IMiraPlugin
///
bool DebugMode => false;
+ ///
+ /// Gets a value indicating whether the plugin relies on the legacy global event scanning system.
+ ///
+ ///
+ /// This property defaults to true to maintain backwards compatibility.
+ /// To upgrade your mod, apply the to any class
+ /// containing events, and then override this property to return .
+ ///
+ [Obsolete("Global event scanning is deprecated. Add [RegisterEventContainer] to your event classes and override this property to false.")]
+ bool ForceRegisterEvents => true;
+
///
/// Gets the for the plugin.
///
diff --git a/MiraAPI/PluginLoading/MiraPluginManager.cs b/MiraAPI/PluginLoading/MiraPluginManager.cs
index d0bcaa14..737ee2eb 100644
--- a/MiraAPI/PluginLoading/MiraPluginManager.cs
+++ b/MiraAPI/PluginLoading/MiraPluginManager.cs
@@ -49,6 +49,16 @@ public sealed class MiraPluginManager
internal static ConfigFile MiraApiConfig { get; private set; } = null!;
internal static MiraPluginInfo MiraApiPluginInfo { get; private set; } = null!;
+ ///
+ /// Get a mira plugin by its GUID.
+ ///
+ /// The plugin GUID.
+ /// A .
+ public static MiraPluginInfo? GetPluginByGuid(string pluginId)
+ {
+ return Instance._registeredPlugins.GetValueOrDefault(pluginId);
+ }
+
internal void Initialize(BasePlugin miraApiPlugin, IMiraPlugin apiPlugin)
{
CustomGameModeManager.RegisterDefaultMode();
@@ -130,12 +140,17 @@ public sealed class MiraPluginManager
[SuppressMessage("Minor Code Smell", "S3626:Jump statements should not be redundant", Justification = "Code consistency.")]
private static void ProcessAssemblyTypes(Assembly assembly, MiraPluginInfo info, BasePlugin basePlugin, IMiraPlugin plugin, List roles, bool isMira)
{
+ if (plugin.ForceRegisterEvents)
+ {
+ Warning("For optimisation reasons, MiraAPI no longer automatically searches every type for events. Please use RegisterEventsAttribute moving forward.");
+ }
+
foreach (var type in assembly.GetTypes())
{
if ((isMira && type.IsAbstract) || type.IsDefined())
continue;
- RegisterEvents(type);
+ RegisterEvents(type, plugin);
if (RegisterOptions(type, info))
continue;
@@ -172,11 +187,11 @@ public sealed class MiraPluginManager
}
}
- private static void RegisterEvents(Type type)
+ private static void RegisterEvents(Type type, IMiraPlugin miraPlugin)
{
try
{
- MiraEventManager.RegisterEvents(type);
+ MiraEventManager.RegisterEvents(type, miraPlugin);
}
catch (Exception e)
{
@@ -184,16 +199,6 @@ public sealed class MiraPluginManager
}
}
- ///
- /// Get a mira plugin by its GUID.
- ///
- /// The plugin GUID.
- /// A .
- public static MiraPluginInfo? GetPluginByGuid(string pluginId)
- {
- return Instance._registeredPlugins.GetValueOrDefault(pluginId);
- }
-
private static bool RegisterGameOver(Type type)
{
try
diff --git a/MiraAPI/VanillaEvents/JudgeEvents.cs b/MiraAPI/VanillaEvents/JudgeEvents.cs
index 6cfd5263..5a4b9fcd 100644
--- a/MiraAPI/VanillaEvents/JudgeEvents.cs
+++ b/MiraAPI/VanillaEvents/JudgeEvents.cs
@@ -4,15 +4,11 @@ using MiraAPI.Roles;
namespace MiraAPI.VanillaEvents;
+[RegisterEventContainer]
internal static class JudgeEvents
{
- public static void Initialize()
- {
- // This is required because MiraAPI isn't an IMiraPlugin (besides in the gamemodes branch)
- MiraEventManager.RegisterEventHandler(ProcessVotesEventHandler, -1000);
- }
-
- private static void ProcessVotesEventHandler(ProcessVotesEvent @event)
+ [RegisterEvent(-1000)]
+ public static void ProcessVotesEventHandler(ProcessVotesEvent @event)
{
if (!MeetingHud.Instance.TryGetWinningOverrule(
out var judgeOverrule,
diff --git a/MiraAPI/VanillaEvents/UiResetEvents.cs b/MiraAPI/VanillaEvents/UiResetEvents.cs
index 84d00225..37574f1e 100644
--- a/MiraAPI/VanillaEvents/UiResetEvents.cs
+++ b/MiraAPI/VanillaEvents/UiResetEvents.cs
@@ -5,17 +5,11 @@ using MiraAPI.LocalSettings;
namespace MiraAPI.VanillaEvents;
+[RegisterEventContainer]
internal static class UiResetEvents
{
- public static void Initialize()
- {
- MiraEventManager.RegisterEventHandler(ResetButtonParents);
- MiraEventManager.RegisterEventHandler(PlaceWikiButton, -900);
- MiraEventManager.RegisterEventHandler(PlaceSubmergedButton, -800);
- MiraEventManager.RegisterEventHandler(PlaceModifierUi, -700);
- }
-
- private static void ResetButtonParents(UiButtonResetEvent _)
+ [RegisterEvent]
+ public static void ResetButtonParents(UiButtonResetEvent _)
{
var wikiButton = MiraHudHelper.VanillaMatchInfoButton;
var subButton = MiraHudHelper.SubmergedFloorButton;
@@ -36,7 +30,8 @@ internal static class UiResetEvents
}
}
- private static void PlaceWikiButton(UiButtonPostResetEvent @event)
+ [RegisterEvent(-900)]
+ public static void PlaceWikiButton(UiButtonPostResetEvent @event)
{
var wikiButton = MiraHudHelper.VanillaMatchInfoButton;
if (!wikiButton)
@@ -50,7 +45,8 @@ internal static class UiResetEvents
wikiButton.transform.SetParent(opts.WikiOnBottomRow.Value ? secondRow.transform : firstRow.transform);
}
- private static void PlaceSubmergedButton(UiButtonPostResetEvent @event)
+ [RegisterEvent(-800)]
+ public static void PlaceSubmergedButton(UiButtonPostResetEvent @event)
{
var subButton = MiraHudHelper.SubmergedFloorButton;
if (!subButton)
@@ -62,7 +58,8 @@ internal static class UiResetEvents
subButton.transform.SetParent(secondRow.transform);
}
- private static void PlaceModifierUi(UiButtonPostResetEvent @event)
+ [RegisterEvent(-700)]
+ public static void PlaceModifierUi(UiButtonPostResetEvent @event)
{
var modDisplay = MiraHudHelper.ModifierDisplayOnRight ? MiraHudHelper.ModifierDisplayObject : null!;
if (!modDisplay)