From accfd0420903ea47306a3e826e90a896ed130d9f Mon Sep 17 00:00:00 2001 From: Az <56699741+AlchlcDvl@users.noreply.github.com> Date: Mon, 26 Oct 2026 04:47:07 -0400 Subject: [PATCH] several big changes - abstract display value behaviour to the option itself - updated deprecation notices a bit - enforced some consistency - moved the notification code up in the call stack into SetValue - made the number option round floats instead of directly casting, due to casting having a truncation behaviour (0.999 becomes 0, not 1) - optimised the wiki behaviour --- MiraAPI/GameOptions/AbstractOptionGroup.cs | 2 +- .../Attributes/ModdedEnumOptionAttribute.cs | 2 +- .../ModdedEnumOptionListAttribute.cs | 2 +- MiraAPI/GameOptions/IModdedOption.cs | 6 + MiraAPI/GameOptions/ModdedOptionsManager.cs | 4 +- .../OptionTypes/ModdedEnumOption.cs | 38 +---- .../OptionTypes/ModdedNumberOption.cs | 43 ++++-- .../GameOptions/OptionTypes/ModdedOption.cs | 30 ++++ .../OptionTypes/ModdedPlayerOption.cs | 14 +- .../OptionTypes/ModdedStringOption.cs | 8 +- .../OptionTypes/ModdedToggleOption.cs | 30 ++-- MiraAPI/Patches/RoleGuidePatches.cs | 2 +- MiraAPI/Resources/Locale/en_US.xml | 2 - MiraAPI/Translation/MiraLocaleManager.cs | 4 +- .../Assets/LoadableAudioResourceAsset.cs | 2 +- .../Utilities/Assets/LoadableResourceAsset.cs | 2 +- MiraAPI/Utilities/GuideOptionsPatches.cs | 46 +------ MiraAPI/Utilities/Helpers.cs | 130 +++++------------- 18 files changed, 131 insertions(+), 236 deletions(-) diff --git a/MiraAPI/GameOptions/AbstractOptionGroup.cs b/MiraAPI/GameOptions/AbstractOptionGroup.cs index 589f705e..94d9409b 100644 --- a/MiraAPI/GameOptions/AbstractOptionGroup.cs +++ b/MiraAPI/GameOptions/AbstractOptionGroup.cs @@ -44,7 +44,7 @@ public abstract class AbstractOptionGroup /// Gets a value indicating whether the group should be shown in the modifiers menu. /// // Completed: make this not a boolean - [Obsolete("Use ParentMenu instead.", true)] + [Obsolete("Use ParentMenu instead. This will be removed in a future version.", true)] [SuppressMessage("Info Code Smell", "S1133:Deprecated code should be removed", Justification = "Retained.")] public virtual bool ShowInModifiersMenu => false; diff --git a/MiraAPI/GameOptions/Attributes/ModdedEnumOptionAttribute.cs b/MiraAPI/GameOptions/Attributes/ModdedEnumOptionAttribute.cs index ece868d7..8cc34656 100644 --- a/MiraAPI/GameOptions/Attributes/ModdedEnumOptionAttribute.cs +++ b/MiraAPI/GameOptions/Attributes/ModdedEnumOptionAttribute.cs @@ -11,7 +11,7 @@ namespace MiraAPI.GameOptions.Attributes; /// The type. /// An option list of string values to use in place of the name. [AttributeUsage(AttributeTargets.Property)] -[Obsolete("Use ModdedEnumOptionAttribute instead.")] +[Obsolete("Use ModdedEnumOptionAttribute instead. This will be removed in a future version.")] public class ModdedEnumOptionAttribute(string title, Type enumType, string[]? values = null) : ModdedOptionAttribute(title) { diff --git a/MiraAPI/GameOptions/Attributes/OptionList/ModdedEnumOptionListAttribute.cs b/MiraAPI/GameOptions/Attributes/OptionList/ModdedEnumOptionListAttribute.cs index 8a6a237c..7fe03f6f 100644 --- a/MiraAPI/GameOptions/Attributes/OptionList/ModdedEnumOptionListAttribute.cs +++ b/MiraAPI/GameOptions/Attributes/OptionList/ModdedEnumOptionListAttribute.cs @@ -12,7 +12,7 @@ namespace MiraAPI.GameOptions.Attributes; /// The type. /// An option list of string values to use in place of the name. [AttributeUsage(AttributeTargets.Property)] -[Obsolete("Use ModdedEnumOptionListAttribute instead.")] +[Obsolete("Use ModdedEnumOptionListAttribute instead. This will be removed in a future version.")] public class ModdedEnumOptionListAttribute(string title, Type enumType, string[]? values = null) : ModdedOptionListAttribute(title) { diff --git a/MiraAPI/GameOptions/IModdedOption.cs b/MiraAPI/GameOptions/IModdedOption.cs index a40c9460..34a2ffdd 100644 --- a/MiraAPI/GameOptions/IModdedOption.cs +++ b/MiraAPI/GameOptions/IModdedOption.cs @@ -98,6 +98,12 @@ public interface IModdedOption /// The created object. OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOption numberOpt, StringOption stringOpt, PlayerOption playerOpt, Transform container); + /// + /// Gets the translated display value of the toggle option. + /// + /// The display value. + string GetDisplayValue(); + /// /// Gets the value as a . /// diff --git a/MiraAPI/GameOptions/ModdedOptionsManager.cs b/MiraAPI/GameOptions/ModdedOptionsManager.cs index 81fba525..5515d609 100644 --- a/MiraAPI/GameOptions/ModdedOptionsManager.cs +++ b/MiraAPI/GameOptions/ModdedOptionsManager.cs @@ -443,7 +443,7 @@ public static class ModdedOptionsManager /// The new object value. [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Harmony naming convention.")] [SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "Read above.")] - [Obsolete("Use PropertySetterPatch instead.")] + [Obsolete("Use PropertySetterPatch instead. This will be removed in a future version.")] public static void PropertySetterPatch(MethodBase __originalMethod, object value) { if (!SetterLookups.TryGetValue(__originalMethod, out var attribute)) @@ -485,7 +485,7 @@ public static class ModdedOptionsManager /// so the original getter gets skipped. [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Harmony naming convention")] [SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "Read above.")] - [Obsolete("Use PropertyGetterPatch instead.")] + [Obsolete("Use PropertyGetterPatch instead. This will be removed in a future version.")] public static bool PropertyGetterPatch(MethodBase __originalMethod, ref object __result) { if (!GetterLookups.TryGetValue(__originalMethod, out var attribute)) diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedEnumOption.cs b/MiraAPI/GameOptions/OptionTypes/ModdedEnumOption.cs index c23fc76f..bbe61675 100644 --- a/MiraAPI/GameOptions/OptionTypes/ModdedEnumOption.cs +++ b/MiraAPI/GameOptions/OptionTypes/ModdedEnumOption.cs @@ -13,7 +13,7 @@ namespace MiraAPI.GameOptions.OptionTypes; /// /// An option for selecting an value. /// -[Obsolete("Use ModdedEnum instead.")] +[Obsolete("Use ModdedEnumOption instead. This will be removed in a future version.")] public class ModdedEnumOption : ModdedOption { /// @@ -94,23 +94,7 @@ public class ModdedEnumOption : ModdedOption /// protected override void OnValueChanged(int newValue) { - if (ShowNotification) - { - ModdedOptionsManager.AddSettingsChangeMessage( - HudManager.Instance.Notifier, - StringName, - Data.GetValueString(newValue), - Configuration.PopUpTextColor, - Configuration.PopUpIconTmp, - false); - } - - if (!OptionBehaviour) - { - return; - } - - if (OptionBehaviour is StringOption opt) + if (OptionBehaviour && OptionBehaviour is StringOption opt) { opt.Value = newValue; } @@ -201,23 +185,7 @@ public class ModdedEnumOption : ModdedOption /// protected override void OnValueChanged(T newValue) { - if (ShowNotification) - { - ModdedOptionsManager.AddSettingsChangeMessage( - HudManager.Instance.Notifier, - StringName, - Data.GetValueString(Convert.ToInt32(newValue, NumberFormatInfo.InvariantInfo)), - Configuration.PopUpTextColor, - Configuration.PopUpIconTmp, - false); - } - - if (!OptionBehaviour) - { - return; - } - - if (OptionBehaviour is StringOption opt) + if (OptionBehaviour && OptionBehaviour is StringOption opt) { opt.Value = Convert.ToInt32(newValue, NumberFormatInfo.InvariantInfo); } diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedNumberOption.cs b/MiraAPI/GameOptions/OptionTypes/ModdedNumberOption.cs index 3f3be883..5135027d 100644 --- a/MiraAPI/GameOptions/OptionTypes/ModdedNumberOption.cs +++ b/MiraAPI/GameOptions/OptionTypes/ModdedNumberOption.cs @@ -1,4 +1,5 @@ using System; +using System.Text.RegularExpressions; using AmongUs.GameOptions; using MiraAPI.Networking; using MiraAPI.Utilities; @@ -52,6 +53,8 @@ public class ModdedNumberOption : ModdedOption /// public bool ShiftIncrement { get; } + private static readonly Regex TrailingZeroRegex = new(@"\.0+(?!\d)", RegexOptions.Compiled, TimeSpan.FromMilliseconds(100)); + /// /// Initializes a new instance of the class. /// @@ -155,7 +158,7 @@ public class ModdedNumberOption : ModdedOption optionComponent.ZeroValue = ZeroWordValue; numberOption.SetUpFromData(Data, 20); - numberOption.OnValueChanged = (Il2CppSystem.Action)ValueChanged; + numberOption.OnValueChanged = (Action)ValueChanged; numberOption.Title = StringName; numberOption.Value = Value; @@ -171,6 +174,22 @@ public class ModdedNumberOption : ModdedOption return numberOption; } + /// + public override string GetDisplayValue() + { + if (this is { NegativeWordValue: not "#", Value: -1 }) + { + return NegativeWordValue; + } + + if (this is { ZeroWordValue: not "#", Value: 0 }) + { + return ZeroWordValue; + } + + return TrailingZeroRegex.Replace(Data.GetValueString(Value), string.Empty); + } + /// public override float GetFloatData() { @@ -198,24 +217,18 @@ public class ModdedNumberOption : ModdedOption /// protected override void OnValueChanged(float newValue) { - Value = Mathf.Clamp(newValue, Min, Max); - if (ShowNotification) - { - ModdedOptionsManager.AddSettingsChangeMessage( - HudManager.Instance.Notifier, - StringName, - Data.GetValueString(newValue), - Configuration.PopUpTextColor, - Configuration.PopUpIconTmp, - false); - } - - if (OptionBehaviour is NumberOption opt) + if (OptionBehaviour && OptionBehaviour is NumberOption opt) { opt.Value = Value; } } + /// + protected override float ModifyValue(float newValue, float oldValue) + { + return Math.Clamp(newValue, Min, Max); + } + /// /// Implicitly converts the option to an . /// @@ -223,6 +236,6 @@ public class ModdedNumberOption : ModdedOption /// Integer value. public static implicit operator int(ModdedNumberOption option) { - return (int)option.Value; + return (int)Math.Round(option.Value); } } diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedOption.cs b/MiraAPI/GameOptions/OptionTypes/ModdedOption.cs index 88b73642..fff57a57 100644 --- a/MiraAPI/GameOptions/OptionTypes/ModdedOption.cs +++ b/MiraAPI/GameOptions/OptionTypes/ModdedOption.cs @@ -50,6 +50,8 @@ public abstract class ModdedOption : IModdedOption get => Getter != null ? Getter(_value) : _value; protected set { + value = ModifyValue(value, _value); + if (Setter != null) value = Setter(value, _value); @@ -162,6 +164,17 @@ public abstract class ModdedOption : IModdedOption Rpc.Instance.Send(PlayerControl.LocalPlayer, new MiraOptionNetData(true, [GetNetData()]), true); } + if (ShowNotification) + { + ModdedOptionsManager.AddSettingsChangeMessage( + HudManager.Instance.Notifier, + StringName, + GetDisplayValue(), + Configuration.PopUpTextColor, + Configuration.PopUpIconTmp, + false); + } + OnValueChanged(_value); ShowNotification = true; } @@ -213,12 +226,29 @@ public abstract class ModdedOption : IModdedOption /// public abstract void HandleNetData(byte[] data); + /// + public virtual string GetDisplayValue() + { + return Data.GetValueString(GetFloatData()); + } + /// /// Handles the value changed event. /// /// The new value. protected abstract void OnValueChanged(T newValue); + /// + /// Modifies the incoming value with respect to the previous value. This is invoked before the (if it's set). + /// + /// The new value. + /// The old value. + /// The modified value. + protected virtual T ModifyValue(T newValue, T oldValue) + { + return newValue; + } + /// /// Gets the value from the option behaviour. /// diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedPlayerOption.cs b/MiraAPI/GameOptions/OptionTypes/ModdedPlayerOption.cs index 513aad0d..1b7f8090 100644 --- a/MiraAPI/GameOptions/OptionTypes/ModdedPlayerOption.cs +++ b/MiraAPI/GameOptions/OptionTypes/ModdedPlayerOption.cs @@ -138,19 +138,7 @@ public class ModdedPlayerOption : ModdedOption /// protected override void OnValueChanged(int newValue) { - ModdedOptionsManager.AddSettingsChangeMessage( - HudManager.Instance.Notifier, - StringName, - Data.GetValueString(newValue), - Configuration.PopUpTextColor, - Configuration.PopUpIconTmp, - false); - if (!OptionBehaviour) - { - return; - } - - if (OptionBehaviour is PlayerOption opt) + if (OptionBehaviour && OptionBehaviour is PlayerOption opt) { opt.Value = newValue; } diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedStringOption.cs b/MiraAPI/GameOptions/OptionTypes/ModdedStringOption.cs index b1c9cc17..ca52d831 100644 --- a/MiraAPI/GameOptions/OptionTypes/ModdedStringOption.cs +++ b/MiraAPI/GameOptions/OptionTypes/ModdedStringOption.cs @@ -168,13 +168,7 @@ public class ModdedStringOption : ModdedOption /// protected override void OnValueChanged(string newValue) { - HudManager.Instance.Notifier.AddSettingsChangeMessage(StringName, Data.GetValueString(GetIndex(newValue)), false); - if (!OptionBehaviour) - { - return; - } - - if (OptionBehaviour is StringOption opt) + if (OptionBehaviour && OptionBehaviour is StringOption opt) { opt.Value = GetIndex(newValue); } diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedToggleOption.cs b/MiraAPI/GameOptions/OptionTypes/ModdedToggleOption.cs index 31f04d12..4ce0d551 100644 --- a/MiraAPI/GameOptions/OptionTypes/ModdedToggleOption.cs +++ b/MiraAPI/GameOptions/OptionTypes/ModdedToggleOption.cs @@ -72,6 +72,18 @@ public class ModdedToggleOption : ModdedOption return toggleOption; } + /// + public override string GetDisplayValue() + { + return Value + ? (OnValue == "On" + ? TranslationController.Instance.GetString(StringNames.SettingsOn) + : OnValue.Translate()) + : (OffValue == "Off" + ? TranslationController.Instance.GetString(StringNames.SettingsOff) + : OffValue.Translate()); + } + /// public override float GetFloatData() { @@ -99,23 +111,7 @@ public class ModdedToggleOption : ModdedOption /// protected override void OnValueChanged(bool newValue) { - if (ShowNotification) - { - ModdedOptionsManager.AddSettingsChangeMessage( - HudManager.Instance.Notifier, - StringName, - (newValue ? OnValue : OffValue).Translate(), - Configuration.PopUpTextColor, - Configuration.PopUpIconTmp, - false); - } - - if (!OptionBehaviour) - { - return; - } - - if (OptionBehaviour is ToggleOption toggleOpt) + if (OptionBehaviour && OptionBehaviour is ToggleOption toggleOpt) { toggleOpt.CheckMark.enabled = newValue; } diff --git a/MiraAPI/Patches/RoleGuidePatches.cs b/MiraAPI/Patches/RoleGuidePatches.cs index f658d6fa..4afaa39f 100644 --- a/MiraAPI/Patches/RoleGuidePatches.cs +++ b/MiraAPI/Patches/RoleGuidePatches.cs @@ -2086,7 +2086,7 @@ public static class RoleGuidePatches vanillaEntries = BuildVanillaSettingsEntries(guide); } - _settingsPages = GuideOptionPages.BuildPages(guide.GetBoolString); + _settingsPages = GuideOptionPages.BuildPages(); if (_settingsPage > _settingsPages.Count) { _settingsPage = 0; diff --git a/MiraAPI/Resources/Locale/en_US.xml b/MiraAPI/Resources/Locale/en_US.xml index 9073a6f5..a95b4770 100644 --- a/MiraAPI/Resources/Locale/en_US.xml +++ b/MiraAPI/Resources/Locale/en_US.xml @@ -71,8 +71,6 @@ Outlive the timer or eliminate the crew. Previous Next No Results Found - On - Off Enabled Disabled Active diff --git a/MiraAPI/Translation/MiraLocaleManager.cs b/MiraAPI/Translation/MiraLocaleManager.cs index 1bd925df..d003223c 100644 --- a/MiraAPI/Translation/MiraLocaleManager.cs +++ b/MiraAPI/Translation/MiraLocaleManager.cs @@ -126,7 +126,7 @@ public static class MiraLocaleManager /// Call once per mod during plugin Load(). /// /// The mod GUID (e.g., "mira.example"). - [Obsolete("Pass typeof(YourPlugin).Assembly directly to Register for better performance.")] + [Obsolete("Pass typeof(YourPlugin).Assembly directly to Register for better performance. This will be removed in a future version.")] public static void Register(string modGuid) { var callingAssembly = Assembly.GetCallingAssembly(); @@ -150,7 +150,7 @@ public static class MiraLocaleManager /// /// The mod GUID (e.g., "mira.example"). /// The mod's root namespace (e.g., "mira.api"). - [Obsolete("Pass typeof(YourPlugin).Assembly directly to Register for better performance.")] + [Obsolete("Pass typeof(YourPlugin).Assembly directly to Register for better performance. This will be removed in a future version.")] public static void Register(string modGuid, string internalName) { var callingAssembly = Assembly.GetCallingAssembly(); diff --git a/MiraAPI/Utilities/Assets/LoadableAudioResourceAsset.cs b/MiraAPI/Utilities/Assets/LoadableAudioResourceAsset.cs index 164a3c78..6b6ebbcb 100644 --- a/MiraAPI/Utilities/Assets/LoadableAudioResourceAsset.cs +++ b/MiraAPI/Utilities/Assets/LoadableAudioResourceAsset.cs @@ -27,7 +27,7 @@ public class LoadableAudioResourceAsset : LoadableAsset /// Initializes a new instance of the class with the provided resource path. /// /// The path of the wave file. - [Obsolete("Pass typeof(YourPlugin).Assembly directly to the constructor for better performance.")] + [Obsolete("Pass typeof(YourPlugin).Assembly directly to the constructor for better performance. This will be removed in a future version.")] public LoadableAudioResourceAsset(string path) { _path = path; diff --git a/MiraAPI/Utilities/Assets/LoadableResourceAsset.cs b/MiraAPI/Utilities/Assets/LoadableResourceAsset.cs index 02303d43..b970d3f2 100644 --- a/MiraAPI/Utilities/Assets/LoadableResourceAsset.cs +++ b/MiraAPI/Utilities/Assets/LoadableResourceAsset.cs @@ -31,7 +31,7 @@ public class LoadableResourceAsset : LoadableAsset /// /// The path to the embedded resource. /// The pixels per unit for the loaded sprite. - [Obsolete("Pass typeof(YourPlugin).Assembly directly to the constructor for better performance.")] + [Obsolete("Pass typeof(YourPlugin).Assembly directly to the constructor for better performance. This will be removed in a future version.")] public LoadableResourceAsset(string path, float pixelsPerUnit = 100f) { _path = path; diff --git a/MiraAPI/Utilities/GuideOptionsPatches.cs b/MiraAPI/Utilities/GuideOptionsPatches.cs index a86472ac..d5df8b9e 100644 --- a/MiraAPI/Utilities/GuideOptionsPatches.cs +++ b/MiraAPI/Utilities/GuideOptionsPatches.cs @@ -1,10 +1,8 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text.RegularExpressions; using MiraAPI.GameModes; using MiraAPI.GameOptions; -using MiraAPI.GameOptions.OptionTypes; using MiraAPI.PluginLoading; using MiraAPI.Translation; using MiraAPI.Utilities.Assets; @@ -56,9 +54,7 @@ internal sealed class GuideOptionPage( internal static class GuideOptionPages { - private static readonly Regex TrailingZeroRegex = new(@"\.0+(?!\d)", RegexOptions.Compiled, TimeSpan.FromMilliseconds(100)); - - public static List BuildPages(Func formatBool) + public static List BuildPages() { var pages = new List(); var combinedPages = new Dictionary(StringComparer.OrdinalIgnoreCase); @@ -94,8 +90,7 @@ internal static class GuideOptionPages item.Group, groupName, item.ModAbbreviation, - modSearchTerms, - formatBool); + modSearchTerms); if (entry != null) { entries.Add(entry); @@ -293,13 +288,12 @@ internal static class GuideOptionPages AbstractOptionGroup group, string groupName, string modAbbreviation, - IReadOnlyList modSearchTerms, - Func formatBool) + IReadOnlyList modSearchTerms) { try { var title = TranslationController.Instance.GetString(option.StringName); - var value = FormatValue(option, formatBool); + var value = option.GetDisplayValue(); return new GuideOptionEntry(title, value, group, groupName, modAbbreviation, modSearchTerms); } catch (Exception e) @@ -308,36 +302,4 @@ internal static class GuideOptionPages return null; } } - - private static string FormatValue(IModdedOption option, Func formatBool) - { - switch (option) - { - case ModdedToggleOption toggle: - return formatBool(toggle.Value); - - case ModdedEnumOption enumOption: - return MiraLocaleManager.Get( - enumOption.Values[enumOption.Value], - enumOption.Values[enumOption.Value]); - - case ModdedNumberOption number: - if (number is { NegativeWordValue: not "#", Value: -1 }) - { - return number.NegativeWordValue; - } - - if (number is { ZeroWordValue: not "#", Value: 0 }) - { - return number.ZeroWordValue; - } - - return TrailingZeroRegex.Replace(number.Data.GetValueString(number.Value), string.Empty); - - default: - return option.Data.Type == OptionTypes.Checkbox - ? formatBool(Math.Abs(option.GetFloatData() - 1f) < 0.0001f) - : option.Data.GetValueString(option.GetFloatData()); - } - } } diff --git a/MiraAPI/Utilities/Helpers.cs b/MiraAPI/Utilities/Helpers.cs index 94fea9f2..e41d60ec 100644 --- a/MiraAPI/Utilities/Helpers.cs +++ b/MiraAPI/Utilities/Helpers.cs @@ -5,13 +5,12 @@ using System.Collections.ObjectModel; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Linq; +using System.Reflection; using System.Text; -using System.Text.RegularExpressions; using HarmonyLib; using Il2CppInterop.Runtime; using Il2CppInterop.Runtime.InteropTypes.Arrays; using MiraAPI.GameOptions; -using MiraAPI.GameOptions.OptionTypes; using MiraAPI.Modifiers; using MiraAPI.Roles; using MiraAPI.Translation; @@ -20,7 +19,6 @@ using Rewired; using TMPro; using UnityEngine; using UnityEngine.UI; -using MethodBase = System.Reflection.MethodBase; using Object = UnityEngine.Object; using Random = UnityEngine.Random; @@ -34,7 +32,6 @@ internal readonly record struct AdvancedSettingsRow(string Title, string Value, public static class Helpers { internal const float MatchInfoGuideBaseScale = 1.3f; - private static readonly Regex TrailingZeroRegex = new(@"\.0+(?!\d)", RegexOptions.Compiled, TimeSpan.FromMilliseconds(100)); private static NumberOption? _advancedSettingsRowPrefab; /// @@ -57,111 +54,54 @@ public static class Helpers public static string GetOptionsText(Type classType) { var group = ModdedOptionsManager.Groups.FirstOrDefault(x => x.OptionableType == classType); - var options = group?.Children; + + if (group == null) + { + return string.Empty; + } + + var headerAdded = false; + var options = group.Children; + var builder = new StringBuilder(); var summaryProvider = group as ISummarizedOptions; var hiddenKeys = summaryProvider?.WikiHiddenOptionKeys; - if (options == null) + void EnsureHeader() { - return string.Empty; + if (headerAdded) + return; + + builder.AppendLine(MiraApiPlugin.Culture, $"\n \n{MiraLocaleManager.Get("Options")}"); + headerAdded = true; + } + + if (summaryProvider != null) + { + foreach (var line in summaryProvider.GetWikiOptionSummaryLines()) + { + if (string.IsNullOrWhiteSpace(line)) + continue; + + EnsureHeader(); + builder.AppendLine(line); + } } - var lines = new List(); - var insertedSummary = false; foreach (var option in options) { - StringNames? key = option switch - { - ModdedToggleOption t => t.StringName, - ModdedEnumOption e => e.StringName, - ModdedNumberOption n => n.StringName, - _ => null, - }; + var key = option.StringName; - if (!insertedSummary && summaryProvider != null && hiddenKeys != null && - key.HasValue && hiddenKeys.Contains(key.Value)) - { - foreach (var line in summaryProvider.GetWikiOptionSummaryLines()) - { - if (!string.IsNullOrWhiteSpace(line)) - { - lines.Add(line); - } - } - - insertedSummary = true; - } - - if (!option.Visible()) - { + if (hiddenKeys?.Contains(key) == true || !option.Visible()) continue; - } - if (key.HasValue && hiddenKeys?.Contains(key.Value) == true) - { - continue; - } + EnsureHeader(); - switch (option) - { - case ModdedToggleOption toggleOption: - lines.Add( - TranslationController.Instance.GetString(toggleOption.StringName) + ": " + - toggleOption.Value); - break; - /*case ModdedMultiSelectOption enumOption: - if (!enumOption.Visible()) - { - continue; - } - - builder.AppendLine(enumOption.Title + ": " + enumOption.Values[enumOption.Value]); - break;*/ - case ModdedEnumOption enumOption: - lines.Add( - TranslationController.Instance.GetString(enumOption.StringName) + ": " + - MiraLocaleManager.Get( - enumOption.Values[enumOption.Value], - enumOption.Values[enumOption.Value])); - break; - case ModdedNumberOption numberOption: - var optionStr = TrailingZeroRegex.Replace( - numberOption.Data.GetValueString(numberOption.Value), - string.Empty); - - var title = TranslationController.Instance.GetString(numberOption.StringName); - if (numberOption is { NegativeWordValue: not "#", Value: -1 }) - { - lines.Add(title + $": {numberOption.NegativeWordValue}"); - } - else if (numberOption is { ZeroWordValue: not "#", Value: 0 }) - { - lines.Add(title + $": {numberOption.ZeroWordValue}"); - } - else - { - lines.Add(title + ": " + optionStr); - } - - break; - } + builder.Append(TranslationController.Instance.GetString(key)) + .Append(": ") + .AppendLine(option.GetDisplayValue()); } - if (lines.Count == 0) - { - return string.Empty; - } - - var builder = new StringBuilder(); - builder.AppendLine( - MiraApiPlugin.Culture, - $"\n \n{MiraLocaleManager.Get("Options")}"); - foreach (var line in lines) - { - builder.AppendLine(line); - } - - return builder.ToString(); + return builder.Length == 0 ? string.Empty : builder.ToString(); } ///