diff --git a/.editorconfig b/.editorconfig index a1b1ab37..d5fe079c 100644 --- a/.editorconfig +++ b/.editorconfig @@ -119,3 +119,6 @@ dotnet_diagnostic.SA1114.severity = none # SA1143: Conditional expressions must be formatted consistently dotnet_diagnostic.SA1143.severity = none + +# SA1504: All accessors should be single-line or multi-line +dotnet_diagnostic.SA1504.severity = none diff --git a/MiraAPI/Colors/PaletteManager.cs b/MiraAPI/Colors/PaletteManager.cs index 837e6bc2..c5cba43f 100644 --- a/MiraAPI/Colors/PaletteManager.cs +++ b/MiraAPI/Colors/PaletteManager.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using HarmonyLib; @@ -46,8 +45,11 @@ public static class PaletteManager private static int originalLength; - internal static bool RegisterColors(Type type, MiraPluginInfo miraPlugin) + internal static bool RegisterColors(in MiraPluginManager.RegistrationContext context) { + var type = context.Type; + var miraPlugin = context.MiraPlugin; + if (!type.TryGetCustomAttribute(out var attribute)) { return false; diff --git a/MiraAPI/Events/MiraEventManager.cs b/MiraAPI/Events/MiraEventManager.cs index 299a4b8d..a19bda9f 100644 --- a/MiraAPI/Events/MiraEventManager.cs +++ b/MiraAPI/Events/MiraEventManager.cs @@ -156,11 +156,16 @@ public static class MiraEventManager return true; } - internal static void RegisterEvents(Type type, IMiraPlugin miraPlugin) + internal static bool RegisterEvents(in MiraPluginManager.RegistrationContext context) { - if (!miraPlugin.ForceRegisterEvents && !type.IsDefined()) + var type = context.Type; + var miraPlugin = context.MiraPlugin; + + var hasContainerAttribute = type.IsDefined(); + + if (!miraPlugin.ForceRegisterEvents && !hasContainerAttribute) { - return; + return false; } foreach (var method in type.GetMethods(MiraPluginManager.DeclaredStaticMembers)) @@ -179,5 +184,7 @@ public static class MiraEventManager var paramType = parameters[0].ParameterType; RegisterEventHandler(paramType, method, eventAttribute.Priority); } + + return hasContainerAttribute; } } diff --git a/MiraAPI/GameEnd/GameOverManager.cs b/MiraAPI/GameEnd/GameOverManager.cs index 0a6288d8..e86c2c7a 100644 --- a/MiraAPI/GameEnd/GameOverManager.cs +++ b/MiraAPI/GameEnd/GameOverManager.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using MiraAPI.PluginLoading; namespace MiraAPI.GameEnd; @@ -14,26 +15,28 @@ public static class GameOverManager private static int nextId = Enum.GetNames().Length; - internal static bool RegisterGameOver(Type gameOverType) + internal static bool RegisterGameOver(in MiraPluginManager.RegistrationContext context) { - if (!typeof(CustomGameOver).IsAssignableFrom(gameOverType)) + var type = context.Type; + + if (!typeof(CustomGameOver).IsAssignableFrom(type)) { return false; } - if (gameOverType.IsAbstract) + if (type.IsAbstract) { return false; } - if (gameOverType.GetConstructor(Type.EmptyTypes) == null) + if (type.GetConstructor(Type.EmptyTypes) == null) { Error("The type must have a parameterless constructor."); return false; } - GameOverIds.Add(gameOverType, nextId); - GameOverTypes.Add(nextId, gameOverType); + GameOverIds.Add(type, nextId); + GameOverTypes.Add(nextId, type); nextId++; return true; } diff --git a/MiraAPI/GameModes/CustomGameModeManager.cs b/MiraAPI/GameModes/CustomGameModeManager.cs index b6e90e39..11f8fd6c 100644 --- a/MiraAPI/GameModes/CustomGameModeManager.cs +++ b/MiraAPI/GameModes/CustomGameModeManager.cs @@ -19,16 +19,19 @@ public static class CustomGameModeManager return ++lastId; } - internal static bool RegisterGameMode(Type gameModeType, MiraPluginInfo pluginInfo) + internal static bool RegisterGameMode(in MiraPluginManager.RegistrationContext context) { - if (!typeof(AbstractGameMode).IsAssignableFrom(gameModeType)) + var type = context.Type; + var pluginInfo = context.PluginInfo; + + if (!typeof(AbstractGameMode).IsAssignableFrom(type)) { return false; } - if (Activator.CreateInstance(gameModeType) is not AbstractGameMode mode) + if (Activator.CreateInstance(type) is not AbstractGameMode mode) { - Error($"Failed to create instance of {gameModeType.Name}"); + Error($"Failed to create instance of {type.Name}"); return false; } diff --git a/MiraAPI/GameOptions/ModdedOptionsManager.cs b/MiraAPI/GameOptions/ModdedOptionsManager.cs index 3153cc21..8071299f 100644 --- a/MiraAPI/GameOptions/ModdedOptionsManager.cs +++ b/MiraAPI/GameOptions/ModdedOptionsManager.cs @@ -70,8 +70,11 @@ public static class ModdedOptionsManager "Major Code Smell", "S3011:Reflection should not be used to increase accessibility of classes, methods, or fields", Justification = "Dynamic singleton initialization requires reflection to bypass the private field access modifier because the type is only known at runtime.")] - internal static bool RegisterGroup(Type type, MiraPluginInfo pluginInfo) + internal static bool RegisterGroup(in MiraPluginManager.RegistrationContext context) { + var type = context.Type; + var pluginInfo = context.PluginInfo; + if (!type.IsAssignableTo(typeof(AbstractOptionGroup))) { return false; diff --git a/MiraAPI/Hud/CustomButtonManager.cs b/MiraAPI/Hud/CustomButtonManager.cs index 1e8656ed..36e4da66 100644 --- a/MiraAPI/Hud/CustomButtonManager.cs +++ b/MiraAPI/Hud/CustomButtonManager.cs @@ -16,17 +16,17 @@ public static class CustomButtonManager /// /// Gets a list of all registered custom s. /// - public static ReadOnlyCollection Buttons { get; internal set; } = new([]); + public static ReadOnlyCollection Buttons { get; private set; } = new([]); /// /// Gets a list of all registered button-specific s. /// - public static ReadOnlyDictionary EventTypes { get; internal set; } = new(new Dictionary()); + public static ReadOnlyDictionary EventTypes { get; private set; } = new(new Dictionary()); /// /// Gets a list of all registered button-specific s. /// - public static ReadOnlyDictionary CancelledEventTypes { get; internal set; } = new(new Dictionary()); + public static ReadOnlyDictionary CancelledEventTypes { get; private set; } = new(new Dictionary()); internal static readonly List CustomButtons = []; internal static readonly Dictionary ButtonEventTypes = []; @@ -36,9 +36,12 @@ public static class CustomButtonManager "Major Code Smell", "S3011:Reflection should not be used to increase accessibility of classes, methods, or fields", Justification = "Dynamic singleton initialization requires reflection to bypass the private field access modifier because the type is only known at runtime.")] - internal static bool RegisterButton(Type buttonType, MiraPluginInfo pluginInfo) + internal static bool RegisterButton(in MiraPluginManager.RegistrationContext context) { - if (!buttonType.IsAssignableTo(typeof(CustomActionButton)) || Activator.CreateInstance(buttonType) is not CustomActionButton button) + var type = context.Type; + var pluginInfo = context.PluginInfo; + + if (!type.IsAssignableTo(typeof(CustomActionButton)) || Activator.CreateInstance(type) is not CustomActionButton button) { return false; } @@ -46,13 +49,21 @@ public static class CustomButtonManager CustomButtons.Add(button); pluginInfo.InternalButtons.Add(button); - typeof(CustomButtonSingleton<>).MakeGenericType(buttonType) + typeof(CustomButtonSingleton<>).MakeGenericType(type) .GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic)! // Suppression message refers to this .SetValue(null, button); - ButtonEventTypes.Add(buttonType, typeof(MiraButtonClickEvent<>).MakeGenericType(buttonType)); - ButtonCancelledEventTypes.Add(buttonType, typeof(MiraButtonCancelledEvent<>).MakeGenericType(buttonType)); + ButtonEventTypes.Add(type, typeof(MiraButtonClickEvent<>).MakeGenericType(type)); + ButtonCancelledEventTypes.Add(type, typeof(MiraButtonCancelledEvent<>).MakeGenericType(type)); return true; } + + internal static void RegisterAllButtons() + { + // Save all buttons into a read-only collection for easy access + Buttons = new ReadOnlyCollection(CustomButtonManager.CustomButtons); + EventTypes = new ReadOnlyDictionary(CustomButtonManager.ButtonEventTypes); + CancelledEventTypes = new ReadOnlyDictionary(CustomButtonManager.ButtonCancelledEventTypes); + } } diff --git a/MiraAPI/Keybinds/KeybindManager.cs b/MiraAPI/Keybinds/KeybindManager.cs index 2f0287d2..cffe9b35 100644 --- a/MiraAPI/Keybinds/KeybindManager.cs +++ b/MiraAPI/Keybinds/KeybindManager.cs @@ -78,8 +78,11 @@ public static class KeybindManager } } - internal static bool RegisterKeybinds(Type type, IMiraPlugin plugin) + internal static bool RegisterKeybinds(in MiraPluginManager.RegistrationContext context) { + var type = context.Type; + var miraPlugin = context.MiraPlugin; + if (!type.IsDefined()) { return false; @@ -87,7 +90,7 @@ public static class KeybindManager foreach (var property in type.GetProperties(MiraPluginManager.DeclaredStaticMembers)) { - if (property.PropertyType != typeof(MiraKeybind)) + if (!property.PropertyType.IsAssignableTo(typeof(MiraKeybind))) { continue; } @@ -99,10 +102,10 @@ public static class KeybindManager } Keybinds.Add(keybind); - keybind.SourcePluginName = plugin.OptionsTitleText.Translate(); + keybind.SourcePluginName = miraPlugin.OptionsTitleText.Translate(); } - if (!plugin.DebugMode) + if (!miraPlugin.DebugMode) return true; foreach (var field in type.GetFields(MiraPluginManager.DeclaredStaticMembers) diff --git a/MiraAPI/LocalSettings/LocalSettingsManager.cs b/MiraAPI/LocalSettings/LocalSettingsManager.cs index 44d77681..646a2664 100644 --- a/MiraAPI/LocalSettings/LocalSettingsManager.cs +++ b/MiraAPI/LocalSettings/LocalSettingsManager.cs @@ -4,7 +4,6 @@ using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection; using BepInEx.Configuration; -using BepInEx.Unity.IL2CPP; using MiraAPI.LocalSettings.Attributes; using MiraAPI.PluginLoading; using MiraAPI.Utilities; @@ -24,8 +23,11 @@ public static class LocalSettingsManager "Major Code Smell", "S3011:Reflection should not be used to increase accessibility of classes, methods, or fields", Justification = "Dynamic singleton initialization requires reflection to bypass the private field access modifier because the type is only known at runtime.")] - internal static bool RegisterLocalTabs(Type type, BasePlugin pluginInfo) + internal static bool RegisterLocalTabs(in MiraPluginManager.RegistrationContext context) { + var type = context.Type; + var basePlugin = context.BasePlugin; + if (!type.IsAssignableTo(typeof(LocalSettingsTab))) { return false; @@ -37,7 +39,7 @@ public static class LocalSettingsManager return false; } - if (Activator.CreateInstance(type, pluginInfo.Config) is not LocalSettingsTab tabInstance) + if (Activator.CreateInstance(type, basePlugin.Config) is not LocalSettingsTab tabInstance) { return false; } diff --git a/MiraAPI/MeetingAbilities/MeetingButtonManager.cs b/MiraAPI/MeetingAbilities/MeetingButtonManager.cs index 92348f5d..f93aa7c6 100644 --- a/MiraAPI/MeetingAbilities/MeetingButtonManager.cs +++ b/MiraAPI/MeetingAbilities/MeetingButtonManager.cs @@ -22,28 +22,34 @@ public static class MeetingButtonManager /// public static List UntargetedButtons { get; internal set; } = []; - internal static bool RegisterTargetedMeetingButton(Type type, MiraPluginInfo info) + internal static bool RegisterTargetedMeetingButton(in MiraPluginManager.RegistrationContext context) { + var type = context.Type; + var pluginInfo = context.PluginInfo; + if (!typeof(TargetedMeetingButton).IsAssignableFrom(type)) { return false; } var button = (Activator.CreateInstance(type) as TargetedMeetingButton)!; - info.InternalTargetedMeetingButtons.Add(button); + pluginInfo.InternalTargetedMeetingButtons.Add(button); TargetedButtons.Add(button); return true; } - internal static bool RegisterMeetingButton(Type type, MiraPluginInfo info) + internal static bool RegisterMeetingButton(in MiraPluginManager.RegistrationContext context) { + var type = context.Type; + var pluginInfo = context.PluginInfo; + if (!typeof(MeetingActionButton).IsAssignableFrom(type)) { return false; } var button = (Activator.CreateInstance(type) as MeetingActionButton)!; - info.InternalMeetingButtons.Add(button); + pluginInfo.InternalMeetingButtons.Add(button); UntargetedButtons.Add(button); return true; } diff --git a/MiraAPI/Modifiers/ModifierManager.cs b/MiraAPI/Modifiers/ModifierManager.cs index aa6c5120..d22edb67 100644 --- a/MiraAPI/Modifiers/ModifierManager.cs +++ b/MiraAPI/Modifiers/ModifierManager.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using System.Runtime.Serialization; using MiraAPI.Modifiers.Types; @@ -25,7 +26,7 @@ public static class ModifierManager /// /// Gets the list of all registered s. /// - public static IReadOnlyList Modifiers { get; internal set; } = []; + public static IReadOnlyList Modifiers { get; private set; } = []; internal static List InternalModifiers { get; } = []; @@ -61,31 +62,39 @@ public static class ModifierManager return !TypeToIdModifierMap.TryGetValue(type, out var id) ? null : id; } - internal static bool RegisterModifier(Type modifierType, MiraPluginInfo info) + internal static void RegisterAllModifiers() { - if (!typeof(BaseModifier).IsAssignableFrom(modifierType)) + Modifiers = new ReadOnlyCollection(InternalModifiers); + } + + internal static bool RegisterModifier(in MiraPluginManager.RegistrationContext context) + { + var type = context.Type; + var pluginInfo = context.PluginInfo; + + if (!typeof(BaseModifier).IsAssignableFrom(type)) { return false; } var id = GetNextTypeId(); - IdToTypeModifierMap.Add(id, modifierType); - TypeToIdModifierMap.Add(modifierType, id); + IdToTypeModifierMap.Add(id, type); + TypeToIdModifierMap.Add(type, id); BaseModifier modifier; - var ctor = modifierType.GetConstructor(Type.EmptyTypes); + var ctor = type.GetConstructor(Type.EmptyTypes); if (ctor != null) { - modifier = (BaseModifier)Activator.CreateInstance(modifierType)!; + modifier = (BaseModifier)Activator.CreateInstance(type)!; } else { // this is probably terrible but its good enough for now. - modifier = (BaseModifier)FormatterServices.GetUninitializedObject(modifierType); + modifier = (BaseModifier)FormatterServices.GetUninitializedObject(type); } - info.InternalModifiers.Add(modifier); + pluginInfo.InternalModifiers.Add(modifier); InternalModifiers.Add(modifier); if (modifier is not GameModifier gameModifier) @@ -95,7 +104,7 @@ public static class ModifierManager if (ctor == null) { - Error($"Game Modifier {modifierType.FullName} does not have a parameterless constructor!"); + Error($"Game Modifier {type.FullName} does not have a parameterless constructor!"); return false; } diff --git a/MiraAPI/PluginLoading/MiraPluginManager.cs b/MiraAPI/PluginLoading/MiraPluginManager.cs index 737ee2eb..a1869c93 100644 --- a/MiraAPI/PluginLoading/MiraPluginManager.cs +++ b/MiraAPI/PluginLoading/MiraPluginManager.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Collections.ObjectModel; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Linq; @@ -31,6 +30,10 @@ namespace MiraAPI.PluginLoading; [SuppressMessage("Major Code Smell", "S3011:Reflection should not be used to increase accessibility of classes, methods, or fields", Justification = "Intended behaviour.")] public sealed class MiraPluginManager { + internal delegate bool TypeRegistrationHandler(in RegistrationContext context); + + internal readonly record struct RegistrationContext(Type Type, MiraPluginInfo PluginInfo, IMiraPlugin MiraPlugin, BasePlugin BasePlugin, List Roles); + private MiraPluginManager() { } @@ -49,6 +52,22 @@ public sealed class MiraPluginManager internal static ConfigFile MiraApiConfig { get; private set; } = null!; internal static MiraPluginInfo MiraApiPluginInfo { get; private set; } = null!; + private static readonly (string LogContext, TypeRegistrationHandler Handler)[] RegistrationHandlers = + [ + ("events in", MiraEventManager.RegisterEvents), + ("options for", ModdedOptionsManager.RegisterGroup), + ("options for", LocalSettingsManager.RegisterLocalTabs), + ("color class", PaletteManager.RegisterColors), + ("role for", CustomRoleManager.RegisterRole), + ("modifier", ModifierManager.RegisterModifier), + ("button", CustomButtonManager.RegisterButton), + ("keybind class", KeybindManager.RegisterKeybinds), + ("gamemode", CustomGameModeManager.RegisterGameMode), + ("game over", GameOverManager.RegisterGameOver), + ("meeting ability", MeetingButtonManager.RegisterMeetingButton), + ("targeted meeting ability", MeetingButtonManager.RegisterTargetedMeetingButton), + ]; + /// /// Get a mira plugin by its GUID. /// @@ -92,18 +111,13 @@ public sealed class MiraPluginManager IL2CPPChainloader.Instance.Finished += () => { PaletteManager.RegisterAllColors(); - - // Save all buttons into a read-only collection for easy access - CustomButtonManager.Buttons = new ReadOnlyCollection(CustomButtonManager.CustomButtons); - CustomButtonManager.EventTypes = new ReadOnlyDictionary(CustomButtonManager.ButtonEventTypes); - CustomButtonManager.CancelledEventTypes = new ReadOnlyDictionary(CustomButtonManager.ButtonCancelledEventTypes); + CustomButtonManager.RegisterAllButtons(); + ModifierManager.RegisterAllModifiers(); // Cache all the registered plugins into an array for easy access RegisteredPlugins = [.. _registeredPlugins.Values]; RegisteredPluginsWithOptions = [.. RegisteredPlugins.Where(m => m.MiraPlugin.DisplayOnOptionsMenu)]; PluginsWithOptionsOrGameModes = [.. RegisteredPlugins.Where(m => m.MiraPlugin.DisplayOnOptionsMenu || m.GameModes.Count > 0)]; - - ModifierManager.Modifiers = new ReadOnlyCollection(ModifierManager.InternalModifiers); }; } @@ -137,12 +151,11 @@ public sealed class MiraPluginManager field.Name.Contains(">k__BackingField", StringComparison.Ordinal); } - [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."); + Warning("For optimisation reasons, MiraAPI no longer automatically searches every type for events. Please use RegisterEventContainerAttribute moving forward."); } foreach (var type in assembly.GetTypes()) @@ -150,205 +163,20 @@ public sealed class MiraPluginManager if ((isMira && type.IsAbstract) || type.IsDefined()) continue; - RegisterEvents(type, plugin); + var context = new RegistrationContext(type, info, plugin, basePlugin, roles); - if (RegisterOptions(type, info)) - continue; - - if (RegisterLocalTabs(type, basePlugin)) - continue; - - if (RegisterColors(type, info)) - continue; - - if (RegisterRole(type, info, roles)) - continue; - - if (RegisterModifier(type, info)) - continue; - - if (RegisterButton(type, info)) - continue; - - if (RegisterKeybinds(type, plugin)) - continue; - - if (RegisterGameModeAttribute(type, info)) - continue; - - if (RegisterGameOver(type)) - continue; - - if (RegisterMeetingAbility(type, info)) - continue; - - if (RegisterTargetedMeetingAbility(type, info)) - continue; + foreach (var (logContext, handler) in RegistrationHandlers) + { + try + { + if (handler(in context)) + break; + } + catch (Exception e) + { + Error($"Failed to register {logContext} {type.Name}: {e}"); + } + } } } - - private static void RegisterEvents(Type type, IMiraPlugin miraPlugin) - { - try - { - MiraEventManager.RegisterEvents(type, miraPlugin); - } - catch (Exception e) - { - Error($"Failed to register events in {type.Name}: {e}"); - } - } - - private static bool RegisterGameOver(Type type) - { - try - { - return GameOverManager.RegisterGameOver(type); - } - catch (Exception e) - { - Error($"Failed to register game over {type.Name}: {e}"); - return false; - } - } - - private static bool RegisterOptions(Type type, MiraPluginInfo pluginInfo) - { - try - { - return ModdedOptionsManager.RegisterGroup(type, pluginInfo); - } - catch (Exception e) - { - Error($"Failed to register options for {type.Name}: {e}"); - } - - return false; - } - - private static bool RegisterRole(Type type, MiraPluginInfo pluginInfo, List roles) - { - try - { - return CustomRoleManager.RegisterRole(type, pluginInfo, roles); - } - catch (Exception e) - { - Error($"Failed to register role for {type.Name}: {e}"); - } - - return false; - } - - private static bool RegisterColors(Type type, MiraPluginInfo miraPlugin) - { - try - { - return PaletteManager.RegisterColors(type, miraPlugin); - } - catch (Exception e) - { - Error($"Failed to register color class {type.Name}: {e}"); - } - - return false; - } - - private static bool RegisterModifier(Type type, MiraPluginInfo info) - { - try - { - return ModifierManager.RegisterModifier(type, info); - } - catch (Exception e) - { - Error($"Failed to register modifier {type.Name}: {e}"); - } - - return false; - } - - private static bool RegisterButton(Type type, MiraPluginInfo pluginInfo) - { - try - { - return CustomButtonManager.RegisterButton(type, pluginInfo); - } - catch (Exception e) - { - Error($"Failed to register button {type.Name}: {e}"); - } - - return false; - } - - private static bool RegisterGameModeAttribute(Type type, MiraPluginInfo pluginInfo) - { - try - { - return CustomGameModeManager.RegisterGameMode(type, pluginInfo); - } - catch (Exception e) - { - Error($"Failed to register gamemode {type.Name}: {e}"); - } - - return false; - } - - private static bool RegisterLocalTabs(Type type, BasePlugin pluginInfo) - { - try - { - return LocalSettingsManager.RegisterLocalTabs(type, pluginInfo); - } - catch (Exception e) - { - Error($"Failed to register options for {type.Name}: {e}"); - } - - return false; - } - - private static bool RegisterKeybinds(Type type, IMiraPlugin plugin) - { - try - { - return KeybindManager.RegisterKeybinds(type, plugin); - } - catch (Exception e) - { - Error($"Failed to register keybind class {type.Name}: {e}"); - } - - return false; - } - - private static bool RegisterMeetingAbility(Type type, MiraPluginInfo info) - { - try - { - return MeetingButtonManager.RegisterMeetingButton(type, info); - } - catch (Exception e) - { - Error($"Failed to register meeting ability {type.Name}: {e}"); - } - - return false; - } - - private static bool RegisterTargetedMeetingAbility(Type type, MiraPluginInfo info) - { - try - { - return MeetingButtonManager.RegisterTargetedMeetingButton(type, info); - } - catch (Exception e) - { - Error($"Failed to register targeted meeting ability {type.Name}: {e}"); - } - - return false; - } } diff --git a/MiraAPI/Roles/CustomRoleManager.cs b/MiraAPI/Roles/CustomRoleManager.cs index a2a0164f..e836d23e 100644 --- a/MiraAPI/Roles/CustomRoleManager.cs +++ b/MiraAPI/Roles/CustomRoleManager.cs @@ -119,14 +119,18 @@ public static class CustomRoleManager pluginInfo.PluginConfig.SaveOnConfigSet = oldConfigSetting; } - internal static bool RegisterRole(Type type, MiraPluginInfo miraPlugin, List roles) + internal static bool RegisterRole(in MiraPluginManager.RegistrationContext context) { + var type = context.Type; + var pluginInfo = context.PluginInfo; + var roles = context.Roles; + if (!(typeof(RoleBehaviour).IsAssignableFrom(type) && typeof(ICustomRole).IsAssignableFrom(type))) { return false; } - if (!ModList.GetById(miraPlugin.PluginId).IsRequiredOnAllClients) + if (!ModList.GetById(pluginInfo.PluginId).IsRequiredOnAllClients) { Error("Custom roles are only supported on all clients."); return false; @@ -144,7 +148,7 @@ public static class CustomRoleManager return null; } - var roleBehaviour = (RoleBehaviour)new GameObject(roleType.Name).DontDestroy().AddComponent(Il2CppType.From(roleType)); + var roleBehaviour = new GameObject(roleType.Name).DontDestroy().AddComponent(Il2CppType.From(roleType)).Cast(); if (roleBehaviour is not ICustomRole customRole) {