allow enum value filtering + minor improvements

This commit is contained in:
Az committed 2026-11-18 23:31:26 -05:00
1 parent 33fdd86b56
commit 5c2216dfc9
5 files changed
+170 -70

No files matched your search

@@ -76,34 +76,59 @@ public class ModdedEnumOptionAttribute(string? title, Type enumType, string idPr
/// <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>
/// <param name="allowedValues">An option array of enum values to filter down what values can actually be used.</param>
/// <param name="values">An optional array of string values to use in place of the <see langword="enum"/> name.</param>
[AttributeUsage(AttributeTargets.Property)]
public class ModdedEnumOptionAttribute<T>(string? title, string idPrefix, string[]? values = null)
public class ModdedEnumOptionAttribute<T>(string? title, string idPrefix, T[]? allowedValues = null, string[]? values = null)
: ModdedOptionAttribute(title), ITypedOption<T>
where T : struct, Enum
{
/// <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)
private const string ObsoleteMsg = "This constructor is deprecated. Please use the equivalent constructor that includes the 'T[]? allowedValues' parameter. This will be removed in a future version.";
/// <inheritdoc cref="ModdedEnumOptionAttribute{T}(string, string, T[], string[])"/>
public ModdedEnumOptionAttribute(string? title, T[]? allowedValues = null, string[]? values = null)
: this(title, string.Empty, allowedValues, 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)
/// <inheritdoc cref="ModdedEnumOptionAttribute{T}(string, string, T[], string[])"/>
public ModdedEnumOptionAttribute(T[]? allowedValues = null, string[]? values = null, string idPrefix = "")
: this(null, idPrefix, allowedValues, values)
{
}
/// <inheritdoc cref="ModdedEnumOptionAttribute{T}(string, string, T[], string[])"/>
[Obsolete(ObsoleteMsg)]
public ModdedEnumOptionAttribute(string? title, string idPrefix, string[] values)
: this(title, idPrefix, null, values)
{
}
/// <inheritdoc cref="ModdedEnumOptionAttribute{T}(string, string, T[], string[])"/>
[Obsolete(ObsoleteMsg)]
public ModdedEnumOptionAttribute(string? title, string[] values)
: this(title, string.Empty, null, values)
{
}
/// <inheritdoc cref="ModdedEnumOptionAttribute{T}(string, string, T[], string[])"/>
[Obsolete(ObsoleteMsg)]
public ModdedEnumOptionAttribute(string[] values, string idPrefix)
: this(null, idPrefix, null, values)
{
}
/// <inheritdoc cref="ModdedEnumOptionAttribute{T}(string, string, T[], string[])"/>
[Obsolete(ObsoleteMsg)]
public ModdedEnumOptionAttribute(string[] values)
: this(null, string.Empty, null, values)
{
}
/// <inheritdoc />
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);
var opt = new ModdedEnumOption<T>(group.GroupPrefix, Title ?? property.Name, (T)(value ?? 0), idPrefix, allowedValues, values);
Title = opt.Title;
return opt;
}
@@ -78,36 +78,60 @@ public class ModdedEnumOptionListAttribute(string? title, Type enumType, string
/// <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>
/// <param name="allowedValues">An option array of enum values to filter down what values can actually be used.</param>
/// <param name="values">An optional array of string values to use in place of the <see langword="enum"/> name.</param>
[AttributeUsage(AttributeTargets.Property)]
public class ModdedEnumOptionListAttribute<T>(string? title, string idPrefix, string[]? values = null)
public class ModdedEnumOptionListAttribute<T>(string? title, string idPrefix, T[]? allowedValues = null, string[]? values = null)
: ModdedOptionListAttribute(title), ITypedOptionList<T>
where T : struct, Enum
{
/// <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)
private const string ObsoleteMsg = "This constructor is deprecated. Please use the equivalent constructor that includes the 'T[]? allowedValues' parameter. This will be removed in a future version.";
/// <inheritdoc cref="ModdedEnumOptionListAttribute{T}(string, string, T[], string[])"/>
public ModdedEnumOptionListAttribute(string? title, T[]? allowedValues = null, string[]? values = null)
: this(title, string.Empty, allowedValues, 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)
/// <inheritdoc cref="ModdedEnumOptionListAttribute{T}(string, string, T[], string[])"/>
public ModdedEnumOptionListAttribute(T[]? allowedValues = null, string[]? values = null, string idPrefix = "")
: this(null, idPrefix, allowedValues, values)
{
}
/// <inheritdoc cref="ModdedEnumOptionListAttribute{T}(string, string, T[], string[])"/>
[Obsolete(ObsoleteMsg)]
public ModdedEnumOptionListAttribute(string? title, string idPrefix, string[] values)
: this(title, idPrefix, null, values)
{
}
/// <inheritdoc cref="ModdedEnumOptionListAttribute{T}(string, string, T[], string[])"/>
[Obsolete(ObsoleteMsg)]
public ModdedEnumOptionListAttribute(string? title, string[] values)
: this(title, string.Empty, null, values)
{
}
/// <inheritdoc cref="ModdedEnumOptionListAttribute{T}(string, string, T[], string[])"/>
[Obsolete(ObsoleteMsg)]
public ModdedEnumOptionListAttribute(string[] values, string idPrefix)
: this(null, idPrefix, null, values)
{
}
/// <inheritdoc cref="ModdedEnumOptionListAttribute{T}(string, string, T[], string[])"/>
[Obsolete(ObsoleteMsg)]
public ModdedEnumOptionListAttribute(string[] values)
: this(null, string.Empty, null, values)
{
}
/// <inheritdoc />
internal override IBaseModdedOptionList CreateOptionList(IList list, PropertyInfo property, AbstractOptionGroup group)
{
var optList = new ModdedOptionList<ModdedEnumOption<T>>(
list.Count, idx => new(group.GroupPrefix, GetFormattedTitle(idx, property), (T)(list[idx] ?? 0), idPrefix, values));
list.Count, idx => new(group.GroupPrefix, GetFormattedTitle(idx, property), (T)(list[idx] ?? 0), idPrefix, allowedValues, values));
return optList;
}
@@ -124,6 +124,8 @@ public class ModdedEnumOption : ModdedBaseEnumOption<int>
public class ModdedEnumOption<T> : ModdedBaseEnumOption<T>
where T : struct, Enum
{
private const string ObsoleteMsg = "This constructor is deprecated. Please use the equivalent constructor that includes the 'T[]? allowedValues' parameter. This will be removed in a future version.";
/// <summary>
/// Initializes a new instance of the <see cref="ModdedEnumOption{T}"/> class.
/// </summary>
@@ -131,54 +133,103 @@ public class ModdedEnumOption<T> : ModdedBaseEnumOption<T>
/// <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="allowedValues">An option array of enum values to filter down what values can actually be used.</param>
/// <param name="values">An optional array 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)
public ModdedEnumOption(string groupPrefix, string title, T defaultValue, string idPrefix, T[]? allowedValues = null, string[]? values = null, bool includeInPreset = true)
: base(groupPrefix, title, defaultValue, includeInPreset)
{
idPrefix = idPrefix.Replace("%type%", typeof(T).Name);
UnderlyingValues = Enum.GetValues<T>();
Values = values ?? Enum.GetNames<T>();
UnderlyingValues = allowedValues ?? Enum.GetValues<T>();
if (Array.IndexOf(UnderlyingValues, defaultValue) == -1)
{
throw new ArgumentException($"The defaultValue '{defaultValue}' is not present in the allowed enum values.", nameof(defaultValue));
}
if (values != null && values.Length != UnderlyingValues.Length)
{
throw new ArgumentException($"The number of custom values ({values.Length}) must exactly match the number of allowed enum values ({UnderlyingValues.Length}).", nameof(values));
}
Values = values ?? [.. UnderlyingValues.Select(v => v.ToString())];
StringNameValues = [.. Values.Select(x => MiraLocaleManager.GetOrCreateLocaleString($"{idPrefix}{x}"))];
InitializeData(GetIndex(defaultValue));
}
/// <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, string.Empty, values, includeInPreset)
/// <inheritdoc cref="ModdedEnumOption{T}(string, string, T, string, T[], string[], bool)"/>
public ModdedEnumOption(string groupPrefix, string title, T defaultValue, T[]? allowedValues = null, string[]? values = null, bool includeInPreset = true)
: this(groupPrefix, title, defaultValue, string.Empty, allowedValues, values, includeInPreset)
{
}
/// <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="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)
: this(string.Empty, title, defaultValue, values, includeInPreset)
/// <inheritdoc cref="ModdedEnumOption{T}(string, string, T, string, T[], string[], bool)"/>
public ModdedEnumOption(string title, T defaultValue, T[]? allowedValues = null, string[]? values = null, bool includeInPreset = true)
: this(string.Empty, title, defaultValue, string.Empty, allowedValues, values, includeInPreset)
{
}
/// <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 cref="ModdedEnumOption{T}(string, string, T, string, T[], string[], bool)"/>
public ModdedEnumOption(string title, T defaultValue, string idPrefix, T[]? allowedValues = null, string[]? values = null, bool includeInPreset = true)
: this(string.Empty, title, defaultValue, idPrefix, allowedValues, values, includeInPreset)
{
}
/// <inheritdoc cref="ModdedEnumOption{T}(string, string, T, string, T[], string[], bool)"/>
[Obsolete(ObsoleteMsg)]
public ModdedEnumOption(string groupPrefix, string title, T defaultValue, string idPrefix, string[] values, bool includeInPreset)
: this(groupPrefix, title, defaultValue, idPrefix, null, values, includeInPreset)
{
}
/// <inheritdoc cref="ModdedEnumOption{T}(string, string, T, string, T[], string[], bool)"/>
[Obsolete(ObsoleteMsg)]
public ModdedEnumOption(string groupPrefix, string title, T defaultValue, string idPrefix, string[] values)
: this(groupPrefix, title, defaultValue, idPrefix, null, values, true)
{
}
/// <inheritdoc cref="ModdedEnumOption{T}(string, string, T, string, T[], string[], bool)"/>
[Obsolete(ObsoleteMsg)]
public ModdedEnumOption(string groupPrefix, string title, T defaultValue, string[] values, bool includeInPreset)
: this(groupPrefix, title, defaultValue, string.Empty, null, values, includeInPreset)
{
}
/// <inheritdoc cref="ModdedEnumOption{T}(string, string, T, string, T[], string[], bool)"/>
[Obsolete(ObsoleteMsg)]
public ModdedEnumOption(string groupPrefix, string title, T defaultValue, string[] values)
: this(groupPrefix, title, defaultValue, string.Empty, null, values, true)
{
}
/// <inheritdoc cref="ModdedEnumOption{T}(string, string, T, string, T[], string[], bool)"/>
[Obsolete(ObsoleteMsg)]
public ModdedEnumOption(string title, T defaultValue, string[] values, bool includeInPreset)
: this(string.Empty, title, defaultValue, string.Empty, null, values, includeInPreset)
{
}
/// <inheritdoc cref="ModdedEnumOption{T}(string, string, T, string, T[], string[], bool)"/>
[Obsolete(ObsoleteMsg)]
public ModdedEnumOption(string title, T defaultValue, string[] values)
: this(string.Empty, title, defaultValue, string.Empty, null, values, true)
{
}
/// <inheritdoc cref="ModdedEnumOption{T}(string, string, T, string, T[], string[], bool)"/>
[Obsolete(ObsoleteMsg)]
public ModdedEnumOption(string title, T defaultValue, string idPrefix, string[] values, bool includeInPreset)
: this(string.Empty, title, defaultValue, idPrefix, null, values, includeInPreset)
{
}
/// <inheritdoc cref="ModdedEnumOption{T}(string, string, T, string, T[], string[], bool)"/>
[Obsolete(ObsoleteMsg)]
public ModdedEnumOption(string title, T defaultValue, string idPrefix, string[] values)
: this(string.Empty, title, defaultValue, idPrefix, null, values, true)
{
}
@@ -98,13 +98,13 @@ public class ModdedStringOption : ModdedBaseStringOption<string>
/// <inheritdoc />
public override NetData GetNetData()
{
return new NetData(Id, Encoding.Unicode.GetBytes(_value));
return new NetData(Id, BitConverter.GetBytes(GetIndex(_value)));
}
/// <inheritdoc />
public override void HandleNetData(byte[] data)
{
SetValue(Encoding.Unicode.GetString(data));
SetValue(UnderlyingValues[BitConverter.ToInt32(data)]);
}
/// <inheritdoc />
@@ -244,7 +244,7 @@ public sealed class HideAndSeekHudHelper(nint cppPtr) : MonoBehaviour(cppPtr)
{
playerControl.ClearTasks();
PlayerTask.GetOrCreateTask<ImportantTextTask>(playerControl).Text =
DestroyableSingleton<TranslationController>.Instance.GetString(StringNames.HideActionButton);
TranslationController.Instance.GetString(StringNames.HideActionButton);
}
}
@@ -259,7 +259,7 @@ public sealed class HideAndSeekHudHelper(nint cppPtr) : MonoBehaviour(cppPtr)
timerBar.StartFinalHide();
SoundManager.Instance.PlaySound(finalHideAlertSfx, false);
DestroyableSingleton<HudManager>.Instance.SetAlertOverlay(true);
HudManager.Instance.SetAlertOverlay(true);
}
private void AdjustEscapeTimer(float timeDeduction, bool forceDirty)