diff --git a/MiraAPI/Patches/Options/OptionsPatches.cs b/MiraAPI/Patches/Options/OptionsPatches.cs index c61613c..1a388d8 100644 --- a/MiraAPI/Patches/Options/OptionsPatches.cs +++ b/MiraAPI/Patches/Options/OptionsPatches.cs @@ -1,5 +1,6 @@ using HarmonyLib; using Il2CppSystem; +using MiraAPI.Roles; using MiraAPI.Utilities; namespace MiraAPI.Patches.Options; @@ -7,6 +8,138 @@ namespace MiraAPI.Patches.Options; [HarmonyPatch] public static class OptionsPatches { + [HarmonyPrefix] + [HarmonyPatch(typeof(RoleOptionSetting), nameof(RoleOptionSetting.IncreaseChance))] + public static bool RoleIncreaseChancePrefix(RoleOptionSetting __instance) + { + if (!__instance.Role.IsCustomRole()) + { + return true; + } + + if (__instance.roleChance == 0) + { + __instance.roleMaxCount = 1; + } + + __instance.roleChance += 10; + if (__instance.roleChance > 100) + { + __instance.roleChance = 0; + } + + __instance.OnValueChanged.Invoke(__instance); + return false; + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(RoleOptionSetting), nameof(RoleOptionSetting.DecreaseChance))] + public static bool RoleDecreaseChancePrefix(RoleOptionSetting __instance) + { + if (!__instance.Role.IsCustomRole()) + { + return true; + } + + if (__instance.roleChance == 0) + { + __instance.roleMaxCount = 1; + } + + __instance.roleChance -= 10; + if (__instance.roleChance < 0) + { + __instance.roleChance = 100; + } + + __instance.OnValueChanged.Invoke(__instance); + return false; + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(RoleOptionSetting), nameof(RoleOptionSetting.IncreaseCount))] + public static bool RoleIncreaseCountPrefix(RoleOptionSetting __instance) + { + if (!__instance.Role.IsCustomRole()) + { + return true; + } + + if (__instance.roleMaxCount == 0) + { + __instance.roleChance = 50; + } + + __instance.roleMaxCount += 1; + if (__instance.roleMaxCount > __instance.role.MaxCount) + { + __instance.roleMaxCount = 0; + } + + __instance.OnValueChanged.Invoke(__instance); + return false; + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(RoleOptionSetting), nameof(RoleOptionSetting.DecreaseCount))] + public static bool RoleDecreaseCountPrefix(RoleOptionSetting __instance) + { + if (!__instance.Role.IsCustomRole()) + { + return true; + } + + if (__instance.roleMaxCount == 0) + { + __instance.roleChance = 50; + } + + __instance.roleMaxCount -= 1; + if (__instance.roleMaxCount < 0) + { + __instance.roleMaxCount = __instance.role.MaxCount; + } + + __instance.OnValueChanged.Invoke(__instance); + return false; + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(NumberOption), nameof(NumberOption.AdjustButtonsActiveState))] + [HarmonyPatch(typeof(StringOption), nameof(StringOption.AdjustButtonsActiveState))] + [HarmonyPatch(typeof(RoleOptionSetting), nameof(RoleOptionSetting.AdjustChanceButtonsActiveState))] + [HarmonyPatch(typeof(RoleOptionSetting), nameof(RoleOptionSetting.AdjustCountButtonsActiveState))] + public static bool AdjustButtonsPrefix(OptionBehaviour __instance) + { + if (__instance.IsCustom()) + { + if (__instance.TryCast() is { } numberOption) + { + numberOption.MinusBtn.SetInteractable(true); + numberOption.PlusBtn.SetInteractable(true); + } + if (__instance.TryCast() is { } stringOption) + { + stringOption.MinusBtn.SetInteractable(true); + stringOption.PlusBtn.SetInteractable(true); + } + + return false; + } + + if (__instance.TryCast() is { } roleOptionSetting && roleOptionSetting.Role.IsCustomRole()) + { + roleOptionSetting.CountMinusBtn.SetInteractable(true); + roleOptionSetting.CountPlusBtn.SetInteractable(true); + roleOptionSetting.ChanceMinusBtn.SetInteractable(true); + roleOptionSetting.ChancePlusBtn.SetInteractable(true); + + return false; + } + + return true; + } + [HarmonyPrefix] [HarmonyPatch(typeof(ToggleOption), nameof(ToggleOption.Initialize))] public static bool ToggleInit(ToggleOption __instance) @@ -16,8 +149,7 @@ public static class OptionsPatches return true; } - __instance.TitleText.text = - TranslationController.Instance.GetString(__instance.Title, Array.Empty()); + __instance.TitleText.text = TranslationController.Instance.GetString(__instance.Title, Array.Empty()); return false; } @@ -45,14 +177,6 @@ public static class OptionsPatches return !__instance.IsCustom(); } - [HarmonyPrefix] - [HarmonyPatch(typeof(NumberOption), nameof(NumberOption.AdjustButtonsActiveState))] - [HarmonyPatch(typeof(StringOption), nameof(StringOption.AdjustButtonsActiveState))] - public static bool AdjustButtonsPrefix(OptionBehaviour __instance) - { - return !__instance.IsCustom(); - } - [HarmonyPrefix] [HarmonyPatch(typeof(NumberOption), nameof(NumberOption.Initialize))] public static bool NumberInit(NumberOption __instance) diff --git a/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs b/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs index 0950bb5..44fc973 100644 --- a/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs +++ b/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs @@ -493,6 +493,10 @@ public static class RoleSettingMenuPatches roleOptionSetting.labelSprite.color = customRole.OptionsMenuColor; roleOptionSetting.OnValueChanged = new Action(ValueChanged); roleOptionSetting.SetClickMask(__instance.ButtonClickMask); + roleOptionSetting.ChanceMinusBtn.SetInteractable(true); + roleOptionSetting.ChancePlusBtn.SetInteractable(true); + roleOptionSetting.CountMinusBtn.SetInteractable(true); + roleOptionSetting.CountPlusBtn.SetInteractable(true); __instance.roleChances.Add(roleOptionSetting); roleOptionSetting.titleText.transform.localPosition = new Vector3(-0.5376f, -0.2923f, 0f); diff --git a/MiraAPI/Roles/CustomRoleUtils.cs b/MiraAPI/Roles/CustomRoleUtils.cs index 7f0ec29..01b402d 100644 --- a/MiraAPI/Roles/CustomRoleUtils.cs +++ b/MiraAPI/Roles/CustomRoleUtils.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using AmongUs.GameOptions; using MiraAPI.Utilities.Assets; +using Reactor.Utilities; using UnityEngine; namespace MiraAPI.Roles; @@ -56,7 +57,21 @@ public static class CustomRoleUtils public static LoadableAsset? GetIntroSound(RoleTypes roleType) { var role = RoleManager.Instance.AllRoles.FirstOrDefault(role => role.Role == roleType); - if (role is ICustomRole customRole) return customRole.Configuration.IntroSound; + if (role is ICustomRole customRole) + { + return customRole.Configuration.IntroSound; + } + return new PreloadedAsset(role!.IntroSound); } + + /// + /// Determines if a role is a custom role or not. + /// + /// The RoleBehaviour to check. + /// True if the role is a custom role, false otherwise. + public static bool IsCustomRole(this RoleBehaviour role) + { + return CustomRoleManager.CustomRoles.ContainsKey((ushort)role.Role); + } }