backport from experimental

This commit is contained in:
NotAKidoS 2023-02-17 06:24:46 -06:00
parent 8a3523539b
commit 5fd8e3d4d6
14 changed files with 686 additions and 560 deletions

View file

@ -1,61 +1,31 @@
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)
{
if (__instance.gripOrigin == null) return;
Transform desktopOrigin = __instance.gripOrigin.Find("[Desktop]");
if (desktopOrigin == null) return;
var pickupTracker = __instance.GetComponent<CVRPickupObjectTracker>();
if (pickupTracker != null) return;
__instance.gameObject.AddComponent<CVRPickupObjectTracker>();
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;
}
}
namespace NAK.Melons.DesktopVRSwitch.Patches;
public class CVRPickupObjectTracker : MonoBehaviour
{
//maybe i should store both transforms instead and getcomponent for CVRPickupObject..?
public static Dictionary<CVRPickupObject, Transform> previousGripOrigin = new();
public CVRPickupObject pickupObject;
public Transform storedGripOrigin;
public void OnSwitch()
void Start()
{
var pickupObject = GetComponent<CVRPickupObject>();
VRModeSwitchTracker.OnPostVRModeSwitch += PostVRModeSwitch;
}
void OnDestroy()
{
VRModeSwitchTracker.OnPostVRModeSwitch -= PostVRModeSwitch;
}
public void PostVRModeSwitch(bool enterVR, Camera activeCamera)
{
if (pickupObject != null)
{
if (pickupObject.IsGrabbedByMe() && pickupObject._controllerRay != null) pickupObject._controllerRay.DropObject(true);
(previousGripOrigin[pickupObject], pickupObject.gripOrigin) = (pickupObject.gripOrigin, previousGripOrigin[pickupObject]);
if (pickupObject._controllerRay != null) pickupObject._controllerRay.DropObject(true);
(storedGripOrigin, pickupObject.gripOrigin) = (pickupObject.gripOrigin, storedGripOrigin);
}
}
private void OnDestroy()
{
var pickupObject = GetComponent<CVRPickupObject>();
if (pickupObject != null)
previousGripOrigin.Remove(pickupObject);
}
}

View file

@ -0,0 +1,24 @@
using ABI_RC.Core.Util.Object_Behaviour;
using UnityEngine;
namespace NAK.Melons.DesktopVRSwitch.Patches;
public class CameraFacingObjectTracker : MonoBehaviour
{
public CameraFacingObject cameraFacingObject;
void Start()
{
cameraFacingObject = GetComponent<CameraFacingObject>();
VRModeSwitchTracker.OnPostVRModeSwitch += PostVRModeSwitch;
}
void OnDestroy()
{
VRModeSwitchTracker.OnPostVRModeSwitch -= PostVRModeSwitch;
}
public void PostVRModeSwitch(bool enterVR, Camera activeCamera)
{
cameraFacingObject.m_Camera = activeCamera;
}
}

View file

@ -0,0 +1,57 @@
using ABI_RC.Systems.IK;
using ABI_RC.Systems.IK.SubSystems;
using ABI_RC.Systems.IK.TrackingModules;
using HarmonyLib;
using UnityEngine;
using System.Reflection;
namespace NAK.Melons.DesktopVRSwitch.Patches;
public class IKSystemTracker : MonoBehaviour
{
public IKSystem ikSystem;
public Traverse _traverseModules;
void Start()
{
ikSystem = GetComponent<IKSystem>();
_traverseModules = Traverse.Create(ikSystem).Field("_trackingModules");
VRModeSwitchTracker.OnPostVRModeSwitch += PostVRModeSwitch;
}
void OnDestroy()
{
VRModeSwitchTracker.OnPostVRModeSwitch -= PostVRModeSwitch;
}
public void PostVRModeSwitch(bool enterVR, Camera activeCamera)
{
var _trackingModules = _traverseModules.GetValue<List<TrackingModule>>();
SteamVRTrackingModule openVRTrackingModule = _trackingModules.FirstOrDefault(m => m is SteamVRTrackingModule) as SteamVRTrackingModule;
if (openVRTrackingModule != null)
{
if (enterVR)
{
openVRTrackingModule.ModuleStart();
}
else
{
//why named destroy when it doesnt ?
openVRTrackingModule.ModuleDestroy();
}
}
else
{
var steamVRTrackingModule = CreateSteamVRTrackingModule();
ikSystem.AddTrackingModule(steamVRTrackingModule);
}
}
//thanks for marking the constructor as internal
private SteamVRTrackingModule CreateSteamVRTrackingModule()
{
var steamVRTrackingModuleType = typeof(SteamVRTrackingModule);
var constructor = steamVRTrackingModuleType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null);
var instance = constructor.Invoke(null);
return (SteamVRTrackingModule)instance;
}
}

