303 lines
8.0 KiB
C#
Raw Normal View History

using QSB.Player;
2021-12-04 23:24:26 +00:00
using QSB.Utility;
using System.Linq;
2021-08-26 08:51:33 +01:00
using UnityEngine;
using UnityEngine.Networking;
2021-08-24 21:08:55 +01:00
using UnityEngine.UI;
2021-06-04 11:31:09 +01:00
namespace QSB.Menus
{
2021-10-29 23:00:13 +01:00
internal class MenuManager : MonoBehaviour
2021-06-04 11:31:09 +01:00
{
public static MenuManager Instance;
2021-06-04 11:31:09 +01:00
private IMenuAPI MenuApi => QSBCore.MenuApi;
2021-08-24 21:08:55 +01:00
private PopupMenu PopupMenu;
2021-12-04 23:24:26 +00:00
private PopupMenu InfoPopup;
private bool _addedPauseLock;
2021-08-24 22:30:53 +01:00
private Button HostButton;
private GameObject ClientButton;
2021-08-24 21:08:55 +01:00
private Button DisconnectButton;
2021-12-04 23:24:26 +00:00
private GameObject ResumeGameButton;
private GameObject NewGameButton;
2021-12-05 11:00:18 +00:00
private const int _ClientButtonIndex = 2;
private const int _DisconnectIndex = 3;
2021-06-04 11:31:09 +01:00
public void Start()
{
Instance = this;
2021-06-04 11:31:09 +01:00
MakeTitleMenus();
2021-08-26 08:51:33 +01:00
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
QSBNetworkManager.Instance.OnClientConnected += OnConnected;
QSBNetworkManager.Instance.OnClientDisconnected += OnDisconnected;
QSBNetworkManager.Instance.OnClientErrorThrown += OnClientError;
2021-08-26 08:51:33 +01:00
}
2021-10-29 23:00:13 +01:00
private void OnSceneLoaded(OWScene oldScene, OWScene newScene, bool isUniverse)
2021-08-26 08:51:33 +01:00
{
if (isUniverse)
{
InitPauseMenus();
return;
}
if (newScene == OWScene.TitleScreen)
{
MakeTitleMenus();
}
}
private void OpenInfoPopup(string message, string buttonText)
{
InfoPopup.SetUpPopup(message, InputLibrary.menuConfirm, InputLibrary.cancel, new ScreenPrompt(buttonText), null, true, false);
OWTime.Pause(OWTime.PauseType.System);
OWInput.ChangeInputMode(InputMode.Menu);
var pauseCommandListener = Locator.GetPauseCommandListener();
if (pauseCommandListener != null)
{
pauseCommandListener.AddPauseCommandLock();
_addedPauseLock = true;
}
InfoPopup.EnableMenu(true);
}
private void OnCloseInfoPopup()
{
var pauseCommandListener = Locator.GetPauseCommandListener();
if (pauseCommandListener != null && _addedPauseLock)
{
pauseCommandListener.RemovePauseCommandLock();
_addedPauseLock = false;
}
2021-11-25 15:38:05 +00:00
OWTime.Unpause(OWTime.PauseType.System);
OWInput.RestorePreviousInputs();
}
private void CreateCommonPopups()
2021-08-26 08:51:33 +01:00
{
PopupMenu = MenuApi.MakeInputFieldPopup("IP Address", "IP Address", "Connect", "Cancel");
PopupMenu.OnPopupConfirm += Connect;
2021-08-27 11:05:47 +01:00
InfoPopup = MenuApi.MakeInfoPopup("", "");
InfoPopup.OnDeactivateMenu += OnCloseInfoPopup;
}
2021-12-04 23:24:26 +00:00
private void SetButtonActive(Button button, bool active)
=> SetButtonActive(button.gameObject, active);
private void SetButtonActive(GameObject button, bool active)
{
2021-12-05 11:00:18 +00:00
if (button == null)
{
return;
}
2021-12-04 23:24:26 +00:00
DebugLog.DebugWrite($"Set {button.name} to {active}");
button.SetActive(active);
button.GetComponent<CanvasGroup>().alpha = active ? 1 : 0;
}
private void InitPauseMenus()
{
CreateCommonPopups();
2021-08-26 08:51:33 +01:00
HostButton = MenuApi.PauseMenu_MakeSimpleButton("MULTIPLAYER (HOST)");
HostButton.onClick.AddListener(Host);
DisconnectButton = MenuApi.PauseMenu_MakeSimpleButton("DISCONNECT");
DisconnectButton.onClick.AddListener(Disconnect);
2021-10-15 21:08:17 +01:00
if (QSBCore.IsInMultiplayer)
{
ClientButton.SetActive(false);
HostButton.gameObject.SetActive(false);
2021-12-04 23:24:26 +00:00
SetButtonActive(DisconnectButton, true);
}
else
{
2021-12-04 23:24:26 +00:00
SetButtonActive(DisconnectButton, false);
}
OnConnected();
2021-06-04 11:31:09 +01:00
}
private void MakeTitleMenus()
{
CreateCommonPopups();
2021-08-26 08:51:33 +01:00
2021-12-04 23:24:26 +00:00
ClientButton = MenuApi.TitleScreen_MakeMenuOpenButton("MULTIPLAYER (CONNECT)", _ClientButtonIndex, PopupMenu);
2021-08-24 21:08:55 +01:00
2021-12-04 23:24:26 +00:00
DisconnectButton = MenuApi.TitleScreen_MakeSimpleButton("DISCONNECT", _DisconnectIndex);
2021-08-24 21:08:55 +01:00
DisconnectButton.onClick.AddListener(Disconnect);
2021-12-04 23:24:26 +00:00
ResumeGameButton = GameObject.Find("MainMenuLayoutGroup/Button-ResumeGame");
NewGameButton = GameObject.Find("MainMenuLayoutGroup/Button-NewGame");
if (QSBCore.IsInMultiplayer)
{
ClientButton.SetActive(false);
2021-12-04 23:24:26 +00:00
SetButtonActive(DisconnectButton, true);
if (QSBCore.IsHost)
{
SetButtonActive(ResumeGameButton, true);
SetButtonActive(NewGameButton, true);
}
else
{
SetButtonActive(ResumeGameButton, false);
SetButtonActive(NewGameButton, false);
}
}
else
{
2021-12-04 23:24:26 +00:00
SetButtonActive(DisconnectButton, false);
SetButtonActive(ResumeGameButton, true);
SetButtonActive(NewGameButton, true);
}
OnConnected();
2021-11-25 22:44:15 +00:00
if (QSBCore.SkipTitleScreen)
{
Application.runInBackground = true;
var titleScreenManager = FindObjectOfType<TitleScreenManager>();
var titleScreenAnimation = titleScreenManager._cameraController;
const float small = 1 / 1000f;
titleScreenAnimation._gamepadSplash = false;
titleScreenAnimation._introPan = false;
titleScreenAnimation._fadeDuration = small;
titleScreenAnimation.Start();
var titleAnimationController = titleScreenManager._gfxController;
titleAnimationController._logoFadeDelay = small;
titleAnimationController._logoFadeDuration = small;
titleAnimationController._echoesFadeDelay = small;
titleAnimationController._optionsFadeDelay = small;
titleAnimationController._optionsFadeDuration = small;
titleAnimationController._optionsFadeSpacing = small;
}
2021-06-04 11:31:09 +01:00
}
2021-12-04 23:24:26 +00:00
private void Join()
{
DebugLog.DebugWrite($"Join");
}
2021-08-24 21:08:55 +01:00
private void Disconnect()
2021-06-04 11:31:09 +01:00
{
2021-08-24 21:08:55 +01:00
QSBNetworkManager.Instance.StopHost();
2021-12-04 23:24:26 +00:00
SetButtonActive(DisconnectButton.gameObject, false);
SetButtonActive(ClientButton, true);
SetButtonActive(HostButton, true);
2021-06-04 11:31:09 +01:00
}
2021-08-24 21:08:55 +01:00
private void Host()
2021-06-04 11:31:09 +01:00
{
2021-12-04 23:24:26 +00:00
DebugLog.DebugWrite($"Host");
if (QSBNetworkManager.Instance.StartHost() != null)
{
SetButtonActive(DisconnectButton, true);
SetButtonActive(ClientButton, false);
SetButtonActive(HostButton, false);
}
else
{
OpenInfoPopup($"Failed to start server.", "OK");
}
2021-06-04 11:31:09 +01:00
}
2021-08-24 21:08:55 +01:00
private void Connect()
2021-06-04 11:31:09 +01:00
{
QSBNetworkManager.Instance.networkAddress = string.Concat((PopupMenu as PopupInputMenu).GetInputText().Where(c => !char.IsWhiteSpace(c)));
2021-06-04 11:31:09 +01:00
QSBNetworkManager.Instance.StartClient();
2021-08-26 08:51:33 +01:00
DisconnectButton.transform.GetChild(0).GetChild(1).GetComponent<Text>().text = "CONNECTING... (STOP)";
2021-12-04 23:24:26 +00:00
SetButtonActive(DisconnectButton, true);
SetButtonActive(ClientButton, false);
if (QSBSceneManager.CurrentScene == OWScene.TitleScreen)
{
SetButtonActive(ResumeGameButton, false);
SetButtonActive(NewGameButton, false);
}
2021-06-04 11:31:09 +01:00
}
2021-08-26 08:51:33 +01:00
private void OnConnected()
{
2021-10-15 21:08:17 +01:00
var text = QSBCore.IsHost
? "STOP HOSTING"
: "DISCONNECT";
DisconnectButton.transform.GetChild(0).GetChild(1).GetComponent<Text>().text = text;
}
public void OnKicked(KickReason reason)
{
2021-11-20 19:49:50 +00:00
var text = reason switch
{
2021-11-20 19:49:50 +00:00
KickReason.QSBVersionNotMatching => "Server refused connection as QSB version does not match.",
KickReason.GameVersionNotMatching => "Server refused connection as Outer Wilds version does not match.",
KickReason.GamePlatformNotMatching => "Server refused connection as Outer Wilds platform does not match. (Steam/Epic)",
KickReason.None => "Kicked from server. No reason given.",
_ => $"Kicked from server. KickReason:{reason}",
};
OpenInfoPopup(text, "OK");
DisconnectButton.gameObject.SetActive(false);
ClientButton.SetActive(true);
2021-12-05 11:00:18 +00:00
HostButton?.gameObject.SetActive(true);
2021-08-26 08:51:33 +01:00
}
private void OnDisconnected(NetworkError error)
{
if (error == NetworkError.Ok)
{
return;
}
2021-11-20 19:49:50 +00:00
var text = error switch
{
2021-11-20 19:49:50 +00:00
NetworkError.Timeout => "Client disconnected with error!\r\nConnection timed out.",
_ => $"Client disconnected with error!\r\nNetworkError:{error}",
};
OpenInfoPopup(text, "OK");
2021-08-26 08:51:33 +01:00
DisconnectButton.gameObject.SetActive(false);
ClientButton.SetActive(true);
2021-12-05 11:00:18 +00:00
HostButton?.gameObject.SetActive(true);
2021-08-26 08:51:33 +01:00
}
private void OnClientError(NetworkError error)
{
if (error == NetworkError.Ok)
{
// lol wut
return;
}
string text;
switch (error)
{
case NetworkError.DNSFailure:
text = "Internal QNet client error!\r\nDNS Faliure. Address was invalid or could not be resolved.";
DisconnectButton.gameObject.SetActive(false);
ClientButton.SetActive(true);
2021-12-05 11:00:18 +00:00
HostButton?.gameObject.SetActive(true);
break;
default:
text = $"Internal QNet client error!\n\nNetworkError:{error}";
break;
}
OpenInfoPopup(text, "OK");
}
2021-06-04 11:31:09 +01:00
}
}