Attributes.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 instance.
  93. /// </summary>
  94. /// <param name="plugin">The plugin instance.</param>
  95. /// <returns>The BepInPlugin metadata of the plugin instance.</returns>
  96. public static BepInPlugin GetMetadata(object plugin)
  97. {
  98. return GetMetadata(plugin.GetType());
  99. }
  100. /// <summary>
  101. /// Retrieves the BepInPlugin metadata from a plugin type.
  102. /// </summary>
  103. /// <param name="plugin">The plugin type.</param>
  104. /// <returns>The BepInPlugin metadata of the plugin type.</returns>
  105. public static BepInPlugin GetMetadata(Type pluginType)
  106. {
  107. object[] attributes = pluginType.GetCustomAttributes(typeof(BepInPlugin), false);
  108. if (attributes.Length == 0)
  109. return null;
  110. return (BepInPlugin)attributes[0];
  111. }
  112. /// <summary>
  113. /// Gets the specified attributes of an instance, if they exist.
  114. /// </summary>
  115. /// <typeparam name="T">The attribute type to retrieve.</typeparam>
  116. /// <param name="plugin">The plugin instance.</param>
  117. /// <returns>The attributes of the instance, if existing.</returns>
  118. public static IEnumerable<T> GetAttributes<T>(object plugin) where T : Attribute
  119. {
  120. return GetAttributes<T>(plugin.GetType());
  121. }
  122. /// <summary>
  123. /// Gets the specified attributes of a type, if they exist.
  124. /// </summary>
  125. /// <typeparam name="T">The attribute type to retrieve.</typeparam>
  126. /// <param name="plugin">The plugin type.</param>
  127. /// <returns>The attributes of the type, if existing.</returns>
  128. public static IEnumerable<T> GetAttributes<T>(Type pluginType) where T : Attribute
  129. {
  130. return pluginType.GetCustomAttributes(typeof(T), true).Cast<T>();
  131. }
  132. /// <summary>
  133. /// Retrieves the dependencies of the specified plugin type.
  134. /// </summary>
  135. /// <param name="Plugin">The plugin type.</param>
  136. /// <param name="AllPlugins">All currently loaded plugin types.</param>
  137. /// <returns>A list of all plugin types that the specified plugin type depends upon.</returns>
  138. public static IEnumerable<Type> GetDependencies(Type Plugin, IEnumerable<Type> AllPlugins)
  139. {
  140. object[] attributes = Plugin.GetCustomAttributes(typeof(BepInDependency), true);
  141. List<Type> dependencyTypes = new List<Type>();
  142. foreach (BepInDependency dependency in attributes)
  143. {
  144. Type dependencyType = AllPlugins.FirstOrDefault(x => GetMetadata(x)?.GUID == dependency.DependencyGUID);
  145. if (dependencyType == null)
  146. {
  147. if ((dependency.Flags & BepInDependency.DependencyFlags.SoftDependency) != 0)
  148. continue; //skip on soft dependencies
  149. throw new MissingDependencyException("Cannot find dependency type.");
  150. }
  151. dependencyTypes.Add(dependencyType);
  152. }
  153. return dependencyTypes;
  154. }
  155. }
  156. /// <summary>
  157. /// An exception which is thrown when a plugin's dependencies cannot be found.
  158. /// </summary>
  159. public class MissingDependencyException : Exception
  160. {
  161. public MissingDependencyException(string message) : base(message) { }
  162. }
  163. #endregion
  164. #region Build configuration
  165. /// <summary>
  166. /// This class is appended to AssemblyInfo.cs when BepInEx is built via a CI pipeline.
  167. /// 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.
  168. /// </summary>
  169. internal class BuildInfoAttribute : Attribute
  170. {
  171. public string Info { get; }
  172. public BuildInfoAttribute(string info)
  173. {
  174. Info = info;
  175. }
  176. }
  177. #endregion
  178. }