Skip to content

Commit

Permalink
Add version-independent helper for retrieving assembly attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ghorsington committed Nov 30, 2020
1 parent cfa6857 commit 28671ce
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
13 changes: 12 additions & 1 deletion BepInEx.Core/Contract/Attributes.cs
Expand Up @@ -2,8 +2,8 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using BepInEx.Bootstrap;
using BepInEx.Core;
using Mono.Cecil;

namespace BepInEx
Expand Down Expand Up @@ -269,6 +269,17 @@ public static BepInPlugin GetMetadata(object plugin)
return (T[])pluginType.GetCustomAttributes(typeof(T), true);
}

/// <summary>
/// Gets the specified attributes of an assembly, if they exist.
/// </summary>
/// <param name="assembly">The assembly.</param>
/// <typeparam name="T">The attribute type to retrieve.</typeparam>
/// <returns>The attributes of the type, if existing.</returns>
public static T[] GetAttributes<T>(Assembly assembly) where T : Attribute
{
return (T[])assembly.GetCustomAttributes(typeof(T), true);
}

/// <summary>
/// Gets the specified attributes of an instance, if they exist.
/// </summary>
Expand Down
3 changes: 1 addition & 2 deletions BepInEx.Core/Paths.cs
@@ -1,5 +1,4 @@
using System.IO;
using System.Linq;
using System.Reflection;
using MonoMod.Utils;

Expand Down Expand Up @@ -34,7 +33,7 @@ internal static void SetPluginPath(string pluginPath)
PluginPath = Utility.CombinePaths(BepInExRootPath, pluginPath);
}

public static SemVer.Version BepInExVersion { get; } = SemVer.Version.Parse(typeof(Paths).Assembly.GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false).Cast<AssemblyInformationalVersionAttribute>().First().InformationalVersion);
public static SemVer.Version BepInExVersion { get; } = SemVer.Version.Parse(MetadataHelper.GetAttributes<AssemblyInformationalVersionAttribute>(typeof(Paths).Assembly)[0].InformationalVersion);

/// <summary>
/// The directory that the core BepInEx DLLs reside in.
Expand Down

0 comments on commit 28671ce

Please sign in to comment.