Browse Source

Add a BepInProcess atribute to specify process filters for plugins

Bepis 6 years ago
parent
commit
ab43bc2ebd

+ 2 - 2
BepInEx.Patcher/Properties/AssemblyInfo.cs

@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
 // You can specify all the values or you can default the Build and Revision Numbers
 // by using the '*' as shown below:
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("3.1.0.0")]
-[assembly: AssemblyFileVersion("3.1.0.0")]
+[assembly: AssemblyVersion("3.2.0.0")]
+[assembly: AssemblyFileVersion("3.2.0.0")]

+ 45 - 3
BepInEx/Attributes.cs

@@ -2,6 +2,9 @@
 
 namespace BepInEx
 {
+    /// <summary>
+    /// This attribute denotes that a class is a plugin, and specifies the required metadata.
+    /// </summary>
     [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
     public class BepInPlugin : Attribute
     {
@@ -21,7 +24,10 @@ namespace BepInEx
         /// The specfic version of the plugin.
         /// </summary>
         public Version Version { get; protected set; }
-
+        
+        /// <param name="GUID">The unique identifier of the plugin. Should not change between plugin versions.</param>
+        /// <param name="Name">The user friendly name of the plugin. Is able to be changed between versions.</param>
+        /// <param name="Version">The specfic version of the plugin.</param>
         public BepInPlugin(string GUID, string Name, string Version)
         {
             this.GUID = GUID;
@@ -30,23 +36,59 @@ namespace BepInEx
         }
     }
 
+    /// <summary>
+    /// This attribute specifies any dependencies that this plugin has on other plugins.
+    /// </summary>
     [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
     public class BepInDependency : Attribute
     {
         public enum DependencyFlags
         {
+            /// <summary>
+            /// The plugin has a hard dependency on the referenced plugin, and will not run without it.
+            /// </summary>
             HardDependency = 1,
-            SoftDependency = 2
+
+            /// <summary>
+            /// This plugin has a soft dependency on the referenced plugin, and is able to run without it.
+            /// </summary>
+            SoftDependency = 2,
         }
 
+        /// <summary>
+        /// The GUID of the referenced plugin.
+        /// </summary>
         public string DependencyGUID { get; protected set; }
 
+        /// <summary>
+        /// The flags associated with this dependency definition.
+        /// </summary>
         public DependencyFlags Flags { get; protected set; }
-
+        
+        /// <param name="DependencyGUID">The GUID of the referenced plugin.</param>
+        /// <param name="Flags">The flags associated with this dependency definition.</param>
         public BepInDependency(string DependencyGUID, DependencyFlags Flags = DependencyFlags.HardDependency)
         {
             this.DependencyGUID = DependencyGUID;
             this.Flags = Flags;
         }
     }
+
+    /// <summary>
+    /// This attribute specifies which processes this plugin should be run for. Not specifying this attribute will load the plugin under every process.
+    /// </summary>
+    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
+    public class BepInProcess : Attribute
+    {
+        /// <summary>
+        /// The name of the process that this plugin will run under.
+        /// </summary>
+        public string ProcessName { get; protected set; }
+        
+        /// <param name="ProcessName">The name of the process that this plugin will run under.</param>
+        public BepInProcess(string ProcessName)
+        {
+            this.ProcessName = ProcessName;
+        }
+    }
 }

+ 18 - 4
BepInEx/Chainloader.cs

@@ -1,6 +1,7 @@
 using BepInEx.Common;
 using System;
 using System.Collections.Generic;
+using System.Diagnostics;
 using System.IO;
 using System.Linq;
 using System.Reflection;
@@ -55,14 +56,27 @@ namespace BepInEx
 			try
 			{
 				BepInLogger.Log($"BepInEx {Assembly.GetExecutingAssembly().GetName().Version}");
-				BepInLogger.Log($"Chainloader started");
+				BepInLogger.Log("Chainloader started");
 
 				UnityEngine.Object.DontDestroyOnLoad(ManagerObject);
-                
 
-				var pluginTypes = TypeLoader.LoadTypes<BaseUnityPlugin>(Common.Utility.PluginsDirectory).ToList();
 
-				BepInLogger.Log($"{pluginTypes.Count} plugins found");
+			    string currentProcess = Process.GetCurrentProcess().ProcessName.ToLower();
+
+				var pluginTypes = TypeLoader.LoadTypes<BaseUnityPlugin>(Utility.PluginsDirectory)
+				    .Where(plugin =>
+				    {
+                        //Perform a filter for currently running process
+				        var filters = TypeLoader.GetAttributes<BepInProcess>(plugin);
+
+				        if (!filters.Any())
+				            return true;
+
+				        return filters.All(x => x.ProcessName.ToLower().Replace(".exe", "") == currentProcess);
+				    })
+				    .ToList();
+
+				BepInLogger.Log($"{pluginTypes.Count} plugins selected");
 
 				Dictionary<Type, IEnumerable<Type>> dependencyDict = new Dictionary<Type, IEnumerable<Type>>();
 

+ 3 - 3
BepInEx/Properties/AssemblyInfo.cs

@@ -6,7 +6,7 @@ using System.Runtime.InteropServices;
 // set of attributes. Change these attribute values to modify the information
 // associated with an assembly.
 [assembly: AssemblyTitle("BepInEx")]
-[assembly: AssemblyDescription("")]
+[assembly: AssemblyDescription("Unity plugin injection framework")]
 [assembly: AssemblyConfiguration("")]
 [assembly: AssemblyCompany("")]
 [assembly: AssemblyProduct("BepInEx")]
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
 // You can specify all the values or you can default the Build and Revision Numbers
 // by using the '*' as shown below:
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("3.1.0.0")]
-[assembly: AssemblyFileVersion("3.1.0.0")]
+[assembly: AssemblyVersion("3.2.0.0")]
+[assembly: AssemblyFileVersion("3.2.0.0")]

+ 14 - 4
BepInEx/TypeLoader.cs

@@ -46,14 +46,14 @@ namespace BepInEx
             return types;
         }
 
-        public static BepInPlugin GetMetadata(object Plugin)
+        public static BepInPlugin GetMetadata(object plugin)
         {
-            return GetMetadata(Plugin.GetType());
+            return GetMetadata(plugin.GetType());
         }
 
-        public static BepInPlugin GetMetadata(Type PluginType)
+        public static BepInPlugin GetMetadata(Type pluginType)
         {
-            object[] attributes = PluginType.GetCustomAttributes(typeof(BepInPlugin), false);
+            object[] attributes = pluginType.GetCustomAttributes(typeof(BepInPlugin), false);
 
             if (attributes.Length == 0)
                 return null;
@@ -61,6 +61,16 @@ namespace BepInEx
             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);