1 // Copyright 2001-2018 Crytek GmbH / Crytek Group. All rights reserved.
7 #include "CryPSN2Lobby.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
)
37 m_pLobbySupport
= NULL
;
38 #if USE_CRY_MATCHMAKING
39 m_pMatchmaking
= NULL
;
40 #endif // USE_CRY_MATCHMAKING
42 #if USE_CRY_TCPSERVICE
43 m_pTCPServiceFactory
= NULL
;
44 #endif // USE_CRY_TCPSERVICE
47 #endif // USE_CRY_STATS
51 #endif // USE_CRY_FRIENDS
55 ECryLobbyError
CCryPSNLobbyService::Initialise(ECryLobbyServiceFeatures features
, CryLobbyServiceCallback pCB
)
57 ECryLobbyError error
= CCryLobbyService::Initialise(features
, pCB
);
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);
79 ret
= sceNpSetNpTitleId((const SceNpTitleId
*)titleInfo
[0].m_pData
, (const SceNpTitleSecret
*)titleInfo
[1].m_pData
);
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
];
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
);
116 NetLog("sceNpSetContentRestriction() failed. ret = 0x%x\n", ret
);
117 SAFE_DELETE_ARRAY(pAges
);
118 return eCLE_InternalError
;
121 SAFE_DELETE_ARRAY(pAges
);
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
);
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
);
147 error
= m_pLobbySupport
->Initialise();
148 if (!m_pLobbySupport
->RegisterEventHandler(eCE_ErrorEvent
, CCryPSNLobbyService::SupportCallback
, this))
150 return eCLE_InternalError
;
155 return eCLE_OutOfMemory
;
160 if ((error
== eCLE_Success
) && (m_pVoice
== NULL
) && (features
& eCLSO_Voice
))
162 m_pVoice
= new CCryPSNVoice(m_pLobby
, this);
166 error
= m_pVoice
->Initialise();
170 return eCLE_OutOfMemory
;
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
);
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
;
190 return eCLE_OutOfMemory
;
193 #endif // USE_CRY_MATCHMAKING
196 if ((error
== eCLE_Success
) && (m_pStats
== NULL
) && (features
& eCLSO_Stats
))
198 m_pStats
= new CCryPSNStats(m_pLobby
, this, m_pLobbySupport
);
202 error
= m_pStats
->Initialise();
203 if (!m_pLobbySupport
->RegisterEventHandler(eCE_ErrorEvent
, CCryPSNStats::SupportCallback
, m_pStats
))
205 return eCLE_InternalError
;
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
);
221 error
= m_pLobbyUI
->Initialise();
222 if (!m_pLobbySupport
->RegisterEventHandler(eCE_ErrorEvent
| eCE_WebApiEvent
, CCryPSNLobbyUI::SupportCallback
, m_pLobbyUI
))
224 return eCLE_InternalError
;
229 return eCLE_OutOfMemory
;
234 if ((error
== eCLE_Success
) && (m_pFriends
== NULL
) && (features
& eCLSO_Friends
))
236 m_pFriends
= new CCryPSNFriends(m_pLobby
, this, m_pLobbySupport
);
240 error
= m_pFriends
->Initialise();
241 if (!m_pLobbySupport
->RegisterEventHandler(eCE_ErrorEvent
| eCE_WebApiEvent
, CCryPSNFriends::SupportCallback
, m_pFriends
))
243 return eCLE_InternalError
;
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
);
258 error
= m_pReward
->Initialise();
262 return eCLE_OutOfMemory
;
266 if (error
== eCLE_Success
)
268 if (features
& eCLSO_Base
)
270 m_pLobby
->SetServicePacketEnd(m_service
, ePSNPT_EndType
);
275 Terminate(features
, NULL
);
280 pCB(error
, m_pLobby
, m_service
);
286 ECryLobbyError
CCryPSNLobbyService::Terminate(ECryLobbyServiceFeatures features
, CryLobbyServiceCallback pCB
)
289 #if USE_CRY_MATCHMAKING
291 #endif // USE_CRY_MATCHMAKING
292 #if USE_CRY_TCPSERVICE
293 m_pTCPServiceFactory
||
294 #endif // USE_CRY_TCPSERVICE
298 #endif // USE_CRY_STATS
302 #endif // USE_CRY_FRIENDS
310 ECryLobbyError ret
= CCryLobbyService::Terminate(features
, pCB
);
313 if (m_pFriends
&& (features
& eCLSO_Friends
))
315 ECryLobbyError error
= m_pFriends
->Terminate();
318 if (ret
== eCLE_Success
)
323 #endif // USE_CRY_FRIENDS
325 if (m_pLobbyUI
&& (features
& eCLSO_LobbyUI
))
327 ECryLobbyError error
= m_pLobbyUI
->Terminate();
330 if (ret
== eCLE_Success
)
337 if (m_pStats
&& (features
& eCLSO_Stats
))
339 ECryLobbyError error
= m_pStats
->Terminate();
342 if (ret
== eCLE_Success
)
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
)
360 #endif // USE_CRY_TCPSERVICE
363 if (m_pVoice
&& (features
& eCLSO_Voice
))
365 ECryLobbyError error
= m_pVoice
->Terminate();
368 if (ret
== eCLE_Success
)
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
)
386 #endif // USE_CRY_MATCHMAKING
388 if (m_pReward
&& (features
& eCLSO_Reward
))
390 ECryLobbyError error
= m_pReward
->Terminate();
393 if (ret
== eCLE_Success
)
399 if (m_pLobbySupport
&& (features
& eCLSO_Base
))
401 ECryLobbyError error
= m_pLobbySupport
->Terminate();
402 m_pLobbySupport
= NULL
;
404 if (ret
== eCLE_Success
)
412 pCB(ret
, m_pLobby
, m_service
);
419 return eCLE_NotInitialised
;
423 void CCryPSNLobbyService::Tick(CTimeValue tv
)
425 if (m_pLobby
->MutexTryLock())
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
);
450 #if USE_CRY_MATCHMAKING
453 m_pMatchmaking
->Tick(tv
);
455 #endif // USE_CRY_MATCHMAKING
469 #endif // USE_CRY_STATS
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
497 m_pMatchmaking
->OnPacket(addr
, pPacket
);
499 #endif // USE_CRY_MATCHMAKING
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
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
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
;
542 pID
->npId
= *(m_pLobbySupport
->GetLocalNpId());
547 return CryUserInvalidID
;
550 void CCryPSNLobbyService::StartTaskRunning(CryLobbyServiceTaskID lsTaskID
)
554 STask
* pTask
= GetTask(lsTaskID
);
558 pTask
->running
= true;
560 switch (pTask
->startedTask
)
562 case eT_GetUserPrivileges
:
563 StartGetUserPrivileges(lsTaskID
);
569 void CCryPSNLobbyService::StopTaskRunning(CryLobbyServiceTaskID lsTaskID
)
571 STask
* pTask
= GetTask(lsTaskID
);
575 pTask
->running
= false;
577 TO_GAME_FROM_LOBBY(&CCryPSNLobbyService::EndTask
, this, lsTaskID
);
581 void CCryPSNLobbyService::EndTask(CryLobbyServiceTaskID lsTaskID
)
585 STask
* pTask
= GetTask(lsTaskID
);
591 switch (pTask
->startedTask
)
593 case eT_GetUserPrivileges
:
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
);
607 if (pTask
->error
!= eCLE_Success
)
609 NetLog("[Lobby] Lobby Service EndTask %d Result %d", pTask
->startedTask
, pTask
->error
);
616 ECryLobbyError
CCryPSNLobbyService::GetUserPrivileges(uint32 user
, CryLobbyTaskID
* pTaskID
, CryLobbyPrivilegeCallback pCB
, void* pCBArg
)
618 ECryLobbyError error
;
619 CryLobbyServiceTaskID tid
;
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
);
633 ECryLobbyError
CCryPSNLobbyService::GetSystemTime(uint32 user
, SCrySystemTime
* pSystemTime
)
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
);
647 case SCE_NP_ERROR_NOT_INITIALIZED
:
648 error
= eCLE_NotInitialised
;
650 case SCE_NP_ERROR_INVALID_ARGUMENT
:
651 error
= eCLE_InvalidParam
;
654 error
= eCLE_InternalError
;
658 if (error
== eCLE_Success
)
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
);
672 error
= eCLE_InternalError
;
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
);
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
;
722 return CryUserInvalidID
;
725 PlatformGUID64
CCryPSNLobbyService::GetGUIDFromUserID(CryUserID id
)
727 return ((SCryPSNUserID
*)id
.get())->guid
;
733 void CCryPSNLobbyService::MakeAddrPCCompatible(TNetAddress
& addr
)
735 //SIPv4Addr* pIPv4Addr = addr.GetPtr<SIPv4Addr>();
739 // pIPv4Addr->lobbyService = eCLS_LAN;
744 #endif // CRY_PLATFORM_ORBIS