From 7f860f4fc22e83540f085a114342bc9492ec41f0 Mon Sep 17 00:00:00 2001 From: NotAKidoS <37721153+NotAKidOnSteam@users.noreply.github.com> Date: Sat, 29 Oct 2022 09:07:27 -0500 Subject: [PATCH] prop origin fixes, version 2 --- DesktopVRSwitch/Main.cs | 66 +++++++++++++------ .../Patches/CVRPickupObjectTracker.cs | 59 +++++++++++++++++ DesktopVRSwitch/Properties/AssemblyInfo.cs | 2 +- 3 files changed, 107 insertions(+), 20 deletions(-) create mode 100644 DesktopVRSwitch/Patches/CVRPickupObjectTracker.cs diff --git a/DesktopVRSwitch/Main.cs b/DesktopVRSwitch/Main.cs index d0f2cd7..c8e1910 100644 --- a/DesktopVRSwitch/Main.cs +++ b/DesktopVRSwitch/Main.cs @@ -1,42 +1,37 @@ using ABI_RC.Core; +using ABI_RC.Core.EventSystem; using ABI_RC.Core.InteractionSystem; using ABI_RC.Core.Player; using ABI_RC.Core.Savior; using ABI_RC.Core.UI; using ABI_RC.Core.Util.Object_Behaviour; -using ABI_RC.Systems.MovementSystem; -using ABI_RC.Core.EventSystem; +using ABI_RC.Systems.Camera; using ABI_RC.Systems.IK.SubSystems; +using ABI_RC.Systems.MovementSystem; +using DesktopVRSwitch.Patches; +using HarmonyLib; using MelonLoader; -using RootMotion.FinalIK; using System.Collections; using UnityEngine; using UnityEngine.XR; using Valve.VR; -using HarmonyLib; using Object = UnityEngine.Object; - //tell the game to change VRMode/DesktopMode for Steam/Discord presence //RichPresence.PopulatePresence(); //nvm that resets the RichPresence clock- i want people to know how long ive wasted staring at mirror - - namespace DesktopVRSwitch; public class DesktopVRSwitch : MelonMod { private static MelonPreferences_Category m_categoryDesktopVRSwitch; - private static MelonPreferences_Entry m_entryReloadInstance; private static MelonPreferences_Entry m_entryTimedErrorCatch; - public override void OnApplicationStart() + public override void OnInitializeMelon() { m_categoryDesktopVRSwitch = MelonPreferences.CreateCategory(nameof(DesktopVRSwitch)); - //m_entryReloadInstance = m_categoryDesktopVRSwitch.CreateEntry("Rejoin Instance", false, description: "Rejoin instance on switch."); m_entryTimedErrorCatch = m_categoryDesktopVRSwitch.CreateEntry("Timed Error Catch", true, description: "Attempt to switch back if an error is found after 10 seconds."); - m_categoryDesktopVRSwitch.SaveToFile(false); } @@ -93,6 +88,7 @@ public class DesktopVRSwitch : MelonMod InitializeSteamVR(VRMode); CloseMenuElements(VRMode); + DisableMirrorCanvas(); yield return new WaitForEndOfFrame(); @@ -107,12 +103,13 @@ public class DesktopVRSwitch : MelonMod UpdateCameraFacingObject(); RepositionCohtmlHud(VRMode); UpdateHudOperations(VRMode); + SwitchPickupOrigins(); yield return new WaitForEndOfFrame(); //needs to come after SetMovementSystem - //UpdateGestureReconizerCam(); + UpdateGestureReconizerCam(); yield return new WaitForEndOfFrame(); @@ -193,7 +190,6 @@ public class DesktopVRSwitch : MelonMod { MelonLogger.Msg("Closed MainMenu Instance."); ViewManager.Instance.UiStateToggle(false); - ViewManager.Instance.VrInputChanged(isVR); } else { @@ -267,7 +263,7 @@ public class DesktopVRSwitch : MelonMod MelonLogger.Msg("Parented CohtmlHud to active camera."); CohtmlHud.Instance.gameObject.transform.parent = isVR ? PlayerSetup.Instance.vrCamera.transform : PlayerSetup.Instance.desktopCamera.transform; - //sets hud position, rotation, and scale based on MetaPort isUsingVr + //sets hud position, rotation, ~~and scale~~ based on MetaPort isUsingVr CVRTools.ConfigureHudAffinity(); CohtmlHud.Instance.gameObject.transform.localScale = new Vector3(1.2f, 1f, 1.2f); } @@ -339,14 +335,13 @@ public class DesktopVRSwitch : MelonMod } } - //this doesnt seem to work - private static void UpdateGestureReconizerCam() + private static void DisableMirrorCanvas() { try { - MelonLogger.Msg("Set GestureReconizerCam camera to Camera.main."); - Camera cam = Traverse.Create(CVRGestureRecognizer.Instance).Field("_camera").GetValue() as Camera; - cam = PlayerSetup.Instance.GetActiveCamera().GetComponent(); + //tell the game we are in mirror mode so itll disable it (if enabled) + PortableCamera.Instance.mode = MirroringMode.Mirror; + PortableCamera.Instance.ChangeMirroring(); } catch (Exception) { @@ -354,4 +349,37 @@ public class DesktopVRSwitch : MelonMod throw; } } + + private static void UpdateGestureReconizerCam() + { + try + { + MelonLogger.Msg("Set GestureReconizerCam camera to active camera."); + Traverse.Create(CVRGestureRecognizer.Instance).Field("_camera").SetValue(PlayerSetup.Instance.GetActiveCamera().GetComponent()); + } + catch (Exception) + { + MelonLogger.Error("Error updating CVRGestureRecognizer camera!"); + throw; + } + } + + private static void SwitchPickupOrigins() + { + try + { + MelonLogger.Msg("Switched pickup origins."); + CVRPickupObjectTracker[] pickups = Object.FindObjectsOfType(); + + for (int i = 0; i < pickups.Count(); i++) + { + pickups[i].OnSwitch(); + } + } + catch (Exception) + { + MelonLogger.Error("Error switching pickup origins!"); + throw; + } + } } \ No newline at end of file diff --git a/DesktopVRSwitch/Patches/CVRPickupObjectTracker.cs b/DesktopVRSwitch/Patches/CVRPickupObjectTracker.cs new file mode 100644 index 0000000..36cac6d --- /dev/null +++ b/DesktopVRSwitch/Patches/CVRPickupObjectTracker.cs @@ -0,0 +1,59 @@ +using ABI.CCK.Components; +using ABI_RC.Core.Savior; +using HarmonyLib; +using MelonLoader; +using UnityEngine; + +//Thanks Ben! I was scared of transpiler so I reworked a bit... + +namespace DesktopVRSwitch.Patches; + +[HarmonyPatch] +internal class CVRPickupObject_Patch +{ + [HarmonyPrefix] + [HarmonyPatch(typeof(CVRPickupObject), "Start")] + private static void CVRPickupObject_Start_Prefix(ref CVRPickupObject __instance) + { + Transform desktopOrigin = __instance.gripOrigin.Find("[Desktop]"); + if (desktopOrigin == null) return; + + var pickupTracker = __instance.GetComponent(); + if (pickupTracker != null) return; + + __instance.gameObject.AddComponent(); + + StorePreviousPosition(__instance, (!MetaPort.Instance.isUsingVr) ? __instance.gripOrigin : desktopOrigin); + } + + private static void StorePreviousPosition(CVRPickupObject pickupObject, Transform gripOrigin) + { + MelonLogger.Msg("Storing previous gripOrigin."); + CVRPickupObjectTracker.previousGripOrigin[pickupObject] = gripOrigin; + } +} + +public class CVRPickupObjectTracker : MonoBehaviour +{ + //maybe i should store both transforms instead and getcomponent for CVRPickupObject..? + public static Dictionary previousGripOrigin = new(); + + public void OnSwitch() + { + var pickupObject = GetComponent(); + + if (pickupObject != null) + { + if (pickupObject.IsGrabbedByMe()) pickupObject.Drop(); + (previousGripOrigin[pickupObject], pickupObject.gripOrigin) = (pickupObject.gripOrigin, previousGripOrigin[pickupObject]); + } + } + + private void OnDestroy() + { + var pickupObject = GetComponent(); + + if (pickupObject != null) + previousGripOrigin.Remove(pickupObject); + } +} diff --git a/DesktopVRSwitch/Properties/AssemblyInfo.cs b/DesktopVRSwitch/Properties/AssemblyInfo.cs index 6795fe7..03ba712 100644 --- a/DesktopVRSwitch/Properties/AssemblyInfo.cs +++ b/DesktopVRSwitch/Properties/AssemblyInfo.cs @@ -25,6 +25,6 @@ using System.Reflection; namespace DesktopVRSwitch.Properties; internal static class AssemblyInfoParams { - public const string Version = "1.0.0"; + public const string Version = "2.0.0"; public const string Author = "NotAKidoS"; } \ No newline at end of file