Attributes.cs 11 KB

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