Fixed plugin values which depended on the region settings of the computer (#1194)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-12 08:57:50 +01:00
1 parent 65d50de033
commit a1b493cb13
20 files changed
+397 -137

No files matched your search

@@ -1,3 +1,5 @@
using System.Globalization;
using AIStudio.Tools.PluginSystem.Assistants.Icons;
namespace AIStudio.Tools.PluginSystem.Assistants.DataModel;
@@ -9,26 +11,58 @@ internal static class AssistantComponentPropHelper
/// </summary>
private const string LUCIDE_PREFIX = "lucide:";
/// <summary>
/// Reads a prop as text. A number, which Lua hands over as a double, becomes the same text on every computer,
/// the one Lua itself writes: 12.5 stays "12.5", also where the region settings use a decimal comma.
/// </summary>
public static string ReadString(Dictionary<string, object> props, string key)
{
if (props.TryGetValue(key, out var value))
return value.ToString() ?? string.Empty;
if (!props.TryGetValue(key, out var value))
return string.Empty;
return string.Empty;
if (value is IFormattable formattable)
return formattable.ToString(null, CultureInfo.InvariantCulture);
return value.ToString() ?? string.Empty;
}
public static void WriteString(Dictionary<string, object> props, string key, string value) => props[key] = value;
public static int ReadInt(Dictionary<string, object> props, string key, int fallback = 0)
{
return props.TryGetValue(key, out var value) && int.TryParse(value.ToString(), out var i) ? i : fallback;
}
public static int ReadInt(Dictionary<string, object> props, string key, int fallback = 0) => TryReadInt(props, key, out var number) ? number : fallback;
public static void WriteInt(Dictionary<string, object> props, string key, int value) => props[key] = value;
public static int? ReadNullableInt(Dictionary<string, object> props, string key)
public static int? ReadNullableInt(Dictionary<string, object> props, string key) => TryReadInt(props, key, out var number) ? number : null;
/// <summary>
/// Reads a whole number, independent of the region settings of the computer.
/// </summary>
/// <remarks>
/// Lua hands over every number as a double, and the setters of the components store an int. A plugin may also
/// write the number as text. A number with a fraction, or one beyond the range of an int, counts as missing.
/// </remarks>
private static bool TryReadInt(Dictionary<string, object> props, string key, out int number)
{
return props.TryGetValue(key, out var value) && int.TryParse(value.ToString(), out var i) ? i : null;
number = 0;
if (!props.TryGetValue(key, out var value))
return false;
switch (value)
{
case int whole:
number = whole;
return true;
case double real when double.IsInteger(real) && real is >= int.MinValue and <= int.MaxValue:
number = (int)real;
return true;
case string text:
return int.TryParse(text, NumberStyles.Integer, CultureInfo.InvariantCulture, out number);
default:
return false;
}
}
public static void WriteNullableInt(Dictionary<string, object> props, string key, int? value)