From e54a0b005037d09ccfdfd07f22e875a981da1da5 Mon Sep 17 00:00:00 2001 From: Az <56699741+AlchlcDvl@users.noreply.github.com> Date: Tue, 3 Nov 2026 09:25:18 -0500 Subject: [PATCH] add IsDefined for better checking --- .../Delegate/ModdedOptionMemberAttribute.cs | 8 ++++---- MiraAPI/PluginLoading/MiraPluginManager.cs | 16 +++++++--------- MiraAPI/Utilities/Extensions.cs | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/MiraAPI/GameOptions/Attributes/Delegate/ModdedOptionMemberAttribute.cs b/MiraAPI/GameOptions/Attributes/Delegate/ModdedOptionMemberAttribute.cs index f98fc0ea..4a9c40d0 100644 --- a/MiraAPI/GameOptions/Attributes/Delegate/ModdedOptionMemberAttribute.cs +++ b/MiraAPI/GameOptions/Attributes/Delegate/ModdedOptionMemberAttribute.cs @@ -166,7 +166,7 @@ public abstract class ModdedOptionMemberAttribute(string? memberName, Type? hold { flags |= BindingFlags.Instance; - var canSearchPrivates = type.GetCustomAttribute() != null; + var canSearchPrivates = type.IsDefined(); if (canSearchPrivates) flags |= BindingFlags.NonPublic; @@ -175,7 +175,7 @@ public abstract class ModdedOptionMemberAttribute(string? memberName, Type? hold foreach (var method in type.GetMethods(getterFlags)) { - if (method.GetParameters().Length > 0 || (method.GetCustomAttribute() == null && method.Name != "GetInstance")) + if (method.GetParameters().Length > 0 || (method.IsDefined() && method.Name != "GetInstance")) continue; instanceGetter = method; @@ -186,7 +186,7 @@ public abstract class ModdedOptionMemberAttribute(string? memberName, Type? hold { foreach (var prop in type.GetProperties(getterFlags)) { - if (prop.GetCustomAttribute() == null && prop.Name != "Instance") + if (prop.IsDefined() && prop.Name != "Instance") continue; var getMethod = prop.GetGetMethod(canSearchPrivates); @@ -213,7 +213,7 @@ public abstract class ModdedOptionMemberAttribute(string? memberName, Type? hold return null; } } - else if (type.GetCustomAttribute() != null) + else if (type.IsDefined()) { flags |= BindingFlags.NonPublic; } diff --git a/MiraAPI/PluginLoading/MiraPluginManager.cs b/MiraAPI/PluginLoading/MiraPluginManager.cs index 3214b099..765ad780 100644 --- a/MiraAPI/PluginLoading/MiraPluginManager.cs +++ b/MiraAPI/PluginLoading/MiraPluginManager.cs @@ -117,7 +117,7 @@ public sealed class MiraPluginManager { foreach (var type in assembly.GetTypes()) { - if ((isMira && type.IsAbstract) || type.GetCustomAttribute() != null) + if ((isMira && type.IsAbstract) || type.IsDefined()) continue; RegisterEvents(type); @@ -223,7 +223,7 @@ public sealed class MiraPluginManager foreach (var property in type.GetProperties()) { - if (property.GetCustomAttribute() != null) + if (property.IsDefined()) continue; if (property.GetMethod?.IsStatic == true) @@ -409,7 +409,7 @@ public sealed class MiraPluginManager continue; } - if (property.GetCustomAttribute() != null && + if (property.IsDefined() && property.PropertyType.IsAssignableTo(typeof(LocalSettingsButton))) { var button = property.GetValue(tabInstance) as LocalSettingsButton; @@ -436,15 +436,13 @@ public sealed class MiraPluginManager } var attribute = property.GetCustomAttribute(); - if (attribute == null) + if (attribute != null) { - continue; + attribute.CreateSetting(type, configEntry); } - - attribute.CreateSetting(type, configEntry); } - foreach (var field in type.GetFields().Where(f => f.FieldType.IsAssignableTo(typeof(ConfigEntryBase)) && f.GetCustomAttribute() != null)) + foreach (var field in type.GetFields().Where(f => f.FieldType.IsAssignableTo(typeof(ConfigEntryBase)) && f.IsDefined())) { Error($"{field.Name} is a field, not a property. Use properties for local settings."); } @@ -463,7 +461,7 @@ public sealed class MiraPluginManager { try { - if (type.GetCustomAttribute() == null) + if (type.IsDefined()) { return; } diff --git a/MiraAPI/Utilities/Extensions.cs b/MiraAPI/Utilities/Extensions.cs index 808faef7..bbc627db 100644 --- a/MiraAPI/Utilities/Extensions.cs +++ b/MiraAPI/Utilities/Extensions.cs @@ -842,4 +842,20 @@ public static class Extensions var comp = go.GetComponent(); return comp ? comp : go.AddComponent(); } + + /// + /// The type of the attribute. + /// true if a custom attribute of type is applied to element; otherwise, false. + public static bool IsDefined(this MemberInfo element, bool inherit) + where T : Attribute + { + return Attribute.IsDefined(element, typeof(T), inherit); + } + + /// + public static bool IsDefined(this MemberInfo element) + where T : Attribute + { + return Attribute.IsDefined(element, typeof(T)); + } }