From 05fdf5313d55d3b66bab3e53d05a48889ea82e62 Mon Sep 17 00:00:00 2001 From: ang-xd Date: Tue, 27 Aug 2024 19:04:32 -0400 Subject: [PATCH 1/2] MiraNumberSuffixes, and checkmark fix --- MiraAPI.Example/Options/ExampleOptions.cs | 3 +- .../Attributes/ModdedNumberOptionAttribute.cs | 3 +- .../OptionTypes/ModdedNumberOption.cs | 36 +++++++-------- .../Patches/Options/GameOptionsMenuPatch.cs | 7 ++- MiraAPI/Patches/Options/SettingPatches.cs | 42 ++++++++++++++++++ MiraAPI/Resources/Checkmark.png | Bin 370 -> 1064 bytes MiraAPI/Utilities/Assets/MiraAssets.cs | 7 ++- MiraAPI/Utilities/Helpers.cs | 9 ++-- MiraAPI/Utilities/MiraNumberSuffixes.cs | 8 ++++ 9 files changed, 86 insertions(+), 29 deletions(-) create mode 100644 MiraAPI/Patches/Options/SettingPatches.cs create mode 100644 MiraAPI/Utilities/MiraNumberSuffixes.cs diff --git a/MiraAPI.Example/Options/ExampleOptions.cs b/MiraAPI.Example/Options/ExampleOptions.cs index e124d91..480b357 100644 --- a/MiraAPI.Example/Options/ExampleOptions.cs +++ b/MiraAPI.Example/Options/ExampleOptions.cs @@ -1,5 +1,6 @@ using MiraAPI.GameOptions; using MiraAPI.GameOptions.Attributes; +using MiraAPI.Utilities; using UnityEngine; namespace MiraAPI.Example.Options; @@ -15,7 +16,7 @@ public class ExampleOptions : AbstractOptionGroup [ModdedToggleOption("Toggle Opt 2")] public bool ToggleOpt2 { get; set; } = true; - [ModdedNumberOption("Number Opt", min: 0, max: 10)] + [ModdedNumberOption("Number Opt", min: 0, max: 10, suffixType: MiraNumberSuffixes.Percent)] public float NumberOpt { get; set; } = 4f; [ModdedStringOption("String Opt", ["Hello", "Hey", "Bye"])] diff --git a/MiraAPI/GameOptions/Attributes/ModdedNumberOptionAttribute.cs b/MiraAPI/GameOptions/Attributes/ModdedNumberOptionAttribute.cs index 00c9ea1..ac451ad 100644 --- a/MiraAPI/GameOptions/Attributes/ModdedNumberOptionAttribute.cs +++ b/MiraAPI/GameOptions/Attributes/ModdedNumberOptionAttribute.cs @@ -1,4 +1,5 @@ using MiraAPI.GameOptions.OptionTypes; +using MiraAPI.Utilities; using System; using System.Reflection; @@ -9,7 +10,7 @@ public class ModdedNumberOptionAttribute( float min, float max, float increment = 1, - NumberSuffixes suffixType = NumberSuffixes.None, + MiraNumberSuffixes suffixType = MiraNumberSuffixes.None, bool zeroInfinity = false, Type roleType = null) : ModdedOptionAttribute(title, roleType) diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedNumberOption.cs b/MiraAPI/GameOptions/OptionTypes/ModdedNumberOption.cs index b10926c..ac04d1b 100644 --- a/MiraAPI/GameOptions/OptionTypes/ModdedNumberOption.cs +++ b/MiraAPI/GameOptions/OptionTypes/ModdedNumberOption.cs @@ -1,7 +1,7 @@ -using System; -using AmongUs.GameOptions; +using AmongUs.GameOptions; using MiraAPI.Networking; using MiraAPI.Utilities; +using System; using UnityEngine; using Object = UnityEngine.Object; @@ -9,17 +9,17 @@ namespace MiraAPI.GameOptions.OptionTypes; public class ModdedNumberOption : ModdedOption { - public float Min { get;} - + public float Min { get; } + public float Max { get; } - + public float Increment { get; } - - public NumberSuffixes SuffixType { get; } - + + public MiraNumberSuffixes SuffixType { get; } + public bool ZeroInfinity { get; } - - public ModdedNumberOption(string title, float defaultValue, float min, float max, float increment, NumberSuffixes suffixType, bool zeroInfinity=false, Type roleType=null) : base(title, defaultValue, roleType) + + public ModdedNumberOption(string title, float defaultValue, float min, float max, float increment, MiraNumberSuffixes suffixType, bool zeroInfinity = false, Type roleType = null) : base(title, defaultValue, roleType) { Min = min; Max = max; @@ -28,18 +28,18 @@ public ModdedNumberOption(string title, float defaultValue, float min, float max ZeroInfinity = zeroInfinity; Value = Mathf.Clamp(defaultValue, min, max); - + Data = ScriptableObject.CreateInstance(); - + var data = (FloatGameSetting)Data; data.Type = global::OptionTypes.Float; data.Title = StringName; data.Value = Value; data.Increment = Increment; data.ValidRange = new FloatRange(Min, Max); - data.FormatString = Increment % 1 == 0 && Value % 1 == 0 && Min % 1 == 0 && Max % 1 == 0 ? "0" : "0.0"; + data.FormatString = Increment % 1 == 0 && Value % 1 == 0 && Min % 1 == 0 && Max % 1 == 0 ? "0" : "0.0"; data.ZeroIsInfinity = ZeroInfinity; - data.SuffixType = SuffixType; + data.SuffixType = (NumberSuffixes)SuffixType; data.OptionName = FloatOptionNames.Invalid; } @@ -49,17 +49,17 @@ public override OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOptio numberOption.SetUpFromData(Data, 20); numberOption.OnValueChanged = (Il2CppSystem.Action)ValueChanged; - + numberOption.Title = StringName; numberOption.Value = Value; numberOption.Increment = Increment; numberOption.ValidRange = new FloatRange(Min, Max); numberOption.FormatString = ((FloatGameSetting)Data).FormatString; numberOption.ZeroIsInfinity = ZeroInfinity; - numberOption.SuffixType = SuffixType; + numberOption.SuffixType = (NumberSuffixes)SuffixType; numberOption.floatOptionName = FloatOptionNames.Invalid; - - + + OptionBehaviour = numberOption; return numberOption; diff --git a/MiraAPI/Patches/Options/GameOptionsMenuPatch.cs b/MiraAPI/Patches/Options/GameOptionsMenuPatch.cs index 14e7a46..0bc37f1 100644 --- a/MiraAPI/Patches/Options/GameOptionsMenuPatch.cs +++ b/MiraAPI/Patches/Options/GameOptionsMenuPatch.cs @@ -3,7 +3,6 @@ using MiraAPI.Utilities.Assets; using Reactor.Localization.Utilities; using Reactor.Utilities.Extensions; -using System.Collections.Generic; using System.Linq; using TMPro; using UnityEngine; @@ -102,7 +101,7 @@ public static bool SettingsPatch(GameOptionsMenu __instance) var options = group.Options.Select(opt => opt.CreateOption(__instance.checkboxOrigin, __instance.numberOptionOrigin, __instance.stringOptionOrigin, __instance.settingsContainer)); - + foreach (var newOpt in options) { newOpt.SetClickMask(__instance.ButtonClickMask); @@ -137,10 +136,10 @@ public static bool SettingsPatch(GameOptionsMenu __instance) if (newOpt is ToggleOption toggle) { toggle.CheckMark.sprite = MiraAssets.Checkmark.LoadAsset(); - + toggle.CheckMark.color = group.GroupColor != Color.clear ? group.GroupColor : MiraAssets.AcceptedTeal; var rend = toggle.CheckMark.transform.parent.FindChild("ActiveSprite").GetComponent(); rend.sprite = MiraAssets.CheckmarkBox.LoadAsset(); - rend.color = group.GroupColor; + rend.color = group.GroupColor != Color.clear ? group.GroupColor : MiraAssets.AcceptedTeal; } __instance.Children.Add(newOpt); diff --git a/MiraAPI/Patches/Options/SettingPatches.cs b/MiraAPI/Patches/Options/SettingPatches.cs new file mode 100644 index 0000000..2591269 --- /dev/null +++ b/MiraAPI/Patches/Options/SettingPatches.cs @@ -0,0 +1,42 @@ +using HarmonyLib; +using MiraAPI.Utilities; +using UnityEngine; + +namespace MiraAPI.Patches.Options; +[HarmonyPatch] +public class SettingPatches +{ + [HarmonyPrefix] + [HarmonyPatch(typeof(FloatGameSetting), nameof(FloatGameSetting.GetValueString))] + public static bool ValueStringPatch(FloatGameSetting __instance, ref string __result, [HarmonyArgument(0)] float value) + { + string result = string.Empty; + MiraNumberSuffixes suffix = (MiraNumberSuffixes)__instance.SuffixType; + + if (__instance.ZeroIsInfinity && Mathf.Abs(value) < 0.0001f) + { + result = ""; + } + else if (suffix == MiraNumberSuffixes.None) + { + result = value.ToString(__instance.FormatString); + } + else if (suffix == MiraNumberSuffixes.Multiplier) + { + result = value.ToString(__instance.FormatString) + "x"; + } + else if (suffix == MiraNumberSuffixes.Percent) + { + result = value.ToString(__instance.FormatString) + "%"; + } + else + { + result = DestroyableSingleton.Instance.GetString(StringNames.GameSecondsAbbrev, (Il2CppSystem.Object[])( + [ + value.ToString(__instance.FormatString) + ])); + } + __result = result; + return false; + } +} \ No newline at end of file diff --git a/MiraAPI/Resources/Checkmark.png b/MiraAPI/Resources/Checkmark.png index 802819a9b789f8583ac6e1e881ee0f2e2dbaae42..435f8cbed96741b8cf2b228c19706ae3e7371a67 100644 GIT binary patch delta 1056 zcmV+*1mFAe0;mX(7k@qo1^@s6bHS)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qV%@7PRL-0H^g#MjZ z@Vr().FirstOrDefault(body => body.ParentId == id); } - public static string GetSuffix(NumberSuffixes suffix) + public static string GetSuffix(MiraNumberSuffixes suffix) { return suffix switch { - NumberSuffixes.None => string.Empty, - NumberSuffixes.Multiplier => "x", - NumberSuffixes.Seconds => "s", + MiraNumberSuffixes.None => string.Empty, + MiraNumberSuffixes.Multiplier => "x", + MiraNumberSuffixes.Seconds => "s", + MiraNumberSuffixes.Percent => "%", _ => string.Empty }; } diff --git a/MiraAPI/Utilities/MiraNumberSuffixes.cs b/MiraAPI/Utilities/MiraNumberSuffixes.cs new file mode 100644 index 0000000..3682e2b --- /dev/null +++ b/MiraAPI/Utilities/MiraNumberSuffixes.cs @@ -0,0 +1,8 @@ +namespace MiraAPI.Utilities; +public enum MiraNumberSuffixes +{ + None, + Multiplier, + Seconds, + Percent +} \ No newline at end of file From 6bc6ab7143fe08119df0dc16372b497435d3f982 Mon Sep 17 00:00:00 2001 From: XtraCube <72575280+XtraCube@users.noreply.github.com> Date: Tue, 27 Aug 2024 19:22:48 -0400 Subject: [PATCH 2/2] fix button issue --- MiraAPI/Hud/CustomActionButton.cs | 3 ++- MiraAPI/Patches/PlayerControlPatches.cs | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/MiraAPI/Hud/CustomActionButton.cs b/MiraAPI/Hud/CustomActionButton.cs index 04102b3..16e72a4 100644 --- a/MiraAPI/Hud/CustomActionButton.cs +++ b/MiraAPI/Hud/CustomActionButton.cs @@ -3,6 +3,7 @@ using MiraAPI.Utilities.Assets; using UnityEngine; using UnityEngine.Events; +using UnityEngine.UI; namespace MiraAPI.Hud; @@ -95,7 +96,7 @@ internal void CreateButton(Transform parent) } var pb = Button.GetComponent(); - pb.OnClick.RemoveAllListeners(); + pb.OnClick = new Button.ButtonClickedEvent(); pb.OnClick.AddListener((UnityAction)ClickHandler); } diff --git a/MiraAPI/Patches/PlayerControlPatches.cs b/MiraAPI/Patches/PlayerControlPatches.cs index 97a7e84..f4f79ce 100644 --- a/MiraAPI/Patches/PlayerControlPatches.cs +++ b/MiraAPI/Patches/PlayerControlPatches.cs @@ -37,9 +37,9 @@ public static void PlayerControlFixedUpdatePostfix(PlayerControl __instance) foreach (var button in CustomButtonManager.CustomButtons) { - if (__instance.Data?.Role != null) + if (__instance.Data?.Role == null) { - return; + continue; } if (!button.Enabled(__instance.Data?.Role))