2022-02-05 18:52:31 -08:00
using EpicTransport ;
using Mirror ;
2022-06-29 15:00:44 -07:00
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 13:39:00 +01:00
using QSB.SaveSync ;
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-08-02 14:36:03 +01:00
private FourChoicePopupMenu ExistingNewCopyPopup ;
private ThreeChoicePopupMenu NewCopyPopup ;
private ThreeChoicePopupMenu ExistingNewPopup ;
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-08-18 12:36:52 +01:00
private const string UpdateChangelog = "QSB Version 0.21.0\r\nMultiplayer saves are now seperate from singleplayer, and items got an overhaul. A lot of bug fixes too." ;
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-08-02 12:22:18 -07:00
private GameObject _choicePopupPrefab ;
2022-05-11 10:54:29 +01:00
2022-03-02 19:46:33 -08:00
public void Start ( )
{
Instance = this ;
2022-05-11 10:54:29 +01:00
2022-08-02 12:24:18 -07:00
if ( ! _choicePopupPrefab )
{
2022-08-12 12:02:19 -07:00
_choicePopupPrefab = Instantiate ( Resources . FindObjectsOfTypeAll < PopupMenu > ( ) . First ( x = > x . name = = "TwoButton-Popup" & & x . transform . parent ? . name = = "PopupCanvas" & & x . transform . parent ? . parent ? . name = = "TitleMenu" ) . gameObject ) ;
2022-08-02 12:24:18 -07:00
DontDestroyOnLoad ( _choicePopupPrefab ) ;
_choicePopupPrefab . SetActive ( false ) ;
}
2022-05-11 10:54:29 +01:00
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
2022-06-08 14:00:37 -07:00
QSBLocalization . 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 )
{
2022-07-23 20:53:51 -07:00
DebugLog . ToConsole ( "Error - Language changed while not in title screen?! Should be impossible!" , OWML . Common . MessageType . Error ) ;
2022-06-04 16:19:55 +01:00
return ;
}
2022-06-08 14:00:37 -07:00
HostButton . transform . GetChild ( 0 ) . GetChild ( 1 ) . GetComponent < Text > ( ) . text = QSBLocalization . Current . MainMenuHost ;
ConnectButton . transform . GetChild ( 0 ) . GetChild ( 1 ) . GetComponent < Text > ( ) . text = QSBLocalization . Current . MainMenuConnect ;
var text = QSBCore . DebugSettings . UseKcpTransport ? QSBLocalization . Current . PublicIPAddress : QSBLocalization . Current . ProductUserID ;
ConnectPopup . SetUpPopup ( text , InputLibrary . menuConfirm , InputLibrary . cancel , new ScreenPrompt ( QSBLocalization . Current . Connect ) , new ScreenPrompt ( QSBLocalization . Current . Cancel ) , false ) ;
2022-06-04 16:19:55 +01:00
ConnectPopup . SetInputFieldPlaceholderText ( text ) ;
2022-08-07 20:02:45 +01:00
ExistingNewCopyPopup . SetUpPopup ( QSBLocalization . Current . HostExistingOrNewOrCopy ,
2022-06-04 16:19:55 +01:00
InputLibrary . menuConfirm ,
InputLibrary . confirm2 ,
2022-07-22 13:39:00 +01:00
InputLibrary . signalscope ,
2022-06-04 16:19:55 +01:00
InputLibrary . cancel ,
2022-06-08 14:00:37 -07:00
new ScreenPrompt ( QSBLocalization . Current . ExistingSave ) ,
new ScreenPrompt ( QSBLocalization . Current . NewSave ) ,
2022-07-22 13:39:00 +01:00
new ScreenPrompt ( QSBLocalization . Current . CopySave ) ,
2022-06-08 14:00:37 -07:00
new ScreenPrompt ( QSBLocalization . Current . Cancel ) ) ;
2022-08-02 14:36:03 +01: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 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 )
{
2022-08-02 12:22:18 -07:00
var newPopup = Instantiate ( _choicePopupPrefab ) ;
2022-05-11 10:54:29 +01: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-23 20:53:51 -07:00
newPopup . GetComponentsInChildren < LocalizedText > ( ) . ForEach ( Destroy ) ;
2022-05-11 10:54:29 +01:00
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-23 20:53:51 -07:00
new ScreenPrompt ( cancelText ) ) ;
2022-05-11 10:54:29 +01:00
return popup ;
}
2022-07-22 13:39:00 +01:00
public FourChoicePopupMenu CreateFourChoicePopup ( string message , string confirm1Text , string confirm2Text , string confirm3Text , string cancelText )
{
2022-08-02 12:22:18 -07:00
var newPopup = Instantiate ( _choicePopupPrefab ) ;
2022-07-22 13:39:00 +01: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-23 20:53:51 -07:00
newPopup . GetComponentsInChildren < LocalizedText > ( ) . ForEach ( Destroy ) ;
2022-07-22 13:39:00 +01: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-23 20:53:51 -07:00
new ScreenPrompt ( cancelText ) ) ;
2022-07-22 13:39:00 +01:00
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-08 14:00:37 -07:00
var text = QSBCore . DebugSettings . UseKcpTransport ? QSBLocalization . Current . PublicIPAddress : QSBLocalization . Current . ProductUserID ;
ConnectPopup = QSBCore . MenuApi . MakeInputFieldPopup ( text , text , QSBLocalization . Current . Connect , QSBLocalization . 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-08-07 20:02:45 +01:00
ExistingNewCopyPopup = CreateFourChoicePopup ( QSBLocalization . Current . HostExistingOrNewOrCopy ,
2022-08-02 14:36:03 +01: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 13:39:00 +01:00
{
2022-07-23 20:53:51 -07:00
DebugLog . DebugWrite ( "Replacing multiplayer save with singleplayer save" ) ;
2022-07-22 13:39:00 +01:00
QSBCore . IsInMultiplayer = true ;
2022-08-07 20:02:45 +01: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 13:39:00 +01:00
Host ( false ) ;
} ;
2022-08-02 14:36:03 +01: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 20:02:45 +01: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 14:36:03 +01: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-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-08 14:00:37 -07:00
DisconnectPopup = QSBCore . MenuApi . MakeTwoChoicePopup ( QSBLocalization . Current . DisconnectAreYouSure , QSBLocalization . Current . Yes , QSBLocalization . Current . No ) ;
2022-03-02 19:46:33 -08:00
DisconnectPopup . OnPopupConfirm + = Disconnect ;
2021-12-08 10:48:11 +00:00
2022-06-08 14:00:37 -07:00
DisconnectButton = QSBCore . MenuApi . PauseMenu_MakeMenuOpenButton ( QSBLocalization . 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-08 14:00:37 -07:00
? QSBLocalization . Current . PauseMenuStopHosting
: QSBLocalization . Current . PauseMenuDisconnect ;
2022-03-02 19:46:33 -08:00
DisconnectButton . transform . GetChild ( 0 ) . GetChild ( 1 ) . GetComponent < Text > ( ) . text = text ;
var popupText = QSBCore . IsHost
2022-06-09 16:42:03 +01:00
? QSBLocalization . Current . StopHostingAreYouSure
: QSBLocalization . Current . DisconnectAreYouSure ;
2022-03-02 19:46:33 -08:00
DisconnectPopup . _labelText . text = popupText ;
2022-06-04 16:19:55 +01:00
var langController = QSBWorldSync . GetUnityObject < PauseMenuManager > ( ) . transform . GetChild ( 0 ) . GetComponent < FontAndLanguageController > ( ) ;
2022-07-23 20:53:51 -07: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 ) ;
2022-03-02 19:46:33 -08:00
}
private void MakeTitleMenus ( )
{
CreateCommonPopups ( ) ;
2021-12-04 23:24:26 +00:00
2022-06-08 14:00:37 -07:00
HostButton = QSBCore . MenuApi . TitleScreen_MakeSimpleButton ( QSBLocalization . 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-08 14:00:37 -07:00
ConnectButton = QSBCore . MenuApi . TitleScreen_MakeMenuOpenButton ( QSBLocalization . 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 > ( ) ;
2022-07-23 20:53:51 -07:00
mainMenuFontController . AddTextElement ( HostButton . transform . GetChild ( 0 ) . GetChild ( 1 ) . GetComponent < Text > ( ) ) ;
mainMenuFontController . AddTextElement ( ConnectButton . transform . GetChild ( 0 ) . GetChild ( 1 ) . GetComponent < Text > ( ) ) ;
2022-06-04 16:19:55 +01:00
2022-07-23 20:53:51 -07:00
mainMenuFontController . AddTextElement ( OneButtonInfoPopup . _labelText , false ) ;
mainMenuFontController . AddTextElement ( OneButtonInfoPopup . _confirmButton . _buttonText , false ) ;
2022-06-04 16:19:55 +01:00
2022-07-23 20:53:51 -07:00
mainMenuFontController . AddTextElement ( TwoButtonInfoPopup . _labelText , false ) ;
mainMenuFontController . AddTextElement ( TwoButtonInfoPopup . _confirmButton . _buttonText , false ) ;
mainMenuFontController . AddTextElement ( TwoButtonInfoPopup . _cancelButton . _buttonText , false ) ;
2022-06-04 16:19:55 +01:00
2022-07-23 20:53:51 -07:00
mainMenuFontController . AddTextElement ( ConnectPopup . _labelText , false ) ;
mainMenuFontController . AddTextElement ( ConnectPopup . _confirmButton . _buttonText , false ) ;
mainMenuFontController . AddTextElement ( ConnectPopup . _cancelButton . _buttonText , false ) ;
2022-06-04 16:19:55 +01:00
2022-08-02 14:36:03 +01: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-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-08-07 20:02:45 +01:00
bool doesSingleplayerSaveExist = false ;
bool doesMultiplayerSaveExist = false ;
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-05-11 10:54:29 +01:00
2022-08-02 14:36:03 +01:00
if ( doesSingleplayerSaveExist & & doesMultiplayerSaveExist )
2022-05-11 10:54:29 +01:00
{
2022-08-02 12:22:34 -07:00
DebugLog . DebugWrite ( "CASE 1 - Both singleplayer and multiplayer saves exist." ) ;
2022-08-02 14:36:03 +01: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 12:22:34 -07:00
DebugLog . DebugWrite ( "CASE 2 - Only a singleplayer save exists." ) ;
2022-08-02 14:36:03 +01: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 12:22:34 -07:00
DebugLog . DebugWrite ( "CASE 3 - Only multiplayer save exists." ) ;
2022-08-02 14:36:03 +01: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 12:22:34 -07:00
DebugLog . DebugWrite ( "CASE 4 - Neither a singleplayer or a multiplayer save exists." ) ;
2022-08-02 14:36:03 +01:00
// create a new multiplayer save - nothing to copy or load
Host ( true ) ;
2022-05-11 10:54:29 +01:00
}
}
2022-07-22 13:39:00 +01:00
private void Host ( bool newMultiplayerSave )
2022-05-11 10:54:29 +01:00
{
2022-07-22 13:39:00 +01:00
QSBCore . IsInMultiplayer = true ;
if ( newMultiplayerSave )
2022-05-11 10:54:29 +01:00
{
2022-07-23 20:53:51 -07:00
DebugLog . DebugWrite ( "Resetting game..." ) ;
2022-05-11 10:54:29 +01:00
PlayerData . ResetGame ( ) ;
}
2022-07-22 13:39:00 +01:00
else
{
2022-07-23 20:53:51 -07:00
DebugLog . DebugWrite ( "Loading multiplayer game..." ) ;
2022-08-07 20:02:45 +01: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 13:39:00 +01:00
}
2022-05-11 10:54:29 +01:00
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 14:00:37 -07:00
OpenInfoPopup ( string . Format ( QSBLocalization . Current . CopyProductUserIDToClipboard , productUserId )
, QSBLocalization . Current . Yes
, QSBLocalization . 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 ( )
{
2022-07-22 13:39:00 +01:00
QSBCore . IsInMultiplayer = true ;
2022-03-02 19:46:33 -08:00
_intentionalDisconnect = false ;
2022-02-24 22:04:54 -08:00
2022-08-07 20:02:45 +01: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 13:39:00 +01: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-08 14:00:37 -07:00
_loadingText . text = QSBLocalization . 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
{
2022-07-22 13:39:00 +01:00
QSBCore . IsInMultiplayer = false ;
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 14:00:37 -07:00
OpenInfoPopup ( string . Format ( QSBLocalization . Current . ServerRefusedConnection , reason ) , QSBLocalization . Current . OK ) ;
2022-03-02 19:46:33 -08:00
}
private void OnDisconnected ( string error )
{
2022-07-22 13:39:00 +01:00
QSBCore . IsInMultiplayer = false ;
2022-03-02 19:46:33 -08:00
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-08 14:00:37 -07:00
OpenInfoPopup ( string . Format ( QSBLocalization . Current . ClientDisconnectWithError , error ) , QSBLocalization . 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-08 14:00:37 -07:00
ConnectButton . transform . GetChild ( 0 ) . GetChild ( 1 ) . GetComponent < Text > ( ) . text = QSBLocalization . Current . MainMenuConnect ;
2022-04-06 11:43:09 -07:00
}
if ( HostButton )
{
2022-06-08 14:00:37 -07:00
HostButton . transform . GetChild ( 0 ) . GetChild ( 1 ) . GetComponent < Text > ( ) . text = QSBLocalization . 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
}