Attributes.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. /// <summary>
  59. /// Flags that are applied to a dependency
  60. /// </summary>
  61. [Flags]
  62. public enum DependencyFlags
  63. {
  64. /// <summary>
  65. /// The plugin has a hard dependency on the referenced plugin, and will not run without it.
  66. /// </summary>
  67. HardDependency = 1,
  68. /// <summary>
  69. /// This plugin has a soft dependency on the referenced plugin, and is able to run without it.
  70. /// </summary>
  71. SoftDependency = 2,
  72. }
  73. /// <summary>
  74. /// The GUID of the referenced plugin.
  75. /// </summary>
  76. public string DependencyGUID { get; protected set; }
  77. /// <summary>
  78. /// The flags associated with this dependency definition.
  79. /// </summary>
  80. public DependencyFlags Flags { get; protected set; }
  81. /// <summary>
  82. /// The minimum version of the referenced plugin.
  83. /// </summary>
  84. public Version MinimumVersion { get; protected set; }
  85. /// <summary>
  86. /// Marks this <see cref="BaseUnityPlugin"/> as depenant on another plugin. The other plugin will be loaded before this one.
  87. /// If the other plugin doesn't exist, what happens depends on the <see cref="Flags"/> parameter.
  88. /// </summary>
  89. /// <param name="DependencyGUID">The GUID of the referenced plugin.</param>
  90. /// <param name="Flags">The flags associated with this dependency definition.</param>
  91. public BepInDependency(string DependencyGUID, DependencyFlags Flags = DependencyFlags.HardDependency)
  92. {
  93. this.DependencyGUID = DependencyGUID;
  94. this.Flags = Flags;
  95. MinimumVersion = new Version();
  96. }
  97. /// <summary>
  98. /// Marks this <see cref="BaseUnityPlugin"/> as depenant on another plugin. The other plugin will be loaded before this one.
  99. /// If the other plugin doesn't exist or is of a version below <see cref="MinimumVersion"/>, this plugin will not load and an error will be logged instead.
  100. /// </summary>
  101. /// <param name="DependencyGUID">The GUID of the referenced plugin.</param>
  102. /// <param name="MinimumDependencyVersion">The minimum version of the referenced plugin.</param>
  103. /// <remarks>When version is supplied the dependency is always treated as HardDependency</remarks>
  104. public BepInDependency(string DependencyGUID, string MinimumDependencyVersion) : this(DependencyGUID)
  105. {
  106. MinimumVersion = new Version(MinimumDependencyVersion);
  107. }
  108. internal static IEnumerable<BepInDependency> FromCecilType(TypeDefinition td)
  109. {
  110. var attrs = MetadataHelper.GetCustomAttributes<BepInDependency>(td, true);
  111. return attrs.Select(customAttribute =>
  112. {
  113. var dependencyGuid = (string)customAttribute.ConstructorArguments[0].Value;
  114. var secondArg = customAttribute.ConstructorArguments[1].Value;
  115. if (secondArg is string minVersion) return new BepInDependency(dependencyGuid, minVersion);
  116. return new BepInDependency(dependencyGuid, (DependencyFlags)secondArg);
  117. }).ToList();
  118. }
  119. void ICacheable.Save(BinaryWriter bw)
  120. {
  121. bw.Write(DependencyGUID);
  122. bw.Write((int)Flags);
  123. bw.Write(MinimumVersion.ToString());
  124. }
  125. void ICacheable.Load(BinaryReader br)
  126. {
  127. DependencyGUID = br.ReadString();
  128. Flags = (DependencyFlags)br.ReadInt32();
  129. MinimumVersion = new Version(br.ReadString());
  130. }
  131. }
  132. /// <summary>
  133. /// This attribute specifies other plugins that are incompatible with this plugin.
  134. /// </summary>
  135. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
  136. public class BepInIncompatibility : Attribute, ICacheable
  137. {
  138. /// <summary>
  139. /// The GUID of the referenced plugin.
  140. /// </summary>
  141. public string IncompatibilityGUID { get; protected set; }
  142. /// <summary>
  143. /// Marks this <see cref="BaseUnityPlugin"/> as incompatible with another plugin.
  144. /// If the other plugin exists, this plugin will not be loaded and a warning will be shown.
  145. /// </summary>
  146. /// <param name="IncompatibilityGUID">The GUID of the referenced plugin.</param>
  147. public BepInIncompatibility(string IncompatibilityGUID)
  148. {
  149. this.IncompatibilityGUID = IncompatibilityGUID;
  150. }
  151. internal static IEnumerable<BepInIncompatibility> FromCecilType(TypeDefinition td)
  152. {
  153. var attrs = MetadataHelper.GetCustomAttributes<BepInIncompatibility>(td, true);
  154. return attrs.Select(customAttribute =>
  155. {
  156. var dependencyGuid = (string)customAttribute.ConstructorArguments[0].Value;
  157. return new BepInIncompatibility(dependencyGuid);
  158. }).ToList();
  159. }
  160. void ICacheable.Save(BinaryWriter bw)
  161. {
  162. bw.Write(IncompatibilityGUID);
  163. }
  164. void ICacheable.Load(BinaryReader br)
  165. {
  166. IncompatibilityGUID = br.ReadString();
  167. }
  168. }
  169. /// <summary>
  170. /// This attribute specifies which processes this plugin should be run for. Not specifying this attribute will load the plugin under every process.
  171. /// </summary>
  172. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
  173. public class BepInProcess : Attribute
  174. {
  175. /// <summary>
  176. /// The name of the process that this plugin will run under.
  177. /// </summary>
  178. public string ProcessName { get; protected set; }
  179. /// <param name="ProcessName">The name of the process that this plugin will run under.</param>
  180. public BepInProcess(string ProcessName)
  181. {
  182. this.ProcessName = ProcessName;
  183. }
  184. internal static List<BepInProcess> FromCecilType(TypeDefinition td)
  185. {
  186. var attrs = MetadataHelper.GetCustomAttributes<BepInProcess>(td, true);
  187. return attrs.Select(customAttribute => new BepInProcess((string)customAttribute.ConstructorArguments[0].Value)).ToList();
  188. }
  189. }
  190. #endregion
  191. #region MetadataHelper
  192. /// <summary>
  193. /// Helper class to use for retrieving metadata about a plugin, defined as attributes.
  194. /// </summary>
  195. public static class MetadataHelper
  196. {
  197. internal static IEnumerable<CustomAttribute> GetCustomAttributes<T>(TypeDefinition td, bool inherit) where T : Attribute
  198. {
  199. var result = new List<CustomAttribute>();
  200. var type = typeof(T);
  201. var currentType = td;
  202. do
  203. {
  204. result.AddRange(currentType.CustomAttributes.Where(ca => ca.AttributeType.FullName == type.FullName));
  205. currentType = currentType.BaseType?.Resolve();
  206. } while (inherit && currentType?.FullName != "System.Object");
  207. return result;
  208. }
  209. /// <summary>
  210. /// Retrieves the BepInPlugin metadata from a plugin type.
  211. /// </summary>
  212. /// <param name="pluginType">The plugin type.</param>
  213. /// <returns>The BepInPlugin metadata of the plugin type.</returns>
  214. public static BepInPlugin GetMetadata(Type pluginType)
  215. {
  216. object[] attributes = pluginType.GetCustomAttributes(typeof(BepInPlugin), false);
  217. if (attributes.Length == 0)
  218. return null;
  219. return (BepInPlugin)attributes[0];
  220. }
  221. /// <summary>
  222. /// Retrieves the BepInPlugin metadata from a plugin instance.
  223. /// </summary>
  224. /// <param name="plugin">The plugin instance.</param>
  225. /// <returns>The BepInPlugin metadata of the plugin instance.</returns>
  226. public static BepInPlugin GetMetadata(object plugin)
  227. => GetMetadata(plugin.GetType());
  228. /// <summary>
  229. /// Gets the specified attributes of a type, if they exist.
  230. /// </summary>
  231. /// <typeparam name="T">The attribute type to retrieve.</typeparam>
  232. /// <param name="pluginType">The plugin type.</param>
  233. /// <returns>The attributes of the type, if existing.</returns>
  234. public static T[] GetAttributes<T>(Type pluginType) where T : Attribute
  235. {
  236. return (T[])pluginType.GetCustomAttributes(typeof(T), true);
  237. }
  238. /// <summary>
  239. /// Gets the specified attributes of an instance, if they exist.
  240. /// </summary>
  241. /// <typeparam name="T">The attribute type to retrieve.</typeparam>
  242. /// <param name="plugin">The plugin instance.</param>
  243. /// <returns>The attributes of the instance, if existing.</returns>
  244. public static IEnumerable<T> GetAttributes<T>(object plugin) where T : Attribute
  245. => GetAttributes<T>(plugin.GetType());
  246. /// <summary>
  247. /// Retrieves the dependencies of the specified plugin type.
  248. /// </summary>
  249. /// <param name="plugin">The plugin type.</param>
  250. /// <returns>A list of all plugin types that the specified plugin type depends upon.</returns>
  251. public static IEnumerable<BepInDependency> GetDependencies(Type plugin)
  252. {
  253. return plugin.GetCustomAttributes(typeof(BepInDependency), true).Cast<BepInDependency>();
  254. }
  255. }
  256. #endregion
  257. #region Build configuration
  258. /// <summary>
  259. /// This class is appended to AssemblyInfo.cs when BepInEx is built via a CI pipeline.
  260. /// 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.
  261. /// </summary>
  262. public class BuildInfoAttribute : Attribute
  263. {
  264. public string Info { get; }
  265. public BuildInfoAttribute(string info)
  266. {
  267. Info = info;
  268. }
  269. }
  270. #endregion
  271. }