diff --git a/MiraAPI/PluginLoading/MiraPluginManager.cs b/MiraAPI/PluginLoading/MiraPluginManager.cs index acb6ebe..8dc0f17 100644 --- a/MiraAPI/PluginLoading/MiraPluginManager.cs +++ b/MiraAPI/PluginLoading/MiraPluginManager.cs @@ -38,17 +38,26 @@ public sealed class MiraPluginManager } var info = new MiraPluginInfo(miraPlugin, pluginInfo); + var roles = new List(); - RegisterModifierAttribute(assembly); - RegisterAllOptions(assembly, info); + foreach (var type in assembly.GetTypes()) + { + RegisterModifierAttribute(type); + RegisterOptions(type, info); - RegisterRoleAttribute(assembly, info); - RegisterButtonAttribute(assembly, info); + if (RegisterRoleAttribute(type, info, out var role)) + { + roles.Add(role!); + } - RegisterColorClasses(assembly); + RegisterButtonAttribute(type, info); + RegisterColorClasses(type); + } + + info.OptionGroups.Sort((x, y) => x.GroupPriority.CompareTo(y.GroupPriority)); + CustomRoleManager.RegisterRoleTypes(roles, info); _registeredPlugins.Add(assembly, info); - Logger.Info($"Registering mod {pluginInfo.Metadata.GUID} with Mira API."); }; IL2CPPChainloader.Instance.Finished += PaletteManager.RegisterAllColors; @@ -65,121 +74,107 @@ public sealed class MiraPluginManager return Instance._registeredPlugins.Values.First(plugin => plugin.PluginId == guid); } - private static void RegisterAllOptions(Assembly assembly, MiraPluginInfo pluginInfo) + private static void RegisterOptions(Type type, MiraPluginInfo pluginInfo) { - var filteredTypes = assembly.GetTypes().Where(type => type.IsAssignableTo(typeof(AbstractOptionGroup))); - - foreach (var type in filteredTypes) + if (!type.IsAssignableTo(typeof(AbstractOptionGroup))) { - if (!ModdedOptionsManager.RegisterGroup(type, pluginInfo)) + return; + } + + if (!ModdedOptionsManager.RegisterGroup(type, pluginInfo)) + { + return; + } + + foreach (var property in type.GetProperties()) + { + if (property.PropertyType.IsAssignableTo(typeof(IModdedOption))) { + ModdedOptionsManager.RegisterPropertyOption(type, property, pluginInfo); continue; } - foreach (var property in type.GetProperties()) - { - if (property.PropertyType.IsAssignableTo(typeof(IModdedOption))) - { - ModdedOptionsManager.RegisterPropertyOption(type, property, pluginInfo); - continue; - } - - var attribute = property.GetCustomAttribute(); - if (attribute == null) - { - continue; - } - - ModdedOptionsManager.RegisterAttributeOption(type, attribute, property, pluginInfo); - } - } - - pluginInfo.OptionGroups.Sort((x, y) => x.GroupPriority.CompareTo(y.GroupPriority)); - } - - private static void RegisterRoleAttribute(Assembly assembly, MiraPluginInfo pluginInfo) - { - List roles = []; - foreach (var type in assembly.GetTypes()) - { - var attribute = type.GetCustomAttribute(); + var attribute = property.GetCustomAttribute(); if (attribute == null) { continue; } - if (!(typeof(RoleBehaviour).IsAssignableFrom(type) && typeof(ICustomRole).IsAssignableFrom(type))) - { - Logger.Error($"{type.Name} does not inherit from RoleBehaviour or ICustomRole."); - continue; - } - - if (!ModList.GetById(pluginInfo.PluginId).IsRequiredOnAllClients) - { - Logger.Error("Custom roles are only supported on all clients."); - return; - } - - roles.Add(type); + ModdedOptionsManager.RegisterAttributeOption(type, attribute, property, pluginInfo); } - - CustomRoleManager.RegisterRoleTypes(roles, pluginInfo); } - private static void RegisterColorClasses(Assembly assembly) + private static bool RegisterRoleAttribute(Type type, MiraPluginInfo pluginInfo, out Type? role) { - foreach (var type in assembly.GetTypes()) + role = null; + + var attribute = type.GetCustomAttribute(); + if (attribute == null) { - if (type.GetCustomAttribute() == null) + return false; + } + + if (!(typeof(RoleBehaviour).IsAssignableFrom(type) && typeof(ICustomRole).IsAssignableFrom(type))) + { + Logger.Error($"{type.Name} does not inherit from RoleBehaviour or ICustomRole."); + return false; + } + + if (!ModList.GetById(pluginInfo.PluginId).IsRequiredOnAllClients) + { + Logger.Error("Custom roles are only supported on all clients."); + return false; + } + + role = type; + return true; + } + + private static void RegisterColorClasses(Type type) + { + if (type.GetCustomAttribute() == null) + { + return; + } + + if (!type.IsStatic()) + { + Logger.Error($"Color class {type.Name} must be static."); + return; + } + + foreach (var property in type.GetProperties()) + { + if (property.PropertyType != typeof(CustomColor)) { continue; } - if (!type.IsStatic()) + if (property.GetValue(null) is not CustomColor color) { - Logger.Error($"Color class {type.Name} must be static."); + Logger.Error($"Color property {property.Name} in {type.Name} is not a CustomColor."); continue; } - foreach (var property in type.GetProperties()) - { - if (property.PropertyType != typeof(CustomColor)) - { - continue; - } - - if (property.GetValue(null) is not CustomColor color) - { - Logger.Error($"Color property {property.Name} in {type.Name} is not a CustomColor."); - continue; - } - - PaletteManager.CustomColors.Add(color); - } + PaletteManager.CustomColors.Add(color); } } - private static void RegisterModifierAttribute(Assembly assembly) + private static void RegisterModifierAttribute(Type type) { - foreach (var type in assembly.GetTypes()) + var attribute = type.GetCustomAttribute(); + if (attribute != null) { - var attribute = type.GetCustomAttribute(); - if (attribute != null) - { - ModifierManager.RegisterModifier(type); - } + ModifierManager.RegisterModifier(type); } } - private static void RegisterButtonAttribute(Assembly assembly, MiraPluginInfo pluginInfo) + private static void RegisterButtonAttribute(Type type, MiraPluginInfo pluginInfo) { - foreach (var type in assembly.GetTypes()) + var attribute = type.GetCustomAttribute(); + if (attribute != null) { - var attribute = type.GetCustomAttribute(); - if (attribute != null) - { - CustomButtonManager.RegisterButton(type, pluginInfo); - } + CustomButtonManager.RegisterButton(type, pluginInfo); } } }