mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-11-19 13:17:10 +00:00
make config ranges safe + more config descriptions
This commit is contained in:
4 files changed
+94
-8
No files matched your search
@@ -315,7 +315,7 @@ public class ModdedNumberOption : ModdedOption<float>
|
||||
/// <inheritdoc />
|
||||
protected override ConfigDescription? CreateDescription()
|
||||
{
|
||||
return new(string.Empty, new AcceptableValueRange<float>(Min, Max));
|
||||
return new(string.Empty, new SafeAcceptableValueRange<float>(Min, Max));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -195,19 +195,22 @@ public class MiraApiSettings(ConfigFile config) : LocalSettingsTab(config)
|
||||
/// Gets the scale of the ability buttons.
|
||||
/// </summary>
|
||||
[LocalSliderSetting(min: 0.5f, max: 1.5f, suffixType: MiraNumberSuffixes.Multiplier, formatString: "0.00", displayValue: true)]
|
||||
public ConfigEntry<float> ButtonUIFactorSlider { get; } = config.Bind("MiraApi.VisualsUi", "MiraApi.ButtonScaleFactor", 0.75f);
|
||||
public ConfigEntry<float> ButtonUIFactorSlider { get; } =
|
||||
config.Bind("MiraApi.VisualsUi", "MiraApi.ButtonScaleFactor", 0.75f, new ConfigDescription(string.Empty, new SafeAcceptableValueRange<float>(0.5f, 1.5f)));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the scale of the UI buttons.
|
||||
/// </summary>
|
||||
[LocalSliderSetting(min: 0.3f, max: 2f, suffixType: MiraNumberSuffixes.Multiplier, formatString: "0.00", displayValue: true)]
|
||||
public ConfigEntry<float> TopRightButtonsFactorSlider { get; } = config.Bind("MiraApi.VisualsUi", "MiraApi.TopRightButtonScaleFactor", 1f);
|
||||
public ConfigEntry<float> TopRightButtonsFactorSlider { get; } =
|
||||
config.Bind("MiraApi.VisualsUi", "MiraApi.TopRightButtonScaleFactor", 1f, new ConfigDescription(string.Empty, new SafeAcceptableValueRange<float>(0.3f, 2f)));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the fps specified by the player.
|
||||
/// </summary>
|
||||
[LocalSliderSetting(min: 60f, max: 240f, suffixType: MiraNumberSuffixes.None, formatString: "0", displayValue: true, roundValue: true)]
|
||||
public ConfigEntry<float> SetFpsSlider { get; } = config.Bind("MiraApi.VisualsUi", "Max FPS", 120f);
|
||||
public ConfigEntry<float> SetFpsSlider { get; } =
|
||||
config.Bind("MiraApi.VisualsUi", "MiraApi.MaxFps", 120f, new ConfigDescription(string.Empty, new SafeAcceptableValueRange<float>(60f, 240f)));
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
[LocalSliderSetting(min: 0.5f, max: 1f, suffixType: MiraNumberSuffixes.Multiplier, formatString: "0.00", displayValue: true)]
|
||||
public ConfigEntry<float> MatchInfoGuideScaleFactor { get; } = config.Bind("MiraApi.MatchInfoGuide", "MiraApi.MatchInfoGuideScaleFactor", 1f);
|
||||
public ConfigEntry<float> MatchInfoGuideScaleFactor { get; } =
|
||||
config.Bind("MiraApi.MatchInfoGuide", "MiraApi.MatchInfoGuideScaleFactor", 1f, new ConfigDescription(string.Empty, new SafeAcceptableValueRange<float>(0.5f, 1f)));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the flag indicating whether the wiki is on the bottom row.
|
||||
|
||||
@@ -186,11 +186,11 @@ public interface ICustomRole : IOptionable
|
||||
/// <summary>
|
||||
/// Binds the configuration options for this role to the provided ConfigFile.
|
||||
/// </summary>
|
||||
/// <param name="config">The ConfigFile to bind the options to.</param>
|
||||
/// <param name="config">The <see cref="ConfigFile"/> to bind the options to.</param>
|
||||
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<int>(0, Configuration.MaxRoleCount)));
|
||||
config.Bind(ChanceConfigDefinition, Configuration.DefaultChance, new ConfigDescription(string.Empty, new SafeAcceptableValueRange<int>(0, 100)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using BepInEx.Configuration;
|
||||
|
||||
namespace MiraAPI.Utilities;
|
||||
|
||||
/// <summary>
|
||||
/// Specify the range of acceptable values for a setting.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the value.</typeparam>
|
||||
public sealed class SafeAcceptableValueRange<T> : AcceptableValueBase
|
||||
where T : IComparable<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the lowest acceptable value.
|
||||
/// </summary>
|
||||
public T Min { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the highest acceptable value.
|
||||
/// </summary>
|
||||
public T Max { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SafeAcceptableValueRange{T}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="min">Lowest acceptable value.</param>
|
||||
/// <param name="max">Highest acceptable value.</param>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override bool IsValid(object value)
|
||||
{
|
||||
if (value is not T val)
|
||||
return false;
|
||||
|
||||
return val.CompareTo(Min) >= 0 && val.CompareTo(Max) <= 0;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string ToDescriptionString()
|
||||
{
|
||||
return $"# Acceptable value range: From {Min} to {Max}";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user