2022-02-05 18:52:31 -08:00
using EpicTransport ;
using Mirror ;
2022-06-04 16:19:55 +01:00
using QSB.Localisation ;
2022-01-17 21:04:02 +00:00
using QSB.Messaging ;
2021-12-05 14:06:43 +00:00
using QSB.Player.TransformSync ;
2021-12-23 17:07:29 -08:00
using QSB.SaveSync.Messages ;
2021-12-04 23:24:26 +00:00
using QSB.Utility ;
2022-06-04 16:19:55 +01:00
using QSB.WorldSync ;
2022-01-17 21:04:02 +00:00
using System ;
2022-05-11 10:54:29 +01:00
using System.Linq ;
2022-04-05 16:45:26 -07:00
using System.Text ;
2021-08-26 08:51:33 +01:00
using UnityEngine ;
2021-08-24 21:08:55 +01:00
using UnityEngine.UI ;
2021-06-04 11:31:09 +01:00
2022-03-02 19:46:33 -08:00
namespace QSB.Menus ;
internal class MenuManager : MonoBehaviour , IAddComponentOnStart
2021-06-04 11:31:09 +01:00
{
2022-03-02 19:46:33 -08:00
public static MenuManager Instance ;
private PopupMenu OneButtonInfoPopup ;
private PopupMenu TwoButtonInfoPopup ;
private bool _addedPauseLock ;
2021-12-08 10:48:11 +00:00
2022-03-02 19:46:33 -08:00
// Pause menu only
private GameObject QuitButton ;
private GameObject DisconnectButton ;
private PopupMenu DisconnectPopup ;
2021-12-04 23:24:26 +00:00
2022-03-02 19:46:33 -08:00
// title screen only
private GameObject ResumeGameButton ;
private GameObject NewGameButton ;
2022-04-05 17:26:30 -07:00
private Button HostButton ;
2022-03-02 19:46:33 -08:00
private GameObject ConnectButton ;
2022-04-05 13:52:28 -07:00
private PopupInputMenu ConnectPopup ;
2022-05-11 10:54:29 +01:00
private ThreeChoicePopupMenu HostGameTypePopup ;
2022-04-05 16:45:26 -07:00
private Text _loadingText ;
private StringBuilder _nowLoadingSB ;
2022-04-05 17:26:30 -07:00
private const int _titleButtonIndex = 2 ;
2022-04-22 15:27:17 -07:00
private float _connectPopupOpenTime ;
2021-12-04 23:24:26 +00:00
2022-05-30 09:16:44 +01:00
private const string UpdateChangelog = $"QSB Version 0.20.0\r\nThis updates brings better ship syncing (including destruction), more things around the village being synced, and general bug fixes." ;
2022-04-30 15:33:55 +01:00
2022-04-05 21:13:25 -07:00
private Action < bool > PopupClose ;
2022-01-16 22:15:29 +00:00
2022-03-02 19:46:33 -08:00
private bool _intentionalDisconnect ;
2022-01-17 21:04:02 +00:00
2022-05-11 10:54:29 +01:00
private GameObject _threeChoicePopupBase ;
2022-03-02 19:46:33 -08:00
public void Start ( )
{
Instance = this ;
2022-05-11 10:54:29 +01:00
_threeChoicePopupBase = Instantiate ( Resources . FindObjectsOfTypeAll < PopupMenu > ( ) . First ( x = > x . name = = "TwoButton-Popup" & & x . transform . parent . name = = "PopupCanvas" & & x . transform . parent . parent . name = = "TitleMenu" ) . gameObject ) ;
DontDestroyOnLoad ( _threeChoicePopupBase ) ;
_threeChoicePopupBase . SetActive ( false ) ;
2022-03-02 19:46:33 -08:00
MakeTitleMenus ( ) ;
QSBSceneManager . OnSceneLoaded + = OnSceneLoaded ;
QSBNetworkManager . singleton . OnClientConnected + = OnConnected ;
QSBNetworkManager . singleton . OnClientDisconnected + = OnDisconnected ;
2022-04-30 15:33:55 +01:00
if ( QSBCore . Storage . LastUsedVersion ! = QSBCore . QSBVersion )
{
// recently updated!
QSBCore . Storage . LastUsedVersion = QSBCore . QSBVersion ;
QSBCore . Helper . Storage . Save ( QSBCore . Storage , "storage.json" ) ;
QSBCore . MenuApi . RegisterStartupPopup ( UpdateChangelog ) ;
}
2022-06-04 16:19:55 +01:00
QSBLocalisation . LanguageChanged + = OnLanguageChanged ;
2022-06-08 21:16:35 +01:00
2022-06-06 23:35:13 -07: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-02 19:46:33 -08:00
}
2022-01-24 11:08:05 -08:00
2022-03-02 19:46:33 -08:00
private void OnSceneLoaded ( OWScene oldScene , OWScene newScene , bool isUniverse )
{
if ( isUniverse )
2021-12-05 14:06:43 +00:00
{
2022-04-05 19:05:09 -07:00
// wait a frame or else the changes won't actually happen
Delay . RunNextFrame ( InitPauseMenus ) ;
2022-03-02 19:46:33 -08:00
return ;
2022-02-27 04:40:44 -08:00
}
2022-02-24 22:04:54 -08:00
2022-03-02 19:46:33 -08:00
if ( newScene = = OWScene . TitleScreen )
2021-08-26 14:56:42 +01:00
{
2022-04-05 19:05:09 -07:00
// wait a frame or else the changes won't actually happen
Delay . RunNextFrame ( MakeTitleMenus ) ;
2022-02-24 22:04:54 -08:00
}
2022-03-02 19:46:33 -08:00
}
2021-08-26 14:56:42 +01:00
2022-04-05 16:45:26 -07:00
private void ResetStringBuilder ( )
{
if ( _nowLoadingSB = = null )
{
_nowLoadingSB = new StringBuilder ( ) ;
return ;
}
_nowLoadingSB . Length = 0 ;
}
2022-06-04 16:19:55 +01:00
private void OnLanguageChanged ( )
{
if ( QSBSceneManager . CurrentScene ! = OWScene . TitleScreen )
{
DebugLog . ToConsole ( $"Error - Language changed while not in title screen?! Should be impossible!" , OWML . Common . MessageType . Error ) ;
return ;
}
HostButton . transform . GetChild ( 0 ) . GetChild ( 1 ) . GetComponent < Text > ( ) . text = QSBLocalisation . Current . MainMenuHost ;
ConnectButton . transform . GetChild ( 0 ) . GetChild ( 1 ) . GetComponent < Text > ( ) . text = QSBLocalisation . Current . MainMenuConnect ;
var text = QSBCore . DebugSettings . UseKcpTransport ? QSBLocalisation . Current . PublicIPAddress : QSBLocalisation . Current . ProductUserID ;
2022-06-04 09:34:57 -07:00
ConnectPopup . SetUpPopup ( text , InputLibrary . menuConfirm , InputLibrary . cancel , new ScreenPrompt ( QSBLocalisation . Current . Connect ) , new ScreenPrompt ( QSBLocalisation . Current . Cancel ) , false ) ;
2022-06-04 16:19:55 +01:00
ConnectPopup . SetInputFieldPlaceholderText ( text ) ;
HostGameTypePopup . SetUpPopup ( QSBLocalisation . Current . HostExistingOrNew ,
InputLibrary . menuConfirm ,
InputLibrary . confirm2 ,
InputLibrary . cancel ,
new ScreenPrompt ( QSBLocalisation . Current . ExistingSave ) ,
new ScreenPrompt ( QSBLocalisation . Current . NewSave ) ,
2022-06-04 09:34:57 -07:00
new ScreenPrompt ( QSBLocalisation . Current . Cancel ) ) ;
2022-06-04 16:19:55 +01:00
}
2022-03-02 19:46:33 -08:00
private void Update ( )
{
2022-04-05 18:51:14 -07:00
if ( ( LoadManager . GetLoadingScene ( ) = = OWScene . SolarSystem | | LoadManager . GetLoadingScene ( ) = = OWScene . EyeOfTheUniverse )
2022-04-05 16:45:26 -07:00
& & _loadingText ! = null )
2022-02-24 22:04:54 -08:00
{
2022-03-02 19:46:33 -08:00
var num = LoadManager . GetAsyncLoadProgress ( ) ;
num = num < 0.1f
? Mathf . InverseLerp ( 0f , 0.1f , num ) * 0.9f
2022-04-05 11:20:06 -07:00
: 0.9f + Mathf . InverseLerp ( 0.1f , 1f , num ) * 0.1f ;
2022-04-05 16:45:26 -07:00
ResetStringBuilder ( ) ;
_nowLoadingSB . Append ( UITextLibrary . GetString ( UITextType . LoadingMessage ) ) ;
_nowLoadingSB . Append ( num . ToString ( "P0" ) ) ;
_loadingText . text = _nowLoadingSB . ToString ( ) ;
2022-03-02 19:46:33 -08:00
}
}
2021-08-26 14:56:42 +01:00
2022-05-11 10:54:29 +01:00
public ThreeChoicePopupMenu CreateThreeChoicePopup ( string message , string confirm1Text , string confirm2Text , string cancelText )
{
var newPopup = Instantiate ( _threeChoicePopupBase ) ;
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 ;
newPopup . GetComponentsInChildren < LocalizedText > ( ) . ToList ( ) . ForEach ( x = > Destroy ( x ) ) ;
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 ) ,
new ScreenPrompt ( cancelText ) ,
true ,
true ) ;
return popup ;
}
2022-04-05 16:45:26 -07:00
public void LoadGame ( bool inEye )
2022-03-02 19:46:33 -08:00
{
2022-04-05 16:49:45 -07:00
var sceneToLoad = inEye ? OWScene . EyeOfTheUniverse : OWScene . SolarSystem ;
LoadManager . LoadSceneAsync ( sceneToLoad , true , LoadManager . FadeType . ToBlack , 1f , false ) ;
Locator . GetMenuInputModule ( ) . DisableInputs ( ) ;
2022-03-02 19:46:33 -08:00
}
2021-08-26 14:56:42 +01:00
2022-03-02 19:46:33 -08: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-02 19:46:33 -08:00
OWTime . Pause ( OWTime . PauseType . Menu ) ;
OWInput . ChangeInputMode ( InputMode . Menu ) ;
2022-01-17 21:04:02 +00:00
2022-03-02 19:46:33 -08:00
var pauseCommandListener = Locator . GetPauseCommandListener ( ) ;
if ( pauseCommandListener ! = null )
2022-02-27 04:40:44 -08:00
{
2022-03-02 19:46:33 -08:00
pauseCommandListener . AddPauseCommandLock ( ) ;
_addedPauseLock = true ;
}
2022-01-17 21:04:02 +00:00
2022-03-02 19:46:33 -08:00
OneButtonInfoPopup . EnableMenu ( true ) ;
}
2022-01-17 21:04:02 +00:00
2022-03-02 19:46:33 -08:00
private void OpenInfoPopup ( string message , string okButtonText , string cancelButtonText )
{
TwoButtonInfoPopup . SetUpPopup ( message , InputLibrary . menuConfirm , InputLibrary . cancel , new ScreenPrompt ( okButtonText ) , new ScreenPrompt ( cancelButtonText ) ) ;
2021-08-26 14:56:42 +01:00
2022-03-02 19:46:33 -08:00
OWTime . Pause ( OWTime . PauseType . Menu ) ;
OWInput . ChangeInputMode ( InputMode . Menu ) ;
2021-11-25 15:38:05 +00:00
2022-03-02 19:46:33 -08:00
var pauseCommandListener = Locator . GetPauseCommandListener ( ) ;
if ( pauseCommandListener ! = null )
2022-02-24 22:04:54 -08:00
{
2022-03-02 19:46:33 -08:00
pauseCommandListener . AddPauseCommandLock ( ) ;
_addedPauseLock = true ;
}
2021-08-26 14:56:42 +01:00
2022-03-02 19:46:33 -08:00
TwoButtonInfoPopup . EnableMenu ( true ) ;
}
2021-08-26 08:51:33 +01:00
2022-04-05 21:13:25 -07:00
private void OnCloseInfoPopup ( bool confirm )
2022-03-02 19:46:33 -08:00
{
var pauseCommandListener = Locator . GetPauseCommandListener ( ) ;
if ( pauseCommandListener ! = null & & _addedPauseLock )
{
pauseCommandListener . RemovePauseCommandLock ( ) ;
_addedPauseLock = false ;
2022-02-27 04:40:44 -08:00
}
2022-01-17 21:04:02 +00:00
2022-03-02 19:46:33 -08:00
OWTime . Unpause ( OWTime . PauseType . Menu ) ;
OWInput . RestorePreviousInputs ( ) ;
2021-08-26 14:56:42 +01:00
2022-04-05 21:13:25 -07:00
PopupClose ? . SafeInvoke ( confirm ) ;
2022-04-05 17:46:24 -07:00
PopupClose = null ;
2022-03-02 19:46:33 -08:00
}
2021-12-04 23:24:26 +00:00
2022-03-02 19:46:33 -08:00
private void CreateCommonPopups ( )
{
2022-06-04 16:19:55 +01:00
var text = QSBCore . DebugSettings . UseKcpTransport ? QSBLocalisation . Current . PublicIPAddress : QSBLocalisation . Current . ProductUserID ;
ConnectPopup = QSBCore . MenuApi . MakeInputFieldPopup ( text , text , QSBLocalisation . Current . Connect , QSBLocalisation . Current . Cancel ) ;
2022-04-22 15:27:17 -07:00
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 ( ) ;
} ;
ConnectPopup . OnActivateMenu + = ( ) = > _connectPopupOpenTime = Time . time ;
2021-12-05 11:00:18 +00:00
2022-04-05 11:20:06 -07:00
OneButtonInfoPopup = QSBCore . MenuApi . MakeInfoPopup ( "" , "" ) ;
2022-04-05 21:13:25 -07:00
OneButtonInfoPopup . OnPopupConfirm + = ( ) = > OnCloseInfoPopup ( true ) ;
2021-12-04 23:24:26 +00:00
2022-04-05 11:20:06 -07:00
TwoButtonInfoPopup = QSBCore . MenuApi . MakeTwoChoicePopup ( "" , "" , "" ) ;
2022-04-05 21:13:25 -07:00
TwoButtonInfoPopup . OnPopupConfirm + = ( ) = > OnCloseInfoPopup ( true ) ;
TwoButtonInfoPopup . OnPopupCancel + = ( ) = > OnCloseInfoPopup ( false ) ;
2022-05-11 10:54:29 +01:00
2022-06-04 16:19:55 +01:00
HostGameTypePopup = CreateThreeChoicePopup ( QSBLocalisation . Current . HostExistingOrNew , QSBLocalisation . Current . ExistingSave , QSBLocalisation . Current . NewSave , QSBLocalisation . Current . Cancel ) ;
2022-05-11 10:54:29 +01:00
HostGameTypePopup . OnPopupConfirm1 + = ( ) = > Host ( false ) ;
HostGameTypePopup . OnPopupConfirm2 + = ( ) = > Host ( true ) ;
2022-03-02 19:46:33 -08:00
}
2021-08-26 08:51:33 +01:00
2022-04-05 11:20:06 -07:00
private static void SetButtonActive ( Button button , bool active )
= > SetButtonActive ( button ? button . gameObject : null , active ) ;
2021-08-26 08:51:33 +01:00
2022-04-05 11:20:06 -07:00
private static void SetButtonActive ( GameObject button , bool active )
2022-03-02 19:46:33 -08:00
{
if ( button = = null )
2022-02-27 04:40:44 -08:00
{
2022-03-02 19:46:33 -08:00
DebugLog . DebugWrite ( $"Warning - Tried to set button to {active}, but it was null." , OWML . Common . MessageType . Warning ) ;
return ;
}
2021-12-08 10:48:11 +00:00
2022-03-02 19:46:33 -08:00
button . SetActive ( active ) ;
button . GetComponent < CanvasGroup > ( ) . alpha = active ? 1 : 0 ;
}
2021-12-08 10:48:11 +00:00
2022-03-02 19:46:33 -08:00
private void InitPauseMenus ( )
{
CreateCommonPopups ( ) ;
2021-10-15 21:08:17 +01:00
2022-06-04 16:19:55 +01:00
DisconnectPopup = QSBCore . MenuApi . MakeTwoChoicePopup ( QSBLocalisation . Current . DisconnectAreYouSure , QSBLocalisation . Current . Yes , QSBLocalisation . Current . No ) ;
2022-03-02 19:46:33 -08:00
DisconnectPopup . OnPopupConfirm + = Disconnect ;
2021-12-08 10:48:11 +00:00
2022-06-04 16:19:55 +01:00
DisconnectButton = QSBCore . MenuApi . PauseMenu_MakeMenuOpenButton ( QSBLocalisation . Current . PauseMenuDisconnect , DisconnectPopup ) ;
2021-12-30 15:53:10 -08:00
2022-03-02 19:46:33 -08:00
QuitButton = FindObjectOfType < PauseMenuManager > ( ) . _exitToMainMenuAction . gameObject ;
2022-02-24 22:04:54 -08:00
2022-03-02 19:46:33 -08:00
if ( QSBCore . IsInMultiplayer )
{
SetButtonActive ( DisconnectButton , true ) ;
SetButtonActive ( QuitButton , false ) ;
2022-02-27 04:40:44 -08:00
}
2022-03-02 19:46:33 -08:00
else
2021-06-04 11:31:09 +01:00
{
2022-03-02 19:46:33 -08:00
SetButtonActive ( DisconnectButton , false ) ;
SetButtonActive ( QuitButton , true ) ;
2022-02-24 22:04:54 -08:00
}
2021-08-26 08:51:33 +01:00
2022-03-02 19:46:33 -08:00
var text = QSBCore . IsHost
2022-06-04 16:19:55 +01:00
? QSBLocalisation . Current . PauseMenuStopHosting
: QSBLocalisation . Current . PauseMenuDisconnect ;
2022-03-02 19:46:33 -08:00
DisconnectButton . transform . GetChild ( 0 ) . GetChild ( 1 ) . GetComponent < Text > ( ) . text = text ;
var popupText = QSBCore . IsHost
? "Are you sure you want to stop hosting?\r\nThis will disconnect all clients and send everyone back to the main menu."
: "Are you sure you want to disconnect?\r\nThis will send you back to the main menu." ;
DisconnectPopup . _labelText . text = popupText ;
2022-06-04 16:19:55 +01:00
var langController = QSBWorldSync . GetUnityObject < PauseMenuManager > ( ) . transform . GetChild ( 0 ) . GetComponent < FontAndLanguageController > ( ) ;
langController . AddTextElement ( DisconnectButton . transform . GetChild ( 0 ) . GetChild ( 1 ) . GetComponent < Text > ( ) , true , true , false ) ;
langController . AddTextElement ( DisconnectPopup . _labelText , false , true , false ) ;
langController . AddTextElement ( DisconnectPopup . _confirmButton . _buttonText , false , true , false ) ;
langController . AddTextElement ( DisconnectPopup . _cancelButton . _buttonText , false , true , false ) ;
2022-03-02 19:46:33 -08:00
}
private void MakeTitleMenus ( )
{
CreateCommonPopups ( ) ;
2021-12-04 23:24:26 +00:00
2022-06-04 16:19:55 +01:00
HostButton = QSBCore . MenuApi . TitleScreen_MakeSimpleButton ( QSBLocalisation . Current . MainMenuHost , _titleButtonIndex ) ;
2022-05-11 10:54:29 +01:00
HostButton . onClick . AddListener ( PreHost ) ;
2022-04-05 17:26:30 -07:00
2022-06-04 16:19:55 +01:00
ConnectButton = QSBCore . MenuApi . TitleScreen_MakeMenuOpenButton ( QSBLocalisation . Current . MainMenuConnect , _titleButtonIndex + 1 , ConnectPopup ) ;
2022-02-24 22:04:54 -08:00
2022-03-02 19:46:33 -08:00
ResumeGameButton = GameObject . Find ( "MainMenuLayoutGroup/Button-ResumeGame" ) ;
NewGameButton = GameObject . Find ( "MainMenuLayoutGroup/Button-NewGame" ) ;
2022-04-05 16:49:45 -07:00
SetButtonActive ( ConnectButton , true ) ;
Delay . RunWhen ( PlayerData . IsLoaded , ( ) = > SetButtonActive ( ResumeGameButton , PlayerData . LoadLoopCount ( ) > 1 ) ) ;
SetButtonActive ( NewGameButton , true ) ;
2021-06-04 11:31:09 +01:00
2022-03-02 19:46:33 -08:00
if ( QSBCore . DebugSettings . 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 ;
}
2022-06-04 16:19:55 +01:00
var mainMenuFontController = GameObject . Find ( "MainMenu" ) . GetComponent < FontAndLanguageController > ( ) ;
mainMenuFontController . AddTextElement ( HostButton . transform . GetChild ( 0 ) . GetChild ( 1 ) . GetComponent < Text > ( ) , true , true , false ) ;
mainMenuFontController . AddTextElement ( ConnectButton . transform . GetChild ( 0 ) . GetChild ( 1 ) . GetComponent < Text > ( ) , true , true , false ) ;
mainMenuFontController . AddTextElement ( OneButtonInfoPopup . _labelText , false , true , false ) ;
mainMenuFontController . AddTextElement ( OneButtonInfoPopup . _confirmButton . _buttonText , false , true , false ) ;
mainMenuFontController . AddTextElement ( TwoButtonInfoPopup . _labelText , false , true , false ) ;
mainMenuFontController . AddTextElement ( TwoButtonInfoPopup . _confirmButton . _buttonText , false , true , false ) ;
mainMenuFontController . AddTextElement ( TwoButtonInfoPopup . _cancelButton . _buttonText , false , true , false ) ;
mainMenuFontController . AddTextElement ( ConnectPopup . _labelText , false , true , false ) ;
mainMenuFontController . AddTextElement ( ConnectPopup . _confirmButton . _buttonText , false , true , false ) ;
mainMenuFontController . AddTextElement ( ConnectPopup . _cancelButton . _buttonText , false , true , false ) ;
mainMenuFontController . AddTextElement ( HostGameTypePopup . _labelText , false , true , false ) ;
mainMenuFontController . AddTextElement ( HostGameTypePopup . _confirmButton1 . _buttonText , false , true , false ) ;
mainMenuFontController . AddTextElement ( HostGameTypePopup . _confirmButton2 . _buttonText , false , true , false ) ;
mainMenuFontController . AddTextElement ( HostGameTypePopup . _cancelButton . _buttonText , false , true , false ) ;
2022-03-02 19:46:33 -08:00
}
2022-02-05 23:01:02 -08:00
2022-03-02 19:46:33 -08:00
private void Disconnect ( )
{
_intentionalDisconnect = true ;
2021-12-08 10:48:11 +00:00
2022-03-02 19:46:33 -08:00
QSBNetworkManager . singleton . StopHost ( ) ;
2022-04-05 16:49:45 -07:00
SetButtonActive ( DisconnectButton , false ) ;
2022-01-24 15:52:05 -08:00
2022-03-02 19:46:33 -08:00
Locator . GetSceneMenuManager ( ) . pauseMenu . _pauseMenu . EnableMenu ( false ) ;
Locator . GetSceneMenuManager ( ) . pauseMenu . _isPaused = false ;
OWInput . RestorePreviousInputs ( ) ;
2022-01-21 22:38:41 +00:00
2022-03-02 19:46:33 -08:00
LoadManager . LoadScene ( OWScene . TitleScreen , LoadManager . FadeType . ToBlack , 2f ) ;
}
2022-01-17 21:04:02 +00:00
2022-05-11 10:54:29 +01:00
private void PreHost ( )
2022-03-02 19:46:33 -08:00
{
2022-05-11 10:54:29 +01:00
var doesSaveExist = StandaloneProfileManager . SharedInstance . currentProfileGameSave . loopCount > 1 ;
if ( ! doesSaveExist )
{
Host ( true ) ;
return ;
}
HostGameTypePopup . EnableMenu ( true ) ;
}
private void Host ( bool newSave )
{
if ( newSave )
{
PlayerData . ResetGame ( ) ;
}
2022-03-02 19:46:33 -08:00
_intentionalDisconnect = false ;
2022-01-17 21:04:02 +00:00
2022-04-05 17:26:30 -07:00
SetButtonActive ( ConnectButton , false ) ;
SetButtonActive ( ResumeGameButton , false ) ;
SetButtonActive ( NewGameButton , false ) ;
_loadingText = HostButton . transform . GetChild ( 0 ) . GetChild ( 1 ) . GetComponent < Text > ( ) ;
2022-01-14 19:26:58 +00:00
2022-04-05 17:46:24 -07:00
if ( ! QSBCore . DebugSettings . UseKcpTransport )
2022-02-24 22:04:54 -08:00
{
2022-03-02 19:46:33 -08:00
var productUserId = EOSSDKComponent . LocalUserProductIdString ;
2021-08-26 08:51:33 +01:00
2022-04-05 21:13:25 -07:00
PopupClose + = confirm = >
2022-04-05 17:46:24 -07:00
{
2022-04-05 21:13:25 -07:00
if ( confirm )
{
GUIUtility . systemCopyBuffer = productUserId ;
}
2022-04-05 18:51:14 -07:00
2022-04-05 17:46:24 -07:00
LoadGame ( PlayerData . GetWarpedToTheEye ( ) ) ;
2022-04-05 18:51:14 -07:00
Delay . RunWhen ( ( ) = > TimeLoop . _initialized , QSBNetworkManager . singleton . StartHost ) ;
2022-04-05 17:46:24 -07:00
} ;
2021-12-05 14:06:43 +00:00
2022-06-08 21:30:56 +01:00
OpenInfoPopup ( string . Format ( QSBLocalisation . Current . CopyProductUserIDToClipboard , productUserId )
2022-06-04 16:19:55 +01:00
, QSBLocalisation . Current . Yes
, QSBLocalisation . Current . No ) ;
2022-03-02 19:46:33 -08:00
}
2022-04-05 18:51:14 -07:00
else
{
LoadGame ( PlayerData . GetWarpedToTheEye ( ) ) ;
Delay . RunWhen ( ( ) = > TimeLoop . _initialized , QSBNetworkManager . singleton . StartHost ) ;
}
2022-03-02 19:46:33 -08:00
}
2021-08-27 13:02:05 +01:00
2022-03-02 19:46:33 -08:00
private void Connect ( )
{
_intentionalDisconnect = false ;
2022-02-24 22:04:54 -08:00
2022-04-05 13:52:28 -07:00
var address = ConnectPopup . GetInputText ( ) ;
2022-03-02 19:46:33 -08:00
if ( address = = string . Empty )
{
address = QSBCore . DefaultServerIP ;
2022-02-24 22:04:54 -08:00
}
2022-02-19 04:16:57 -08:00
2022-04-05 17:26:30 -07:00
SetButtonActive ( HostButton , false ) ;
2022-04-05 16:49:45 -07:00
SetButtonActive ( ResumeGameButton , false ) ;
SetButtonActive ( NewGameButton , false ) ;
2022-04-05 17:26:30 -07:00
_loadingText = ConnectButton . transform . GetChild ( 0 ) . GetChild ( 1 ) . GetComponent < Text > ( ) ;
2022-06-04 16:19:55 +01:00
_loadingText . text = QSBLocalisation . Current . Connecting ;
2022-04-05 16:49:45 -07:00
Locator . GetMenuInputModule ( ) . DisableInputs ( ) ;
2022-02-24 22:04:54 -08:00
2022-03-02 19:46:33 -08:00
QSBNetworkManager . singleton . networkAddress = address ;
// hack to get disconnect call if start client fails immediately
2022-04-05 11:20:06 -07:00
typeof ( NetworkClient ) . GetProperty ( nameof ( NetworkClient . connection ) ) ! . SetValue ( null , new NetworkConnectionToServer ( ) ) ;
2022-03-02 19:46:33 -08:00
QSBNetworkManager . singleton . StartClient ( ) ;
}
2022-04-05 11:20:06 -07:00
private static void OnConnected ( )
2022-03-02 19:46:33 -08:00
{
2022-04-05 16:49:45 -07:00
if ( ! QSBCore . IsHost )
2022-02-24 22:04:54 -08:00
{
2022-04-05 16:49:45 -07:00
Delay . RunWhen ( ( ) = > PlayerTransformSync . LocalInstance ,
( ) = > new RequestGameStateMessage ( ) . Send ( ) ) ;
2022-03-02 19:46:33 -08:00
}
}
2022-03-18 02:58:01 -07:00
public void OnKicked ( string reason )
2022-03-02 19:46:33 -08:00
{
_intentionalDisconnect = true ;
2022-04-05 21:13:25 -07:00
PopupClose + = _ = >
2022-03-02 19:46:33 -08:00
{
if ( QSBSceneManager . IsInUniverse )
2022-02-27 04:40:44 -08:00
{
2022-03-02 19:46:33 -08:00
LoadManager . LoadScene ( OWScene . TitleScreen , LoadManager . FadeType . ToBlack , 2f ) ;
}
} ;
2022-02-27 04:40:44 -08:00
2022-06-08 21:15:02 +01:00
OpenInfoPopup ( string . Format ( QSBLocalisation . Current . ServerRefusedConnection , reason ) , QSBLocalisation . Current . OK ) ;
2022-03-02 19:46:33 -08:00
}
private void OnDisconnected ( string error )
{
if ( _intentionalDisconnect )
{
DebugLog . DebugWrite ( "intentional disconnect. dont show popup" ) ;
_intentionalDisconnect = false ;
2021-08-26 08:51:33 +01:00
}
2022-03-18 02:36:29 -07:00
else
2021-08-26 08:51:33 +01:00
{
2022-04-05 21:13:25 -07:00
PopupClose + = _ = >
2021-08-26 08:51:33 +01:00
{
2022-03-18 02:36:29 -07:00
if ( QSBSceneManager . IsInUniverse )
{
LoadManager . LoadScene ( OWScene . TitleScreen , LoadManager . FadeType . ToBlack , 2f ) ;
}
} ;
2021-08-26 08:51:33 +01:00
2022-06-04 16:19:55 +01:00
OpenInfoPopup ( string . Format ( QSBLocalisation . Current . ClientDisconnectWithError , error ) , QSBLocalisation . Current . OK ) ;
2022-03-18 02:36:29 -07:00
}
2021-08-26 08:51:33 +01:00
2022-03-02 19:46:33 -08:00
SetButtonActive ( DisconnectButton , false ) ;
SetButtonActive ( ConnectButton , true ) ;
SetButtonActive ( QuitButton , true ) ;
SetButtonActive ( HostButton , true ) ;
2022-04-05 16:49:45 -07:00
SetButtonActive ( ResumeGameButton , PlayerData . LoadLoopCount ( ) > 1 ) ;
2022-03-02 19:46:33 -08:00
SetButtonActive ( NewGameButton , true ) ;
2022-04-06 11:43:09 -07:00
if ( ConnectButton )
{
2022-06-04 16:19:55 +01:00
ConnectButton . transform . GetChild ( 0 ) . GetChild ( 1 ) . GetComponent < Text > ( ) . text = QSBLocalisation . Current . MainMenuConnect ;
2022-04-06 11:43:09 -07:00
}
if ( HostButton )
{
2022-06-04 16:19:55 +01:00
HostButton . transform . GetChild ( 0 ) . GetChild ( 1 ) . GetComponent < Text > ( ) . text = QSBLocalisation . Current . MainMenuHost ;
2022-04-06 11:43:09 -07:00
}
2022-04-05 18:51:14 -07:00
_loadingText = null ;
2022-04-05 14:36:32 -07:00
Locator . GetMenuInputModule ( ) . EnableInputs ( ) ;
2021-06-04 11:31:09 +01:00
}
2022-04-05 11:20:06 -07:00
}