HRESULT XblContext::Initialize()

in Source/Services/Common/xbox_live_context.cpp [53:206]


HRESULT XblContext::Initialize(
    std::shared_ptr<real_time_activity::RealTimeActivityManager> rtaManager
)
{
    m_xboxLiveContextSettings = MakeShared<xbox::services::XboxLiveContextSettings>();

    std::weak_ptr<XblContext> thisWeakPtr = shared_from_this();
    TaskQueue globalQueue;
    {
        auto state = GlobalState::Get();
        if (state)
        {
            globalQueue = state->Queue();
        }
    }

    {
        Result<xbox::services::User> userResult = m_user.Copy();
        RETURN_HR_IF_FAILED(userResult.Hresult());
        m_achievementService = MakeShared<xbox::services::achievements::AchievementsService>(userResult.ExtractPayload(), m_xboxLiveContextSettings, AppConfig::Instance(), thisWeakPtr, rtaManager);
    }

    {
        Result<xbox::services::User> userResult = m_user.Copy();
        RETURN_HR_IF_FAILED(userResult.Hresult());
        m_profileService = MakeShared<xbox::services::social::ProfileService>(userResult.ExtractPayload(), m_xboxLiveContextSettings, AppConfig::Instance());
    }

    {
        Result<xbox::services::User> userResult = m_user.Copy();
        RETURN_HR_IF_FAILED(userResult.Hresult());
        m_reputationServiceImpl = MakeShared<xbox::services::social::ReputationService>(userResult.ExtractPayload(), m_xboxLiveContextSettings);
    }

    {
        Result<xbox::services::User> userResult = m_user.Copy();
        RETURN_HR_IF_FAILED(userResult.Hresult());
        m_presenceService = MakeShared<xbox::services::presence::PresenceService>(userResult.ExtractPayload(), m_xboxLiveContextSettings, rtaManager);
    }

    {
        Result<xbox::services::User> userResult = m_user.Copy();
        RETURN_HR_IF_FAILED(userResult.Hresult());
        m_socialService = MakeShared<xbox::services::social::SocialService>(userResult.ExtractPayload(), m_xboxLiveContextSettings, rtaManager);
    }

    {
        Result<xbox::services::User> userResult = m_user.Copy();
        RETURN_HR_IF_FAILED(userResult.Hresult());
        m_stringService = MakeShared<xbox::services::system::StringService>(userResult.ExtractPayload(), m_xboxLiveContextSettings);
    }

    {
        Result<xbox::services::User> userResult = m_user.Copy();
        RETURN_HR_IF_FAILED(userResult.Hresult());
        m_multiplayerService = MakeShared<xbox::services::multiplayer::MultiplayerService>(userResult.ExtractPayload(), m_xboxLiveContextSettings, AppConfig::Instance(), rtaManager);
    }

    {
        Result<xbox::services::User> userResult = m_user.Copy();
        RETURN_HR_IF_FAILED(userResult.Hresult());
        m_privacyService = MakeShared<privacy::PrivacyService>(userResult.ExtractPayload(), m_xboxLiveContextSettings);
    }

    {
        Result<xbox::services::User> userResult = m_user.Copy();
        RETURN_HR_IF_FAILED(userResult.Hresult());
        m_titleManagedStatisticsService = MakeShared<xbox::services::user_statistics::TitleManagedStatisticsService>(userResult.ExtractPayload(), m_xboxLiveContextSettings);
    }

    {
        Result<xbox::services::User> userResult = m_user.Copy();
        RETURN_HR_IF_FAILED(userResult.Hresult());
        m_userStatisticsService = MakeShared<xbox::services::user_statistics::UserStatisticsService>(userResult.ExtractPayload(), m_xboxLiveContextSettings, rtaManager);
    }

    {
        Result<xbox::services::User> userResult = m_user.Copy();
        RETURN_HR_IF_FAILED(userResult.Hresult());
        m_leaderboardService = MakeShared<xbox::services::leaderboard::LeaderboardService>(userResult.ExtractPayload(), m_xboxLiveContextSettings, AppConfig::Instance());
    }

    {
        Result<xbox::services::User> userResult = m_user.Copy();
        RETURN_HR_IF_FAILED(userResult.Hresult());
        m_matchmakingService = MakeShared<xbox::services::matchmaking::MatchmakingService>(userResult.ExtractPayload(), m_xboxLiveContextSettings);
    }

    {
        Result<xbox::services::User> userResult = m_user.Copy();
        RETURN_HR_IF_FAILED(userResult.Hresult());
        m_titleStorageService = MakeShared<xbox::services::title_storage::TitleStorageService>(userResult.ExtractPayload(), m_xboxLiveContextSettings);
    }

    {
        Result<xbox::services::User> userResult = m_user.Copy();
        RETURN_HR_IF_FAILED(userResult.Hresult());
        m_multiplayerActivityService = MakeShared<multiplayer_activity::MultiplayerActivityService>(userResult.ExtractPayload(), globalQueue, m_xboxLiveContextSettings);
    }
    
    {
        Result<xbox::services::User> userResult = m_user.Copy();
        RETURN_HR_IF_FAILED(userResult.Hresult());
#if XSAPI_EVENTS_SERVICE
        m_eventsService = MakeShared<events::EventsService>(
            userResult.ExtractPayload()
#if XSAPI_INTERNAL_EVENTS_SERVICE
            , globalQueue
#endif
            );
        RETURN_HR_IF_FAILED(m_eventsService->Initialize());
#endif
    }

    {
        Result<xbox::services::User> userResult = m_user.Copy();
        RETURN_HR_IF_FAILED(userResult.Hresult());
#if XSAPI_NOTIFICATION_SERVICE
#if !XSAPI_UNIT_TESTS && (HC_PLATFORM == HC_PLATFORM_WIN32 || HC_PLATFORM_IS_EXTERNAL)
        m_notificationService = MakeShared<xbox::services::notification::RTANotificationService>(userResult.ExtractPayload(), globalQueue, m_xboxLiveContextSettings, rtaManager);
        RETURN_HR_IF_FAILED(m_notificationService->Initialize());
#elif HC_PLATFORM == HC_PLATFORM_ANDROID || HC_PLATFORM == HC_PLATFORM_IOS
        m_notificationService = MakeShared<xbox::services::notification::MobileNotificationService>(userResult.ExtractPayload(), m_xboxLiveContextSettings);
#elif HC_PLATFORM == HC_PLATFORM_UWP
        m_notificationService = MakeShared<xbox::services::notification::UWPNotificationService>(userResult.ExtractPayload(), m_xboxLiveContextSettings);
        m_notificationService->RegisterWithNotificationService(AsyncContext<HRESULT>{ globalQueue });
#endif
#endif
    }

#if XSAPI_NOTIFICATION_SERVICE
    auto userChangedRegistrationResult = User::RegisterChangeEventHandler(
        [
            thisWeakPtr,
            queue{ globalQueue.DeriveWorkerQueue() }
        ]
    (UserLocalId localId, UserChangeType changeType)
    {
        auto sharedThis{ thisWeakPtr.lock() };
        if (sharedThis)
        {
            if (sharedThis->m_user.LocalId() == localId.value && changeType == XalUserChange_SignedOut)
            {
                sharedThis->NotificationService()->UnregisterFromNotificationService(AsyncContext<HRESULT>{ queue });
            }
        }
    });

    RETURN_HR_IF_FAILED(userChangedRegistrationResult.Hresult());
    m_userChangeEventToken = userChangedRegistrationResult.Payload();
#endif

    return S_OK;
}