431 lines
11 KiB
C#
Raw Normal View History

2021-02-10 19:34:41 +00:00
using OWML.Common;
using OWML.Utils;
2021-08-09 11:49:58 +01:00
using QSB.ClientServerStateSync;
2021-02-10 19:34:41 +00:00
using QSB.DeathSync;
2020-12-14 16:24:52 +00:00
using QSB.Events;
2021-06-11 14:34:27 +01:00
using QSB.Inputs;
using QSB.Player;
2020-11-03 17:56:48 +00:00
using QSB.TimeSync.Events;
2021-02-10 19:34:41 +00:00
using QSB.Utility;
2020-12-04 22:15:41 +00:00
using QuantumUNET;
using System;
2021-08-09 11:49:58 +01:00
using UnityEngine;
namespace QSB.TimeSync
2020-02-15 20:48:02 +01:00
{
2020-12-23 12:58:45 +00:00
public class WakeUpSync : QNetworkBehaviour
2020-12-02 21:29:53 +00:00
{
public static WakeUpSync LocalInstance { get; private set; }
2021-03-26 14:44:43 +00:00
private const float PauseOrFastForwardThreshold = 1.0f;
2021-03-25 23:07:53 +00:00
private const float TimescaleBounds = 0.3f;
2020-12-02 21:29:53 +00:00
private const float MaxFastForwardSpeed = 60f;
private const float MaxFastForwardDiff = 20f;
private const float MinFastForwardSpeed = 2f;
public enum State { NotLoaded, Loaded, FastForwarding, Pausing }
2020-12-03 08:28:05 +00:00
public State CurrentState { get; private set; } = State.NotLoaded;
public Enum CurrentReason { get; private set; }
2020-12-02 21:29:53 +00:00
private float _sendTimer;
private float _serverTime;
private int _serverLoopCount;
private bool _hasWokenUp;
2020-12-02 21:29:53 +00:00
2020-12-18 20:28:22 +00:00
public override void OnStartLocalPlayer() => LocalInstance = this;
2020-12-02 21:29:53 +00:00
2021-11-11 19:34:25 +00:00
public void OnDisconnect()
{
OWTime.SetTimeScale(1f);
OWTime.SetMaxDeltaTime(0.06666667f);
OWTime.SetFixedTimestep(0.01666667f);
Locator.GetActiveCamera().enabled = true;
CurrentState = State.NotLoaded;
CurrentReason = null;
Physics.SyncTransforms();
SpinnerUI.Hide();
TimeSyncUI.Stop();
QSBInputManager.Instance.SetInputsEnabled(true);
}
2020-12-14 21:41:56 +01:00
public void Start()
2020-12-02 21:29:53 +00:00
{
if (!IsLocalPlayer)
{
return;
}
if (QSBSceneManager.IsInUniverse)
{
Init();
}
2021-06-18 22:38:32 +01:00
2020-12-02 21:29:53 +00:00
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
GlobalMessenger.AddListener(EventNames.WakeUp, OnWakeUp);
}
2021-03-25 23:07:53 +00:00
public float GetTimeDifference()
{
var myTime = Time.timeSinceLevelLoad;
return myTime - _serverTime;
}
2020-12-02 21:29:53 +00:00
private void OnWakeUp()
{
2021-02-14 09:43:36 +00:00
DebugLog.DebugWrite($"OnWakeUp", MessageType.Info);
2020-12-23 12:58:45 +00:00
if (QNetworkServer.active)
2020-12-02 21:29:53 +00:00
{
2021-02-06 22:03:10 +00:00
RespawnOnDeath.Instance.Init();
2020-12-02 21:29:53 +00:00
}
2021-06-18 22:38:32 +01:00
_hasWokenUp = true;
2020-12-02 21:29:53 +00:00
}
2020-12-14 21:41:56 +01:00
public void OnDestroy()
2020-12-02 21:29:53 +00:00
{
QSBSceneManager.OnSceneLoaded -= OnSceneLoaded;
GlobalMessenger.RemoveListener(EventNames.WakeUp, OnWakeUp);
}
private void OnSceneLoaded(OWScene oldScene, OWScene newScene, bool isInUniverse)
2020-12-02 21:29:53 +00:00
{
_hasWokenUp = false;
2020-12-02 21:29:53 +00:00
if (isInUniverse)
{
if (newScene == OWScene.EyeOfTheUniverse)
{
_hasWokenUp = true;
}
2021-06-18 22:38:32 +01:00
2020-12-02 21:29:53 +00:00
Init();
}
else
{
CurrentState = State.NotLoaded;
2020-12-02 21:29:53 +00:00
}
}
private void Init()
{
QSBEventManager.FireEvent(EventNames.QSBRequestStateResync);
CurrentState = State.Loaded;
2021-05-08 16:57:01 +01:00
gameObject.GetAddComponent<PreserveTimeScale>();
2020-12-02 21:29:53 +00:00
if (IsServer)
{
SendServerTime();
}
else
{
2021-11-25 23:52:46 +00:00
if (!QSBCore.SkipTitleScreen)
{
WakeUpOrSleep();
}
2020-12-02 21:29:53 +00:00
}
}
2021-06-19 11:26:05 +01:00
private void SendServerTime()
2021-05-09 19:55:19 +01:00
=> QSBEventManager.FireEvent(EventNames.QSBServerTime, _serverTime, PlayerData.LoadLoopCount());
2020-12-02 21:29:53 +00:00
public void OnClientReceiveMessage(ServerTimeMessage message)
{
_serverTime = message.ServerTime;
_serverLoopCount = message.LoopCount;
}
private void WakeUpOrSleep()
{
if (CurrentState == State.NotLoaded)
2020-12-02 21:29:53 +00:00
{
return;
}
if (PlayerData.LoadLoopCount() != _serverLoopCount && !IsServer)
2021-05-09 19:55:19 +01:00
{
2021-06-23 21:46:52 +01:00
DebugLog.ToConsole($"Warning - ServerLoopCount is not the same as local loop count! local:{PlayerData.LoadLoopCount()} server:{_serverLoopCount}");
2021-05-09 19:55:19 +01:00
return;
}
2020-12-02 21:29:53 +00:00
var myTime = Time.timeSinceLevelLoad;
var diff = myTime - _serverTime;
2021-12-10 22:13:39 +00:00
if (ServerStateManager.Instance.GetServerState() is not ServerState.InSolarSystem and not ServerState.InEye)
{
return;
}
2021-03-25 23:07:53 +00:00
if (diff > PauseOrFastForwardThreshold)
2020-12-02 21:29:53 +00:00
{
StartPausing(PauseReason.TooFarAhead);
2020-12-02 21:29:53 +00:00
return;
}
2021-03-25 23:07:53 +00:00
if (diff < -PauseOrFastForwardThreshold)
2020-12-02 21:29:53 +00:00
{
StartFastForwarding(FastForwardReason.TooFarBehind);
2020-12-02 21:29:53 +00:00
}
}
private void StartFastForwarding(FastForwardReason reason)
2021-05-09 19:55:19 +01:00
{
if (CurrentState == State.FastForwarding)
2020-12-02 21:29:53 +00:00
{
2020-12-19 19:06:13 +00:00
TimeSyncUI.TargetTime = _serverTime;
2020-12-02 21:29:53 +00:00
return;
}
2021-06-18 22:38:32 +01:00
2021-02-10 19:34:41 +00:00
DebugLog.DebugWrite($"START FASTFORWARD (Target:{_serverTime} Current:{Time.timeSinceLevelLoad})", MessageType.Info);
2021-03-08 20:35:36 +00:00
if (Locator.GetActiveCamera() != null)
2021-02-28 14:43:05 +00:00
{
2021-03-08 20:35:36 +00:00
Locator.GetActiveCamera().enabled = false;
2021-02-28 14:43:05 +00:00
}
2021-06-11 14:34:27 +01:00
//OWInput.ChangeInputMode(InputMode.None);
QSBInputManager.Instance.SetInputsEnabled(false);
CurrentState = State.FastForwarding;
CurrentReason = reason;
2021-02-28 14:43:05 +00:00
OWTime.SetMaxDeltaTime(0.033333335f);
2021-03-08 20:35:36 +00:00
OWTime.SetFixedTimestep(0.033333335f);
2020-12-19 19:06:13 +00:00
TimeSyncUI.TargetTime = _serverTime;
TimeSyncUI.Start(TimeSyncType.Fastforwarding, FastForwardReason.TooFarBehind);
2020-12-02 21:29:53 +00:00
}
private void StartPausing(PauseReason reason)
2020-12-02 21:29:53 +00:00
{
if (CurrentState == State.Pausing)
2020-12-02 21:29:53 +00:00
{
return;
}
2021-06-18 22:38:32 +01:00
2021-02-10 19:34:41 +00:00
DebugLog.DebugWrite($"START PAUSING (Target:{_serverTime} Current:{Time.timeSinceLevelLoad})", MessageType.Info);
2021-03-08 20:35:36 +00:00
Locator.GetActiveCamera().enabled = false;
2021-06-11 14:34:27 +01:00
//OWInput.ChangeInputMode(InputMode.None);
QSBInputManager.Instance.SetInputsEnabled(false);
2021-03-26 14:44:43 +00:00
OWTime.SetTimeScale(0f);
CurrentState = State.Pausing;
CurrentReason = reason;
2020-12-02 21:29:53 +00:00
SpinnerUI.Show();
TimeSyncUI.Start(TimeSyncType.Pausing, reason);
2020-12-02 21:29:53 +00:00
}
private void ResetTimeScale()
{
2021-03-26 14:44:43 +00:00
OWTime.SetTimeScale(1f);
2021-02-28 14:43:05 +00:00
OWTime.SetMaxDeltaTime(0.06666667f);
2021-03-08 19:31:46 +00:00
OWTime.SetFixedTimestep(0.01666667f);
2021-03-08 20:35:36 +00:00
Locator.GetActiveCamera().enabled = true;
CurrentState = State.Loaded;
CurrentReason = null;
2020-12-02 21:29:53 +00:00
2021-02-10 19:34:41 +00:00
DebugLog.DebugWrite($"RESET TIMESCALE", MessageType.Info);
2020-12-02 21:29:53 +00:00
Physics.SyncTransforms();
SpinnerUI.Hide();
TimeSyncUI.Stop();
QSBEventManager.FireEvent(EventNames.QSBRequestStateResync);
2020-12-02 21:29:53 +00:00
RespawnOnDeath.Instance.Init();
2021-06-11 14:34:27 +01:00
QSBInputManager.Instance.SetInputsEnabled(true);
if (!_hasWokenUp)
{
WakeUp();
}
2020-12-02 21:29:53 +00:00
}
2021-06-19 11:26:05 +01:00
private void WakeUp()
=> Locator.GetPlayerCamera().GetComponent<PlayerCameraEffectController>().Invoke("WakeUp");
2020-12-14 21:41:56 +01:00
public void Update()
2020-12-02 21:29:53 +00:00
{
if (IsServer)
{
UpdateServer();
}
2021-11-25 23:52:46 +00:00
else if (IsLocalPlayer && !QSBCore.AvoidTimeSync)
2020-12-02 21:29:53 +00:00
{
UpdateClient();
2020-12-02 21:29:53 +00:00
}
}
private void UpdateServer()
{
2021-03-25 23:07:53 +00:00
_serverTime = Time.timeSinceLevelLoad;
2021-08-22 16:54:48 +01:00
2021-11-24 22:16:56 +00:00
if (ServerStateManager.Instance == null)
{
DebugLog.ToConsole($"Warning - ServerStateManager.Instance is null!", MessageType.Warning);
return;
}
if (QSBPlayerManager.LocalPlayer == null)
{
DebugLog.ToConsole($"Warning - LocalPlayer is null!", MessageType.Warning);
return;
}
var serverState = ServerStateManager.Instance.GetServerState();
var clientState = QSBPlayerManager.LocalPlayer.State;
if (serverState == ServerState.WaitingForAllPlayersToReady && clientState == ClientState.WaitingForOthersToReadyInSolarSystem)
{
if (CurrentState != State.Pausing)
{
StartPausing(PauseReason.WaitingForAllPlayersToBeReady);
}
}
if (CurrentState == State.Pausing && (PauseReason)CurrentReason == PauseReason.WaitingForAllPlayersToBeReady)
{
if (clientState == ClientState.AliveInSolarSystem && serverState == ServerState.InSolarSystem)
{
ResetTimeScale();
}
}
2021-10-25 16:31:15 +01:00
if (serverState == ServerState.WaitingForAllPlayersToDie && clientState == ClientState.WaitingForOthersToReadyInSolarSystem)
{
if (CurrentState == State.Pausing && (PauseReason)CurrentReason == PauseReason.WaitingForAllPlayersToBeReady)
{
//?
DebugLog.ToConsole($"Warning - Server waiting for players to die, but players waiting for ready signal! Assume players correct.", MessageType.Warning);
QSBEventManager.FireEvent(EventNames.QSBServerState, ServerState.WaitingForAllPlayersToReady);
}
}
if (CurrentState != State.Loaded)
2020-12-02 21:29:53 +00:00
{
return;
}
_sendTimer += Time.unscaledDeltaTime;
if (_sendTimer > 1)
{
SendServerTime();
_sendTimer = 0;
}
}
private void UpdateClient()
2020-12-02 21:29:53 +00:00
{
_serverTime += Time.unscaledDeltaTime;
var serverState = ServerStateManager.Instance.GetServerState();
var clientState = QSBPlayerManager.LocalPlayer.State;
var currentScene = QSBSceneManager.CurrentScene;
2020-12-02 21:29:53 +00:00
// set fastforwarding timescale
if (CurrentState == State.FastForwarding)
2020-12-02 21:29:53 +00:00
{
2021-02-28 14:43:05 +00:00
if (Locator.GetPlayerCamera() != null && !Locator.GetPlayerCamera().enabled)
{
Locator.GetPlayerCamera().enabled = false;
}
2021-06-18 22:38:32 +01:00
2020-12-02 21:29:53 +00:00
var diff = _serverTime - Time.timeSinceLevelLoad;
2021-03-26 14:44:43 +00:00
OWTime.SetTimeScale(Mathf.SmoothStep(MinFastForwardSpeed, MaxFastForwardSpeed, Mathf.Abs(diff) / MaxFastForwardDiff));
2020-12-02 21:29:53 +00:00
2021-07-29 23:44:59 +01:00
TimeSyncUI.TargetTime = _serverTime;
}
if (CurrentState != State.Loaded && CurrentState != State.NotLoaded && CurrentReason == null)
{
2021-10-24 10:47:25 +01:00
DebugLog.ToConsole($"Warning - CurrentReason is null.", MessageType.Warning);
}
2021-07-29 23:44:59 +01:00
// Checks to pause/fastforward
2021-11-20 19:49:50 +00:00
if (clientState is ClientState.NotLoaded or ClientState.InTitleScreen)
{
return;
}
if (serverState == ServerState.NotLoaded && CurrentState != State.Pausing && QSBSceneManager.IsInUniverse)
{
StartPausing(PauseReason.ServerNotStarted);
}
if (serverState == ServerState.WaitingForAllPlayersToReady && CurrentState != State.Pausing && clientState == ClientState.WaitingForOthersToReadyInSolarSystem)
{
StartPausing(PauseReason.WaitingForAllPlayersToBeReady);
}
if (serverState == ServerState.WaitingForAllPlayersToDie && clientState == ClientState.WaitingForOthersToReadyInSolarSystem)
{
StartPausing(PauseReason.WaitingForAllPlayersToBeReady);
}
// Checks to revert to normal
if (CurrentState == State.Pausing && (PauseReason)CurrentReason == PauseReason.ServerNotStarted)
{
if (serverState != ServerState.NotLoaded)
2020-12-02 21:29:53 +00:00
{
ResetTimeScale();
2020-12-02 21:29:53 +00:00
}
}
if (CurrentState == State.Pausing && (PauseReason)CurrentReason == PauseReason.WaitingForAllPlayersToBeReady)
{
if (clientState == ClientState.AliveInSolarSystem && serverState == ServerState.InSolarSystem)
{
ResetTimeScale();
}
}
2020-12-02 21:29:53 +00:00
if (CurrentState == State.Pausing && (PauseReason)CurrentReason == PauseReason.TooFarAhead)
2020-12-02 21:29:53 +00:00
{
if (Time.timeSinceLevelLoad <= _serverTime)
{
ResetTimeScale();
}
2020-12-02 21:29:53 +00:00
}
2021-03-25 23:07:53 +00:00
if (CurrentState == State.FastForwarding && (FastForwardReason)CurrentReason == FastForwardReason.TooFarBehind)
2021-03-25 23:07:53 +00:00
{
if (Time.timeSinceLevelLoad >= _serverTime)
{
ResetTimeScale();
}
2021-03-25 23:07:53 +00:00
}
2021-08-10 14:54:23 +01:00
if (CurrentState == State.Loaded)
{
CheckTimeDifference();
}
2021-03-25 23:07:53 +00:00
}
private void CheckTimeDifference()
{
var diff = GetTimeDifference();
2021-11-20 19:49:50 +00:00
if (diff is > PauseOrFastForwardThreshold or < (-PauseOrFastForwardThreshold))
2021-03-25 23:07:53 +00:00
{
WakeUpOrSleep();
2021-05-09 19:55:19 +01:00
return;
2021-03-25 23:07:53 +00:00
}
var mappedTimescale = diff.Map(-PauseOrFastForwardThreshold, PauseOrFastForwardThreshold, 1 + TimescaleBounds, 1 - TimescaleBounds);
2021-05-08 09:30:09 +01:00
if (mappedTimescale > 100f)
{
DebugLog.ToConsole($"Warning - CheckTimeDifference() returned over 100 - should have switched into fast-forward!", MessageType.Warning);
2021-06-11 14:34:27 +01:00
mappedTimescale = 0f;
2021-05-08 09:30:09 +01:00
}
2021-06-18 22:38:32 +01:00
2021-05-08 09:30:09 +01:00
if (mappedTimescale < 0)
{
DebugLog.ToConsole($"Warning - CheckTimeDifference() returned below 0 - should have switched into pausing!", MessageType.Warning);
mappedTimescale = 0f;
}
2021-06-18 22:38:32 +01:00
2021-03-25 23:07:53 +00:00
OWTime.SetTimeScale(mappedTimescale);
2020-12-02 21:29:53 +00:00
}
}
2020-11-26 13:09:34 +00:00
}