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
This commit is contained in:
Az committed 2026-10-26 04:47:07 -04:00
1 parent 936da28f6a
commit accfd04209
18 files changed
+131 -236

No files matched your search

+1 -1
View File
@@ -44,7 +44,7 @@ public abstract class AbstractOptionGroup
/// Gets a value indicating whether the group should be shown in the modifiers menu.
/// </summary>
// 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;
@@ -11,7 +11,7 @@ namespace MiraAPI.GameOptions.Attributes;
/// <param name="enumType">The <see cref="Enum"/> type.</param>
/// <param name="values">An option list of string values to use in place of the <see langword="enum"/> name.</param>
[AttributeUsage(AttributeTargets.Property)]
[Obsolete("Use ModdedEnumOptionAttribute<T> instead.")]
[Obsolete("Use ModdedEnumOptionAttribute<T> instead. This will be removed in a future version.")]
public class ModdedEnumOptionAttribute(string title, Type enumType, string[]? values = null)
: ModdedOptionAttribute(title)
{
@@ -12,7 +12,7 @@ namespace MiraAPI.GameOptions.Attributes;
/// <param name="enumType">The <see cref="Enum"/> type.</param>
/// <param name="values">An option list of string values to use in place of the <see langword="enum"/> name.</param>
[AttributeUsage(AttributeTargets.Property)]
[Obsolete("Use ModdedEnumOptionListAttribute<T> instead.")]
[Obsolete("Use ModdedEnumOptionListAttribute<T> instead. This will be removed in a future version.")]
public class ModdedEnumOptionListAttribute(string title, Type enumType, string[]? values = null)
: ModdedOptionListAttribute(title)
{
+6
View File
@@ -98,6 +98,12 @@ public interface IModdedOption
/// <returns>The created <see cref="global::OptionBehaviour"/> object.</returns>
OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOption numberOpt, StringOption stringOpt, PlayerOption playerOpt, Transform container);
/// <summary>
/// Gets the translated display value of the toggle option.
/// </summary>
/// <returns>The display value.</returns>
string GetDisplayValue();
/// <summary>
/// Gets the value as a <see langword="float"/>.
/// </summary>
+2 -2
View File
@@ -443,7 +443,7 @@ public static class ModdedOptionsManager
/// <param name="value">The new object value.</param>
[SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Harmony naming convention.")]
[SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "Read above.")]
[Obsolete("Use PropertySetterPatch<T> instead.")]
[Obsolete("Use PropertySetterPatch<T> 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
/// <returns><see langword="false"/> so the original getter gets skipped.</returns>
[SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Harmony naming convention")]
[SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "Read above.")]
[Obsolete("Use PropertyGetterPatch<T> instead.")]
[Obsolete("Use PropertyGetterPatch<T> 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))
@@ -13,7 +13,7 @@ namespace MiraAPI.GameOptions.OptionTypes;
/// <summary>
/// An option for selecting an <see langword="enum"/> value.
/// </summary>
[Obsolete("Use ModdedEnum<T> instead.")]
[Obsolete("Use ModdedEnumOption<T> instead. This will be removed in a future version.")]
public class ModdedEnumOption : ModdedOption<int>
{
/// <summary>
@@ -94,23 +94,7 @@ public class ModdedEnumOption : ModdedOption<int>
/// <inheritdoc />
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<T> : ModdedOption<T>
/// <inheritdoc />
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);
}
@@ -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<float>
/// </summary>
public bool ShiftIncrement { get; }
private static readonly Regex TrailingZeroRegex = new(@"\.0+(?!\d)", RegexOptions.Compiled, TimeSpan.FromMilliseconds(100));
/// <summary>
/// Initializes a new instance of the <see cref="ModdedNumberOption"/> class.
/// </summary>
@@ -155,7 +158,7 @@ public class ModdedNumberOption : ModdedOption<float>
optionComponent.ZeroValue = ZeroWordValue;
numberOption.SetUpFromData(Data, 20);
numberOption.OnValueChanged = (Il2CppSystem.Action<OptionBehaviour>)ValueChanged;
numberOption.OnValueChanged = (Action<OptionBehaviour>)ValueChanged;
numberOption.Title = StringName;
numberOption.Value = Value;
@@ -171,6 +174,22 @@ public class ModdedNumberOption : ModdedOption<float>
return numberOption;
}
/// <inheritdoc />
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);
}
/// <inheritdoc />
public override float GetFloatData()
{
@@ -198,24 +217,18 @@ public class ModdedNumberOption : ModdedOption<float>
/// <inheritdoc />
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;
}
}
/// <inheritdoc />
protected override float ModifyValue(float newValue, float oldValue)
{
return Math.Clamp(newValue, Min, Max);
}
/// <summary>
/// Implicitly converts the option to an <see langword="int"/>.
/// </summary>
@@ -223,6 +236,6 @@ public class ModdedNumberOption : ModdedOption<float>
/// <returns>Integer value.</returns>
public static implicit operator int(ModdedNumberOption option)
{
return (int)option.Value;
return (int)Math.Round(option.Value);
}
}
@@ -50,6 +50,8 @@ public abstract class ModdedOption<T> : 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<T> : IModdedOption
Rpc<SyncOptionsRpc>.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<T> : IModdedOption
/// <inheritdoc />
public abstract void HandleNetData(byte[] data);
/// <inheritdoc />
public virtual string GetDisplayValue()
{
return Data.GetValueString(GetFloatData());
}
/// <summary>
/// Handles the value changed event.
/// </summary>
/// <param name="newValue">The new value.</param>
protected abstract void OnValueChanged(T newValue);
/// <summary>
/// Modifies the incoming value with respect to the previous value. This is invoked before the <see cref="ModdedOption{T}.Setter"/> (if it's set).
/// </summary>
/// <param name="newValue">The new value.</param>
/// <param name="oldValue">The old value.</param>
/// <returns>The modified value.</returns>
protected virtual T ModifyValue(T newValue, T oldValue)
{
return newValue;
}
/// <summary>
/// Gets the value from the option behaviour.
/// </summary>
@@ -138,19 +138,7 @@ public class ModdedPlayerOption : ModdedOption<int>
/// <inheritdoc />
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;
}
@@ -168,13 +168,7 @@ public class ModdedStringOption : ModdedOption<string>
/// <inheritdoc />
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);
}
@@ -72,6 +72,18 @@ public class ModdedToggleOption : ModdedOption<bool>
return toggleOption;
}
/// <inheritdoc />
public override string GetDisplayValue()
{
return Value
? (OnValue == "On"
? TranslationController.Instance.GetString(StringNames.SettingsOn)
: OnValue.Translate())
: (OffValue == "Off"
? TranslationController.Instance.GetString(StringNames.SettingsOff)
: OffValue.Translate());
}
/// <inheritdoc />
public override float GetFloatData()
{
@@ -99,23 +111,7 @@ public class ModdedToggleOption : ModdedOption<bool>
/// <inheritdoc />
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;
}
+1 -1
View File
@@ -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;
-2
View File
@@ -71,8 +71,6 @@ Outlive the timer or eliminate the crew.</string>
<string name="MiraApi.Wiki.PreviousPage">Previous</string>
<string name="MiraApi.Wiki.NextPage">Next</string>
<string name="MiraApi.Wiki.NoResultsFound">No Results Found</string>
<string name="On">On</string>
<string name="Off">Off</string>
<string name="Enabled">Enabled</string>
<string name="Disabled">Disabled</string>
<string name="Active">Active</string>
+2 -2
View File
@@ -126,7 +126,7 @@ public static class MiraLocaleManager
/// Call once per mod during plugin Load().
/// </summary>
/// <param name="modGuid">The mod GUID (e.g., "mira.example").</param>
[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
/// </summary>
/// <param name="modGuid">The mod GUID (e.g., "mira.example").</param>
/// <param name="internalName">The mod's root namespace (e.g., "mira.api").</param>
[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();
@@ -27,7 +27,7 @@ public class LoadableAudioResourceAsset : LoadableAsset<AudioClip>
/// Initializes a new instance of the <see cref="LoadableAudioResourceAsset"/> class with the provided resource path.
/// </summary>
/// <param name="path">The path of the wave file.</param>
[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;
@@ -31,7 +31,7 @@ public class LoadableResourceAsset : LoadableAsset<Sprite>
/// </summary>
/// <param name="path">The path to the embedded resource.</param>
/// <param name="pixelsPerUnit">The pixels per unit for the loaded sprite.</param>
[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;
+4 -42
View File
@@ -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<GuideOptionPage> BuildPages(Func<bool, string> formatBool)
public static List<GuideOptionPage> BuildPages()
{
var pages = new List<GuideOptionPage>();
var combinedPages = new Dictionary<string, GuideOptionPage>(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<string> modSearchTerms,
Func<bool, string> formatBool)
IReadOnlyList<string> 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<bool, string> 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());
}
}
}
+35 -95
View File
@@ -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;
/// <summary>
@@ -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<size=50%> \n</size><b><color=#29D7B5>{MiraLocaleManager.Get("Options")}</color></b>");
headerAdded = true;
}
if (summaryProvider != null)
{
foreach (var line in summaryProvider.GetWikiOptionSummaryLines())
{
if (string.IsNullOrWhiteSpace(line))
continue;
EnsureHeader();
builder.AppendLine(line);
}
}
var lines = new List<string>();
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<Enum> 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<size=50%> \n</size><b><color=#29D7B5>{MiraLocaleManager.Get("Options")}</color></b>");
foreach (var line in lines)
{
builder.AppendLine(line);
}
return builder.ToString();
return builder.Length == 0 ? string.Empty : builder.ToString();
}
/// <summary>