Przeglądaj źródła

Rename Wrap to GetSetting and add some missing Obsolete overloads

ManlyMarco 5 lat temu
rodzic
commit
0378ba54e3

+ 3 - 3
BepInEx.Preloader/Patching/AssemblyPatcher.cs

@@ -277,17 +277,17 @@ namespace BepInEx.Preloader.Patching
 
 		#region Config
 
-		private static readonly ConfigWrapper<bool> ConfigDumpAssemblies = ConfigFile.CoreConfig.Wrap(
+		private static readonly ConfigWrapper<bool> ConfigDumpAssemblies = ConfigFile.CoreConfig.GetSetting(
 			"Preloader", "DumpAssemblies",
 			false,
 			new ConfigDescription("If enabled, BepInEx will save patched assemblies into BepInEx/DumpedAssemblies.\nThis can be used by developers to inspect and debug preloader patchers."));
 
-		private static readonly ConfigWrapper<bool> ConfigLoadDumpedAssemblies = ConfigFile.CoreConfig.Wrap(
+		private static readonly ConfigWrapper<bool> ConfigLoadDumpedAssemblies = ConfigFile.CoreConfig.GetSetting(
 			"Preloader", "LoadDumpedAssemblies",
 			false,
 			new ConfigDescription("If enabled, BepInEx will load patched assemblies from BepInEx/DumpedAssemblies instead of memory.\nThis can be used to be able to load patched assemblies into debuggers like dnSpy.\nIf set to true, will override DumpAssemblies."));
 
-		private static readonly ConfigWrapper<bool> ConfigBreakBeforeLoadAssemblies = ConfigFile.CoreConfig.Wrap(
+		private static readonly ConfigWrapper<bool> ConfigBreakBeforeLoadAssemblies = ConfigFile.CoreConfig.GetSetting(
 			"Preloader", "BreakBeforeLoadAssemblies",
 			false,
 			new ConfigDescription("If enabled, BepInEx will call Debugger.Break() once before loading patched assemblies.\nThis can be used with debuggers like dnSpy to install breakpoints into patched assemblies before they are loaded."));

+ 6 - 6
BepInEx.Preloader/Preloader.cs

@@ -237,32 +237,32 @@ namespace BepInEx.Preloader
 
 		#region Config
 
-		private static readonly ConfigWrapper<string> ConfigEntrypointAssembly = ConfigFile.CoreConfig.Wrap<string>(
+		private static readonly ConfigWrapper<string> ConfigEntrypointAssembly = ConfigFile.CoreConfig.GetSetting(
 			"Preloader.Entrypoint", "Assembly",
 			IsPostUnity2017 ? "UnityEngine.CoreModule.dll" : "UnityEngine.dll",
 			new ConfigDescription("The local filename of the assembly to target."));
 
-		private static readonly ConfigWrapper<string> ConfigEntrypointType = ConfigFile.CoreConfig.Wrap<string>(
+		private static readonly ConfigWrapper<string> ConfigEntrypointType = ConfigFile.CoreConfig.GetSetting(
 			"Preloader.Entrypoint", "Type",
 			"Application",
 			new ConfigDescription("The name of the type in the entrypoint assembly to search for the entrypoint method."));
 
-		private static readonly ConfigWrapper<string> ConfigEntrypointMethod = ConfigFile.CoreConfig.Wrap<string>(
+		private static readonly ConfigWrapper<string> ConfigEntrypointMethod = ConfigFile.CoreConfig.GetSetting(
 			"Preloader.Entrypoint", "Method",
 			".cctor",
 			new ConfigDescription("The name of the method in the specified entrypoint assembly and type to hook and load Chainloader from."));
 
-		private static readonly ConfigWrapper<bool> ConfigApplyRuntimePatches = ConfigFile.CoreConfig.Wrap(
+		private static readonly ConfigWrapper<bool> ConfigApplyRuntimePatches = ConfigFile.CoreConfig.GetSetting(
 			"Preloader", "ApplyRuntimePatches",
 			true,
 			new ConfigDescription("Enables or disables runtime patches.\nThis should always be true, unless you cannot start the game due to a Harmony related issue (such as running .NET Standard runtime) or you know what you're doing."));
 
-		private static readonly ConfigWrapper<bool> ConfigShimHarmony = ConfigFile.CoreConfig.Wrap(
+		private static readonly ConfigWrapper<bool> ConfigShimHarmony = ConfigFile.CoreConfig.GetSetting(
 			"Preloader", "ShimHarmonySupport",
 			!Utility.CLRSupportsDynamicAssemblies,
 			new ConfigDescription("If enabled, basic Harmony functionality is patched to use MonoMod's RuntimeDetour instead.\nTry using this if Harmony does not work in a game."));
 
-		private static readonly ConfigWrapper<bool> ConfigPreloaderCOutLogging = ConfigFile.CoreConfig.Wrap(
+		private static readonly ConfigWrapper<bool> ConfigPreloaderCOutLogging = ConfigFile.CoreConfig.GetSetting(
 			"Logging", "PreloaderConsoleOutRedirection",
 			true,
 			new ConfigDescription("Redirects text from Console.Out during preloader patch loading to the BepInEx logging system."));

+ 5 - 5
BepInEx/Bootstrap/Chainloader.cs

@@ -307,27 +307,27 @@ namespace BepInEx.Bootstrap
 		#region Config
 
 
-		private static readonly ConfigWrapper<bool> ConfigUnityLogging = ConfigFile.CoreConfig.Wrap(
+		private static readonly ConfigWrapper<bool> ConfigUnityLogging = ConfigFile.CoreConfig.GetSetting(
 			"Logging", "UnityLogListening",
 			true,
 			new ConfigDescription("Enables showing unity log messages in the BepInEx logging system."));
 
-		private static readonly ConfigWrapper<bool> ConfigDiskWriteUnityLog = ConfigFile.CoreConfig.Wrap(
+		private static readonly ConfigWrapper<bool> ConfigDiskWriteUnityLog = ConfigFile.CoreConfig.GetSetting(
 			"Logging.Disk", "WriteUnityLog",
 			false,
 			new ConfigDescription("Include unity log messages in log file output."));
 
-		private static readonly ConfigWrapper<bool> ConfigDiskAppend = ConfigFile.CoreConfig.Wrap(
+		private static readonly ConfigWrapper<bool> ConfigDiskAppend = ConfigFile.CoreConfig.GetSetting(
 			"Logging.Disk", "AppendLog",
 			false,
 			new ConfigDescription("Appends to the log file instead of overwriting, on game startup."));
 
-		private static readonly ConfigWrapper<bool> ConfigDiskLogging = ConfigFile.CoreConfig.Wrap(
+		private static readonly ConfigWrapper<bool> ConfigDiskLogging = ConfigFile.CoreConfig.GetSetting(
 			"Logging.Disk", "Enabled",
 			true,
 			new ConfigDescription("Enables writing log messages to disk."));
 
-		private static readonly ConfigWrapper<LogLevel> ConfigDiskConsoleDisplayedLevel = ConfigFile.CoreConfig.Wrap(
+		private static readonly ConfigWrapper<LogLevel> ConfigDiskConsoleDisplayedLevel = ConfigFile.CoreConfig.GetSetting(
 			"Logging.Disk", "DisplayedLogLevel",
 			LogLevel.Info,
 			new ConfigDescription("Only displays the specified log level and above in the console output."));

+ 1 - 1
BepInEx/Bootstrap/TypeLoader.cs

@@ -257,7 +257,7 @@ namespace BepInEx.Bootstrap
 
 		#region Config
 
-		private static readonly ConfigWrapper<bool> EnableAssemblyCache = ConfigFile.CoreConfig.Wrap(
+		private static readonly ConfigWrapper<bool> EnableAssemblyCache = ConfigFile.CoreConfig.GetSetting(
 			"Caching", "EnableAssemblyCache", 
 			true, 
 			new ConfigDescription("Enable/disable assembly metadata cache\nEnabling this will speed up discovery of plugins and patchers by caching the metadata of all types BepInEx discovers."));

+ 4 - 0
BepInEx/Configuration/ConfigDefinition.cs

@@ -30,6 +30,10 @@ namespace BepInEx.Configuration
 			Section = section ?? throw new ArgumentNullException(nameof(section));
 		}
 
+		/// <inheritdoc />
+		[Obsolete("description argument is no longer used, put it in a ConfigDescription instead")]
+		public ConfigDefinition(string section, string key, string description) : this(section, key) { }
+
 		/// <summary>
 		/// Check if the definitions are the same.
 		/// </summary>

+ 17 - 16
BepInEx/Configuration/ConfigFile.cs

@@ -38,7 +38,7 @@ 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="Wrap{T}(ConfigDefinition,T,ConfigDescription)"/> 
+		/// If you want to access and modify an existing setting then use <see cref="GetSetting{T}(ConfigDefinition,T,ConfigDescription)"/> 
 		/// instead with no description.
 		/// </summary>
 		public ConfigEntryBase[] GetConfigEntries()
@@ -200,13 +200,13 @@ namespace BepInEx.Configuration
 		/// <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>
-		public ConfigWrapper<T> Wrap<T>(ConfigDefinition configDefinition, T defaultValue, ConfigDescription configDescription = null)
+		public ConfigWrapper<T> GetSetting<T>(ConfigDefinition configDefinition, T defaultValue = default(T), ConfigDescription configDescription = null)
 		{
+			if (!TomlTypeConverter.CanConvert(typeof(T)))
+				throw new ArgumentException($"Type {typeof(T)} is not supported by the config system. Supported types: {string.Join(", ", TomlTypeConverter.GetSupportedTypes().Select(x => x.Name).ToArray())}");
+
 			try
 			{
-				if (!TomlTypeConverter.CanConvert(typeof(T)))
-					throw new ArgumentException($"Type {typeof(T)} is not supported by the config system. Supported types: {string.Join(", ", TomlTypeConverter.GetSupportedTypes().Select(x => x.Name).ToArray())}");
-
 				lock (_ioLock)
 				{
 					_disableSaving = true;
@@ -256,22 +256,23 @@ namespace BepInEx.Configuration
 		/// 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>
-		public ConfigWrapper<T> Wrap<T>(string section, string key, T defaultValue, ConfigDescription configDescription = null)
-			=> Wrap(new ConfigDefinition(section, key), defaultValue, configDescription);
+		public ConfigWrapper<T> GetSetting<T>(string section, string key, T defaultValue = default(T), ConfigDescription configDescription = null)
+			=> GetSetting(new ConfigDefinition(section, key), defaultValue, configDescription);
+
+		/// <inheritdoc cref="GetSetting{T}(string,string,T,ConfigDescription)"/>
+		[Obsolete("Use GetSetting instead")]
+		public ConfigWrapper<T> Wrap<T>(string section, string key, string description = null, T defaultValue = default(T))
+			=> GetSetting(new ConfigDefinition(section ?? "", key), defaultValue, string.IsNullOrEmpty(description) ? null : new ConfigDescription(description));
+
+		/// <inheritdoc cref="GetSetting{T}(ConfigDefinition,T,ConfigDescription)"/>
+		[Obsolete("Use GetSetting instead")]
+		public ConfigWrapper<T> Wrap<T>(ConfigDefinition configDefinition, T defaultValue = default(T))
+			=> GetSetting(configDefinition, defaultValue);
 
 		#endregion
 

+ 2 - 2
BepInEx/Configuration/KeyboardShortcut.cs

@@ -12,7 +12,7 @@ namespace BepInEx.Configuration
 	/// triggered when the user presses the exact combination. For example, <c>F + LeftCtrl</c> will trigger only if user 
 	/// presses and holds only LeftCtrl, and then presses F. If any other keys are pressed, the shortcut will not trigger.
 	/// 
-	/// Can be used as a value of a setting in <see cref="ConfigFile.Wrap{T}(ConfigDefinition,T,ConfigDescription)"/> 
+	/// Can be used as a value of a setting in <see cref="ConfigFile.GetSetting{T}(ConfigDefinition,T,ConfigDescription)"/> 
 	/// to allow user to change this shortcut and have the changes saved.
 	/// 
 	/// How to use: Use <see cref="IsDown"/> in this class instead of <see cref="Input.GetKeyDown(KeyCode)"/> in the Update loop.
@@ -25,7 +25,7 @@ namespace BepInEx.Configuration
 				typeof(KeyboardShortcut),
 				new TypeConverter
 				{
-					ConvertToString = (o, type) => (o as KeyboardShortcut)?.Serialize(),
+					ConvertToString = (o, type) => (o as KeyboardShortcut)?.Serialize() ?? "",
 					ConvertToObject = (s, type) => Deserialize(s)
 				});
 		}

+ 2 - 2
BepInEx/ConsoleUtil/ConsoleWindow.cs

@@ -13,12 +13,12 @@ namespace UnityInjector.ConsoleUtil
 {
 	internal class ConsoleWindow
 	{
-		public static readonly ConfigWrapper<bool> ConfigConsoleEnabled = ConfigFile.CoreConfig.Wrap(
+		public static readonly ConfigWrapper<bool> ConfigConsoleEnabled = ConfigFile.CoreConfig.GetSetting(
 			"Logging.Console", "Enabled",
 			false,
 			new ConfigDescription("Enables showing a console for log output."));
 
-		public static readonly ConfigWrapper<bool> ConfigConsoleShiftJis = ConfigFile.CoreConfig.Wrap(
+		public static readonly ConfigWrapper<bool> ConfigConsoleShiftJis = ConfigFile.CoreConfig.GetSetting(
 			"Logging.Console", "ShiftJisEncoding",
 			false,
 			new ConfigDescription("If true, console is set to the Shift-JIS encoding, otherwise UTF-8 encoding."));

+ 1 - 1
BepInEx/Logging/ConsoleLogListener.cs

@@ -23,7 +23,7 @@ namespace BepInEx.Logging
 
 		public void Dispose() { }
 
-		private static readonly ConfigWrapper<LogLevel> ConfigConsoleDisplayedLevel = ConfigFile.CoreConfig.Wrap(
+		private static readonly ConfigWrapper<LogLevel> ConfigConsoleDisplayedLevel = ConfigFile.CoreConfig.GetSetting(
 			"Logging.Console","DisplayedLogLevel",
 			LogLevel.Info,
 			new ConfigDescription("Only displays the specified log level and above in the console output."));

+ 16 - 16
BepInExTests/Configuration/ConfigFileTests.cs

@@ -46,7 +46,7 @@ namespace BepInEx.Configuration.Tests
 		{
 			var c = MakeConfig();
 
-			var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
+			var w = c.GetSetting("Cat", "Key", 0, new ConfigDescription("Test"));
 			var lines = File.ReadAllLines(c.ConfigFilePath);
 			Assert.AreEqual(1, lines.Count(x => x.Equals("[Cat]")));
 			Assert.AreEqual(1, lines.Count(x => x.Equals("## Test")));
@@ -69,7 +69,7 @@ namespace BepInEx.Configuration.Tests
 		public void AutoSaveTest()
 		{
 			var c = MakeConfig();
-			c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
+			c.GetSetting("Cat", "Key", 0, new ConfigDescription("Test"));
 
 			var eventFired = new AutoResetEvent(false);
 			c.ConfigReloaded += (sender, args) => eventFired.Set();
@@ -85,9 +85,9 @@ namespace BepInEx.Configuration.Tests
 			var c = MakeConfig();
 			File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey=1\n");
 			c.Reload();
-			var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
+			var w = c.GetSetting("Cat", "Key", 0, new ConfigDescription("Test"));
 			Assert.AreEqual(w.Value, 1);
-			var w2 = c.Wrap("Cat", "Key2", 0, new ConfigDescription("Test"));
+			var w2 = c.GetSetting("Cat", "Key2", 0, new ConfigDescription("Test"));
 			Assert.AreEqual(w2.Value, 0);
 		}
 
@@ -95,7 +95,7 @@ namespace BepInEx.Configuration.Tests
 		public void ReadTest2()
 		{
 			var c = MakeConfig();
-			var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
+			var w = c.GetSetting("Cat", "Key", 0, new ConfigDescription("Test"));
 			Assert.AreEqual(w.Value, 0);
 
 			File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey = 1 \n");
@@ -121,7 +121,7 @@ namespace BepInEx.Configuration.Tests
 		public void EventTestWrapper()
 		{
 			var c = MakeConfig();
-			var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
+			var w = c.GetSetting("Cat", "Key", 0, new ConfigDescription("Test"));
 
 			File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey=1\n");
 
@@ -141,7 +141,7 @@ namespace BepInEx.Configuration.Tests
 			File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey=1\nHomeless=0");
 			c.Reload();
 
-			var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
+			var w = c.GetSetting("Cat", "Key", 0, new ConfigDescription("Test"));
 
 			c.Save();
 
@@ -154,7 +154,7 @@ namespace BepInEx.Configuration.Tests
 			var c = MakeConfig();
 			var eventFired = false;
 
-			var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
+			var w = c.GetSetting("Cat", "Key", 0, new ConfigDescription("Test"));
 			w.SettingChanged += (sender, args) => eventFired = true;
 
 			Assert.IsFalse(eventFired);
@@ -169,7 +169,7 @@ namespace BepInEx.Configuration.Tests
 		public void ValueRangeTest()
 		{
 			var c = MakeConfig();
-			var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<int>(0, 2)));
+			var w = c.GetSetting("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<int>(0, 2)));
 
 			Assert.AreEqual(0, w.Value);
 			w.Value = 2;
@@ -185,7 +185,7 @@ namespace BepInEx.Configuration.Tests
 		public void ValueRangeBadTypeTest()
 		{
 			var c = MakeConfig();
-			c.Wrap("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<float>(1, 2)));
+			c.GetSetting("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<float>(1, 2)));
 			Assert.Fail();
 		}
 
@@ -193,7 +193,7 @@ namespace BepInEx.Configuration.Tests
 		public void ValueRangeDefaultTest()
 		{
 			var c = MakeConfig();
-			var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<int>(1, 2)));
+			var w = c.GetSetting("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<int>(1, 2)));
 
 			Assert.AreEqual(w.Value, 1);
 		}
@@ -206,7 +206,7 @@ namespace BepInEx.Configuration.Tests
 			File.WriteAllText(c.ConfigFilePath, "[Cat]\nKey = 1\n");
 			c.Reload();
 
-			var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<int>(0, 2)));
+			var w = c.GetSetting("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<int>(0, 2)));
 
 			Assert.AreEqual(w.Value, 1);
 
@@ -220,7 +220,7 @@ namespace BepInEx.Configuration.Tests
 		public void ValueListTest()
 		{
 			var c = MakeConfig();
-			var w = c.Wrap<string>("Cat", "Key", "kek", new ConfigDescription("Test", new AcceptableValueList<string>("lel", "kek", "wew", "why")));
+			var w = c.GetSetting("Cat", "Key", "kek", new ConfigDescription("Test", new AcceptableValueList<string>("lel", "kek", "wew", "why")));
 
 			Assert.AreEqual("kek", w.Value);
 			w.Value = "wew";
@@ -240,7 +240,7 @@ namespace BepInEx.Configuration.Tests
 			Assert.AreEqual(shortcut, d);
 
 			var c = MakeConfig();
-			var w = c.Wrap("Cat", "Key", new KeyboardShortcut(KeyCode.A, KeyCode.LeftShift));
+			var w = c.GetSetting("Cat", "Key", new KeyboardShortcut(KeyCode.A, KeyCode.LeftShift));
 			Assert.AreEqual(new KeyboardShortcut(KeyCode.A, KeyCode.LeftShift), w.Value);
 
 			w.Value = shortcut;
@@ -254,7 +254,7 @@ namespace BepInEx.Configuration.Tests
 			const string testVal = "new line\n test \t\0";
 
 			var c = MakeConfig();
-			var w = c.Wrap<string>("Cat", "Key", testVal, new ConfigDescription("Test"));
+			var w = c.GetSetting("Cat", "Key", testVal, new ConfigDescription("Test"));
 
 			Assert.AreEqual(testVal, w.Value);
 			Assert.IsFalse(w.ConfigEntry.GetSerializedValue().Any(x => x == '\n'));
@@ -279,7 +279,7 @@ namespace BepInEx.Configuration.Tests
 				File.WriteAllText(c.ConfigFilePath, $"[Cat]\n# Test\nKey={testVal}\n");
 				c.Reload();
 
-				var w = c.Wrap<string>("Cat", "Key", "", new ConfigDescription("Test"));
+				var w = c.GetSetting("Cat", "Key", "", new ConfigDescription("Test"));
 
 				Assert.AreEqual(unescaped, w.Value);