Files
BoneSync/BoneSync/Patching/ZonePatches.cs
2025-03-30 15:45:22 +03:00

135 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BoneSync.Data;
using BoneSync.Player;
using BoneSync.Sync.Components;
using HarmonyLib;
using StressLevelZero.Zones;
using UnityEngine;
namespace BoneSync.Patching
{
[HarmonyPatch(typeof(ZoneSpawner))]
public static class ZoneSpawnerPatch
{
// this patch should catch most of the spawning, but the pool spawning patch should catch the rest
[HarmonyPatch(nameof(ZoneSpawner.Spawn)), HarmonyPrefix]
public static bool ZoneSpawnPrefix(ZoneSpawner __instance)
{
if (!BoneSync.IsConnected) return true; // do not block if not connected
//.Msg("ZoneSpawner.Spawn: " + __instance.transform.GetPath());
if (BoneSync.lobby.IsHost)
{
return true;
}
return false; // don't spawn if not host
}
}
[HarmonyPatch(typeof(ZoneEncounter))]
public static class ZoneEncounterPatch
{
[HarmonyPatch(nameof(ZoneEncounter.StartEncounter)), HarmonyPrefix]
public static bool ZoneEncounterSpawnPrefix(ZoneEncounter __instance)
{
if (!BoneSync.IsConnected) return true;
SyncLogger.Msg("ZoneEncounter.StartEncounter: " + __instance.transform.GetPath());
if (BoneSync.lobby.IsHost)
{
return true;
}
return false;
}
}
// SceneZone and PlayerTrigger patches are based on the ones from entanglement mod
[HarmonyPatch(typeof(SceneZone))]
public static class SceneZonePatch
{
public static bool IsChildOfLocalRig(Transform transform)
{
if (PlayerRig.localRigWorldRoot == null) return false;
return transform.root == PlayerRig.localRigWorldRoot;
}
public static bool IsChildOfHostRig(Transform transform)
{
if (!BoneSync.IsConnected|| BoneSync.lobby.IsHost)
{
return IsChildOfLocalRig(transform);
}
else
{
PlayerRig hostRig = PlayerRig.GetPlayerRig(BoneSync.lobby.GetHostId());
Transform hostRoot = hostRig?.transform.root;
return hostRoot != null && transform.root == hostRoot;
}
}
[HarmonyPatch(nameof(SceneZone.OnTriggerEnter)), HarmonyPrefix]
public static bool EnterPrefix(SceneZone __instance, Collider other)
{
if (other.CompareTag("Player"))
{
return IsChildOfLocalRig(other.transform);
}
return true;
}
[HarmonyPatch(nameof(SceneZone.OnTriggerExit)), HarmonyPrefix]
public static bool ExitPrefix(SceneZone __instance, Collider other)
{
if (other.CompareTag("Player"))
{
return IsChildOfLocalRig(other.transform);
}
return true;
}
}
[HarmonyPatch(typeof(PlayerTrigger))]
public static class PlayerTriggerPatch
{
[HarmonyPatch(nameof(PlayerTrigger.OnTriggerEnter)), HarmonyPrefix]
public static bool EnterPrefix(PlayerTrigger __instance, Collider other)
{
if (other.CompareTag("Player"))
{
return SceneZonePatch.IsChildOfLocalRig(other.transform);
}
return true;
}
// only patch exit, as we want to allow activating a trigger always
[HarmonyPatch(nameof(PlayerTrigger.OnTriggerExit)), HarmonyPrefix]
public static bool ExitPrefix(PlayerTrigger __instance, Collider other)
{
if (other.CompareTag("Player"))
{
return SceneZonePatch.IsChildOfLocalRig(other.transform);
}
return true;
}
}
}