diff --git a/BepInEx/Contract/Attributes.cs b/BepInEx/Contract/Attributes.cs index e5989c6..2bebbdb 100644 --- a/BepInEx/Contract/Attributes.cs +++ b/BepInEx/Contract/Attributes.cs @@ -110,7 +110,10 @@ namespace BepInEx public string ProcessName { get; protected set; } /// The name of the process that this plugin will run under. - public BepInProcess(string ProcessName) { this.ProcessName = ProcessName; } + public BepInProcess(string ProcessName) + { + this.ProcessName = ProcessName; + } internal static IEnumerable FromCecilType(TypeDefinition td) { @@ -128,8 +131,6 @@ namespace BepInEx /// public static class MetadataHelper { - public static BepInPlugin GetMetadata(BaseUnityPlugin pluginType) { return pluginType.GetType().GetCustomAttributes(typeof(BepInPlugin), false).FirstOrDefault() as BepInPlugin; } - internal static IEnumerable GetCustomAttributes(TypeDefinition td, bool inherit) where T : Attribute { var result = new List(); @@ -146,12 +147,28 @@ namespace BepInEx return result; } + /// + /// Retrieves the BepInPlugin metadata from a plugin type. + /// + /// The plugin type. + /// The BepInPlugin metadata of the plugin type. + public static BepInPlugin GetMetadata(Type pluginType) + { + object[] attributes = pluginType.GetCustomAttributes(typeof(BepInPlugin), false); + + if (attributes.Length == 0) + return null; + + return (BepInPlugin)attributes[0]; + } + /// /// Retrieves the BepInPlugin metadata from a plugin instance. /// /// The plugin instance. /// The BepInPlugin metadata of the plugin instance. - public static BepInPlugin GetMetadata(object plugin) => GetMetadata(plugin as BaseUnityPlugin); + public static BepInPlugin GetMetadata(object plugin) + => GetMetadata(plugin.GetType()); /// /// Gets the specified attributes of a type, if they exist. @@ -159,7 +176,10 @@ namespace BepInEx /// The attribute type to retrieve. /// The plugin type. /// The attributes of the type, if existing. - public static T[] GetAttributes(Type pluginType) where T : Attribute { return (T[])pluginType.GetCustomAttributes(typeof(T), true); } + public static T[] GetAttributes(Type pluginType) where T : Attribute + { + return (T[])pluginType.GetCustomAttributes(typeof(T), true); + } /// /// Gets the specified attributes of an instance, if they exist. @@ -167,7 +187,8 @@ namespace BepInEx /// The attribute type to retrieve. /// The plugin instance. /// The attributes of the instance, if existing. - public static IEnumerable GetAttributes(object plugin) where T : Attribute => GetAttributes(plugin.GetType()); + public static IEnumerable GetAttributes(object plugin) where T : Attribute + => GetAttributes(plugin.GetType()); /// /// Retrieves the dependencies of the specified plugin type. @@ -175,7 +196,10 @@ namespace BepInEx /// The plugin type. /// All currently loaded plugin types. /// A list of all plugin types that the specified plugin type depends upon. - public static IEnumerable GetDependencies(Type Plugin, IEnumerable AllPlugins) { return Plugin.GetCustomAttributes(typeof(BepInDependency), true).Cast(); } + public static IEnumerable GetDependencies(Type Plugin, IEnumerable AllPlugins) + { + return Plugin.GetCustomAttributes(typeof(BepInDependency), true).Cast(); + } } /// @@ -198,7 +222,10 @@ namespace BepInEx { public string Info { get; } - public BuildInfoAttribute(string info) { Info = info; } + public BuildInfoAttribute(string info) + { + Info = info; + } } #endregion