From 512ffe38b8df648e2b0955763fbd16ef6398b272 Mon Sep 17 00:00:00 2001 From: NotAKidoS <37721153+NotAKidOnSteam@users.noreply.github.com> Date: Tue, 18 Jun 2024 15:47:03 -0500 Subject: [PATCH] [OriginShift] Fix Spawnable & Object Sync cached net pos --- .../Player/OriginShiftObjectSyncReceiver.cs | 46 +++++++++++++++++++ .../Player/OriginShiftSpawnableReceiver.cs | 45 ++++++++++++++++++ OriginShift/Patches.cs | 10 +++- 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 OriginShift/OriginShift/Player/OriginShiftObjectSyncReceiver.cs create mode 100644 OriginShift/OriginShift/Player/OriginShiftSpawnableReceiver.cs diff --git a/OriginShift/OriginShift/Player/OriginShiftObjectSyncReceiver.cs b/OriginShift/OriginShift/Player/OriginShiftObjectSyncReceiver.cs new file mode 100644 index 0000000..3aba111 --- /dev/null +++ b/OriginShift/OriginShift/Player/OriginShiftObjectSyncReceiver.cs @@ -0,0 +1,46 @@ +using ABI.CCK.Components; +using UnityEngine; + +namespace NAK.OriginShift; + +public class OriginShiftObjectSyncReceiver : MonoBehaviour +{ + private CVRObjectSync _objectSync; + + #region Unity Events + + private void Start() + { + _objectSync = GetComponent(); + if (_objectSync == null) + { + OriginShiftMod.Logger.Error("OriginShiftObjectSyncReceiver: No CVRObjectSync found on GameObject: " + gameObject.name, this); + enabled = false; + } + OriginShiftManager.OnOriginShifted += OnOriginShifted; + } + + private void OnEnable() + { + OriginShiftManager.OnOriginShifted += OnOriginShifted; + } + + private void OnDisable() + { + OriginShiftManager.OnOriginShifted -= OnOriginShifted; + } + + #endregion Unity Events + + #region Origin Shift Events + + private void OnOriginShifted(Vector3 shift) + { + // idc, just shift all of them + _objectSync._oldData.position += shift; + _objectSync._currData.position += shift; + _objectSync._futureData.position += shift; + } + + #endregion Origin Shift Events +} \ No newline at end of file diff --git a/OriginShift/OriginShift/Player/OriginShiftSpawnableReceiver.cs b/OriginShift/OriginShift/Player/OriginShiftSpawnableReceiver.cs new file mode 100644 index 0000000..41ffd23 --- /dev/null +++ b/OriginShift/OriginShift/Player/OriginShiftSpawnableReceiver.cs @@ -0,0 +1,45 @@ +using ABI.CCK.Components; +using UnityEngine; + +namespace NAK.OriginShift; + +public class OriginShiftSpawnableReceiver : MonoBehaviour +{ + private CVRSpawnable _spawnable; + + #region Unity Events + + private void Start() + { + _spawnable = GetComponent(); + if (_spawnable == null) + { + OriginShiftMod.Logger.Error("OriginShiftSpawnableReceiver: No CVRSpawnable found on GameObject: " + gameObject.name, this); + enabled = false; + } + OriginShiftManager.OnOriginShifted += OnOriginShifted; + } + + private void OnEnable() + { + OriginShiftManager.OnOriginShifted += OnOriginShifted; + } + + private void OnDisable() + { + OriginShiftManager.OnOriginShifted -= OnOriginShifted; + } + + #endregion Unity Events + + #region Origin Shift Events + + private void OnOriginShifted(Vector3 shift) + { + _spawnable.futurePosition += shift; + _spawnable.currentPosition += shift; + _spawnable.pastPosition += shift; // not used by game, just cached ? + } + + #endregion Origin Shift Events +} \ No newline at end of file diff --git a/OriginShift/Patches.cs b/OriginShift/Patches.cs index 63760b3..7051261 100644 --- a/OriginShift/Patches.cs +++ b/OriginShift/Patches.cs @@ -63,9 +63,10 @@ internal static class CVRSpawnablePatches [HarmonyPatch(typeof(CVRSpawnable), nameof(CVRSpawnable.Start))] private static void Postfix_CVRSpawnable_Start(ref CVRSpawnable __instance) { - Transform wrapper = __instance.transform.parent; + __instance.AddComponentIfMissing(); // test adding to the wrapper of the spawnable + Transform wrapper = __instance.transform.parent; wrapper.AddComponentIfMissing(); wrapper.AddComponentIfMissing(); wrapper.AddComponentIfMissing(); @@ -215,6 +216,13 @@ internal static class CVRSyncHelperPatches internal static class CVRObjectSyncPatches { + [HarmonyPostfix] + [HarmonyPatch(typeof(CVRObjectSync), nameof(CVRObjectSync.Start))] + private static void Postfix_CVRObjectSync_Start(ref CVRObjectSync __instance) + { + __instance.gameObject.AddComponentIfMissing(); + } + [HarmonyPrefix] // inbound object sync [HarmonyPatch(typeof(CVRObjectSync), nameof(CVRObjectSync.receiveNetworkData))] [HarmonyPatch(typeof(CVRObjectSync), nameof(CVRObjectSync.receiveNetworkDataJoin))]