Skip to content

Commit

Permalink
Merge pull request #19 from RedBigz/preview/flatscreen
Browse files Browse the repository at this point in the history
Merge preview/flatscreen
  • Loading branch information
RedBigz authored Dec 24, 2024
2 parents eee7af6 + 4631e99 commit 43e948a
Show file tree
Hide file tree
Showing 19 changed files with 133 additions and 10 deletions.
11 changes: 11 additions & 0 deletions TABGVR/Native.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Runtime.InteropServices;

namespace TABGVR;

public static class Native
{
[DllImport("shlwapi.dll", CharSet = CharSet.Ansi)]
public static extern int ShellMessageBox(IntPtr hAppInst, IntPtr hWnd, string lpcText, string lpcTitle,
uint fuStyle);
}
15 changes: 12 additions & 3 deletions TABGVR/Network/NetKinematics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class NetKinematics : MonoBehaviour
NetworkPlayer netPlayer;
private Holding holding;

private bool isSetUp;

/// <summary>
/// Sets up joint for kinematics
/// </summary>
Expand All @@ -23,20 +25,27 @@ private void Setup(Rigidbody joint)

joint.isKinematic = false;
arm.GetComponent<Rigidbody>().isKinematic = false;

isSetUp = true;
}

private void Start()
{
netPlayer = GetComponent<NetworkPlayer>();
holding = GetComponent<Holding>();

Setup(holding.leftHand);
Setup(holding.rightHand);
}

