From 59ab2736abd4b96edcd8bfe2215d1f841bbdffce Mon Sep 17 00:00:00 2001 From: XtraCube <72575280+XtraCube@users.noreply.github.com> Date: Thu, 13 Feb 2025 16:19:12 -0500 Subject: [PATCH] further optimization? --- MiraAPI/GameOptions/ModdedOptionsManager.cs | 1 - MiraAPI/Hud/CustomButtonManager.cs | 15 +- MiraAPI/Modifiers/ModifierManager.cs | 7 +- MiraAPI/PluginLoading/MiraPluginManager.cs | 165 +++++++++++++------- 4 files changed, 115 insertions(+), 73 deletions(-) diff --git a/MiraAPI/GameOptions/ModdedOptionsManager.cs b/MiraAPI/GameOptions/ModdedOptionsManager.cs index b6c42bb..a6dd1c9 100644 --- a/MiraAPI/GameOptions/ModdedOptionsManager.cs +++ b/MiraAPI/GameOptions/ModdedOptionsManager.cs @@ -31,7 +31,6 @@ public static class ModdedOptionsManager { if (Activator.CreateInstance(type) is not AbstractOptionGroup group) { - Logger.Error($"Skipping option group {type.Name}. Does not inherit AbstractOptionGroup."); return false; } diff --git a/MiraAPI/Hud/CustomButtonManager.cs b/MiraAPI/Hud/CustomButtonManager.cs index c66ae4d..60f2fc6 100644 --- a/MiraAPI/Hud/CustomButtonManager.cs +++ b/MiraAPI/Hud/CustomButtonManager.cs @@ -16,18 +16,11 @@ public static class CustomButtonManager internal static readonly Dictionary ButtonEventTypes = []; internal static readonly Dictionary ButtonCancelledEventTypes = []; - internal static void RegisterButton(Type buttonType, MiraPluginInfo pluginInfo) + internal static bool RegisterButton(Type buttonType, MiraPluginInfo pluginInfo) { - if (!typeof(CustomActionButton).IsAssignableFrom(buttonType)) + if (!buttonType.IsAssignableTo(typeof(CustomActionButton)) || Activator.CreateInstance(buttonType) is not CustomActionButton button) { - Logger.Error($"Skipping button {buttonType.Name}. Does not inherit CustomActionButton!"); - return; - } - - if (Activator.CreateInstance(buttonType) is not CustomActionButton button) - { - Logger.Error($"Failed to create button from {buttonType.Name}"); - return; + return false; } CustomButtons.Add(button); @@ -38,5 +31,7 @@ public static class CustomButtonManager ButtonEventTypes.Add(buttonType, typeof(MiraButtonClickEvent<>).MakeGenericType(buttonType)); ButtonCancelledEventTypes.Add(buttonType, typeof(MiraButtonCancelledEvent<>).MakeGenericType(buttonType)); + + return true; } } diff --git a/MiraAPI/Modifiers/ModifierManager.cs b/MiraAPI/Modifiers/ModifierManager.cs index fab840c..ddb3c87 100644 --- a/MiraAPI/Modifiers/ModifierManager.cs +++ b/MiraAPI/Modifiers/ModifierManager.cs @@ -49,11 +49,11 @@ public static class ModifierManager return TypeToIdModifierMap.GetValueOrDefault(type); } - internal static void RegisterModifier(Type modifierType) + internal static bool RegisterModifier(Type modifierType) { if (!typeof(BaseModifier).IsAssignableFrom(modifierType)) { - return; + return false; } IdToTypeModifierMap.Add(GetNextId(), modifierType); @@ -61,7 +61,7 @@ public static class ModifierManager if (!typeof(GameModifier).IsAssignableFrom(modifierType)) { - return; + return true; } var mod = Activator.CreateInstance(modifierType) as GameModifier; @@ -73,6 +73,7 @@ public static class ModifierManager } list.Add(_nextId); + return true; } internal static void AssignModifiers(List plrs) diff --git a/MiraAPI/PluginLoading/MiraPluginManager.cs b/MiraAPI/PluginLoading/MiraPluginManager.cs index 15c9dcf..82362ec 100644 --- a/MiraAPI/PluginLoading/MiraPluginManager.cs +++ b/MiraAPI/PluginLoading/MiraPluginManager.cs @@ -47,15 +47,27 @@ public sealed class MiraPluginManager continue; } - RegisterModifier(type); - RegisterOptions(type, info); + if (RegisterModifier(type)) + { + continue; + } + + if (RegisterOptions(type, info)) + { + continue; + } if (RegisterRoleAttribute(type, info, out var role)) { roles.Add(role!); + continue; + } + + if (RegisterButtonAttribute(type, info)) + { + continue; } - RegisterButtonAttribute(type, info); RegisterColorClasses(type); } @@ -72,105 +84,140 @@ public sealed class MiraPluginManager /// /// Get a mira plugin by its GUID. /// - /// The plugin GUID. + /// The plugin GUID. /// A MiraPluginInfo. - public static MiraPluginInfo GetPluginByGuid(string guid) + public static MiraPluginInfo GetPluginByGuid(string pluginId) { - return Instance._registeredPlugins.Values.First(plugin => plugin.PluginId == guid); + return Instance._registeredPlugins.Values.First(plugin => plugin.PluginId == pluginId); } - private static void RegisterOptions(Type type, MiraPluginInfo pluginInfo) + private static bool RegisterOptions(Type type, MiraPluginInfo pluginInfo) { - if (!type.IsAssignableTo(typeof(AbstractOptionGroup))) + try { - return; - } - - if (!ModdedOptionsManager.RegisterGroup(type, pluginInfo)) - { - return; - } - - foreach (var property in type.GetProperties()) - { - if (property.PropertyType.IsAssignableTo(typeof(IModdedOption))) + if (!type.IsAssignableTo(typeof(AbstractOptionGroup))) { - ModdedOptionsManager.RegisterPropertyOption(type, property, pluginInfo); - continue; + return false; } - var attribute = property.GetCustomAttribute(); - if (attribute == null) + if (!ModdedOptionsManager.RegisterGroup(type, pluginInfo)) { - continue; + return false; } - ModdedOptionsManager.RegisterAttributeOption(type, attribute, property, pluginInfo); + 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); + } + + return true; } + catch (Exception e) + { + Logger.Error($"Failed to register options for {type.Name}: {e}"); + } + return false; } private static bool RegisterRoleAttribute(Type type, MiraPluginInfo pluginInfo, out Type? role) { role = null; - - if (!(typeof(RoleBehaviour).IsAssignableFrom(type) && typeof(ICustomRole).IsAssignableFrom(type))) + try { - return false; - } + if (!(typeof(RoleBehaviour).IsAssignableFrom(type) && typeof(ICustomRole).IsAssignableFrom(type))) + { + return false; + } - if (!ModList.GetById(pluginInfo.PluginId).IsRequiredOnAllClients) + if (!ModList.GetById(pluginInfo.PluginId).IsRequiredOnAllClients) + { + Logger.Error("Custom roles are only supported on all clients."); + return false; + } + + role = type; + return true; + } + catch (Exception e) { - Logger.Error("Custom roles are only supported on all clients."); - return false; + Logger.Error($"Failed to register role for {type.Name}: {e}"); } - - role = type; - return true; + return false; } private static void RegisterColorClasses(Type type) { - if (type.GetCustomAttribute() == null) + try { - 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)) + if (type.GetCustomAttribute() == null) { - continue; + return; } - if (property.GetValue(null) is not CustomColor color) + if (!type.IsStatic()) { - Logger.Error($"Color property {property.Name} in {type.Name} is not a CustomColor."); - continue; + Logger.Error($"Color class {type.Name} must be static."); + return; } - PaletteManager.CustomColors.Add(color); + 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); + } + } + catch (Exception e) + { + Logger.Error($"Failed to register color class {type.Name}: {e}"); } } - private static void RegisterModifier(Type type) + private static bool RegisterModifier(Type type) { - if (type.IsAssignableTo(typeof(BaseModifier))) + try { - ModifierManager.RegisterModifier(type); + return ModifierManager.RegisterModifier(type); + } + catch (Exception e) + { + Logger.Error($"Failed to register modifier {type.Name}: {e}"); + return false; } } - private static void RegisterButtonAttribute(Type type, MiraPluginInfo pluginInfo) + private static bool RegisterButtonAttribute(Type type, MiraPluginInfo pluginInfo) { - if (type.IsAssignableTo(typeof(CustomActionButton)) || type.IsAssignableTo(typeof(CustomActionButton<>))) + try { - CustomButtonManager.RegisterButton(type, pluginInfo); + return CustomButtonManager.RegisterButton(type, pluginInfo); } + catch (Exception e) + { + Logger.Error($"Failed to register button {type.Name}: {e}"); + } + + return false; } }