View file

@ -0,0 +1,39 @@
using ABI_RC.Systems.MovementSystem;
using UnityEngine;
namespace NAK.Melons.DesktopVRSwitch.Patches;
public class MovementSystemTracker : MonoBehaviour
{
public MovementSystem movementSystem;
public Vector3 preSwitchWorldPosition;
public Quaternion preSwitchWorldRotation;
void Start()
{
movementSystem = GetComponent<MovementSystem>();
VRModeSwitchTracker.OnPostVRModeSwitch += PreVRModeSwitch;
VRModeSwitchTracker.OnPostVRModeSwitch += PostVRModeSwitch;
}
void OnDestroy()
{
VRModeSwitchTracker.OnPostVRModeSwitch -= PreVRModeSwitch;
VRModeSwitchTracker.OnPostVRModeSwitch -= PostVRModeSwitch;
}
public void PreVRModeSwitch(bool enterVR, Camera activeCamera)
{
preSwitchWorldPosition = movementSystem.rotationPivot.transform.position;
preSwitchWorldRotation = movementSystem.rotationPivot.transform.rotation;
}
public void PostVRModeSwitch(bool enterVR, Camera activeCamera)
{
//lazy way of correcting Desktop & VR offset issue (game does the maths)
movementSystem.TeleportToPosRot(preSwitchWorldPosition, preSwitchWorldRotation, false);
//recenter desktop collision to player object
if (!enterVR) movementSystem.UpdateColliderCenter(movementSystem.transform.position);
}
}

View file

