123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using BepInEx.Bootstrap;
- using Mono.Cecil;
- namespace BepInEx
- {
- #region BaseUnityPlugin
-
-
-
- [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
- public class PluginMetadata : Attribute
- {
-
-
-
- public string GUID { get; protected set; }
-
-
-
- public string Name { get; protected set; }
-
-
-
- public Version Version { get; protected set; }
-
-
-
- public PluginMetadata(string GUID, string Name, string Version)
- {
- this.GUID = GUID;
- this.Name = Name;
- try
- {
- this.Version = new Version(Version);
- }
- catch
- {
- this.Version = null;
- }
- }
- internal static PluginMetadata FromCecilType(TypeDefinition td)
- {
- var attr = MetadataHelper.GetCustomAttributes<PluginMetadata>(td, false).FirstOrDefault()
- ?? MetadataHelper.GetCustomAttributes<BepInPlugin>(td, false).FirstOrDefault();
- if (attr == null)
- return null;
- return new PluginMetadata((string)attr.ConstructorArguments[0].Value, (string)attr.ConstructorArguments[1].Value, (string)attr.ConstructorArguments[2].Value);
- }
- }
-
-
-
- [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
- public class PluginDependency : Attribute, ICacheable
- {
- public enum DependencyFlags
- {
-
-
-
- HardDependency = 1,
-
-
-
- SoftDependency = 2,
- }
-
-
-
- public string DependencyGUID { get; protected set; }
-
-
-
- public DependencyFlags Flags { get; protected set; }
-
-
-
- public Version MinimumVersion { get; protected set; }
-
-
-
-
-
-
- public PluginDependency(string DependencyGUID, DependencyFlags Flags = DependencyFlags.HardDependency)
- {
- this.DependencyGUID = DependencyGUID;
- this.Flags = Flags;
- MinimumVersion = new Version();
- }
-
-
-
-
-
-
-
- public PluginDependency(string DependencyGUID, string MinimumDependencyVersion) : this(DependencyGUID)
- {
- MinimumVersion = new Version(MinimumDependencyVersion);
- }
- internal static IEnumerable<PluginDependency> FromCecilType(TypeDefinition td)
- {
- var attrs = MetadataHelper.GetCustomAttributes<PluginDependency>(td, true);
- return attrs.Select(customAttribute =>
- {
- var dependencyGuid = (string)customAttribute.ConstructorArguments[0].Value;
- var secondArg = customAttribute.ConstructorArguments[1].Value;
- if (secondArg is string minVersion) return new PluginDependency(dependencyGuid, minVersion);
- return new PluginDependency(dependencyGuid, (DependencyFlags)secondArg);
- }).ToList();
- }
- void ICacheable.Save(BinaryWriter bw)
- {
- bw.Write(DependencyGUID);
- bw.Write((int)Flags);
- bw.Write(MinimumVersion.ToString());
- }
- void ICacheable.Load(BinaryReader br)
- {
- DependencyGUID = br.ReadString();
- Flags = (DependencyFlags)br.ReadInt32();
- MinimumVersion = new Version(br.ReadString());
- }
- }
-
-
-
- [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
- public class PluginIncompatibility : Attribute, ICacheable
- {
-
-
-
- public string IncompatibilityGUID { get; protected set; }
-
-
-
-
-
-
- public PluginIncompatibility(string IncompatibilityGUID)
- {
- this.IncompatibilityGUID = IncompatibilityGUID;
- }
- internal static IEnumerable<PluginIncompatibility> FromCecilType(TypeDefinition td)
- {
- var attrs = MetadataHelper.GetCustomAttributes<PluginIncompatibility>(td, true);
- return attrs.Select(customAttribute =>
- {
- var dependencyGuid = (string)customAttribute.ConstructorArguments[0].Value;
- return new PluginIncompatibility(dependencyGuid);
- }).ToList();
- }
- void ICacheable.Save(BinaryWriter bw)
- {
- bw.Write(IncompatibilityGUID);
- }
- void ICacheable.Load(BinaryReader br)
- {
- IncompatibilityGUID = br.ReadString();
- }
- }
-
-
-
- [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
- public class ProcessFilter : Attribute
- {
-
-
-
- public string ProcessName { get; protected set; }
-
- public ProcessFilter(string ProcessName)
- {
- this.ProcessName = ProcessName;
- }
- internal static List<ProcessFilter> FromCecilType(TypeDefinition td)
- {
- var attrs = MetadataHelper.GetCustomAttributes<ProcessFilter>(td, true);
- return attrs.Select(customAttribute => new ProcessFilter((string)customAttribute.ConstructorArguments[0].Value)).ToList();
- }
- }
- #endregion
- #region Shims
- [Obsolete("Use PluginMetadata instead")]
- public class BepInPlugin : PluginMetadata
- {
- public BepInPlugin(string GUID, string Name, string Version) : base(GUID, Name, Version) { }
- }
- [Obsolete("Use PluginDependency instead")]
- public class BepInDependency : PluginDependency
- {
- public enum DependencyFlags
- {
-
-
-
- HardDependency = 1,
-
-
-
- SoftDependency = 2,
- }
- public BepInDependency(string DependencyGUID, DependencyFlags Flags = DependencyFlags.HardDependency) : base(DependencyGUID, (PluginDependency.DependencyFlags)(int)Flags) { }
- public BepInDependency(string DependencyGUID, string MinimumDependencyVersion) : base(DependencyGUID, MinimumDependencyVersion) { }
- }
- [Obsolete("Use PluginIncompatibility instead")]
- public class BepInIncompatibility : PluginIncompatibility
- {
- public BepInIncompatibility(string IncompatibilityGUID) : base(IncompatibilityGUID) { }
- }
- [Obsolete("Use ProcessFilter instead")]
- public class BepInProcess : ProcessFilter
- {
- public BepInProcess(string ProcessName) : base(ProcessName) { }
- }
- #endregion
- #region MetadataHelper
-
-
-
- public static class MetadataHelper
- {
- internal static IEnumerable<CustomAttribute> GetCustomAttributes<T>(TypeDefinition td, bool inherit) where T : Attribute
- {
- var result = new List<CustomAttribute>();
- var type = typeof(T);
- var currentType = td;
- do
- {
- result.AddRange(currentType.CustomAttributes.Where(ca => ca.AttributeType.FullName == type.FullName));
- currentType = currentType.BaseType?.Resolve();
- } while (inherit && currentType?.FullName != "System.Object");
- return result;
- }
-
-
-
-
-
- public static PluginMetadata GetMetadata(Type pluginType)
- {
- object[] attributes = pluginType.GetCustomAttributes(typeof(PluginMetadata), false)
- .Concat(pluginType.GetCustomAttributes(typeof(BepInPlugin), false))
- .ToArray();
- if (attributes.Length == 0)
- return null;
- return (PluginMetadata)attributes[0];
- }
-
-
-
-
-
- public static PluginMetadata GetMetadata(object plugin)
- => GetMetadata(plugin.GetType());
-
-
-
-
-
-
- public static T[] GetAttributes<T>(Type pluginType) where T : Attribute
- {
- return (T[])pluginType.GetCustomAttributes(typeof(T), true);
- }
-
-
-
-
-
-
- public static IEnumerable<T> GetAttributes<T>(object plugin) where T : Attribute
- => GetAttributes<T>(plugin.GetType());
-
-
-
-
-
- public static IEnumerable<PluginDependency> GetDependencies(Type plugin)
- {
- return plugin.GetCustomAttributes(typeof(PluginDependency), true).Cast<PluginDependency>();
- }
- }
- #endregion
- #region Build configuration
-
-
-
-
- internal class BuildInfoAttribute : Attribute
- {
- public string Info { get; }
- public BuildInfoAttribute(string info)
- {
- Info = info;
- }
- }
- #endregion
- }
|