quantum-space-buddies/QSB/Player/Messages/RequestStateResyncMessage.cs

66 lines
1.6 KiB
C#
Raw Normal View History

2021-12-26 06:53:36 +00:00
using OWML.Common;
2023-08-02 20:46:50 +00:00
using QSB.API.Messages;
using QSB.ClientServerStateSync;
2021-12-25 06:33:29 +00:00
using QSB.ClientServerStateSync.Messages;
2020-12-14 16:28:03 +00:00
using QSB.Messaging;
2020-08-22 19:21:13 +00:00
using QSB.Utility;
2020-08-10 13:40:06 +00:00
2022-03-03 03:46:33 +00:00
namespace QSB.Player.Messages;
// Can be sent by any client (including host) to signal they want latest player and server information
public class RequestStateResyncMessage : QSBMessage
2020-08-10 13:40:06 +00:00
{
2022-03-03 03:46:33 +00:00
/// <summary>
/// set to true when we send this, and false when we receive a player info message back. <br/>
/// this prevents message spam a bit.
/// </summary>
internal static bool _waitingForEvent;
/// <summary>
/// used instead of QSBMessageManager.Send to do the extra check
/// </summary>
public void Send()
2020-12-02 21:23:01 +00:00
{
2022-03-03 03:46:33 +00:00
if (_waitingForEvent)
2021-12-11 11:47:21 +00:00
{
2022-03-03 03:46:33 +00:00
return;
}
2022-03-03 03:46:33 +00:00
_waitingForEvent = true;
QSBMessageManager.Send(this);
}
2022-03-03 03:46:33 +00:00
public override void OnReceiveLocal() => Delay.RunFramesLater(60,
() => {
if (_waitingForEvent)
{
if (QSBPlayerManager.PlayerList.Count > 1)
{
DebugLog.ToConsole($"Did not receive PlayerInformationEvent in time. Setting _waitingForEvent to false.", MessageType.Info);
2021-12-11 11:47:21 +00:00
}
2022-03-03 03:46:33 +00:00
_waitingForEvent = false;
2021-12-25 06:15:31 +00:00
}
2022-03-03 03:46:33 +00:00
});
2021-12-25 06:15:31 +00:00
2022-03-03 03:46:33 +00:00
public override void OnReceiveRemote()
{
if (QSBCore.IsHost)
{
new ServerStateMessage(ServerStateManager.Instance.GetServerState()) { To = From }.Send();
}
2022-03-03 03:46:33 +00:00
new PlayerInformationMessage { To = From }.Send();
2023-08-02 20:46:50 +00:00
// Initial sync of all custom data from APIs
2023-08-02 21:07:44 +00:00
foreach (var kvp in QSBPlayerManager.LocalPlayer._customData)
2023-08-02 20:46:50 +00:00
{
if (!kvp.Value.GetType().IsSerializable)
{
continue;
}
2023-08-02 21:07:44 +00:00
new AddonCustomDataSyncMessage(QSBPlayerManager.LocalPlayerId, kvp.Key, kvp.Value) { To = From }.Send();
2023-08-02 20:46:50 +00:00
}
2020-12-02 21:23:01 +00:00
}
}