From b485ab325996d56ccf257e3cceaeab38a2e6090d Mon Sep 17 00:00:00 2001 From: Aaro Varis Date: Sun, 2 Mar 2025 01:30:39 +0200 Subject: [PATCH] Client spawning fix --- .../Messages/OnwershipTransferMessage.cs | 2 +- BoneSync/Patching/PoolPatches.cs | 3 +- BoneSync/Sync/Components/SyncableBase.cs | 61 ++++++++++++++----- BoneSync/Sync/ObjectSync.cs | 11 +++- 4 files changed, 56 insertions(+), 21 deletions(-) diff --git a/BoneSync/Networking/Messages/OnwershipTransferMessage.cs b/BoneSync/Networking/Messages/OnwershipTransferMessage.cs index 46ed38b..43d6986 100644 --- a/BoneSync/Networking/Messages/OnwershipTransferMessage.cs +++ b/BoneSync/Networking/Messages/OnwershipTransferMessage.cs @@ -14,7 +14,7 @@ namespace BoneSync.Networking.Messages public ulong NewOwnerId; } - [PacketType(PacketType.ObjectOwnership)] + [PacketType(PacketType.ObjectOwnership), PacketReliability(PacketReliability.ReliableFast)] internal class OnwershipTransferMessage : NetworkMessage { private OnwershipTransferMessageData Data; diff --git a/BoneSync/Patching/PoolPatches.cs b/BoneSync/Patching/PoolPatches.cs index 9f361f4..3ef0c0e 100644 --- a/BoneSync/Patching/PoolPatches.cs +++ b/BoneSync/Patching/PoolPatches.cs @@ -74,6 +74,7 @@ namespace BoneSync.Patching [HarmonyPostfix] private static void InstantiatePooleePatchPost(Pool __instance, Poolee __result, Vector3 position, Quaternion rotation) { + if (CallPatchedMethods.allowPatchedMethodCall) return; if (__instance == null) return; if (PoolBlacklist.isBlacklistedPool(__instance)) return; MelonLogger.Msg("Patched Instantiating object in pool: " + __instance.name); @@ -128,7 +129,7 @@ namespace BoneSync.Patching MelonLogger.Msg("Poolee.OnSpawn: " + __instance.gameObject.transform.GetPath()); - ObjectSync.MakeOrGetSyncable(__instance); + Syncable syncable = ObjectSync.MakeOrGetSyncable(__instance); bool spawnNormally = BoneSync.lobby.IsHost || PoolBlacklist.IsClientSpawnPool(__instance.pool); diff --git a/BoneSync/Sync/Components/SyncableBase.cs b/BoneSync/Sync/Components/SyncableBase.cs index d8a1710..0c71a9f 100644 --- a/BoneSync/Sync/Components/SyncableBase.cs +++ b/BoneSync/Sync/Components/SyncableBase.cs @@ -92,6 +92,17 @@ namespace BoneSync.Sync.Components private SpawnFragment spawnFragment; + private IEnumerator _CheckAutoSyncCo() + { + yield return null; + bool shouldAutoSync = CheckIfShouldAutoSync(); + if (shouldAutoSync && (BoneSync.lobby.IsHost || ClientSpawningAllowed())) + { + MelonLogger.Msg("AutoSyncing: " + transform.GetPath()); + RegisterSyncable(); + } + yield break; + } public void OnEnable() { @@ -99,12 +110,7 @@ namespace BoneSync.Sync.Components FindComponents(); - bool shouldAutoSync = CheckIfShouldAutoSync(); - if (shouldAutoSync && (BoneSync.lobby.IsHost || ClientSpawningAllowed())) - { - MelonLogger.Msg("AutoSyncing: " + transform.GetPath()); - RegisterSyncable(); - } + MelonCoroutines.Start(_CheckAutoSyncCo()); } public bool CheckIfShouldAutoSync() @@ -156,26 +162,49 @@ namespace BoneSync.Sync.Components return false; } - public void OnDestroy() - { - DiscardSyncable(); - //MelonLogger.Msg("Syncable destroyed: " + transform.GetPath()); - } - - public void DiscardSyncable() + private void _DiscardSyncable(bool force) { if (Registered) { - MelonLogger.Warning("Discarding registered syncable: " + transform.GetPath()); + MelonLogger.Warning("Discarding registered syncable: " + transform.GetPath() + " force: " + force); + if (!force) return; } syncablesCache.Remove(gameObject); ObjectSyncCache.RemoveSyncable(this); - Destroy(this); + Destroy(this); // delete the component + } + + public void OnDestroy() + { + if (Registered) + { + MelonLogger.Warning("Destroying registered syncable: " + transform.GetPath()); + } + _DiscardSyncable(true); + //MelonLogger.Msg("Syncable destroyed: " + transform.GetPath()); + } + + private IEnumerator _FlagForDiscardCo(bool force) + { + yield return null; + _DiscardSyncable(force); + } + + public void DiscardSyncable(bool force = false) + { + MelonCoroutines.Start(_FlagForDiscardCo(force)); } public void OnDisable() { - DiscardSyncable(); + if (Registered && !isOwner) + { + MelonLogger.Warning("tried to disable non-owner syncable: " + transform.GetPath()); + gameObject.SetActive(true); + } else + { + DiscardSyncable(); + } } public void RegisterSyncable() diff --git a/BoneSync/Sync/ObjectSync.cs b/BoneSync/Sync/ObjectSync.cs index 62db004..813f619 100644 --- a/BoneSync/Sync/ObjectSync.cs +++ b/BoneSync/Sync/ObjectSync.cs @@ -111,7 +111,7 @@ namespace BoneSync.Sync // delete all sub syncables if (deleteSubSyncabled) { - Syncable[] subSyncables = gameObject.GetComponentsInChildren(true); + Syncable[] subSyncables = gameObject.GetComponentsInChildren(); for (int i = 0; i < subSyncables.Length; i++) { if (subSyncables[i] != syncable) @@ -244,10 +244,15 @@ namespace BoneSync.Sync public static void OnObjectSyncMessage(ObjectSyncMessage objectSyncMessage) { ObjectSyncMessageData data = objectSyncMessage.objectSyncMessageData; - Syncable syncable = ObjectSyncCache.GetSyncable(data.objectId); + ushort objectId = data.objectId; + if (objectId >= _nextSyncableId && !BoneSync.lobby.IsHost) + { + _nextSyncableId = (ushort)(objectId + 1); + } + Syncable syncable = ObjectSyncCache.GetSyncable(objectId); if (syncable == null) { - MelonLogger.Msg("SyncEvent: Syncable not found for id: " + data.objectId); + MelonLogger.Msg("SyncEvent: Syncable not found for id: " + objectId); return; } syncable.ApplyObjectSyncTransforms(data.objectSyncTransforms);