private void FixedUpdate()
{
if (!NetworkStoreList.NetworkStores.TryGetValue(netPlayer.Index, out var store)) return;

if (!isSetUp)
{
// ReSharper disable once Unity.PerformanceCriticalCodeInvocation
Setup(holding.leftHand);
// ReSharper disable once Unity.PerformanceCriticalCodeInvocation
Setup(holding.rightHand);
}

var leftHandPosition = store.LeftHandPosition - store.HmdPosition + netPlayer.m_Camera.position;
var rightHandPosition = store.RightHandPosition - store.HmdPosition + netPlayer.m_Camera.position;
Expand Down
8 changes: 8 additions & 0 deletions TABGVR/PatchAttributes/FlatscreenPatchAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace TABGVR.PatchAttributes;

[AttributeUsage(AttributeTargets.Class)]
public class FlatscreenPatchAttribute : Attribute
{
}
8 changes: 8 additions & 0 deletions TABGVR/PatchAttributes/VRPatchAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace TABGVR.PatchAttributes;

[AttributeUsage(AttributeTargets.Class)]
public class VRPatchAttribute : Attribute
{
}
2 changes: 2 additions & 0 deletions TABGVR/Patches/CameraPatches/CameraMovementPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
using System.Linq;
using System.Reflection.Emit;
using HarmonyLib;
using TABGVR.PatchAttributes;

namespace TABGVR.Patches.CameraPatches;

[HarmonyPatch(typeof(CameraMovement))]
[VRPatch]
public class CameraMovementPatch
{
[HarmonyPatch(nameof(CameraMovement.LateUpdate))]
Expand Down
2 changes: 2 additions & 0 deletions TABGVR/Patches/CameraPatches/CameraPatch.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using DeepSky.Haze;
using HarmonyLib;
using HighlightingSystem;
using TABGVR.PatchAttributes;
using TABGVR.Player;
using UnityEngine;
using UnityEngine.Animations;
Expand All @@ -10,6 +11,7 @@
namespace TABGVR.Patches.CameraPatches;

[HarmonyPatch(typeof(PlayerCamera))]
[VRPatch]
public class CameraPatch
{
/// <summary>
Expand Down
21 changes: 21 additions & 0 deletions TABGVR/Patches/FlatscreenKinematicsHelperPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using HarmonyLib;
using Landfall.Network;
using TABGVR.Network;
using TABGVR.PatchAttributes;
using UnityEngine.SceneManagement;

namespace TABGVR.Patches;

[HarmonyPatch(typeof(Holding), nameof(Holding.Start))]
[FlatscreenPatch]
public class FlatscreenKinematicsHelperPatch
{
private static void Postfix(Holding __instance)
{
if (__instance.m_player == global::Player.localPlayer) return;
if (!PhotonServerConnector.IsNetworkMatch) return;
if (SceneManager.GetActiveScene().name == "MainMenu") return;

__instance.gameObject.AddComponent<NetKinematics>();
}
}
2 changes: 2 additions & 0 deletions TABGVR/Patches/Interactions/KinematicsPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Landfall.Network;
using TABGVR.Input;
using TABGVR.Network;
using TABGVR.PatchAttributes;
using TABGVR.Player;
using TABGVR.Player.Mundanities;
using TABGVR.Util;
Expand All @@ -17,6 +18,7 @@
namespace TABGVR.Patches.Interactions;

[HarmonyPatch(typeof(Holding))]
[VRPatch]
internal class KinematicsPatch
{
internal static bool GripAvailable;
Expand Down
2 changes: 2 additions & 0 deletions TABGVR/Patches/Interactions/ShootHapticPatch.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using HarmonyLib;
using TABGVR.Input;
using TABGVR.PatchAttributes;
using TABGVR.Player;

namespace TABGVR.Patches.Interactions;

[HarmonyPatch(typeof(Weapon), nameof(Weapon.Awake))]
[VRPatch]
public class ShootHapticPatch
{
private const float Amplitude = 0.8f;
Expand Down
2 changes: 2 additions & 0 deletions TABGVR/Patches/Interactions/SwapWeaponPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
using HarmonyLib;
using TABGInput;
using TABGVR.Input;
using TABGVR.PatchAttributes;
using TABGVR.Player;

namespace TABGVR.Patches.Interactions;

[HarmonyPatch(typeof(InteractionHandler), nameof(InteractionHandler.StartPickup), MethodType.Enumerator)]
[VRPatch]
public static class SwapWeaponPatch
{
[HarmonyTranspiler]
Expand Down
2 changes: 2 additions & 0 deletions TABGVR/Patches/Interactions/ThrowingPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
using System.Collections.Generic;
using System.Reflection.Emit;
using HarmonyLib;
// using TABGVR.PatchAttributes;
using TABGVR.Player;

namespace TABGVR.Patches.Interactions;

// [HarmonyPatch(typeof(InteractionHandler), nameof(InteractionHandler.Throwing), MethodType.Enumerator)]
// [HarmonyDebug]
// [VRPatch]
public static class ThrowingPatch
{
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) =>
Expand Down
3 changes: 3 additions & 0 deletions TABGVR/Patches/Networking/NetworkEventPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
using HarmonyLib;
using Landfall.Network;
using TABGVR.Network;
using TABGVR.PatchAttributes;
using UnityEngine;

namespace TABGVR.Patches.Networking;

[HarmonyPatch(typeof(ServerConnector), nameof(ServerConnector.OnEvent))]
[VRPatch, FlatscreenPatch]
public class NetworkEventPatch
{
public static bool Interrogated = false;
Expand All @@ -24,6 +26,7 @@ public static bool Prefix(ClientPackage clientPackage)
switch (clientPackage.Code)
{
case (EventCode)PacketCodes.Interrogate: // Interrogate
if (!Plugin.VREnabled.Value) return false; // flatscreen players aren't allowed to respond to interrogations
ServerConnector.m_ServerHandler.SendMessageToServer((EventCode)PacketCodes.Interrogate, [], true);
Interrogated = true;
return false;
Expand Down
2 changes: 2 additions & 0 deletions TABGVR/Patches/PlayerPatch.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using HarmonyLib;
using TABGVR.Input;
using TABGVR.PatchAttributes;
using TABGVR.Player;
using UnityEngine;
using UnityEngine.SpatialTracking;

namespace TABGVR.Patches;

[HarmonyPatch(typeof(global::Player))]
[VRPatch]
public class PlayerPatch
{
public static VRControls CurrentVRControls;
Expand Down
2 changes: 2 additions & 0 deletions TABGVR/Patches/UI/DraggedItemPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Reflection.Emit;
using HarmonyLib;
using Landfall.TABG.UI;
using TABGVR.PatchAttributes;
using TABGVR.Util;
using UnityEngine;

Expand All @@ -11,6 +12,7 @@ namespace TABGVR.Patches.UI;
#if DEBUG
[HarmonyDebug]
#endif
[VRPatch]
public class DraggedItemPatch
{
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
Expand Down
2 changes: 2 additions & 0 deletions TABGVR/Patches/UI/InventoryUIManagerUpdatePatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
using HarmonyLib;
using Landfall.TABG.UI;
using TABGVR.Input;
using TABGVR.PatchAttributes;

namespace TABGVR.Patches.UI;

[HarmonyPatch(typeof(InventoryUIManager), nameof(InventoryUIManager.Update))]
[VRPatch]
public class InventoryUIManagerUpdatePatch
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions TABGVR/Patches/UI/InventoryUIPatch.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using HarmonyLib;
using TABGVR.PatchAttributes;
using TABGVR.Util;
using UnityEngine;
using UnityEngine.UI;

namespace TABGVR.Patches.UI;

[HarmonyPatch(typeof(InventoryUI), nameof(InventoryUI.Start))]
[VRPatch]
public static class InventoryUIPatch
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions TABGVR/Patches/UI/MainMenuCameraPatch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DeepSky.Haze;
using HarmonyLib;
using TABGVR.PatchAttributes;
using TABGVR.Player;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
Expand All @@ -8,6 +9,7 @@
namespace TABGVR.Patches.UI;

[HarmonyPatch(typeof(CameraIdleMovement), nameof(CameraIdleMovement.Start))]
[VRPatch]
public static class MainMenuCameraPatch
{
public static void Postfix(CameraIdleMovement __instance)
Expand Down
2 changes: 2 additions & 0 deletions TABGVR/Patches/UI/MainMenuPatch.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using HarmonyLib;
using Landfall.TABG.UI.MainMenu;
using TABGVR.PatchAttributes;
using TABGVR.Util;
using UnityEngine;
using UnityEngine.UI;

namespace TABGVR.Patches.UI;

[HarmonyPatch(typeof(MainMenuManager), nameof(MainMenuManager.Start))]
[VRPatch]
public static class MainMenuPatch
{
public static void Postfix(MainMenuManager __instance)
Expand Down
53 changes: 46 additions & 7 deletions TABGVR/Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using BepInEx;
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using TABGVR.PatchAttributes;
using TABGVR.Patches.Misc;
using TABGVR.Player;
using TABGVR.Util;
Expand All @@ -19,26 +24,60 @@ public class Plugin : BaseUnityPlugin
internal new static ManualLogSource Logger;

public static ConfigEntry<bool> SnapTurnEnabled;
public static ConfigEntry<bool> AskOnStartup;
public static ConfigEntry<bool> VREnabled;

private void Awake()
{
SnapTurnEnabled = Config.Bind("Input", "SnapTurn", true, "Use snap turn instead of smooth turn.");

AskOnStartup = Config.Bind("Input", "AskOnStartup", true, "Ask to use VR mode on startup.");
VREnabled = Config.Bind("Input", "VREnabled", true,
"If enabled, will always use VR mode if AskOnStartup is false.");

Logger = base.Logger;

Logger.LogInfo("TABGVR plugin loaded.");

AntiCheatBypass.Bypass();

XRLoader.LoadXR();

Harmony harmony = new(MyPluginInfo.PLUGIN_GUID);

#if DEBUG
HarmonyFileLog.Enabled = true;
#endif

harmony.PatchAll();

if (AskOnStartup.Value)
switch (Native.ShellMessageBox(IntPtr.Zero, IntPtr.Zero,
"Would you like to launch TABG in VR?\n\nMake sure your headset is connected and your launcher (Oculus Dash or SteamVR) is configured as the default OpenXR API.\nLaunching without VR will still let you see VR players' hand movements.",
"TABGVR", 0x1043))
{
case 2:
Process.GetCurrentProcess().Kill();
break;
case 7:
VREnabled.Value = false;
break;
case 6:
VREnabled.Value = true;
break;
}

var allTypes = Assembly.GetExecutingAssembly().GetTypes();

if (VREnabled.Value)
{
// patch vr types
foreach (var type in allTypes.Where(type => type.IsDefined(typeof(VRPatchAttribute))))
harmony.PatchAll(type);

XRLoader.LoadXR();
}
else
{
// patch flatscreen types
foreach (var type in allTypes.Where(type => type.IsDefined(typeof(FlatscreenPatchAttribute))))
harmony.PatchAll(type);
}

SceneManager.sceneLoaded += (_, _) => Controllers.Setup();
SceneManager.sceneUnloaded += (_) =>
Expand Down

0 comments on commit 43e948a

Please sign in to comment.