aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/Common/UI/UIScene_ConnectingProgress.cpp
blob: 40557cd5c233ae46a28d8c898a098de4c7e5c00d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include "stdafx.h"
#include "UI.h"
#include "UIScene_ConnectingProgress.h"
#include "..\..\Minecraft.h"
#ifdef _WINDOWS64
#include "..\..\Windows64\Network\WinsockNetLayer.h"
#include "..\..\..\Minecraft.World\DisconnectPacket.h"

static int ConnectingProgress_OnRejectedDialogOK(LPVOID, int iPad, const C4JStorage::EMessageResult)
{
	ui.NavigateBack(iPad);
	return 0;
}
#endif

UIScene_ConnectingProgress::UIScene_ConnectingProgress(int iPad, void *_initData, UILayer *parentLayer) : UIScene(iPad, parentLayer)
{
	// Setup all the Iggy references we need for this scene
	initialiseMovie();

	parentLayer->addComponent(iPad,eUIComponent_Panorama);
	parentLayer->addComponent(iPad,eUIComponent_Logo);

	m_progressBar.showBar(false);
	m_progressBar.setVisible( false );
	m_labelTip.setVisible( false );

	ConnectionProgressParams *param = static_cast<ConnectionProgressParams *>(_initData);

	if( param->stringId >= 0 )
	{
		m_labelTitle.init( app.GetString( param->stringId ) );
	}
	else
	{		
		m_labelTitle.init( L"" );
	}
	m_progressBar.init(L"",0,0,100,0);
	m_buttonConfirm.init( app.GetString( IDS_CONFIRM_OK ), eControl_Confirm );
	m_buttonConfirm.setVisible(false);

#if 0
	if(app.GetLocalPlayerCount()>1)
	{
		app.AdjustSplitscreenScene(m_hObj,&m_OriginalPosition,m_iPad,false);
	}
#endif

	m_showTooltips = param->showTooltips;
	m_runFailTimer = param->setFailTimer;
	m_timerTime = param->timerTime;
	m_cancelFunc = param->cancelFunc;
	m_cancelFuncParam = param->cancelFuncParam;
	m_removeLocalPlayer = false;
	m_showingButton = false;

#ifdef _WINDOWS64
	WinsockNetLayer::eJoinState initState = WinsockNetLayer::GetJoinState();
	m_asyncJoinActive = (initState != WinsockNetLayer::eJoinState_Idle && initState != WinsockNetLayer::eJoinState_Cancelled);
	m_asyncJoinFailed = false;
#endif
}

UIScene_ConnectingProgress::~UIScene_ConnectingProgress()
{
	m_parentLayer->removeComponent(eUIComponent_Panorama);
	m_parentLayer->removeComponent(eUIComponent_Logo);
}

void UIScene_ConnectingProgress::updateTooltips()
{
#ifdef _WINDOWS64
	if (m_asyncJoinActive)
	{
		ui.SetTooltips( m_iPad, -1, IDS_TOOLTIPS_BACK);
		return;
	}
	if (m_asyncJoinFailed)
	{
		ui.SetTooltips( m_iPad, IDS_TOOLTIPS_SELECT, -1);
		return;
	}
#endif
	// 4J-PB - removing the option of cancel join, since it didn't work anyway
	//ui.SetTooltips( m_iPad, -1, m_showTooltips?IDS_TOOLTIPS_CANCEL_JOIN:-1);
	ui.SetTooltips( m_iPad, -1, -1);
}