@ -0,0 +1,91 @@
using ABI_RC.Core.Base;
using ABI_RC.Core.Player;
using ABI_RC.Core.Savior;
using Aura2API;
using BeautifyEffect;
using UnityEngine;
using UnityEngine.AzureSky;
using UnityEngine.Rendering.PostProcessing;
using Object = UnityEngine.Object;
namespace NAK.Melons.DesktopVRSwitch.Patches;
internal class ReferenceCameraPatch
{
internal static void OnWorldLoad()
{
Camera activeCamera = (MetaPort.Instance.isUsingVr ? PlayerSetup.Instance.vrCamera : PlayerSetup.Instance.desktopCamera).GetComponent<Camera>();
Camera inactiveCamera = (MetaPort.Instance.isUsingVr ? PlayerSetup.Instance.desktopCamera : PlayerSetup.Instance.vrCamera).GetComponent<Camera>();
CopyToInactiveCam(activeCamera, inactiveCamera);
}
internal static void CopyToInactiveCam(Camera activeCam, Camera inactiveCam)
{
DesktopVRSwitchMod.Logger.Msg("Copying active camera settings & components to inactive camera.");
//steal basic settings
inactiveCam.farClipPlane = activeCam.farClipPlane;
inactiveCam.nearClipPlane = activeCam.nearClipPlane;
inactiveCam.cullingMask = activeCam.cullingMask;
inactiveCam.depthTextureMode = activeCam.depthTextureMode;
//steal post processing if added
PostProcessLayer ppLayerActiveCam = activeCam.GetComponent<PostProcessLayer>();
PostProcessLayer ppLayerInactiveCam = inactiveCam.AddComponentIfMissing<PostProcessLayer>();
if (ppLayerActiveCam != null && ppLayerInactiveCam != null)
{
ppLayerInactiveCam.enabled = ppLayerActiveCam.enabled;
ppLayerInactiveCam.volumeLayer = ppLayerActiveCam.volumeLayer;
}
//what even is this aura camera stuff
AuraCamera auraActiveCam = activeCam.GetComponent<AuraCamera>();
AuraCamera auraInactiveCam = inactiveCam.AddComponentIfMissing<AuraCamera>();
if (auraActiveCam != null && auraInactiveCam != null)
{
auraInactiveCam.enabled = auraActiveCam.enabled;
auraInactiveCam.frustumSettings = auraActiveCam.frustumSettings;
}
else
{
auraInactiveCam.enabled = false;
}
//flare layer thing? the sun :_:_:_:_:_:
FlareLayer flareActiveCam = activeCam.GetComponent<FlareLayer>();
FlareLayer flareInactiveCam = inactiveCam.AddComponentIfMissing<FlareLayer>();
if (flareActiveCam != null && flareInactiveCam != null)
{
flareInactiveCam.enabled = flareActiveCam.enabled;
}
else
{
flareInactiveCam.enabled = false;
}
//and now what the fuck is fog scattering
AzureFogScattering azureFogActiveCam = activeCam.GetComponent<AzureFogScattering>();
AzureFogScattering azureFogInactiveCam = inactiveCam.AddComponentIfMissing<AzureFogScattering>();
if (azureFogActiveCam != null && azureFogInactiveCam != null)
{
azureFogInactiveCam.fogScatteringMaterial = azureFogActiveCam.fogScatteringMaterial;
}
else
{
Object.Destroy(inactiveCam.GetComponent<AzureFogScattering>());
}
//why is there so many thingsssssssss
Beautify beautifyActiveCam = activeCam.GetComponent<Beautify>();
Beautify beautifyInactiveCam = inactiveCam.AddComponentIfMissing<Beautify>();
if (beautifyActiveCam != null && beautifyInactiveCam != null)
{
beautifyInactiveCam.quality = beautifyActiveCam.quality;
beautifyInactiveCam.profile = beautifyActiveCam.profile;
}
else
{
Object.Destroy(inactiveCam.gameObject.GetComponent<Beautify>());
}
}
}

View file

@ -0,0 +1,33 @@
using ABI_RC.Core.Player;
using UnityEngine;
using UnityEngine.Events;
namespace NAK.Melons.DesktopVRSwitch.Patches;
public class VRModeSwitchTracker
{
public static event UnityAction<bool, Camera> OnPreVRModeSwitch;
public static event UnityAction<bool, Camera> OnPostVRModeSwitch;
public static void PreVRModeSwitch(bool enterVR)
{
TryCatchHell.TryCatchWrapper(() =>
{
DesktopVRSwitchMod.Logger.Msg("Invoking VRModeSwitchTracker.OnPreVRModeSwitch.");
Camera activeCamera = PlayerSetup.Instance.GetActiveCamera().GetComponent<Camera>();
VRModeSwitchTracker.OnPreVRModeSwitch?.Invoke(enterVR, activeCamera);
},
"Error while invoking VRModeSwitchTracker.OnPreVRModeSwitch. Did someone do a fucky?");
}
public static void PostVRModeSwitch(bool enterVR)
{
TryCatchHell.TryCatchWrapper(() =>
{
DesktopVRSwitchMod.Logger.Msg("Invoking VRModeSwitchTracker.OnPostVRModeSwitch.");
Camera activeCamera = PlayerSetup.Instance.GetActiveCamera().GetComponent<Camera>();
VRModeSwitchTracker.OnPostVRModeSwitch?.Invoke(enterVR, activeCamera);
},
"Error while invoking VRModeSwitchTracker.OnPostVRModeSwitch. Did someone do a fucky?");
}
}