From 5885d211f5a2e9377029c76bea86ccd7e783a76b Mon Sep 17 00:00:00 2001 From: Az <56699741+AlchlcDvl@users.noreply.github.com> Date: Wed, 18 Nov 2026 14:06:04 -0500 Subject: [PATCH] make config ranges safe + more config descriptions --- .../OptionTypes/ModdedNumberOption.cs | 2 +- MiraAPI/MiraApiSettings.cs | 12 ++- MiraAPI/Roles/ICustomRole.cs | 6 +- MiraAPI/Utilities/SafeAcceptableValueRange.cs | 82 +++++++++++++++++++ 4 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 MiraAPI/Utilities/SafeAcceptableValueRange.cs diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedNumberOption.cs b/MiraAPI/GameOptions/OptionTypes/ModdedNumberOption.cs index 2361eed7..f48ee1be 100644 --- a/MiraAPI/GameOptions/OptionTypes/ModdedNumberOption.cs +++ b/MiraAPI/GameOptions/OptionTypes/ModdedNumberOption.cs @@ -315,7 +315,7 @@ public class ModdedNumberOption : ModdedOption /// protected override ConfigDescription? CreateDescription() { - return new(string.Empty, new AcceptableValueRange(Min, Max)); + return new(string.Empty, new SafeAcceptableValueRange(Min, Max)); } /// diff --git a/MiraAPI/MiraApiSettings.cs b/MiraAPI/MiraApiSettings.cs index c6dcb0b3..70348ee6 100644 --- a/MiraAPI/MiraApiSettings.cs +++ b/MiraAPI/MiraApiSettings.cs @@ -195,19 +195,22 @@ public class MiraApiSettings(ConfigFile config) : LocalSettingsTab(config) /// Gets the scale of the ability buttons. /// [LocalSliderSetting(min: 0.5f, max: 1.5f, suffixType: MiraNumberSuffixes.Multiplier, formatString: "0.00", displayValue: true)] - public ConfigEntry ButtonUIFactorSlider { get; } = config.Bind("MiraApi.VisualsUi", "MiraApi.ButtonScaleFactor", 0.75f); + public ConfigEntry ButtonUIFactorSlider { get; } = + config.Bind("MiraApi.VisualsUi", "MiraApi.ButtonScaleFactor", 0.75f, new ConfigDescription(string.Empty, new SafeAcceptableValueRange(0.5f, 1.5f))); /// /// Gets the scale of the UI buttons. /// [LocalSliderSetting(min: 0.3f, max: 2f, suffixType: MiraNumberSuffixes.Multiplier, formatString: "0.00", displayValue: true)] - public ConfigEntry TopRightButtonsFactorSlider { get; } = config.Bind("MiraApi.VisualsUi", "MiraApi.TopRightButtonScaleFactor", 1f); + public ConfigEntry TopRightButtonsFactorSlider { get; } = + config.Bind("MiraApi.VisualsUi", "MiraApi.TopRightButtonScaleFactor", 1f, new ConfigDescription(string.Empty, new SafeAcceptableValueRange(0.3f, 2f))); /// /// Gets the fps specified by the player. /// [LocalSliderSetting(min: 60f, max: 240f, suffixType: MiraNumberSuffixes.None, formatString: "0", displayValue: true, roundValue: true)] - public ConfigEntry SetFpsSlider { get; } = config.Bind("MiraApi.VisualsUi", "Max FPS", 120f); + public ConfigEntry SetFpsSlider { get; } = + config.Bind("MiraApi.VisualsUi", "MiraApi.MaxFps", 120f, new ConfigDescription(string.Empty, new SafeAcceptableValueRange(60f, 240f))); /// /// Gets whether the modifiers hud should be on the left side of the screen (under roles/task tab). Recommended for streamers. @@ -225,7 +228,8 @@ public class MiraApiSettings(ConfigFile config) : LocalSettingsTab(config) /// Gets the scale of the match info guide. /// [LocalSliderSetting(min: 0.5f, max: 1f, suffixType: MiraNumberSuffixes.Multiplier, formatString: "0.00", displayValue: true)] - public ConfigEntry MatchInfoGuideScaleFactor { get; } = config.Bind("MiraApi.MatchInfoGuide", "MiraApi.MatchInfoGuideScaleFactor", 1f); + public ConfigEntry MatchInfoGuideScaleFactor { get; } = + config.Bind("MiraApi.MatchInfoGuide", "MiraApi.MatchInfoGuideScaleFactor", 1f, new ConfigDescription(string.Empty, new SafeAcceptableValueRange(0.5f, 1f))); /// /// Gets the flag indicating whether the wiki is on the bottom row. diff --git a/MiraAPI/Roles/ICustomRole.cs b/MiraAPI/Roles/ICustomRole.cs index ba5c896a..51ed457f 100644 --- a/MiraAPI/Roles/ICustomRole.cs +++ b/MiraAPI/Roles/ICustomRole.cs @@ -186,11 +186,11 @@ public interface ICustomRole : IOptionable /// /// Binds the configuration options for this role to the provided ConfigFile. /// - /// The ConfigFile to bind the options to. + /// The to bind the options to. void BindConfig(ConfigFile config) { - config.Bind(NumConfigDefinition, Configuration.DefaultRoleCount); - config.Bind(ChanceConfigDefinition, Configuration.DefaultChance); + config.Bind(NumConfigDefinition, Configuration.DefaultRoleCount, new ConfigDescription(string.Empty, new SafeAcceptableValueRange(0, Configuration.MaxRoleCount))); + config.Bind(ChanceConfigDefinition, Configuration.DefaultChance, new ConfigDescription(string.Empty, new SafeAcceptableValueRange(0, 100))); } /// diff --git a/MiraAPI/Utilities/SafeAcceptableValueRange.cs b/MiraAPI/Utilities/SafeAcceptableValueRange.cs new file mode 100644 index 00000000..ab80de2a --- /dev/null +++ b/MiraAPI/Utilities/SafeAcceptableValueRange.cs @@ -0,0 +1,82 @@ +using System; +using BepInEx.Configuration; + +namespace MiraAPI.Utilities; + +/// +/// Specify the range of acceptable values for a setting. +/// +/// The type of the value. +public sealed class SafeAcceptableValueRange : AcceptableValueBase + where T : IComparable +{ + /// + /// Gets the lowest acceptable value. + /// + public T Min { get; } + + /// + /// Gets the highest acceptable value. + /// + public T Max { get; } + + /// + /// Initializes a new instance of the class. + /// + /// Lowest acceptable value. + /// Highest acceptable value. + public SafeAcceptableValueRange(T min, T max) + : base(typeof(T)) + { + if (min is null) + { + throw new ArgumentNullException(nameof(min)); + } + + if (max is null) + { + throw new ArgumentNullException(nameof(max)); + } + + if (min.CompareTo(max) > 0) + { + Min = max; + Max = min; + } + else + { + Min = min; + Max = max; + } + } + + /// + public override object Clamp(object value) + { + if (value is not T val) + return Min; + + if (val.CompareTo(Min) <= 0) + return Min; + + if (val.CompareTo(Max) >= 0) + return Max; + + return val; + } + + /// + public override bool IsValid(object value) + { + if (value is not T val) + return false; + + return val.CompareTo(Min) >= 0 && val.CompareTo(Max) <= 0; + } + + /// + public override string ToDescriptionString() + { + return $"# Acceptable value range: From {Min} to {Max}"; + } +}