From a5e56961b6d55150e9a61c639b3712eccc3fc429 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Tue, 6 Oct 2026 23:15:11 +0200 Subject: [PATCH] Make sure numerical settings parsing uses InvariantCulture --- src/DesktopMagicPluginAPI/Settings/IntegerUpDown.cs | 10 ++++++++-- src/DesktopMagicPluginAPI/Settings/Slider.cs | 11 +++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/DesktopMagicPluginAPI/Settings/IntegerUpDown.cs b/src/DesktopMagicPluginAPI/Settings/IntegerUpDown.cs index 1361d45..0da6fc8 100644 --- a/src/DesktopMagicPluginAPI/Settings/IntegerUpDown.cs +++ b/src/DesktopMagicPluginAPI/Settings/IntegerUpDown.cs @@ -1,4 +1,5 @@ using System; +using System.Globalization; namespace DesktopMagic.Api.Settings; @@ -64,12 +65,17 @@ public class IntegerUpDown : Setting internal override string GetJsonValue() { - return Value.ToString(); + return Value.ToString(CultureInfo.InvariantCulture); } internal override void SetJsonValue(string value) { - _ = int.TryParse(value, out int result); + // Values saved by older versions use the current culture (e.g. a culture specific minus sign), so fall back to it. + if (!int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int result)) + { + _ = int.TryParse(value, NumberStyles.Integer, CultureInfo.CurrentCulture, out result); + } + Value = result; } } \ No newline at end of file diff --git a/src/DesktopMagicPluginAPI/Settings/Slider.cs b/src/DesktopMagicPluginAPI/Settings/Slider.cs index 003a58a..f066dc7 100644 --- a/src/DesktopMagicPluginAPI/Settings/Slider.cs +++ b/src/DesktopMagicPluginAPI/Settings/Slider.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics.CodeAnalysis; +using System.Globalization; namespace DesktopMagic.Api.Settings; @@ -65,12 +66,18 @@ public sealed class Slider : Setting internal override string GetJsonValue() { - return Value.ToString(); + return Value.ToString(CultureInfo.InvariantCulture); } internal override void SetJsonValue(string value) { - _ = double.TryParse(value, out double result); + // Values saved by older versions use the current culture (e.g. "30,5"), so fall back to it. + // Thousands separators are not allowed, otherwise "30,5" would be read as 305 by the invariant culture. + if (!double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out double result)) + { + _ = double.TryParse(value, NumberStyles.Float, CultureInfo.CurrentCulture, out result); + } + Value = result; } } \ No newline at end of file