From 9665188a18461b50e1a46ec9f5e4978e06868f72 Mon Sep 17 00:00:00 2001 From: XtraCube <72575280+XtraCube@users.noreply.github.com> Date: Mon, 2 Sep 2024 13:46:10 -0400 Subject: [PATCH] various adjustments to code style --- MiraAPI/GameOptions/IModdedOption.cs | 2 +- MiraAPI/Patches/Modifiers/VentPatches.cs | 4 +- .../Options/NotificationPopperPatch.cs | 34 ++--- .../Patches/Options/RoleSettingMenuPatches.cs | 118 +++++++++++++----- 4 files changed, 107 insertions(+), 51 deletions(-) diff --git a/MiraAPI/GameOptions/IModdedOption.cs b/MiraAPI/GameOptions/IModdedOption.cs index 4f7e950e..544d64bc 100644 --- a/MiraAPI/GameOptions/IModdedOption.cs +++ b/MiraAPI/GameOptions/IModdedOption.cs @@ -17,7 +17,7 @@ internal interface IModdedOption OptionBehaviour? OptionBehaviour { get; } Func Visible { get; set; } ConfigDefinition? ConfigDefinition { get; set; } - OptionBehaviour? CreateOption(ToggleOption toggleOpt, NumberOption numberOpt, StringOption stringOpt, Transform container); + OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOption numberOpt, StringOption stringOpt, Transform container); float GetFloatData(); NetData GetNetData(); void HandleNetData(byte[] data); diff --git a/MiraAPI/Patches/Modifiers/VentPatches.cs b/MiraAPI/Patches/Modifiers/VentPatches.cs index 68948483..4aeea091 100644 --- a/MiraAPI/Patches/Modifiers/VentPatches.cs +++ b/MiraAPI/Patches/Modifiers/VentPatches.cs @@ -23,10 +23,10 @@ public static class VentPatches switch (role.CanVent) { - case true when modifiers.Any(x => !x.CanVent()): + case true when modifiers.Exists(x => !x.CanVent()): couldUse = canUse = false; return; - case false when modifiers.Any(x => x.CanVent()): + case false when modifiers.Exists(x => x.CanVent()): couldUse = true; break; } diff --git a/MiraAPI/Patches/Options/NotificationPopperPatch.cs b/MiraAPI/Patches/Options/NotificationPopperPatch.cs index 0a8fc573..faa24eb1 100644 --- a/MiraAPI/Patches/Options/NotificationPopperPatch.cs +++ b/MiraAPI/Patches/Options/NotificationPopperPatch.cs @@ -6,34 +6,38 @@ using System.Linq; namespace MiraAPI.Patches.Options; [HarmonyPatch(typeof(NotificationPopper))] -public class NotificationPopperPatch +public static class NotificationPopperPatch { [HarmonyPrefix] [HarmonyPatch(nameof(NotificationPopper.AddRoleSettingsChangeMessage))] - public static bool RoleChangeMsgPatch(NotificationPopper __instance, - [HarmonyArgument(0)] StringNames key, [HarmonyArgument(1)] int roleCount, [HarmonyArgument(2)] int roleChance, [HarmonyArgument(3)] RoleTeamTypes teamType, + public static bool RoleChangeMsgPatch( + NotificationPopper __instance, + [HarmonyArgument(0)] StringNames key, + [HarmonyArgument(1)] int roleCount, + [HarmonyArgument(2)] int roleChance, + [HarmonyArgument(3)] RoleTeamTypes teamType, [HarmonyArgument(4)] bool playSound) { if (CustomRoleManager.CustomRoles.Values.Any(role => role.StringName == key)) { - var item = string.Empty; - var text = (teamType == RoleTeamTypes.Crewmate) ? Palette.CrewmateSettingChangeText.ToTextColor() : Palette.ImpostorRed.ToTextColor(); - item = DestroyableSingleton.Instance.GetString(StringNames.LobbyChangeSettingNotificationRole, new Object[] - { - string.Concat( - [ + var text = teamType == RoleTeamTypes.Crewmate + ? Palette.CrewmateSettingChangeText.ToTextColor() + : Palette.ImpostorRed.ToTextColor(); + + var item = DestroyableSingleton.Instance.GetString( + StringNames.LobbyChangeSettingNotificationRole, "", text, DestroyableSingleton.Instance.GetString(key, Array.Empty()), - "" - ]), - "" + roleCount.ToString() + "", - "" + roleChance.ToString() + "%" - }); + "", + "" + roleCount + "", + "" + roleChance + "%" + ); + __instance.SettingsChangeMessageLogic(key, item, playSound); return false; } return true; } -} \ No newline at end of file +} diff --git a/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs b/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs index 420fff8c..8ba2c931 100644 --- a/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs +++ b/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs @@ -54,58 +54,72 @@ public static class RoleSettingMenuPatches var num3 = 0; - var crewRoles = GameSettingMenuPatches.SelectedMod.CustomRoles.Values + var crewRoles = GameSettingMenuPatches.SelectedMod?.CustomRoles.Values .OfType() - .Where(role => role.Team == ModdedRoleTeams.Crewmate && !role.HideSettings) + .Where(role => role is { Team: ModdedRoleTeams.Crewmate, HideSettings: false }) .ToList(); - if (crewRoles.Count > 0) + if (crewRoles is { Count: > 0 }) { - - var categoryHeaderEditRole = Object.Instantiate(__instance.categoryHeaderEditRoleOrigin, Vector3.zero, Quaternion.identity, __instance.RoleChancesSettings.transform); + var categoryHeaderEditRole = Object.Instantiate( + __instance.categoryHeaderEditRoleOrigin, + Vector3.zero, + Quaternion.identity, + __instance.RoleChancesSettings.transform); categoryHeaderEditRole.SetHeader(StringNames.CrewmateRolesHeader, 20); categoryHeaderEditRole.transform.localPosition = new Vector3(4.986f, num, -2f); num -= 0.522f; foreach (var role in crewRoles) { - CreateQuotaOption(__instance, role as RoleBehaviour, ref num, num3); + if (role is RoleBehaviour roleBehaviour) + { + CreateQuotaOption(__instance, roleBehaviour, ref num, num3); + } num3++; } } - var impRoles = GameSettingMenuPatches.SelectedMod.CustomRoles.Values + var impRoles = GameSettingMenuPatches.SelectedMod?.CustomRoles.Values .OfType() - .Where(role => role.Team == ModdedRoleTeams.Impostor && !role.HideSettings) + .Where(role => role is { Team: ModdedRoleTeams.Impostor, HideSettings: false }) .ToList(); - if (impRoles.Count > 0) + if (impRoles is { Count: > 0 }) { num -= 0.4f; - var categoryHeaderEditRole2 = Object.Instantiate(__instance.categoryHeaderEditRoleOrigin, - Vector3.zero, Quaternion.identity, __instance.RoleChancesSettings.transform); + var categoryHeaderEditRole2 = Object.Instantiate( + __instance.categoryHeaderEditRoleOrigin, + Vector3.zero, + Quaternion.identity, + __instance.RoleChancesSettings.transform); categoryHeaderEditRole2.SetHeader(StringNames.ImpostorRolesHeader, 20); categoryHeaderEditRole2.transform.localPosition = new Vector3(4.986f, num, -2f); num -= 0.522f; - foreach (var role in impRoles) { - CreateQuotaOption(__instance, role as RoleBehaviour, ref num, num3); + if (role is RoleBehaviour roleBehaviour) + { + CreateQuotaOption(__instance, roleBehaviour, ref num, num3); + } num3++; } } - var neutRoles = GameSettingMenuPatches.SelectedMod.CustomRoles.Values + var neutRoles = GameSettingMenuPatches.SelectedMod?.CustomRoles.Values .OfType() - .Where(role => role.Team == ModdedRoleTeams.Neutral && !role.HideSettings) + .Where(role => role is { Team: ModdedRoleTeams.Neutral, HideSettings: false }) .ToList(); - if (neutRoles.Count > 0) + if (neutRoles is { Count: > 0 }) { num -= 0.4f; - var categoryHeaderEditRole3 = Object.Instantiate(__instance.categoryHeaderEditRoleOrigin, - Vector3.zero, Quaternion.identity, __instance.RoleChancesSettings.transform); + var categoryHeaderEditRole3 = Object.Instantiate( + __instance.categoryHeaderEditRoleOrigin, + Vector3.zero, + Quaternion.identity, + __instance.RoleChancesSettings.transform); categoryHeaderEditRole3.SetHeader(StringNames.None, 20); categoryHeaderEditRole3.Title.text = "Neutral Roles"; categoryHeaderEditRole3.Background.color = Color.gray; @@ -115,7 +129,10 @@ public static class RoleSettingMenuPatches foreach (var role in neutRoles) { - CreateQuotaOption(__instance, role as RoleBehaviour, ref num, num3); + if (role is RoleBehaviour roleBehaviour) + { + CreateQuotaOption(__instance, roleBehaviour, ref num, num3); + } num3++; } } @@ -161,12 +178,18 @@ public static class RoleSettingMenuPatches } roleSetting.UpdateValuesAndText(GameOptionsManager.Instance.CurrentGameOptions.RoleOptions); - DestroyableSingleton.Instance.Notifier.AddRoleSettingsChangeMessage(roleSetting.Role.StringName, roleSetting.RoleMaxCount, roleSetting.RoleChance, roleSetting.Role.TeamType, false); + DestroyableSingleton.Instance.Notifier.AddRoleSettingsChangeMessage( + roleSetting.Role.StringName, + roleSetting.RoleMaxCount, + roleSetting.RoleChance, + roleSetting.Role.TeamType, + false); if (AmongUsClient.Instance.AmHost) { Rpc.Instance.Send(PlayerControl.LocalPlayer, [role.GetNetData()], true); } + GameOptionsManager.Instance.GameHostOptions = GameOptionsManager.Instance.CurrentGameOptions; } @@ -181,7 +204,7 @@ public static class RoleSettingMenuPatches var num = -0.872f; - var filteredOptions = GameSettingMenuPatches.SelectedMod.Options.Where(x => x.AdvancedRole == role.GetType()); + var filteredOptions = GameSettingMenuPatches.SelectedMod?.Options.Where(x => x.AdvancedRole == role.GetType()) ?? []; foreach (var option in filteredOptions) { @@ -190,7 +213,12 @@ public static class RoleSettingMenuPatches continue; } - var newOpt = option.CreateOption(__instance.checkboxOrigin, __instance.numberOptionOrigin, __instance.stringOptionOrigin, __instance.AdvancedRolesSettings.transform); + var newOpt = option.CreateOption( + __instance.checkboxOrigin, + __instance.numberOptionOrigin, + __instance.stringOptionOrigin, + __instance.AdvancedRolesSettings.transform); + newOpt.transform.localPosition = new Vector3(2.17f, num, -2f); newOpt.SetClickMask(__instance.ButtonClickMask); @@ -199,10 +227,11 @@ public static class RoleSettingMenuPatches { renderer.material.SetInt(PlayerMaterial.MaskLayer, 20); } - foreach (var textMeshPro in newOpt.GetComponentsInChildren(true)) + + foreach (var fontMat in newOpt.GetComponentsInChildren(true).Select(x=>x.fontMaterial)) { - textMeshPro.fontMaterial.SetFloat(ShaderID.StencilComp, 3f); - textMeshPro.fontMaterial.SetFloat(ShaderID.Stencil, 20); + fontMat.SetFloat(ShaderID.StencilComp, 3f); + fontMat.SetFloat(ShaderID.Stencil, 20); } newOpt.LabelBackground.enabled = false; @@ -215,12 +244,24 @@ public static class RoleSettingMenuPatches __instance.scrollBar.CalculateAndSetYBounds(__instance.advancedSettingChildren.Count + 3, 1f, 6f, 0.45f); __instance.scrollBar.ScrollToTop(); } + private static void ChangeTab(RoleBehaviour role, RolesSettingsMenu __instance) { - var customRole = role as ICustomRole; + if (role is not ICustomRole customRole) + { + Logger.Error($"Role {role.NiceName} is not a custom role."); + return; + } + __instance.roleDescriptionText.text = customRole.RoleLongDescription; - __instance.roleTitleText.text = DestroyableSingleton.Instance.GetString(role.StringName, new Il2CppReferenceArray(0)); - __instance.roleScreenshot.sprite = Sprite.Create(customRole.OptionsScreenshot.LoadAsset().texture, new Rect(0, 0, 370, 230), Vector2.one / 2, 100); + __instance.roleTitleText.text = DestroyableSingleton.Instance.GetString( + role.StringName, + new Il2CppReferenceArray(0)); + __instance.roleScreenshot.sprite = Sprite.Create( + customRole.OptionsScreenshot.LoadAsset().texture, + new Rect(0, 0, 370, 230), + Vector2.one / 2, + 100); __instance.roleScreenshot.drawMode = SpriteDrawMode.Sliced; __instance.roleHeaderSprite.color = customRole.RoleColor; __instance.roleHeaderText.color = customRole.RoleColor.GetAlternateColor(); @@ -244,15 +285,25 @@ public static class RoleSettingMenuPatches optionBehaviour.SetAsPlayer(); } } + __instance.RoleChancesSettings.SetActive(false); __instance.AdvancedRolesSettings.SetActive(true); __instance.RefreshChildren(); } - public static void CreateQuotaOption(RolesSettingsMenu __instance, RoleBehaviour role, ref float yPos, int index) + private static void CreateQuotaOption(RolesSettingsMenu __instance, RoleBehaviour role, ref float yPos, int index) { - var customRole = role as ICustomRole; - var roleOptionSetting = Object.Instantiate(__instance.roleOptionSettingOrigin, Vector3.zero, Quaternion.identity, __instance.RoleChancesSettings.transform); + if (role is not ICustomRole customRole) + { + Logger.Error($"Role {role.NiceName} is not a custom role."); + return; + } + + var roleOptionSetting = Object.Instantiate( + __instance.roleOptionSettingOrigin, + Vector3.zero, + Quaternion.identity, + __instance.RoleChancesSettings.transform); roleOptionSetting.transform.localPosition = new Vector3(-0.15f, yPos, -2f); roleOptionSetting.SetRole(GameOptionsManager.Instance.CurrentGameOptions.RoleOptions, role, 20); roleOptionSetting.labelSprite.color = customRole.RoleColor; @@ -265,7 +316,8 @@ public static class RoleSettingMenuPatches roleOptionSetting.titleText.horizontalAlignment = HorizontalAlignmentOptions.Left; if (GameSettingMenuPatches.SelectedMod is null || - GameSettingMenuPatches.SelectedMod.Options.Any(x => x.AdvancedRole != null && x.AdvancedRole.IsInstanceOfType(role))) + GameSettingMenuPatches.SelectedMod.Options.Exists( + x => x.AdvancedRole != null && x.AdvancedRole.IsInstanceOfType(role))) { var newButton = Object.Instantiate(roleOptionSetting.buttons[0], roleOptionSetting.transform); newButton.name = "ConfigButton"; @@ -289,4 +341,4 @@ public static class RoleSettingMenuPatches yPos += -0.43f; } } -} \ No newline at end of file +}