From 8f6f97bbc073fba89636ed1b6fa8f6c20925dccc Mon Sep 17 00:00:00 2001 From: Az <56699741+AlchlcDvl@users.noreply.github.com> Date: Mon, 2 Nov 2026 17:32:56 -0500 Subject: [PATCH] options translation handling overhaul --- MiraAPI/GameOptions/AbstractOptionGroup.cs | 19 +- .../Attributes/ModdedEnumOptionAttribute.cs | 67 ++++++- .../Attributes/ModdedNumberOptionAttribute.cs | 57 +++++- .../Attributes/ModdedOptionAttribute.cs | 18 +- .../Attributes/ModdedPlayerOptionAttribute.cs | 20 +- .../Attributes/ModdedStringOptionAttribute.cs | 20 +- .../Attributes/ModdedToggleOptionAttribute.cs | 32 +++- .../ModdedEnumOptionListAttribute.cs | 47 ++++- .../ModdedNumberOptionListAttribute.cs | 63 ++++++- .../OptionList/ModdedOptionListAttribute.cs | 28 ++- .../ModdedPlayerOptionListAttribute.cs | 25 ++- .../ModdedStringOptionListAttribute.cs | 22 ++- .../ModdedToggleOptionListAttribute.cs | 31 ++- MiraAPI/GameOptions/ModdedOptionsManager.cs | 13 +- .../OptionTypes/ModdedEnumOption.cs | 176 ++++++++++++++---- .../OptionTypes/ModdedNumberOption.cs | 83 +++++++-- .../GameOptions/OptionTypes/ModdedOption.cs | 112 ++++++----- .../OptionTypes/ModdedOptionList.cs | 6 +- .../OptionTypes/ModdedPlayerOption.cs | 20 +- .../OptionTypes/ModdedStringOption.cs | 20 +- .../OptionTypes/ModdedToggleOption.cs | 36 +++- 21 files changed, 695 insertions(+), 220 deletions(-) diff --git a/MiraAPI/GameOptions/AbstractOptionGroup.cs b/MiraAPI/GameOptions/AbstractOptionGroup.cs index 94d9409b..3a207603 100644 --- a/MiraAPI/GameOptions/AbstractOptionGroup.cs +++ b/MiraAPI/GameOptions/AbstractOptionGroup.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; -using System.Diagnostics.CodeAnalysis; using System.Linq; using Il2CppInterop.Runtime.Attributes; using MiraAPI.Modifiers; @@ -19,16 +18,31 @@ public abstract class AbstractOptionGroup { internal List Options { get; } = []; + private readonly ReadOnlyCollection children; + + /// + /// Initializes a new instance of the class. + /// + protected AbstractOptionGroup() + { + children = new(Options); + } + /// /// Gets a list of the options which are a part of this group. /// - public ReadOnlyCollection Children => new(Options); + public ReadOnlyCollection Children => children; /// /// Gets the name of the group. Visible in options menu. /// public abstract string GroupName { get; } + /// + /// Gets the ID prefix used for options in this group (e.g., "Options.Teleporter"). + /// + public virtual string GroupPrefix => string.Empty; + /// /// Gets the sprite of the group that is displayed in the match info guide. /// @@ -45,7 +59,6 @@ public abstract class AbstractOptionGroup /// // Completed: make this not a boolean [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 8cc34656..d0bc52a0 100644 --- a/MiraAPI/GameOptions/Attributes/ModdedEnumOptionAttribute.cs +++ b/MiraAPI/GameOptions/Attributes/ModdedEnumOptionAttribute.cs @@ -9,15 +9,49 @@ namespace MiraAPI.GameOptions.Attributes; /// /// The title of the option. /// The type. +/// The common translation ID prefix of all enum values. /// An option list of string values to use in place of the name. [AttributeUsage(AttributeTargets.Property)] [Obsolete("Use ModdedEnumOptionAttribute instead. This will be removed in a future version.")] -public class ModdedEnumOptionAttribute(string title, Type enumType, string[]? values = null) +public class ModdedEnumOptionAttribute(string? title, Type enumType, string idPrefix, string[]? values = null) : ModdedOptionAttribute(title) { - internal override IModdedOption CreateOption(object? value, PropertyInfo property) + /// + /// Initializes a new instance of the class. + /// + /// The title of the option. + /// The type. + /// An option list of string values to use in place of the name. + public ModdedEnumOptionAttribute(string? title, Type enumType, string[]? values = null) + : this(title, enumType, string.Empty, values) { - var opt = new ModdedEnumOption(Title, (int)(value ?? 0), enumType, values); + } + + /// + /// Initializes a new instance of the class. + /// + /// The type. + /// The common translation ID prefix of all enum values. + /// An option list of string values to use in place of the name. + public ModdedEnumOptionAttribute(Type enumType, string idPrefix, string[]? values = null) + : this(null, enumType, idPrefix, values) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The type. + /// An option list of string values to use in place of the name. + public ModdedEnumOptionAttribute(Type enumType, string[]? values = null) + : this(null, enumType, string.Empty, values) + { + } + + internal override IModdedOption CreateOption(object? value, PropertyInfo property, AbstractOptionGroup group) + { + var opt = new ModdedEnumOption(group.GroupPrefix, Title ?? property.Name, (int)(value ?? 0), enumType, idPrefix, values); + Title = opt.Title; return opt; } @@ -42,15 +76,36 @@ public class ModdedEnumOptionAttribute(string title, Type enumType, string[]? va /// /// The enum type. /// The title of the option. +/// The common translation ID prefix of all enum values. /// An option list of string values to use in place of the name. [AttributeUsage(AttributeTargets.Property)] -public class ModdedEnumOptionAttribute(string title, string[]? values = null) +public class ModdedEnumOptionAttribute(string? title, string idPrefix, string[]? values = null) : ModdedOptionAttribute(title), ITypedOption where T : struct, Enum { - internal override IModdedOption CreateOption(object? value, PropertyInfo property) + /// + /// Initializes a new instance of the class. + /// + /// The title of the option. + /// An option list of string values to use in place of the name. + public ModdedEnumOptionAttribute(string? title, string[]? values = null) + : this(title, string.Empty, values) { - var opt = new ModdedEnumOption(Title, (T)(value ?? 0), values); + } + + /// + /// Initializes a new instance of the class. + /// + /// An option list of string values to use in place of the name. + public ModdedEnumOptionAttribute(string[]? values = null) + : this(null, string.Empty, values) + { + } + + internal override IModdedOption CreateOption(object? value, PropertyInfo property, AbstractOptionGroup group) + { + var opt = new ModdedEnumOption(group.GroupPrefix, Title ?? property.Name, (T)(value ?? 0), idPrefix, values); + Title = opt.Title; return opt; } diff --git a/MiraAPI/GameOptions/Attributes/ModdedNumberOptionAttribute.cs b/MiraAPI/GameOptions/Attributes/ModdedNumberOptionAttribute.cs index dd52dfa0..5c5d7bcf 100644 --- a/MiraAPI/GameOptions/Attributes/ModdedNumberOptionAttribute.cs +++ b/MiraAPI/GameOptions/Attributes/ModdedNumberOptionAttribute.cs @@ -19,7 +19,7 @@ namespace MiraAPI.GameOptions.Attributes; /// Whether increments can be split in half (using Shift). [AttributeUsage(AttributeTargets.Property)] public class ModdedNumberOptionAttribute( - string title, + string? title, float min, float max, float increment, @@ -30,6 +30,30 @@ public class ModdedNumberOptionAttribute( bool halfIncrements = false) : ModdedOptionAttribute(title), ITypedOption, ITypedOption { + /// + /// Initializes a new instance of the class. + /// + /// The minimum value. + /// The maximum value. + /// The increment. + /// Determines what is shown for zero. + /// Determines what is shown for negative 1. + /// The suffix type. + /// Optional format string for the option screen. + /// Whether increments can be split in half (using Shift). + public ModdedNumberOptionAttribute( + float min, + float max, + float increment, + string zeroBehavior, + string negativeBehavior, + MiraNumberSuffixes suffixType = MiraNumberSuffixes.None, + string? formatString = null, + bool halfIncrements = false) + : this(null, min, max, increment, zeroBehavior, negativeBehavior, suffixType, formatString, halfIncrements) + { + } + /// /// Initializes a new instance of the class. /// @@ -41,7 +65,7 @@ public class ModdedNumberOptionAttribute( /// Whether zero is infinity or not. /// Optional format string for the option screen. public ModdedNumberOptionAttribute( - string title, + string? title, float min, float max, float increment = 1, @@ -52,7 +76,27 @@ public class ModdedNumberOptionAttribute( { } - internal override IModdedOption CreateOption(object? value, PropertyInfo property) + /// + /// Initializes a new instance of the class. + /// + /// The minimum value. + /// The maximum value. + /// The increment. + /// The suffix type. + /// Whether zero is infinity or not. + /// Optional format string for the option screen. + public ModdedNumberOptionAttribute( + float min, + float max, + float increment = 1, + MiraNumberSuffixes suffixType = MiraNumberSuffixes.None, + string? formatString = null, + bool zeroInfinity = false) + : this(null, min, max, increment, suffixType, formatString, zeroInfinity) + { + } + + internal override IModdedOption CreateOption(object? value, PropertyInfo property, AbstractOptionGroup group) { var final = value switch { @@ -61,8 +105,9 @@ public class ModdedNumberOptionAttribute( _ => (float)value, }; - return new ModdedNumberOption( - Title, + var opt = new ModdedNumberOption( + group.GroupPrefix, + Title ?? property.Name, final, min, max, @@ -72,6 +117,8 @@ public class ModdedNumberOptionAttribute( suffixType, formatString, halfIncrements); + Title = opt.Title; + return opt; } /// diff --git a/MiraAPI/GameOptions/Attributes/ModdedOptionAttribute.cs b/MiraAPI/GameOptions/Attributes/ModdedOptionAttribute.cs index 91b78568..0287a524 100644 --- a/MiraAPI/GameOptions/Attributes/ModdedOptionAttribute.cs +++ b/MiraAPI/GameOptions/Attributes/ModdedOptionAttribute.cs @@ -10,25 +10,25 @@ namespace MiraAPI.GameOptions.Attributes; /// Optional parameter to specify a role Type. /// Optional parameter to specify a game mode Type. [AttributeUsage(AttributeTargets.Property)] -public abstract class ModdedOptionAttribute(string title, Type? roleType = null, Type? modeType = null) : PropertyOptionAttribute +public abstract class ModdedOptionAttribute(string? title = null, Type? roleType = null, Type? modeType = null) : PropertyOptionAttribute { internal IModdedOption? HolderOption { get; set; } internal Type ValueType { get; set; } - /// - /// Gets the title of the option. - /// - public string Title => title; - /// /// Gets the role type of the option. /// - protected Type? RoleType { get; private set; } = roleType; + protected Type? RoleType { get; } = roleType; /// /// Gets the game mode type of the option. /// - protected Type? ModeType { get; private set; } = modeType; + protected Type? ModeType { get; } = modeType; - internal abstract IModdedOption? CreateOption(object? value, PropertyInfo property); + /// + /// Gets or sets the title of the option. + /// + public string? Title { get; protected set; } = title; + + internal abstract IModdedOption? CreateOption(object? value, PropertyInfo property, AbstractOptionGroup group); } diff --git a/MiraAPI/GameOptions/Attributes/ModdedPlayerOptionAttribute.cs b/MiraAPI/GameOptions/Attributes/ModdedPlayerOptionAttribute.cs index b4d296e3..93bd78f2 100644 --- a/MiraAPI/GameOptions/Attributes/ModdedPlayerOptionAttribute.cs +++ b/MiraAPI/GameOptions/Attributes/ModdedPlayerOptionAttribute.cs @@ -10,15 +10,23 @@ namespace MiraAPI.GameOptions.Attributes; /// The title of the option. /// Whether the option includes a none option. If None is selected, value will return -1. [AttributeUsage(AttributeTargets.Property)] -public class ModdedPlayerOptionAttribute( - string title, - bool allowNone = true) +public class ModdedPlayerOptionAttribute(string? title, bool allowNone = true) : ModdedOptionAttribute(title), ITypedOption, ITypedOption { - internal override IModdedOption? CreateOption(object? value, PropertyInfo property) + /// + /// Initializes a new instance of the class. + /// + /// Whether the option includes a none option. If None is selected, value will return -1. + public ModdedPlayerOptionAttribute(bool allowNone = true) + : this(null, allowNone) { - var option = new ModdedPlayerOption(Title, allowNone); - return option; + } + + internal override IModdedOption? CreateOption(object? value, PropertyInfo property, AbstractOptionGroup group) + { + var opt = new ModdedPlayerOption(group.GroupPrefix, Title ?? property.Name, allowNone); + Title = opt.Title; + return opt; } /// diff --git a/MiraAPI/GameOptions/Attributes/ModdedStringOptionAttribute.cs b/MiraAPI/GameOptions/Attributes/ModdedStringOptionAttribute.cs index 379c820f..5c9074e8 100644 --- a/MiraAPI/GameOptions/Attributes/ModdedStringOptionAttribute.cs +++ b/MiraAPI/GameOptions/Attributes/ModdedStringOptionAttribute.cs @@ -10,15 +10,23 @@ namespace MiraAPI.GameOptions.Attributes; /// The title of the option. /// An option list of string value. [AttributeUsage(AttributeTargets.Property)] -public class ModdedStringOptionAttribute( - string title, - string[] values) +public class ModdedStringOptionAttribute(string? title, string[] values) : ModdedOptionAttribute(title), ITypedOption { - internal override IModdedOption? CreateOption(object? value, PropertyInfo property) + /// + /// Initializes a new instance of the class. + /// + /// An option list of string value. + public ModdedStringOptionAttribute(string[] values) + : this(null, values) { - var option = new ModdedStringOption(Title, (string)(value ?? string.Empty), values); - return option; + } + + internal override IModdedOption? CreateOption(object? value, PropertyInfo property, AbstractOptionGroup group) + { + var opt = new ModdedStringOption(group.GroupPrefix, Title ?? property.Name, value as string ?? string.Empty, values); + Title = opt.Title; + return opt; } /// diff --git a/MiraAPI/GameOptions/Attributes/ModdedToggleOptionAttribute.cs b/MiraAPI/GameOptions/Attributes/ModdedToggleOptionAttribute.cs index e94d8e49..ea41e2d0 100644 --- a/MiraAPI/GameOptions/Attributes/ModdedToggleOptionAttribute.cs +++ b/MiraAPI/GameOptions/Attributes/ModdedToggleOptionAttribute.cs @@ -11,25 +11,41 @@ namespace MiraAPI.GameOptions.Attributes; /// What the option displays when toggled on. /// What the option displays when toggled off. [AttributeUsage(AttributeTargets.Property)] -public class ModdedToggleOptionAttribute( - string title, - string on, - string off) +public class ModdedToggleOptionAttribute(string? title, string on, string off) : ModdedOptionAttribute(title), ITypedOption { + /// + /// Initializes a new instance of the class. + /// + /// What the option displays when toggled on. + /// What the option displays when toggled off. + public ModdedToggleOptionAttribute(string on, string off) + : this(null, on, off) + { + } + /// /// Initializes a new instance of the class. /// /// The option title. - public ModdedToggleOptionAttribute(string title) + public ModdedToggleOptionAttribute(string? title) : this(title, "On", "Off") { } - internal override IModdedOption CreateOption(object? value, PropertyInfo property) + /// + /// Initializes a new instance of the class. + /// + public ModdedToggleOptionAttribute() + : this(null, "On", "Off") { - var toggleOpt = new ModdedToggleOption(Title, (bool)(value ?? false), on, off); - return toggleOpt; + } + + internal override IModdedOption CreateOption(object? value, PropertyInfo property, AbstractOptionGroup group) + { + var opt = new ModdedToggleOption(group.GroupPrefix, Title ?? property.Name, (bool)(value ?? false), on, off); + Title = opt.Title; + return opt; } /// diff --git a/MiraAPI/GameOptions/Attributes/OptionList/ModdedEnumOptionListAttribute.cs b/MiraAPI/GameOptions/Attributes/OptionList/ModdedEnumOptionListAttribute.cs index 7fe03f6f..5eff07a4 100644 --- a/MiraAPI/GameOptions/Attributes/OptionList/ModdedEnumOptionListAttribute.cs +++ b/MiraAPI/GameOptions/Attributes/OptionList/ModdedEnumOptionListAttribute.cs @@ -13,13 +13,23 @@ namespace MiraAPI.GameOptions.Attributes; /// An option list of string values to use in place of the name. [AttributeUsage(AttributeTargets.Property)] [Obsolete("Use ModdedEnumOptionListAttribute instead. This will be removed in a future version.")] -public class ModdedEnumOptionListAttribute(string title, Type enumType, string[]? values = null) +public class ModdedEnumOptionListAttribute(string? title, Type enumType, string[]? values = null) : ModdedOptionListAttribute(title) { - internal override IModdedOptionList CreateOptionList(IList value, PropertyInfo property) + /// + /// Initializes a new instance of the class. + /// + /// The type. + /// An option list of string values to use in place of the name. + public ModdedEnumOptionListAttribute(Type enumType, string[]? values = null) + : this(null, enumType, values) + { + } + + internal override IModdedOptionList CreateOptionList(IList list, PropertyInfo property, AbstractOptionGroup group) { var optList = new ModdedOptionList( - value.Count, idx => new(GetFormattedTitle(idx), (int)(value[idx] ?? 0), enumType, values)); + list.Count, idx => new(group.GroupPrefix, GetFormattedTitle(idx, property), (int)(list[idx] ?? 0), enumType, values)); return optList; } @@ -35,7 +45,7 @@ public class ModdedEnumOptionListAttribute(string title, Type enumType, string[] { return HolderOptionList?[idx] is ModdedEnumOption opt ? Enum.ToObject(enumType, opt.Value) - : throw new InvalidOperationException($"HolderOption for option \"{GetFormattedTitle(idx)}\" is not a ModdedEnumOption with enum type {enumType.FullName}"); + : throw new InvalidOperationException($"HolderOption for option \"{Titles[idx]}\" is not a ModdedEnumOption with enum type {enumType.FullName}"); } } @@ -44,16 +54,37 @@ public class ModdedEnumOptionListAttribute(string title, Type enumType, string[] /// /// The enum type. /// The option's title. +/// The common translation ID prefix of all enum values. /// The option's possible values. [AttributeUsage(AttributeTargets.Property)] -public class ModdedEnumOptionListAttribute(string title, string[]? values = null) +public class ModdedEnumOptionListAttribute(string? title, string idPrefix, string[]? values = null) : ModdedOptionListAttribute(title), ITypedOptionList where T : struct, Enum { - internal override IModdedOptionList CreateOptionList(IList value, PropertyInfo property) + /// + /// Initializes a new instance of the class. + /// + /// The option's title. + /// The option's possible values. + public ModdedEnumOptionListAttribute(string? title, string[]? values = null) + : this(title, string.Empty, values) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The option's possible values. + /// The common translation ID prefix of all enum values. + public ModdedEnumOptionListAttribute(string[]? values = null, string idPrefix = "") + : this(null, idPrefix, values) + { + } + + internal override IModdedOptionList CreateOptionList(IList list, PropertyInfo property, AbstractOptionGroup group) { var optList = new ModdedOptionList>( - value.Count, idx => new(GetFormattedTitle(idx), (T)(value[idx] ?? 0), values)); + list.Count, idx => new(group.GroupPrefix, GetFormattedTitle(idx, property), (T)(list[idx] ?? 0), idPrefix, values)); return optList; } @@ -80,6 +111,6 @@ public class ModdedEnumOptionListAttribute(string title, string[]? values = n { return HolderOptionList?[idx] is ModdedEnumOption opt ? opt.Value - : throw new InvalidOperationException($"HolderOption for option \"{GetFormattedTitle(idx)}\" is not a ModdedEnumOption<{typeof(T).FullName}>"); + : throw new InvalidOperationException($"HolderOption for option \"{Titles[idx]}\" is not a ModdedEnumOption<{typeof(T).FullName}>"); } } diff --git a/MiraAPI/GameOptions/Attributes/OptionList/ModdedNumberOptionListAttribute.cs b/MiraAPI/GameOptions/Attributes/OptionList/ModdedNumberOptionListAttribute.cs index 79335a39..4f403994 100644 --- a/MiraAPI/GameOptions/Attributes/OptionList/ModdedNumberOptionListAttribute.cs +++ b/MiraAPI/GameOptions/Attributes/OptionList/ModdedNumberOptionListAttribute.cs @@ -20,7 +20,7 @@ namespace MiraAPI.GameOptions.Attributes; /// Whether increments can be split in half (using Shift). [AttributeUsage(AttributeTargets.Property)] public class ModdedNumberOptionListAttribute( - string title, + string? title, float min, float max, float increment, @@ -31,6 +31,30 @@ public class ModdedNumberOptionListAttribute( bool halfIncrements = false) : ModdedOptionListAttribute(title), ITypedOptionList, ITypedOptionList { + /// + /// Initializes a new instance of the class. + /// + /// The minimum value. + /// The maximum value. + /// The increment. + /// Determines what is shown for zero. + /// Determines what is shown for negative 1. + /// The suffix type. + /// Optional format string for the option screen. + /// Whether increments can be split in half (using Shift). + public ModdedNumberOptionListAttribute( + float min, + float max, + float increment, + string zeroBehavior, + string negativeBehavior, + MiraNumberSuffixes suffixType = MiraNumberSuffixes.None, + string? formatString = null, + bool halfIncrements = false) + : this(null, min, max, increment, zeroBehavior, negativeBehavior, suffixType, formatString, halfIncrements) + { + } + /// /// Initializes a new instance of the class. /// @@ -42,7 +66,7 @@ public class ModdedNumberOptionListAttribute( /// Whether zero is infinity or not. /// Optional format string for the option screen. public ModdedNumberOptionListAttribute( - string title, + string? title, float min, float max, float increment = 1, @@ -53,19 +77,40 @@ public class ModdedNumberOptionListAttribute( { } - internal override IModdedOptionList CreateOptionList(IList value, PropertyInfo property) + /// + /// Initializes a new instance of the class. + /// + /// The minimum value. + /// The maximum value. + /// The increment. + /// The suffix type. + /// Whether zero is infinity or not. + /// Optional format string for the option screen. + public ModdedNumberOptionListAttribute( + float min, + float max, + float increment = 1, + MiraNumberSuffixes suffixType = MiraNumberSuffixes.None, + string? formatString = null, + bool zeroInfinity = false) + : this(null, min, max, increment, suffixType, formatString, zeroInfinity) { - return new ModdedOptionList(value.Count, idx => + } + + internal override IModdedOptionList CreateOptionList(IList list, PropertyInfo property, AbstractOptionGroup group) + { + return new ModdedOptionList(list.Count, idx => { - var final = value[idx] switch + var final = list[idx] switch { null => min + increment, int integer => integer, - _ => (float)value[idx]!, + _ => (float)list[idx]!, }; return new( - GetFormattedTitle(idx), + group.GroupPrefix, + GetFormattedTitle(idx, property), final, min, max, @@ -108,7 +153,7 @@ public class ModdedNumberOptionListAttribute( public override object GetValue(int idx) { if (HolderOptionList?[idx] is not ModdedNumberOption opt) - throw new InvalidOperationException($"HolderOption for option \"{GetFormattedTitle(idx)}\" is not a ModdedNumberOption"); + throw new InvalidOperationException($"HolderOption for option \"{Titles[idx]}\" is not a ModdedNumberOption"); var value = opt.Value; @@ -123,7 +168,7 @@ public class ModdedNumberOptionListAttribute( { return HolderOptionList?[idx] is ModdedNumberOption opt ? opt.Value - : throw new InvalidOperationException($"HolderOption for option \"{GetFormattedTitle(idx)}\" is not a ModdedNumberOption"); + : throw new InvalidOperationException($"HolderOption for option \"{Titles[idx]}\" is not a ModdedNumberOption"); } /// diff --git a/MiraAPI/GameOptions/Attributes/OptionList/ModdedOptionListAttribute.cs b/MiraAPI/GameOptions/Attributes/OptionList/ModdedOptionListAttribute.cs index 17d2b492..5205c8c7 100644 --- a/MiraAPI/GameOptions/Attributes/OptionList/ModdedOptionListAttribute.cs +++ b/MiraAPI/GameOptions/Attributes/OptionList/ModdedOptionListAttribute.cs @@ -11,19 +11,22 @@ namespace MiraAPI.GameOptions.Attributes; /// /// The title of the options. [AttributeUsage(AttributeTargets.Property)] -public abstract class ModdedOptionListAttribute(string title) : PropertyOptionAttribute +public abstract class ModdedOptionListAttribute(string? title = null) : PropertyOptionAttribute { internal IModdedOptionList? HolderOptionList { get; set; } internal IList? Value { get; set; } internal Type ValueType { get; set; } + internal string[] Titles { get; set; } + + private static readonly Regex FormatParams = new(@"\{(\d+)(?::[^}]+)?\}", default, Regex.InfiniteMatchTimeout); /// - /// Gets the title of the options. + /// Gets or sets the title of the options. /// If the string contains a format parameter, e.g., {0}, then the option's index will be given to it. /// The string cannot contain more than one parameter. /// If no parameter is given, the title will simply append the index to the end of it. /// - public string Title => title; + public string? Title { get; protected set; } = title; /// /// Sets the value of all the options. @@ -67,28 +70,33 @@ public abstract class ModdedOptionListAttribute(string title) : PropertyOptionAt /// The value of the option as an object. public abstract object GetValue(int idx); - internal abstract IModdedOptionList? CreateOptionList(IList value, PropertyInfo property); + internal abstract IModdedOptionList? CreateOptionList(IList list, PropertyInfo property, AbstractOptionGroup group); /// /// Gets the formatted with the option's index. /// /// The option's index. + /// The property info for fallback title. /// The formatted option's title. - protected string GetFormattedTitle(int index) + protected string GetFormattedTitle(int index, PropertyInfo property) { - var match = Regex.Match(Title, @"\{(\d+)(?::[^}]+)?\}", default, Regex.InfiniteMatchTimeout); + var baseTitle = Title ?? property.Name; + return Titles[index] = GetFormattedTitle(baseTitle, index, FormatParams.Match(baseTitle)); + } + private static string GetFormattedTitle(string baseTitle, int index, Match match) + { if (!match.Success) { - return Title + index; + return baseTitle + index; } if (match.Value == "{0}" && !match.NextMatch().Success) { - return string.Format(CultureInfo.InvariantCulture, Title, index); + return string.Format(CultureInfo.InvariantCulture, baseTitle, index); } - Error($"ModdedOptionList Title of {Title} cannot contain more than the first parameter for formatting."); - return Title + index; + Error($"ModdedOptionList Title of {baseTitle} cannot contain more than the first parameter for formatting."); + return baseTitle + index; } } diff --git a/MiraAPI/GameOptions/Attributes/OptionList/ModdedPlayerOptionListAttribute.cs b/MiraAPI/GameOptions/Attributes/OptionList/ModdedPlayerOptionListAttribute.cs index ec98aea0..a2bc0d31 100644 --- a/MiraAPI/GameOptions/Attributes/OptionList/ModdedPlayerOptionListAttribute.cs +++ b/MiraAPI/GameOptions/Attributes/OptionList/ModdedPlayerOptionListAttribute.cs @@ -11,16 +11,23 @@ namespace MiraAPI.GameOptions.Attributes; /// The option title. /// Whether the option includes a none option. If None is selected, value will return -1. [AttributeUsage(AttributeTargets.Property)] -public class ModdedPlayerOptionListAttribute( - string title, - bool allowNone = true) +public class ModdedPlayerOptionListAttribute(string? title, bool allowNone = true) : ModdedOptionListAttribute(title), ITypedOptionList, ITypedOptionList { - internal override IModdedOptionList CreateOptionList(IList value, PropertyInfo property) + /// + /// Initializes a new instance of the class. + /// + /// Whether the option includes a none option. If None is selected, value will return -1. + public ModdedPlayerOptionListAttribute(bool allowNone = true) + : this(null, allowNone) { - return new ModdedOptionList(value.Count, idx => + } + + internal override IModdedOptionList CreateOptionList(IList list, PropertyInfo property, AbstractOptionGroup group) + { + return new ModdedOptionList(list.Count, idx => { - return new(GetFormattedTitle(idx), allowNone); + return new(group.GroupPrefix, GetFormattedTitle(idx, property), allowNone); }); } @@ -65,7 +72,7 @@ public class ModdedPlayerOptionListAttribute( public override object GetValue(int idx) { if (HolderOptionList?[idx] is not ModdedPlayerOption opt) - throw new InvalidOperationException($"HolderOption for option \"{GetFormattedTitle(idx)}\" is not a ModdedPlayerOption"); + throw new InvalidOperationException($"HolderOption for option \"{Titles[idx]}\" is not a ModdedPlayerOption"); if (ValueType == typeof(int)) return opt.Value; @@ -78,7 +85,7 @@ public class ModdedPlayerOptionListAttribute( { return HolderOptionList?[idx] is ModdedPlayerOption opt ? opt.SelectedPlayer - : throw new InvalidOperationException($"HolderOption for option \"{GetFormattedTitle(idx)}\" is not a ModdedPlayerOption"); + : throw new InvalidOperationException($"HolderOption for option \"{Titles[idx]}\" is not a ModdedPlayerOption"); } /// @@ -86,6 +93,6 @@ public class ModdedPlayerOptionListAttribute( { return HolderOptionList?[idx] is ModdedPlayerOption opt ? opt.Value - : throw new InvalidOperationException($"HolderOption for option \"{GetFormattedTitle(idx)}\" is not a ModdedPlayerOption"); + : throw new InvalidOperationException($"HolderOption for option \"{Titles[idx]}\" is not a ModdedPlayerOption"); } } diff --git a/MiraAPI/GameOptions/Attributes/OptionList/ModdedStringOptionListAttribute.cs b/MiraAPI/GameOptions/Attributes/OptionList/ModdedStringOptionListAttribute.cs index 3613c98a..1f28680d 100644 --- a/MiraAPI/GameOptions/Attributes/OptionList/ModdedStringOptionListAttribute.cs +++ b/MiraAPI/GameOptions/Attributes/OptionList/ModdedStringOptionListAttribute.cs @@ -2,7 +2,6 @@ using System; using System.Collections; using System.Reflection; using MiraAPI.GameOptions.OptionTypes; -using MiraAPI.Translation; namespace MiraAPI.GameOptions.Attributes; @@ -12,16 +11,23 @@ namespace MiraAPI.GameOptions.Attributes; /// The title of the option. /// An option list of string value. [AttributeUsage(AttributeTargets.Property)] -public class ModdedStringOptionListAttribute( - string title, - string[] values) +public class ModdedStringOptionListAttribute(string? title, string[] values) : ModdedOptionListAttribute(title), ITypedOptionList { - internal override IModdedOptionList CreateOptionList(IList value, PropertyInfo property) + /// + /// Initializes a new instance of the class. + /// + /// An option list of string value. + public ModdedStringOptionListAttribute(string[] values) + : this(null, values) { - return new ModdedOptionList(value.Count, idx => + } + + internal override IModdedOptionList CreateOptionList(IList list, PropertyInfo property, AbstractOptionGroup group) + { + return new ModdedOptionList(list.Count, idx => { - return new ModdedStringOption(Title.Translate(), (string)(value[idx] ?? string.Empty), values); + return new ModdedStringOption(group.GroupPrefix, GetFormattedTitle(idx, property), (string)(list[idx] ?? string.Empty), values); }); } @@ -48,6 +54,6 @@ public class ModdedStringOptionListAttribute( { return HolderOptionList?[idx] is ModdedStringOption opt ? opt.Value - : throw new InvalidOperationException($"HolderOption for option \"{GetFormattedTitle(idx)}\" is not a ModdedStringOption"); + : throw new InvalidOperationException($"HolderOption for option \"{Titles[idx]}\" is not a ModdedStringOption"); } } diff --git a/MiraAPI/GameOptions/Attributes/OptionList/ModdedToggleOptionListAttribute.cs b/MiraAPI/GameOptions/Attributes/OptionList/ModdedToggleOptionListAttribute.cs index decc51f2..20835e72 100644 --- a/MiraAPI/GameOptions/Attributes/OptionList/ModdedToggleOptionListAttribute.cs +++ b/MiraAPI/GameOptions/Attributes/OptionList/ModdedToggleOptionListAttribute.cs @@ -12,25 +12,40 @@ namespace MiraAPI.GameOptions.Attributes; /// What the option displays when toggled on. /// What the option displays when toggled off. [AttributeUsage(AttributeTargets.Property)] -public class ModdedToggleOptionListAttribute( - string title, - string on, - string off) +public class ModdedToggleOptionListAttribute(string? title, string on, string off) : ModdedOptionListAttribute(title), ITypedOptionList { + /// + /// Initializes a new instance of the class. + /// + /// What the option displays when toggled on. + /// What the option displays when toggled off. + public ModdedToggleOptionListAttribute(string on, string off) + : this(null, on, off) + { + } + /// /// Initializes a new instance of the class. /// /// The option's title. - public ModdedToggleOptionListAttribute(string title) + public ModdedToggleOptionListAttribute(string? title) : this(title, "On", "Off") { } - internal override IModdedOptionList CreateOptionList(IList value, PropertyInfo property) + /// + /// Initializes a new instance of the class. + /// + public ModdedToggleOptionListAttribute() + : this(null, "On", "Off") + { + } + + internal override IModdedOptionList CreateOptionList(IList list, PropertyInfo property, AbstractOptionGroup group) { var optList = new ModdedOptionList( - value.Count, idx => new(GetFormattedTitle(idx), (bool)(value[idx] ?? false), on, off)); + list.Count, idx => new(group.GroupPrefix, GetFormattedTitle(idx, property), (bool)(list[idx] ?? false), on, off)); return optList; } @@ -57,6 +72,6 @@ public class ModdedToggleOptionListAttribute( { return HolderOptionList?[idx] is ModdedToggleOption opt ? opt.Value - : throw new InvalidOperationException($"Holder option for {GetFormattedTitle(idx)} is not a ModdedToggleOption."); + : throw new InvalidOperationException($"Holder option \"{Titles[idx]}\" is not a ModdedToggleOption."); } } diff --git a/MiraAPI/GameOptions/ModdedOptionsManager.cs b/MiraAPI/GameOptions/ModdedOptionsManager.cs index 366214ee..fdb4362e 100644 --- a/MiraAPI/GameOptions/ModdedOptionsManager.cs +++ b/MiraAPI/GameOptions/ModdedOptionsManager.cs @@ -170,7 +170,7 @@ public static class ModdedOptionsManager return; } - var option = attribute.CreateOption(property.GetValue(group), property); + var option = attribute.CreateOption(property.GetValue(group), property, group); if (option == null) { @@ -277,8 +277,11 @@ public static class ModdedOptionsManager return; } - var propertyList = (IList)propertyVal; - var optionList = attribute.CreateOptionList(propertyList, property); + var list = (IList)propertyVal; + + attribute.Titles = new string[list.Count]; + + var optionList = attribute.CreateOptionList(list, property, group); if (optionList == null) { @@ -286,7 +289,7 @@ public static class ModdedOptionsManager return; } - if (propertyList.Count != optionList.Count) + if (list.Count != optionList.Count) { Error("Mismatch in count between values and created options."); return; @@ -326,7 +329,7 @@ public static class ModdedOptionsManager var valueType = propType.GetGenericArguments()[0]; attribute.HolderOptionList = optionList; - attribute.Value = propertyList; + attribute.Value = list; attribute.ValueType = valueType; ListInstanceCache.Add(propertyVal, attribute); diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedEnumOption.cs b/MiraAPI/GameOptions/OptionTypes/ModdedEnumOption.cs index bbe61675..bd48bdc0 100644 --- a/MiraAPI/GameOptions/OptionTypes/ModdedEnumOption.cs +++ b/MiraAPI/GameOptions/OptionTypes/ModdedEnumOption.cs @@ -1,8 +1,7 @@ using System; using System.Globalization; using System.Linq; -using System.Text; -using Il2CppInterop.Runtime.InteropTypes.Arrays; +using System.Runtime.CompilerServices; using MiraAPI.Networking; using MiraAPI.Translation; using UnityEngine; @@ -16,11 +15,64 @@ namespace MiraAPI.GameOptions.OptionTypes; [Obsolete("Use ModdedEnumOption instead. This will be removed in a future version.")] public class ModdedEnumOption : ModdedOption { + private readonly int[] underlyingValues; + /// /// Gets the string values of the enum. /// public string[] Values { get; } + /// + /// Gets the String Name values of the option. + /// + public StringNames[] StringNameValues { get; } + + /// + /// Initializes a new instance of the class. + /// + /// The translation ID prefix of the group containing this option. + /// The title of the option. + /// The default value as an int. + /// The type. + /// The common translation ID prefix of all enum values. + /// An option list of string values to use in place of the name. + /// Whether to include this option in the preset or not. + public ModdedEnumOption(string groupPrefix, string title, int defaultValue, Type enumType, string idPrefix, string[]? values = null, bool includeInPreset = true) + : base(groupPrefix, title, defaultValue, includeInPreset) + { + var rawValues = Enum.GetValues(enumType); + underlyingValues = new int[rawValues.Length]; + + for (var i = 0; i < rawValues.Length; i++) + underlyingValues[i] = Convert.ToInt32(rawValues.GetValue(i), NumberFormatInfo.InvariantInfo); + + Values = values ?? Enum.GetNames(enumType); + + StringNameValues = [.. Values.Select(x => MiraLocaleManager.GetOrCreateLocaleString($"{idPrefix}{x}"))]; + + var data = ScriptableObject.CreateInstance(); + data.Title = StringName; + data.Type = global::OptionTypes.String; + data.Values = StringNameValues; + data.Index = Array.IndexOf(underlyingValues, defaultValue); + + Data = data; + } + + /// + /// Initializes a new instance of the class. + /// + /// The translation ID prefix of the group containing this option. + /// The title of the option. + /// The default value as an int. + /// The type. + /// An option list of string values to use in place of the name. + /// Whether to include this option in the preset or not. + public ModdedEnumOption(string groupPrefix, string title, int defaultValue, Type enumType, string[]? values = null, bool includeInPreset = true) + : this(groupPrefix, title, defaultValue, enumType, string.Empty, values, includeInPreset) + { + } + /// /// Initializes a new instance of the class. /// @@ -30,25 +82,29 @@ public class ModdedEnumOption : ModdedOption /// An option list of string values to use in place of the name. /// Whether to include this option in the preset or not. public ModdedEnumOption(string title, int defaultValue, Type enumType, string[]? values = null, bool includeInPreset = true) - : base(title, defaultValue, includeInPreset) + : this(string.Empty, title, defaultValue, enumType, values, includeInPreset) { - Values = values ?? Enum.GetNames(enumType); + } - var data = ScriptableObject.CreateInstance(); - data.Title = StringName; - data.Type = global::OptionTypes.String; - data.Values = Values.Select(MiraLocaleManager.GetOrCreateLocaleString).ToArray(); - data.Index = Value; - - Data = data; + /// + /// Initializes a new instance of the class. + /// + /// The title of the option. + /// The default value as an int. + /// The type. + /// The common translation ID prefix of all enum values. + /// An option list of string values to use in place of the name. + /// Whether to include this option in the preset or not. + public ModdedEnumOption(string title, int defaultValue, Type enumType, string idPrefix, string[]? values = null, bool includeInPreset = true) + : this(string.Empty, title, defaultValue, enumType, idPrefix, values, includeInPreset) + { } /// public override OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOption numberOpt, StringOption stringOpt, PlayerOption playerOpt, Transform container) { var stringOption = Object.Instantiate(stringOpt, container); - stringOption.name = - $"{ParentMod!.OptionsTitleText}.EnumOption.{TranslationController.Instance.GetString(StringName)}"; + stringOption.name = $"{ParentMod!.OptionsTitleText}.EnumOption.{Title}"; stringOption.SetUpFromData(Data, 20); stringOption.OnValueChanged = (Action)ValueChanged; @@ -59,8 +115,8 @@ public class ModdedEnumOption : ModdedOption // SetUpFromData method doesn't work correctly so we must set the values manually stringOption.Title = StringName; - stringOption.Values = (Data as StringGameSetting)?.Values ?? new Il2CppStructArray(0); - stringOption.Value = Value; + stringOption.Values = StringNameValues; + stringOption.Value = Array.IndexOf(underlyingValues, Value); OptionBehaviour = stringOption; @@ -88,7 +144,12 @@ public class ModdedEnumOption : ModdedOption /// public override int GetValueFromOptionBehaviour(OptionBehaviour optionBehaviour) { - return optionBehaviour.GetInt(); + var index = optionBehaviour.GetInt(); + + if (index >= 0 && index < underlyingValues.Length) + return underlyingValues[index]; + + return underlyingValues.Length > 0 ? underlyingValues[0] : 0; } /// @@ -96,7 +157,7 @@ public class ModdedEnumOption : ModdedOption { if (OptionBehaviour && OptionBehaviour is StringOption opt) { - opt.Value = newValue; + opt.Value = Array.IndexOf(underlyingValues, newValue); } } } @@ -108,11 +169,57 @@ public class ModdedEnumOption : ModdedOption public class ModdedEnumOption : ModdedOption where T : struct, Enum { + private readonly T[] underlyingValues; + /// /// Gets the string values of the . /// public string[] Values { get; } + /// + /// Gets the String Name values of the option. + /// + public StringNames[] StringNameValues { get; } + + /// + /// Initializes a new instance of the class. + /// + /// The translation ID prefix of the group containing this option. + /// The title of the option. + /// The default value as an . + /// The common translation ID prefix of all enum values. + /// An option list of string values to use in place of the name. + /// Whether to include this option in the preset or not. + public ModdedEnumOption(string groupPrefix, string title, T defaultValue, string idPrefix, string[]? values = null, bool includeInPreset = true) + : base(groupPrefix, title, defaultValue, includeInPreset) + { + underlyingValues = Enum.GetValues(); + + Values = values ?? Enum.GetNames(); + StringNameValues = [.. Values.Select(x => MiraLocaleManager.GetOrCreateLocaleString($"{idPrefix}{x}"))]; + + var data = ScriptableObject.CreateInstance(); + data.Title = StringName; + data.Type = global::OptionTypes.String; + data.Values = StringNameValues; + data.Index = Array.IndexOf(underlyingValues, defaultValue); + + Data = data; + } + + /// + /// Initializes a new instance of the class. + /// + /// The translation ID prefix of the group containing this option. + /// The title of the option. + /// The default value as an . + /// An option list of string values to use in place of the name. + /// Whether to include this option in the preset or not. + public ModdedEnumOption(string groupPrefix, string title, T defaultValue, string[]? values = null, bool includeInPreset = true) + : this(groupPrefix, title, defaultValue, groupPrefix, values, includeInPreset) + { + } + /// /// Initializes a new instance of the class. /// @@ -121,25 +228,28 @@ public class ModdedEnumOption : ModdedOption /// An option list of string values to use in place of the name. /// Whether to include this option in the preset or not. public ModdedEnumOption(string title, T defaultValue, string[]? values = null, bool includeInPreset = true) - : base(title, defaultValue, includeInPreset) + : this(string.Empty, title, defaultValue, values, includeInPreset) { - Values = values ?? Enum.GetNames(); + } - var data = ScriptableObject.CreateInstance(); - data.Title = StringName; - data.Type = global::OptionTypes.String; - data.Values = Values.Select(MiraLocaleManager.GetOrCreateLocaleString).ToArray(); - data.Index = Convert.ToInt32(Value, NumberFormatInfo.InvariantInfo); - - Data = data; + /// + /// Initializes a new instance of the class. + /// + /// The title of the option. + /// The default value as an . + /// The common translation ID prefix of all enum values. + /// An option list of string values to use in place of the name. + /// Whether to include this option in the preset or not. + public ModdedEnumOption(string title, T defaultValue, string idPrefix, string[]? values = null, bool includeInPreset = true) + : this(string.Empty, title, defaultValue, idPrefix, values, includeInPreset) + { } /// public override OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOption numberOpt, StringOption stringOpt, PlayerOption playerOpt, Transform container) { var stringOption = Object.Instantiate(stringOpt, container); - stringOption.name = - $"{ParentMod!.OptionsTitleText}.EnumOption.{TranslationController.Instance.GetString(StringName)}"; + stringOption.name = $"{ParentMod!.OptionsTitleText}.EnumOption.{Title}"; stringOption.SetUpFromData(Data, 20); stringOption.OnValueChanged = (Action)ValueChanged; @@ -150,8 +260,8 @@ public class ModdedEnumOption : ModdedOption // SetUpFromData method doesn't work correctly so we must set the values manually stringOption.Title = StringName; - stringOption.Values = (Data as StringGameSetting)?.Values ?? new Il2CppStructArray(0); - stringOption.Value = Convert.ToInt32(Value, NumberFormatInfo.InvariantInfo); + stringOption.Values = StringNameValues; + stringOption.Value = Array.IndexOf(underlyingValues, Value); OptionBehaviour = stringOption; @@ -167,13 +277,15 @@ public class ModdedEnumOption : ModdedOption /// public override NetData GetNetData() { - return new NetData(Id, Encoding.Unicode.GetBytes(Convert.ToString(Value, NumberFormatInfo.InvariantInfo)!)); + var buffer = new byte[Unsafe.SizeOf()]; + Unsafe.WriteUnaligned(ref buffer[0], Value); + return new NetData(Id, buffer); } /// public override void HandleNetData(byte[] data) { - SetValue(Enum.Parse(Encoding.Unicode.GetString(data))); + SetValue(Unsafe.ReadUnaligned(ref data[0])); } /// diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedNumberOption.cs b/MiraAPI/GameOptions/OptionTypes/ModdedNumberOption.cs index 5135027d..b64267e1 100644 --- a/MiraAPI/GameOptions/OptionTypes/ModdedNumberOption.cs +++ b/MiraAPI/GameOptions/OptionTypes/ModdedNumberOption.cs @@ -43,33 +43,35 @@ public class ModdedNumberOption : ModdedOption /// public string NegativeWordValue { get; } - /// - /// Gets a value indicating whether zero is infinity. - /// - public bool ZeroInfinity => ZeroWordValue == "∞"; - /// /// Gets a value indicating whether holding shift will divide the increment. /// public bool ShiftIncrement { get; } + /// + /// Gets a value indicating whether zero is infinity. + /// + public bool ZeroInfinity => ZeroWordValue == "∞"; + private static readonly Regex TrailingZeroRegex = new(@"\.0+(?!\d)", RegexOptions.Compiled, TimeSpan.FromMilliseconds(100)); /// /// Initializes a new instance of the class. /// + /// The translation ID prefix of the group containing this option. /// The title of the option. /// The default value as a float. /// The minimum value. /// The maximum value. /// The increment. - /// The suffix type. /// Determines what is shown for zero. /// Determines what is shown for negative 1. - /// Whether increments can be split in half. + /// The suffix type. /// Optional format string for the option screen. + /// Whether increments can be split in half. /// Whether to include this option in the preset or not. public ModdedNumberOption( + string groupPrefix, string title, float defaultValue, float min, @@ -81,7 +83,7 @@ public class ModdedNumberOption : ModdedOption string? formatString = null, bool halfIncrements = false, bool includeInPreset = true) - : base(title, defaultValue, includeInPreset) + : base(groupPrefix, title, defaultValue, includeInPreset) { Min = min; Max = max; @@ -116,14 +118,72 @@ public class ModdedNumberOption : ModdedOption /// /// Initializes a new instance of the class. /// + /// The translation ID prefix of the group containing this option. /// The title of the option. /// The default value as a float. /// The minimum value. /// The maximum value. /// The increment. /// The suffix type. - /// Whether zero is infinity or not. /// Optional format string for the option screen. + /// Whether zero is infinity or not. + /// Whether to include this option in the preset or not. + public ModdedNumberOption( + string groupPrefix, + string title, + float defaultValue, + float min, + float max, + float increment, + MiraNumberSuffixes suffixType, + string? formatString = null, + bool zeroInfinity = false, + bool includeInPreset = true) + : this(groupPrefix, title, defaultValue, min, max, increment, zeroInfinity ? "∞" : "#", "#", suffixType, formatString, increment == 1f, includeInPreset) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The title of the option. + /// The default value as a float. + /// The minimum value. + /// The maximum value. + /// The increment. + /// Determines what is shown for zero. + /// Determines what is shown for negative 1. + /// The suffix type. + /// Optional format string for the option screen. + /// Whether increments can be split in half. + /// Whether to include this option in the preset or not. + public ModdedNumberOption( + string title, + float defaultValue, + float min, + float max, + float increment, + string zeroBehavior, + string negativeBehavior, + MiraNumberSuffixes suffixType, + string? formatString = null, + bool halfIncrements = false, + bool includeInPreset = true) + : this(string.Empty, title, defaultValue, min, max, increment, zeroBehavior, negativeBehavior, suffixType, formatString, halfIncrements, includeInPreset) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The title of the option. + /// The default value as a float. + /// The minimum value. + /// The maximum value. + /// The increment. + /// The suffix type. + /// Optional format string for the option screen. + /// Whether zero is infinity or not. /// Whether to include this option in the preset or not. public ModdedNumberOption( string title, @@ -135,7 +195,7 @@ public class ModdedNumberOption : ModdedOption string? formatString = null, bool zeroInfinity = false, bool includeInPreset = true) - : this(title, defaultValue, min, max, increment, zeroInfinity ? "∞" : "#", "#", suffixType, formatString, increment == 1f, includeInPreset) + : this(string.Empty, title, defaultValue, min, max, increment, suffixType, formatString, zeroInfinity, includeInPreset) { } @@ -148,8 +208,7 @@ public class ModdedNumberOption : ModdedOption Transform container) { var numberOption = Object.Instantiate(numberOpt, Vector3.zero, Quaternion.identity, container); - numberOption.name = - $"{ParentMod!.OptionsTitleText}.NumberOption.{TranslationController.Instance.GetString(StringName)}"; + numberOption.name = $"{ParentMod!.OptionsTitleText}.NumberOption.{Title}"; var optionComponent = numberOption.gameObject.AddComponent(); optionComponent.NumberOption = this; optionComponent.DefaultIncrement = Increment; diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedOption.cs b/MiraAPI/GameOptions/OptionTypes/ModdedOption.cs index fff57a57..e0781ed1 100644 --- a/MiraAPI/GameOptions/OptionTypes/ModdedOption.cs +++ b/MiraAPI/GameOptions/OptionTypes/ModdedOption.cs @@ -14,33 +14,20 @@ namespace MiraAPI.GameOptions.OptionTypes; /// Represents a modded option. /// /// The value type. -public abstract class ModdedOption : IModdedOption +/// The translation ID prefix of the group containing this option. +/// The option title. +/// The default value. +/// Whether to include the option in the preset. +public abstract class ModdedOption(string groupPrefix, string title, T defaultValue, bool includeInPreset = true) : IModdedOption { /// - public uint Id { get; } + public uint Id { get; } = ModdedOptionsManager.NextId; /// - public string Title { get; set; } + public string Title { get; set; } = groupPrefix + title; /// - public StringNames StringName { get; } - - /// - public BaseGameSetting Data { get; protected set; } = null!; - - /// - public IMiraPlugin? ParentMod - { - get; - set - { - if (field != null || value == null) return; - field = value; - - var entry = field.GetConfigFile().Bind(ConfigDefinition, DefaultValue); - Value = entry.Value; - } - } + public StringNames StringName { get; } = MiraLocaleManager.GetOrCreateLocaleString(groupPrefix + title); /// /// Gets or sets the value of the option. @@ -59,12 +46,24 @@ public abstract class ModdedOption : IModdedOption } } - private T _value; + private T _value = defaultValue; /// /// Gets the default value of the option. /// - public T DefaultValue { get; } + public T DefaultValue { get; } = defaultValue; + + /// + public Func Visible { get; set; } = DelegateUtilities.AlwaysTrue; + + /// + public bool IncludeInPreset { get; set; } = includeInPreset; + + /// + public bool ShowNotification { get; set; } = true; + + /// + public BaseGameSetting Data { get; protected set; } = null!; /// public Func? Getter { get; set; } @@ -75,15 +74,6 @@ public abstract class ModdedOption : IModdedOption /// public Action? ChangedEvent { get; set; } - /// - public Func Visible { get; set; } - - /// - public bool IncludeInPreset { get; set; } - - /// - public bool ShowNotification { get; set; } = true; - /// public OptionBehaviour? OptionBehaviour { get; protected set; } @@ -94,7 +84,18 @@ public abstract class ModdedOption : IModdedOption public AbstractOptionGroup ParentGroup { get; set; } /// - public virtual OptionNotifConfiguration Configuration => new(ParentGroup); + public IMiraPlugin? ParentMod + { + get; + set + { + if (field != null || value == null) return; + field = value; + + var entry = field.GetConfigFile().Bind(ConfigDefinition, DefaultValue); + Value = entry.Value; + } + } /// Delegate? IModdedOption.Getter @@ -117,6 +118,9 @@ public abstract class ModdedOption : IModdedOption set => ChangedEvent = value as Action; } + /// + public virtual OptionNotifConfiguration Configuration => new(ParentGroup); + /// /// Initializes a new instance of the class. /// @@ -124,14 +128,8 @@ public abstract class ModdedOption : IModdedOption /// The default value. /// Whether to include the option in the preset. protected ModdedOption(string title, T defaultValue, bool includeInPreset = true) + : this(string.Empty, title, defaultValue, includeInPreset) { - Id = ModdedOptionsManager.NextId; - Title = title.Translate(); - DefaultValue = defaultValue; - Value = defaultValue; - StringName = MiraLocaleManager.GetOrCreateLocaleString(Title); - Visible = DelegateUtilities.AlwaysTrue; - IncludeInPreset = includeInPreset; } internal void ValueChanged(OptionBehaviour optionBehaviour) @@ -226,29 +224,12 @@ 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. /// @@ -264,6 +245,23 @@ public abstract class ModdedOption : IModdedOption PlayerOption playerOpt, Transform container); + /// + public virtual string GetDisplayValue() + { + return Data.GetValueString(GetFloatData()); + } + + /// + /// 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; + } + /// /// Implicitly converts the option to type of . /// diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedOptionList.cs b/MiraAPI/GameOptions/OptionTypes/ModdedOptionList.cs index 6e9e9ad3..e134fe4d 100644 --- a/MiraAPI/GameOptions/OptionTypes/ModdedOptionList.cs +++ b/MiraAPI/GameOptions/OptionTypes/ModdedOptionList.cs @@ -51,6 +51,9 @@ public class ModdedOptionList : IModdedOptionList /// option of type . public T this[int idx] => Options[idx]; + /// + IModdedOption IReadOnlyList.this[int index] => this[index]; + /// public IEnumerator GetEnumerator() { @@ -68,7 +71,4 @@ public class ModdedOptionList : IModdedOptionList { return ((IEnumerable)Options).GetEnumerator(); } - - /// - IModdedOption IReadOnlyList.this[int index] => this[index]; } diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedPlayerOption.cs b/MiraAPI/GameOptions/OptionTypes/ModdedPlayerOption.cs index 1b7f8090..e5479199 100644 --- a/MiraAPI/GameOptions/OptionTypes/ModdedPlayerOption.cs +++ b/MiraAPI/GameOptions/OptionTypes/ModdedPlayerOption.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using MiraAPI.Networking; -using MiraAPI.Translation; using MiraAPI.Utilities; using UnityEngine; using Object = UnityEngine.Object; @@ -37,10 +36,11 @@ public class ModdedPlayerOption : ModdedOption /// /// Initializes a new instance of the class. /// + /// The translation ID prefix of the group containing this option. /// The title of the option. /// Whether the option includes a none option. If None is selected, value will return -1. - public ModdedPlayerOption(string title, bool allowNone = true) - : base(title, allowNone ? -1 : 0, false) + public ModdedPlayerOption(string groupPrefix, string title, bool allowNone = true) + : base(groupPrefix, title, allowNone ? -1 : 0, false) { Values = []; AllowNone = allowNone; @@ -54,6 +54,16 @@ public class ModdedPlayerOption : ModdedOption Data = data; } + /// + /// Initializes a new instance of the class. + /// + /// The title of the option. + /// Whether the option includes a none option. If None is selected, value will return -1. + public ModdedPlayerOption(string title, bool allowNone = true) + : this(string.Empty, title, allowNone) + { + } + /// public override OptionBehaviour CreateOption( ToggleOption toggleOpt, @@ -63,8 +73,7 @@ public class ModdedPlayerOption : ModdedOption Transform container) { var playerOption = Object.Instantiate(playerOpt, container); - playerOption.name = - $"{ParentMod!.OptionsTitleText}.PlayerOption.{TranslationController.Instance.GetString(StringName)}"; + playerOption.name = $"{ParentMod!.OptionsTitleText}.PlayerOption.{Title}"; Values = GetFilteredPlayers(); @@ -72,7 +81,6 @@ public class ModdedPlayerOption : ModdedOption playerOption.OnValueChanged = (Action)ValueChanged; playerOption.Title = StringName; - playerOption.TitleText.text = Title.Translate(); playerOption.Values = Values.ToIl2CppList(); playerOption.Value = Value; playerOption.playerIndex = Values.FindIndex(p => p.PlayerId == Value); diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedStringOption.cs b/MiraAPI/GameOptions/OptionTypes/ModdedStringOption.cs index ca52d831..10ca7aaf 100644 --- a/MiraAPI/GameOptions/OptionTypes/ModdedStringOption.cs +++ b/MiraAPI/GameOptions/OptionTypes/ModdedStringOption.cs @@ -28,12 +28,13 @@ public class ModdedStringOption : ModdedOption /// /// Initializes a new instance of the class. /// + /// The translation ID prefix of the group containing this option. /// The title of the option. /// The default value as a string. /// An option list of string values to use. /// Whether to include this option in the preset or not. - public ModdedStringOption(string title, string defaultValue, string[] values, bool includeInPreset = true) - : base(title, defaultValue, includeInPreset) + public ModdedStringOption(string groupPrefix, string title, string defaultValue, string[] values, bool includeInPreset = true) + : base(groupPrefix, title, defaultValue, includeInPreset) { Values = values; StringNameValues = [.. values.Select(MiraLocaleManager.GetOrCreateLocaleString)]; @@ -47,6 +48,18 @@ public class ModdedStringOption : ModdedOption Data = data; } + /// + /// Initializes a new instance of the class. + /// + /// The title of the option. + /// The default value as a string. + /// An option list of string values to use. + /// Whether to include this option in the preset or not. + public ModdedStringOption(string title, string defaultValue, string[] values, bool includeInPreset = true) + : this(string.Empty, title, defaultValue, values, includeInPreset) + { + } + /// /// Gets the string's index. /// @@ -120,8 +133,7 @@ public class ModdedStringOption : ModdedOption public override OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOption numberOpt, StringOption stringOpt, PlayerOption playerOpt, Transform container) { var stringOption = Object.Instantiate(stringOpt, container); - stringOption.name = - $"{ParentMod!.OptionsTitleText}.StringOption.{TranslationController.Instance.GetString(StringName)}"; + stringOption.name = $"{ParentMod!.OptionsTitleText}.StringOption.{Title}"; stringOption.SetUpFromData(Data, 20); stringOption.OnValueChanged = (Action)ValueChanged; diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedToggleOption.cs b/MiraAPI/GameOptions/OptionTypes/ModdedToggleOption.cs index 4ce0d551..1e92d19d 100644 --- a/MiraAPI/GameOptions/OptionTypes/ModdedToggleOption.cs +++ b/MiraAPI/GameOptions/OptionTypes/ModdedToggleOption.cs @@ -24,13 +24,14 @@ public class ModdedToggleOption : ModdedOption /// /// Initializes a new instance of the class. /// + /// The translation ID prefix of the group containing this option. /// The option title. /// The default value. /// What the option displays when toggled on. /// What the option displays when toggled off. /// Whether to include this option in the preset or not. - public ModdedToggleOption(string title, bool defaultValue, string on, string off, bool includeInPreset = true) - : base(title, defaultValue, includeInPreset) + public ModdedToggleOption(string groupPrefix, string title, bool defaultValue, string on, string off, bool includeInPreset = true) + : base(groupPrefix, title, defaultValue, includeInPreset) { OnValue = on; OffValue = off; @@ -42,6 +43,31 @@ public class ModdedToggleOption : ModdedOption Data = data; } + /// + /// Initializes a new instance of the class. + /// + /// The translation ID prefix of the group containing this option. + /// The option title. + /// The default value. + /// Whether to include this option in the preset or not. + public ModdedToggleOption(string groupPrefix, string title, bool defaultValue, bool includeInPreset = true) + : this(groupPrefix, title, defaultValue, "On", "Off", includeInPreset) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The option title. + /// The default value. + /// What the option displays when toggled on. + /// What the option displays when toggled off. + /// Whether to include this option in the preset or not. + public ModdedToggleOption(string title, bool defaultValue, string on, string off, bool includeInPreset = true) + : this(string.Empty, title, defaultValue, on, off, includeInPreset) + { + } + /// /// Initializes a new instance of the class. /// @@ -49,7 +75,7 @@ public class ModdedToggleOption : ModdedOption /// The default value. /// Whether to include this option in the preset or not. public ModdedToggleOption(string title, bool defaultValue, bool includeInPreset = true) - : this(title, defaultValue, "On", "Off", includeInPreset) + : this(string.Empty, title, defaultValue, includeInPreset) { } @@ -57,13 +83,11 @@ public class ModdedToggleOption : ModdedOption public override OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOption numberOpt, StringOption stringOpt, PlayerOption playerOpt, Transform container) { var toggleOption = Object.Instantiate(toggleOpt, Vector3.zero, Quaternion.identity, container); - toggleOption.name = - $"{ParentMod!.OptionsTitleText}.ToggleOption.{TranslationController.Instance.GetString(StringName)}"; + toggleOption.name = $"{ParentMod!.OptionsTitleText}.ToggleOption.{Title}"; toggleOption.SetUpFromData(Data, 20); toggleOption.Title = StringName; - toggleOption.TitleText.text = Title.Translate(); toggleOption.CheckMark.enabled = Value; toggleOption.OnValueChanged = (Action)ValueChanged;