mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-11-19 13:17:10 +00:00
add config descriptions for options
This commit is contained in:
6 files changed
+103
-2
No files matched your search
@@ -83,7 +83,7 @@ public abstract class ModdedOptionListAttribute(string? title = null) : Property
|
||||
/// <returns>The formatted option's title.</returns>
|
||||
protected string GetFormattedTitle(int index, PropertyInfo property)
|
||||
{
|
||||
var baseTitle = Title ?? property.Name;
|
||||
var baseTitle = Title ??= property.Name;
|
||||
return Titles[index] = GetFormattedTitle(baseTitle, index, FormatParams.Match(baseTitle));
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using BepInEx.Configuration;
|
||||
using MiraAPI.Networking;
|
||||
using MiraAPI.Translation;
|
||||
using MiraAPI.Utilities;
|
||||
|
||||
namespace MiraAPI.GameOptions.OptionTypes;
|
||||
|
||||
@@ -107,6 +109,12 @@ public class ModdedEnumOption : ModdedBaseEnumOption<int>
|
||||
{
|
||||
SetValue(BitConverter.ToInt32(data));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected internal override ConfigDescription? CreateDescription()
|
||||
{
|
||||
return new(string.Empty, new AcceptableValueList<int>(UnderlyingValues));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -187,4 +195,10 @@ public class ModdedEnumOption<T> : ModdedBaseEnumOption<T>
|
||||
{
|
||||
SetValue(Unsafe.ReadUnaligned<T>(ref data[0]));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected internal override ConfigDescription? CreateDescription()
|
||||
{
|
||||
return new(string.Empty, new AcceptableEnumValues<T>(UnderlyingValues));
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
using AmongUs.GameOptions;
|
||||
using BepInEx.Configuration;
|
||||
using MiraAPI.Networking;
|
||||
using MiraAPI.Utilities;
|
||||
using UnityEngine;
|
||||
@@ -294,6 +295,12 @@ public class ModdedNumberOption : ModdedOption<float>
|
||||
return Math.Clamp(newValue, Min, Max);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected internal override ConfigDescription? CreateDescription()
|
||||
{
|
||||
return new(string.Empty, new AcceptableValueRange<float>(Min, Max));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicitly converts the option to an <see langword="int"/>.
|
||||
/// </summary>
|
||||
|
||||
@@ -92,7 +92,7 @@ public abstract class ModdedOption<T>(string groupPrefix, string title, T defaul
|
||||
if (field != null || value == null) return;
|
||||
field = value;
|
||||
|
||||
var entry = field.GetConfigFile().Bind(ConfigDefinition, DefaultValue);
|
||||
var entry = field.GetConfigFile().Bind(ConfigDefinition, DefaultValue, CreateDescription());
|
||||
Value = entry.Value;
|
||||
}
|
||||
}
|
||||
@@ -262,6 +262,13 @@ public abstract class ModdedOption<T>(string groupPrefix, string title, T defaul
|
||||
return newValue;
|
||||
}
|
||||
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member (Justification: Internal code.)
|
||||
protected internal virtual ConfigDescription? CreateDescription()
|
||||
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicitly converts the option to type of <typeparamref name="T"/>.
|
||||
/// </summary>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using BepInEx.Configuration;
|
||||
using MiraAPI.Networking;
|
||||
using MiraAPI.Translation;
|
||||
|
||||
@@ -103,4 +104,10 @@ public class ModdedStringOption : ModdedBaseStringOption<string>
|
||||
{
|
||||
SetValue(Encoding.Unicode.GetString(data));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected internal override ConfigDescription? CreateDescription()
|
||||
{
|
||||
return new(string.Empty, new AcceptableValueList<string>(UnderlyingValues));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using BepInEx.Configuration;
|
||||
|
||||
namespace MiraAPI.Utilities;
|
||||
|
||||
/// <summary>
|
||||
/// Specify the list of acceptable enum values for a setting.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the enum.</typeparam>
|
||||
public sealed class AcceptableEnumValues<T> : AcceptableValueBase
|
||||
where T : struct, Enum
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a list of values that a setting can take.
|
||||
/// </summary>
|
||||
public T[] AcceptableValues { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AcceptableEnumValues{T}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="acceptableValues">The acceptable values.</param>
|
||||
/// <exception cref="ArgumentNullException">Thrown when the given values are null.</exception>
|
||||
/// <exception cref="ArgumentException">Thrown when no values are given.</exception>
|
||||
public AcceptableEnumValues(params T[] acceptableValues)
|
||||
: base(typeof(T))
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(acceptableValues);
|
||||
|
||||
if (acceptableValues.Length == 0)
|
||||
{
|
||||
throw new ArgumentException("At least one acceptable value is needed", nameof(acceptableValues));
|
||||
}
|
||||
|
||||
AcceptableValues = acceptableValues;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override object Clamp(object value)
|
||||
{
|
||||
if (IsValid(value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
return AcceptableValues[0];
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override bool IsValid(object value)
|
||||
{
|
||||
if (value is not T v)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return AcceptableValues.Any(x => EqualityComparer<T>.Default.Equals(x, v));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string ToDescriptionString()
|
||||
{
|
||||
return "# Acceptable values: " + string.Join(", ", AcceptableValues.Select(x => x.ToString()));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user