/// <summary>
        /// Checks if the current user has a specific privilege
        /// </summary>
        /// <param name="user">XboxLiveUser that identifies the user to show the UI on behalf of.</param>
        /// <param name="privilege">The privilege to check.</param>
        /// <returns>
        /// A boolean which is true if the current user has the privilege.
        /// </returns>
        public static bool CheckGamingPrivilegeSilently(XboxLiveUser user, GamingPrivilege privilege)
        {
            var tcs = new TaskCompletionSource <bool>();

            Task.Run(() =>
            {
                int contextKey = XsapiCallbackContext <object, bool> .CreateContext(null, tcs);

                var result = TCUICheckGamingPrivilegeSilently(privilege, CheckGamingPrivilegeComplete, (IntPtr)contextKey, XboxLive.DefaultTaskGroupId);
                if (result != XSAPI_RESULT.XSAPI_RESULT_OK)
                {
                    tcs.SetException(new XboxException(result));
                }
            });

            tcs.Task.Wait();
            return(tcs.Task.Result);
        }
        public static bool TryRemove(int contextKey, out XsapiCallbackContext <T, T2> context)
        {
            context = null;

            lock (contextsLock)
            {
                IDisposable genericContext;
                if (contextsMap.TryGetValue(contextKey, out genericContext))
                {
                    context = genericContext as XsapiCallbackContext <T, T2>;
                    if (context != null)
                    {
                        contextsMap.Remove(contextKey);
                        return(true);
                    }
                }
            }
            return(false);
        }
Esempio n. 3
0
        public Task <GetTokenAndSignatureResult> InternalGetTokenAndSignatureAsync(string httpMethod, string url, string headers, byte[] body, bool promptForCredentialsIfNeeded, bool forceRefresh)
        {
            var tcs = new TaskCompletionSource <GetTokenAndSignatureResult>();

            Task.Run(() =>
            {
                IntPtr pHttpMethod = MarshalingHelpers.StringToHGlobalUtf8(httpMethod);
                IntPtr pUrl        = MarshalingHelpers.StringToHGlobalUtf8(url);
                IntPtr pHeaders    = MarshalingHelpers.StringToHGlobalUtf8(headers);

                IntPtr pBody = IntPtr.Zero;
                if (body != null)
                {
                    Marshal.AllocHGlobal(body.Length + 1);
                    Marshal.Copy(body, 0, pBody, body.Length);
                    Marshal.WriteByte(pBody, body.Length, 0);
                }

                int contextKey;
                var context            = XsapiCallbackContext <UserImpl, GetTokenAndSignatureResult> .CreateContext(this, tcs, out contextKey);
                context.PointersToFree = new List <IntPtr> {
                    pHttpMethod, pUrl, pHeaders, pBody
                };

                var result = XboxLiveUserGetTokenAndSignature(
                    XboxLiveUserPtr,
                    pHttpMethod,
                    pUrl,
                    pHeaders,
                    pBody,
                    GetTokenAndSignatureComplete,
                    (IntPtr)contextKey,
                    XboxLive.DefaultTaskGroupId);

                if (result != XSAPI_RESULT.XSAPI_RESULT_OK)
                {
                    throw new XboxException(result);
                }
            });

            return(tcs.Task);
        }
Esempio n. 4
0
        public Task <SignInResult> SignInImpl(bool showUI, bool forceRefresh)
        {
            var tcs = new TaskCompletionSource <SignInResult>();

            Task.Run(() =>
            {
                int contextKey;
                var context = XsapiCallbackContext <UserImpl, SignInResult> .CreateContext(this, tcs, out contextKey);

                XSAPI_RESULT result = XSAPI_RESULT.XSAPI_RESULT_OK;
                if (showUI)
                {
                    var coreDispatcherPtr     = Marshal.GetIUnknownForObject(Windows.ApplicationModel.Core.CoreApplication.MainView.Dispatcher);
                    context.PointersToRelease = new List <IntPtr> {
                        coreDispatcherPtr
                    };

                    result = XboxLiveUserSignInWithCoreDispatcher(
                        XboxLiveUserPtr,
                        coreDispatcherPtr,
                        SignInComplete,
                        (IntPtr)contextKey,
                        XboxLive.DefaultTaskGroupId);
                }
                else
                {
                    result = XboxLiveUserSignInSilently(
                        XboxLiveUserPtr,
                        SignInComplete,
                        (IntPtr)contextKey,
                        XboxLive.DefaultTaskGroupId);
                }

                if (result != XSAPI_RESULT.XSAPI_RESULT_OK)
                {
                    throw new XboxException(result);
                }
            });

            return(tcs.Task);
        }
        /// <summary>
        /// Shows UI displaying the profile card for a specified user.
        /// </summary>
        /// <param name="user">XboxLiveUser that identifies the user to show the UI on behalf of.</param>
        /// <param name="targetXboxUserId">The Xbox User ID to show information about.</param>
        /// <returns>
        /// An interface for tracking the progress of the asynchronous call.
        /// The operation completes when the UI is closed.
        /// </returns>
        public static Task ShowProfileCardUIAsync(XboxLiveUser user, string targetXboxUserId)
        {
            var tcs = new TaskCompletionSource <object>();

            Task.Run(() =>
            {
                var pTargetXboxUserId = MarshalingHelpers.StringToHGlobalUtf8(targetXboxUserId);

                int contextKey;
                var context            = XsapiCallbackContext <object, object> .CreateContext(null, tcs, out contextKey);
                context.PointersToFree = new List <IntPtr> {
                    pTargetXboxUserId
                };

                var result = TCUIShowProfileCardUI(pTargetXboxUserId, ShowProfileCardUIComplete, (IntPtr)contextKey, XboxLive.DefaultTaskGroupId);
                if (result != XSAPI_RESULT.XSAPI_RESULT_OK)
                {
                    tcs.SetException(new XboxException(result));
                }
            });

            return(tcs.Task);
        }
Esempio n. 6
0
        private static void SignInComplete(XSAPI_RESULT_INFO result, XSAPI_SIGN_IN_RESULT payload, IntPtr context)
        {
            int contextKey = context.ToInt32();

            XsapiCallbackContext <UserImpl, SignInResult> contextObject;

            if (XsapiCallbackContext <UserImpl, SignInResult> .TryRemove(contextKey, out contextObject))
            {
                UserImpl @this = contextObject.Context;
                if (result.errorCode == XSAPI_RESULT.XSAPI_RESULT_OK)
                {
                    @this.UpdatePropertiesFromXboxLiveUserPtr();
                    @this.SignInCompleted(@this, new EventArgs());

                    contextObject.TaskCompletionSource.SetResult(new SignInResult(payload.status));
                }
                else
                {
                    contextObject.TaskCompletionSource.SetException(new XboxException(result));
                }
                contextObject.Dispose();
            }
        }