using System.Linq.Expressions; using AIStudio.Settings.DataModel; using LuaTable = Lua.LuaTable; using LuaValueType = Lua.LuaValueType; namespace AIStudio.Settings; public static partial class ManagedConfiguration { public static bool TryProcessProfileIds( Expression> configSelection, Expression>> propertyExpression, Guid configPluginId, LuaTable settings, bool dryRun) { if (!TryGet(configSelection, propertyExpression, out var configMeta)) return false; var successful = false; ISet configuredValue = configMeta.Default; if (settings.TryGetValue(SettingsManager.ToSettingName(propertyExpression), out var configuredLuaList) && configuredLuaList.TryRead(out var valueTable)) { successful = TryReadProfileIds(valueTable, out var values); configuredValue = values; } return HandleParsedScalarValue( configPluginId, dryRun, successful, configMeta, configuredValue, ReadManagedConfigurationMode(propertyExpression, settings), SettingsManager.ToSettingName(propertyExpression)); } public static bool TryProcessLegacyProfileIds( Expression> configSelection, Expression>> propertyExpression, string legacySettingName, Guid configPluginId, LuaTable settings, bool dryRun) { if (!TryGet(configSelection, propertyExpression, out var configMeta) || !settings.TryGetValue(legacySettingName, out var legacyValue) || !legacyValue.TryRead(out var legacyProfileId)) return false; if (!string.IsNullOrWhiteSpace(legacyProfileId) && !Guid.TryParse(legacyProfileId, out _)) return false; ISet configuredValue = Guid.TryParse(legacyProfileId, out var profileId) && profileId != Guid.Empty ? new HashSet { profileId.ToString() } : new HashSet(); return HandleParsedScalarValue( configPluginId, dryRun, true, configMeta, configuredValue, ReadManagedConfigurationMode(propertyExpression, settings), SettingsManager.ToSettingName(propertyExpression)); } public static HashSet? RegisterProfilePreselection( Expression>? configSelection, Expression?>> propertyExpression) { if (configSelection is null) return null; var configPath = Path(configSelection, propertyExpression); if (!METADATA.ContainsKey(configPath)) { METADATA[configPath] = new ConfigMeta?>(configSelection, propertyExpression) { Default = null, AllowNullSnapshot = true, }; } return null; } public static bool TryGetProfilePreselection( Expression> configSelection, Expression?>> propertyExpression, out ConfigMeta?> configMeta) { var configPath = Path(configSelection, propertyExpression); if (METADATA.TryGetValue(configPath, out var value) && value is ConfigMeta?> meta) { meta.RestoreLockedConfiguration(); configMeta = meta; return true; } configMeta = new NoConfig?>(configSelection, propertyExpression) { Default = null, }; return false; } public static bool TryProcessProfilePreselection( Expression> configSelection, Expression?>> propertyExpression, Guid configPluginId, LuaTable settings, bool dryRun) { if (!TryGetProfilePreselection(configSelection, propertyExpression, out var configMeta)) return false; var successful = false; HashSet? configuredValue = null; if (settings.TryGetValue(SettingsManager.ToSettingName(propertyExpression), out var configuredLuaList) && configuredLuaList.Type is LuaValueType.Table && configuredLuaList.TryRead(out var valueTable)) { successful = TryReadProfileIds(valueTable, out var values); if (successful) configuredValue = values; } if (dryRun) return successful; return HandleParsedScalarValue( configPluginId, dryRun, successful, configMeta, configuredValue, ReadManagedConfigurationMode(propertyExpression, settings), SettingsManager.ToSettingName(propertyExpression)); } public static bool TryProcessLegacyProfilePreselection( Expression> configSelection, Expression?>> propertyExpression, string legacySettingName, Guid configPluginId, LuaTable settings, bool dryRun) { if (!TryGetProfilePreselection(configSelection, propertyExpression, out var configMeta) || !settings.TryGetValue(legacySettingName, out var legacyValue) || !legacyValue.TryRead(out var legacyProfileId)) return false; HashSet? configuredValue; if (string.IsNullOrWhiteSpace(legacyProfileId)) configuredValue = null; else if (!Guid.TryParse(legacyProfileId, out var parsedProfileId)) return false; else configuredValue = parsedProfileId == Guid.Empty ? [] : [parsedProfileId.ToString()]; return HandleParsedScalarValue( configPluginId, dryRun, true, configMeta, configuredValue, ReadManagedConfigurationMode(propertyExpression, settings), SettingsManager.ToSettingName(propertyExpression)); } private static bool TryReadProfileIds(LuaTable valueTable, out HashSet values) { values = new HashSet(StringComparer.OrdinalIgnoreCase); for (var index = 1; index <= valueTable.ArrayLength; index++) { if (!valueTable[index].TryRead(out var value) || !Guid.TryParse(value, out var profileId) || profileId == Guid.Empty || !values.Add(profileId.ToString())) return false; } return true; } }