diff --git a/MiraAPI/GameOptions/Attributes/ModdedEnumOptionAttribute.cs b/MiraAPI/GameOptions/Attributes/ModdedEnumOptionAttribute.cs index 65fc5366..4ca030f7 100644 --- a/MiraAPI/GameOptions/Attributes/ModdedEnumOptionAttribute.cs +++ b/MiraAPI/GameOptions/Attributes/ModdedEnumOptionAttribute.cs @@ -76,34 +76,59 @@ public class ModdedEnumOptionAttribute(string? title, Type enumType, string idPr /// 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. +/// An option array of enum values to filter down what values can actually be used. +/// An optional array of string values to use in place of the name. [AttributeUsage(AttributeTargets.Property)] -public class ModdedEnumOptionAttribute(string? title, string idPrefix, string[]? values = null) +public class ModdedEnumOptionAttribute(string? title, string idPrefix, T[]? allowedValues = null, string[]? values = null) : ModdedOptionAttribute(title), ITypedOption where T : struct, Enum { - /// - /// 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) + 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."; + + /// + public ModdedEnumOptionAttribute(string? title, T[]? allowedValues = null, string[]? values = null) + : this(title, string.Empty, allowedValues, 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) + /// + public ModdedEnumOptionAttribute(T[]? allowedValues = null, string[]? values = null, string idPrefix = "") + : this(null, idPrefix, allowedValues, values) { } + /// + [Obsolete(ObsoleteMsg)] + public ModdedEnumOptionAttribute(string? title, string idPrefix, string[] values) + : this(title, idPrefix, null, values) + { + } + + /// + [Obsolete(ObsoleteMsg)] + public ModdedEnumOptionAttribute(string? title, string[] values) + : this(title, string.Empty, null, values) + { + } + + /// + [Obsolete(ObsoleteMsg)] + public ModdedEnumOptionAttribute(string[] values, string idPrefix) + : this(null, idPrefix, null, values) + { + } + + /// + [Obsolete(ObsoleteMsg)] + public ModdedEnumOptionAttribute(string[] values) + : this(null, string.Empty, null, 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); + var opt = new ModdedEnumOption(group.GroupPrefix, Title ?? property.Name, (T)(value ?? 0), idPrefix, allowedValues, values); Title = opt.Title; return opt; } diff --git a/MiraAPI/GameOptions/Attributes/OptionList/ModdedEnumOptionListAttribute.cs b/MiraAPI/GameOptions/Attributes/OptionList/ModdedEnumOptionListAttribute.cs index 3539de35..11629dc3 100644 --- a/MiraAPI/GameOptions/Attributes/OptionList/ModdedEnumOptionListAttribute.cs +++ b/MiraAPI/GameOptions/Attributes/OptionList/ModdedEnumOptionListAttribute.cs @@ -78,36 +78,60 @@ 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. +/// An option array of enum values to filter down what values can actually be used. +/// An optional array of string values to use in place of the name. [AttributeUsage(AttributeTargets.Property)] -public class ModdedEnumOptionListAttribute(string? title, string idPrefix, string[]? values = null) +public class ModdedEnumOptionListAttribute(string? title, string idPrefix, T[]? allowedValues = null, string[]? values = null) : ModdedOptionListAttribute(title), ITypedOptionList where T : struct, Enum { - /// - /// 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) + 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."; + + /// + public ModdedEnumOptionListAttribute(string? title, T[]? allowedValues = null, string[]? values = null) + : this(title, string.Empty, allowedValues, 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) + /// + public ModdedEnumOptionListAttribute(T[]? allowedValues = null, string[]? values = null, string idPrefix = "") + : this(null, idPrefix, allowedValues, values) { } + /// + [Obsolete(ObsoleteMsg)] + public ModdedEnumOptionListAttribute(string? title, string idPrefix, string[] values) + : this(title, idPrefix, null, values) + { + } + + /// + [Obsolete(ObsoleteMsg)] + public ModdedEnumOptionListAttribute(string? title, string[] values) + : this(title, string.Empty, null, values) + { + } + + /// + [Obsolete(ObsoleteMsg)] + public ModdedEnumOptionListAttribute(string[] values, string idPrefix) + : this(null, idPrefix, null, values) + { + } + + /// + [Obsolete(ObsoleteMsg)] + public ModdedEnumOptionListAttribute(string[] values) + : this(null, string.Empty, null, values) + { + } + + /// internal override IBaseModdedOptionList CreateOptionList(IList list, PropertyInfo property, AbstractOptionGroup group) { var optList = new ModdedOptionList>( - 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; } diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedEnumOption.cs b/MiraAPI/GameOptions/OptionTypes/ModdedEnumOption.cs index 9d80dac6..1cb60d7c 100644 --- a/MiraAPI/GameOptions/OptionTypes/ModdedEnumOption.cs +++ b/MiraAPI/GameOptions/OptionTypes/ModdedEnumOption.cs @@ -124,6 +124,8 @@ public class ModdedEnumOption : ModdedBaseEnumOption public class ModdedEnumOption : ModdedBaseEnumOption 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."; + /// /// Initializes a new instance of the class. /// @@ -131,54 +133,103 @@ public class ModdedEnumOption : ModdedBaseEnumOption /// 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. + /// An option array of enum values to filter down what values can actually be used. + /// An optional array 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) + 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(); - Values = values ?? Enum.GetNames(); + + UnderlyingValues = allowedValues ?? Enum.GetValues(); + + 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)); } - /// - /// 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, string.Empty, values, includeInPreset) + /// + 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) { } - /// - /// Initializes a new instance of the class. - /// - /// 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 title, T defaultValue, string[]? values = null, bool includeInPreset = true) - : this(string.Empty, title, defaultValue, values, includeInPreset) + /// + 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) { } - /// - /// 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 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) + { + } + + /// + [Obsolete(ObsoleteMsg)] + public ModdedEnumOption(string groupPrefix, string title, T defaultValue, string idPrefix, string[] values, bool includeInPreset) + : this(groupPrefix, title, defaultValue, idPrefix, null, values, includeInPreset) + { + } + + /// + [Obsolete(ObsoleteMsg)] + public ModdedEnumOption(string groupPrefix, string title, T defaultValue, string idPrefix, string[] values) + : this(groupPrefix, title, defaultValue, idPrefix, null, values, true) + { + } + + /// + [Obsolete(ObsoleteMsg)] + public ModdedEnumOption(string groupPrefix, string title, T defaultValue, string[] values, bool includeInPreset) + : this(groupPrefix, title, defaultValue, string.Empty, null, values, includeInPreset) + { + } + + /// + [Obsolete(ObsoleteMsg)] + public ModdedEnumOption(string groupPrefix, string title, T defaultValue, string[] values) + : this(groupPrefix, title, defaultValue, string.Empty, null, values, true) + { + } + + /// + [Obsolete(ObsoleteMsg)] + public ModdedEnumOption(string title, T defaultValue, string[] values, bool includeInPreset) + : this(string.Empty, title, defaultValue, string.Empty, null, values, includeInPreset) + { + } + + /// + [Obsolete(ObsoleteMsg)] + public ModdedEnumOption(string title, T defaultValue, string[] values) + : this(string.Empty, title, defaultValue, string.Empty, null, values, true) + { + } + + /// + [Obsolete(ObsoleteMsg)] + public ModdedEnumOption(string title, T defaultValue, string idPrefix, string[] values, bool includeInPreset) + : this(string.Empty, title, defaultValue, idPrefix, null, values, includeInPreset) + { + } + + /// + [Obsolete(ObsoleteMsg)] + public ModdedEnumOption(string title, T defaultValue, string idPrefix, string[] values) + : this(string.Empty, title, defaultValue, idPrefix, null, values, true) { } diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedStringOption.cs b/MiraAPI/GameOptions/OptionTypes/ModdedStringOption.cs index 55932286..dcda5b5c 100644 --- a/MiraAPI/GameOptions/OptionTypes/ModdedStringOption.cs +++ b/MiraAPI/GameOptions/OptionTypes/ModdedStringOption.cs @@ -98,13 +98,13 @@ public class ModdedStringOption : ModdedBaseStringOption /// public override NetData GetNetData() { - return new NetData(Id, Encoding.Unicode.GetBytes(_value)); + return new NetData(Id, BitConverter.GetBytes(GetIndex(_value))); } /// public override void HandleNetData(byte[] data) { - SetValue(Encoding.Unicode.GetString(data)); + SetValue(UnderlyingValues[BitConverter.ToInt32(data)]); } /// diff --git a/MiraAPI/HnsReimplemented/HideAndSeekHudHelper.cs b/MiraAPI/HnsReimplemented/HideAndSeekHudHelper.cs index 8f2aa513..7d73f59f 100644 --- a/MiraAPI/HnsReimplemented/HideAndSeekHudHelper.cs +++ b/MiraAPI/HnsReimplemented/HideAndSeekHudHelper.cs @@ -244,7 +244,7 @@ public sealed class HideAndSeekHudHelper(nint cppPtr) : MonoBehaviour(cppPtr) { playerControl.ClearTasks(); PlayerTask.GetOrCreateTask(playerControl).Text = - DestroyableSingleton.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.Instance.SetAlertOverlay(true); + HudManager.Instance.SetAlertOverlay(true); } private void AdjustEscapeTimer(float timeDeduction, bool forceDirty)