Skip to content
This repository has been archived by the owner on Feb 7, 2022. It is now read-only.

Commit

Permalink
Automatic plugin info
Browse files Browse the repository at this point in the history
  • Loading branch information
js6pak committed Jun 20, 2021
1 parent f426eca commit dd89953
Showing 1 changed file with 67 additions and 5 deletions.
72 changes: 67 additions & 5 deletions BepInEx.Core/Contract/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Reflection;
using BepInEx.Bootstrap;
using Mono.Cecil;
using MonoMod.Utils;
using Version = SemVer.Version;
using Range = SemVer.Range;

Expand All @@ -21,7 +22,7 @@ public class BepInPlugin : Attribute
/// <param name="GUID">The unique identifier of the plugin. Should not change between plugin versions.</param>
/// <param name="Name">The user friendly name of the plugin. Is able to be changed between versions.</param>
/// <param name="Version">The specific version of the plugin.</param>
public BepInPlugin(string GUID, string Name, string Version)
public BepInPlugin(string GUID, string Name = null, string Version = null)
{
this.GUID = GUID;
this.Name = Name;
Expand Down Expand Up @@ -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<AssemblyTitleAttribute>(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<AssemblyInformationalVersionAttribute>(assembly)
.Single();
if (versionAttribute != null)
{
if (Version.TryParse(versionAttribute.InformationalVersion, out var v))
{
Version = v;
}
}
}
}
}

Expand Down Expand Up @@ -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;
}

/// <summary>
Expand Down

0 comments on commit dd89953

Please sign in to comment.