diff --git a/MiraAPI.Example/Buttons/ExampleButton.cs b/MiraAPI.Example/Buttons/ExampleButton.cs deleted file mode 100644 index 4c9c7d69..00000000 --- a/MiraAPI.Example/Buttons/ExampleButton.cs +++ /dev/null @@ -1,29 +0,0 @@ -using MiraAPI.Example.Modifiers; -using MiraAPI.Hud; -using MiraAPI.Utilities; -using MiraAPI.Utilities.Assets; -using Reactor.Utilities; -using UnityEngine; - -namespace MiraAPI.Example.Buttons; - -[RegisterButton] -public class ExampleButton : CustomActionButton -{ - public override string Name => "Example Button"; - public override float Cooldown => 5f; - public override float EffectDuration => 0f; - public override int MaxUses => 5; - public override LoadableAsset Sprite => ExampleAssets.ExampleButton; - public override ButtonLocation Location => ButtonLocation.BottomRight; - - protected override void OnClick() - { - Logger.Info("Example button clicked!"); - } - - public override bool Enabled(RoleBehaviour role) - { - return PlayerControl.LocalPlayer.HasModifier(); - } -} \ No newline at end of file diff --git a/MiraAPI.Example/Buttons/ExampleTargetButton.cs b/MiraAPI.Example/Buttons/Freezer/FreezeButton.cs similarity index 59% rename from MiraAPI.Example/Buttons/ExampleTargetButton.cs rename to MiraAPI.Example/Buttons/Freezer/FreezeButton.cs index aa368543..c04d4a44 100644 --- a/MiraAPI.Example/Buttons/ExampleTargetButton.cs +++ b/MiraAPI.Example/Buttons/Freezer/FreezeButton.cs @@ -1,20 +1,24 @@ -using MiraAPI.Hud; +using MiraAPI.Example.Modifiers.Freezer; +using MiraAPI.Example.Roles; +using MiraAPI.Hud; +using MiraAPI.Utilities; using MiraAPI.Utilities.Assets; using UnityEngine; -namespace MiraAPI.Example.Buttons; +namespace MiraAPI.Example.Buttons.Freezer; -public class ExampleTargetButton : CustomActionButton +[RegisterButton] +public class FreezeButton : CustomActionButton { - public override string Name => "Shoot"; - public override float Cooldown => 0f; + public override string Name => "Freeze"; + public override float Cooldown => 5f; public override float EffectDuration => 0f; public override int MaxUses => 0; public override LoadableAsset Sprite => ExampleAssets.ExampleButton; protected override void OnClick() { - PlayerControl.LocalPlayer.CmdCheckMurder(Target); + Target.AddModifier(); } public override PlayerControl GetTarget() @@ -24,17 +28,17 @@ public class ExampleTargetButton : CustomActionButton public override void SetOutline(bool active) { - Target?.cosmetics.SetOutline(active, new Il2CppSystem.Nullable(Color.red)); + Target?.cosmetics.SetOutline(active, new Il2CppSystem.Nullable(Palette.Blue)); } public override bool IsTargetValid(PlayerControl target) { - return PlayerControl.LocalPlayer.Data.Role.IsValidTarget(target.Data); + return true; } public override bool Enabled(RoleBehaviour role) { - return role.IsImpostor; + return role is FreezerRole; } } \ No newline at end of file diff --git a/MiraAPI.Example/Buttons/MeetingButton.cs b/MiraAPI.Example/Buttons/MeetingButton.cs new file mode 100644 index 00000000..c7f202d7 --- /dev/null +++ b/MiraAPI.Example/Buttons/MeetingButton.cs @@ -0,0 +1,47 @@ +using MiraAPI.Example; +using MiraAPI.Example.Modifiers; +using MiraAPI.Hud; +using MiraAPI.Utilities; +using MiraAPI.Utilities.Assets; +using UnityEngine; + +[RegisterButton] +public class MeetingButton : CustomActionButton +{ + public override string Name => "Call Meeting"; + + public override float Cooldown => 15; + + public override float EffectDuration => 0; + + public override int MaxUses => 3; + + public override LoadableAsset Sprite => ExampleAssets.ExampleButton; + + public override bool Enabled(RoleBehaviour role) + { + return PlayerControl.LocalPlayer.HasModifier(); + } + + protected override void OnClick() + { + var bt = ShipStatus.Instance.EmergencyButton; + + PlayerControl.LocalPlayer.NetTransform.Halt(); + var minigame = Object.Instantiate(bt.MinigamePrefab, Camera.main!.transform, false); + + var taskAdderGame = minigame as TaskAdderGame; + if (taskAdderGame != null) + { + taskAdderGame.SafePositionWorld = bt.SafePositionLocal + (Vector2)bt.transform.position; + } + + minigame.transform.localPosition = new Vector3(0f, 0f, -50f); + minigame.Begin(null); + + if (UsesLeft == 0 && PlayerControl.LocalPlayer.HasModifier()) + { + PlayerControl.LocalPlayer.RemoveModifier(); + } + } +} diff --git a/MiraAPI.Example/Buttons/Teleporter/TeleportButton.cs b/MiraAPI.Example/Buttons/Teleporter/TeleportButton.cs new file mode 100644 index 00000000..a860d362 --- /dev/null +++ b/MiraAPI.Example/Buttons/Teleporter/TeleportButton.cs @@ -0,0 +1,86 @@ +using MiraAPI.Example.Options.Roles; +using MiraAPI.Example.Roles; +using MiraAPI.GameOptions; +using MiraAPI.Hud; +using MiraAPI.Utilities.Assets; +using Reactor.Utilities; +using System.Collections; +using UnityEngine; + +namespace MiraAPI.Example.Buttons.Teleporter; +[RegisterButton] +public class TeleportButton : CustomActionButton +{ + public override string Name => "Teleport"; + + public override float Cooldown => OptionGroupSingleton.Instance.TeleportCooldown; + + public override float EffectDuration => OptionGroupSingleton.Instance.TeleportDuration; + + public override int MaxUses => 0; + + public override LoadableAsset Sprite => ExampleAssets.ExampleButton; + public static bool IsZoom { get; private set; } + + public override bool Enabled(RoleBehaviour role) + { + return role is TeleporterRole; + } + protected override void OnClick() + { + Coroutines.Start(ZoomOutCoroutine()); + } + + public override void OnEffectEnd() + { + Coroutines.Start(ZoomInCoroutine()); + } + + protected override void FixedUpdate(PlayerControl playerControl) + { + base.FixedUpdate(playerControl); + + if (!EffectActive) return; + + if (Input.GetKey(KeyCode.Mouse0)) + { + playerControl.NetTransform.RpcSnapTo(Camera.main.ScreenToWorldPoint(Input.mousePosition)); + ResetCooldown(); + } + } + + private static IEnumerator ZoomOutCoroutine() + { + HudManager.Instance.ShadowQuad.gameObject.SetActive(false); + IsZoom = true; + var zoomDistance = OptionGroupSingleton.Instance.ZoomDistance; + for (var ft = Camera.main!.orthographicSize; ft < zoomDistance; ft += 0.3f) + { + Camera.main.orthographicSize = MeetingHud.Instance ? 3f : ft; + ResolutionManager.ResolutionChanged.Invoke((float)Screen.width / Screen.height, Screen.width, Screen.height, Screen.fullScreen); + foreach (var cam in Camera.allCameras) cam.orthographicSize = Camera.main.orthographicSize; + yield return null; + } + + foreach (var cam in Camera.allCameras) cam.orthographicSize = zoomDistance; + ResolutionManager.ResolutionChanged.Invoke((float)Screen.width / Screen.height, Screen.width, Screen.height, Screen.fullScreen); + } + + private static IEnumerator ZoomInCoroutine() + { + for (var ft = Camera.main!.orthographicSize; ft > 3f; ft -= 0.3f) + { + Camera.main.orthographicSize = MeetingHud.Instance ? 3f : ft; + ResolutionManager.ResolutionChanged.Invoke((float)Screen.width / Screen.height, Screen.width, Screen.height, Screen.fullScreen); + foreach (var cam in Camera.allCameras) cam.orthographicSize = Camera.main.orthographicSize; + + yield return null; + } + + foreach (var cam in Camera.allCameras) cam.orthographicSize = 3f; + HudManager.Instance.ShadowQuad.gameObject.SetActive(true); + IsZoom = false; + + ResolutionManager.ResolutionChanged.Invoke((float)Screen.width / Screen.height, Screen.width, Screen.height, Screen.fullScreen); + } +} diff --git a/MiraAPI.Example/MiraDebugWindow.cs b/MiraAPI.Example/MiraDebugWindow.cs index 90c644a3..03327762 100644 --- a/MiraAPI.Example/MiraDebugWindow.cs +++ b/MiraAPI.Example/MiraDebugWindow.cs @@ -1,8 +1,8 @@ -using MiraAPI.Utilities; +using MiraAPI.Example.Modifiers; +using MiraAPI.Utilities; using Reactor.Utilities.Attributes; using Reactor.Utilities.ImGui; using System; -using MiraAPI.Example.Modifiers; using UnityEngine; namespace MiraAPI.Example; @@ -13,11 +13,11 @@ public class MiraDebugWindow(IntPtr ptr) : MonoBehaviour(ptr) { if (GUILayout.Button("Test modifier")) { - PlayerControl.LocalPlayer.AddModifier(); + //PlayerControl.LocalPlayer.AddModifier(); } if (GUILayout.Button("Remove modifier")) { - PlayerControl.LocalPlayer.RemoveModifier(); + //PlayerControl.LocalPlayer.RemoveModifier(); } }) { diff --git a/MiraAPI.Example/Modifiers/GameModifierExample.cs b/MiraAPI.Example/Modifiers/CaptainModifier.cs similarity index 59% rename from MiraAPI.Example/Modifiers/GameModifierExample.cs rename to MiraAPI.Example/Modifiers/CaptainModifier.cs index 2b491aef..80d9224a 100644 --- a/MiraAPI.Example/Modifiers/GameModifierExample.cs +++ b/MiraAPI.Example/Modifiers/CaptainModifier.cs @@ -4,15 +4,9 @@ using MiraAPI.Modifiers.Types; namespace MiraAPI.Example.Modifiers; [RegisterModifier] -public class GameModifierExample : GameModifier +public class CaptainModifier : GameModifier { - public override string ModifierName => "Example Game Modifier"; - - public override bool CanVent() - { - return true; - } - + public override string ModifierName => "Captain"; public override int GetAmountPerGame() { return 1; diff --git a/MiraAPI.Example/Modifiers/Freezer/FreezeModifier.cs b/MiraAPI.Example/Modifiers/Freezer/FreezeModifier.cs new file mode 100644 index 00000000..d1124063 --- /dev/null +++ b/MiraAPI.Example/Modifiers/Freezer/FreezeModifier.cs @@ -0,0 +1,30 @@ +using MiraAPI.Modifiers; +using MiraAPI.Modifiers.Types; + +namespace MiraAPI.Example.Modifiers.Freezer; + +[RegisterModifier] +public class FreezeModifier : TimedModifier +{ + public override string ModifierName => "Freezed"; + public override bool HideOnUi => false; + public override float Duration => 15f; + public override bool AutoStart => true; + public override bool RemoveOnComplete => true; + + public override void OnActivate() + { + if (Player.AmOwner) + { + Player.moveable = false; + } + } + + public override void OnTimerComplete() + { + if (Player.AmOwner) + { + Player.moveable = true; + } + } +} diff --git a/MiraAPI.Example/Modifiers/ModifierTimerExample.cs b/MiraAPI.Example/Modifiers/ModifierTimerExample.cs deleted file mode 100644 index 3b7e1fd5..00000000 --- a/MiraAPI.Example/Modifiers/ModifierTimerExample.cs +++ /dev/null @@ -1,21 +0,0 @@ -using MiraAPI.Modifiers; -using MiraAPI.Modifiers.Types; - -namespace MiraAPI.Example.Modifiers; - -[RegisterModifier] -public class ModifierTimerExample : TimedModifier -{ - public override string ModifierName => "Color Changer"; - public override bool HideOnUi => false; - public override float Duration => 10f; - public override bool AutoStart => true; - - private readonly System.Random _rand = new(); - - public override void OnTimerComplete() - { - Player.RpcSetColor((byte)_rand.Next(0, Palette.PlayerColors.Count)); - StartTimer(); - } -} diff --git a/MiraAPI.Example/Options/Roles/CustomRole2Settings.cs b/MiraAPI.Example/Options/Roles/CustomRole2Settings.cs index 97450357..e10931bf 100644 --- a/MiraAPI.Example/Options/Roles/CustomRole2Settings.cs +++ b/MiraAPI.Example/Options/Roles/CustomRole2Settings.cs @@ -9,7 +9,7 @@ public class CustomRole2Settings : AbstractOptionGroup { public override string GroupName => "Custom Role"; - public override Type AdvancedRole => typeof(CustomRole2); + public override Type AdvancedRole => typeof(FreezerRole); [ModdedToggleOption("Teleportation ability")] public bool Teleport { get; set; } = true; diff --git a/MiraAPI.Example/Options/Roles/CustomRoleSettings.cs b/MiraAPI.Example/Options/Roles/CustomRoleSettings.cs deleted file mode 100644 index e2ca4a68..00000000 --- a/MiraAPI.Example/Options/Roles/CustomRoleSettings.cs +++ /dev/null @@ -1,16 +0,0 @@ -using MiraAPI.Example.Roles; -using MiraAPI.GameOptions; -using MiraAPI.GameOptions.Attributes; -using System; - -namespace MiraAPI.Example.Options.Roles; - -public class CustomRoleSettings : AbstractOptionGroup -{ - public override string GroupName => "Custom Role"; - - public override Type AdvancedRole => typeof(CustomRole); - - [ModdedNumberOption("Number Opt", min: 3, max: 9)] public float NumberOpt { get; set; } = 4; - -} \ No newline at end of file diff --git a/MiraAPI.Example/Options/Roles/TeleporterOptions.cs b/MiraAPI.Example/Options/Roles/TeleporterOptions.cs new file mode 100644 index 00000000..3e51e334 --- /dev/null +++ b/MiraAPI.Example/Options/Roles/TeleporterOptions.cs @@ -0,0 +1,23 @@ +using MiraAPI.Example.Roles; +using MiraAPI.GameOptions; +using MiraAPI.GameOptions.Attributes; +using MiraAPI.Utilities; +using System; + +namespace MiraAPI.Example.Options.Roles; + +public class TeleporterOptions : AbstractOptionGroup +{ + public override string GroupName => "Teleporter"; + + public override Type AdvancedRole => typeof(TeleporterRole); + + [ModdedNumberOption("Teleport Cooldown", 5, 60, 2.5f, MiraNumberSuffixes.Seconds)] + public float TeleportCooldown { get; set; } = 5; + + [ModdedNumberOption("Teleport Duration", 5, 25, 1, MiraNumberSuffixes.Seconds)] + public float TeleportDuration { get; set; } = 10; + + [ModdedNumberOption("Zoom Distance", 4, 15)] + public float ZoomDistance { get; set; } = 6; +} \ No newline at end of file diff --git a/MiraAPI.Example/Roles/CustomRole.cs b/MiraAPI.Example/Roles/FreezerRole.cs similarity index 58% rename from MiraAPI.Example/Roles/CustomRole.cs rename to MiraAPI.Example/Roles/FreezerRole.cs index 7a042839..7415dd67 100644 --- a/MiraAPI.Example/Roles/CustomRole.cs +++ b/MiraAPI.Example/Roles/FreezerRole.cs @@ -5,12 +5,12 @@ using UnityEngine; namespace MiraAPI.Example.Roles; [RegisterCustomRole] -public class CustomRole : ImpostorRole, ICustomRole +public class FreezerRole : ImpostorRole, ICustomRole { - public string RoleName => "Custom Role"; - public string RoleLongDescription => "Eliminate everyone with your special ability!"; + public string RoleName => "Freezer"; + public string RoleLongDescription => "Freeze another player for a duration of time."; public string RoleDescription => RoleLongDescription; - public Color RoleColor => Palette.Black; + public Color RoleColor => Palette.Blue; public ModdedRoleTeams Team => ModdedRoleTeams.Impostor; public LoadableAsset OptionsScreenshot => ExampleAssets.Banner; diff --git a/MiraAPI.Example/Roles/CustomRole2.cs b/MiraAPI.Example/Roles/TeleporterRole.cs similarity index 57% rename from MiraAPI.Example/Roles/CustomRole2.cs rename to MiraAPI.Example/Roles/TeleporterRole.cs index ce0650dd..be7528b7 100644 --- a/MiraAPI.Example/Roles/CustomRole2.cs +++ b/MiraAPI.Example/Roles/TeleporterRole.cs @@ -5,12 +5,12 @@ using UnityEngine; namespace MiraAPI.Example.Roles; [RegisterCustomRole] -public class CustomRole2 : CrewmateRole, ICustomRole +public class TeleporterRole : CrewmateRole, ICustomRole { - public string RoleName => "Custom Role 2"; - public string RoleLongDescription => "Save everyone from dying!"; + public string RoleName => "Teleporter"; + public string RoleLongDescription => "Zoom out and teleport across the map!"; public string RoleDescription => RoleLongDescription; - public Color RoleColor => Palette.AcceptedGreen; + public Color RoleColor => new Color32(221, 176, 152, 255); public ModdedRoleTeams Team => ModdedRoleTeams.Crewmate; public LoadableAsset OptionsScreenshot => ExampleAssets.Banner; diff --git a/MiraAPI/Hud/CustomActionButton.cs b/MiraAPI/Hud/CustomActionButton.cs index c2db82dc..c24f5eaa 100644 --- a/MiraAPI/Hud/CustomActionButton.cs +++ b/MiraAPI/Hud/CustomActionButton.cs @@ -71,7 +71,7 @@ public abstract class CustomActionButton /// The button object in game. This is created by Mira API automatically. /// protected ActionButton? Button { get; private set; } - + internal void CreateButton(Transform parent) { if (Button) @@ -110,10 +110,10 @@ public abstract class CustomActionButton { OnEffectEnd(); } - + EffectActive = false; } - + /// /// A utility function to override the sprite of the button. /// @@ -240,7 +240,7 @@ public abstract class CustomActionButton { Button?.SetDisabled(); } - + Button?.SetCoolDown(Timer, EffectActive ? EffectDuration : Cooldown); FixedUpdate(playerControl); @@ -257,12 +257,12 @@ public abstract class CustomActionButton : CustomActionButton where T : MonoB /// The target object of the button. /// public T? Target { get; private set; } - + /// /// The distance the player must be from the target object to use the button. /// public virtual float Distance => PlayerControl.LocalPlayer.Data.Role.GetAbilityDistance(); - + /// /// An optional collider tag to filter the target object by. /// @@ -286,8 +286,8 @@ public abstract class CustomActionButton : CustomActionButton where T : MonoB /// /// Should the outline be active public abstract void SetOutline(bool active); - - + + public override bool CanUse() { var newTarget = GetTarget(); @@ -298,7 +298,7 @@ public abstract class CustomActionButton : CustomActionButton where T : MonoB Target = IsTargetValid(newTarget) ? newTarget : null; SetOutline(true); - + return base.CanUse() && Target; } } \ No newline at end of file diff --git a/MiraAPI/MiraApiPlugin.cs b/MiraAPI/MiraApiPlugin.cs index f04e4543..918b53bb 100644 --- a/MiraAPI/MiraApiPlugin.cs +++ b/MiraAPI/MiraApiPlugin.cs @@ -26,7 +26,7 @@ public partial class MiraApiPlugin : BasePlugin { Harmony.PatchAll(); - ReactorCredits.Register("Mira API", Version.Truncate(11, "") ?? Version, true, ReactorCredits.AlwaysShow); + ReactorCredits.Register("Mira API", Version, true, ReactorCredits.AlwaysShow); PluginManager = new MiraPluginManager(); PluginManager.Initialize(); diff --git a/MiraAPI/Modifiers/ModifierComponent.cs b/MiraAPI/Modifiers/ModifierComponent.cs index 7558ffd1..55c3f30f 100644 --- a/MiraAPI/Modifiers/ModifierComponent.cs +++ b/MiraAPI/Modifiers/ModifierComponent.cs @@ -84,7 +84,7 @@ public class ModifierComponent(IntPtr ptr) : MonoBehaviour(ptr) var modifierComponent = target.GetModifierComponent(); - var modifier = modifierComponent.ActiveModifiers.Find(x => x.GetType() == type); + var modifier = modifierComponent?.ActiveModifiers.Find(x => x.GetType() == type); if (modifier is null) { diff --git a/MiraAPI/Modifiers/ModifierManager.cs b/MiraAPI/Modifiers/ModifierManager.cs index f853e0ee..c45cc01e 100644 --- a/MiraAPI/Modifiers/ModifierManager.cs +++ b/MiraAPI/Modifiers/ModifierManager.cs @@ -7,6 +7,7 @@ using Reactor.Utilities.Extensions; using System; using System.Collections.Generic; using System.Linq; +using Reactor.Utilities; using Random = System.Random; namespace MiraAPI.Modifiers; @@ -41,12 +42,27 @@ public static class ModifierManager List filteredModifiers = []; - foreach (var modifier in IdToTypeModifiers.Where(pair => pair.Value.GetType() == typeof(GameModifier))) + foreach (var modifier in IdToTypeModifiers.Where(pair => pair.Value.IsAssignableTo(typeof(GameModifier)))) { var mod = (GameModifier)Activator.CreateInstance(modifier.Value); - var num = mod.GetAmountPerGame(); - var chance = mod.GetAssignmentChance(); + if (mod is null) + { + Logger.Error($"Failed to create instance of {modifier.Value.Name}"); + continue; + } + + if (!plrs.Any(x=>IsGameModifierValid(x, mod, modifier.Key))) + { + Logger.Warning("No players are valid for modifier: " + mod.ModifierName); + continue; + } + + var maxCount = plrs.Count(x=>IsGameModifierValid(x, mod, modifier.Key)); + + var num = Math.Clamp(mod.GetAmountPerGame(), 0, maxCount); + var chance = mod.GetAssignmentChance(); + for (var i = 0; i < num; i++) { var randomNum = rand.Next(100); @@ -63,23 +79,28 @@ public static class ModifierManager { shuffledModifiers = shuffledModifiers.GetRange(0, plrs.Count); } - + while (shuffledModifiers.Count > 0) { var id = shuffledModifiers[0]; var mod = (GameModifier)Activator.CreateInstance(IdToTypeModifiers[id]); + + if (mod is null) + { + Logger.Error($"Failed to create instance of {IdToTypeModifiers[id].Name}"); + continue; + } + + if (!plrs.Any(x=>IsGameModifierValid(x, mod, id))) + { + shuffledModifiers.RemoveAt(0); + continue; + } + var plr = plrs.Random(); - if (plr.Data.Role is ICustomRole modRole) - { - if (!modRole.IsModifierApplicable(mod)) - { - continue; - } - } - - if (plr.HasModifier(id) || mod.IsModifierValidOn(plr.Data.Role)) + if (!IsGameModifierValid(plr, mod, id)) { continue; } @@ -89,6 +110,13 @@ public static class ModifierManager } } + private static bool IsGameModifierValid(PlayerControl player, GameModifier modifier, uint modifierId) + { + return (player.Data.Role is not ICustomRole role || role.IsModifierApplicable(modifier)) && + modifier.IsModifierValidOn(player.Data.Role) && + !player.HasModifier(modifierId); + } + internal static void SyncAllModifiers(int targetId = -1) { var data = new List(); diff --git a/MiraAPI/Modifiers/Types/TimedModifier.cs b/MiraAPI/Modifiers/Types/TimedModifier.cs index 3dff8b4b..b37c80ed 100644 --- a/MiraAPI/Modifiers/Types/TimedModifier.cs +++ b/MiraAPI/Modifiers/Types/TimedModifier.cs @@ -6,6 +6,7 @@ public abstract class TimedModifier : BaseModifier { public abstract float Duration { get; } public virtual bool AutoStart => false; + public virtual bool RemoveOnComplete => true; public abstract void OnTimerComplete(); public bool TimerActive { get; protected set; } @@ -28,6 +29,11 @@ public abstract class TimedModifier : BaseModifier TimerActive = false; TimeRemaining = Duration; OnTimerComplete(); + + if (RemoveOnComplete) + { + ModifierComponent.RpcRemoveModifier(Player, ModifierId); + } } } diff --git a/MiraAPI/Patches/Options/GameSettingMenuPatches.cs b/MiraAPI/Patches/Options/GameSettingMenuPatches.cs index 026d6c8f..b74581bf 100644 --- a/MiraAPI/Patches/Options/GameSettingMenuPatches.cs +++ b/MiraAPI/Patches/Options/GameSettingMenuPatches.cs @@ -1,4 +1,5 @@ -using HarmonyLib; +#nullable enable +using HarmonyLib; using MiraAPI.PluginLoading; using MiraAPI.Utilities.Assets; using Reactor.Utilities.Extensions; @@ -17,9 +18,9 @@ public static class GameSettingMenuPatches { public static int CurrentSelectedMod { get; private set; } - public static MiraPluginInfo SelectedMod { get; private set; } + public static MiraPluginInfo? SelectedMod { get; private set; } - private static TextMeshPro _text; + private static TextMeshPro? _text; private static Vector3 _roleBtnOgPos; @@ -80,12 +81,12 @@ public static class GameSettingMenuPatches private static void UpdateText(GameSettingMenu menu, GameOptionsMenu settings, RolesSettingsMenu roles) { - if (CurrentSelectedMod == 0) + if (_text is not null && CurrentSelectedMod == 0) { _text.text = "Default"; _text.fontSizeMax = 3.2f; } - else + else if (_text is not null) { _text.fontSizeMax = 2.3f; SelectedMod = MiraPluginManager.Instance.RegisteredPlugins.ElementAt(CurrentSelectedMod - 1).Value; @@ -153,7 +154,7 @@ public static class GameSettingMenuPatches roles.scrollBar.ScrollToTop(); } - if (settings?.Children != null && SelectedMod?.OptionGroups.Count != 0) + if (settings.Children != null && SelectedMod?.OptionGroups.Count != 0) { foreach (var child in settings.Children) { diff --git a/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs b/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs index 0e863269..b65cc2b0 100644 --- a/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs +++ b/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs @@ -263,27 +263,28 @@ public static class RoleSettingMenuPatches roleOptionSetting.titleText.transform.localPosition = new Vector3(-0.5376f, -0.2923f, 0f); roleOptionSetting.titleText.color = customRole.RoleColor.GetAlternateColor(); roleOptionSetting.titleText.horizontalAlignment = HorizontalAlignmentOptions.Left; - - var newButton = Object.Instantiate(roleOptionSetting.buttons[0], roleOptionSetting.transform); - newButton.name = "ConfigButton"; - newButton.transform.localPosition = new Vector3(0.4473f, -0.3f, -2f); - newButton.transform.FindChild("Text_TMP").gameObject.DestroyImmediate(); - newButton.activeSprites.Destroy(); - - var btnRend = newButton.transform.FindChild("ButtonSprite").GetComponent(); - btnRend.sprite = MiraAssets.Cog.LoadAsset(); - - var passiveButton = newButton.GetComponent(); - passiveButton.OnClick = new ButtonClickedEvent(); - passiveButton.interactableColor = btnRend.color = customRole.RoleColor.GetAlternateColor(); - passiveButton.interactableHoveredColor = Color.white; - - passiveButton.OnClick.AddListener((UnityAction)(() => + + if (GameSettingMenuPatches.SelectedMod is null || + GameSettingMenuPatches.SelectedMod.Options.Any(x => x.AdvancedRole != null && x.AdvancedRole.IsInstanceOfType(role))) { - ChangeTab(role, __instance); - })); + var newButton = Object.Instantiate(roleOptionSetting.buttons[0], roleOptionSetting.transform); + newButton.name = "ConfigButton"; + newButton.transform.localPosition = new Vector3(0.4473f, -0.3f, -2f); + newButton.transform.FindChild("Text_TMP").gameObject.DestroyImmediate(); + newButton.activeSprites.Destroy(); - if (index < GameSettingMenuPatches.SelectedMod.CustomRoles.Count - 1) + var btnRend = newButton.transform.FindChild("ButtonSprite").GetComponent(); + btnRend.sprite = MiraAssets.Cog.LoadAsset(); + + var passiveButton = newButton.GetComponent(); + passiveButton.OnClick = new ButtonClickedEvent(); + passiveButton.interactableColor = btnRend.color = customRole.RoleColor.GetAlternateColor(); + passiveButton.interactableHoveredColor = Color.white; + + passiveButton.OnClick.AddListener((UnityAction)(() => { ChangeTab(role, __instance); })); + } + + if (index < GameSettingMenuPatches.SelectedMod?.CustomRoles.Count - 1) { yPos += -0.43f; }