2025-06-01 19:14:21 +00:00
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
|
|
|
namespace AIStudio.Settings;
|
|
|
|
|
|
|
|
public sealed class SettingsLocker
|
|
|
|
{
|
|
|
|
private readonly Dictionary<string, Dictionary<string, Guid>> lockedProperties = new();
|
|
|
|
|
2025-07-31 13:51:41 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Registers a property of a class to be locked by a specific configuration plugin ID.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="propertyExpression">The property expression to lock.</param>
|
|
|
|
/// <param name="configurationPluginId">The ID of the configuration plugin that locks the property.</param>
|
|
|
|
/// <typeparam name="T">The type of the class that contains the property.</typeparam>
|
2025-06-01 19:14:21 +00:00
|
|
|
public void Register<T>(Expression<Func<T, object>> propertyExpression, Guid configurationPluginId)
|
|
|
|
{
|
2025-07-31 13:51:19 +00:00
|
|
|
var memberExpression = propertyExpression.GetMemberExpression();
|
2025-06-01 19:14:21 +00:00
|
|
|
var className = typeof(T).Name;
|
|
|
|
var propertyName = memberExpression.Member.Name;
|
|
|
|
|
|
|
|
if (!this.lockedProperties.ContainsKey(className))
|
|
|
|
this.lockedProperties[className] = [];
|
|
|
|
|
|
|
|
this.lockedProperties[className].TryAdd(propertyName, configurationPluginId);
|
|
|
|
}
|
|
|
|
|
2025-07-31 13:51:41 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Removes the lock for a property of a class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="propertyExpression">The property expression to remove the lock for.</param>
|
|
|
|
/// <typeparam name="T">The type of the class that contains the property.</typeparam>
|
2025-06-01 19:14:21 +00:00
|
|
|
public void Remove<T>(Expression<Func<T, object>> propertyExpression)
|
|
|
|
{
|
2025-07-31 13:51:19 +00:00
|
|
|
var memberExpression = propertyExpression.GetMemberExpression();
|
2025-06-01 19:14:21 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-07-31 13:51:41 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the configuration plugin ID that locks a specific property of a class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="propertyExpression"></param>
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
/// <returns></returns>
|
2025-06-01 19:14:21 +00:00
|
|
|
public Guid GetConfigurationPluginId<T>(Expression<Func<T, object>> propertyExpression)
|
|
|
|
{
|
2025-07-31 13:51:19 +00:00
|
|
|
var memberExpression = propertyExpression.GetMemberExpression();
|
2025-06-01 19:14:21 +00:00
|
|
|
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<T>(Expression<Func<T, object>> propertyExpression)
|
|
|
|
{
|
2025-07-31 13:51:19 +00:00
|
|
|
var memberExpression = propertyExpression.GetMemberExpression();
|
2025-06-01 19:14:21 +00:00
|
|
|
var className = typeof(T).Name;
|
|
|
|
var propertyName = memberExpression.Member.Name;
|
|
|
|
|
|
|
|
return this.lockedProperties.TryGetValue(className, out var props) && props.ContainsKey(propertyName);
|
|
|
|
}
|
|
|
|
}
|