void UIScene_ConnectingProgress::tick()
{
	UIScene::tick();

#ifdef _WINDOWS64
	if (m_asyncJoinActive)
	{
		WinsockNetLayer::eJoinState state = WinsockNetLayer::GetJoinState();
		if (state == WinsockNetLayer::eJoinState_Connecting)
		{
			// connecting.............
			int attempt = WinsockNetLayer::GetJoinAttempt();
			int maxAttempts = WinsockNetLayer::GetJoinMaxAttempts();
			char buf[128];
			if (attempt <= 1)
				sprintf_s(buf, "Connecting...");
			else
				sprintf_s(buf, "Connecting failed, trying again (%d/%d)", attempt, maxAttempts);
			wchar_t wbuf[128];
			mbstowcs(wbuf, buf, 128);
			m_labelTitle.setLabel(wstring(wbuf));
		}
		else if (state == WinsockNetLayer::eJoinState_Success)
		{
			m_asyncJoinActive = false;
			// go go go
		}
		else if (state == WinsockNetLayer::eJoinState_Cancelled)
		{
			// cancel
			m_asyncJoinActive = false;
			navigateBack();
		}
		else if (state == WinsockNetLayer::eJoinState_Rejected)
		{
			// server full and banned are passed differently compared to other disconnects it seems
			m_asyncJoinActive = false;
			DisconnectPacket::eDisconnectReason reason = WinsockNetLayer::GetJoinRejectReason();
			int exitReasonStringId;
			switch (reason)
			{
			case DisconnectPacket::eDisconnect_ServerFull:
				exitReasonStringId = IDS_DISCONNECTED_SERVER_FULL;
				break;
			case DisconnectPacket::eDisconnect_Banned:
				exitReasonStringId = IDS_DISCONNECTED_KICKED;
				break;
			default:
				exitReasonStringId = IDS_CONNECTION_LOST_SERVER;
				break;
			}
			UINT uiIDA[1];
			uiIDA[0] = IDS_CONFIRM_OK;
			ui.RequestErrorMessage(IDS_CONNECTION_FAILED, exitReasonStringId, uiIDA, 1, ProfileManager.GetPrimaryPad(), ConnectingProgress_OnRejectedDialogOK, nullptr, nullptr);
		}
		else if (state == WinsockNetLayer::eJoinState_Failed)
		{
			// FAIL
			m_asyncJoinActive = false;
			m_asyncJoinFailed = true;

			int maxAttempts = WinsockNetLayer::GetJoinMaxAttempts();
			char buf[256];
			sprintf_s(buf, "Failed to connect after %d attempts. The server may be unavailable.", maxAttempts);
			wchar_t wbuf[256];
			mbstowcs(wbuf, buf, 256);

			// TIL that these exist
			// not going to use a actual popup due to it requiring messing with strings which can really mess things up
			// i dont trust myself with that
			// these need to be touched up later as teh button is a bit offset
			m_labelTitle.setLabel(L"Unable to connect to server");
			m_progressBar.setLabel(wstring(wbuf));
			m_progressBar.showBar(false);
			m_progressBar.setVisible(true);
			m_buttonConfirm.setVisible(true);
			m_showingButton = true;
			m_controlTimer.setVisible(false);
		}
		return;
	}
#endif

	if( m_removeLocalPlayer )
	{
		m_removeLocalPlayer = false;
		
		Minecraft *pMinecraft = Minecraft::GetInstance();
		pMinecraft->removeLocalPlayerIdx(m_iPad);
#ifdef _XBOX_ONE
		ProfileManager.RemoveGamepadFromGame(m_iPad);
#endif
	}
}

wstring UIScene_ConnectingProgress::getMoviePath()
{
	if(app.GetLocalPlayerCount() > 1 && !m_parentLayer->IsFullscreenGroup())
	{
		return L"FullscreenProgressSplit";
	}
	else
	{
		return L"FullscreenProgress";
	}
}

void UIScene_ConnectingProgress::handleGainFocus(bool navBack)
{
	UIScene::handleGainFocus(navBack);
	if(!navBack && m_runFailTimer) addTimer(0,m_timerTime);
}

void UIScene_ConnectingProgress::handleLoseFocus()
{
	if (!m_runFailTimer) return;

	int millisecsLeft = getTimer(0)->targetTime - System::currentTimeMillis();
	int millisecsTaken = getTimer(0)->duration - millisecsLeft;
	app.DebugPrintf("\n");
	app.DebugPrintf("---------------------------------------------------------\n");
	app.DebugPrintf("---------------------------------------------------------\n");
	app.DebugPrintf("UIScene_ConnectingProgress time taken = %d millisecs\n", millisecsTaken);
	app.DebugPrintf("---------------------------------------------------------\n");
	app.DebugPrintf("---------------------------------------------------------\n");
	app.DebugPrintf("\n");


	killTimer(0);
}

