options translation handling overhaul

This commit is contained in:
Az committed 2026-11-02 17:32:56 -05:00
1 parent ac7e9244f2
commit 8f6f97bbc0
21 files changed
+695 -220

No files matched your search

+16 -3
View File
@@ -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<IModdedOption> Options { get; } = [];
private readonly ReadOnlyCollection<IModdedOption> children;
/// <summary>
/// Initializes a new instance of the <see cref="AbstractOptionGroup"/> class.
/// </summary>
protected AbstractOptionGroup()
{
children = new(Options);
}
/// <summary>
/// Gets a list of the options which are a part of this group.
/// </summary>
public ReadOnlyCollection<IModdedOption> Children => new(Options);
public ReadOnlyCollection<IModdedOption> Children => children;
/// <summary>
/// Gets the name of the group. Visible in options menu.
/// </summary>
public abstract string GroupName { get; }
/// <summary>
/// Gets the ID prefix used for options in this group (e.g., "Options.Teleporter").
/// </summary>
public virtual string GroupPrefix => string.Empty;
/// <summary>
/// Gets the sprite of the group that is displayed in the match info guide.
/// </summary>
@@ -45,7 +59,6 @@ public abstract class AbstractOptionGroup
/// </summary>
// 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;
/// <summary>
@@ -9,15 +9,49 @@ namespace MiraAPI.GameOptions.Attributes;
/// </summary>
/// <param name="title">The title of the option.</param>
/// <param name="enumType">The <see cref="Enum"/> type.</param>
/// <param name="idPrefix">The common translation ID prefix of all enum values.</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. 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)
/// <summary>
/// Initializes a new instance of the <see cref="ModdedEnumOptionAttribute"/> class.
/// </summary>
/// <param name="title">The title of the option.</param>
/// <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>
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);
}
/// <summary>
/// Initializes a new instance of the <see cref="ModdedEnumOptionAttribute"/> class.
/// </summary>
/// <param name="enumType">The <see cref="Enum"/> type.</param>
/// <param name="idPrefix">The common translation ID prefix of all enum values.</param>
/// <param name="values">An option list of string values to use in place of the <see langword="enum"/> name.</param>
public ModdedEnumOptionAttribute(Type enumType, string idPrefix, string[]? values = null)
: this(null, enumType, idPrefix, values)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ModdedEnumOptionAttribute"/> class.
/// </summary>
/// <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>
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
/// </summary>
/// <typeparam name="T">The enum type.</typeparam>
/// <param name="title">The title of the option.</param>
/// <param name="idPrefix">The common translation ID prefix of all enum values.</param>
/// <param name="values">An option list of string values to use in place of the <see langword="enum"/> name.</param>
[AttributeUsage(AttributeTargets.Property)]
public class ModdedEnumOptionAttribute<T>(string title, string[]? values = null)
public class ModdedEnumOptionAttribute<T>(string? title, string idPrefix, string[]? values = null)
: ModdedOptionAttribute(title), ITypedOption<T>
where T : struct, Enum
{
internal override IModdedOption CreateOption(object? value, PropertyInfo property)
/// <summary>
/// Initializes a new instance of the <see cref="ModdedEnumOptionAttribute{T}"/> class.
/// </summary>
/// <param name="title">The title of the option.</param>
/// <param name="values">An option list of string values to use in place of the <see langword="enum"/> name.</param>
public ModdedEnumOptionAttribute(string? title, string[]? values = null)
: this(title, string.Empty, values)
{
var opt = new ModdedEnumOption<T>(Title, (T)(value ?? 0), values);
}
/// <summary>
/// Initializes a new instance of the <see cref="ModdedEnumOptionAttribute{T}"/> class.
/// </summary>
/// <param name="values">An option list of string values to use in place of the <see langword="enum"/> name.</param>
public ModdedEnumOptionAttribute(string[]? values = null)
: this(null, string.Empty, values)
{
}
internal override IModdedOption CreateOption(object? value, PropertyInfo property, AbstractOptionGroup group)
{
var opt = new ModdedEnumOption<T>(group.GroupPrefix, Title ?? property.Name, (T)(value ?? 0), idPrefix, values);
Title = opt.Title;
return opt;
}
@@ -19,7 +19,7 @@ namespace MiraAPI.GameOptions.Attributes;
/// <param name="halfIncrements">Whether increments can be split in half (using Shift).</param>
[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<int>, ITypedOption<float>
{
/// <summary>
/// Initializes a new instance of the <see cref="ModdedNumberOptionAttribute"/> class.
/// </summary>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <param name="increment">The increment.</param>
/// <param name="zeroBehavior">Determines what is shown for zero.</param>
/// <param name="negativeBehavior">Determines what is shown for negative 1.</param>
/// <param name="suffixType">The suffix type.</param>
/// <param name="formatString">Optional format string for the option screen.</param>
/// <param name="halfIncrements">Whether increments can be split in half (using Shift).</param>
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)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ModdedNumberOptionAttribute"/> class.
/// </summary>
@@ -41,7 +65,7 @@ public class ModdedNumberOptionAttribute(
/// <param name="zeroInfinity">Whether zero is infinity or not.</param>
/// <param name="formatString">Optional format string for the option screen.</param>
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)
/// <summary>
/// Initializes a new instance of the <see cref="ModdedNumberOptionAttribute"/> class.
/// </summary>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <param name="increment">The increment.</param>
/// <param name="suffixType">The suffix type.</param>
/// <param name="zeroInfinity">Whether zero is infinity or not.</param>
/// <param name="formatString">Optional format string for the option screen.</param>
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;
}
/// <inheritdoc />
@@ -10,25 +10,25 @@ namespace MiraAPI.GameOptions.Attributes;
/// <param name="roleType">Optional parameter to specify a role Type.</param>
/// <param name="modeType">Optional parameter to specify a game mode Type.</param>
[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; }
/// <summary>
/// Gets the title of the option.
/// </summary>
public string Title => title;
/// <summary>
/// Gets the role type of the option.
/// </summary>
protected Type? RoleType { get; private set; } = roleType;
protected Type? RoleType { get; } = roleType;
/// <summary>
/// Gets the game mode type of the option.
/// </summary>
protected Type? ModeType { get; private set; } = modeType;
protected Type? ModeType { get; } = modeType;
internal abstract IModdedOption? CreateOption(object? value, PropertyInfo property);
/// <summary>
/// Gets or sets the title of the option.
/// </summary>
public string? Title { get; protected set; } = title;
internal abstract IModdedOption? CreateOption(object? value, PropertyInfo property, AbstractOptionGroup group);
}
@@ -10,15 +10,23 @@ namespace MiraAPI.GameOptions.Attributes;
/// <param name="title">The title of the option.</param>
/// <param name="allowNone">Whether the option includes a none option. If None is selected, value will return -1.</param>
[AttributeUsage(AttributeTargets.Property)]
public class ModdedPlayerOptionAttribute(
string title,
bool allowNone = true)
public class ModdedPlayerOptionAttribute(string? title, bool allowNone = true)
: ModdedOptionAttribute(title), ITypedOption<int>, ITypedOption<NetworkedPlayerInfo?>
{
internal override IModdedOption? CreateOption(object? value, PropertyInfo property)
/// <summary>
/// Initializes a new instance of the <see cref="ModdedPlayerOptionAttribute"/> class.
/// </summary>
/// <param name="allowNone">Whether the option includes a none option. If None is selected, value will return -1.</param>
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;
}
/// <inheritdoc />
@@ -10,15 +10,23 @@ namespace MiraAPI.GameOptions.Attributes;
/// <param name="title">The title of the option.</param>
/// <param name="values">An option list of string value.</param>
[AttributeUsage(AttributeTargets.Property)]
public class ModdedStringOptionAttribute(
string title,
string[] values)
public class ModdedStringOptionAttribute(string? title, string[] values)
: ModdedOptionAttribute(title), ITypedOption<string>
{
internal override IModdedOption? CreateOption(object? value, PropertyInfo property)
/// <summary>
/// Initializes a new instance of the <see cref="ModdedStringOptionAttribute"/> class.
/// </summary>
/// <param name="values">An option list of string value.</param>
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;
}
/// <inheritdoc />
@@ -11,25 +11,41 @@ namespace MiraAPI.GameOptions.Attributes;
/// <param name="on">What the option displays when toggled on.</param>
/// <param name="off">What the option displays when toggled off.</param>
[AttributeUsage(AttributeTargets.Property)]
public class ModdedToggleOptionAttribute(
string title,
string on,
string off)
public class ModdedToggleOptionAttribute(string? title, string on, string off)
: ModdedOptionAttribute(title), ITypedOption<bool>
{
/// <summary>
/// Initializes a new instance of the <see cref="ModdedToggleOptionAttribute"/> class.
/// </summary>
/// <param name="on">What the option displays when toggled on.</param>
/// <param name="off">What the option displays when toggled off.</param>
public ModdedToggleOptionAttribute(string on, string off)
: this(null, on, off)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ModdedToggleOptionAttribute"/> class.
/// </summary>
/// <param name="title">The option title.</param>
public ModdedToggleOptionAttribute(string title)
public ModdedToggleOptionAttribute(string? title)
: this(title, "On", "Off")
{
}
internal override IModdedOption CreateOption(object? value, PropertyInfo property)
/// <summary>
/// Initializes a new instance of the <see cref="ModdedToggleOptionAttribute"/> class.
/// </summary>
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;
}
/// <inheritdoc />
@@ -13,13 +13,23 @@ namespace MiraAPI.GameOptions.Attributes;
/// <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. 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)
/// <summary>
/// Initializes a new instance of the <see cref="ModdedEnumOptionListAttribute"/> class.
/// </summary>
/// <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>
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<ModdedEnumOption>(
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[]
/// </summary>
/// <typeparam name="T">The enum type.</typeparam>
/// <param name="title">The option's title.</param>
/// <param name="idPrefix">The common translation ID prefix of all enum values.</param>
/// <param name="values">The option's possible values.</param>
[AttributeUsage(AttributeTargets.Property)]
public class ModdedEnumOptionListAttribute<T>(string title, string[]? values = null)
public class ModdedEnumOptionListAttribute<T>(string? title, string idPrefix, string[]? values = null)
: ModdedOptionListAttribute(title), ITypedOptionList<T>
where T : struct, Enum
{
internal override IModdedOptionList CreateOptionList(IList value, PropertyInfo property)
/// <summary>
/// Initializes a new instance of the <see cref="ModdedEnumOptionListAttribute{T}"/> class.
/// </summary>
/// <param name="title">The option's title.</param>
/// <param name="values">The option's possible values.</param>
public ModdedEnumOptionListAttribute(string? title, string[]? values = null)
: this(title, string.Empty, values)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ModdedEnumOptionListAttribute{T}"/> class.
/// </summary>
/// <param name="values">The option's possible values.</param>
/// <param name="idPrefix">The common translation ID prefix of all enum values.</param>
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<ModdedEnumOption<T>>(
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<T>(string title, string[]? values = n
{
return HolderOptionList?[idx] is ModdedEnumOption<T> 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}>");
}
}
@@ -20,7 +20,7 @@ namespace MiraAPI.GameOptions.Attributes;
/// <param name="halfIncrements">Whether increments can be split in half (using Shift).</param>
[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<int>, ITypedOptionList<float>
{
/// <summary>
/// Initializes a new instance of the <see cref="ModdedNumberOptionListAttribute"/> class.
/// </summary>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <param name="increment">The increment.</param>
/// <param name="zeroBehavior">Determines what is shown for zero.</param>
/// <param name="negativeBehavior">Determines what is shown for negative 1.</param>
/// <param name="suffixType">The suffix type.</param>
/// <param name="formatString">Optional format string for the option screen.</param>
/// <param name="halfIncrements">Whether increments can be split in half (using Shift).</param>
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)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ModdedNumberOptionListAttribute"/> class.
/// </summary>
@@ -42,7 +66,7 @@ public class ModdedNumberOptionListAttribute(
/// <param name="zeroInfinity">Whether zero is infinity or not.</param>
/// <param name="formatString">Optional format string for the option screen.</param>
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)
/// <summary>
/// Initializes a new instance of the <see cref="ModdedNumberOptionListAttribute"/> class.
/// </summary>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <param name="increment">The increment.</param>
/// <param name="suffixType">The suffix type.</param>
/// <param name="zeroInfinity">Whether zero is infinity or not.</param>
/// <param name="formatString">Optional format string for the option screen.</param>
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<ModdedNumberOption>(value.Count, idx =>
}
internal override IModdedOptionList CreateOptionList(IList list, PropertyInfo property, AbstractOptionGroup group)
{
return new ModdedOptionList<ModdedNumberOption>(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");
}
/// <inheritdoc />
@@ -11,19 +11,22 @@ namespace MiraAPI.GameOptions.Attributes;
/// </summary>
/// <param name="title">The title of the options.</param>
[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);
/// <summary>
/// Gets the title of the options.
/// Gets or sets the title of the options.
/// If the string contains a format parameter, e.g., <c>{0}</c>, 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.
/// </summary>
public string Title => title;
public string? Title { get; protected set; } = title;
/// <summary>
/// Sets the value of all the options.
@@ -67,28 +70,33 @@ public abstract class ModdedOptionListAttribute(string title) : PropertyOptionAt
/// <returns>The value of the option as an object.</returns>
public abstract object GetValue(int idx);
internal abstract IModdedOptionList? CreateOptionList(IList value, PropertyInfo property);
internal abstract IModdedOptionList? CreateOptionList(IList list, PropertyInfo property, AbstractOptionGroup group);
/// <summary>
/// Gets the <see cref="Title"/> formatted with the option's index.
/// </summary>
/// <param name="index">The option's index.</param>
/// <param name="property">The property info for fallback title.</param>
/// <returns>The formatted option's title.</returns>
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;
}
}
@@ -11,16 +11,23 @@ namespace MiraAPI.GameOptions.Attributes;
/// <param name="title">The option title.</param>
/// <param name="allowNone">Whether the option includes a none option. If None is selected, value will return -1.</param>
[AttributeUsage(AttributeTargets.Property)]
public class ModdedPlayerOptionListAttribute(
string title,
bool allowNone = true)
public class ModdedPlayerOptionListAttribute(string? title, bool allowNone = true)
: ModdedOptionListAttribute(title), ITypedOptionList<int>, ITypedOptionList<NetworkedPlayerInfo?>
{
internal override IModdedOptionList CreateOptionList(IList value, PropertyInfo property)
/// <summary>
/// Initializes a new instance of the <see cref="ModdedPlayerOptionListAttribute"/> class.
/// </summary>
/// <param name="allowNone">Whether the option includes a none option. If None is selected, value will return -1.</param>
public ModdedPlayerOptionListAttribute(bool allowNone = true)
: this(null, allowNone)
{
return new ModdedOptionList<ModdedPlayerOption>(value.Count, idx =>
}
internal override IModdedOptionList CreateOptionList(IList list, PropertyInfo property, AbstractOptionGroup group)
{
return new ModdedOptionList<ModdedPlayerOption>(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");
}
/// <inheritdoc />
@@ -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");
}
}
@@ -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;
/// <param name="title">The title of the option.</param>
/// <param name="values">An option list of string value.</param>
[AttributeUsage(AttributeTargets.Property)]
public class ModdedStringOptionListAttribute(
string title,
string[] values)
public class ModdedStringOptionListAttribute(string? title, string[] values)
: ModdedOptionListAttribute(title), ITypedOptionList<string>
{
internal override IModdedOptionList CreateOptionList(IList value, PropertyInfo property)
/// <summary>
/// Initializes a new instance of the <see cref="ModdedStringOptionListAttribute"/> class.
/// </summary>
/// <param name="values">An option list of string value.</param>
public ModdedStringOptionListAttribute(string[] values)
: this(null, values)
{
return new ModdedOptionList<ModdedStringOption>(value.Count, idx =>
}
internal override IModdedOptionList CreateOptionList(IList list, PropertyInfo property, AbstractOptionGroup group)
{
return new ModdedOptionList<ModdedStringOption>(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");
}
}
@@ -12,25 +12,40 @@ namespace MiraAPI.GameOptions.Attributes;
/// <param name="on">What the option displays when toggled on.</param>
/// <param name="off">What the option displays when toggled off.</param>
[AttributeUsage(AttributeTargets.Property)]
public class ModdedToggleOptionListAttribute(
string title,
string on,
string off)
public class ModdedToggleOptionListAttribute(string? title, string on, string off)
: ModdedOptionListAttribute(title), ITypedOptionList<bool>
{
/// <summary>
/// Initializes a new instance of the <see cref="ModdedToggleOptionListAttribute"/> class.
/// </summary>
/// <param name="on">What the option displays when toggled on.</param>
/// <param name="off">What the option displays when toggled off.</param>
public ModdedToggleOptionListAttribute(string on, string off)
: this(null, on, off)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ModdedToggleOptionListAttribute"/> class.
/// </summary>
/// <param name="title">The option's title.</param>
public ModdedToggleOptionListAttribute(string title)
public ModdedToggleOptionListAttribute(string? title)
: this(title, "On", "Off")
{
}
internal override IModdedOptionList CreateOptionList(IList value, PropertyInfo property)
/// <summary>
/// Initializes a new instance of the <see cref="ModdedToggleOptionListAttribute"/> class.
/// </summary>
public ModdedToggleOptionListAttribute()
: this(null, "On", "Off")
{
}
internal override IModdedOptionList CreateOptionList(IList list, PropertyInfo property, AbstractOptionGroup group)
{
var optList = new ModdedOptionList<ModdedToggleOption>(
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.");
}
}
+8 -5
View File
@@ -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);
@@ -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<T> instead. This will be removed in a future version.")]
public class ModdedEnumOption : ModdedOption<int>
{
private readonly int[] underlyingValues;
/// <summary>
/// Gets the string values of the enum.
/// </summary>
public string[] Values { get; }
/// <summary>
/// Gets the String Name values of the option.
/// </summary>
public StringNames[] StringNameValues { get; }
/// <summary>
/// Initializes a new instance of the <see cref="ModdedEnumOption"/> class.
/// </summary>
/// <param name="groupPrefix">The translation ID prefix of the group containing this option.</param>
/// <param name="title">The title of the option.</param>
/// <param name="defaultValue">The default value as an int.</param>
/// <param name="enumType">The <see cref="Enum"/> type.</param>
/// <param name="idPrefix">The common translation ID prefix of all enum values.</param>
/// <param name="values">An option list of string values to use in place of the <see langword="enum"/> name.</param>
/// <param name="includeInPreset">Whether to include this option in the preset or not.</param>
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<StringGameSetting>();
data.Title = StringName;
data.Type = global::OptionTypes.String;
data.Values = StringNameValues;
data.Index = Array.IndexOf(underlyingValues, defaultValue);
Data = data;
}
/// <summary>
/// Initializes a new instance of the <see cref="ModdedEnumOption"/> class.
/// </summary>
/// <param name="groupPrefix">The translation ID prefix of the group containing this option.</param>
/// <param name="title">The title of the option.</param>
/// <param name="defaultValue">The default value as an int.</param>
/// <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>
/// <param name="includeInPreset">Whether to include this option in the preset or not.</param>
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)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ModdedEnumOption"/> class.
/// </summary>
@@ -30,25 +82,29 @@ public class ModdedEnumOption : ModdedOption<int>
/// <param name="values">An option list of string values to use in place of the <see langword="enum"/> name.</param>
/// <param name="includeInPreset">Whether to include this option in the preset or not.</param>
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<StringGameSetting>();
data.Title = StringName;
data.Type = global::OptionTypes.String;
data.Values = Values.Select(MiraLocaleManager.GetOrCreateLocaleString).ToArray();
data.Index = Value;
Data = data;
/// <summary>
/// Initializes a new instance of the <see cref="ModdedEnumOption"/> class.
/// </summary>
/// <param name="title">The title of the option.</param>
/// <param name="defaultValue">The default value as an int.</param>
/// <param name="enumType">The <see cref="Enum"/> type.</param>
/// <param name="idPrefix">The common translation ID prefix of all enum values.</param>
/// <param name="values">An option list of string values to use in place of the <see langword="enum"/> name.</param>
/// <param name="includeInPreset">Whether to include this option in the preset or not.</param>
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)
{
}
/// <inheritdoc />
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<OptionBehaviour>)ValueChanged;
@@ -59,8 +115,8 @@ public class ModdedEnumOption : ModdedOption<int>
// SetUpFromData method doesn't work correctly so we must set the values manually
stringOption.Title = StringName;
stringOption.Values = (Data as StringGameSetting)?.Values ?? new Il2CppStructArray<StringNames>(0);
stringOption.Value = Value;
stringOption.Values = StringNameValues;
stringOption.Value = Array.IndexOf(underlyingValues, Value);
OptionBehaviour = stringOption;
@@ -88,7 +144,12 @@ public class ModdedEnumOption : ModdedOption<int>
/// <inheritdoc />
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;
}
/// <inheritdoc />
@@ -96,7 +157,7 @@ public class ModdedEnumOption : ModdedOption<int>
{
if (OptionBehaviour && OptionBehaviour is StringOption opt)
{
opt.Value = newValue;
opt.Value = Array.IndexOf(underlyingValues, newValue);
}
}
}
@@ -108,11 +169,57 @@ public class ModdedEnumOption : ModdedOption<int>
public class ModdedEnumOption<T> : ModdedOption<T>
where T : struct, Enum
{
private readonly T[] underlyingValues;
/// <summary>
/// Gets the string values of the <see langword="enum"/>.
/// </summary>
public string[] Values { get; }
/// <summary>
/// Gets the String Name values of the option.
/// </summary>
public StringNames[] StringNameValues { get; }
/// <summary>
/// Initializes a new instance of the <see cref="ModdedEnumOption{T}"/> class.
/// </summary>
/// <param name="groupPrefix">The translation ID prefix of the group containing this option.</param>
/// <param name="title">The title of the option.</param>
/// <param name="defaultValue">The default value as an <see langword="int"/>.</param>
/// <param name="idPrefix">The common translation ID prefix of all enum values.</param>
/// <param name="values">An option list of string values to use in place of the <see langword="enum"/> name.</param>
/// <param name="includeInPreset">Whether to include this option in the preset or not.</param>
public ModdedEnumOption(string groupPrefix, string title, T defaultValue, string idPrefix, string[]? values = null, bool includeInPreset = true)
: base(groupPrefix, title, defaultValue, includeInPreset)
{
underlyingValues = Enum.GetValues<T>();
Values = values ?? Enum.GetNames<T>();
StringNameValues = [.. Values.Select(x => MiraLocaleManager.GetOrCreateLocaleString($"{idPrefix}{x}"))];
var data = ScriptableObject.CreateInstance<StringGameSetting>();
data.Title = StringName;
data.Type = global::OptionTypes.String;
data.Values = StringNameValues;
data.Index = Array.IndexOf(underlyingValues, defaultValue);
Data = data;
}
/// <summary>
/// Initializes a new instance of the <see cref="ModdedEnumOption{T}"/> class.
/// </summary>
/// <param name="groupPrefix">The translation ID prefix of the group containing this option.</param>
/// <param name="title">The title of the option.</param>
/// <param name="defaultValue">The default value as an <see langword="int"/>.</param>
/// <param name="values">An option list of string values to use in place of the <see langword="enum"/> name.</param>
/// <param name="includeInPreset">Whether to include this option in the preset or not.</param>
public ModdedEnumOption(string groupPrefix, string title, T defaultValue, string[]? values = null, bool includeInPreset = true)
: this(groupPrefix, title, defaultValue, groupPrefix, values, includeInPreset)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ModdedEnumOption{T}"/> class.
/// </summary>
@@ -121,25 +228,28 @@ public class ModdedEnumOption<T> : ModdedOption<T>
/// <param name="values">An option list of string values to use in place of the <see langword="enum"/> name.</param>
/// <param name="includeInPreset">Whether to include this option in the preset or not.</param>
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<T>();
}
var data = ScriptableObject.CreateInstance<StringGameSetting>();
data.Title = StringName;
data.Type = global::OptionTypes.String;
data.Values = Values.Select(MiraLocaleManager.GetOrCreateLocaleString).ToArray();
data.Index = Convert.ToInt32(Value, NumberFormatInfo.InvariantInfo);
Data = data;
/// <summary>
/// Initializes a new instance of the <see cref="ModdedEnumOption{T}"/> class.
/// </summary>
/// <param name="title">The title of the option.</param>
/// <param name="defaultValue">The default value as an <see langword="int"/>.</param>
/// <param name="idPrefix">The common translation ID prefix of all enum values.</param>
/// <param name="values">An option list of string values to use in place of the <see langword="enum"/> name.</param>
/// <param name="includeInPreset">Whether to include this option in the preset or not.</param>
public ModdedEnumOption(string title, T defaultValue, string idPrefix, string[]? values = null, bool includeInPreset = true)
: this(string.Empty, title, defaultValue, idPrefix, values, includeInPreset)
{
}
/// <inheritdoc />
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<OptionBehaviour>)ValueChanged;
@@ -150,8 +260,8 @@ public class ModdedEnumOption<T> : ModdedOption<T>
// SetUpFromData method doesn't work correctly so we must set the values manually
stringOption.Title = StringName;
stringOption.Values = (Data as StringGameSetting)?.Values ?? new Il2CppStructArray<StringNames>(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<T> : ModdedOption<T>
/// <inheritdoc />
public override NetData GetNetData()
{
return new NetData(Id, Encoding.Unicode.GetBytes(Convert.ToString(Value, NumberFormatInfo.InvariantInfo)!));
var buffer = new byte[Unsafe.SizeOf<T>()];
Unsafe.WriteUnaligned(ref buffer[0], Value);
return new NetData(Id, buffer);
}
/// <inheritdoc />
public override void HandleNetData(byte[] data)
{
SetValue(Enum.Parse<T>(Encoding.Unicode.GetString(data)));
SetValue(Unsafe.ReadUnaligned<T>(ref data[0]));
}
/// <inheritdoc />
@@ -43,33 +43,35 @@ public class ModdedNumberOption : ModdedOption<float>
/// </summary>
public string NegativeWordValue { get; }
/// <summary>
/// Gets a value indicating whether zero is infinity.
/// </summary>
public bool ZeroInfinity => ZeroWordValue == "∞";
/// <summary>
/// Gets a value indicating whether holding shift will divide the increment.
/// </summary>
public bool ShiftIncrement { get; }
/// <summary>
/// Gets a value indicating whether zero is infinity.
/// </summary>
public bool ZeroInfinity => ZeroWordValue == "∞";
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>
/// <param name="groupPrefix">The translation ID prefix of the group containing this option.</param>
/// <param name="title">The title of the option.</param>
/// <param name="defaultValue">The default value as a float.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <param name="increment">The increment.</param>
/// <param name="suffixType">The suffix type.</param>
/// <param name="zeroBehavior">Determines what is shown for zero.</param>
/// <param name="negativeBehavior">Determines what is shown for negative 1.</param>
/// <param name="halfIncrements">Whether increments can be split in half.</param>
/// <param name="suffixType">The suffix type.</param>
/// <param name="formatString">Optional format string for the option screen.</param>
/// <param name="halfIncrements">Whether increments can be split in half.</param>
/// <param name="includeInPreset">Whether to include this option in the preset or not.</param>
public ModdedNumberOption(
string groupPrefix,
string title,
float defaultValue,
float min,
@@ -81,7 +83,7 @@ public class ModdedNumberOption : ModdedOption<float>
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<float>
/// <summary>
/// Initializes a new instance of the <see cref="ModdedNumberOption"/> class.
/// </summary>
/// <param name="groupPrefix">The translation ID prefix of the group containing this option.</param>
/// <param name="title">The title of the option.</param>
/// <param name="defaultValue">The default value as a float.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <param name="increment">The increment.</param>
/// <param name="suffixType">The suffix type.</param>
/// <param name="zeroInfinity">Whether zero is infinity or not.</param>
/// <param name="formatString">Optional format string for the option screen.</param>
/// <param name="zeroInfinity">Whether zero is infinity or not.</param>
/// <param name="includeInPreset">Whether to include this option in the preset or not.</param>
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)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ModdedNumberOption"/> class.
/// </summary>
/// <param name="title">The title of the option.</param>
/// <param name="defaultValue">The default value as a float.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <param name="increment">The increment.</param>
/// <param name="zeroBehavior">Determines what is shown for zero.</param>
/// <param name="negativeBehavior">Determines what is shown for negative 1.</param>
/// <param name="suffixType">The suffix type.</param>
/// <param name="formatString">Optional format string for the option screen.</param>
/// <param name="halfIncrements">Whether increments can be split in half.</param>
/// <param name="includeInPreset">Whether to include this option in the preset or not.</param>
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)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ModdedNumberOption"/> class.
/// </summary>
/// <param name="title">The title of the option.</param>
/// <param name="defaultValue">The default value as a float.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <param name="increment">The increment.</param>
/// <param name="suffixType">The suffix type.</param>
/// <param name="formatString">Optional format string for the option screen.</param>
/// <param name="zeroInfinity">Whether zero is infinity or not.</param>
/// <param name="includeInPreset">Whether to include this option in the preset or not.</param>
public ModdedNumberOption(
string title,
@@ -135,7 +195,7 @@ public class ModdedNumberOption : ModdedOption<float>
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<float>
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<MiraNumberOptionComponent>();
optionComponent.NumberOption = this;
optionComponent.DefaultIncrement = Increment;
+55 -57
View File
@@ -14,33 +14,20 @@ namespace MiraAPI.GameOptions.OptionTypes;
/// Represents a modded option.
/// </summary>
/// <typeparam name="T">The value type.</typeparam>
public abstract class ModdedOption<T> : IModdedOption
/// <param name="groupPrefix">The translation ID prefix of the group containing this option.</param>
/// <param name="title">The option title.</param>
/// <param name="defaultValue">The default value.</param>
/// <param name="includeInPreset">Whether to include the option in the preset.</param>
public abstract class ModdedOption<T>(string groupPrefix, string title, T defaultValue, bool includeInPreset = true) : IModdedOption
{
/// <inheritdoc />
public uint Id { get; }
public uint Id { get; } = ModdedOptionsManager.NextId;
/// <inheritdoc />
public string Title { get; set; }
public string Title { get; set; } = groupPrefix + title;
/// <inheritdoc />
public StringNames StringName { get; }
/// <inheritdoc />
public BaseGameSetting Data { get; protected set; } = null!;
/// <inheritdoc />
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);
/// <summary>
/// Gets or sets the value of the option.
@@ -59,12 +46,24 @@ public abstract class ModdedOption<T> : IModdedOption
}
}
private T _value;
private T _value = defaultValue;
/// <summary>
/// Gets the default value of the option.
/// </summary>
public T DefaultValue { get; }
public T DefaultValue { get; } = defaultValue;
/// <inheritdoc />
public Func<bool> Visible { get; set; } = DelegateUtilities.AlwaysTrue;
/// <inheritdoc />
public bool IncludeInPreset { get; set; } = includeInPreset;
/// <inheritdoc />
public bool ShowNotification { get; set; } = true;
/// <inheritdoc />
public BaseGameSetting Data { get; protected set; } = null!;
/// <inheritdoc cref="IModdedOption.Getter"/>
public Func<T, T>? Getter { get; set; }
@@ -75,15 +74,6 @@ public abstract class ModdedOption<T> : IModdedOption
/// <inheritdoc cref="IModdedOption.ChangedEvent"/>
public Action<T>? ChangedEvent { get; set; }
/// <inheritdoc />
public Func<bool> Visible { get; set; }
/// <inheritdoc />
public bool IncludeInPreset { get; set; }
/// <inheritdoc />
public bool ShowNotification { get; set; } = true;
/// <inheritdoc />
public OptionBehaviour? OptionBehaviour { get; protected set; }
@@ -94,7 +84,18 @@ public abstract class ModdedOption<T> : IModdedOption
public AbstractOptionGroup ParentGroup { get; set; }
/// <inheritdoc />
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;
}
}
/// <inheritdoc />
Delegate? IModdedOption.Getter
@@ -117,6 +118,9 @@ public abstract class ModdedOption<T> : IModdedOption
set => ChangedEvent = value as Action<T>;
}
/// <inheritdoc />
public virtual OptionNotifConfiguration Configuration => new(ParentGroup);
/// <summary>
/// Initializes a new instance of the <see cref="ModdedOption{T}"/> class.
/// </summary>
@@ -124,14 +128,8 @@ public abstract class ModdedOption<T> : IModdedOption
/// <param name="defaultValue">The default value.</param>
/// <param name="includeInPreset">Whether to include the option in the preset.</param>
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<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>
@@ -264,6 +245,23 @@ public abstract class ModdedOption<T> : IModdedOption
PlayerOption playerOpt,
Transform container);
/// <inheritdoc />
public virtual string GetDisplayValue()
{
return Data.GetValueString(GetFloatData());
}
/// <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>
/// Implicitly converts the option to type of <typeparamref name="T"/>.
/// </summary>
@@ -51,6 +51,9 @@ public class ModdedOptionList<T> : IModdedOptionList<T>
/// <returns>option of type <typeparamref name="T"/>.</returns>
public T this[int idx] => Options[idx];
/// <inheritdoc cref="IReadOnlyList{T}.this"/>
IModdedOption IReadOnlyList<IModdedOption>.this[int index] => this[index];
/// <inheritdoc cref="IEnumerable{T}.GetEnumerator"/>
public IEnumerator<T> GetEnumerator()
{
@@ -68,7 +71,4 @@ public class ModdedOptionList<T> : IModdedOptionList<T>
{
return ((IEnumerable<IModdedOption>)Options).GetEnumerator();
}
/// <inheritdoc cref="IReadOnlyList{T}.this"/>
IModdedOption IReadOnlyList<IModdedOption>.this[int index] => this[index];
}
@@ -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<int>
/// <summary>
/// Initializes a new instance of the <see cref="ModdedPlayerOption"/> class.
/// </summary>
/// <param name="groupPrefix">The translation ID prefix of the group containing this option.</param>
/// <param name="title">The title of the option.</param>
/// <param name="allowNone">Whether the option includes a none option. If None is selected, value will return -1.</param>
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<int>
Data = data;
}
/// <summary>
/// Initializes a new instance of the <see cref="ModdedPlayerOption"/> class.
/// </summary>
/// <param name="title">The title of the option.</param>
/// <param name="allowNone">Whether the option includes a none option. If None is selected, value will return -1.</param>
public ModdedPlayerOption(string title, bool allowNone = true)
: this(string.Empty, title, allowNone)
{
}
/// <inheritdoc />
public override OptionBehaviour CreateOption(
ToggleOption toggleOpt,
@@ -63,8 +73,7 @@ public class ModdedPlayerOption : ModdedOption<int>
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<int>
playerOption.OnValueChanged = (Action<OptionBehaviour>)ValueChanged;
playerOption.Title = StringName;
playerOption.TitleText.text = Title.Translate();
playerOption.Values = Values.ToIl2CppList();
playerOption.Value = Value;
playerOption.playerIndex = Values.FindIndex(p => p.PlayerId == Value);
@@ -28,12 +28,13 @@ public class ModdedStringOption : ModdedOption<string>
/// <summary>
/// Initializes a new instance of the <see cref="ModdedStringOption"/> class.
/// </summary>
/// <param name="groupPrefix">The translation ID prefix of the group containing this option.</param>
/// <param name="title">The title of the option.</param>
/// <param name="defaultValue">The default value as a string.</param>
/// <param name="values">An option list of string values to use.</param>
/// <param name="includeInPreset">Whether to include this option in the preset or not.</param>
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<string>
Data = data;
}
/// <summary>
/// Initializes a new instance of the <see cref="ModdedStringOption"/> class.
/// </summary>
/// <param name="title">The title of the option.</param>
/// <param name="defaultValue">The default value as a string.</param>
/// <param name="values">An option list of string values to use.</param>
/// <param name="includeInPreset">Whether to include this option in the preset or not.</param>
public ModdedStringOption(string title, string defaultValue, string[] values, bool includeInPreset = true)
: this(string.Empty, title, defaultValue, values, includeInPreset)
{
}
/// <summary>
/// Gets the string's index.
/// </summary>
@@ -120,8 +133,7 @@ public class ModdedStringOption : ModdedOption<string>
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<OptionBehaviour>)ValueChanged;
@@ -24,13 +24,14 @@ public class ModdedToggleOption : ModdedOption<bool>
/// <summary>
/// Initializes a new instance of the <see cref="ModdedToggleOption"/> class.
/// </summary>
/// <param name="groupPrefix">The translation ID prefix of the group containing this option.</param>
/// <param name="title">The option title.</param>
/// <param name="defaultValue">The default value.</param>
/// <param name="on">What the option displays when toggled on.</param>
/// <param name="off">What the option displays when toggled off.</param>
/// <param name="includeInPreset">Whether to include this option in the preset or not.</param>
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<bool>
Data = data;
}
/// <summary>
/// Initializes a new instance of the <see cref="ModdedToggleOption"/> class.
/// </summary>
/// <param name="groupPrefix">The translation ID prefix of the group containing this option.</param>
/// <param name="title">The option title.</param>
/// <param name="defaultValue">The default value.</param>
/// <param name="includeInPreset">Whether to include this option in the preset or not.</param>
public ModdedToggleOption(string groupPrefix, string title, bool defaultValue, bool includeInPreset = true)
: this(groupPrefix, title, defaultValue, "On", "Off", includeInPreset)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ModdedToggleOption"/> class.
/// </summary>
/// <param name="title">The option title.</param>
/// <param name="defaultValue">The default value.</param>
/// <param name="on">What the option displays when toggled on.</param>
/// <param name="off">What the option displays when toggled off.</param>
/// <param name="includeInPreset">Whether to include this option in the preset or not.</param>
public ModdedToggleOption(string title, bool defaultValue, string on, string off, bool includeInPreset = true)
: this(string.Empty, title, defaultValue, on, off, includeInPreset)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ModdedToggleOption"/> class.
/// </summary>
@@ -49,7 +75,7 @@ public class ModdedToggleOption : ModdedOption<bool>
/// <param name="defaultValue">The default value.</param>
/// <param name="includeInPreset">Whether to include this option in the preset or not.</param>
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<bool>
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<OptionBehaviour>)ValueChanged;