From 1bd96041eeb04555a57c2f232f4fb53963432587 Mon Sep 17 00:00:00 2001 From: Az <56699741+AlchlcDvl@users.noreply.github.com> Date: Wed, 4 Nov 2026 15:00:54 -0500 Subject: [PATCH] complete two todos + minor improvements --- .../Patches/GameModes/CreateOptionsPatch.cs | 19 ------- .../LocalSettings/OptionsMenuPatches.cs | 39 ++++++++------ .../Patches/Options/RoleSettingMenuPatches.cs | 51 ++++++++----------- MiraAPI/PluginLoading/MiraPluginManager.cs | 4 +- .../Components/RoleSettingsMenuUpdater.cs | 49 ++++++++++++++++++ 5 files changed, 95 insertions(+), 67 deletions(-) delete mode 100644 MiraAPI/Patches/GameModes/CreateOptionsPatch.cs create mode 100644 MiraAPI/Utilities/Components/RoleSettingsMenuUpdater.cs diff --git a/MiraAPI/Patches/GameModes/CreateOptionsPatch.cs b/MiraAPI/Patches/GameModes/CreateOptionsPatch.cs deleted file mode 100644 index b33c1fd3..00000000 --- a/MiraAPI/Patches/GameModes/CreateOptionsPatch.cs +++ /dev/null @@ -1,19 +0,0 @@ -using HarmonyLib; -using GameModesEnum = AmongUs.GameOptions.GameModes; - -namespace MiraAPI.Patches.GameModes; - -[HarmonyPatch(typeof(CreateOptionsPicker), nameof(CreateOptionsPicker.SetGameMode))] -internal static class CreateOptionsPatch -{ - public static bool Prefix(CreateOptionsPicker __instance, GameModesEnum mode) - { - if (mode <= GameModesEnum.SeekFools) - { - return true; - } - - // TODO: finish this - return false; - } -} diff --git a/MiraAPI/Patches/LocalSettings/OptionsMenuPatches.cs b/MiraAPI/Patches/LocalSettings/OptionsMenuPatches.cs index 9e043cf6..074afd4e 100644 --- a/MiraAPI/Patches/LocalSettings/OptionsMenuPatches.cs +++ b/MiraAPI/Patches/LocalSettings/OptionsMenuPatches.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using HarmonyLib; @@ -70,7 +71,7 @@ public static class OptionsMenuPatches _currentPage = 1; TabButtons.Clear(); - float yOffset = 0; + var yOffset = 0f; var i = 0; var tabIdx = 0; var page = 1; @@ -144,9 +145,9 @@ public static class OptionsMenuPatches UpdatePages(); })); button.OnMouseOver = new UnityEvent(); - button.OnMouseOver.AddListener((UnityAction)(() => rollover.DoMouseOver())); + button.OnMouseOver.AddListener((Action)rollover.DoMouseOver); button.OnMouseOut = new UnityEvent(); - button.OnMouseOut.AddListener((UnityAction)(() => rollover.DoMouseOut())); + button.OnMouseOut.AddListener((Action)rollover.DoMouseOut); } private static void ChangePage(int increase) @@ -207,14 +208,17 @@ public static class OptionsMenuPatches private static void CustomOpen(LocalSettingsTab tab) { - if (tab.TabButton?.Button) - tab.TabButton!.Button.color = tab.TabAppearance.TabButtonHoverColor; + if (tab.TabButton) + { + if (tab.TabButton!.Button) + tab.TabButton.Button.color = tab.TabAppearance.TabButtonHoverColor; - if (tab.TabButton?.Rollover) - tab.TabButton!.Rollover.OutColor = tab.TabAppearance.TabButtonHoverColor; + if (tab.TabButton.Rollover) + tab.TabButton.Rollover.OutColor = tab.TabAppearance.TabButtonHoverColor; - if (tab.TabButton?.Content) - tab.TabButton!.Content.SetActive(true); + if (tab.TabButton.Content) + tab.TabButton.Content.SetActive(true); + } if (background != null) background.color = tab.TabAppearance.TabColor; @@ -224,14 +228,17 @@ public static class OptionsMenuPatches private static void CustomClose(LocalSettingsTab tab) { - if (tab.TabButton?.Button) - tab.TabButton!.Button.color = tab.TabAppearance.TabButtonColor; + if (tab.TabButton) + { + if (tab.TabButton!.Button) + tab.TabButton.Button.color = tab.TabAppearance.TabButtonColor; - if (tab.TabButton?.Rollover) - tab.TabButton!.Rollover.OutColor = tab.TabAppearance.TabButtonColor; + if (tab.TabButton.Rollover) + tab.TabButton.Rollover.OutColor = tab.TabAppearance.TabButtonColor; - if (tab.TabButton?.Content) - tab.TabButton!.Content.SetActive(false); + if (tab.TabButton.Content) + tab.TabButton.Content.SetActive(false); + } if (background != null) background.color = Color.white; diff --git a/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs b/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs index 3d2ed2ec..3106adba 100644 --- a/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs +++ b/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using AmongUs.GameOptions; using BepInEx.Unity.IL2CPP.Utils.Collections; @@ -12,6 +13,7 @@ using MiraAPI.Roles; using MiraAPI.Translation; using MiraAPI.Utilities; using MiraAPI.Utilities.Assets; +using MiraAPI.Utilities.Components; using Reactor.Networking.Rpc; using Reactor.Utilities.Extensions; using TMPro; @@ -23,15 +25,24 @@ using Object = UnityEngine.Object; namespace MiraAPI.Patches.Options; [HarmonyPatch(typeof(RolesSettingsMenu))] +[SuppressMessage("Critical Code Smell", "S2223:Non-constant static fields should not be visible", Justification = "Is internal code. Property-level protections are not required.")] +[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:Fields should be private", Justification = "Read above.")] public static class RoleSettingMenuPatches { private static readonly Dictionary RoleGroupHidden = []; - private static RoleBehaviour? currentRole; - private static List? currentRoleOptions; + internal static RoleBehaviour? CurrentRole; + internal static List? CurrentRoleOptions; private static float scrollerNum = 0.522f; + [HarmonyPostfix] + [HarmonyPatch(nameof(RolesSettingsMenu.Awake))] + public static void OnAwake(RolesSettingsMenu __instance) + { + __instance.gameObject.AddComponent(); + } + [HarmonyPostfix] [HarmonyPatch(nameof(RolesSettingsMenu.OnEnable))] public static void OpenPatch() @@ -41,8 +52,8 @@ public static class RoleSettingMenuPatches private static IEnumerator CoQuotaTabPatch(RolesSettingsMenu roleMenu) { - currentRole = null; - currentRoleOptions = null; + CurrentRole = null; + CurrentRoleOptions = null; roleMenu.QuotaTabSelectables.Clear(); roleMenu.roleChances.Clear(); @@ -91,7 +102,7 @@ public static class RoleSettingMenuPatches Object.Destroy(child.gameObject); } - yield return new WaitForEndOfFrame(); + yield return null; MenuState.Instance.QueuedRoleMenuRefresh[MenuState.Instance.CurrentModIdx] = false; } @@ -186,7 +197,7 @@ public static class RoleSettingMenuPatches Object.Destroy(child.gameObject); } - yield return new WaitForEndOfFrame(); + yield return null; var roleGroups = MenuState.Instance.CurrentMod.InternalRoles.Values.OfType() .ToLookup(x => x.RoleOptionsGroup); @@ -431,32 +442,12 @@ public static class RoleSettingMenuPatches scroller.SetYBoundsMax(-scrollerNum - 2); } - // TODO: turn this into a fixed update + // Completed: turn this into a fixed update [HarmonyPrefix] [HarmonyPatch(nameof(RolesSettingsMenu.Update))] public static bool UpdatePatch(RolesSettingsMenu __instance) { - if (MenuState.Instance.CurrentModIdx == 0) return true; - if (currentRole == null || currentRoleOptions == null) return false; - - var hasImage = currentRole.RoleScreenshot != null; - var num = hasImage ? -0.872f : -1; - foreach (var opt in currentRoleOptions) - { - if (opt.OptionBehaviour == null) continue; - - if (!opt.Visible.Invoke()) - { - opt.OptionBehaviour.gameObject.SetActive(false); - continue; - } - - opt.OptionBehaviour.transform.localPosition = new Vector3(hasImage ? 2.17f : 1.1f, num, -2f); - opt.OptionBehaviour.gameObject.SetActive(true); - num += -0.45f; - } - - __instance.scrollBar.SetYBoundsMax(-num - 3); + // See RoleSettingsMenuUpdater.FixedUpdate in Utilities/Components/RoleSettingsMenuUpdater.cs return false; } @@ -508,7 +499,7 @@ public static class RoleSettingMenuPatches optBehaviour.gameObject.Destroy(); } - currentRole = role; + CurrentRole = role; __instance.advancedSettingChildren.Clear(); // TODO: create sub groups under the role settings. @@ -547,7 +538,7 @@ public static class RoleSettingMenuPatches newOpt.Initialize(); } - currentRoleOptions = filteredOptions; + CurrentRoleOptions = filteredOptions; } [HarmonyPrefix] diff --git a/MiraAPI/PluginLoading/MiraPluginManager.cs b/MiraAPI/PluginLoading/MiraPluginManager.cs index c2f1caed..840d8926 100644 --- a/MiraAPI/PluginLoading/MiraPluginManager.cs +++ b/MiraAPI/PluginLoading/MiraPluginManager.cs @@ -68,7 +68,7 @@ public sealed class MiraPluginManager MiraApiPluginInfo = info; registrationTimer.Stop(); - Info($"Registering Mira API as mod in {registrationTimer.ElapsedMilliseconds}ms."); + Info($"Registered Mira API as mod in {registrationTimer.ElapsedMilliseconds}ms."); } IL2CPPChainloader.Instance.PluginLoad += (pluginInfo, assembly, plugin) => @@ -81,7 +81,7 @@ public sealed class MiraPluginManager var registrationTimer = Stopwatch.StartNew(); RegisterPlugin(assembly, miraPlugin, pluginInfo, plugin, false); registrationTimer.Stop(); - Info($"Registering mod {pluginInfo.Metadata.Name} with Mira API in {registrationTimer.ElapsedMilliseconds}ms."); + Info($"Registered mod {pluginInfo.Metadata.Name} with Mira API in {registrationTimer.ElapsedMilliseconds}ms."); }; IL2CPPChainloader.Instance.Finished += () => diff --git a/MiraAPI/Utilities/Components/RoleSettingsMenuUpdater.cs b/MiraAPI/Utilities/Components/RoleSettingsMenuUpdater.cs new file mode 100644 index 00000000..219e4f28 --- /dev/null +++ b/MiraAPI/Utilities/Components/RoleSettingsMenuUpdater.cs @@ -0,0 +1,49 @@ +using System.Diagnostics.CodeAnalysis; +using MiraAPI.Patches.Options; +using Reactor.Utilities.Attributes; +using UnityEngine; + +namespace MiraAPI.Utilities.Components; + +/// +/// Updates the visuals of the in a fixed update loop. +/// +/// The C++ pointer to the object. +[RegisterInIl2Cpp] +[SuppressMessage("Style", "IDE0051:Remove unused private members", Justification = "Unity convention.")] +public sealed class RoleSettingsMenuUpdater(nint cppPtr) : MonoBehaviour(cppPtr) +{ + private RolesSettingsMenu menu; + + private void Awake() + { + menu = GetComponent(); + } + + private void FixedUpdate() + { + if (MenuState.Instance.CurrentModIdx == 0) return; + if (menu == null || RoleSettingMenuPatches.CurrentRole == null || RoleSettingMenuPatches.CurrentRoleOptions == null) return; + + var hasImage = RoleSettingMenuPatches.CurrentRole.RoleScreenshot != null; + var num = hasImage ? -0.872f : -1; + foreach (var opt in RoleSettingMenuPatches.CurrentRoleOptions) + { + var behaviour = opt.OptionBehaviour; + + if (behaviour == null) continue; + + if (!opt.Visible()) + { + behaviour.gameObject.SetActive(false); + continue; + } + + behaviour.transform.localPosition = new Vector3(hasImage ? 2.17f : 1.1f, num, -2f); + behaviour.gameObject.SetActive(true); + num += -0.45f; + } + + menu.scrollBar.SetYBoundsMax(-num - 3); + } +}