diff --git a/MonkeyLoader.Resonite.Integration/DataFeeds/Settings/ConfigSectionSettingsItems.cs b/MonkeyLoader.Resonite.Integration/DataFeeds/Settings/ConfigSectionSettingsItems.cs index ada6437..3468cbf 100644 --- a/MonkeyLoader.Resonite.Integration/DataFeeds/Settings/ConfigSectionSettingsItems.cs +++ b/MonkeyLoader.Resonite.Integration/DataFeeds/Settings/ConfigSectionSettingsItems.cs @@ -25,7 +25,7 @@ internal sealed class ConfigSectionSettingsItems : DataFeedBuildingBlockMonkey 400; @@ -99,56 +99,22 @@ private static async IAsyncEnumerable GenerateEnumItemsAsync(IR yield break; } - var flagsEnumGroup = new DataFeedGroup(); - flagsEnumGroup.InitBase(configKey.FullId, path, groupKeys, configKey.GetLocaleKey("Name").AsLocaleKey()); - flagsEnumGroup.InitDescription(configKey.GetLocaleKey("Description").AsLocaleKey()); - yield return flagsEnumGroup; - - var flagsGrouping = groupKeys.Concat(configKey.FullId).ToArray(); + var items = (IEnumerable)_generateFlagsEnumItems + .MakeGenericMethod(typeof(T)) + .Invoke(null, [path, groupKeys, configKey]); - foreach (var value in Enum.GetValues(configKey.ValueType).Cast()) + foreach (var item in items) { - var name = value.ToString(); - var longValue = Convert.ToInt64(value); - - var flagToggle = new DataFeedToggle(); - flagToggle.InitBase($"{configKey.FullId}.{name}", path, flagsGrouping, name); - flagToggle.InitDescription(Mod.GetLocaleString("EnumToggle.Description", ("EnumName", configKey.ValueType.Name), ("FlagName", name))); - flagToggle.InitSetupValue(field => + if (item is DataFeedGroup) { - field.Value = (Convert.ToInt64(configKey.GetValue()) & longValue) == longValue; - - void FieldChanged(IChangeable changeable) - => configKey.TrySetValue(Enum.ToObject(configKey.ValueType, field.Value ? Convert.ToInt64(configKey.GetValue()) | longValue : Convert.ToInt64(configKey.GetValue()) & ~longValue)); - - void KeyChanged(object sender, ConfigKeyChangedEventArgs changedEvent) - { - if (field.FilterWorldElement() is null) - { - configKey.Changed -= KeyChanged; - return; - } - - var newValue = Convert.ToInt64(changedEvent.NewValue); - var isPartialCombinedValue = (newValue & longValue) != 0; - - field.World.RunSynchronously(() => - { - if (isPartialCombinedValue) - field.Changed -= FieldChanged; - - field.Value = (newValue & longValue) == longValue; - - if (isPartialCombinedValue) - field.Changed += FieldChanged; - }); - } - - field.Changed += FieldChanged; - configKey.Changed += KeyChanged; - }); - - yield return flagToggle; + item.ItemKey = configKey.FullId + ".Flags"; + item.GroupingParameters = groupKeys; + } + else if (item is DataFeedToggle) + { + item.GroupingParameters = groupKeys.Concat([configKey.FullId + ".Flags"]).ToArray(); + } + yield return item; } } @@ -233,12 +199,14 @@ private static async IAsyncEnumerable GenerateNullableEnumItemsAsy await Task.CompletedTask; var nullableEnumGroup = new DataFeedGroup(); - nullableEnumGroup.InitBase(configKey.FullId + ".NullableGroup", path, [configKey.Section.Id], configKey.GetLocaleKey("Name").AsLocaleKey()); + nullableEnumGroup.InitBase(configKey.FullId + ".NullableGroup", path, groupKeys, configKey.GetLocaleKey("Name").AsLocaleKey()); nullableEnumGroup.InitDescription(configKey.GetLocaleKey("Description").AsLocaleKey()); yield return nullableEnumGroup; + var nullableGroupKeys = groupKeys.Concat([configKey.FullId + ".NullableGroup"]).ToArray(); + var nullableToggle = new DataFeedToggle(); - nullableToggle.InitBase(configKey.FullId + ".HasValue", path, [configKey.Section.Id, configKey.FullId + ".NullableGroup"], configKey.GetLocaleKey("HasValue").AsLocaleKey()); + nullableToggle.InitBase(configKey.FullId + ".HasValue", path, nullableGroupKeys, configKey.GetLocaleKey("HasValue").AsLocaleKey()); nullableToggle.InitDescription(configKey.GetLocaleKey("HasValue").AsLocaleKey()); nullableToggle.InitSetupValue(field => field.SyncWithNullableConfigKeyHasValue(configKey)); yield return nullableToggle; @@ -246,7 +214,7 @@ private static async IAsyncEnumerable GenerateNullableEnumItemsAsy if (typeof(T).GetCustomAttribute() is null) { var enumField = new DataFeedEnum(); - enumField.InitBase(configKey.FullId, path, [configKey.Section.Id, configKey.FullId + ".NullableGroup"], configKey.GetLocaleString("Name"), configKey.GetLocaleString("Description")); + enumField.InitBase(configKey.FullId, path, nullableGroupKeys, configKey.GetLocaleString("Name"), configKey.GetLocaleString("Description")); enumField.InitSetupValue(field => { var slot = field.FindNearestParent(); @@ -263,35 +231,38 @@ private static async IAsyncEnumerable GenerateNullableEnumItemsAsy } else { - var items = (IEnumerable)_generateNullableFlagsEnumItemsAsync + var items = (IEnumerable)_generateFlagsEnumItems .MakeGenericMethod(typeof(T)) - .Invoke(null, [path, configKey]); + .Invoke(null, [path, groupKeys, configKey]); foreach (var item in items) { if (item is DataFeedGroup) { item.ItemKey = configKey.FullId + ".Flags"; - item.GroupingParameters = [configKey.Section.Id, configKey.FullId + ".NullableGroup"]; + item.GroupingParameters = nullableGroupKeys; } else if (item is DataFeedToggle) { - item.GroupingParameters = [configKey.Section.Id, configKey.FullId + ".NullableGroup", configKey.FullId + ".Flags"]; + item.GroupingParameters = nullableGroupKeys.Concat([configKey.FullId + ".Flags"]).ToArray(); } yield return item; } } } - private static IEnumerable GenerateNullableFlagsEnumFields(IReadOnlyList path, IDefiningConfigKey configKey) - where T : struct, Enum + private static async IAsyncEnumerable GenerateFlagsEnumFields(IReadOnlyList path, IReadOnlyList groupKeys, IDefiningConfigKey configKey) + where T : Enum { + await Task.CompletedTask; + var flagsEnumGroup = new DataFeedGroup(); - flagsEnumGroup.InitBase(configKey.FullId, path, [configKey.Section.Id], configKey.GetLocaleKey("Name").AsLocaleKey()); + flagsEnumGroup.InitBase(configKey.FullId, path, groupKeys, configKey.GetLocaleKey("Name").AsLocaleKey()); flagsEnumGroup.InitDescription(configKey.GetLocaleKey("Description").AsLocaleKey()); yield return flagsEnumGroup; - var flagsGrouping = new[] { configKey.Section.Id, configKey.FullId }; + //var flagsGrouping = new[] { configKey.Section.Id, configKey.FullId }; + var flagsGrouping = groupKeys.Concat(configKey.FullId).ToArray(); var enumType = typeof(T);