quantum-space-buddies/QSB/Menus/MenuManager.cs

778 lines
27 KiB
C#
Raw Normal View History

2023-06-09 21:06:35 +00:00
using Mirror;
2023-05-08 00:26:40 +00:00
using OWML.Common;
using OWML.Utils;
using QSB.Localization;
2022-01-17 21:04:02 +00:00
using QSB.Messaging;
2021-12-05 14:06:43 +00:00
using QSB.Player.TransformSync;
2022-07-22 12:39:00 +00:00
using QSB.SaveSync;
2021-12-24 01:07:29 +00:00
using QSB.SaveSync.Messages;
2021-12-04 23:24:26 +00:00
using QSB.Utility;
2022-06-04 15:19:55 +00:00
using QSB.WorldSync;
2023-06-09 22:57:11 +00:00
using Steamworks;
2022-01-17 21:04:02 +00:00
using System;
using System.Linq;
2022-04-05 23:45:26 +00:00
using System.Text;
2021-08-26 07:51:33 +00:00
using UnityEngine;
2021-08-24 20:08:55 +00:00
using UnityEngine.UI;
2021-06-04 10:31:09 +00:00
2022-03-03 03:46:33 +00:00
namespace QSB.Menus;
2023-07-28 18:30:57 +00:00
public class MenuManager : MonoBehaviour, IAddComponentOnStart
2021-06-04 10:31:09 +00:00
{
2022-03-03 03:46:33 +00:00
public static MenuManager Instance;
private PopupMenu OneButtonInfoPopup;
private PopupMenu TwoButtonInfoPopup;
private bool _addedPauseLock;
2021-12-08 10:48:11 +00:00
2022-03-03 03:46:33 +00:00
// Pause menu only
private GameObject QuitButton;
private GameObject DisconnectButton;
private PopupMenu DisconnectPopup;
2021-12-04 23:24:26 +00:00
2022-03-03 03:46:33 +00:00
// title screen only
private GameObject ResumeGameButton;
private GameObject NewGameButton;
2022-04-06 00:26:30 +00:00
private Button HostButton;
2022-03-03 03:46:33 +00:00
private GameObject ConnectButton;
private PopupInputMenu ConnectPopup;
2022-08-02 13:36:03 +00:00
private FourChoicePopupMenu ExistingNewCopyPopup;
private ThreeChoicePopupMenu NewCopyPopup;
private ThreeChoicePopupMenu ExistingNewPopup;
2022-04-05 23:45:26 +00:00
private Text _loadingText;
private StringBuilder _nowLoadingSB;
2022-04-06 00:26:30 +00:00
private const int _titleButtonIndex = 2;
private float _connectPopupOpenTime;
2021-12-04 23:24:26 +00:00
2022-04-06 04:13:25 +00:00
private Action<bool> PopupClose;
2022-01-16 22:15:29 +00:00
2022-03-03 03:46:33 +00:00
private bool _intentionalDisconnect;
2022-01-17 21:04:02 +00:00
2022-08-02 19:22:18 +00:00
private GameObject _choicePopupPrefab;
2024-05-05 19:19:10 +00:00
public bool WillBeHost;
2022-03-03 03:46:33 +00:00
public void Start()
{
Instance = this;
if (!_choicePopupPrefab)
{
_choicePopupPrefab = Instantiate(Resources.FindObjectsOfTypeAll<PopupMenu>().First(x => x.name == "TwoButton-Popup" && x.transform.parent?.name == "PopupCanvas" && x.transform.parent?.parent?.name == "TitleMenu").gameObject);
DontDestroyOnLoad(_choicePopupPrefab);
_choicePopupPrefab.SetActive(false);
}
2022-03-03 03:46:33 +00:00
MakeTitleMenus();
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
QSBNetworkManager.singleton.OnClientConnected += OnConnected;
QSBNetworkManager.singleton.OnClientDisconnected += OnDisconnected;
2022-04-30 14:33:55 +00:00
QSBLocalization.LanguageChanged += OnLanguageChanged;
2022-06-08 20:16:35 +00:00
2022-06-07 06:35:13 +00:00
if (QSBCore.DebugSettings.AutoStart)
{
// auto host/connect
Delay.RunWhen(PlayerData.IsLoaded, () =>
{
if (DebugLog.ProcessInstanceId == 0)
{
Host(false);
}
else
{
QSBCore.DefaultServerIP = "localhost";
Connect();
}
});
}
2022-03-03 03:46:33 +00:00
}
2022-03-03 03:46:33 +00:00
private void OnSceneLoaded(OWScene oldScene, OWScene newScene, bool isUniverse)
{
if (isUniverse)
2021-12-05 14:06:43 +00:00
{
2022-04-06 02:05:09 +00:00
// wait a frame or else the changes won't actually happen
Delay.RunNextFrame(InitPauseMenus);
2022-03-03 03:46:33 +00:00
return;
}
2022-03-03 03:46:33 +00:00
if (newScene == OWScene.TitleScreen)
{
2022-04-06 02:05:09 +00:00
// wait a frame or else the changes won't actually happen
Delay.RunNextFrame(MakeTitleMenus);
}
2022-03-03 03:46:33 +00:00
}
2022-04-05 23:45:26 +00:00
private void ResetStringBuilder()
{
if (_nowLoadingSB == null)
{
_nowLoadingSB = new StringBuilder();
return;
}
_nowLoadingSB.Length = 0;
}
2023-05-07 01:49:40 +00:00
public void OnLanguageChanged()
2022-06-04 15:19:55 +00:00
{
if (QSBSceneManager.CurrentScene != OWScene.TitleScreen)
{
2023-05-08 00:26:40 +00:00
DebugLog.ToConsole("Error - Language changed while not in title screen?! Should be impossible!", MessageType.Error);
2022-06-04 15:19:55 +00:00
return;
}
HostButton.transform.GetChild(0).GetChild(1).GetComponent<Text>().text = QSBLocalization.Current.MainMenuHost;
ConnectButton.transform.GetChild(0).GetChild(1).GetComponent<Text>().text = QSBLocalization.Current.MainMenuConnect;
2023-09-18 19:54:52 +00:00
var text = QSBCore.UseKcpTransport ? QSBLocalization.Current.PublicIPAddress : QSBLocalization.Current.SteamID;
ConnectPopup.SetUpPopup(text, InputLibrary.menuConfirm, InputLibrary.cancel, new ScreenPrompt(QSBLocalization.Current.Connect), new ScreenPrompt(QSBLocalization.Current.Cancel), false);
2022-06-04 15:19:55 +00:00
ConnectPopup.SetInputFieldPlaceholderText(text);
2022-08-07 19:02:45 +00:00
ExistingNewCopyPopup.SetUpPopup(QSBLocalization.Current.HostExistingOrNewOrCopy,
2022-06-04 15:19:55 +00:00
InputLibrary.menuConfirm,
InputLibrary.confirm2,
2022-07-22 12:39:00 +00:00
InputLibrary.signalscope,
2022-06-04 15:19:55 +00:00
InputLibrary.cancel,
new ScreenPrompt(QSBLocalization.Current.ExistingSave),
new ScreenPrompt(QSBLocalization.Current.NewSave),
2022-07-22 12:39:00 +00:00
new ScreenPrompt(QSBLocalization.Current.CopySave),
new ScreenPrompt(QSBLocalization.Current.Cancel));
2022-08-02 13:36:03 +00:00
NewCopyPopup.SetUpPopup(QSBLocalization.Current.HostNewOrCopy,
InputLibrary.menuConfirm,
InputLibrary.confirm2,
InputLibrary.cancel,
new ScreenPrompt(QSBLocalization.Current.NewSave),
new ScreenPrompt(QSBLocalization.Current.CopySave),
new ScreenPrompt(QSBLocalization.Current.Cancel));
ExistingNewPopup.SetUpPopup(QSBLocalization.Current.HostExistingOrNew,
InputLibrary.menuConfirm,
InputLibrary.confirm2,
InputLibrary.cancel,
new ScreenPrompt(QSBLocalization.Current.ExistingSave),
new ScreenPrompt(QSBLocalization.Current.NewSave),
new ScreenPrompt(QSBLocalization.Current.Cancel));
2022-06-04 15:19:55 +00:00
}
2022-03-03 03:46:33 +00:00
private void Update()
{
2022-04-06 01:51:14 +00:00
if ((LoadManager.GetLoadingScene() == OWScene.SolarSystem || LoadManager.GetLoadingScene() == OWScene.EyeOfTheUniverse)
2022-04-05 23:45:26 +00:00
&& _loadingText != null)
{
2022-03-03 03:46:33 +00:00
var num = LoadManager.GetAsyncLoadProgress();
num = num < 0.1f
? Mathf.InverseLerp(0f, 0.1f, num) * 0.9f
2022-04-05 18:20:06 +00:00
: 0.9f + Mathf.InverseLerp(0.1f, 1f, num) * 0.1f;
2022-04-05 23:45:26 +00:00
ResetStringBuilder();
_nowLoadingSB.Append(UITextLibrary.GetString(UITextType.LoadingMessage));
_nowLoadingSB.Append(num.ToString("P0"));
_loadingText.text = _nowLoadingSB.ToString();
2022-03-03 03:46:33 +00:00
}
}
public ThreeChoicePopupMenu CreateThreeChoicePopup(string message, string confirm1Text, string confirm2Text, string cancelText)
{
2022-08-02 19:22:18 +00:00
var newPopup = Instantiate(_choicePopupPrefab);
switch (LoadManager.GetCurrentScene())
{
case OWScene.TitleScreen:
newPopup.transform.parent = GameObject.Find("/TitleMenu/PopupCanvas").transform;
break;
case OWScene.SolarSystem:
case OWScene.EyeOfTheUniverse:
newPopup.transform.parent = GameObject.Find("/PauseMenu/PopupCanvas").transform;
break;
}
newPopup.transform.localPosition = Vector3.zero;
newPopup.transform.localScale = Vector3.one;
2022-07-24 03:53:51 +00:00
newPopup.GetComponentsInChildren<LocalizedText>().ForEach(Destroy);
var originalPopup = newPopup.GetComponent<PopupMenu>();
var ok1Button = originalPopup._confirmButton.gameObject;
var ok2Button = Instantiate(ok1Button, ok1Button.transform.parent);
ok2Button.transform.SetSiblingIndex(1);
var popup = newPopup.AddComponent<ThreeChoicePopupMenu>();
popup._labelText = originalPopup._labelText;
popup._cancelAction = originalPopup._cancelAction;
popup._ok1Action = originalPopup._okAction;
popup._ok2Action = ok2Button.GetComponent<SubmitAction>();
popup._cancelButton = originalPopup._cancelButton;
popup._confirmButton1 = originalPopup._confirmButton;
popup._confirmButton2 = ok2Button.GetComponent<ButtonWithHotkeyImageElement>();
popup._rootCanvas = originalPopup._rootCanvas;
popup._menuActivationRoot = originalPopup._menuActivationRoot;
popup._startEnabled = originalPopup._startEnabled;
popup._selectOnActivate = originalPopup._selectOnActivate;
popup._selectableItemsRoot = originalPopup._selectableItemsRoot;
popup._subMenus = originalPopup._subMenus;
popup._menuOptions = originalPopup._menuOptions;
popup.SetUpPopup(
message,
InputLibrary.menuConfirm,
InputLibrary.confirm2,
InputLibrary.cancel,
new ScreenPrompt(confirm1Text),
new ScreenPrompt(confirm2Text),
2022-07-24 03:53:51 +00:00
new ScreenPrompt(cancelText));
return popup;
}
2022-07-22 12:39:00 +00:00
public FourChoicePopupMenu CreateFourChoicePopup(string message, string confirm1Text, string confirm2Text, string confirm3Text, string cancelText)
{
2022-08-02 19:22:18 +00:00
var newPopup = Instantiate(_choicePopupPrefab);
2022-07-22 12:39:00 +00:00
switch (LoadManager.GetCurrentScene())
{
case OWScene.TitleScreen:
newPopup.transform.parent = GameObject.Find("/TitleMenu/PopupCanvas").transform;
break;
case OWScene.SolarSystem:
case OWScene.EyeOfTheUniverse:
newPopup.transform.parent = GameObject.Find("/PauseMenu/PopupCanvas").transform;
break;
}
newPopup.transform.localPosition = Vector3.zero;
newPopup.transform.localScale = Vector3.one;
2022-07-24 03:53:51 +00:00
newPopup.GetComponentsInChildren<LocalizedText>().ForEach(Destroy);
2022-07-22 12:39:00 +00:00
var originalPopup = newPopup.GetComponent<PopupMenu>();
var ok1Button = originalPopup._confirmButton.gameObject;
var ok2Button = Instantiate(ok1Button, ok1Button.transform.parent);
ok2Button.transform.SetSiblingIndex(1);
var ok3Button = Instantiate(ok1Button, ok1Button.transform.parent);
ok3Button.transform.SetSiblingIndex(2);
var popup = newPopup.AddComponent<FourChoicePopupMenu>();
popup._labelText = originalPopup._labelText;
popup._cancelAction = originalPopup._cancelAction;
popup._ok1Action = originalPopup._okAction;
popup._ok2Action = ok2Button.GetComponent<SubmitAction>();
popup._ok3Action = ok3Button.GetComponent<SubmitAction>();
popup._cancelButton = originalPopup._cancelButton;
popup._confirmButton1 = originalPopup._confirmButton;
popup._confirmButton2 = ok2Button.GetComponent<ButtonWithHotkeyImageElement>();
popup._confirmButton3 = ok3Button.GetComponent<ButtonWithHotkeyImageElement>();
popup._rootCanvas = originalPopup._rootCanvas;
popup._menuActivationRoot = originalPopup._menuActivationRoot;
popup._startEnabled = originalPopup._startEnabled;
popup._selectOnActivate = originalPopup._selectOnActivate;
popup._selectableItemsRoot = originalPopup._selectableItemsRoot;
popup._subMenus = originalPopup._subMenus;
popup._menuOptions = originalPopup._menuOptions;
popup.SetUpPopup(
message,
InputLibrary.menuConfirm,
InputLibrary.confirm2,
InputLibrary.signalscope,
InputLibrary.cancel,
new ScreenPrompt(confirm1Text),
new ScreenPrompt(confirm2Text),
new ScreenPrompt(confirm3Text),
2022-07-24 03:53:51 +00:00
new ScreenPrompt(cancelText));
2022-07-22 12:39:00 +00:00
return popup;
}
2022-04-05 23:45:26 +00:00
public void LoadGame(bool inEye)
2022-03-03 03:46:33 +00:00
{
2022-04-05 23:49:45 +00:00
var sceneToLoad = inEye ? OWScene.EyeOfTheUniverse : OWScene.SolarSystem;
LoadManager.LoadSceneAsync(sceneToLoad, true, LoadManager.FadeType.ToBlack, 1f, false);
Locator.GetMenuInputModule().DisableInputs();
2022-03-03 03:46:33 +00:00
}
2022-03-03 03:46:33 +00:00
private void OpenInfoPopup(string message, string okButtonText)
{
OneButtonInfoPopup.SetUpPopup(message, InputLibrary.menuConfirm, InputLibrary.cancel, new ScreenPrompt(okButtonText), null, true, false);
2022-01-17 21:04:02 +00:00
2022-03-03 03:46:33 +00:00
OWTime.Pause(OWTime.PauseType.Menu);
OWInput.ChangeInputMode(InputMode.Menu);
2022-01-17 21:04:02 +00:00
2022-03-03 03:46:33 +00:00
var pauseCommandListener = Locator.GetPauseCommandListener();
if (pauseCommandListener != null)
{
2022-03-03 03:46:33 +00:00
pauseCommandListener.AddPauseCommandLock();
_addedPauseLock = true;
}
2022-01-17 21:04:02 +00:00
2022-03-03 03:46:33 +00:00
OneButtonInfoPopup.EnableMenu(true);
}
2022-01-17 21:04:02 +00:00
2022-03-03 03:46:33 +00:00
private void OpenInfoPopup(string message, string okButtonText, string cancelButtonText)
{
TwoButtonInfoPopup.SetUpPopup(message, InputLibrary.menuConfirm, InputLibrary.cancel, new ScreenPrompt(okButtonText), new ScreenPrompt(cancelButtonText));
2022-03-03 03:46:33 +00:00
OWTime.Pause(OWTime.PauseType.Menu);
OWInput.ChangeInputMode(InputMode.Menu);
2021-11-25 15:38:05 +00:00
2022-03-03 03:46:33 +00:00
var pauseCommandListener = Locator.GetPauseCommandListener();
if (pauseCommandListener != null)
{
2022-03-03 03:46:33 +00:00
pauseCommandListener.AddPauseCommandLock();
_addedPauseLock = true;
}
2022-03-03 03:46:33 +00:00
TwoButtonInfoPopup.EnableMenu(true);
}
2021-08-26 07:51:33 +00:00
2022-04-06 04:13:25 +00:00
private void OnCloseInfoPopup(bool confirm)
2022-03-03 03:46:33 +00:00
{
var pauseCommandListener = Locator.GetPauseCommandListener();
if (pauseCommandListener != null && _addedPauseLock)
{
pauseCommandListener.RemovePauseCommandLock();
_addedPauseLock = false;
}
2022-01-17 21:04:02 +00:00
2022-03-03 03:46:33 +00:00
OWTime.Unpause(OWTime.PauseType.Menu);
OWInput.RestorePreviousInputs();
2022-04-06 04:13:25 +00:00
PopupClose?.SafeInvoke(confirm);
2022-04-06 00:46:24 +00:00
PopupClose = null;
2022-03-03 03:46:33 +00:00
}
2021-12-04 23:24:26 +00:00
2022-03-03 03:46:33 +00:00
private void CreateCommonPopups()
{
2023-09-18 19:54:52 +00:00
var text = QSBCore.UseKcpTransport ? QSBLocalization.Current.PublicIPAddress : QSBLocalization.Current.SteamID;
ConnectPopup = QSBCore.MenuApi.MakeInputFieldPopup(text, text, QSBLocalization.Current.Connect, QSBLocalization.Current.Cancel);
ConnectPopup.CloseMenuOnOk(false);
ConnectPopup.OnPopupConfirm += () =>
{
// fixes dumb thing with using keyboard to open popup
if (OWMath.ApproxEquals(Time.time, _connectPopupOpenTime))
{
return;
}
ConnectPopup.EnableMenu(false);
Connect();
};
2022-08-25 12:08:54 +00:00
ConnectPopup.OnActivateMenu += () =>
{
_connectPopupOpenTime = Time.time;
if (QSBCore.Helper.Interaction.ModExists("Raicuparta.NomaiVR"))
{
// ClearInputTextField is called AFTER OnActivateMenu
Delay.RunNextFrame(() => ConnectPopup._inputField.SetTextWithoutNotify(GUIUtility.systemCopyBuffer));
}
2022-08-25 12:08:54 +00:00
};
2021-12-05 11:00:18 +00:00
2022-04-05 18:20:06 +00:00
OneButtonInfoPopup = QSBCore.MenuApi.MakeInfoPopup("", "");
2022-04-06 04:13:25 +00:00
OneButtonInfoPopup.OnPopupConfirm += () => OnCloseInfoPopup(true);
2021-12-04 23:24:26 +00:00
2022-04-05 18:20:06 +00:00
TwoButtonInfoPopup = QSBCore.MenuApi.MakeTwoChoicePopup("", "", "");
2022-04-06 04:13:25 +00:00
TwoButtonInfoPopup.OnPopupConfirm += () => OnCloseInfoPopup(true);
TwoButtonInfoPopup.OnPopupCancel += () => OnCloseInfoPopup(false);
2022-08-07 19:02:45 +00:00
ExistingNewCopyPopup = CreateFourChoicePopup(QSBLocalization.Current.HostExistingOrNewOrCopy,
2022-08-02 13:36:03 +00:00
QSBLocalization.Current.ExistingSave,
QSBLocalization.Current.NewSave,
QSBLocalization.Current.CopySave,
QSBLocalization.Current.Cancel);
ExistingNewCopyPopup.OnPopupConfirm1 += () => Host(false);
ExistingNewCopyPopup.OnPopupConfirm2 += () => Host(true);
ExistingNewCopyPopup.OnPopupConfirm3 += () =>
2022-07-22 12:39:00 +00:00
{
2022-07-24 03:53:51 +00:00
DebugLog.DebugWrite("Replacing multiplayer save with singleplayer save");
2022-07-22 12:39:00 +00:00
QSBCore.IsInMultiplayer = true;
2022-08-07 19:02:45 +00:00
if (QSBCore.IsStandalone)
{
var currentProfile = QSBStandaloneProfileManager.SharedInstance.currentProfile;
QSBStandaloneProfileManager.SharedInstance.SaveGame(currentProfile.gameSave, null, null, null);
}
else
{
var gameSave = QSBMSStoreProfileManager.SharedInstance.currentProfileGameSave;
QSBMSStoreProfileManager.SharedInstance.SaveGame(gameSave, null, null, null);
}
2022-07-22 12:39:00 +00:00
Host(false);
};
2022-08-02 13:36:03 +00:00
NewCopyPopup = CreateThreeChoicePopup(QSBLocalization.Current.HostNewOrCopy,
QSBLocalization.Current.NewSave,
QSBLocalization.Current.CopySave,
QSBLocalization.Current.Cancel);
NewCopyPopup.OnPopupConfirm1 += () => Host(true);
NewCopyPopup.OnPopupConfirm2 += () =>
{
DebugLog.DebugWrite("Replacing multiplayer save with singleplayer save");
QSBCore.IsInMultiplayer = true;
2022-08-07 19:02:45 +00:00
if (QSBCore.IsStandalone)
{
var currentProfile = QSBStandaloneProfileManager.SharedInstance.currentProfile;
QSBStandaloneProfileManager.SharedInstance.SaveGame(currentProfile.gameSave, null, null, null);
}
else
{
var gameSave = QSBMSStoreProfileManager.SharedInstance.currentProfileGameSave;
QSBMSStoreProfileManager.SharedInstance.SaveGame(gameSave, null, null, null);
}
2022-08-02 13:36:03 +00:00
Host(false);
};
ExistingNewPopup = CreateThreeChoicePopup(QSBLocalization.Current.HostExistingOrNew,
QSBLocalization.Current.ExistingSave,
QSBLocalization.Current.NewSave,
QSBLocalization.Current.Cancel);
ExistingNewPopup.OnPopupConfirm1 += () => Host(false);
ExistingNewPopup.OnPopupConfirm2 += () => Host(true);
2022-03-03 03:46:33 +00:00
}
2021-08-26 07:51:33 +00:00
2022-04-05 18:20:06 +00:00
private static void SetButtonActive(Button button, bool active)
=> SetButtonActive(button ? button.gameObject : null, active);
2021-08-26 07:51:33 +00:00
2022-04-05 18:20:06 +00:00
private static void SetButtonActive(GameObject button, bool active)
2022-03-03 03:46:33 +00:00
{
if (button == null)
{
2023-05-08 00:26:40 +00:00
DebugLog.DebugWrite($"Warning - Tried to set button to {active}, but it was null.", MessageType.Warning);
2022-03-03 03:46:33 +00:00
return;
}
2021-12-08 10:48:11 +00:00
2023-05-07 02:54:08 +00:00
var activeAlpha = 1;
2023-05-07 02:54:08 +00:00
if (QSBSceneManager.CurrentScene == OWScene.TitleScreen)
{
var titleAnimationController = QSBWorldSync.GetUnityObject<TitleScreenManager>()._gfxController;
activeAlpha = titleAnimationController.IsTitleAnimationComplete() ? 1 : 0;
}
2022-03-03 03:46:33 +00:00
button.SetActive(active);
button.GetComponent<CanvasGroup>().alpha = active ? activeAlpha : 0;
2022-03-03 03:46:33 +00:00
}
2021-12-08 10:48:11 +00:00
2022-03-03 03:46:33 +00:00
private void InitPauseMenus()
{
CreateCommonPopups();
2021-10-15 20:08:17 +00:00
DisconnectPopup = QSBCore.MenuApi.MakeTwoChoicePopup(QSBLocalization.Current.DisconnectAreYouSure, QSBLocalization.Current.Yes, QSBLocalization.Current.No);
2022-03-03 03:46:33 +00:00
DisconnectPopup.OnPopupConfirm += Disconnect;
2021-12-08 10:48:11 +00:00
DisconnectButton = QSBCore.MenuApi.PauseMenu_MakeMenuOpenButton(QSBLocalization.Current.PauseMenuDisconnect, DisconnectPopup);
2022-03-03 03:46:33 +00:00
QuitButton = FindObjectOfType<PauseMenuManager>()._exitToMainMenuAction.gameObject;
2022-03-03 03:46:33 +00:00
if (QSBCore.IsInMultiplayer)
{
SetButtonActive(DisconnectButton, true);
SetButtonActive(QuitButton, false);
}
2022-03-03 03:46:33 +00:00
else
2021-06-04 10:31:09 +00:00
{
2022-03-03 03:46:33 +00:00
SetButtonActive(DisconnectButton, false);
SetButtonActive(QuitButton, true);
}
2021-08-26 07:51:33 +00:00
2022-03-03 03:46:33 +00:00
var text = QSBCore.IsHost
? QSBLocalization.Current.PauseMenuStopHosting
: QSBLocalization.Current.PauseMenuDisconnect;
2022-03-03 03:46:33 +00:00
DisconnectButton.transform.GetChild(0).GetChild(1).GetComponent<Text>().text = text;
var popupText = QSBCore.IsHost
? QSBLocalization.Current.StopHostingAreYouSure
: QSBLocalization.Current.DisconnectAreYouSure;
2022-03-03 03:46:33 +00:00
DisconnectPopup._labelText.text = popupText;
2022-06-04 15:19:55 +00:00
var langController = QSBWorldSync.GetUnityObject<PauseMenuManager>().transform.GetChild(0).GetComponent<FontAndLanguageController>();
2022-07-24 03:53:51 +00:00
langController.AddTextElement(DisconnectButton.transform.GetChild(0).GetChild(1).GetComponent<Text>());
langController.AddTextElement(DisconnectPopup._labelText, false);
langController.AddTextElement(DisconnectPopup._confirmButton._buttonText, false);
langController.AddTextElement(DisconnectPopup._cancelButton._buttonText, false);
langController.AddTextElement(OneButtonInfoPopup._labelText, false);
langController.AddTextElement(OneButtonInfoPopup._confirmButton._buttonText, false);
langController.AddTextElement(TwoButtonInfoPopup._labelText, false);
langController.AddTextElement(TwoButtonInfoPopup._confirmButton._buttonText, false);
langController.AddTextElement(TwoButtonInfoPopup._cancelButton._buttonText, false);
2022-03-03 03:46:33 +00:00
}
private void MakeTitleMenus()
{
CreateCommonPopups();
2021-12-04 23:24:26 +00:00
HostButton = QSBCore.MenuApi.TitleScreen_MakeSimpleButton(QSBLocalization.Current.MainMenuHost, _titleButtonIndex);
HostButton.onClick.AddListener(PreHost);
2022-04-06 00:26:30 +00:00
ConnectButton = QSBCore.MenuApi.TitleScreen_MakeMenuOpenButton(QSBLocalization.Current.MainMenuConnect, _titleButtonIndex + 1, ConnectPopup);
2022-03-03 03:46:33 +00:00
ResumeGameButton = GameObject.Find("MainMenuLayoutGroup/Button-ResumeGame");
NewGameButton = GameObject.Find("MainMenuLayoutGroup/Button-NewGame");
2022-04-05 23:49:45 +00:00
SetButtonActive(ConnectButton, true);
Delay.RunWhen(PlayerData.IsLoaded, () => SetButtonActive(ResumeGameButton, PlayerData.LoadLoopCount() > 1));
SetButtonActive(NewGameButton, true);
2021-06-04 10:31:09 +00:00
2022-06-04 15:19:55 +00:00
var mainMenuFontController = GameObject.Find("MainMenu").GetComponent<FontAndLanguageController>();
2022-07-24 03:53:51 +00:00
mainMenuFontController.AddTextElement(HostButton.transform.GetChild(0).GetChild(1).GetComponent<Text>());
mainMenuFontController.AddTextElement(ConnectButton.transform.GetChild(0).GetChild(1).GetComponent<Text>());
2022-06-04 15:19:55 +00:00
2022-07-24 03:53:51 +00:00
mainMenuFontController.AddTextElement(OneButtonInfoPopup._labelText, false);
mainMenuFontController.AddTextElement(OneButtonInfoPopup._confirmButton._buttonText, false);
2022-06-04 15:19:55 +00:00
2022-07-24 03:53:51 +00:00
mainMenuFontController.AddTextElement(TwoButtonInfoPopup._labelText, false);
mainMenuFontController.AddTextElement(TwoButtonInfoPopup._confirmButton._buttonText, false);
mainMenuFontController.AddTextElement(TwoButtonInfoPopup._cancelButton._buttonText, false);
2022-06-04 15:19:55 +00:00
2022-07-24 03:53:51 +00:00
mainMenuFontController.AddTextElement(ConnectPopup._labelText, false);
mainMenuFontController.AddTextElement(ConnectPopup._confirmButton._buttonText, false);
mainMenuFontController.AddTextElement(ConnectPopup._cancelButton._buttonText, false);
2022-06-04 15:19:55 +00:00
2022-08-02 13:36:03 +00:00
mainMenuFontController.AddTextElement(ExistingNewCopyPopup._labelText, false);
mainMenuFontController.AddTextElement(ExistingNewCopyPopup._confirmButton1._buttonText, false);
mainMenuFontController.AddTextElement(ExistingNewCopyPopup._confirmButton2._buttonText, false);
mainMenuFontController.AddTextElement(ExistingNewCopyPopup._confirmButton3._buttonText, false);
mainMenuFontController.AddTextElement(ExistingNewCopyPopup._cancelButton._buttonText, false);
mainMenuFontController.AddTextElement(NewCopyPopup._labelText, false);
mainMenuFontController.AddTextElement(NewCopyPopup._confirmButton1._buttonText, false);
mainMenuFontController.AddTextElement(NewCopyPopup._confirmButton2._buttonText, false);
mainMenuFontController.AddTextElement(NewCopyPopup._cancelButton._buttonText, false);
mainMenuFontController.AddTextElement(ExistingNewPopup._labelText, false);
mainMenuFontController.AddTextElement(ExistingNewPopup._confirmButton1._buttonText, false);
mainMenuFontController.AddTextElement(ExistingNewPopup._confirmButton2._buttonText, false);
mainMenuFontController.AddTextElement(ExistingNewPopup._cancelButton._buttonText, false);
2022-03-03 03:46:33 +00:00
}
2022-02-06 07:01:02 +00:00
2022-03-03 03:46:33 +00:00
private void Disconnect()
{
_intentionalDisconnect = true;
2021-12-08 10:48:11 +00:00
2022-03-03 03:46:33 +00:00
QSBNetworkManager.singleton.StopHost();
2022-04-05 23:49:45 +00:00
SetButtonActive(DisconnectButton, false);
2022-01-24 23:52:05 +00:00
2022-03-03 03:46:33 +00:00
Locator.GetSceneMenuManager().pauseMenu._pauseMenu.EnableMenu(false);
Locator.GetSceneMenuManager().pauseMenu._isPaused = false;
OWInput.RestorePreviousInputs();
2022-01-21 22:38:41 +00:00
2022-03-03 03:46:33 +00:00
LoadManager.LoadScene(OWScene.TitleScreen, LoadManager.FadeType.ToBlack, 2f);
}
2022-01-17 21:04:02 +00:00
private void PreHost()
2022-03-03 03:46:33 +00:00
{
2022-08-23 04:01:09 +00:00
var doesSingleplayerSaveExist = false;
var doesMultiplayerSaveExist = false;
2022-08-07 19:02:45 +00:00
if (!QSBCore.IsStandalone)
{
var manager = QSBMSStoreProfileManager.SharedInstance;
doesSingleplayerSaveExist = manager.currentProfileGameSave.loopCount > 1;
doesMultiplayerSaveExist = manager.currentProfileMultiplayerGameSave.loopCount > 1;
}
else
{
var profile = QSBStandaloneProfileManager.SharedInstance.currentProfile;
doesSingleplayerSaveExist = profile.gameSave.loopCount > 1;
doesMultiplayerSaveExist = profile.multiplayerGameSave.loopCount > 1;
}
2022-08-02 13:36:03 +00:00
if (doesSingleplayerSaveExist && doesMultiplayerSaveExist)
{
2022-08-02 19:22:34 +00:00
DebugLog.DebugWrite("CASE 1 - Both singleplayer and multiplayer saves exist.");
2022-08-02 13:36:03 +00:00
// ask if we want to use the existing multiplayer save,
// start a new multiplayer save, or copy the singleplayer save
ExistingNewCopyPopup.EnableMenu(true);
}
else if (doesSingleplayerSaveExist && !doesMultiplayerSaveExist)
{
2022-08-02 19:22:34 +00:00
DebugLog.DebugWrite("CASE 2 - Only a singleplayer save exists.");
2022-08-02 13:36:03 +00:00
// ask if we want to start a new multiplayer save or copy the singleplayer save
NewCopyPopup.EnableMenu(true);
}
else if (!doesSingleplayerSaveExist && doesMultiplayerSaveExist)
{
2022-08-02 19:22:34 +00:00
DebugLog.DebugWrite("CASE 3 - Only multiplayer save exists.");
2022-08-02 13:36:03 +00:00
// ask if we want to use the existing multiplayer save or start a new one
ExistingNewPopup.EnableMenu(true);
}
else if (!doesSingleplayerSaveExist && !doesMultiplayerSaveExist)
{
2022-08-02 19:22:34 +00:00
DebugLog.DebugWrite("CASE 4 - Neither a singleplayer or a multiplayer save exists.");
2022-08-02 13:36:03 +00:00
// create a new multiplayer save - nothing to copy or load
Host(true);
}
}
2022-07-22 12:39:00 +00:00
private void Host(bool newMultiplayerSave)
{
2022-07-22 12:39:00 +00:00
QSBCore.IsInMultiplayer = true;
2024-05-05 19:19:10 +00:00
WillBeHost = true;
2022-07-22 12:39:00 +00:00
if (newMultiplayerSave)
{
2022-07-24 03:53:51 +00:00
DebugLog.DebugWrite("Resetting game...");
PlayerData.ResetGame();
}
2022-07-22 12:39:00 +00:00
else
{
2022-07-24 03:53:51 +00:00
DebugLog.DebugWrite("Loading multiplayer game...");
2022-08-07 19:02:45 +00:00
if (QSBCore.IsStandalone)
{
var profile = QSBStandaloneProfileManager.SharedInstance.currentProfile;
PlayerData.Init(profile.multiplayerGameSave, profile.settingsSave, profile.graphicsSettings, profile.inputJSON);
}
else
{
var manager = QSBMSStoreProfileManager.SharedInstance;
PlayerData.Init(manager.currentProfileMultiplayerGameSave, manager.currentProfileGameSettings, manager.currentProfileGraphicsSettings, manager.currentProfileInputJSON);
}
2022-07-22 12:39:00 +00:00
}
2022-03-03 03:46:33 +00:00
_intentionalDisconnect = false;
2022-01-17 21:04:02 +00:00
2022-04-06 00:26:30 +00:00
SetButtonActive(ConnectButton, false);
SetButtonActive(ResumeGameButton, false);
SetButtonActive(NewGameButton, false);
_loadingText = HostButton.transform.GetChild(0).GetChild(1).GetComponent<Text>();
2023-05-07 01:44:20 +00:00
if (!QSBCore.UseKcpTransport)
{
2023-06-10 18:45:44 +00:00
var steamId = SteamUser.GetSteamID().ToString();
2021-08-26 07:51:33 +00:00
2022-04-06 04:13:25 +00:00
PopupClose += confirm =>
2022-04-06 00:46:24 +00:00
{
2022-04-06 04:13:25 +00:00
if (confirm)
{
2023-06-10 18:45:44 +00:00
GUIUtility.systemCopyBuffer = steamId;
2022-04-06 04:13:25 +00:00
}
2022-04-06 01:51:14 +00:00
2022-04-06 00:46:24 +00:00
LoadGame(PlayerData.GetWarpedToTheEye());
2023-08-02 05:09:35 +00:00
// wait until scene load and then wait until Start has ran
// why is this done? GameStateMessage etc works on title screen since nonhost has to deal with that
2024-05-05 19:19:10 +00:00
Delay.RunWhen(() => TimeLoop._initialized, () =>
{
QSBNetworkManager.singleton.StartHost();
Delay.RunWhen(() => NetworkServer.active, () => WillBeHost = false);
});
2022-04-06 00:46:24 +00:00
};
2021-12-05 14:06:43 +00:00
2023-06-10 18:45:44 +00:00
OpenInfoPopup(string.Format(QSBLocalization.Current.CopySteamIDToClipboard, steamId)
, QSBLocalization.Current.Yes
2023-06-09 22:57:11 +00:00
, QSBLocalization.Current.No);
2022-03-03 03:46:33 +00:00
}
2022-04-06 01:51:14 +00:00
else
{
LoadGame(PlayerData.GetWarpedToTheEye());
2023-08-02 05:09:35 +00:00
// wait until scene load and then wait until Start has ran
// why is this done? GameStateMessage etc works on title screen since nonhost has to deal with that
2024-05-05 19:19:10 +00:00
Delay.RunWhen(() => TimeLoop._initialized, () =>
{
QSBNetworkManager.singleton.StartHost();
Delay.RunWhen(() => NetworkServer.active, () => WillBeHost = false);
});
2022-04-06 01:51:14 +00:00
}
2022-03-03 03:46:33 +00:00
}
2022-03-03 03:46:33 +00:00
private void Connect()
{
2022-07-22 12:39:00 +00:00
QSBCore.IsInMultiplayer = true;
2022-03-03 03:46:33 +00:00
_intentionalDisconnect = false;
2022-08-07 19:02:45 +00:00
if (QSBCore.IsStandalone)
{
var profile = QSBStandaloneProfileManager.SharedInstance.currentProfile;
PlayerData.Init(profile.multiplayerGameSave, profile.settingsSave, profile.graphicsSettings, profile.inputJSON);
}
else
{
var manager = QSBMSStoreProfileManager.SharedInstance;
PlayerData.Init(manager.currentProfileMultiplayerGameSave, manager.currentProfileGameSettings, manager.currentProfileGraphicsSettings, manager.currentProfileInputJSON);
}
2022-07-22 12:39:00 +00:00
2022-08-23 04:01:09 +00:00
var address = ConnectPopup.GetInputText().Trim();
2022-03-03 03:46:33 +00:00
if (address == string.Empty)
{
address = QSBCore.DefaultServerIP;
}
2022-04-06 00:26:30 +00:00
SetButtonActive(HostButton, false);
2022-04-05 23:49:45 +00:00
SetButtonActive(ResumeGameButton, false);
SetButtonActive(NewGameButton, false);
2022-04-06 00:26:30 +00:00
_loadingText = ConnectButton.transform.GetChild(0).GetChild(1).GetComponent<Text>();
_loadingText.text = QSBLocalization.Current.Connecting;
2022-04-05 23:49:45 +00:00
Locator.GetMenuInputModule().DisableInputs();
2022-03-03 03:46:33 +00:00
QSBNetworkManager.singleton.networkAddress = address;
QSBNetworkManager.singleton.StartClient();
}
2022-04-05 18:20:06 +00:00
private static void OnConnected()
2022-03-03 03:46:33 +00:00
{
2022-04-05 23:49:45 +00:00
if (!QSBCore.IsHost)
{
2022-04-05 23:49:45 +00:00
Delay.RunWhen(() => PlayerTransformSync.LocalInstance,
() => new RequestGameStateMessage().Send());
2022-03-03 03:46:33 +00:00
}
}
2022-03-18 09:58:01 +00:00
public void OnKicked(string reason)
2022-03-03 03:46:33 +00:00
{
2022-07-22 12:39:00 +00:00
QSBCore.IsInMultiplayer = false;
2022-03-03 03:46:33 +00:00
_intentionalDisconnect = true;
2022-04-06 04:13:25 +00:00
PopupClose += _ =>
2022-03-03 03:46:33 +00:00
{
if (QSBSceneManager.IsInUniverse)
{
2022-03-03 03:46:33 +00:00
LoadManager.LoadScene(OWScene.TitleScreen, LoadManager.FadeType.ToBlack, 2f);
}
};
OpenInfoPopup(string.Format(QSBLocalization.Current.ServerRefusedConnection, reason), QSBLocalization.Current.OK);
2022-03-03 03:46:33 +00:00
}
private void OnDisconnected(TransportError error, string reason)
2022-03-03 03:46:33 +00:00
{
2022-07-22 12:39:00 +00:00
QSBCore.IsInMultiplayer = false;
2022-03-03 03:46:33 +00:00
if (_intentionalDisconnect)
{
DebugLog.DebugWrite("intentional disconnect. dont show popup");
_intentionalDisconnect = false;
2021-08-26 07:51:33 +00:00
}
else
2021-08-26 07:51:33 +00:00
{
2022-04-06 04:13:25 +00:00
PopupClose += _ =>
2021-08-26 07:51:33 +00:00
{
if (QSBSceneManager.IsInUniverse)
{
LoadManager.LoadScene(OWScene.TitleScreen, LoadManager.FadeType.ToBlack, 2f);
}
};
2021-08-26 07:51:33 +00:00
OpenInfoPopup(string.Format(QSBLocalization.Current.ClientDisconnectWithError, reason), QSBLocalization.Current.OK);
}
2021-08-26 07:51:33 +00:00
2022-03-03 03:46:33 +00:00
SetButtonActive(DisconnectButton, false);
SetButtonActive(ConnectButton, true);
SetButtonActive(QuitButton, true);
SetButtonActive(HostButton, true);
2022-04-05 23:49:45 +00:00
SetButtonActive(ResumeGameButton, PlayerData.LoadLoopCount() > 1);
2022-03-03 03:46:33 +00:00
SetButtonActive(NewGameButton, true);
2022-04-06 18:43:09 +00:00
if (ConnectButton)
{
ConnectButton.transform.GetChild(0).GetChild(1).GetComponent<Text>().text = QSBLocalization.Current.MainMenuConnect;
2022-04-06 18:43:09 +00:00
}
if (HostButton)
{
HostButton.transform.GetChild(0).GetChild(1).GetComponent<Text>().text = QSBLocalization.Current.MainMenuHost;
2022-04-06 18:43:09 +00:00
}
2022-04-06 01:51:14 +00:00
_loadingText = null;
2022-04-05 21:36:32 +00:00
Locator.GetMenuInputModule().EnableInputs();
2021-06-04 10:31:09 +00:00
}
2022-04-05 18:20:06 +00:00
}