123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace BepInEx
- {
- #region BaseUnityPlugin
-
-
-
- [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
- public class BepInPlugin : Attribute
- {
-
-
-
- public string GUID { get; protected set; }
-
-
-
-
- public string Name { get; protected set; }
-
-
-
-
- public Version Version { get; protected set; }
-
-
-
-
- public BepInPlugin(string GUID, string Name, string Version)
- {
- this.GUID = GUID;
- this.Name = Name;
- this.Version = new Version(Version);
- }
- }
-
-
-
- [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
- public class BepInDependency : Attribute
- {
- public enum DependencyFlags
- {
-
-
-
- HardDependency = 1,
-
-
-
- SoftDependency = 2,
- }
-
-
-
- public string DependencyGUID { get; protected set; }
-
-
-
- public DependencyFlags Flags { get; protected set; }
-
-
-
- public BepInDependency(string DependencyGUID, DependencyFlags Flags = DependencyFlags.HardDependency)
- {
- this.DependencyGUID = DependencyGUID;
- this.Flags = Flags;
- }
- }
-
-
-
- [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
- public class BepInProcess : Attribute
- {
-
-
-
- public string ProcessName { get; protected set; }
-
-
- public BepInProcess(string ProcessName)
- {
- this.ProcessName = ProcessName;
- }
- }
- #endregion
- #region MetadataHelper
-
-
-
- public static class MetadataHelper
- {
-
-
-
-
-
- public static BepInPlugin GetMetadata(object plugin)
- {
- return GetMetadata(plugin.GetType());
- }
-
-
-
-
-
-
- public static BepInPlugin GetMetadata(Type pluginType)
- {
- object[] attributes = pluginType.GetCustomAttributes(typeof(BepInPlugin), false);
- if (attributes.Length == 0)
- return null;
- return (BepInPlugin)attributes[0];
- }
-
-
-
-
-
-
- public static IEnumerable<T> GetAttributes<T>(object plugin) where T : Attribute
- {
- return GetAttributes<T>(plugin.GetType());
- }
-
-
-
-
-
-
-
- public static IEnumerable<T> GetAttributes<T>(Type pluginType) where T : Attribute
- {
- return pluginType.GetCustomAttributes(typeof(T), true).Cast<T>();
- }
-
-
-
-
-
-
- public static IEnumerable<Type> GetDependencies(Type Plugin, IEnumerable<Type> AllPlugins)
- {
- object[] attributes = Plugin.GetCustomAttributes(typeof(BepInDependency), true);
- List<Type> dependencyTypes = new List<Type>();
- foreach (BepInDependency dependency in attributes)
- {
- Type dependencyType = AllPlugins.FirstOrDefault(x => GetMetadata(x)?.GUID == dependency.DependencyGUID);
- if (dependencyType == null)
- {
- if ((dependency.Flags & BepInDependency.DependencyFlags.SoftDependency) != 0)
- continue;
- throw new MissingDependencyException("Cannot find dependency type.");
- }
-
- dependencyTypes.Add(dependencyType);
- }
- return dependencyTypes;
- }
- }
-
-
-
- public class MissingDependencyException : Exception
- {
- public MissingDependencyException(string message) : base(message)
- {
- }
- }
- #endregion
- #region Debug
- #if DEBUG
- public class DebugInfoAttribute : Attribute
- {
- public string Info { get; }
- public DebugInfoAttribute(string info)
- {
- Info = info;
- }
- }
- #endif
- #endregion
- }
|