From bd644927db7f08d9655c2f41a787e96cae4baebd Mon Sep 17 00:00:00 2001 From: winstxnhdw Date: Mon, 14 Oct 2024 01:13:25 +0100 Subject: [PATCH] refactor: convert existing coroutines to Tasks --- lc-hax/Scripts/Commands/Bomb/BombCommand.cs | 14 +++------ lc-hax/Scripts/Commands/FakeDeathCommand.cs | 5 ++- lc-hax/Scripts/Commands/MaskCommand.cs | 15 +++++---- .../Commands/Unlockable/HornCommand.cs | 31 ++++++++++++------- lc-hax/Scripts/Helpers/FindObjects.cs | 13 ++++++++ lc-hax/Scripts/Helpers/WaitUntil.cs | 11 +++++++ 6 files changed, 57 insertions(+), 32 deletions(-) create mode 100644 lc-hax/Scripts/Helpers/WaitUntil.cs diff --git a/lc-hax/Scripts/Commands/Bomb/BombCommand.cs b/lc-hax/Scripts/Commands/Bomb/BombCommand.cs index a5ebfd6c..db3fbaec 100644 --- a/lc-hax/Scripts/Commands/Bomb/BombCommand.cs +++ b/lc-hax/Scripts/Commands/Bomb/BombCommand.cs @@ -1,15 +1,9 @@ using System.Threading; using System.Threading.Tasks; -using System; using GameNetcodeStuff; [Command("bomb")] class BombCommand : ICommand, IJetpack { - Action BlowUpLocation(PlayerControllerB localPlayer, PlayerControllerB targetPlayer, JetpackItem jetpack) => () => { - localPlayer.DiscardHeldObject(placeObject: true, parentObjectTo: targetPlayer.NetworkObject); - Helper.ShortDelay(jetpack.ExplodeJetpackServerRpc); - }; - public async Task Execute(string[] args, CancellationToken cancellationToken) { if (Helper.LocalPlayer is not PlayerControllerB localPlayer) return; if (args.Length is 0) { @@ -32,8 +26,10 @@ public async Task Execute(string[] args, CancellationToken cancellationToken) { return; } - Helper.CreateComponent("Throw Bomb") - .SetPredicate(() => localPlayer.ItemSlots[localPlayer.currentItemSlot] == jetpack) - .Init(this.BlowUpLocation(localPlayer, targetPlayer, jetpack)); + GrabbableObject[] itemSlots = localPlayer.ItemSlots; + await Helper.WaitUntil(() => itemSlots[localPlayer.currentItemSlot] == jetpack, cancellationToken); + localPlayer.DiscardHeldObject(placeObject: true, parentObjectTo: targetPlayer.NetworkObject); + await Task.Delay(500, cancellationToken); + jetpack.ExplodeJetpackServerRpc(); } } diff --git a/lc-hax/Scripts/Commands/FakeDeathCommand.cs b/lc-hax/Scripts/Commands/FakeDeathCommand.cs index 0a2f495a..06d28aa5 100644 --- a/lc-hax/Scripts/Commands/FakeDeathCommand.cs +++ b/lc-hax/Scripts/Commands/FakeDeathCommand.cs @@ -19,8 +19,7 @@ public async Task Execute(string[] args, CancellationToken cancellationToken) { 0 ); - Helper.CreateComponent("Respawn") - .SetPredicate(() => player.playersManager.shipIsLeaving) - .Init(player.KillPlayer); + await Helper.WaitUntil(() => player.playersManager.shipIsLeaving, cancellationToken); + player.KillPlayer(); } } diff --git a/lc-hax/Scripts/Commands/MaskCommand.cs b/lc-hax/Scripts/Commands/MaskCommand.cs index 8894fe5a..ad752ab3 100644 --- a/lc-hax/Scripts/Commands/MaskCommand.cs +++ b/lc-hax/Scripts/Commands/MaskCommand.cs @@ -4,11 +4,6 @@ [Command("mask")] class MaskCommand : ICommand { - void SpawnMimicOnPlayer(PlayerControllerB player, HauntedMaskItem mask, ulong amount = 1) => - Helper.CreateComponent("Mask").Init(_ => { - mask.CreateMimicServerRpc(player.isInsideFactory, player.transform.position); - }, amount * 0.1f, 0.1f); - public async Task Execute(string[] args, CancellationToken cancellationToken) { if (Helper.LocalPlayer is not PlayerControllerB localPlayer) return; if (Helper.Grabbables.First(grabbable => grabbable is HauntedMaskItem) is not HauntedMaskItem hauntedMaskItem) { @@ -35,8 +30,12 @@ public async Task Execute(string[] args, CancellationToken cancellationToken) { return; } - Helper.CreateComponent() - .SetPredicate(() => localPlayer.ItemSlots[localPlayer.currentItemSlot] is HauntedMaskItem) - .Init(() => this.SpawnMimicOnPlayer(targetPlayer, hauntedMaskItem, amount)); + GrabbableObject[] itemSlots = localPlayer.ItemSlots; + await Helper.WaitUntil(() => localPlayer.ItemSlots[localPlayer.currentItemSlot] is HauntedMaskItem, cancellationToken); + + for (ulong i = 0; i < amount; i++) { + hauntedMaskItem.CreateMimicServerRpc(targetPlayer.isInsideFactory, targetPlayer.transform.position); + await Task.Delay(100, cancellationToken); + } } } diff --git a/lc-hax/Scripts/Commands/Unlockable/HornCommand.cs b/lc-hax/Scripts/Commands/Unlockable/HornCommand.cs index 3ca5fb27..97a866cd 100644 --- a/lc-hax/Scripts/Commands/Unlockable/HornCommand.cs +++ b/lc-hax/Scripts/Commands/Unlockable/HornCommand.cs @@ -1,31 +1,38 @@ +using System; using System.Threading; using System.Threading.Tasks; -using System; [Command("horn")] class HornCommand : ICommand { - Action PullHornLater(int hornDuration) => () => { - ShipAlarmCord? shipAlarmCord = Helper.FindObject(); - shipAlarmCord?.PullCordServerRpc(-1); - - Helper.CreateComponent("Stop Pulling Horn") - .SetPredicate(time => time >= hornDuration) - .Init(() => shipAlarmCord?.StopPullingCordServerRpc(-1)); - }; - public async Task Execute(string[] args, CancellationToken cancellationToken) { if (args.Length is 0) { Chat.Print("Usage: horn "); return; } - if (!int.TryParse(args[0], out int hornDuration)) { + if (!int.TryParse(args[0], out int hornDurationSeconds)) { Chat.Print("Invalid duration!"); return; } Helper.BuyUnlockable(Unlockable.LOUD_HORN); Helper.ReturnUnlockable(Unlockable.LOUD_HORN); - Helper.ShortDelay(this.PullHornLater(hornDuration)); + + if (await Helper.FindObject(cancellationToken) is not ShipAlarmCord shipAlarmCord) { + return; + } + + try { + shipAlarmCord.PullCordServerRpc(-1); + await Task.Delay(hornDurationSeconds * 1000, cancellationToken); + } + + catch (Exception exception) { + throw exception; + } + + finally { + shipAlarmCord.StopPullingCordServerRpc(-1); + } } } diff --git a/lc-hax/Scripts/Helpers/FindObjects.cs b/lc-hax/Scripts/Helpers/FindObjects.cs index 0906e6b6..9e28257b 100644 --- a/lc-hax/Scripts/Helpers/FindObjects.cs +++ b/lc-hax/Scripts/Helpers/FindObjects.cs @@ -1,3 +1,5 @@ +using System.Threading; +using System.Threading.Tasks; using UnityEngine; static partial class Helper { @@ -5,4 +7,15 @@ static partial class Helper { internal static T[] FindObjects(FindObjectsSortMode sortMode = FindObjectsSortMode.None) where T : Component => Object.FindObjectsByType(sortMode); + + internal static async Task FindObject(CancellationToken cancellationToken) where T : Component { + T? obj = Helper.FindObject(); + + while (obj is null && !cancellationToken.IsCancellationRequested) { + await Task.Yield(); + obj = Helper.FindObject(); + } + + return obj; + } } diff --git a/lc-hax/Scripts/Helpers/WaitUntil.cs b/lc-hax/Scripts/Helpers/WaitUntil.cs new file mode 100644 index 00000000..a7e30b22 --- /dev/null +++ b/lc-hax/Scripts/Helpers/WaitUntil.cs @@ -0,0 +1,11 @@ +using System; +using System.Threading; +using System.Threading.Tasks; + +static partial class Helper { + internal static async Task WaitUntil(Func predicate, CancellationToken cancellationToken) { + while (!predicate() && !cancellationToken.IsCancellationRequested) { + await Task.Yield(); + } + } +}