Browse Source

Split into separate classes

ManlyMarco 4 years ago
parent
commit
c85cfabee1
3 changed files with 150 additions and 147 deletions
  1. 2 0
      BepInEx/BepInEx.csproj
  2. 1 147
      BepInEx/Configuration/ConfigEntry.cs
  3. 147 0
      BepInEx/Configuration/ConfigEntryBase.cs

+ 2 - 0
BepInEx/BepInEx.csproj

@@ -52,6 +52,7 @@
     <Compile Include="Configuration\AcceptableValueBase.cs" />
     <Compile Include="Configuration\AcceptableValueList.cs" />
     <Compile Include="Configuration\AcceptableValueRange.cs" />
+    <Compile Include="Configuration\ConfigEntryBase.cs" />
     <Compile Include="Contract\PluginInfo.cs" />
     <Compile Include="Configuration\ConfigDefinition.cs" />
     <Compile Include="Configuration\ConfigDescription.cs" />
@@ -85,6 +86,7 @@
     <Compile Include="Paths.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Bootstrap\TypeLoader.cs" />
+    <Compile Include="ThreadingHelper.cs" />
     <Compile Include="Utility.cs" />
   </ItemGroup>
   <ItemGroup>

+ 1 - 147
BepInEx/Configuration/ConfigEntry.cs

