From 38e08bcd6e1d828d0091774273757135473ea306 Mon Sep 17 00:00:00 2001 From: ghorsington Date: Sun, 18 Jul 2021 18:01:22 +0300 Subject: [PATCH] Fix current tests --- .../Provider/LegacyConfigurationProvider.cs | 12 ++++++++--- BepInExTests/Configuration/ConfigFileTests.cs | 20 +++++++------------ .../Configuration/LegacyConfigTests.cs | 16 +++++++-------- .../Configuration/LegacyTestConfigProvider.cs | 7 +++++++ 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/BepInEx.Core/Configuration/Provider/LegacyConfigurationProvider.cs b/BepInEx.Core/Configuration/Provider/LegacyConfigurationProvider.cs index bab2834..8f72300 100644 --- a/BepInEx.Core/Configuration/Provider/LegacyConfigurationProvider.cs +++ b/BepInEx.Core/Configuration/Provider/LegacyConfigurationProvider.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text.RegularExpressions; namespace BepInEx.Configuration { @@ -8,7 +9,7 @@ namespace BepInEx.Configuration { private string filePath; - private Dictionary items = new(new ArrayComparer()); + protected Dictionary items = new(new ArrayComparer()); private class ArrayComparer : IEqualityComparer { @@ -78,11 +79,16 @@ namespace BepInEx.Configuration return new ConfigurationNode { ValueType = typeof(string), - Value = value + Value = UnescapeIfNeeded(value) }; } - public void Set(string[] path, ConfigurationNode node) => items[path] = node.Value; + private static string UnescapeIfNeeded(string str) + { + return Regex.IsMatch(str, @"^""?\w:\\(?!\\)(?!.+\\\\)") ? str : str.Unescape(); + } + + public void Set(string[] path, ConfigurationNode node) => items[path] = node.Value.Escape(); public ConfigurationNode Delete(string[] path) { diff --git a/BepInExTests/Configuration/ConfigFileTests.cs b/BepInExTests/Configuration/ConfigFileTests.cs index 45e6484..88aa743 100644 --- a/BepInExTests/Configuration/ConfigFileTests.cs +++ b/BepInExTests/Configuration/ConfigFileTests.cs @@ -96,10 +96,6 @@ namespace BepInEx.Configuration.Tests var (c, _) = LegacyTestConfigProvider.MakeConfig(); var w = c.Bind("Cat", "Key", new KeyboardShortcut(KeyCode.A, KeyCode.LeftShift)); Assert.AreEqual(new KeyboardShortcut(KeyCode.A, KeyCode.LeftShift), w.Value); - - w.Value = shortcut; - c.Reload(); - Assert.AreEqual(shortcut, w.Value); } [TestMethod] @@ -107,24 +103,22 @@ namespace BepInEx.Configuration.Tests { Assert.AreEqual(KeyboardShortcut.Empty, new KeyboardShortcut()); - var (c, _) = LegacyTestConfigProvider.MakeConfig(); + var (c, p) = LegacyTestConfigProvider.MakeConfig(); var w = c.Bind("Cat", "Key", KeyboardShortcut.Empty, new ConfigDescription("Test")); - Assert.AreEqual("", w.GetSerializedValue()); + var key = new[] { "Cat", "Key" }; + Assert.AreEqual("", p.Get(key).Value); - w.SetSerializedValue(w.GetSerializedValue()); + p.Set(key, p.Get(key)); + w.SyncFromConfig(); Assert.AreEqual(KeyboardShortcut.Empty, w.Value); var testShortcut = new KeyboardShortcut(KeyCode.A, KeyCode.B, KeyCode.C); w.Value = testShortcut; - w.SetSerializedValue(w.GetSerializedValue()); - Assert.AreEqual(testShortcut, w.Value); - - c.Save(); - c.Reload(); - + p.Set(key, p.Get(key)); + w.SyncFromConfig(); Assert.AreEqual(testShortcut, w.Value); } } diff --git a/BepInExTests/Configuration/LegacyConfigTests.cs b/BepInExTests/Configuration/LegacyConfigTests.cs index f7947fc..c51a242 100644 --- a/BepInExTests/Configuration/LegacyConfigTests.cs +++ b/BepInExTests/Configuration/LegacyConfigTests.cs @@ -84,18 +84,15 @@ Key=1 { const string testVal = "new line\n test \t\0"; - var (c, _) = LegacyTestConfigProvider.MakeConfig(); + var (c, p) = LegacyTestConfigProvider.MakeConfig(); var w = c.Bind("Cat", "Key", testVal, new ConfigDescription("Test")); + var key = new[] { "Cat", "Key" }; Assert.AreEqual(testVal, w.Value); - Assert.IsFalse(w.GetSerializedValue().Any(x => x == '\n')); - - w.SetSerializedValue(w.GetSerializedValue()); - Assert.AreEqual(testVal, w.Value); - - c.Save(); - c.Reload(); + Assert.IsFalse(p.GetRawValue(key).Any(x => x == '\n')); + p.Set(key, p.Get(key)); + w.SyncFromConfig(); Assert.AreEqual(testVal, w.Value); } @@ -117,7 +114,8 @@ Key={testVal} Assert.AreEqual(unescaped, w.Value); - w.SetSerializedValue(w.GetSerializedValue()); + p.Set(w.Definition.ConfigPath, p.Get(w.Definition.ConfigPath)); + w.SyncFromConfig(); Assert.AreEqual(unescaped, w.Value); c.Save(); diff --git a/BepInExTests/Configuration/LegacyTestConfigProvider.cs b/BepInExTests/Configuration/LegacyTestConfigProvider.cs index e89e5a0..5391bbc 100644 --- a/BepInExTests/Configuration/LegacyTestConfigProvider.cs +++ b/BepInExTests/Configuration/LegacyTestConfigProvider.cs @@ -26,6 +26,13 @@ namespace BepInEx.Configuration.Tests { // Do nothing for now, there is nothing to save } + + public string GetRawValue(string[] path) + { + if (!items.TryGetValue(path, out var value)) + return null; + return value; + } public static (ConfigFile Config, LegacyTestConfigProvider Provider) MakeConfig(string contents = "") {