use send initial state event

This commit is contained in:
JohnCorby 2022-04-06 11:35:40 -07:00
parent d45e94bfdf
commit d0cce35103
4 changed files with 31 additions and 28 deletions

View File

@ -139,6 +139,7 @@ public abstract class SyncBase : QSBNetworkTransform
public override void OnStopClient()
{
base.OnStopClient();
QSBSceneManager.OnSceneLoaded -= OnSceneLoaded;
if (IsInitialized)
{

View File

@ -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;
/// <summary>
/// checked before serializing
@ -80,11 +86,11 @@ public abstract class QSBNetworkBehaviour : NetworkBehaviour
/// <para/>
/// world objects will be ready on both sides at this point
/// </summary>
public void SendInitialState(NetworkConnectionToClient target)
private void SendInitialState(uint to)
{
if (_lastKnownData != null)
{
TargetSendInitialData(target, new ArraySegment<byte>(_lastKnownData));
TargetSendInitialData(to.GetNetworkConnection(), new ArraySegment<byte>(_lastKnownData));
}
}

View File

@ -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<IWorldObject> WorldObjects = new();
private static readonly Dictionary<MonoBehaviour, IWorldObject> 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<TUnityObject>
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));

View File

@ -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>())
{
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}");
}