From a562484006ab37de7d6f004f29c30e3e67342cdc Mon Sep 17 00:00:00 2001 From: MistaOmega Date: Fri, 15 Dec 2023 15:06:22 +0000 Subject: [PATCH 01/11] Modified PlayerTeleportedProcessor.cs to include check for vehicle, and to teleport that Rigidbody if present --- .../Processors/PlayerTeleportedProcessor.cs | 54 +++++++++++++++++-- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/NitroxClient/Communication/Packets/Processors/PlayerTeleportedProcessor.cs b/NitroxClient/Communication/Packets/Processors/PlayerTeleportedProcessor.cs index dae6422b4c..a8d321e18d 100644 --- a/NitroxClient/Communication/Packets/Processors/PlayerTeleportedProcessor.cs +++ b/NitroxClient/Communication/Packets/Processors/PlayerTeleportedProcessor.cs @@ -1,5 +1,6 @@ using System; using NitroxClient.Communication.Packets.Processors.Abstract; +using NitroxClient.GameLogic; using NitroxClient.MonoBehaviours; using NitroxModel.Packets; using NitroxModel_Subnautica.DataStructures; @@ -30,17 +31,64 @@ public override void Process(PlayerTeleported packet) Player.main.SetCurrentSub(subRoot); } - - Player.main.SetPosition(packet.DestinationTo.ToUnity()); + + Vehicle currentVehicle = Player.main.currentMountedVehicle; + // Check to make sure the player is in a vehicle + if(currentVehicle != null) + { + Rigidbody vehicleRigidbody = currentVehicle.GetComponent(); + if (vehicleRigidbody != null) + { + if (!vehicleRigidbody.isKinematic) // let's do kinematic switching only if needed + { + // Make the rigidbody kinematic + vehicleRigidbody.isKinematic = true; + Quaternion preservedRotation = currentVehicle.transform.rotation; // Preserving the current rotation + currentVehicle.teleporting = true; + // Teleport the vehicle by modifying its position + vehicleRigidbody.position = packet.DestinationTo.ToUnity(); + currentVehicle.transform.rotation = preservedRotation; // Applying the preserved rotation after teleportation + currentVehicle.teleporting = false; + // Reset the RB kinematic to false + vehicleRigidbody.isKinematic = false; + + } + else + { + Quaternion preservedRotation = currentVehicle.transform.rotation; + currentVehicle.teleporting = true; + vehicleRigidbody.position = packet.DestinationTo.ToUnity(); + currentVehicle.transform.rotation = preservedRotation; + currentVehicle.teleporting = false; + } + RunTerrainLoadCoroutine(); + } + else + { + Log.Error("Tried to teleport vehicle, but was unable to acquire the RigidBody component"); + } + } + else + { + Player.main.SetPosition(packet.DestinationTo.ToUnity()); + RunTerrainLoadCoroutine(); + } + } + + private static void RunTerrainLoadCoroutine() + { // Freeze the player while he's loading its new position Player.main.cinematicModeActive = true; try { CoroutineHost.StartCoroutine(Terrain.WaitForWorldLoad()); - } catch (Exception e) + } + catch (Exception e) { Player.main.cinematicModeActive = false; Log.Warn($"Something wrong happened while waiting for the terrain to load.\n{e}"); } } } + + From 717fc40a840b0a70b5d52dd448377f526affb31a Mon Sep 17 00:00:00 2001 From: MistaOmega Date: Fri, 15 Dec 2023 15:08:01 +0000 Subject: [PATCH 02/11] Code cleanup --- .../Packets/Processors/PlayerTeleportedProcessor.cs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/NitroxClient/Communication/Packets/Processors/PlayerTeleportedProcessor.cs b/NitroxClient/Communication/Packets/Processors/PlayerTeleportedProcessor.cs index a8d321e18d..be573f153e 100644 --- a/NitroxClient/Communication/Packets/Processors/PlayerTeleportedProcessor.cs +++ b/NitroxClient/Communication/Packets/Processors/PlayerTeleportedProcessor.cs @@ -1,9 +1,8 @@ using System; using NitroxClient.Communication.Packets.Processors.Abstract; -using NitroxClient.GameLogic; using NitroxClient.MonoBehaviours; -using NitroxModel.Packets; using NitroxModel_Subnautica.DataStructures; +using NitroxModel.Packets; using UnityEngine; using UWE; using Terrain = NitroxClient.GameLogic.Terrain; @@ -22,7 +21,7 @@ public override void Process(PlayerTeleported packet) if (subRoot.isCyclops) { // Reversing calculations from PlayerMovementBroadcaster.Update() - Vector3 position = (subRoot.transform.rotation * packet.DestinationTo.ToUnity()) + subRoot.transform.position; + Vector3 position = subRoot.transform.rotation * packet.DestinationTo.ToUnity() + subRoot.transform.position; Player.main.SetPosition(position); Player.main.SetCurrentSub(subRoot); @@ -34,7 +33,7 @@ public override void Process(PlayerTeleported packet) Vehicle currentVehicle = Player.main.currentMountedVehicle; // Check to make sure the player is in a vehicle - if(currentVehicle != null) + if (currentVehicle != null) { Rigidbody vehicleRigidbody = currentVehicle.GetComponent(); if (vehicleRigidbody != null) @@ -51,7 +50,6 @@ public override void Process(PlayerTeleported packet) currentVehicle.teleporting = false; // Reset the RB kinematic to false vehicleRigidbody.isKinematic = false; - } else { @@ -61,6 +59,7 @@ public override void Process(PlayerTeleported packet) currentVehicle.transform.rotation = preservedRotation; currentVehicle.teleporting = false; } + RunTerrainLoadCoroutine(); } else @@ -90,5 +89,3 @@ private static void RunTerrainLoadCoroutine() } } } - - From 17e0c2f5593fcf56b0ca0c2c7544f84b61517819 Mon Sep 17 00:00:00 2001 From: MistaOmega Date: Fri, 15 Dec 2023 15:43:13 +0000 Subject: [PATCH 03/11] Switch to calling currentVehicle.TeleportVehicle --- .../Processors/PlayerTeleportedProcessor.cs | 60 ++++--------------- 1 file changed, 13 insertions(+), 47 deletions(-) diff --git a/NitroxClient/Communication/Packets/Processors/PlayerTeleportedProcessor.cs b/NitroxClient/Communication/Packets/Processors/PlayerTeleportedProcessor.cs index be573f153e..864a53c06b 100644 --- a/NitroxClient/Communication/Packets/Processors/PlayerTeleportedProcessor.cs +++ b/NitroxClient/Communication/Packets/Processors/PlayerTeleportedProcessor.cs @@ -35,57 +35,23 @@ public override void Process(PlayerTeleported packet) // Check to make sure the player is in a vehicle if (currentVehicle != null) { - Rigidbody vehicleRigidbody = currentVehicle.GetComponent(); - if (vehicleRigidbody != null) - { - if (!vehicleRigidbody.isKinematic) // let's do kinematic switching only if needed - { - // Make the rigidbody kinematic - vehicleRigidbody.isKinematic = true; - Quaternion preservedRotation = currentVehicle.transform.rotation; // Preserving the current rotation - currentVehicle.teleporting = true; - // Teleport the vehicle by modifying its position - vehicleRigidbody.position = packet.DestinationTo.ToUnity(); - currentVehicle.transform.rotation = preservedRotation; // Applying the preserved rotation after teleportation - currentVehicle.teleporting = false; - // Reset the RB kinematic to false - vehicleRigidbody.isKinematic = false; - } - else - { - Quaternion preservedRotation = currentVehicle.transform.rotation; - currentVehicle.teleporting = true; - vehicleRigidbody.position = packet.DestinationTo.ToUnity(); - currentVehicle.transform.rotation = preservedRotation; - currentVehicle.teleporting = false; - } - - RunTerrainLoadCoroutine(); - } - else - { - Log.Error("Tried to teleport vehicle, but was unable to acquire the RigidBody component"); - } + currentVehicle.TeleportVehicle(packet.DestinationTo.ToUnity(), currentVehicle.transform.rotation); } else { Player.main.SetPosition(packet.DestinationTo.ToUnity()); - RunTerrainLoadCoroutine(); - } - } - - private static void RunTerrainLoadCoroutine() - { - // Freeze the player while he's loading its new position - Player.main.cinematicModeActive = true; - try - { - CoroutineHost.StartCoroutine(Terrain.WaitForWorldLoad()); - } - catch (Exception e) - { - Player.main.cinematicModeActive = false; - Log.Warn($"Something wrong happened while waiting for the terrain to load.\n{e}"); + // Freeze the player while he's loading its new position + Player.main.cinematicModeActive = true; + try + { + CoroutineHost.StartCoroutine(Terrain.WaitForWorldLoad()); + } + catch (Exception e) + { + // Freeze the player while he's loading its new position + Player.main.cinematicModeActive = false; + Log.Warn($"Something wrong happened while waiting for the terrain to load.\n{e}"); + } } } } From 4ae65a14362e5290e236e3fc352da4cdb524255a Mon Sep 17 00:00:00 2001 From: MistaOmega Date: Sun, 17 Mar 2024 17:49:04 +0000 Subject: [PATCH 04/11] Added handler for case where player is in vehicle to GoTo command --- NitroxPatcher/Patches/Dynamic/GoTo_Patch.cs | 22 +++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 NitroxPatcher/Patches/Dynamic/GoTo_Patch.cs diff --git a/NitroxPatcher/Patches/Dynamic/GoTo_Patch.cs b/NitroxPatcher/Patches/Dynamic/GoTo_Patch.cs new file mode 100644 index 0000000000..2efe2c91c8 --- /dev/null +++ b/NitroxPatcher/Patches/Dynamic/GoTo_Patch.cs @@ -0,0 +1,22 @@ +using System.Reflection; +using HarmonyLib; +using NitroxModel.Helper; +using UnityEngine; + +namespace NitroxPatcher.Patches.Dynamic; + +public sealed partial class GoTo_Patch : NitroxPatch, IDynamicPatch +{ + public static readonly MethodInfo TARGET_METHOD = Reflect.Method((GotoConsoleCommand gotoConsoleCommand) => gotoConsoleCommand.GotoPosition(default, default)); + + public static bool Prefix(Vector3 position, bool gotoImmediate = false) + { + Vehicle currentMountedVehicle = Player.main.currentMountedVehicle; + if (currentMountedVehicle == null) + { + return true; // Normal GoTo behaviour if not in vehicle + } + currentMountedVehicle.TeleportVehicle(position, currentMountedVehicle.transform.rotation); // handle GoTo with a vehicle teleport, which takes player too + return false; + } +} From 35d9caa2aa5bfeaef2cf83d9a7486d54a91a6db3 Mon Sep 17 00:00:00 2001 From: MistaOmega Date: Fri, 24 May 2024 15:59:46 +0100 Subject: [PATCH 05/11] Recommended fixes --- .../Processors/PlayerTeleportedProcessor.cs | 28 ++++++++----------- NitroxPatcher/Patches/Dynamic/GoTo_Patch.cs | 5 ++-- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/NitroxClient/Communication/Packets/Processors/PlayerTeleportedProcessor.cs b/NitroxClient/Communication/Packets/Processors/PlayerTeleportedProcessor.cs index 864a53c06b..a05d6fe544 100644 --- a/NitroxClient/Communication/Packets/Processors/PlayerTeleportedProcessor.cs +++ b/NitroxClient/Communication/Packets/Processors/PlayerTeleportedProcessor.cs @@ -32,26 +32,22 @@ public override void Process(PlayerTeleported packet) } Vehicle currentVehicle = Player.main.currentMountedVehicle; - // Check to make sure the player is in a vehicle - if (currentVehicle != null) + if (currentVehicle) { currentVehicle.TeleportVehicle(packet.DestinationTo.ToUnity(), currentVehicle.transform.rotation); + return; } - else + + Player.main.SetPosition(packet.DestinationTo.ToUnity()); + Player.main.cinematicModeActive = true; + try { - Player.main.SetPosition(packet.DestinationTo.ToUnity()); - // Freeze the player while he's loading its new position - Player.main.cinematicModeActive = true; - try - { - CoroutineHost.StartCoroutine(Terrain.WaitForWorldLoad()); - } - catch (Exception e) - { - // Freeze the player while he's loading its new position - Player.main.cinematicModeActive = false; - Log.Warn($"Something wrong happened while waiting for the terrain to load.\n{e}"); - } + CoroutineHost.StartCoroutine(Terrain.WaitForWorldLoad()); + } + catch (Exception e) + { + Player.main.cinematicModeActive = false; + Log.Warn($"Something wrong happened while waiting for the terrain to load.\n{e}"); } } } diff --git a/NitroxPatcher/Patches/Dynamic/GoTo_Patch.cs b/NitroxPatcher/Patches/Dynamic/GoTo_Patch.cs index 2efe2c91c8..2ac16dcded 100644 --- a/NitroxPatcher/Patches/Dynamic/GoTo_Patch.cs +++ b/NitroxPatcher/Patches/Dynamic/GoTo_Patch.cs @@ -1,5 +1,4 @@ using System.Reflection; -using HarmonyLib; using NitroxModel.Helper; using UnityEngine; @@ -7,12 +6,12 @@ namespace NitroxPatcher.Patches.Dynamic; public sealed partial class GoTo_Patch : NitroxPatch, IDynamicPatch { - public static readonly MethodInfo TARGET_METHOD = Reflect.Method((GotoConsoleCommand gotoConsoleCommand) => gotoConsoleCommand.GotoPosition(default, default)); + public static readonly MethodInfo TARGET_METHOD = Reflect.Method((GotoConsoleCommand t) => t.GotoPosition(default, default)); public static bool Prefix(Vector3 position, bool gotoImmediate = false) { Vehicle currentMountedVehicle = Player.main.currentMountedVehicle; - if (currentMountedVehicle == null) + if (!currentMountedVehicle) { return true; // Normal GoTo behaviour if not in vehicle } From b69265be9e72eb9d6cf883cb8d00d4e406bdbbce Mon Sep 17 00:00:00 2001 From: MistaOmega Date: Fri, 24 May 2024 16:02:33 +0100 Subject: [PATCH 06/11] Renamed patch --- .../{GoTo_Patch.cs => GotoConsoleCommand_GotoPosition_Patch.cs} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename NitroxPatcher/Patches/Dynamic/{GoTo_Patch.cs => GotoConsoleCommand_GotoPosition_Patch.cs} (88%) diff --git a/NitroxPatcher/Patches/Dynamic/GoTo_Patch.cs b/NitroxPatcher/Patches/Dynamic/GotoConsoleCommand_GotoPosition_Patch.cs similarity index 88% rename from NitroxPatcher/Patches/Dynamic/GoTo_Patch.cs rename to NitroxPatcher/Patches/Dynamic/GotoConsoleCommand_GotoPosition_Patch.cs index 2ac16dcded..26652eb009 100644 --- a/NitroxPatcher/Patches/Dynamic/GoTo_Patch.cs +++ b/NitroxPatcher/Patches/Dynamic/GotoConsoleCommand_GotoPosition_Patch.cs @@ -4,7 +4,7 @@ namespace NitroxPatcher.Patches.Dynamic; -public sealed partial class GoTo_Patch : NitroxPatch, IDynamicPatch +public sealed partial class GotoConsoleCommand_GotoPosition_Patch : NitroxPatch, IDynamicPatch { public static readonly MethodInfo TARGET_METHOD = Reflect.Method((GotoConsoleCommand t) => t.GotoPosition(default, default)); From 495287fe2a88a9479b8c790e1f7e3e137a3528f0 Mon Sep 17 00:00:00 2001 From: MistaOmega Date: Tue, 24 Sep 2024 20:02:23 +0100 Subject: [PATCH 07/11] Removed unneeded prefix var --- .../Patches/Dynamic/GotoConsoleCommand_GotoPosition_Patch.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NitroxPatcher/Patches/Dynamic/GotoConsoleCommand_GotoPosition_Patch.cs b/NitroxPatcher/Patches/Dynamic/GotoConsoleCommand_GotoPosition_Patch.cs index 26652eb009..4f1ec6e68a 100644 --- a/NitroxPatcher/Patches/Dynamic/GotoConsoleCommand_GotoPosition_Patch.cs +++ b/NitroxPatcher/Patches/Dynamic/GotoConsoleCommand_GotoPosition_Patch.cs @@ -8,7 +8,7 @@ public sealed partial class GotoConsoleCommand_GotoPosition_Patch : NitroxPatch, { public static readonly MethodInfo TARGET_METHOD = Reflect.Method((GotoConsoleCommand t) => t.GotoPosition(default, default)); - public static bool Prefix(Vector3 position, bool gotoImmediate = false) + public static bool Prefix(Vector3 position) { Vehicle currentMountedVehicle = Player.main.currentMountedVehicle; if (!currentMountedVehicle) From c1000178c1220e1e709c6caa106a3a637c69260d Mon Sep 17 00:00:00 2001 From: MistaOmega Date: Fri, 22 Nov 2024 18:44:34 +0000 Subject: [PATCH 08/11] Added warpforward support --- .../Dynamic/Player_WarpForward_Patch.cs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 NitroxPatcher/Patches/Dynamic/Player_WarpForward_Patch.cs diff --git a/NitroxPatcher/Patches/Dynamic/Player_WarpForward_Patch.cs b/NitroxPatcher/Patches/Dynamic/Player_WarpForward_Patch.cs new file mode 100644 index 0000000000..0af163c319 --- /dev/null +++ b/NitroxPatcher/Patches/Dynamic/Player_WarpForward_Patch.cs @@ -0,0 +1,26 @@ +using System.Reflection; +using NitroxModel.Helper; +using UnityEngine; + +namespace NitroxPatcher.Patches.Dynamic; + +public sealed partial class Player_WarpForward_Patch : NitroxPatch, IDynamicPatch +{ + public static readonly MethodInfo TARGET_METHOD = Reflect.Method((Player t) => t.OnConsoleCommand_warpforward(default)); + + public static bool Prefix(Player __instance, NotificationCenter.Notification n) + { + float num; + DevConsole.ParseFloat(n, 0, out num, 3f); + Vector3 newPosition = __instance.transform.position + __instance.camRoot.GetAimingTransform().forward * num; + + if (__instance.currentMountedVehicle == null) + { + return true; + } + __instance.currentMountedVehicle.TeleportVehicle(newPosition, __instance.currentMountedVehicle.transform.rotation); + __instance.OnPlayerPositionCheat(); + return false; + } + +} From d44cee815ecb9fd5d703198849712c7332f95ee4 Mon Sep 17 00:00:00 2001 From: MistaOmega Date: Wed, 18 Dec 2024 22:50:36 +0000 Subject: [PATCH 09/11] Code review changes --- .../Patches/Dynamic/GotoConsoleCommand_GotoPosition_Patch.cs | 2 +- NitroxPatcher/Patches/Dynamic/Player_WarpForward_Patch.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/NitroxPatcher/Patches/Dynamic/GotoConsoleCommand_GotoPosition_Patch.cs b/NitroxPatcher/Patches/Dynamic/GotoConsoleCommand_GotoPosition_Patch.cs index 4f1ec6e68a..5553d1f9a0 100644 --- a/NitroxPatcher/Patches/Dynamic/GotoConsoleCommand_GotoPosition_Patch.cs +++ b/NitroxPatcher/Patches/Dynamic/GotoConsoleCommand_GotoPosition_Patch.cs @@ -15,7 +15,7 @@ public static bool Prefix(Vector3 position) { return true; // Normal GoTo behaviour if not in vehicle } - currentMountedVehicle.TeleportVehicle(position, currentMountedVehicle.transform.rotation); // handle GoTo with a vehicle teleport, which takes player too + currentMountedVehicle.TeleportVehicle(position, currentMountedVehicle.transform.rotation); // Handle GoTo with a vehicle teleport, which takes player too return false; } } diff --git a/NitroxPatcher/Patches/Dynamic/Player_WarpForward_Patch.cs b/NitroxPatcher/Patches/Dynamic/Player_WarpForward_Patch.cs index 0af163c319..e5d419c9f9 100644 --- a/NitroxPatcher/Patches/Dynamic/Player_WarpForward_Patch.cs +++ b/NitroxPatcher/Patches/Dynamic/Player_WarpForward_Patch.cs @@ -14,7 +14,7 @@ public static bool Prefix(Player __instance, NotificationCenter.Notification n) DevConsole.ParseFloat(n, 0, out num, 3f); Vector3 newPosition = __instance.transform.position + __instance.camRoot.GetAimingTransform().forward * num; - if (__instance.currentMountedVehicle == null) + if (!__instance.currentMountedVehicle) { return true; } From b37aedf734a3f1caaecaae8311091adc10031e67 Mon Sep 17 00:00:00 2001 From: MistaOmega Date: Wed, 18 Dec 2024 22:51:28 +0000 Subject: [PATCH 10/11] Bracket change to warpforward --- NitroxPatcher/Patches/Dynamic/Player_WarpForward_Patch.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NitroxPatcher/Patches/Dynamic/Player_WarpForward_Patch.cs b/NitroxPatcher/Patches/Dynamic/Player_WarpForward_Patch.cs index e5d419c9f9..13a73e1e63 100644 --- a/NitroxPatcher/Patches/Dynamic/Player_WarpForward_Patch.cs +++ b/NitroxPatcher/Patches/Dynamic/Player_WarpForward_Patch.cs @@ -12,7 +12,7 @@ public static bool Prefix(Player __instance, NotificationCenter.Notification n) { float num; DevConsole.ParseFloat(n, 0, out num, 3f); - Vector3 newPosition = __instance.transform.position + __instance.camRoot.GetAimingTransform().forward * num; + Vector3 newPosition = __instance.transform.position + (__instance.camRoot.GetAimingTransform().forward * num); if (!__instance.currentMountedVehicle) { From efd6bb31d9e4f1c965a9628bf615b6cc7f54251f Mon Sep 17 00:00:00 2001 From: MistaOmega Date: Wed, 18 Dec 2024 22:52:32 +0000 Subject: [PATCH 11/11] Kept brackets for clarity --- .../Packets/Processors/PlayerTeleportedProcessor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NitroxClient/Communication/Packets/Processors/PlayerTeleportedProcessor.cs b/NitroxClient/Communication/Packets/Processors/PlayerTeleportedProcessor.cs index a05d6fe544..a75afd7b2a 100644 --- a/NitroxClient/Communication/Packets/Processors/PlayerTeleportedProcessor.cs +++ b/NitroxClient/Communication/Packets/Processors/PlayerTeleportedProcessor.cs @@ -21,7 +21,7 @@ public override void Process(PlayerTeleported packet) if (subRoot.isCyclops) { // Reversing calculations from PlayerMovementBroadcaster.Update() - Vector3 position = subRoot.transform.rotation * packet.DestinationTo.ToUnity() + subRoot.transform.position; + Vector3 position = (subRoot.transform.rotation * packet.DestinationTo.ToUnity()) + subRoot.transform.position; Player.main.SetPosition(position); Player.main.SetCurrentSub(subRoot);