Browse Source

Simplify the config class structure

ManlyMarco 4 years ago
parent
commit
04be8b17de

+ 0 - 1
BepInEx/BepInEx.csproj

@@ -56,7 +56,6 @@
     <Compile Include="Contract\PluginInfo.cs" />
     <Compile Include="Configuration\ConfigDefinition.cs" />
     <Compile Include="Configuration\ConfigDescription.cs" />
-    <Compile Include="Configuration\ConfigEntry.cs" />
     <Compile Include="Configuration\ConfigFile.cs" />
     <Compile Include="Configuration\ConfigWrapper.cs" />
     <Compile Include="Configuration\KeyboardShortcut.cs" />

+ 0 - 33
BepInEx/Configuration/ConfigEntry.cs

@@ -1,33 +0,0 @@
-namespace BepInEx.Configuration
-{
-	/// <inheritdoc />
-	public class ConfigEntry<T> : ConfigEntryBase
-	{
-		private T _typedValue;
-		internal ConfigEntry(ConfigFile configFile, ConfigDefinition definition, T defaultValue) : base(configFile, definition, typeof(T), defaultValue) { }
-
-		/// <summary>
-		/// Get or set the value of the setting without boxing.
-		/// </summary>
-		public T TypedValue
-		{
-			get => _typedValue;
-			set
-			{
-				value = ClampValue(value);
-				if (Equals(_typedValue, value))
-					return;
-
-				_typedValue = value;
-				OnSettingChanged(this);
-			}
-		}
-
-		/// <inheritdoc />
-		public override object Value
-		{
-			get => TypedValue;
-			set => TypedValue = (T)value;
-		}
-	}
-}

+ 6 - 6
BepInEx/Configuration/ConfigEntryBase.cs

@@ -21,7 +21,7 @@ namespace BepInEx.Configuration
 			SettingType = settingType ?? throw new ArgumentNullException(nameof(settingType));
 
 			// Free type check
-			Value = defaultValue;
+			BoxedValue = defaultValue;
 			DefaultValue = defaultValue;
 		}
 
@@ -41,7 +41,7 @@ namespace BepInEx.Configuration
 		public ConfigDescription Description { get; private set; }
 
 		/// <summary>
-		/// Type of the <see cref="Value"/> that this setting holds.
+		/// Type of the <see cref="BoxedValue"/> that this setting holds.
 		/// </summary>
 		public Type SettingType { get; }
 
@@ -53,14 +53,14 @@ namespace BepInEx.Configuration
 		/// <summary>
 		/// Get or set the value of the setting.
 		/// </summary>
-		public abstract object Value { get; set; }
+		public abstract object BoxedValue { get; set; }
 
 		/// <summary>
 		/// Get the serialized representation of the value.
 		/// </summary>
 		public string GetSerializedValue()
 		{
-			return TomlTypeConverter.ConvertToString(Value, SettingType);
+			return TomlTypeConverter.ConvertToString(BoxedValue, SettingType);
 		}
 
 		/// <summary>
@@ -71,7 +71,7 @@ namespace BepInEx.Configuration
 			try
 			{
 				var newValue = TomlTypeConverter.ConvertToValue(value, SettingType);
-				Value = newValue;
+				BoxedValue = newValue;
 			}
 			catch (Exception e)
 			{
@@ -88,7 +88,7 @@ namespace BepInEx.Configuration
 			Description = configDescription;
 
 			// Automatically calls ClampValue in case it changed
-			Value = Value;
+			BoxedValue = BoxedValue;
 		}
 
 		/// <summary>

+ 4 - 4
BepInEx/Configuration/ConfigFile.cs

@@ -213,14 +213,14 @@ namespace BepInEx.Configuration
 
 					Entries.TryGetValue(configDefinition, out var existingEntry);
 
-					if (existingEntry != null && !(existingEntry is ConfigEntry<T>))
+					if (existingEntry != null && !(existingEntry is ConfigWrapper<T>))
 						throw new ArgumentException("The defined setting already exists with a different setting type - " + existingEntry.SettingType.Name);
 
-					var entry = (ConfigEntry<T>)existingEntry;
+					var entry = (ConfigWrapper<T>)existingEntry;
 
 					if (entry == null)
 					{
-						entry = new ConfigEntry<T>(this, configDefinition, defaultValue);
+						entry = new ConfigWrapper<T>(this, configDefinition, defaultValue);
 						Entries[configDefinition] = entry;
 					}
 
@@ -242,7 +242,7 @@ namespace BepInEx.Configuration
 					if (SaveOnConfigSet)
 						Save();
 
-					return new ConfigWrapper<T>(entry);
+					return entry;
 				}
 			}
 			finally

+ 22 - 22
BepInEx/Configuration/ConfigWrapper.cs

@@ -6,44 +6,44 @@ namespace BepInEx.Configuration
 	/// Provides access to a single setting inside of a <see cref="Configuration.ConfigFile"/>.
 	/// </summary>
 	/// <typeparam name="T">Type of the setting.</typeparam>
-	public sealed class ConfigWrapper<T>
+	public sealed class ConfigWrapper<T> : ConfigEntryBase
 	{
 		/// <summary>
-		/// Entry of this setting in the <see cref="Configuration.ConfigFile"/>.
-		/// </summary>
-		public ConfigEntry<T> ConfigEntry { get; }
-
-		/// <summary>
-		/// Unique definition of this setting.
-		/// </summary>
-		public ConfigDefinition Definition => ConfigEntry.Definition;
-
-		/// <summary>
-		/// Config file this setting is inside of.
-		/// </summary>
-		public ConfigFile ConfigFile => ConfigEntry.ConfigFile;
-
-		/// <summary>
 		/// Fired when the setting is changed. Does not detect changes made outside from this object.
 		/// </summary>
 		public event EventHandler SettingChanged;
 
+		private T _typedValue;
+
 		/// <summary>
 		/// Value of this setting.
 		/// </summary>
 		public T Value
 		{
-			get => ConfigEntry.TypedValue;
-			set => ConfigEntry.TypedValue = value;
+			get => _typedValue;
+			set
+			{
+				value = ClampValue(value);
+				if (Equals(_typedValue, value))
+					return;
+
+				_typedValue = value;
+				OnSettingChanged(this);
+			}
 		}
 
-		internal ConfigWrapper(ConfigEntry<T> configEntry)
+		/// <inheritdoc />
+		public override object BoxedValue
 		{
-			ConfigEntry = configEntry ?? throw new ArgumentNullException(nameof(configEntry));
+			get => Value;
+			set => Value = (T)value;
+		}
 
-			configEntry.ConfigFile.SettingChanged += (sender, args) =>
+		internal ConfigWrapper(ConfigFile configFile, ConfigDefinition definition, T defaultValue) : base(configFile, definition, typeof(T), defaultValue)
+		{
+			configFile.SettingChanged += (sender, args) =>
 			{
-				if (args.ChangedSetting == configEntry) SettingChanged?.Invoke(sender, args);
+				if (args.ChangedSetting == this) SettingChanged?.Invoke(sender, args);
 			};
 		}
 	}