void UIScene_ConnectingProgress::handleTimerComplete(int id)
{
	// Check if the connection failed
	Minecraft *pMinecraft = Minecraft::GetInstance();

	if( pMinecraft->m_connectionFailed[m_iPad] || !g_NetworkManager.IsInSession() )
	{

#if 0
		app.RemoveBackScene(m_iPad);
#endif

		int exitReasonStringId;
		switch(pMinecraft->m_connectionFailedReason[m_iPad])
		{
		case DisconnectPacket::eDisconnect_LoginTooLong:
			exitReasonStringId = IDS_DISCONNECTED_LOGIN_TOO_LONG;
			break;
		case DisconnectPacket::eDisconnect_ServerFull:
			exitReasonStringId = IDS_DISCONNECTED_SERVER_FULL;
			break;
		case DisconnectPacket::eDisconnect_Kicked:
			exitReasonStringId = IDS_DISCONNECTED_KICKED;
			break;
		case DisconnectPacket::eDisconnect_NoUGC_AllLocal:
			exitReasonStringId = IDS_NO_USER_CREATED_CONTENT_PRIVILEGE_ALL_LOCAL;
			break;
		case DisconnectPacket::eDisconnect_NoUGC_Single_Local:
			exitReasonStringId = IDS_NO_USER_CREATED_CONTENT_PRIVILEGE_SINGLE_LOCAL;
			break;
#if defined(__PS3__) || defined(__ORBIS__)
		case DisconnectPacket::eDisconnect_ContentRestricted_AllLocal:
			exitReasonStringId = IDS_CONTENT_RESTRICTION_MULTIPLAYER;
			break;
		case DisconnectPacket::eDisconnect_ContentRestricted_Single_Local:
			exitReasonStringId = IDS_CONTENT_RESTRICTION;
			break;
#endif
#ifdef _XBOX
		case DisconnectPacket::eDisconnect_NoUGC_Remote:
			exitReasonStringId = IDS_NO_USER_CREATED_CONTENT_PRIVILEGE_REMOTE;
			break;
#endif
		case DisconnectPacket::eDisconnect_NoFlying:
			exitReasonStringId = IDS_DISCONNECTED_FLYING;
			break;
		case DisconnectPacket::eDisconnect_Quitting:
			exitReasonStringId = IDS_DISCONNECTED_SERVER_QUIT;
			break;
		case DisconnectPacket::eDisconnect_OutdatedServer:
			exitReasonStringId = IDS_DISCONNECTED_SERVER_OLD;
			break;
		case DisconnectPacket::eDisconnect_OutdatedClient:
			exitReasonStringId = IDS_DISCONNECTED_CLIENT_OLD;
			break;
#if defined __ORBIS__ || defined __PS3__ || defined __PSVITA__
		case DisconnectPacket::eDisconnect_NATMismatch:
			exitReasonStringId = IDS_DISCONNECTED_NAT_TYPE_MISMATCH;
			break;
#endif
		default:
			exitReasonStringId = IDS_CONNECTION_LOST_SERVER;
			break;
		}

		if( m_iPad != ProfileManager.GetPrimaryPad() && g_NetworkManager.IsInSession() )
		{
			m_buttonConfirm.setVisible(true);
			m_showingButton = true;

			// Set text
			m_labelTitle.setLabel( app.GetString( IDS_CONNECTION_FAILED ) );
			m_progressBar.setLabel( app.GetString( exitReasonStringId ) );
			m_progressBar.setVisible( true );
			m_controlTimer.setVisible( false );
		}
		else
		{
			UINT uiIDA[1];
			uiIDA[0]=IDS_CONFIRM_OK;
			ui.RequestErrorMessage( IDS_CONNECTION_FAILED, exitReasonStringId, uiIDA,1,ProfileManager.GetPrimaryPad());
			exitReasonStringId = -1;

			//app.NavigateToHomeMenu();
			app.SetAction(ProfileManager.GetPrimaryPad(),eAppAction_ExitWorld,(void *)TRUE);
		}
	}
}

void UIScene_ConnectingProgress::handleInput(int iPad, int key, bool repeat, bool pressed, bool released, bool &handled)
{
	//app.DebugPrintf("UIScene_DebugOverlay handling input for pad %d, key %d, down- %s, pressed- %s, released- %s\n", iPad, key, down?"TRUE":"FALSE", pressed?"TRUE":"FALSE", released?"TRUE":"FALSE");
	
	if( m_showTooltips )
	{
		ui.AnimateKeyPress(m_iPad, key, repeat, pressed, released);

		switch(key)
		{
// 4J-PB - Removed the option to cancel join - it didn't work anyway
#ifdef _WINDOWS64
		case ACTION_MENU_CANCEL:
			if (pressed && m_asyncJoinActive)
			{
				m_asyncJoinActive = false;
				WinsockNetLayer::CancelJoinGame();
				navigateBack();
				handled = true;
			}
			break;
#endif
// 		case ACTION_MENU_CANCEL:
// 			{
// 				if(m_cancelFunc != nullptr)
// 				{
// 					m_cancelFunc(m_cancelFuncParam);
// 				}
// 				else
// 				{
// 					// Cancel the join
// 					Minecraft *pMinecraft = Minecraft::GetInstance();
// 					pMinecraft->removeLocalPlayerIdx(m_iPad);
// 				}
// 				handled = true;
// 			}
// 			break;
		case ACTION_MENU_OK:
#ifdef __ORBIS__
		case ACTION_MENU_TOUCHPAD_PRESS:
#endif
			if(pressed)
			{
				sendInputToMovie(key, repeat, pressed, released);
			}
			break;
		case ACTION_MENU_UP:
		case ACTION_MENU_DOWN:
			if(pressed)
			{
				sendInputToMovie(key, repeat, pressed, released);
			}
			break;
		}
	}
}

void UIScene_ConnectingProgress::handlePress(F64 controlId, F64 childId)
{
	switch(static_cast<int>(controlId))
	{
	case eControl_Confirm:
		if(m_showingButton)
		{
#ifdef _WINDOWS64
			if (m_asyncJoinFailed)
			{
				navigateBack();
			}
			else
#endif
			if( m_iPad != ProfileManager.GetPrimaryPad() && g_NetworkManager.IsInSession() )
			{
				// The connection failed if we see the button, so the temp player should be removed and the viewports updated again
				// This is actually done in the tick as we can't pull down the scene we are currently in from here
				m_removeLocalPlayer = true;
			}
			else
			{
				ui.NavigateToHomeMenu();
				//app.NavigateBack( ProfileManager.GetPrimaryPad() );
			}
		}
		break;
	}
}