From 00f0a33a47fb5a434622ffc54aac0754561cf8d3 Mon Sep 17 00:00:00 2001 From: Aaro Varis Date: Mon, 10 Mar 2025 01:54:27 +0200 Subject: [PATCH] Fix stuff? --- .../Networking/Messages/PlugSyncMessage.cs | 4 +-- BoneSync/Networking/Packet.cs | 6 +++++ BoneSync/Patching/PlugPatches.cs | 2 +- BoneSync/Patching/ZonePatches.cs | 3 ++- BoneSync/Sync/Components/SyncableAI.cs | 1 + BoneSync/Sync/Components/SyncableBase.cs | 1 + .../Sync/Components/SyncableNetworking.cs | 9 ++++++- BoneSync/Sync/Components/SyncablePlugs.cs | 25 +++++++++++++++++++ BoneSync/Sync/SceneSync.cs | 4 ++- 9 files changed, 49 insertions(+), 6 deletions(-) diff --git a/BoneSync/Networking/Messages/PlugSyncMessage.cs b/BoneSync/Networking/Messages/PlugSyncMessage.cs index 0d9568e..a858df0 100644 --- a/BoneSync/Networking/Messages/PlugSyncMessage.cs +++ b/BoneSync/Networking/Messages/PlugSyncMessage.cs @@ -42,7 +42,7 @@ namespace BoneSync.Networking.Messages public override void Execute() { - MelonLogger.Msg("Got PlugSyncMessage " + messageData.plugSyncId + ":" + messageData.plugIndex + " " + messageData.socketSyncId + ":" + messageData.socketIndex); + //MelonLogger.Msg("Got PlugSyncMessage " + messageData.plugSyncId + ":" + messageData.plugIndex + " " + messageData.socketSyncId + ":" + messageData.socketIndex); Syncable plugSyncable = ObjectSyncCache.GetSyncable(messageData.plugSyncId); Syncable socketSyncable = ObjectSyncCache.GetSyncable(messageData.socketSyncId); @@ -76,7 +76,7 @@ namespace BoneSync.Networking.Messages return; } - MelonLogger.Msg("Inserting networked plug"); + //MelonLogger.Msg("Inserting networked plug"); plug.SafeInsert(socket); } diff --git a/BoneSync/Networking/Packet.cs b/BoneSync/Networking/Packet.cs index 96cfee8..475c5d5 100644 --- a/BoneSync/Networking/Packet.cs +++ b/BoneSync/Networking/Packet.cs @@ -2,6 +2,7 @@ using BoneSync.Networking.Messages; using BoneSync.Sync; using Facepunch.Steamworks; +using MelonLoader; using System; using System.Collections.Generic; using System.Linq; @@ -145,6 +146,7 @@ namespace BoneSync.Networking { bool addToQueue = networkMessage.GetCatchupOrder() > 0; if (!addToQueue) return false; + MelonLogger.Msg("Adding packet to queue for scene " + packet._packetInfo.sceneIndex); EnsureQueueForScene(packet._packetInfo.sceneIndex); _packetQueues[packet._packetInfo.sceneIndex].Enqueue(networkMessage); return true; @@ -174,6 +176,10 @@ namespace BoneSync.Networking NetworkMessage networkMessage = queue.Dequeue(); networkMessage.Execute(); } + if (processed > 0) + { + MelonLogger.Msg("Processed " + processed + " queued packets for scene " + sceneIndex); + } } public byte[] Data => _dataBytes; diff --git a/BoneSync/Patching/PlugPatches.cs b/BoneSync/Patching/PlugPatches.cs index cfad389..f121a33 100644 --- a/BoneSync/Patching/PlugPatches.cs +++ b/BoneSync/Patching/PlugPatches.cs @@ -54,7 +54,7 @@ namespace BoneSync.Patching byte plugId = plugSyncable.GetPlugId(plug); byte socketId = socketSyncable != null ? socketSyncable.GetSocketId(socket) : byte.MaxValue; - MelonLogger.Msg("AlignPlug state:" + plug.transform.GetPath() + " Socket:" + socket?.transform?.GetPath() + " PlugID:" + plugId + " SocketID:" + socketId); + //MelonLogger.Msg("AlignPlug state:" + plug.transform.GetPath() + " Socket:" + socket?.transform?.GetPath() + " PlugID:" + plugId + " SocketID:" + socketId); if (!plugSyncable.isOwner) return; diff --git a/BoneSync/Patching/ZonePatches.cs b/BoneSync/Patching/ZonePatches.cs index aec916f..b2a5652 100644 --- a/BoneSync/Patching/ZonePatches.cs +++ b/BoneSync/Patching/ZonePatches.cs @@ -127,6 +127,7 @@ namespace BoneSync.Patching } // SceneZone and PlayerTrigger patches are based on the ones from entanglement mod + /* [HarmonyPatch(typeof(SceneZone))] public static class SceneZonePatch { @@ -185,5 +186,5 @@ namespace BoneSync.Patching return true; } - } + }*/ } diff --git a/BoneSync/Sync/Components/SyncableAI.cs b/BoneSync/Sync/Components/SyncableAI.cs index 0f618bf..1278725 100644 --- a/BoneSync/Sync/Components/SyncableAI.cs +++ b/BoneSync/Sync/Components/SyncableAI.cs @@ -16,6 +16,7 @@ namespace BoneSync.Sync.Components private void TrySendAISync() { + if (!Registered) return; if (!isOwner) return; if (!aiBrain) return; if (Time.realtimeSinceStartup - _lastAISyncTime > 1 / AI_SYNC_FPS) diff --git a/BoneSync/Sync/Components/SyncableBase.cs b/BoneSync/Sync/Components/SyncableBase.cs index c178182..1fc1981 100644 --- a/BoneSync/Sync/Components/SyncableBase.cs +++ b/BoneSync/Sync/Components/SyncableBase.cs @@ -156,6 +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 (InPoolManagerTransform()) return false; if (!gameObject.activeInHierarchy) return false; if (poolee && poolee.pool) { diff --git a/BoneSync/Sync/Components/SyncableNetworking.cs b/BoneSync/Sync/Components/SyncableNetworking.cs index 0da83a3..3f2c844 100644 --- a/BoneSync/Sync/Components/SyncableNetworking.cs +++ b/BoneSync/Sync/Components/SyncableNetworking.cs @@ -19,6 +19,11 @@ namespace BoneSync.Sync.Components _lastSyncTime = Time.realtimeSinceStartup; ObjectSync.SendObjectSyncMessage(this); } + + private void TrySendObjectSync() + { + if (ShouldSendSync()) SendObjectSync(); + } public IEnumerator SyncCoroutineAsync() { MelonLogger.Msg("Running sync coroutine for: " + transform.GetPath()); @@ -26,7 +31,9 @@ namespace BoneSync.Sync.Components _syncCoroutineRunning = true; while (isOwner) { - if (ShouldSendSync()) SendObjectSync(); + TrySendAISync(); + TrySendObjectSync(); + //TrySendPlugSync(); yield return new WaitForSeconds(!Registered ? 1 : 1 / SYNC_FPS); } _syncCoroutineRunning = false; diff --git a/BoneSync/Sync/Components/SyncablePlugs.cs b/BoneSync/Sync/Components/SyncablePlugs.cs index b5b5148..bd90389 100644 --- a/BoneSync/Sync/Components/SyncablePlugs.cs +++ b/BoneSync/Sync/Components/SyncablePlugs.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using UnityEngine; using MelonLoader; using StressLevelZero.Interaction; +using BoneSync.Patching; namespace BoneSync.Sync.Components { @@ -64,6 +65,30 @@ namespace BoneSync.Sync.Components } public partial class Syncable : MonoBehaviour { + private float _lastPlugSyncTime; + private const float PLUG_SYNC_FPS = 1f; + private void TrySendPlugSync() + { + if (!Registered) return; + if (!isOwner) return; + if (Time.realtimeSinceStartup - _lastPlugSyncTime > 1 / PLUG_SYNC_FPS) + { + _SendPlugSync(); + } + } + + private void _SendPlugSync() + { + if (!isOwner) return; + if (!Registered) return; + foreach (AlignPlug plug in plugs) + { + if (!plug) continue; + Socket socket = plug.GetSocket(); + AlignPlugPatches.OnPlugSocketChange(plug, socket); + _lastPlugSyncTime = Time.realtimeSinceStartup; + } + } public void EjectAllPlugs(bool force = false) { diff --git a/BoneSync/Sync/SceneSync.cs b/BoneSync/Sync/SceneSync.cs index 62e755f..57e321c 100644 --- a/BoneSync/Sync/SceneSync.cs +++ b/BoneSync/Sync/SceneSync.cs @@ -17,7 +17,8 @@ namespace BoneSync.Sync private static List scenes = new List(); private static string _currentSceneName; private static int _currentSceneIndex; - + private static float _lastSceneChangeTime; + public static float TimeSinceLastSceneChange => Time.realtimeSinceStartup - _lastSceneChangeTime; public static int CurrentSceneIndex => _currentSceneIndex; public static string CurrentSceneDisplayName @@ -69,6 +70,7 @@ namespace BoneSync.Sync public static void OnSceneInit(int buildIndex) { Scene scene = SceneManager.GetSceneByBuildIndex(buildIndex); + _lastSceneChangeTime = Time.realtimeSinceStartup; _currentSceneIndex = buildIndex; string SceneName = scene.name; _currentSceneName = SceneName;