mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-11-19 13:17:10 +00:00
allow multiple summarisations in a group
This commit is contained in:
3 files changed
+62
-10
No files matched your search
@@ -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.
|
||||
/// </summary>
|
||||
[Obsolete("Use IWikiSummaryProvider instead, as it allows for multiple summarisations. This will be removed in future versions.")]
|
||||
public interface ISummarizedOptions
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MiraAPI.GameOptions;
|
||||
|
||||
/// <summary>
|
||||
/// A holder option for a summary and option string names that are hidden behind it.
|
||||
/// </summary>
|
||||
/// <param name="hiddenKeys">The <see cref="StringNames"/> of the options being hidden.</param>
|
||||
/// <param name="summaryLines">A delegate that produces the summary to replace the default text of the options being hidden.</param>
|
||||
public sealed class WikiSummaryGroup(IReadOnlySet<StringNames>? hiddenKeys, Func<IEnumerable<string>>? summaryLines)
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the option string keys (StringNames) to omit from the wiki options list (e.g., a large set of related options).
|
||||
/// </summary>
|
||||
public IReadOnlySet<StringNames> HiddenKeys { get; } = hiddenKeys ?? new HashSet<StringNames>(0);
|
||||
|
||||
/// <summary>
|
||||
/// Gets summary lines to insert when the first hidden option would have appeared.
|
||||
/// Lines should already be formatted as "Title: Value".
|
||||
/// </summary>
|
||||
public Func<IEnumerable<string>> SummaryLines { get; } = summaryLines ?? Array.Empty<string>;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allows an option group to compress and format blocks or noisy options in the wiki "Options" section.
|
||||
/// </summary>
|
||||
public interface IWikiSummaryProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a dictionary where the key is the target option's <see cref="StringNames"/> to insert the
|
||||
/// summary at, and the value is the specific group of options being replaced by a summary.
|
||||
/// </summary>
|
||||
IReadOnlyDictionary<StringNames, WikiSummaryGroup> SummaryGroups { get; }
|
||||
}
|
||||
@@ -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<StringNames>();
|
||||
var summaryGroups = new Dictionary<StringNames, WikiSummaryGroup>();
|
||||
|
||||
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();
|
||||
|
||||
Reference in new issue
Block a user