Attributes.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace BepInEx
  5. {
  6. #region BaseUnityPlugin
  7. /// <summary>
  8. /// This attribute denotes that a class is a plugin, and specifies the required metadata.
  9. /// </summary>
  10. [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
  11. public class BepInPlugin : Attribute
  12. {
  13. /// <summary>
  14. /// The unique identifier of the plugin. Should not change between plugin versions.
  15. /// </summary>
  16. public string GUID { get; protected set; }
  17. /// <summary>
  18. /// The user friendly name of the plugin. Is able to be changed between versions.
  19. /// </summary>
  20. public string Name { get; protected set; }
  21. /// <summary>
  22. /// The specfic version of the plugin.
  23. /// </summary>
  24. public Version Version { get; protected set; }
  25. /// <param name="GUID">The unique identifier of the plugin. Should not change between plugin versions.</param>
  26. /// <param name="Name">The user friendly name of the plugin. Is able to be changed between versions.</param>
  27. /// <param name="Version">The specfic version of the plugin.</param>
  28. public BepInPlugin(string GUID, string Name, string Version)
  29. {
  30. this.GUID = GUID;
  31. this.Name = Name;
  32. this.Version = new Version(Version);
  33. }
  34. }
  35. /// <summary>
  36. /// This attribute specifies any dependencies that this plugin has on other plugins.
  37. /// </summary>
  38. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
  39. public class BepInDependency : Attribute
  40. {
  41. public enum DependencyFlags
  42. {
  43. /// <summary>
  44. /// The plugin has a hard dependency on the referenced plugin, and will not run without it.
  45. /// </summary>
  46. HardDependency = 1,
  47. /// <summary>
  48. /// This plugin has a soft dependency on the referenced plugin, and is able to run without it.
  49. /// </summary>
  50. SoftDependency = 2,
  51. }
  52. /// <summary>
  53. /// The GUID of the referenced plugin.
  54. /// </summary>
  55. public string DependencyGUID { get; protected set; }
  56. /// <summary>
  57. /// The flags associated with this dependency definition.
  58. /// </summary>
  59. public DependencyFlags Flags { get; protected set; }
  60. /// <param name="DependencyGUID">The GUID of the referenced plugin.</param>
  61. /// <param name="Flags">The flags associated with this dependency definition.</param>
  62. public BepInDependency(string DependencyGUID, DependencyFlags Flags = DependencyFlags.HardDependency)
  63. {
  64. this.DependencyGUID = DependencyGUID;
  65. this.Flags = Flags;
  66. }
  67. }
  68. /// <summary>
  69. /// This attribute specifies which processes this plugin should be run for. Not specifying this attribute will load the plugin under every process.
  70. /// </summary>
  71. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
  72. public class BepInProcess : Attribute
  73. {
  74. /// <summary>
  75. /// The name of the process that this plugin will run under.
  76. /// </summary>
  77. public string ProcessName { get; protected set; }
  78. /// <param name="ProcessName">The name of the process that this plugin will run under.</param>
  79. public BepInProcess(string ProcessName)
  80. {
  81. this.ProcessName = ProcessName;
  82. }
  83. }
  84. #endregion
  85. #region MetadataHelper
  86. public static class MetadataHelper
  87. {
  88. public static BepInPlugin GetMetadata(object plugin)
  89. {
  90. return GetMetadata(plugin.GetType());
  91. }
  92. public static BepInPlugin GetMetadata(Type pluginType)
  93. {
  94. object[] attributes = pluginType.GetCustomAttributes(typeof(BepInPlugin), false);
  95. if (attributes.Length == 0)
  96. return null;
  97. return (BepInPlugin)attributes[0];
  98. }
  99. public static IEnumerable<T> GetAttributes<T>(object plugin) where T : Attribute
  100. {
  101. return GetAttributes<T>(plugin.GetType());
  102. }
  103. public static IEnumerable<T> GetAttributes<T>(Type pluginType) where T : Attribute
  104. {
  105. return pluginType.GetCustomAttributes(typeof(T), true).Cast<T>();
  106. }
  107. public static IEnumerable<Type> GetDependencies(Type Plugin, IEnumerable<Type> AllPlugins)
  108. {
  109. object[] attributes = Plugin.GetCustomAttributes(typeof(BepInDependency), true);
  110. List<Type> dependencyTypes = new List<Type>();
  111. foreach (BepInDependency dependency in attributes)
  112. {
  113. Type dependencyType = AllPlugins.FirstOrDefault(x => GetMetadata(x)?.GUID == dependency.DependencyGUID);
  114. if (dependencyType == null)
  115. {
  116. if ((dependency.Flags & BepInDependency.DependencyFlags.SoftDependency) != 0)
  117. continue; //skip on soft dependencies
  118. throw new MissingDependencyException("Cannot find dependency type.");
  119. }
  120. dependencyTypes.Add(dependencyType);
  121. }
  122. return dependencyTypes;
  123. }
  124. }
  125. public class MissingDependencyException : Exception
  126. {
  127. public MissingDependencyException(string message) : base(message)
  128. {
  129. }
  130. }
  131. #endregion
  132. }