2020-03-02 17:25:37 +01:00
|
|
|
|
using OWML.ModHelper.Events;
|
2020-02-15 20:48:02 +01:00
|
|
|
|
using QSB.Messaging;
|
2020-02-28 22:07:39 +01:00
|
|
|
|
using UnityEngine;
|
2020-02-24 19:55:16 +01:00
|
|
|
|
using UnityEngine.Networking;
|
2020-02-28 22:07:39 +01:00
|
|
|
|
using UnityEngine.SceneManagement;
|
2020-02-14 22:14:24 +01:00
|
|
|
|
|
2020-02-24 19:55:16 +01:00
|
|
|
|
namespace QSB.TimeSync
|
2020-02-15 20:48:02 +01:00
|
|
|
|
{
|
2020-02-24 19:55:16 +01:00
|
|
|
|
public class WakeUpSync : NetworkBehaviour
|
2020-02-15 20:48:02 +01:00
|
|
|
|
{
|
2020-02-28 22:07:39 +01:00
|
|
|
|
private const float TimeThreshold = 0.5f;
|
2020-02-29 22:12:09 +01:00
|
|
|
|
private const float FastForwardSpeed = 10f;
|
2020-02-28 22:07:39 +01:00
|
|
|
|
|
2020-02-29 22:12:09 +01:00
|
|
|
|
private enum State { NotLoaded, EyesClosed, Awake, FastForwarding, Pausing }
|
2020-02-28 22:07:39 +01:00
|
|
|
|
private State _state = State.NotLoaded;
|
|
|
|
|
|
2020-02-21 23:36:07 +01:00
|
|
|
|
private MessageHandler<WakeUpMessage> _wakeUpHandler;
|
2020-02-28 22:07:39 +01:00
|
|
|
|
|
|
|
|
|
private float _sendTimer;
|
|
|
|
|
private float _serverTime;
|
2020-02-29 22:12:09 +01:00
|
|
|
|
private float _timeScale;
|
|
|
|
|
private bool _isInputEnabled = true;
|
2020-02-15 20:48:02 +01:00
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
2020-02-24 19:55:16 +01:00
|
|
|
|
if (!isLocalPlayer)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-02-28 22:07:39 +01:00
|
|
|
|
|
2020-02-14 22:14:24 +01:00
|
|
|
|
DebugLog.Screen("Start WakeUpSync");
|
|
|
|
|
GlobalMessenger.AddListener("WakeUp", OnWakeUp);
|
2020-02-28 22:07:39 +01:00
|
|
|
|
SceneManager.sceneLoaded += OnSceneLoaded;
|
|
|
|
|
|
2020-02-21 23:36:07 +01:00
|
|
|
|
_wakeUpHandler = new MessageHandler<WakeUpMessage>();
|
|
|
|
|
_wakeUpHandler.OnClientReceiveMessage += OnClientReceiveMessage;
|
2020-02-14 22:14:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-28 22:07:39 +01:00
|
|
|
|
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
|
|
|
|
{
|
|
|
|
|
if (scene.name == "SolarSystem")
|
|
|
|
|
{
|
|
|
|
|
_state = State.EyesClosed;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 20:48:02 +01:00
|
|
|
|
private void OnWakeUp()
|
|
|
|
|
{
|
2020-02-28 22:07:39 +01:00
|
|
|
|
_state = State.Awake;
|
2020-02-29 09:39:15 +01:00
|
|
|
|
gameObject.AddComponent<PreserveTimeScale>();
|
2020-02-28 22:07:39 +01:00
|
|
|
|
if (isServer)
|
2020-02-15 20:48:02 +01:00
|
|
|
|
{
|
2020-02-28 22:07:39 +01:00
|
|
|
|
SendServerTime();
|
2020-02-14 22:14:24 +01:00
|
|
|
|
}
|
2020-02-28 22:07:39 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
WakeUpOrSleep();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SendServerTime()
|
|
|
|
|
{
|
|
|
|
|
var message = new WakeUpMessage
|
|
|
|
|
{
|
|
|
|
|
ServerTime = Time.timeSinceLevelLoad
|
|
|
|
|
};
|
|
|
|
|
_wakeUpHandler.SendToAll(message);
|
2020-02-14 22:14:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-21 23:36:07 +01:00
|
|
|
|
private void OnClientReceiveMessage(WakeUpMessage message)
|
2020-02-15 20:48:02 +01:00
|
|
|
|
{
|
2020-02-24 19:55:16 +01:00
|
|
|
|
if (isServer)
|
2020-02-15 20:48:02 +01:00
|
|
|
|
{
|
2020-02-14 22:14:24 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-02-28 22:07:39 +01:00
|
|
|
|
_serverTime = message.ServerTime;
|
|
|
|
|
WakeUpOrSleep();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void WakeUpOrSleep()
|
|
|
|
|
{
|
|
|
|
|
if (_state == State.NotLoaded)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_state == State.EyesClosed)
|
|
|
|
|
{
|
|
|
|
|
OpenEyes();
|
|
|
|
|
_state = State.Awake;
|
2020-02-29 22:12:09 +01:00
|
|
|
|
|
|
|
|
|
if (!isServer)
|
|
|
|
|
{
|
|
|
|
|
DisableInput();
|
|
|
|
|
}
|
2020-02-28 22:07:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var myTime = Time.timeSinceLevelLoad;
|
|
|
|
|
var diff = myTime - _serverTime;
|
|
|
|
|
|
|
|
|
|
if (diff > TimeThreshold)
|
|
|
|
|
{
|
|
|
|
|
StartPausing();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-02-14 22:14:24 +01:00
|
|
|
|
|
2020-02-28 22:07:39 +01:00
|
|
|
|
if (diff < -TimeThreshold)
|
|
|
|
|
{
|
2020-02-29 22:12:09 +01:00
|
|
|
|
StartFastForwarding();
|
2020-02-28 22:07:39 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OpenEyes()
|
|
|
|
|
{
|
2020-02-14 22:14:24 +01:00
|
|
|
|
// I copied all of this from my AutoResume mod, since that already wakes up the player instantly.
|
|
|
|
|
// There must be a simpler way to do this though, I just couldn't find it.
|
|
|
|
|
|
|
|
|
|
// Skip wake up animation.
|
|
|
|
|
var cameraEffectController = FindObjectOfType<PlayerCameraEffectController>();
|
|
|
|
|
cameraEffectController.OpenEyes(0, true);
|
|
|
|
|
cameraEffectController.SetValue("_wakeLength", 0f);
|
|
|
|
|
cameraEffectController.SetValue("_waitForWakeInput", false);
|
|
|
|
|
|
|
|
|
|
// Skip wake up prompt.
|
|
|
|
|
LateInitializerManager.pauseOnInitialization = false;
|
|
|
|
|
Locator.GetPauseCommandListener().RemovePauseCommandLock();
|
|
|
|
|
Locator.GetPromptManager().RemoveScreenPrompt(cameraEffectController.GetValue<ScreenPrompt>("_wakePrompt"));
|
|
|
|
|
OWTime.Unpause(OWTime.PauseType.Sleeping);
|
|
|
|
|
cameraEffectController.Invoke("WakeUp");
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 22:12:09 +01:00
|
|
|
|
private void StartFastForwarding()
|
2020-02-28 22:07:39 +01:00
|
|
|
|
{
|
2020-02-29 22:12:09 +01:00
|
|
|
|
if (_state == State.FastForwarding)
|
2020-02-28 22:07:39 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-02-29 22:12:09 +01:00
|
|
|
|
_timeScale = FastForwardSpeed;
|
|
|
|
|
_state = State.FastForwarding;
|
2020-02-28 22:07:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void StartPausing()
|
|
|
|
|
{
|
|
|
|
|
if (_state == State.Pausing)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-02-29 22:12:09 +01:00
|
|
|
|
_timeScale = 0f;
|
2020-02-28 22:07:39 +01:00
|
|
|
|
_state = State.Pausing;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 22:12:09 +01:00
|
|
|
|
private void ResetTimeScale()
|
2020-02-28 22:07:39 +01:00
|
|
|
|
{
|
2020-02-29 22:12:09 +01:00
|
|
|
|
_timeScale = 1f;
|
|
|
|
|
_state = State.Awake;
|
|
|
|
|
|
|
|
|
|
if (!_isInputEnabled)
|
2020-02-28 22:07:39 +01:00
|
|
|
|
{
|
2020-02-29 22:12:09 +01:00
|
|
|
|
EnableInput();
|
2020-02-28 22:07:39 +01:00
|
|
|
|
}
|
2020-02-29 22:12:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DisableInput()
|
|
|
|
|
{
|
|
|
|
|
_isInputEnabled = false;
|
|
|
|
|
OWInput.ChangeInputMode(InputMode.None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void EnableInput()
|
|
|
|
|
{
|
|
|
|
|
_isInputEnabled = true;
|
|
|
|
|
OWInput.ChangeInputMode(InputMode.Character);
|
2020-02-28 22:07:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
|
|
|
|
if (isServer)
|
|
|
|
|
{
|
|
|
|
|
UpdateServer();
|
|
|
|
|
}
|
|
|
|
|
else if (isLocalPlayer)
|
|
|
|
|
{
|
|
|
|
|
UpdateLocal();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateServer()
|
|
|
|
|
{
|
|
|
|
|
if (_state != State.Awake)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_sendTimer += Time.unscaledDeltaTime;
|
|
|
|
|
if (_sendTimer > 1)
|
|
|
|
|
{
|
|
|
|
|
SendServerTime();
|
|
|
|
|
_sendTimer = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateLocal()
|
|
|
|
|
{
|
|
|
|
|
_serverTime += Time.unscaledDeltaTime;
|
|
|
|
|
|
|
|
|
|
if (_state == State.NotLoaded || _state == State.EyesClosed)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 22:12:09 +01:00
|
|
|
|
bool isDoneFastForwarding = _state == State.FastForwarding && Time.timeSinceLevelLoad >= _serverTime;
|
|
|
|
|
bool isDonePausing = _state == State.Pausing && Time.timeSinceLevelLoad < _serverTime;
|
|
|
|
|
|
|
|
|
|
if (isDoneFastForwarding || isDonePausing)
|
2020-02-28 22:07:39 +01:00
|
|
|
|
{
|
2020-02-29 22:12:09 +01:00
|
|
|
|
ResetTimeScale();
|
2020-02-28 22:07:39 +01:00
|
|
|
|
}
|
2020-02-29 22:12:09 +01:00
|
|
|
|
|
|
|
|
|
Time.timeScale = _timeScale;
|
|
|
|
|
|
|
|
|
|
if (!_isInputEnabled && OWInput.GetInputMode() != InputMode.None)
|
2020-02-28 22:07:39 +01:00
|
|
|
|
{
|
2020-02-29 22:12:09 +01:00
|
|
|
|
DisableInput();
|
2020-02-28 22:07:39 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 22:14:24 +01:00
|
|
|
|
}
|
|
|
|
|
}
|