mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-20 01:46:15 +00:00
Compare commits
15
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5049d16ac9 | ||
|
|
dccd06eac3 | ||
|
|
2c2a2207c5 | ||
|
|
960573243c | ||
|
|
94bb5a7a37 | ||
|
|
5eba77dba9 | ||
|
|
ef329bfcbd | ||
|
|
a95cac055f | ||
|
|
7909b8f13d | ||
|
|
8ad2e220c9 | ||
|
|
40d88750da | ||
|
|
28636e961d | ||
|
|
5eba1c9d7f | ||
|
|
7cbf9ffe1b | ||
|
|
38ec50bcc7 |
No files matched your search
+2
-1
@@ -6,4 +6,5 @@ bin/
|
|||||||
.qodo/
|
.qodo/
|
||||||
|
|
||||||
*.user
|
*.user
|
||||||
*.lock.json
|
*.lock.json
|
||||||
|
.qodo
|
||||||
@@ -19,11 +19,14 @@ public class ExampleOptions : AbstractOptionGroup
|
|||||||
[ModdedNumberOption("Number Opt", min: 0, max: 10, increment: .25f, formatString: "0.00", suffixType: MiraNumberSuffixes.Percent)]
|
[ModdedNumberOption("Number Opt", min: 0, max: 10, increment: .25f, formatString: "0.00", suffixType: MiraNumberSuffixes.Percent)]
|
||||||
public float NumberOpt { get; set; } = 4f;
|
public float NumberOpt { get; set; } = 4f;
|
||||||
|
|
||||||
[ModdedEnumOption("Best API", typeof(BestApi), ["Mira API", "Mitochondria", "Reactor"])]
|
[ModdedEnumOption("Best API", typeof(AuApis), ["Mira API", "Mitochondria", "Reactor"])]
|
||||||
public BestApi Opt { get; set; } = BestApi.MiraAPI;
|
public AuApis BestApiOpt { get; set; } = AuApis.MiraAPI;
|
||||||
|
|
||||||
|
[ModdedEnumOption<AuApis>("Worst API", ["Mira API", "Mitochondria", "Reactor"])]
|
||||||
|
public AuApis WorstApiOpt { get; set; } = AuApis.Mitochondria;
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum BestApi
|
public enum AuApis
|
||||||
{
|
{
|
||||||
MiraAPI,
|
MiraAPI,
|
||||||
Mitochondria,
|
Mitochondria,
|
||||||
|
|||||||
@@ -23,6 +23,12 @@ public class ExampleOptions2 : AbstractOptionGroup
|
|||||||
{
|
{
|
||||||
ChangedEvent = x => Logger<ExamplePlugin>.Info($"changed Enum Opt to {x}"),
|
ChangedEvent = x => Logger<ExamplePlugin>.Info($"changed Enum Opt to {x}"),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
public ModdedMultiSelectOption<TestingData> MultiSelectOption { get; } = new(
|
||||||
|
"Emotions",
|
||||||
|
TestingData.Happy,
|
||||||
|
TestingData.All,
|
||||||
|
TestingData.None);
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum TestingData : ulong
|
public enum TestingData : ulong
|
||||||
@@ -30,4 +36,6 @@ public enum TestingData : ulong
|
|||||||
Happy,
|
Happy,
|
||||||
Sad,
|
Sad,
|
||||||
Neutral,
|
Neutral,
|
||||||
|
All,
|
||||||
|
None,
|
||||||
}
|
}
|
||||||
@@ -32,3 +32,33 @@ public class ModdedEnumOptionAttribute(string title, Type enumType, string[]? va
|
|||||||
: throw new InvalidOperationException($"HolderOption for option \"{Title}\" with EnumType ${enumType.FullName} is not a ModdedEnumOption");
|
: throw new InvalidOperationException($"HolderOption for option \"{Title}\" with EnumType ${enumType.FullName} is not a ModdedEnumOption");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Generic attribute for creating an enum option.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">An enum type.</typeparam>
|
||||||
|
[AttributeUsage(AttributeTargets.Property)]
|
||||||
|
public class ModdedEnumOptionAttribute<T>(string title, string[]? values = null)
|
||||||
|
: ModdedOptionAttribute(title) where T : struct, Enum
|
||||||
|
{
|
||||||
|
internal override IModdedOption CreateOption(object? value, PropertyInfo property)
|
||||||
|
{
|
||||||
|
var opt = new ModdedEnumOption<T>(Title, (T)(value ?? 0), values);
|
||||||
|
return opt;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void SetValue(object value)
|
||||||
|
{
|
||||||
|
var opt = HolderOption as ModdedEnumOption;
|
||||||
|
opt?.SetValue((int)value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override object GetValue()
|
||||||
|
{
|
||||||
|
return HolderOption is ModdedEnumOption<T> opt
|
||||||
|
? Enum.ToObject(typeof(T), opt.Value)
|
||||||
|
: throw new InvalidOperationException($"HolderOption for option \"{Title}\" is not a ModdedEnumOption<{typeof(T).Name}>");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -92,6 +92,11 @@ public interface IModdedOption
|
|||||||
/// <param name="saveDefault">Indicates whether to save the default value instead of the current value.</param>
|
/// <param name="saveDefault">Indicates whether to save the default value instead of the current value.</param>
|
||||||
void SaveToPreset(ConfigFile presetConfig, bool saveDefault = false);
|
void SaveToPreset(ConfigFile presetConfig, bool saveDefault = false);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Changes the scales and positions of the option if it's attached to the game options menu.
|
||||||
|
/// </summary>
|
||||||
|
void ChangeGameSetting();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Binds the option to a configuration file.
|
/// Binds the option to a configuration file.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -47,11 +47,13 @@ public static class ModdedOptionsManager
|
|||||||
TypeToGroup.Add(type, group);
|
TypeToGroup.Add(type, group);
|
||||||
pluginInfo.InternalOptionGroups.Add(group);
|
pluginInfo.InternalOptionGroups.Add(group);
|
||||||
|
|
||||||
|
#pragma warning disable S3011 // Reflection should not be used to increase accessibility of classes, methods, or fields
|
||||||
typeof(OptionGroupSingleton<>).MakeGenericType(type)
|
typeof(OptionGroupSingleton<>).MakeGenericType(type)
|
||||||
#pragma warning disable S3011
|
#pragma warning disable S3011
|
||||||
.GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic)!
|
.GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic)!
|
||||||
#pragma warning restore S3011
|
#pragma warning restore S3011
|
||||||
.SetValue(null, group);
|
.SetValue(null, group);
|
||||||
|
#pragma warning restore S3011 // Reflection should not be used to increase accessibility of classes, methods, or fields
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,7 +53,11 @@ public class ModdedEnumOption : ModdedOption<int>
|
|||||||
stringOption.SetUpFromData(Data, 20);
|
stringOption.SetUpFromData(Data, 20);
|
||||||
stringOption.OnValueChanged = (Il2CppSystem.Action<OptionBehaviour>)ValueChanged;
|
stringOption.OnValueChanged = (Il2CppSystem.Action<OptionBehaviour>)ValueChanged;
|
||||||
|
|
||||||
// SetUpFromData method doesnt work correctly so we must set the values manually
|
// 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.Title = StringName;
|
||||||
stringOption.Values = (Data as StringGameSetting)?.Values ?? new Il2CppStructArray<StringNames>(0);
|
stringOption.Values = (Data as StringGameSetting)?.Values ?? new Il2CppStructArray<StringNames>(0);
|
||||||
stringOption.Value = Value;
|
stringOption.Value = Value;
|
||||||
@@ -63,6 +67,32 @@ public class ModdedEnumOption : ModdedOption<int>
|
|||||||
return stringOption;
|
return stringOption;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void ChangeGameSetting()
|
||||||
|
{
|
||||||
|
var stringOption = OptionBehaviour as StringOption;
|
||||||
|
|
||||||
|
stringOption!.MinusBtn.transform.localPosition += new Vector3(0.6f, 0f, 0f);
|
||||||
|
stringOption.PlusBtn.transform.localPosition += new Vector3(1.5f, 0f, 0f);
|
||||||
|
|
||||||
|
// The value box itself becomes the button
|
||||||
|
stringOption.PlusBtn.gameObject.SetActive(false);
|
||||||
|
stringOption.MinusBtn.gameObject.SetActive(false);
|
||||||
|
|
||||||
|
var background = stringOption.transform.GetChild(0);
|
||||||
|
background.localPosition += new Vector3(-0.8f, 0f, 0f);
|
||||||
|
background.localScale += new Vector3(1f, 0f, 0f);
|
||||||
|
|
||||||
|
var title = stringOption.TitleText;
|
||||||
|
title.transform.localPosition = new(-2.0466f, 0f, -2.9968f);
|
||||||
|
title.GetComponent<RectTransform>().sizeDelta = new(5.8f, 0.458f);
|
||||||
|
title.fontSize = 2.9f; // Why is it different for string options??
|
||||||
|
|
||||||
|
var valueBox = stringOption.transform.GetChild(5);
|
||||||
|
valueBox.localPosition += new Vector3(1.05f, 0f, 0f);
|
||||||
|
valueBox.localScale += new Vector3(0.2f, 0f, 0f);
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override float GetFloatData()
|
public override float GetFloatData()
|
||||||
{
|
{
|
||||||
@@ -107,7 +137,7 @@ public class ModdedEnumOption : ModdedOption<int>
|
|||||||
/// An option for selecting an enum value.
|
/// An option for selecting an enum value.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The enum type.</typeparam>
|
/// <typeparam name="T">The enum type.</typeparam>
|
||||||
public class ModdedEnumOption<T> : ModdedOption<T> where T : Enum
|
public class ModdedEnumOption<T> : ModdedOption<T> where T : struct, Enum
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the string values of the enum.
|
/// Gets the string values of the enum.
|
||||||
@@ -118,12 +148,12 @@ public class ModdedEnumOption<T> : ModdedOption<T> where T : Enum
|
|||||||
/// Initializes a new instance of the <see cref="ModdedEnumOption{T}"/> class.
|
/// Initializes a new instance of the <see cref="ModdedEnumOption{T}"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="title">The title of the option.</param>
|
/// <param name="title">The title of the option.</param>
|
||||||
/// <param name="defaultValue">The default value as an int.</param>
|
/// <param name="defaultValue">The default value.</param>
|
||||||
/// <param name="values">An option list of string values to use in place of the enum name.</param>
|
/// <param name="values">An option list of string values to use in place of the enum name.</param>
|
||||||
/// <param name="includeInPreset">Whether to include this option in the preset or not.</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) : base(title, defaultValue, includeInPreset)
|
public ModdedEnumOption(string title, T defaultValue, string[]? values = null, bool includeInPreset=true) : base(title, defaultValue, includeInPreset)
|
||||||
{
|
{
|
||||||
Values = values ?? Enum.GetNames(typeof(T));
|
Values = values ?? Enum.GetNames<T>();
|
||||||
Data = ScriptableObject.CreateInstance<StringGameSetting>();
|
Data = ScriptableObject.CreateInstance<StringGameSetting>();
|
||||||
var data = (StringGameSetting)Data;
|
var data = (StringGameSetting)Data;
|
||||||
|
|
||||||
@@ -144,9 +174,13 @@ public class ModdedEnumOption<T> : ModdedOption<T> where T : Enum
|
|||||||
$"{ParentMod!.OptionsTitleText}.EnumOption.{TranslationController.Instance.GetString(StringName)}";
|
$"{ParentMod!.OptionsTitleText}.EnumOption.{TranslationController.Instance.GetString(StringName)}";
|
||||||
|
|
||||||
stringOption.SetUpFromData(Data, 20);
|
stringOption.SetUpFromData(Data, 20);
|
||||||
stringOption.OnValueChanged = (Il2CppSystem.Action<OptionBehaviour>)ValueChanged;
|
stringOption.OnValueChanged = (Action<OptionBehaviour>)ValueChanged;
|
||||||
|
|
||||||
// SetUpFromData method doesnt work correctly so we must set the values manually
|
// 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.Title = StringName;
|
||||||
stringOption.Values = (Data as StringGameSetting)?.Values ?? new Il2CppStructArray<StringNames>(0);
|
stringOption.Values = (Data as StringGameSetting)?.Values ?? new Il2CppStructArray<StringNames>(0);
|
||||||
stringOption.Value = Convert.ToInt32(Value, NumberFormatInfo.InvariantInfo);
|
stringOption.Value = Convert.ToInt32(Value, NumberFormatInfo.InvariantInfo);
|
||||||
@@ -156,6 +190,32 @@ public class ModdedEnumOption<T> : ModdedOption<T> where T : Enum
|
|||||||
return stringOption;
|
return stringOption;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void ChangeGameSetting()
|
||||||
|
{
|
||||||
|
var stringOption = OptionBehaviour as StringOption;
|
||||||
|
|
||||||
|
stringOption!.MinusBtn.transform.localPosition += new Vector3(0.6f, 0f, 0f);
|
||||||
|
stringOption.PlusBtn.transform.localPosition += new Vector3(1.5f, 0f, 0f);
|
||||||
|
|
||||||
|
// The value box itself becomes the button
|
||||||
|
stringOption.PlusBtn.gameObject.SetActive(false);
|
||||||
|
stringOption.MinusBtn.gameObject.SetActive(false);
|
||||||
|
|
||||||
|
var background = stringOption.transform.GetChild(0);
|
||||||
|
background.localPosition += new Vector3(-0.8f, 0f, 0f);
|
||||||
|
background.localScale += new Vector3(1f, 0f, 0f);
|
||||||
|
|
||||||
|
var title = stringOption.TitleText;
|
||||||
|
title.transform.localPosition = new(-2.0466f, 0f, -2.9968f);
|
||||||
|
title.GetComponent<RectTransform>().sizeDelta = new(5.8f, 0.458f);
|
||||||
|
title.fontSize = 2.9f; // Why is it different for string options??
|
||||||
|
|
||||||
|
var valueBox = stringOption.transform.GetChild(5);
|
||||||
|
valueBox.localPosition += new Vector3(1.05f, 0f, 0f);
|
||||||
|
valueBox.localScale += new Vector3(0.2f, 0f, 0f);
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override float GetFloatData()
|
public override float GetFloatData()
|
||||||
{
|
{
|
||||||
@@ -171,19 +231,19 @@ public class ModdedEnumOption<T> : ModdedOption<T> where T : Enum
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void HandleNetData(byte[] data)
|
public override void HandleNetData(byte[] data)
|
||||||
{
|
{
|
||||||
SetValue((T)Enum.Parse(typeof(T), Encoding.Unicode.GetString(data)));
|
SetValue(Enum.Parse<T>(Encoding.Unicode.GetString(data)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override T GetValueFromOptionBehaviour(OptionBehaviour optionBehaviour)
|
public override T GetValueFromOptionBehaviour(OptionBehaviour optionBehaviour)
|
||||||
{
|
{
|
||||||
return (T)Enum.Parse(typeof(T), optionBehaviour.GetInt().ToString(NumberFormatInfo.InvariantInfo));
|
return Enum.Parse<T>(optionBehaviour.GetInt().ToString(NumberFormatInfo.InvariantInfo));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void OnValueChanged(T newValue)
|
protected override void OnValueChanged(T newValue)
|
||||||
{
|
{
|
||||||
HudManager.Instance.Notifier.AddSettingsChangeMessage(StringName, Data.GetValueString(Convert.ToInt32(newValue, NumberFormatInfo.InvariantInfo)), false);
|
HudManager.Instance.Notifier.AddSettingsChangeMessage(StringName, Data!.GetValueString(Convert.ToInt32(newValue, NumberFormatInfo.InvariantInfo)), false);
|
||||||
if (!OptionBehaviour)
|
if (!OptionBehaviour)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -0,0 +1,182 @@
|
|||||||
|
using AmongUs.GameOptions;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using Il2CppInterop.Runtime.InteropTypes.Arrays;
|
||||||
|
using MiraAPI.Modules;
|
||||||
|
using MiraAPI.Networking;
|
||||||
|
using MiraAPI.Utilities;
|
||||||
|
using MiraAPI.Utilities.Assets;
|
||||||
|
using Reactor.Utilities.Extensions;
|
||||||
|
using UnityEngine;
|
||||||
|
using Object = UnityEngine.Object;
|
||||||
|
|
||||||
|
namespace MiraAPI.GameOptions.OptionTypes;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// An option for selecting an enum value.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The enum type.</typeparam>
|
||||||
|
public class ModdedMultiSelectOption<T> : ModdedOption<T> where T : struct, Enum
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the string values of the enum.
|
||||||
|
/// </summary>
|
||||||
|
public string[]? Values { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a value indicating "all of the above".
|
||||||
|
/// </summary>
|
||||||
|
public T All { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a value indicating "none of the above".
|
||||||
|
/// </summary>
|
||||||
|
public T None { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the list of buttons that relate to the current instance.
|
||||||
|
/// </summary>
|
||||||
|
public List<MonoBehaviour> Buttons { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="ModdedMultiSelectOption{T}"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="title">The title of the option.</param>
|
||||||
|
/// <param name="defaultValue">The default value as an int.</param>
|
||||||
|
/// <param name="all">The all value.</param>
|
||||||
|
/// <param name="none">The none value.</param>
|
||||||
|
/// <param name="values">An option list of string values to use in place of the enum name.</param>
|
||||||
|
/// <exception cref="ArgumentException">The enum type doesn't have the flags attribute.</exception>
|
||||||
|
public ModdedMultiSelectOption(string title, T defaultValue, T all, T none, string[]? values = null) : base(title, defaultValue)
|
||||||
|
{
|
||||||
|
var tType = typeof(T);
|
||||||
|
|
||||||
|
if (tType.GetCustomAttribute<FlagsAttribute>() == null)
|
||||||
|
throw new ArgumentException($"{tType.Name} is not a flag enum type");
|
||||||
|
|
||||||
|
Values = values ?? Enum.GetNames<T>();
|
||||||
|
All = all;
|
||||||
|
None = none;
|
||||||
|
Data = ScriptableObject.CreateInstance<MultiSelectGameSetting>();
|
||||||
|
Buttons = [];
|
||||||
|
|
||||||
|
var data = (MultiSelectGameSetting)Data;
|
||||||
|
data.Title = StringName;
|
||||||
|
data.OptionName = Int32OptionNames.Invalid;
|
||||||
|
data.Type = global::OptionTypes.MultipleChoice; // How nice of the game to provide an enum value themselves
|
||||||
|
data.EnumNames = []; // We don't initialise the translation here, we just add one as and when needed
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override OptionBehaviour CreateOption(
|
||||||
|
ToggleOption toggleOpt,
|
||||||
|
NumberOption numberOpt,
|
||||||
|
StringOption stringOpt,
|
||||||
|
PlayerOption playerOpt,
|
||||||
|
Transform container)
|
||||||
|
{
|
||||||
|
var stringOption = Object.Instantiate(stringOpt, container);
|
||||||
|
|
||||||
|
stringOption.SetUpFromData(Data, 20);
|
||||||
|
stringOption.OnValueChanged = (Action<OptionBehaviour>)ValueChanged;
|
||||||
|
|
||||||
|
var toggle = Object.Instantiate(toggleOpt.GetComponentInChildren<PassiveButton>(), stringOption.transform);
|
||||||
|
toggle.name = "Button";
|
||||||
|
toggle.transform.DestroyChildren();
|
||||||
|
toggle.OverrideOnClickListeners(ToggleButtons);
|
||||||
|
|
||||||
|
var box = toggle.GetComponent<BoxCollider2D>();
|
||||||
|
var prevColliderSize = box.size;
|
||||||
|
prevColliderSize.x *= 4.97f;
|
||||||
|
box.size = prevColliderSize;
|
||||||
|
|
||||||
|
var rend = stringOption.transform.GetChild(5).GetComponent<SpriteRenderer>();
|
||||||
|
toggle.OverrideOnMouseOverListeners(() => rend.color = Buttons.Count > 0 ? Color.white : MiraAssets.AcceptedTeal);
|
||||||
|
toggle.OverrideOnMouseOutListeners(() => rend.color = Buttons.Count > 0 ? MiraAssets.AcceptedTeal : Color.white);
|
||||||
|
|
||||||
|
// SetUpFromData method doesn't work correctly so we must set the values manually
|
||||||
|
stringOption.Title = StringName;
|
||||||
|
stringOption.Values = (Data as MultiSelectGameSetting)?.EnumNames?.Values?.ToArray() ?? new Il2CppStructArray<StringNames>(0);
|
||||||
|
stringOption.Value = Convert.ToInt32(Value, NumberFormatInfo.InvariantInfo);
|
||||||
|
|
||||||
|
OptionBehaviour = stringOption;
|
||||||
|
|
||||||
|
return stringOption;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void ChangeGameSetting()
|
||||||
|
{
|
||||||
|
var stringOption = OptionBehaviour as StringOption;
|
||||||
|
|
||||||
|
// The value box itself becomes the button
|
||||||
|
stringOption!.PlusBtn.gameObject.SetActive(false);
|
||||||
|
stringOption.MinusBtn.gameObject.SetActive(false);
|
||||||
|
|
||||||
|
var background = stringOption.transform.GetChild(0);
|
||||||
|
background.localPosition += new Vector3(-0.8f, 0f, 0f);
|
||||||
|
background.localScale += new Vector3(1f, 0f, 0f);
|
||||||
|
|
||||||
|
var title = stringOption.TitleText;
|
||||||
|
title.transform.localPosition = new(-2.0466f, 0f, -2.9968f);
|
||||||
|
title.GetComponent<RectTransform>().sizeDelta = new(5.8f, 0.458f);
|
||||||
|
title.fontSize = 2.9f; // Why is it different for string options??
|
||||||
|
|
||||||
|
var valueBox = stringOption.transform.GetChild(5);
|
||||||
|
valueBox.localPosition += new Vector3(1.05f, 0f, 0f);
|
||||||
|
valueBox.localScale += new Vector3(0.2f, 0f, 0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ToggleButtons()
|
||||||
|
{
|
||||||
|
if (Buttons.Count > 0)
|
||||||
|
{
|
||||||
|
Buttons.ForEach(x => x.gameObject.Destroy());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Implement this
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override float GetFloatData()
|
||||||
|
{
|
||||||
|
return Convert.ToSingle(Value, NumberFormatInfo.InvariantInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override NetData GetNetData()
|
||||||
|
{
|
||||||
|
return new NetData(Id, EnumSerializer.EnumToBytes(Value));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void HandleNetData(byte[] data)
|
||||||
|
{
|
||||||
|
SetValue(EnumSerializer.EnumFromBytes<T>(data, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override T GetValueFromOptionBehaviour(OptionBehaviour optionBehaviour)
|
||||||
|
{
|
||||||
|
return Enum.Parse<T>(optionBehaviour.GetInt().ToString(NumberFormatInfo.InvariantInfo));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void OnValueChanged(T newValue)
|
||||||
|
{
|
||||||
|
HudManager.Instance.Notifier.AddSettingsChangeMessage(StringName, Data!.GetValueString(Convert.ToInt32(newValue, NumberFormatInfo.InvariantInfo)), false);
|
||||||
|
if (!OptionBehaviour)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (OptionBehaviour is StringOption opt)
|
||||||
|
{
|
||||||
|
opt.Value = Convert.ToInt32(newValue, NumberFormatInfo.InvariantInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -128,6 +128,27 @@ public class ModdedNumberOption : ModdedOption<float>
|
|||||||
return numberOption;
|
return numberOption;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void ChangeGameSetting()
|
||||||
|
{
|
||||||
|
var numberOption = OptionBehaviour as NumberOption;
|
||||||
|
|
||||||
|
numberOption!.PlusBtn.transform.localPosition += new Vector3(0.6f, 0f, 0f);
|
||||||
|
numberOption.MinusBtn.transform.localPosition += new Vector3(1.5f, 0f, 0f);
|
||||||
|
|
||||||
|
var background = numberOption.transform.GetChild(0);
|
||||||
|
background.localPosition += new Vector3(-0.8f, 0f, 0f);
|
||||||
|
background.localScale += new Vector3(1f, 0f, 0f);
|
||||||
|
|
||||||
|
var title = numberOption.TitleText;
|
||||||
|
title.transform.localPosition = new(-2.0466f, 0f, -2.9968f);
|
||||||
|
title.GetComponent<RectTransform>().sizeDelta = new(5.8f, 0.458f);
|
||||||
|
|
||||||
|
var valueBox = numberOption.transform.GetChild(5);
|
||||||
|
valueBox.localPosition += new Vector3(1.05f, 0f, 0f);
|
||||||
|
valueBox.localScale += new Vector3(0.2f, 0f, 0f);
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override float GetFloatData()
|
public override float GetFloatData()
|
||||||
{
|
{
|
||||||
@@ -158,7 +179,7 @@ public class ModdedNumberOption : ModdedOption<float>
|
|||||||
Value = Mathf.Clamp(newValue, Min, Max);
|
Value = Mathf.Clamp(newValue, Min, Max);
|
||||||
HudManager.Instance.Notifier.AddSettingsChangeMessage(
|
HudManager.Instance.Notifier.AddSettingsChangeMessage(
|
||||||
StringName,
|
StringName,
|
||||||
Data.GetValueString(Value),
|
Data!.GetValueString(Value),
|
||||||
false);
|
false);
|
||||||
|
|
||||||
if (OptionBehaviour is NumberOption opt)
|
if (OptionBehaviour is NumberOption opt)
|
||||||
|
|||||||
@@ -181,6 +181,23 @@ public abstract class ModdedOption<T> : IModdedOption
|
|||||||
PlayerOption playerOpt,
|
PlayerOption playerOpt,
|
||||||
Transform container);
|
Transform container);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Visually adjust the game option as needed.
|
||||||
|
/// </summary>
|
||||||
|
public abstract void ChangeGameSetting();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Binds the option to the configuration file of the parent mod, if available.
|
||||||
|
/// </summary>
|
||||||
|
public virtual void BindConfig()
|
||||||
|
{
|
||||||
|
var entry = ParentMod?.GetConfigFile().Bind(ConfigDefinition, DefaultValue);
|
||||||
|
if (entry != null)
|
||||||
|
{
|
||||||
|
Value = entry.Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Implicitly converts the option to type of <typeparamref name="T"/>.
|
/// Implicitly converts the option to type of <typeparamref name="T"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -28,6 +28,27 @@ public class ModdedPlayerOption : ModdedOption<int>
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool AllowNone { get; set; }
|
public bool AllowNone { get; set; }
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void ChangeGameSetting()
|
||||||
|
{
|
||||||
|
var numberOption = OptionBehaviour as PlayerOption;
|
||||||
|
|
||||||
|
numberOption!.PlusBtn.transform.localPosition += new Vector3(0.6f, 0f, 0f);
|
||||||
|
numberOption.MinusBtn.transform.localPosition += new Vector3(1.5f, 0f, 0f);
|
||||||
|
|
||||||
|
var background = numberOption.transform.GetChild(0);
|
||||||
|
background.localPosition += new Vector3(-0.8f, 0f, 0f);
|
||||||
|
background.localScale += new Vector3(1f, 0f, 0f);
|
||||||
|
|
||||||
|
var title = numberOption.TitleText;
|
||||||
|
title.transform.localPosition = new(-2.0466f, 0f, -2.9968f);
|
||||||
|
title.GetComponent<RectTransform>().sizeDelta = new(5.8f, 0.458f);
|
||||||
|
|
||||||
|
var valueBox = numberOption.transform.GetChild(5);
|
||||||
|
valueBox.localPosition += new Vector3(1.05f, 0f, 0f);
|
||||||
|
valueBox.localScale += new Vector3(0.2f, 0f, 0f);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="ModdedPlayerOption"/> class.
|
/// Initializes a new instance of the <see cref="ModdedPlayerOption"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -44,6 +44,21 @@ public class ModdedToggleOption : ModdedOption<bool>
|
|||||||
return toggleOption;
|
return toggleOption;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void ChangeGameSetting()
|
||||||
|
{
|
||||||
|
var toggleOption = OptionBehaviour as ToggleOption;
|
||||||
|
toggleOption!.transform.GetChild(1).localPosition += new Vector3(2.2f, 0f, 0f); // Checkbox
|
||||||
|
|
||||||
|
var title = toggleOption.TitleText;
|
||||||
|
title.transform.localPosition = new(-2.0466f, 0f, -2.9968f);
|
||||||
|
title.GetComponent<RectTransform>().sizeDelta = new(5.8f, 0.458f);
|
||||||
|
|
||||||
|
var background = toggleOption.transform.GetChild(2);
|
||||||
|
background.localPosition += new Vector3(-0.8f, 0f, 0f);
|
||||||
|
background.localScale += new Vector3(1f, 0f, 0f);
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override float GetFloatData()
|
public override float GetFloatData()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
global using static Reactor.Utilities.Logger<MiraAPI.MiraApiPlugin>;
|
global using static Reactor.Utilities.Logger<MiraAPI.MiraApiPlugin>;
|
||||||
using BepInEx;
|
using BepInEx;
|
||||||
|
using BepInEx.Configuration;
|
||||||
using BepInEx.Unity.IL2CPP;
|
using BepInEx.Unity.IL2CPP;
|
||||||
using HarmonyLib;
|
using HarmonyLib;
|
||||||
using MiraAPI.PluginLoading;
|
using MiraAPI.PluginLoading;
|
||||||
@@ -31,6 +32,7 @@ public partial class MiraApiPlugin : BasePlugin
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static Color DefaultHeaderColor { get; } = new Color32(77, 77, 77, 255);
|
public static Color DefaultHeaderColor { get; } = new Color32(77, 77, 77, 255);
|
||||||
|
|
||||||
|
internal static ConfigEntry<string>? DistanceSuffix { get; private set; }
|
||||||
private static MiraPluginManager? PluginManager { get; set; }
|
private static MiraPluginManager? PluginManager { get; set; }
|
||||||
internal Harmony Harmony { get; } = new(Id);
|
internal Harmony Harmony { get; } = new(Id);
|
||||||
|
|
||||||
@@ -41,6 +43,8 @@ public partial class MiraApiPlugin : BasePlugin
|
|||||||
|
|
||||||
ReactorCredits.Register("Mira API", Version, true, ReactorCredits.AlwaysShow);
|
ReactorCredits.Register("Mira API", Version, true, ReactorCredits.AlwaysShow);
|
||||||
|
|
||||||
|
DistanceSuffix = Config.Bind("Display", "Distance", "unit(s)", "Dictates how a distance is shown in numeric option displays");
|
||||||
|
|
||||||
PluginManager = new MiraPluginManager();
|
PluginManager = new MiraPluginManager();
|
||||||
PluginManager.Initialize();
|
PluginManager.Initialize();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace MiraAPI.Modules;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Denotes a failure in conversion of values.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="message">The message to log.</param>
|
||||||
|
public class ConversionFailException(string message) : Exception(message);
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
// TODO: Remove later before completing the pr
|
||||||
|
#pragma warning disable
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using AmongUs.GameOptions;
|
||||||
|
using Il2CppInterop.Runtime.Attributes;
|
||||||
|
using Il2CppInterop.Runtime.InteropTypes.Arrays;
|
||||||
|
using Reactor.Utilities.Attributes;
|
||||||
|
|
||||||
|
namespace MiraAPI.Modules;
|
||||||
|
|
||||||
|
[RegisterInIl2Cpp, Serializable]
|
||||||
|
public class MultiSelectGameSetting : BaseGameSetting
|
||||||
|
{
|
||||||
|
public Int32OptionNames OptionName;
|
||||||
|
public Dictionary<int, StringNames> EnumNames;
|
||||||
|
|
||||||
|
public override string GetValueString(float value) => TranslationController.Instance.GetString(EnumNames[(int)value]);
|
||||||
|
}
|
||||||
@@ -0,0 +1,202 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using MiraAPI.Utilities;
|
||||||
|
|
||||||
|
namespace MiraAPI.Modules;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a collection of enum values that can be stored and manipulated as a comma-separated string.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The enum type that this collection will store.</typeparam>
|
||||||
|
/// <param name="values">One or more enum values to initialize the collection with.</param>
|
||||||
|
[Serializable]
|
||||||
|
public struct MultiSelectValue<T>(params T[] values) : IEnumerable<T>, IEquatable<MultiSelectValue<T>>, IDisposable where T : struct, Enum
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the underlying comma-separated string representation of enum values.
|
||||||
|
/// </summary>
|
||||||
|
public string Values
|
||||||
|
{
|
||||||
|
readonly get => Helpers.Join(',', _values);
|
||||||
|
set => _values = [.. value.TrueSplit(',').Select(Enum.Parse<T>)];
|
||||||
|
}
|
||||||
|
private HashSet<T> _values = [.. values];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the number of values contained within.
|
||||||
|
/// </summary>
|
||||||
|
public readonly int Count => _values.Count;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a single enum value to the end of the collection.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item">The enum value to add.</param>
|
||||||
|
/// <returns><c>true</c> if successfully added; otherwise, <c>false</c>.</returns>
|
||||||
|
public readonly bool Add(T item) => _values.Add(item);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes the first occurrence of the specified enum value.<br/>
|
||||||
|
/// Returns <c>true</c> if the value was found and removed, <c>false</c> otherwise.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item">The enum value to remove.</param>
|
||||||
|
/// <returns><c>true</c> if successfully removed; otherwise, <c>false</c>.</returns>
|
||||||
|
public readonly bool Remove(T item) => _values.Remove(item);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds multiple enum values to the end of the collection.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="items">The collection of enum values to add.</param>
|
||||||
|
public readonly void AddRange(IEnumerable<T> items)
|
||||||
|
{
|
||||||
|
foreach (var item in items)
|
||||||
|
_values.Add(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes multiple enum values from the collection.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="items">The enum values to remove.</param>
|
||||||
|
/// <returns>The number of items successfully removed.</returns>
|
||||||
|
public readonly int RemoveRange(params T[] items) => RemoveRange((IEnumerable<T>)items);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes multiple enum values from the collection.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="items">The collection of enum values to remove.</param>
|
||||||
|
/// <returns>The number of items successfully removed.</returns>
|
||||||
|
public readonly int RemoveRange(IEnumerable<T> items) => items.Count(Remove);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines whether the collection contains the specified enum value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item">The enum value to locate.</param>
|
||||||
|
/// <returns>true if the item is found in the collection; otherwise, false.</returns>
|
||||||
|
public readonly bool Contains(T item) => _values.Contains(item);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes all enum values that match the specified predicate condition.<br/>
|
||||||
|
/// Iterates through the collection and removes each matching value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="predicate">A function that defines the condition for removing items.</param>
|
||||||
|
/// <returns>The number of items removed from the collection.</returns>
|
||||||
|
public readonly int RemoveAll(Func<T, bool> predicate)
|
||||||
|
{
|
||||||
|
var count = 0;
|
||||||
|
|
||||||
|
foreach (var item in this)
|
||||||
|
{
|
||||||
|
if (predicate(item) && Remove(item))
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes all enum values from the collection.
|
||||||
|
/// </summary>
|
||||||
|
public readonly void Clear() => _values.Clear();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the first value in the collection.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Returns the first value.</returns>
|
||||||
|
public readonly T First() => _values.First();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Serializes the current instance to an array of bytes.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>An array of bytes representing the current instance.</returns>
|
||||||
|
public readonly byte[] ToBytes() => EnumSerializer.EnumsToBytes(this);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts the collection to its string representation.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A comma-separated string of enum values.</returns>
|
||||||
|
public readonly override string ToString() => Values;
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public readonly override bool Equals(object? obj) => obj is MultiSelectValue<T> other && Equals(other);
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public readonly bool Equals(MultiSelectValue<T> other) => _values.SetEquals(other._values);
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public readonly override int GetHashCode() => Values.GetHashCode();
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public readonly IEnumerator<T> GetEnumerator() => ((IEnumerable<T>)_values).GetEnumerator();
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
readonly IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public readonly void Dispose() => _values.Clear();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates an instance of <see cref="MultiSelectValue{T}"/> from the provided byte array.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bytes">The bytes to deserialize from.</param>
|
||||||
|
/// <returns>An instance of <see cref="MultiSelectValue{T}"/>.</returns>
|
||||||
|
#pragma warning disable CA1000 // Do not declare static members on generic types
|
||||||
|
public static MultiSelectValue<T> FromBytes(byte[] bytes) => new([.. EnumSerializer.EnumsFromBytes<T>(bytes)]);
|
||||||
|
#pragma warning restore CA1000 // Do not declare static members on generic types
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts the current instance to its string representation.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The value to convert.</param>
|
||||||
|
public static implicit operator string(MultiSelectValue<T> value) => value.Values;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts the current instance to its array representation of values.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The value to convert.</param>
|
||||||
|
public static implicit operator T[](MultiSelectValue<T> value) => [.. value._values];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts the current instance to one singular value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The value to convert.</param>
|
||||||
|
/// <exception cref="ConversionFailException">There were either no values, or more than one value.</exception>
|
||||||
|
#pragma warning disable S3877 // Exceptions should not be thrown from unexpected methods
|
||||||
|
public static implicit operator T(MultiSelectValue<T> value) =>
|
||||||
|
value.Count == 1
|
||||||
|
? value._values.First()
|
||||||
|
: throw new ConversionFailException($"Tried to convert multiple or no values ({value.Values}) to a singular one");
|
||||||
|
#pragma warning restore S3877 // Exceptions should not be thrown from unexpected methods
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts the value to an instance of MultiSelectValue.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The value to convert.</param>
|
||||||
|
public static implicit operator MultiSelectValue<T>(T value) => new(value);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts the array of values to an instance of MultiSelectValue.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="values">The values to convert.</param>
|
||||||
|
public static implicit operator MultiSelectValue<T>(T[] values) => new(values);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts the value string to an instance of MultiSelectValue.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="values">The values to convert.</param>
|
||||||
|
public static implicit operator MultiSelectValue<T>(string values) => new([.. values.TrueSplit(',').Select(Enum.Parse<T>)]);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Equality comparison.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="left">Left.</param>
|
||||||
|
/// <param name="right">Right.</param>
|
||||||
|
public static bool operator ==(MultiSelectValue<T> left, MultiSelectValue<T> right) => left.Equals(right);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Inequality comparison.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="left">Left.</param>
|
||||||
|
/// <param name="right">Right.</param>
|
||||||
|
public static bool operator !=(MultiSelectValue<T> left, MultiSelectValue<T> right) => !left.Equals(right);
|
||||||
|
}
|
||||||
@@ -284,6 +284,9 @@ internal static class GameOptionsMenuPatch
|
|||||||
menu.playerOptionOrigin,
|
menu.playerOptionOrigin,
|
||||||
menu.settingsContainer));
|
menu.settingsContainer));
|
||||||
|
|
||||||
|
// TODO: Reimplement this without breaking the menu.
|
||||||
|
// group.Options.ForEach(x => x.ChangeGameSetting());
|
||||||
|
|
||||||
foreach (var newOpt in options)
|
foreach (var newOpt in options)
|
||||||
{
|
{
|
||||||
newOpt.SetClickMask(menu.ButtonClickMask);
|
newOpt.SetClickMask(menu.ButtonClickMask);
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ public static class SettingPatches
|
|||||||
MiraNumberSuffixes.None => value.ToString(__instance.FormatString, NumberFormatInfo.InvariantInfo),
|
MiraNumberSuffixes.None => value.ToString(__instance.FormatString, NumberFormatInfo.InvariantInfo),
|
||||||
MiraNumberSuffixes.Multiplier => value.ToString(__instance.FormatString, NumberFormatInfo.InvariantInfo) + "x",
|
MiraNumberSuffixes.Multiplier => value.ToString(__instance.FormatString, NumberFormatInfo.InvariantInfo) + "x",
|
||||||
MiraNumberSuffixes.Percent => value.ToString(__instance.FormatString, NumberFormatInfo.InvariantInfo) + "%",
|
MiraNumberSuffixes.Percent => value.ToString(__instance.FormatString, NumberFormatInfo.InvariantInfo) + "%",
|
||||||
|
MiraNumberSuffixes.Distance => value.ToString(__instance.FormatString, NumberFormatInfo.InvariantInfo) + MiraApiPlugin.DistanceSuffix.Value,
|
||||||
_ => TranslationController.Instance.GetString(
|
_ => TranslationController.Instance.GetString(
|
||||||
StringNames.GameSecondsAbbrev,
|
StringNames.GameSecondsAbbrev,
|
||||||
(Il2CppSystem.Object[])[value.ToString(__instance.FormatString, CultureInfo.InvariantCulture)]),
|
(Il2CppSystem.Object[])[value.ToString(__instance.FormatString, CultureInfo.InvariantCulture)]),
|
||||||
|
|||||||
@@ -0,0 +1,145 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace MiraAPI.Utilities;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Enum serializer for converting enums to and from byte arrays.
|
||||||
|
/// </summary>
|
||||||
|
public static class EnumSerializer
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Serializes an enum to an array of bytes.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The enum value to serialize.</param>
|
||||||
|
/// <typeparam name="T">The enum type.</typeparam>
|
||||||
|
/// <returns>A byte array representing the enum value.</returns>
|
||||||
|
/// <exception cref="ArgumentException">Thrown when the underlying type is not recognized.</exception>
|
||||||
|
public static byte[] EnumToBytes<T>(T value) where T : struct, Enum
|
||||||
|
{
|
||||||
|
var underlyingType = typeof(T).GetEnumUnderlyingType();
|
||||||
|
|
||||||
|
return underlyingType switch
|
||||||
|
{
|
||||||
|
not null when underlyingType == typeof(byte) => [(byte)(object)value],
|
||||||
|
not null when underlyingType == typeof(sbyte) => [(byte)(sbyte)(object)value],
|
||||||
|
not null when underlyingType == typeof(short) => BitConverter.GetBytes((short)(object)value),
|
||||||
|
not null when underlyingType == typeof(ushort) => BitConverter.GetBytes((ushort)(object)value),
|
||||||
|
not null when underlyingType == typeof(int) => BitConverter.GetBytes((int)(object)value),
|
||||||
|
not null when underlyingType == typeof(uint) => BitConverter.GetBytes((uint)(object)value),
|
||||||
|
not null when underlyingType == typeof(long) => BitConverter.GetBytes((long)(object)value),
|
||||||
|
not null when underlyingType == typeof(ulong) => BitConverter.GetBytes((ulong)(object)value),
|
||||||
|
_ => throw new ArgumentException("Unknown underlying type for " + typeof(T).Name),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts bytes to an enum using the underlying type.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bytes">The byte array to convert.</param>
|
||||||
|
/// <param name="offset">The offset in the byte array to start reading from.</param>
|
||||||
|
/// <param name="underlyingType">The underlying type of the enum.</param>
|
||||||
|
/// <typeparam name="T">The enum type.</typeparam>
|
||||||
|
/// <returns>The enum value represented by the bytes.</returns>
|
||||||
|
/// <exception cref="ArgumentException">Thrown when the underlying type is not recognized.</exception>
|
||||||
|
public static T EnumFromBytes<T>(byte[] bytes, int offset, Type underlyingType) where T : struct, Enum
|
||||||
|
{
|
||||||
|
return underlyingType switch
|
||||||
|
{
|
||||||
|
{ } t when t == typeof(byte) || t == typeof(sbyte) => (T)(object)bytes[offset],
|
||||||
|
{ } t when t == typeof(short) => (T)(object)BitConverter.ToInt16(bytes, offset),
|
||||||
|
{ } t when t == typeof(ushort) => (T)(object)BitConverter.ToUInt16(bytes, offset),
|
||||||
|
{ } t when t == typeof(int) => (T)(object)BitConverter.ToInt32(bytes, offset),
|
||||||
|
{ } t when t == typeof(uint) => (T)(object)BitConverter.ToUInt32(bytes, offset),
|
||||||
|
{ } t when t == typeof(long) => (T)(object)BitConverter.ToInt64(bytes, offset),
|
||||||
|
{ } t when t == typeof(ulong) => (T)(object)BitConverter.ToUInt64(bytes, offset),
|
||||||
|
_ => throw new ArgumentException("Unknown underlying type for " + typeof(T).Name),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="EnumFromBytes{T}(byte[], int, Type)"/>
|
||||||
|
public static T EnumFromBytes<T>(byte[] bytes, int offset) where T : struct, Enum => EnumFromBytes<T>(bytes, offset, typeof(T).GetEnumUnderlyingType());
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts a byte array into a collection of enum values.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bytes">The byte array to convert.</param>
|
||||||
|
/// <typeparam name="T">The enum type.</typeparam>
|
||||||
|
/// <returns>An enumerable collection of enum values.</returns>
|
||||||
|
/// <exception cref="ArgumentException">Thrown when the byte array length is not a multiple of the enum's underlying type size.</exception>
|
||||||
|
public static IEnumerable<T> EnumsFromBytes<T>(byte[] bytes) where T : struct, Enum
|
||||||
|
{
|
||||||
|
var underlyingType = typeof(T).GetEnumUnderlyingType();
|
||||||
|
var size = GetManagedSize(underlyingType);
|
||||||
|
|
||||||
|
if (bytes.Length % size != 0)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Byte array length must be a multiple of " + size + ".");
|
||||||
|
}
|
||||||
|
|
||||||
|
return EnumsFromBytesIterator<T>(bytes, underlyingType, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IEnumerable<T> EnumsFromBytesIterator<T>(byte[] bytes, Type underlyingType, int size) where T : struct, Enum
|
||||||
|
{
|
||||||
|
var count = bytes.Length / size;
|
||||||
|
var underlyingArray = Array.CreateInstance(underlyingType, count);
|
||||||
|
Buffer.BlockCopy(bytes, 0, underlyingArray, 0, bytes.Length);
|
||||||
|
|
||||||
|
foreach (var value in underlyingArray)
|
||||||
|
yield return (T)value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts a collection of enum values to a byte array.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="values"> The collection of enum values to convert.</param>
|
||||||
|
/// <typeparam name="T">The enum type.</typeparam>
|
||||||
|
/// <returns>A byte array representing the collection of enum values.</returns>
|
||||||
|
public static byte[] EnumsToBytes<T>(IEnumerable<T> values) where T : struct, Enum
|
||||||
|
{
|
||||||
|
var underlyingType = typeof(T).GetEnumUnderlyingType();
|
||||||
|
var size = GetManagedSize(underlyingType);
|
||||||
|
var valuesArray = values.ToArray();
|
||||||
|
var count = valuesArray.Length;
|
||||||
|
|
||||||
|
var underlyingArray = Array.CreateInstance(underlyingType, count);
|
||||||
|
for (var i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
underlyingArray.SetValue(ConvertToUnderlyingType(valuesArray[i], underlyingType), i);
|
||||||
|
}
|
||||||
|
|
||||||
|
var bytes = new byte[count * size];
|
||||||
|
Buffer.BlockCopy(underlyingArray, 0, bytes, 0, bytes.Length);
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static object ConvertToUnderlyingType<T>(T value, Type underlyingType) where T : struct, Enum
|
||||||
|
{
|
||||||
|
return underlyingType switch
|
||||||
|
{
|
||||||
|
not null when underlyingType == typeof(byte) => (byte)(object)value,
|
||||||
|
not null when underlyingType == typeof(sbyte) => (sbyte)(object)value,
|
||||||
|
not null when underlyingType == typeof(short) => (short)(object)value,
|
||||||
|
not null when underlyingType == typeof(ushort) => (ushort)(object)value,
|
||||||
|
not null when underlyingType == typeof(int) => (int)(object)value,
|
||||||
|
not null when underlyingType == typeof(uint) => (uint)(object)value,
|
||||||
|
not null when underlyingType == typeof(long) => (long)(object)value,
|
||||||
|
not null when underlyingType == typeof(ulong) => (ulong)(object)value,
|
||||||
|
_ => throw new InvalidOperationException("Unsupported underlying type."),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int GetManagedSize(Type type)
|
||||||
|
{
|
||||||
|
return Type.GetTypeCode(type) switch
|
||||||
|
{
|
||||||
|
TypeCode.Byte or TypeCode.SByte => 1,
|
||||||
|
TypeCode.Int16 or TypeCode.UInt16 => 2,
|
||||||
|
TypeCode.Int32 or TypeCode.UInt32 => 4,
|
||||||
|
TypeCode.Int64 or TypeCode.UInt64 => 8,
|
||||||
|
_ => throw new ArgumentException("Unsupported underlying type: " + type.Name),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Collections;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -12,6 +13,7 @@ using TMPro;
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using MethodBase = System.Reflection.MethodBase;
|
using MethodBase = System.Reflection.MethodBase;
|
||||||
using Object = UnityEngine.Object;
|
using Object = UnityEngine.Object;
|
||||||
|
using Random = UnityEngine.Random;
|
||||||
|
|
||||||
namespace MiraAPI.Utilities;
|
namespace MiraAPI.Utilities;
|
||||||
|
|
||||||
@@ -522,6 +524,79 @@ public static class Helpers
|
|||||||
(Il2CppSystem.Object[])[value.ToString(formatString, CultureInfo.InvariantCulture)]),
|
(Il2CppSystem.Object[])[value.ToString(formatString, CultureInfo.InvariantCulture)]),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Shorthand for <see cref="string.Split(char, StringSplitOptions)"/> that removes empty entries and trims leading and ending spaces.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The string to split.</param>
|
||||||
|
/// <param name="separator">The separator.</param>
|
||||||
|
/// <returns>An array of substrings.</returns>
|
||||||
|
public static string[] TrueSplit(this string value, char separator) => value.Split(separator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Joins a collection of items that are separated by a character.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="separator">The separator.</param>
|
||||||
|
/// <param name="items">The collection of items to string together.</param>
|
||||||
|
/// <typeparam name="T">The type of the items.</typeparam>
|
||||||
|
/// <returns>A string formed by concatenating the ToString results of each item, each separated by a separator.</returns>
|
||||||
|
public static string Join<T>(char separator, IEnumerable<T> items) => Join(separator.ToString(), items);
|
||||||
|
|
||||||
|
/// <inheritdoc cref="Join{T}(char, IEnumerable{T})"/>
|
||||||
|
public static string Join<T>(string separator, IEnumerable<T> items)
|
||||||
|
{
|
||||||
|
if (!items.Any())
|
||||||
|
return string.Empty;
|
||||||
|
|
||||||
|
var result = string.Empty;
|
||||||
|
items.Do(x => result += $"{separator}{x}");
|
||||||
|
return result[separator.Length..];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Shorthand helper for overriding the on click listeners for PassiveButton.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="passive">The PassiveButton being modified.</param>
|
||||||
|
/// <param name="action">The action that's replacing the original listeners.</param>
|
||||||
|
public static void OverrideOnClickListeners(this PassiveButton passive, Action action)
|
||||||
|
{
|
||||||
|
if (!passive)
|
||||||
|
return;
|
||||||
|
|
||||||
|
passive.OnClick?.RemoveAllListeners();
|
||||||
|
passive.OnClick = new();
|
||||||
|
passive.OnClick.AddListener(action);
|
||||||
|
passive.enabled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Shorthand helper for overriding the on mouse over listeners for PassiveButton.
|
||||||
|
/// </summary>
|
||||||
|
/// <inheritdoc cref="OverrideOnClickListeners"/>
|
||||||
|
public static void OverrideOnMouseOverListeners(this PassiveButton passive, Action action)
|
||||||
|
{
|
||||||
|
if (!passive)
|
||||||
|
return;
|
||||||
|
|
||||||
|
passive.OnMouseOver?.RemoveAllListeners();
|
||||||
|
passive.OnMouseOver = new();
|
||||||
|
passive.OnMouseOver.AddListener(action);
|
||||||
|
passive.enabled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Shorthand helper for overriding the on mouse out listeners for PassiveButton.
|
||||||
|
/// </summary>
|
||||||
|
/// <inheritdoc cref="OverrideOnClickListeners"/>
|
||||||
|
public static void OverrideOnMouseOutListeners(this PassiveButton passive, Action action)
|
||||||
|
{
|
||||||
|
if (!passive)
|
||||||
|
return;
|
||||||
|
|
||||||
|
passive.OnMouseOut?.RemoveAllListeners();
|
||||||
|
passive.OnMouseOut = new();
|
||||||
|
passive.OnMouseOut.AddListener(action);
|
||||||
|
passive.enabled = true;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns the string of a role.
|
/// Returns the string of a role.
|
||||||
|
|||||||
@@ -24,4 +24,9 @@ public enum MiraNumberSuffixes
|
|||||||
/// Percentage suffix (%).
|
/// Percentage suffix (%).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Percent,
|
Percent,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Display in units.
|
||||||
|
/// </summary>
|
||||||
|
Distance,
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user