From ed006f3a4b12ef0f690cd66f6e210791f3364f66 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sun, 1 Jun 2025 20:46:33 +0200 Subject: [PATCH] Added SettingsLocker service and implementation --- app/MindWork AI Studio/Program.cs | 1 + .../Settings/SettingsLocker.cs | 78 +++++++++++++++++++ .../Tools/PluginSystem/PluginFactory.cs | 1 + 3 files changed, 80 insertions(+) create mode 100644 app/MindWork AI Studio/Settings/SettingsLocker.cs diff --git a/app/MindWork AI Studio/Program.cs b/app/MindWork AI Studio/Program.cs index 81b25fa7..04247f56 100644 --- a/app/MindWork AI Studio/Program.cs +++ b/app/MindWork AI Studio/Program.cs @@ -126,6 +126,7 @@ internal sealed class Program builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); + builder.Services.AddSingleton(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); diff --git a/app/MindWork AI Studio/Settings/SettingsLocker.cs b/app/MindWork AI Studio/Settings/SettingsLocker.cs new file mode 100644 index 00000000..b23e1085 --- /dev/null +++ b/app/MindWork AI Studio/Settings/SettingsLocker.cs @@ -0,0 +1,78 @@ +using System.Linq.Expressions; + +namespace AIStudio.Settings; + +public sealed class SettingsLocker +{ + private static readonly ILogger LOGGER = Program.LOGGER_FACTORY.CreateLogger(); + private readonly Dictionary> lockedProperties = new(); + + public void Register(Expression> propertyExpression, Guid configurationPluginId) + { + var memberExpression = GetMemberExpression(propertyExpression); + var className = typeof(T).Name; + var propertyName = memberExpression.Member.Name; + + if (!this.lockedProperties.ContainsKey(className)) + this.lockedProperties[className] = []; + + this.lockedProperties[className].TryAdd(propertyName, configurationPluginId); + } + + public void Remove(Expression> propertyExpression) + { + var memberExpression = GetMemberExpression(propertyExpression); + var className = typeof(T).Name; + var propertyName = memberExpression.Member.Name; + + if (this.lockedProperties.TryGetValue(className, out var props)) + { + if (props.Remove(propertyName)) + { + // If the property was removed, check if the class has no more locked properties: + if (props.Count == 0) + this.lockedProperties.Remove(className); + } + } + } + + public Guid GetConfigurationPluginId(Expression> propertyExpression) + { + var memberExpression = GetMemberExpression(propertyExpression); + var className = typeof(T).Name; + var propertyName = memberExpression.Member.Name; + + if (this.lockedProperties.TryGetValue(className, out var props) && props.TryGetValue(propertyName, out var configurationPluginId)) + return configurationPluginId; + + // No configuration plugin ID found for this property: + return Guid.Empty; + } + + public bool IsLocked(Expression> propertyExpression) + { + var memberExpression = GetMemberExpression(propertyExpression); + var className = typeof(T).Name; + var propertyName = memberExpression.Member.Name; + + return this.lockedProperties.TryGetValue(className, out var props) && props.ContainsKey(propertyName); + } + + private static MemberExpression GetMemberExpression(Expression> expression) + { + switch (expression.Body) + { + // Case for value types, which are wrapped in UnaryExpression: + case UnaryExpression { NodeType: ExpressionType.Convert } unaryExpression: + return (MemberExpression)unaryExpression.Operand; + + // Case for reference types, which are directly MemberExpressions: + case MemberExpression memberExpression: + return memberExpression; + + default: + LOGGER.LogError($"Expression '{expression}' is not a valid property expression."); + throw new ArgumentException($"Expression '{expression}' is not a valid property expression.", nameof(expression)); + } + } +} \ No newline at end of file diff --git a/app/MindWork AI Studio/Tools/PluginSystem/PluginFactory.cs b/app/MindWork AI Studio/Tools/PluginSystem/PluginFactory.cs index 8dc83966..f424a133 100644 --- a/app/MindWork AI Studio/Tools/PluginSystem/PluginFactory.cs +++ b/app/MindWork AI Studio/Tools/PluginSystem/PluginFactory.cs @@ -6,6 +6,7 @@ public static partial class PluginFactory { private static readonly ILogger LOG = Program.LOGGER_FACTORY.CreateLogger(nameof(PluginFactory)); private static readonly SettingsManager SETTINGS_MANAGER = Program.SERVICE_PROVIDER.GetRequiredService(); + private static readonly SettingsLocker SETTINGS_LOCKER = Program.SERVICE_PROVIDER.GetRequiredService(); private static bool IS_INITIALIZED; private static string DATA_DIR = string.Empty;