Exemplo n.º 1
0
        /// <summary>Handles the completion of an image download.</summary>
        protected virtual void OnDownloadCompleted(UnityWebRequest webRequest, string imageURL)
        {
            // early out if destroyed
            if (this == null)
            {
                return;
            }

            Debug.Assert(webRequest != null);

            // handle callbacks
            Callbacks callbacks;
            bool      isURLMapped = this.m_callbackMap.TryGetValue(imageURL, out callbacks);

            if (callbacks == null)
            {
                Debug.LogWarning("[mod.io] ImageRequestManager completed a download but the callbacks"
                                 + " entry for the download was null."
                                 + "\nImageURL = " + imageURL
                                 + "\nWebRequest.URL = " + webRequest.url
                                 + "\nm_callbackMap.TryGetValue() = " + isURLMapped.ToString()
                                 );
                return;
            }

            if (webRequest.isHttpError || webRequest.isNetworkError)
            {
                if (callbacks.failed.Count > 0)
                {
                    WebRequestError error = WebRequestError.GenerateFromWebRequest(webRequest);

                    foreach (var errorCallback in callbacks.failed)
                    {
                        errorCallback(error);
                    }
                }
            }
            else
            {
                Texture2D texture = ((DownloadHandlerTexture)webRequest.downloadHandler).texture;

                if (this.isActiveAndEnabled || !this.clearCacheOnDisable)
                {
                    this.cache[imageURL] = texture;
                }

                foreach (var successCallback in callbacks.succeeded)
                {
                    successCallback(texture);
                }
            }

            // remove from "in progress"
            this.m_callbackMap.Remove(imageURL);
        }
        /// <summary>Handles the completion of an image download.</summary>
        protected virtual void OnDownloadCompleted(UnityWebRequest webRequest, string imageURL)
        {
            // early out if destroyed
            if (this == null)
            {
                return;
            }

            Debug.Assert(webRequest != null);

            // - logging -
            #if DEBUG
            if (PluginSettings.data.logAllRequests && logDownloads)
            {
                if (webRequest.isNetworkError || webRequest.isHttpError)
                {
                    WebRequestError.LogAsWarning(WebRequestError.GenerateFromWebRequest(webRequest));
                }
                else
                {
                    var headerString    = new System.Text.StringBuilder();
                    var responseHeaders = webRequest.GetResponseHeaders();
                    if (responseHeaders != null &&
                        responseHeaders.Count > 0)
                    {
                        headerString.Append("\n");
                        foreach (var kvp in responseHeaders)
                        {
                            headerString.AppendLine("- [" + kvp.Key + "] " + kvp.Value);
                        }
                    }
                    else
                    {
                        headerString.Append(" NONE");
                    }

                    var    responseTimeStamp = ServerTimeStamp.Now;
                    string logString         = ("IMAGE DOWNLOAD SUCCEEDED"
                                                + "\nURL: " + webRequest.url
                                                + "\nTime Stamp: " + responseTimeStamp + " ("
                                                + ServerTimeStamp.ToLocalDateTime(responseTimeStamp) + ")"
                                                + "\nResponse Headers: " + headerString.ToString()
                                                + "\nResponse Code: " + webRequest.responseCode
                                                + "\nResponse Error: " + webRequest.error
                                                + "\n");
                    Debug.Log(logString);
                }
            }
            #endif

            // handle callbacks
            Callbacks callbacks;
            bool      isURLMapped = this.m_callbackMap.TryGetValue(imageURL, out callbacks);
            if (callbacks == null)
            {
                Debug.LogWarning("[mod.io] ImageRequestManager completed a download but the callbacks"
                                 + " entry for the download was null."
                                 + "\nImageURL = " + imageURL
                                 + "\nWebRequest.URL = " + webRequest.url
                                 + "\nm_callbackMap.TryGetValue() = " + isURLMapped.ToString()
                                 );
                return;
            }

            if (webRequest.isHttpError || webRequest.isNetworkError)
            {
                if (callbacks.failed.Count > 0)
                {
                    WebRequestError error = WebRequestError.GenerateFromWebRequest(webRequest);

                    foreach (var errorCallback in callbacks.failed)
                    {
                        errorCallback(error);
                    }
                }
            }
            else
            {
                Texture2D texture = ((DownloadHandlerTexture)webRequest.downloadHandler).texture;

                if (this.isActiveAndEnabled || !this.clearCacheOnDisable)
                {
                    this.cache[imageURL] = texture;
                }

                foreach (var successCallback in callbacks.succeeded)
                {
                    successCallback(texture);
                }
            }

            // remove from "in progress"
            this.m_callbackMap.Remove(imageURL);
        }