Browse Source

Merge branch '5.0-rc2' of https://github.com/BepInEx/BepInEx into 5.0-rc2

ghorsington 5 years ago
parent
commit
e3252bc865

+ 1 - 1
BepInEx.Preloader/Logger/PreloaderLogWriter.cs

@@ -9,7 +9,7 @@ namespace BepInEx.Preloader
 {
 	public class PreloaderConsoleListener : ILogListener
 	{
-		public List<LogEventArgs> LogEvents { get; } = new List<LogEventArgs>();
+		public static List<LogEventArgs> LogEvents { get; } = new List<LogEventArgs>();
 		protected StringBuilder LogBuilder = new StringBuilder();
 
 		public static TextWriter StandardOut { get; set; }

+ 0 - 4
BepInEx.Preloader/Preloader.cs

@@ -205,10 +205,6 @@ namespace BepInEx.Preloader
 
 					il.InsertBefore(ins,
 						il.Create(OpCodes.Call, assembly.MainModule.ImportReference(
-							AccessTools.PropertyGetter(typeof(Preloader), nameof(PreloaderLog)))));
-
-					il.InsertBefore(ins,
-						il.Create(OpCodes.Call, assembly.MainModule.ImportReference(
 							AccessTools.PropertyGetter(typeof(PreloaderConsoleListener), nameof(PreloaderConsoleListener.LogEvents))))); // preloaderLogEvents (load from Preloader.PreloaderLog.LogEvents)
 
                     il.InsertBefore(ins,

+ 8 - 8
BepInEx/Bootstrap/Chainloader.cs

@@ -124,7 +124,7 @@ namespace BepInEx.Bootstrap
 
 		private static Regex allowedGuidRegex { get; } = new Regex(@"^[a-zA-Z0-9\._\-]+$");
 
-		public static PluginInfo ToPluginInfo(TypeDefinition type)
+		internal static PluginInfo ToPluginInfo(TypeDefinition type)
 		{
 			if (type.IsInterface || type.IsAbstract)
 				return null;
@@ -134,13 +134,13 @@ namespace BepInEx.Bootstrap
 				if (!type.IsSubtypeOf(typeof(BaseUnityPlugin)))
 					return null;
 			}
-			catch (AssemblyResolutionException)
+			catch (AssemblyResolutionException ex)
 			{
 				// Can happen if this type inherits a type from an assembly that can't be found. Safe to assume it's not a plugin.
 				return null;
 			}
 
-			var metadata = BepInPlugin.FromCecilType(type);
+			var metadata = PluginMetadata.FromCecilType(type);
 
 			// Perform checks that will prevent the plugin from being loaded in ALL cases
 			if (metadata == null)
@@ -167,9 +167,9 @@ namespace BepInEx.Bootstrap
 				return null;
 			}
 
-			var filters = BepInProcess.FromCecilType(type);
-			var dependencies = BepInDependency.FromCecilType(type);
-			var incompatibilities = BepInIncompatibility.FromCecilType(type);
+			var filters = ProcessFilter.FromCecilType(type);
+			var dependencies = PluginDependency.FromCecilType(type);
+			var incompatibilities = PluginIncompatibility.FromCecilType(type);
 
 			return new PluginInfo
 			{
@@ -292,7 +292,7 @@ namespace BepInEx.Bootstrap
 						continue;
 
 					var dependsOnInvalidPlugin = false;
-					var missingDependencies = new List<BepInDependency>();
+					var missingDependencies = new List<PluginDependency>();
 					foreach (var dependency in pluginInfo.Dependencies)
 					{
 						// If the depenency wasn't already processed, it's missing altogether
@@ -300,7 +300,7 @@ namespace BepInEx.Bootstrap
 						if (!depenencyExists || pluginVersion < dependency.MinimumVersion)
 						{
 							// If the dependency is hard, collect it into a list to show
-							if ((dependency.Flags & BepInDependency.DependencyFlags.HardDependency) != 0)
+							if ((dependency.Flags & PluginDependency.DependencyFlags.HardDependency) != 0)
 								missingDependencies.Add(dependency);
 							continue;
 						}

+ 55 - 5
BepInEx/Configuration/ConfigFile.cs

@@ -14,7 +14,7 @@ namespace BepInEx.Configuration
 	/// </summary>
 	public class ConfigFile : IDictionary<ConfigDefinition, ConfigEntryBase>
 	{
-		private readonly BepInPlugin _ownerMetadata;
+		private readonly PluginMetadata _ownerMetadata;
 
 		internal static ConfigFile CoreConfig { get; } = new ConfigFile(Paths.BepInExConfigPath, true);
 
@@ -39,6 +39,18 @@ namespace BepInEx.Configuration
 				}
 			}
 		}
+		
+		/// <summary>
+		/// Create an array with all config entries inside of this config file. Should be only used for metadata purposes.
+		/// If you want to access and modify an existing setting then use <see cref="AddSetting{T}(ConfigDefinition,T,ConfigDescription)"/> 
+		/// instead with no description.
+		/// </summary>
+		[Obsolete("Use Values instead")]
+		public ConfigEntryBase[] GetConfigEntries()
+		{
+			lock (_ioLock)
+				return Entries.Values.ToArray();
+		}
 
 		/// <summary>
 		/// Full path to the config file. The file might not exist until a setting is added and changed, or <see cref="Save"/> is called.
@@ -51,7 +63,7 @@ namespace BepInEx.Configuration
 		/// </summary>
 		public bool SaveOnConfigSet { get; set; } = true;
 
-		/// <inheritdoc cref="ConfigFile(string, bool, BepInPlugin)"/>
+		/// <inheritdoc cref="ConfigFile(string, bool, PluginMetadata)"/>
 		public ConfigFile(string configPath, bool saveOnInit) : this(configPath, saveOnInit, null) { }
 
 		/// <summary>
@@ -60,7 +72,7 @@ namespace BepInEx.Configuration
 		/// <param name="configPath">Full path to a file that contains settings. The file will be created as needed.</param>
 		/// <param name="saveOnInit">If the config file/directory doesn't exist, create it immediately.</param>
 		/// <param name="ownerMetadata">Information about the plugin that owns this setting file.</param>
-		public ConfigFile(string configPath, bool saveOnInit, BepInPlugin ownerMetadata)
+		public ConfigFile(string configPath, bool saveOnInit, PluginMetadata ownerMetadata)
 		{
 			_ownerMetadata = ownerMetadata;
 
@@ -269,8 +281,46 @@ namespace BepInEx.Configuration
 		public ConfigEntry<T> Bind<T>(string section, string key, T defaultValue, string description)
 			=> Bind(new ConfigDefinition(section, key), defaultValue, new ConfigDescription(description));
 
+		/// <summary>
+		/// Create a new setting. The setting is saved to drive and loaded automatically.
+		/// Each definition can be used to add only one setting, trying to add a second setting will throw an exception.
+		/// </summary>
+		/// <typeparam name="T">Type of the value contained in this setting.</typeparam>
+		/// <param name="configDefinition">Section and Key of the setting.</param>
+		/// <param name="defaultValue">Value of the setting if the setting was not created yet.</param>
+		/// <param name="configDescription">Description of the setting shown to the user and other metadata.</param>
+		[Obsolete("Use bind instead")]
+		public ConfigEntry<T> AddSetting<T>(ConfigDefinition configDefinition, T defaultValue, ConfigDescription configDescription = null)
+			=> Bind(configDefinition, defaultValue, configDescription);
+
+		/// <summary>
+		/// Create a new setting. The setting is saved to drive and loaded automatically.
+		/// Each section and key pair can be used to add only one setting, trying to add a second setting will throw an exception.
+		/// </summary>
+		/// <typeparam name="T">Type of the value contained in this setting.</typeparam>
+		/// <param name="section">Section/category/group of the setting. Settings are grouped by this.</param>
+		/// <param name="key">Name of the setting.</param>
+		/// <param name="defaultValue">Value of the setting if the setting was not created yet.</param>
+		/// <param name="configDescription">Description of the setting shown to the user and other metadata.</param>
+		[Obsolete("Use bind instead")]
+		public ConfigEntry<T> AddSetting<T>(string section, string key, T defaultValue, ConfigDescription configDescription = null)
+			=> Bind(new ConfigDefinition(section, key), defaultValue, configDescription);
+
+		/// <summary>
+		/// Create a new setting. The setting is saved to drive and loaded automatically.
+		/// Each section and key pair can be used to add only one setting, trying to add a second setting will throw an exception.
+		/// </summary>
+		/// <typeparam name="T">Type of the value contained in this setting.</typeparam>
+		/// <param name="section">Section/category/group of the setting. Settings are grouped by this.</param>
+		/// <param name="key">Name of the setting.</param>
+		/// <param name="defaultValue">Value of the setting if the setting was not created yet.</param>
+		/// <param name="description">Simple description of the setting shown to the user.</param>
+		[Obsolete("Use bind instead")]
+		public ConfigEntry<T> AddSetting<T>(string section, string key, T defaultValue, string description)
+			=> Bind(new ConfigDefinition(section, key), defaultValue, new ConfigDescription(description));
+
         /// <summary>
-        /// Access a setting. Use Bind and GetSetting instead.
+        /// Access a setting. Use Bind instead.
         /// </summary>
         [Obsolete("Use Bind instead")]
 		public ConfigWrapper<T> Wrap<T>(string section, string key, string description = null, T defaultValue = default(T))
@@ -284,7 +334,7 @@ namespace BepInEx.Configuration
 		}
 
 		/// <summary>
-		/// Access a setting. Use Bind and GetSetting instead.
+		/// Access a setting. Use Bind instead.
 		/// </summary>
 		[Obsolete("Use Bind instead")]
 		public ConfigWrapper<T> Wrap<T>(ConfigDefinition configDefinition, T defaultValue = default(T))

+ 77 - 32
BepInEx/Contract/Attributes.cs

@@ -13,7 +13,7 @@ namespace BepInEx
 	/// This attribute denotes that a class is a plugin, and specifies the required metadata.
 	/// </summary>
 	[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
-	public class BepInPlugin : Attribute
+	public class PluginMetadata : Attribute
 	{
 		/// <summary>
 		/// The unique identifier of the plugin. Should not change between plugin versions.
@@ -35,7 +35,7 @@ namespace BepInEx
 		/// <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)
+		public PluginMetadata(string GUID, string Name, string Version)
 		{
 			this.GUID = GUID;
 			this.Name = Name;
@@ -50,14 +50,15 @@ namespace BepInEx
 			}
 		}
 
-		internal static BepInPlugin FromCecilType(TypeDefinition td)
+		internal static PluginMetadata FromCecilType(TypeDefinition td)
 		{
-			var attr = MetadataHelper.GetCustomAttributes<BepInPlugin>(td, false).FirstOrDefault();
+			var attr = MetadataHelper.GetCustomAttributes<PluginMetadata>(td, false).FirstOrDefault()
+				 ?? MetadataHelper.GetCustomAttributes<BepInPlugin>(td, false).FirstOrDefault();
 
 			if (attr == null)
 				return null;
 
-			return new BepInPlugin((string)attr.ConstructorArguments[0].Value, (string)attr.ConstructorArguments[1].Value, (string)attr.ConstructorArguments[2].Value);
+			return new PluginMetadata((string)attr.ConstructorArguments[0].Value, (string)attr.ConstructorArguments[1].Value, (string)attr.ConstructorArguments[2].Value);
 		}
 	}
 
@@ -65,7 +66,7 @@ namespace BepInEx
 	/// This attribute specifies any dependencies that this plugin has on other plugins.
 	/// </summary>
 	[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
-	public class BepInDependency : Attribute, ICacheable
+	public class PluginDependency : Attribute, ICacheable
 	{
 		public enum DependencyFlags
 		{
@@ -101,7 +102,7 @@ namespace BepInEx
 		/// </summary>
 		/// <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)
+		public PluginDependency(string DependencyGUID, DependencyFlags Flags = DependencyFlags.HardDependency)
 		{
 			this.DependencyGUID = DependencyGUID;
 			this.Flags = Flags;
@@ -115,20 +116,20 @@ namespace BepInEx
 		/// <param name="DependencyGUID">The GUID of the referenced plugin.</param>
 		/// <param name="MinimumDependencyVersion">The minimum version of the referenced plugin.</param>
 		/// <remarks>When version is supplied the dependency is always treated as HardDependency</remarks>
-		public BepInDependency(string DependencyGUID, string MinimumDependencyVersion) : this(DependencyGUID)
+		public PluginDependency(string DependencyGUID, string MinimumDependencyVersion) : this(DependencyGUID)
 		{
 			MinimumVersion = new Version(MinimumDependencyVersion);
 		}
 
-		internal static IEnumerable<BepInDependency> FromCecilType(TypeDefinition td)
+		internal static IEnumerable<PluginDependency> FromCecilType(TypeDefinition td)
 		{
-			var attrs = MetadataHelper.GetCustomAttributes<BepInDependency>(td, true);
+			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 BepInDependency(dependencyGuid, minVersion);
-				return new BepInDependency(dependencyGuid, (DependencyFlags)secondArg);
+				if (secondArg is string minVersion) return new PluginDependency(dependencyGuid, minVersion);
+				return new PluginDependency(dependencyGuid, (DependencyFlags)secondArg);
 			}).ToList();
 		}
 
@@ -151,7 +152,7 @@ namespace BepInEx
 	/// This attribute specifies other plugins that are incompatible with this plugin.
 	/// </summary>
 	[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
-	public class BepInIncompatibility : Attribute, ICacheable
+	public class PluginIncompatibility : Attribute, ICacheable
 	{
 		/// <summary>
 		/// The GUID of the referenced plugin.
@@ -163,18 +164,18 @@ namespace BepInEx
 		/// If the other plugin exists, this plugin will not be loaded and a warning will be shown.
 		/// </summary>
 		/// <param name="IncompatibilityGUID">The GUID of the referenced plugin.</param>
-		public BepInIncompatibility(string IncompatibilityGUID)
+		public PluginIncompatibility(string IncompatibilityGUID)
 		{
 			this.IncompatibilityGUID = IncompatibilityGUID;
 		}
 
-		internal static IEnumerable<BepInIncompatibility> FromCecilType(TypeDefinition td)
+		internal static IEnumerable<PluginIncompatibility> FromCecilType(TypeDefinition td)
 		{
-			var attrs = MetadataHelper.GetCustomAttributes<BepInIncompatibility>(td, true);
+			var attrs = MetadataHelper.GetCustomAttributes<PluginIncompatibility>(td, true);
 			return attrs.Select(customAttribute =>
 			{
 				var dependencyGuid = (string)customAttribute.ConstructorArguments[0].Value;
-				return new BepInIncompatibility(dependencyGuid);
+				return new PluginIncompatibility(dependencyGuid);
 			}).ToList();
 		}
 
@@ -193,7 +194,7 @@ namespace BepInEx
 	/// 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
+	public class ProcessFilter : Attribute
 	{
 		/// <summary>
 		/// The name of the process that this plugin will run under.
@@ -201,20 +202,62 @@ namespace BepInEx
 		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)
+		public ProcessFilter(string ProcessName)
 		{
 			this.ProcessName = ProcessName;
 		}
 
-		internal static List<BepInProcess> FromCecilType(TypeDefinition td)
+		internal static List<ProcessFilter> FromCecilType(TypeDefinition td)
 		{
-			var attrs = MetadataHelper.GetCustomAttributes<BepInProcess>(td, true);
-			return attrs.Select(customAttribute => new BepInProcess((string)customAttribute.ConstructorArguments[0].Value)).ToList();
+			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
+		{
+			/// <summary>
+			/// The plugin has a hard dependency on the referenced plugin, and will not run without it.
+			/// </summary>
+			HardDependency = 1,
+
+			/// <summary>
+			/// This plugin has a soft dependency on the referenced plugin, and is able to run without it.
+			/// </summary>
+			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
 
 	/// <summary>
@@ -239,26 +282,28 @@ namespace BepInEx
 		}
 
 		/// <summary>
-		/// Retrieves the BepInPlugin metadata from a plugin type.
+		/// Retrieves the PluginMetadata metadata from a plugin type.
 		/// </summary>
 		/// <param name="pluginType">The plugin type.</param>
-		/// <returns>The BepInPlugin metadata of the plugin type.</returns>
-		public static BepInPlugin GetMetadata(Type pluginType)
+		/// <returns>The PluginMetadata metadata of the plugin type.</returns>
+		public static PluginMetadata GetMetadata(Type pluginType)
 		{
-			object[] attributes = pluginType.GetCustomAttributes(typeof(BepInPlugin), false);
+			object[] attributes = pluginType.GetCustomAttributes(typeof(PluginMetadata), false)
+											.Concat(pluginType.GetCustomAttributes(typeof(BepInPlugin), false))
+											.ToArray();
 
 			if (attributes.Length == 0)
 				return null;
 
-			return (BepInPlugin)attributes[0];
+			return (PluginMetadata)attributes[0];
 		}
 
 		/// <summary>
-		/// Retrieves the BepInPlugin metadata from a plugin instance.
+		/// Retrieves the PluginMetadata metadata from a plugin instance.
 		/// </summary>
 		/// <param name="plugin">The plugin instance.</param>
-		/// <returns>The BepInPlugin metadata of the plugin instance.</returns>
-		public static BepInPlugin GetMetadata(object plugin)
+		/// <returns>The PluginMetadata metadata of the plugin instance.</returns>
+		public static PluginMetadata GetMetadata(object plugin)
 			=> GetMetadata(plugin.GetType());
 
 		/// <summary>
@@ -286,9 +331,9 @@ namespace BepInEx
 		/// </summary>
 		/// <param name="Plugin">The plugin type.</param>
 		/// <returns>A list of all plugin types that the specified plugin type depends upon.</returns>
-		public static IEnumerable<BepInDependency> GetDependencies(Type plugin)
+		public static IEnumerable<PluginDependency> GetDependencies(Type plugin)
 		{
-			return plugin.GetCustomAttributes(typeof(BepInDependency), true).Cast<BepInDependency>();
+			return plugin.GetCustomAttributes(typeof(PluginDependency), true).Cast<PluginDependency>();
 		}
 	}
 

+ 3 - 3
BepInEx/Contract/BaseUnityPlugin.cs

@@ -29,12 +29,12 @@ namespace BepInEx
         /// <summary>
         /// Create a new instance of a plugin and all of its tied in objects.
         /// </summary>
-        /// <exception cref="InvalidOperationException">BepInPlugin attribute is missing.</exception>
+        /// <exception cref="InvalidOperationException">PluginMetadata attribute is missing.</exception>
         protected BaseUnityPlugin()
 		{
 			var metadata = MetadataHelper.GetMetadata(this);
 			if(metadata == null)
-				throw new InvalidOperationException("Can't create an instance of " + GetType().FullName + " because it inherits from BaseUnityPlugin and the BepInPlugin attribute is missing.");
+				throw new InvalidOperationException("Can't create an instance of " + GetType().FullName + " because it inherits from BaseUnityPlugin and the PluginMetadata attribute is missing.");
 
 			if (Chainloader.PluginInfos.TryGetValue(metadata.GUID, out var info))
 				Info = info;
@@ -45,7 +45,7 @@ namespace BepInEx
 					Metadata = metadata,
 					Instance = this,
 					Dependencies = MetadataHelper.GetDependencies(GetType()),
-					Processes = MetadataHelper.GetAttributes<BepInProcess>(GetType()),
+					Processes = MetadataHelper.GetAttributes<ProcessFilter>(GetType()),
 					Location = GetType().Assembly.Location
 				};
 			}

+ 11 - 11
BepInEx/Contract/PluginInfo.cs

@@ -7,13 +7,13 @@ namespace BepInEx
 {
 	public class PluginInfo : ICacheable
 	{
-		public BepInPlugin Metadata { get; internal set; }
+		public PluginMetadata Metadata { get; internal set; }
 
-		public IEnumerable<BepInProcess> Processes { get; internal set; }
+		public IEnumerable<ProcessFilter> Processes { get; internal set; }
 
-		public IEnumerable<BepInDependency> Dependencies { get; internal set; }
+		public IEnumerable<PluginDependency> Dependencies { get; internal set; }
 
-		public IEnumerable<BepInIncompatibility> Incompatibilities { get; internal set; }
+		public IEnumerable<PluginIncompatibility> Incompatibilities { get; internal set; }
 
 		public string Location { get; internal set; }
 
@@ -49,19 +49,19 @@ namespace BepInEx
 		{
 			TypeName = br.ReadString();
 
-			Metadata = new BepInPlugin(br.ReadString(), br.ReadString(), br.ReadString());
+			Metadata = new PluginMetadata(br.ReadString(), br.ReadString(), br.ReadString());
 
 			var processListCount = br.ReadInt32();
-			var processList = new List<BepInProcess>(processListCount);
+			var processList = new List<ProcessFilter>(processListCount);
 			for (int i = 0; i < processListCount; i++)
-				processList.Add(new BepInProcess(br.ReadString()));
+				processList.Add(new ProcessFilter(br.ReadString()));
 			Processes = processList;
 
 			var depCount = br.ReadInt32();
-			var depList = new List<BepInDependency>(depCount);
+			var depList = new List<PluginDependency>(depCount);
 			for (int i = 0; i < depCount; i++)
 			{
-				var dep = new BepInDependency("");
+				var dep = new PluginDependency("");
 				((ICacheable)dep).Load(br);
 				depList.Add(dep);
 			}
@@ -69,10 +69,10 @@ namespace BepInEx
 			Dependencies = depList;
 
 			var incCount = br.ReadInt32();
-			var incList = new List<BepInIncompatibility>(incCount);
+			var incList = new List<PluginIncompatibility>(incCount);
 			for (int i = 0; i < incCount; i++)
 			{
-				var inc = new BepInIncompatibility("");
+				var inc = new PluginIncompatibility("");
 				((ICacheable)inc).Load(br);
 				incList.Add(inc);
 			}