Attributes.cs 6.0 KB

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