diff --git a/BoneSync/MelonLoaderMod.cs b/BoneSync/MelonLoaderMod.cs index 5dde35d..00b63b8 100644 --- a/BoneSync/MelonLoaderMod.cs +++ b/BoneSync/MelonLoaderMod.cs @@ -53,8 +53,8 @@ namespace BoneSync public static void PatchAll() { - HarmonyLib.Harmony harmony = new HarmonyLib.Harmony("com.aarov.bonesync"); - harmony.PatchAll(); + //HarmonyLib.Harmony harmony = new HarmonyLib.Harmony("com.aarov.bonesync"); + //harmony.i } public override void OnSceneWasLoaded(int buildIndex, string sceneName) diff --git a/BoneSync/Networking/Packet.cs b/BoneSync/Networking/Packet.cs index 475c5d5..f3ea32b 100644 --- a/BoneSync/Networking/Packet.cs +++ b/BoneSync/Networking/Packet.cs @@ -170,7 +170,7 @@ namespace BoneSync.Networking } Queue queue = _packetQueues[sceneIndex]; int processed = 0; - while (queue.Count > 0 && processed < 10) + while (queue.Count > 0 && processed < 10 && SceneSync.TimeSinceLastSceneChange > SceneSync.MAP_LOAD_GRACE_PERIOD) { processed++; NetworkMessage networkMessage = queue.Dequeue(); diff --git a/BoneSync/Patching/AIHealthPatches.cs b/BoneSync/Patching/AIHealthPatches.cs index e7fad6b..a03424f 100644 --- a/BoneSync/Patching/AIHealthPatches.cs +++ b/BoneSync/Patching/AIHealthPatches.cs @@ -3,18 +3,23 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using BoneSync.Sync.Components; using HarmonyLib; using PuppetMasta; namespace BoneSync.Patching { - /* + [HarmonyPatch(typeof(SubBehaviourHealth))] internal class AIHealthPatches { - [HarmonyPatch(nameof(SubBehaviourHealth.TakeDamage)), HarmonyPostfix] - private static void DamagePrefix(SubBehaviourHealth __instance) + [HarmonyPatch(nameof(SubBehaviourHealth.TakeDamage)), HarmonyPrefix] + private static bool DamagePrefix(SubBehaviourHealth __instance) { - + if (!BoneSync.IsConnected) return true; + if (CallPatchedMethods.allowPatchedMethodCall) return true; + Syncable syncable = __instance.behaviour.GetComponentInParent(); + if (syncable != null && syncable.Registered && !syncable.isOwner) return false; + return true; } - }*/ + } } diff --git a/BoneSync/Patching/ObjectHealthPatches.cs b/BoneSync/Patching/ObjectHealthPatches.cs index 7cb494d..8043cd6 100644 --- a/BoneSync/Patching/ObjectHealthPatches.cs +++ b/BoneSync/Patching/ObjectHealthPatches.cs @@ -26,13 +26,13 @@ namespace BoneSync.Patching if (CallPatchedMethods.allowPatchedMethodCall) return true; if (!BoneSync.IsConnected) return true; if (damage < 0.05f) return true; // ignore small damage (e.g. a little bit of fall damage) - MelonLoader.MelonLogger.Msg("ObjectDestructable.TakeDamage: " + damage); + //MelonLoader.MelonLogger.Msg("ObjectDestructable.TakeDamage: " + damage); Syncable syncable = ObjectSync.MakeOrGetSyncable(__instance.gameObject); if (syncable != null) { if (damage > 0.5f) syncable.RegisterSyncable(); // register syncable if damage is very significant, e.g. a bullet hit if (syncable.Registered && !syncable.isOwner) return false; - MelonLoader.MelonLogger.Msg("Patch: ObjectDestructable.TakeDamage: " + damage + " " + syncable.gameObject.name); + //MelonLoader.MelonLogger.Msg("Patch: ObjectDestructable.TakeDamage: " + damage + " " + syncable.gameObject.name); ObjectHealthInfo objectHealthInfo = new ObjectHealthInfo(__instance._health, __instance._hits, normal, damage, crit, attackType); ObjectSync.SendObjectDamageMessage(syncable, ObjectDamageType.DestructibleTakeDamage, objectHealthInfo); @@ -67,13 +67,13 @@ namespace BoneSync.Patching { if (CallPatchedMethods.allowPatchedMethodCall) return true; if (!BoneSync.IsConnected) return true; - MelonLoader.MelonLogger.Msg("Prop_Health.TAKEDAMAGE: " + damage); + //MelonLoader.MelonLogger.Msg("Prop_Health.TAKEDAMAGE: " + damage); Syncable syncable = ObjectSync.MakeOrGetSyncable(__instance.gameObject); if (syncable != null) { if (damage > 0.5f) syncable.RegisterSyncable(); if (syncable.Registered && !syncable.isOwner) return false; - MelonLoader.MelonLogger.Msg("Patch: Prop_Health.TAKEDAMAGE: " + damage + " " + syncable.gameObject.name); + //MelonLoader.MelonLogger.Msg("Patch: Prop_Health.TAKEDAMAGE: " + damage + " " + syncable.gameObject.name); ObjectHealthInfo objectHealthInfo = new ObjectHealthInfo(__instance.cur_Health, __instance.hits, Vector3.zero, damage, crit, attackType); ObjectSync.SendObjectDamageMessage(syncable, ObjectDamageType.PropHealthTakeDamage, objectHealthInfo); diff --git a/BoneSync/Patching/ZonePatches.cs b/BoneSync/Patching/ZonePatches.cs index b2a5652..9109ccd 100644 --- a/BoneSync/Patching/ZonePatches.cs +++ b/BoneSync/Patching/ZonePatches.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using BoneSync.Player; using BoneSync.Sync.Components; using HarmonyLib; using MelonLoader; @@ -13,79 +14,6 @@ using UnityEngine; namespace BoneSync.Patching { - public static class ZoneTrackingUtilities - { - public static Dictionary zoneCount = new Dictionary(); - public static Dictionary triggerCount = new Dictionary(); - - public static void Increment(SceneZone zone) - { - if (!zoneCount.ContainsKey(zone)) - zoneCount.Add(zone, 0); - - zoneCount[zone]++; - } - - public static void Decrement(SceneZone zone) - { - if (!zoneCount.ContainsKey(zone)) - zoneCount.Add(zone, 0); - - zoneCount[zone]--; - zoneCount[zone] = Mathf.Clamp(zoneCount[zone], 0, int.MaxValue); - } - - public static bool CanEnter(SceneZone zone) - { - if (!zoneCount.ContainsKey(zone)) - return false; - - return zoneCount[zone] <= 1; - } - - public static bool CanExit(SceneZone zone) - { - if (!zoneCount.ContainsKey(zone)) - return false; - - return zoneCount[zone] <= 0; - } - - public static void Increment(PlayerTrigger trigger) - { - if (!triggerCount.ContainsKey(trigger)) - triggerCount.Add(trigger, 0); - - triggerCount[trigger]++; - } - - public static void Decrement(PlayerTrigger trigger) - { - if (!triggerCount.ContainsKey(trigger)) - triggerCount.Add(trigger, 0); - - triggerCount[trigger]--; - triggerCount[trigger] = Mathf.Clamp(triggerCount[trigger], 0, int.MaxValue); - } - - public static bool CanEnter(PlayerTrigger trigger) - { - if (!triggerCount.ContainsKey(trigger)) - return false; - - return triggerCount[trigger] <= 1; - } - - public static bool CanExit(PlayerTrigger trigger) - { - if (!triggerCount.ContainsKey(trigger)) - return false; - - return triggerCount[trigger] <= 0; - } - } - - [HarmonyPatch(typeof(ZoneSpawner))] public static class ZoneSpawnerPatch { @@ -127,7 +55,7 @@ namespace BoneSync.Patching } // SceneZone and PlayerTrigger patches are based on the ones from entanglement mod - /* + [HarmonyPatch(typeof(SceneZone))] public static class SceneZonePatch { @@ -136,9 +64,7 @@ namespace BoneSync.Patching { if (other.CompareTag("Player")) { - ZoneTrackingUtilities.Increment(__instance); - bool canEnter = ZoneTrackingUtilities.CanEnter(__instance); - return canEnter; + return other.transform.IsChildOf(PlayerRig.localRigRoot); } return true; @@ -149,9 +75,7 @@ namespace BoneSync.Patching { if (other.CompareTag("Player")) { - ZoneTrackingUtilities.Decrement(__instance); - bool canExit = ZoneTrackingUtilities.CanExit(__instance); - return canExit; + return other.transform.IsChildOf(PlayerRig.localRigRoot); } return true; @@ -166,9 +90,8 @@ namespace BoneSync.Patching { if (other.CompareTag("Player")) { - ZoneTrackingUtilities.Increment(__instance); - bool canEnter = ZoneTrackingUtilities.CanEnter(__instance); - return canEnter; + + return other.transform.IsChildOf(PlayerRig.localRigRoot); } return true; @@ -179,12 +102,11 @@ namespace BoneSync.Patching { if (other.CompareTag("Player")) { - ZoneTrackingUtilities.Decrement(__instance); - bool canExit = ZoneTrackingUtilities.CanExit(__instance); - return canExit; + + return other.transform.IsChildOf(PlayerRig.localRigRoot); } return true; } - }*/ + } } diff --git a/BoneSync/Player/PlayerRig.cs b/BoneSync/Player/PlayerRig.cs index 65e689f..cee63ef 100644 --- a/BoneSync/Player/PlayerRig.cs +++ b/BoneSync/Player/PlayerRig.cs @@ -109,8 +109,9 @@ namespace BoneSync.Player public void UpdatePoseRadius(Handedness hand, float radius) => characterAnimationManager?.SetCylinderRadius(hand, radius); - private static GameObject localPlayerRig; - private static Transform localRigRoot; + public static GameObject localPlayerRig; + public static Transform localRigRoot; + private static Transform localRigHeadTransform; private static Transform localRigLeftHandTransform; private static Transform localRigRightHandTransform; diff --git a/BoneSync/Sync/Components/SyncableBase.cs b/BoneSync/Sync/Components/SyncableBase.cs index 1fc1981..0e98339 100644 --- a/BoneSync/Sync/Components/SyncableBase.cs +++ b/BoneSync/Sync/Components/SyncableBase.cs @@ -156,7 +156,7 @@ namespace BoneSync.Sync.Components public bool ShouldAutoSync() { - if (SceneSync.TimeSinceLastSceneChange < 5f) return false; // don't sync if scene just changed, to prevent some weird stuff that happens when a level is loaded + if (SceneSync.TimeSinceLastSceneChange < SceneSync.MAP_LOAD_GRACE_PERIOD) return false; // don't sync if scene just changed, to prevent some weird stuff that happens when a level is loaded if (InPoolManagerTransform()) return false; if (!gameObject.activeInHierarchy) return false; if (poolee && poolee.pool) { @@ -207,6 +207,7 @@ namespace BoneSync.Sync.Components UpdateTransformList(); TryPatchUnityEvents(); + TrySendAttributeSync(); ObjectSyncCache.AddSyncable(this); } diff --git a/BoneSync/Sync/Components/SyncableProperties.cs b/BoneSync/Sync/Components/SyncableProperties.cs index f0a6385..a428d0c 100644 --- a/BoneSync/Sync/Components/SyncableProperties.cs +++ b/BoneSync/Sync/Components/SyncableProperties.cs @@ -13,16 +13,28 @@ namespace BoneSync.Sync.Components { public partial class Syncable : MonoBehaviour { - + private const float ATTRIBUTE_SYNC_FPS = 0.2f; + private float _lastAttributeSyncTime; + private void StripDamage(ref BulletObject bulletObject) { AmmoVariables ammoVariables = bulletObject.ammoVariables; ammoVariables.AttackDamage = 0f; bulletObject.ammoVariables = ammoVariables; } + + private void TrySendAttributeSync() + { + if (!Registered) return; + if (!isOwner) return; + if (Time.realtimeSinceStartup - _lastAttributeSyncTime > 1 / ATTRIBUTE_SYNC_FPS) + { + _SendAttributeSync(); + } + } + private void _SendAttributeSync() { - if (!isOwner) return; _SendMagazineData(); } public void ApplyMagazineData(MagazineSyncData magazineSyncData) @@ -68,7 +80,7 @@ namespace BoneSync.Sync.Components Quaternion rotation = firepointTransform.rotation; BulletObject bulletObject = gunSyncInfo.bulletObject; StripDamage(ref bulletObject); - gun.EjectCartridge(); + //gun.EjectCartridge(); PoolSpawner.SpawnProjectile(position, rotation, gunSyncInfo.bulletObject, "1911", null); PoolSpawner.SpawnMuzzleFlare(position, rotation, PoolSpawner.MuzzleFlareType.Default); return; diff --git a/BoneSync/Sync/SceneSync.cs b/BoneSync/Sync/SceneSync.cs index 57e321c..7bc544d 100644 --- a/BoneSync/Sync/SceneSync.cs +++ b/BoneSync/Sync/SceneSync.cs @@ -14,6 +14,8 @@ namespace BoneSync.Sync { internal class SceneSync { + public const float MAP_LOAD_GRACE_PERIOD = 5f; + private static List scenes = new List(); private static string _currentSceneName; private static int _currentSceneIndex;