Attributes.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using BepInEx.Bootstrap;
  6. using Mono.Cecil;
  7. namespace BepInEx
  8. {
  9. #region BaseUnityPlugin
  10. /// <summary>
  11. /// This attribute denotes that a class is a plugin, and specifies the required metadata.
  12. /// </summary>
  13. [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
  14. public class BepInPlugin : Attribute
  15. {
  16. /// <summary>
  17. /// The unique identifier of the plugin. Should not change between plugin versions.
  18. /// </summary>
  19. public string GUID { get; protected set; }
  20. /// <summary>
  21. /// The user friendly name of the plugin. Is able to be changed between versions.
  22. /// </summary>
  23. public string Name { get; protected set; }
  24. /// <summary>
  25. /// The specfic version of the plugin.
  26. /// </summary>
  27. public Version Version { get; protected set; }
  28. /// <param name="GUID">The unique identifier of the plugin. Should not change between plugin versions.</param>
  29. /// <param name="Name">The user friendly name of the plugin. Is able to be changed between versions.</param>
  30. /// <param name="Version">The specfic version of the plugin.</param>
  31. public BepInPlugin(string GUID, string Name, string Version)
  32. {
  33. this.GUID = GUID;
  34. this.Name = Name;
  35. try
  36. {
  37. this.Version = new Version(Version);
  38. }
  39. catch
  40. {
  41. this.Version = null;
  42. }
  43. }
  44. internal static BepInPlugin FromCecilType(TypeDefinition td)
  45. {
  46. var attr = MetadataHelper.GetCustomAttributes<BepInPlugin>(td, false).FirstOrDefault();
  47. if (attr == null)
  48. return null;
  49. return new BepInPlugin((string)attr.ConstructorArguments[0].Value, (string)attr.ConstructorArguments[1].Value, (string)attr.ConstructorArguments[2].Value);
  50. }
  51. }
  52. /// <summary>
  53. /// This attribute specifies any dependencies that this plugin has on other plugins.
  54. /// </summary>
  55. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
  56. public class BepInDependency : Attribute, ICacheable
  57. {
  58. public enum DependencyFlags
  59. {
  60. /// <summary>
  61. /// The plugin has a hard dependency on the referenced plugin, and will not run without it.
  62. /// </summary>
  63. HardDependency = 1,
  64. /// <summary>
  65. /// This plugin has a soft dependency on the referenced plugin, and is able to run without it.
  66. /// </summary>
  67. SoftDependency = 2,
  68. }
  69. /// <summary>
  70. /// The GUID of the referenced plugin.
  71. /// </summary>
  72. public string DependencyGUID { get; protected set; }
  73. /// <summary>
  74. /// The flags associated with this dependency definition.
  75. /// </summary>
  76. public DependencyFlags Flags { get; protected set; }
  77. /// <summary>
  78. /// The minimum version of the referenced plugin.
  79. /// </summary>
  80. public Version MinimumVersion { get; protected set; }
  81. /// <summary>
  82. /// Marks this <see cref="BaseUnityPlugin"/> as depenant on another plugin. The other plugin will be loaded before this one.
  83. /// If the other plugin doesn't exist, what happens depends on the <see cref="Flags"/> parameter.
  84. /// </summary>
  85. /// <param name="DependencyGUID">The GUID of the referenced plugin.</param>
  86. /// <param name="Flags">The flags associated with this dependency definition.</param>
  87. public BepInDependency(string DependencyGUID, DependencyFlags Flags = DependencyFlags.HardDependency)
  88. {
  89. this.DependencyGUID = DependencyGUID;
  90. this.Flags = Flags;
  91. MinimumVersion = new Version();
  92. }
  93. /// <summary>
  94. /// Marks this <see cref="BaseUnityPlugin"/> as depenant on another plugin. The other plugin will be loaded before this one.
  95. /// If the other plugin doesn't exist or is of a version below <see cref="MinimumDependencyVersion"/>, this plugin will not load and an error will be logged instead.
  96. /// </summary>
  97. /// <param name="DependencyGUID">The GUID of the referenced plugin.</param>
  98. /// <param name="MinimumDependencyVersion">The minimum version of the referenced plugin.</param>
  99. public BepInDependency(string DependencyGUID, string MinimumDependencyVersion) : this(DependencyGUID)
  100. {
  101. MinimumVersion = new Version(MinimumDependencyVersion);
  102. }
  103. internal static IEnumerable<BepInDependency> FromCecilType(TypeDefinition td)
  104. {
  105. var attrs = MetadataHelper.GetCustomAttributes<BepInDependency>(td, true);
  106. return attrs.Select(customAttribute =>
  107. {
  108. var dependencyGuid = (string)customAttribute.ConstructorArguments[0].Value;
  109. var secondArg = customAttribute.ConstructorArguments[1].Value;
  110. if (secondArg is string minVersion) return new BepInDependency(dependencyGuid, minVersion);
  111. return new BepInDependency(dependencyGuid, (DependencyFlags)secondArg);
  112. }).ToList();
  113. }
  114. void ICacheable.Save(BinaryWriter bw)
  115. {
  116. bw.Write(DependencyGUID);
  117. bw.Write((int)Flags);
  118. bw.Write(MinimumVersion.ToString());
  119. }
  120. void ICacheable.Load(BinaryReader br)
  121. {
  122. DependencyGUID = br.ReadString();
  123. Flags = (DependencyFlags)br.ReadInt32();
  124. MinimumVersion = new Version(br.ReadString());
  125. }
  126. }
  127. /// <summary>
  128. /// This attribute specifies which processes this plugin should be run for. Not specifying this attribute will load the plugin under every process.
  129. /// </summary>
  130. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
  131. public class BepInProcess : Attribute
  132. {
  133. /// <summary>
  134. /// The name of the process that this plugin will run under.
  135. /// </summary>
  136. public string ProcessName { get; protected set; }
  137. /// <param name="ProcessName">The name of the process that this plugin will run under.</param>
  138. public BepInProcess(string ProcessName)
  139. {
  140. this.ProcessName = ProcessName;
  141. }
  142. internal static List<BepInProcess> FromCecilType(TypeDefinition td)
  143. {
  144. var attrs = MetadataHelper.GetCustomAttributes<BepInProcess>(td, true);
  145. return attrs.Select(customAttribute => new BepInProcess((string)customAttribute.ConstructorArguments[0].Value)).ToList();
  146. }
  147. }
  148. #endregion
  149. #region MetadataHelper
  150. /// <summary>
  151. /// Helper class to use for retrieving metadata about a plugin, defined as attributes.
  152. /// </summary>
  153. public static class MetadataHelper
  154. {
  155. internal static IEnumerable<CustomAttribute> GetCustomAttributes<T>(TypeDefinition td, bool inherit) where T : Attribute
  156. {
  157. var result = new List<CustomAttribute>();
  158. var type = typeof(T);
  159. var currentType = td;
  160. do
  161. {
  162. result.AddRange(currentType.CustomAttributes.Where(ca => ca.AttributeType.FullName == type.FullName));
  163. currentType = currentType.BaseType?.Resolve();
  164. } while (inherit && currentType?.FullName != "System.Object");
  165. return result;
  166. }
  167. /// <summary>
  168. /// Retrieves the BepInPlugin metadata from a plugin type.
  169. /// </summary>
  170. /// <param name="plugin">The plugin type.</param>
  171. /// <returns>The BepInPlugin metadata of the plugin type.</returns>
  172. public static BepInPlugin GetMetadata(Type pluginType)
  173. {
  174. object[] attributes = pluginType.GetCustomAttributes(typeof(BepInPlugin), false);
  175. if (attributes.Length == 0)
  176. return null;
  177. return (BepInPlugin)attributes[0];
  178. }
  179. /// <summary>
  180. /// Retrieves the BepInPlugin metadata from a plugin instance.
  181. /// </summary>
  182. /// <param name="plugin">The plugin instance.</param>
  183. /// <returns>The BepInPlugin metadata of the plugin instance.</returns>
  184. public static BepInPlugin GetMetadata(object plugin)
  185. => GetMetadata(plugin.GetType());
  186. /// <summary>
  187. /// Gets the specified attributes of a type, if they exist.
  188. /// </summary>
  189. /// <typeparam name="T">The attribute type to retrieve.</typeparam>
  190. /// <param name="plugin">The plugin type.</param>
  191. /// <returns>The attributes of the type, if existing.</returns>
  192. public static T[] GetAttributes<T>(Type pluginType) where T : Attribute
  193. {
  194. return (T[])pluginType.GetCustomAttributes(typeof(T), true);
  195. }
  196. /// <summary>
  197. /// Gets the specified attributes of an instance, if they exist.
  198. /// </summary>
  199. /// <typeparam name="T">The attribute type to retrieve.</typeparam>
  200. /// <param name="plugin">The plugin instance.</param>
  201. /// <returns>The attributes of the instance, if existing.</returns>
  202. public static IEnumerable<T> GetAttributes<T>(object plugin) where T : Attribute
  203. => GetAttributes<T>(plugin.GetType());
  204. /// <summary>
  205. /// Retrieves the dependencies of the specified plugin type.
  206. /// </summary>
  207. /// <param name="Plugin">The plugin type.</param>
  208. /// <returns>A list of all plugin types that the specified plugin type depends upon.</returns>
  209. public static IEnumerable<BepInDependency> GetDependencies(Type plugin)
  210. {
  211. return plugin.GetCustomAttributes(typeof(BepInDependency), true).Cast<BepInDependency>();
  212. }
  213. }
  214. /// <summary>
  215. /// An exception which is thrown when a plugin's dependencies cannot be found.
  216. /// </summary>
  217. public class MissingDependencyException : Exception
  218. {
  219. public MissingDependencyException(string message) : base(message) { }
  220. }
  221. #endregion
  222. #region Build configuration
  223. /// <summary>
  224. /// This class is appended to AssemblyInfo.cs when BepInEx is built via a CI pipeline.
  225. /// 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.
  226. /// </summary>
  227. internal class BuildInfoAttribute : Attribute
  228. {
  229. public string Info { get; }
  230. public BuildInfoAttribute(string info)
  231. {
  232. Info = info;
  233. }
  234. }
  235. #endregion
  236. }