From 1dce1960f6b823478032439983ea223f4ba496e0 Mon Sep 17 00:00:00 2001 From: Aaro Varis Date: Fri, 28 Feb 2025 12:20:55 +0200 Subject: [PATCH] More Networking stuff --- BoneSync/BoneSync.csproj | 1 + BoneSync/Networking/ByteEncoder.cs | 18 +++++++++ BoneSync/Patching/CallPatchedMethod.cs | 42 +++++++++++++++++++++ BoneSync/Patching/ObjectHealthPatches.cs | 26 +------------ BoneSync/Patching/PoolPatches.cs | 31 +++++++-------- BoneSync/Sync/Components/SyncableBase.cs | 8 ++-- BoneSync/Sync/Components/SyncablePhysics.cs | 5 ++- BoneSync/Sync/ObjectSync.cs | 4 ++ 8 files changed, 92 insertions(+), 43 deletions(-) create mode 100644 BoneSync/Patching/CallPatchedMethod.cs diff --git a/BoneSync/BoneSync.csproj b/BoneSync/BoneSync.csproj index 71ad709..8ff8c19 100644 --- a/BoneSync/BoneSync.csproj +++ b/BoneSync/BoneSync.csproj @@ -116,6 +116,7 @@ + diff --git a/BoneSync/Networking/ByteEncoder.cs b/BoneSync/Networking/ByteEncoder.cs index 6d223d8..f664478 100644 --- a/BoneSync/Networking/ByteEncoder.cs +++ b/BoneSync/Networking/ByteEncoder.cs @@ -194,5 +194,23 @@ namespace BoneSync.Networking WriteEnum(attack.attackType); WriteFloat(attack.damage); } + + public void WriteMatrix4x4(UnityEngine.Matrix4x4 matrix) + { + for (int i = 0; i < 16; i++) + { + WriteFloat(matrix[i]); + } + } + + public UnityEngine.Matrix4x4 ReadMatrix4x4() + { + UnityEngine.Matrix4x4 matrix = new UnityEngine.Matrix4x4(); + for (int i = 0; i < 16; i++) + { + matrix[i] = ReadFloat(); + } + return matrix; + } } } diff --git a/BoneSync/Patching/CallPatchedMethod.cs b/BoneSync/Patching/CallPatchedMethod.cs new file mode 100644 index 0000000..af58b1b --- /dev/null +++ b/BoneSync/Patching/CallPatchedMethod.cs @@ -0,0 +1,42 @@ +using StressLevelZero.Combat; +using StressLevelZero.Pool; +using StressLevelZero.Props; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; + +namespace BoneSync.Patching +{ + public static class CallPatchedMethods + { + public static bool allowPatchedMethodCall + { + private set; + get; + } + public static void TakeDamage(ObjectDestructable __instance, ref Vector3 normal, ref float damage, ref bool crit, ref AttackType attackType) + { + allowPatchedMethodCall = true; + __instance.TakeDamage(normal, damage, crit, attackType); + allowPatchedMethodCall = false; + } + + public static void TakeDamage(Prop_Health __instance, ref float damage, ref bool crit, ref AttackType attackType) + { + allowPatchedMethodCall = true; + __instance.TAKEDAMAGE(damage, crit, attackType); + allowPatchedMethodCall = false; + } + + public static GameObject PoolSpawn(Pool __instance, ref Vector3 position, ref Quaternion rotation, ref Il2CppSystem.Nullable scale, ref Il2CppSystem.Nullable autoEnable) + { + allowPatchedMethodCall = true; + GameObject result = __instance.Spawn(position, rotation, scale, autoEnable); + allowPatchedMethodCall = false; + return result; + } + } +} diff --git a/BoneSync/Patching/ObjectHealthPatches.cs b/BoneSync/Patching/ObjectHealthPatches.cs index 79349a9..cc8b490 100644 --- a/BoneSync/Patching/ObjectHealthPatches.cs +++ b/BoneSync/Patching/ObjectHealthPatches.cs @@ -14,28 +14,6 @@ using UnityEngine; namespace BoneSync.Patching { - public static class CallPatchedMethods - { - public static bool allowDamageMethodCall - { - private set; - get; - } - public static void TakeDamage(ObjectDestructable __instance, ref Vector3 normal, ref float damage, ref bool crit, ref AttackType attackType) - { - allowDamageMethodCall = true; - __instance.TakeDamage(normal, damage, crit, attackType); - allowDamageMethodCall = false; - } - - public static void TakeDamage(Prop_Health __instance, ref float damage, ref bool crit, ref AttackType attackType) - { - allowDamageMethodCall = true; - __instance.TAKEDAMAGE(damage, crit, attackType); - allowDamageMethodCall = false; - } - } - [HarmonyPatch(typeof(ObjectDestructable))] public class ObjectDestructablePatches { @@ -43,7 +21,7 @@ namespace BoneSync.Patching [HarmonyPrefix] private static bool TakeDamagePatch(ObjectDestructable __instance, ref Vector3 normal, ref float damage, ref bool crit, ref AttackType attackType) { - if (CallPatchedMethods.allowDamageMethodCall) return true; + if (CallPatchedMethods.allowPatchedMethodCall) return true; MelonLoader.MelonLogger.Msg("ObjectDestructable.TakeDamage: " + damage); Syncable syncable = ObjectSync.GetSyncableFromCache(__instance.gameObject); if (syncable != null) @@ -67,7 +45,7 @@ namespace BoneSync.Patching [HarmonyPrefix] private static bool TakeDamagePatch(Prop_Health __instance, ref float damage, ref bool crit, ref AttackType attackType) { - if (CallPatchedMethods.allowDamageMethodCall) return true; + if (CallPatchedMethods.allowPatchedMethodCall) return true; MelonLoader.MelonLogger.Msg("Prop_Health.TAKEDAMAGE: " + damage); Syncable syncable = ObjectSync.GetSyncableFromCache(__instance.gameObject); if (syncable != null) diff --git a/BoneSync/Patching/PoolPatches.cs b/BoneSync/Patching/PoolPatches.cs index 0d6d2f3..a4942c3 100644 --- a/BoneSync/Patching/PoolPatches.cs +++ b/BoneSync/Patching/PoolPatches.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using HarmonyLib; @@ -11,31 +12,31 @@ using UnityEngine; namespace BoneSync.Patching { - /* + [HarmonyPatch(typeof(Pool))] internal class PoolPatches { [HarmonyPatch(nameof(Pool.Spawn))] [HarmonyPostfix] - private static void SpawnPatch(Pool __instance, ref GameObject __result) + private static void SpawnPatchPost(Pool __instance, ref GameObject __result) { - MelonLogger.Msg("Spawned object: " + __result.name); + //MelonLogger.Msg("Spawned object: " + __result.name); } - [HarmonyPatch(nameof(Pool.FlagPooleeForRespawn))] - [HarmonyPostfix] - private static void FlagPooleeForRespawnPatch(Pool __instance, ref GameObject p) + [HarmonyPatch(nameof(Pool.Spawn))] + [HarmonyPrefix] + private static bool SpawnPatchPre(Pool __instance, ref Vector3 position, ref Quaternion rotation, ref Il2CppSystem.Nullable scale, ref Il2CppSystem.Nullable autoEnable) { - MelonLogger.Msg("Flagged object for respawn: " + p.name); - } - [HarmonyPatch(nameof(Pool.InstantiatePoolee))] - [HarmonyPostfix] - private static void InstantiatePooleePatch(Pool __instance, ref Poolee __result) - { - MelonLogger.Msg("Instantiated object: " + __result.name); + if (CallPatchedMethods.allowPatchedMethodCall) return true; + + if (BoneSync.lobby.IsConnected()) + { + MelonLogger.Msg("Patched Spawning object in pool: " + __instance.name); + return false; + } + return true; } - - }*/ + } } diff --git a/BoneSync/Sync/Components/SyncableBase.cs b/BoneSync/Sync/Components/SyncableBase.cs index 1598fbe..53feeaa 100644 --- a/BoneSync/Sync/Components/SyncableBase.cs +++ b/BoneSync/Sync/Components/SyncableBase.cs @@ -15,6 +15,9 @@ using System.Collections; using StressLevelZero.Props; using BoneSync.Patching; using StressLevelZero.Props.Weapons; +using StressLevelZero.AI; +using PuppetMasta; +using UnityEngine.SceneManagement; namespace BoneSync.Sync.Components @@ -105,14 +108,13 @@ namespace BoneSync.Sync.Components private Plug[] plugs; - - + private AIBrain aiBrain; + private PuppetMaster puppetMaster; public void OnEnable() { FindComponents(); - MelonLogger.Msg("Syncable enabled: " + transform.GetPath()); } public string GetSyncableWorldPath() diff --git a/BoneSync/Sync/Components/SyncablePhysics.cs b/BoneSync/Sync/Components/SyncablePhysics.cs index 01d9d19..967cb25 100644 --- a/BoneSync/Sync/Components/SyncablePhysics.cs +++ b/BoneSync/Sync/Components/SyncablePhysics.cs @@ -73,7 +73,8 @@ namespace BoneSync.Sync.Components private void UpdateTransformList() { - Rigidbody[] rbs = UpdateRigidbodyList(); + // get non-null rigidbodies + Rigidbody[] rbs = UpdateRigidbodyList().Where(rb => rb != null).ToArray(); rigidbodies = rbs; _transforms = rbs.Select(rb => rb.transform).ToArray(); } @@ -82,5 +83,7 @@ namespace BoneSync.Sync.Components { SetKinematic(_ownerId != BoneSync.lobby.GetLocalId() && Registered); } + + // on collision } } diff --git a/BoneSync/Sync/ObjectSync.cs b/BoneSync/Sync/ObjectSync.cs index 87a53d4..db4eb78 100644 --- a/BoneSync/Sync/ObjectSync.cs +++ b/BoneSync/Sync/ObjectSync.cs @@ -9,6 +9,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using UnityEngine; +using UnityEngine.SceneManagement; namespace BoneSync.Sync { @@ -87,6 +88,9 @@ namespace BoneSync.Sync } private static Syncable _MakeOrGetSyncable(GameObject gameObject, bool deleteSubSyncabled = true) { + //Scene scene = gameObject.scene; + //MelonLogger.Msg("Making or getting syncable for: " + gameObject.name + " in scene: " + scene.name); + Syncable[] subSyncables = gameObject.GetComponentsInChildren(); Syncable syncable = gameObject.GetComponent();