diff --git a/MiraAPI/GameOptions/ISummarizedOptions.cs b/MiraAPI/GameOptions/ISummarizedOptions.cs index 0ee63e17..6e9305c5 100644 --- a/MiraAPI/GameOptions/ISummarizedOptions.cs +++ b/MiraAPI/GameOptions/ISummarizedOptions.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; namespace MiraAPI.GameOptions; @@ -6,6 +7,7 @@ namespace MiraAPI.GameOptions; /// Allows an option group to compress noisy option blocks in the wiki "Options" section. /// Implementers can hide specific option keys and replace them with one or more summary lines. /// +[Obsolete("Use IWikiSummaryProvider instead, as it allows for multiple summarisations. This will be removed in future versions.")] public interface ISummarizedOptions { /// diff --git a/MiraAPI/GameOptions/IWikiSummaryProvider.cs b/MiraAPI/GameOptions/IWikiSummaryProvider.cs new file mode 100644 index 00000000..ee32fa31 --- /dev/null +++ b/MiraAPI/GameOptions/IWikiSummaryProvider.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; + +namespace MiraAPI.GameOptions; + +/// +/// A holder option for a summary and option string names that are hidden behind it. +/// +/// The of the options being hidden. +/// A delegate that produces the summary to replace the default text of the options being hidden. +public sealed class WikiSummaryGroup(IReadOnlySet? hiddenKeys, Func>? summaryLines) +{ + /// + /// Gets the option string keys (StringNames) to omit from the wiki options list (e.g., a large set of related options). + /// + public IReadOnlySet HiddenKeys { get; } = hiddenKeys ?? new HashSet(0); + + /// + /// Gets summary lines to insert when the first hidden option would have appeared. + /// Lines should already be formatted as "Title: Value". + /// + public Func> SummaryLines { get; } = summaryLines ?? Array.Empty; +} + +/// +/// Allows an option group to compress and format blocks or noisy options in the wiki "Options" section. +/// +public interface IWikiSummaryProvider +{ + /// + /// Gets a dictionary where the key is the target option's to insert the + /// summary at, and the value is the specific group of options being replaced by a summary. + /// + IReadOnlyDictionary SummaryGroups { get; } +} diff --git a/MiraAPI/Utilities/Helpers.cs b/MiraAPI/Utilities/Helpers.cs index fdbf034d..219af1c2 100644 --- a/MiraAPI/Utilities/Helpers.cs +++ b/MiraAPI/Utilities/Helpers.cs @@ -63,9 +63,24 @@ public static class Helpers var headerAdded = false; var options = group.Children; var builder = new StringBuilder(); - var summaryProvider = group as ISummarizedOptions; - var hiddenKeys = summaryProvider?.WikiHiddenOptionKeys; - var insertedSummary = false; + var allHiddenKeys = new HashSet(); + var summaryGroups = new Dictionary(); + + if (group is IWikiSummaryProvider provider && provider.SummaryGroups != null) + { + foreach (var (header, summaryGroup) in provider.SummaryGroups) + { + summaryGroups[header] = summaryGroup; + allHiddenKeys.UnionWith(summaryGroup.HiddenKeys); + } + } + else if (group is ISummarizedOptions summariser && summariser.WikiHiddenOptionKeys?.Count is > 0) + { + summaryGroups[summariser.WikiHiddenOptionKeys.First()] = new(summariser.WikiHiddenOptionKeys, summariser.GetWikiOptionSummaryLines); + allHiddenKeys.UnionWith(summariser.WikiHiddenOptionKeys); + } + + allHiddenKeys.UnionWith(summaryGroups.Keys); void EnsureHeader() { @@ -78,12 +93,14 @@ public static class Helpers foreach (var option in options) { - var key = option.StringName; - var isHidden = hiddenKeys?.Contains(key) == true; + if (!option.Visible()) + continue; - if (!insertedSummary && summaryProvider != null && isHidden) + var key = option.StringName; + + if (summaryGroups.TryGetValue(key, out var summaryGroup)) { - foreach (var line in summaryProvider.GetWikiOptionSummaryLines()) + foreach (var line in summaryGroup.SummaryLines()) { if (string.IsNullOrWhiteSpace(line)) continue; @@ -91,11 +108,9 @@ public static class Helpers EnsureHeader(); builder.AppendLine(line); } - - insertedSummary = true; } - if (isHidden || !option.Visible()) + if (allHiddenKeys.Contains(key)) continue; EnsureHeader();