From dd899535f444a17128061a9ff96c17b85b9e6967 Mon Sep 17 00:00:00 2001 From: js6pak Date: Sat, 12 Jun 2021 22:06:27 +0200 Subject: [PATCH] Automatic plugin info --- BepInEx.Core/Contract/Attributes.cs | 72 +++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/BepInEx.Core/Contract/Attributes.cs b/BepInEx.Core/Contract/Attributes.cs index 93402b50..51acd739 100644 --- a/BepInEx.Core/Contract/Attributes.cs +++ b/BepInEx.Core/Contract/Attributes.cs @@ -5,6 +5,7 @@ using System.Reflection; using BepInEx.Bootstrap; using Mono.Cecil; +using MonoMod.Utils; using Version = SemVer.Version; using Range = SemVer.Range; @@ -21,7 +22,7 @@ public class BepInPlugin : Attribute /// The unique identifier of the plugin. Should not change between plugin versions. /// The user friendly name of the plugin. Is able to be changed between versions. /// The specific version of the plugin. - public BepInPlugin(string GUID, string Name, string Version) + public BepInPlugin(string GUID, string Name = null, string Version = null) { this.GUID = GUID; this.Name = Name; @@ -70,9 +71,67 @@ internal static BepInPlugin FromCecilType(TypeDefinition td) if (attr == null) return null; - return new BepInPlugin((string) attr.ConstructorArguments[0].Value, - (string) attr.ConstructorArguments[1].Value, - (string) attr.ConstructorArguments[2].Value); + var guid = (string) attr.ConstructorArguments[0].Value; + var name = (string) attr.ConstructorArguments[1].Value; + var version = (string) attr.ConstructorArguments[2].Value; + + var assembly = td.Module.Assembly; + + if (name == null) + { + name = assembly.Name.Name; + + var nameAttribute = assembly.GetCustomAttribute(typeof(AssemblyTitleAttribute).FullName); + if (nameAttribute != null && nameAttribute.ConstructorArguments.Count == 1) + { + name = (string) nameAttribute.ConstructorArguments.Single().Value; + } + } + + if (version == null) + { + version = assembly.Name.Version.ToString(); + + var versionAttribute = assembly.GetCustomAttribute(typeof(AssemblyInformationalVersionAttribute).FullName); + if (versionAttribute != null && versionAttribute.ConstructorArguments.Count == 1) + { + version = (string) versionAttribute.ConstructorArguments.Single().Value; + } + } + + return new BepInPlugin(guid, name, version); + } + + internal void Update(Type pluginType) + { + var assembly = pluginType.Assembly; + + if (Name == null) + { + Name = assembly.GetName().Name; + + var nameAttribute = MetadataHelper.GetAttributes(assembly).Single(); + if (nameAttribute != null) + { + Name = nameAttribute.Title; + } + } + + if (Version == null) + { + var version = assembly.GetName().Version; + Version = new Version(version.Major, version.Minor, version.Build != -1 ? version.Build : 0); + + var versionAttribute = MetadataHelper.GetAttributes(assembly) + .Single(); + if (versionAttribute != null) + { + if (Version.TryParse(versionAttribute.InformationalVersion, out var v)) + { + Version = v; + } + } + } } } @@ -271,7 +330,10 @@ public static BepInPlugin GetMetadata(Type pluginType) if (attributes.Length == 0) return null; - return (BepInPlugin) attributes[0]; + var attribute = (BepInPlugin) attributes[0]; + attribute.Update(pluginType); + + return attribute; } ///