From 75dd5e8a35576f2a8e6e0c9efbfc0628cee574a3 Mon Sep 17 00:00:00 2001 From: CallOfCreator Date: Wed, 4 Sep 2024 16:27:57 +0000 Subject: [PATCH 1/2] Add CanUseSabotage support in ICustomRole --- MiraAPI/Patches/HudManagerPatches.cs | 35 ++++++++++++++++++++++++++++ MiraAPI/Roles/ICustomRole.cs | 5 ++++ 2 files changed, 40 insertions(+) diff --git a/MiraAPI/Patches/HudManagerPatches.cs b/MiraAPI/Patches/HudManagerPatches.cs index cdef7cc..ddd31f8 100644 --- a/MiraAPI/Patches/HudManagerPatches.cs +++ b/MiraAPI/Patches/HudManagerPatches.cs @@ -1,5 +1,6 @@ using HarmonyLib; using MiraAPI.Hud; +using MiraAPI.Roles; using Reactor.Utilities.Extensions; using UnityEngine; using Object = UnityEngine.Object; @@ -92,10 +93,44 @@ public static class HudManagerPatches { return; } + if (CustomRoleManager.GetCustomRoleBehaviour(role.Role, out var customRole)) + { + if (customRole.CanUseSabotage) + { + __instance.SabotageButton.gameObject.SetActive(true); + } + else + { + __instance.SabotageButton.gameObject.SetActive(role.IsImpostor && isActive); + } + } foreach (var button in CustomButtonManager.CustomButtons) { button.SetActive(isActive, role); } } + + /// + /// Patches the Sabotage button to check if the player's custom role can use sabotage. + /// + [HarmonyPatch(typeof(SabotageButton), nameof(SabotageButton.DoClick))] + [HarmonyPrefix] + public static bool StartPrefix(SabotageButton __instance) + { + + var player = PlayerControl.LocalPlayer; + + if (CustomRoleManager.GetCustomRoleBehaviour(player.Data.Role.Role, out var customRole)) + { + if (!customRole.CanUseSabotage) return false; + + DestroyableSingleton.Instance.ToggleMapVisible(new MapOptions() + { + Mode = MapOptions.Modes.Sabotage + }); + return false; + } + return true; + } } diff --git a/MiraAPI/Roles/ICustomRole.cs b/MiraAPI/Roles/ICustomRole.cs index 3087dbe..9fc2b61 100644 --- a/MiraAPI/Roles/ICustomRole.cs +++ b/MiraAPI/Roles/ICustomRole.cs @@ -77,6 +77,11 @@ public interface ICustomRole /// bool CanUseVent => Team == ModdedRoleTeams.Impostor; + /// + /// Gets a value indicating whether the role can use the sabotage button. + /// + bool CanUseSabotage => Team == ModdedRoleTeams.Impostor; + /// /// Gets a value indicating whether the role's tasks count towards task progress. /// From b2651adcabe6ca31c5bcc00b95694f91c3134703 Mon Sep 17 00:00:00 2001 From: XtraCube <72575280+XtraCube@users.noreply.github.com> Date: Thu, 5 Sep 2024 07:18:20 -0400 Subject: [PATCH 2/2] adjustments --- MiraAPI/Patches/HudManagerPatches.cs | 34 ----------- MiraAPI/Patches/Roles/HudManagerPatches.cs | 3 +- MiraAPI/Patches/Roles/SabotageButtonPatch.cs | 62 ++++++++++++++++++++ 3 files changed, 64 insertions(+), 35 deletions(-) create mode 100644 MiraAPI/Patches/Roles/SabotageButtonPatch.cs diff --git a/MiraAPI/Patches/HudManagerPatches.cs b/MiraAPI/Patches/HudManagerPatches.cs index ddd31f8..50e248b 100644 --- a/MiraAPI/Patches/HudManagerPatches.cs +++ b/MiraAPI/Patches/HudManagerPatches.cs @@ -93,44 +93,10 @@ public static class HudManagerPatches { return; } - if (CustomRoleManager.GetCustomRoleBehaviour(role.Role, out var customRole)) - { - if (customRole.CanUseSabotage) - { - __instance.SabotageButton.gameObject.SetActive(true); - } - else - { - __instance.SabotageButton.gameObject.SetActive(role.IsImpostor && isActive); - } - } foreach (var button in CustomButtonManager.CustomButtons) { button.SetActive(isActive, role); } } - - /// - /// Patches the Sabotage button to check if the player's custom role can use sabotage. - /// - [HarmonyPatch(typeof(SabotageButton), nameof(SabotageButton.DoClick))] - [HarmonyPrefix] - public static bool StartPrefix(SabotageButton __instance) - { - - var player = PlayerControl.LocalPlayer; - - if (CustomRoleManager.GetCustomRoleBehaviour(player.Data.Role.Role, out var customRole)) - { - if (!customRole.CanUseSabotage) return false; - - DestroyableSingleton.Instance.ToggleMapVisible(new MapOptions() - { - Mode = MapOptions.Modes.Sabotage - }); - return false; - } - return true; - } } diff --git a/MiraAPI/Patches/Roles/HudManagerPatches.cs b/MiraAPI/Patches/Roles/HudManagerPatches.cs index 34f0bb9..f833651 100644 --- a/MiraAPI/Patches/Roles/HudManagerPatches.cs +++ b/MiraAPI/Patches/Roles/HudManagerPatches.cs @@ -27,10 +27,11 @@ public static class HudManagerPatches { var flag = localPlayer.Data != null && localPlayer.Data.IsDead; - if (role is ICustomRole) + if (role is ICustomRole customRole) { __instance.KillButton.ToggleVisible(isActive && role.CanUseKillButton && !flag); __instance.ImpostorVentButton.ToggleVisible(isActive && role.CanVent && !flag); + __instance.SabotageButton.gameObject.SetActive(isActive && customRole.CanUseSabotage); } if (_roleTab) diff --git a/MiraAPI/Patches/Roles/SabotageButtonPatch.cs b/MiraAPI/Patches/Roles/SabotageButtonPatch.cs new file mode 100644 index 0000000..49c5190 --- /dev/null +++ b/MiraAPI/Patches/Roles/SabotageButtonPatch.cs @@ -0,0 +1,62 @@ +using HarmonyLib; +using MiraAPI.Roles; + +namespace MiraAPI.Patches.Roles; + +[HarmonyPatch(typeof(SabotageButton))] +public class SabotageButtonPatch +{ + /// + /// Patches the Sabotage button to check if the player's custom role can use sabotage. + /// + [HarmonyPatch(nameof(SabotageButton.DoClick))] + [HarmonyPrefix] + public static bool DoClickPrefix(SabotageButton __instance) + { + var player = PlayerControl.LocalPlayer; + + if (player.Data.Role is not ICustomRole customRole) + { + return true; + } + + if (!customRole.CanUseSabotage || PlayerControl.LocalPlayer.inVent || !GameManager.Instance.SabotagesEnabled()) + { + return false; + } + + DestroyableSingleton.Instance.ToggleMapVisible( + new MapOptions + { + Mode = MapOptions.Modes.Sabotage, + }); + return false; + } + + [HarmonyPrefix] + [HarmonyPatch(nameof(SabotageButton.Refresh))] + public static bool RefreshPrefix(SabotageButton __instance) + { + var player = PlayerControl.LocalPlayer; + if (GameManager.Instance == null || player == null) + { + __instance.ToggleVisible(false); + __instance.SetDisabled(); + return false; + } + + if (player.Data.Role is not ICustomRole customRole) + { + return true; + } + + if (player.inVent || !GameManager.Instance.SabotagesEnabled() || player.petting) + { + __instance.ToggleVisible(customRole.CanUseSabotage && GameManager.Instance.SabotagesEnabled()); + __instance.SetDisabled(); + return false; + } + __instance.SetEnabled(); + return false; + } +}