From 8340ebba4e28f7aad4133e445fa7b124f727cfe1 Mon Sep 17 00:00:00 2001 From: Az <56699741+AlchlcDvl@users.noreply.github.com> Date: Thu, 19 Nov 2026 02:53:28 -0500 Subject: [PATCH] WIP multi select options --- .../Components/FlagsGameSetting.cs | 101 ++++++++++++ MiraAPI/GameOptions/Components/FlagsOption.cs | 51 ++++++ .../OptionTypes/ModdedMultiSelectOption.cs | 148 ++++++++++++++++++ MiraAPI/Resources/Locale/en_US.xml | 3 + MiraAPI/Translation/MiraLocaleManager.cs | 17 ++ MiraAPI/Translation/TranslationExtensions.cs | 11 ++ 6 files changed, 331 insertions(+) create mode 100644 MiraAPI/GameOptions/Components/FlagsGameSetting.cs create mode 100644 MiraAPI/GameOptions/Components/FlagsOption.cs create mode 100644 MiraAPI/GameOptions/OptionTypes/ModdedMultiSelectOption.cs diff --git a/MiraAPI/GameOptions/Components/FlagsGameSetting.cs b/MiraAPI/GameOptions/Components/FlagsGameSetting.cs new file mode 100644 index 00000000..2253d852 --- /dev/null +++ b/MiraAPI/GameOptions/Components/FlagsGameSetting.cs @@ -0,0 +1,101 @@ +using Il2CppInterop.Runtime.InteropTypes.Arrays; +using MiraAPI.Translation; +using Reactor.Utilities.Attributes; + +namespace MiraAPI.GameOptions.Components; + +/// +/// A implementation that supports multi-select options. +/// +/// The C++ pointer to the object. +[RegisterInIl2Cpp] +public class FlagsGameSetting(nint cppPtr) : BaseGameSetting(cppPtr) +{ + /// + /// Gets or sets the value currently held by the setting. + /// + public int Value { get; set; } + + /// + /// Gets or sets the integer value used to denote that all flags have been selected. + /// + public int All { get; set; } + + /// + /// Gets or sets every defined flag within the enum. + /// + public Il2CppStructArray Flags { get; set; } + + /// + /// Gets or sets the string names associated with every flag. + /// + public Il2CppStructArray Values { get; set; } + + /// + /// Gets or sets the string name for when no value is selected. + /// + public StringNames NoneName { get; set; } + + /// + /// Gets or sets the string name for when all values are selected. + /// + public StringNames AllName { get; set; } + + /// + public override string GetValueString(float value) + { + return GetValueString((int)value); + } + + /// + /// Gets a value display string. + /// + /// The enum value as an int. + /// A value string. + public string GetValueString(int value) + { + if (value == 0) + { + return TranslationController.Instance.GetString(NoneName).Translate(); + } + + if (value == All) + { + return TranslationController.Instance.GetString(AllName).Translate(); + } + + var activeCount = 0; + var firstActiveName = StringNames.None; + var foundFirst = false; + + for (var i = 0; i < Flags.Length; i++) + { + var flag = Flags[i]; + + if (flag == 0 || (value & flag) != flag) + continue; + + if (!foundFirst) + { + firstActiveName = Values[i]; + foundFirst = true; + } + + activeCount++; + } + + if (activeCount == 0) + { + return TranslationController.Instance.GetString(NoneName).Translate(); + } + + var firstNameString = TranslationController.Instance.GetString(firstActiveName).Translate(); + + if (activeCount == 1) + { + return firstNameString; + } + + return MiraLocaleManager.GetParsed("MiraApi.Compound", string.Empty, firstNameString, activeCount - 1); + } +} diff --git a/MiraAPI/GameOptions/Components/FlagsOption.cs b/MiraAPI/GameOptions/Components/FlagsOption.cs new file mode 100644 index 00000000..726a394b --- /dev/null +++ b/MiraAPI/GameOptions/Components/FlagsOption.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using Il2CppInterop.Runtime.InteropTypes.Arrays; +using MiraAPI.Translation; +using TMPro; +using UnityEngine; +using UnityEngine.UI; + +namespace MiraAPI.GameOptions.Components; + +/// +/// An option behaviour that acts as a container for dynamically generated flag toggles. +/// +/// The pointer to the C++ object. +[SuppressMessage("Style", "IDE0051:Remove unused private members", Justification = "Unity convention.")] +public class FlagsOption(nint cppPtr) : OptionBehaviour(cppPtr) +{ + public TextMeshPro TitleText; + public TextMeshPro ValueText; + public int Value; + public Il2CppStructArray Values; + + private int oldValue = -1; + + private void Start() + { + Initialize(); + } + + public override int GetInt() + { + return Value; + } + + public override void SetUpFromData(BaseGameSetting data, int maskLayer) + { + base.SetUpFromData(data, maskLayer); + var stringGameSetting = data.TryCast(); + + if (stringGameSetting == null) + { + return; + } + + Title = stringGameSetting.Title; + Value = stringGameSetting.Value; + Values = stringGameSetting.Values; + } +} diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedMultiSelectOption.cs b/MiraAPI/GameOptions/OptionTypes/ModdedMultiSelectOption.cs new file mode 100644 index 00000000..0370f838 --- /dev/null +++ b/MiraAPI/GameOptions/OptionTypes/ModdedMultiSelectOption.cs @@ -0,0 +1,148 @@ +using System; +using System.Globalization; +using System.Linq; +using System.Runtime.CompilerServices; +using MiraAPI.GameOptions.Components; +using MiraAPI.Networking; +using MiraAPI.Translation; +using MiraAPI.Utilities; +using UnityEngine; + +namespace MiraAPI.GameOptions.OptionTypes; + +// TODO: Add documentation once completed +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +public interface IMultiSelectOption +{ + int Value { get; } +} + +public class ModdedMultiSelectOption : ModdedOption, IMultiSelectOption + where T : struct, Enum +{ + /// + /// Gets an array of int values where only a bit is turned on to denote each flag contained within . + /// + public int[] Flags { get; } + + /// + /// Gets the string values of the option. + /// + public string[] Values { get; } = []; + + /// + /// Gets the String Name values of the option. + /// + public StringNames[] StringNameValues { get; } = []; + + public StringNames NoneName { get; } + + public StringNames AllName { get; } + + public int All { get; } + + int IMultiSelectOption.Value => Convert.ToInt32(Value, NumberFormatInfo.InvariantInfo); + + public ModdedMultiSelectOption(string groupPrefix, string title, T defaultValue, string idPrefix, T[]? allowedValues = null, string[]? values = null, string? noneId = null, string? allId = null, bool includeInPreset = true) + : base(groupPrefix, title, defaultValue, includeInPreset) + { + var enumType = typeof(T); + + if (!enumType.IsDefined(false)) + throw new ArgumentException($"{enumType.Name} does not have [Flags], and therefore cannot be used for multi-select options."); + + var targetValues = allowedValues ?? Enum.GetValues(); + + var tuples = targetValues + .Select(x => (Val: x, Bit: Convert.ToInt32(x, NumberFormatInfo.InvariantInfo))) + .Where(val => val.Bit > 0 && (val.Bit & (val.Bit - 1)) == 0) + .DistinctBy(x => x.Bit) + .ToArray(); + + if (values != null && values.Length != tuples.Length) + { + throw new ArgumentException($"The number of custom values ({values.Length}) must exactly match the number of allowed single-bit flags ({tuples.Length}).", nameof(values)); + } + + var all = 0; + + foreach (var (_, bit) in tuples) + { + all |= bit; + } + + All = all; + + var defaultBits = Convert.ToInt32(defaultValue, NumberFormatInfo.InvariantInfo); + + if ((defaultBits & ~All) != 0) + { + throw new ArgumentException($"The defaultValue contains flags that are not present in the allowed subset.", nameof(defaultValue)); + } + + idPrefix = idPrefix.Replace("%type%", enumType.Name); + + Flags = [.. tuples.Select(x => x.Bit)]; + Values = values ?? [.. tuples.Select(x => x.Val.ToString())]; + AllName = MiraLocaleManager.GetOrCreateLocaleString(idPrefix + (allId ?? "All")); + NoneName = MiraLocaleManager.GetOrCreateLocaleString(idPrefix + (noneId ?? "None")); + StringNameValues = [.. Values.Select(x => MiraLocaleManager.GetOrCreateLocaleString(idPrefix + x))]; + + var data = ScriptableObject.CreateInstance(); + data.Type = global::OptionTypes.MultipleChoice; // How nice of Innersloth to provide us with a pre-defined enum value + data.Value = defaultBits; + data.All = All; + data.Flags = Flags; + data.Values = StringNameValues; + + Data = data; + } + + public ModdedMultiSelectOption(string groupPrefix, string title, T defaultValue, T[]? allowedValues = null, string[]? values = null, string? noneId = null, string? allId = null, bool includeInPreset = true) + : this(groupPrefix, title, defaultValue, string.Empty, allowedValues, values, noneId, allId, includeInPreset) + { + } + + public ModdedMultiSelectOption(string title, T defaultValue, string idPrefix, T[]? allowedValues = null, string[]? values = null, string? noneId = null, string? allId = null, bool includeInPreset = true) + : this(string.Empty, title, defaultValue, idPrefix, allowedValues, values, noneId, allId, includeInPreset) + { + } + + public ModdedMultiSelectOption(string title, T defaultValue, T[]? allowedValues = null, string[]? values = null, string? noneId = null, string? allId = null, bool includeInPreset = true) + : this(string.Empty, title, defaultValue, string.Empty, allowedValues, values, noneId, allId, includeInPreset) + { + } + + public override OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOption numberOpt, StringOption stringOpt, PlayerOption playerOpt, Transform container) + { + throw new NotImplementedException(); + } + + public override float GetFloatData() + { + return Convert.ToInt32(_value, NumberFormatInfo.InvariantInfo); + } + + public override NetData GetNetData() + { + var buffer = new byte[Unsafe.SizeOf()]; + Unsafe.WriteUnaligned(ref buffer[0], _value); + return new(Id, buffer); + } + + public override void HandleNetData(byte[] data) + { + SetValue(Unsafe.ReadUnaligned(ref data[0])); + } + + public override T GetValueFromOptionBehaviour(OptionBehaviour optionBehaviour) + { + throw new NotImplementedException(); + } + + protected override void OnValueChanged(T newValue) + { + throw new NotImplementedException(); + } +} diff --git a/MiraAPI/Resources/Locale/en_US.xml b/MiraAPI/Resources/Locale/en_US.xml index 7bca2ae2..34edcf01 100644 --- a/MiraAPI/Resources/Locale/en_US.xml +++ b/MiraAPI/Resources/Locale/en_US.xml @@ -76,6 +76,9 @@ Activated Deactivated unit(s) + All + None + {0} + {1} Crewmates Player Speed diff --git a/MiraAPI/Translation/MiraLocaleManager.cs b/MiraAPI/Translation/MiraLocaleManager.cs index 077431a7..b6ec5c16 100644 --- a/MiraAPI/Translation/MiraLocaleManager.cs +++ b/MiraAPI/Translation/MiraLocaleManager.cs @@ -249,6 +249,23 @@ public static class MiraLocaleManager return GetParsed(language, key, string.Empty, parseList); } + /// + public static string GetParsed( + string key, + string fallback, + params object[] parseList) + { + return GetParsed(CurrentLanguage, key, fallback, parseList); + } + + /// + public static string GetParsed( + string key, + params object[] parseList) + { + return GetParsed(CurrentLanguage, key, string.Empty, parseList); + } + /// /// Builds a formatted translation ID string using a mod ID, a specific ID part, and a suffix. /// diff --git a/MiraAPI/Translation/TranslationExtensions.cs b/MiraAPI/Translation/TranslationExtensions.cs index 8b45b48c..f9133fe0 100644 --- a/MiraAPI/Translation/TranslationExtensions.cs +++ b/MiraAPI/Translation/TranslationExtensions.cs @@ -14,4 +14,15 @@ public static class TranslationExtensions { return MiraLocaleManager.Get(key); } + + /// + /// Translates a key to the current language. + /// + /// The translation key. + /// The values to format the string with. + /// The corresponding translation. + public static string Translate(this string key, params object[] args) + { + return MiraLocaleManager.GetParsed(key, string.Empty, args); + } }