From d5e43a84d302b749a4721dc19e5dd6fa9580368c Mon Sep 17 00:00:00 2001 From: Az <56699741+AlchlcDvl@users.noreply.github.com> Date: Thu, 5 Nov 2026 15:26:40 -0500 Subject: [PATCH] abstract away string option code and also make the behaviour in line with base game handling --- MiraAPI/GameOptions/ModdedOptionsManager.cs | 1 - .../OptionTypes/ModdedBaseStringOption.cs | 125 ++++++++++++ .../OptionTypes/ModdedEnumOption.cs | 191 +++--------------- .../OptionTypes/ModdedStringOption.cs | 110 +--------- MiraAPI/LocalSettings/LocalSettingsTab.cs | 6 +- 5 files changed, 161 insertions(+), 272 deletions(-) create mode 100644 MiraAPI/GameOptions/OptionTypes/ModdedBaseStringOption.cs diff --git a/MiraAPI/GameOptions/ModdedOptionsManager.cs b/MiraAPI/GameOptions/ModdedOptionsManager.cs index 44b69f70..44a684b7 100644 --- a/MiraAPI/GameOptions/ModdedOptionsManager.cs +++ b/MiraAPI/GameOptions/ModdedOptionsManager.cs @@ -39,7 +39,6 @@ public static class ModdedOptionsManager private static readonly Dictionary ConcretisedSetters = []; internal static readonly Dictionary CreatedPlayerOptions = []; - internal static readonly Dictionary CreatedStringOptions = []; internal static readonly Dictionary> GameModeOptionGroups = []; internal static readonly Dictionary ModdedOptions = []; internal static readonly List Groups = []; diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedBaseStringOption.cs b/MiraAPI/GameOptions/OptionTypes/ModdedBaseStringOption.cs new file mode 100644 index 00000000..df08cafc --- /dev/null +++ b/MiraAPI/GameOptions/OptionTypes/ModdedBaseStringOption.cs @@ -0,0 +1,125 @@ +using System; +using UnityEngine; +using Object = UnityEngine.Object; + +namespace MiraAPI.GameOptions.OptionTypes; + +/// +/// A base class for modded options that utilize a string-based interface. +/// +/// The underlying option type. +/// The translation ID prefix of the group containing this option. +/// The title of the option. +/// The default value. +/// Whether to include this option in the preset or not. +public abstract class ModdedBaseStringOption(string groupPrefix, string title, T defaultValue, bool includeInPreset = true) + : ModdedOption(groupPrefix, title, defaultValue, includeInPreset) +{ + /// + /// Gets or sets the string values of the option. + /// + public string[] Values { get; protected set; } = []; + + /// + /// Gets or sets the underlying values of the option. + /// + public T[] UnderlyingValues { get; protected set; } = []; + + /// + /// Gets or sets the String Name values of the option. + /// + public StringNames[] StringNameValues { get; protected set; } = []; + + /// + /// Gets the option type name used for naming the generated GameObject. + /// + protected abstract string OptionTypeName { get; } + + /// + /// Initializes the common StringGameSetting data for this option. + /// + /// The default index to assign. + protected void InitializeData(int defaultIndex) + { + var data = ScriptableObject.CreateInstance(); + data.Title = StringName; + data.Type = global::OptionTypes.String; + data.Values = StringNameValues; + data.Index = defaultIndex; + + Data = data; + } + + /// + /// Gets the index of a specific value. + /// + /// The option's value. + /// The index of the value in the underlying array. + public int GetIndex(T value) + { + var index = Array.IndexOf(UnderlyingValues, value); + + if (index == -1) + Info($"Cannot find a valid index for {value}. Returning -1 instead. Values: {string.Join(", ", Values)}"); + + return index; + } + + /// + public override OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOption numberOpt, StringOption stringOpt, PlayerOption playerOpt, Transform container) + { + var stringOption = Object.Instantiate(stringOpt, container); + stringOption.name = $"{ParentMod!.OptionsTitleText}.{OptionTypeName}.{Title}"; + + stringOption.SetUpFromData(Data, 20); + stringOption.OnValueChanged = (Action)ValueChanged; + + // Changing the button text to make them more distinct from number options + stringOption.MinusBtn.ChangeButtonText("<"); + stringOption.PlusBtn.ChangeButtonText(">"); + + // SetUpFromData method doesn't work correctly so we must set the values manually + stringOption.Title = StringName; + stringOption.Values = StringNameValues; + stringOption.Value = GetIndex(Value); + + OptionBehaviour = stringOption; + + return stringOption; + } + + /// + protected override void OnValueChanged(T newValue) + { + if (OptionBehaviour && OptionBehaviour is StringOption opt) + { + opt.Value = GetIndex(newValue); + } + } + + /// + public override string GetDisplayValue() + { + var index = GetIndex(Value); + return (uint)index < (uint)StringNameValues.Length + ? TranslationController.Instance.GetString(StringNameValues[index]) + : Value!.ToString()!; + } + + /// + public override float GetFloatData() + { + return GetIndex(Value); + } + + /// + public override T GetValueFromOptionBehaviour(OptionBehaviour optionBehaviour) + { + var index = optionBehaviour.GetInt(); + + if ((uint)index < (uint)UnderlyingValues.Length) + return UnderlyingValues[index]; + + return UnderlyingValues.Length > 0 ? UnderlyingValues[0] : default!; + } +} diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedEnumOption.cs b/MiraAPI/GameOptions/OptionTypes/ModdedEnumOption.cs index 6c71a313..3e14f15b 100644 --- a/MiraAPI/GameOptions/OptionTypes/ModdedEnumOption.cs +++ b/MiraAPI/GameOptions/OptionTypes/ModdedEnumOption.cs @@ -4,29 +4,30 @@ using System.Linq; using System.Runtime.CompilerServices; using MiraAPI.Networking; using MiraAPI.Translation; -using UnityEngine; -using Object = UnityEngine.Object; namespace MiraAPI.GameOptions.OptionTypes; +/// +/// The base class for enum options. Exists mainly to get rid of an abstract implementation. +/// +/// The type of the value. +/// The translation ID prefix of the group containing this option. +/// The title of the option. +/// The default value as an int. +/// Whether to include this option in the preset or not. +public abstract class ModdedBaseEnumOption(string groupPrefix, string title, T defaultValue, bool includeInPreset = true) + : ModdedBaseStringOption(groupPrefix, title, defaultValue, includeInPreset) +{ + /// + protected override string OptionTypeName => "EnumOption"; +} + /// /// An option for selecting an value. /// [Obsolete("Use ModdedEnumOption instead. This will be removed in a future version.")] -public class ModdedEnumOption : ModdedOption +public class ModdedEnumOption : ModdedBaseEnumOption { - private readonly int[] underlyingValues; - - /// - /// Gets the string values of the enum. - /// - public string[] Values { get; } - - /// - /// Gets the String Name values of the option. - /// - public StringNames[] StringNameValues { get; } - /// /// Initializes a new instance of the class. /// @@ -41,22 +42,15 @@ public class ModdedEnumOption : ModdedOption : base(groupPrefix, title, defaultValue, includeInPreset) { var rawValues = Enum.GetValues(enumType); - underlyingValues = new int[rawValues.Length]; + UnderlyingValues = new int[rawValues.Length]; for (var i = 0; i < rawValues.Length; i++) - underlyingValues[i] = Convert.ToInt32(rawValues.GetValue(i), NumberFormatInfo.InvariantInfo); + UnderlyingValues[i] = Convert.ToInt32(rawValues.GetValue(i), NumberFormatInfo.InvariantInfo); Values = values ?? Enum.GetNames(enumType); - StringNameValues = [.. Values.Select(x => MiraLocaleManager.GetOrCreateLocaleString($"{idPrefix}{x}"))]; - var data = ScriptableObject.CreateInstance(); - data.Title = StringName; - data.Type = global::OptionTypes.String; - data.Values = StringNameValues; - data.Index = Array.IndexOf(underlyingValues, defaultValue); - - Data = data; + InitializeData(GetIndex(defaultValue)); } /// @@ -100,39 +94,10 @@ public class ModdedEnumOption : ModdedOption { } - /// - public override OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOption numberOpt, StringOption stringOpt, PlayerOption playerOpt, Transform container) - { - var stringOption = Object.Instantiate(stringOpt, container); - stringOption.name = $"{ParentMod!.OptionsTitleText}.EnumOption.{Title}"; - - stringOption.SetUpFromData(Data, 20); - stringOption.OnValueChanged = (Action)ValueChanged; - - // Changing the button text to make them more distinct from number options - stringOption.MinusBtn.ChangeButtonText("<"); - stringOption.PlusBtn.ChangeButtonText(">"); - - // SetUpFromData method doesn't work correctly so we must set the values manually - stringOption.Title = StringName; - stringOption.Values = StringNameValues; - stringOption.Value = Array.IndexOf(underlyingValues, Value); - - OptionBehaviour = stringOption; - - return stringOption; - } - - /// - public override float GetFloatData() - { - return Value; - } - /// public override NetData GetNetData() { - return new NetData(Id, BitConverter.GetBytes(Value)); + return new(Id, BitConverter.GetBytes(Value)); } /// @@ -140,57 +105,15 @@ public class ModdedEnumOption : ModdedOption { SetValue(BitConverter.ToInt32(data)); } - - /// - public override int GetValueFromOptionBehaviour(OptionBehaviour optionBehaviour) - { - var index = optionBehaviour.GetInt(); - - if (index >= 0 && index < underlyingValues.Length) - return underlyingValues[index]; - - return underlyingValues.Length > 0 ? underlyingValues[0] : 0; - } - - /// - protected override void OnValueChanged(int newValue) - { - if (OptionBehaviour && OptionBehaviour is StringOption opt) - { - opt.Value = Array.IndexOf(underlyingValues, newValue); - } - } - - /// - 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); - } } /// /// An option for selecting an value. /// /// The type. -public class ModdedEnumOption : ModdedOption +public class ModdedEnumOption : ModdedBaseEnumOption where T : struct, Enum { - private readonly T[] underlyingValues; - - /// - /// Gets the string values of the . - /// - public string[] Values { get; } - - /// - /// Gets the String Name values of the option. - /// - public StringNames[] StringNameValues { get; } - /// /// Initializes a new instance of the class. /// @@ -203,18 +126,11 @@ public class ModdedEnumOption : ModdedOption public ModdedEnumOption(string groupPrefix, string title, T defaultValue, string idPrefix, string[]? values = null, bool includeInPreset = true) : base(groupPrefix, title, defaultValue, includeInPreset) { - underlyingValues = Enum.GetValues(); - + UnderlyingValues = Enum.GetValues(); Values = values ?? Enum.GetNames(); StringNameValues = [.. Values.Select(x => MiraLocaleManager.GetOrCreateLocaleString($"{idPrefix}{x}"))]; - var data = ScriptableObject.CreateInstance(); - data.Title = StringName; - data.Type = global::OptionTypes.String; - data.Values = StringNameValues; - data.Index = Array.IndexOf(underlyingValues, defaultValue); - - Data = data; + InitializeData(GetIndex(defaultValue)); } /// @@ -255,41 +171,12 @@ public class ModdedEnumOption : ModdedOption { } - /// - public override OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOption numberOpt, StringOption stringOpt, PlayerOption playerOpt, Transform container) - { - var stringOption = Object.Instantiate(stringOpt, container); - stringOption.name = $"{ParentMod!.OptionsTitleText}.EnumOption.{Title}"; - - stringOption.SetUpFromData(Data, 20); - stringOption.OnValueChanged = (Action)ValueChanged; - - // Changing the button text to make them more distinct from number options - stringOption.MinusBtn.ChangeButtonText("<"); - stringOption.PlusBtn.ChangeButtonText(">"); - - // SetUpFromData method doesn't work correctly so we must set the values manually - stringOption.Title = StringName; - stringOption.Values = StringNameValues; - stringOption.Value = Array.IndexOf(underlyingValues, Value); - - OptionBehaviour = stringOption; - - return stringOption; - } - - /// - public override float GetFloatData() - { - return Convert.ToSingle(Value, NumberFormatInfo.InvariantInfo); - } - /// public override NetData GetNetData() { var buffer = new byte[Unsafe.SizeOf()]; Unsafe.WriteUnaligned(ref buffer[0], Value); - return new NetData(Id, buffer); + return new(Id, buffer); } /// @@ -297,34 +184,4 @@ public class ModdedEnumOption : ModdedOption { SetValue(Unsafe.ReadUnaligned(ref data[0])); } - - /// - public override T GetValueFromOptionBehaviour(OptionBehaviour optionBehaviour) - { - var index = optionBehaviour.GetInt(); - - if (index >= 0 && index < underlyingValues.Length) - return underlyingValues[index]; - - return underlyingValues.Length > 0 ? underlyingValues[0] : default; - } - - /// - protected override void OnValueChanged(T newValue) - { - if (OptionBehaviour && OptionBehaviour is StringOption opt) - { - opt.Value = Array.IndexOf(underlyingValues, newValue); - } - } - - /// - public override string GetDisplayValue() - { - var index = Array.IndexOf(underlyingValues, Value); - - return (uint)index >= (uint)StringNameValues.Length - ? TranslationController.Instance.GetString(StringNameValues[index]) - : Value.ToString(); - } } diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedStringOption.cs b/MiraAPI/GameOptions/OptionTypes/ModdedStringOption.cs index 9aa6cfc7..7eee8ba5 100644 --- a/MiraAPI/GameOptions/OptionTypes/ModdedStringOption.cs +++ b/MiraAPI/GameOptions/OptionTypes/ModdedStringOption.cs @@ -2,28 +2,18 @@ using System.Globalization; using System.Linq; using System.Text; -using Il2CppInterop.Runtime.InteropTypes.Arrays; using MiraAPI.Networking; using MiraAPI.Translation; -using UnityEngine; -using Object = UnityEngine.Object; namespace MiraAPI.GameOptions.OptionTypes; /// -/// An option for selecting an enum value. +/// An option for selecting a string value from a predefined list. /// -public class ModdedStringOption : ModdedOption +public class ModdedStringOption : ModdedBaseStringOption { - /// - /// Gets or sets the string values of the option. - /// - public string[] Values { get; set; } - - /// - /// Gets or sets the String Name values of the option. - /// - public StringNames[] StringNameValues { get; set; } + /// + protected override string OptionTypeName => "StringOption"; /// /// Initializes a new instance of the class. @@ -37,15 +27,10 @@ public class ModdedStringOption : ModdedOption : base(groupPrefix, title, defaultValue, includeInPreset) { Values = values; + UnderlyingValues = values; StringNameValues = [.. values.Select(MiraLocaleManager.GetOrCreateLocaleString)]; - var data = ScriptableObject.CreateInstance(); - data.Title = StringName; - data.Type = global::OptionTypes.String; - data.Values = StringNameValues; - data.Index = GetIndex(Value); - - Data = data; + InitializeData(GetIndex(defaultValue)); } /// @@ -61,23 +46,7 @@ public class ModdedStringOption : ModdedOption } /// - /// Gets the string's index. - /// - /// The option's string value. - /// The option's value in the index. - public int GetIndex(string value) - { - if (Values.Contains(value)) - { - return Array.IndexOf(Values, value); - } - - Info($"Cannot find a valid index for {value}. Returning -1 instead. Values: {string.Join(", ", Values)}"); - return -1; - } - - /// - /// Adds a value to the option. + /// Adds a value to the option dynamically. /// /// The option's string value. /// The additional value's index on the list. @@ -91,6 +60,8 @@ public class ModdedStringOption : ModdedOption var newVals = Values.ToList(); newVals.Insert(index, value); Values = [.. newVals]; + UnderlyingValues = Values; + var newStrVals = StringNameValues.ToList(); newStrVals.Insert(index, MiraLocaleManager.GetOrCreateLocaleString(value)); StringNameValues = [.. newStrVals]; @@ -98,7 +69,6 @@ public class ModdedStringOption : ModdedOption if (Data == null) { - Error("Data is null."); return; } @@ -106,57 +76,20 @@ public class ModdedStringOption : ModdedOption data.Values = StringNameValues; data.Index = GetIndex(Value); - Warning("Data is valid"); - if (OptionBehaviour == null) { - Error("OptionBehaviour is null."); return; } var strOpt = OptionBehaviour.TryCast(); - Warning("OptionBehaviour is valid"); - if (strOpt == null) { - Error("StringOption is null."); return; } strOpt.Values = StringNameValues; strOpt.Value = GetIndex(Value); - Warning("StringOption is valid"); - } - - /// - 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.{Title}"; - - stringOption.SetUpFromData(Data, 20); - stringOption.OnValueChanged = (Action)ValueChanged; - - // Changing the button text to make them more distinct from number options - stringOption.MinusBtn.ChangeButtonText("<"); - stringOption.PlusBtn.ChangeButtonText(">"); - - // SetUpFromData method doesn't work correctly so we must set the values manually - stringOption.Title = StringName; - stringOption.Values = (Data as StringGameSetting)?.Values ?? new Il2CppStructArray(0); - stringOption.Value = GetIndex(Value); - - OptionBehaviour = stringOption; - ModdedOptionsManager.CreatedStringOptions.TryAdd(stringOption, this); - - return stringOption; - } - - /// - public override float GetFloatData() - { - return Array.IndexOf(Values, Value); } /// @@ -170,29 +103,4 @@ public class ModdedStringOption : ModdedOption { SetValue(Encoding.Unicode.GetString(data)); } - - /// - public override string GetValueFromOptionBehaviour(OptionBehaviour optionBehaviour) - { - return Values[optionBehaviour.GetInt()]; - } - - /// - protected override void OnValueChanged(string newValue) - { - if (OptionBehaviour && OptionBehaviour is StringOption opt) - { - opt.Value = GetIndex(newValue); - } - } - - /// - public override string GetDisplayValue() - { - var index = Array.IndexOf(Values, Value); - - return (uint)index >= (uint)StringNameValues.Length - ? TranslationController.Instance.GetString(StringNameValues[index]) - : Value; - } } diff --git a/MiraAPI/LocalSettings/LocalSettingsTab.cs b/MiraAPI/LocalSettings/LocalSettingsTab.cs index 5994bdd2..140a2716 100644 --- a/MiraAPI/LocalSettings/LocalSettingsTab.cs +++ b/MiraAPI/LocalSettings/LocalSettingsTab.cs @@ -75,7 +75,7 @@ public abstract class LocalSettingsTab(ConfigFile config) protected int TabIndex => LocalSettingsManager.Tabs.IndexOf(this) + 10; private Scroller? scroller; - private float MaxScroll { get; set; } + private float maxScroll; /// /// Invoked when an option value is changed. @@ -115,7 +115,7 @@ public abstract class LocalSettingsTab(ConfigFile config) return; } - scroller.SetBounds(new FloatRange(0, MaxScroll), new FloatRange(0, 0)); + scroller.SetBounds(new FloatRange(0, maxScroll), new FloatRange(0, 0)); scroller.ScrollToTop(); } @@ -235,7 +235,7 @@ public abstract class LocalSettingsTab(ConfigFile config) const float extraPadding = 0.25f; var itemRange = (Settings.Count + Buttons.Count) * 0.5f - visibleHeight; var offsetRange = contentOffset - visibleHeight + extraPadding; - MaxScroll = Mathf.Max(0f, itemRange, offsetRange); + maxScroll = Mathf.Max(0f, itemRange, offsetRange); return tab; }