missing display values and some constructors

This commit is contained in:
Az committed 2026-11-03 00:56:55 -05:00
1 parent 1117d957a3
commit d2859dcddf
4 files changed
+72 -5

No files matched your search

@@ -10,26 +10,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 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 idPrefix, string[]? values = null)
: ModdedOptionListAttribute(title)
{
/// <summary>
/// Initializes a new instance of the <see cref="ModdedEnumOptionListAttribute"/> 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 ModdedEnumOptionListAttribute(string? title, Type enumType, string[]? values = null)
: this(title, enumType, string.Empty, values)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ModdedEnumOptionListAttribute"/> 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 ModdedEnumOptionListAttribute(Type enumType, string idPrefix, string[]? values = null)
: this(null, enumType, idPrefix, values)
{
}
/// <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)
: this(null, enumType, string.Empty, values)
{
}
internal override IModdedOptionList CreateOptionList(IList list, PropertyInfo property, AbstractOptionGroup group)
{
var optList = new ModdedOptionList<ModdedEnumOption>(
list.Count, idx => new(group.GroupPrefix, GetFormattedTitle(idx, property), (int)(list[idx] ?? 0), enumType, values));
list.Count, idx => new(group.GroupPrefix, GetFormattedTitle(idx, property), (int)(list[idx] ?? 0), enumType, idPrefix, values));
return optList;
}
@@ -160,6 +160,16 @@ public class ModdedEnumOption : ModdedOption<int>
opt.Value = Array.IndexOf(underlyingValues, newValue);
}
}
/// <inheritdoc />
public override string GetDisplayValue()
{
var index = Array.IndexOf(underlyingValues, Value);
return (uint)index >= (uint)StringNameValues.Length
? TranslationController.Instance.GetString(StringNameValues[index])
: Value.ToString(CultureInfo.InvariantCulture);
}
}
/// <summary>
@@ -291,7 +301,12 @@ public class ModdedEnumOption<T> : ModdedOption<T>
/// <inheritdoc />
public override T GetValueFromOptionBehaviour(OptionBehaviour optionBehaviour)
{
return Enum.Parse<T>(optionBehaviour.GetInt().ToString(NumberFormatInfo.InvariantInfo));
var index = optionBehaviour.GetInt();
if (index >= 0 && index < underlyingValues.Length)
return underlyingValues[index];
return underlyingValues.Length > 0 ? underlyingValues[0] : default;
}
/// <inheritdoc />
@@ -299,7 +314,17 @@ public class ModdedEnumOption<T> : ModdedOption<T>
{
if (OptionBehaviour && OptionBehaviour is StringOption opt)
{
opt.Value = Convert.ToInt32(newValue, NumberFormatInfo.InvariantInfo);
opt.Value = Array.IndexOf(underlyingValues, newValue);
}
}
/// <inheritdoc />
public override string GetDisplayValue()
{
var index = Array.IndexOf(underlyingValues, Value);
return (uint)index >= (uint)StringNameValues.Length
? TranslationController.Instance.GetString(StringNameValues[index])
: Value.ToString();
}
}
@@ -151,4 +151,13 @@ public class ModdedPlayerOption : ModdedOption<int>
opt.Value = newValue;
}
}
/// <inheritdoc />
public override string GetDisplayValue()
{
var player = GetPlayerValue();
return player
? TranslationController.Instance.GetString(StringNames.None)
: player!.PlayerName;
}
}
@@ -185,4 +185,14 @@ public class ModdedStringOption : ModdedOption<string>
opt.Value = GetIndex(newValue);
}
}
/// <inheritdoc />
public override string GetDisplayValue()
{
var index = Array.IndexOf(Values, Value);
return (uint)index >= (uint)StringNameValues.Length
? TranslationController.Instance.GetString(StringNameValues[index])
: Value;
}
}