From d0cce351036af5aa0d7d184b3d4c4765111ff536 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Wed, 6 Apr 2022 11:35:40 -0700 Subject: [PATCH] use send initial state event --- QSB/Syncs/SyncBase.cs | 1 + QSB/Utility/QSBNetworkBehaviour.cs | 12 +++++++--- QSB/WorldSync/QSBWorldSync.cs | 23 +++++++++++++++++--- QSB/WorldSync/RequestInitialStatesMessage.cs | 23 +------------------- 4 files changed, 31 insertions(+), 28 deletions(-) diff --git a/QSB/Syncs/SyncBase.cs b/QSB/Syncs/SyncBase.cs index a1deaa67..f919a077 100644 --- a/QSB/Syncs/SyncBase.cs +++ b/QSB/Syncs/SyncBase.cs @@ -139,6 +139,7 @@ public abstract class SyncBase : QSBNetworkTransform public override void OnStopClient() { + base.OnStopClient(); QSBSceneManager.OnSceneLoaded -= OnSceneLoaded; if (IsInitialized) { diff --git a/QSB/Utility/QSBNetworkBehaviour.cs b/QSB/Utility/QSBNetworkBehaviour.cs index db6e51f2..534fda1c 100644 --- a/QSB/Utility/QSBNetworkBehaviour.cs +++ b/QSB/Utility/QSBNetworkBehaviour.cs @@ -12,7 +12,13 @@ public abstract class QSBNetworkBehaviour : NetworkBehaviour private double _lastSendTime; private byte[] _lastKnownData; - public override void OnStartClient() => DontDestroyOnLoad(gameObject); + public override void OnStartClient() + { + DontDestroyOnLoad(gameObject); + RequestInitialStatesMessage.SendInitialState += SendInitialState; + } + + public override void OnStopClient() => RequestInitialStatesMessage.SendInitialState -= SendInitialState; /// /// checked before serializing @@ -80,11 +86,11 @@ public abstract class QSBNetworkBehaviour : NetworkBehaviour /// /// world objects will be ready on both sides at this point /// - public void SendInitialState(NetworkConnectionToClient target) + private void SendInitialState(uint to) { if (_lastKnownData != null) { - TargetSendInitialData(target, new ArraySegment(_lastKnownData)); + TargetSendInitialData(to.GetNetworkConnection(), new ArraySegment(_lastKnownData)); } } diff --git a/QSB/WorldSync/QSBWorldSync.cs b/QSB/WorldSync/QSBWorldSync.cs index d899c4f4..8c5bc07f 100644 --- a/QSB/WorldSync/QSBWorldSync.cs +++ b/QSB/WorldSync/QSBWorldSync.cs @@ -1,7 +1,9 @@ using Cysharp.Threading.Tasks; using MonoMod.Utils; using OWML.Common; +using QSB.ConversationSync.Messages; using QSB.LogSync; +using QSB.LogSync.Messages; using QSB.Messaging; using QSB.Player.TransformSync; using QSB.TriggerSync.WorldObjects; @@ -137,9 +139,10 @@ public static class QSBWorldSync GameReset(); - foreach (var item in WorldObjects) + foreach (var worldObject in WorldObjects) { - item.Try("removing", item.OnRemoval); + worldObject.Try("removing", worldObject.OnRemoval); + RequestInitialStatesMessage.SendInitialState -= worldObject.SendInitialState; } WorldObjects.Clear(); @@ -161,6 +164,18 @@ public static class QSBWorldSync private static readonly List WorldObjects = new(); private static readonly Dictionary UnityObjectsToWorldObjects = new(); + static QSBWorldSync() + { + RequestInitialStatesMessage.SendInitialState += to => + { + DialogueConditions.ForEach(condition + => new DialogueConditionMessage(condition.Key, condition.Value) { To = to }.Send()); + + ShipLogFacts.ForEach(fact + => new RevealFactMessage(fact.Id, fact.SaveGame, false) { To = to }.Send()); + }; + } + private static void GameInit() { DebugLog.DebugWrite($"GameInit QSBWorldSync", MessageType.Info); @@ -303,13 +318,15 @@ public static class QSBWorldSync where TWorldObject : WorldObject where TUnityObject : MonoBehaviour { - WorldObjects.Add(worldObject); if (!UnityObjectsToWorldObjects.TryAdd(unityObject, worldObject)) { DebugLog.ToConsole($"Error - UnityObjectsToWorldObjects already contains \"{unityObject.name}\"! TWorldObject:{typeof(TWorldObject).Name}, TUnityObject:{unityObject.GetType().Name}, Stacktrace:\r\n{Environment.StackTrace}", MessageType.Error); return; } + WorldObjects.Add(worldObject); + RequestInitialStatesMessage.SendInitialState += worldObject.SendInitialState; + var task = UniTask.Create(async () => { await worldObject.Try("initing", () => worldObject.Init(_cts.Token)); diff --git a/QSB/WorldSync/RequestInitialStatesMessage.cs b/QSB/WorldSync/RequestInitialStatesMessage.cs index bed19746..16a3ba76 100644 --- a/QSB/WorldSync/RequestInitialStatesMessage.cs +++ b/QSB/WorldSync/RequestInitialStatesMessage.cs @@ -1,6 +1,4 @@ -using QSB.ConversationSync.Messages; -using QSB.LogSync.Messages; -using QSB.Messaging; +using QSB.Messaging; using QSB.Utility; using System; @@ -21,26 +19,7 @@ public class RequestInitialStatesMessage : QSBMessage private static void SendInitialStates(uint to) { - QSBWorldSync.DialogueConditions.ForEach(condition - => new DialogueConditionMessage(condition.Key, condition.Value) { To = to }.Send()); - - QSBWorldSync.ShipLogFacts.ForEach(fact - => new RevealFactMessage(fact.Id, fact.SaveGame, false) { To = to }.Send()); - - var target = to.GetNetworkConnection(); - foreach (var qsbNetworkBehaviour in QSBWorldSync.GetUnityObjects()) - { - qsbNetworkBehaviour.SendInitialState(target); - } - - foreach (var worldObject in QSBWorldSync.GetWorldObjects()) - { - worldObject.Try("sending initial state", () => - worldObject.SendInitialState(to)); - } - SendInitialState?.SafeInvoke(to); - DebugLog.DebugWrite($"sent initial states to {to}"); }