Parcourir la source

Added documentation, misc accessibility changes

ManlyMarco il y a 5 ans
Parent
commit
970438883c

+ 31 - 0
BepInEx/Configuration/ConfigDefinition.cs

@@ -2,18 +2,38 @@
 
 namespace BepInEx.Configuration
 {
+	/// <summary>
+	/// Section and key of a setting. Used as a unique key for identification within a <see cref="T:BepInEx.Configuration.ConfigFile" />.
+	/// The same definition can be used in multiple config files, it will point to different settings then.
+	/// </summary>
+	/// <inheritdoc />
 	public class ConfigDefinition : IEquatable<ConfigDefinition>
 	{
+		/// <summary>
+		/// Group of the setting. All settings within a config file are grouped by this.
+		/// </summary>
 		public string Section { get; }
 
+		/// <summary>
+		/// Name of the setting.
+		/// </summary>
 		public string Key { get; }
 
+		/// <summary>
+		/// Create a new definition. Definitions with same section and key are equal.
+		/// </summary>
+		/// <param name="section">Group of the setting, case sensitive.</param>
+		/// <param name="key">Name of the setting, case sensitive.</param>
 		public ConfigDefinition(string section, string key)
 		{
 			Key = key;
 			Section = section;
 		}
 
+		/// <summary>
+		/// Check if the definitions are the same.
+		/// </summary>
+		/// <inheritdoc />
 		public bool Equals(ConfigDefinition other)
 		{
 			if (other == null) return false;
@@ -21,6 +41,9 @@ namespace BepInEx.Configuration
 				   && string.Equals(Section, other.Section);
 		}
 
+		/// <summary>
+		/// Check if the definitions are the same.
+		/// </summary>
 		public override bool Equals(object obj)
 		{
 			if (ReferenceEquals(null, obj))
@@ -31,6 +54,7 @@ namespace BepInEx.Configuration
 			return Equals(obj as ConfigDefinition);
 		}
 
+		/// <inheritdoc />
 		public override int GetHashCode()
 		{
 			unchecked
@@ -41,12 +65,19 @@ namespace BepInEx.Configuration
 			}
 		}
 
+		/// <summary>
+		/// Check if the definitions are the same.
+		/// </summary>
 		public static bool operator ==(ConfigDefinition left, ConfigDefinition right)
 			=> Equals(left, right);
 
+		/// <summary>
+		/// Check if the definitions are the same.
+		/// </summary>
 		public static bool operator !=(ConfigDefinition left, ConfigDefinition right)
 			=> !Equals(left, right);
 
+		/// <inheritdoc />
 		public override string ToString()
 		{
 			return Section + " / " + Key;

+ 14 - 2
BepInEx/Configuration/ConfigDescription.cs

@@ -2,17 +2,29 @@
 
 namespace BepInEx.Configuration
 {
+	//todo value range
+	/// <summary>
+	/// Metadata of a <see cref="ConfigEntry"/>.
+	/// </summary>
 	public class ConfigDescription
 	{
+		/// <summary>
+		/// Create a new description.
+		/// </summary>
+		/// <param name="description">Text describing the function of the setting and any notes or warnings.</param>
 		public ConfigDescription(string description)
 		{
 			Description = description ?? throw new ArgumentNullException(nameof(description));
 		}
 
+		/// <summary>
+		/// Text describing the function of the setting and any notes or warnings.
+		/// </summary>
 		public string Description { get; }
 
-		//todo value range
-
+		/// <summary>
+		/// Convert the description object into a form suitable for writing into a config file.
+		/// </summary>
 		public string ToSerializedString()
 		{
 			return $"# {Description.Replace("\n", "\n# ")}";

+ 34 - 1
BepInEx/Configuration/ConfigEntry.cs

@@ -5,6 +5,10 @@ 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 sealed class ConfigEntry
 	{
 		internal ConfigEntry(ConfigFile configFile, ConfigDefinition definition, Type settingType, object defaultValue) : this(configFile, definition)
@@ -49,12 +53,29 @@ namespace BepInEx.Configuration
 		private object _convertedValue;
 		private string _serializedValue;
 
+		/// <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; internal set; }
 
+		/// <summary>
+		/// Type of the <see cref="Value"/> that this setting holds.
+		/// </summary>
 		public Type SettingType { get; private set; }
+
+		/// <summary>
+		/// Default value of this setting (set only if the setting was not changed before).
+		/// </summary>
 		public object DefaultValue { get; private set; }
 
 		/// <summary>
@@ -64,6 +85,7 @@ namespace BepInEx.Configuration
 		public bool IsDefined => SettingType != null;
 
 		/// <summary>
+		/// Get or set the value of the setting.
 		/// Can't be used when <see cref="IsDefined"/> is false.
 		/// </summary>
 		public object Value
@@ -91,6 +113,9 @@ namespace BepInEx.Configuration
 			}
 		}
 
+		/// <summary>
+		/// Get the serialized representation of the value.
+		/// </summary>
 		public string GetSerializedValue()
 		{
 			if (_serializedValue != null)
@@ -102,7 +127,12 @@ namespace BepInEx.Configuration
 			return TomlTypeConverter.ConvertToString(Value, SettingType);
 		}
 
-		public void SetSerializedValue(string newValue, bool fireEvent, object sender)
+		/// <summary>
+		/// Set the value by using its serialized form.
+		/// </summary>
+		public void SetSerializedValue(string newValue) => SetSerializedValue(newValue, true, this);
+
+		internal void SetSerializedValue(string newValue, bool fireEvent, object sender)
 		{
 			string current = GetSerializedValue();
 			if (string.Equals(current, newValue)) return;
@@ -162,6 +192,9 @@ namespace BepInEx.Configuration
 			ConfigFile.OnSettingChanged(sender, this);
 		}
 
+		/// <summary>
+		/// Write a description of this setting using all available metadata.
+		/// </summary>
 		public void WriteDescription(StreamWriter writer)
 		{
 			if (Description != null)

+ 60 - 8
BepInEx/Configuration/ConfigFile.cs

@@ -15,20 +15,40 @@ namespace BepInEx.Configuration
 	{
 		internal static ConfigFile CoreConfig { get; } = new ConfigFile(Paths.BepInExConfigPath, true);
 
+		/// <summary>
+		/// All config entries inside 
+		/// </summary>
 		protected Dictionary<ConfigDefinition, ConfigEntry> Entries { get; } = new Dictionary<ConfigDefinition, ConfigEntry>();
 
-		[Obsolete("Use ConfigEntries instead")]
+		/// <summary>
+		/// Create a list with all config entries inside of this config file.
+		/// </summary>
+		[Obsolete("Use GetConfigEntries instead")]
 		public ReadOnlyCollection<ConfigDefinition> ConfigDefinitions => Entries.Keys.ToList().AsReadOnly();
 
-		public ReadOnlyCollection<ConfigEntry> ConfigEntries => Entries.Values.ToList().AsReadOnly();
+		/// <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="Wrap{T}(ConfigDefinition,T,ConfigDescription)"/> 
+		/// instead with no description.
+		/// </summary>
+		public ConfigEntry[] GetConfigEntries() => 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.
+		/// </summary>
 		public string ConfigFilePath { get; }
 
 		/// <summary>
-		/// If enabled, writes the config to disk every time a value is set. If disabled, you have to manually save or the changes will be lost!
+		/// If enabled, writes the config to disk every time a value is set. 
+		/// If disabled, you have to manually use <see cref="Save"/> or the changes will be lost!
 		/// </summary>
 		public bool SaveOnConfigSet { get; set; } = true;
 
+		/// <summary>
+		/// Create a new config file at the specified config path.
+		/// </summary>
+		/// <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>
 		public ConfigFile(string configPath, bool saveOnInit)
 		{
 			if (configPath == null) throw new ArgumentNullException(nameof(configPath));
@@ -138,6 +158,16 @@ namespace BepInEx.Configuration
 
 		#region Wraps
 
+		/// <summary>
+		/// Create a new setting or access one of the existing ones. The setting is saved to drive and loaded automatically.
+		/// If you are the creator of the setting, provide a ConfigDescription object to give user information about the setting.
+		/// If you are using a setting created by another plugin/class, do not provide any ConfigDescription.
+		/// </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.</param>
+		/// <returns></returns>
 		public ConfigWrapper<T> Wrap<T>(ConfigDefinition configDefinition, T defaultValue, ConfigDescription configDescription = null)
 		{
 			if (!TomlTypeConverter.CanConvert(typeof(T)))
@@ -166,10 +196,26 @@ namespace BepInEx.Configuration
 			return new ConfigWrapper<T>(entry);
 		}
 
+		/// <summary>
+		/// Create a new setting or access one of the existing ones. The setting is saved to drive and loaded automatically.
+		/// If you are the creator of the setting, provide a ConfigDescription object to give user information about the setting.
+		/// If you are using a setting created by another plugin/class, do not provide any ConfigDescription.
+		/// </summary>
 		[Obsolete("Use other Wrap overloads instead")]
 		public ConfigWrapper<T> Wrap<T>(string section, string key, string description = null, T defaultValue = default(T))
 			=> Wrap(new ConfigDefinition(section, key), defaultValue, string.IsNullOrEmpty(description) ? null : new ConfigDescription(description));
 
+		/// <summary>
+		/// Create a new setting or access one of the existing ones. The setting is saved to drive and loaded automatically.
+		/// If you are the creator of the setting, provide a ConfigDescription object to give user information about the setting.
+		/// If you are using a setting created by another plugin/class, do not provide any ConfigDescription.
+		/// </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.</param>
+		/// <returns></returns>
 		public ConfigWrapper<T> Wrap<T>(string section, string key, T defaultValue, ConfigDescription configDescription = null)
 			=> Wrap(new ConfigDefinition(section, key), defaultValue, configDescription);
 
@@ -187,7 +233,7 @@ namespace BepInEx.Configuration
 		/// </summary>
 		public event EventHandler<SettingChangedEventArgs> SettingChanged;
 
-		protected internal void OnSettingChanged(object sender, ConfigEntry changedEntry)
+		internal void OnSettingChanged(object sender, ConfigEntry changedEntry)
 		{
 			if (changedEntry == null) throw new ArgumentNullException(nameof(changedEntry));
 
@@ -208,12 +254,13 @@ namespace BepInEx.Configuration
 				}
 			}
 
-			// todo better way to prevent write loop? maybe do some caching?
+			// Check sender to prevent infinite loops
+			// todo batching / async?
 			if (sender != this && SaveOnConfigSet)
 				Save();
 		}
 
-		protected void OnConfigReloaded()
+		private void OnConfigReloaded()
 		{
 			if (ConfigReloaded != null)
 			{
@@ -266,11 +313,16 @@ namespace BepInEx.Configuration
 		{
 			lock (_ioLock)
 			{
-				_watcher?.Dispose();
-				_watcher = null;
+				if (_watcher != null)
+				{
+					_watcher.EnableRaisingEvents = false;
+					_watcher.Dispose();
+					_watcher = null;
+				}
 			}
 		}
 
+		/// <inheritdoc />
 		~ConfigFile()
 		{
 			StopWatching();

+ 17 - 0
BepInEx/Configuration/ConfigWrapper.cs

@@ -2,11 +2,25 @@
 
 namespace BepInEx.Configuration
 {
+	/// <summary>
+	/// 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>
 	{
+		/// <summary>
+		/// Entry of this setting in the <see cref="Configuration.ConfigFile"/>.
+		/// </summary>
 		public ConfigEntry 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>
@@ -14,6 +28,9 @@ namespace BepInEx.Configuration
 		/// </summary>
 		public event EventHandler SettingChanged;
 
+		/// <summary>
+		/// Value of this setting.
+		/// </summary>
 		public T Value
 		{
 			get => (T)ConfigEntry.Value;

+ 8 - 0
BepInEx/Configuration/SettingChangedEventArgs.cs

@@ -2,13 +2,21 @@
 
 namespace BepInEx.Configuration
 {
+	/// <summary>
+	/// Arguments for events concerning a change of a setting.
+	/// </summary>
+	/// <inheritdoc />
 	public sealed class SettingChangedEventArgs : EventArgs
 	{
+		/// <inheritdoc />
 		public SettingChangedEventArgs(ConfigEntry changedSetting)
 		{
 			ChangedSetting = changedSetting;
 		}
 
+		/// <summary>
+		/// Setting that was changed
+		/// </summary>
 		public ConfigEntry ChangedSetting { get; }
 	}
 }

+ 1 - 1
BepInEx/Configuration/TomlTypeConverter.cs

@@ -4,7 +4,7 @@ using System.Globalization;
 
 namespace BepInEx.Configuration
 {
-	public class TypeConverter
+	internal class TypeConverter
 	{
 		public Func<object, Type, string> ConvertToString { get; set; }
 		public Func<string, Type, object> ConvertToObject { get; set; }