Attributes.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. this.Version = new Version(Version);
  37. }
  38. internal static BepInPlugin FromCecilType(TypeDefinition td)
  39. {
  40. var attr = MetadataHelper.GetCustomAttributes<BepInPlugin>(td, false).FirstOrDefault();
  41. if (attr == null)
  42. return null;
  43. return new BepInPlugin((string)attr.ConstructorArguments[0].Value, (string)attr.ConstructorArguments[1].Value, (string)attr.ConstructorArguments[2].Value);
  44. }
  45. }
  46. /// <summary>
  47. /// This attribute specifies any dependencies that this plugin has on other plugins.
  48. /// </summary>
  49. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
  50. public class BepInDependency : Attribute
  51. {
  52. public enum DependencyFlags
  53. {
  54. /// <summary>
  55. /// The plugin has a hard dependency on the referenced plugin, and will not run without it.
  56. /// </summary>
  57. HardDependency = 1,
  58. /// <summary>
  59. /// This plugin has a soft dependency on the referenced plugin, and is able to run without it.
  60. /// </summary>
  61. SoftDependency = 2,
  62. }
  63. /// <summary>
  64. /// The GUID of the referenced plugin.
  65. /// </summary>
  66. public string DependencyGUID { get; protected set; }
  67. /// <summary>
  68. /// The flags associated with this dependency definition.
  69. /// </summary>
  70. public DependencyFlags Flags { get; protected set; }
  71. /// <param name="DependencyGUID">The GUID of the referenced plugin.</param>
  72. /// <param name="Flags">The flags associated with this dependency definition.</param>
  73. public BepInDependency(string DependencyGUID, DependencyFlags Flags = DependencyFlags.HardDependency)
  74. {
  75. this.DependencyGUID = DependencyGUID;
  76. this.Flags = Flags;
  77. }
  78. internal static IEnumerable<BepInDependency> FromCecilType(TypeDefinition td)
  79. {
  80. var attrs = MetadataHelper.GetCustomAttributes<BepInDependency>(td, true);
  81. return attrs.Select(customAttribute => new BepInDependency((string)customAttribute.ConstructorArguments[0].Value, (DependencyFlags)customAttribute.ConstructorArguments[1].Value)).ToList();
  82. }
  83. }
  84. /// <summary>
  85. /// This attribute specifies which processes this plugin should be run for. Not specifying this attribute will load the plugin under every process.
  86. /// </summary>
  87. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
  88. public class BepInProcess : Attribute
  89. {
  90. /// <summary>
  91. /// The name of the process that this plugin will run under.
  92. /// </summary>
  93. public string ProcessName { get; protected set; }
  94. /// <param name="ProcessName">The name of the process that this plugin will run under.</param>
  95. public BepInProcess(string ProcessName)
  96. {
  97. this.ProcessName = ProcessName;
  98. }
  99. internal static IEnumerable<BepInProcess> FromCecilType(TypeDefinition td)
  100. {
  101. var attrs = MetadataHelper.GetCustomAttributes<BepInProcess>(td, true);
  102. return attrs.Select(customAttribute => new BepInProcess((string)customAttribute.ConstructorArguments[0].Value)).ToList();
  103. }
  104. }
  105. #endregion
  106. #region MetadataHelper
  107. /// <summary>
  108. /// Helper class to use for retrieving metadata about a plugin, defined as attributes.
  109. /// </summary>
  110. public static class MetadataHelper
  111. {
  112. internal static IEnumerable<CustomAttribute> GetCustomAttributes<T>(TypeDefinition td, bool inherit) where T : Attribute
  113. {
  114. var result = new List<CustomAttribute>();
  115. var type = typeof(T);
  116. var currentType = td;
  117. do
  118. {
  119. result.AddRange(currentType.CustomAttributes.Where(ca => ca.AttributeType.FullName == type.FullName));
  120. currentType = currentType.BaseType?.Resolve();
  121. } while (inherit && currentType?.FullName != "System.Object");
  122. return result;
  123. }
  124. /// <summary>
  125. /// Retrieves the BepInPlugin metadata from a plugin type.
  126. /// </summary>
  127. /// <param name="plugin">The plugin type.</param>
  128. /// <returns>The BepInPlugin metadata of the plugin type.</returns>
  129. public static BepInPlugin GetMetadata(Type pluginType)
  130. {
  131. object[] attributes = pluginType.GetCustomAttributes(typeof(BepInPlugin), false);
  132. if (attributes.Length == 0)
  133. return null;
  134. return (BepInPlugin)attributes[0];
  135. }
  136. /// <summary>
  137. /// Retrieves the BepInPlugin metadata from a plugin instance.
  138. /// </summary>
  139. /// <param name="plugin">The plugin instance.</param>
  140. /// <returns>The BepInPlugin metadata of the plugin instance.</returns>
  141. public static BepInPlugin GetMetadata(object plugin)
  142. => GetMetadata(plugin.GetType());
  143. /// <summary>
  144. /// Gets the specified attributes of a type, if they exist.
  145. /// </summary>
  146. /// <typeparam name="T">The attribute type to retrieve.</typeparam>
  147. /// <param name="plugin">The plugin type.</param>
  148. /// <returns>The attributes of the type, if existing.</returns>
  149. public static T[] GetAttributes<T>(Type pluginType) where T : Attribute
  150. {
  151. return (T[])pluginType.GetCustomAttributes(typeof(T), true);
  152. }
  153. /// <summary>
  154. /// Gets the specified attributes of an instance, if they exist.
  155. /// </summary>
  156. /// <typeparam name="T">The attribute type to retrieve.</typeparam>
  157. /// <param name="plugin">The plugin instance.</param>
  158. /// <returns>The attributes of the instance, if existing.</returns>
  159. public static IEnumerable<T> GetAttributes<T>(object plugin) where T : Attribute
  160. => GetAttributes<T>(plugin.GetType());
  161. /// <summary>
  162. /// Retrieves the dependencies of the specified plugin type.
  163. /// </summary>
  164. /// <param name="Plugin">The plugin type.</param>
  165. /// <param name="AllPlugins">All currently loaded plugin types.</param>
  166. /// <returns>A list of all plugin types that the specified plugin type depends upon.</returns>
  167. public static IEnumerable<BepInDependency> GetDependencies(Type Plugin, IEnumerable<Type> AllPlugins)
  168. {
  169. return Plugin.GetCustomAttributes(typeof(BepInDependency), true).Cast<BepInDependency>();
  170. }
  171. }
  172. /// <summary>
  173. /// An exception which is thrown when a plugin's dependencies cannot be found.
  174. /// </summary>
  175. public class MissingDependencyException : Exception
  176. {
  177. public MissingDependencyException(string message) : base(message) { }
  178. }
  179. #endregion
  180. #region Build configuration
  181. /// <summary>
  182. /// This class is appended to AssemblyInfo.cs when BepInEx is built via a CI pipeline.
  183. /// 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.
  184. /// </summary>
  185. internal class BuildInfoAttribute : Attribute
  186. {
  187. public string Info { get; }
  188. public BuildInfoAttribute(string info)
  189. {
  190. Info = info;
  191. }
  192. }
  193. #endregion
  194. }