Attributes.cs 6.1 KB

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