Browse Source

Fixed homeless entries not getting saved

ManlyMarco 5 years ago
parent
commit
b730c16443

+ 4 - 3
BepInEx/Configuration/ConfigEntryBase.cs

@@ -3,7 +3,8 @@ 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.
@@ -75,7 +76,7 @@ namespace BepInEx.Configuration {
 			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}");
+											 $"parsed and will be ignored. Reason: {e.Message}; Value: {value}");
 			}
 		}
 
@@ -86,7 +87,7 @@ namespace BepInEx.Configuration {
 				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;
 		}

+ 9 - 4
BepInEx/Configuration/ConfigFile.cs

@@ -93,6 +93,8 @@ namespace BepInEx.Configuration
 		{
 			lock (_ioLock)
 			{
+				HomelessEntries.Clear();
+
 				try
 				{
 					_disableSaving = true;
@@ -159,18 +161,21 @@ namespace BepInEx.Configuration
 						writer.WriteLine();
 					}
 
-					foreach (var sectionKv in Entries.GroupBy(x => x.Key.Section).OrderBy(x => x.Key))
+					var allConfigEntries = Entries.Select(x => new { x.Key, entry = x.Value, value = x.Value.GetSerializedValue() })
+						.Concat(HomelessEntries.Select(x => new { x.Key, entry = (ConfigEntryBase)null, value = x.Value }));
+
+					foreach (var sectionKv in allConfigEntries.GroupBy(x => x.Key.Section).OrderBy(x => x.Key))
 					{
 						// Section heading
 						writer.WriteLine($"[{sectionKv.Key}]");
 
-						foreach (var configEntry in sectionKv.Select(x => x.Value))
+						foreach (var configEntry in sectionKv)
 						{
 							writer.WriteLine();
 
-							configEntry.WriteDescription(writer);
+							configEntry.entry?.WriteDescription(writer);
 
-							writer.WriteLine($"{configEntry.Definition.Key} = {configEntry.GetSerializedValue()}");
+							writer.WriteLine($"{configEntry.Key.Key} = {configEntry.value}");
 						}
 
 						writer.WriteLine();

+ 16 - 1
BepInExTests/Configuration/ConfigFileTests.cs

@@ -103,7 +103,7 @@ namespace BepInEx.Configuration.Tests
 			c.Reload();
 			Assert.AreEqual(w.Value, 1);
 		}
-		
+
 		[TestMethod]
 		public void FileWatchTestNoSelfReload()
 		{
@@ -134,6 +134,21 @@ namespace BepInEx.Configuration.Tests
 		}
 
 		[TestMethod]
+		public void PersistHomeless()
+		{
+			var c = MakeConfig();
+
+			File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey=1\nHomeless=0");
+			c.Reload();
+
+			var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
+
+			c.Save();
+
+			Assert.IsTrue(File.ReadAllLines(c.ConfigFilePath).Single(x => x.StartsWith("Homeless") && x.EndsWith("0")) != null);
+		}
+
+		[TestMethod]
 		public void EventTestReload()
 		{
 			var c = MakeConfig();