Attributes.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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)
  103. {
  104. this.ProcessName = ProcessName;
  105. }
  106. internal static IEnumerable<BepInProcess> FromCecilType(TypeDefinition td)
  107. {
  108. var attrs = MetadataHelper.GetCustomAttributes<BepInProcess>(td, true);
  109. return attrs.Select(customAttribute => new BepInProcess((string)customAttribute.ConstructorArguments[0].Value)).ToList();
  110. }
  111. }
  112. #endregion
  113. #region MetadataHelper
  114. /// <summary>
  115. /// Helper class to use for retrieving metadata about a plugin, defined as attributes.
  116. /// </summary>
  117. public static class MetadataHelper
  118. {
  119. internal static IEnumerable<CustomAttribute> GetCustomAttributes<T>(TypeDefinition td, bool inherit) where T : Attribute
  120. {
  121. var result = new List<CustomAttribute>();
  122. var type = typeof(T);
  123. var currentType = td;
  124. do
  125. {
  126. result.AddRange(currentType.CustomAttributes.Where(ca => ca.AttributeType.FullName == type.FullName));
  127. currentType = currentType.BaseType?.Resolve();
  128. } while (inherit && currentType?.FullName != "System.Object");
  129. return result;
  130. }
  131. /// <summary>
  132. /// Retrieves the BepInPlugin metadata from a plugin type.
  133. /// </summary>
  134. /// <param name="plugin">The plugin type.</param>
  135. /// <returns>The BepInPlugin metadata of the plugin type.</returns>
  136. public static BepInPlugin GetMetadata(Type pluginType)
  137. {
  138. object[] attributes = pluginType.GetCustomAttributes(typeof(BepInPlugin), false);
  139. if (attributes.Length == 0)
  140. return null;
  141. return (BepInPlugin)attributes[0];
  142. }
  143. /// <summary>
  144. /// Retrieves the BepInPlugin metadata from a plugin instance.
  145. /// </summary>
  146. /// <param name="plugin">The plugin instance.</param>
  147. /// <returns>The BepInPlugin metadata of the plugin instance.</returns>
  148. public static BepInPlugin GetMetadata(object plugin)
  149. => GetMetadata(plugin.GetType());
  150. /// <summary>
  151. /// Gets the specified attributes of a type, if they exist.
  152. /// </summary>
  153. /// <typeparam name="T">The attribute type to retrieve.</typeparam>
  154. /// <param name="plugin">The plugin type.</param>
  155. /// <returns>The attributes of the type, if existing.</returns>
  156. public static T[] GetAttributes<T>(Type pluginType) where T : Attribute
  157. {
  158. return (T[])pluginType.GetCustomAttributes(typeof(T), true);
  159. }
  160. /// <summary>
  161. /// Gets the specified attributes of an instance, if they exist.
  162. /// </summary>
  163. /// <typeparam name="T">The attribute type to retrieve.</typeparam>
  164. /// <param name="plugin">The plugin instance.</param>
  165. /// <returns>The attributes of the instance, if existing.</returns>
  166. public static IEnumerable<T> GetAttributes<T>(object plugin) where T : Attribute
  167. => GetAttributes<T>(plugin.GetType());
  168. /// <summary>
  169. /// Retrieves the dependencies of the specified plugin type.
  170. /// </summary>
  171. /// <param name="Plugin">The plugin type.</param>
  172. /// <returns>A list of all plugin types that the specified plugin type depends upon.</returns>
  173. public static IEnumerable<BepInDependency> GetDependencies(Type plugin)
  174. {
  175. return plugin.GetCustomAttributes(typeof(BepInDependency), true).Cast<BepInDependency>();
  176. }
  177. }
  178. /// <summary>
  179. /// An exception which is thrown when a plugin's dependencies cannot be found.
  180. /// </summary>
  181. public class MissingDependencyException : Exception
  182. {
  183. public MissingDependencyException(string message) : base(message) { }
  184. }
  185. #endregion
  186. #region Build configuration
  187. /// <summary>
  188. /// This class is appended to AssemblyInfo.cs when BepInEx is built via a CI pipeline.
  189. /// 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.
  190. /// </summary>
  191. internal class BuildInfoAttribute : Attribute
  192. {
  193. public string Info { get; }
  194. public BuildInfoAttribute(string info)
  195. {
  196. Info = info;
  197. }
  198. }
  199. #endregion
  200. }