!I 1937152 from //dev_game_hunt/consoles_candidate: !B Remove allocations of unnecess...
[CRYENGINE.git] / Code / CryPlugins / CryLobby / Module / PSNOrbis / CryPSN2Lobby.cpp
blobaff77a5f16ea584bcddb37d75c91b6b728f12330
1 // Copyright 2001-2018 Crytek GmbH / Crytek Group. All rights reserved.
3 #include "StdAfx.h"
5 #if CRY_PLATFORM_ORBIS
7 #include "CryPSN2Lobby.h"
9 #if USE_PSN
10 #include <net.h>
11 #include <libnetctl.h>
13 void CCryPSNLobbyService::SupportCallback(ECryPSNSupportCallbackEvent ecb, SCryPSNSupportCallbackEventData& data, void* pArg)
15 CCryPSNLobbyService* _this = static_cast<CCryPSNLobbyService*>(pArg);
17 if (ecb == eCE_ErrorEvent)
19 for (uint32 i = 0; i < MAX_LOBBY_TASKS; i++)
21 STask* pTask = _this->GetTask(i);
23 if (pTask->used && pTask->running)
25 _this->UpdateTaskError(i, MapSupportErrorToLobbyError(data.m_errorEvent.m_error));
26 _this->StopTaskRunning(i);
32 CCryPSNLobbyService::CCryPSNLobbyService(CCryLobby* pLobby, ECryLobbyService service) : CCryLobbyService(pLobby, service)
34 #if USE_PSN_VOICE
35 m_pVoice = NULL;
36 #endif//USE_PSN_VOICE
37 m_pLobbySupport = NULL;
38 #if USE_CRY_MATCHMAKING
39 m_pMatchmaking = NULL;
40 #endif // USE_CRY_MATCHMAKING
41 m_NPInited = false;
42 #if USE_CRY_TCPSERVICE
43 m_pTCPServiceFactory = NULL;
44 #endif // USE_CRY_TCPSERVICE
45 #if USE_CRY_STATS
46 m_pStats = NULL;
47 #endif // USE_CRY_STATS
48 m_pLobbyUI = NULL;
49 #if USE_CRY_FRIENDS
50 m_pFriends = NULL;
51 #endif // USE_CRY_FRIENDS
52 m_pReward = NULL;
55 ECryLobbyError CCryPSNLobbyService::Initialise(ECryLobbyServiceFeatures features, CryLobbyServiceCallback pCB)
57 ECryLobbyError error = CCryLobbyService::Initialise(features, pCB);
59 if (!m_NPInited)
61 int ret;
63 ret = sceNetCtlInit();
64 if ((ret < PSN_OK) && (ret != SCE_NET_CTL_ERROR_NOT_INITIALIZED))
66 NetLog("sceNetCtlInit() failed. ret = 0x%x\n", ret);
67 return eCLE_InternalError;
70 SConfigurationParams titleInfo[3] = {
71 { CLCC_PSN_TITLE_ID, { NULL }
72 },{ CLCC_PSN_TITLE_SECRET, { NULL }
73 },{ CLCC_PSN_AGE_LIMIT, { NULL }
76 GetConfigurationInformation(titleInfo, 3);
78 #if !USE_NPTITLE_DAT
79 ret = sceNpSetNpTitleId((const SceNpTitleId*)titleInfo[0].m_pData, (const SceNpTitleSecret*)titleInfo[1].m_pData);
80 if (ret < PSN_OK)
82 NetLog("sceNpSetNpTitleId() failed. ret = 0x%x\n", ret);
83 return eCLE_InternalError;
85 #endif // !USE_NPTITLE_DAT
87 SCryLobbyAgeRestrictionSetup* pAgeSetup = (SCryLobbyAgeRestrictionSetup*)titleInfo[2].m_pData;
88 SceNpAgeRestriction* pAges = NULL;
90 SceNpContentRestriction contentRestriction;
91 memset(&contentRestriction, 0, sizeof(contentRestriction));
92 contentRestriction.size = sizeof(contentRestriction);
93 contentRestriction.defaultAgeRestriction = SCE_NP_NO_AGE_RESTRICTION;
95 if (pAgeSetup && pAgeSetup->m_numCountries && pAgeSetup->m_pCountries)
97 pAges = new SceNpAgeRestriction[pAgeSetup->m_numCountries];
98 if (pAges)
100 memset(pAges, 0, pAgeSetup->m_numCountries * sizeof(SceNpAgeRestriction));
102 for (int i = 0; i < pAgeSetup->m_numCountries; ++i)
104 strncpy(pAges[i].countryCode.data, pAgeSetup->m_pCountries[i].id, 2);
105 pAges[i].age = pAgeSetup->m_pCountries[i].age;
108 contentRestriction.ageRestrictionCount = pAgeSetup->m_numCountries;
109 contentRestriction.ageRestriction = pAges;
113 ret = sceNpSetContentRestriction(&contentRestriction);
114 if (ret < PSN_OK)
116 NetLog("sceNpSetContentRestriction() failed. ret = 0x%x\n", ret);
117 SAFE_DELETE_ARRAY(pAges);
118 return eCLE_InternalError;
121 SAFE_DELETE_ARRAY(pAges);
123 m_NPInited = true;
126 #if USE_CRY_TCPSERVICE
127 if ((m_pTCPServiceFactory == NULL) && (features & eCLSO_TCPService))
129 m_pTCPServiceFactory = new CCryTCPServiceFactory();
131 if (m_pTCPServiceFactory)
133 error = m_pTCPServiceFactory->Initialise("Scripts/Network/TCPServices.xml", CCryTCPServiceFactory::k_defaultMaxSockets);
135 else
137 return eCLE_OutOfMemory;
140 #endif // USE_CRY_TCPSERVICE
142 if ((error == eCLE_Success) && (m_pLobbySupport == NULL) && (features & eCLSO_Base))
144 m_pLobbySupport = new CCryPSNSupport(m_pLobby, this, features);
145 if (m_pLobbySupport)
147 error = m_pLobbySupport->Initialise();
148 if (!m_pLobbySupport->RegisterEventHandler(eCE_ErrorEvent, CCryPSNLobbyService::SupportCallback, this))
150 return eCLE_InternalError;
153 else
155 return eCLE_OutOfMemory;
159 #if USE_PSN_VOICE
160 if ((error == eCLE_Success) && (m_pVoice == NULL) && (features & eCLSO_Voice))
162 m_pVoice = new CCryPSNVoice(m_pLobby, this);
164 if (m_pVoice)
166 error = m_pVoice->Initialise();
168 else
170 return eCLE_OutOfMemory;
173 #endif
175 #if USE_CRY_MATCHMAKING
176 if ((error == eCLE_Success) && (m_pMatchmaking == NULL) && (features & eCLSO_Matchmaking))
178 m_pMatchmaking = new CCryPSNMatchMaking(m_pLobby, this, m_pLobbySupport, m_service);
180 if (m_pMatchmaking)
182 error = m_pMatchmaking->Initialise();
183 if (!m_pLobbySupport->RegisterEventHandler(eCE_ErrorEvent | eCE_RequestEvent | eCE_RoomEvent | eCE_SignalEvent | eCE_WebApiEvent, CCryPSNMatchMaking::SupportCallback, m_pMatchmaking))
185 return eCLE_InternalError;
188 else
190 return eCLE_OutOfMemory;
193 #endif // USE_CRY_MATCHMAKING
195 #if USE_CRY_STATS
196 if ((error == eCLE_Success) && (m_pStats == NULL) && (features & eCLSO_Stats))
198 m_pStats = new CCryPSNStats(m_pLobby, this, m_pLobbySupport);
200 if (m_pStats)
202 error = m_pStats->Initialise();
203 if (!m_pLobbySupport->RegisterEventHandler(eCE_ErrorEvent, CCryPSNStats::SupportCallback, m_pStats))
205 return eCLE_InternalError;
208 else
210 return eCLE_OutOfMemory;
213 #endif // USE_CRY_STATS
215 if ((error == eCLE_Success) && (m_pLobbyUI == NULL) && (features & eCLSO_LobbyUI))
217 m_pLobbyUI = new CCryPSNLobbyUI(m_pLobby, this, m_pLobbySupport);
219 if (m_pLobbyUI)
221 error = m_pLobbyUI->Initialise();
222 if (!m_pLobbySupport->RegisterEventHandler(eCE_ErrorEvent | eCE_WebApiEvent, CCryPSNLobbyUI::SupportCallback, m_pLobbyUI))
224 return eCLE_InternalError;
227 else
229 return eCLE_OutOfMemory;
233 #if USE_CRY_FRIENDS
234 if ((error == eCLE_Success) && (m_pFriends == NULL) && (features & eCLSO_Friends))
236 m_pFriends = new CCryPSNFriends(m_pLobby, this, m_pLobbySupport);
238 if (m_pFriends)
240 error = m_pFriends->Initialise();
241 if (!m_pLobbySupport->RegisterEventHandler(eCE_ErrorEvent | eCE_WebApiEvent, CCryPSNFriends::SupportCallback, m_pFriends))
243 return eCLE_InternalError;
246 else
248 return eCLE_OutOfMemory;
251 #endif // USE_CRY_FRIENDS
253 if ((error == eCLE_Success) && (m_pReward == NULL) && (features & eCLSO_Reward))
255 m_pReward = new CCryPSNReward(m_pLobby);
256 if (m_pReward)
258 error = m_pReward->Initialise();
260 else
262 return eCLE_OutOfMemory;
266 if (error == eCLE_Success)
268 if (features & eCLSO_Base)
270 m_pLobby->SetServicePacketEnd(m_service, ePSNPT_EndType);
273 else
275 Terminate(features, NULL);
278 if (pCB)
280 pCB(error, m_pLobby, m_service);
283 return error;
286 ECryLobbyError CCryPSNLobbyService::Terminate(ECryLobbyServiceFeatures features, CryLobbyServiceCallback pCB)
288 if (
289 #if USE_CRY_MATCHMAKING
290 m_pMatchmaking ||
291 #endif // USE_CRY_MATCHMAKING
292 #if USE_CRY_TCPSERVICE
293 m_pTCPServiceFactory ||
294 #endif // USE_CRY_TCPSERVICE
295 m_pLobbySupport ||
296 #if USE_CRY_STATS
297 m_pStats ||
298 #endif // USE_CRY_STATS
299 m_pLobbyUI ||
300 #if USE_CRY_FRIENDS
301 m_pFriends ||
302 #endif // USE_CRY_FRIENDS
303 m_pReward ||
304 #if USE_PSN_VOICE
305 m_pVoice ||
306 #endif
310 ECryLobbyError ret = CCryLobbyService::Terminate(features, pCB);
312 #if USE_CRY_FRIENDS
313 if (m_pFriends && (features & eCLSO_Friends))
315 ECryLobbyError error = m_pFriends->Terminate();
316 m_pFriends = NULL;
318 if (ret == eCLE_Success)
320 ret = error;
323 #endif // USE_CRY_FRIENDS
325 if (m_pLobbyUI && (features & eCLSO_LobbyUI))
327 ECryLobbyError error = m_pLobbyUI->Terminate();
328 m_pLobbyUI = NULL;
330 if (ret == eCLE_Success)
332 ret = error;
336 #if USE_CRY_STATS
337 if (m_pStats && (features & eCLSO_Stats))
339 ECryLobbyError error = m_pStats->Terminate();
340 m_pStats = NULL;
342 if (ret == eCLE_Success)
344 ret = error;
347 #endif // USE_CRY_STATS
349 #if USE_CRY_TCPSERVICE
350 if (m_pTCPServiceFactory && (features & eCLSO_TCPService))
352 ECryLobbyError error = m_pTCPServiceFactory->Terminate(false);
353 m_pTCPServiceFactory = NULL;
355 if (ret == eCLE_Success)
357 ret = error;
360 #endif // USE_CRY_TCPSERVICE
362 #if USE_PSN_VOICE
363 if (m_pVoice && (features & eCLSO_Voice))
365 ECryLobbyError error = m_pVoice->Terminate();
366 m_pVoice = NULL;
368 if (ret == eCLE_Success)
370 ret = error;
373 #endif
375 #if USE_CRY_MATCHMAKING
376 if (m_pMatchmaking && (features & eCLSO_Matchmaking))
378 ECryLobbyError error = m_pMatchmaking->Terminate();
379 m_pMatchmaking = NULL;
381 if (ret == eCLE_Success)
383 ret = error;
386 #endif // USE_CRY_MATCHMAKING
388 if (m_pReward && (features & eCLSO_Reward))
390 ECryLobbyError error = m_pReward->Terminate();
391 m_pReward = NULL;
393 if (ret == eCLE_Success)
395 ret = error;
399 if (m_pLobbySupport && (features & eCLSO_Base))
401 ECryLobbyError error = m_pLobbySupport->Terminate();
402 m_pLobbySupport = NULL;
404 if (ret == eCLE_Success)
406 ret = error;
410 if (pCB)
412 pCB(ret, m_pLobby, m_service);
415 return ret;
417 else
419 return eCLE_NotInitialised;
423 void CCryPSNLobbyService::Tick(CTimeValue tv)
425 if (m_pLobby->MutexTryLock())
427 if (m_pLobbySupport)
429 m_pLobbySupport->Tick();
432 for (uint32 i = 0; i < MAX_LOBBY_TASKS; i++)
434 STask* pTask = GetTask(i);
436 if (pTask->used && pTask->running)
438 if (pTask->error == eCLE_Success)
440 switch (pTask->startedTask)
442 case eT_GetUserPrivileges:
443 TickGetUserPrivileges(i);
444 break;
450 #if USE_CRY_MATCHMAKING
451 if (m_pMatchmaking)
453 m_pMatchmaking->Tick(tv);
455 #endif // USE_CRY_MATCHMAKING
457 #if USE_PSN_VOICE
458 if (m_pVoice)
460 m_pVoice->Tick(tv);
462 #endif
464 #if USE_CRY_STATS
465 if (m_pStats)
467 m_pStats->Tick(tv);
469 #endif // USE_CRY_STATS
471 if (m_pReward)
473 m_pReward->Tick(tv);
476 if (m_pLobbyUI)
478 m_pLobbyUI->Tick(tv);
481 m_pLobby->MutexUnlock();
484 #if USE_CRY_TCPSERVICE
485 if (m_pTCPServiceFactory)
487 m_pTCPServiceFactory->Tick(tv);
489 #endif // USE_CRY_TCPSERVICE
492 void CCryPSNLobbyService::OnPacket(const TNetAddress& addr, CCryLobbyPacket* pPacket)
494 #if USE_CRY_MATCHMAKING
495 if (m_pMatchmaking)
497 m_pMatchmaking->OnPacket(addr, pPacket);
499 #endif // USE_CRY_MATCHMAKING
501 #if USE_PSN_VOICE
502 if (m_pVoice)
504 m_pVoice->OnPacket(addr, pPacket);
506 #endif // USE_PSN_VOICE
509 void CCryPSNLobbyService::OnError(const TNetAddress& addr, ESocketError error, CryLobbySendID sendID)
511 #if USE_CRY_MATCHMAKING
512 if (m_pMatchmaking)
514 m_pMatchmaking->OnError(addr, error, sendID);
516 #endif // USE_CRY_MATCHMAKING
519 void CCryPSNLobbyService::OnSendComplete(const TNetAddress& addr, CryLobbySendID sendID)
521 #if USE_CRY_MATCHMAKING
522 if (m_pMatchmaking)
524 m_pMatchmaking->OnSendComplete(addr, sendID);
526 #endif // USE_CRY_MATCHMAKING
529 void CCryPSNLobbyService::GetConfigurationInformation(SConfigurationParams* infos, uint32 cnt)
531 m_pLobby->GetConfigurationInformation(infos, cnt);
534 CryUserID CCryPSNLobbyService::GetUserID(uint32 user)
536 if (m_pLobbySupport && (m_pLobbySupport->GetOnlineState() >= ePSNOS_Online))
538 //-- the npId should be valid
539 SCryPSNUserID* pID = new SCryPSNUserID;
540 if (pID)
542 pID->npId = *(m_pLobbySupport->GetLocalNpId());
543 return pID;
547 return CryUserInvalidID;
550 void CCryPSNLobbyService::StartTaskRunning(CryLobbyServiceTaskID lsTaskID)
552 LOBBY_AUTO_LOCK;
554 STask* pTask = GetTask(lsTaskID);
556 if (pTask->used)
558 pTask->running = true;
560 switch (pTask->startedTask)
562 case eT_GetUserPrivileges:
563 StartGetUserPrivileges(lsTaskID);
564 break;
569 void CCryPSNLobbyService::StopTaskRunning(CryLobbyServiceTaskID lsTaskID)
571 STask* pTask = GetTask(lsTaskID);
573 if (pTask->used)
575 pTask->running = false;
577 TO_GAME_FROM_LOBBY(&CCryPSNLobbyService::EndTask, this, lsTaskID);
581 void CCryPSNLobbyService::EndTask(CryLobbyServiceTaskID lsTaskID)
583 LOBBY_AUTO_LOCK;
585 STask* pTask = GetTask(lsTaskID);
587 if (pTask->used)
589 if (pTask->pCB)
591 switch (pTask->startedTask)
593 case eT_GetUserPrivileges:
595 uint32 result = 0;
596 if (!m_pLobbySupport || (m_pLobbySupport->GetOnlineState() < ePSNOS_Online) || (pTask->error == eCLE_InsufficientPrivileges) || (pTask->error == eCLE_AgeRestricted))
598 result |= CLPF_BlockMultiplayerSessons;
601 ((CryLobbyPrivilegeCallback)pTask->pCB)(pTask->lTaskID, pTask->error, result, pTask->pCBArg);
603 break;
607 if (pTask->error != eCLE_Success)
609 NetLog("[Lobby] Lobby Service EndTask %d Result %d", pTask->startedTask, pTask->error);
612 FreeTask(lsTaskID);
616 ECryLobbyError CCryPSNLobbyService::GetUserPrivileges(uint32 user, CryLobbyTaskID* pTaskID, CryLobbyPrivilegeCallback pCB, void* pCBArg)
618 ECryLobbyError error;
619 CryLobbyServiceTaskID tid;
621 LOBBY_AUTO_LOCK;
623 error = StartTask(eT_GetUserPrivileges, user, false, &tid, pTaskID, (void*)pCB, pCBArg);
625 if (error == eCLE_Success)
627 FROM_GAME_TO_LOBBY(&CCryPSNLobbyService::StartTaskRunning, this, tid);
630 return error;
633 ECryLobbyError CCryPSNLobbyService::GetSystemTime(uint32 user, SCrySystemTime* pSystemTime)
635 SceRtcTick tick;
636 ECryLobbyError error = eCLE_Success;
638 memset(pSystemTime, 0, sizeof(SCrySystemTime));
640 // There's no Network Clock as far as I can see. We'll have to use the system clock for now, which isn't as accurate.
641 int rc = sceRtcGetCurrentTick(&tick);
642 switch (rc)
644 case 0:
645 // success
646 break;
647 case SCE_NP_ERROR_NOT_INITIALIZED:
648 error = eCLE_NotInitialised;
649 break;
650 case SCE_NP_ERROR_INVALID_ARGUMENT:
651 error = eCLE_InvalidParam;
652 break;
653 default:
654 error = eCLE_InternalError;
655 break;
658 if (error == eCLE_Success)
660 SceRtcDateTime time;
661 if (sceRtcSetTick(&time, &tick) == SCE_OK)
663 pSystemTime->m_Year = time.year;
664 pSystemTime->m_Month = static_cast<uint8>(time.month);
665 pSystemTime->m_Day = static_cast<uint8>(time.day);
666 pSystemTime->m_Hour = static_cast<uint8>(time.hour);
667 pSystemTime->m_Minute = static_cast<uint8>(time.minute);
668 pSystemTime->m_Second = static_cast<uint8>(time.second);
670 else
672 error = eCLE_InternalError;
676 return error;
679 void CCryPSNLobbyService::StartGetUserPrivileges(CryLobbyServiceTaskID lsTaskID)
681 if (m_pLobbySupport && m_pLobby->GetInternalSocket(m_service))
683 // change this to ePSNOS_Matchmaking_Available if you want GetUserPrivileges to include PS+ test, otherwise it will test user age/PSN account privileges only
684 m_pLobbySupport->ResumeTransitioning(ePSNOS_Online);
686 else
688 UpdateTaskError(lsTaskID, eCLE_InternalError);
689 StopTaskRunning(lsTaskID);
693 void CCryPSNLobbyService::TickGetUserPrivileges(CryLobbyServiceTaskID lsTaskID)
695 // change this to ePSNOS_Matchmaking_Available if you want GetUserPrivileges to include PS+ test, otherwise it will test user age/PSN account privileges only
696 m_pLobbySupport->ResumeTransitioning(ePSNOS_Online);
697 if (m_pLobbySupport->HasTransitioningReachedState(ePSNOS_Online))
699 StopTaskRunning(lsTaskID);
703 void CCryPSNLobbyService::GetSocketPorts(uint16& connectPort, uint16& listenPort)
705 connectPort = SCE_NP_PORT;
706 listenPort = SCE_NP_PORT;
709 #if defined(WARFACE_CONSOLE_VERSION)
710 #if defined(GUID_STYLE_DB)
712 CryUserID CCryPSNLobbyService::GetUserIDFromGUID(PlatformGUID64 xuid)
714 SCryPSNUserID* pID = new SCryPSNUserID;
716 if (pID)
718 pID->guid = xuid;
719 return pID;
722 return CryUserInvalidID;
725 PlatformGUID64 CCryPSNLobbyService::GetGUIDFromUserID(CryUserID id)
727 return ((SCryPSNUserID*)id.get())->guid;
730 #endif
731 #endif
733 void CCryPSNLobbyService::MakeAddrPCCompatible(TNetAddress& addr)
735 //SIPv4Addr* pIPv4Addr = addr.GetPtr<SIPv4Addr>();
737 //if (pIPv4Addr)
739 // pIPv4Addr->lobbyService = eCLS_LAN;
743 #endif // USE_PSN
744 #endif // CRY_PLATFORM_ORBIS