Attributes.cs 12 KB

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