示例#1
0
            static void ToggleGoogleKeyStateVisibility(VisualElement fieldBlock, GooglePlayKeyState importState)
            {
                if (fieldBlock != null)
                {
                    var verifiedMode = fieldBlock.Q(k_VerifiedMode);
                    if (verifiedMode != null)
                    {
                        verifiedMode.style.display = (importState == GooglePlayKeyState.Verified) ? DisplayStyle.Flex : DisplayStyle.None;
                    }

                    var unVerifiedMode = fieldBlock.Q(k_UnverifiedMode);
                    if (unVerifiedMode != null)
                    {
                        unVerifiedMode.style.display = (importState == GooglePlayKeyState.Verified)
                            ? DisplayStyle.None
                            : DisplayStyle.Flex;

                        var badFormatError = fieldBlock.Q(k_ErrorKeyFormat);
                        if (badFormatError != null)
                        {
                            badFormatError.style.display = (importState == GooglePlayKeyState.InvalidFormat)
                                ? DisplayStyle.Flex
                                : DisplayStyle.None;
                        }

                        var unauthUserError = fieldBlock.Q(k_ErrorUnauthorized);
                        if (unauthUserError != null)
                        {
                            unauthUserError.style.display = (importState == GooglePlayKeyState.UnauthorizedUser)
                                ? DisplayStyle.Flex
                                : DisplayStyle.None;
                        }

                        var serverError = fieldBlock.Q(k_ErrorServer);
                        if (serverError != null)
                        {
                            serverError.style.display = (importState == GooglePlayKeyState.ServerError)
                                ? DisplayStyle.Flex
                                : DisplayStyle.None;
                        }

                        var cantFetchError = fieldBlock.Q(k_ErrorFethcKey);
                        if (cantFetchError != null)
                        {
                            cantFetchError.style.display = (importState == GooglePlayKeyState.CantFetch)
                                ? DisplayStyle.Flex
                                : DisplayStyle.None;
                        }
                    }
                }
            }
            void OnGetGooglePlayKey(AsyncOperation op)
            {
                UnityWebRequestAsyncOperation webOp = (UnityWebRequestAsyncOperation)op;

                if (webOp != null && webOp.isDone)
                {
                    UnityWebRequest completedRequest = webOp.webRequest;
                    if (completedRequest != null)
                    {
                        if ((completedRequest.result != UnityWebRequest.Result.ProtocolError) && (completedRequest.result != UnityWebRequest.Result.ConnectionError))
                        {
                            var jsonParser = new JSONParser(completedRequest.downloadHandler.text);
                            try
                            {
                                var json   = jsonParser.Parse();
                                var rawKey = json.AsDict()[PurchasingService.instance.googleKeyJsonLabel];

                                m_GooglePlayKey = (!rawKey.IsNull()) ? rawKey.AsString() : null;

                                if (!String.IsNullOrEmpty(m_GooglePlayKey))
                                {
                                    m_GooglePlayKeyState = GooglePlayKeyState.Verified;
                                    m_IapOptionsBlock.Q <TextField>(k_GooglePlayKeyEntry).SetValueWithoutNotify(m_GooglePlayKey);
                                }
                                else
                                {
                                    m_GooglePlayKeyState = GooglePlayKeyState.CantFetch;
                                }
                            }
                            catch (Exception ex)
                            {
                                NotificationManager.instance.Publish(provider.serviceInstance.notificationTopic, Notification.Severity.Error,
                                                                     string.Format(L10n.Tr(k_KeyParsingExceptionMessage), ex.Message));

                                Debug.LogException(ex);

                                m_GooglePlayKey      = "";
                                m_GooglePlayKeyState = GooglePlayKeyState.CantFetch;
                            }
                        }
                        else
                        {
                            m_GooglePlayKeyState = GooglePlayKeyState.CantFetch;
                        }
                    }
                }

                ToggleGoogleKeyStateVisibility(m_IapOptionsBlock, m_GooglePlayKeyState);
            }
            void OnSubmitGooglePlayKey(AsyncOperation op)
            {
                UnityWebRequestAsyncOperation webOp = (UnityWebRequestAsyncOperation)op;

                if (webOp != null)
                {
                    bool            verified         = false;
                    UnityWebRequest completedRequest = webOp.webRequest;
                    if (completedRequest != null)
                    {
                        if ((completedRequest.result != UnityWebRequest.Result.ProtocolError) && (completedRequest.result != UnityWebRequest.Result.ConnectionError))
                        {
                            verified             = true;
                            m_GooglePlayKeyState = GooglePlayKeyState.Verified;
                        }
                        else if (completedRequest.result == UnityWebRequest.Result.ProtocolError)
                        {
                            if (completedRequest.responseCode == 405 || completedRequest.responseCode == 500)
                            {
                                m_GooglePlayKeyState = GooglePlayKeyState.ServerError;
                            }
                            else if (completedRequest.responseCode == 401 || completedRequest.responseCode == 403)
                            {
                                m_GooglePlayKeyState = GooglePlayKeyState.UnauthorizedUser;
                            }
                            else
                            {
                                m_GooglePlayKeyState = GooglePlayKeyState.InvalidFormat;
                            }
                        }
                        else
                        {
                            m_GooglePlayKeyState = GooglePlayKeyState.InvalidFormat;
                        }
                    }

                    EditorAnalytics.SendValidatePublicKeyEvent(new PublicKeyInfo()
                    {
                        key = "Google Play", success = verified
                    });
                }

                ToggleGoogleKeyStateVisibility(m_IapOptionsBlock, m_GooglePlayKeyState);
            }
            void ToggleGoogleKeyStateVisibility(VisualElement fieldBlock, GooglePlayKeyState importState)
            {
                if (fieldBlock != null)
                {
                    var verifiedMode = fieldBlock.Q(k_VerifiedMode);
                    if (verifiedMode != null)
                    {
                        var updateGooglePlayKeyBtn = m_IapOptionsBlock.Q <Button>(k_UpdateGooglePlayKeyBtn);
                        if (importState == GooglePlayKeyState.Verified)
                        {
                            if (updateGooglePlayKeyBtn != null)
                            {
                                updateGooglePlayKeyBtn.text = L10n.Tr(k_GooglePlayKeyBtnUpdateLabel);
                            }
                            verifiedMode.style.display = DisplayStyle.Flex;
                        }
                        else
                        {
                            if (updateGooglePlayKeyBtn != null)
                            {
                                updateGooglePlayKeyBtn.text = L10n.Tr(k_GooglePlayKeyBtnVerifyLabel);
                            }
                            verifiedMode.style.display = DisplayStyle.None;
                        }
                    }

                    var unVerifiedMode = fieldBlock.Q(k_UnverifiedMode);
                    if (unVerifiedMode != null)
                    {
                        unVerifiedMode.style.display = (importState == GooglePlayKeyState.Verified)
                            ? DisplayStyle.None
                            : DisplayStyle.Flex;

                        var badFormatError = fieldBlock.Q(k_ErrorKeyFormat);
                        if (badFormatError != null)
                        {
                            badFormatError.style.display = (importState == GooglePlayKeyState.InvalidFormat)
                                ? DisplayStyle.Flex
                                : DisplayStyle.None;
                        }

                        var unauthUserError = fieldBlock.Q(k_ErrorUnauthorized);
                        if (unauthUserError != null)
                        {
                            unauthUserError.style.display = (importState == GooglePlayKeyState.UnauthorizedUser)
                                ? DisplayStyle.Flex
                                : DisplayStyle.None;
                        }

                        var serverError = fieldBlock.Q(k_ErrorServer);
                        if (serverError != null)
                        {
                            serverError.style.display = (importState == GooglePlayKeyState.ServerError)
                                ? DisplayStyle.Flex
                                : DisplayStyle.None;
                        }

                        var cantFetchError = fieldBlock.Q(k_ErrorFethcKey);
                        if (cantFetchError != null)
                        {
                            cantFetchError.style.display = (importState == GooglePlayKeyState.CantFetch)
                                ? DisplayStyle.Flex
                                : DisplayStyle.None;
                        }
                    }
                }
            }