@@ -1,151 +1,5 @@
-using System;
-using System.IO;
-using System.Linq;
-using BepInEx.Logging;
-
-namespace BepInEx.Configuration
+namespace BepInEx.Configuration
 {
-	/// <summary>
-	/// Container for a single setting of a <see cref="Configuration.ConfigFile"/>. 
-	/// Each config entry is linked to one config file.
-	/// </summary>
-	public abstract class ConfigEntryBase
-	{
-		/// <summary>
-		/// Types of defaultValue and definition.AcceptableValues have to be the same as settingType.
-		/// </summary>
-		internal ConfigEntryBase(ConfigFile configFile, ConfigDefinition definition, Type settingType, object defaultValue)
-		{
-			ConfigFile = configFile ?? throw new ArgumentNullException(nameof(configFile));
-			Definition = definition ?? throw new ArgumentNullException(nameof(definition));
-			SettingType = settingType ?? throw new ArgumentNullException(nameof(settingType));
-
-			// Free type check
-			Value = defaultValue;
-			DefaultValue = defaultValue;
-		}
-
-		/// <summary>
-		/// Config file this entry is a part of.
-		/// </summary>
-		public ConfigFile ConfigFile { get; }
-
-		/// <summary>
-		/// Category and name of this setting. Used as a unique key for identification within a <see cref="Configuration.ConfigFile"/>.
-		/// </summary>
-		public ConfigDefinition Definition { get; }
-
-		/// <summary>
-		/// Description / metadata of this setting.
-		/// </summary>
-		public ConfigDescription Description { get; private set; }
-
-		/// <summary>
-		/// Type of the <see cref="Value"/> that this setting holds.
-		/// </summary>
-		public Type SettingType { get; }
-
-		/// <summary>
-		/// Default value of this setting (set only if the setting was not changed before).
-		/// </summary>
-		public object DefaultValue { get; }
-
-		/// <summary>
-		/// Get or set the value of the setting.
-		/// </summary>
-		public abstract object Value { get; set; }
-
-		/// <summary>
-		/// Get the serialized representation of the value.
-		/// </summary>
-		public string GetSerializedValue()
-		{
-			return TomlTypeConverter.ConvertToString(Value, SettingType);
-		}
-
-		/// <summary>
-		/// Set the value by using its serialized form.
-		/// </summary>
-		public void SetSerializedValue(string value)
-		{
-			try
-			{
-				var newValue = TomlTypeConverter.ConvertToValue(value, SettingType);
-				Value = newValue;
-			}
-			catch (Exception e)
-			{
-				Logger.Log(LogLevel.Warning, $"Config value of setting \"{Definition}\" could not be " +
-											 $"parsed and will be ignored. Reason: {e.Message}; Value: {value}");
-			}
-		}
-
-		internal void SetDescription(ConfigDescription configDescription)
-		{
-			if (configDescription == null) throw new ArgumentNullException(nameof(configDescription));
-			if (configDescription.AcceptableValues != null && !SettingType.IsAssignableFrom(configDescription.AcceptableValues.ValueType))
-				throw new ArgumentException("configDescription.AcceptableValues is for a different type than the type of this setting");
-
-			Description = configDescription;
-
-			// Automatically calls ClampValue in case it changed
-			Value = Value;
-		}
-
-		/// <summary>
-		/// If necessary, clamp the value to acceptable value range. T has to be equal to settingType.
-		/// </summary>
-		protected T ClampValue<T>(T value)
-		{
-			if (Description?.AcceptableValues != null)
-				return (T)Description.AcceptableValues.Clamp(value);
-			return value;
-		}
-
-		/// <summary>
-		/// Trigger setting changed event.
-		/// </summary>
-		protected void OnSettingChanged(object sender)
-		{
-			ConfigFile.OnSettingChanged(sender, this);
-		}
-
-		/// <summary>
-		/// Write a description of this setting using all available metadata.
-		/// </summary>
-		public void WriteDescription(StreamWriter writer)
-		{
-			bool hasDescription = Description != null;
-			bool hasType = SettingType != null;
-
-			if (hasDescription)
-				writer.WriteLine(Description.ToSerializedString());
-
-			if (hasType)
-			{
-				if (SettingType.IsEnum && SettingType.GetCustomAttributes(typeof(FlagsAttribute), true).Any())
-					writer.WriteLine("# Multiple values can be set at the same time by separating them with , (e.g. Debug, Warning)");
-
-				writer.WriteLine("# Setting type: " + SettingType.Name);
-
-				writer.WriteLine("# Default value: " + DefaultValue);
-			}
-
-			if (hasDescription && Description.AcceptableValues != null)
-			{
-				writer.WriteLine(Description.AcceptableValues.ToSerializedString());
-			}
-			else if (hasType)
-			{
-				/*if (SettingType == typeof(bool))
-					writer.WriteLine("# Acceptable values: True, False");
-				else*/
-				if (SettingType.IsEnum)
-					writer.WriteLine("# Acceptable values: " + string.Join(", ", Enum.GetNames(SettingType)));
-			}
-		}
-	}
-
 	/// <inheritdoc />
 	public class ConfigEntry<T> : ConfigEntryBase
 	{

+ 147 - 0
BepInEx/Configuration/ConfigEntryBase.cs

@@ -0,0 +1,147 @@
+using System;
+using System.IO;
+using System.Linq;
+using BepInEx.Logging;
+
+namespace BepInEx.Configuration {
+	/// <summary>
+	/// Container for a single setting of a <see cref="Configuration.ConfigFile"/>. 
+	/// Each config entry is linked to one config file.
+	/// </summary>
+	public abstract class ConfigEntryBase
+	{
+		/// <summary>
+		/// Types of defaultValue and definition.AcceptableValues have to be the same as settingType.
+		/// </summary>
+		internal ConfigEntryBase(ConfigFile configFile, ConfigDefinition definition, Type settingType, object defaultValue)
+		{
+			ConfigFile = configFile ?? throw new ArgumentNullException(nameof(configFile));
+			Definition = definition ?? throw new ArgumentNullException(nameof(definition));
+			SettingType = settingType ?? throw new ArgumentNullException(nameof(settingType));
+
+			// Free type check
+			Value = defaultValue;
+			DefaultValue = defaultValue;
+		}
+
+		/// <summary>
+		/// Config file this entry is a part of.
+		/// </summary>
+		public ConfigFile ConfigFile { get; }
+
+		/// <summary>
+		/// Category and name of this setting. Used as a unique key for identification within a <see cref="Configuration.ConfigFile"/>.
+		/// </summary>
+		public ConfigDefinition Definition { get; }
+
+		/// <summary>
+		/// Description / metadata of this setting.
+		/// </summary>
+		public ConfigDescription Description { get; private set; }
+
+		/// <summary>
+		/// Type of the <see cref="Value"/> that this setting holds.
+		/// </summary>
+		public Type SettingType { get; }
+
+		/// <summary>
+		/// Default value of this setting (set only if the setting was not changed before).
+		/// </summary>
+		public object DefaultValue { get; }
+
+		/// <summary>
+		/// Get or set the value of the setting.
+		/// </summary>
+		public abstract object Value { get; set; }
+
+		/// <summary>
+		/// Get the serialized representation of the value.
+		/// </summary>
+		public string GetSerializedValue()
+		{
+			return TomlTypeConverter.ConvertToString(Value, SettingType);
+		}
+
+		/// <summary>
+		/// Set the value by using its serialized form.
+		/// </summary>
+		public void SetSerializedValue(string value)
+		{
+			try
+			{
+				var newValue = TomlTypeConverter.ConvertToValue(value, SettingType);
+				Value = newValue;
+			}
+			catch (Exception e)
+			{
+				Logger.Log(LogLevel.Warning, $"Config value of setting \"{Definition}\" could not be " +
+				                             $"parsed and will be ignored. Reason: {e.Message}; Value: {value}");
+			}
+		}
+
+		internal void SetDescription(ConfigDescription configDescription)
+		{
+			if (configDescription == null) throw new ArgumentNullException(nameof(configDescription));
+			if (configDescription.AcceptableValues != null && !SettingType.IsAssignableFrom(configDescription.AcceptableValues.ValueType))
+				throw new ArgumentException("configDescription.AcceptableValues is for a different type than the type of this setting");
+
+			Description = configDescription;
+			
+			// Automatically calls ClampValue in case it changed
+			Value = Value;
+		}
+
+		/// <summary>
+		/// If necessary, clamp the value to acceptable value range. T has to be equal to settingType.
+		/// </summary>
+		protected T ClampValue<T>(T value)
+		{
+			if (Description?.AcceptableValues != null)
+				return (T)Description.AcceptableValues.Clamp(value);
+			return value;
+		}
+
+		/// <summary>
+		/// Trigger setting changed event.
+		/// </summary>
+		protected void OnSettingChanged(object sender)
+		{
+			ConfigFile.OnSettingChanged(sender, this);
+		}
+
+		/// <summary>
+		/// Write a description of this setting using all available metadata.
+		/// </summary>
+		public void WriteDescription(StreamWriter writer)
+		{
+			bool hasDescription = Description != null;
+			bool hasType = SettingType != null;
+
+			if (hasDescription)
+				writer.WriteLine(Description.ToSerializedString());
+
+			if (hasType)
+			{
+				if (SettingType.IsEnum && SettingType.GetCustomAttributes(typeof(FlagsAttribute), true).Any())
+					writer.WriteLine("# Multiple values can be set at the same time by separating them with , (e.g. Debug, Warning)");
+
+				writer.WriteLine("# Setting type: " + SettingType.Name);
+
+				writer.WriteLine("# Default value: " + DefaultValue);
+			}
+
+			if (hasDescription && Description.AcceptableValues != null)
+			{
+				writer.WriteLine(Description.AcceptableValues.ToSerializedString());
+			}
+			else if (hasType)
+			{
+				/*if (SettingType == typeof(bool))
+					writer.WriteLine("# Acceptable values: True, False");
+				else*/
+				if (SettingType.IsEnum)
+					writer.WriteLine("# Acceptable values: " + string.Join(", ", Enum.GetNames(SettingType)));
+			}
+		}
+	}
+}