using System.Linq.Expressions; using AIStudio.Settings.DataModel; namespace AIStudio.Settings; /// /// Represents configuration metadata for a specific class and property. /// /// The class type that contains the configuration property. /// The type of the configuration property value. public record ConfigMeta : ConfigMetaBase { public ConfigMeta(Expression> configSelection, Expression> propertyExpression) : base(SettingsManager.ToSettingName(propertyExpression)) { this.ConfigSelection = configSelection; this.PropertyExpression = propertyExpression; } /// /// The expression to select the configuration class from the settings data. /// private Expression> ConfigSelection { get; } /// /// The expression to select the property within the configuration class. /// private Expression> PropertyExpression { get; } /// /// The default value for the configuration property. This is used when resetting the property to its default state. /// public required TValue Default { get; init; } /// /// The additive value contribution provided by a configuration plugin. /// public TValue PluginContribution { get; private set; } = default!; /// /// Stores an additive plugin contribution. /// public void SetPluginContribution(TValue value, Guid pluginId) { this.PluginContribution = value; this.PluginContributionByConfigPluginId = pluginId; this.HasPluginContribution = true; } /// public override void ClearPluginContribution() { this.PluginContribution = default!; base.ClearPluginContribution(); } /// protected override void Reset() { var configInstance = this.ConfigSelection.Compile().Invoke(SettingsManagerAccess.ConfigurationData); var memberExpression = this.PropertyExpression.GetMemberExpression(); if (memberExpression.Member is System.Reflection.PropertyInfo propertyInfo) propertyInfo.SetValue(configInstance, this.Default); } /// /// Sets the value of the configuration property to the specified value. /// /// The value to set for the configuration property. public void SetValue(TValue value) { var configInstance = this.ConfigSelection.Compile().Invoke(SettingsManagerAccess.ConfigurationData); var memberExpression = this.PropertyExpression.GetMemberExpression(); if (memberExpression.Member is System.Reflection.PropertyInfo propertyInfo) propertyInfo.SetValue(configInstance, value); } /// /// Gets the current value of the configuration property. /// public TValue GetValue() { var configInstance = this.ConfigSelection.Compile().Invoke(SettingsManagerAccess.ConfigurationData); var memberExpression = this.PropertyExpression.GetMemberExpression(); if (memberExpression.Member is System.Reflection.PropertyInfo propertyInfo && propertyInfo.GetValue(configInstance) is TValue value) return value; return default!; } }