Attributes.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using BepInEx.Logging;
  6. using Mono.Cecil;
  7. using Mono.Collections.Generic;
  8. namespace BepInEx
  9. {
  10. #region BaseUnityPlugin
  11. /// <summary>
  12. /// This attribute denotes that a class is a plugin, and specifies the required metadata.
  13. /// </summary>
  14. [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
  15. public class BepInPlugin : Attribute
  16. {
  17. /// <summary>
  18. /// The unique identifier of the plugin. Should not change between plugin versions.
  19. /// </summary>
  20. public string GUID { get; protected set; }
  21. /// <summary>
  22. /// The user friendly name of the plugin. Is able to be changed between versions.
  23. /// </summary>
  24. public string Name { get; protected set; }
  25. /// <summary>
  26. /// The specfic version of the plugin.
  27. /// </summary>
  28. public Version Version { get; protected set; }
  29. /// <param name="GUID">The unique identifier of the plugin. Should not change between plugin versions.</param>
  30. /// <param name="Name">The user friendly name of the plugin. Is able to be changed between versions.</param>
  31. /// <param name="Version">The specfic version of the plugin.</param>
  32. public BepInPlugin(string GUID, string Name, string Version)
  33. {
  34. this.GUID = GUID;
  35. this.Name = Name;
  36. try
  37. {
  38. this.Version = new Version(Version);
  39. }
  40. catch
  41. {
  42. this.Version = null;
  43. }
  44. }
  45. internal static BepInPlugin FromCecilType(TypeDefinition td)
  46. {
  47. var attr = MetadataHelper.GetCustomAttributes<BepInPlugin>(td, false).FirstOrDefault();
  48. if (attr == null)
  49. return null;
  50. return new BepInPlugin((string)attr.ConstructorArguments[0].Value, (string)attr.ConstructorArguments[1].Value, (string)attr.ConstructorArguments[2].Value);
  51. }
  52. }
  53. /// <summary>
  54. /// This attribute specifies any dependencies that this plugin has on other plugins.
  55. /// </summary>
  56. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
  57. public class BepInDependency : Attribute
  58. {
  59. public enum DependencyFlags
  60. {
  61. /// <summary>
  62. /// The plugin has a hard dependency on the referenced plugin, and will not run without it.
  63. /// </summary>
  64. HardDependency = 1,
  65. /// <summary>
  66. /// This plugin has a soft dependency on the referenced plugin, and is able to run without it.
  67. /// </summary>
  68. SoftDependency = 2,
  69. }
  70. /// <summary>
  71. /// The GUID of the referenced plugin.
  72. /// </summary>
  73. public string DependencyGUID { get; protected set; }
  74. /// <summary>
  75. /// The flags associated with this dependency definition.
  76. /// </summary>
  77. public DependencyFlags Flags { get; protected set; }
  78. /// <param name="DependencyGUID">The GUID of the referenced plugin.</param>
  79. /// <param name="Flags">The flags associated with this dependency definition.</param>
  80. public BepInDependency(string DependencyGUID, DependencyFlags Flags = DependencyFlags.HardDependency)
  81. {
  82. this.DependencyGUID = DependencyGUID;
  83. this.Flags = Flags;
  84. }
  85. internal static IEnumerable<BepInDependency> FromCecilType(TypeDefinition td)
  86. {
  87. var attrs = MetadataHelper.GetCustomAttributes<BepInDependency>(td, true);
  88. return attrs.Select(customAttribute => new BepInDependency((string)customAttribute.ConstructorArguments[0].Value, (DependencyFlags)customAttribute.ConstructorArguments[1].Value)).ToList();
  89. }
  90. }
  91. /// <summary>
  92. /// This attribute specifies which processes this plugin should be run for. Not specifying this attribute will load the plugin under every process.
  93. /// </summary>
  94. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
  95. public class BepInProcess : Attribute
  96. {
  97. /// <summary>
  98. /// The name of the process that this plugin will run under.
  99. /// </summary>
  100. public string ProcessName { get; protected set; }
  101. /// <param name="ProcessName">The name of the process that this plugin will run under.</param>
  102. public BepInProcess(string ProcessName) { this.ProcessName = ProcessName; }
  103. internal static IEnumerable<BepInProcess> FromCecilType(TypeDefinition td)
  104. {
  105. var attrs = MetadataHelper.GetCustomAttributes<BepInProcess>(td, true);
  106. return attrs.Select(customAttribute => new BepInProcess((string)customAttribute.ConstructorArguments[0].Value)).ToList();
  107. }
  108. }
  109. #endregion
  110. #region MetadataHelper
  111. /// <summary>
  112. /// Helper class to use for retrieving metadata about a plugin, defined as attributes.
  113. /// </summary>
  114. public static class MetadataHelper
  115. {
  116. public static BepInPlugin GetMetadata(BaseUnityPlugin pluginType) { return pluginType.GetType().GetCustomAttributes(typeof(BepInPlugin), false).FirstOrDefault() as BepInPlugin; }
  117. internal static IEnumerable<CustomAttribute> GetCustomAttributes<T>(TypeDefinition td, bool inherit) where T : Attribute
  118. {
  119. var result = new List<CustomAttribute>();
  120. var type = typeof(T);
  121. var currentType = td;
  122. do
  123. {
  124. result.AddRange(currentType.CustomAttributes.Where(ca => ca.AttributeType.FullName == type.FullName));
  125. currentType = currentType.BaseType?.Resolve();
  126. } while (inherit && currentType?.FullName != "System.Object");
  127. return result;
  128. }
  129. /// <summary>
  130. /// Retrieves the BepInPlugin metadata from a plugin instance.
  131. /// </summary>
  132. /// <param name="plugin">The plugin instance.</param>
  133. /// <returns>The BepInPlugin metadata of the plugin instance.</returns>
  134. public static BepInPlugin GetMetadata(object plugin) => GetMetadata(plugin.GetType());
  135. /// <summary>
  136. /// Gets the specified attributes of a type, if they exist.
  137. /// </summary>
  138. /// <typeparam name="T">The attribute type to retrieve.</typeparam>
  139. /// <param name="plugin">The plugin type.</param>
  140. /// <returns>The attributes of the type, if existing.</returns>
  141. public static T[] GetAttributes<T>(Type pluginType) where T : Attribute { return (T[])pluginType.GetCustomAttributes(typeof(T), true); }
  142. /// <summary>
  143. /// Gets the specified attributes of an instance, if they exist.
  144. /// </summary>
  145. /// <typeparam name="T">The attribute type to retrieve.</typeparam>
  146. /// <param name="plugin">The plugin instance.</param>
  147. /// <returns>The attributes of the instance, if existing.</returns>
  148. public static IEnumerable<T> GetAttributes<T>(object plugin) where T : Attribute => GetAttributes<T>(plugin.GetType());
  149. /// <summary>
  150. /// Retrieves the dependencies of the specified plugin type.
  151. /// </summary>
  152. /// <param name="Plugin">The plugin type.</param>
  153. /// <param name="AllPlugins">All currently loaded plugin types.</param>
  154. /// <returns>A list of all plugin types that the specified plugin type depends upon.</returns>
  155. public static IEnumerable<BepInDependency> GetDependencies(Type Plugin, IEnumerable<Type> AllPlugins) { return Plugin.GetCustomAttributes(typeof(BepInDependency), true).Cast<BepInDependency>(); }
  156. }
  157. /// <summary>
  158. /// An exception which is thrown when a plugin's dependencies cannot be found.
  159. /// </summary>
  160. public class MissingDependencyException : Exception
  161. {
  162. public MissingDependencyException(string message) : base(message) { }
  163. }
  164. #endregion
  165. #region Build configuration
  166. /// <summary>
  167. /// This class is appended to AssemblyInfo.cs when BepInEx is built via a CI pipeline.
  168. /// It is mainly intended to signify that the current build is not a release build and is special, like for instance a bleeding edge build.
  169. /// </summary>
  170. internal class BuildInfoAttribute : Attribute
  171. {
  172. public string Info { get; }
  173. public BuildInfoAttribute(string info) { Info = info; }
  174. }
  175. #endregion
  176. }