Attributes.cs 9.0 KB

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