From ae25ed327aa4247093253c9ee989369f27e23921 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Wed, 18 Sep 2019 21:59:03 -0500 Subject: [PATCH] Fix AreUnifiedThresholdsSynced modifying config. --- smx-config/ConfigPresets.cs | 2 +- smx-config/Helpers.cs | 15 +++++++++++++++ smx-config/SMX.cs | 2 ++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/smx-config/ConfigPresets.cs b/smx-config/ConfigPresets.cs index 9c8808b..cbfeaf1 100644 --- a/smx-config/ConfigPresets.cs +++ b/smx-config/ConfigPresets.cs @@ -141,7 +141,7 @@ namespace smx_config // have no effect. static public bool AreUnifiedThresholdsSynced(SMX.SMXConfig config) { - SMX.SMXConfig config2 = config; + SMX.SMXConfig config2 = Helpers.DeepClone(config); SyncUnifiedThresholds(ref config2); return SamePreset(config, config2); } diff --git a/smx-config/Helpers.cs b/smx-config/Helpers.cs index 765260a..a50404f 100644 --- a/smx-config/Helpers.cs +++ b/smx-config/Helpers.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Runtime.InteropServices; +using System.Runtime.Serialization.Formatters.Binary; using System.Windows; using System.Windows.Media; using System.Windows.Resources; @@ -107,6 +109,19 @@ namespace smx_config return new System.ComponentModel.Win32Exception(error).Message; } + // https://stackoverflow.com/a/129395/136829 + public static T DeepClone(T obj) + { + using (var ms = new MemoryStream()) + { + var formatter = new BinaryFormatter(); + formatter.Serialize(ms, obj); + ms.Position = 0; + + return (T) formatter.Deserialize(ms); + } + } + // Work around Enumerable.SequenceEqual not checking if the arrays are null. public static bool SequenceEqual(this IEnumerable first, IEnumerable second) { diff --git a/smx-config/SMX.cs b/smx-config/SMX.cs index 58ff795..a48e2bf 100644 --- a/smx-config/SMX.cs +++ b/smx-config/SMX.cs @@ -27,6 +27,7 @@ namespace SMX PlatformFlags_FSR = 1 << 1, }; + [Serializable] public struct PackedSensorSettings { // Load cell thresholds: public Byte loadCellLowThreshold; @@ -47,6 +48,7 @@ namespace SMX }; [StructLayout(LayoutKind.Sequential, Pack=1)] + [Serializable] public struct SMXConfig { public Byte masterVersion; public Byte configVersion;